1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2014 Broadcom
5 * Copyright 2018 Alyssa Rosenzweig
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 #include "drm-uapi/drm_fourcc.h"
34 #include "state_tracker/winsys_handle.h"
35 #include "util/u_format.h"
36 #include "util/u_memory.h"
37 #include "util/u_surface.h"
38 #include "util/u_transfer.h"
39 #include "util/u_transfer_helper.h"
41 #include "pan_context.h"
42 #include "pan_screen.h"
43 #include "pan_resource.h"
44 #include "pan_swizzle.h"
47 static struct pipe_resource
*
48 panfrost_resource_from_handle(struct pipe_screen
*pscreen
,
49 const struct pipe_resource
*templat
,
50 struct winsys_handle
*whandle
,
53 struct panfrost_screen
*screen
= pan_screen(pscreen
);
54 struct panfrost_resource
*rsc
;
55 struct pipe_resource
*prsc
;
57 assert(whandle
->type
== WINSYS_HANDLE_TYPE_FD
);
59 rsc
= CALLOC_STRUCT(panfrost_resource
);
67 pipe_reference_init(&prsc
->reference
, 1);
68 prsc
->screen
= pscreen
;
70 rsc
->bo
= screen
->driver
->import_bo(screen
, whandle
);
71 rsc
->bo
->slices
[0].stride
= whandle
->stride
;
75 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
76 /* failure is expected in some cases.. */
83 panfrost_resource_get_handle(struct pipe_screen
*pscreen
,
84 struct pipe_context
*ctx
,
85 struct pipe_resource
*pt
,
86 struct winsys_handle
*handle
,
89 struct panfrost_screen
*screen
= pan_screen(pscreen
);
90 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
91 struct renderonly_scanout
*scanout
= rsrc
->scanout
;
93 handle
->modifier
= DRM_FORMAT_MOD_INVALID
;
95 if (handle
->type
== WINSYS_HANDLE_TYPE_SHARED
) {
97 } else if (handle
->type
== WINSYS_HANDLE_TYPE_KMS
) {
98 if (renderonly_get_handle(scanout
, handle
))
101 handle
->handle
= rsrc
->bo
->gem_handle
;
102 handle
->stride
= rsrc
->bo
->slices
[0].stride
;
104 } else if (handle
->type
== WINSYS_HANDLE_TYPE_FD
) {
106 struct drm_prime_handle args
= {
107 .handle
= scanout
->handle
,
108 .flags
= DRM_CLOEXEC
,
111 int ret
= drmIoctl(screen
->ro
->kms_fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
115 handle
->stride
= scanout
->stride
;
116 handle
->handle
= args
.fd
;
120 return screen
->driver
->export_bo(screen
, rsrc
->bo
->gem_handle
, rsrc
->bo
->slices
[0].stride
, handle
);
127 panfrost_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
129 //DBG("TODO %s\n", __func__);
133 panfrost_blit(struct pipe_context
*pipe
,
134 const struct pipe_blit_info
*info
)
136 if (util_try_blit_via_copy_region(pipe
, info
))
140 DBG("Unhandled blit.\n");
145 static struct pipe_surface
*
146 panfrost_create_surface(struct pipe_context
*pipe
,
147 struct pipe_resource
*pt
,
148 const struct pipe_surface
*surf_tmpl
)
150 struct pipe_surface
*ps
= NULL
;
152 ps
= CALLOC_STRUCT(pipe_surface
);
155 pipe_reference_init(&ps
->reference
, 1);
156 pipe_resource_reference(&ps
->texture
, pt
);
158 ps
->format
= surf_tmpl
->format
;
160 if (pt
->target
!= PIPE_BUFFER
) {
161 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
162 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
163 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
164 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
165 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
166 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
168 /* setting width as number of elements should get us correct renderbuffer width */
169 ps
->width
= surf_tmpl
->u
.buf
.last_element
- surf_tmpl
->u
.buf
.first_element
+ 1;
170 ps
->height
= pt
->height0
;
171 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
172 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
173 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
174 assert(ps
->u
.buf
.last_element
< ps
->width
);
182 panfrost_surface_destroy(struct pipe_context
*pipe
,
183 struct pipe_surface
*surf
)
185 assert(surf
->texture
);
186 pipe_resource_reference(&surf
->texture
, NULL
);
191 panfrost_setup_slices(const struct pipe_resource
*tmpl
, struct panfrost_bo
*bo
)
193 unsigned width
= tmpl
->width0
;
194 unsigned height
= tmpl
->height0
;
195 unsigned bytes_per_pixel
= util_format_get_blocksize(tmpl
->format
);
199 for (unsigned l
= 0; l
<= tmpl
->last_level
; ++l
) {
200 struct panfrost_slice
*slice
= &bo
->slices
[l
];
202 unsigned effective_width
= width
;
203 unsigned effective_height
= height
;
205 /* Tiled operates blockwise; linear is packed */
207 if (bo
->layout
== PAN_TILED
) {
208 effective_width
= ALIGN(effective_width
, 16);
209 effective_height
= ALIGN(effective_height
, 16);
212 slice
->offset
= offset
;
213 slice
->stride
= bytes_per_pixel
* effective_width
;
215 offset
+= slice
->stride
* effective_height
;
217 width
= u_minify(width
, 1);
218 height
= u_minify(height
, 1);
221 assert(tmpl
->array_size
);
223 bo
->cubemap_stride
= ALIGN(offset
, 64);
224 bo
->size
= ALIGN(bo
->cubemap_stride
* tmpl
->array_size
, 4096);
227 static struct panfrost_bo
*
228 panfrost_create_bo(struct panfrost_screen
*screen
, const struct pipe_resource
*template)
230 struct panfrost_bo
*bo
= CALLOC_STRUCT(panfrost_bo
);
231 pipe_reference_init(&bo
->reference
, 1);
233 /* Based on the usage, figure out what storing will be used. There are
236 * Linear: the basic format, bad for memory bandwidth, bad for cache
237 * use. Zero-copy, though. Renderable.
239 * Tiled: Not compressed, but cache-optimized. Expensive to write into
240 * (due to software tiling), but cheap to sample from. Ideal for most
243 * AFBC: Compressed and renderable (so always desirable for non-scanout
244 * rendertargets). Cheap to sample from. The format is black box, so we
245 * can't read/write from software.
248 /* Tiling textures is almost always faster, unless we only use it once */
249 bool should_tile
= (template->usage
!= PIPE_USAGE_STREAM
) && (template->bind
& PIPE_BIND_SAMPLER_VIEW
);
251 /* For unclear reasons, depth/stencil is faster linear than AFBC, so
252 * make sure it's linear */
254 if (template->bind
& PIPE_BIND_DEPTH_STENCIL
)
257 /* Set the layout appropriately */
258 bo
->layout
= should_tile
? PAN_TILED
: PAN_LINEAR
;
260 panfrost_setup_slices(template, bo
);
262 if (bo
->layout
== PAN_TILED
|| bo
->layout
== PAN_LINEAR
) {
263 struct panfrost_memory mem
;
265 screen
->driver
->allocate_slab(screen
, &mem
, bo
->size
/ 4096, true, 0, 0, 0);
269 bo
->gem_handle
= mem
.gem_handle
;
275 static struct pipe_resource
*
276 panfrost_resource_create(struct pipe_screen
*screen
,
277 const struct pipe_resource
*template)
279 struct panfrost_resource
*so
= CALLOC_STRUCT(panfrost_resource
);
280 struct panfrost_screen
*pscreen
= (struct panfrost_screen
*) screen
;
282 so
->base
= *template;
283 so
->base
.screen
= screen
;
285 pipe_reference_init(&so
->base
.reference
, 1);
287 /* Make sure we're familiar */
288 switch (template->target
) {
290 case PIPE_TEXTURE_1D
:
291 case PIPE_TEXTURE_2D
:
292 case PIPE_TEXTURE_3D
:
293 case PIPE_TEXTURE_CUBE
:
294 case PIPE_TEXTURE_RECT
:
297 DBG("Unknown texture target %d\n", template->target
);
301 util_range_init(&so
->valid_buffer_range
);
303 if (template->bind
& PIPE_BIND_DISPLAY_TARGET
||
304 template->bind
& PIPE_BIND_SCANOUT
||
305 template->bind
& PIPE_BIND_SHARED
) {
306 struct pipe_resource scanout_templat
= *template;
307 struct renderonly_scanout
*scanout
;
308 struct winsys_handle handle
;
310 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
311 pscreen
->ro
, &handle
);
315 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
316 /* TODO: handle modifiers? */
317 so
= pan_resource(screen
->resource_from_handle(screen
, template,
319 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
320 close(handle
.handle
);
324 so
->scanout
= scanout
;
325 pscreen
->display_target
= so
;
327 so
->bo
= panfrost_create_bo(pscreen
, template);
330 return (struct pipe_resource
*)so
;
334 panfrost_destroy_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*pbo
)
336 struct panfrost_bo
*bo
= (struct panfrost_bo
*)pbo
;
338 if ((bo
->layout
== PAN_LINEAR
|| bo
->layout
== PAN_TILED
) &&
340 struct panfrost_memory mem
= {
344 .gem_handle
= bo
->gem_handle
,
347 screen
->driver
->free_slab(screen
, &mem
);
350 if (bo
->layout
== PAN_AFBC
) {
352 DBG("--leaking afbc (%d bytes)--\n", bo
->afbc_metadata_size
);
355 if (bo
->has_checksum
) {
356 struct panfrost_memory mem
= {
357 .cpu
= bo
->checksum_slab
.cpu
,
358 .gpu
= bo
->checksum_slab
.gpu
,
359 .size
= bo
->checksum_slab
.size
,
360 .gem_handle
= bo
->checksum_slab
.gem_handle
,
363 screen
->driver
->free_slab(screen
, &mem
);
367 screen
->driver
->free_imported_bo(screen
, bo
);
372 panfrost_bo_reference(struct panfrost_bo
*bo
)
374 pipe_reference(NULL
, &bo
->reference
);
378 panfrost_bo_unreference(struct pipe_screen
*screen
, struct panfrost_bo
*bo
)
380 /* When the reference count goes to zero, we need to cleanup */
382 if (pipe_reference(&bo
->reference
, NULL
)) {
383 panfrost_destroy_bo(pan_screen(screen
), bo
);
388 panfrost_resource_destroy(struct pipe_screen
*screen
,
389 struct pipe_resource
*pt
)
391 struct panfrost_screen
*pscreen
= pan_screen(screen
);
392 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
395 renderonly_scanout_destroy(rsrc
->scanout
, pscreen
->ro
);
398 panfrost_bo_unreference(screen
, rsrc
->bo
);
400 util_range_destroy(&rsrc
->valid_buffer_range
);
405 panfrost_transfer_map(struct pipe_context
*pctx
,
406 struct pipe_resource
*resource
,
408 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
409 const struct pipe_box
*box
,
410 struct pipe_transfer
**out_transfer
)
412 int bytes_per_pixel
= util_format_get_blocksize(resource
->format
);
413 struct panfrost_resource
*rsrc
= pan_resource(resource
);
414 struct panfrost_bo
*bo
= rsrc
->bo
;
416 struct panfrost_gtransfer
*transfer
= CALLOC_STRUCT(panfrost_gtransfer
);
417 transfer
->base
.level
= level
;
418 transfer
->base
.usage
= usage
;
419 transfer
->base
.box
= *box
;
421 pipe_resource_reference(&transfer
->base
.resource
, resource
);
423 *out_transfer
= &transfer
->base
;
425 /* Check if we're bound for rendering and this is a read pixels. If so,
426 * we need to flush */
428 struct panfrost_context
*ctx
= pan_context(pctx
);
429 struct pipe_framebuffer_state
*fb
= &ctx
->pipe_framebuffer
;
431 bool is_bound
= false;
433 for (unsigned c
= 0; c
< fb
->nr_cbufs
; ++c
) {
434 is_bound
|= fb
->cbufs
[c
]->texture
== resource
;
437 if (is_bound
&& (usage
& PIPE_TRANSFER_READ
)) {
439 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
442 /* TODO: Respect usage flags */
444 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
445 /* TODO: reallocate */
446 //printf("debug: Missed reallocate\n");
447 } else if ((usage
& PIPE_TRANSFER_WRITE
)
448 && resource
->target
== PIPE_BUFFER
449 && !util_ranges_intersect(&rsrc
->valid_buffer_range
, box
->x
, box
->x
+ box
->width
)) {
450 /* No flush for writes to uninitialized */
451 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
452 if (usage
& PIPE_TRANSFER_WRITE
) {
453 /* STUB: flush reading */
454 //printf("debug: missed reading flush %d\n", resource->target);
455 } else if (usage
& PIPE_TRANSFER_READ
) {
456 /* STUB: flush writing */
457 //printf("debug: missed writing flush %d (%d-%d)\n", resource->target, box->x, box->x + box->width);
459 /* Why are you even mapping?! */
463 if (bo
->layout
!= PAN_LINEAR
) {
464 /* Non-linear resources need to be indirectly mapped */
466 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
469 transfer
->base
.stride
= box
->width
* bytes_per_pixel
;
470 transfer
->base
.layer_stride
= transfer
->base
.stride
* box
->height
;
473 transfer
->map
= malloc(transfer
->base
.layer_stride
* box
->depth
);
475 return transfer
->map
;
477 transfer
->base
.stride
= bo
->slices
[level
].stride
;
478 transfer
->base
.layer_stride
= bo
->cubemap_stride
;
481 + bo
->slices
[level
].offset
482 + transfer
->base
.box
.z
* bo
->cubemap_stride
483 + transfer
->base
.box
.y
* bo
->slices
[level
].stride
484 + transfer
->base
.box
.x
* bytes_per_pixel
;
489 panfrost_tile_texture(struct panfrost_screen
*screen
, struct panfrost_resource
*rsrc
, struct panfrost_gtransfer
*trans
)
491 struct panfrost_bo
*bo
= (struct panfrost_bo
*)rsrc
->bo
;
493 unsigned level
= trans
->base
.level
;
495 panfrost_texture_swizzle(
498 trans
->base
.box
.width
,
499 trans
->base
.box
.height
,
500 util_format_get_blocksize(rsrc
->base
.format
),
501 u_minify(rsrc
->base
.width0
, level
),
504 + bo
->slices
[level
].offset
505 + bo
->cubemap_stride
* trans
->base
.box
.z
510 panfrost_transfer_unmap(struct pipe_context
*pctx
,
511 struct pipe_transfer
*transfer
)
513 struct panfrost_context
*ctx
= pan_context(pctx
);
515 /* Gallium expects writeback here, so we tile */
517 struct panfrost_gtransfer
*trans
= pan_transfer(transfer
);
518 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) transfer
->resource
;
521 struct panfrost_bo
*bo
= prsrc
->bo
;
523 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
525 if (bo
->layout
== PAN_AFBC
) {
526 DBG("Unimplemented: writes to AFBC\n");
527 } else if (bo
->layout
== PAN_TILED
) {
528 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
529 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
530 assert(transfer
->box
.depth
== 1);
531 panfrost_tile_texture(screen
, prsrc
, trans
);
539 util_range_add(&prsrc
->valid_buffer_range
,
541 transfer
->box
.x
+ transfer
->box
.width
);
543 /* Derefence the resource */
544 pipe_resource_reference(&transfer
->resource
, NULL
);
546 /* Transfer itself is CALLOCed at the moment */
551 panfrost_transfer_flush_region(struct pipe_context
*pctx
,
552 struct pipe_transfer
*transfer
,
553 const struct pipe_box
*box
)
555 struct panfrost_resource
*rsc
= pan_resource(transfer
->resource
);
557 if (transfer
->resource
->target
== PIPE_BUFFER
) {
558 util_range_add(&rsc
->valid_buffer_range
,
559 transfer
->box
.x
+ box
->x
,
560 transfer
->box
.x
+ box
->x
+ box
->width
);
564 static struct pb_slab
*
565 panfrost_slab_alloc(void *priv
, unsigned heap
, unsigned entry_size
, unsigned group_index
)
567 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
568 struct panfrost_memory
*mem
= CALLOC_STRUCT(panfrost_memory
);
570 size_t slab_size
= (1 << (MAX_SLAB_ENTRY_SIZE
+ 1));
572 mem
->slab
.num_entries
= slab_size
/ entry_size
;
573 mem
->slab
.num_free
= mem
->slab
.num_entries
;
575 LIST_INITHEAD(&mem
->slab
.free
);
576 for (unsigned i
= 0; i
< mem
->slab
.num_entries
; ++i
) {
577 /* Create a slab entry */
578 struct panfrost_memory_entry
*entry
= CALLOC_STRUCT(panfrost_memory_entry
);
579 entry
->offset
= entry_size
* i
;
581 entry
->base
.slab
= &mem
->slab
;
582 entry
->base
.group_index
= group_index
;
584 LIST_ADDTAIL(&entry
->base
.head
, &mem
->slab
.free
);
587 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
590 screen
->driver
->allocate_slab(screen
, mem
, slab_size
/ 4096, true, 0, 0, 0);
596 panfrost_slab_can_reclaim(void *priv
, struct pb_slab_entry
*entry
)
598 struct panfrost_memory_entry
*p_entry
= (struct panfrost_memory_entry
*) entry
;
599 return p_entry
->freed
;
603 panfrost_slab_free(void *priv
, struct pb_slab
*slab
)
605 struct panfrost_memory
*mem
= (struct panfrost_memory
*) slab
;
606 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
608 screen
->driver
->free_slab(screen
, mem
);
612 panfrost_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
614 //DBG("TODO %s\n", __func__);
617 static enum pipe_format
618 panfrost_resource_get_internal_format(struct pipe_resource
*prsrc
)
620 return prsrc
->format
;
624 panfrost_resource_set_stencil(struct pipe_resource
*prsrc
,
625 struct pipe_resource
*stencil
)
627 pan_resource(prsrc
)->separate_stencil
= pan_resource(stencil
);
630 static struct pipe_resource
*
631 panfrost_resource_get_stencil(struct pipe_resource
*prsrc
)
633 return &pan_resource(prsrc
)->separate_stencil
->base
;
636 static const struct u_transfer_vtbl transfer_vtbl
= {
637 .resource_create
= panfrost_resource_create
,
638 .resource_destroy
= panfrost_resource_destroy
,
639 .transfer_map
= panfrost_transfer_map
,
640 .transfer_unmap
= panfrost_transfer_unmap
,
641 .transfer_flush_region
= panfrost_transfer_flush_region
,
642 .get_internal_format
= panfrost_resource_get_internal_format
,
643 .set_stencil
= panfrost_resource_set_stencil
,
644 .get_stencil
= panfrost_resource_get_stencil
,
648 panfrost_resource_screen_init(struct panfrost_screen
*pscreen
)
650 //pscreen->base.resource_create_with_modifiers =
651 // panfrost_resource_create_with_modifiers;
652 pscreen
->base
.resource_create
= u_transfer_helper_resource_create
;
653 pscreen
->base
.resource_destroy
= u_transfer_helper_resource_destroy
;
654 pscreen
->base
.resource_from_handle
= panfrost_resource_from_handle
;
655 pscreen
->base
.resource_get_handle
= panfrost_resource_get_handle
;
656 pscreen
->base
.transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
660 pb_slabs_init(&pscreen
->slabs
,
664 3, /* Number of heaps */
668 panfrost_slab_can_reclaim
,
674 panfrost_resource_context_init(struct pipe_context
*pctx
)
676 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
677 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
678 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
679 pctx
->buffer_subdata
= u_default_buffer_subdata
;
680 pctx
->create_surface
= panfrost_create_surface
;
681 pctx
->surface_destroy
= panfrost_surface_destroy
;
682 pctx
->resource_copy_region
= util_resource_copy_region
;
683 pctx
->blit
= panfrost_blit
;
684 pctx
->flush_resource
= panfrost_flush_resource
;
685 pctx
->invalidate_resource
= panfrost_invalidate_resource
;
686 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
687 pctx
->buffer_subdata
= u_default_buffer_subdata
;
688 pctx
->texture_subdata
= u_default_texture_subdata
;