1/*
2 * Copyright © 2014 Broadcom
3 * Copyright © 208 Alyssa Rosenzweig
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#include <errno.h>
26#include <unistd.h>
27#include <fcntl.h>
28
29#include "util/format/u_format.h"
30#include "util/os_file.h"
31#include "util/u_math.h"
32#include "util/u_memory.h"
33
34#include "drm-uapi/drm.h"
35#include "renderonly/renderonly.h"
36#include "panfrost_drm_public.h"
37#include "panfrost/pan_public.h"
38#include "xf86drm.h"
39
40static struct renderonly_scanout *
41panfrost_create_kms_dumb_buffer_for_resource(struct pipe_resource *rsc,
42                                             struct renderonly *ro,
43                                             struct winsys_handle *out_handle)
44{
45   /* Find the smallest width alignment that gives us a 64byte aligned stride */
46   unsigned blk_sz = util_format_get_blocksize(rsc->format);
47
48   assert(blk_sz);
49
50   unsigned align_w = 1;
51   for (unsigned i = 1; i <= blk_sz; i++) {
52      if (!((64 * i) % blk_sz)) {
53         align_w = (64 * i) / blk_sz;
54	 break;
55      }
56   }
57
58   struct drm_mode_create_dumb create_dumb = {
59      .width = ALIGN_NPOT(rsc->width0, align_w),
60      .height = rsc->height0,
61      .bpp = util_format_get_blocksizebits(rsc->format),
62   };
63   struct drm_mode_destroy_dumb destroy_dumb = {0};
64
65   /* Align width to end up with a buffer that's aligned on 64 bytes. */
66
67   struct renderonly_scanout *scanout = CALLOC_STRUCT(renderonly_scanout);
68   if (!scanout)
69      return NULL;
70
71   /* create dumb buffer at scanout GPU */
72   int err = drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
73   if (err < 0) {
74      fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n",
75            strerror(errno));
76      goto free_scanout;
77   }
78
79   if (create_dumb.pitch % 64)
80      goto free_dumb;
81
82   scanout->handle = create_dumb.handle;
83   scanout->stride = create_dumb.pitch;
84
85   if (!out_handle)
86      return scanout;
87
88   /* fill in winsys handle */
89   memset(out_handle, 0, sizeof(*out_handle));
90   out_handle->type = WINSYS_HANDLE_TYPE_FD;
91   out_handle->stride = create_dumb.pitch;
92
93   err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC,
94                            (int *)&out_handle->handle);
95   if (err < 0) {
96      fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno));
97      goto free_dumb;
98   }
99
100   return scanout;
101
102free_dumb:
103   destroy_dumb.handle = scanout->handle;
104   drmIoctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
105
106free_scanout:
107   FREE(scanout);
108
109   return NULL;
110
111}
112
113struct pipe_screen *
114panfrost_drm_screen_create(int fd)
115{
116   return panfrost_create_screen(os_dupfd_cloexec(fd), NULL);
117}
118
119struct pipe_screen *
120panfrost_drm_screen_create_renderonly(struct renderonly *ro)
121{
122   ro->create_for_resource = panfrost_create_kms_dumb_buffer_for_resource;
123   return panfrost_create_screen(os_dupfd_cloexec(ro->gpu_fd), ro);
124}
125