1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2018 Etnaviv Project
6 #include <linux/component.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/module.h>
9 #include <linux/of_platform.h>
10 #include <linux/uaccess.h>
12 #include <drm/drm_debugfs.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_of.h>
17 #include <drm/drm_prime.h>
19 #include "etnaviv_cmdbuf.h"
20 #include "etnaviv_drv.h"
21 #include "etnaviv_gpu.h"
22 #include "etnaviv_gem.h"
23 #include "etnaviv_mmu.h"
24 #include "etnaviv_perfmon.h"
25 #include "common.xml.h"
32 static void load_gpu(struct drm_device *dev)
34 struct etnaviv_drm_private *priv = dev->dev_private;
37 for (i = 0; i < ETNA_MAX_PIPES; i++) {
38 struct etnaviv_gpu *g = priv->gpu[i];
43 ret = etnaviv_gpu_init(g);
50 static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
52 struct etnaviv_drm_private *priv = dev->dev_private;
53 struct etnaviv_file_private *ctx;
56 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
60 ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
61 xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
65 ctx->mmu = etnaviv_iommu_context_init(priv->mmu_global,
66 priv->cmdbuf_suballoc);
72 for (i = 0; i < ETNA_MAX_PIPES; i++) {
73 struct etnaviv_gpu *gpu = priv->gpu[i];
74 struct drm_gpu_scheduler *sched;
78 drm_sched_entity_init(&ctx->sched_entity[i],
79 DRM_SCHED_PRIORITY_NORMAL, &sched,
84 file->driver_priv = ctx;
93 static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
95 struct etnaviv_drm_private *priv = dev->dev_private;
96 struct etnaviv_file_private *ctx = file->driver_priv;
99 for (i = 0; i < ETNA_MAX_PIPES; i++) {
100 struct etnaviv_gpu *gpu = priv->gpu[i];
103 drm_sched_entity_destroy(&ctx->sched_entity[i]);
106 etnaviv_iommu_context_put(ctx->mmu);
108 xa_erase(&priv->active_contexts, ctx->id);
117 #ifdef CONFIG_DEBUG_FS
118 static int etnaviv_gem_show(struct drm_device *dev, struct seq_file *m)
120 struct etnaviv_drm_private *priv = dev->dev_private;
122 etnaviv_gem_describe_objects(priv, m);
127 static int etnaviv_mm_show(struct drm_device *dev, struct seq_file *m)
129 struct drm_printer p = drm_seq_file_printer(m);
131 read_lock(&dev->vma_offset_manager->vm_lock);
132 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
133 read_unlock(&dev->vma_offset_manager->vm_lock);
138 static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)
140 struct drm_printer p = drm_seq_file_printer(m);
141 struct etnaviv_iommu_context *mmu_context;
143 seq_printf(m, "Active Objects (%s):\n", dev_name(gpu->dev));
146 * Lock the GPU to avoid a MMU context switch just now and elevate
147 * the refcount of the current context to avoid it disappearing from
150 mutex_lock(&gpu->lock);
151 mmu_context = gpu->mmu_context;
153 etnaviv_iommu_context_get(mmu_context);
154 mutex_unlock(&gpu->lock);
159 mutex_lock(&mmu_context->lock);
160 drm_mm_print(&mmu_context->mm, &p);
161 mutex_unlock(&mmu_context->lock);
163 etnaviv_iommu_context_put(mmu_context);
168 static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu, struct seq_file *m)
170 struct etnaviv_cmdbuf *buf = &gpu->buffer;
171 u32 size = buf->size;
172 u32 *ptr = buf->vaddr;
175 seq_printf(m, "virt %p - phys 0x%llx - free 0x%08x\n",
176 buf->vaddr, (u64)etnaviv_cmdbuf_get_pa(buf),
177 size - buf->user_size);
179 for (i = 0; i < size / 4; i++) {
183 seq_printf(m, "\t0x%p: ", ptr + i);
184 seq_printf(m, "%08x ", *(ptr + i));
189 static int etnaviv_ring_show(struct etnaviv_gpu *gpu, struct seq_file *m)
191 seq_printf(m, "Ring Buffer (%s): ", dev_name(gpu->dev));
193 mutex_lock(&gpu->lock);
194 etnaviv_buffer_dump(gpu, m);
195 mutex_unlock(&gpu->lock);
200 static int show_unlocked(struct seq_file *m, void *arg)
202 struct drm_info_node *node = (struct drm_info_node *) m->private;
203 struct drm_device *dev = node->minor->dev;
204 int (*show)(struct drm_device *dev, struct seq_file *m) =
205 node->info_ent->data;
210 static int show_each_gpu(struct seq_file *m, void *arg)
212 struct drm_info_node *node = (struct drm_info_node *) m->private;
213 struct drm_device *dev = node->minor->dev;
214 struct etnaviv_drm_private *priv = dev->dev_private;
215 struct etnaviv_gpu *gpu;
216 int (*show)(struct etnaviv_gpu *gpu, struct seq_file *m) =
217 node->info_ent->data;
221 for (i = 0; i < ETNA_MAX_PIPES; i++) {
234 static struct drm_info_list etnaviv_debugfs_list[] = {
235 {"gpu", show_each_gpu, 0, etnaviv_gpu_debugfs},
236 {"gem", show_unlocked, 0, etnaviv_gem_show},
237 { "mm", show_unlocked, 0, etnaviv_mm_show },
238 {"mmu", show_each_gpu, 0, etnaviv_mmu_show},
239 {"ring", show_each_gpu, 0, etnaviv_ring_show},
242 static void etnaviv_debugfs_init(struct drm_minor *minor)
244 drm_debugfs_create_files(etnaviv_debugfs_list,
245 ARRAY_SIZE(etnaviv_debugfs_list),
246 minor->debugfs_root, minor);
254 static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
255 struct drm_file *file)
257 struct etnaviv_drm_private *priv = dev->dev_private;
258 struct drm_etnaviv_param *args = data;
259 struct etnaviv_gpu *gpu;
261 if (args->pipe >= ETNA_MAX_PIPES)
264 gpu = priv->gpu[args->pipe];
268 return etnaviv_gpu_get_param(gpu, args->param, &args->value);
271 static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
272 struct drm_file *file)
274 struct drm_etnaviv_gem_new *args = data;
276 if (args->flags & ~(ETNA_BO_CACHED | ETNA_BO_WC | ETNA_BO_UNCACHED |
280 return etnaviv_gem_new_handle(dev, file, args->size,
281 args->flags, &args->handle);
284 static int etnaviv_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
285 struct drm_file *file)
287 struct drm_etnaviv_gem_cpu_prep *args = data;
288 struct drm_gem_object *obj;
291 if (args->op & ~(ETNA_PREP_READ | ETNA_PREP_WRITE | ETNA_PREP_NOSYNC))
294 obj = drm_gem_object_lookup(file, args->handle);
298 ret = etnaviv_gem_cpu_prep(obj, args->op, &args->timeout);
300 drm_gem_object_put(obj);
305 static int etnaviv_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
306 struct drm_file *file)
308 struct drm_etnaviv_gem_cpu_fini *args = data;
309 struct drm_gem_object *obj;
315 obj = drm_gem_object_lookup(file, args->handle);
319 ret = etnaviv_gem_cpu_fini(obj);
321 drm_gem_object_put(obj);
326 static int etnaviv_ioctl_gem_info(struct drm_device *dev, void *data,
327 struct drm_file *file)
329 struct drm_etnaviv_gem_info *args = data;
330 struct drm_gem_object *obj;
336 obj = drm_gem_object_lookup(file, args->handle);
340 ret = etnaviv_gem_mmap_offset(obj, &args->offset);
341 drm_gem_object_put(obj);
346 static int etnaviv_ioctl_wait_fence(struct drm_device *dev, void *data,
347 struct drm_file *file)
349 struct drm_etnaviv_wait_fence *args = data;
350 struct etnaviv_drm_private *priv = dev->dev_private;
351 struct drm_etnaviv_timespec *timeout = &args->timeout;
352 struct etnaviv_gpu *gpu;
354 if (args->flags & ~(ETNA_WAIT_NONBLOCK))
357 if (args->pipe >= ETNA_MAX_PIPES)
360 gpu = priv->gpu[args->pipe];
364 if (args->flags & ETNA_WAIT_NONBLOCK)
367 return etnaviv_gpu_wait_fence_interruptible(gpu, args->fence,
371 static int etnaviv_ioctl_gem_userptr(struct drm_device *dev, void *data,
372 struct drm_file *file)
374 struct drm_etnaviv_gem_userptr *args = data;
376 if (args->flags & ~(ETNA_USERPTR_READ|ETNA_USERPTR_WRITE) ||
380 if (offset_in_page(args->user_ptr | args->user_size) ||
381 (uintptr_t)args->user_ptr != args->user_ptr ||
382 (u32)args->user_size != args->user_size ||
383 args->user_ptr & ~PAGE_MASK)
386 if (!access_ok((void __user *)(unsigned long)args->user_ptr,
390 return etnaviv_gem_new_userptr(dev, file, args->user_ptr,
391 args->user_size, args->flags,
395 static int etnaviv_ioctl_gem_wait(struct drm_device *dev, void *data,
396 struct drm_file *file)
398 struct etnaviv_drm_private *priv = dev->dev_private;
399 struct drm_etnaviv_gem_wait *args = data;
400 struct drm_etnaviv_timespec *timeout = &args->timeout;
401 struct drm_gem_object *obj;
402 struct etnaviv_gpu *gpu;
405 if (args->flags & ~(ETNA_WAIT_NONBLOCK))
408 if (args->pipe >= ETNA_MAX_PIPES)
411 gpu = priv->gpu[args->pipe];
415 obj = drm_gem_object_lookup(file, args->handle);
419 if (args->flags & ETNA_WAIT_NONBLOCK)
422 ret = etnaviv_gem_wait_bo(gpu, obj, timeout);
424 drm_gem_object_put(obj);
429 static int etnaviv_ioctl_pm_query_dom(struct drm_device *dev, void *data,
430 struct drm_file *file)
432 struct etnaviv_drm_private *priv = dev->dev_private;
433 struct drm_etnaviv_pm_domain *args = data;
434 struct etnaviv_gpu *gpu;
436 if (args->pipe >= ETNA_MAX_PIPES)
439 gpu = priv->gpu[args->pipe];
443 return etnaviv_pm_query_dom(gpu, args);
446 static int etnaviv_ioctl_pm_query_sig(struct drm_device *dev, void *data,
447 struct drm_file *file)
449 struct etnaviv_drm_private *priv = dev->dev_private;
450 struct drm_etnaviv_pm_signal *args = data;
451 struct etnaviv_gpu *gpu;
453 if (args->pipe >= ETNA_MAX_PIPES)
456 gpu = priv->gpu[args->pipe];
460 return etnaviv_pm_query_sig(gpu, args);
463 static const struct drm_ioctl_desc etnaviv_ioctls[] = {
464 #define ETNA_IOCTL(n, func, flags) \
465 DRM_IOCTL_DEF_DRV(ETNAVIV_##n, etnaviv_ioctl_##func, flags)
466 ETNA_IOCTL(GET_PARAM, get_param, DRM_RENDER_ALLOW),
467 ETNA_IOCTL(GEM_NEW, gem_new, DRM_RENDER_ALLOW),
468 ETNA_IOCTL(GEM_INFO, gem_info, DRM_RENDER_ALLOW),
469 ETNA_IOCTL(GEM_CPU_PREP, gem_cpu_prep, DRM_RENDER_ALLOW),
470 ETNA_IOCTL(GEM_CPU_FINI, gem_cpu_fini, DRM_RENDER_ALLOW),
471 ETNA_IOCTL(GEM_SUBMIT, gem_submit, DRM_RENDER_ALLOW),
472 ETNA_IOCTL(WAIT_FENCE, wait_fence, DRM_RENDER_ALLOW),
473 ETNA_IOCTL(GEM_USERPTR, gem_userptr, DRM_RENDER_ALLOW),
474 ETNA_IOCTL(GEM_WAIT, gem_wait, DRM_RENDER_ALLOW),
475 ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
476 ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
479 static void etnaviv_fop_show_fdinfo(struct seq_file *m, struct file *f)
481 struct drm_file *file = f->private_data;
482 struct drm_device *dev = file->minor->dev;
483 struct etnaviv_drm_private *priv = dev->dev_private;
484 struct etnaviv_file_private *ctx = file->driver_priv;
487 * For a description of the text output format used here, see
488 * Documentation/gpu/drm-usage-stats.rst.
490 seq_printf(m, "drm-driver:\t%s\n", dev->driver->name);
491 seq_printf(m, "drm-client-id:\t%u\n", ctx->id);
493 for (int i = 0; i < ETNA_MAX_PIPES; i++) {
494 struct etnaviv_gpu *gpu = priv->gpu[i];
495 char engine[10] = "UNK";
501 if (gpu->identity.features & chipFeatures_PIPE_2D)
502 cur = snprintf(engine, sizeof(engine), "2D");
503 if (gpu->identity.features & chipFeatures_PIPE_3D)
504 cur = snprintf(engine + cur, sizeof(engine) - cur,
505 "%s3D", cur ? "/" : "");
506 if (gpu->identity.nn_core_count > 0)
507 cur = snprintf(engine + cur, sizeof(engine) - cur,
508 "%sNN", cur ? "/" : "");
510 seq_printf(m, "drm-engine-%s:\t%llu ns\n", engine,
511 ctx->sched_entity[i].elapsed_ns);
515 static const struct file_operations fops = {
516 .owner = THIS_MODULE,
518 .show_fdinfo = etnaviv_fop_show_fdinfo,
521 static const struct drm_driver etnaviv_drm_driver = {
522 .driver_features = DRIVER_GEM | DRIVER_RENDER,
523 .open = etnaviv_open,
524 .postclose = etnaviv_postclose,
525 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
526 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
527 .gem_prime_import_sg_table = etnaviv_gem_prime_import_sg_table,
528 .gem_prime_mmap = drm_gem_prime_mmap,
529 #ifdef CONFIG_DEBUG_FS
530 .debugfs_init = etnaviv_debugfs_init,
532 .ioctls = etnaviv_ioctls,
533 .num_ioctls = DRM_ETNAVIV_NUM_IOCTLS,
536 .desc = "etnaviv DRM",
545 static int etnaviv_bind(struct device *dev)
547 struct etnaviv_drm_private *priv;
548 struct drm_device *drm;
551 drm = drm_dev_alloc(&etnaviv_drm_driver, dev);
555 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
557 dev_err(dev, "failed to allocate private data\n");
561 drm->dev_private = priv;
563 dma_set_max_seg_size(dev, SZ_2G);
565 xa_init_flags(&priv->active_contexts, XA_FLAGS_ALLOC);
567 mutex_init(&priv->gem_lock);
568 INIT_LIST_HEAD(&priv->gem_list);
570 priv->shm_gfp_mask = GFP_HIGHUSER | __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
572 priv->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(drm->dev);
573 if (IS_ERR(priv->cmdbuf_suballoc)) {
574 dev_err(drm->dev, "Failed to create cmdbuf suballocator\n");
575 ret = PTR_ERR(priv->cmdbuf_suballoc);
579 dev_set_drvdata(dev, drm);
581 ret = component_bind_all(dev, drm);
583 goto out_destroy_suballoc;
587 ret = drm_dev_register(drm, 0);
594 component_unbind_all(dev, drm);
595 out_destroy_suballoc:
596 etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
605 static void etnaviv_unbind(struct device *dev)
607 struct drm_device *drm = dev_get_drvdata(dev);
608 struct etnaviv_drm_private *priv = drm->dev_private;
610 drm_dev_unregister(drm);
612 component_unbind_all(dev, drm);
614 etnaviv_cmdbuf_suballoc_destroy(priv->cmdbuf_suballoc);
616 xa_destroy(&priv->active_contexts);
618 drm->dev_private = NULL;
624 static const struct component_master_ops etnaviv_master_ops = {
625 .bind = etnaviv_bind,
626 .unbind = etnaviv_unbind,
629 static int etnaviv_pdev_probe(struct platform_device *pdev)
631 struct device *dev = &pdev->dev;
632 struct device_node *first_node = NULL;
633 struct component_match *match = NULL;
635 if (!dev->platform_data) {
636 struct device_node *core_node;
638 for_each_compatible_node(core_node, NULL, "vivante,gc") {
639 if (!of_device_is_available(core_node))
643 first_node = core_node;
645 drm_of_component_match_add(&pdev->dev, &match,
646 component_compare_of, core_node);
649 char **names = dev->platform_data;
652 for (i = 0; names[i]; i++)
653 component_match_add(dev, &match, component_compare_dev_name, names[i]);
657 * PTA and MTLB can have 40 bit base addresses, but
658 * unfortunately, an entry in the MTLB can only point to a
659 * 32 bit base address of a STLB. Moreover, to initialize the
660 * MMU we need a command buffer with a 32 bit address because
661 * without an MMU there is only an indentity mapping between
662 * the internal 32 bit addresses and the bus addresses.
664 * To make things easy, we set the dma_coherent_mask to 32
665 * bit to make sure we are allocating the command buffers and
666 * TLBs in the lower 4 GiB address space.
668 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(40)) ||
669 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32))) {
670 dev_dbg(&pdev->dev, "No suitable DMA available\n");
675 * Apply the same DMA configuration to the virtual etnaviv
676 * device as the GPU we found. This assumes that all Vivante
677 * GPUs in the system share the same DMA constraints.
680 of_dma_configure(&pdev->dev, first_node, true);
682 return component_master_add_with_match(dev, &etnaviv_master_ops, match);
685 static int etnaviv_pdev_remove(struct platform_device *pdev)
687 component_master_del(&pdev->dev, &etnaviv_master_ops);
692 static struct platform_driver etnaviv_platform_driver = {
693 .probe = etnaviv_pdev_probe,
694 .remove = etnaviv_pdev_remove,
700 static struct platform_device *etnaviv_drm;
702 static int __init etnaviv_init(void)
704 struct platform_device *pdev;
706 struct device_node *np;
708 etnaviv_validate_init();
710 ret = platform_driver_register(&etnaviv_gpu_driver);
714 ret = platform_driver_register(&etnaviv_platform_driver);
716 goto unregister_gpu_driver;
719 * If the DT contains at least one available GPU device, instantiate
720 * the DRM platform device.
722 for_each_compatible_node(np, NULL, "vivante,gc") {
723 if (!of_device_is_available(np))
726 pdev = platform_device_alloc("etnaviv", PLATFORM_DEVID_NONE);
730 goto unregister_platform_driver;
733 ret = platform_device_add(pdev);
735 platform_device_put(pdev);
737 goto unregister_platform_driver;
747 unregister_platform_driver:
748 platform_driver_unregister(&etnaviv_platform_driver);
749 unregister_gpu_driver:
750 platform_driver_unregister(&etnaviv_gpu_driver);
753 module_init(etnaviv_init);
755 static void __exit etnaviv_exit(void)
757 platform_device_unregister(etnaviv_drm);
758 platform_driver_unregister(&etnaviv_platform_driver);
759 platform_driver_unregister(&etnaviv_gpu_driver);
761 module_exit(etnaviv_exit);
763 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
764 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
765 MODULE_AUTHOR("Lucas Stach <l.stach@pengutronix.de>");
766 MODULE_DESCRIPTION("etnaviv DRM Driver");
767 MODULE_LICENSE("GPL v2");
768 MODULE_ALIAS("platform:etnaviv");