panfrost: Move BO meta-data out of panfrost_bo
[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->slices[0].stride = whandle->stride;
75 rsc->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->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->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 static struct pipe_resource *
184 panfrost_create_scanout_res(struct pipe_screen *screen,
185 const struct pipe_resource *template)
186 {
187 struct panfrost_screen *pscreen = pan_screen(screen);
188 struct pipe_resource scanout_templat = *template;
189 struct renderonly_scanout *scanout;
190 struct winsys_handle handle;
191 struct pipe_resource *res;
192
193 scanout = renderonly_scanout_for_resource(&scanout_templat,
194 pscreen->ro, &handle);
195 if (!scanout)
196 return NULL;
197
198 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
199 /* TODO: handle modifiers? */
200 res = screen->resource_from_handle(screen, template, &handle,
201 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
202 close(handle.handle);
203 if (!res)
204 return NULL;
205
206 struct panfrost_resource *pres = pan_resource(res);
207
208 pres->scanout = scanout;
209 pscreen->display_target = pres;
210
211 return res;
212 }
213
214 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile */
215
216 #define CHECKSUM_TILE_WIDTH 16
217 #define CHECKSUM_TILE_HEIGHT 16
218 #define CHECKSUM_BYTES_PER_TILE 8
219
220 static unsigned
221 panfrost_compute_checksum_sizes(
222 struct panfrost_slice *slice,
223 unsigned width,
224 unsigned height)
225 {
226 unsigned aligned_width = ALIGN(width, CHECKSUM_TILE_WIDTH);
227 unsigned aligned_height = ALIGN(height, CHECKSUM_TILE_HEIGHT);
228
229 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
230 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
231
232 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
233
234 return slice->checksum_stride * tile_count_y;
235 }
236
237 /* Setup the mip tree given a particular layout, possibly with checksumming */
238
239 static void
240 panfrost_setup_slices(struct panfrost_resource *pres, size_t *bo_size)
241 {
242 struct pipe_resource *res = &pres->base;
243 unsigned width = res->width0;
244 unsigned height = res->height0;
245 unsigned depth = res->depth0;
246 unsigned bytes_per_pixel = util_format_get_blocksize(res->format);
247
248 assert(depth > 0);
249
250 /* Tiled operates blockwise; linear is packed. Also, anything
251 * we render to has to be tile-aligned. Maybe not strictly
252 * necessary, but we're not *that* pressed for memory and it
253 * makes code a lot simpler */
254
255 bool renderable = res->bind &
256 (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL);
257 bool afbc = pres->layout == PAN_AFBC;
258 bool tiled = pres->layout == PAN_TILED;
259 bool should_align = renderable || tiled;
260
261 /* We don't know how to specify a 2D stride for 3D textures */
262
263 bool can_align_stride =
264 res->target != PIPE_TEXTURE_3D;
265
266 should_align &= can_align_stride;
267
268 unsigned offset = 0;
269 unsigned size_2d = 0;
270
271 for (unsigned l = 0; l <= res->last_level; ++l) {
272 struct panfrost_slice *slice = &pres->slices[l];
273
274 unsigned effective_width = width;
275 unsigned effective_height = height;
276 unsigned effective_depth = depth;
277
278 if (should_align) {
279 effective_width = ALIGN(effective_width, 16);
280 effective_height = ALIGN(effective_height, 16);
281
282 /* We don't need to align depth */
283 }
284
285 slice->offset = offset;
286
287 /* Compute the would-be stride */
288 unsigned stride = bytes_per_pixel * effective_width;
289
290 /* ..but cache-line align it for performance */
291 if (can_align_stride && pres->layout == PAN_LINEAR)
292 stride = ALIGN(stride, 64);
293
294 slice->stride = stride;
295
296 unsigned slice_one_size = slice->stride * effective_height;
297 unsigned slice_full_size = slice_one_size * effective_depth;
298
299 /* Report 2D size for 3D texturing */
300
301 if (l == 0)
302 size_2d = slice_one_size;
303
304 /* Compute AFBC sizes if necessary */
305 if (afbc) {
306 slice->header_size =
307 panfrost_afbc_header_size(width, height);
308
309 offset += slice->header_size;
310 }
311
312 offset += slice_full_size;
313
314 /* Add a checksum region if necessary */
315 if (pres->checksummed) {
316 slice->checksum_offset = offset;
317
318 unsigned size = panfrost_compute_checksum_sizes(
319 slice, width, height);
320
321 offset += size;
322 }
323
324 width = u_minify(width, 1);
325 height = u_minify(height, 1);
326 depth = u_minify(depth, 1);
327 }
328
329 assert(res->array_size);
330
331 if (res->target != PIPE_TEXTURE_3D) {
332 /* Arrays and cubemaps have the entire miptree duplicated */
333
334 pres->cubemap_stride = ALIGN(offset, 64);
335 *bo_size = ALIGN(pres->cubemap_stride * res->array_size, 4096);
336 } else {
337 /* 3D strides across the 2D layers */
338 assert(res->array_size == 1);
339
340 pres->cubemap_stride = size_2d;
341 *bo_size = ALIGN(offset, 4096);
342 }
343 }
344
345 static void
346 panfrost_resource_create_bo(struct panfrost_screen *screen, struct panfrost_resource *pres)
347 {
348 struct pipe_resource *res = &pres->base;
349
350 /* Based on the usage, figure out what storing will be used. There are
351 * various tradeoffs:
352 *
353 * Linear: the basic format, bad for memory bandwidth, bad for cache
354 * use. Zero-copy, though. Renderable.
355 *
356 * Tiled: Not compressed, but cache-optimized. Expensive to write into
357 * (due to software tiling), but cheap to sample from. Ideal for most
358 * textures.
359 *
360 * AFBC: Compressed and renderable (so always desirable for non-scanout
361 * rendertargets). Cheap to sample from. The format is black box, so we
362 * can't read/write from software.
363 */
364
365 /* Tiling textures is almost always faster, unless we only use it once */
366
367 bool is_texture = (res->bind & PIPE_BIND_SAMPLER_VIEW);
368 bool is_2d = res->depth0 == 1 && res->array_size == 1;
369 bool is_streaming = (res->usage != PIPE_USAGE_STREAM);
370
371 bool should_tile = is_streaming && is_texture && is_2d;
372
373 /* Depth/stencil can't be tiled, only linear or AFBC */
374 should_tile &= !(res->bind & PIPE_BIND_DEPTH_STENCIL);
375
376 /* FBOs we would like to checksum, if at all possible */
377 bool can_checksum = !(res->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED));
378 bool should_checksum = res->bind & PIPE_BIND_RENDER_TARGET;
379
380 pres->checksummed = can_checksum && should_checksum;
381
382 /* Set the layout appropriately */
383 pres->layout = should_tile ? PAN_TILED : PAN_LINEAR;
384
385 size_t bo_size;
386
387 panfrost_setup_slices(pres, &bo_size);
388
389 struct panfrost_memory mem;
390 struct panfrost_bo *bo = rzalloc(screen, struct panfrost_bo);
391
392 pipe_reference_init(&bo->reference, 1);
393 panfrost_drm_allocate_slab(screen, &mem, bo_size / 4096, true, 0, 0, 0);
394
395 bo->cpu = mem.cpu;
396 bo->gpu = mem.gpu;
397 bo->gem_handle = mem.gem_handle;
398 bo->size = bo_size;
399 pres->bo = bo;
400 }
401
402 static struct pipe_resource *
403 panfrost_resource_create(struct pipe_screen *screen,
404 const struct pipe_resource *template)
405 {
406 /* Make sure we're familiar */
407 switch (template->target) {
408 case PIPE_BUFFER:
409 case PIPE_TEXTURE_1D:
410 case PIPE_TEXTURE_2D:
411 case PIPE_TEXTURE_3D:
412 case PIPE_TEXTURE_CUBE:
413 case PIPE_TEXTURE_RECT:
414 case PIPE_TEXTURE_2D_ARRAY:
415 break;
416 default:
417 DBG("Unknown texture target %d\n", template->target);
418 assert(0);
419 }
420
421 if (template->bind &
422 (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))
423 return panfrost_create_scanout_res(screen, template);
424
425 struct panfrost_resource *so = rzalloc(screen, struct panfrost_resource);
426 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
427
428 so->base = *template;
429 so->base.screen = screen;
430
431 pipe_reference_init(&so->base.reference, 1);
432
433 util_range_init(&so->valid_buffer_range);
434
435 panfrost_resource_create_bo(pscreen, so);
436 return (struct pipe_resource *)so;
437 }
438
439 static void
440 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *bo)
441 {
442 struct panfrost_memory mem = {
443 .cpu = bo->cpu,
444 .gpu = bo->gpu,
445 .size = bo->size,
446 .gem_handle = bo->gem_handle,
447 };
448
449 panfrost_drm_free_slab(screen, &mem);
450 ralloc_free(bo);
451 }
452
453 void
454 panfrost_bo_reference(struct panfrost_bo *bo)
455 {
456 pipe_reference(NULL, &bo->reference);
457 }
458
459 void
460 panfrost_bo_unreference(struct pipe_screen *screen, struct panfrost_bo *bo)
461 {
462 /* When the reference count goes to zero, we need to cleanup */
463
464 if (pipe_reference(&bo->reference, NULL)) {
465 panfrost_destroy_bo(pan_screen(screen), bo);
466 }
467 }
468
469 static void
470 panfrost_resource_destroy(struct pipe_screen *screen,
471 struct pipe_resource *pt)
472 {
473 struct panfrost_screen *pscreen = pan_screen(screen);
474 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
475
476 if (rsrc->scanout)
477 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
478
479 if (rsrc->bo)
480 panfrost_bo_unreference(screen, rsrc->bo);
481
482 util_range_destroy(&rsrc->valid_buffer_range);
483 ralloc_free(rsrc);
484 }
485
486 static void *
487 panfrost_transfer_map(struct pipe_context *pctx,
488 struct pipe_resource *resource,
489 unsigned level,
490 unsigned usage, /* a combination of PIPE_TRANSFER_x */
491 const struct pipe_box *box,
492 struct pipe_transfer **out_transfer)
493 {
494 int bytes_per_pixel = util_format_get_blocksize(resource->format);
495 struct panfrost_resource *rsrc = pan_resource(resource);
496 struct panfrost_bo *bo = rsrc->bo;
497
498 struct panfrost_gtransfer *transfer = rzalloc(pctx, struct panfrost_gtransfer);
499 transfer->base.level = level;
500 transfer->base.usage = usage;
501 transfer->base.box = *box;
502
503 pipe_resource_reference(&transfer->base.resource, resource);
504
505 *out_transfer = &transfer->base;
506
507 /* Check if we're bound for rendering and this is a read pixels. If so,
508 * we need to flush */
509
510 struct panfrost_context *ctx = pan_context(pctx);
511 struct pipe_framebuffer_state *fb = &ctx->pipe_framebuffer;
512
513 bool is_bound = false;
514
515 for (unsigned c = 0; c < fb->nr_cbufs; ++c) {
516 is_bound |= fb->cbufs[c]->texture == resource;
517 }
518
519 if (is_bound && (usage & PIPE_TRANSFER_READ)) {
520 assert(level == 0);
521 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
522 }
523
524 /* TODO: Respect usage flags */
525
526 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
527 /* TODO: reallocate */
528 //printf("debug: Missed reallocate\n");
529 } else if ((usage & PIPE_TRANSFER_WRITE)
530 && resource->target == PIPE_BUFFER
531 && !util_ranges_intersect(&rsrc->valid_buffer_range, box->x, box->x + box->width)) {
532 /* No flush for writes to uninitialized */
533 } else if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
534 if (usage & PIPE_TRANSFER_WRITE) {
535 /* STUB: flush reading */
536 //printf("debug: missed reading flush %d\n", resource->target);
537 } else if (usage & PIPE_TRANSFER_READ) {
538 /* STUB: flush writing */
539 //printf("debug: missed writing flush %d (%d-%d)\n", resource->target, box->x, box->x + box->width);
540 } else {
541 /* Why are you even mapping?! */
542 }
543 }
544
545 if (rsrc->layout != PAN_LINEAR) {
546 /* Non-linear resources need to be indirectly mapped */
547
548 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
549 return NULL;
550
551 transfer->base.stride = box->width * bytes_per_pixel;
552 transfer->base.layer_stride = transfer->base.stride * box->height;
553 transfer->map = rzalloc_size(transfer, transfer->base.layer_stride * box->depth);
554 assert(box->depth == 1);
555
556 if ((usage & PIPE_TRANSFER_READ) && rsrc->slices[level].initialized) {
557 if (rsrc->layout == PAN_AFBC) {
558 DBG("Unimplemented: reads from AFBC");
559 } else if (rsrc->layout == PAN_TILED) {
560 panfrost_load_tiled_image(
561 transfer->map,
562 bo->cpu + rsrc->slices[level].offset,
563 box,
564 transfer->base.stride,
565 rsrc->slices[level].stride,
566 util_format_get_blocksize(resource->format));
567 }
568 }
569
570 return transfer->map;
571 } else {
572 transfer->base.stride = rsrc->slices[level].stride;
573 transfer->base.layer_stride = rsrc->cubemap_stride;
574
575 /* By mapping direct-write, we're implicitly already
576 * initialized (maybe), so be conservative */
577
578 if ((usage & PIPE_TRANSFER_WRITE) && (usage & PIPE_TRANSFER_MAP_DIRECTLY))
579 rsrc->slices[level].initialized = true;
580
581 return bo->cpu
582 + rsrc->slices[level].offset
583 + transfer->base.box.z * rsrc->cubemap_stride
584 + transfer->base.box.y * rsrc->slices[level].stride
585 + transfer->base.box.x * bytes_per_pixel;
586 }
587 }
588
589 static void
590 panfrost_transfer_unmap(struct pipe_context *pctx,
591 struct pipe_transfer *transfer)
592 {
593 /* Gallium expects writeback here, so we tile */
594
595 struct panfrost_gtransfer *trans = pan_transfer(transfer);
596 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
597
598 if (trans->map) {
599 struct panfrost_bo *bo = prsrc->bo;
600
601 if (transfer->usage & PIPE_TRANSFER_WRITE) {
602 unsigned level = transfer->level;
603 prsrc->slices[level].initialized = true;
604
605 if (prsrc->layout == PAN_AFBC) {
606 DBG("Unimplemented: writes to AFBC\n");
607 } else if (prsrc->layout == PAN_TILED) {
608 assert(transfer->box.depth == 1);
609
610 panfrost_store_tiled_image(
611 bo->cpu + prsrc->slices[level].offset,
612 trans->map,
613 &transfer->box,
614 prsrc->slices[level].stride,
615 transfer->stride,
616 util_format_get_blocksize(prsrc->base.format));
617 }
618 }
619 }
620
621
622 util_range_add(&prsrc->valid_buffer_range,
623 transfer->box.x,
624 transfer->box.x + transfer->box.width);
625
626 /* Derefence the resource */
627 pipe_resource_reference(&transfer->resource, NULL);
628
629 /* Transfer itself is RALLOCed at the moment */
630 ralloc_free(transfer);
631 }
632
633 static void
634 panfrost_transfer_flush_region(struct pipe_context *pctx,
635 struct pipe_transfer *transfer,
636 const struct pipe_box *box)
637 {
638 struct panfrost_resource *rsc = pan_resource(transfer->resource);
639
640 if (transfer->resource->target == PIPE_BUFFER) {
641 util_range_add(&rsc->valid_buffer_range,
642 transfer->box.x + box->x,
643 transfer->box.x + box->x + box->width);
644 }
645 }
646
647 static struct pb_slab *
648 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
649 {
650 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
651 struct panfrost_memory *mem = rzalloc(screen, struct panfrost_memory);
652
653 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
654
655 mem->slab.num_entries = slab_size / entry_size;
656 mem->slab.num_free = mem->slab.num_entries;
657
658 LIST_INITHEAD(&mem->slab.free);
659 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
660 /* Create a slab entry */
661 struct panfrost_memory_entry *entry = rzalloc(mem, struct panfrost_memory_entry);
662 entry->offset = entry_size * i;
663
664 entry->base.slab = &mem->slab;
665 entry->base.group_index = group_index;
666
667 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
668 }
669
670 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
671 * special flags */
672
673 panfrost_drm_allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
674
675 return &mem->slab;
676 }
677
678 static bool
679 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
680 {
681 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
682 return p_entry->freed;
683 }
684
685 static void
686 panfrost_slab_free(void *priv, struct pb_slab *slab)
687 {
688 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
689 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
690
691 panfrost_drm_free_slab(screen, mem);
692 ralloc_free(mem);
693 }
694
695 static void
696 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
697 {
698 //DBG("TODO %s\n", __func__);
699 }
700
701 static enum pipe_format
702 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
703 {
704 return prsrc->format;
705 }
706
707 static boolean
708 panfrost_generate_mipmap(
709 struct pipe_context *pctx,
710 struct pipe_resource *prsrc,
711 enum pipe_format format,
712 unsigned base_level,
713 unsigned last_level,
714 unsigned first_layer,
715 unsigned last_layer)
716 {
717 struct panfrost_context *ctx = pan_context(pctx);
718 struct panfrost_resource *rsrc = pan_resource(prsrc);
719
720 /* Generating a mipmap invalidates the written levels, so make that
721 * explicit so we don't try to wallpaper them back and end up with
722 * u_blitter recursion */
723
724 assert(rsrc->bo);
725 for (unsigned l = base_level + 1; l <= last_level; ++l)
726 rsrc->slices[l].initialized = false;
727
728 /* Beyond that, we just delegate the hard stuff. We're careful to
729 * include flushes on both ends to make sure the data is really valid.
730 * We could be doing a lot better perf-wise, especially once we have
731 * reorder-type optimizations in place. But for now prioritize
732 * correctness. */
733
734 struct panfrost_job *job = panfrost_get_job_for_fbo(ctx);
735 bool has_draws = job->last_job.gpu;
736
737 if (has_draws)
738 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
739
740 /* We've flushed the original buffer if needed, now trigger a blit */
741
742 bool blit_res = util_gen_mipmap(
743 pctx, prsrc, format,
744 base_level, last_level,
745 first_layer, last_layer,
746 PIPE_TEX_FILTER_LINEAR);
747
748 /* If the blit was successful, flush once more. If it wasn't, well, let
749 * the state tracker deal with it. */
750
751 if (blit_res)
752 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
753
754 return blit_res;
755 }
756
757 /* Computes the address to a texture at a particular slice */
758
759 mali_ptr
760 panfrost_get_texture_address(
761 struct panfrost_resource *rsrc,
762 unsigned level, unsigned face)
763 {
764 unsigned level_offset = rsrc->slices[level].offset;
765 unsigned face_offset = face * rsrc->cubemap_stride;
766
767 return rsrc->bo->gpu + level_offset + face_offset;
768 }
769
770 static void
771 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
772 struct pipe_resource *stencil)
773 {
774 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
775 }
776
777 static struct pipe_resource *
778 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
779 {
780 return &pan_resource(prsrc)->separate_stencil->base;
781 }
782
783 static const struct u_transfer_vtbl transfer_vtbl = {
784 .resource_create = panfrost_resource_create,
785 .resource_destroy = panfrost_resource_destroy,
786 .transfer_map = panfrost_transfer_map,
787 .transfer_unmap = panfrost_transfer_unmap,
788 .transfer_flush_region = panfrost_transfer_flush_region,
789 .get_internal_format = panfrost_resource_get_internal_format,
790 .set_stencil = panfrost_resource_set_stencil,
791 .get_stencil = panfrost_resource_get_stencil,
792 };
793
794 void
795 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
796 {
797 //pscreen->base.resource_create_with_modifiers =
798 // panfrost_resource_create_with_modifiers;
799 pscreen->base.resource_create = u_transfer_helper_resource_create;
800 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
801 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
802 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
803 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
804 true, false,
805 true, true);
806
807 pb_slabs_init(&pscreen->slabs,
808 MIN_SLAB_ENTRY_SIZE,
809 MAX_SLAB_ENTRY_SIZE,
810
811 3, /* Number of heaps */
812
813 pscreen,
814
815 panfrost_slab_can_reclaim,
816 panfrost_slab_alloc,
817 panfrost_slab_free);
818 }
819
820 void
821 panfrost_resource_screen_deinit(struct panfrost_screen *pscreen)
822 {
823 pb_slabs_deinit(&pscreen->slabs);
824 }
825
826 void
827 panfrost_resource_context_init(struct pipe_context *pctx)
828 {
829 pctx->transfer_map = u_transfer_helper_transfer_map;
830 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
831 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
832 pctx->buffer_subdata = u_default_buffer_subdata;
833 pctx->create_surface = panfrost_create_surface;
834 pctx->surface_destroy = panfrost_surface_destroy;
835 pctx->resource_copy_region = util_resource_copy_region;
836 pctx->blit = panfrost_blit;
837 pctx->generate_mipmap = panfrost_generate_mipmap;
838 pctx->flush_resource = panfrost_flush_resource;
839 pctx->invalidate_resource = panfrost_invalidate_resource;
840 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
841 pctx->buffer_subdata = u_default_buffer_subdata;
842 pctx->texture_subdata = u_default_texture_subdata;
843 }