panfrost: Preliminary work for mipmaps
[mesa.git] / src / gallium / drivers / panfrost / pan_resource.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2014 Broadcom
5 * Copyright 2018 Alyssa Rosenzweig
6 * All Rights Reserved.
7 *
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:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
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.
27 *
28 **************************************************************************/
29
30 #include <xf86drm.h>
31 #include <fcntl.h>
32 #include "drm-uapi/drm_fourcc.h"
33
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"
40
41 #include "pan_context.h"
42 #include "pan_screen.h"
43 #include "pan_resource.h"
44 #include "pan_swizzle.h"
45 #include "pan_util.h"
46
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,
51 unsigned usage)
52 {
53 struct panfrost_screen *screen = pan_screen(pscreen);
54 struct panfrost_resource *rsc;
55 struct pipe_resource *prsc;
56
57 assert(whandle->type == WINSYS_HANDLE_TYPE_FD);
58
59 rsc = CALLOC_STRUCT(panfrost_resource);
60 if (!rsc)
61 return NULL;
62
63 prsc = &rsc->base;
64
65 *prsc = *templat;
66
67 pipe_reference_init(&prsc->reference, 1);
68 prsc->screen = pscreen;
69
70 rsc->bo = screen->driver->import_bo(screen, whandle);
71 rsc->bo->slices[0].stride = whandle->stride;
72
73 if (screen->ro) {
74 rsc->scanout =
75 renderonly_create_gpu_import_for_resource(prsc, screen->ro, NULL);
76 /* failure is expected in some cases.. */
77 }
78
79 return prsc;
80 }
81
82 static boolean
83 panfrost_resource_get_handle(struct pipe_screen *pscreen,
84 struct pipe_context *ctx,
85 struct pipe_resource *pt,
86 struct winsys_handle *handle,
87 unsigned usage)
88 {
89 struct panfrost_screen *screen = pan_screen(pscreen);
90 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
91 struct renderonly_scanout *scanout = rsrc->scanout;
92
93 handle->modifier = DRM_FORMAT_MOD_INVALID;
94
95 if (handle->type == WINSYS_HANDLE_TYPE_SHARED) {
96 return FALSE;
97 } else if (handle->type == WINSYS_HANDLE_TYPE_KMS) {
98 if (renderonly_get_handle(scanout, handle))
99 return TRUE;
100
101 handle->handle = rsrc->bo->gem_handle;
102 handle->stride = rsrc->bo->slices[0].stride;
103 return TRUE;
104 } else if (handle->type == WINSYS_HANDLE_TYPE_FD) {
105 if (scanout) {
106 struct drm_prime_handle args = {
107 .handle = scanout->handle,
108 .flags = DRM_CLOEXEC,
109 };
110
111 int ret = drmIoctl(screen->ro->kms_fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
112 if (ret == -1)
113 return FALSE;
114
115 handle->stride = scanout->stride;
116 handle->handle = args.fd;
117
118 return TRUE;
119 } else
120 return screen->driver->export_bo(screen, rsrc->bo->gem_handle, rsrc->bo->slices[0].stride, handle);
121 }
122
123 return FALSE;
124 }
125
126 static void
127 panfrost_flush_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
128 {
129 //DBG("TODO %s\n", __func__);
130 }
131
132 static void
133 panfrost_blit(struct pipe_context *pipe,
134 const struct pipe_blit_info *info)
135 {
136 if (util_try_blit_via_copy_region(pipe, info))
137 return;
138
139 /* TODO */
140 DBG("Unhandled blit.\n");
141
142 return;
143 }
144
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)
149 {
150 struct pipe_surface *ps = NULL;
151
152 ps = CALLOC_STRUCT(pipe_surface);
153
154 if (ps) {
155 pipe_reference_init(&ps->reference, 1);
156 pipe_resource_reference(&ps->texture, pt);
157 ps->context = pipe;
158 ps->format = surf_tmpl->format;
159
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;
167 } else {
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);
175 }
176 }
177
178 return ps;
179 }
180
181 static void
182 panfrost_surface_destroy(struct pipe_context *pipe,
183 struct pipe_surface *surf)
184 {
185 assert(surf->texture);
186 pipe_resource_reference(&surf->texture, NULL);
187 free(surf);
188 }
189
190 static void
191 panfrost_setup_slices(const struct pipe_resource *tmpl, struct panfrost_bo *bo)
192 {
193 unsigned width = tmpl->width0;
194 unsigned height = tmpl->height0;
195 unsigned bytes_per_pixel = util_format_get_blocksize(tmpl->format);
196
197 unsigned offset = 0;
198
199 for (unsigned l = 0; l <= tmpl->last_level; ++l) {
200 struct panfrost_slice *slice = &bo->slices[l];
201
202 unsigned effective_width = width;
203 unsigned effective_height = height;
204
205 /* Tiled operates blockwise; linear is packed */
206
207 if (bo->layout == PAN_TILED) {
208 effective_width = ALIGN(effective_width, 16);
209 effective_height = ALIGN(effective_height, 16);
210 }
211
212 slice->offset = offset;
213 slice->stride = bytes_per_pixel * effective_width;
214
215 offset += slice->stride * effective_height;
216
217 width = u_minify(width, 1);
218 height = u_minify(height, 1);
219 }
220
221 bo->size = ALIGN(offset, 4096);
222 }
223
224 static struct panfrost_bo *
225 panfrost_create_bo(struct panfrost_screen *screen, const struct pipe_resource *template)
226 {
227 struct panfrost_bo *bo = CALLOC_STRUCT(panfrost_bo);
228
229 /* Based on the usage, figure out what storing will be used. There are
230 * various tradeoffs:
231 *
232 * Linear: the basic format, bad for memory bandwidth, bad for cache
233 * use. Zero-copy, though. Renderable.
234 *
235 * Tiled: Not compressed, but cache-optimized. Expensive to write into
236 * (due to software tiling), but cheap to sample from. Ideal for most
237 * textures.
238 *
239 * AFBC: Compressed and renderable (so always desirable for non-scanout
240 * rendertargets). Cheap to sample from. The format is black box, so we
241 * can't read/write from software.
242 */
243
244 /* Tiling textures is almost always faster, unless we only use it once */
245 bool should_tile = (template->usage != PIPE_USAGE_STREAM) && (template->bind & PIPE_BIND_SAMPLER_VIEW);
246
247 /* For unclear reasons, depth/stencil is faster linear than AFBC, so
248 * make sure it's linear */
249
250 if (template->bind & PIPE_BIND_DEPTH_STENCIL)
251 should_tile = false;
252
253 /* Set the layout appropriately */
254 bo->layout = should_tile ? PAN_TILED : PAN_LINEAR;
255
256 panfrost_setup_slices(template, bo);
257
258 if (bo->layout == PAN_TILED || bo->layout == PAN_LINEAR) {
259 struct panfrost_memory mem;
260
261 screen->driver->allocate_slab(screen, &mem, bo->size / 4096, true, 0, 0, 0);
262
263 bo->cpu = mem.cpu;
264 bo->gpu = mem.gpu;
265 bo->gem_handle = mem.gem_handle;
266 }
267
268 return bo;
269 }
270
271 static struct pipe_resource *
272 panfrost_resource_create(struct pipe_screen *screen,
273 const struct pipe_resource *template)
274 {
275 struct panfrost_resource *so = CALLOC_STRUCT(panfrost_resource);
276 struct panfrost_screen *pscreen = (struct panfrost_screen *) screen;
277
278 so->base = *template;
279 so->base.screen = screen;
280
281 pipe_reference_init(&so->base.reference, 1);
282
283 /* Make sure we're familiar */
284 switch (template->target) {
285 case PIPE_BUFFER:
286 case PIPE_TEXTURE_1D:
287 case PIPE_TEXTURE_2D:
288 case PIPE_TEXTURE_3D:
289 case PIPE_TEXTURE_RECT:
290 break;
291 default:
292 DBG("Unknown texture target %d\n", template->target);
293 assert(0);
294 }
295
296 if (template->bind & PIPE_BIND_DISPLAY_TARGET ||
297 template->bind & PIPE_BIND_SCANOUT ||
298 template->bind & PIPE_BIND_SHARED) {
299 struct pipe_resource scanout_templat = *template;
300 struct renderonly_scanout *scanout;
301 struct winsys_handle handle;
302
303 scanout = renderonly_scanout_for_resource(&scanout_templat,
304 pscreen->ro, &handle);
305 if (!scanout)
306 return NULL;
307
308 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
309 /* TODO: handle modifiers? */
310 so = pan_resource(screen->resource_from_handle(screen, template,
311 &handle,
312 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE));
313 close(handle.handle);
314 if (!so)
315 return NULL;
316
317 so->scanout = scanout;
318 pscreen->display_target = so;
319 } else {
320 so->bo = panfrost_create_bo(pscreen, template);
321 }
322
323 return (struct pipe_resource *)so;
324 }
325
326 static void
327 panfrost_destroy_bo(struct panfrost_screen *screen, struct panfrost_bo *pbo)
328 {
329 struct panfrost_bo *bo = (struct panfrost_bo *)pbo;
330
331 if ((bo->layout == PAN_LINEAR || bo->layout == PAN_TILED) &&
332 !bo->imported) {
333 struct panfrost_memory mem = {
334 .cpu = bo->cpu,
335 .gpu = bo->gpu,
336 .size = bo->size,
337 .gem_handle = bo->gem_handle,
338 };
339
340 screen->driver->free_slab(screen, &mem);
341 }
342
343 if (bo->layout == PAN_AFBC) {
344 /* TODO */
345 DBG("--leaking afbc (%d bytes)--\n", bo->afbc_metadata_size);
346 }
347
348 if (bo->has_checksum) {
349 /* TODO */
350 DBG("--leaking checksum (%zd bytes)--\n", bo->checksum_slab.size);
351 }
352
353 if (bo->imported) {
354 screen->driver->free_imported_bo(screen, bo);
355 }
356 }
357
358 static void
359 panfrost_resource_destroy(struct pipe_screen *screen,
360 struct pipe_resource *pt)
361 {
362 struct panfrost_screen *pscreen = pan_screen(screen);
363 struct panfrost_resource *rsrc = (struct panfrost_resource *) pt;
364
365 if (rsrc->scanout)
366 renderonly_scanout_destroy(rsrc->scanout, pscreen->ro);
367
368 if (rsrc->bo)
369 panfrost_destroy_bo(pscreen, rsrc->bo);
370
371 FREE(rsrc);
372 }
373
374 static void *
375 panfrost_transfer_map(struct pipe_context *pctx,
376 struct pipe_resource *resource,
377 unsigned level,
378 unsigned usage, /* a combination of PIPE_TRANSFER_x */
379 const struct pipe_box *box,
380 struct pipe_transfer **out_transfer)
381 {
382 int bytes_per_pixel = util_format_get_blocksize(resource->format);
383 struct panfrost_bo *bo = pan_resource(resource)->bo;
384
385 struct panfrost_gtransfer *transfer = CALLOC_STRUCT(panfrost_gtransfer);
386 transfer->base.level = level;
387 transfer->base.usage = usage;
388 transfer->base.box = *box;
389 transfer->base.stride = bo->slices[level].stride;
390 transfer->base.layer_stride = bytes_per_pixel * resource->width0; /* TODO: Cubemaps */
391 assert(!transfer->base.box.z);
392
393 pipe_resource_reference(&transfer->base.resource, resource);
394
395 *out_transfer = &transfer->base;
396
397 if (resource->bind & PIPE_BIND_DISPLAY_TARGET ||
398 resource->bind & PIPE_BIND_SCANOUT ||
399 resource->bind & PIPE_BIND_SHARED) {
400 /* Mipmapped readpixels?! */
401 assert(level == 0);
402
403 /* Force a flush -- kill the pipeline */
404 panfrost_flush(pctx, NULL, PIPE_FLUSH_END_OF_FRAME);
405 }
406
407 if (bo->layout != PAN_LINEAR) {
408 /* Non-linear resources need to be indirectly mapped */
409
410 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
411 return NULL;
412
413 /* TODO: Reads */
414 transfer->map = malloc(ALIGN(box->width, 16) * ALIGN(box->height, 16) * bytes_per_pixel);
415
416 return transfer->map;
417 } else {
418 return bo->cpu
419 + bo->slices[level].offset
420 + transfer->base.box.y * bo->slices[level].stride
421 + transfer->base.box.x * bytes_per_pixel;
422 }
423 }
424
425 static void
426 panfrost_tile_texture(struct panfrost_screen *screen, struct panfrost_resource *rsrc, struct panfrost_gtransfer *trans)
427 {
428 struct panfrost_bo *bo = (struct panfrost_bo *)rsrc->bo;
429
430 unsigned level = trans->base.level;
431
432 assert(!trans->base.box.z);
433
434 panfrost_texture_swizzle(
435 trans->base.box.x,
436 trans->base.box.y,
437 trans->base.box.width,
438 trans->base.box.height,
439 util_format_get_blocksize(rsrc->base.format),
440 bo->slices[level].stride,
441 trans->map,
442 bo->cpu + bo->slices[level].offset);
443 }
444
445 static void
446 panfrost_unmap_bo(struct panfrost_context *ctx,
447 struct pipe_transfer *transfer)
448 {
449 struct panfrost_gtransfer *trans = pan_transfer(transfer);
450 struct panfrost_bo *bo = (struct panfrost_bo *)pan_resource(transfer->resource)->bo;
451
452 if (transfer->usage & PIPE_TRANSFER_WRITE) {
453 if (transfer->resource->target == PIPE_TEXTURE_2D) {
454 struct panfrost_resource *prsrc = (struct panfrost_resource *) transfer->resource;
455
456 /* Gallium thinks writeback happens here; instead, this is our cue to tile */
457 if (bo->layout == PAN_AFBC) {
458 DBG("Warning: writes to afbc surface can't possibly work out well for you...\n");
459 } else if (bo->layout == PAN_TILED) {
460 struct pipe_context *gallium = (struct pipe_context *) ctx;
461 struct panfrost_screen *screen = pan_screen(gallium->screen);
462 panfrost_tile_texture(screen, prsrc, trans);
463 }
464 }
465 }
466
467 free(trans->map);
468 }
469
470 static void
471 panfrost_transfer_unmap(struct pipe_context *pctx,
472 struct pipe_transfer *transfer)
473 {
474 struct panfrost_context *ctx = pan_context(pctx);
475
476 panfrost_unmap_bo(ctx, transfer);
477
478 /* Derefence the resource */
479 pipe_resource_reference(&transfer->resource, NULL);
480
481 /* Transfer itself is CALLOCed at the moment */
482 free(transfer);
483 }
484
485 static struct pb_slab *
486 panfrost_slab_alloc(void *priv, unsigned heap, unsigned entry_size, unsigned group_index)
487 {
488 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
489 struct panfrost_memory *mem = CALLOC_STRUCT(panfrost_memory);
490
491 size_t slab_size = (1 << (MAX_SLAB_ENTRY_SIZE + 1));
492
493 mem->slab.num_entries = slab_size / entry_size;
494 mem->slab.num_free = mem->slab.num_entries;
495
496 LIST_INITHEAD(&mem->slab.free);
497 for (unsigned i = 0; i < mem->slab.num_entries; ++i) {
498 /* Create a slab entry */
499 struct panfrost_memory_entry *entry = CALLOC_STRUCT(panfrost_memory_entry);
500 entry->offset = entry_size * i;
501
502 entry->base.slab = &mem->slab;
503 entry->base.group_index = group_index;
504
505 LIST_ADDTAIL(&entry->base.head, &mem->slab.free);
506 }
507
508 /* Actually allocate the memory from kernel-space. Mapped, same_va, no
509 * special flags */
510
511 screen->driver->allocate_slab(screen, mem, slab_size / 4096, true, 0, 0, 0);
512
513 return &mem->slab;
514 }
515
516 static bool
517 panfrost_slab_can_reclaim(void *priv, struct pb_slab_entry *entry)
518 {
519 struct panfrost_memory_entry *p_entry = (struct panfrost_memory_entry *) entry;
520 return p_entry->freed;
521 }
522
523 static void
524 panfrost_slab_free(void *priv, struct pb_slab *slab)
525 {
526 struct panfrost_memory *mem = (struct panfrost_memory *) slab;
527 struct panfrost_screen *screen = (struct panfrost_screen *) priv;
528
529 screen->driver->free_slab(screen, mem);
530 }
531
532 static void
533 panfrost_invalidate_resource(struct pipe_context *pctx, struct pipe_resource *prsc)
534 {
535 //DBG("TODO %s\n", __func__);
536 }
537
538 static enum pipe_format
539 panfrost_resource_get_internal_format(struct pipe_resource *prsrc)
540 {
541 return prsrc->format;
542 }
543
544 static void
545 panfrost_resource_set_stencil(struct pipe_resource *prsrc,
546 struct pipe_resource *stencil)
547 {
548 pan_resource(prsrc)->separate_stencil = pan_resource(stencil);
549 }
550
551 static struct pipe_resource *
552 panfrost_resource_get_stencil(struct pipe_resource *prsrc)
553 {
554 return &pan_resource(prsrc)->separate_stencil->base;
555 }
556
557 static const struct u_transfer_vtbl transfer_vtbl = {
558 .resource_create = panfrost_resource_create,
559 .resource_destroy = panfrost_resource_destroy,
560 .transfer_map = panfrost_transfer_map,
561 .transfer_unmap = panfrost_transfer_unmap,
562 .transfer_flush_region = u_default_transfer_flush_region,
563 .get_internal_format = panfrost_resource_get_internal_format,
564 .set_stencil = panfrost_resource_set_stencil,
565 .get_stencil = panfrost_resource_get_stencil,
566 };
567
568 void
569 panfrost_resource_screen_init(struct panfrost_screen *pscreen)
570 {
571 //pscreen->base.resource_create_with_modifiers =
572 // panfrost_resource_create_with_modifiers;
573 pscreen->base.resource_create = u_transfer_helper_resource_create;
574 pscreen->base.resource_destroy = u_transfer_helper_resource_destroy;
575 pscreen->base.resource_from_handle = panfrost_resource_from_handle;
576 pscreen->base.resource_get_handle = panfrost_resource_get_handle;
577 pscreen->base.transfer_helper = u_transfer_helper_create(&transfer_vtbl,
578 true, false,
579 true, true);
580
581 pb_slabs_init(&pscreen->slabs,
582 MIN_SLAB_ENTRY_SIZE,
583 MAX_SLAB_ENTRY_SIZE,
584
585 3, /* Number of heaps */
586
587 pscreen,
588
589 panfrost_slab_can_reclaim,
590 panfrost_slab_alloc,
591 panfrost_slab_free);
592 }
593
594 void
595 panfrost_resource_context_init(struct pipe_context *pctx)
596 {
597 pctx->transfer_map = u_transfer_helper_transfer_map;
598 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
599 pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
600 pctx->buffer_subdata = u_default_buffer_subdata;
601 pctx->create_surface = panfrost_create_surface;
602 pctx->surface_destroy = panfrost_surface_destroy;
603 pctx->resource_copy_region = util_resource_copy_region;
604 pctx->blit = panfrost_blit;
605 pctx->flush_resource = panfrost_flush_resource;
606 pctx->invalidate_resource = panfrost_invalidate_resource;
607 pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
608 pctx->buffer_subdata = u_default_buffer_subdata;
609 pctx->texture_subdata = u_default_texture_subdata;
610 }