1 /*
2  * Copyright © 2014-2017 Broadcom
3  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "pipe/p_defines.h"
26 #include "util/u_memory.h"
27 #include "util/format/u_format.h"
28 #include "util/u_inlines.h"
29 #include "util/u_surface.h"
30 #include "util/u_transfer_helper.h"
31 #include "util/u_upload_mgr.h"
32 #include "util/format/u_format_zs.h"
33 #include "util/u_drm.h"
34 
35 #include "drm-uapi/drm_fourcc.h"
36 #include "v3d_screen.h"
37 #include "v3d_context.h"
38 #include "v3d_resource.h"
39 #include "v3d_tiling.h"
40 #include "broadcom/cle/v3d_packet_v33_pack.h"
41 
42 static void
v3d_debug_resource_layout(struct v3d_resource * rsc,const char * caller)43 v3d_debug_resource_layout(struct v3d_resource *rsc, const char *caller)
44 {
45         if (!(V3D_DEBUG & V3D_DEBUG_SURFACE))
46                 return;
47 
48         struct pipe_resource *prsc = &rsc->base;
49 
50         if (prsc->target == PIPE_BUFFER) {
51                 fprintf(stderr,
52                         "rsc %s %p (format %s), %dx%d buffer @0x%08x-0x%08x\n",
53                         caller, rsc,
54                         util_format_short_name(prsc->format),
55                         prsc->width0, prsc->height0,
56                         rsc->bo->offset,
57                         rsc->bo->offset + rsc->bo->size - 1);
58                 return;
59         }
60 
61         static const char *const tiling_descriptions[] = {
62                 [VC5_TILING_RASTER] = "R",
63                 [VC5_TILING_LINEARTILE] = "LT",
64                 [VC5_TILING_UBLINEAR_1_COLUMN] = "UB1",
65                 [VC5_TILING_UBLINEAR_2_COLUMN] = "UB2",
66                 [VC5_TILING_UIF_NO_XOR] = "UIF",
67                 [VC5_TILING_UIF_XOR] = "UIF^",
68         };
69 
70         for (int i = 0; i <= prsc->last_level; i++) {
71                 struct v3d_resource_slice *slice = &rsc->slices[i];
72 
73                 int level_width = slice->stride / rsc->cpp;
74                 int level_height = slice->padded_height;
75                 int level_depth =
76                         u_minify(util_next_power_of_two(prsc->depth0), i);
77 
78                 fprintf(stderr,
79                         "rsc %s %p (format %s), %dx%d: "
80                         "level %d (%s) %dx%dx%d -> %dx%dx%d, stride %d@0x%08x\n",
81                         caller, rsc,
82                         util_format_short_name(prsc->format),
83                         prsc->width0, prsc->height0,
84                         i, tiling_descriptions[slice->tiling],
85                         u_minify(prsc->width0, i),
86                         u_minify(prsc->height0, i),
87                         u_minify(prsc->depth0, i),
88                         level_width,
89                         level_height,
90                         level_depth,
91                         slice->stride,
92                         rsc->bo->offset + slice->offset);
93         }
94 }
95 
96 static bool
v3d_resource_bo_alloc(struct v3d_resource * rsc)97 v3d_resource_bo_alloc(struct v3d_resource *rsc)
98 {
99         struct pipe_resource *prsc = &rsc->base;
100         struct pipe_screen *pscreen = prsc->screen;
101         struct v3d_bo *bo;
102 
103         bo = v3d_bo_alloc(v3d_screen(pscreen), rsc->size, "resource");
104         if (bo) {
105                 v3d_bo_unreference(&rsc->bo);
106                 rsc->bo = bo;
107                 v3d_debug_resource_layout(rsc, "alloc");
108                 return true;
109         } else {
110                 return false;
111         }
112 }
113 
114 static void
v3d_resource_transfer_unmap(struct pipe_context * pctx,struct pipe_transfer * ptrans)115 v3d_resource_transfer_unmap(struct pipe_context *pctx,
116                             struct pipe_transfer *ptrans)
117 {
118         struct v3d_context *v3d = v3d_context(pctx);
119         struct v3d_transfer *trans = v3d_transfer(ptrans);
120 
121         if (trans->map) {
122                 struct v3d_resource *rsc = v3d_resource(ptrans->resource);
123                 struct v3d_resource_slice *slice = &rsc->slices[ptrans->level];
124 
125                 if (ptrans->usage & PIPE_MAP_WRITE) {
126                         for (int z = 0; z < ptrans->box.depth; z++) {
127                                 void *dst = rsc->bo->map +
128                                         v3d_layer_offset(&rsc->base,
129                                                          ptrans->level,
130                                                          ptrans->box.z + z);
131                                 v3d_store_tiled_image(dst,
132                                                       slice->stride,
133                                                       (trans->map +
134                                                        ptrans->stride *
135                                                        ptrans->box.height * z),
136                                                       ptrans->stride,
137                                                       slice->tiling, rsc->cpp,
138                                                       slice->padded_height,
139                                                       &ptrans->box);
140                         }
141                 }
142                 free(trans->map);
143         }
144 
145         pipe_resource_reference(&ptrans->resource, NULL);
146         slab_free(&v3d->transfer_pool, ptrans);
147 }
148 
149 static void
rebind_sampler_views(struct v3d_context * v3d,struct v3d_resource * rsc)150 rebind_sampler_views(struct v3d_context *v3d,
151                      struct v3d_resource *rsc)
152 {
153         for (int st = 0; st < PIPE_SHADER_TYPES; st++) {
154                 struct v3d_texture_stateobj *tex = v3d->tex + st;
155 
156                 for (unsigned i = 0; i < tex->num_textures; i++) {
157                         struct pipe_sampler_view *psview = tex->textures[i];
158 
159                         if (psview->texture != &rsc->base)
160                                 continue;
161 
162                         struct v3d_sampler_view *sview =
163                                 v3d_sampler_view(psview);
164 
165                         v3d_create_texture_shader_state_bo(v3d, sview);
166 
167                         v3d_flag_dirty_sampler_state(v3d, st);
168                 }
169         }
170 }
171 
172 static void
v3d_map_usage_prep(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned usage)173 v3d_map_usage_prep(struct pipe_context *pctx,
174                    struct pipe_resource *prsc,
175                    unsigned usage)
176 {
177         struct v3d_context *v3d = v3d_context(pctx);
178         struct v3d_resource *rsc = v3d_resource(prsc);
179 
180         if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
181                 if (v3d_resource_bo_alloc(rsc)) {
182                         /* If it might be bound as one of our vertex buffers
183                          * or UBOs, make sure we re-emit vertex buffer state
184                          * or uniforms.
185                          */
186                         if (prsc->bind & PIPE_BIND_VERTEX_BUFFER)
187                                 v3d->dirty |= VC5_DIRTY_VTXBUF;
188                         if (prsc->bind & PIPE_BIND_CONSTANT_BUFFER)
189                                 v3d->dirty |= VC5_DIRTY_CONSTBUF;
190                         if (prsc->bind & PIPE_BIND_SAMPLER_VIEW)
191                                 rebind_sampler_views(v3d, rsc);
192                 } else {
193                         /* If we failed to reallocate, flush users so that we
194                          * don't violate any syncing requirements.
195                          */
196                         v3d_flush_jobs_reading_resource(v3d, prsc,
197                                                         V3D_FLUSH_DEFAULT,
198                                                         false);
199                 }
200         } else if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
201                 /* If we're writing and the buffer is being used by the CL, we
202                  * have to flush the CL first.  If we're only reading, we need
203                  * to flush if the CL has written our buffer.
204                  */
205                 if (usage & PIPE_MAP_WRITE) {
206                         v3d_flush_jobs_reading_resource(v3d, prsc,
207                                                         V3D_FLUSH_ALWAYS,
208                                                         false);
209                 } else {
210                         v3d_flush_jobs_writing_resource(v3d, prsc,
211                                                         V3D_FLUSH_ALWAYS,
212                                                         false);
213                 }
214         }
215 
216         if (usage & PIPE_MAP_WRITE) {
217                 rsc->writes++;
218                 rsc->initialized_buffers = ~0;
219         }
220 }
221 
222 static void *
v3d_resource_transfer_map(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** pptrans)223 v3d_resource_transfer_map(struct pipe_context *pctx,
224                           struct pipe_resource *prsc,
225                           unsigned level, unsigned usage,
226                           const struct pipe_box *box,
227                           struct pipe_transfer **pptrans)
228 {
229         struct v3d_context *v3d = v3d_context(pctx);
230         struct v3d_resource *rsc = v3d_resource(prsc);
231         struct v3d_transfer *trans;
232         struct pipe_transfer *ptrans;
233         enum pipe_format format = prsc->format;
234         char *buf;
235 
236         /* MSAA maps should have been handled by u_transfer_helper. */
237         assert(prsc->nr_samples <= 1);
238 
239         /* Upgrade DISCARD_RANGE to WHOLE_RESOURCE if the whole resource is
240          * being mapped.
241          */
242         if ((usage & PIPE_MAP_DISCARD_RANGE) &&
243             !(usage & PIPE_MAP_UNSYNCHRONIZED) &&
244             !(prsc->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) &&
245             prsc->last_level == 0 &&
246             prsc->width0 == box->width &&
247             prsc->height0 == box->height &&
248             prsc->depth0 == box->depth &&
249             prsc->array_size == 1 &&
250             rsc->bo->private) {
251                 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
252         }
253 
254         v3d_map_usage_prep(pctx, prsc, usage);
255 
256         trans = slab_alloc(&v3d->transfer_pool);
257         if (!trans)
258                 return NULL;
259 
260         /* XXX: Handle DONTBLOCK, DISCARD_RANGE, PERSISTENT, COHERENT. */
261 
262         /* slab_alloc_st() doesn't zero: */
263         memset(trans, 0, sizeof(*trans));
264         ptrans = &trans->base;
265 
266         pipe_resource_reference(&ptrans->resource, prsc);
267         ptrans->level = level;
268         ptrans->usage = usage;
269         ptrans->box = *box;
270 
271         /* Note that the current kernel implementation is synchronous, so no
272          * need to do syncing stuff here yet.
273          */
274 
275         if (usage & PIPE_MAP_UNSYNCHRONIZED)
276                 buf = v3d_bo_map_unsynchronized(rsc->bo);
277         else
278                 buf = v3d_bo_map(rsc->bo);
279         if (!buf) {
280                 fprintf(stderr, "Failed to map bo\n");
281                 goto fail;
282         }
283 
284         *pptrans = ptrans;
285 
286         /* Our load/store routines work on entire compressed blocks. */
287         ptrans->box.x /= util_format_get_blockwidth(format);
288         ptrans->box.y /= util_format_get_blockheight(format);
289         ptrans->box.width = DIV_ROUND_UP(ptrans->box.width,
290                                          util_format_get_blockwidth(format));
291         ptrans->box.height = DIV_ROUND_UP(ptrans->box.height,
292                                           util_format_get_blockheight(format));
293 
294         struct v3d_resource_slice *slice = &rsc->slices[level];
295         if (rsc->tiled) {
296                 /* No direct mappings of tiled, since we need to manually
297                  * tile/untile.
298                  */
299                 if (usage & PIPE_MAP_DIRECTLY)
300                         return NULL;
301 
302                 ptrans->stride = ptrans->box.width * rsc->cpp;
303                 ptrans->layer_stride = ptrans->stride * ptrans->box.height;
304 
305                 trans->map = malloc(ptrans->layer_stride * ptrans->box.depth);
306 
307                 if (usage & PIPE_MAP_READ) {
308                         for (int z = 0; z < ptrans->box.depth; z++) {
309                                 void *src = rsc->bo->map +
310                                         v3d_layer_offset(&rsc->base,
311                                                          ptrans->level,
312                                                          ptrans->box.z + z);
313                                 v3d_load_tiled_image((trans->map +
314                                                       ptrans->stride *
315                                                       ptrans->box.height * z),
316                                                      ptrans->stride,
317                                                      src,
318                                                      slice->stride,
319                                                      slice->tiling, rsc->cpp,
320                                                      slice->padded_height,
321                                              &ptrans->box);
322                         }
323                 }
324                 return trans->map;
325         } else {
326                 ptrans->stride = slice->stride;
327                 ptrans->layer_stride = rsc->cube_map_stride;
328 
329                 return buf + slice->offset +
330                         ptrans->box.y * ptrans->stride +
331                         ptrans->box.x * rsc->cpp +
332                         ptrans->box.z * rsc->cube_map_stride;
333         }
334 
335 
336 fail:
337         v3d_resource_transfer_unmap(pctx, ptrans);
338         return NULL;
339 }
340 
341 static void
v3d_texture_subdata(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned level,unsigned usage,const struct pipe_box * box,const void * data,unsigned stride,unsigned layer_stride)342 v3d_texture_subdata(struct pipe_context *pctx,
343                     struct pipe_resource *prsc,
344                     unsigned level,
345                     unsigned usage,
346                     const struct pipe_box *box,
347                     const void *data,
348                     unsigned stride,
349                     unsigned layer_stride)
350 {
351         struct v3d_resource *rsc = v3d_resource(prsc);
352         struct v3d_resource_slice *slice = &rsc->slices[level];
353 
354         /* For a direct mapping, we can just take the u_transfer path. */
355         if (!rsc->tiled) {
356                 return u_default_texture_subdata(pctx, prsc, level, usage, box,
357                                                  data, stride, layer_stride);
358         }
359 
360         /* Otherwise, map and store the texture data directly into the tiled
361          * texture.  Note that gallium's texture_subdata may be called with
362          * obvious usage flags missing!
363          */
364         v3d_map_usage_prep(pctx, prsc, usage | (PIPE_MAP_WRITE |
365                                                 PIPE_MAP_DISCARD_RANGE));
366 
367         void *buf;
368         if (usage & PIPE_MAP_UNSYNCHRONIZED)
369                 buf = v3d_bo_map_unsynchronized(rsc->bo);
370         else
371                 buf = v3d_bo_map(rsc->bo);
372 
373         for (int i = 0; i < box->depth; i++) {
374                 v3d_store_tiled_image(buf +
375                                       v3d_layer_offset(&rsc->base,
376                                                        level,
377                                                        box->z + i),
378                                       slice->stride,
379                                       (void *)data + layer_stride * i,
380                                       stride,
381                                       slice->tiling, rsc->cpp, slice->padded_height,
382                                       box);
383         }
384 }
385 
386 static void
v3d_resource_destroy(struct pipe_screen * pscreen,struct pipe_resource * prsc)387 v3d_resource_destroy(struct pipe_screen *pscreen,
388                      struct pipe_resource *prsc)
389 {
390         struct v3d_screen *screen = v3d_screen(pscreen);
391         struct v3d_resource *rsc = v3d_resource(prsc);
392 
393         if (rsc->scanout)
394                 renderonly_scanout_destroy(rsc->scanout, screen->ro);
395 
396         v3d_bo_unreference(&rsc->bo);
397         free(rsc);
398 }
399 
400 static bool
v3d_resource_get_handle(struct pipe_screen * pscreen,struct pipe_context * pctx,struct pipe_resource * prsc,struct winsys_handle * whandle,unsigned usage)401 v3d_resource_get_handle(struct pipe_screen *pscreen,
402                         struct pipe_context *pctx,
403                         struct pipe_resource *prsc,
404                         struct winsys_handle *whandle,
405                         unsigned usage)
406 {
407         struct v3d_screen *screen = v3d_screen(pscreen);
408         struct v3d_resource *rsc = v3d_resource(prsc);
409         struct v3d_bo *bo = rsc->bo;
410 
411         whandle->stride = rsc->slices[0].stride;
412         whandle->offset = 0;
413 
414         /* If we're passing some reference to our BO out to some other part of
415          * the system, then we can't do any optimizations about only us being
416          * the ones seeing it (like BO caching).
417          */
418         bo->private = false;
419 
420         if (rsc->tiled) {
421                 /* A shared tiled buffer should always be allocated as UIF,
422                  * not UBLINEAR or LT.
423                  */
424                 assert(rsc->slices[0].tiling == VC5_TILING_UIF_XOR ||
425                        rsc->slices[0].tiling == VC5_TILING_UIF_NO_XOR);
426                 whandle->modifier = DRM_FORMAT_MOD_BROADCOM_UIF;
427         } else {
428                 whandle->modifier = DRM_FORMAT_MOD_LINEAR;
429         }
430 
431         switch (whandle->type) {
432         case WINSYS_HANDLE_TYPE_SHARED:
433                 return v3d_bo_flink(bo, &whandle->handle);
434         case WINSYS_HANDLE_TYPE_KMS:
435                 if (screen->ro) {
436                         assert(rsc->scanout);
437                         bool ok = renderonly_get_handle(rsc->scanout, whandle);
438                         whandle->stride = rsc->slices[0].stride;
439                         return ok;
440                 }
441                 whandle->handle = bo->handle;
442                 return true;
443         case WINSYS_HANDLE_TYPE_FD:
444                 whandle->handle = v3d_bo_get_dmabuf(bo);
445                 return whandle->handle != -1;
446         }
447 
448         return false;
449 }
450 
451 #define PAGE_UB_ROWS (VC5_UIFCFG_PAGE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
452 #define PAGE_UB_ROWS_TIMES_1_5 ((PAGE_UB_ROWS * 3) >> 1)
453 #define PAGE_CACHE_UB_ROWS (VC5_PAGE_CACHE_SIZE / VC5_UIFBLOCK_ROW_SIZE)
454 #define PAGE_CACHE_MINUS_1_5_UB_ROWS (PAGE_CACHE_UB_ROWS - PAGE_UB_ROWS_TIMES_1_5)
455 
456 /**
457  * Computes the HW's UIFblock padding for a given height/cpp.
458  *
459  * The goal of the padding is to keep pages of the same color (bank number) at
460  * least half a page away from each other vertically when crossing between
461  * between columns of UIF blocks.
462  */
463 static uint32_t
v3d_get_ub_pad(struct v3d_resource * rsc,uint32_t height)464 v3d_get_ub_pad(struct v3d_resource *rsc, uint32_t height)
465 {
466         uint32_t utile_h = v3d_utile_height(rsc->cpp);
467         uint32_t uif_block_h = utile_h * 2;
468         uint32_t height_ub = height / uif_block_h;
469 
470         uint32_t height_offset_in_pc = height_ub % PAGE_CACHE_UB_ROWS;
471 
472         /* For the perfectly-aligned-for-UIF-XOR case, don't add any pad. */
473         if (height_offset_in_pc == 0)
474                 return 0;
475 
476         /* Try padding up to where we're offset by at least half a page. */
477         if (height_offset_in_pc < PAGE_UB_ROWS_TIMES_1_5) {
478                 /* If we fit entirely in the page cache, don't pad. */
479                 if (height_ub < PAGE_CACHE_UB_ROWS)
480                         return 0;
481                 else
482                         return PAGE_UB_ROWS_TIMES_1_5 - height_offset_in_pc;
483         }
484 
485         /* If we're close to being aligned to page cache size, then round up
486          * and rely on XOR.
487          */
488         if (height_offset_in_pc > PAGE_CACHE_MINUS_1_5_UB_ROWS)
489                 return PAGE_CACHE_UB_ROWS - height_offset_in_pc;
490 
491         /* Otherwise, we're far enough away (top and bottom) to not need any
492          * padding.
493          */
494         return 0;
495 }
496 
497 static void
v3d_setup_slices(struct v3d_resource * rsc,uint32_t winsys_stride,bool uif_top)498 v3d_setup_slices(struct v3d_resource *rsc, uint32_t winsys_stride,
499                  bool uif_top)
500 {
501         struct pipe_resource *prsc = &rsc->base;
502         uint32_t width = prsc->width0;
503         uint32_t height = prsc->height0;
504         uint32_t depth = prsc->depth0;
505         /* Note that power-of-two padding is based on level 1.  These are not
506          * equivalent to just util_next_power_of_two(dimension), because at a
507          * level 0 dimension of 9, the level 1 power-of-two padded value is 4,
508          * not 8.
509          */
510         uint32_t pot_width = 2 * util_next_power_of_two(u_minify(width, 1));
511         uint32_t pot_height = 2 * util_next_power_of_two(u_minify(height, 1));
512         uint32_t pot_depth = 2 * util_next_power_of_two(u_minify(depth, 1));
513         uint32_t offset = 0;
514         uint32_t utile_w = v3d_utile_width(rsc->cpp);
515         uint32_t utile_h = v3d_utile_height(rsc->cpp);
516         uint32_t uif_block_w = utile_w * 2;
517         uint32_t uif_block_h = utile_h * 2;
518         uint32_t block_width = util_format_get_blockwidth(prsc->format);
519         uint32_t block_height = util_format_get_blockheight(prsc->format);
520         bool msaa = prsc->nr_samples > 1;
521 
522         /* MSAA textures/renderbuffers are always laid out as single-level
523          * UIF.
524          */
525         uif_top |= msaa;
526 
527         /* Check some easy mistakes to make in a resource_create() call that
528          * will break our setup.
529          */
530         assert(prsc->array_size != 0);
531         assert(prsc->depth0 != 0);
532 
533         for (int i = prsc->last_level; i >= 0; i--) {
534                 struct v3d_resource_slice *slice = &rsc->slices[i];
535 
536                 uint32_t level_width, level_height, level_depth;
537                 if (i < 2) {
538                         level_width = u_minify(width, i);
539                         level_height = u_minify(height, i);
540                 } else {
541                         level_width = u_minify(pot_width, i);
542                         level_height = u_minify(pot_height, i);
543                 }
544                 if (i < 1)
545                         level_depth = u_minify(depth, i);
546                 else
547                         level_depth = u_minify(pot_depth, i);
548 
549                 if (msaa) {
550                         level_width *= 2;
551                         level_height *= 2;
552                 }
553 
554                 level_width = DIV_ROUND_UP(level_width, block_width);
555                 level_height = DIV_ROUND_UP(level_height, block_height);
556 
557                 if (!rsc->tiled) {
558                         slice->tiling = VC5_TILING_RASTER;
559                         if (prsc->target == PIPE_TEXTURE_1D)
560                                 level_width = align(level_width, 64 / rsc->cpp);
561                 } else {
562                         if ((i != 0 || !uif_top) &&
563                             (level_width <= utile_w ||
564                              level_height <= utile_h)) {
565                                 slice->tiling = VC5_TILING_LINEARTILE;
566                                 level_width = align(level_width, utile_w);
567                                 level_height = align(level_height, utile_h);
568                         } else if ((i != 0 || !uif_top) &&
569                                    level_width <= uif_block_w) {
570                                 slice->tiling = VC5_TILING_UBLINEAR_1_COLUMN;
571                                 level_width = align(level_width, uif_block_w);
572                                 level_height = align(level_height, uif_block_h);
573                         } else if ((i != 0 || !uif_top) &&
574                                    level_width <= 2 * uif_block_w) {
575                                 slice->tiling = VC5_TILING_UBLINEAR_2_COLUMN;
576                                 level_width = align(level_width, 2 * uif_block_w);
577                                 level_height = align(level_height, uif_block_h);
578                         } else {
579                                 /* We align the width to a 4-block column of
580                                  * UIF blocks, but we only align height to UIF
581                                  * blocks.
582                                  */
583                                 level_width = align(level_width,
584                                                     4 * uif_block_w);
585                                 level_height = align(level_height,
586                                                      uif_block_h);
587 
588                                 slice->ub_pad = v3d_get_ub_pad(rsc,
589                                                                level_height);
590                                 level_height += slice->ub_pad * uif_block_h;
591 
592                                 /* If the padding set us to to be aligned to
593                                  * the page cache size, then the HW will use
594                                  * the XOR bit on odd columns to get us
595                                  * perfectly misaligned
596                                  */
597                                 if ((level_height / uif_block_h) %
598                                     (VC5_PAGE_CACHE_SIZE /
599                                      VC5_UIFBLOCK_ROW_SIZE) == 0) {
600                                         slice->tiling = VC5_TILING_UIF_XOR;
601                                 } else {
602                                         slice->tiling = VC5_TILING_UIF_NO_XOR;
603                                 }
604                         }
605                 }
606 
607                 slice->offset = offset;
608                 if (winsys_stride)
609                         slice->stride = winsys_stride;
610                 else
611                         slice->stride = level_width * rsc->cpp;
612                 slice->padded_height = level_height;
613                 slice->size = level_height * slice->stride;
614 
615                 uint32_t slice_total_size = slice->size * level_depth;
616 
617                 /* The HW aligns level 1's base to a page if any of level 1 or
618                  * below could be UIF XOR.  The lower levels then inherit the
619                  * alignment for as long as necesary, thanks to being power of
620                  * two aligned.
621                  */
622                 if (i == 1 &&
623                     level_width > 4 * uif_block_w &&
624                     level_height > PAGE_CACHE_MINUS_1_5_UB_ROWS * uif_block_h) {
625                         slice_total_size = align(slice_total_size,
626                                                  VC5_UIFCFG_PAGE_SIZE);
627                 }
628 
629                 offset += slice_total_size;
630 
631         }
632         rsc->size = offset;
633 
634         /* UIF/UBLINEAR levels need to be aligned to UIF-blocks, and LT only
635          * needs to be aligned to utile boundaries.  Since tiles are laid out
636          * from small to big in memory, we need to align the later UIF slices
637          * to UIF blocks, if they were preceded by non-UIF-block-aligned LT
638          * slices.
639          *
640          * We additionally align to 4k, which improves UIF XOR performance.
641          */
642         uint32_t page_align_offset = (align(rsc->slices[0].offset, 4096) -
643                                       rsc->slices[0].offset);
644         if (page_align_offset) {
645                 rsc->size += page_align_offset;
646                 for (int i = 0; i <= prsc->last_level; i++)
647                         rsc->slices[i].offset += page_align_offset;
648         }
649 
650         /* Arrays and cube textures have a stride which is the distance from
651          * one full mipmap tree to the next (64b aligned).  For 3D textures,
652          * we need to program the stride between slices of miplevel 0.
653          */
654         if (prsc->target != PIPE_TEXTURE_3D) {
655                 rsc->cube_map_stride = align(rsc->slices[0].offset +
656                                              rsc->slices[0].size, 64);
657                 rsc->size += rsc->cube_map_stride * (prsc->array_size - 1);
658         } else {
659                 rsc->cube_map_stride = rsc->slices[0].size;
660         }
661 }
662 
663 uint32_t
v3d_layer_offset(struct pipe_resource * prsc,uint32_t level,uint32_t layer)664 v3d_layer_offset(struct pipe_resource *prsc, uint32_t level, uint32_t layer)
665 {
666         struct v3d_resource *rsc = v3d_resource(prsc);
667         struct v3d_resource_slice *slice = &rsc->slices[level];
668 
669         if (prsc->target == PIPE_TEXTURE_3D)
670                 return slice->offset + layer * slice->size;
671         else
672                 return slice->offset + layer * rsc->cube_map_stride;
673 }
674 
675 static struct v3d_resource *
v3d_resource_setup(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)676 v3d_resource_setup(struct pipe_screen *pscreen,
677                    const struct pipe_resource *tmpl)
678 {
679         struct v3d_screen *screen = v3d_screen(pscreen);
680         struct v3d_resource *rsc = CALLOC_STRUCT(v3d_resource);
681         if (!rsc)
682                 return NULL;
683         struct pipe_resource *prsc = &rsc->base;
684 
685         *prsc = *tmpl;
686 
687         pipe_reference_init(&prsc->reference, 1);
688         prsc->screen = pscreen;
689 
690         if (prsc->nr_samples <= 1 ||
691             screen->devinfo.ver >= 40 ||
692             util_format_is_depth_or_stencil(prsc->format)) {
693                 rsc->cpp = util_format_get_blocksize(prsc->format);
694                 if (screen->devinfo.ver < 40 && prsc->nr_samples > 1)
695                         rsc->cpp *= prsc->nr_samples;
696         } else {
697                 assert(v3d_rt_format_supported(&screen->devinfo, prsc->format));
698                 uint32_t output_image_format =
699                         v3d_get_rt_format(&screen->devinfo, prsc->format);
700                 uint32_t internal_type;
701                 uint32_t internal_bpp;
702                 v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
703                                                             output_image_format,
704                                                             &internal_type,
705                                                             &internal_bpp);
706                 switch (internal_bpp) {
707                 case V3D_INTERNAL_BPP_32:
708                         rsc->cpp = 4;
709                         break;
710                 case V3D_INTERNAL_BPP_64:
711                         rsc->cpp = 8;
712                         break;
713                 case V3D_INTERNAL_BPP_128:
714                         rsc->cpp = 16;
715                         break;
716                 }
717         }
718 
719         assert(rsc->cpp);
720 
721         return rsc;
722 }
723 
724 static struct pipe_resource *
v3d_resource_create_with_modifiers(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,const uint64_t * modifiers,int count)725 v3d_resource_create_with_modifiers(struct pipe_screen *pscreen,
726                                    const struct pipe_resource *tmpl,
727                                    const uint64_t *modifiers,
728                                    int count)
729 {
730         struct v3d_screen *screen = v3d_screen(pscreen);
731 
732         bool linear_ok = drm_find_modifier(DRM_FORMAT_MOD_LINEAR, modifiers, count);
733         struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
734         struct pipe_resource *prsc = &rsc->base;
735         /* Use a tiled layout if we can, for better 3D performance. */
736         bool should_tile = true;
737 
738         /* VBOs/PBOs are untiled (and 1 height). */
739         if (tmpl->target == PIPE_BUFFER)
740                 should_tile = false;
741 
742         /* Cursors are always linear, and the user can request linear as well.
743          */
744         if (tmpl->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR))
745                 should_tile = false;
746 
747         /* 1D and 1D_ARRAY textures are always raster-order. */
748         if (tmpl->target == PIPE_TEXTURE_1D ||
749             tmpl->target == PIPE_TEXTURE_1D_ARRAY)
750                 should_tile = false;
751 
752         /* Scanout BOs for simulator need to be linear for interaction with
753          * i965.
754          */
755         if (using_v3d_simulator &&
756             tmpl->bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
757                 should_tile = false;
758 
759         /* If using the old-school SCANOUT flag, we don't know what the screen
760          * might support other than linear. Just force linear.
761          */
762         if (tmpl->bind & PIPE_BIND_SCANOUT)
763                 should_tile = false;
764 
765         /* No user-specified modifier; determine our own. */
766         if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
767                 linear_ok = true;
768                 rsc->tiled = should_tile;
769         } else if (should_tile &&
770                    drm_find_modifier(DRM_FORMAT_MOD_BROADCOM_UIF,
771                                  modifiers, count)) {
772                 rsc->tiled = true;
773         } else if (linear_ok) {
774                 rsc->tiled = false;
775         } else {
776                 fprintf(stderr, "Unsupported modifier requested\n");
777                 goto fail;
778         }
779 
780         rsc->internal_format = prsc->format;
781 
782         v3d_setup_slices(rsc, 0, tmpl->bind & PIPE_BIND_SHARED);
783 
784         /* If we're in a renderonly setup, use the other device to perform our
785          * allocation and just import it to v3d.  The other device may be
786          * using CMA, and V3D can import from CMA but doesn't do CMA
787          * allocations on its own.
788          *
789          * We always allocate this way for SHARED, because get_handle will
790          * need a resource on the display fd.
791          */
792         if (screen->ro && (tmpl->bind & (PIPE_BIND_SCANOUT |
793                                          PIPE_BIND_SHARED))) {
794                 struct winsys_handle handle;
795                 struct pipe_resource scanout_tmpl = {
796                         .target = prsc->target,
797                         .format = PIPE_FORMAT_RGBA8888_UNORM,
798                         .width0 = 1024, /* one page */
799                         .height0 = align(rsc->size, 4096) / 4096,
800                         .depth0 = 1,
801                         .array_size = 1,
802                 };
803 
804                 rsc->scanout =
805                         renderonly_scanout_for_resource(&scanout_tmpl,
806                                                         screen->ro,
807                                                         &handle);
808 
809                 if (!rsc->scanout) {
810                         fprintf(stderr, "Failed to create scanout resource\n");
811                         return NULL;
812                 }
813                 assert(handle.type == WINSYS_HANDLE_TYPE_FD);
814                 rsc->bo = v3d_bo_open_dmabuf(screen, handle.handle);
815                 close(handle.handle);
816 
817                 if (!rsc->bo)
818                         goto fail;
819 
820                 v3d_debug_resource_layout(rsc, "renderonly");
821 
822                 return prsc;
823         } else {
824                 if (!v3d_resource_bo_alloc(rsc))
825                         goto fail;
826         }
827 
828         return prsc;
829 fail:
830         v3d_resource_destroy(pscreen, prsc);
831         return NULL;
832 }
833 
834 struct pipe_resource *
v3d_resource_create(struct pipe_screen * pscreen,const struct pipe_resource * tmpl)835 v3d_resource_create(struct pipe_screen *pscreen,
836                     const struct pipe_resource *tmpl)
837 {
838         const uint64_t mod = DRM_FORMAT_MOD_INVALID;
839         return v3d_resource_create_with_modifiers(pscreen, tmpl, &mod, 1);
840 }
841 
842 static struct pipe_resource *
v3d_resource_from_handle(struct pipe_screen * pscreen,const struct pipe_resource * tmpl,struct winsys_handle * whandle,unsigned usage)843 v3d_resource_from_handle(struct pipe_screen *pscreen,
844                          const struct pipe_resource *tmpl,
845                          struct winsys_handle *whandle,
846                          unsigned usage)
847 {
848         struct v3d_screen *screen = v3d_screen(pscreen);
849         struct v3d_resource *rsc = v3d_resource_setup(pscreen, tmpl);
850         struct pipe_resource *prsc = &rsc->base;
851         struct v3d_resource_slice *slice = &rsc->slices[0];
852 
853         if (!rsc)
854                 return NULL;
855 
856         switch (whandle->modifier) {
857         case DRM_FORMAT_MOD_LINEAR:
858                 rsc->tiled = false;
859                 break;
860         case DRM_FORMAT_MOD_BROADCOM_UIF:
861                 rsc->tiled = true;
862                 break;
863         case DRM_FORMAT_MOD_INVALID:
864                 rsc->tiled = screen->ro == NULL;
865                 break;
866         default:
867                 fprintf(stderr,
868                         "Attempt to import unsupported modifier 0x%llx\n",
869                         (long long)whandle->modifier);
870                 goto fail;
871         }
872 
873         switch (whandle->type) {
874         case WINSYS_HANDLE_TYPE_SHARED:
875                 rsc->bo = v3d_bo_open_name(screen, whandle->handle);
876                 break;
877         case WINSYS_HANDLE_TYPE_FD:
878                 rsc->bo = v3d_bo_open_dmabuf(screen, whandle->handle);
879                 break;
880         default:
881                 fprintf(stderr,
882                         "Attempt to import unsupported handle type %d\n",
883                         whandle->type);
884                 goto fail;
885         }
886 
887         if (!rsc->bo)
888                 goto fail;
889 
890         rsc->internal_format = prsc->format;
891 
892         v3d_setup_slices(rsc, whandle->stride, true);
893         v3d_debug_resource_layout(rsc, "import");
894 
895         if (whandle->offset != 0) {
896                 if (rsc->tiled) {
897                         fprintf(stderr,
898                                 "Attempt to import unsupported winsys offset %u\n",
899                                 whandle->offset);
900                         goto fail;
901                 }
902                 rsc->slices[0].offset += whandle->offset;
903 
904                 if (rsc->slices[0].offset + rsc->slices[0].size >
905                     rsc->bo->size) {
906                         fprintf(stderr, "Attempt to import "
907                                 "with overflowing offset (%d + %d > %d)\n",
908                                 whandle->offset,
909                                 rsc->slices[0].size,
910                                 rsc->bo->size);
911                          goto fail;
912                  }
913         }
914 
915         if (screen->ro) {
916                 /* Make sure that renderonly has a handle to our buffer in the
917                  * display's fd, so that a later renderonly_get_handle()
918                  * returns correct handles or GEM names.
919                  */
920                 rsc->scanout =
921                         renderonly_create_gpu_import_for_resource(prsc,
922                                                                   screen->ro,
923                                                                   NULL);
924                 if (!rsc->scanout) {
925                         fprintf(stderr, "Failed to create scanout resource.\n");
926                         goto fail;
927                 }
928         }
929 
930         if (rsc->tiled && whandle->stride != slice->stride) {
931                 static bool warned = false;
932                 if (!warned) {
933                         warned = true;
934                         fprintf(stderr,
935                                 "Attempting to import %dx%d %s with "
936                                 "unsupported stride %d instead of %d\n",
937                                 prsc->width0, prsc->height0,
938                                 util_format_short_name(prsc->format),
939                                 whandle->stride,
940                                 slice->stride);
941                 }
942                 goto fail;
943         } else if (!rsc->tiled) {
944                 slice->stride = whandle->stride;
945         }
946 
947         return prsc;
948 
949 fail:
950         v3d_resource_destroy(pscreen, prsc);
951         return NULL;
952 }
953 
954 void
v3d_update_shadow_texture(struct pipe_context * pctx,struct pipe_sampler_view * pview)955 v3d_update_shadow_texture(struct pipe_context *pctx,
956                           struct pipe_sampler_view *pview)
957 {
958         struct v3d_context *v3d = v3d_context(pctx);
959         struct v3d_sampler_view *view = v3d_sampler_view(pview);
960         struct v3d_resource *shadow = v3d_resource(view->texture);
961         struct v3d_resource *orig = v3d_resource(pview->texture);
962 
963         assert(view->texture != pview->texture);
964 
965         if (shadow->writes == orig->writes && orig->bo->private)
966                 return;
967 
968         perf_debug("Updating %dx%d@%d shadow for linear texture\n",
969                    orig->base.width0, orig->base.height0,
970                    pview->u.tex.first_level);
971 
972         for (int i = 0; i <= shadow->base.last_level; i++) {
973                 unsigned width = u_minify(shadow->base.width0, i);
974                 unsigned height = u_minify(shadow->base.height0, i);
975                 struct pipe_blit_info info = {
976                         .dst = {
977                                 .resource = &shadow->base,
978                                 .level = i,
979                                 .box = {
980                                         .x = 0,
981                                         .y = 0,
982                                         .z = 0,
983                                         .width = width,
984                                         .height = height,
985                                         .depth = 1,
986                                 },
987                                 .format = shadow->base.format,
988                         },
989                         .src = {
990                                 .resource = &orig->base,
991                                 .level = pview->u.tex.first_level + i,
992                                 .box = {
993                                         .x = 0,
994                                         .y = 0,
995                                         .z = 0,
996                                         .width = width,
997                                         .height = height,
998                                         .depth = 1,
999                                 },
1000                                 .format = orig->base.format,
1001                         },
1002                         .mask = util_format_get_mask(orig->base.format),
1003                 };
1004                 pctx->blit(pctx, &info);
1005         }
1006 
1007         shadow->writes = orig->writes;
1008 }
1009 
1010 static struct pipe_surface *
v3d_create_surface(struct pipe_context * pctx,struct pipe_resource * ptex,const struct pipe_surface * surf_tmpl)1011 v3d_create_surface(struct pipe_context *pctx,
1012                    struct pipe_resource *ptex,
1013                    const struct pipe_surface *surf_tmpl)
1014 {
1015         struct v3d_context *v3d = v3d_context(pctx);
1016         struct v3d_screen *screen = v3d->screen;
1017         struct v3d_surface *surface = CALLOC_STRUCT(v3d_surface);
1018         struct v3d_resource *rsc = v3d_resource(ptex);
1019 
1020         if (!surface)
1021                 return NULL;
1022 
1023         struct pipe_surface *psurf = &surface->base;
1024         unsigned level = surf_tmpl->u.tex.level;
1025         struct v3d_resource_slice *slice = &rsc->slices[level];
1026 
1027         pipe_reference_init(&psurf->reference, 1);
1028         pipe_resource_reference(&psurf->texture, ptex);
1029 
1030         psurf->context = pctx;
1031         psurf->format = surf_tmpl->format;
1032         psurf->width = u_minify(ptex->width0, level);
1033         psurf->height = u_minify(ptex->height0, level);
1034         psurf->u.tex.level = level;
1035         psurf->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
1036         psurf->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
1037 
1038         surface->offset = v3d_layer_offset(ptex, level,
1039                                            psurf->u.tex.first_layer);
1040         surface->tiling = slice->tiling;
1041 
1042         surface->format = v3d_get_rt_format(&screen->devinfo, psurf->format);
1043 
1044         const struct util_format_description *desc =
1045                 util_format_description(psurf->format);
1046 
1047         surface->swap_rb = (desc->swizzle[0] == PIPE_SWIZZLE_Z &&
1048                             psurf->format != PIPE_FORMAT_B5G6R5_UNORM);
1049 
1050         if (util_format_is_depth_or_stencil(psurf->format)) {
1051                 switch (psurf->format) {
1052                 case PIPE_FORMAT_Z16_UNORM:
1053                         surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_16;
1054                         break;
1055                 case PIPE_FORMAT_Z32_FLOAT:
1056                 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1057                         surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_32F;
1058                         break;
1059                 default:
1060                         surface->internal_type = V3D_INTERNAL_TYPE_DEPTH_24;
1061                 }
1062         } else {
1063                 uint32_t bpp, type;
1064                 v3d_get_internal_type_bpp_for_output_format(&screen->devinfo,
1065                                                             surface->format,
1066                                                             &type, &bpp);
1067                 surface->internal_type = type;
1068                 surface->internal_bpp = bpp;
1069         }
1070 
1071         if (surface->tiling == VC5_TILING_UIF_NO_XOR ||
1072             surface->tiling == VC5_TILING_UIF_XOR) {
1073                 surface->padded_height_of_output_image_in_uif_blocks =
1074                         (slice->padded_height /
1075                          (2 * v3d_utile_height(rsc->cpp)));
1076         }
1077 
1078         if (rsc->separate_stencil) {
1079                 surface->separate_stencil =
1080                         v3d_create_surface(pctx, &rsc->separate_stencil->base,
1081                                            surf_tmpl);
1082         }
1083 
1084         return &surface->base;
1085 }
1086 
1087 static void
v3d_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurf)1088 v3d_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
1089 {
1090         struct v3d_surface *surf = v3d_surface(psurf);
1091 
1092         if (surf->separate_stencil)
1093                 pipe_surface_reference(&surf->separate_stencil, NULL);
1094 
1095         pipe_resource_reference(&psurf->texture, NULL);
1096         FREE(psurf);
1097 }
1098 
1099 static void
v3d_flush_resource(struct pipe_context * pctx,struct pipe_resource * resource)1100 v3d_flush_resource(struct pipe_context *pctx, struct pipe_resource *resource)
1101 {
1102         /* All calls to flush_resource are followed by a flush of the context,
1103          * so there's nothing to do.
1104          */
1105 }
1106 
1107 static enum pipe_format
v3d_resource_get_internal_format(struct pipe_resource * prsc)1108 v3d_resource_get_internal_format(struct pipe_resource *prsc)
1109 {
1110         return v3d_resource(prsc)->internal_format;
1111 }
1112 
1113 static void
v3d_resource_set_stencil(struct pipe_resource * prsc,struct pipe_resource * stencil)1114 v3d_resource_set_stencil(struct pipe_resource *prsc,
1115                          struct pipe_resource *stencil)
1116 {
1117         v3d_resource(prsc)->separate_stencil = v3d_resource(stencil);
1118 }
1119 
1120 static struct pipe_resource *
v3d_resource_get_stencil(struct pipe_resource * prsc)1121 v3d_resource_get_stencil(struct pipe_resource *prsc)
1122 {
1123         struct v3d_resource *rsc = v3d_resource(prsc);
1124 
1125         return &rsc->separate_stencil->base;
1126 }
1127 
1128 static const struct u_transfer_vtbl transfer_vtbl = {
1129         .resource_create          = v3d_resource_create,
1130         .resource_destroy         = v3d_resource_destroy,
1131         .transfer_map             = v3d_resource_transfer_map,
1132         .transfer_unmap           = v3d_resource_transfer_unmap,
1133         .transfer_flush_region    = u_default_transfer_flush_region,
1134         .get_internal_format      = v3d_resource_get_internal_format,
1135         .set_stencil              = v3d_resource_set_stencil,
1136         .get_stencil              = v3d_resource_get_stencil,
1137 };
1138 
1139 void
v3d_resource_screen_init(struct pipe_screen * pscreen)1140 v3d_resource_screen_init(struct pipe_screen *pscreen)
1141 {
1142         pscreen->resource_create_with_modifiers =
1143                 v3d_resource_create_with_modifiers;
1144         pscreen->resource_create = u_transfer_helper_resource_create;
1145         pscreen->resource_from_handle = v3d_resource_from_handle;
1146         pscreen->resource_get_handle = v3d_resource_get_handle;
1147         pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1148         pscreen->transfer_helper = u_transfer_helper_create(&transfer_vtbl,
1149                                                             true, false,
1150                                                             true, true);
1151 }
1152 
1153 void
v3d_resource_context_init(struct pipe_context * pctx)1154 v3d_resource_context_init(struct pipe_context *pctx)
1155 {
1156         pctx->transfer_map = u_transfer_helper_transfer_map;
1157         pctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1158         pctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1159         pctx->buffer_subdata = u_default_buffer_subdata;
1160         pctx->texture_subdata = v3d_texture_subdata;
1161         pctx->create_surface = v3d_create_surface;
1162         pctx->surface_destroy = v3d_surface_destroy;
1163         pctx->resource_copy_region = util_resource_copy_region;
1164         pctx->blit = v3d_blit;
1165         pctx->generate_mipmap = v3d_generate_mipmap;
1166         pctx->flush_resource = v3d_flush_resource;
1167 }
1168