1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Broadcom
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifdef USE_VC4_SIMULATOR
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "vc4_drv.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/*
29bf215546Sopenharmony_ci * Copies in the user's binning command list and generates the validated bin
30bf215546Sopenharmony_ci * CL, along with associated data (shader records, uniforms).
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_cistatic int
33bf215546Sopenharmony_civc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci	struct drm_vc4_submit_cl *args = exec->args;
36bf215546Sopenharmony_ci	void *temp = NULL;
37bf215546Sopenharmony_ci	void *bin;
38bf215546Sopenharmony_ci	int ret = 0;
39bf215546Sopenharmony_ci	uint32_t bin_offset = 0;
40bf215546Sopenharmony_ci	uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
41bf215546Sopenharmony_ci					     16);
42bf215546Sopenharmony_ci	uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
43bf215546Sopenharmony_ci	uint32_t exec_size = uniforms_offset + args->uniforms_size;
44bf215546Sopenharmony_ci	uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
45bf215546Sopenharmony_ci					  args->shader_rec_count);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci	if (uniforms_offset < shader_rec_offset ||
48bf215546Sopenharmony_ci	    exec_size < uniforms_offset ||
49bf215546Sopenharmony_ci	    args->shader_rec_count >= (UINT_MAX /
50bf215546Sopenharmony_ci					  sizeof(struct vc4_shader_state)) ||
51bf215546Sopenharmony_ci	    temp_size < exec_size) {
52bf215546Sopenharmony_ci		DRM_ERROR("overflow in exec arguments\n");
53bf215546Sopenharmony_ci		goto fail;
54bf215546Sopenharmony_ci	}
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci	/* Allocate space where we'll store the copied in user command lists
57bf215546Sopenharmony_ci	 * and shader records.
58bf215546Sopenharmony_ci	 *
59bf215546Sopenharmony_ci	 * We don't just copy directly into the BOs because we need to
60bf215546Sopenharmony_ci	 * read the contents back for validation, and I think the
61bf215546Sopenharmony_ci	 * bo->vaddr is uncached access.
62bf215546Sopenharmony_ci	 */
63bf215546Sopenharmony_ci	temp = kmalloc(temp_size, GFP_KERNEL);
64bf215546Sopenharmony_ci	if (!temp) {
65bf215546Sopenharmony_ci		DRM_ERROR("Failed to allocate storage for copying "
66bf215546Sopenharmony_ci			  "in bin/render CLs.\n");
67bf215546Sopenharmony_ci		ret = -ENOMEM;
68bf215546Sopenharmony_ci		goto fail;
69bf215546Sopenharmony_ci	}
70bf215546Sopenharmony_ci	bin = temp + bin_offset;
71bf215546Sopenharmony_ci	exec->shader_rec_u = temp + shader_rec_offset;
72bf215546Sopenharmony_ci	exec->uniforms_u = temp + uniforms_offset;
73bf215546Sopenharmony_ci	exec->shader_state = temp + exec_size;
74bf215546Sopenharmony_ci	exec->shader_state_size = args->shader_rec_count;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci	ret = copy_from_user(bin,
77bf215546Sopenharmony_ci			     (void __user *)(uintptr_t)args->bin_cl,
78bf215546Sopenharmony_ci			     args->bin_cl_size);
79bf215546Sopenharmony_ci	if (ret) {
80bf215546Sopenharmony_ci		DRM_ERROR("Failed to copy in bin cl\n");
81bf215546Sopenharmony_ci		goto fail;
82bf215546Sopenharmony_ci	}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci	ret = copy_from_user(exec->shader_rec_u,
85bf215546Sopenharmony_ci			     (void __user *)(uintptr_t)args->shader_rec,
86bf215546Sopenharmony_ci			     args->shader_rec_size);
87bf215546Sopenharmony_ci	if (ret) {
88bf215546Sopenharmony_ci		DRM_ERROR("Failed to copy in shader recs\n");
89bf215546Sopenharmony_ci		goto fail;
90bf215546Sopenharmony_ci	}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci	ret = copy_from_user(exec->uniforms_u,
93bf215546Sopenharmony_ci			     (void __user *)(uintptr_t)args->uniforms,
94bf215546Sopenharmony_ci			     args->uniforms_size);
95bf215546Sopenharmony_ci	if (ret) {
96bf215546Sopenharmony_ci		DRM_ERROR("Failed to copy in uniforms cl\n");
97bf215546Sopenharmony_ci		goto fail;
98bf215546Sopenharmony_ci	}
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci	exec->exec_bo = drm_gem_cma_create(dev, exec_size);
101bf215546Sopenharmony_ci#if 0
102bf215546Sopenharmony_ci	if (IS_ERR(exec->exec_bo)) {
103bf215546Sopenharmony_ci		DRM_ERROR("Couldn't allocate BO for exec\n");
104bf215546Sopenharmony_ci		ret = PTR_ERR(exec->exec_bo);
105bf215546Sopenharmony_ci		exec->exec_bo = NULL;
106bf215546Sopenharmony_ci		goto fail;
107bf215546Sopenharmony_ci	}
108bf215546Sopenharmony_ci#endif
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci	list_addtail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
111bf215546Sopenharmony_ci		     &exec->unref_list);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci	exec->ct0ca = exec->exec_bo->paddr + bin_offset;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci	exec->bin_u = bin;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci	exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
118bf215546Sopenharmony_ci	exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
119bf215546Sopenharmony_ci	exec->shader_rec_size = args->shader_rec_size;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci	exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
122bf215546Sopenharmony_ci	exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
123bf215546Sopenharmony_ci	exec->uniforms_size = args->uniforms_size;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci	ret = vc4_validate_bin_cl(dev,
126bf215546Sopenharmony_ci				  exec->exec_bo->vaddr + bin_offset,
127bf215546Sopenharmony_ci				  bin,
128bf215546Sopenharmony_ci				  exec);
129bf215546Sopenharmony_ci	if (ret)
130bf215546Sopenharmony_ci		goto fail;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci	ret = vc4_validate_shader_recs(dev, exec);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_cifail:
135bf215546Sopenharmony_ci	kfree(temp);
136bf215546Sopenharmony_ci	return ret;
137bf215546Sopenharmony_ci}
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ciint
140bf215546Sopenharmony_civc4_cl_validate(struct drm_device *dev, struct vc4_exec_info *exec)
141bf215546Sopenharmony_ci{
142bf215546Sopenharmony_ci	struct drm_vc4_submit_cl *args = exec->args;
143bf215546Sopenharmony_ci	int ret = 0;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci	if (args->color_write.bits & VC4_RENDER_CONFIG_MS_MODE_4X) {
146bf215546Sopenharmony_ci		exec->tile_width = 32;
147bf215546Sopenharmony_ci		exec->tile_height = 32;
148bf215546Sopenharmony_ci	} else {
149bf215546Sopenharmony_ci		exec->tile_width = 64;
150bf215546Sopenharmony_ci		exec->tile_height = 64;
151bf215546Sopenharmony_ci	}
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci	if (exec->args->bin_cl_size != 0) {
154bf215546Sopenharmony_ci		ret = vc4_get_bcl(dev, exec);
155bf215546Sopenharmony_ci		if (ret)
156bf215546Sopenharmony_ci			goto fail;
157bf215546Sopenharmony_ci	} else {
158bf215546Sopenharmony_ci		exec->ct0ca = exec->ct0ea = 0;
159bf215546Sopenharmony_ci	}
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci	ret = vc4_get_rcl(dev, exec);
162bf215546Sopenharmony_ci	if (ret)
163bf215546Sopenharmony_ci		goto fail;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_cifail:
166bf215546Sopenharmony_ci	return ret;
167bf215546Sopenharmony_ci}
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci#endif /* USE_VC4_SIMULATOR */
170