2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019 Collabora
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:
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
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
26 * Authors (Collabora):
27 * Tomeu Vizoso <tomeu.vizoso@collabora.com>
28 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
34 #include "drm-uapi/drm_fourcc.h"
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"
43 #include "pan_context.h"
44 #include "pan_screen.h"
45 #include "pan_resource.h"
46 #include "pan_swizzle.h"
49 static struct pipe_resource
*
50 panfrost_resource_from_handle(struct pipe_screen
*pscreen
,
51 const struct pipe_resource
*templat
,
52 struct winsys_handle
*whandle
,
55 struct panfrost_screen
*screen
= pan_screen(pscreen
);
56 struct panfrost_resource
*rsc
;
57 struct pipe_resource
*prsc
;
59 assert(whandle
->type
== WINSYS_HANDLE_TYPE_FD
);
61 rsc
= CALLOC_STRUCT(panfrost_resource
);
69 pipe_reference_init(&prsc
->reference
, 1);
70 prsc
->screen
= pscreen
;
72 rsc
->bo
= screen
->driver
->import_bo(screen
, whandle
);
73 rsc
->bo
->slices
[0].stride
= whandle
->stride
;
77 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
78 /* failure is expected in some cases.. */
85 panfrost_resource_get_handle(struct pipe_screen
*pscreen
,
86 struct pipe_context
*ctx
,
87 struct pipe_resource
*pt
,
88 struct winsys_handle
*handle
,
91 struct panfrost_screen
*screen
= pan_screen(pscreen
);
92 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
93 struct renderonly_scanout
*scanout
= rsrc
->scanout
;
95 handle
->modifier
= DRM_FORMAT_MOD_INVALID
;
97 if (handle
->type
== WINSYS_HANDLE_TYPE_SHARED
) {
99 } else if (handle
->type
== WINSYS_HANDLE_TYPE_KMS
) {
100 if (renderonly_get_handle(scanout
, handle
))
103 handle
->handle
= rsrc
->bo
->gem_handle
;
104 handle
->stride
= rsrc
->bo
->slices
[0].stride
;
106 } else if (handle
->type
== WINSYS_HANDLE_TYPE_FD
) {
108 struct drm_prime_handle args
= {
109 .handle
= scanout
->handle
,
110 .flags
= DRM_CLOEXEC
,
113 int ret
= drmIoctl(screen
->ro
->kms_fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
117 handle
->stride
= scanout
->stride
;
118 handle
->handle
= args
.fd
;
122 return screen
->driver
->export_bo(screen
, rsrc
->bo
->gem_handle
, rsrc
->bo
->slices
[0].stride
, handle
);
129 panfrost_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
131 //DBG("TODO %s\n", __func__);
134 static struct pipe_surface
*
135 panfrost_create_surface(struct pipe_context
*pipe
,
136 struct pipe_resource
*pt
,
137 const struct pipe_surface
*surf_tmpl
)
139 struct pipe_surface
*ps
= NULL
;
141 ps
= CALLOC_STRUCT(pipe_surface
);
144 pipe_reference_init(&ps
->reference
, 1);
145 pipe_resource_reference(&ps
->texture
, pt
);
147 ps
->format
= surf_tmpl
->format
;
149 if (pt
->target
!= PIPE_BUFFER
) {
150 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
151 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
152 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
153 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
154 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
155 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
157 /* setting width as number of elements should get us correct renderbuffer width */
158 ps
->width
= surf_tmpl
->u
.buf
.last_element
- surf_tmpl
->u
.buf
.first_element
+ 1;
159 ps
->height
= pt
->height0
;
160 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
161 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
162 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
163 assert(ps
->u
.buf
.last_element
< ps
->width
);
171 panfrost_surface_destroy(struct pipe_context
*pipe
,
172 struct pipe_surface
*surf
)
174 assert(surf
->texture
);
175 pipe_resource_reference(&surf
->texture
, NULL
);
180 panfrost_setup_slices(const struct pipe_resource
*tmpl
, struct panfrost_bo
*bo
)
182 unsigned width
= tmpl
->width0
;
183 unsigned height
= tmpl
->height0
;
184 unsigned bytes_per_pixel
= util_format_get_blocksize(tmpl
->format
);
188 for (unsigned l
= 0; l
<= tmpl
->last_level
; ++l
) {
189 struct panfrost_slice
*slice
= &bo
->slices
[l
];
191 unsigned effective_width
= width
;
192 unsigned effective_height
= height
;
194 /* Tiled operates blockwise; linear is packed */
196 if (bo
->layout
== PAN_TILED
) {
197 effective_width
= ALIGN(effective_width
, 16);
198 effective_height
= ALIGN(effective_height
, 16);
201 slice
->offset
= offset
;
203 /* Compute the would-be stride */
204 unsigned stride
= bytes_per_pixel
* effective_width
;
206 /* ..but cache-line align it for performance */
207 stride
= ALIGN(stride
, 64);
208 slice
->stride
= stride
;
210 offset
+= slice
->stride
* effective_height
;
212 width
= u_minify(width
, 1);
213 height
= u_minify(height
, 1);
216 assert(tmpl
->array_size
);
218 bo
->cubemap_stride
= ALIGN(offset
, 64);
219 bo
->size
= ALIGN(bo
->cubemap_stride
* tmpl
->array_size
, 4096);
222 static struct panfrost_bo
*
223 panfrost_create_bo(struct panfrost_screen
*screen
, const struct pipe_resource
*template)
225 struct panfrost_bo
*bo
= CALLOC_STRUCT(panfrost_bo
);
226 pipe_reference_init(&bo
->reference
, 1);
228 /* Based on the usage, figure out what storing will be used. There are
231 * Linear: the basic format, bad for memory bandwidth, bad for cache
232 * use. Zero-copy, though. Renderable.
234 * Tiled: Not compressed, but cache-optimized. Expensive to write into
235 * (due to software tiling), but cheap to sample from. Ideal for most
238 * AFBC: Compressed and renderable (so always desirable for non-scanout
239 * rendertargets). Cheap to sample from. The format is black box, so we
240 * can't read/write from software.
243 /* Tiling textures is almost always faster, unless we only use it once */
244 bool should_tile
= (template->usage
!= PIPE_USAGE_STREAM
) && (template->bind
& PIPE_BIND_SAMPLER_VIEW
);
246 /* For unclear reasons, depth/stencil is faster linear than AFBC, so
247 * make sure it's linear */
249 if (template->bind
& PIPE_BIND_DEPTH_STENCIL
)
252 /* Set the layout appropriately */
253 bo
->layout
= should_tile
? PAN_TILED
: PAN_LINEAR
;
255 panfrost_setup_slices(template, bo
);
257 if (bo
->layout
== PAN_TILED
|| bo
->layout
== PAN_LINEAR
) {
258 struct panfrost_memory mem
;
260 screen
->driver
->allocate_slab(screen
, &mem
, bo
->size
/ 4096, true, 0, 0, 0);
264 bo
->gem_handle
= mem
.gem_handle
;
270 static struct pipe_resource
*
271 panfrost_resource_create(struct pipe_screen
*screen
,
272 const struct pipe_resource
*template)
274 struct panfrost_resource
*so
= CALLOC_STRUCT(panfrost_resource
);
275 struct panfrost_screen
*pscreen
= (struct panfrost_screen
*) screen
;
277 so
->base
= *template;
278 so
->base
.screen
= screen
;
280 pipe_reference_init(&so
->base
.reference
, 1);
282 /* Make sure we're familiar */
283 switch (template->target
) {
285 case PIPE_TEXTURE_1D
:
286 case PIPE_TEXTURE_2D
:
287 case PIPE_TEXTURE_3D
:
288 case PIPE_TEXTURE_CUBE
:
289 case PIPE_TEXTURE_RECT
:
292 DBG("Unknown texture target %d\n", template->target
);
296 util_range_init(&so
->valid_buffer_range
);
298 if (template->bind
& PIPE_BIND_DISPLAY_TARGET
||
299 template->bind
& PIPE_BIND_SCANOUT
||
300 template->bind
& PIPE_BIND_SHARED
) {
301 struct pipe_resource scanout_templat
= *template;
302 struct renderonly_scanout
*scanout
;
303 struct winsys_handle handle
;
305 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
306 pscreen
->ro
, &handle
);
310 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
311 /* TODO: handle modifiers? */
312 so
= pan_resource(screen
->resource_from_handle(screen
, template,
314 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
315 close(handle
.handle
);
319 so
->scanout
= scanout
;
320 pscreen
->display_target
= so
;
322 so
->bo
= panfrost_create_bo(pscreen
, template);
325 return (struct pipe_resource
*)so
;
329 panfrost_destroy_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*pbo
)
331 struct panfrost_bo
*bo
= (struct panfrost_bo
*)pbo
;
333 if ((bo
->layout
== PAN_LINEAR
|| bo
->layout
== PAN_TILED
) &&
335 struct panfrost_memory mem
= {
339 .gem_handle
= bo
->gem_handle
,
342 screen
->driver
->free_slab(screen
, &mem
);
345 if (bo
->layout
== PAN_AFBC
) {
347 DBG("--leaking afbc (%d bytes)--\n", bo
->afbc_metadata_size
);
350 if (bo
->has_checksum
) {
351 struct panfrost_memory mem
= {
352 .cpu
= bo
->checksum_slab
.cpu
,
353 .gpu
= bo
->checksum_slab
.gpu
,
354 .size
= bo
->checksum_slab
.size
,
355 .gem_handle
= bo
->checksum_slab
.gem_handle
,
358 screen
->driver
->free_slab(screen
, &mem
);
362 screen
->driver
->free_imported_bo(screen
, bo
);
367 panfrost_bo_reference(struct panfrost_bo
*bo
)
369 pipe_reference(NULL
, &bo
->reference
);
373 panfrost_bo_unreference(struct pipe_screen
*screen
, struct panfrost_bo
*bo
)
375 /* When the reference count goes to zero, we need to cleanup */
377 if (pipe_reference(&bo
->reference
, NULL
)) {
378 panfrost_destroy_bo(pan_screen(screen
), bo
);
383 panfrost_resource_destroy(struct pipe_screen
*screen
,
384 struct pipe_resource
*pt
)
386 struct panfrost_screen
*pscreen
= pan_screen(screen
);
387 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
390 renderonly_scanout_destroy(rsrc
->scanout
, pscreen
->ro
);
393 panfrost_bo_unreference(screen
, rsrc
->bo
);
395 util_range_destroy(&rsrc
->valid_buffer_range
);
400 panfrost_transfer_map(struct pipe_context
*pctx
,
401 struct pipe_resource
*resource
,
403 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
404 const struct pipe_box
*box
,
405 struct pipe_transfer
**out_transfer
)
407 int bytes_per_pixel
= util_format_get_blocksize(resource
->format
);
408 struct panfrost_resource
*rsrc
= pan_resource(resource
);
409 struct panfrost_bo
*bo
= rsrc
->bo
;
411 struct panfrost_gtransfer
*transfer
= CALLOC_STRUCT(panfrost_gtransfer
);
412 transfer
->base
.level
= level
;
413 transfer
->base
.usage
= usage
;
414 transfer
->base
.box
= *box
;
416 pipe_resource_reference(&transfer
->base
.resource
, resource
);
418 *out_transfer
= &transfer
->base
;
420 /* Check if we're bound for rendering and this is a read pixels. If so,
421 * we need to flush */
423 struct panfrost_context
*ctx
= pan_context(pctx
);
424 struct pipe_framebuffer_state
*fb
= &ctx
->pipe_framebuffer
;
426 bool is_bound
= false;
428 for (unsigned c
= 0; c
< fb
->nr_cbufs
; ++c
) {
429 is_bound
|= fb
->cbufs
[c
]->texture
== resource
;
432 if (is_bound
&& (usage
& PIPE_TRANSFER_READ
)) {
434 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
437 /* TODO: Respect usage flags */
439 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
440 /* TODO: reallocate */
441 //printf("debug: Missed reallocate\n");
442 } else if ((usage
& PIPE_TRANSFER_WRITE
)
443 && resource
->target
== PIPE_BUFFER
444 && !util_ranges_intersect(&rsrc
->valid_buffer_range
, box
->x
, box
->x
+ box
->width
)) {
445 /* No flush for writes to uninitialized */
446 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
447 if (usage
& PIPE_TRANSFER_WRITE
) {
448 /* STUB: flush reading */
449 //printf("debug: missed reading flush %d\n", resource->target);
450 } else if (usage
& PIPE_TRANSFER_READ
) {
451 /* STUB: flush writing */
452 //printf("debug: missed writing flush %d (%d-%d)\n", resource->target, box->x, box->x + box->width);
454 /* Why are you even mapping?! */
458 if (bo
->layout
!= PAN_LINEAR
) {
459 /* Non-linear resources need to be indirectly mapped */
461 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
464 transfer
->base
.stride
= box
->width
* bytes_per_pixel
;
465 transfer
->base
.layer_stride
= transfer
->base
.stride
* box
->height
;
468 transfer
->map
= malloc(transfer
->base
.layer_stride
* box
->depth
);
470 return transfer
->map
;
472 transfer
->base
.stride
= bo
->slices
[level
].stride
;
473 transfer
->base
.layer_stride
= bo
->cubemap_stride
;
476 + bo
->slices
[level
].offset
477 + transfer
->base
.box
.z
* bo
->cubemap_stride
478 + transfer
->base
.box
.y
* bo
->slices
[level
].stride
479 + transfer
->base
.box
.x
* bytes_per_pixel
;
484 panfrost_tile_texture(struct panfrost_screen
*screen
, struct panfrost_resource
*rsrc
, struct panfrost_gtransfer
*trans
)
486 struct panfrost_bo
*bo
= (struct panfrost_bo
*)rsrc
->bo
;
488 unsigned level
= trans
->base
.level
;
490 panfrost_texture_swizzle(
493 trans
->base
.box
.width
,
494 trans
->base
.box
.height
,
495 util_format_get_blocksize(rsrc
->base
.format
),
496 u_minify(rsrc
->base
.width0
, level
),
499 + bo
->slices
[level
].offset
500 + bo
->cubemap_stride
* trans
->base
.box
.z
505 panfrost_transfer_unmap(struct pipe_context
*pctx
,
506 struct pipe_transfer
*transfer
)
508 struct panfrost_context
*ctx
= pan_context(pctx
);
510 /* Gallium expects writeback here, so we tile */
512 struct panfrost_gtransfer
*trans
= pan_transfer(transfer
);
513 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) transfer
->resource
;
516 struct panfrost_bo
*bo
= prsrc
->bo
;
518 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
520 if (bo
->layout
== PAN_AFBC
) {
521 DBG("Unimplemented: writes to AFBC\n");
522 } else if (bo
->layout
== PAN_TILED
) {
523 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
524 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
525 assert(transfer
->box
.depth
== 1);
526 panfrost_tile_texture(screen
, prsrc
, trans
);
534 util_range_add(&prsrc
->valid_buffer_range
,
536 transfer
->box
.x
+ transfer
->box
.width
);
538 /* Derefence the resource */
539 pipe_resource_reference(&transfer
->resource
, NULL
);
541 /* Transfer itself is CALLOCed at the moment */
546 panfrost_transfer_flush_region(struct pipe_context
*pctx
,
547 struct pipe_transfer
*transfer
,
548 const struct pipe_box
*box
)
550 struct panfrost_resource
*rsc
= pan_resource(transfer
->resource
);
552 if (transfer
->resource
->target
== PIPE_BUFFER
) {
553 util_range_add(&rsc
->valid_buffer_range
,
554 transfer
->box
.x
+ box
->x
,
555 transfer
->box
.x
+ box
->x
+ box
->width
);
559 static struct pb_slab
*
560 panfrost_slab_alloc(void *priv
, unsigned heap
, unsigned entry_size
, unsigned group_index
)
562 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
563 struct panfrost_memory
*mem
= CALLOC_STRUCT(panfrost_memory
);
565 size_t slab_size
= (1 << (MAX_SLAB_ENTRY_SIZE
+ 1));
567 mem
->slab
.num_entries
= slab_size
/ entry_size
;
568 mem
->slab
.num_free
= mem
->slab
.num_entries
;
570 LIST_INITHEAD(&mem
->slab
.free
);
571 for (unsigned i
= 0; i
< mem
->slab
.num_entries
; ++i
) {
572 /* Create a slab entry */
573 struct panfrost_memory_entry
*entry
= CALLOC_STRUCT(panfrost_memory_entry
);
574 entry
->offset
= entry_size
* i
;
576 entry
->base
.slab
= &mem
->slab
;
577 entry
->base
.group_index
= group_index
;
579 LIST_ADDTAIL(&entry
->base
.head
, &mem
->slab
.free
);
582 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
585 screen
->driver
->allocate_slab(screen
, mem
, slab_size
/ 4096, true, 0, 0, 0);
591 panfrost_slab_can_reclaim(void *priv
, struct pb_slab_entry
*entry
)
593 struct panfrost_memory_entry
*p_entry
= (struct panfrost_memory_entry
*) entry
;
594 return p_entry
->freed
;
598 panfrost_slab_free(void *priv
, struct pb_slab
*slab
)
600 struct panfrost_memory
*mem
= (struct panfrost_memory
*) slab
;
601 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
603 screen
->driver
->free_slab(screen
, mem
);
607 panfrost_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
609 //DBG("TODO %s\n", __func__);
612 static enum pipe_format
613 panfrost_resource_get_internal_format(struct pipe_resource
*prsrc
)
615 return prsrc
->format
;
619 panfrost_resource_set_stencil(struct pipe_resource
*prsrc
,
620 struct pipe_resource
*stencil
)
622 pan_resource(prsrc
)->separate_stencil
= pan_resource(stencil
);
625 static struct pipe_resource
*
626 panfrost_resource_get_stencil(struct pipe_resource
*prsrc
)
628 return &pan_resource(prsrc
)->separate_stencil
->base
;
631 static const struct u_transfer_vtbl transfer_vtbl
= {
632 .resource_create
= panfrost_resource_create
,
633 .resource_destroy
= panfrost_resource_destroy
,
634 .transfer_map
= panfrost_transfer_map
,
635 .transfer_unmap
= panfrost_transfer_unmap
,
636 .transfer_flush_region
= panfrost_transfer_flush_region
,
637 .get_internal_format
= panfrost_resource_get_internal_format
,
638 .set_stencil
= panfrost_resource_set_stencil
,
639 .get_stencil
= panfrost_resource_get_stencil
,
643 panfrost_resource_screen_init(struct panfrost_screen
*pscreen
)
645 //pscreen->base.resource_create_with_modifiers =
646 // panfrost_resource_create_with_modifiers;
647 pscreen
->base
.resource_create
= u_transfer_helper_resource_create
;
648 pscreen
->base
.resource_destroy
= u_transfer_helper_resource_destroy
;
649 pscreen
->base
.resource_from_handle
= panfrost_resource_from_handle
;
650 pscreen
->base
.resource_get_handle
= panfrost_resource_get_handle
;
651 pscreen
->base
.transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
655 pb_slabs_init(&pscreen
->slabs
,
659 3, /* Number of heaps */
663 panfrost_slab_can_reclaim
,
669 panfrost_resource_context_init(struct pipe_context
*pctx
)
671 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
672 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
673 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
674 pctx
->buffer_subdata
= u_default_buffer_subdata
;
675 pctx
->create_surface
= panfrost_create_surface
;
676 pctx
->surface_destroy
= panfrost_surface_destroy
;
677 pctx
->resource_copy_region
= util_resource_copy_region
;
678 pctx
->blit
= panfrost_blit
;
679 pctx
->flush_resource
= panfrost_flush_resource
;
680 pctx
->invalidate_resource
= panfrost_invalidate_resource
;
681 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
682 pctx
->buffer_subdata
= u_default_buffer_subdata
;
683 pctx
->texture_subdata
= u_default_texture_subdata
;