vc4: Align following shader recs to 16 bytes.
[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 BUG_ON(condition) assert(!(condition))
45
46 static inline int
47 copy_from_user(void *dst, void *src, size_t size)
48 {
49 memcpy(dst, src, size);
50 return 0;
51 }
52
53 typedef uint8_t u8;
54 typedef uint16_t u16;
55 typedef uint32_t u32;
56
57 struct drm_device {
58 struct vc4_context *vc4;
59 uint32_t simulator_mem_next;
60 };
61
62 struct drm_gem_cma_object {
63 struct vc4_bo *bo;
64
65 struct {
66 uint32_t size;
67 } base;
68 uint32_t paddr;
69 void *vaddr;
70 };
71
72 struct exec_info {
73 /* Kernel-space copy of the ioctl arguments */
74 struct drm_vc4_submit_cl *args;
75
76 /* This is the array of BOs that were looked up at the start of exec.
77 * Command validation will use indices into this array.
78 */
79 struct drm_gem_cma_object **bo;
80 uint32_t bo_count;
81
82 /* Current indices into @bo loaded by the non-hardware packet
83 * that passes in indices. This can be used even without
84 * checking that we've seen one of those packets, because
85 * @bo_count is always >= 1, and this struct is initialized to
86 * 0.
87 */
88 uint32_t bo_index[2];
89 uint32_t max_width, max_height;
90
91 /* This is the BO where we store the validated command lists, shader
92 * records, and uniforms.
93 */
94 struct drm_gem_cma_object *exec_bo;
95
96 /**
97 * This tracks the per-shader-record state (packet 64) that
98 * determines the length of the shader record and the offset
99 * it's expected to be found at. It gets read in from the
100 * command lists.
101 */
102 struct vc4_shader_state {
103 uint8_t packet;
104 uint32_t addr;
105 } *shader_state;
106
107 /** How many shader states the user declared they were using. */
108 uint32_t shader_state_size;
109 /** How many shader state records the validator has seen. */
110 uint32_t shader_state_count;
111
112 /**
113 * Computed addresses pointing into exec_bo where we start the
114 * bin thread (ct0) and render thread (ct1).
115 */
116 uint32_t ct0ca, ct0ea;
117 uint32_t ct1ca, ct1ea;
118
119 /* Pointers to the shader recs. These paddr gets incremented as CL
120 * packets are relocated in validate_gl_shader_state, and the vaddrs
121 * (u and v) get incremented and size decremented as the shader recs
122 * themselves are validated.
123 */
124 void *shader_rec_u;
125 void *shader_rec_v;
126 uint32_t shader_rec_p;
127 uint32_t shader_rec_size;
128
129 /* Pointers to the uniform data. These pointers are incremented, and
130 * size decremented, as each batch of uniforms is uploaded.
131 */
132 void *uniforms_u;
133 void *uniforms_v;
134 uint32_t uniforms_p;
135 uint32_t uniforms_size;
136 };
137
138 /**
139 * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
140 * setup parameters.
141 *
142 * This will be used at draw time to relocate the reference to the texture
143 * contents in p0, and validate that the offset combined with
144 * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
145 * Note that the hardware treats unprovided config parameters as 0, so not all
146 * of them need to be set up for every texure sample, and we'll store ~0 as
147 * the offset to mark the unused ones.
148 *
149 * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
150 * Setup") for definitions of the texture parameters.
151 */
152 struct vc4_texture_sample_info {
153 uint32_t p_offset[4];
154 };
155
156 /**
157 * struct vc4_validated_shader_info - information about validated shaders that
158 * needs to be used from command list validation.
159 *
160 * For a given shader, each time a shader state record references it, we need
161 * to verify that the shader doesn't read more uniforms than the shader state
162 * record's uniform BO pointer can provide, and we need to apply relocations
163 * and validate the shader state record's uniforms that define the texture
164 * samples.
165 */
166 struct vc4_validated_shader_info
167 {
168 uint32_t uniforms_size;
169 uint32_t uniforms_src_size;
170 uint32_t num_texture_samples;
171 struct vc4_texture_sample_info *texture_samples;
172 };
173
174 int vc4_validate_cl(struct drm_device *dev,
175 void *validated,
176 void *unvalidated,
177 uint32_t len,
178 bool is_bin,
179 struct exec_info *exec);
180
181 int vc4_validate_shader_recs(struct drm_device *dev, struct exec_info *exec);
182
183 struct vc4_validated_shader_info *
184 vc4_validate_shader(struct drm_gem_cma_object *shader_obj,
185 uint32_t start_offset);
186
187 #endif /* VC4_SIMULATOR_VALIDATE_H */