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