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"
40 #include "pandecode/decode.h"
43 panfrost_drm_allocate_slab(struct panfrost_screen
*screen
,
44 struct panfrost_memory
*mem
,
51 struct drm_panfrost_create_bo create_bo
= {
53 .flags
= 0, // TODO figure out proper flags..
55 struct drm_panfrost_mmap_bo mmap_bo
= {0,};
58 // TODO cache allocations
59 // TODO properly handle errors
60 // TODO take into account extra_flags
62 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_CREATE_BO
, &create_bo
);
64 fprintf(stderr
, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret
);
68 mem
->gpu
= create_bo
.offset
;
69 mem
->gem_handle
= create_bo
.handle
;
70 mem
->stack_bottom
= 0;
71 mem
->size
= create_bo
.size
;
73 // TODO map and unmap on demand?
74 mmap_bo
.handle
= create_bo
.handle
;
75 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MMAP_BO
, &mmap_bo
);
77 fprintf(stderr
, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret
);
81 mem
->cpu
= os_mmap(NULL
, mem
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
82 screen
->fd
, mmap_bo
.offset
);
83 if (mem
->cpu
== MAP_FAILED
) {
84 fprintf(stderr
, "mmap failed: %p\n", mem
->cpu
);
88 /* Record the mmap if we're tracing */
89 if (pan_debug
& PAN_DBG_TRACE
)
90 pandecode_inject_mmap(mem
->gpu
, mem
->cpu
, mem
->size
, NULL
);
94 panfrost_drm_free_slab(struct panfrost_screen
*screen
, struct panfrost_memory
*mem
)
96 struct drm_gem_close gem_close
= {
97 .handle
= mem
->gem_handle
,
101 if (os_munmap((void *) (uintptr_t) mem
->cpu
, mem
->size
)) {
108 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
110 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret
);
114 mem
->gem_handle
= -1;
118 panfrost_drm_import_bo(struct panfrost_screen
*screen
, struct winsys_handle
*whandle
)
120 struct panfrost_bo
*bo
= rzalloc(screen
, struct panfrost_bo
);
121 struct drm_panfrost_get_bo_offset get_bo_offset
= {0,};
122 struct drm_panfrost_mmap_bo mmap_bo
= {0,};
126 ret
= drmPrimeFDToHandle(screen
->fd
, whandle
->handle
, &gem_handle
);
129 get_bo_offset
.handle
= gem_handle
;
130 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_BO_OFFSET
, &get_bo_offset
);
133 bo
->gem_handle
= gem_handle
;
134 bo
->gpu
= (mali_ptr
) get_bo_offset
.offset
;
135 pipe_reference_init(&bo
->reference
, 1);
137 // TODO map and unmap on demand?
138 mmap_bo
.handle
= gem_handle
;
139 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MMAP_BO
, &mmap_bo
);
141 fprintf(stderr
, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret
);
145 bo
->size
= lseek(whandle
->handle
, 0, SEEK_END
);
146 assert(bo
->size
> 0);
147 bo
->cpu
= os_mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
148 screen
->fd
, mmap_bo
.offset
);
149 if (bo
->cpu
== MAP_FAILED
) {
150 fprintf(stderr
, "mmap failed: %p\n", bo
->cpu
);
154 /* Record the mmap if we're tracing */
155 if (pan_debug
& PAN_DBG_TRACE
)
156 pandecode_inject_mmap(bo
->gpu
, bo
->cpu
, bo
->size
, NULL
);
162 panfrost_drm_export_bo(struct panfrost_screen
*screen
, int gem_handle
, unsigned int stride
, struct winsys_handle
*whandle
)
164 struct drm_prime_handle args
= {
165 .handle
= gem_handle
,
166 .flags
= DRM_CLOEXEC
,
169 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
173 whandle
->handle
= args
.fd
;
174 whandle
->stride
= stride
;
180 panfrost_drm_free_imported_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*bo
)
182 struct drm_gem_close gem_close
= {
183 .handle
= bo
->gem_handle
,
187 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
189 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret
);
194 bo
->gpu
= (mali_ptr
)NULL
;
198 panfrost_drm_submit_job(struct panfrost_context
*ctx
, u64 job_desc
, int reqs
, struct pipe_surface
*surf
)
200 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
201 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
202 struct drm_panfrost_submit submit
= {0,};
205 submit
.in_syncs
= (u64
) (uintptr_t) &ctx
->out_sync
;
206 submit
.in_sync_count
= 1;
208 submit
.out_sync
= ctx
->out_sync
;
210 submit
.jc
= job_desc
;
211 submit
.requirements
= reqs
;
214 struct panfrost_resource
*res
= pan_resource(surf
->texture
);
215 assert(res
->bo
->gem_handle
> 0);
216 bo_handles
[submit
.bo_handle_count
++] = res
->bo
->gem_handle
;
218 if (res
->bo
->checksum_slab
.gem_handle
)
219 bo_handles
[submit
.bo_handle_count
++] = res
->bo
->checksum_slab
.gem_handle
;
222 /* TODO: Add here the transient pools */
223 /* TODO: Add here the BOs listed in the panfrost_job */
224 bo_handles
[submit
.bo_handle_count
++] = ctx
->shaders
.gem_handle
;
225 bo_handles
[submit
.bo_handle_count
++] = ctx
->scratchpad
.gem_handle
;
226 bo_handles
[submit
.bo_handle_count
++] = ctx
->tiler_heap
.gem_handle
;
227 bo_handles
[submit
.bo_handle_count
++] = ctx
->varying_mem
.gem_handle
;
228 bo_handles
[submit
.bo_handle_count
++] = ctx
->tiler_polygon_list
.gem_handle
;
229 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
231 if (drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
)) {
232 fprintf(stderr
, "Error submitting: %m\n");
236 /* Trace the job if we're doing that */
238 if (pan_debug
& PAN_DBG_TRACE
)
239 pandecode_replay_jc(submit
.jc
, FALSE
);
245 panfrost_drm_submit_vs_fs_job(struct panfrost_context
*ctx
, bool has_draws
, bool is_scanout
)
247 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
250 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
252 if (job
->first_job
.gpu
) {
253 ret
= panfrost_drm_submit_job(ctx
, job
->first_job
.gpu
, 0, NULL
);
257 if (job
->first_tiler
.gpu
|| job
->clear
) {
258 ret
= panfrost_drm_submit_job(ctx
, panfrost_fragment_job(ctx
, has_draws
), PANFROST_JD_REQ_FS
, surf
);
265 struct panfrost_fence
*
266 panfrost_fence_create(struct panfrost_context
*ctx
)
268 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
269 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
270 struct panfrost_fence
*f
= calloc(1, sizeof(*f
));
274 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
275 * another syncobj instead of a sync file, but this is all we get.
276 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
279 drmSyncobjExportSyncFile(screen
->fd
, ctx
->out_sync
, &f
->fd
);
281 fprintf(stderr
, "export failed\n");
286 pipe_reference_init(&f
->reference
, 1);
292 panfrost_drm_force_flush_fragment(struct panfrost_context
*ctx
,
293 struct pipe_fence_handle
**fence
)
295 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
296 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
298 if (!screen
->last_fragment_flushed
) {
299 drmSyncobjWait(screen
->fd
, &ctx
->out_sync
, 1, INT64_MAX
, 0, NULL
);
300 screen
->last_fragment_flushed
= true;
302 /* The job finished up, so we're safe to clean it up now */
303 panfrost_free_job(ctx
, screen
->last_job
);
307 struct panfrost_fence
*f
= panfrost_fence_create(ctx
);
308 gallium
->screen
->fence_reference(gallium
->screen
, fence
, NULL
);
309 *fence
= (struct pipe_fence_handle
*)f
;
314 panfrost_drm_query_gpu_version(struct panfrost_screen
*screen
)
316 struct drm_panfrost_get_param get_param
= {0,};
319 get_param
.param
= DRM_PANFROST_PARAM_GPU_PROD_ID
;
320 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_PARAM
, &get_param
);
323 return get_param
.value
;
327 panfrost_drm_init_context(struct panfrost_context
*ctx
)
329 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
330 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
332 return drmSyncobjCreate(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
,
337 panfrost_drm_fence_reference(struct pipe_screen
*screen
,
338 struct pipe_fence_handle
**ptr
,
339 struct pipe_fence_handle
*fence
)
341 struct panfrost_fence
**p
= (struct panfrost_fence
**)ptr
;
342 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
343 struct panfrost_fence
*old
= *p
;
345 if (pipe_reference(&(*p
)->reference
, &f
->reference
)) {
353 panfrost_drm_fence_finish(struct pipe_screen
*pscreen
,
354 struct pipe_context
*ctx
,
355 struct pipe_fence_handle
*fence
,
358 struct panfrost_screen
*screen
= pan_screen(pscreen
);
359 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
363 ret
= drmSyncobjCreate(screen
->fd
, 0, &syncobj
);
365 fprintf(stderr
, "Failed to create syncobj to wait on: %m\n");
369 drmSyncobjImportSyncFile(screen
->fd
, syncobj
, f
->fd
);
371 fprintf(stderr
, "Failed to import fence to syncobj: %m\n");
375 uint64_t abs_timeout
= os_time_get_absolute_timeout(timeout
);
376 if (abs_timeout
== OS_TIMEOUT_INFINITE
)
377 abs_timeout
= INT64_MAX
;
379 ret
= drmSyncobjWait(screen
->fd
, &syncobj
, 1, abs_timeout
, 0, NULL
);
381 drmSyncobjDestroy(screen
->fd
, syncobj
);