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
;
88 /* Kernel will fail (confusingly) with EPERM otherwise */
91 unsigned translated_flags
= 0;
93 /* TODO: translate flags to kernel flags, if the kernel supports */
95 struct drm_panfrost_create_bo create_bo
= {
97 .flags
= translated_flags
,
100 /* Before creating a BO, we first want to check the cache */
102 bo
= panfrost_bo_cache_fetch(screen
, size
, flags
);
105 /* Otherwise, the cache misses and we need to allocate a BO fresh from
110 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_CREATE_BO
, &create_bo
);
112 fprintf(stderr
, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret
);
116 /* We have a BO allocated from the kernel; fill in the userspace
119 bo
= rzalloc(screen
, struct panfrost_bo
);
120 bo
->size
= create_bo
.size
;
121 bo
->gpu
= create_bo
.offset
;
122 bo
->gem_handle
= create_bo
.handle
;
125 /* Only mmap now if we know we need to. For CPU-invisible buffers, we
126 * never map since we don't care about their contents; they're purely
127 * for GPU-internal use. */
129 if (!(flags
& (PAN_ALLOCATE_INVISIBLE
| PAN_ALLOCATE_DELAY_MMAP
)))
130 panfrost_drm_mmap_bo(screen
, bo
);
132 pipe_reference_init(&bo
->reference
, 1);
137 panfrost_drm_release_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*bo
, bool cacheable
)
139 struct drm_gem_close gem_close
= { .handle
= bo
->gem_handle
};
145 /* Rather than freeing the BO now, we'll cache the BO for later
146 * allocations if we're allowed to */
149 bool cached
= panfrost_bo_cache_put(screen
, bo
);
155 /* Otherwise, if the BO wasn't cached, we'll legitimately free the BO */
157 panfrost_drm_munmap_bo(screen
, bo
);
159 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
161 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret
);
169 panfrost_drm_allocate_slab(struct panfrost_screen
*screen
,
170 struct panfrost_memory
*mem
,
177 // TODO cache allocations
178 // TODO properly handle errors
179 // TODO take into account extra_flags
180 mem
->bo
= panfrost_drm_create_bo(screen
, pages
* 4096, extra_flags
);
181 mem
->stack_bottom
= 0;
185 panfrost_drm_free_slab(struct panfrost_screen
*screen
, struct panfrost_memory
*mem
)
187 panfrost_bo_unreference(&screen
->base
, mem
->bo
);
192 panfrost_drm_import_bo(struct panfrost_screen
*screen
, int fd
)
194 struct panfrost_bo
*bo
= rzalloc(screen
, struct panfrost_bo
);
195 struct drm_panfrost_get_bo_offset get_bo_offset
= {0,};
196 MAYBE_UNUSED
int ret
;
199 ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &gem_handle
);
202 get_bo_offset
.handle
= gem_handle
;
203 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_BO_OFFSET
, &get_bo_offset
);
206 bo
->gem_handle
= gem_handle
;
207 bo
->gpu
= (mali_ptr
) get_bo_offset
.offset
;
208 bo
->size
= lseek(fd
, 0, SEEK_END
);
209 assert(bo
->size
> 0);
210 pipe_reference_init(&bo
->reference
, 1);
212 // TODO map and unmap on demand?
213 panfrost_drm_mmap_bo(screen
, bo
);
218 panfrost_drm_export_bo(struct panfrost_screen
*screen
, const struct panfrost_bo
*bo
)
220 struct drm_prime_handle args
= {
221 .handle
= bo
->gem_handle
,
222 .flags
= DRM_CLOEXEC
,
225 int ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
233 panfrost_drm_submit_job(struct panfrost_context
*ctx
, u64 job_desc
, int reqs
)
235 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
236 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
237 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
238 struct drm_panfrost_submit submit
= {0,};
239 int *bo_handles
, ret
;
241 submit
.in_syncs
= (u64
) (uintptr_t) &ctx
->out_sync
;
242 submit
.in_sync_count
= 1;
244 submit
.out_sync
= ctx
->out_sync
;
246 submit
.jc
= job_desc
;
247 submit
.requirements
= reqs
;
249 bo_handles
= calloc(job
->bos
->entries
, sizeof(*bo_handles
));
252 set_foreach(job
->bos
, entry
) {
253 struct panfrost_bo
*bo
= (struct panfrost_bo
*)entry
->key
;
254 assert(bo
->gem_handle
> 0);
255 bo_handles
[submit
.bo_handle_count
++] = bo
->gem_handle
;
258 submit
.bo_handles
= (u64
) (uintptr_t) bo_handles
;
259 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_SUBMIT
, &submit
);
262 fprintf(stderr
, "Error submitting: %m\n");
266 /* Trace the job if we're doing that */
267 if (pan_debug
& PAN_DBG_TRACE
) {
268 /* Wait so we can get errors reported back */
269 drmSyncobjWait(screen
->fd
, &ctx
->out_sync
, 1, INT64_MAX
, 0, NULL
);
270 pandecode_jc(submit
.jc
, FALSE
);
277 panfrost_drm_submit_vs_fs_job(struct panfrost_context
*ctx
, bool has_draws
, bool is_scanout
)
281 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
283 /* TODO: Add here the transient pools */
284 panfrost_job_add_bo(job
, ctx
->shaders
.bo
);
285 panfrost_job_add_bo(job
, ctx
->scratchpad
.bo
);
286 panfrost_job_add_bo(job
, ctx
->tiler_heap
.bo
);
287 panfrost_job_add_bo(job
, ctx
->varying_mem
.bo
);
288 panfrost_job_add_bo(job
, ctx
->tiler_polygon_list
.bo
);
290 if (job
->first_job
.gpu
) {
291 ret
= panfrost_drm_submit_job(ctx
, job
->first_job
.gpu
, 0);
295 if (job
->first_tiler
.gpu
|| job
->clear
) {
296 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
298 struct panfrost_resource
*res
= pan_resource(surf
->texture
);
300 panfrost_job_add_bo(job
, res
->bo
);
302 ret
= panfrost_drm_submit_job(ctx
, panfrost_fragment_job(ctx
, has_draws
), PANFROST_JD_REQ_FS
);
309 static struct panfrost_fence
*
310 panfrost_fence_create(struct panfrost_context
*ctx
)
312 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
313 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
314 struct panfrost_fence
*f
= calloc(1, sizeof(*f
));
318 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
319 * another syncobj instead of a sync file, but this is all we get.
320 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
323 drmSyncobjExportSyncFile(screen
->fd
, ctx
->out_sync
, &f
->fd
);
325 fprintf(stderr
, "export failed\n");
330 pipe_reference_init(&f
->reference
, 1);
336 panfrost_drm_force_flush_fragment(struct panfrost_context
*ctx
,
337 struct pipe_fence_handle
**fence
)
339 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
340 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
342 if (!screen
->last_fragment_flushed
) {
343 drmSyncobjWait(screen
->fd
, &ctx
->out_sync
, 1, INT64_MAX
, 0, NULL
);
344 screen
->last_fragment_flushed
= true;
346 /* The job finished up, so we're safe to clean it up now */
347 panfrost_free_job(ctx
, screen
->last_job
);
351 struct panfrost_fence
*f
= panfrost_fence_create(ctx
);
352 gallium
->screen
->fence_reference(gallium
->screen
, fence
, NULL
);
353 *fence
= (struct pipe_fence_handle
*)f
;
358 panfrost_drm_query_gpu_version(struct panfrost_screen
*screen
)
360 struct drm_panfrost_get_param get_param
= {0,};
361 MAYBE_UNUSED
int ret
;
363 get_param
.param
= DRM_PANFROST_PARAM_GPU_PROD_ID
;
364 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_PARAM
, &get_param
);
367 return get_param
.value
;
371 panfrost_drm_init_context(struct panfrost_context
*ctx
)
373 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
374 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
376 return drmSyncobjCreate(screen
->fd
, DRM_SYNCOBJ_CREATE_SIGNALED
,
381 panfrost_drm_fence_reference(struct pipe_screen
*screen
,
382 struct pipe_fence_handle
**ptr
,
383 struct pipe_fence_handle
*fence
)
385 struct panfrost_fence
**p
= (struct panfrost_fence
**)ptr
;
386 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
387 struct panfrost_fence
*old
= *p
;
389 if (pipe_reference(&(*p
)->reference
, &f
->reference
)) {
397 panfrost_drm_fence_finish(struct pipe_screen
*pscreen
,
398 struct pipe_context
*ctx
,
399 struct pipe_fence_handle
*fence
,
402 struct panfrost_screen
*screen
= pan_screen(pscreen
);
403 struct panfrost_fence
*f
= (struct panfrost_fence
*)fence
;
407 ret
= drmSyncobjCreate(screen
->fd
, 0, &syncobj
);
409 fprintf(stderr
, "Failed to create syncobj to wait on: %m\n");
413 drmSyncobjImportSyncFile(screen
->fd
, syncobj
, f
->fd
);
415 fprintf(stderr
, "Failed to import fence to syncobj: %m\n");
419 uint64_t abs_timeout
= os_time_get_absolute_timeout(timeout
);
420 if (abs_timeout
== OS_TIMEOUT_INFINITE
)
421 abs_timeout
= INT64_MAX
;
423 ret
= drmSyncobjWait(screen
->fd
, &syncobj
, 1, abs_timeout
, 0, NULL
);
425 drmSyncobjDestroy(screen
->fd
, syncobj
);