panfrost: Only tag AFBC addresses when sampling
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Authors (Collabora):
27 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
28 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
29 *
30 */
31
32 #include <xf86drm.h>
33 #include <fcntl.h>
34 #include "drm-uapi/drm_fourcc.h"
35
36 #include "state_tracker/winsys_handle.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_surface.h"
40 #include "util/u_transfer.h"
41 #include "util/u_transfer_helper.h"
42 #include "util/u_gen_mipmap.h"
43
44 #include "pan_context.h"
45 #include "pan_screen.h"
46 #include "pan_resource.h"
47 #include "pan_util.h"
48 #include "pan_tiling.h"
49
50 static struct pipe_resource *
51 panfrost_resource_from_handle(struct pipe_screen *pscreen,
52 const struct pipe_resource *templat,
53 struct winsys_handle *whandle,
54 unsigned usage)
55 {
56 struct panfrost_screen *screen = pan_screen(pscreen);
57 struct panfrost_resource *rsc;
58 struct pipe_resource *prsc;
59
60 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
61
62 rsc = rzalloc(pscreen, struct panfrost_resource);
63 if (!rsc)
64 return NULL;
65
66 prsc = &rsc->base;
67
68 *prsc = *templat;
69
70 pipe_reference_init(&prsc->reference, 1);
71 prsc->screen = pscreen;
72
73 rsc->bo = panfrost_drm_import_bo(screen, whandle);
74 rsc->bo->slices[0].stride = whandle->stride;
75 rsc->bo->slices[0].initialized = true;
76
77 if (screen->ro) {
78 rsc->scanout =
79 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
80 /* failure is expected in some cases.. */
81 }
82
83 return prsc;
84 }
85
86 static boolean
87 panfrost_resource_get_handle(struct pipe_screen *pscreen,
88 struct pipe_context *ctx,
89 struct pipe_resource *pt,
90 struct winsys_handle *handle,
91 unsigned usage)
92 {
93 struct panfrost_screen *screen = pan_screen(pscreen);
94 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
95 struct renderonly_scanout *scanout = rsrc->scanout;
96
97 handle->modifier = DRM_FORMAT_MOD_INVALID;
98
99 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
100 return FALSE;
101 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
102 if (renderonly_get_handle(scanout, handle))
103 return TRUE;
104
105 handle->handle = rsrc->bo->gem_handle;
106 handle->stride = rsrc->bo->slices[0].stride;
107 return TRUE;
108 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
109 if (scanout) {
110 struct drm_prime_handle args = {
111 .handle = scanout->handle,
112 .flags = DRM_CLOEXEC,
113 };
114
115 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
116 if (ret == -1)
117 return FALSE;
118
119 handle->stride = scanout->stride;
120 handle->handle = args.fd;
121
122 return TRUE;
123 } else
124 return panfrost_drm_export_bo(screen, rsrc->bo->gem_handle,
125 rsrc->bo->slices[0].stride,
126 handle);
127 }
128
129 return FALSE;
130 }
131
132 static void
133 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
134 {
135 //DBG("TODO %s\n", __func__);
136 }
137
138 static struct pipe_surface *
139 panfrost_create_surface(struct pipe_context *pipe,
140 struct pipe_resource *pt,
141 const struct pipe_surface *surf_tmpl)
142 {
143 struct pipe_surface *ps = NULL;
144
145 ps = rzalloc(pipe, struct pipe_surface);
146
147 if (ps) {
148 pipe_reference_init(&ps->reference, 1);
149 pipe_resource_reference(&ps->texture, pt);
150 ps->context = pipe;
151 ps->format = surf_tmpl->format;
152
153 if (pt->target != PIPE_BUFFER) {
154 assert(surf_tmpl->u.tex.level <= pt->last_level);
155 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
156 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
157 ps->u.tex.level = surf_tmpl->u.tex.level;
158 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
159 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
160 } else {
161 /* setting width as number of elements should get us correct renderbuffer width */
162 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
163 ps->height = pt->height0;
164 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
165 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
166 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
167 assert(ps->u.buf.last_element < ps->width);
168 }
169 }
170
171 return ps;
172 }
173
174 static void
175 panfrost_surface_destroy(struct pipe_context *pipe,
176 struct pipe_surface *surf)
177 {
178 assert(surf->texture);
179 pipe_resource_reference(&surf->texture, NULL);
180 ralloc_free(surf);
181 }
182
183 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile */
184
185 #define CHECKSUM_TILE_WIDTH 16
186 #define CHECKSUM_TILE_HEIGHT 16
187 #define CHECKSUM_BYTES_PER_TILE 8
188
189 static unsigned
190 panfrost_compute_checksum_sizes(
191 struct panfrost_slice *slice,
192 unsigned width,
193 unsigned height)
194 {
195 unsigned aligned_width = ALIGN(width, CHECKSUM_TILE_WIDTH);
196 unsigned aligned_height = ALIGN(height, CHECKSUM_TILE_HEIGHT);
197
198 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
199 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
200
201 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
202
203 return slice->checksum_stride * tile_count_y;
204 }
205
206 /* Setup the mip tree given a particular layout, possibly with checksumming */
207
208 static void
209 panfrost_setup_slices(const struct pipe_resource *tmpl, struct panfrost_bo *bo)
210 {
211 unsigned width = tmpl->width0;
212 unsigned height = tmpl->height0;
213 unsigned depth = tmpl->depth0;
214 unsigned bytes_per_pixel = util_format_get_blocksize(tmpl->format);
215
216 assert(depth > 0);
217
218 /* Tiled operates blockwise; linear is packed. Also, anything
219 * we render to has to be tile-aligned. Maybe not strictly
220 * necessary, but we're not *that* pressed for memory and it
221 * makes code a lot simpler */
222
223 bool renderable = tmpl->bind &
224 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL);
225 bool afbc = bo->layout == PAN_AFBC;
226 bool tiled = bo->layout == PAN_TILED;
227 bool should_align = renderable || tiled;
228
229 /* We don't know how to specify a 2D stride for 3D textures */
230
231 bool can_align_stride =
232 tmpl->target != PIPE_TEXTURE_3D;
233
234 should_align &= can_align_stride;
235
236 unsigned offset = 0;
237 unsigned size_2d = 0;
238
239 for (unsigned l = 0; l <= tmpl->last_level; ++l) {
240 struct panfrost_slice *slice = &bo->slices[l];
241
242 unsigned effective_width = width;
243 unsigned effective_height = height;
244 unsigned effective_depth = depth;
245
246 if (should_align) {
247 effective_width = ALIGN(effective_width, 16);
248 effective_height = ALIGN(effective_height, 16);
249
250 /* We don't need to align depth */
251 }
252
253 slice->offset = offset;
254
255 /* Compute the would-be stride */
256 unsigned stride = bytes_per_pixel * effective_width;
257
258 /* ..but cache-line align it for performance */
259 if (can_align_stride && bo->layout == PAN_LINEAR)
260 stride = ALIGN(stride, 64);
261
262 slice->stride = stride;
263
264 unsigned slice_one_size = slice->stride * effective_height;
265 unsigned slice_full_size = slice_one_size * effective_depth;
266
267 /* Report 2D size for 3D texturing */
268
269 if (l == 0)
270 size_2d = slice_one_size;
271
272 /* Compute AFBC sizes if necessary */
273 if (afbc) {
274 slice->header_size =
275 panfrost_afbc_header_size(width, height);
276
277 offset += slice->header_size;
278 }
279
280 offset += slice_full_size;
281
282 /* Add a checksum region if necessary */
283 if (bo->checksummed) {
284 slice->checksum_offset = offset;
285
286 unsigned size = panfrost_compute_checksum_sizes(
287 slice, width, height);
288
289 offset += size;
290 }
291
292 width = u_minify(width, 1);
293 height = u_minify(height, 1);
294 depth = u_minify(depth, 1);
295 }
296
297 assert(tmpl->array_size);
298
299 if (tmpl->target != PIPE_TEXTURE_3D) {
300 /* Arrays and cubemaps have the entire miptree duplicated */
301
302 bo->cubemap_stride = ALIGN(offset, 64);
303 bo->size = ALIGN(bo->cubemap_stride * tmpl->array_size, 4096);
304 } else {
305 /* 3D strides across the 2D layers */
306 assert(tmpl->array_size == 1);
307
308 bo->cubemap_stride = size_2d;
309 bo->size = ALIGN(offset, 4096);
310 }
311 }
312
313 static struct panfrost_bo *
314 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
315 {
316 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
317 pipe_reference_init(&bo->reference, 1);
318
319 /* Based on the usage, figure out what storing will be used. There are
320 * various tradeoffs:
321 *
322 * Linear: the basic format, bad for memory bandwidth, bad for cache
323 * use. Zero-copy, though. Renderable.
324 *
325 * Tiled: Not compressed, but cache-optimized. Expensive to write into
326 * (due to software tiling), but cheap to sample from. Ideal for most
327 * textures.
328 *
329 * AFBC: Compressed and renderable (so always desirable for non-scanout
330 * rendertargets). Cheap to sample from. The format is black box, so we
331 * can't read/write from software.
332 */
333
334 /* Tiling textures is almost always faster, unless we only use it once */
335
336 bool is_texture = (template->bind & PIPE_BIND_SAMPLER_VIEW);
337 bool is_2d = template->depth0 == 1 && template->array_size == 1;
338 bool is_streaming = (template->usage != PIPE_USAGE_STREAM);
339
340 bool should_tile = is_streaming && is_texture && is_2d;
341
342 /* Depth/stencil can't be tiled, only linear or AFBC */
343 should_tile &= !(template->bind & PIPE_BIND_DEPTH_STENCIL);
344
345 /* FBOs we would like to checksum, if at all possible */
346 bool can_checksum = !(template->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED));
347 bool should_checksum = template->bind & PIPE_BIND_RENDER_TARGET;
348
349 bo->checksummed = can_checksum && should_checksum;
350
351 /* Set the layout appropriately */
352 bo->layout = should_tile ? PAN_TILED : PAN_LINEAR;
353
354 panfrost_setup_slices(template, bo);
355
356 struct panfrost_memory mem;
357
358 panfrost_drm_allocate_slab(screen, &mem, bo->size / 4096, true, 0, 0, 0);
359
360 bo->cpu = mem.cpu;
361 bo->gpu = mem.gpu;
362 bo->gem_handle = mem.gem_handle;
363
364 return bo;
365 }
366
367 static struct pipe_resource *
368 panfrost_resource_create(struct pipe_screen *screen,
369 const struct pipe_resource *template)
370 {
371 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
372 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
373
374 so->base = *template;
375 so->base.screen = screen;
376
377 pipe_reference_init(&so->base.reference, 1);
378
379 /* Make sure we're familiar */
380 switch (template->target) {
381 case PIPE_BUFFER:
382 case PIPE_TEXTURE_1D:
383 case PIPE_TEXTURE_2D:
384 case PIPE_TEXTURE_3D:
385 case PIPE_TEXTURE_CUBE:
386 case PIPE_TEXTURE_RECT:
387 case PIPE_TEXTURE_2D_ARRAY:
388 break;
389 default:
390 DBG("Unknown texture target %d\n", template->target);
391 assert(0);
392 }
393
394 util_range_init(&so->valid_buffer_range);
395
396 if (template->bind & PIPE_BIND_DISPLAY_TARGET ||
397 template->bind & PIPE_BIND_SCANOUT ||
398 template->bind & PIPE_BIND_SHARED) {
399 struct pipe_resource scanout_templat = *template;
400 struct renderonly_scanout *scanout;
401 struct winsys_handle handle;
402
403 scanout = renderonly_scanout_for_resource(&scanout_templat,
404 pscreen->ro, &handle);
405 if (!scanout)
406 return NULL;
407
408 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
409 /* TODO: handle modifiers? */
410 so = pan_resource(screen->resource_from_handle(screen, template,
411 &handle,
412 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
413 close(handle.handle);
414 if (!so)
415 return NULL;
416
417 so->scanout = scanout;
418 pscreen->display_target = so;
419 } else {
420 so->bo = panfrost_create_bo(pscreen, template);
421 }
422
423 return (struct pipe_resource *)so;
424 }
425
426 static void
427 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
428 {
429 if (bo->imported) {
430 panfrost_drm_free_imported_bo(screen, bo);
431 } else {
432 struct panfrost_memory mem = {
433 .cpu = bo->cpu,
434 .gpu = bo->gpu,
435 .size = bo->size,
436 .gem_handle = bo->gem_handle,
437 };
438
439 panfrost_drm_free_slab(screen, &mem);
440 }
441
442 ralloc_free(bo);
443 }
444
445 void
446 panfrost_bo_reference(struct panfrost_bo *bo)
447 {
448 pipe_reference(NULL, &bo->reference);
449 }
450
451 void
452 panfrost_bo_unreference(struct pipe_screen *screen, struct panfrost_bo *bo)
453 {
454 /* When the reference count goes to zero, we need to cleanup */
455
456 if (pipe_reference(&bo->reference, NULL)) {
457 panfrost_destroy_bo(pan_screen(screen), bo);
458 }
459 }
460
461 static void
462 panfrost_resource_destroy(struct pipe_screen *screen,
463 struct pipe_resource *pt)
464 {
465 struct panfrost_screen *pscreen = pan_screen(screen);
466 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
467
468 if (rsrc->scanout)
469 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
470
471 if (rsrc->bo)
472 panfrost_bo_unreference(screen, rsrc->bo);
473
474 util_range_destroy(&rsrc->valid_buffer_range);
475 ralloc_free(rsrc);
476 }
477
478 static void *
479 panfrost_transfer_map(struct pipe_context *pctx,
480 struct pipe_resource *resource,
481 unsigned level,
482 unsigned usage, /* a combination of PIPE_TRANSFER_x */
483 const struct pipe_box *box,
484 struct pipe_transfer **out_transfer)
485 {
486 int bytes_per_pixel = util_format_get_blocksize(resource->format);
487 struct panfrost_resource *rsrc = pan_resource(resource);
488 struct panfrost_bo *bo = rsrc->bo;
489
490 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
491 transfer->base.level = level;
492 transfer->base.usage = usage;
493 transfer->base.box = *box;
494
495 pipe_resource_reference(&transfer->base.resource, resource);
496
497 *out_transfer = &transfer->base;
498
499 /* Check if we're bound for rendering and this is a read pixels. If so,
500 * we need to flush */
501
502 struct panfrost_context *ctx = pan_context(pctx);
503 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
504
505 bool is_bound = false;
506
507 for (unsigned c = 0; c < fb->nr_cbufs; ++c) {
508 is_bound |= fb->cbufs[c]->texture == resource;
509 }
510
511 if (is_bound && (usage & PIPE_TRANSFER_READ)) {
512 assert(level == 0);
513 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
514 }
515
516 /* TODO: Respect usage flags */
517
518 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
519 /* TODO: reallocate */
520 //printf("debug: Missed reallocate\n");
521 } else if ((usage & PIPE_TRANSFER_WRITE)
522 && resource->target == PIPE_BUFFER
523 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
524 /* No flush for writes to uninitialized */
525 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
526 if (usage & PIPE_TRANSFER_WRITE) {
527 /* STUB: flush reading */
528 //printf("debug: missed reading flush %d\n", resource->target);
529 } else if (usage & PIPE_TRANSFER_READ) {
530 /* STUB: flush writing */
531 //printf("debug: missed writing flush %d (%d-%d)\n", resource->target, box->x, box->x + box->width);
532 } else {
533 /* Why are you even mapping?! */
534 }
535 }
536
537 if (bo->layout != PAN_LINEAR) {
538 /* Non-linear resources need to be indirectly mapped */
539
540 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
541 return NULL;
542
543 transfer->base.stride = box->width * bytes_per_pixel;
544 transfer->base.layer_stride = transfer->base.stride * box->height;
545 transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
546 assert(box->depth == 1);
547
548 if ((usage & PIPE_TRANSFER_READ) && bo->slices[level].initialized) {
549 if (bo->layout == PAN_AFBC) {
550 DBG("Unimplemented: reads from AFBC");
551 } else if (bo->layout == PAN_TILED) {
552 panfrost_load_tiled_image(
553 transfer->map,
554 bo->cpu + bo->slices[level].offset,
555 box,
556 transfer->base.stride,
557 bo->slices[level].stride,
558 util_format_get_blocksize(resource->format));
559 }
560 }
561
562 return transfer->map;
563 } else {
564 transfer->base.stride = bo->slices[level].stride;
565 transfer->base.layer_stride = bo->cubemap_stride;
566
567 /* By mapping direct-write, we're implicitly already
568 * initialized (maybe), so be conservative */
569
570 if ((usage & PIPE_TRANSFER_WRITE) && (usage & PIPE_TRANSFER_MAP_DIRECTLY))
571 bo->slices[level].initialized = true;
572
573 return bo->cpu
574 + bo->slices[level].offset
575 + transfer->base.box.z * bo->cubemap_stride
576 + transfer->base.box.y * bo->slices[level].stride
577 + transfer->base.box.x * bytes_per_pixel;
578 }
579 }
580
581 static void
582 panfrost_transfer_unmap(struct pipe_context *pctx,
583 struct pipe_transfer *transfer)
584 {
585 /* Gallium expects writeback here, so we tile */
586
587 struct panfrost_gtransfer *trans = pan_transfer(transfer);
588 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
589
590 if (trans->map) {
591 struct panfrost_bo *bo = prsrc->bo;
592
593 if (transfer->usage & PIPE_TRANSFER_WRITE) {
594 unsigned level = transfer->level;
595 bo->slices[level].initialized = true;
596
597 if (bo->layout == PAN_AFBC) {
598 DBG("Unimplemented: writes to AFBC\n");
599 } else if (bo->layout == PAN_TILED) {
600 assert(transfer->box.depth == 1);
601
602 panfrost_store_tiled_image(
603 bo->cpu + bo->slices[level].offset,
604 trans->map,
605 &transfer->box,
606 bo->slices[level].stride,
607 transfer->stride,
608 util_format_get_blocksize(prsrc->base.format));
609 }
610 }
611 }
612
613
614 util_range_add(&prsrc->valid_buffer_range,
615 transfer->box.x,
616 transfer->box.x + transfer->box.width);
617
618 /* Derefence the resource */
619 pipe_resource_reference(&transfer->resource, NULL);
620
621 /* Transfer itself is RALLOCed at the moment */
622 ralloc_free(transfer);
623 }
624
625 static void
626 panfrost_transfer_flush_region(struct pipe_context *pctx,
627 struct pipe_transfer *transfer,
628 const struct pipe_box *box)
629 {
630 struct panfrost_resource *rsc = pan_resource(transfer->resource);
631
632 if (transfer->resource->target == PIPE_BUFFER) {
633 util_range_add(&rsc->valid_buffer_range,
634 transfer->box.x + box->x,
635 transfer->box.x + box->x + box->width);
636 }
637 }
638
639 static struct pb_slab *
640 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
641 {
642 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
643 struct panfrost_memory *mem = rzalloc(screen, struct panfrost_memory);
644
645 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
646
647 mem->slab.num_entries = slab_size / entry_size;
648 mem->slab.num_free = mem->slab.num_entries;
649
650 LIST_INITHEAD(&mem->slab.free);
651 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
652 /* Create a slab entry */
653 struct panfrost_memory_entry *entry = rzalloc(mem, struct panfrost_memory_entry);
654 entry->offset = entry_size * i;
655
656 entry->base.slab = &mem->slab;
657 entry->base.group_index = group_index;
658
659 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
660 }
661
662 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
663 * special flags */
664
665 panfrost_drm_allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
666
667 return &mem->slab;
668 }
669
670 static bool
671 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
672 {
673 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
674 return p_entry->freed;
675 }
676
677 static void
678 panfrost_slab_free(void *priv, struct pb_slab *slab)
679 {
680 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
681 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
682
683 panfrost_drm_free_slab(screen, mem);
684 ralloc_free(mem);
685 }
686
687 static void
688 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
689 {
690 //DBG("TODO %s\n", __func__);
691 }
692
693 static enum pipe_format
694 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
695 {
696 return prsrc->format;
697 }
698
699 static boolean
700 panfrost_generate_mipmap(
701 struct pipe_context *pctx,
702 struct pipe_resource *prsrc,
703 enum pipe_format format,
704 unsigned base_level,
705 unsigned last_level,
706 unsigned first_layer,
707 unsigned last_layer)
708 {
709 struct panfrost_context *ctx = pan_context(pctx);
710 struct panfrost_resource *rsrc = pan_resource(prsrc);
711
712 /* Generating a mipmap invalidates the written levels, so make that
713 * explicit so we don't try to wallpaper them back and end up with
714 * u_blitter recursion */
715
716 assert(rsrc->bo);
717 for (unsigned l = base_level + 1; l <= last_level; ++l)
718 rsrc->bo->slices[l].initialized = false;
719
720 /* Beyond that, we just delegate the hard stuff. We're careful to
721 * include flushes on both ends to make sure the data is really valid.
722 * We could be doing a lot better perf-wise, especially once we have
723 * reorder-type optimizations in place. But for now prioritize
724 * correctness. */
725
726 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
727 bool has_draws = job->last_job.gpu;
728
729 if (has_draws)
730 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
731
732 /* We've flushed the original buffer if needed, now trigger a blit */
733
734 bool blit_res = util_gen_mipmap(
735 pctx, prsrc, format,
736 base_level, last_level,
737 first_layer, last_layer,
738 PIPE_TEX_FILTER_LINEAR);
739
740 /* If the blit was successful, flush once more. If it wasn't, well, let
741 * the state tracker deal with it. */
742
743 if (blit_res)
744 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
745
746 return blit_res;
747 }
748
749 /* Computes the address to a texture at a particular slice */
750
751 mali_ptr
752 panfrost_get_texture_address(
753 struct panfrost_resource *rsrc,
754 unsigned level, unsigned face)
755 {
756 unsigned level_offset = rsrc->bo->slices[level].offset;
757 unsigned face_offset = face * rsrc->bo->cubemap_stride;
758
759 return rsrc->bo->gpu + level_offset + face_offset;
760 }
761
762 static void
763 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
764 struct pipe_resource *stencil)
765 {
766 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
767 }
768
769 static struct pipe_resource *
770 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
771 {
772 return &pan_resource(prsrc)->separate_stencil->base;
773 }
774
775 static const struct u_transfer_vtbl transfer_vtbl = {
776 .resource_create = panfrost_resource_create,
777 .resource_destroy = panfrost_resource_destroy,
778 .transfer_map = panfrost_transfer_map,
779 .transfer_unmap = panfrost_transfer_unmap,
780 .transfer_flush_region = panfrost_transfer_flush_region,
781 .get_internal_format = panfrost_resource_get_internal_format,
782 .set_stencil = panfrost_resource_set_stencil,
783 .get_stencil = panfrost_resource_get_stencil,
784 };
785
786 void
787 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
788 {
789 //pscreen->base.resource_create_with_modifiers =
790 // panfrost_resource_create_with_modifiers;
791 pscreen->base.resource_create = u_transfer_helper_resource_create;
792 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
793 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
794 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
795 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
796 true, false,
797 true, true);
798
799 pb_slabs_init(&pscreen->slabs,
800 MIN_SLAB_ENTRY_SIZE,
801 MAX_SLAB_ENTRY_SIZE,
802
803 3, /* Number of heaps */
804
805 pscreen,
806
807 panfrost_slab_can_reclaim,
808 panfrost_slab_alloc,
809 panfrost_slab_free);
810 }
811
812 void
813 panfrost_resource_screen_deinit(struct panfrost_screen *pscreen)
814 {
815 pb_slabs_deinit(&pscreen->slabs);
816 }
817
818 void
819 panfrost_resource_context_init(struct pipe_context *pctx)
820 {
821 pctx->transfer_map = u_transfer_helper_transfer_map;
822 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
823 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
824 pctx->buffer_subdata = u_default_buffer_subdata;
825 pctx->create_surface = panfrost_create_surface;
826 pctx->surface_destroy = panfrost_surface_destroy;
827 pctx->resource_copy_region = util_resource_copy_region;
828 pctx->blit = panfrost_blit;
829 pctx->generate_mipmap = panfrost_generate_mipmap;
830 pctx->flush_resource = panfrost_flush_resource;
831 pctx->invalidate_resource = panfrost_invalidate_resource;
832 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
833 pctx->buffer_subdata = u_default_buffer_subdata;
834 pctx->texture_subdata = u_default_texture_subdata;
835 }