1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016-2018, 2020-2021 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #include <linux/dma-mapping.h>
9 #include <linux/fault-inject.h>
10 #include <linux/kthread.h>
11 #include <linux/of_address.h>
12 #include <linux/sched/mm.h>
13 #include <linux/uaccess.h>
14 #include <uapi/linux/sched/types.h>
16 #include <drm/drm_aperture.h>
17 #include <drm/drm_bridge.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_file.h>
20 #include <drm/drm_ioctl.h>
21 #include <drm/drm_prime.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_vblank.h>
25 #include "disp/msm_disp_snapshot.h"
27 #include "msm_debugfs.h"
28 #include "msm_fence.h"
33 #include "adreno/adreno_gpu.h"
37 * - 1.0.0 - initial interface
38 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
39 * - 1.2.0 - adds explicit fence support for submit ioctl
40 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
41 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
43 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
44 * GEM object's debug name
45 * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl
46 * - 1.6.0 - Syncobj support
47 * - 1.7.0 - Add MSM_PARAM_SUSPENDS to access suspend count
48 * - 1.8.0 - Add MSM_BO_CACHED_COHERENT for supported GPUs (a6xx)
49 * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN
50 * - 1.10.0 - Add MSM_SUBMIT_BO_NO_IMPLICIT
51 * - 1.11.0 - Add wait boost (MSM_WAIT_FENCE_BOOST, MSM_PREP_BOOST)
53 #define MSM_VERSION_MAJOR 1
54 #define MSM_VERSION_MINOR 10
55 #define MSM_VERSION_PATCHLEVEL 0
57 static void msm_deinit_vram(struct drm_device *ddev);
59 static const struct drm_mode_config_funcs mode_config_funcs = {
60 .fb_create = msm_framebuffer_create,
61 .atomic_check = msm_atomic_check,
62 .atomic_commit = drm_atomic_helper_commit,
65 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
66 .atomic_commit_tail = msm_atomic_commit_tail,
69 static char *vram = "16m";
70 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
71 module_param(vram, charp, 0);
74 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
75 module_param(dumpstate, bool, 0600);
77 static bool modeset = true;
78 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
79 module_param(modeset, bool, 0600);
81 #ifdef CONFIG_FAULT_INJECTION
82 DECLARE_FAULT_ATTR(fail_gem_alloc);
83 DECLARE_FAULT_ATTR(fail_gem_iova);
86 static irqreturn_t msm_irq(int irq, void *arg)
88 struct drm_device *dev = arg;
89 struct msm_drm_private *priv = dev->dev_private;
90 struct msm_kms *kms = priv->kms;
94 return kms->funcs->irq(kms);
97 static void msm_irq_preinstall(struct drm_device *dev)
99 struct msm_drm_private *priv = dev->dev_private;
100 struct msm_kms *kms = priv->kms;
104 kms->funcs->irq_preinstall(kms);
107 static int msm_irq_postinstall(struct drm_device *dev)
109 struct msm_drm_private *priv = dev->dev_private;
110 struct msm_kms *kms = priv->kms;
114 if (kms->funcs->irq_postinstall)
115 return kms->funcs->irq_postinstall(kms);
120 static int msm_irq_install(struct drm_device *dev, unsigned int irq)
122 struct msm_drm_private *priv = dev->dev_private;
123 struct msm_kms *kms = priv->kms;
126 if (irq == IRQ_NOTCONNECTED)
129 msm_irq_preinstall(dev);
131 ret = request_irq(irq, msm_irq, 0, dev->driver->name, dev);
135 kms->irq_requested = true;
137 ret = msm_irq_postinstall(dev);
146 static void msm_irq_uninstall(struct drm_device *dev)
148 struct msm_drm_private *priv = dev->dev_private;
149 struct msm_kms *kms = priv->kms;
151 kms->funcs->irq_uninstall(kms);
152 if (kms->irq_requested)
153 free_irq(kms->irq, dev);
156 struct msm_vblank_work {
157 struct work_struct work;
158 struct drm_crtc *crtc;
160 struct msm_drm_private *priv;
163 static void vblank_ctrl_worker(struct work_struct *work)
165 struct msm_vblank_work *vbl_work = container_of(work,
166 struct msm_vblank_work, work);
167 struct msm_drm_private *priv = vbl_work->priv;
168 struct msm_kms *kms = priv->kms;
170 if (vbl_work->enable)
171 kms->funcs->enable_vblank(kms, vbl_work->crtc);
173 kms->funcs->disable_vblank(kms, vbl_work->crtc);
178 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
179 struct drm_crtc *crtc, bool enable)
181 struct msm_vblank_work *vbl_work;
183 vbl_work = kzalloc(sizeof(*vbl_work), GFP_ATOMIC);
187 INIT_WORK(&vbl_work->work, vblank_ctrl_worker);
189 vbl_work->crtc = crtc;
190 vbl_work->enable = enable;
191 vbl_work->priv = priv;
193 queue_work(priv->wq, &vbl_work->work);
198 static int msm_drm_uninit(struct device *dev)
200 struct platform_device *pdev = to_platform_device(dev);
201 struct msm_drm_private *priv = platform_get_drvdata(pdev);
202 struct drm_device *ddev = priv->dev;
203 struct msm_kms *kms = priv->kms;
207 * Shutdown the hw if we're far enough along where things might be on.
208 * If we run this too early, we'll end up panicking in any variety of
209 * places. Since we don't register the drm device until late in
210 * msm_drm_init, drm_dev->registered is used as an indicator that the
211 * shutdown will be successful.
213 if (ddev->registered) {
214 drm_dev_unregister(ddev);
215 drm_atomic_helper_shutdown(ddev);
218 /* We must cancel and cleanup any pending vblank enable/disable
219 * work before msm_irq_uninstall() to avoid work re-enabling an
220 * irq after uninstall has disabled it.
223 flush_workqueue(priv->wq);
225 /* clean up event worker threads */
226 for (i = 0; i < priv->num_crtcs; i++) {
227 if (priv->event_thread[i].worker)
228 kthread_destroy_worker(priv->event_thread[i].worker);
231 msm_gem_shrinker_cleanup(ddev);
233 drm_kms_helper_poll_fini(ddev);
235 msm_perf_debugfs_cleanup(priv);
236 msm_rd_debugfs_cleanup(priv);
239 msm_disp_snapshot_destroy(ddev);
241 drm_mode_config_cleanup(ddev);
243 for (i = 0; i < priv->num_bridges; i++)
244 drm_bridge_remove(priv->bridges[i]);
245 priv->num_bridges = 0;
248 pm_runtime_get_sync(dev);
249 msm_irq_uninstall(ddev);
250 pm_runtime_put_sync(dev);
253 if (kms && kms->funcs)
254 kms->funcs->destroy(kms);
256 msm_deinit_vram(ddev);
258 component_unbind_all(dev, ddev);
260 ddev->dev_private = NULL;
263 destroy_workqueue(priv->wq);
268 struct msm_gem_address_space *msm_kms_init_aspace(struct drm_device *dev)
270 struct msm_gem_address_space *aspace;
272 struct device *mdp_dev = dev->dev;
273 struct device *mdss_dev = mdp_dev->parent;
274 struct device *iommu_dev;
277 * IOMMUs can be a part of MDSS device tree binding, or the
280 if (device_iommu_mapped(mdp_dev))
283 iommu_dev = mdss_dev;
285 mmu = msm_iommu_new(iommu_dev, 0);
287 return ERR_CAST(mmu);
290 drm_info(dev, "no IOMMU, fallback to phys contig buffers for scanout\n");
294 aspace = msm_gem_address_space_create(mmu, "mdp_kms",
295 0x1000, 0x100000000 - 0x1000);
296 if (IS_ERR(aspace)) {
297 dev_err(mdp_dev, "aspace create, error %pe\n", aspace);
298 mmu->funcs->destroy(mmu);
304 bool msm_use_mmu(struct drm_device *dev)
306 struct msm_drm_private *priv = dev->dev_private;
309 * a2xx comes with its own MMU
310 * On other platforms IOMMU can be declared specified either for the
311 * MDP/DPU device or for its parent, MDSS device.
313 return priv->is_a2xx ||
314 device_iommu_mapped(dev->dev) ||
315 device_iommu_mapped(dev->dev->parent);
318 static int msm_init_vram(struct drm_device *dev)
320 struct msm_drm_private *priv = dev->dev_private;
321 struct device_node *node;
322 unsigned long size = 0;
325 /* In the device-tree world, we could have a 'memory-region'
326 * phandle, which gives us a link to our "vram". Allocating
327 * is all nicely abstracted behind the dma api, but we need
328 * to know the entire size to allocate it all in one go. There
330 * 1) device with no IOMMU, in which case we need exclusive
331 * access to a VRAM carveout big enough for all gpu
333 * 2) device with IOMMU, but where the bootloader puts up
334 * a splash screen. In this case, the VRAM carveout
335 * need only be large enough for fbdev fb. But we need
336 * exclusive access to the buffer to avoid the kernel
337 * using those pages for other purposes (which appears
338 * as corruption on screen before we have a chance to
339 * load and do initial modeset)
342 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
345 ret = of_address_to_resource(node, 0, &r);
349 size = r.end - r.start + 1;
350 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
352 /* if we have no IOMMU, then we need to use carveout allocator.
353 * Grab the entire DMA chunk carved out in early startup in
356 } else if (!msm_use_mmu(dev)) {
357 DRM_INFO("using %s VRAM carveout\n", vram);
358 size = memparse(vram, NULL);
362 unsigned long attrs = 0;
365 priv->vram.size = size;
367 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
368 spin_lock_init(&priv->vram.lock);
370 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
371 attrs |= DMA_ATTR_WRITE_COMBINE;
373 /* note that for no-kernel-mapping, the vaddr returned
374 * is bogus, but non-null if allocation succeeded:
376 p = dma_alloc_attrs(dev->dev, size,
377 &priv->vram.paddr, GFP_KERNEL, attrs);
379 DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
380 priv->vram.paddr = 0;
384 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
385 (uint32_t)priv->vram.paddr,
386 (uint32_t)(priv->vram.paddr + size));
392 static void msm_deinit_vram(struct drm_device *ddev)
394 struct msm_drm_private *priv = ddev->dev_private;
395 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
397 if (!priv->vram.paddr)
400 drm_mm_takedown(&priv->vram.mm);
401 dma_free_attrs(ddev->dev, priv->vram.size, NULL, priv->vram.paddr,
405 static int msm_drm_init(struct device *dev, const struct drm_driver *drv)
407 struct msm_drm_private *priv = dev_get_drvdata(dev);
408 struct drm_device *ddev;
410 struct drm_crtc *crtc;
413 if (drm_firmware_drivers_only())
416 ddev = drm_dev_alloc(drv, dev);
418 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
419 return PTR_ERR(ddev);
421 ddev->dev_private = priv;
424 priv->wq = alloc_ordered_workqueue("msm", 0);
430 INIT_LIST_HEAD(&priv->objects);
431 mutex_init(&priv->obj_lock);
434 * Initialize the LRUs:
436 mutex_init(&priv->lru.lock);
437 drm_gem_lru_init(&priv->lru.unbacked, &priv->lru.lock);
438 drm_gem_lru_init(&priv->lru.pinned, &priv->lru.lock);
439 drm_gem_lru_init(&priv->lru.willneed, &priv->lru.lock);
440 drm_gem_lru_init(&priv->lru.dontneed, &priv->lru.lock);
442 /* Teach lockdep about lock ordering wrt. shrinker: */
443 fs_reclaim_acquire(GFP_KERNEL);
444 might_lock(&priv->lru.lock);
445 fs_reclaim_release(GFP_KERNEL);
447 drm_mode_config_init(ddev);
449 ret = msm_init_vram(ddev);
451 goto err_cleanup_mode_config;
453 dma_set_max_seg_size(dev, UINT_MAX);
455 /* Bind all our sub-components: */
456 ret = component_bind_all(dev, ddev);
458 goto err_deinit_vram;
460 /* the fw fb could be anywhere in memory */
461 ret = drm_aperture_remove_framebuffers(drv);
465 msm_gem_shrinker_init(ddev);
467 if (priv->kms_init) {
468 ret = priv->kms_init(ddev);
470 DRM_DEV_ERROR(dev, "failed to load kms\n");
476 /* valid only for the dummy headless case, where of_node=NULL */
477 WARN_ON(dev->of_node);
481 /* Enable normalization of plane zpos */
482 ddev->mode_config.normalize_zpos = true;
486 ret = kms->funcs->hw_init(kms);
488 DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
493 drm_helper_move_panel_connectors_to_head(ddev);
495 ddev->mode_config.funcs = &mode_config_funcs;
496 ddev->mode_config.helper_private = &mode_config_helper_funcs;
498 drm_for_each_crtc(crtc, ddev) {
499 struct msm_drm_thread *ev_thread;
501 /* initialize event thread */
502 ev_thread = &priv->event_thread[drm_crtc_index(crtc)];
503 ev_thread->dev = ddev;
504 ev_thread->worker = kthread_create_worker(0, "crtc_event:%d", crtc->base.id);
505 if (IS_ERR(ev_thread->worker)) {
506 ret = PTR_ERR(ev_thread->worker);
507 DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n");
508 ev_thread->worker = NULL;
512 sched_set_fifo(ev_thread->worker->task);
515 ret = drm_vblank_init(ddev, priv->num_crtcs);
517 DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
522 pm_runtime_get_sync(dev);
523 ret = msm_irq_install(ddev, kms->irq);
524 pm_runtime_put_sync(dev);
526 DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
531 ret = drm_dev_register(ddev, 0);
536 ret = msm_disp_snapshot_init(ddev);
538 DRM_DEV_ERROR(dev, "msm_disp_snapshot_init failed ret = %d\n", ret);
540 drm_mode_config_reset(ddev);
542 ret = msm_debugfs_late_init(ddev);
546 drm_kms_helper_poll_init(ddev);
549 msm_fbdev_setup(ddev);
559 msm_deinit_vram(ddev);
560 err_cleanup_mode_config:
561 drm_mode_config_cleanup(ddev);
562 destroy_workqueue(priv->wq);
573 static void load_gpu(struct drm_device *dev)
575 static DEFINE_MUTEX(init_lock);
576 struct msm_drm_private *priv = dev->dev_private;
578 mutex_lock(&init_lock);
581 priv->gpu = adreno_load_gpu(dev);
583 mutex_unlock(&init_lock);
586 static int context_init(struct drm_device *dev, struct drm_file *file)
588 static atomic_t ident = ATOMIC_INIT(0);
589 struct msm_drm_private *priv = dev->dev_private;
590 struct msm_file_private *ctx;
592 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
596 INIT_LIST_HEAD(&ctx->submitqueues);
597 rwlock_init(&ctx->queuelock);
599 kref_init(&ctx->ref);
600 msm_submitqueue_init(dev, ctx);
602 ctx->aspace = msm_gpu_create_private_address_space(priv->gpu, current);
603 file->driver_priv = ctx;
605 ctx->seqno = atomic_inc_return(&ident);
610 static int msm_open(struct drm_device *dev, struct drm_file *file)
612 /* For now, load gpu on open.. to avoid the requirement of having
613 * firmware in the initrd.
617 return context_init(dev, file);
620 static void context_close(struct msm_file_private *ctx)
622 msm_submitqueue_close(ctx);
623 msm_file_private_put(ctx);
626 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
628 struct msm_drm_private *priv = dev->dev_private;
629 struct msm_file_private *ctx = file->driver_priv;
632 * It is not possible to set sysprof param to non-zero if gpu
633 * is not initialized:
636 msm_file_private_set_sysprof(ctx, priv->gpu, 0);
641 int msm_crtc_enable_vblank(struct drm_crtc *crtc)
643 struct drm_device *dev = crtc->dev;
644 struct msm_drm_private *priv = dev->dev_private;
645 struct msm_kms *kms = priv->kms;
648 drm_dbg_vbl(dev, "crtc=%u", crtc->base.id);
649 return vblank_ctrl_queue_work(priv, crtc, true);
652 void msm_crtc_disable_vblank(struct drm_crtc *crtc)
654 struct drm_device *dev = crtc->dev;
655 struct msm_drm_private *priv = dev->dev_private;
656 struct msm_kms *kms = priv->kms;
659 drm_dbg_vbl(dev, "crtc=%u", crtc->base.id);
660 vblank_ctrl_queue_work(priv, crtc, false);
667 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
668 struct drm_file *file)
670 struct msm_drm_private *priv = dev->dev_private;
671 struct drm_msm_param *args = data;
674 /* for now, we just have 3d pipe.. eventually this would need to
675 * be more clever to dispatch to appropriate gpu module:
677 if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0))
685 return gpu->funcs->get_param(gpu, file->driver_priv,
686 args->param, &args->value, &args->len);
689 static int msm_ioctl_set_param(struct drm_device *dev, void *data,
690 struct drm_file *file)
692 struct msm_drm_private *priv = dev->dev_private;
693 struct drm_msm_param *args = data;
696 if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0))
704 return gpu->funcs->set_param(gpu, file->driver_priv,
705 args->param, args->value, args->len);
708 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
709 struct drm_file *file)
711 struct drm_msm_gem_new *args = data;
712 uint32_t flags = args->flags;
714 if (args->flags & ~MSM_BO_FLAGS) {
715 DRM_ERROR("invalid flags: %08x\n", args->flags);
720 * Uncached CPU mappings are deprecated, as of:
722 * 9ef364432db4 ("drm/msm: deprecate MSM_BO_UNCACHED (map as writecombine instead)")
724 * So promote them to WC.
726 if (flags & MSM_BO_UNCACHED) {
727 flags &= ~MSM_BO_CACHED;
731 if (should_fail(&fail_gem_alloc, args->size))
734 return msm_gem_new_handle(dev, file, args->size,
735 args->flags, &args->handle, NULL);
738 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
740 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
743 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
744 struct drm_file *file)
746 struct drm_msm_gem_cpu_prep *args = data;
747 struct drm_gem_object *obj;
748 ktime_t timeout = to_ktime(args->timeout);
751 if (args->op & ~MSM_PREP_FLAGS) {
752 DRM_ERROR("invalid op: %08x\n", args->op);
756 obj = drm_gem_object_lookup(file, args->handle);
760 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
762 drm_gem_object_put(obj);
767 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
768 struct drm_file *file)
770 struct drm_msm_gem_cpu_fini *args = data;
771 struct drm_gem_object *obj;
774 obj = drm_gem_object_lookup(file, args->handle);
778 ret = msm_gem_cpu_fini(obj);
780 drm_gem_object_put(obj);
785 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
786 struct drm_file *file, struct drm_gem_object *obj,
789 struct msm_drm_private *priv = dev->dev_private;
790 struct msm_file_private *ctx = file->driver_priv;
795 if (should_fail(&fail_gem_iova, obj->size))
799 * Don't pin the memory here - just get an address so that userspace can
802 return msm_gem_get_iova(obj, ctx->aspace, iova);
805 static int msm_ioctl_gem_info_set_iova(struct drm_device *dev,
806 struct drm_file *file, struct drm_gem_object *obj,
809 struct msm_drm_private *priv = dev->dev_private;
810 struct msm_file_private *ctx = file->driver_priv;
815 /* Only supported if per-process address space is supported: */
816 if (priv->gpu->aspace == ctx->aspace)
819 if (should_fail(&fail_gem_iova, obj->size))
822 return msm_gem_set_iova(obj, ctx->aspace, iova);
825 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
826 struct drm_file *file)
828 struct drm_msm_gem_info *args = data;
829 struct drm_gem_object *obj;
830 struct msm_gem_object *msm_obj;
836 switch (args->info) {
837 case MSM_INFO_GET_OFFSET:
838 case MSM_INFO_GET_IOVA:
839 case MSM_INFO_SET_IOVA:
840 case MSM_INFO_GET_FLAGS:
841 /* value returned as immediate, not pointer, so len==0: */
845 case MSM_INFO_SET_NAME:
846 case MSM_INFO_GET_NAME:
852 obj = drm_gem_object_lookup(file, args->handle);
856 msm_obj = to_msm_bo(obj);
858 switch (args->info) {
859 case MSM_INFO_GET_OFFSET:
860 args->value = msm_gem_mmap_offset(obj);
862 case MSM_INFO_GET_IOVA:
863 ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value);
865 case MSM_INFO_SET_IOVA:
866 ret = msm_ioctl_gem_info_set_iova(dev, file, obj, args->value);
868 case MSM_INFO_GET_FLAGS:
869 if (obj->import_attach) {
873 /* Hide internal kernel-only flags: */
874 args->value = to_msm_bo(obj)->flags & MSM_BO_FLAGS;
877 case MSM_INFO_SET_NAME:
878 /* length check should leave room for terminating null: */
879 if (args->len >= sizeof(msm_obj->name)) {
883 if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value),
885 msm_obj->name[0] = '\0';
889 msm_obj->name[args->len] = '\0';
890 for (i = 0; i < args->len; i++) {
891 if (!isprint(msm_obj->name[i])) {
892 msm_obj->name[i] = '\0';
897 case MSM_INFO_GET_NAME:
898 if (args->value && (args->len < strlen(msm_obj->name))) {
902 args->len = strlen(msm_obj->name);
904 if (copy_to_user(u64_to_user_ptr(args->value),
905 msm_obj->name, args->len))
911 drm_gem_object_put(obj);
916 static int wait_fence(struct msm_gpu_submitqueue *queue, uint32_t fence_id,
917 ktime_t timeout, uint32_t flags)
919 struct dma_fence *fence;
922 if (fence_after(fence_id, queue->last_fence)) {
923 DRM_ERROR_RATELIMITED("waiting on invalid fence: %u (of %u)\n",
924 fence_id, queue->last_fence);
929 * Map submitqueue scoped "seqno" (which is actually an idr key)
930 * back to underlying dma-fence
932 * The fence is removed from the fence_idr when the submit is
933 * retired, so if the fence is not found it means there is nothing
936 spin_lock(&queue->idr_lock);
937 fence = idr_find(&queue->fence_idr, fence_id);
939 fence = dma_fence_get_rcu(fence);
940 spin_unlock(&queue->idr_lock);
945 if (flags & MSM_WAIT_FENCE_BOOST)
946 dma_fence_set_deadline(fence, ktime_get());
948 ret = dma_fence_wait_timeout(fence, true, timeout_to_jiffies(&timeout));
951 } else if (ret != -ERESTARTSYS) {
955 dma_fence_put(fence);
960 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
961 struct drm_file *file)
963 struct msm_drm_private *priv = dev->dev_private;
964 struct drm_msm_wait_fence *args = data;
965 struct msm_gpu_submitqueue *queue;
968 if (args->flags & ~MSM_WAIT_FENCE_FLAGS) {
969 DRM_ERROR("invalid flags: %08x\n", args->flags);
976 queue = msm_submitqueue_get(file->driver_priv, args->queueid);
980 ret = wait_fence(queue, args->fence, to_ktime(args->timeout), args->flags);
982 msm_submitqueue_put(queue);
987 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
988 struct drm_file *file)
990 struct drm_msm_gem_madvise *args = data;
991 struct drm_gem_object *obj;
994 switch (args->madv) {
995 case MSM_MADV_DONTNEED:
996 case MSM_MADV_WILLNEED:
1002 obj = drm_gem_object_lookup(file, args->handle);
1007 ret = msm_gem_madvise(obj, args->madv);
1009 args->retained = ret;
1013 drm_gem_object_put(obj);
1019 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
1020 struct drm_file *file)
1022 struct drm_msm_submitqueue *args = data;
1024 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
1027 return msm_submitqueue_create(dev, file->driver_priv, args->prio,
1028 args->flags, &args->id);
1031 static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data,
1032 struct drm_file *file)
1034 return msm_submitqueue_query(dev, file->driver_priv, data);
1037 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
1038 struct drm_file *file)
1040 u32 id = *(u32 *) data;
1042 return msm_submitqueue_remove(file->driver_priv, id);
1045 static const struct drm_ioctl_desc msm_ioctls[] = {
1046 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_RENDER_ALLOW),
1047 DRM_IOCTL_DEF_DRV(MSM_SET_PARAM, msm_ioctl_set_param, DRM_RENDER_ALLOW),
1048 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_RENDER_ALLOW),
1049 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_RENDER_ALLOW),
1050 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW),
1051 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW),
1052 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_RENDER_ALLOW),
1053 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_RENDER_ALLOW),
1054 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_RENDER_ALLOW),
1055 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_RENDER_ALLOW),
1056 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW),
1057 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW),
1060 static void msm_show_fdinfo(struct drm_printer *p, struct drm_file *file)
1062 struct drm_device *dev = file->minor->dev;
1063 struct msm_drm_private *priv = dev->dev_private;
1068 msm_gpu_show_fdinfo(priv->gpu, file->driver_priv, p);
1070 drm_show_memory_stats(p, file);
1073 static const struct file_operations fops = {
1074 .owner = THIS_MODULE,
1076 .show_fdinfo = drm_show_fdinfo,
1079 static const struct drm_driver msm_driver = {
1080 .driver_features = DRIVER_GEM |
1086 .postclose = msm_postclose,
1087 .dumb_create = msm_gem_dumb_create,
1088 .dumb_map_offset = msm_gem_dumb_map_offset,
1089 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
1090 #ifdef CONFIG_DEBUG_FS
1091 .debugfs_init = msm_debugfs_init,
1093 .show_fdinfo = msm_show_fdinfo,
1094 .ioctls = msm_ioctls,
1095 .num_ioctls = ARRAY_SIZE(msm_ioctls),
1098 .desc = "MSM Snapdragon DRM",
1100 .major = MSM_VERSION_MAJOR,
1101 .minor = MSM_VERSION_MINOR,
1102 .patchlevel = MSM_VERSION_PATCHLEVEL,
1105 int msm_pm_prepare(struct device *dev)
1107 struct msm_drm_private *priv = dev_get_drvdata(dev);
1108 struct drm_device *ddev = priv ? priv->dev : NULL;
1110 if (!priv || !priv->kms)
1113 return drm_mode_config_helper_suspend(ddev);
1116 void msm_pm_complete(struct device *dev)
1118 struct msm_drm_private *priv = dev_get_drvdata(dev);
1119 struct drm_device *ddev = priv ? priv->dev : NULL;
1121 if (!priv || !priv->kms)
1124 drm_mode_config_helper_resume(ddev);
1127 static const struct dev_pm_ops msm_pm_ops = {
1128 .prepare = msm_pm_prepare,
1129 .complete = msm_pm_complete,
1133 * Componentized driver support:
1137 * Identify what components need to be added by parsing what remote-endpoints
1138 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1139 * is no external component that we need to add since LVDS is within MDP4
1142 static int add_components_mdp(struct device *master_dev,
1143 struct component_match **matchptr)
1145 struct device_node *np = master_dev->of_node;
1146 struct device_node *ep_node;
1148 for_each_endpoint_of_node(np, ep_node) {
1149 struct device_node *intf;
1150 struct of_endpoint ep;
1153 ret = of_graph_parse_endpoint(ep_node, &ep);
1155 DRM_DEV_ERROR(master_dev, "unable to parse port endpoint\n");
1156 of_node_put(ep_node);
1161 * The LCDC/LVDS port on MDP4 is a speacial case where the
1162 * remote-endpoint isn't a component that we need to add
1164 if (of_device_is_compatible(np, "qcom,mdp4") &&
1169 * It's okay if some of the ports don't have a remote endpoint
1170 * specified. It just means that the port isn't connected to
1171 * any external interface.
1173 intf = of_graph_get_remote_port_parent(ep_node);
1177 if (of_device_is_available(intf))
1178 drm_of_component_match_add(master_dev, matchptr,
1179 component_compare_of, intf);
1188 * We don't know what's the best binding to link the gpu with the drm device.
1189 * Fow now, we just hunt for all the possible gpus that we support, and add them
1192 static const struct of_device_id msm_gpu_match[] = {
1193 { .compatible = "qcom,adreno" },
1194 { .compatible = "qcom,adreno-3xx" },
1195 { .compatible = "amd,imageon" },
1196 { .compatible = "qcom,kgsl-3d0" },
1200 static int add_gpu_components(struct device *dev,
1201 struct component_match **matchptr)
1203 struct device_node *np;
1205 np = of_find_matching_node(NULL, msm_gpu_match);
1209 if (of_device_is_available(np))
1210 drm_of_component_match_add(dev, matchptr, component_compare_of, np);
1217 static int msm_drm_bind(struct device *dev)
1219 return msm_drm_init(dev, &msm_driver);
1222 static void msm_drm_unbind(struct device *dev)
1224 msm_drm_uninit(dev);
1227 const struct component_master_ops msm_drm_ops = {
1228 .bind = msm_drm_bind,
1229 .unbind = msm_drm_unbind,
1232 int msm_drv_probe(struct device *master_dev,
1233 int (*kms_init)(struct drm_device *dev))
1235 struct msm_drm_private *priv;
1236 struct component_match *match = NULL;
1239 priv = devm_kzalloc(master_dev, sizeof(*priv), GFP_KERNEL);
1243 priv->kms_init = kms_init;
1244 dev_set_drvdata(master_dev, priv);
1246 /* Add mdp components if we have KMS. */
1248 ret = add_components_mdp(master_dev, &match);
1253 ret = add_gpu_components(master_dev, &match);
1257 /* on all devices that I am aware of, iommu's which can map
1258 * any address the cpu can see are used:
1260 ret = dma_set_mask_and_coherent(master_dev, ~0);
1264 ret = component_master_add_with_match(master_dev, &msm_drm_ops, match);
1273 * Used only for headlesss GPU instances
1276 static int msm_pdev_probe(struct platform_device *pdev)
1278 return msm_drv_probe(&pdev->dev, NULL);
1281 static int msm_pdev_remove(struct platform_device *pdev)
1283 component_master_del(&pdev->dev, &msm_drm_ops);
1288 void msm_drv_shutdown(struct platform_device *pdev)
1290 struct msm_drm_private *priv = platform_get_drvdata(pdev);
1291 struct drm_device *drm = priv ? priv->dev : NULL;
1294 * Shutdown the hw if we're far enough along where things might be on.
1295 * If we run this too early, we'll end up panicking in any variety of
1296 * places. Since we don't register the drm device until late in
1297 * msm_drm_init, drm_dev->registered is used as an indicator that the
1298 * shutdown will be successful.
1300 if (drm && drm->registered && priv->kms)
1301 drm_atomic_helper_shutdown(drm);
1304 static struct platform_driver msm_platform_driver = {
1305 .probe = msm_pdev_probe,
1306 .remove = msm_pdev_remove,
1307 .shutdown = msm_drv_shutdown,
1314 static int __init msm_drm_register(void)
1323 msm_hdmi_register();
1326 msm_mdp4_register();
1327 msm_mdss_register();
1328 return platform_driver_register(&msm_platform_driver);
1331 static void __exit msm_drm_unregister(void)
1334 platform_driver_unregister(&msm_platform_driver);
1335 msm_mdss_unregister();
1336 msm_mdp4_unregister();
1337 msm_dp_unregister();
1338 msm_hdmi_unregister();
1339 adreno_unregister();
1340 msm_dsi_unregister();
1341 msm_mdp_unregister();
1342 msm_dpu_unregister();
1345 module_init(msm_drm_register);
1346 module_exit(msm_drm_unregister);
1348 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1349 MODULE_DESCRIPTION("MSM DRM Driver");
1350 MODULE_LICENSE("GPL");