panfrost: Make SLAB pool creation rely on BO helpers
[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_util.h"
39 #include "pandecode/decode.h"
40
41 static void
42 panfrost_drm_mmap_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
43 {
44 struct drm_panfrost_mmap_bo mmap_bo = { .handle = bo->gem_handle };
45 int ret;
46
47 if (bo->cpu)
48 return;
49
50 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
51 if (ret) {
52 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %d\n", ret);
53 assert(0);
54 }
55
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);
60 assert(0);
61 }
62
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);
66 }
67
68 static void
69 panfrost_drm_munmap_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
70 {
71 if (!bo->cpu)
72 return;
73
74 if (os_munmap((void *) (uintptr_t)bo->cpu, bo->size)) {
75 perror("munmap");
76 abort();
77 }
78
79 bo->cpu = NULL;
80 }
81
82 struct panfrost_bo *
83 panfrost_drm_create_bo(struct panfrost_screen *screen, size_t size,
84 uint32_t flags)
85 {
86 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
87 struct drm_panfrost_create_bo create_bo = {
88 .size = size,
89 .flags = flags,
90 };
91 int ret;
92
93 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
94 if (ret) {
95 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
96 assert(0);
97 }
98
99 bo->size = create_bo.size;
100 bo->gpu = create_bo.offset;
101 bo->gem_handle = create_bo.handle;
102
103 // TODO map and unmap on demand?
104 panfrost_drm_mmap_bo(screen, bo);
105
106 pipe_reference_init(&bo->reference, 1);
107 return bo;
108 }
109
110 void
111 panfrost_drm_release_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
112 {
113 struct drm_gem_close gem_close = { .handle = bo->gem_handle };
114 int ret;
115
116 if (!bo)
117 return;
118
119 panfrost_drm_munmap_bo(screen, bo);
120
121 ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
122 if (ret) {
123 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
124 assert(0);
125 }
126
127 ralloc_free(bo);
128 }
129
130 void
131 panfrost_drm_allocate_slab(struct panfrost_screen *screen,
132 struct panfrost_memory *mem,
133 size_t pages,
134 bool same_va,
135 int extra_flags,
136 int commit_count,
137 int extent)
138 {
139 // TODO cache allocations
140 // TODO properly handle errors
141 // TODO take into account extra_flags
142 mem->bo = panfrost_drm_create_bo(screen, pages * 4096, 0);
143 mem->stack_bottom = 0;
144 }
145
146 void
147 panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
148 {
149 panfrost_bo_unreference(&screen->base, mem->bo);
150 mem->bo = NULL;
151 }
152
153 struct panfrost_bo *
154 panfrost_drm_import_bo(struct panfrost_screen *screen, int fd)
155 {
156 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
157 struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
158 int ret;
159 unsigned gem_handle;
160
161 ret = drmPrimeFDToHandle(screen->fd, fd, &gem_handle);
162 assert(!ret);
163
164 get_bo_offset.handle = gem_handle;
165 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
166 assert(!ret);
167
168 bo->gem_handle = gem_handle;
169 bo->gpu = (mali_ptr) get_bo_offset.offset;
170 bo->size = lseek(fd, 0, SEEK_END);
171 assert(bo->size > 0);
172 pipe_reference_init(&bo->reference, 1);
173
174 // TODO map and unmap on demand?
175 panfrost_drm_mmap_bo(screen, bo);
176 return bo;
177 }
178
179 int
180 panfrost_drm_export_bo(struct panfrost_screen *screen, const struct panfrost_bo *bo)
181 {
182 struct drm_prime_handle args = {
183 .handle = bo->gem_handle,
184 .flags = DRM_CLOEXEC,
185 };
186
187 int ret = drmIoctl(screen->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
188 if (ret == -1)
189 return -1;
190
191 return args.fd;
192 }
193
194 static int
195 panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs, struct pipe_surface *surf)
196 {
197 struct pipe_context *gallium = (struct pipe_context *) ctx;
198 struct panfrost_screen *screen = pan_screen(gallium->screen);
199 struct drm_panfrost_submit submit = {0,};
200 int bo_handles[7];
201
202 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
203 submit.in_sync_count = 1;
204
205 submit.out_sync = ctx->out_sync;
206
207 submit.jc = job_desc;
208 submit.requirements = reqs;
209
210 if (surf) {
211 struct panfrost_resource *res = pan_resource(surf->texture);
212 assert(res->bo->gem_handle > 0);
213 bo_handles[submit.bo_handle_count++] = res->bo->gem_handle;
214 }
215
216 /* TODO: Add here the transient pools */
217 /* TODO: Add here the BOs listed in the panfrost_job */
218 bo_handles[submit.bo_handle_count++] = ctx->shaders.bo->gem_handle;
219 bo_handles[submit.bo_handle_count++] = ctx->scratchpad.bo->gem_handle;
220 bo_handles[submit.bo_handle_count++] = ctx->tiler_heap.bo->gem_handle;
221 bo_handles[submit.bo_handle_count++] = ctx->varying_mem.bo->gem_handle;
222 bo_handles[submit.bo_handle_count++] = ctx->tiler_polygon_list.bo->gem_handle;
223 submit.bo_handles = (u64) (uintptr_t) bo_handles;
224
225 if (drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit)) {
226 fprintf(stderr, "Error submitting: %m\n");
227 return errno;
228 }
229
230 /* Trace the job if we're doing that */
231 if (pan_debug & PAN_DBG_TRACE) {
232 /* Wait so we can get errors reported back */
233 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
234 pandecode_replay_jc(submit.jc, FALSE);
235 }
236
237 return 0;
238 }
239
240 int
241 panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
242 {
243 struct pipe_surface *surf = ctx->pipe_framebuffer.cbufs[0];
244 int ret;
245
246 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
247
248 if (job->first_job.gpu) {
249 ret = panfrost_drm_submit_job(ctx, job->first_job.gpu, 0, NULL);
250 assert(!ret);
251 }
252
253 if (job->first_tiler.gpu || job->clear) {
254 ret = panfrost_drm_submit_job(ctx, panfrost_fragment_job(ctx, has_draws), PANFROST_JD_REQ_FS, surf);
255 assert(!ret);
256 }
257
258 return ret;
259 }
260
261 static struct panfrost_fence *
262 panfrost_fence_create(struct panfrost_context *ctx)
263 {
264 struct pipe_context *gallium = (struct pipe_context *) ctx;
265 struct panfrost_screen *screen = pan_screen(gallium->screen);
266 struct panfrost_fence *f = calloc(1, sizeof(*f));
267 if (!f)
268 return NULL;
269
270 /* Snapshot the last Panfrost's rendering's out fence. We'd rather have
271 * another syncobj instead of a sync file, but this is all we get.
272 * (HandleToFD/FDToHandle just gives you another syncobj ID for the
273 * same syncobj).
274 */
275 drmSyncobjExportSyncFile(screen->fd, ctx->out_sync, &f->fd);
276 if (f->fd == -1) {
277 fprintf(stderr, "export failed\n");
278 free(f);
279 return NULL;
280 }
281
282 pipe_reference_init(&f->reference, 1);
283
284 return f;
285 }
286
287 void
288 panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
289 struct pipe_fence_handle **fence)
290 {
291 struct pipe_context *gallium = (struct pipe_context *) ctx;
292 struct panfrost_screen *screen = pan_screen(gallium->screen);
293
294 if (!screen->last_fragment_flushed) {
295 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
296 screen->last_fragment_flushed = true;
297
298 /* The job finished up, so we're safe to clean it up now */
299 panfrost_free_job(ctx, screen->last_job);
300 }
301
302 if (fence) {
303 struct panfrost_fence *f = panfrost_fence_create(ctx);
304 gallium->screen->fence_reference(gallium->screen, fence, NULL);
305 *fence = (struct pipe_fence_handle *)f;
306 }
307 }
308
309 unsigned
310 panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
311 {
312 struct drm_panfrost_get_param get_param = {0,};
313 int ret;
314
315 get_param.param = DRM_PANFROST_PARAM_GPU_PROD_ID;
316 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
317 assert(!ret);
318
319 return get_param.value;
320 }
321
322 int
323 panfrost_drm_init_context(struct panfrost_context *ctx)
324 {
325 struct pipe_context *gallium = (struct pipe_context *) ctx;
326 struct panfrost_screen *screen = pan_screen(gallium->screen);
327
328 return drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
329 &ctx->out_sync);
330 }
331
332 void
333 panfrost_drm_fence_reference(struct pipe_screen *screen,
334 struct pipe_fence_handle **ptr,
335 struct pipe_fence_handle *fence)
336 {
337 struct panfrost_fence **p = (struct panfrost_fence **)ptr;
338 struct panfrost_fence *f = (struct panfrost_fence *)fence;
339 struct panfrost_fence *old = *p;
340
341 if (pipe_reference(&(*p)->reference, &f->reference)) {
342 close(old->fd);
343 free(old);
344 }
345 *p = f;
346 }
347
348 boolean
349 panfrost_drm_fence_finish(struct pipe_screen *pscreen,
350 struct pipe_context *ctx,
351 struct pipe_fence_handle *fence,
352 uint64_t timeout)
353 {
354 struct panfrost_screen *screen = pan_screen(pscreen);
355 struct panfrost_fence *f = (struct panfrost_fence *)fence;
356 int ret;
357
358 unsigned syncobj;
359 ret = drmSyncobjCreate(screen->fd, 0, &syncobj);
360 if (ret) {
361 fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
362 return false;
363 }
364
365 drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd);
366 if (ret) {
367 fprintf(stderr, "Failed to import fence to syncobj: %m\n");
368 return false;
369 }
370
371 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
372 if (abs_timeout == OS_TIMEOUT_INFINITE)
373 abs_timeout = INT64_MAX;
374
375 ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);
376
377 drmSyncobjDestroy(screen->fd, syncobj);
378
379 return ret >= 0;
380 }