vc4: Ignore non-address bits of the offset for load/store.
[mesa.git] / src / gallium / drivers / vc4 / vc4_simulator_validate.h
1 /*
2 * Copyright © 2014 Broadcom
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VC4_SIMULATOR_VALIDATE_H
25 #define VC4_SIMULATOR_VALIDATE_H
26
27 #include <stdbool.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <errno.h>
33
34 #include "vc4_context.h"
35 #include "vc4_qpu_defines.h"
36
37 #define DRM_INFO(...) fprintf(stderr, __VA_ARGS__)
38 #define DRM_ERROR(...) fprintf(stderr, __VA_ARGS__)
39 #define kmalloc(size, arg) malloc(size)
40 #define kcalloc(size, count, arg) calloc(size, count)
41 #define kfree(ptr) free(ptr)
42 #define krealloc(ptr, size, args) realloc(ptr, size)
43 #define roundup(x, y) align(x, y)
44 #define max(x, y) MAX2(x, y)
45 #define min(x, y) MiN2(x, y)
46 #define BUG_ON(condition) assert(!(condition))
47
48 static inline int
49 copy_from_user(void *dst, void *src, size_t size)
50 {
51 memcpy(dst, src, size);
52 return 0;
53 }
54
55 typedef uint8_t u8;
56 typedef uint16_t u16;
57 typedef uint32_t u32;
58
59 struct drm_device {
60 struct vc4_context *vc4;
61 uint32_t simulator_mem_next;
62 };
63
64 struct drm_gem_cma_object {
65 struct vc4_bo *bo;
66
67 struct {
68 uint32_t size;
69 } base;
70 uint32_t paddr;
71 void *vaddr;
72 };
73
74 enum vc4_bo_mode {
75 VC4_MODE_UNDECIDED,
76 VC4_MODE_TILE_ALLOC,
77 VC4_MODE_TSDA,
78 VC4_MODE_RENDER,
79 VC4_MODE_SHADER,
80 };
81
82 struct vc4_bo_exec_state {
83 struct drm_gem_cma_object *bo;
84 enum vc4_bo_mode mode;
85 };
86
87 struct exec_info {
88 /* Kernel-space copy of the ioctl arguments */
89 struct drm_vc4_submit_cl *args;
90
91 /* This is the array of BOs that were looked up at the start of exec.
92 * Command validation will use indices into this array.
93 */
94 struct vc4_bo_exec_state *bo;
95 uint32_t bo_count;
96
97 /* Current unvalidated indices into @bo loaded by the non-hardware
98 * VC4_PACKET_GEM_HANDLES.
99 */
100 uint32_t bo_index[2];
101
102 /* This is the BO where we store the validated command lists, shader
103 * records, and uniforms.
104 */
105 struct drm_gem_cma_object *exec_bo;
106
107 /**
108 * This tracks the per-shader-record state (packet 64) that
109 * determines the length of the shader record and the offset
110 * it's expected to be found at. It gets read in from the
111 * command lists.
112 */
113 struct vc4_shader_state {
114 uint8_t packet;
115 uint32_t addr;
116 /* Maximum vertex index referenced by any primitive using this
117 * shader state.
118 */
119 uint32_t max_index;
120 } *shader_state;
121
122 /** How many shader states the user declared they were using. */
123 uint32_t shader_state_size;
124 /** How many shader state records the validator has seen. */
125 uint32_t shader_state_count;
126
127 bool found_tile_binning_mode_config_packet;
128 bool found_tile_rendering_mode_config_packet;
129 bool found_start_tile_binning_packet;
130 uint8_t bin_tiles_x, bin_tiles_y;
131 uint32_t fb_width, fb_height;
132 uint32_t tile_alloc_init_block_size;
133 struct drm_gem_cma_object *tile_alloc_bo;
134
135 /**
136 * Computed addresses pointing into exec_bo where we start the
137 * bin thread (ct0) and render thread (ct1).
138 */
139 uint32_t ct0ca, ct0ea;
140 uint32_t ct1ca, ct1ea;
141
142 /* Pointers to the shader recs. These paddr gets incremented as CL
143 * packets are relocated in validate_gl_shader_state, and the vaddrs
144 * (u and v) get incremented and size decremented as the shader recs
145 * themselves are validated.
146 */
147 void *shader_rec_u;
148 void *shader_rec_v;
149 uint32_t shader_rec_p;
150 uint32_t shader_rec_size;
151
152 /* Pointers to the uniform data. These pointers are incremented, and
153 * size decremented, as each batch of uniforms is uploaded.
154 */
155 void *uniforms_u;
156 void *uniforms_v;
157 uint32_t uniforms_p;
158 uint32_t uniforms_size;
159 };
160
161 /**
162 * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
163 * setup parameters.
164 *
165 * This will be used at draw time to relocate the reference to the texture
166 * contents in p0, and validate that the offset combined with
167 * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
168 * Note that the hardware treats unprovided config parameters as 0, so not all
169 * of them need to be set up for every texure sample, and we'll store ~0 as
170 * the offset to mark the unused ones.
171 *
172 * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
173 * Setup") for definitions of the texture parameters.
174 */
175 struct vc4_texture_sample_info {
176 uint32_t p_offset[4];
177 };
178
179 /**
180 * struct vc4_validated_shader_info - information about validated shaders that
181 * needs to be used from command list validation.
182 *
183 * For a given shader, each time a shader state record references it, we need
184 * to verify that the shader doesn't read more uniforms than the shader state
185 * record's uniform BO pointer can provide, and we need to apply relocations
186 * and validate the shader state record's uniforms that define the texture
187 * samples.
188 */
189 struct vc4_validated_shader_info
190 {
191 uint32_t uniforms_size;
192 uint32_t uniforms_src_size;
193 uint32_t num_texture_samples;
194 struct vc4_texture_sample_info *texture_samples;
195 };
196
197 int vc4_validate_cl(struct drm_device *dev,
198 void *validated,
199 void *unvalidated,
200 uint32_t len,
201 bool is_bin,
202 struct exec_info *exec);
203
204 int vc4_validate_shader_recs(struct drm_device *dev, struct exec_info *exec);
205
206 struct vc4_validated_shader_info *
207 vc4_validate_shader(struct drm_gem_cma_object *shader_obj,
208 uint32_t start_offset);
209
210 #endif /* VC4_SIMULATOR_VALIDATE_H */