1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
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>
14 #include "lima_device.h"
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
;
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);
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);
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);
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);
36 static int lima_ioctl_get_param(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
38 struct drm_lima_get_param
*args
= data
;
39 struct lima_device
*ldev
= to_lima_dev(dev
);
44 switch (args
->param
) {
45 case DRM_LIMA_PARAM_GPU_ID
:
47 case lima_gpu_mali400
:
48 args
->value
= DRM_LIMA_PARAM_GPU_ID_MALI400
;
50 case lima_gpu_mali450
:
51 args
->value
= DRM_LIMA_PARAM_GPU_ID_MALI450
;
54 args
->value
= DRM_LIMA_PARAM_GPU_ID_UNKNOWN
;
59 case DRM_LIMA_PARAM_NUM_PP
:
60 args
->value
= ldev
->pipe
[lima_pipe_pp
].num_processor
;
63 case DRM_LIMA_PARAM_GP_VERSION
:
64 args
->value
= ldev
->gp_version
;
67 case DRM_LIMA_PARAM_PP_VERSION
:
68 args
->value
= ldev
->pp_version
;
78 static int lima_ioctl_gem_create(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
80 struct drm_lima_gem_create
*args
= data
;
85 if (args
->flags
& ~(LIMA_BO_FLAG_HEAP
))
91 return lima_gem_create_handle(dev
, file
, args
->size
, args
->flags
, &args
->handle
);
94 static int lima_ioctl_gem_info(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
96 struct drm_lima_gem_info
*args
= data
;
98 return lima_gem_get_info(file
, args
->handle
, &args
->va
, &args
->offset
);
101 static int lima_ioctl_gem_submit(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
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};
114 if (args
->pipe
>= lima_pipe_num
|| args
->nr_bos
== 0)
117 if (args
->flags
& ~(LIMA_SUBMIT_FLAG_EXPLICIT_FENCE
))
120 pipe
= ldev
->pipe
+ args
->pipe
;
121 if (args
->frame_size
!= pipe
->frame_size
)
124 bos
= kvcalloc(args
->nr_bos
, sizeof(*submit
.bos
) + sizeof(*submit
.lbos
), GFP_KERNEL
);
128 size
= args
->nr_bos
* sizeof(*submit
.bos
);
129 if (copy_from_user(bos
, u64_to_user_ptr(args
->bos
), size
)) {
134 task
= kmem_cache_zalloc(pipe
->task_slab
, GFP_KERNEL
);
140 task
->frame
= task
+ 1;
141 if (copy_from_user(task
->frame
, u64_to_user_ptr(args
->frame
), args
->frame_size
)) {
146 err
= pipe
->task_validate(pipe
, task
);
150 ctx
= lima_ctx_get(&priv
->ctx_mgr
, args
->ctx
);
156 submit
.pipe
= args
->pipe
;
158 submit
.lbos
= (void *)bos
+ size
;
159 submit
.nr_bos
= args
->nr_bos
;
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
;
167 err
= lima_gem_submit(file
, &submit
);
172 kmem_cache_free(pipe
->task_slab
, task
);
178 static int lima_ioctl_gem_wait(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
180 struct drm_lima_gem_wait
*args
= data
;
182 if (args
->op
& ~(LIMA_GEM_WAIT_READ
|LIMA_GEM_WAIT_WRITE
))
185 return lima_gem_wait(file
, args
->handle
, args
->op
, args
->timeout_ns
);
188 static int lima_ioctl_ctx_create(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
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
);
197 return lima_ctx_create(ldev
, &priv
->ctx_mgr
, &args
->id
);
200 static int lima_ioctl_ctx_free(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
202 struct drm_lima_ctx_create
*args
= data
;
203 struct lima_drm_priv
*priv
= file
->driver_priv
;
208 return lima_ctx_free(&priv
->ctx_mgr
, args
->id
);
211 static int lima_drm_driver_open(struct drm_device
*dev
, struct drm_file
*file
)
214 struct lima_drm_priv
*priv
;
215 struct lima_device
*ldev
= to_lima_dev(dev
);
217 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
221 priv
->vm
= lima_vm_create(ldev
);
227 lima_ctx_mgr_init(&priv
->ctx_mgr
);
229 file
->driver_priv
= priv
;
237 static void lima_drm_driver_postclose(struct drm_device
*dev
, struct drm_file
*file
)
239 struct lima_drm_priv
*priv
= file
->driver_priv
;
241 lima_ctx_mgr_fini(&priv
->ctx_mgr
);
242 lima_vm_put(priv
->vm
);
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
),
256 DEFINE_DRM_GEM_FOPS(lima_drm_driver_fops
);
261 * - 1.1.0 - add heap buffer support
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
,
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
,
285 struct lima_block_reader
{
293 static bool lima_read_block(struct lima_block_reader
*reader
,
294 void *src
, size_t src_size
)
296 size_t max_off
= reader
->base
+ src_size
;
298 if (reader
->off
< max_off
) {
299 size_t size
= min_t(size_t, max_off
- reader
->off
,
302 memcpy(reader
->dst
, src
+ (reader
->off
- reader
->base
), size
);
306 reader
->read
+= size
;
307 reader
->count
-= size
;
310 reader
->base
= max_off
;
312 return !!reader
->count
;
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
)
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
= {
328 mutex_lock(&ldev
->error_task_list_lock
);
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
))
337 mutex_unlock(&ldev
->error_task_list_lock
);
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
)
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
;
349 mutex_lock(&ldev
->error_task_list_lock
);
351 list_for_each_entry_safe(et
, tmp
, &ldev
->error_task_list
, list
) {
357 ldev
->dump
.num_tasks
= 0;
359 mutex_unlock(&ldev
->error_task_list_lock
);
364 static const struct bin_attribute lima_error_state_attr
= {
365 .attr
.name
= "error",
368 .read
= lima_error_state_read
,
369 .write
= lima_error_state_write
,
372 static int lima_pdev_probe(struct platform_device
*pdev
)
374 struct lima_device
*ldev
;
375 struct drm_device
*ddev
;
378 err
= lima_sched_slab_init();
382 ldev
= devm_kzalloc(&pdev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
388 ldev
->dev
= &pdev
->dev
;
389 ldev
->id
= (enum lima_gpu_id
)of_device_get_match_data(&pdev
->dev
);
391 platform_set_drvdata(pdev
, ldev
);
393 /* Allocate and initialize the DRM device. */
394 ddev
= drm_dev_alloc(&lima_drm_driver
, &pdev
->dev
);
396 return PTR_ERR(ddev
);
398 ddev
->dev_private
= ldev
;
401 err
= lima_device_init(ldev
);
405 err
= lima_devfreq_init(ldev
);
407 dev_err(&pdev
->dev
, "Fatal error during devfreq init\n");
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
);
418 * Register the DRM device with the core and the connectors with
421 err
= drm_dev_register(ddev
, 0);
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");
431 pm_runtime_disable(ldev
->dev
);
432 lima_devfreq_fini(ldev
);
434 lima_device_fini(ldev
);
438 lima_sched_slab_fini();
442 static int lima_pdev_remove(struct platform_device
*pdev
)
444 struct lima_device
*ldev
= platform_get_drvdata(pdev
);
445 struct drm_device
*ddev
= ldev
->ddev
;
447 sysfs_remove_bin_file(&ldev
->dev
->kobj
, &lima_error_state_attr
);
449 drm_dev_unregister(ddev
);
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
);
455 lima_devfreq_fini(ldev
);
456 lima_device_fini(ldev
);
459 lima_sched_slab_fini();
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
},
468 MODULE_DEVICE_TABLE(of
, dt_match
);
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
)
475 static struct platform_driver lima_platform_driver
= {
476 .probe
= lima_pdev_probe
,
477 .remove
= lima_pdev_remove
,
481 .of_match_table
= dt_match
,
485 module_platform_driver(lima_platform_driver
);
487 MODULE_AUTHOR("Lima Project Developers");
488 MODULE_DESCRIPTION("Lima DRM Driver");
489 MODULE_LICENSE("GPL v2");