panfrost: Remove the panfrost_driver abstraction
[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 void
43 panfrost_drm_allocate_slab(struct panfrost_screen *screen,
44 struct panfrost_memory *mem,
45 size_t pages,
46 bool same_va,
47 int extra_flags,
48 int commit_count,
49 int extent)
50 {
51 struct drm_panfrost_create_bo create_bo = {
52 .size = pages * 4096,
53 .flags = 0, // TODO figure out proper flags..
54 };
55 struct drm_panfrost_mmap_bo mmap_bo = {0,};
56 int ret;
57
58 // TODO cache allocations
59 // TODO properly handle errors
60 // TODO take into account extra_flags
61
62 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
63 if (ret) {
64 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
65 assert(0);
66 }
67
68 mem->gpu = create_bo.offset;
69 mem->gem_handle = create_bo.handle;
70 mem->stack_bottom = 0;
71 mem->size = create_bo.size;
72
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);
76 if (ret) {
77 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
78 assert(0);
79 }
80
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);
85 assert(0);
86 }
87
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);
91 }
92
93 void
94 panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
95 {
96 struct drm_gem_close gem_close = {
97 .handle = mem->gem_handle,
98 };
99 int ret;
100
101 if (os_munmap((void *) (uintptr_t) mem->cpu, mem->size)) {
102 perror("munmap");
103 abort();
104 }
105
106 mem->cpu = NULL;
107
108 ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
109 if (ret) {
110 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
111 assert(0);
112 }
113
114 mem->gem_handle = -1;
115 }
116
117 struct panfrost_bo *
118 panfrost_drm_import_bo(struct panfrost_screen *screen, struct winsys_handle *whandle)
119 {
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,};
123 int ret;
124 unsigned gem_handle;
125
126 ret = drmPrimeFDToHandle(screen->fd, whandle->handle, &gem_handle);
127 assert(!ret);
128
129 get_bo_offset.handle = gem_handle;
130 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
131 assert(!ret);
132
133 bo->gem_handle = gem_handle;
134 bo->gpu = (mali_ptr) get_bo_offset.offset;
135 pipe_reference_init(&bo->reference, 1);
136
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);
140 if (ret) {
141 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
142 assert(0);
143 }
144
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);
151 assert(0);
152 }
153
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);
157
158 return bo;
159 }
160
161 int
162 panfrost_drm_export_bo(struct panfrost_screen *screen, int gem_handle, unsigned int stride, struct winsys_handle *whandle)
163 {
164 struct drm_prime_handle args = {
165 .handle = gem_handle,
166 .flags = DRM_CLOEXEC,
167 };
168
169 int ret = drmIoctl(screen->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
170 if (ret == -1)
171 return FALSE;
172
173 whandle->handle = args.fd;
174 whandle->stride = stride;
175
176 return TRUE;
177 }
178
179 void
180 panfrost_drm_free_imported_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
181 {
182 struct drm_gem_close gem_close = {
183 .handle = bo->gem_handle,
184 };
185 int ret;
186
187 ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
188 if (ret) {
189 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
190 assert(0);
191 }
192
193 bo->gem_handle = -1;
194 bo->gpu = (mali_ptr)NULL;
195 }
196
197 int
198 panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs, struct pipe_surface *surf)
199 {
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,};
203 int bo_handles[7];
204
205 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
206 submit.in_sync_count = 1;
207
208 submit.out_sync = ctx->out_sync;
209
210 submit.jc = job_desc;
211 submit.requirements = reqs;
212
213 if (surf) {
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;
217
218 if (res->bo->checksum_slab.gem_handle)
219 bo_handles[submit.bo_handle_count++] = res->bo->checksum_slab.gem_handle;
220 }
221
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;
230
231 if (drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit)) {
232 fprintf(stderr, "Error submitting: %m\n");
233 return errno;
234 }
235
236 /* Trace the job if we're doing that */
237
238 if (pan_debug & PAN_DBG_TRACE)
239 pandecode_replay_jc(submit.jc, FALSE);
240
241 return 0;
242 }
243
244 int
245 panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
246 {
247 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
248 int ret;
249
250 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
251
252 if (job->first_job.gpu) {
253 ret = panfrost_drm_submit_job(ctx, job->first_job.gpu, 0, NULL);
254 assert(!ret);
255 }
256
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);
259 assert(!ret);
260 }
261
262 return ret;
263 }
264
265 struct panfrost_fence *
266 panfrost_fence_create(struct panfrost_context *ctx)
267 {
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));
271 if (!f)
272 return NULL;
273
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
277 * same syncobj).
278 */
279 drmSyncobjExportSyncFile(screen->fd, ctx->out_sync, &f->fd);
280 if (f->fd == -1) {
281 fprintf(stderr, "export failed\n");
282 free(f);
283 return NULL;
284 }
285
286 pipe_reference_init(&f->reference, 1);
287
288 return f;
289 }
290
291 void
292 panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
293 struct pipe_fence_handle **fence)
294 {
295 struct pipe_context *gallium = (struct pipe_context *) ctx;
296 struct panfrost_screen *screen = pan_screen(gallium->screen);
297
298 if (!screen->last_fragment_flushed) {
299 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
300 screen->last_fragment_flushed = true;
301
302 /* The job finished up, so we're safe to clean it up now */
303 panfrost_free_job(ctx, screen->last_job);
304 }
305
306 if (fence) {
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;
310 }
311 }
312
313 unsigned
314 panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
315 {
316 struct drm_panfrost_get_param get_param = {0,};
317 int ret;
318
319 get_param.param = DRM_PANFROST_PARAM_GPU_PROD_ID;
320 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
321 assert(!ret);
322
323 return get_param.value;
324 }
325
326 int
327 panfrost_drm_init_context(struct panfrost_context *ctx)
328 {
329 struct pipe_context *gallium = (struct pipe_context *) ctx;
330 struct panfrost_screen *screen = pan_screen(gallium->screen);
331
332 return drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
333 &ctx->out_sync);
334 }
335
336 void
337 panfrost_drm_fence_reference(struct pipe_screen *screen,
338 struct pipe_fence_handle **ptr,
339 struct pipe_fence_handle *fence)
340 {
341 struct panfrost_fence **p = (struct panfrost_fence **)ptr;
342 struct panfrost_fence *f = (struct panfrost_fence *)fence;
343 struct panfrost_fence *old = *p;
344
345 if (pipe_reference(&(*p)->reference, &f->reference)) {
346 close(old->fd);
347 free(old);
348 }
349 *p = f;
350 }
351
352 boolean
353 panfrost_drm_fence_finish(struct pipe_screen *pscreen,
354 struct pipe_context *ctx,
355 struct pipe_fence_handle *fence,
356 uint64_t timeout)
357 {
358 struct panfrost_screen *screen = pan_screen(pscreen);
359 struct panfrost_fence *f = (struct panfrost_fence *)fence;
360 int ret;
361
362 unsigned syncobj;
363 ret = drmSyncobjCreate(screen->fd, 0, &syncobj);
364 if (ret) {
365 fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
366 return false;
367 }
368
369 drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd);
370 if (ret) {
371 fprintf(stderr, "Failed to import fence to syncobj: %m\n");
372 return false;
373 }
374
375 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
376 if (abs_timeout == OS_TIMEOUT_INFINITE)
377 abs_timeout = INT64_MAX;
378
379 ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);
380
381 drmSyncobjDestroy(screen->fd, syncobj);
382
383 return ret >= 0;
384 }