1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd., Rob Herring <robh@kernel.org> */
4 /* Copyright 2019 Collabora ltd. */
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
8 #include <linux/pagemap.h>
9 #include <linux/pm_runtime.h>
10 #include <drm/panfrost_drm.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_ioctl.h>
13 #include <drm/drm_syncobj.h>
14 #include <drm/drm_utils.h>
16 #include "panfrost_device.h"
17 #include "panfrost_devfreq.h"
18 #include "panfrost_gem.h"
19 #include "panfrost_mmu.h"
20 #include "panfrost_job.h"
21 #include "panfrost_gpu.h"
23 static int panfrost_ioctl_get_param(struct drm_device *ddev, void *data, struct drm_file *file)
25 struct drm_panfrost_get_param *param = data;
26 struct panfrost_device *pfdev = ddev->dev_private;
31 switch (param->param) {
32 case DRM_PANFROST_PARAM_GPU_PROD_ID:
33 param->value = pfdev->features.id;
42 static int panfrost_ioctl_create_bo(struct drm_device *dev, void *data,
43 struct drm_file *file)
46 struct drm_gem_shmem_object *shmem;
47 struct drm_panfrost_create_bo *args = data;
49 if (!args->size || args->flags || args->pad)
52 shmem = drm_gem_shmem_create_with_handle(file, dev, args->size,
55 return PTR_ERR(shmem);
57 ret = panfrost_mmu_map(to_panfrost_bo(&shmem->base));
61 args->offset = to_panfrost_bo(&shmem->base)->node.start << PAGE_SHIFT;
66 drm_gem_object_put_unlocked(&shmem->base);
71 * panfrost_lookup_bos() - Sets up job->bo[] with the GEM objects
72 * referenced by the job.
74 * @file_priv: DRM file for this fd
76 * @job: job being set up
78 * Resolve handles from userspace to BOs and attach them to job.
80 * Note that this function doesn't need to unreference the BOs on
81 * failure, because that will happen at panfrost_job_cleanup() time.
84 panfrost_lookup_bos(struct drm_device *dev,
85 struct drm_file *file_priv,
86 struct drm_panfrost_submit *args,
87 struct panfrost_job *job)
89 job->bo_count = args->bo_handle_count;
94 job->implicit_fences = kvmalloc_array(job->bo_count,
95 sizeof(struct dma_fence *),
96 GFP_KERNEL | __GFP_ZERO);
97 if (!job->implicit_fences)
100 return drm_gem_objects_lookup(file_priv,
101 (void __user *)(uintptr_t)args->bo_handles,
102 job->bo_count, &job->bos);
106 * panfrost_copy_in_sync() - Sets up job->in_fences[] with the sync objects
107 * referenced by the job.
109 * @file_priv: DRM file for this fd
111 * @job: job being set up
113 * Resolve syncobjs from userspace to fences and attach them to job.
115 * Note that this function doesn't need to unreference the fences on
116 * failure, because that will happen at panfrost_job_cleanup() time.
119 panfrost_copy_in_sync(struct drm_device *dev,
120 struct drm_file *file_priv,
121 struct drm_panfrost_submit *args,
122 struct panfrost_job *job)
128 job->in_fence_count = args->in_sync_count;
130 if (!job->in_fence_count)
133 job->in_fences = kvmalloc_array(job->in_fence_count,
134 sizeof(struct dma_fence *),
135 GFP_KERNEL | __GFP_ZERO);
136 if (!job->in_fences) {
137 DRM_DEBUG("Failed to allocate job in fences\n");
141 handles = kvmalloc_array(job->in_fence_count, sizeof(u32), GFP_KERNEL);
144 DRM_DEBUG("Failed to allocate incoming syncobj handles\n");
148 if (copy_from_user(handles,
149 (void __user *)(uintptr_t)args->in_syncs,
150 job->in_fence_count * sizeof(u32))) {
152 DRM_DEBUG("Failed to copy in syncobj handles\n");
156 for (i = 0; i < job->in_fence_count; i++) {
157 ret = drm_syncobj_find_fence(file_priv, handles[i], 0, 0,
168 static int panfrost_ioctl_submit(struct drm_device *dev, void *data,
169 struct drm_file *file)
171 struct panfrost_device *pfdev = dev->dev_private;
172 struct drm_panfrost_submit *args = data;
173 struct drm_syncobj *sync_out = NULL;
174 struct panfrost_job *job;
180 if (args->requirements && args->requirements != PANFROST_JD_REQ_FS)
183 if (args->out_sync > 0) {
184 sync_out = drm_syncobj_find(file, args->out_sync);
189 job = kzalloc(sizeof(*job), GFP_KERNEL);
195 kref_init(&job->refcount);
199 job->requirements = args->requirements;
200 job->flush_id = panfrost_gpu_get_latest_flush_id(pfdev);
201 job->file_priv = file->driver_priv;
203 ret = panfrost_copy_in_sync(dev, file, args, job);
207 ret = panfrost_lookup_bos(dev, file, args, job);
211 ret = panfrost_job_push(job);
215 /* Update the return sync object for the job */
217 drm_syncobj_replace_fence(sync_out, job->render_done_fence);
220 panfrost_job_put(job);
223 drm_syncobj_put(sync_out);
229 panfrost_ioctl_wait_bo(struct drm_device *dev, void *data,
230 struct drm_file *file_priv)
233 struct drm_panfrost_wait_bo *args = data;
234 struct drm_gem_object *gem_obj;
235 unsigned long timeout = drm_timeout_abs_to_jiffies(args->timeout_ns);
240 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
244 ret = reservation_object_wait_timeout_rcu(gem_obj->resv, true,
247 ret = timeout ? -ETIMEDOUT : -EBUSY;
249 drm_gem_object_put_unlocked(gem_obj);
254 static int panfrost_ioctl_mmap_bo(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
257 struct drm_panfrost_mmap_bo *args = data;
258 struct drm_gem_object *gem_obj;
261 if (args->flags != 0) {
262 DRM_INFO("unknown mmap_bo flags: %d\n", args->flags);
266 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
268 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
272 ret = drm_gem_create_mmap_offset(gem_obj);
274 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
275 drm_gem_object_put_unlocked(gem_obj);
280 static int panfrost_ioctl_get_bo_offset(struct drm_device *dev, void *data,
281 struct drm_file *file_priv)
283 struct drm_panfrost_get_bo_offset *args = data;
284 struct drm_gem_object *gem_obj;
285 struct panfrost_gem_object *bo;
287 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
289 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
292 bo = to_panfrost_bo(gem_obj);
294 args->offset = bo->node.start << PAGE_SHIFT;
296 drm_gem_object_put_unlocked(gem_obj);
301 panfrost_open(struct drm_device *dev, struct drm_file *file)
303 struct panfrost_device *pfdev = dev->dev_private;
304 struct panfrost_file_priv *panfrost_priv;
306 panfrost_priv = kzalloc(sizeof(*panfrost_priv), GFP_KERNEL);
310 panfrost_priv->pfdev = pfdev;
311 file->driver_priv = panfrost_priv;
313 return panfrost_job_open(panfrost_priv);
317 panfrost_postclose(struct drm_device *dev, struct drm_file *file)
319 struct panfrost_file_priv *panfrost_priv = file->driver_priv;
321 panfrost_job_close(panfrost_priv);
323 kfree(panfrost_priv);
326 /* DRM_AUTH is required on SUBMIT for now, while all clients share a single
327 * address space. Note that render nodes would be able to submit jobs that
328 * could access BOs from clients authenticated with the master node.
330 static const struct drm_ioctl_desc panfrost_drm_driver_ioctls[] = {
331 #define PANFROST_IOCTL(n, func, flags) \
332 DRM_IOCTL_DEF_DRV(PANFROST_##n, panfrost_ioctl_##func, flags)
334 PANFROST_IOCTL(SUBMIT, submit, DRM_RENDER_ALLOW | DRM_AUTH),
335 PANFROST_IOCTL(WAIT_BO, wait_bo, DRM_RENDER_ALLOW),
336 PANFROST_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW),
337 PANFROST_IOCTL(MMAP_BO, mmap_bo, DRM_RENDER_ALLOW),
338 PANFROST_IOCTL(GET_PARAM, get_param, DRM_RENDER_ALLOW),
339 PANFROST_IOCTL(GET_BO_OFFSET, get_bo_offset, DRM_RENDER_ALLOW),
342 DEFINE_DRM_GEM_SHMEM_FOPS(panfrost_drm_driver_fops);
344 static struct drm_driver panfrost_drm_driver = {
345 .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_PRIME |
347 .open = panfrost_open,
348 .postclose = panfrost_postclose,
349 .ioctls = panfrost_drm_driver_ioctls,
350 .num_ioctls = ARRAY_SIZE(panfrost_drm_driver_ioctls),
351 .fops = &panfrost_drm_driver_fops,
353 .desc = "panfrost DRM",
358 .gem_create_object = panfrost_gem_create_object,
359 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
360 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
361 .gem_prime_import_sg_table = panfrost_gem_prime_import_sg_table,
362 .gem_prime_mmap = drm_gem_prime_mmap,
365 static int panfrost_probe(struct platform_device *pdev)
367 struct panfrost_device *pfdev;
368 struct drm_device *ddev;
371 pfdev = devm_kzalloc(&pdev->dev, sizeof(*pfdev), GFP_KERNEL);
376 pfdev->dev = &pdev->dev;
378 platform_set_drvdata(pdev, pfdev);
380 /* Allocate and initialze the DRM device. */
381 ddev = drm_dev_alloc(&panfrost_drm_driver, &pdev->dev);
383 return PTR_ERR(ddev);
385 ddev->dev_private = pfdev;
388 spin_lock_init(&pfdev->mm_lock);
390 /* 4G enough for now. can be 48-bit */
391 drm_mm_init(&pfdev->mm, SZ_32M >> PAGE_SHIFT, (SZ_4G - SZ_32M) >> PAGE_SHIFT);
393 pm_runtime_use_autosuspend(pfdev->dev);
394 pm_runtime_set_autosuspend_delay(pfdev->dev, 50); /* ~3 frames */
395 pm_runtime_enable(pfdev->dev);
397 err = panfrost_device_init(pfdev);
399 if (err != -EPROBE_DEFER)
400 dev_err(&pdev->dev, "Fatal error during GPU init\n");
404 err = panfrost_devfreq_init(pfdev);
406 if (err != -EPROBE_DEFER)
407 dev_err(&pdev->dev, "Fatal error during devfreq init\n");
412 * Register the DRM device with the core and the connectors with
415 err = drm_dev_register(ddev, 0);
422 panfrost_device_fini(pfdev);
424 pm_runtime_disable(pfdev->dev);
429 static int panfrost_remove(struct platform_device *pdev)
431 struct panfrost_device *pfdev = platform_get_drvdata(pdev);
432 struct drm_device *ddev = pfdev->ddev;
434 drm_dev_unregister(ddev);
435 pm_runtime_get_sync(pfdev->dev);
436 pm_runtime_put_sync_autosuspend(pfdev->dev);
437 pm_runtime_disable(pfdev->dev);
438 panfrost_device_fini(pfdev);
443 static const struct of_device_id dt_match[] = {
444 { .compatible = "arm,mali-t604" },
445 { .compatible = "arm,mali-t624" },
446 { .compatible = "arm,mali-t628" },
447 { .compatible = "arm,mali-t720" },
448 { .compatible = "arm,mali-t760" },
449 { .compatible = "arm,mali-t820" },
450 { .compatible = "arm,mali-t830" },
451 { .compatible = "arm,mali-t860" },
452 { .compatible = "arm,mali-t880" },
455 MODULE_DEVICE_TABLE(of, dt_match);
457 static const struct dev_pm_ops panfrost_pm_ops = {
458 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
459 SET_RUNTIME_PM_OPS(panfrost_device_suspend, panfrost_device_resume, NULL)
462 static struct platform_driver panfrost_driver = {
463 .probe = panfrost_probe,
464 .remove = panfrost_remove,
467 .pm = &panfrost_pm_ops,
468 .of_match_table = dt_match,
471 module_platform_driver(panfrost_driver);
473 MODULE_AUTHOR("Panfrost Project Developers");
474 MODULE_DESCRIPTION("Panfrost DRM Driver");
475 MODULE_LICENSE("GPL v2");