vc4: Move the mirrored kernel code to a kernel/ directory.
[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 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, args->bin_cl, args->bin_cl_size);
76 if (ret) {
77 DRM_ERROR("Failed to copy in bin cl\n");
78 goto fail;
79 }
80
81 ret = copy_from_user(render, args->render_cl, args->render_cl_size);
82 if (ret) {
83 DRM_ERROR("Failed to copy in render cl\n");
84 goto fail;
85 }
86
87 ret = copy_from_user(exec->shader_rec_u, args->shader_rec,
88 args->shader_rec_size);
89 if (ret) {
90 DRM_ERROR("Failed to copy in shader recs\n");
91 goto fail;
92 }
93
94 ret = copy_from_user(exec->uniforms_u, args->uniforms,
95 args->uniforms_size);
96 if (ret) {
97 DRM_ERROR("Failed to copy in uniforms cl\n");
98 goto fail;
99 }
100
101 exec->exec_bo = drm_gem_cma_create(dev, exec_size);
102 #if 0
103 if (IS_ERR(exec->exec_bo)) {
104 DRM_ERROR("Couldn't allocate BO for exec\n");
105 ret = PTR_ERR(exec->exec_bo);
106 exec->exec_bo = NULL;
107 goto fail;
108 }
109 #endif
110
111 exec->ct0ca = exec->exec_bo->paddr + bin_offset;
112 exec->ct1ca = exec->exec_bo->paddr + render_offset;
113
114 exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
115 exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
116 exec->shader_rec_size = args->shader_rec_size;
117
118 exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
119 exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
120 exec->uniforms_size = args->uniforms_size;
121
122 ret = vc4_validate_cl(dev,
123 exec->exec_bo->vaddr + bin_offset,
124 bin,
125 args->bin_cl_size,
126 true,
127 exec);
128 if (ret)
129 goto fail;
130
131 ret = vc4_validate_cl(dev,
132 exec->exec_bo->vaddr + render_offset,
133 render,
134 args->render_cl_size,
135 false,
136 exec);
137 if (ret)
138 goto fail;
139
140 ret = vc4_validate_shader_recs(dev, exec);
141
142 fail:
143 kfree(temp);
144 return ret;
145 }
146
147 #endif /* USE_VC4_SIMULATOR */