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_gem.h"
18 #include "panfrost_mmu.h"
19 #include "panfrost_job.h"
20 #include "panfrost_gpu.h"
21 #include "panfrost_perfcnt.h"
23 static bool unstable_ioctls;
24 module_param_unsafe(unstable_ioctls, bool, 0600);
26 static int panfrost_ioctl_get_param(struct drm_device *ddev, void *data, struct drm_file *file)
28 struct drm_panfrost_get_param *param = data;
29 struct panfrost_device *pfdev = ddev->dev_private;
34 #define PANFROST_FEATURE(name, member) \
35 case DRM_PANFROST_PARAM_ ## name: \
36 param->value = pfdev->features.member; \
38 #define PANFROST_FEATURE_ARRAY(name, member, max) \
39 case DRM_PANFROST_PARAM_ ## name ## 0 ... \
40 DRM_PANFROST_PARAM_ ## name ## max: \
41 param->value = pfdev->features.member[param->param - \
42 DRM_PANFROST_PARAM_ ## name ## 0]; \
45 switch (param->param) {
46 PANFROST_FEATURE(GPU_PROD_ID, id);
47 PANFROST_FEATURE(GPU_REVISION, revision);
48 PANFROST_FEATURE(SHADER_PRESENT, shader_present);
49 PANFROST_FEATURE(TILER_PRESENT, tiler_present);
50 PANFROST_FEATURE(L2_PRESENT, l2_present);
51 PANFROST_FEATURE(STACK_PRESENT, stack_present);
52 PANFROST_FEATURE(AS_PRESENT, as_present);
53 PANFROST_FEATURE(JS_PRESENT, js_present);
54 PANFROST_FEATURE(L2_FEATURES, l2_features);
55 PANFROST_FEATURE(CORE_FEATURES, core_features);
56 PANFROST_FEATURE(TILER_FEATURES, tiler_features);
57 PANFROST_FEATURE(MEM_FEATURES, mem_features);
58 PANFROST_FEATURE(MMU_FEATURES, mmu_features);
59 PANFROST_FEATURE(THREAD_FEATURES, thread_features);
60 PANFROST_FEATURE(MAX_THREADS, max_threads);
61 PANFROST_FEATURE(THREAD_MAX_WORKGROUP_SZ,
62 thread_max_workgroup_sz);
63 PANFROST_FEATURE(THREAD_MAX_BARRIER_SZ,
64 thread_max_barrier_sz);
65 PANFROST_FEATURE(COHERENCY_FEATURES, coherency_features);
66 PANFROST_FEATURE(AFBC_FEATURES, afbc_features);
67 PANFROST_FEATURE_ARRAY(TEXTURE_FEATURES, texture_features, 3);
68 PANFROST_FEATURE_ARRAY(JS_FEATURES, js_features, 15);
69 PANFROST_FEATURE(NR_CORE_GROUPS, nr_core_groups);
70 PANFROST_FEATURE(THREAD_TLS_ALLOC, thread_tls_alloc);
78 static int panfrost_ioctl_create_bo(struct drm_device *dev, void *data,
79 struct drm_file *file)
81 struct panfrost_file_priv *priv = file->driver_priv;
82 struct panfrost_gem_object *bo;
83 struct drm_panfrost_create_bo *args = data;
84 struct panfrost_gem_mapping *mapping;
87 if (!args->size || args->pad ||
88 (args->flags & ~(PANFROST_BO_NOEXEC | PANFROST_BO_HEAP)))
91 /* Heaps should never be executable */
92 if ((args->flags & PANFROST_BO_HEAP) &&
93 !(args->flags & PANFROST_BO_NOEXEC))
96 bo = panfrost_gem_create(dev, args->size, args->flags);
100 ret = drm_gem_handle_create(file, &bo->base.base, &args->handle);
104 mapping = panfrost_gem_mapping_get(bo, priv);
106 args->offset = mapping->mmnode.start << PAGE_SHIFT;
107 panfrost_gem_mapping_put(mapping);
109 /* This can only happen if the handle from
110 * drm_gem_handle_create() has already been guessed and freed
117 drm_gem_object_put(&bo->base.base);
122 * panfrost_lookup_bos() - Sets up job->bo[] with the GEM objects
123 * referenced by the job.
125 * @file_priv: DRM file for this fd
127 * @job: job being set up
129 * Resolve handles from userspace to BOs and attach them to job.
131 * Note that this function doesn't need to unreference the BOs on
132 * failure, because that will happen at panfrost_job_cleanup() time.
135 panfrost_lookup_bos(struct drm_device *dev,
136 struct drm_file *file_priv,
137 struct drm_panfrost_submit *args,
138 struct panfrost_job *job)
140 struct panfrost_file_priv *priv = file_priv->driver_priv;
141 struct panfrost_gem_object *bo;
145 job->bo_count = args->bo_handle_count;
150 ret = drm_gem_objects_lookup(file_priv,
151 (void __user *)(uintptr_t)args->bo_handles,
152 job->bo_count, &job->bos);
156 job->mappings = kvmalloc_array(job->bo_count,
157 sizeof(struct panfrost_gem_mapping *),
158 GFP_KERNEL | __GFP_ZERO);
162 for (i = 0; i < job->bo_count; i++) {
163 struct panfrost_gem_mapping *mapping;
165 bo = to_panfrost_bo(job->bos[i]);
166 mapping = panfrost_gem_mapping_get(bo, priv);
172 atomic_inc(&bo->gpu_usecount);
173 job->mappings[i] = mapping;
180 * panfrost_copy_in_sync() - Sets up job->deps with the sync objects
181 * referenced by the job.
183 * @file_priv: DRM file for this fd
185 * @job: job being set up
187 * Resolve syncobjs from userspace to fences and attach them to job.
189 * Note that this function doesn't need to unreference the fences on
190 * failure, because that will happen at panfrost_job_cleanup() time.
193 panfrost_copy_in_sync(struct drm_device *dev,
194 struct drm_file *file_priv,
195 struct drm_panfrost_submit *args,
196 struct panfrost_job *job)
200 int i, in_fence_count;
202 in_fence_count = args->in_sync_count;
207 handles = kvmalloc_array(in_fence_count, sizeof(u32), GFP_KERNEL);
210 DRM_DEBUG("Failed to allocate incoming syncobj handles\n");
214 if (copy_from_user(handles,
215 (void __user *)(uintptr_t)args->in_syncs,
216 in_fence_count * sizeof(u32))) {
218 DRM_DEBUG("Failed to copy in syncobj handles\n");
222 for (i = 0; i < in_fence_count; i++) {
223 struct dma_fence *fence;
225 ret = drm_syncobj_find_fence(file_priv, handles[i], 0, 0,
230 ret = drm_sched_job_add_dependency(&job->base, fence);
241 static int panfrost_ioctl_submit(struct drm_device *dev, void *data,
242 struct drm_file *file)
244 struct panfrost_device *pfdev = dev->dev_private;
245 struct panfrost_file_priv *file_priv = file->driver_priv;
246 struct drm_panfrost_submit *args = data;
247 struct drm_syncobj *sync_out = NULL;
248 struct panfrost_job *job;
254 if (args->requirements && args->requirements != PANFROST_JD_REQ_FS)
257 if (args->out_sync > 0) {
258 sync_out = drm_syncobj_find(file, args->out_sync);
263 job = kzalloc(sizeof(*job), GFP_KERNEL);
266 goto out_put_syncout;
269 kref_init(&job->refcount);
273 job->requirements = args->requirements;
274 job->flush_id = panfrost_gpu_get_latest_flush_id(pfdev);
275 job->mmu = file_priv->mmu;
277 slot = panfrost_job_get_slot(job);
279 ret = drm_sched_job_init(&job->base,
280 &file_priv->sched_entity[slot],
285 ret = panfrost_copy_in_sync(dev, file, args, job);
287 goto out_cleanup_job;
289 ret = panfrost_lookup_bos(dev, file, args, job);
291 goto out_cleanup_job;
293 ret = panfrost_job_push(job);
295 goto out_cleanup_job;
297 /* Update the return sync object for the job */
299 drm_syncobj_replace_fence(sync_out, job->render_done_fence);
303 drm_sched_job_cleanup(&job->base);
305 panfrost_job_put(job);
308 drm_syncobj_put(sync_out);
314 panfrost_ioctl_wait_bo(struct drm_device *dev, void *data,
315 struct drm_file *file_priv)
318 struct drm_panfrost_wait_bo *args = data;
319 struct drm_gem_object *gem_obj;
320 unsigned long timeout = drm_timeout_abs_to_jiffies(args->timeout_ns);
325 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
329 ret = dma_resv_wait_timeout(gem_obj->resv, DMA_RESV_USAGE_READ,
332 ret = timeout ? -ETIMEDOUT : -EBUSY;
334 drm_gem_object_put(gem_obj);
339 static int panfrost_ioctl_mmap_bo(struct drm_device *dev, void *data,
340 struct drm_file *file_priv)
342 struct drm_panfrost_mmap_bo *args = data;
343 struct drm_gem_object *gem_obj;
346 if (args->flags != 0) {
347 DRM_INFO("unknown mmap_bo flags: %d\n", args->flags);
351 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
353 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
357 /* Don't allow mmapping of heap objects as pages are not pinned. */
358 if (to_panfrost_bo(gem_obj)->is_heap) {
363 ret = drm_gem_create_mmap_offset(gem_obj);
365 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
368 drm_gem_object_put(gem_obj);
372 static int panfrost_ioctl_get_bo_offset(struct drm_device *dev, void *data,
373 struct drm_file *file_priv)
375 struct panfrost_file_priv *priv = file_priv->driver_priv;
376 struct drm_panfrost_get_bo_offset *args = data;
377 struct panfrost_gem_mapping *mapping;
378 struct drm_gem_object *gem_obj;
379 struct panfrost_gem_object *bo;
381 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
383 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
386 bo = to_panfrost_bo(gem_obj);
388 mapping = panfrost_gem_mapping_get(bo, priv);
389 drm_gem_object_put(gem_obj);
394 args->offset = mapping->mmnode.start << PAGE_SHIFT;
395 panfrost_gem_mapping_put(mapping);
399 static int panfrost_ioctl_madvise(struct drm_device *dev, void *data,
400 struct drm_file *file_priv)
402 struct panfrost_file_priv *priv = file_priv->driver_priv;
403 struct drm_panfrost_madvise *args = data;
404 struct panfrost_device *pfdev = dev->dev_private;
405 struct drm_gem_object *gem_obj;
406 struct panfrost_gem_object *bo;
409 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
411 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
415 bo = to_panfrost_bo(gem_obj);
417 mutex_lock(&pfdev->shrinker_lock);
418 mutex_lock(&bo->mappings.lock);
419 if (args->madv == PANFROST_MADV_DONTNEED) {
420 struct panfrost_gem_mapping *first;
422 first = list_first_entry(&bo->mappings.list,
423 struct panfrost_gem_mapping,
427 * If we want to mark the BO purgeable, there must be only one
428 * user: the caller FD.
429 * We could do something smarter and mark the BO purgeable only
430 * when all its users have marked it purgeable, but globally
431 * visible/shared BOs are likely to never be marked purgeable
432 * anyway, so let's not bother.
434 if (!list_is_singular(&bo->mappings.list) ||
435 WARN_ON_ONCE(first->mmu != priv->mmu)) {
437 goto out_unlock_mappings;
441 args->retained = drm_gem_shmem_madvise(&bo->base, args->madv);
443 if (args->retained) {
444 if (args->madv == PANFROST_MADV_DONTNEED)
445 list_move_tail(&bo->base.madv_list,
446 &pfdev->shrinker_list);
447 else if (args->madv == PANFROST_MADV_WILLNEED)
448 list_del_init(&bo->base.madv_list);
452 mutex_unlock(&bo->mappings.lock);
453 mutex_unlock(&pfdev->shrinker_lock);
455 drm_gem_object_put(gem_obj);
459 int panfrost_unstable_ioctl_check(void)
461 if (!unstable_ioctls)
468 panfrost_open(struct drm_device *dev, struct drm_file *file)
471 struct panfrost_device *pfdev = dev->dev_private;
472 struct panfrost_file_priv *panfrost_priv;
474 panfrost_priv = kzalloc(sizeof(*panfrost_priv), GFP_KERNEL);
478 panfrost_priv->pfdev = pfdev;
479 file->driver_priv = panfrost_priv;
481 panfrost_priv->mmu = panfrost_mmu_ctx_create(pfdev);
482 if (IS_ERR(panfrost_priv->mmu)) {
483 ret = PTR_ERR(panfrost_priv->mmu);
487 ret = panfrost_job_open(panfrost_priv);
494 panfrost_mmu_ctx_put(panfrost_priv->mmu);
496 kfree(panfrost_priv);
501 panfrost_postclose(struct drm_device *dev, struct drm_file *file)
503 struct panfrost_file_priv *panfrost_priv = file->driver_priv;
505 panfrost_perfcnt_close(file);
506 panfrost_job_close(panfrost_priv);
508 panfrost_mmu_ctx_put(panfrost_priv->mmu);
509 kfree(panfrost_priv);
512 static const struct drm_ioctl_desc panfrost_drm_driver_ioctls[] = {
513 #define PANFROST_IOCTL(n, func, flags) \
514 DRM_IOCTL_DEF_DRV(PANFROST_##n, panfrost_ioctl_##func, flags)
516 PANFROST_IOCTL(SUBMIT, submit, DRM_RENDER_ALLOW),
517 PANFROST_IOCTL(WAIT_BO, wait_bo, DRM_RENDER_ALLOW),
518 PANFROST_IOCTL(CREATE_BO, create_bo, DRM_RENDER_ALLOW),
519 PANFROST_IOCTL(MMAP_BO, mmap_bo, DRM_RENDER_ALLOW),
520 PANFROST_IOCTL(GET_PARAM, get_param, DRM_RENDER_ALLOW),
521 PANFROST_IOCTL(GET_BO_OFFSET, get_bo_offset, DRM_RENDER_ALLOW),
522 PANFROST_IOCTL(PERFCNT_ENABLE, perfcnt_enable, DRM_RENDER_ALLOW),
523 PANFROST_IOCTL(PERFCNT_DUMP, perfcnt_dump, DRM_RENDER_ALLOW),
524 PANFROST_IOCTL(MADVISE, madvise, DRM_RENDER_ALLOW),
527 DEFINE_DRM_GEM_FOPS(panfrost_drm_driver_fops);
530 * Panfrost driver version:
531 * - 1.0 - initial interface
532 * - 1.1 - adds HEAP and NOEXEC flags for CREATE_BO
533 * - 1.2 - adds AFBC_FEATURES query
535 static const struct drm_driver panfrost_drm_driver = {
536 .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
537 .open = panfrost_open,
538 .postclose = panfrost_postclose,
539 .ioctls = panfrost_drm_driver_ioctls,
540 .num_ioctls = ARRAY_SIZE(panfrost_drm_driver_ioctls),
541 .fops = &panfrost_drm_driver_fops,
543 .desc = "panfrost DRM",
548 .gem_create_object = panfrost_gem_create_object,
549 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
550 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
551 .gem_prime_import_sg_table = panfrost_gem_prime_import_sg_table,
552 .gem_prime_mmap = drm_gem_prime_mmap,
555 static int panfrost_probe(struct platform_device *pdev)
557 struct panfrost_device *pfdev;
558 struct drm_device *ddev;
561 pfdev = devm_kzalloc(&pdev->dev, sizeof(*pfdev), GFP_KERNEL);
566 pfdev->dev = &pdev->dev;
568 platform_set_drvdata(pdev, pfdev);
570 pfdev->comp = of_device_get_match_data(&pdev->dev);
574 pfdev->coherent = device_get_dma_attr(&pdev->dev) == DEV_DMA_COHERENT;
576 /* Allocate and initialize the DRM device. */
577 ddev = drm_dev_alloc(&panfrost_drm_driver, &pdev->dev);
579 return PTR_ERR(ddev);
581 ddev->dev_private = pfdev;
584 mutex_init(&pfdev->shrinker_lock);
585 INIT_LIST_HEAD(&pfdev->shrinker_list);
587 err = panfrost_device_init(pfdev);
589 if (err != -EPROBE_DEFER)
590 dev_err(&pdev->dev, "Fatal error during GPU init\n");
594 pm_runtime_set_active(pfdev->dev);
595 pm_runtime_mark_last_busy(pfdev->dev);
596 pm_runtime_enable(pfdev->dev);
597 pm_runtime_set_autosuspend_delay(pfdev->dev, 50); /* ~3 frames */
598 pm_runtime_use_autosuspend(pfdev->dev);
601 * Register the DRM device with the core and the connectors with
604 err = drm_dev_register(ddev, 0);
608 panfrost_gem_shrinker_init(ddev);
613 pm_runtime_disable(pfdev->dev);
614 panfrost_device_fini(pfdev);
615 pm_runtime_set_suspended(pfdev->dev);
621 static int panfrost_remove(struct platform_device *pdev)
623 struct panfrost_device *pfdev = platform_get_drvdata(pdev);
624 struct drm_device *ddev = pfdev->ddev;
626 drm_dev_unregister(ddev);
627 panfrost_gem_shrinker_cleanup(ddev);
629 pm_runtime_get_sync(pfdev->dev);
630 pm_runtime_disable(pfdev->dev);
631 panfrost_device_fini(pfdev);
632 pm_runtime_set_suspended(pfdev->dev);
639 * The OPP core wants the supply names to be NULL terminated, but we need the
640 * correct num_supplies value for regulator core. Hence, we NULL terminate here
641 * and then initialize num_supplies with ARRAY_SIZE - 1.
643 static const char * const default_supplies[] = { "mali", NULL };
644 static const struct panfrost_compatible default_data = {
645 .num_supplies = ARRAY_SIZE(default_supplies) - 1,
646 .supply_names = default_supplies,
647 .num_pm_domains = 1, /* optional */
648 .pm_domain_names = NULL,
651 static const struct panfrost_compatible amlogic_data = {
652 .num_supplies = ARRAY_SIZE(default_supplies) - 1,
653 .supply_names = default_supplies,
654 .vendor_quirk = panfrost_gpu_amlogic_quirk,
657 static const char * const mediatek_mt8183_supplies[] = { "mali", "sram", NULL };
658 static const char * const mediatek_mt8183_pm_domains[] = { "core0", "core1", "core2" };
659 static const struct panfrost_compatible mediatek_mt8183_data = {
660 .num_supplies = ARRAY_SIZE(mediatek_mt8183_supplies) - 1,
661 .supply_names = mediatek_mt8183_supplies,
662 .num_pm_domains = ARRAY_SIZE(mediatek_mt8183_pm_domains),
663 .pm_domain_names = mediatek_mt8183_pm_domains,
666 static const struct of_device_id dt_match[] = {
667 /* Set first to probe before the generic compatibles */
668 { .compatible = "amlogic,meson-gxm-mali",
669 .data = &amlogic_data, },
670 { .compatible = "amlogic,meson-g12a-mali",
671 .data = &amlogic_data, },
672 { .compatible = "arm,mali-t604", .data = &default_data, },
673 { .compatible = "arm,mali-t624", .data = &default_data, },
674 { .compatible = "arm,mali-t628", .data = &default_data, },
675 { .compatible = "arm,mali-t720", .data = &default_data, },
676 { .compatible = "arm,mali-t760", .data = &default_data, },
677 { .compatible = "arm,mali-t820", .data = &default_data, },
678 { .compatible = "arm,mali-t830", .data = &default_data, },
679 { .compatible = "arm,mali-t860", .data = &default_data, },
680 { .compatible = "arm,mali-t880", .data = &default_data, },
681 { .compatible = "arm,mali-bifrost", .data = &default_data, },
682 { .compatible = "arm,mali-valhall-jm", .data = &default_data, },
683 { .compatible = "mediatek,mt8183-mali", .data = &mediatek_mt8183_data },
686 MODULE_DEVICE_TABLE(of, dt_match);
688 static struct platform_driver panfrost_driver = {
689 .probe = panfrost_probe,
690 .remove = panfrost_remove,
693 .pm = pm_ptr(&panfrost_pm_ops),
694 .of_match_table = dt_match,
697 module_platform_driver(panfrost_driver);
699 MODULE_AUTHOR("Panfrost Project Developers");
700 MODULE_DESCRIPTION("Panfrost DRM Driver");
701 MODULE_LICENSE("GPL v2");