panfrost: Mark BOs as NOEXEC
[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 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;
87
88 /* Kernel will fail (confusingly) with EPERM otherwise */
89 assert(size > 0);
90
91 /* To maximize BO cache usage, don't allocate tiny BOs */
92 size = MAX2(size, 4096);
93
94 unsigned translated_flags = 0;
95
96 if (screen->kernel_version->version_major > 1 ||
97 screen->kernel_version->version_minor >= 1) {
98 if (!(flags & PAN_ALLOCATE_EXECUTE))
99 translated_flags |= PANFROST_BO_NOEXEC;
100 }
101
102 struct drm_panfrost_create_bo create_bo = {
103 .size = size,
104 .flags = translated_flags,
105 };
106
107 /* Before creating a BO, we first want to check the cache */
108
109 bo = panfrost_bo_cache_fetch(screen, size, flags);
110
111 if (bo == NULL) {
112 /* Otherwise, the cache misses and we need to allocate a BO fresh from
113 * the kernel */
114
115 int ret;
116
117 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
118 if (ret) {
119 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %d\n", ret);
120 assert(0);
121 }
122
123 /* We have a BO allocated from the kernel; fill in the userspace
124 * version */
125
126 bo = rzalloc(screen, struct panfrost_bo);
127 bo->size = create_bo.size;
128 bo->gpu = create_bo.offset;
129 bo->gem_handle = create_bo.handle;
130 bo->flags = flags;
131 }
132
133 /* Only mmap now if we know we need to. For CPU-invisible buffers, we
134 * never map since we don't care about their contents; they're purely
135 * for GPU-internal use. But we do trace them anyway. */
136
137 if (!(flags & (PAN_ALLOCATE_INVISIBLE | PAN_ALLOCATE_DELAY_MMAP)))
138 panfrost_drm_mmap_bo(screen, bo);
139 else if (flags & PAN_ALLOCATE_INVISIBLE) {
140 if (pan_debug & PAN_DBG_TRACE)
141 pandecode_inject_mmap(bo->gpu, NULL, bo->size, NULL);
142 }
143
144 pipe_reference_init(&bo->reference, 1);
145 return bo;
146 }
147
148 void
149 panfrost_drm_release_bo(struct panfrost_screen *screen, struct panfrost_bo *bo, bool cacheable)
150 {
151 struct drm_gem_close gem_close = { .handle = bo->gem_handle };
152 int ret;
153
154 if (!bo)
155 return;
156
157 /* Rather than freeing the BO now, we'll cache the BO for later
158 * allocations if we're allowed to */
159
160 if (cacheable) {
161 bool cached = panfrost_bo_cache_put(screen, bo);
162
163 if (cached)
164 return;
165 }
166
167 /* Otherwise, if the BO wasn't cached, we'll legitimately free the BO */
168
169 panfrost_drm_munmap_bo(screen, bo);
170
171 ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
172 if (ret) {
173 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %d\n", ret);
174 assert(0);
175 }
176
177 ralloc_free(bo);
178 }
179
180 void
181 panfrost_drm_allocate_slab(struct panfrost_screen *screen,
182 struct panfrost_memory *mem,
183 size_t pages,
184 bool same_va,
185 int extra_flags,
186 int commit_count,
187 int extent)
188 {
189 // TODO cache allocations
190 // TODO properly handle errors
191 // TODO take into account extra_flags
192 mem->bo = panfrost_drm_create_bo(screen, pages * 4096, extra_flags);
193 mem->stack_bottom = 0;
194 }
195
196 void
197 panfrost_drm_free_slab(struct panfrost_screen *screen, struct panfrost_memory *mem)
198 {
199 panfrost_bo_unreference(&screen->base, mem->bo);
200 mem->bo = NULL;
201 }
202
203 struct panfrost_bo *
204 panfrost_drm_import_bo(struct panfrost_screen *screen, int fd)
205 {
206 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
207 struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
208 ASSERTED int ret;
209 unsigned gem_handle;
210
211 ret = drmPrimeFDToHandle(screen->fd, fd, &gem_handle);
212 assert(!ret);
213
214 get_bo_offset.handle = gem_handle;
215 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
216 assert(!ret);
217
218 bo->gem_handle = gem_handle;
219 bo->gpu = (mali_ptr) get_bo_offset.offset;
220 bo->size = lseek(fd, 0, SEEK_END);
221 assert(bo->size > 0);
222 pipe_reference_init(&bo->reference, 1);
223
224 // TODO map and unmap on demand?
225 panfrost_drm_mmap_bo(screen, bo);
226 return bo;
227 }
228
229 int
230 panfrost_drm_export_bo(struct panfrost_screen *screen, const struct panfrost_bo *bo)
231 {
232 struct drm_prime_handle args = {
233 .handle = bo->gem_handle,
234 .flags = DRM_CLOEXEC,
235 };
236
237 int ret = drmIoctl(screen->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
238 if (ret == -1)
239 return -1;
240
241 return args.fd;
242 }
243
244 static int
245 panfrost_drm_submit_job(struct panfrost_context *ctx, u64 job_desc, int reqs)
246 {
247 struct pipe_context *gallium = (struct pipe_context *) ctx;
248 struct panfrost_screen *screen = pan_screen(gallium->screen);
249 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
250 struct drm_panfrost_submit submit = {0,};
251 int *bo_handles, ret;
252
253 submit.in_syncs = (u64) (uintptr_t) &ctx->out_sync;
254 submit.in_sync_count = 1;
255
256 submit.out_sync = ctx->out_sync;
257
258 submit.jc = job_desc;
259 submit.requirements = reqs;
260
261 bo_handles = calloc(job->bos->entries, sizeof(*bo_handles));
262 assert(bo_handles);
263
264 set_foreach(job->bos, entry) {
265 struct panfrost_bo *bo = (struct panfrost_bo *)entry->key;
266 assert(bo->gem_handle > 0);
267 bo_handles[submit.bo_handle_count++] = bo->gem_handle;
268 }
269
270 submit.bo_handles = (u64) (uintptr_t) bo_handles;
271 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_SUBMIT, &submit);
272 free(bo_handles);
273 if (ret) {
274 fprintf(stderr, "Error submitting: %m\n");
275 return errno;
276 }
277
278 /* Trace the job if we're doing that */
279 if (pan_debug & PAN_DBG_TRACE) {
280 /* Wait so we can get errors reported back */
281 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
282 pandecode_jc(submit.jc, FALSE);
283 }
284
285 return 0;
286 }
287
288 int
289 panfrost_drm_submit_vs_fs_job(struct panfrost_context *ctx, bool has_draws, bool is_scanout)
290 {
291 int ret = 0;
292
293 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
294
295 /* TODO: Add here the transient pools */
296 panfrost_job_add_bo(job, ctx->scratchpad.bo);
297 panfrost_job_add_bo(job, ctx->tiler_heap.bo);
298 panfrost_job_add_bo(job, job->polygon_list);
299
300 if (job->first_job.gpu) {
301 ret = panfrost_drm_submit_job(ctx, job->first_job.gpu, 0);
302 assert(!ret);
303 }
304
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);
307 assert(!ret);
308 }
309
310 return ret;
311 }
312
313 static struct panfrost_fence *
314 panfrost_fence_create(struct panfrost_context *ctx)
315 {
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));
319 if (!f)
320 return NULL;
321
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
325 * same syncobj).
326 */
327 drmSyncobjExportSyncFile(screen->fd, ctx->out_sync, &f->fd);
328 if (f->fd == -1) {
329 fprintf(stderr, "export failed\n");
330 free(f);
331 return NULL;
332 }
333
334 pipe_reference_init(&f->reference, 1);
335
336 return f;
337 }
338
339 void
340 panfrost_drm_force_flush_fragment(struct panfrost_context *ctx,
341 struct pipe_fence_handle **fence)
342 {
343 struct pipe_context *gallium = (struct pipe_context *) ctx;
344 struct panfrost_screen *screen = pan_screen(gallium->screen);
345
346 if (!screen->last_fragment_flushed) {
347 drmSyncobjWait(screen->fd, &ctx->out_sync, 1, INT64_MAX, 0, NULL);
348 screen->last_fragment_flushed = true;
349
350 /* The job finished up, so we're safe to clean it up now */
351 panfrost_free_job(ctx, screen->last_job);
352 }
353
354 if (fence) {
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;
358 }
359 }
360
361 unsigned
362 panfrost_drm_query_gpu_version(struct panfrost_screen *screen)
363 {
364 struct drm_panfrost_get_param get_param = {0,};
365 ASSERTED int ret;
366
367 get_param.param = DRM_PANFROST_PARAM_GPU_PROD_ID;
368 ret = drmIoctl(screen->fd, DRM_IOCTL_PANFROST_GET_PARAM, &get_param);
369 assert(!ret);
370
371 return get_param.value;
372 }
373
374 int
375 panfrost_drm_init_context(struct panfrost_context *ctx)
376 {
377 struct pipe_context *gallium = (struct pipe_context *) ctx;
378 struct panfrost_screen *screen = pan_screen(gallium->screen);
379
380 return drmSyncobjCreate(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
381 &ctx->out_sync);
382 }
383
384 void
385 panfrost_drm_fence_reference(struct pipe_screen *screen,
386 struct pipe_fence_handle **ptr,
387 struct pipe_fence_handle *fence)
388 {
389 struct panfrost_fence **p = (struct panfrost_fence **)ptr;
390 struct panfrost_fence *f = (struct panfrost_fence *)fence;
391 struct panfrost_fence *old = *p;
392
393 if (pipe_reference(&(*p)->reference, &f->reference)) {
394 close(old->fd);
395 free(old);
396 }
397 *p = f;
398 }
399
400 boolean
401 panfrost_drm_fence_finish(struct pipe_screen *pscreen,
402 struct pipe_context *ctx,
403 struct pipe_fence_handle *fence,
404 uint64_t timeout)
405 {
406 struct panfrost_screen *screen = pan_screen(pscreen);
407 struct panfrost_fence *f = (struct panfrost_fence *)fence;
408 int ret;
409
410 unsigned syncobj;
411 ret = drmSyncobjCreate(screen->fd, 0, &syncobj);
412 if (ret) {
413 fprintf(stderr, "Failed to create syncobj to wait on: %m\n");
414 return false;
415 }
416
417 drmSyncobjImportSyncFile(screen->fd, syncobj, f->fd);
418 if (ret) {
419 fprintf(stderr, "Failed to import fence to syncobj: %m\n");
420 return false;
421 }
422
423 uint64_t abs_timeout = os_time_get_absolute_timeout(timeout);
424 if (abs_timeout == OS_TIMEOUT_INFINITE)
425 abs_timeout = INT64_MAX;
426
427 ret = drmSyncobjWait(screen->fd, &syncobj, 1, abs_timeout, 0, NULL);
428
429 drmSyncobjDestroy(screen->fd, syncobj);
430
431 return ret >= 0;
432 }