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
);
74 renderonly_create_gpu_import_for_resource(prsc
, screen
->ro
, NULL
);
75 /* failure is expected in some cases.. */
82 panfrost_resource_get_handle(struct pipe_screen
*pscreen
,
83 struct pipe_context
*ctx
,
84 struct pipe_resource
*pt
,
85 struct winsys_handle
*handle
,
88 struct panfrost_screen
*screen
= pan_screen(pscreen
);
89 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
90 struct renderonly_scanout
*scanout
= rsrc
->scanout
;
91 int bytes_per_pixel
= util_format_get_blocksize(rsrc
->base
.format
);
92 int stride
= bytes_per_pixel
* rsrc
->base
.width0
; /* TODO: Alignment? */
94 handle
->stride
= stride
;
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
;
105 } else if (handle
->type
== WINSYS_HANDLE_TYPE_FD
) {
107 struct drm_prime_handle args
= {
108 .handle
= scanout
->handle
,
109 .flags
= DRM_CLOEXEC
,
112 int ret
= drmIoctl(screen
->ro
->kms_fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);
116 handle
->handle
= args
.fd
;
120 return screen
->driver
->export_bo(screen
, rsrc
->bo
->gem_handle
, 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
)
137 DBG("Skipping blit XXX\n");
141 static struct pipe_surface
*
142 panfrost_create_surface(struct pipe_context
*pipe
,
143 struct pipe_resource
*pt
,
144 const struct pipe_surface
*surf_tmpl
)
146 struct pipe_surface
*ps
= NULL
;
148 ps
= CALLOC_STRUCT(pipe_surface
);
151 pipe_reference_init(&ps
->reference
, 1);
152 pipe_resource_reference(&ps
->texture
, pt
);
154 ps
->format
= surf_tmpl
->format
;
156 if (pt
->target
!= PIPE_BUFFER
) {
157 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
158 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
159 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
160 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
161 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
162 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
164 /* setting width as number of elements should get us correct renderbuffer width */
165 ps
->width
= surf_tmpl
->u
.buf
.last_element
- surf_tmpl
->u
.buf
.first_element
+ 1;
166 ps
->height
= pt
->height0
;
167 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
168 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
169 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
170 assert(ps
->u
.buf
.last_element
< ps
->width
);
178 panfrost_surface_destroy(struct pipe_context
*pipe
,
179 struct pipe_surface
*surf
)
181 assert(surf
->texture
);
182 pipe_resource_reference(&surf
->texture
, NULL
);
186 static struct panfrost_bo
*
187 panfrost_create_bo(struct panfrost_screen
*screen
, const struct pipe_resource
*template)
189 struct panfrost_bo
*bo
= CALLOC_STRUCT(panfrost_bo
);
191 /* Calculate the size of the bo */
193 int bytes_per_pixel
= util_format_get_blocksize(template->format
);
194 int stride
= bytes_per_pixel
* template->width0
; /* TODO: Alignment? */
197 if (template->height0
) sz
*= template->height0
;
198 if (template->depth0
) sz
*= template->depth0
;
200 /* Based on the usage, figure out what storing will be used. There are
203 * Linear: the basic format, bad for memory bandwidth, bad for cache
204 * use. Zero-copy, though. Renderable.
206 * Tiled: Not compressed, but cache-optimized. Expensive to write into
207 * (due to software tiling), but cheap to sample from. Ideal for most
210 * AFBC: Compressed and renderable (so always desirable for non-scanout
211 * rendertargets). Cheap to sample from. The format is black box, so we
212 * can't read/write from software.
215 /* Tiling textures is almost always faster, unless we only use it once */
216 bool should_tile
= (template->usage
!= PIPE_USAGE_STREAM
) && (template->bind
& PIPE_BIND_SAMPLER_VIEW
);
218 /* Set the layout appropriately */
219 bo
->layout
= should_tile
? PAN_TILED
: PAN_LINEAR
;
221 if (bo
->layout
== PAN_TILED
) {
222 /* For tiled, we don't map directly, so just malloc any old buffer */
224 for (int l
= 0; l
< (template->last_level
+ 1); ++l
) {
225 bo
->cpu
[l
] = malloc(sz
);
229 /* But for linear, we can! */
231 struct pb_slab_entry
*entry
= pb_slab_alloc(&screen
->slabs
, sz
, HEAP_TEXTURE
);
232 struct panfrost_memory_entry
*p_entry
= (struct panfrost_memory_entry
*) entry
;
233 struct panfrost_memory
*backing
= (struct panfrost_memory
*) entry
->slab
;
234 bo
->entry
[0] = p_entry
;
235 bo
->cpu
[0] = backing
->cpu
+ p_entry
->offset
;
236 bo
->gpu
[0] = backing
->gpu
+ p_entry
->offset
;
244 static struct pipe_resource
*
245 panfrost_resource_create(struct pipe_screen
*screen
,
246 const struct pipe_resource
*template)
248 struct panfrost_resource
*so
= CALLOC_STRUCT(panfrost_resource
);
249 struct panfrost_screen
*pscreen
= (struct panfrost_screen
*) screen
;
251 so
->base
= *template;
252 so
->base
.screen
= screen
;
254 pipe_reference_init(&so
->base
.reference
, 1);
256 /* Make sure we're familiar */
257 switch (template->target
) {
259 case PIPE_TEXTURE_1D
:
260 case PIPE_TEXTURE_2D
:
261 case PIPE_TEXTURE_3D
:
262 case PIPE_TEXTURE_RECT
:
265 DBG("Unknown texture target %d\n", template->target
);
269 if (template->bind
& PIPE_BIND_DISPLAY_TARGET
||
270 template->bind
& PIPE_BIND_SCANOUT
||
271 template->bind
& PIPE_BIND_SHARED
) {
272 struct pipe_resource scanout_templat
= *template;
273 struct renderonly_scanout
*scanout
;
274 struct winsys_handle handle
;
276 /* TODO: align width0 and height0? */
278 scanout
= renderonly_scanout_for_resource(&scanout_templat
,
279 pscreen
->ro
, &handle
);
283 assert(handle
.type
== WINSYS_HANDLE_TYPE_FD
);
284 /* TODO: handle modifiers? */
285 so
= pan_resource(screen
->resource_from_handle(screen
, template,
287 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE
));
288 close(handle
.handle
);
292 so
->scanout
= scanout
;
293 pscreen
->display_target
= so
;
295 so
->bo
= panfrost_create_bo(pscreen
, template);
298 return (struct pipe_resource
*)so
;
302 panfrost_destroy_bo(struct panfrost_screen
*screen
, struct panfrost_bo
*pbo
)
304 struct panfrost_bo
*bo
= (struct panfrost_bo
*)pbo
;
306 for (int l
= 0; l
< MAX_MIP_LEVELS
; ++l
) {
307 if (bo
->entry
[l
] != NULL
) {
308 /* Most allocations have an entry to free */
309 bo
->entry
[l
]->freed
= true;
310 pb_slab_free(&screen
->slabs
, &bo
->entry
[l
]->base
);
314 if (bo
->layout
== PAN_TILED
) {
315 /* Tiled has a malloc'd CPU, so just plain ol' free needed */
317 for (int l
= 0; l
< MAX_MIP_LEVELS
; ++l
) {
322 if (bo
->layout
== PAN_AFBC
) {
324 DBG("--leaking afbc (%d bytes)--\n", bo
->afbc_metadata_size
);
327 if (bo
->has_checksum
) {
329 DBG("--leaking checksum (%zd bytes)--\n", bo
->checksum_slab
.size
);
333 screen
->driver
->free_imported_bo(screen
, bo
);
338 panfrost_resource_destroy(struct pipe_screen
*screen
,
339 struct pipe_resource
*pt
)
341 struct panfrost_screen
*pscreen
= pan_screen(screen
);
342 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) pt
;
345 renderonly_scanout_destroy(rsrc
->scanout
, pscreen
->ro
);
348 panfrost_destroy_bo(pscreen
, rsrc
->bo
);
354 panfrost_map_bo(struct panfrost_context
*ctx
, struct pipe_transfer
*transfer
)
356 struct panfrost_bo
*bo
= (struct panfrost_bo
*)pan_resource(transfer
->resource
)->bo
;
358 /* If non-zero level, it's a mipmapped resource and needs to be treated as such */
359 bo
->is_mipmap
|= transfer
->level
;
361 if (transfer
->usage
& PIPE_TRANSFER_MAP_DIRECTLY
&& bo
->layout
!= PAN_LINEAR
) {
362 /* We can only directly map linear resources */
366 if (transfer
->resource
->bind
& PIPE_BIND_DEPTH_STENCIL
) {
367 /* Mipmapped readpixels?! */
368 assert(transfer
->level
== 0);
370 /* Set the CPU mapping to that of the depth/stencil buffer in memory, untiled */
371 bo
->cpu
[transfer
->level
] = ctx
->depth_stencil_buffer
.cpu
;
374 return bo
->cpu
[transfer
->level
];
378 panfrost_transfer_map(struct pipe_context
*pctx
,
379 struct pipe_resource
*resource
,
381 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
382 const struct pipe_box
*box
,
383 struct pipe_transfer
**out_transfer
)
385 struct panfrost_context
*ctx
= pan_context(pctx
);
386 int bytes_per_pixel
= util_format_get_blocksize(resource
->format
);
387 int stride
= bytes_per_pixel
* resource
->width0
; /* TODO: Alignment? */
390 struct pipe_transfer
*transfer
= CALLOC_STRUCT(pipe_transfer
);
391 transfer
->level
= level
;
392 transfer
->usage
= usage
;
393 transfer
->box
= *box
;
394 transfer
->stride
= stride
;
395 assert(!transfer
->box
.z
);
397 pipe_resource_reference(&transfer
->resource
, resource
);
399 *out_transfer
= transfer
;
401 if (resource
->bind
& PIPE_BIND_DISPLAY_TARGET
||
402 resource
->bind
& PIPE_BIND_SCANOUT
||
403 resource
->bind
& PIPE_BIND_SHARED
) {
404 /* Mipmapped readpixels?! */
407 /* Force a flush -- kill the pipeline */
408 panfrost_flush(pctx
, NULL
, PIPE_FLUSH_END_OF_FRAME
);
411 cpu
= panfrost_map_bo(ctx
, transfer
);
415 return cpu
+ transfer
->box
.x
* bytes_per_pixel
+ transfer
->box
.y
* stride
;
419 panfrost_tile_texture(struct panfrost_screen
*screen
, struct panfrost_resource
*rsrc
, int level
)
421 struct panfrost_bo
*bo
= (struct panfrost_bo
*)rsrc
->bo
;
422 int bytes_per_pixel
= util_format_get_blocksize(rsrc
->base
.format
);
423 int stride
= bytes_per_pixel
* rsrc
->base
.width0
; /* TODO: Alignment? */
425 int width
= rsrc
->base
.width0
>> level
;
426 int height
= rsrc
->base
.height0
>> level
;
428 /* Estimate swizzled bitmap size. Slight overestimates are fine.
429 * Underestimates will result in memory corruption or worse. */
431 int swizzled_sz
= panfrost_swizzled_size(width
, height
, bytes_per_pixel
);
433 /* Save the entry. But if there was already an entry here (from a
434 * previous upload of the resource), free that one so we don't leak */
436 if (bo
->entry
[level
] != NULL
) {
437 bo
->entry
[level
]->freed
= true;
438 pb_slab_free(&screen
->slabs
, &bo
->entry
[level
]->base
);
441 /* Allocate the transfer given that known size but do not copy */
442 struct pb_slab_entry
*entry
= pb_slab_alloc(&screen
->slabs
, swizzled_sz
, HEAP_TEXTURE
);
443 struct panfrost_memory_entry
*p_entry
= (struct panfrost_memory_entry
*) entry
;
444 struct panfrost_memory
*backing
= (struct panfrost_memory
*) entry
->slab
;
445 uint8_t *swizzled
= backing
->cpu
+ p_entry
->offset
;
447 bo
->entry
[level
] = p_entry
;
448 bo
->gpu
[level
] = backing
->gpu
+ p_entry
->offset
;
450 /* Run actual texture swizzle, writing directly to the mapped
451 * GPU chunk we allocated */
453 panfrost_texture_swizzle(width
, height
, bytes_per_pixel
, stride
, bo
->cpu
[level
], swizzled
);
457 panfrost_unmap_bo(struct panfrost_context
*ctx
,
458 struct pipe_transfer
*transfer
)
460 struct panfrost_bo
*bo
= (struct panfrost_bo
*)pan_resource(transfer
->resource
)->bo
;
462 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
463 if (transfer
->resource
->target
== PIPE_TEXTURE_2D
) {
464 struct panfrost_resource
*prsrc
= (struct panfrost_resource
*) transfer
->resource
;
466 /* Gallium thinks writeback happens here; instead, this is our cue to tile */
467 if (bo
->layout
== PAN_AFBC
) {
468 DBG("Warning: writes to afbc surface can't possibly work out well for you...\n");
469 } else if (bo
->layout
== PAN_TILED
) {
470 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
471 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
472 panfrost_tile_texture(screen
, prsrc
, transfer
->level
);
479 panfrost_transfer_unmap(struct pipe_context
*pctx
,
480 struct pipe_transfer
*transfer
)
482 struct panfrost_context
*ctx
= pan_context(pctx
);
484 panfrost_unmap_bo(ctx
, transfer
);
486 /* Derefence the resource */
487 pipe_resource_reference(&transfer
->resource
, NULL
);
489 /* Transfer itself is CALLOCed at the moment */
493 static struct pb_slab
*
494 panfrost_slab_alloc(void *priv
, unsigned heap
, unsigned entry_size
, unsigned group_index
)
496 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
497 struct panfrost_memory
*mem
= CALLOC_STRUCT(panfrost_memory
);
499 size_t slab_size
= (1 << (MAX_SLAB_ENTRY_SIZE
+ 1));
501 mem
->slab
.num_entries
= slab_size
/ entry_size
;
502 mem
->slab
.num_free
= mem
->slab
.num_entries
;
504 LIST_INITHEAD(&mem
->slab
.free
);
505 for (unsigned i
= 0; i
< mem
->slab
.num_entries
; ++i
) {
506 /* Create a slab entry */
507 struct panfrost_memory_entry
*entry
= CALLOC_STRUCT(panfrost_memory_entry
);
508 entry
->offset
= entry_size
* i
;
510 entry
->base
.slab
= &mem
->slab
;
511 entry
->base
.group_index
= group_index
;
513 LIST_ADDTAIL(&entry
->base
.head
, &mem
->slab
.free
);
516 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
519 screen
->driver
->allocate_slab(screen
, mem
, slab_size
/ 4096, true, 0, 0, 0);
525 panfrost_slab_can_reclaim(void *priv
, struct pb_slab_entry
*entry
)
527 struct panfrost_memory_entry
*p_entry
= (struct panfrost_memory_entry
*) entry
;
528 return p_entry
->freed
;
532 panfrost_slab_free(void *priv
, struct pb_slab
*slab
)
534 struct panfrost_memory
*mem
= (struct panfrost_memory
*) slab
;
535 struct panfrost_screen
*screen
= (struct panfrost_screen
*) priv
;
537 screen
->driver
->free_slab(screen
, mem
);
541 panfrost_invalidate_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
543 //DBG("TODO %s\n", __func__);
546 static enum pipe_format
547 panfrost_resource_get_internal_format(struct pipe_resource
*prsrc
)
549 return prsrc
->format
;
553 panfrost_resource_set_stencil(struct pipe_resource
*prsrc
,
554 struct pipe_resource
*stencil
)
556 pan_resource(prsrc
)->separate_stencil
= pan_resource(stencil
);
559 static struct pipe_resource
*
560 panfrost_resource_get_stencil(struct pipe_resource
*prsrc
)
562 return &pan_resource(prsrc
)->separate_stencil
->base
;
565 static const struct u_transfer_vtbl transfer_vtbl
= {
566 .resource_create
= panfrost_resource_create
,
567 .resource_destroy
= panfrost_resource_destroy
,
568 .transfer_map
= panfrost_transfer_map
,
569 .transfer_unmap
= panfrost_transfer_unmap
,
570 .transfer_flush_region
= u_default_transfer_flush_region
,
571 .get_internal_format
= panfrost_resource_get_internal_format
,
572 .set_stencil
= panfrost_resource_set_stencil
,
573 .get_stencil
= panfrost_resource_get_stencil
,
577 panfrost_resource_screen_init(struct panfrost_screen
*pscreen
)
579 //pscreen->base.resource_create_with_modifiers =
580 // panfrost_resource_create_with_modifiers;
581 pscreen
->base
.resource_create
= u_transfer_helper_resource_create
;
582 pscreen
->base
.resource_destroy
= u_transfer_helper_resource_destroy
;
583 pscreen
->base
.resource_from_handle
= panfrost_resource_from_handle
;
584 pscreen
->base
.resource_get_handle
= panfrost_resource_get_handle
;
585 pscreen
->base
.transfer_helper
= u_transfer_helper_create(&transfer_vtbl
,
589 pb_slabs_init(&pscreen
->slabs
,
593 3, /* Number of heaps */
597 panfrost_slab_can_reclaim
,
603 panfrost_resource_context_init(struct pipe_context
*pctx
)
605 pctx
->transfer_map
= u_transfer_helper_transfer_map
;
606 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
607 pctx
->transfer_unmap
= u_transfer_helper_transfer_unmap
;
608 pctx
->buffer_subdata
= u_default_buffer_subdata
;
609 pctx
->create_surface
= panfrost_create_surface
;
610 pctx
->surface_destroy
= panfrost_surface_destroy
;
611 pctx
->resource_copy_region
= util_resource_copy_region
;
612 pctx
->blit
= panfrost_blit
;
613 //pctx->generate_mipmap = panfrost_generate_mipmap;
614 pctx
->flush_resource
= panfrost_flush_resource
;
615 pctx
->invalidate_resource
= panfrost_invalidate_resource
;
616 pctx
->transfer_flush_region
= u_transfer_helper_transfer_flush_region
;
617 pctx
->buffer_subdata
= u_default_buffer_subdata
;
618 pctx
->texture_subdata
= u_default_texture_subdata
;