vc4: Allow submitting jobs with no bin CL in validation.
[mesa.git] / src / gallium / drivers / vc4 / kernel / vc4_gem.c
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 #ifdef USE_VC4_SIMULATOR
25
26 #include "vc4_drv.h"
27
28 int
29 vc4_cl_validate(struct drm_device *dev, struct vc4_exec_info *exec)
30 {
31 struct drm_vc4_submit_cl *args = exec->args;
32 void *temp = NULL;
33 void *bin, *render;
34 int ret = 0;
35 uint32_t bin_offset = 0;
36 uint32_t render_offset = bin_offset + args->bin_cl_size;
37 uint32_t shader_rec_offset = roundup(render_offset +
38 args->render_cl_size, 16);
39 uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
40 uint32_t exec_size = uniforms_offset + args->uniforms_size;
41 uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
42 args->shader_rec_count);
43
44 if (shader_rec_offset < render_offset ||
45 uniforms_offset < shader_rec_offset ||
46 exec_size < uniforms_offset ||
47 args->shader_rec_count >= (UINT_MAX /
48 sizeof(struct vc4_shader_state)) ||
49 temp_size < exec_size) {
50 DRM_ERROR("overflow in exec arguments\n");
51 goto fail;
52 }
53
54 /* Allocate space where we'll store the copied in user command lists
55 * and shader records.
56 *
57 * We don't just copy directly into the BOs because we need to
58 * read the contents back for validation, and I think the
59 * bo->vaddr is uncached access.
60 */
61 temp = kmalloc(temp_size, GFP_KERNEL);
62 if (!temp) {
63 DRM_ERROR("Failed to allocate storage for copying "
64 "in bin/render CLs.\n");
65 ret = -ENOMEM;
66 goto fail;
67 }
68 bin = temp + bin_offset;
69 render = temp + render_offset;
70 exec->shader_rec_u = temp + shader_rec_offset;
71 exec->uniforms_u = temp + uniforms_offset;
72 exec->shader_state = temp + exec_size;
73 exec->shader_state_size = args->shader_rec_count;
74
75 ret = copy_from_user(bin,
76 (void __user *)(uintptr_t)args->bin_cl,
77 args->bin_cl_size);
78 if (ret) {
79 DRM_ERROR("Failed to copy in bin cl\n");
80 goto fail;
81 }
82
83 ret = copy_from_user(render,
84 (void __user *)(uintptr_t)args->render_cl,
85 args->render_cl_size);
86 if (ret) {
87 DRM_ERROR("Failed to copy in render cl\n");
88 goto fail;
89 }
90
91 ret = copy_from_user(exec->shader_rec_u,
92 (void __user *)(uintptr_t)args->shader_rec,
93 args->shader_rec_size);
94 if (ret) {
95 DRM_ERROR("Failed to copy in shader recs\n");
96 goto fail;
97 }
98
99 ret = copy_from_user(exec->uniforms_u,
100 (void __user *)(uintptr_t)args->uniforms,
101 args->uniforms_size);
102 if (ret) {
103 DRM_ERROR("Failed to copy in uniforms cl\n");
104 goto fail;
105 }
106
107 exec->exec_bo = drm_gem_cma_create(dev, exec_size);
108 #if 0
109 if (IS_ERR(exec->exec_bo)) {
110 DRM_ERROR("Couldn't allocate BO for exec\n");
111 ret = PTR_ERR(exec->exec_bo);
112 exec->exec_bo = NULL;
113 goto fail;
114 }
115 #endif
116
117 exec->ct0ca = exec->exec_bo->paddr + bin_offset;
118 exec->ct1ca = exec->exec_bo->paddr + render_offset;
119
120 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
121 exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
122 exec->shader_rec_size = args->shader_rec_size;
123
124 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
125 exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
126 exec->uniforms_size = args->uniforms_size;
127
128 ret = vc4_validate_cl(dev,
129 exec->exec_bo->vaddr + bin_offset,
130 bin,
131 args->bin_cl_size,
132 true,
133 args->bin_cl_size != 0,
134 exec);
135 if (ret)
136 goto fail;
137
138 ret = vc4_validate_cl(dev,
139 exec->exec_bo->vaddr + render_offset,
140 render,
141 args->render_cl_size,
142 false,
143 args->bin_cl_size != 0,
144 exec);
145 if (ret)
146 goto fail;
147
148 ret = vc4_validate_shader_recs(dev, exec);
149
150 fail:
151 kfree(temp);
152 return ret;
153 }
154
155 #endif /* USE_VC4_SIMULATOR */