2 * © Copyright 2019 Collabora, Ltd.
3 * Copyright 2019 Alyssa Rosenzweig
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:
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
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
29 #include "drm-uapi/panfrost_drm.h"
31 #include "util/u_memory.h"
32 #include "util/os_time.h"
33 #include "os/os_mman.h"
35 #include "pan_screen.h"
36 #include "pan_resource.h"
37 #include "pan_context.h"
39 #include "pandecode/decode.h"
42 panfrost_drm_mmap_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*bo
)
44 struct drm_panfrost_mmap_bo mmap_bo
= { .handle
= bo
->gem_handle
};
50 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MMAP_BO
, &mmap_bo
);
52 fprintf(stderr
, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret
);
56 bo
->cpu
= os_mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
57 screen
->fd
, mmap_bo
.offset
);
58 if (bo
->cpu
== MAP_FAILED
) {
59 fprintf(stderr
, "mmap failed: %p\n", bo
->cpu
);
63 /* Record the mmap if we're tracing */
64 if (pan_debug
& PAN_DBG_TRACE
)
65 pandecode_inject_mmap(bo
->gpu
, bo
->cpu
, bo
->size
, NULL
);
69 panfrost_drm_munmap_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*bo
)
74 if (os_munmap((void *) (uintptr_t)bo
->cpu
, bo
->size
)) {
83 panfrost_drm_create_bo(struct panfrost_screen
*screen
, size_t size
,
86 struct panfrost_bo
*bo
= rzalloc(screen
, struct panfrost_bo
);
87 struct drm_panfrost_create_bo create_bo
= {
93 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_CREATE_BO
, &create_bo
);
95 fprintf(stderr
, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret
);
99 bo
->size
= create_bo
.size
;
100 bo
->gpu
= create_bo
.offset
;
101 bo
->gem_handle
= create_bo
.handle
;
103 // TODO map and unmap on demand?
104 panfrost_drm_mmap_bo(screen
, bo
);
106 pipe_reference_init(&bo
->reference
, 1);
111 panfrost_drm_release_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*bo
)
113 struct drm_gem_close gem_close
= { .handle
= bo
->gem_handle
};
119 panfrost_drm_munmap_bo(screen
, bo
);
121 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
123 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret
);
131 panfrost_drm_allocate_slab(struct panfrost_screen
*screen
,
132 struct panfrost_memory
*mem
,
139 struct drm_panfrost_create_bo create_bo
= {
140 .size
= pages
* 4096,
141 .flags
= 0, // TODO figure out proper flags..
143 struct drm_panfrost_mmap_bo mmap_bo
= {0,};
146 // TODO cache allocations
147 // TODO properly handle errors
148 // TODO take into account extra_flags
150 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_CREATE_BO
, &create_bo
);
152 fprintf(stderr
, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret
);
156 mem
->gpu
= create_bo
.offset
;
157 mem
->gem_handle
= create_bo
.handle
;
158 mem
->stack_bottom
= 0;
159 mem
->size
= create_bo
.size
;
161 // TODO map and unmap on demand?
162 mmap_bo
.handle
= create_bo
.handle
;
163 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MMAP_BO
, &mmap_bo
);
165 fprintf(stderr
, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret
);
169 mem
->cpu
= os_mmap(NULL
, mem
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
170 screen
->fd
, mmap_bo
.offset
);
171 if (mem
->cpu
== MAP_FAILED
) {
172 fprintf(stderr
, "mmap failed: %p\n", mem
->cpu
);
176 /* Record the mmap if we're tracing */
177 if (pan_debug
& PAN_DBG_TRACE
)
178 pandecode_inject_mmap(mem
->gpu
, mem
->cpu
, mem
->size
, NULL
);
182 panfrost_drm_free_slab(struct panfrost_screen
*screen
, struct panfrost_memory
*mem
)
184 struct drm_gem_close gem_close
= {
185 .handle
= mem
->gem_handle
,
189 if (os_munmap((void *) (uintptr_t) mem
->cpu
, mem
->size
)) {
196 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
198 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret
);
202 mem
->gem_handle
= -1;
206 panfrost_drm_import_bo(struct panfrost_screen
*screen
, int fd
)
208 struct panfrost_bo
*bo
= rzalloc(screen
, struct panfrost_bo
);
209 struct drm_panfrost_get_bo_offset get_bo_offset
= {0,};
213 ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &gem_handle
);
216 get_bo_offset
.handle
= gem_handle
;
217 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_BO_OFFSET
, &get_bo_offset
);
220 bo
->gem_handle
= gem_handle
;
221 bo
->gpu
= (mali_ptr
) get_bo_offset
.offset
;
222 bo
->size
= lseek(fd
, 0, SEEK_END
);
223 assert(bo
->size
> 0);
224 pipe_reference_init(&bo
->reference
, 1);
226 // TODO map and unmap on demand?
227 panfrost_drm_mmap_bo(screen
, bo
);
232 panfrost_drm_export_bo(struct panfrost_screen
*screen
, const struct panfrost_bo
*bo
)
234 struct drm_prime_handle args
= {
235 .handle
= bo
->gem_handle
,
236 .flags
= DRM_CLOEXEC
,
239 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
247 panfrost_drm_submit_job(struct panfrost_context
*ctx
, u64 job_desc
, int reqs
, struct pipe_surface
*surf
)
249 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
250 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
251 struct drm_panfrost_submit submit
= {0,};
254 submit
.in_syncs
= (u64
) (uintptr_t) &ctx
->out_sync
;
255 submit
.in_sync_count
= 1;
257 submit
.out_sync
= ctx
->out_sync
;
259 submit
.jc
= job_desc
;
260 submit
.requirements
= reqs
;
263 struct panfrost_resource
*res
= pan_resource(surf
->texture
);
264 assert(res
->bo
->gem_handle
> 0);
265 bo_handles
[submit
.bo_handle_count
++] = res
->bo
->gem_handle
;
268 /* TODO: Add here the transient pools */
269 /* TODO: Add here the BOs listed in the panfrost_job */
270 bo_handles
[submit
.bo_handle_count
++] = ctx
->shaders
.gem_handle
;
271 bo_handles
[submit
.bo_handle_count
++] = ctx
->scratchpad
.gem_handle
;
272 bo_handles
[submit
.bo_handle_count
++] = ctx
->tiler_heap
.gem_handle
;
273 bo_handles
[submit
.bo_handle_count
++] = ctx
->varying_mem
.gem_handle
;
274 bo_handles
[submit
.bo_handle_count
++] = ctx
->tiler_polygon_list
.gem_handle
;
275 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
277 if (drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
)) {
278 fprintf(stderr
, "Error submitting: %m\n");
282 /* Trace the job if we're doing that */
283 if (pan_debug
& PAN_DBG_TRACE
) {
284 /* Wait so we can get errors reported back */
285 drmSyncobjWait(screen
->fd
, &ctx
->out_sync
, 1, INT64_MAX
, 0, NULL
);
286 pandecode_replay_jc(submit
.jc
, FALSE
);
293 panfrost_drm_submit_vs_fs_job(struct panfrost_context
*ctx
, bool has_draws
, bool is_scanout
)
295 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
298 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
300 if (job
->first_job
.gpu
) {
301 ret
= panfrost_drm_submit_job(ctx
, job
->first_job
.gpu
, 0, NULL
);
305 if (job
->first_tiler
.gpu
|| job
->clear
) {
306 ret
= panfrost_drm_submit_job(ctx
, panfrost_fragment_job(ctx
, has_draws
), PANFROST_JD_REQ_FS
, surf
);
313 static struct panfrost_fence
*
314 panfrost_fence_create(struct panfrost_context
*ctx
)
316 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
317 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
318 struct panfrost_fence
*f
= calloc(1, sizeof(*f
));
322 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
323 * another syncobj instead of a sync file, but this is all we get.
324 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
327 drmSyncobjExportSyncFile(screen
->fd
, ctx
->out_sync
, &f
->fd
);
329 fprintf(stderr
, "export failed\n");
334 pipe_reference_init(&f
->reference
, 1);
340 panfrost_drm_force_flush_fragment(struct panfrost_context
*ctx
,
341 struct pipe_fence_handle
**fence
)
343 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
344 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
346 if (!screen
->last_fragment_flushed
) {
347 drmSyncobjWait(screen
->fd
, &ctx
->out_sync
, 1, INT64_MAX
, 0, NULL
);
348 screen
->last_fragment_flushed
= true;
350 /* The job finished up, so we're safe to clean it up now */
351 panfrost_free_job(ctx
, screen
->last_job
);
355 struct panfrost_fence
*f
= panfrost_fence_create(ctx
);
356 gallium
->screen
->fence_reference(gallium
->screen
, fence
, NULL
);
357 *fence
= (struct pipe_fence_handle
*)f
;
362 panfrost_drm_query_gpu_version(struct panfrost_screen
*screen
)
364 struct drm_panfrost_get_param get_param
= {0,};
367 get_param
.param
= DRM_PANFROST_PARAM_GPU_PROD_ID
;
368 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_PARAM
, &get_param
);
371 return get_param
.value
;
375 panfrost_drm_init_context(struct panfrost_context
*ctx
)
377 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
378 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
380 return drmSyncobjCreate(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
,
385 panfrost_drm_fence_reference(struct pipe_screen
*screen
,
386 struct pipe_fence_handle
**ptr
,
387 struct pipe_fence_handle
*fence
)
389 struct panfrost_fence
**p
= (struct panfrost_fence
**)ptr
;
390 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
391 struct panfrost_fence
*old
= *p
;
393 if (pipe_reference(&(*p
)->reference
, &f
->reference
)) {
401 panfrost_drm_fence_finish(struct pipe_screen
*pscreen
,
402 struct pipe_context
*ctx
,
403 struct pipe_fence_handle
*fence
,
406 struct panfrost_screen
*screen
= pan_screen(pscreen
);
407 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
411 ret
= drmSyncobjCreate(screen
->fd
, 0, &syncobj
);
413 fprintf(stderr
, "Failed to create syncobj to wait on: %m\n");
417 drmSyncobjImportSyncFile(screen
->fd
, syncobj
, f
->fd
);
419 fprintf(stderr
, "Failed to import fence to syncobj: %m\n");
423 uint64_t abs_timeout
= os_time_get_absolute_timeout(timeout
);
424 if (abs_timeout
== OS_TIMEOUT_INFINITE
)
425 abs_timeout
= INT64_MAX
;
427 ret
= drmSyncobjWait(screen
->fd
, &syncobj
, 1, abs_timeout
, 0, NULL
);
429 drmSyncobjDestroy(screen
->fd
, syncobj
);