xref: /openbmc/linux/drivers/gpu/drm/lima/lima_drv.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
3 
4 #include <linux/module.h>
5 #include <linux/of_platform.h>
6 #include <linux/uaccess.h>
7 #include <linux/slab.h>
8 #include <drm/drm_ioctl.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_prime.h>
11 #include <drm/lima_drm.h>
12 
13 #include "lima_drv.h"
14 #include "lima_gem.h"
15 #include "lima_gem_prime.h"
16 #include "lima_vm.h"
17 
18 int lima_sched_timeout_ms;
19 
20 MODULE_PARM_DESC(sched_timeout_ms, "task run timeout in ms");
21 module_param_named(sched_timeout_ms, lima_sched_timeout_ms, int, 0444);
22 
23 static int lima_ioctl_get_param(struct drm_device *dev, void *data, struct drm_file *file)
24 {
25 	struct drm_lima_get_param *args = data;
26 	struct lima_device *ldev = to_lima_dev(dev);
27 
28 	if (args->pad)
29 		return -EINVAL;
30 
31 	switch (args->param) {
32 	case DRM_LIMA_PARAM_GPU_ID:
33 		switch (ldev->id) {
34 		case lima_gpu_mali400:
35 			args->value = DRM_LIMA_PARAM_GPU_ID_MALI400;
36 			break;
37 		case lima_gpu_mali450:
38 			args->value = DRM_LIMA_PARAM_GPU_ID_MALI450;
39 			break;
40 		default:
41 			args->value = DRM_LIMA_PARAM_GPU_ID_UNKNOWN;
42 			break;
43 		}
44 		break;
45 
46 	case DRM_LIMA_PARAM_NUM_PP:
47 		args->value = ldev->pipe[lima_pipe_pp].num_processor;
48 		break;
49 
50 	case DRM_LIMA_PARAM_GP_VERSION:
51 		args->value = ldev->gp_version;
52 		break;
53 
54 	case DRM_LIMA_PARAM_PP_VERSION:
55 		args->value = ldev->pp_version;
56 		break;
57 
58 	default:
59 		return -EINVAL;
60 	}
61 
62 	return 0;
63 }
64 
65 static int lima_ioctl_gem_create(struct drm_device *dev, void *data, struct drm_file *file)
66 {
67 	struct drm_lima_gem_create *args = data;
68 
69 	if (args->pad)
70 		return -EINVAL;
71 
72 	if (args->flags)
73 		return -EINVAL;
74 
75 	if (args->size == 0)
76 		return -EINVAL;
77 
78 	return lima_gem_create_handle(dev, file, args->size, args->flags, &args->handle);
79 }
80 
81 static int lima_ioctl_gem_info(struct drm_device *dev, void *data, struct drm_file *file)
82 {
83 	struct drm_lima_gem_info *args = data;
84 
85 	return lima_gem_get_info(file, args->handle, &args->va, &args->offset);
86 }
87 
88 static int lima_ioctl_gem_submit(struct drm_device *dev, void *data, struct drm_file *file)
89 {
90 	struct drm_lima_gem_submit *args = data;
91 	struct lima_device *ldev = to_lima_dev(dev);
92 	struct lima_drm_priv *priv = file->driver_priv;
93 	struct drm_lima_gem_submit_bo *bos;
94 	struct lima_sched_pipe *pipe;
95 	struct lima_sched_task *task;
96 	struct lima_ctx *ctx;
97 	struct lima_submit submit = {0};
98 	size_t size;
99 	int err = 0;
100 
101 	if (args->pipe >= lima_pipe_num || args->nr_bos == 0)
102 		return -EINVAL;
103 
104 	if (args->flags & ~(LIMA_SUBMIT_FLAG_EXPLICIT_FENCE))
105 		return -EINVAL;
106 
107 	pipe = ldev->pipe + args->pipe;
108 	if (args->frame_size != pipe->frame_size)
109 		return -EINVAL;
110 
111 	bos = kvcalloc(args->nr_bos, sizeof(*submit.bos) + sizeof(*submit.lbos), GFP_KERNEL);
112 	if (!bos)
113 		return -ENOMEM;
114 
115 	size = args->nr_bos * sizeof(*submit.bos);
116 	if (copy_from_user(bos, u64_to_user_ptr(args->bos), size)) {
117 		err = -EFAULT;
118 		goto out0;
119 	}
120 
121 	task = kmem_cache_zalloc(pipe->task_slab, GFP_KERNEL);
122 	if (!task) {
123 		err = -ENOMEM;
124 		goto out0;
125 	}
126 
127 	task->frame = task + 1;
128 	if (copy_from_user(task->frame, u64_to_user_ptr(args->frame), args->frame_size)) {
129 		err = -EFAULT;
130 		goto out1;
131 	}
132 
133 	err = pipe->task_validate(pipe, task);
134 	if (err)
135 		goto out1;
136 
137 	ctx = lima_ctx_get(&priv->ctx_mgr, args->ctx);
138 	if (!ctx) {
139 		err = -ENOENT;
140 		goto out1;
141 	}
142 
143 	submit.pipe = args->pipe;
144 	submit.bos = bos;
145 	submit.lbos = (void *)bos + size;
146 	submit.nr_bos = args->nr_bos;
147 	submit.task = task;
148 	submit.ctx = ctx;
149 	submit.flags = args->flags;
150 	submit.in_sync[0] = args->in_sync[0];
151 	submit.in_sync[1] = args->in_sync[1];
152 	submit.out_sync = args->out_sync;
153 
154 	err = lima_gem_submit(file, &submit);
155 
156 	lima_ctx_put(ctx);
157 out1:
158 	if (err)
159 		kmem_cache_free(pipe->task_slab, task);
160 out0:
161 	kvfree(bos);
162 	return err;
163 }
164 
165 static int lima_ioctl_gem_wait(struct drm_device *dev, void *data, struct drm_file *file)
166 {
167 	struct drm_lima_gem_wait *args = data;
168 
169 	if (args->op & ~(LIMA_GEM_WAIT_READ|LIMA_GEM_WAIT_WRITE))
170 		return -EINVAL;
171 
172 	return lima_gem_wait(file, args->handle, args->op, args->timeout_ns);
173 }
174 
175 static int lima_ioctl_ctx_create(struct drm_device *dev, void *data, struct drm_file *file)
176 {
177 	struct drm_lima_ctx_create *args = data;
178 	struct lima_drm_priv *priv = file->driver_priv;
179 	struct lima_device *ldev = to_lima_dev(dev);
180 
181 	if (args->_pad)
182 		return -EINVAL;
183 
184 	return lima_ctx_create(ldev, &priv->ctx_mgr, &args->id);
185 }
186 
187 static int lima_ioctl_ctx_free(struct drm_device *dev, void *data, struct drm_file *file)
188 {
189 	struct drm_lima_ctx_create *args = data;
190 	struct lima_drm_priv *priv = file->driver_priv;
191 
192 	if (args->_pad)
193 		return -EINVAL;
194 
195 	return lima_ctx_free(&priv->ctx_mgr, args->id);
196 }
197 
198 static int lima_drm_driver_open(struct drm_device *dev, struct drm_file *file)
199 {
200 	int err;
201 	struct lima_drm_priv *priv;
202 	struct lima_device *ldev = to_lima_dev(dev);
203 
204 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
205 	if (!priv)
206 		return -ENOMEM;
207 
208 	priv->vm = lima_vm_create(ldev);
209 	if (!priv->vm) {
210 		err = -ENOMEM;
211 		goto err_out0;
212 	}
213 
214 	lima_ctx_mgr_init(&priv->ctx_mgr);
215 
216 	file->driver_priv = priv;
217 	return 0;
218 
219 err_out0:
220 	kfree(priv);
221 	return err;
222 }
223 
224 static void lima_drm_driver_postclose(struct drm_device *dev, struct drm_file *file)
225 {
226 	struct lima_drm_priv *priv = file->driver_priv;
227 
228 	lima_ctx_mgr_fini(&priv->ctx_mgr);
229 	lima_vm_put(priv->vm);
230 	kfree(priv);
231 }
232 
233 static const struct drm_ioctl_desc lima_drm_driver_ioctls[] = {
234 	DRM_IOCTL_DEF_DRV(LIMA_GET_PARAM, lima_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
235 	DRM_IOCTL_DEF_DRV(LIMA_GEM_CREATE, lima_ioctl_gem_create, DRM_AUTH|DRM_RENDER_ALLOW),
236 	DRM_IOCTL_DEF_DRV(LIMA_GEM_INFO, lima_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
237 	DRM_IOCTL_DEF_DRV(LIMA_GEM_SUBMIT, lima_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
238 	DRM_IOCTL_DEF_DRV(LIMA_GEM_WAIT, lima_ioctl_gem_wait, DRM_AUTH|DRM_RENDER_ALLOW),
239 	DRM_IOCTL_DEF_DRV(LIMA_CTX_CREATE, lima_ioctl_ctx_create, DRM_AUTH|DRM_RENDER_ALLOW),
240 	DRM_IOCTL_DEF_DRV(LIMA_CTX_FREE, lima_ioctl_ctx_free, DRM_AUTH|DRM_RENDER_ALLOW),
241 };
242 
243 static const struct file_operations lima_drm_driver_fops = {
244 	.owner              = THIS_MODULE,
245 	.open               = drm_open,
246 	.release            = drm_release,
247 	.unlocked_ioctl     = drm_ioctl,
248 #ifdef CONFIG_COMPAT
249 	.compat_ioctl       = drm_compat_ioctl,
250 #endif
251 	.mmap               = lima_gem_mmap,
252 };
253 
254 static struct drm_driver lima_drm_driver = {
255 	.driver_features    = DRIVER_RENDER | DRIVER_GEM | DRIVER_PRIME | DRIVER_SYNCOBJ,
256 	.open               = lima_drm_driver_open,
257 	.postclose          = lima_drm_driver_postclose,
258 	.ioctls             = lima_drm_driver_ioctls,
259 	.num_ioctls         = ARRAY_SIZE(lima_drm_driver_ioctls),
260 	.fops               = &lima_drm_driver_fops,
261 	.gem_free_object_unlocked = lima_gem_free_object,
262 	.gem_open_object    = lima_gem_object_open,
263 	.gem_close_object   = lima_gem_object_close,
264 	.gem_vm_ops         = &lima_gem_vm_ops,
265 	.name               = "lima",
266 	.desc               = "lima DRM",
267 	.date               = "20190217",
268 	.major              = 1,
269 	.minor              = 0,
270 	.patchlevel         = 0,
271 
272 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
273 	.gem_prime_import_sg_table = lima_gem_prime_import_sg_table,
274 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
275 	.gem_prime_get_sg_table = lima_gem_prime_get_sg_table,
276 	.gem_prime_mmap = lima_gem_prime_mmap,
277 };
278 
279 static int lima_pdev_probe(struct platform_device *pdev)
280 {
281 	struct lima_device *ldev;
282 	struct drm_device *ddev;
283 	int err;
284 
285 	err = lima_sched_slab_init();
286 	if (err)
287 		return err;
288 
289 	ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
290 	if (!ldev) {
291 		err = -ENOMEM;
292 		goto err_out0;
293 	}
294 
295 	ldev->pdev = pdev;
296 	ldev->dev = &pdev->dev;
297 	ldev->id = (enum lima_gpu_id)of_device_get_match_data(&pdev->dev);
298 
299 	platform_set_drvdata(pdev, ldev);
300 
301 	/* Allocate and initialize the DRM device. */
302 	ddev = drm_dev_alloc(&lima_drm_driver, &pdev->dev);
303 	if (IS_ERR(ddev))
304 		return PTR_ERR(ddev);
305 
306 	ddev->dev_private = ldev;
307 	ldev->ddev = ddev;
308 
309 	err = lima_device_init(ldev);
310 	if (err) {
311 		dev_err(&pdev->dev, "Fatal error during GPU init\n");
312 		goto err_out1;
313 	}
314 
315 	/*
316 	 * Register the DRM device with the core and the connectors with
317 	 * sysfs.
318 	 */
319 	err = drm_dev_register(ddev, 0);
320 	if (err < 0)
321 		goto err_out2;
322 
323 	return 0;
324 
325 err_out2:
326 	lima_device_fini(ldev);
327 err_out1:
328 	drm_dev_put(ddev);
329 err_out0:
330 	lima_sched_slab_fini();
331 	return err;
332 }
333 
334 static int lima_pdev_remove(struct platform_device *pdev)
335 {
336 	struct lima_device *ldev = platform_get_drvdata(pdev);
337 	struct drm_device *ddev = ldev->ddev;
338 
339 	drm_dev_unregister(ddev);
340 	lima_device_fini(ldev);
341 	drm_dev_put(ddev);
342 	lima_sched_slab_fini();
343 	return 0;
344 }
345 
346 static const struct of_device_id dt_match[] = {
347 	{ .compatible = "arm,mali-400", .data = (void *)lima_gpu_mali400 },
348 	{ .compatible = "arm,mali-450", .data = (void *)lima_gpu_mali450 },
349 	{}
350 };
351 MODULE_DEVICE_TABLE(of, dt_match);
352 
353 static struct platform_driver lima_platform_driver = {
354 	.probe      = lima_pdev_probe,
355 	.remove     = lima_pdev_remove,
356 	.driver     = {
357 		.name   = "lima",
358 		.of_match_table = dt_match,
359 	},
360 };
361 
362 static int __init lima_init(void)
363 {
364 	return platform_driver_register(&lima_platform_driver);
365 }
366 module_init(lima_init);
367 
368 static void __exit lima_exit(void)
369 {
370 	platform_driver_unregister(&lima_platform_driver);
371 }
372 module_exit(lima_exit);
373 
374 MODULE_AUTHOR("Lima Project Developers");
375 MODULE_DESCRIPTION("Lima DRM Driver");
376 MODULE_LICENSE("GPL v2");
377