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