1361fc4cbSmaya/*
2361fc4cbSmaya * Copyright © 2015 Intel Corporation
3361fc4cbSmaya *
4361fc4cbSmaya * Permission is hereby granted, free of charge, to any person obtaining a
5361fc4cbSmaya * copy of this software and associated documentation files (the "Software"),
6361fc4cbSmaya * to deal in the Software without restriction, including without limitation
7361fc4cbSmaya * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8361fc4cbSmaya * and/or sell copies of the Software, and to permit persons to whom the
9361fc4cbSmaya * Software is furnished to do so, subject to the following conditions:
10361fc4cbSmaya *
11361fc4cbSmaya * The above copyright notice and this permission notice (including the next
12361fc4cbSmaya * paragraph) shall be included in all copies or substantial portions of the
13361fc4cbSmaya * Software.
14361fc4cbSmaya *
15361fc4cbSmaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16361fc4cbSmaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17361fc4cbSmaya * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18361fc4cbSmaya * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19361fc4cbSmaya * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20361fc4cbSmaya * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21361fc4cbSmaya * DEALINGS IN THE SOFTWARE.
22361fc4cbSmaya */
23361fc4cbSmaya
24361fc4cbSmaya#include "tu_private.h"
25361fc4cbSmaya
26361fc4cbSmaya#include <assert.h>
27361fc4cbSmaya#include <errno.h>
28361fc4cbSmaya#include <stdarg.h>
29361fc4cbSmaya#include <stdio.h>
30361fc4cbSmaya#include <stdlib.h>
31361fc4cbSmaya#include <string.h>
32361fc4cbSmaya
33361fc4cbSmaya#include "util/u_math.h"
34361fc4cbSmaya#include "vk_enum_to_str.h"
35361fc4cbSmaya
367ec681f3Smrgvoid PRINTFLIKE(3, 4)
37361fc4cbSmaya   __tu_finishme(const char *file, int line, const char *format, ...)
38361fc4cbSmaya{
39361fc4cbSmaya   va_list ap;
40361fc4cbSmaya   char buffer[256];
41361fc4cbSmaya
42361fc4cbSmaya   va_start(ap, format);
43361fc4cbSmaya   vsnprintf(buffer, sizeof(buffer), format, ap);
44361fc4cbSmaya   va_end(ap);
45361fc4cbSmaya
467ec681f3Smrg   mesa_loge("%s:%d: FINISHME: %s\n", file, line, buffer);
47361fc4cbSmaya}
48361fc4cbSmaya
49361fc4cbSmayaVkResult
507ec681f3Smrg__vk_startup_errorf(struct tu_instance *instance,
517ec681f3Smrg                    VkResult error,
527ec681f3Smrg                    bool always_print,
537ec681f3Smrg                    const char *file,
547ec681f3Smrg                    int line,
557ec681f3Smrg                    const char *format,
567ec681f3Smrg                    ...)
57361fc4cbSmaya{
58361fc4cbSmaya   va_list ap;
59361fc4cbSmaya   char buffer[256];
60361fc4cbSmaya
61361fc4cbSmaya   const char *error_str = vk_Result_to_str(error);
62361fc4cbSmaya
63361fc4cbSmaya#ifndef DEBUG
647ec681f3Smrg   if (!always_print)
657ec681f3Smrg      return error;
66361fc4cbSmaya#endif
67361fc4cbSmaya
68361fc4cbSmaya   if (format) {
69361fc4cbSmaya      va_start(ap, format);
70361fc4cbSmaya      vsnprintf(buffer, sizeof(buffer), format, ap);
71361fc4cbSmaya      va_end(ap);
72361fc4cbSmaya
737ec681f3Smrg      mesa_loge("%s:%d: %s (%s)\n", file, line, buffer, error_str);
74361fc4cbSmaya   } else {
757ec681f3Smrg      mesa_loge("%s:%d: %s\n", file, line, error_str);
76361fc4cbSmaya   }
77361fc4cbSmaya
78361fc4cbSmaya   return error;
79361fc4cbSmaya}
807ec681f3Smrg
817ec681f3Smrgstatic void
827ec681f3Smrgtu_tiling_config_update_tile_layout(struct tu_framebuffer *fb,
837ec681f3Smrg                                    const struct tu_device *dev,
847ec681f3Smrg                                    const struct tu_render_pass *pass)
857ec681f3Smrg{
867ec681f3Smrg   const uint32_t tile_align_w = pass->tile_align_w;
877ec681f3Smrg   const uint32_t tile_align_h = dev->physical_device->info->tile_align_h;
887ec681f3Smrg   const uint32_t max_tile_width = dev->physical_device->info->tile_max_w;
897ec681f3Smrg   const uint32_t max_tile_height = dev->physical_device->info->tile_max_h;
907ec681f3Smrg
917ec681f3Smrg   /* start from 1 tile */
927ec681f3Smrg   fb->tile_count = (VkExtent2D) {
937ec681f3Smrg      .width = 1,
947ec681f3Smrg      .height = 1,
957ec681f3Smrg   };
967ec681f3Smrg   fb->tile0 = (VkExtent2D) {
977ec681f3Smrg      .width = util_align_npot(fb->width, tile_align_w),
987ec681f3Smrg      .height = align(fb->height, tile_align_h),
997ec681f3Smrg   };
1007ec681f3Smrg
1017ec681f3Smrg   /* will force to sysmem, don't bother trying to have a valid tile config
1027ec681f3Smrg    * TODO: just skip all GMEM stuff when sysmem is forced?
1037ec681f3Smrg    */
1047ec681f3Smrg   if (!pass->gmem_pixels)
1057ec681f3Smrg      return;
1067ec681f3Smrg
1077ec681f3Smrg   if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_FORCEBIN)) {
1087ec681f3Smrg      /* start with 2x2 tiles */
1097ec681f3Smrg      fb->tile_count.width = 2;
1107ec681f3Smrg      fb->tile_count.height = 2;
1117ec681f3Smrg      fb->tile0.width = util_align_npot(DIV_ROUND_UP(fb->width, 2), tile_align_w);
1127ec681f3Smrg      fb->tile0.height = align(DIV_ROUND_UP(fb->height, 2), tile_align_h);
1137ec681f3Smrg   }
1147ec681f3Smrg
1157ec681f3Smrg   /* do not exceed max tile width */
1167ec681f3Smrg   while (fb->tile0.width > max_tile_width) {
1177ec681f3Smrg      fb->tile_count.width++;
1187ec681f3Smrg      fb->tile0.width =
1197ec681f3Smrg         util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
1207ec681f3Smrg   }
1217ec681f3Smrg
1227ec681f3Smrg   /* do not exceed max tile height */
1237ec681f3Smrg   while (fb->tile0.height > max_tile_height) {
1247ec681f3Smrg      fb->tile_count.height++;
1257ec681f3Smrg      fb->tile0.height =
1267ec681f3Smrg         util_align_npot(DIV_ROUND_UP(fb->height, fb->tile_count.height), tile_align_h);
1277ec681f3Smrg   }
1287ec681f3Smrg
1297ec681f3Smrg   /* do not exceed gmem size */
1307ec681f3Smrg   while (fb->tile0.width * fb->tile0.height > pass->gmem_pixels) {
1317ec681f3Smrg      if (fb->tile0.width > MAX2(tile_align_w, fb->tile0.height)) {
1327ec681f3Smrg         fb->tile_count.width++;
1337ec681f3Smrg         fb->tile0.width =
1347ec681f3Smrg            util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
1357ec681f3Smrg      } else {
1367ec681f3Smrg         /* if this assert fails then layout is impossible.. */
1377ec681f3Smrg         assert(fb->tile0.height > tile_align_h);
1387ec681f3Smrg         fb->tile_count.height++;
1397ec681f3Smrg         fb->tile0.height =
1407ec681f3Smrg            align(DIV_ROUND_UP(fb->height, fb->tile_count.height), tile_align_h);
1417ec681f3Smrg      }
1427ec681f3Smrg   }
1437ec681f3Smrg}
1447ec681f3Smrg
1457ec681f3Smrgstatic void
1467ec681f3Smrgtu_tiling_config_update_pipe_layout(struct tu_framebuffer *fb,
1477ec681f3Smrg                                    const struct tu_device *dev)
1487ec681f3Smrg{
1497ec681f3Smrg   const uint32_t max_pipe_count = 32; /* A6xx */
1507ec681f3Smrg
1517ec681f3Smrg   /* start from 1 tile per pipe */
1527ec681f3Smrg   fb->pipe0 = (VkExtent2D) {
1537ec681f3Smrg      .width = 1,
1547ec681f3Smrg      .height = 1,
1557ec681f3Smrg   };
1567ec681f3Smrg   fb->pipe_count = fb->tile_count;
1577ec681f3Smrg
1587ec681f3Smrg   while (fb->pipe_count.width * fb->pipe_count.height > max_pipe_count) {
1597ec681f3Smrg      if (fb->pipe0.width < fb->pipe0.height) {
1607ec681f3Smrg         fb->pipe0.width += 1;
1617ec681f3Smrg         fb->pipe_count.width =
1627ec681f3Smrg            DIV_ROUND_UP(fb->tile_count.width, fb->pipe0.width);
1637ec681f3Smrg      } else {
1647ec681f3Smrg         fb->pipe0.height += 1;
1657ec681f3Smrg         fb->pipe_count.height =
1667ec681f3Smrg            DIV_ROUND_UP(fb->tile_count.height, fb->pipe0.height);
1677ec681f3Smrg      }
1687ec681f3Smrg   }
1697ec681f3Smrg}
1707ec681f3Smrg
1717ec681f3Smrgstatic void
1727ec681f3Smrgtu_tiling_config_update_pipes(struct tu_framebuffer *fb,
1737ec681f3Smrg                              const struct tu_device *dev)
1747ec681f3Smrg{
1757ec681f3Smrg   const uint32_t max_pipe_count = 32; /* A6xx */
1767ec681f3Smrg   const uint32_t used_pipe_count =
1777ec681f3Smrg      fb->pipe_count.width * fb->pipe_count.height;
1787ec681f3Smrg   const VkExtent2D last_pipe = {
1797ec681f3Smrg      .width = (fb->tile_count.width - 1) % fb->pipe0.width + 1,
1807ec681f3Smrg      .height = (fb->tile_count.height - 1) % fb->pipe0.height + 1,
1817ec681f3Smrg   };
1827ec681f3Smrg
1837ec681f3Smrg   assert(used_pipe_count <= max_pipe_count);
1847ec681f3Smrg   assert(max_pipe_count <= ARRAY_SIZE(fb->pipe_config));
1857ec681f3Smrg
1867ec681f3Smrg   for (uint32_t y = 0; y < fb->pipe_count.height; y++) {
1877ec681f3Smrg      for (uint32_t x = 0; x < fb->pipe_count.width; x++) {
1887ec681f3Smrg         const uint32_t pipe_x = fb->pipe0.width * x;
1897ec681f3Smrg         const uint32_t pipe_y = fb->pipe0.height * y;
1907ec681f3Smrg         const uint32_t pipe_w = (x == fb->pipe_count.width - 1)
1917ec681f3Smrg                                    ? last_pipe.width
1927ec681f3Smrg                                    : fb->pipe0.width;
1937ec681f3Smrg         const uint32_t pipe_h = (y == fb->pipe_count.height - 1)
1947ec681f3Smrg                                    ? last_pipe.height
1957ec681f3Smrg                                    : fb->pipe0.height;
1967ec681f3Smrg         const uint32_t n = fb->pipe_count.width * y + x;
1977ec681f3Smrg
1987ec681f3Smrg         fb->pipe_config[n] = A6XX_VSC_PIPE_CONFIG_REG_X(pipe_x) |
1997ec681f3Smrg                                  A6XX_VSC_PIPE_CONFIG_REG_Y(pipe_y) |
2007ec681f3Smrg                                  A6XX_VSC_PIPE_CONFIG_REG_W(pipe_w) |
2017ec681f3Smrg                                  A6XX_VSC_PIPE_CONFIG_REG_H(pipe_h);
2027ec681f3Smrg         fb->pipe_sizes[n] = CP_SET_BIN_DATA5_0_VSC_SIZE(pipe_w * pipe_h);
2037ec681f3Smrg      }
2047ec681f3Smrg   }
2057ec681f3Smrg
2067ec681f3Smrg   memset(fb->pipe_config + used_pipe_count, 0,
2077ec681f3Smrg          sizeof(uint32_t) * (max_pipe_count - used_pipe_count));
2087ec681f3Smrg}
2097ec681f3Smrg
2107ec681f3Smrgvoid
2117ec681f3Smrgtu_framebuffer_tiling_config(struct tu_framebuffer *fb,
2127ec681f3Smrg                             const struct tu_device *device,
2137ec681f3Smrg                             const struct tu_render_pass *pass)
2147ec681f3Smrg{
2157ec681f3Smrg   tu_tiling_config_update_tile_layout(fb, device, pass);
2167ec681f3Smrg   tu_tiling_config_update_pipe_layout(fb, device);
2177ec681f3Smrg   tu_tiling_config_update_pipes(fb, device);
2187ec681f3Smrg}
219