aed50477ff7d4caea318894395ffba5c2d754074
[mesa.git] / src / gallium / drivers / panfrost / pan_drm.c
1 /*
2 * © Copyright 2019 Collabora, Ltd.
3 * Copyright 2019 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 FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include <fcntl.h>
27 #include <xf86drm.h>
28
29 #include "drm-uapi/panfrost_drm.h"
30
31 #include "util/u_memory.h"
32 #include "util/os_time.h"
33 #include "os/os_mman.h"
34
35 #include "pan_screen.h"
36 #include "pan_resource.h"
37 #include "pan_context.h"
38 #include "pan_drm.h"
39 #include "pan_util.h"
40 #include "pandecode/decode.h"
41
42 struct panfrost_drm {
43 struct panfrost_driver base;
44 int fd;
45 };
46
47 static void
48 panfrost_drm_allocate_slab(struct panfrost_screen *screen,
49 struct panfrost_memory *mem,
50 size_t pages,
51 bool same_va,
52 int extra_flags,
53 int commit_count,
54 int extent)
55 {
56 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
57 struct drm_panfrost_create_bo create_bo = {
58 .size = pages * 4096,
59 .flags = 0, // TODO figure out proper flags..
60 };
61 struct drm_panfrost_mmap_bo mmap_bo = {0,};
62 int ret;
63
64 // TODO cache allocations
65 // TODO properly handle errors
66 // TODO take into account extra_flags
67
68 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
69 if (ret) {
70 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
71 assert(0);
72 }
73
74 mem->gpu = create_bo.offset;
75 mem->gem_handle = create_bo.handle;
76 mem->stack_bottom = 0;
77 mem->size = create_bo.size;
78
79 // TODO map and unmap on demand?
80 mmap_bo.handle = create_bo.handle;
81 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
82 if (ret) {
83 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
84 assert(0);
85 }
86
87 mem->cpu = os_mmap(NULL, mem->size, PROT_READ | PROT_WRITE, MAP_SHARED,
88 drm->fd, mmap_bo.offset);
89 if (mem->cpu == MAP_FAILED) {
90 fprintf(stderr, "mmap failed: %p\n", mem->cpu);
91 assert(0);
92 }
93
94 /* Record the mmap if we're tracing */
95 if (pan_debug & PAN_DBG_TRACE)
96 pandecode_inject_mmap(mem->gpu, mem->cpu, mem->size, NULL);
97 }
98
99 static void
100 panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
101 {
102 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
103 struct drm_gem_close gem_close = {
104 .handle = mem->gem_handle,
105 };
106 int ret;
107
108 if (os_munmap((void *) (uintptr_t) mem->cpu, mem->size)) {
109 perror("munmap");
110 abort();
111 }
112
113 mem->cpu = NULL;
114
115 ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
116 if (ret) {
117 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
118 assert(0);
119 }
120
121 mem->gem_handle = -1;
122 }
123
124 static struct panfrost_bo *
125 panfrost_drm_import_bo(struct panfrost_screen *screen, struct winsys_handle *whandle)
126 {
127 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
128 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
129 struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
130 struct drm_panfrost_mmap_bo mmap_bo = {0,};
131 int ret;
132 unsigned gem_handle;
133
134 ret = drmPrimeFDToHandle(drm->fd, whandle->handle, &gem_handle);
135 assert(!ret);
136
137 get_bo_offset.handle = gem_handle;
138 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
139 assert(!ret);
140
141 bo->gem_handle = gem_handle;
142 bo->gpu = (mali_ptr) get_bo_offset.offset;
143 pipe_reference_init(&bo->reference, 1);
144
145 // TODO map and unmap on demand?
146 mmap_bo.handle = gem_handle;
147 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
148 if (ret) {
149 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
150 assert(0);
151 }
152
153 bo->size = lseek(whandle->handle, 0, SEEK_END);
154 assert(bo->size > 0);
155 bo->cpu = os_mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
156 drm->fd, mmap_bo.offset);
157 if (bo->cpu == MAP_FAILED) {
158 fprintf(stderr, "mmap failed: %p\n", bo->cpu);
159 assert(0);
160 }
161
162 /* Record the mmap if we're tracing */
163 if (pan_debug & PAN_DBG_TRACE)
164 pandecode_inject_mmap(bo->gpu, bo->cpu, bo->size, NULL);
165
166 return bo;
167 }
168
169 static int
170 panfrost_drm_export_bo(struct panfrost_screen *screen, int gem_handle, unsigned int stride, struct winsys_handle *whandle)
171 {
172 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
173 struct drm_prime_handle args = {
174 .handle = gem_handle,
175 .flags = DRM_CLOEXEC,
176 };
177
178 int ret = drmIoctl(drm->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
179 if (ret == -1)
180 return FALSE;
181
182 whandle->handle = args.fd;
183 whandle->stride = stride;
184
185 return TRUE;
186 }
187
188 static void
189 panfrost_drm_free_imported_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
190 {
191 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
192 struct drm_gem_close gem_close = {
193 .handle = bo->gem_handle,
194 };
195 int ret;
196
197 ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
198 if (ret) {
199 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
200 assert(0);
201 }
202
203 bo->gem_handle = -1;
204 bo->gpu = (mali_ptr)NULL;
205 }
206
207 static int
208 panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs, struct pipe_surface *surf)
209 {
210 struct pipe_context *gallium = (struct pipe_context *) ctx;
211 struct panfrost_screen *screen = pan_screen(gallium->screen);
212 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
213 struct drm_panfrost_submit submit = {0,};
214 int bo_handles[7];
215
216 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
217 submit.in_sync_count = 1;
218
219 submit.out_sync = ctx->out_sync;
220
221 submit.jc = job_desc;
222 submit.requirements = reqs;
223
224 if (surf) {
225 struct panfrost_resource *res = pan_resource(surf->texture);
226 assert(res->bo->gem_handle > 0);
227 bo_handles[submit.bo_handle_count++] = res->bo->gem_handle;
228
229 if (res->bo->checksum_slab.gem_handle)
230 bo_handles[submit.bo_handle_count++] = res->bo->checksum_slab.gem_handle;
231 }
232
233 /* TODO: Add here the transient pools */
234 /* TODO: Add here the BOs listed in the panfrost_job */
235 bo_handles[submit.bo_handle_count++] = ctx->shaders.gem_handle;
236 bo_handles[submit.bo_handle_count++] = ctx->scratchpad.gem_handle;
237 bo_handles[submit.bo_handle_count++] = ctx->tiler_heap.gem_handle;
238 bo_handles[submit.bo_handle_count++] = ctx->varying_mem.gem_handle;
239 bo_handles[submit.bo_handle_count++] = ctx->tiler_polygon_list.gem_handle;
240 submit.bo_handles = (u64) (uintptr_t) bo_handles;
241
242 if (drmIoctl(drm->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit)) {
243 fprintf(stderr, "Error submitting: %m\n");
244 return errno;
245 }
246
247 /* Trace the job if we're doing that */
248
249 if (pan_debug & PAN_DBG_TRACE)
250 pandecode_replay_jc(submit.jc, FALSE);
251
252 return 0;
253 }
254
255 static int
256 panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
257 {
258 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
259 int ret;
260
261 if (has_draws) {
262 ret = panfrost_drm_submit_job(ctx, ctx->set_value_job, 0, NULL);
263 assert(!ret);
264 }
265
266 ret = panfrost_drm_submit_job(ctx, panfrost_fragment_job(ctx, has_draws), PANFROST_JD_REQ_FS, surf);
267
268 return ret;
269 }
270
271 static struct panfrost_fence *
272 panfrost_fence_create(struct panfrost_context *ctx)
273 {
274 struct pipe_context *gallium = (struct pipe_context *) ctx;
275 struct panfrost_screen *screen = pan_screen(gallium->screen);
276 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
277 struct panfrost_fence *f = calloc(1, sizeof(*f));
278 if (!f)
279 return NULL;
280
281 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
282 * another syncobj instead of a sync file, but this is all we get.
283 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
284 * same syncobj).
285 */
286 drmSyncobjExportSyncFile(drm->fd, ctx->out_sync, &f->fd);
287 if (f->fd == -1) {
288 fprintf(stderr, "export failed\n");
289 free(f);
290 return NULL;
291 }
292
293 pipe_reference_init(&f->reference, 1);
294
295 return f;
296 }
297
298 static void
299 panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
300 struct pipe_fence_handle **fence)
301 {
302 struct pipe_context *gallium = (struct pipe_context *) ctx;
303 struct panfrost_screen *screen = pan_screen(gallium->screen);
304 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
305
306 if (!screen->last_fragment_flushed) {
307 drmSyncobjWait(drm->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
308 screen->last_fragment_flushed = true;
309
310 /* The job finished up, so we're safe to clean it up now */
311 panfrost_free_job(ctx, screen->last_job);
312 }
313
314 if (fence) {
315 struct panfrost_fence *f = panfrost_fence_create(ctx);
316 gallium->screen->fence_reference(gallium->screen, fence, NULL);
317 *fence = (struct pipe_fence_handle *)f;
318 }
319 }
320
321 static void
322 panfrost_drm_enable_counters(struct panfrost_screen *screen)
323 {
324 fprintf(stderr, "unimplemented: %s\n", __func__);
325 }
326
327 static void
328 panfrost_drm_dump_counters(struct panfrost_screen *screen)
329 {
330 fprintf(stderr, "unimplemented: %s\n", __func__);
331 }
332
333 static unsigned
334 panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
335 {
336 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
337 struct drm_panfrost_get_param get_param = {0,};
338 int ret;
339
340 get_param.param = DRM_PANFROST_PARAM_GPU_PROD_ID;
341 ret = drmIoctl(drm->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
342 assert(!ret);
343
344 return get_param.value;
345 }
346
347 static int
348 panfrost_drm_init_context(struct panfrost_context *ctx)
349 {
350 struct pipe_context *gallium = (struct pipe_context *) ctx;
351 struct panfrost_screen *screen = pan_screen(gallium->screen);
352 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
353
354 return drmSyncobjCreate(drm->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
355 &ctx->out_sync);
356 }
357
358 static void
359 panfrost_drm_fence_reference(struct pipe_screen *screen,
360 struct pipe_fence_handle **ptr,
361 struct pipe_fence_handle *fence)
362 {
363 struct panfrost_fence **p = (struct panfrost_fence **)ptr;
364 struct panfrost_fence *f = (struct panfrost_fence *)fence;
365 struct panfrost_fence *old = *p;
366
367 if (pipe_reference(&(*p)->reference, &f->reference)) {
368 close(old->fd);
369 free(old);
370 }
371 *p = f;
372 }
373
374 static boolean
375 panfrost_drm_fence_finish(struct pipe_screen *pscreen,
376 struct pipe_context *ctx,
377 struct pipe_fence_handle *fence,
378 uint64_t timeout)
379 {
380 struct panfrost_screen *screen = pan_screen(pscreen);
381 struct panfrost_drm *drm = (struct panfrost_drm *)screen->driver;
382 struct panfrost_fence *f = (struct panfrost_fence *)fence;
383 int ret;
384
385 unsigned syncobj;
386 ret = drmSyncobjCreate(drm->fd, 0, &syncobj);
387 if (ret) {
388 fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
389 return false;
390 }
391
392 drmSyncobjImportSyncFile(drm->fd, syncobj, f->fd);
393 if (ret) {
394 fprintf(stderr, "Failed to import fence to syncobj: %m\n");
395 return false;
396 }
397
398 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
399 if (abs_timeout == OS_TIMEOUT_INFINITE)
400 abs_timeout = INT64_MAX;
401
402 ret = drmSyncobjWait(drm->fd, &syncobj, 1, abs_timeout, 0, NULL);
403
404 drmSyncobjDestroy(drm->fd, syncobj);
405
406 return ret >= 0;
407 }
408
409 struct panfrost_driver *
410 panfrost_create_drm_driver(int fd)
411 {
412 struct panfrost_drm *driver = CALLOC_STRUCT(panfrost_drm);
413
414 driver->fd = fd;
415
416 driver->base.import_bo = panfrost_drm_import_bo;
417 driver->base.export_bo = panfrost_drm_export_bo;
418 driver->base.free_imported_bo = panfrost_drm_free_imported_bo;
419 driver->base.submit_vs_fs_job = panfrost_drm_submit_vs_fs_job;
420 driver->base.force_flush_fragment = panfrost_drm_force_flush_fragment;
421 driver->base.allocate_slab = panfrost_drm_allocate_slab;
422 driver->base.free_slab = panfrost_drm_free_slab;
423 driver->base.enable_counters = panfrost_drm_enable_counters;
424 driver->base.query_gpu_version = panfrost_drm_query_gpu_version;
425 driver->base.init_context = panfrost_drm_init_context;
426 driver->base.fence_reference = panfrost_drm_fence_reference;
427 driver->base.fence_finish = panfrost_drm_fence_finish;
428 driver->base.dump_counters = panfrost_drm_dump_counters;
429
430 return &driver->base;
431 }