1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "tu_private.h"
25 
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "util/u_math.h"
34 #include "vk_enum_to_str.h"
35 
36 void PRINTFLIKE(3, 4)
__tu_finishme(const char * file,int line,const char * format,...)37    __tu_finishme(const char *file, int line, const char *format, ...)
38 {
39    va_list ap;
40    char buffer[256];
41 
42    va_start(ap, format);
43    vsnprintf(buffer, sizeof(buffer), format, ap);
44    va_end(ap);
45 
46    mesa_loge("%s:%d: FINISHME: %s\n", file, line, buffer);
47 }
48 
49 VkResult
__vk_startup_errorf(struct tu_instance * instance,VkResult error,bool always_print,const char * file,int line,const char * format,...)50 __vk_startup_errorf(struct tu_instance *instance,
51                     VkResult error,
52                     bool always_print,
53                     const char *file,
54                     int line,
55                     const char *format,
56                     ...)
57 {
58    va_list ap;
59    char buffer[256];
60 
61    const char *error_str = vk_Result_to_str(error);
62 
63 #ifndef DEBUG
64    if (!always_print)
65       return error;
66 #endif
67 
68    if (format) {
69       va_start(ap, format);
70       vsnprintf(buffer, sizeof(buffer), format, ap);
71       va_end(ap);
72 
73       mesa_loge("%s:%d: %s (%s)\n", file, line, buffer, error_str);
74    } else {
75       mesa_loge("%s:%d: %s\n", file, line, error_str);
76    }
77 
78    return error;
79 }
80 
81 static void
tu_tiling_config_update_tile_layout(struct tu_framebuffer * fb,const struct tu_device * dev,const struct tu_render_pass * pass)82 tu_tiling_config_update_tile_layout(struct tu_framebuffer *fb,
83                                     const struct tu_device *dev,
84                                     const struct tu_render_pass *pass)
85 {
86    const uint32_t tile_align_w = pass->tile_align_w;
87    const uint32_t tile_align_h = dev->physical_device->info->tile_align_h;
88    const uint32_t max_tile_width = dev->physical_device->info->tile_max_w;
89    const uint32_t max_tile_height = dev->physical_device->info->tile_max_h;
90 
91    /* start from 1 tile */
92    fb->tile_count = (VkExtent2D) {
93       .width = 1,
94       .height = 1,
95    };
96    fb->tile0 = (VkExtent2D) {
97       .width = util_align_npot(fb->width, tile_align_w),
98       .height = align(fb->height, tile_align_h),
99    };
100 
101    /* will force to sysmem, don't bother trying to have a valid tile config
102     * TODO: just skip all GMEM stuff when sysmem is forced?
103     */
104    if (!pass->gmem_pixels)
105       return;
106 
107    if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_FORCEBIN)) {
108       /* start with 2x2 tiles */
109       fb->tile_count.width = 2;
110       fb->tile_count.height = 2;
111       fb->tile0.width = util_align_npot(DIV_ROUND_UP(fb->width, 2), tile_align_w);
112       fb->tile0.height = align(DIV_ROUND_UP(fb->height, 2), tile_align_h);
113    }
114 
115    /* do not exceed max tile width */
116    while (fb->tile0.width > max_tile_width) {
117       fb->tile_count.width++;
118       fb->tile0.width =
119          util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
120    }
121 
122    /* do not exceed max tile height */
123    while (fb->tile0.height > max_tile_height) {
124       fb->tile_count.height++;
125       fb->tile0.height =
126          util_align_npot(DIV_ROUND_UP(fb->height, fb->tile_count.height), tile_align_h);
127    }
128 
129    /* do not exceed gmem size */
130    while (fb->tile0.width * fb->tile0.height > pass->gmem_pixels) {
131       if (fb->tile0.width > MAX2(tile_align_w, fb->tile0.height)) {
132          fb->tile_count.width++;
133          fb->tile0.width =
134             util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
135       } else {
136          /* if this assert fails then layout is impossible.. */
137          assert(fb->tile0.height > tile_align_h);
138          fb->tile_count.height++;
139          fb->tile0.height =
140             align(DIV_ROUND_UP(fb->height, fb->tile_count.height), tile_align_h);
141       }
142    }
143 }
144 
145 static void
tu_tiling_config_update_pipe_layout(struct tu_framebuffer * fb,const struct tu_device * dev)146 tu_tiling_config_update_pipe_layout(struct tu_framebuffer *fb,
147                                     const struct tu_device *dev)
148 {
149    const uint32_t max_pipe_count = 32; /* A6xx */
150 
151    /* start from 1 tile per pipe */
152    fb->pipe0 = (VkExtent2D) {
153       .width = 1,
154       .height = 1,
155    };
156    fb->pipe_count = fb->tile_count;
157 
158    while (fb->pipe_count.width * fb->pipe_count.height > max_pipe_count) {
159       if (fb->pipe0.width < fb->pipe0.height) {
160          fb->pipe0.width += 1;
161          fb->pipe_count.width =
162             DIV_ROUND_UP(fb->tile_count.width, fb->pipe0.width);
163       } else {
164          fb->pipe0.height += 1;
165          fb->pipe_count.height =
166             DIV_ROUND_UP(fb->tile_count.height, fb->pipe0.height);
167       }
168    }
169 }
170 
171 static void
tu_tiling_config_update_pipes(struct tu_framebuffer * fb,const struct tu_device * dev)172 tu_tiling_config_update_pipes(struct tu_framebuffer *fb,
173                               const struct tu_device *dev)
174 {
175    const uint32_t max_pipe_count = 32; /* A6xx */
176    const uint32_t used_pipe_count =
177       fb->pipe_count.width * fb->pipe_count.height;
178    const VkExtent2D last_pipe = {
179       .width = (fb->tile_count.width - 1) % fb->pipe0.width + 1,
180       .height = (fb->tile_count.height - 1) % fb->pipe0.height + 1,
181    };
182 
183    assert(used_pipe_count <= max_pipe_count);
184    assert(max_pipe_count <= ARRAY_SIZE(fb->pipe_config));
185 
186    for (uint32_t y = 0; y < fb->pipe_count.height; y++) {
187       for (uint32_t x = 0; x < fb->pipe_count.width; x++) {
188          const uint32_t pipe_x = fb->pipe0.width * x;
189          const uint32_t pipe_y = fb->pipe0.height * y;
190          const uint32_t pipe_w = (x == fb->pipe_count.width - 1)
191                                     ? last_pipe.width
192                                     : fb->pipe0.width;
193          const uint32_t pipe_h = (y == fb->pipe_count.height - 1)
194                                     ? last_pipe.height
195                                     : fb->pipe0.height;
196          const uint32_t n = fb->pipe_count.width * y + x;
197 
198          fb->pipe_config[n] = A6XX_VSC_PIPE_CONFIG_REG_X(pipe_x) |
199                                   A6XX_VSC_PIPE_CONFIG_REG_Y(pipe_y) |
200                                   A6XX_VSC_PIPE_CONFIG_REG_W(pipe_w) |
201                                   A6XX_VSC_PIPE_CONFIG_REG_H(pipe_h);
202          fb->pipe_sizes[n] = CP_SET_BIN_DATA5_0_VSC_SIZE(pipe_w * pipe_h);
203       }
204    }
205 
206    memset(fb->pipe_config + used_pipe_count, 0,
207           sizeof(uint32_t) * (max_pipe_count - used_pipe_count));
208 }
209 
210 void
tu_framebuffer_tiling_config(struct tu_framebuffer * fb,const struct tu_device * device,const struct tu_render_pass * pass)211 tu_framebuffer_tiling_config(struct tu_framebuffer *fb,
212                              const struct tu_device *device,
213                              const struct tu_render_pass *pass)
214 {
215    tu_tiling_config_update_tile_layout(fb, device, pass);
216    tu_tiling_config_update_pipe_layout(fb, device);
217    tu_tiling_config_update_pipes(fb, device);
218 }
219