xref: /openbmc/linux/drivers/gpu/drm/lima/lima_drv.c (revision 82df5b73)
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 <linux/pm_runtime.h>
9 #include <drm/drm_ioctl.h>
10 #include <drm/drm_drv.h>
11 #include <drm/drm_prime.h>
12 #include <drm/lima_drm.h>
13 
14 #include "lima_device.h"
15 #include "lima_drv.h"
16 #include "lima_gem.h"
17 #include "lima_vm.h"
18 
19 int lima_sched_timeout_ms;
20 uint lima_heap_init_nr_pages = 8;
21 uint lima_max_error_tasks;
22 
23 MODULE_PARM_DESC(sched_timeout_ms, "task run timeout in ms");
24 module_param_named(sched_timeout_ms, lima_sched_timeout_ms, int, 0444);
25 
26 MODULE_PARM_DESC(heap_init_nr_pages, "heap buffer init number of pages");
27 module_param_named(heap_init_nr_pages, lima_heap_init_nr_pages, uint, 0444);
28 
29 MODULE_PARM_DESC(max_error_tasks, "max number of error tasks to save");
30 module_param_named(max_error_tasks, lima_max_error_tasks, uint, 0644);
31 
32 static int lima_ioctl_get_param(struct drm_device *dev, void *data, struct drm_file *file)
33 {
34 	struct drm_lima_get_param *args = data;
35 	struct lima_device *ldev = to_lima_dev(dev);
36 
37 	if (args->pad)
38 		return -EINVAL;
39 
40 	switch (args->param) {
41 	case DRM_LIMA_PARAM_GPU_ID:
42 		switch (ldev->id) {
43 		case lima_gpu_mali400:
44 			args->value = DRM_LIMA_PARAM_GPU_ID_MALI400;
45 			break;
46 		case lima_gpu_mali450:
47 			args->value = DRM_LIMA_PARAM_GPU_ID_MALI450;
48 			break;
49 		default:
50 			args->value = DRM_LIMA_PARAM_GPU_ID_UNKNOWN;
51 			break;
52 		}
53 		break;
54 
55 	case DRM_LIMA_PARAM_NUM_PP:
56 		args->value = ldev->pipe[lima_pipe_pp].num_processor;
57 		break;
58 
59 	case DRM_LIMA_PARAM_GP_VERSION:
60 		args->value = ldev->gp_version;
61 		break;
62 
63 	case DRM_LIMA_PARAM_PP_VERSION:
64 		args->value = ldev->pp_version;
65 		break;
66 
67 	default:
68 		return -EINVAL;
69 	}
70 
71 	return 0;
72 }
73 
74 static int lima_ioctl_gem_create(struct drm_device *dev, void *data, struct drm_file *file)
75 {
76 	struct drm_lima_gem_create *args = data;
77 
78 	if (args->pad)
79 		return -EINVAL;
80 
81 	if (args->flags & ~(LIMA_BO_FLAG_HEAP))
82 		return -EINVAL;
83 
84 	if (args->size == 0)
85 		return -EINVAL;
86 
87 	return lima_gem_create_handle(dev, file, args->size, args->flags, &args->handle);
88 }
89 
90 static int lima_ioctl_gem_info(struct drm_device *dev, void *data, struct drm_file *file)
91 {
92 	struct drm_lima_gem_info *args = data;
93 
94 	return lima_gem_get_info(file, args->handle, &args->va, &args->offset);
95 }
96 
97 static int lima_ioctl_gem_submit(struct drm_device *dev, void *data, struct drm_file *file)
98 {
99 	struct drm_lima_gem_submit *args = data;
100 	struct lima_device *ldev = to_lima_dev(dev);
101 	struct lima_drm_priv *priv = file->driver_priv;
102 	struct drm_lima_gem_submit_bo *bos;
103 	struct lima_sched_pipe *pipe;
104 	struct lima_sched_task *task;
105 	struct lima_ctx *ctx;
106 	struct lima_submit submit = {0};
107 	size_t size;
108 	int err = 0;
109 
110 	if (args->pipe >= lima_pipe_num || args->nr_bos == 0)
111 		return -EINVAL;
112 
113 	if (args->flags & ~(LIMA_SUBMIT_FLAG_EXPLICIT_FENCE))
114 		return -EINVAL;
115 
116 	pipe = ldev->pipe + args->pipe;
117 	if (args->frame_size != pipe->frame_size)
118 		return -EINVAL;
119 
120 	bos = kvcalloc(args->nr_bos, sizeof(*submit.bos) + sizeof(*submit.lbos), GFP_KERNEL);
121 	if (!bos)
122 		return -ENOMEM;
123 
124 	size = args->nr_bos * sizeof(*submit.bos);
125 	if (copy_from_user(bos, u64_to_user_ptr(args->bos), size)) {
126 		err = -EFAULT;
127 		goto out0;
128 	}
129 
130 	task = kmem_cache_zalloc(pipe->task_slab, GFP_KERNEL);
131 	if (!task) {
132 		err = -ENOMEM;
133 		goto out0;
134 	}
135 
136 	task->frame = task + 1;
137 	if (copy_from_user(task->frame, u64_to_user_ptr(args->frame), args->frame_size)) {
138 		err = -EFAULT;
139 		goto out1;
140 	}
141 
142 	err = pipe->task_validate(pipe, task);
143 	if (err)
144 		goto out1;
145 
146 	ctx = lima_ctx_get(&priv->ctx_mgr, args->ctx);
147 	if (!ctx) {
148 		err = -ENOENT;
149 		goto out1;
150 	}
151 
152 	submit.pipe = args->pipe;
153 	submit.bos = bos;
154 	submit.lbos = (void *)bos + size;
155 	submit.nr_bos = args->nr_bos;
156 	submit.task = task;
157 	submit.ctx = ctx;
158 	submit.flags = args->flags;
159 	submit.in_sync[0] = args->in_sync[0];
160 	submit.in_sync[1] = args->in_sync[1];
161 	submit.out_sync = args->out_sync;
162 
163 	err = lima_gem_submit(file, &submit);
164 
165 	lima_ctx_put(ctx);
166 out1:
167 	if (err)
168 		kmem_cache_free(pipe->task_slab, task);
169 out0:
170 	kvfree(bos);
171 	return err;
172 }
173 
174 static int lima_ioctl_gem_wait(struct drm_device *dev, void *data, struct drm_file *file)
175 {
176 	struct drm_lima_gem_wait *args = data;
177 
178 	if (args->op & ~(LIMA_GEM_WAIT_READ|LIMA_GEM_WAIT_WRITE))
179 		return -EINVAL;
180 
181 	return lima_gem_wait(file, args->handle, args->op, args->timeout_ns);
182 }
183 
184 static int lima_ioctl_ctx_create(struct drm_device *dev, void *data, struct drm_file *file)
185 {
186 	struct drm_lima_ctx_create *args = data;
187 	struct lima_drm_priv *priv = file->driver_priv;
188 	struct lima_device *ldev = to_lima_dev(dev);
189 
190 	if (args->_pad)
191 		return -EINVAL;
192 
193 	return lima_ctx_create(ldev, &priv->ctx_mgr, &args->id);
194 }
195 
196 static int lima_ioctl_ctx_free(struct drm_device *dev, void *data, struct drm_file *file)
197 {
198 	struct drm_lima_ctx_create *args = data;
199 	struct lima_drm_priv *priv = file->driver_priv;
200 
201 	if (args->_pad)
202 		return -EINVAL;
203 
204 	return lima_ctx_free(&priv->ctx_mgr, args->id);
205 }
206 
207 static int lima_drm_driver_open(struct drm_device *dev, struct drm_file *file)
208 {
209 	int err;
210 	struct lima_drm_priv *priv;
211 	struct lima_device *ldev = to_lima_dev(dev);
212 
213 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
214 	if (!priv)
215 		return -ENOMEM;
216 
217 	priv->vm = lima_vm_create(ldev);
218 	if (!priv->vm) {
219 		err = -ENOMEM;
220 		goto err_out0;
221 	}
222 
223 	lima_ctx_mgr_init(&priv->ctx_mgr);
224 
225 	file->driver_priv = priv;
226 	return 0;
227 
228 err_out0:
229 	kfree(priv);
230 	return err;
231 }
232 
233 static void lima_drm_driver_postclose(struct drm_device *dev, struct drm_file *file)
234 {
235 	struct lima_drm_priv *priv = file->driver_priv;
236 
237 	lima_ctx_mgr_fini(&priv->ctx_mgr);
238 	lima_vm_put(priv->vm);
239 	kfree(priv);
240 }
241 
242 static const struct drm_ioctl_desc lima_drm_driver_ioctls[] = {
243 	DRM_IOCTL_DEF_DRV(LIMA_GET_PARAM, lima_ioctl_get_param, DRM_RENDER_ALLOW),
244 	DRM_IOCTL_DEF_DRV(LIMA_GEM_CREATE, lima_ioctl_gem_create, DRM_RENDER_ALLOW),
245 	DRM_IOCTL_DEF_DRV(LIMA_GEM_INFO, lima_ioctl_gem_info, DRM_RENDER_ALLOW),
246 	DRM_IOCTL_DEF_DRV(LIMA_GEM_SUBMIT, lima_ioctl_gem_submit, DRM_RENDER_ALLOW),
247 	DRM_IOCTL_DEF_DRV(LIMA_GEM_WAIT, lima_ioctl_gem_wait, DRM_RENDER_ALLOW),
248 	DRM_IOCTL_DEF_DRV(LIMA_CTX_CREATE, lima_ioctl_ctx_create, DRM_RENDER_ALLOW),
249 	DRM_IOCTL_DEF_DRV(LIMA_CTX_FREE, lima_ioctl_ctx_free, DRM_RENDER_ALLOW),
250 };
251 
252 DEFINE_DRM_GEM_FOPS(lima_drm_driver_fops);
253 
254 /**
255  * Changelog:
256  *
257  * - 1.1.0 - add heap buffer support
258  */
259 
260 static struct drm_driver lima_drm_driver = {
261 	.driver_features    = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
262 	.open               = lima_drm_driver_open,
263 	.postclose          = lima_drm_driver_postclose,
264 	.ioctls             = lima_drm_driver_ioctls,
265 	.num_ioctls         = ARRAY_SIZE(lima_drm_driver_ioctls),
266 	.fops               = &lima_drm_driver_fops,
267 	.name               = "lima",
268 	.desc               = "lima DRM",
269 	.date               = "20191231",
270 	.major              = 1,
271 	.minor              = 1,
272 	.patchlevel         = 0,
273 
274 	.gem_create_object  = lima_gem_create_object,
275 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
276 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
277 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
278 	.gem_prime_mmap = drm_gem_prime_mmap,
279 };
280 
281 struct lima_block_reader {
282 	void *dst;
283 	size_t base;
284 	size_t count;
285 	size_t off;
286 	ssize_t read;
287 };
288 
289 static bool lima_read_block(struct lima_block_reader *reader,
290 			    void *src, size_t src_size)
291 {
292 	size_t max_off = reader->base + src_size;
293 
294 	if (reader->off < max_off) {
295 		size_t size = min_t(size_t, max_off - reader->off,
296 				    reader->count);
297 
298 		memcpy(reader->dst, src + (reader->off - reader->base), size);
299 
300 		reader->dst += size;
301 		reader->off += size;
302 		reader->read += size;
303 		reader->count -= size;
304 	}
305 
306 	reader->base = max_off;
307 
308 	return !!reader->count;
309 }
310 
311 static ssize_t lima_error_state_read(struct file *filp, struct kobject *kobj,
312 				     struct bin_attribute *attr, char *buf,
313 				     loff_t off, size_t count)
314 {
315 	struct device *dev = kobj_to_dev(kobj);
316 	struct lima_device *ldev = dev_get_drvdata(dev);
317 	struct lima_sched_error_task *et;
318 	struct lima_block_reader reader = {
319 		.dst = buf,
320 		.count = count,
321 		.off = off,
322 	};
323 
324 	mutex_lock(&ldev->error_task_list_lock);
325 
326 	if (lima_read_block(&reader, &ldev->dump, sizeof(ldev->dump))) {
327 		list_for_each_entry(et, &ldev->error_task_list, list) {
328 			if (!lima_read_block(&reader, et->data, et->size))
329 				break;
330 		}
331 	}
332 
333 	mutex_unlock(&ldev->error_task_list_lock);
334 	return reader.read;
335 }
336 
337 static ssize_t lima_error_state_write(struct file *file, struct kobject *kobj,
338 				      struct bin_attribute *attr, char *buf,
339 				      loff_t off, size_t count)
340 {
341 	struct device *dev = kobj_to_dev(kobj);
342 	struct lima_device *ldev = dev_get_drvdata(dev);
343 	struct lima_sched_error_task *et, *tmp;
344 
345 	mutex_lock(&ldev->error_task_list_lock);
346 
347 	list_for_each_entry_safe(et, tmp, &ldev->error_task_list, list) {
348 		list_del(&et->list);
349 		kvfree(et);
350 	}
351 
352 	ldev->dump.size = 0;
353 	ldev->dump.num_tasks = 0;
354 
355 	mutex_unlock(&ldev->error_task_list_lock);
356 
357 	return count;
358 }
359 
360 static const struct bin_attribute lima_error_state_attr = {
361 	.attr.name = "error",
362 	.attr.mode = 0600,
363 	.size = 0,
364 	.read = lima_error_state_read,
365 	.write = lima_error_state_write,
366 };
367 
368 static int lima_pdev_probe(struct platform_device *pdev)
369 {
370 	struct lima_device *ldev;
371 	struct drm_device *ddev;
372 	int err;
373 
374 	err = lima_sched_slab_init();
375 	if (err)
376 		return err;
377 
378 	ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
379 	if (!ldev) {
380 		err = -ENOMEM;
381 		goto err_out0;
382 	}
383 
384 	ldev->dev = &pdev->dev;
385 	ldev->id = (enum lima_gpu_id)of_device_get_match_data(&pdev->dev);
386 
387 	platform_set_drvdata(pdev, ldev);
388 
389 	/* Allocate and initialize the DRM device. */
390 	ddev = drm_dev_alloc(&lima_drm_driver, &pdev->dev);
391 	if (IS_ERR(ddev))
392 		return PTR_ERR(ddev);
393 
394 	ddev->dev_private = ldev;
395 	ldev->ddev = ddev;
396 
397 	err = lima_device_init(ldev);
398 	if (err)
399 		goto err_out1;
400 
401 	err = lima_devfreq_init(ldev);
402 	if (err) {
403 		dev_err(&pdev->dev, "Fatal error during devfreq init\n");
404 		goto err_out2;
405 	}
406 
407 	pm_runtime_set_active(ldev->dev);
408 	pm_runtime_mark_last_busy(ldev->dev);
409 	pm_runtime_set_autosuspend_delay(ldev->dev, 200);
410 	pm_runtime_use_autosuspend(ldev->dev);
411 	pm_runtime_enable(ldev->dev);
412 
413 	/*
414 	 * Register the DRM device with the core and the connectors with
415 	 * sysfs.
416 	 */
417 	err = drm_dev_register(ddev, 0);
418 	if (err < 0)
419 		goto err_out3;
420 
421 	if (sysfs_create_bin_file(&ldev->dev->kobj, &lima_error_state_attr))
422 		dev_warn(ldev->dev, "fail to create error state sysfs\n");
423 
424 	return 0;
425 
426 err_out3:
427 	pm_runtime_disable(ldev->dev);
428 	lima_devfreq_fini(ldev);
429 err_out2:
430 	lima_device_fini(ldev);
431 err_out1:
432 	drm_dev_put(ddev);
433 err_out0:
434 	lima_sched_slab_fini();
435 	return err;
436 }
437 
438 static int lima_pdev_remove(struct platform_device *pdev)
439 {
440 	struct lima_device *ldev = platform_get_drvdata(pdev);
441 	struct drm_device *ddev = ldev->ddev;
442 
443 	sysfs_remove_bin_file(&ldev->dev->kobj, &lima_error_state_attr);
444 
445 	drm_dev_unregister(ddev);
446 
447 	/* stop autosuspend to make sure device is in active state */
448 	pm_runtime_set_autosuspend_delay(ldev->dev, -1);
449 	pm_runtime_disable(ldev->dev);
450 
451 	lima_devfreq_fini(ldev);
452 	lima_device_fini(ldev);
453 
454 	drm_dev_put(ddev);
455 	lima_sched_slab_fini();
456 	return 0;
457 }
458 
459 static const struct of_device_id dt_match[] = {
460 	{ .compatible = "arm,mali-400", .data = (void *)lima_gpu_mali400 },
461 	{ .compatible = "arm,mali-450", .data = (void *)lima_gpu_mali450 },
462 	{}
463 };
464 MODULE_DEVICE_TABLE(of, dt_match);
465 
466 static const struct dev_pm_ops lima_pm_ops = {
467 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
468 	SET_RUNTIME_PM_OPS(lima_device_suspend, lima_device_resume, NULL)
469 };
470 
471 static struct platform_driver lima_platform_driver = {
472 	.probe      = lima_pdev_probe,
473 	.remove     = lima_pdev_remove,
474 	.driver     = {
475 		.name   = "lima",
476 		.pm	= &lima_pm_ops,
477 		.of_match_table = dt_match,
478 	},
479 };
480 
481 module_platform_driver(lima_platform_driver);
482 
483 MODULE_AUTHOR("Lima Project Developers");
484 MODULE_DESCRIPTION("Lima DRM Driver");
485 MODULE_LICENSE("GPL v2");
486