panfrost: Workaround buffer overrun with mip level
[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 #include "pan_util.h"
46
47 static struct pipe_resource *
48 panfrost_resource_from_handle(struct pipe_screen *pscreen,
49 const struct pipe_resource *templat,
50 struct winsys_handle *whandle,
51 unsigned usage)
52 {
53 struct panfrost_screen *screen = pan_screen(pscreen);
54 struct panfrost_resource *rsc;
55 struct pipe_resource *prsc;
56
57 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
58
59 rsc = CALLOC_STRUCT(panfrost_resource);
60 if (!rsc)
61 return NULL;
62
63 prsc = &rsc->base;
64
65 *prsc = *templat;
66
67 pipe_reference_init(&prsc->reference, 1);
68 prsc->screen = pscreen;
69
70 rsc->bo = screen->driver->import_bo(screen, whandle);
71
72 if (screen->ro) {
73 rsc->scanout =
74 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
75 /* failure is expected in some cases.. */
76 }
77
78 return prsc;
79 }
80
81 static boolean
82 panfrost_resource_get_handle(struct pipe_screen *pscreen,
83 struct pipe_context *ctx,
84 struct pipe_resource *pt,
85 struct winsys_handle *handle,
86 unsigned usage)
87 {
88 struct panfrost_screen *screen = pan_screen(pscreen);
89 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
90 struct renderonly_scanout *scanout = rsrc->scanout;
91 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
92 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
93
94 handle->stride = stride;
95 handle->modifier = DRM_FORMAT_MOD_INVALID;
96
97 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
98 return FALSE;
99 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
100 if (renderonly_get_handle(scanout, handle))
101 return TRUE;
102
103 handle->handle = rsrc->bo->gem_handle;
104 return TRUE;
105 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
106 if (scanout) {
107 struct drm_prime_handle args = {
108 .handle = scanout->handle,
109 .flags = DRM_CLOEXEC,
110 };
111
112 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
113 if (ret == -1)
114 return FALSE;
115
116 handle->handle = args.fd;
117
118 return TRUE;
119 } else
120 return screen->driver->export_bo(screen, rsrc->bo->gem_handle, handle);
121 }
122
123 return FALSE;
124 }
125
126 static void
127 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
128 {
129 //DBG("TODO %s\n", __func__);
130 }
131
132 static void
133 panfrost_blit(struct pipe_context *pipe,
134 const struct pipe_blit_info *info)
135 {
136 /* STUB */
137 DBG("Skipping blit XXX\n");
138 return;
139 }
140
141 static struct pipe_surface *
142 panfrost_create_surface(struct pipe_context *pipe,
143 struct pipe_resource *pt,
144 const struct pipe_surface *surf_tmpl)
145 {
146 struct pipe_surface *ps = NULL;
147
148 ps = CALLOC_STRUCT(pipe_surface);
149
150 if (ps) {
151 pipe_reference_init(&ps->reference, 1);
152 pipe_resource_reference(&ps->texture, pt);
153 ps->context = pipe;
154 ps->format = surf_tmpl->format;
155
156 if (pt->target != PIPE_BUFFER) {
157 assert(surf_tmpl->u.tex.level <= pt->last_level);
158 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
159 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
160 ps->u.tex.level = surf_tmpl->u.tex.level;
161 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
162 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
163 } else {
164 /* setting width as number of elements should get us correct renderbuffer width */
165 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
166 ps->height = pt->height0;
167 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
168 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
169 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
170 assert(ps->u.buf.last_element < ps->width);
171 }
172 }
173
174 return ps;
175 }
176
177 static void
178 panfrost_surface_destroy(struct pipe_context *pipe,
179 struct pipe_surface *surf)
180 {
181 assert(surf->texture);
182 pipe_resource_reference(&surf->texture, NULL);
183 free(surf);
184 }
185
186 static struct panfrost_bo *
187 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
188 {
189 struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
190
191 /* Calculate the size of the bo */
192
193 int bytes_per_pixel = util_format_get_blocksize(template->format);
194 int stride = bytes_per_pixel * template->width0; /* TODO: Alignment? */
195 size_t sz = stride;
196
197 if (template->height0) sz *= template->height0;
198 if (template->depth0) sz *= template->depth0;
199
200 /* Depth buffers require extra space for unknown reasons */
201
202 if (template->bind & PIPE_BIND_DEPTH_STENCIL)
203 sz = sz + sz/256;
204
205 /* Based on the usage, figure out what storing will be used. There are
206 * various tradeoffs:
207 *
208 * Linear: the basic format, bad for memory bandwidth, bad for cache
209 * use. Zero-copy, though. Renderable.
210 *
211 * Tiled: Not compressed, but cache-optimized. Expensive to write into
212 * (due to software tiling), but cheap to sample from. Ideal for most
213 * textures.
214 *
215 * AFBC: Compressed and renderable (so always desirable for non-scanout
216 * rendertargets). Cheap to sample from. The format is black box, so we
217 * can't read/write from software.
218 */
219
220 /* Tiling textures is almost always faster, unless we only use it once */
221 bool should_tile = (template->usage != PIPE_USAGE_STREAM) && (template->bind & PIPE_BIND_SAMPLER_VIEW);
222
223 /* For unclear reasons, depth/stencil is faster linear than AFBC, so
224 * make sure it's linear */
225
226 if (template->bind & PIPE_BIND_DEPTH_STENCIL)
227 should_tile = false;
228
229 /* Set the layout appropriately */
230 bo->layout = should_tile ? PAN_TILED : PAN_LINEAR;
231
232 if (bo->layout == PAN_TILED) {
233 /* For tiled, we don't map directly, so just malloc any old buffer */
234
235 for (int l = 0; l < (template->last_level + 1); ++l) {
236 bo->cpu[l] = malloc(sz);
237 bo->size[l] = sz;
238 }
239 } else {
240 /* For a linear resource, allocate a block of memory from
241 * kernel space */
242
243 struct panfrost_memory mem;
244
245 bo->size[0] = ALIGN(sz, 4096);
246 screen->driver->allocate_slab(screen, &mem, bo->size[0] / 4096, true, 0, 0, 0);
247
248 bo->cpu[0] = mem.cpu;
249 bo->gpu[0] = mem.gpu;
250 bo->gem_handle = mem.gem_handle;
251
252 /* TODO: Mipmap */
253 }
254
255 return bo;
256 }
257
258 static struct pipe_resource *
259 panfrost_resource_create(struct pipe_screen *screen,
260 const struct pipe_resource *template)
261 {
262 struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
263 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
264
265 so->base = *template;
266 so->base.screen = screen;
267
268 pipe_reference_init(&so->base.reference, 1);
269
270 /* Make sure we're familiar */
271 switch (template->target) {
272 case PIPE_BUFFER:
273 case PIPE_TEXTURE_1D:
274 case PIPE_TEXTURE_2D:
275 case PIPE_TEXTURE_3D:
276 case PIPE_TEXTURE_RECT:
277 break;
278 default:
279 DBG("Unknown texture target %d\n", template->target);
280 assert(0);
281 }
282
283 if (template->bind & PIPE_BIND_DISPLAY_TARGET ||
284 template->bind & PIPE_BIND_SCANOUT ||
285 template->bind & PIPE_BIND_SHARED) {
286 struct pipe_resource scanout_templat = *template;
287 struct renderonly_scanout *scanout;
288 struct winsys_handle handle;
289
290 /* TODO: align width0 and height0? */
291
292 scanout = renderonly_scanout_for_resource(&scanout_templat,
293 pscreen->ro, &handle);
294 if (!scanout)
295 return NULL;
296
297 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
298 /* TODO: handle modifiers? */
299 so = pan_resource(screen->resource_from_handle(screen, template,
300 &handle,
301 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
302 close(handle.handle);
303 if (!so)
304 return NULL;
305
306 so->scanout = scanout;
307 pscreen->display_target = so;
308 } else {
309 so->bo = panfrost_create_bo(pscreen, template);
310 }
311
312 return (struct pipe_resource *)so;
313 }
314
315 static void
316 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
317 {
318 struct panfrost_bo *bo = (struct panfrost_bo *)pbo;
319
320 if (bo->layout == PAN_LINEAR && !bo->imported) {
321 /* Construct a memory object for all mip levels */
322
323 struct panfrost_memory mem = {
324 .cpu = bo->cpu[0],
325 .gpu = bo->gpu[0],
326 .size = bo->size[0],
327 .gem_handle = bo->gem_handle,
328 };
329
330 screen->driver->free_slab(screen, &mem);
331 }
332
333 if (bo->layout == PAN_TILED) {
334 /* Tiled has a malloc'd CPU, so just plain ol' free needed */
335
336 for (int l = 0; l < MAX_MIP_LEVELS; ++l) {
337 free(bo->cpu[l]);
338 }
339 }
340
341 if (bo->layout == PAN_AFBC) {
342 /* TODO */
343 DBG("--leaking afbc (%d bytes)--\n", bo->afbc_metadata_size);
344 }
345
346 if (bo->has_checksum) {
347 /* TODO */
348 DBG("--leaking checksum (%zd bytes)--\n", bo->checksum_slab.size);
349 }
350
351 if (bo->imported) {
352 screen->driver->free_imported_bo(screen, bo);
353 }
354 }
355
356 static void
357 panfrost_resource_destroy(struct pipe_screen *screen,
358 struct pipe_resource *pt)
359 {
360 struct panfrost_screen *pscreen = pan_screen(screen);
361 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
362
363 if (rsrc->scanout)
364 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
365
366 if (rsrc->bo)
367 panfrost_destroy_bo(pscreen, rsrc->bo);
368
369 FREE(rsrc);
370 }
371
372 static uint8_t *
373 panfrost_map_bo(struct panfrost_context *ctx, struct pipe_transfer *transfer)
374 {
375 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
376
377 /* If non-zero level, it's a mipmapped resource and needs to be treated as such */
378 bo->is_mipmap |= transfer->level;
379
380 if (transfer->usage & PIPE_TRANSFER_MAP_DIRECTLY && bo->layout != PAN_LINEAR) {
381 /* We can only directly map linear resources */
382 return NULL;
383 }
384
385 if (transfer->resource->bind & PIPE_BIND_DEPTH_STENCIL) {
386 /* Mipmapped readpixels?! */
387 assert(transfer->level == 0);
388
389 /* Set the CPU mapping to that of the depth/stencil buffer in memory, untiled */
390 bo->cpu[transfer->level] = ctx->depth_stencil_buffer.cpu;
391 }
392
393 return bo->cpu[transfer->level];
394 }
395
396 static void *
397 panfrost_transfer_map(struct pipe_context *pctx,
398 struct pipe_resource *resource,
399 unsigned level,
400 unsigned usage, /* a combination of PIPE_TRANSFER_x */
401 const struct pipe_box *box,
402 struct pipe_transfer **out_transfer)
403 {
404 struct panfrost_context *ctx = pan_context(pctx);
405 int bytes_per_pixel = util_format_get_blocksize(resource->format);
406 int stride = bytes_per_pixel * resource->width0; /* TODO: Alignment? */
407 uint8_t *cpu;
408
409 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
410 transfer->level = level;
411 transfer->usage = usage;
412 transfer->box = *box;
413 transfer->stride = stride;
414 assert(!transfer->box.z);
415
416 pipe_resource_reference(&transfer->resource, resource);
417
418 *out_transfer = transfer;
419
420 if (resource->bind & PIPE_BIND_DISPLAY_TARGET ||
421 resource->bind & PIPE_BIND_SCANOUT ||
422 resource->bind & PIPE_BIND_SHARED) {
423 /* Mipmapped readpixels?! */
424 assert(level == 0);
425
426 /* Force a flush -- kill the pipeline */
427 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
428 }
429
430 cpu = panfrost_map_bo(ctx, transfer);
431 if (cpu == NULL)
432 return NULL;
433
434 return cpu + transfer->box.x * bytes_per_pixel + transfer->box.y * stride;
435 }
436
437 static void
438 panfrost_tile_texture(struct panfrost_screen *screen, struct panfrost_resource *rsrc, int level)
439 {
440 struct panfrost_bo *bo = (struct panfrost_bo *)rsrc->bo;
441 int bytes_per_pixel = util_format_get_blocksize(rsrc->base.format);
442 int stride = bytes_per_pixel * rsrc->base.width0; /* TODO: Alignment? */
443
444 int width = rsrc->base.width0 >> level;
445 int height = rsrc->base.height0 >> level;
446
447 /* Estimate swizzled bitmap size. Slight overestimates are fine.
448 * Underestimates will result in memory corruption or worse. */
449
450 int swizzled_sz = panfrost_swizzled_size(width, height, bytes_per_pixel);
451
452 /* Save the entry. But if there was already an entry here (from a
453 * previous upload of the resource), free that one so we don't leak */
454
455 if (bo->entry[level] != NULL) {
456 bo->entry[level]->freed = true;
457 pb_slab_free(&screen->slabs, &bo->entry[level]->base);
458 }
459
460 /* Allocate the transfer given that known size but do not copy */
461 struct pb_slab_entry *entry = pb_slab_alloc(&screen->slabs, swizzled_sz, HEAP_TEXTURE);
462 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
463 struct panfrost_memory *backing = (struct panfrost_memory *) entry->slab;
464 uint8_t *swizzled = backing->cpu + p_entry->offset;
465
466 bo->entry[level] = p_entry;
467 bo->gpu[level] = backing->gpu + p_entry->offset;
468
469 /* Run actual texture swizzle, writing directly to the mapped
470 * GPU chunk we allocated */
471
472 panfrost_texture_swizzle(width, height, bytes_per_pixel, stride, bo->cpu[level], swizzled);
473 }
474
475 static void
476 panfrost_unmap_bo(struct panfrost_context *ctx,
477 struct pipe_transfer *transfer)
478 {
479 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
480
481 if (transfer->usage & PIPE_TRANSFER_WRITE) {
482 if (transfer->resource->target == PIPE_TEXTURE_2D) {
483 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
484
485 /* Gallium thinks writeback happens here; instead, this is our cue to tile */
486 if (bo->layout == PAN_AFBC) {
487 DBG("Warning: writes to afbc surface can't possibly work out well for you...\n");
488 } else if (bo->layout == PAN_TILED) {
489 struct pipe_context *gallium = (struct pipe_context *) ctx;
490 struct panfrost_screen *screen = pan_screen(gallium->screen);
491 panfrost_tile_texture(screen, prsrc, transfer->level);
492 }
493 }
494 }
495 }
496
497 static void
498 panfrost_transfer_unmap(struct pipe_context *pctx,
499 struct pipe_transfer *transfer)
500 {
501 struct panfrost_context *ctx = pan_context(pctx);
502
503 panfrost_unmap_bo(ctx, transfer);
504
505 /* Derefence the resource */
506 pipe_resource_reference(&transfer->resource, NULL);
507
508 /* Transfer itself is CALLOCed at the moment */
509 free(transfer);
510 }
511
512 static struct pb_slab *
513 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
514 {
515 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
516 struct panfrost_memory *mem = CALLOC_STRUCT(panfrost_memory);
517
518 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
519
520 mem->slab.num_entries = slab_size / entry_size;
521 mem->slab.num_free = mem->slab.num_entries;
522
523 LIST_INITHEAD(&mem->slab.free);
524 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
525 /* Create a slab entry */
526 struct panfrost_memory_entry *entry = CALLOC_STRUCT(panfrost_memory_entry);
527 entry->offset = entry_size * i;
528
529 entry->base.slab = &mem->slab;
530 entry->base.group_index = group_index;
531
532 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
533 }
534
535 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
536 * special flags */
537
538 screen->driver->allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
539
540 return &mem->slab;
541 }
542
543 static bool
544 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
545 {
546 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
547 return p_entry->freed;
548 }
549
550 static void
551 panfrost_slab_free(void *priv, struct pb_slab *slab)
552 {
553 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
554 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
555
556 screen->driver->free_slab(screen, mem);
557 }
558
559 static void
560 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
561 {
562 //DBG("TODO %s\n", __func__);
563 }
564
565 static enum pipe_format
566 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
567 {
568 return prsrc->format;
569 }
570
571 static void
572 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
573 struct pipe_resource *stencil)
574 {
575 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
576 }
577
578 static struct pipe_resource *
579 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
580 {
581 return &pan_resource(prsrc)->separate_stencil->base;
582 }
583
584 static const struct u_transfer_vtbl transfer_vtbl = {
585 .resource_create = panfrost_resource_create,
586 .resource_destroy = panfrost_resource_destroy,
587 .transfer_map = panfrost_transfer_map,
588 .transfer_unmap = panfrost_transfer_unmap,
589 .transfer_flush_region = u_default_transfer_flush_region,
590 .get_internal_format = panfrost_resource_get_internal_format,
591 .set_stencil = panfrost_resource_set_stencil,
592 .get_stencil = panfrost_resource_get_stencil,
593 };
594
595 void
596 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
597 {
598 //pscreen->base.resource_create_with_modifiers =
599 // panfrost_resource_create_with_modifiers;
600 pscreen->base.resource_create = u_transfer_helper_resource_create;
601 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
602 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
603 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
604 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
605 true, false,
606 true, true);
607
608 pb_slabs_init(&pscreen->slabs,
609 MIN_SLAB_ENTRY_SIZE,
610 MAX_SLAB_ENTRY_SIZE,
611
612 3, /* Number of heaps */
613
614 pscreen,
615
616 panfrost_slab_can_reclaim,
617 panfrost_slab_alloc,
618 panfrost_slab_free);
619 }
620
621 void
622 panfrost_resource_context_init(struct pipe_context *pctx)
623 {
624 pctx->transfer_map = u_transfer_helper_transfer_map;
625 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
626 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
627 pctx->buffer_subdata = u_default_buffer_subdata;
628 pctx->create_surface = panfrost_create_surface;
629 pctx->surface_destroy = panfrost_surface_destroy;
630 pctx->resource_copy_region = util_resource_copy_region;
631 pctx->blit = panfrost_blit;
632 //pctx->generate_mipmap = panfrost_generate_mipmap;
633 pctx->flush_resource = panfrost_flush_resource;
634 pctx->invalidate_resource = panfrost_invalidate_resource;
635 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
636 pctx->buffer_subdata = u_default_buffer_subdata;
637 pctx->texture_subdata = u_default_texture_subdata;
638 }