2 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/kthread.h>
20 #include <uapi/linux/sched/types.h>
21 #include <drm/drm_of.h>
24 #include "msm_debugfs.h"
25 #include "msm_fence.h"
32 * - 1.0.0 - initial interface
33 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
34 * - 1.2.0 - adds explicit fence support for submit ioctl
35 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
36 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
39 #define MSM_VERSION_MAJOR 1
40 #define MSM_VERSION_MINOR 3
41 #define MSM_VERSION_PATCHLEVEL 0
43 static const struct drm_mode_config_funcs mode_config_funcs = {
44 .fb_create = msm_framebuffer_create,
45 .output_poll_changed = drm_fb_helper_output_poll_changed,
46 .atomic_check = drm_atomic_helper_check,
47 .atomic_commit = drm_atomic_helper_commit,
50 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
51 .atomic_commit_tail = msm_atomic_commit_tail,
54 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
55 static bool reglog = false;
56 MODULE_PARM_DESC(reglog, "Enable register read/write logging");
57 module_param(reglog, bool, 0600);
62 #ifdef CONFIG_DRM_FBDEV_EMULATION
63 static bool fbdev = true;
64 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
65 module_param(fbdev, bool, 0600);
68 static char *vram = "16m";
69 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
70 module_param(vram, charp, 0);
72 bool dumpstate = false;
73 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
74 module_param(dumpstate, bool, 0600);
76 static bool modeset = true;
77 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
78 module_param(modeset, bool, 0600);
84 struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
89 clk = devm_clk_get(&pdev->dev, name);
90 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
93 snprintf(name2, sizeof(name2), "%s_clk", name);
95 clk = devm_clk_get(&pdev->dev, name2);
97 dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
98 "\"%s\" instead of \"%s\"\n", name, name2);
103 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
106 struct resource *res;
111 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
116 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
117 return ERR_PTR(-EINVAL);
120 size = resource_size(res);
122 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
124 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
125 return ERR_PTR(-ENOMEM);
129 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
134 void msm_writel(u32 data, void __iomem *addr)
137 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
141 u32 msm_readl(const void __iomem *addr)
143 u32 val = readl(addr);
145 pr_err("IO:R %p %08x\n", addr, val);
149 struct vblank_event {
150 struct list_head node;
155 static void vblank_ctrl_worker(struct kthread_work *work)
157 struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
158 struct msm_vblank_ctrl, work);
159 struct msm_drm_private *priv = container_of(vbl_ctrl,
160 struct msm_drm_private, vblank_ctrl);
161 struct msm_kms *kms = priv->kms;
162 struct vblank_event *vbl_ev, *tmp;
165 spin_lock_irqsave(&vbl_ctrl->lock, flags);
166 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
167 list_del(&vbl_ev->node);
168 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
171 kms->funcs->enable_vblank(kms,
172 priv->crtcs[vbl_ev->crtc_id]);
174 kms->funcs->disable_vblank(kms,
175 priv->crtcs[vbl_ev->crtc_id]);
179 spin_lock_irqsave(&vbl_ctrl->lock, flags);
182 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
185 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
186 int crtc_id, bool enable)
188 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
189 struct vblank_event *vbl_ev;
192 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
196 vbl_ev->crtc_id = crtc_id;
197 vbl_ev->enable = enable;
199 spin_lock_irqsave(&vbl_ctrl->lock, flags);
200 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
201 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
203 kthread_queue_work(&priv->disp_thread[crtc_id].worker,
209 static int msm_drm_uninit(struct device *dev)
211 struct platform_device *pdev = to_platform_device(dev);
212 struct drm_device *ddev = platform_get_drvdata(pdev);
213 struct msm_drm_private *priv = ddev->dev_private;
214 struct msm_kms *kms = priv->kms;
215 struct msm_mdss *mdss = priv->mdss;
216 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
217 struct vblank_event *vbl_ev, *tmp;
220 /* We must cancel and cleanup any pending vblank enable/disable
221 * work before drm_irq_uninstall() to avoid work re-enabling an
222 * irq after uninstall has disabled it.
224 kthread_flush_work(&vbl_ctrl->work);
225 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
226 list_del(&vbl_ev->node);
230 /* clean up display commit/event worker threads */
231 for (i = 0; i < priv->num_crtcs; i++) {
232 if (priv->disp_thread[i].thread) {
233 kthread_flush_worker(&priv->disp_thread[i].worker);
234 kthread_stop(priv->disp_thread[i].thread);
235 priv->disp_thread[i].thread = NULL;
238 if (priv->event_thread[i].thread) {
239 kthread_flush_worker(&priv->event_thread[i].worker);
240 kthread_stop(priv->event_thread[i].thread);
241 priv->event_thread[i].thread = NULL;
245 msm_gem_shrinker_cleanup(ddev);
247 drm_kms_helper_poll_fini(ddev);
249 drm_dev_unregister(ddev);
251 msm_perf_debugfs_cleanup(priv);
252 msm_rd_debugfs_cleanup(priv);
254 #ifdef CONFIG_DRM_FBDEV_EMULATION
255 if (fbdev && priv->fbdev)
256 msm_fbdev_free(ddev);
258 drm_mode_config_cleanup(ddev);
260 pm_runtime_get_sync(dev);
261 drm_irq_uninstall(ddev);
262 pm_runtime_put_sync(dev);
264 flush_workqueue(priv->wq);
265 destroy_workqueue(priv->wq);
267 if (kms && kms->funcs)
268 kms->funcs->destroy(kms);
270 if (priv->vram.paddr) {
271 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
272 drm_mm_takedown(&priv->vram.mm);
273 dma_free_attrs(dev, priv->vram.size, NULL,
274 priv->vram.paddr, attrs);
277 component_unbind_all(dev, ddev);
279 if (mdss && mdss->funcs)
280 mdss->funcs->destroy(ddev);
282 ddev->dev_private = NULL;
294 static int get_mdp_ver(struct platform_device *pdev)
296 struct device *dev = &pdev->dev;
298 return (int) (unsigned long) of_device_get_match_data(dev);
301 #include <linux/of_address.h>
303 static int msm_init_vram(struct drm_device *dev)
305 struct msm_drm_private *priv = dev->dev_private;
306 struct device_node *node;
307 unsigned long size = 0;
310 /* In the device-tree world, we could have a 'memory-region'
311 * phandle, which gives us a link to our "vram". Allocating
312 * is all nicely abstracted behind the dma api, but we need
313 * to know the entire size to allocate it all in one go. There
315 * 1) device with no IOMMU, in which case we need exclusive
316 * access to a VRAM carveout big enough for all gpu
318 * 2) device with IOMMU, but where the bootloader puts up
319 * a splash screen. In this case, the VRAM carveout
320 * need only be large enough for fbdev fb. But we need
321 * exclusive access to the buffer to avoid the kernel
322 * using those pages for other purposes (which appears
323 * as corruption on screen before we have a chance to
324 * load and do initial modeset)
327 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
330 ret = of_address_to_resource(node, 0, &r);
334 size = r.end - r.start;
335 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
337 /* if we have no IOMMU, then we need to use carveout allocator.
338 * Grab the entire CMA chunk carved out in early startup in
341 } else if (!iommu_present(&platform_bus_type)) {
342 DRM_INFO("using %s VRAM carveout\n", vram);
343 size = memparse(vram, NULL);
347 unsigned long attrs = 0;
350 priv->vram.size = size;
352 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
353 spin_lock_init(&priv->vram.lock);
355 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
356 attrs |= DMA_ATTR_WRITE_COMBINE;
358 /* note that for no-kernel-mapping, the vaddr returned
359 * is bogus, but non-null if allocation succeeded:
361 p = dma_alloc_attrs(dev->dev, size,
362 &priv->vram.paddr, GFP_KERNEL, attrs);
364 dev_err(dev->dev, "failed to allocate VRAM\n");
365 priv->vram.paddr = 0;
369 dev_info(dev->dev, "VRAM: %08x->%08x\n",
370 (uint32_t)priv->vram.paddr,
371 (uint32_t)(priv->vram.paddr + size));
377 static int msm_drm_init(struct device *dev, struct drm_driver *drv)
379 struct platform_device *pdev = to_platform_device(dev);
380 struct drm_device *ddev;
381 struct msm_drm_private *priv;
383 struct msm_mdss *mdss;
385 struct sched_param param;
387 ddev = drm_dev_alloc(drv, dev);
389 dev_err(dev, "failed to allocate drm_device\n");
390 return PTR_ERR(ddev);
393 platform_set_drvdata(pdev, ddev);
395 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
398 goto err_unref_drm_dev;
401 ddev->dev_private = priv;
404 switch (get_mdp_ver(pdev)) {
406 ret = mdp5_mdss_init(ddev);
409 ret = dpu_mdss_init(ddev);
420 priv->wq = alloc_ordered_workqueue("msm", 0);
422 INIT_LIST_HEAD(&priv->inactive_list);
423 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
424 kthread_init_work(&priv->vblank_ctrl.work, vblank_ctrl_worker);
425 spin_lock_init(&priv->vblank_ctrl.lock);
427 drm_mode_config_init(ddev);
429 /* Bind all our sub-components: */
430 ret = component_bind_all(dev, ddev);
432 goto err_destroy_mdss;
434 ret = msm_init_vram(ddev);
438 msm_gem_shrinker_init(ddev);
440 switch (get_mdp_ver(pdev)) {
442 kms = mdp4_kms_init(ddev);
446 kms = mdp5_kms_init(ddev);
449 kms = dpu_kms_init(ddev);
453 kms = ERR_PTR(-ENODEV);
459 * NOTE: once we have GPU support, having no kms should not
460 * be considered fatal.. ideally we would still support gpu
461 * and (for example) use dmabuf/prime to share buffers with
462 * imx drm driver on iMX5
464 dev_err(dev, "failed to load kms\n");
469 /* Enable normalization of plane zpos */
470 ddev->mode_config.normalize_zpos = true;
473 ret = kms->funcs->hw_init(kms);
475 dev_err(dev, "kms hw init failed: %d\n", ret);
480 ddev->mode_config.funcs = &mode_config_funcs;
481 ddev->mode_config.helper_private = &mode_config_helper_funcs;
484 * this priority was found during empiric testing to have appropriate
485 * realtime scheduling to process display updates and interact with
486 * other real time and normal priority task
488 param.sched_priority = 16;
489 for (i = 0; i < priv->num_crtcs; i++) {
491 /* initialize display thread */
492 priv->disp_thread[i].crtc_id = priv->crtcs[i]->base.id;
493 kthread_init_worker(&priv->disp_thread[i].worker);
494 priv->disp_thread[i].dev = ddev;
495 priv->disp_thread[i].thread =
496 kthread_run(kthread_worker_fn,
497 &priv->disp_thread[i].worker,
498 "crtc_commit:%d", priv->disp_thread[i].crtc_id);
499 ret = sched_setscheduler(priv->disp_thread[i].thread,
502 pr_warn("display thread priority update failed: %d\n",
505 if (IS_ERR(priv->disp_thread[i].thread)) {
506 dev_err(dev, "failed to create crtc_commit kthread\n");
507 priv->disp_thread[i].thread = NULL;
510 /* initialize event thread */
511 priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
512 kthread_init_worker(&priv->event_thread[i].worker);
513 priv->event_thread[i].dev = ddev;
514 priv->event_thread[i].thread =
515 kthread_run(kthread_worker_fn,
516 &priv->event_thread[i].worker,
517 "crtc_event:%d", priv->event_thread[i].crtc_id);
519 * event thread should also run at same priority as disp_thread
520 * because it is handling frame_done events. A lower priority
521 * event thread and higher priority disp_thread can causes
522 * frame_pending counters beyond 2. This can lead to commit
523 * failure at crtc commit level.
525 ret = sched_setscheduler(priv->event_thread[i].thread,
528 pr_warn("display event thread priority update failed: %d\n",
531 if (IS_ERR(priv->event_thread[i].thread)) {
532 dev_err(dev, "failed to create crtc_event kthread\n");
533 priv->event_thread[i].thread = NULL;
536 if ((!priv->disp_thread[i].thread) ||
537 !priv->event_thread[i].thread) {
538 /* clean up previously created threads if any */
539 for ( ; i >= 0; i--) {
540 if (priv->disp_thread[i].thread) {
542 priv->disp_thread[i].thread);
543 priv->disp_thread[i].thread = NULL;
546 if (priv->event_thread[i].thread) {
548 priv->event_thread[i].thread);
549 priv->event_thread[i].thread = NULL;
556 ret = drm_vblank_init(ddev, priv->num_crtcs);
558 dev_err(dev, "failed to initialize vblank\n");
563 pm_runtime_get_sync(dev);
564 ret = drm_irq_install(ddev, kms->irq);
565 pm_runtime_put_sync(dev);
567 dev_err(dev, "failed to install IRQ handler\n");
572 ret = drm_dev_register(ddev, 0);
576 drm_mode_config_reset(ddev);
578 #ifdef CONFIG_DRM_FBDEV_EMULATION
580 priv->fbdev = msm_fbdev_init(ddev);
583 ret = msm_debugfs_late_init(ddev);
587 drm_kms_helper_poll_init(ddev);
595 if (mdss && mdss->funcs)
596 mdss->funcs->destroy(ddev);
608 static void load_gpu(struct drm_device *dev)
610 static DEFINE_MUTEX(init_lock);
611 struct msm_drm_private *priv = dev->dev_private;
613 mutex_lock(&init_lock);
616 priv->gpu = adreno_load_gpu(dev);
618 mutex_unlock(&init_lock);
621 static int context_init(struct drm_device *dev, struct drm_file *file)
623 struct msm_file_private *ctx;
625 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
629 msm_submitqueue_init(dev, ctx);
631 file->driver_priv = ctx;
636 static int msm_open(struct drm_device *dev, struct drm_file *file)
638 /* For now, load gpu on open.. to avoid the requirement of having
639 * firmware in the initrd.
643 return context_init(dev, file);
646 static void context_close(struct msm_file_private *ctx)
648 msm_submitqueue_close(ctx);
652 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
654 struct msm_drm_private *priv = dev->dev_private;
655 struct msm_file_private *ctx = file->driver_priv;
657 mutex_lock(&dev->struct_mutex);
658 if (ctx == priv->lastctx)
659 priv->lastctx = NULL;
660 mutex_unlock(&dev->struct_mutex);
665 static irqreturn_t msm_irq(int irq, void *arg)
667 struct drm_device *dev = arg;
668 struct msm_drm_private *priv = dev->dev_private;
669 struct msm_kms *kms = priv->kms;
671 return kms->funcs->irq(kms);
674 static void msm_irq_preinstall(struct drm_device *dev)
676 struct msm_drm_private *priv = dev->dev_private;
677 struct msm_kms *kms = priv->kms;
679 kms->funcs->irq_preinstall(kms);
682 static int msm_irq_postinstall(struct drm_device *dev)
684 struct msm_drm_private *priv = dev->dev_private;
685 struct msm_kms *kms = priv->kms;
687 return kms->funcs->irq_postinstall(kms);
690 static void msm_irq_uninstall(struct drm_device *dev)
692 struct msm_drm_private *priv = dev->dev_private;
693 struct msm_kms *kms = priv->kms;
695 kms->funcs->irq_uninstall(kms);
698 static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
700 struct msm_drm_private *priv = dev->dev_private;
701 struct msm_kms *kms = priv->kms;
704 DBG("dev=%p, crtc=%u", dev, pipe);
705 return vblank_ctrl_queue_work(priv, pipe, true);
708 static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
710 struct msm_drm_private *priv = dev->dev_private;
711 struct msm_kms *kms = priv->kms;
714 DBG("dev=%p, crtc=%u", dev, pipe);
715 vblank_ctrl_queue_work(priv, pipe, false);
722 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
723 struct drm_file *file)
725 struct msm_drm_private *priv = dev->dev_private;
726 struct drm_msm_param *args = data;
729 /* for now, we just have 3d pipe.. eventually this would need to
730 * be more clever to dispatch to appropriate gpu module:
732 if (args->pipe != MSM_PIPE_3D0)
740 return gpu->funcs->get_param(gpu, args->param, &args->value);
743 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
744 struct drm_file *file)
746 struct drm_msm_gem_new *args = data;
748 if (args->flags & ~MSM_BO_FLAGS) {
749 DRM_ERROR("invalid flags: %08x\n", args->flags);
753 return msm_gem_new_handle(dev, file, args->size,
754 args->flags, &args->handle);
757 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
759 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
762 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
763 struct drm_file *file)
765 struct drm_msm_gem_cpu_prep *args = data;
766 struct drm_gem_object *obj;
767 ktime_t timeout = to_ktime(args->timeout);
770 if (args->op & ~MSM_PREP_FLAGS) {
771 DRM_ERROR("invalid op: %08x\n", args->op);
775 obj = drm_gem_object_lookup(file, args->handle);
779 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
781 drm_gem_object_put_unlocked(obj);
786 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
787 struct drm_file *file)
789 struct drm_msm_gem_cpu_fini *args = data;
790 struct drm_gem_object *obj;
793 obj = drm_gem_object_lookup(file, args->handle);
797 ret = msm_gem_cpu_fini(obj);
799 drm_gem_object_put_unlocked(obj);
804 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
805 struct drm_gem_object *obj, uint64_t *iova)
807 struct msm_drm_private *priv = dev->dev_private;
812 return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
815 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
816 struct drm_file *file)
818 struct drm_msm_gem_info *args = data;
819 struct drm_gem_object *obj;
822 if (args->flags & ~MSM_INFO_FLAGS)
825 obj = drm_gem_object_lookup(file, args->handle);
829 if (args->flags & MSM_INFO_IOVA) {
832 ret = msm_ioctl_gem_info_iova(dev, obj, &iova);
836 args->offset = msm_gem_mmap_offset(obj);
839 drm_gem_object_put_unlocked(obj);
844 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
845 struct drm_file *file)
847 struct msm_drm_private *priv = dev->dev_private;
848 struct drm_msm_wait_fence *args = data;
849 ktime_t timeout = to_ktime(args->timeout);
850 struct msm_gpu_submitqueue *queue;
851 struct msm_gpu *gpu = priv->gpu;
855 DRM_ERROR("invalid pad: %08x\n", args->pad);
862 queue = msm_submitqueue_get(file->driver_priv, args->queueid);
866 ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
869 msm_submitqueue_put(queue);
873 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
874 struct drm_file *file)
876 struct drm_msm_gem_madvise *args = data;
877 struct drm_gem_object *obj;
880 switch (args->madv) {
881 case MSM_MADV_DONTNEED:
882 case MSM_MADV_WILLNEED:
888 ret = mutex_lock_interruptible(&dev->struct_mutex);
892 obj = drm_gem_object_lookup(file, args->handle);
898 ret = msm_gem_madvise(obj, args->madv);
900 args->retained = ret;
904 drm_gem_object_put(obj);
907 mutex_unlock(&dev->struct_mutex);
912 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
913 struct drm_file *file)
915 struct drm_msm_submitqueue *args = data;
917 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
920 return msm_submitqueue_create(dev, file->driver_priv, args->prio,
921 args->flags, &args->id);
925 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
926 struct drm_file *file)
928 u32 id = *(u32 *) data;
930 return msm_submitqueue_remove(file->driver_priv, id);
933 static const struct drm_ioctl_desc msm_ioctls[] = {
934 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
935 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
936 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
937 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
938 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
939 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
940 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
941 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
942 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_AUTH|DRM_RENDER_ALLOW),
943 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_AUTH|DRM_RENDER_ALLOW),
946 static const struct vm_operations_struct vm_ops = {
947 .fault = msm_gem_fault,
948 .open = drm_gem_vm_open,
949 .close = drm_gem_vm_close,
952 static const struct file_operations fops = {
953 .owner = THIS_MODULE,
955 .release = drm_release,
956 .unlocked_ioctl = drm_ioctl,
957 .compat_ioctl = drm_compat_ioctl,
961 .mmap = msm_gem_mmap,
964 static struct drm_driver msm_driver = {
965 .driver_features = DRIVER_HAVE_IRQ |
972 .postclose = msm_postclose,
973 .lastclose = drm_fb_helper_lastclose,
974 .irq_handler = msm_irq,
975 .irq_preinstall = msm_irq_preinstall,
976 .irq_postinstall = msm_irq_postinstall,
977 .irq_uninstall = msm_irq_uninstall,
978 .enable_vblank = msm_enable_vblank,
979 .disable_vblank = msm_disable_vblank,
980 .gem_free_object = msm_gem_free_object,
981 .gem_vm_ops = &vm_ops,
982 .dumb_create = msm_gem_dumb_create,
983 .dumb_map_offset = msm_gem_dumb_map_offset,
984 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
985 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
986 .gem_prime_export = drm_gem_prime_export,
987 .gem_prime_import = drm_gem_prime_import,
988 .gem_prime_res_obj = msm_gem_prime_res_obj,
989 .gem_prime_pin = msm_gem_prime_pin,
990 .gem_prime_unpin = msm_gem_prime_unpin,
991 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
992 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
993 .gem_prime_vmap = msm_gem_prime_vmap,
994 .gem_prime_vunmap = msm_gem_prime_vunmap,
995 .gem_prime_mmap = msm_gem_prime_mmap,
996 #ifdef CONFIG_DEBUG_FS
997 .debugfs_init = msm_debugfs_init,
999 .ioctls = msm_ioctls,
1000 .num_ioctls = ARRAY_SIZE(msm_ioctls),
1003 .desc = "MSM Snapdragon DRM",
1005 .major = MSM_VERSION_MAJOR,
1006 .minor = MSM_VERSION_MINOR,
1007 .patchlevel = MSM_VERSION_PATCHLEVEL,
1010 #ifdef CONFIG_PM_SLEEP
1011 static int msm_pm_suspend(struct device *dev)
1013 struct drm_device *ddev = dev_get_drvdata(dev);
1014 struct msm_drm_private *priv = ddev->dev_private;
1015 struct msm_kms *kms = priv->kms;
1017 /* TODO: Use atomic helper suspend/resume */
1018 if (kms && kms->funcs && kms->funcs->pm_suspend)
1019 return kms->funcs->pm_suspend(dev);
1021 drm_kms_helper_poll_disable(ddev);
1023 priv->pm_state = drm_atomic_helper_suspend(ddev);
1024 if (IS_ERR(priv->pm_state)) {
1025 drm_kms_helper_poll_enable(ddev);
1026 return PTR_ERR(priv->pm_state);
1032 static int msm_pm_resume(struct device *dev)
1034 struct drm_device *ddev = dev_get_drvdata(dev);
1035 struct msm_drm_private *priv = ddev->dev_private;
1036 struct msm_kms *kms = priv->kms;
1038 /* TODO: Use atomic helper suspend/resume */
1039 if (kms && kms->funcs && kms->funcs->pm_resume)
1040 return kms->funcs->pm_resume(dev);
1042 drm_atomic_helper_resume(ddev, priv->pm_state);
1043 drm_kms_helper_poll_enable(ddev);
1050 static int msm_runtime_suspend(struct device *dev)
1052 struct drm_device *ddev = dev_get_drvdata(dev);
1053 struct msm_drm_private *priv = ddev->dev_private;
1054 struct msm_mdss *mdss = priv->mdss;
1058 if (mdss && mdss->funcs)
1059 return mdss->funcs->disable(mdss);
1064 static int msm_runtime_resume(struct device *dev)
1066 struct drm_device *ddev = dev_get_drvdata(dev);
1067 struct msm_drm_private *priv = ddev->dev_private;
1068 struct msm_mdss *mdss = priv->mdss;
1072 if (mdss && mdss->funcs)
1073 return mdss->funcs->enable(mdss);
1079 static const struct dev_pm_ops msm_pm_ops = {
1080 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
1081 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
1085 * Componentized driver support:
1089 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1090 * so probably some room for some helpers
1092 static int compare_of(struct device *dev, void *data)
1094 return dev->of_node == data;
1098 * Identify what components need to be added by parsing what remote-endpoints
1099 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1100 * is no external component that we need to add since LVDS is within MDP4
1103 static int add_components_mdp(struct device *mdp_dev,
1104 struct component_match **matchptr)
1106 struct device_node *np = mdp_dev->of_node;
1107 struct device_node *ep_node;
1108 struct device *master_dev;
1111 * on MDP4 based platforms, the MDP platform device is the component
1112 * master that adds other display interface components to itself.
1114 * on MDP5 based platforms, the MDSS platform device is the component
1115 * master that adds MDP5 and other display interface components to
1118 if (of_device_is_compatible(np, "qcom,mdp4"))
1119 master_dev = mdp_dev;
1121 master_dev = mdp_dev->parent;
1123 for_each_endpoint_of_node(np, ep_node) {
1124 struct device_node *intf;
1125 struct of_endpoint ep;
1128 ret = of_graph_parse_endpoint(ep_node, &ep);
1130 dev_err(mdp_dev, "unable to parse port endpoint\n");
1131 of_node_put(ep_node);
1136 * The LCDC/LVDS port on MDP4 is a speacial case where the
1137 * remote-endpoint isn't a component that we need to add
1139 if (of_device_is_compatible(np, "qcom,mdp4") &&
1144 * It's okay if some of the ports don't have a remote endpoint
1145 * specified. It just means that the port isn't connected to
1146 * any external interface.
1148 intf = of_graph_get_remote_port_parent(ep_node);
1152 drm_of_component_match_add(master_dev, matchptr, compare_of,
1160 static int compare_name_mdp(struct device *dev, void *data)
1162 return (strstr(dev_name(dev), "mdp") != NULL);
1165 static int add_display_components(struct device *dev,
1166 struct component_match **matchptr)
1168 struct device *mdp_dev;
1172 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1173 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1174 * Populate the children devices, find the MDP5/DPU node, and then add
1175 * the interfaces to our components list.
1177 if (of_device_is_compatible(dev->of_node, "qcom,mdss") ||
1178 of_device_is_compatible(dev->of_node, "qcom,sdm845-mdss")) {
1179 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1181 dev_err(dev, "failed to populate children devices\n");
1185 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1187 dev_err(dev, "failed to find MDSS MDP node\n");
1188 of_platform_depopulate(dev);
1192 put_device(mdp_dev);
1194 /* add the MDP component itself */
1195 drm_of_component_match_add(dev, matchptr, compare_of,
1202 ret = add_components_mdp(mdp_dev, matchptr);
1204 of_platform_depopulate(dev);
1210 * We don't know what's the best binding to link the gpu with the drm device.
1211 * Fow now, we just hunt for all the possible gpus that we support, and add them
1214 static const struct of_device_id msm_gpu_match[] = {
1215 { .compatible = "qcom,adreno" },
1216 { .compatible = "qcom,adreno-3xx" },
1217 { .compatible = "qcom,kgsl-3d0" },
1221 static int add_gpu_components(struct device *dev,
1222 struct component_match **matchptr)
1224 struct device_node *np;
1226 np = of_find_matching_node(NULL, msm_gpu_match);
1230 drm_of_component_match_add(dev, matchptr, compare_of, np);
1237 static int msm_drm_bind(struct device *dev)
1239 return msm_drm_init(dev, &msm_driver);
1242 static void msm_drm_unbind(struct device *dev)
1244 msm_drm_uninit(dev);
1247 static const struct component_master_ops msm_drm_ops = {
1248 .bind = msm_drm_bind,
1249 .unbind = msm_drm_unbind,
1256 static int msm_pdev_probe(struct platform_device *pdev)
1258 struct component_match *match = NULL;
1261 ret = add_display_components(&pdev->dev, &match);
1265 ret = add_gpu_components(&pdev->dev, &match);
1269 /* on all devices that I am aware of, iommu's which can map
1270 * any address the cpu can see are used:
1272 ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1276 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1279 static int msm_pdev_remove(struct platform_device *pdev)
1281 component_master_del(&pdev->dev, &msm_drm_ops);
1282 of_platform_depopulate(&pdev->dev);
1287 static const struct of_device_id dt_match[] = {
1288 { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
1289 { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
1290 { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU },
1293 MODULE_DEVICE_TABLE(of, dt_match);
1295 static struct platform_driver msm_platform_driver = {
1296 .probe = msm_pdev_probe,
1297 .remove = msm_pdev_remove,
1300 .of_match_table = dt_match,
1305 static int __init msm_drm_register(void)
1315 msm_hdmi_register();
1317 return platform_driver_register(&msm_platform_driver);
1320 static void __exit msm_drm_unregister(void)
1323 platform_driver_unregister(&msm_platform_driver);
1324 msm_hdmi_unregister();
1325 adreno_unregister();
1326 msm_edp_unregister();
1327 msm_dsi_unregister();
1328 msm_mdp_unregister();
1329 msm_dpu_unregister();
1332 module_init(msm_drm_register);
1333 module_exit(msm_drm_unregister);
1336 MODULE_DESCRIPTION("MSM DRM Driver");
1337 MODULE_LICENSE("GPL");