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