drm-uapi: use local files, not system libdrm
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2014 Broadcom
5 * Copyright 2018 Alyssa Rosenzweig
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include <xf86drm.h>
31 #include <fcntl.h>
32 #include "drm-uapi/drm_fourcc.h"
33
34 #include "state_tracker/winsys_handle.h"
35 #include "util/u_format.h"
36 #include "util/u_memory.h"
37 #include "util/u_surface.h"
38 #include "util/u_transfer.h"
39 #include "util/u_transfer_helper.h"
40
41 #include "pan_context.h"
42 #include "pan_screen.h"
43 #include "pan_resource.h"
44 #include "pan_swizzle.h"
45
46 static struct pipe_resource *
47 panfrost_resource_from_handle(struct pipe_screen *pscreen,
48 const struct pipe_resource *templat,
49 struct winsys_handle *whandle,
50 unsigned usage)
51 {
52 struct panfrost_screen *screen = pan_screen(pscreen);
53 struct panfrost_resource *rsc;
54 struct pipe_resource *prsc;
55
56 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
57
58 rsc = CALLOC_STRUCT(panfrost_resource);
59 if (!rsc)
60 return NULL;
61
62 prsc = &rsc->base;
63
64 *prsc = *templat;
65
66 pipe_reference_init(&prsc->reference, 1);
67 prsc->screen = pscreen;
68
69 rsc->bo = screen->driver->import_bo(screen, whandle);
70
71 return prsc;
72 }
73
74 static boolean
75 panfrost_resource_get_handle(struct pipe_screen *pscreen,
76 struct pipe_context *ctx,
77 struct pipe_resource *pt,
78 struct winsys_handle *handle,
79 unsigned usage)
80 {
81 struct panfrost_screen *screen = pan_screen(pscreen);
82 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
83 struct renderonly_scanout *scanout = rsrc->scanout;
84 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
85 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
86
87 handle->stride = stride;
88 handle->modifier = DRM_FORMAT_MOD_INVALID;
89
90 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
91 printf("Missed shared handle\n");
92 return FALSE;
93 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
94 if (renderonly_get_handle(scanout, handle)) {
95 return TRUE;
96 } else {
97 printf("Missed nonrenderonly KMS handle for resource %p with scanout %p\n", pt, scanout);
98 return FALSE;
99 }
100 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
101 if (scanout) {
102 struct drm_prime_handle args = {
103 .handle = scanout->handle,
104 .flags = DRM_CLOEXEC,
105 };
106
107 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
108 if (ret == -1)
109 return FALSE;
110
111 handle->handle = args.fd;
112
113 return TRUE;
114 } else {
115 printf("Missed nonscanout FD handle\n");
116 assert(0);
117 return FALSE;
118 }
119 }
120
121 return FALSE;
122 }
123
124 static void
125 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
126 {
127 //fprintf(stderr, "TODO %s\n", __func__);
128 }
129
130 static void
131 panfrost_blit(struct pipe_context *pipe,
132 const struct pipe_blit_info *info)
133 {
134 /* STUB */
135 printf("Skipping blit XXX\n");
136 return;
137 }
138
139 static struct pipe_surface *
140 panfrost_create_surface(struct pipe_context *pipe,
141 struct pipe_resource *pt,
142 const struct pipe_surface *surf_tmpl)
143 {
144 struct pipe_surface *ps = NULL;
145
146 ps = CALLOC_STRUCT(pipe_surface);
147
148 if (ps) {
149 pipe_reference_init(&ps->reference, 1);
150 pipe_resource_reference(&ps->texture, pt);
151 ps->context = pipe;
152 ps->format = surf_tmpl->format;
153
154 if (pt->target != PIPE_BUFFER) {
155 assert(surf_tmpl->u.tex.level <= pt->last_level);
156 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
157 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
158 ps->u.tex.level = surf_tmpl->u.tex.level;
159 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
160 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
161 } else {
162 /* setting width as number of elements should get us correct renderbuffer width */
163 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
164 ps->height = pt->height0;
165 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
166 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
167 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
168 assert(ps->u.buf.last_element < ps->width);
169 }
170 }
171
172 return ps;
173 }
174
175 static void
176 panfrost_surface_destroy(struct pipe_context *pipe,
177 struct pipe_surface *surf)
178 {
179 assert(surf->texture);
180 pipe_resource_reference(&surf->texture, NULL);
181 free(surf);
182 }
183
184 static struct panfrost_bo *
185 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
186 {
187 struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
188 int bytes_per_pixel = util_format_get_blocksize(template->format);
189 int stride = bytes_per_pixel * template->width0; /* TODO: Alignment? */
190 size_t sz = stride;
191
192 if (template->height0) sz *= template->height0;
193
194 if (template->depth0) sz *= template->depth0;
195
196 if ((template->bind & PIPE_BIND_RENDER_TARGET) || (template->bind & PIPE_BIND_DEPTH_STENCIL)) {
197 /* TODO: Mipmapped RTs */
198 //assert(template->last_level == 0);
199
200 /* Allocate the framebuffer as its own slab of GPU-accessible memory */
201 struct panfrost_memory slab;
202 screen->driver->allocate_slab(screen, &slab, (sz / 4096) + 1, false, 0, 0, 0);
203
204 /* Make the resource out of the slab */
205 bo->cpu[0] = slab.cpu;
206 bo->gpu[0] = slab.gpu;
207 } else {
208 /* TODO: For linear resources, allocate straight on the cmdstream for
209 * zero-copy operation */
210
211 /* Tiling textures is almost always faster, unless we only use it once */
212 bo->tiled = (template->usage != PIPE_USAGE_STREAM) && (template->bind & PIPE_BIND_SAMPLER_VIEW);
213
214 if (bo->tiled) {
215 /* For tiled, we don't map directly, so just malloc any old buffer */
216
217 for (int l = 0; l < (template->last_level + 1); ++l) {
218 bo->cpu[l] = malloc(sz);
219 //sz >>= 2;
220 }
221 } else {
222 /* But for linear, we can! */
223
224 struct pb_slab_entry *entry = pb_slab_alloc(&screen->slabs, sz, HEAP_TEXTURE);
225 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
226 struct panfrost_memory *backing = (struct panfrost_memory *) entry->slab;
227 bo->entry[0] = p_entry;
228 bo->cpu[0] = backing->cpu + p_entry->offset;
229 bo->gpu[0] = backing->gpu + p_entry->offset;
230
231 /* TODO: Mipmap */
232 }
233 }
234
235 return bo;
236 }
237
238 static struct pipe_resource *
239 panfrost_resource_create(struct pipe_screen *screen,
240 const struct pipe_resource *template)
241 {
242 struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
243 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
244
245 so->base = *template;
246 so->base.screen = screen;
247
248 pipe_reference_init(&so->base.reference, 1);
249
250 /* Make sure we're familiar */
251 switch (template->target) {
252 case PIPE_BUFFER:
253 case PIPE_TEXTURE_1D:
254 case PIPE_TEXTURE_2D:
255 case PIPE_TEXTURE_3D:
256 case PIPE_TEXTURE_RECT:
257 break;
258 default:
259 fprintf(stderr, "Unknown texture target %d\n", template->target);
260 assert(0);
261 }
262
263 if ((template->bind & PIPE_BIND_RENDER_TARGET) || (template->bind & PIPE_BIND_DEPTH_STENCIL)) {
264 if (template->bind & PIPE_BIND_DISPLAY_TARGET ||
265 template->bind & PIPE_BIND_SCANOUT ||
266 template->bind & PIPE_BIND_SHARED) {
267 struct pipe_resource scanout_templat = *template;
268 struct renderonly_scanout *scanout;
269 struct winsys_handle handle;
270
271 /* TODO: align width0 and height0? */
272
273 scanout = renderonly_scanout_for_resource(&scanout_templat,
274 pscreen->ro, &handle);
275 if (!scanout)
276 return NULL;
277
278 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
279 /* TODO: handle modifiers? */
280 so = pan_resource(screen->resource_from_handle(screen, template,
281 &handle,
282 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
283 close(handle.handle);
284 if (!so)
285 return NULL;
286
287 so->scanout = scanout;
288 pscreen->display_target = so;
289 } else {
290 so->bo = panfrost_create_bo(pscreen, template);
291 }
292 } else {
293 so->bo = panfrost_create_bo(pscreen, template);
294 }
295
296 printf("Created resource %p with scanout %p\n", so, so->scanout);
297
298 return (struct pipe_resource *)so;
299 }
300
301 static void
302 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
303 {
304 struct panfrost_bo *bo = (struct panfrost_bo *)pbo;
305
306 if (bo->tiled) {
307 /* CPU is all malloc'ed, so just plain ol' free needed */
308
309 for (int l = 0; bo->cpu[l]; l++) {
310 free(bo->cpu[l]);
311 }
312 } else if (bo->entry[0] != NULL) {
313 bo->entry[0]->freed = true;
314 pb_slab_free(&screen->slabs, &bo->entry[0]->base);
315 } else {
316 /* TODO */
317 printf("--leaking main allocation--\n");
318 }
319
320 if (bo->has_afbc) {
321 /* TODO */
322 printf("--leaking afbc--\n");
323 }
324
325 if (bo->has_checksum) {
326 /* TODO */
327 printf("--leaking checksum--\n");
328 }
329 }
330
331 static void
332 panfrost_resource_destroy(struct pipe_screen *screen,
333 struct pipe_resource *pt)
334 {
335 struct panfrost_screen *pscreen = panfrost_screen(screen);
336 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
337
338 if (rsrc->scanout)
339 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
340
341 if (rsrc->bo)
342 panfrost_destroy_bo(pscreen, rsrc->bo);
343
344 FREE(rsrc);
345 }
346
347 static uint8_t *
348 panfrost_map_bo(struct panfrost_context *ctx, struct pipe_transfer *transfer)
349 {
350 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
351
352 /* If non-zero level, it's a mipmapped resource and needs to be treated as such */
353 bo->is_mipmap |= transfer->level;
354
355 if (transfer->usage & PIPE_TRANSFER_MAP_DIRECTLY && bo->tiled) {
356 /* We cannot directly map tiled textures */
357 return NULL;
358 }
359
360 if (transfer->resource->bind & PIPE_BIND_DEPTH_STENCIL) {
361 /* Mipmapped readpixels?! */
362 assert(transfer->level == 0);
363
364 /* Set the CPU mapping to that of the depth/stencil buffer in memory, untiled */
365 bo->cpu[transfer->level] = ctx->depth_stencil_buffer.cpu;
366 }
367
368 return bo->cpu[transfer->level];
369 }
370
371 static void *
372 panfrost_transfer_map(struct pipe_context *pctx,
373 struct pipe_resource *resource,
374 unsigned level,
375 unsigned usage, /* a combination of PIPE_TRANSFER_x */
376 const struct pipe_box *box,
377 struct pipe_transfer **out_transfer)
378 {
379 struct panfrost_context *ctx = pan_context(pctx);
380 int bytes_per_pixel = util_format_get_blocksize(resource->format);
381 int stride = bytes_per_pixel * resource->width0; /* TODO: Alignment? */
382 uint8_t *cpu;
383
384 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
385 transfer->level = level;
386 transfer->usage = usage;
387 transfer->box = *box;
388 transfer->stride = stride;
389 assert(!transfer->box.z);
390
391 pipe_resource_reference(&transfer->resource, resource);
392
393 *out_transfer = transfer;
394
395 if (resource->bind & PIPE_BIND_DISPLAY_TARGET ||
396 resource->bind & PIPE_BIND_SCANOUT ||
397 resource->bind & PIPE_BIND_SHARED) {
398 /* Mipmapped readpixels?! */
399 assert(level == 0);
400
401 /* Force a flush -- kill the pipeline */
402 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
403 }
404
405 cpu = panfrost_map_bo(ctx, transfer);
406 if (cpu == NULL)
407 return NULL;
408
409 return cpu + transfer->box.x * bytes_per_pixel + transfer->box.y * stride;
410 }
411
412 static void
413 panfrost_tile_texture(struct panfrost_screen *screen, struct panfrost_resource *rsrc, int level)
414 {
415 struct panfrost_bo *bo = (struct panfrost_bo *)rsrc->bo;
416 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
417 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
418
419 int width = rsrc->base.width0 >> level;
420 int height = rsrc->base.height0 >> level;
421
422 /* Estimate swizzled bitmap size. Slight overestimates are fine.
423 * Underestimates will result in memory corruption or worse. */
424
425 int swizzled_sz = panfrost_swizzled_size(width, height, bytes_per_pixel);
426
427 /* Allocate the transfer given that known size but do not copy */
428 struct pb_slab_entry *entry = pb_slab_alloc(&screen->slabs, swizzled_sz, HEAP_TEXTURE);
429 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
430 struct panfrost_memory *backing = (struct panfrost_memory *) entry->slab;
431 uint8_t *swizzled = backing->cpu + p_entry->offset;
432
433 /* Save the entry. But if there was already an entry here (from a
434 * previous upload of the resource), free that one so we don't leak */
435
436 if (bo->entry[level] != NULL) {
437 bo->entry[level]->freed = true;
438 pb_slab_free(&screen->slabs, &bo->entry[level]->base);
439 }
440
441 bo->entry[level] = p_entry;
442 bo->gpu[level] = backing->gpu + p_entry->offset;
443
444 /* Run actual texture swizzle, writing directly to the mapped
445 * GPU chunk we allocated */
446
447 panfrost_texture_swizzle(width, height, bytes_per_pixel, stride, bo->cpu[level], swizzled);
448 }
449
450 static void
451 panfrost_unmap_bo(struct panfrost_context *ctx,
452 struct pipe_transfer *transfer)
453 {
454 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
455
456 if (transfer->usage & PIPE_TRANSFER_WRITE) {
457 if (transfer->resource->target == PIPE_TEXTURE_2D) {
458 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
459
460 /* Gallium thinks writeback happens here; instead, this is our cue to tile */
461 if (bo->has_afbc) {
462 printf("Warning: writes to afbc surface can't possibly work out well for you...\n");
463 } else if (bo->tiled) {
464 struct pipe_context *gallium = (struct pipe_context *) ctx;
465 struct panfrost_screen *screen = pan_screen(gallium->screen);
466 panfrost_tile_texture(screen, prsrc, transfer->level);
467 }
468 }
469 }
470 }
471
472 static void
473 panfrost_transfer_unmap(struct pipe_context *pctx,
474 struct pipe_transfer *transfer)
475 {
476 struct panfrost_context *ctx = pan_context(pctx);
477
478 panfrost_unmap_bo(ctx, transfer);
479
480 /* Derefence the resource */
481 pipe_resource_reference(&transfer->resource, NULL);
482
483 /* Transfer itself is CALLOCed at the moment */
484 free(transfer);
485 }
486
487 static struct pb_slab *
488 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
489 {
490 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
491 struct panfrost_memory *mem = CALLOC_STRUCT(panfrost_memory);
492
493 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
494
495 mem->slab.num_entries = slab_size / entry_size;
496 mem->slab.num_free = mem->slab.num_entries;
497
498 LIST_INITHEAD(&mem->slab.free);
499 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
500 /* Create a slab entry */
501 struct panfrost_memory_entry *entry = CALLOC_STRUCT(panfrost_memory_entry);
502 entry->offset = entry_size * i;
503
504 entry->base.slab = &mem->slab;
505 entry->base.group_index = group_index;
506
507 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
508 }
509
510 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
511 * special flags */
512
513 screen->driver->allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
514
515 return &mem->slab;
516 }
517
518 static bool
519 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
520 {
521 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
522 return p_entry->freed;
523 }
524
525 static void
526 panfrost_slab_free(void *priv, struct pb_slab *slab)
527 {
528 /* STUB */
529 //struct panfrost_memory *mem = (struct panfrost_memory *) slab;
530 printf("stub: Tried to free slab\n");
531 }
532
533 static void
534 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
535 {
536 //fprintf(stderr, "TODO %s\n", __func__);
537 }
538
539 static const struct u_transfer_vtbl transfer_vtbl = {
540 .resource_create = panfrost_resource_create,
541 .resource_destroy = panfrost_resource_destroy,
542 .transfer_map = panfrost_transfer_map,
543 .transfer_unmap = panfrost_transfer_unmap,
544 .transfer_flush_region = u_default_transfer_flush_region,
545 //.get_internal_format = panfrost_resource_get_internal_format,
546 //.set_stencil = panfrost_resource_set_stencil,
547 //.get_stencil = panfrost_resource_get_stencil,
548 };
549
550 void
551 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
552 {
553 //pscreen->base.resource_create_with_modifiers =
554 // panfrost_resource_create_with_modifiers;
555 pscreen->base.resource_create = u_transfer_helper_resource_create;
556 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
557 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
558 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
559 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
560 true, true,
561 true, true);
562
563 pb_slabs_init(&pscreen->slabs,
564 MIN_SLAB_ENTRY_SIZE,
565 MAX_SLAB_ENTRY_SIZE,
566
567 3, /* Number of heaps */
568
569 pscreen,
570
571 panfrost_slab_can_reclaim,
572 panfrost_slab_alloc,
573 panfrost_slab_free);
574 }
575
576 void
577 panfrost_resource_context_init(struct pipe_context *pctx)
578 {
579 pctx->transfer_map = u_transfer_helper_transfer_map;
580 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
581 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
582 pctx->buffer_subdata = u_default_buffer_subdata;
583 pctx->create_surface = panfrost_create_surface;
584 pctx->surface_destroy = panfrost_surface_destroy;
585 pctx->resource_copy_region = util_resource_copy_region;
586 pctx->blit = panfrost_blit;
587 //pctx->generate_mipmap = panfrost_generate_mipmap;
588 pctx->flush_resource = panfrost_flush_resource;
589 pctx->invalidate_resource = panfrost_invalidate_resource;
590 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
591 pctx->buffer_subdata = u_default_buffer_subdata;
592 pctx->texture_subdata = u_default_texture_subdata;
593 }