]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/msm/msm_drv.c
drm/msm/adreno: load gpu at probe/bind time
[linux.git] / drivers / gpu / drm / msm / msm_drv.c
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <drm/drm_of.h>
19
20 #include "msm_drv.h"
21 #include "msm_debugfs.h"
22 #include "msm_fence.h"
23 #include "msm_gpu.h"
24 #include "msm_kms.h"
25
26
27 /*
28  * MSM driver version:
29  * - 1.0.0 - initial interface
30  * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
31  * - 1.2.0 - adds explicit fence support for submit ioctl
32  */
33 #define MSM_VERSION_MAJOR       1
34 #define MSM_VERSION_MINOR       2
35 #define MSM_VERSION_PATCHLEVEL  0
36
37 static void msm_fb_output_poll_changed(struct drm_device *dev)
38 {
39         struct msm_drm_private *priv = dev->dev_private;
40         if (priv->fbdev)
41                 drm_fb_helper_hotplug_event(priv->fbdev);
42 }
43
44 static const struct drm_mode_config_funcs mode_config_funcs = {
45         .fb_create = msm_framebuffer_create,
46         .output_poll_changed = msm_fb_output_poll_changed,
47         .atomic_check = msm_atomic_check,
48         .atomic_commit = msm_atomic_commit,
49         .atomic_state_alloc = msm_atomic_state_alloc,
50         .atomic_state_clear = msm_atomic_state_clear,
51         .atomic_state_free = msm_atomic_state_free,
52 };
53
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);
58 #else
59 #define reglog 0
60 #endif
61
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);
66 #endif
67
68 static char *vram = "16m";
69 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
70 module_param(vram, charp, 0);
71
72 bool dumpstate = false;
73 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
74 module_param(dumpstate, bool, 0600);
75
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);
79
80 /*
81  * Util/helpers:
82  */
83
84 struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
85 {
86         struct clk *clk;
87         char name2[32];
88
89         clk = devm_clk_get(&pdev->dev, name);
90         if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
91                 return clk;
92
93         snprintf(name2, sizeof(name2), "%s_clk", name);
94
95         clk = devm_clk_get(&pdev->dev, name2);
96         if (!IS_ERR(clk))
97                 dev_warn(&pdev->dev, "Using legacy clk name binding.  Use "
98                                 "\"%s\" instead of \"%s\"\n", name, name2);
99
100         return clk;
101 }
102
103 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
104                 const char *dbgname)
105 {
106         struct resource *res;
107         unsigned long size;
108         void __iomem *ptr;
109
110         if (name)
111                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
112         else
113                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114
115         if (!res) {
116                 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
117                 return ERR_PTR(-EINVAL);
118         }
119
120         size = resource_size(res);
121
122         ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
123         if (!ptr) {
124                 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
125                 return ERR_PTR(-ENOMEM);
126         }
127
128         if (reglog)
129                 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
130
131         return ptr;
132 }
133
134 void msm_writel(u32 data, void __iomem *addr)
135 {
136         if (reglog)
137                 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
138         writel(data, addr);
139 }
140
141 u32 msm_readl(const void __iomem *addr)
142 {
143         u32 val = readl(addr);
144         if (reglog)
145                 pr_err("IO:R %p %08x\n", addr, val);
146         return val;
147 }
148
149 struct vblank_event {
150         struct list_head node;
151         int crtc_id;
152         bool enable;
153 };
154
155 static void vblank_ctrl_worker(struct work_struct *work)
156 {
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;
163         unsigned long flags;
164
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);
169
170                 if (vbl_ev->enable)
171                         kms->funcs->enable_vblank(kms,
172                                                 priv->crtcs[vbl_ev->crtc_id]);
173                 else
174                         kms->funcs->disable_vblank(kms,
175                                                 priv->crtcs[vbl_ev->crtc_id]);
176
177                 kfree(vbl_ev);
178
179                 spin_lock_irqsave(&vbl_ctrl->lock, flags);
180         }
181
182         spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
183 }
184
185 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
186                                         int crtc_id, bool enable)
187 {
188         struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
189         struct vblank_event *vbl_ev;
190         unsigned long flags;
191
192         vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
193         if (!vbl_ev)
194                 return -ENOMEM;
195
196         vbl_ev->crtc_id = crtc_id;
197         vbl_ev->enable = enable;
198
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);
202
203         queue_work(priv->wq, &vbl_ctrl->work);
204
205         return 0;
206 }
207
208 static int msm_drm_uninit(struct device *dev)
209 {
210         struct platform_device *pdev = to_platform_device(dev);
211         struct drm_device *ddev = platform_get_drvdata(pdev);
212         struct msm_drm_private *priv = ddev->dev_private;
213         struct msm_kms *kms = priv->kms;
214         struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
215         struct vblank_event *vbl_ev, *tmp;
216
217         /* We must cancel and cleanup any pending vblank enable/disable
218          * work before drm_irq_uninstall() to avoid work re-enabling an
219          * irq after uninstall has disabled it.
220          */
221         cancel_work_sync(&vbl_ctrl->work);
222         list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
223                 list_del(&vbl_ev->node);
224                 kfree(vbl_ev);
225         }
226
227         msm_gem_shrinker_cleanup(ddev);
228
229         drm_kms_helper_poll_fini(ddev);
230
231         drm_dev_unregister(ddev);
232
233         msm_perf_debugfs_cleanup(priv);
234         msm_rd_debugfs_cleanup(priv);
235
236 #ifdef CONFIG_DRM_FBDEV_EMULATION
237         if (fbdev && priv->fbdev)
238                 msm_fbdev_free(ddev);
239 #endif
240         drm_mode_config_cleanup(ddev);
241
242         pm_runtime_get_sync(dev);
243         drm_irq_uninstall(ddev);
244         pm_runtime_put_sync(dev);
245
246         flush_workqueue(priv->wq);
247         destroy_workqueue(priv->wq);
248
249         flush_workqueue(priv->atomic_wq);
250         destroy_workqueue(priv->atomic_wq);
251
252         if (kms && kms->funcs)
253                 kms->funcs->destroy(kms);
254
255         if (priv->vram.paddr) {
256                 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
257                 drm_mm_takedown(&priv->vram.mm);
258                 dma_free_attrs(dev, priv->vram.size, NULL,
259                                priv->vram.paddr, attrs);
260         }
261
262         component_unbind_all(dev, ddev);
263
264         msm_mdss_destroy(ddev);
265
266         ddev->dev_private = NULL;
267         drm_dev_unref(ddev);
268
269         kfree(priv);
270
271         return 0;
272 }
273
274 static int get_mdp_ver(struct platform_device *pdev)
275 {
276         struct device *dev = &pdev->dev;
277
278         return (int) (unsigned long) of_device_get_match_data(dev);
279 }
280
281 #include <linux/of_address.h>
282
283 static int msm_init_vram(struct drm_device *dev)
284 {
285         struct msm_drm_private *priv = dev->dev_private;
286         struct device_node *node;
287         unsigned long size = 0;
288         int ret = 0;
289
290         /* In the device-tree world, we could have a 'memory-region'
291          * phandle, which gives us a link to our "vram".  Allocating
292          * is all nicely abstracted behind the dma api, but we need
293          * to know the entire size to allocate it all in one go. There
294          * are two cases:
295          *  1) device with no IOMMU, in which case we need exclusive
296          *     access to a VRAM carveout big enough for all gpu
297          *     buffers
298          *  2) device with IOMMU, but where the bootloader puts up
299          *     a splash screen.  In this case, the VRAM carveout
300          *     need only be large enough for fbdev fb.  But we need
301          *     exclusive access to the buffer to avoid the kernel
302          *     using those pages for other purposes (which appears
303          *     as corruption on screen before we have a chance to
304          *     load and do initial modeset)
305          */
306
307         node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
308         if (node) {
309                 struct resource r;
310                 ret = of_address_to_resource(node, 0, &r);
311                 of_node_put(node);
312                 if (ret)
313                         return ret;
314                 size = r.end - r.start;
315                 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
316
317                 /* if we have no IOMMU, then we need to use carveout allocator.
318                  * Grab the entire CMA chunk carved out in early startup in
319                  * mach-msm:
320                  */
321         } else if (!iommu_present(&platform_bus_type)) {
322                 DRM_INFO("using %s VRAM carveout\n", vram);
323                 size = memparse(vram, NULL);
324         }
325
326         if (size) {
327                 unsigned long attrs = 0;
328                 void *p;
329
330                 priv->vram.size = size;
331
332                 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
333                 spin_lock_init(&priv->vram.lock);
334
335                 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
336                 attrs |= DMA_ATTR_WRITE_COMBINE;
337
338                 /* note that for no-kernel-mapping, the vaddr returned
339                  * is bogus, but non-null if allocation succeeded:
340                  */
341                 p = dma_alloc_attrs(dev->dev, size,
342                                 &priv->vram.paddr, GFP_KERNEL, attrs);
343                 if (!p) {
344                         dev_err(dev->dev, "failed to allocate VRAM\n");
345                         priv->vram.paddr = 0;
346                         return -ENOMEM;
347                 }
348
349                 dev_info(dev->dev, "VRAM: %08x->%08x\n",
350                                 (uint32_t)priv->vram.paddr,
351                                 (uint32_t)(priv->vram.paddr + size));
352         }
353
354         return ret;
355 }
356
357 static int msm_drm_init(struct device *dev, struct drm_driver *drv)
358 {
359         struct platform_device *pdev = to_platform_device(dev);
360         struct drm_device *ddev;
361         struct msm_drm_private *priv;
362         struct msm_kms *kms;
363         int ret;
364
365         ddev = drm_dev_alloc(drv, dev);
366         if (IS_ERR(ddev)) {
367                 dev_err(dev, "failed to allocate drm_device\n");
368                 return PTR_ERR(ddev);
369         }
370
371         platform_set_drvdata(pdev, ddev);
372
373         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
374         if (!priv) {
375                 drm_dev_unref(ddev);
376                 return -ENOMEM;
377         }
378
379         ddev->dev_private = priv;
380         priv->dev = ddev;
381
382         ret = msm_mdss_init(ddev);
383         if (ret) {
384                 kfree(priv);
385                 drm_dev_unref(ddev);
386                 return ret;
387         }
388
389         priv->wq = alloc_ordered_workqueue("msm", 0);
390         priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
391         init_waitqueue_head(&priv->pending_crtcs_event);
392
393         INIT_LIST_HEAD(&priv->inactive_list);
394         INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
395         INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
396         spin_lock_init(&priv->vblank_ctrl.lock);
397
398         drm_mode_config_init(ddev);
399
400         /* Bind all our sub-components: */
401         ret = component_bind_all(dev, ddev);
402         if (ret) {
403                 msm_mdss_destroy(ddev);
404                 kfree(priv);
405                 drm_dev_unref(ddev);
406                 return ret;
407         }
408
409         ret = msm_init_vram(ddev);
410         if (ret)
411                 goto fail;
412
413         msm_gem_shrinker_init(ddev);
414
415         switch (get_mdp_ver(pdev)) {
416         case 4:
417                 kms = mdp4_kms_init(ddev);
418                 priv->kms = kms;
419                 break;
420         case 5:
421                 kms = mdp5_kms_init(ddev);
422                 break;
423         default:
424                 kms = ERR_PTR(-ENODEV);
425                 break;
426         }
427
428         if (IS_ERR(kms)) {
429                 /*
430                  * NOTE: once we have GPU support, having no kms should not
431                  * be considered fatal.. ideally we would still support gpu
432                  * and (for example) use dmabuf/prime to share buffers with
433                  * imx drm driver on iMX5
434                  */
435                 dev_err(dev, "failed to load kms\n");
436                 ret = PTR_ERR(kms);
437                 goto fail;
438         }
439
440         if (kms) {
441                 ret = kms->funcs->hw_init(kms);
442                 if (ret) {
443                         dev_err(dev, "kms hw init failed: %d\n", ret);
444                         goto fail;
445                 }
446         }
447
448         ddev->mode_config.funcs = &mode_config_funcs;
449
450         ret = drm_vblank_init(ddev, priv->num_crtcs);
451         if (ret < 0) {
452                 dev_err(dev, "failed to initialize vblank\n");
453                 goto fail;
454         }
455
456         if (kms) {
457                 pm_runtime_get_sync(dev);
458                 ret = drm_irq_install(ddev, kms->irq);
459                 pm_runtime_put_sync(dev);
460                 if (ret < 0) {
461                         dev_err(dev, "failed to install IRQ handler\n");
462                         goto fail;
463                 }
464         }
465
466         ret = drm_dev_register(ddev, 0);
467         if (ret)
468                 goto fail;
469
470         drm_mode_config_reset(ddev);
471
472 #ifdef CONFIG_DRM_FBDEV_EMULATION
473         if (fbdev)
474                 priv->fbdev = msm_fbdev_init(ddev);
475 #endif
476
477         ret = msm_debugfs_late_init(ddev);
478         if (ret)
479                 goto fail;
480
481         drm_kms_helper_poll_init(ddev);
482
483         return 0;
484
485 fail:
486         msm_drm_uninit(dev);
487         return ret;
488 }
489
490 /*
491  * DRM operations:
492  */
493
494 static void load_gpu(struct drm_device *dev)
495 {
496         static DEFINE_MUTEX(init_lock);
497         struct msm_drm_private *priv = dev->dev_private;
498
499         mutex_lock(&init_lock);
500
501         if (!priv->gpu)
502                 priv->gpu = adreno_load_gpu(dev);
503
504         mutex_unlock(&init_lock);
505 }
506
507 static int msm_open(struct drm_device *dev, struct drm_file *file)
508 {
509         struct msm_file_private *ctx;
510
511         /* For now, load gpu on open.. to avoid the requirement of having
512          * firmware in the initrd.
513          */
514         load_gpu(dev);
515
516         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
517         if (!ctx)
518                 return -ENOMEM;
519
520         file->driver_priv = ctx;
521
522         return 0;
523 }
524
525 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
526 {
527         struct msm_drm_private *priv = dev->dev_private;
528         struct msm_file_private *ctx = file->driver_priv;
529
530         mutex_lock(&dev->struct_mutex);
531         if (ctx == priv->lastctx)
532                 priv->lastctx = NULL;
533         mutex_unlock(&dev->struct_mutex);
534
535         kfree(ctx);
536 }
537
538 static void msm_lastclose(struct drm_device *dev)
539 {
540         struct msm_drm_private *priv = dev->dev_private;
541         if (priv->fbdev)
542                 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
543 }
544
545 static irqreturn_t msm_irq(int irq, void *arg)
546 {
547         struct drm_device *dev = arg;
548         struct msm_drm_private *priv = dev->dev_private;
549         struct msm_kms *kms = priv->kms;
550         BUG_ON(!kms);
551         return kms->funcs->irq(kms);
552 }
553
554 static void msm_irq_preinstall(struct drm_device *dev)
555 {
556         struct msm_drm_private *priv = dev->dev_private;
557         struct msm_kms *kms = priv->kms;
558         BUG_ON(!kms);
559         kms->funcs->irq_preinstall(kms);
560 }
561
562 static int msm_irq_postinstall(struct drm_device *dev)
563 {
564         struct msm_drm_private *priv = dev->dev_private;
565         struct msm_kms *kms = priv->kms;
566         BUG_ON(!kms);
567         return kms->funcs->irq_postinstall(kms);
568 }
569
570 static void msm_irq_uninstall(struct drm_device *dev)
571 {
572         struct msm_drm_private *priv = dev->dev_private;
573         struct msm_kms *kms = priv->kms;
574         BUG_ON(!kms);
575         kms->funcs->irq_uninstall(kms);
576 }
577
578 static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
579 {
580         struct msm_drm_private *priv = dev->dev_private;
581         struct msm_kms *kms = priv->kms;
582         if (!kms)
583                 return -ENXIO;
584         DBG("dev=%p, crtc=%u", dev, pipe);
585         return vblank_ctrl_queue_work(priv, pipe, true);
586 }
587
588 static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
589 {
590         struct msm_drm_private *priv = dev->dev_private;
591         struct msm_kms *kms = priv->kms;
592         if (!kms)
593                 return;
594         DBG("dev=%p, crtc=%u", dev, pipe);
595         vblank_ctrl_queue_work(priv, pipe, false);
596 }
597
598 /*
599  * DRM ioctls:
600  */
601
602 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
603                 struct drm_file *file)
604 {
605         struct msm_drm_private *priv = dev->dev_private;
606         struct drm_msm_param *args = data;
607         struct msm_gpu *gpu;
608
609         /* for now, we just have 3d pipe.. eventually this would need to
610          * be more clever to dispatch to appropriate gpu module:
611          */
612         if (args->pipe != MSM_PIPE_3D0)
613                 return -EINVAL;
614
615         gpu = priv->gpu;
616
617         if (!gpu)
618                 return -ENXIO;
619
620         return gpu->funcs->get_param(gpu, args->param, &args->value);
621 }
622
623 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
624                 struct drm_file *file)
625 {
626         struct drm_msm_gem_new *args = data;
627
628         if (args->flags & ~MSM_BO_FLAGS) {
629                 DRM_ERROR("invalid flags: %08x\n", args->flags);
630                 return -EINVAL;
631         }
632
633         return msm_gem_new_handle(dev, file, args->size,
634                         args->flags, &args->handle);
635 }
636
637 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
638 {
639         return ktime_set(timeout.tv_sec, timeout.tv_nsec);
640 }
641
642 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
643                 struct drm_file *file)
644 {
645         struct drm_msm_gem_cpu_prep *args = data;
646         struct drm_gem_object *obj;
647         ktime_t timeout = to_ktime(args->timeout);
648         int ret;
649
650         if (args->op & ~MSM_PREP_FLAGS) {
651                 DRM_ERROR("invalid op: %08x\n", args->op);
652                 return -EINVAL;
653         }
654
655         obj = drm_gem_object_lookup(file, args->handle);
656         if (!obj)
657                 return -ENOENT;
658
659         ret = msm_gem_cpu_prep(obj, args->op, &timeout);
660
661         drm_gem_object_unreference_unlocked(obj);
662
663         return ret;
664 }
665
666 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
667                 struct drm_file *file)
668 {
669         struct drm_msm_gem_cpu_fini *args = data;
670         struct drm_gem_object *obj;
671         int ret;
672
673         obj = drm_gem_object_lookup(file, args->handle);
674         if (!obj)
675                 return -ENOENT;
676
677         ret = msm_gem_cpu_fini(obj);
678
679         drm_gem_object_unreference_unlocked(obj);
680
681         return ret;
682 }
683
684 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
685                 struct drm_gem_object *obj, uint64_t *iova)
686 {
687         struct msm_drm_private *priv = dev->dev_private;
688
689         if (!priv->gpu)
690                 return -EINVAL;
691
692         return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
693 }
694
695 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
696                 struct drm_file *file)
697 {
698         struct drm_msm_gem_info *args = data;
699         struct drm_gem_object *obj;
700         int ret = 0;
701
702         if (args->flags & ~MSM_INFO_FLAGS)
703                 return -EINVAL;
704
705         obj = drm_gem_object_lookup(file, args->handle);
706         if (!obj)
707                 return -ENOENT;
708
709         if (args->flags & MSM_INFO_IOVA) {
710                 uint64_t iova;
711
712                 ret = msm_ioctl_gem_info_iova(dev, obj, &iova);
713                 if (!ret)
714                         args->offset = iova;
715         } else {
716                 args->offset = msm_gem_mmap_offset(obj);
717         }
718
719         drm_gem_object_unreference_unlocked(obj);
720
721         return ret;
722 }
723
724 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
725                 struct drm_file *file)
726 {
727         struct msm_drm_private *priv = dev->dev_private;
728         struct drm_msm_wait_fence *args = data;
729         ktime_t timeout = to_ktime(args->timeout);
730
731         if (args->pad) {
732                 DRM_ERROR("invalid pad: %08x\n", args->pad);
733                 return -EINVAL;
734         }
735
736         if (!priv->gpu)
737                 return 0;
738
739         return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
740 }
741
742 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
743                 struct drm_file *file)
744 {
745         struct drm_msm_gem_madvise *args = data;
746         struct drm_gem_object *obj;
747         int ret;
748
749         switch (args->madv) {
750         case MSM_MADV_DONTNEED:
751         case MSM_MADV_WILLNEED:
752                 break;
753         default:
754                 return -EINVAL;
755         }
756
757         ret = mutex_lock_interruptible(&dev->struct_mutex);
758         if (ret)
759                 return ret;
760
761         obj = drm_gem_object_lookup(file, args->handle);
762         if (!obj) {
763                 ret = -ENOENT;
764                 goto unlock;
765         }
766
767         ret = msm_gem_madvise(obj, args->madv);
768         if (ret >= 0) {
769                 args->retained = ret;
770                 ret = 0;
771         }
772
773         drm_gem_object_unreference(obj);
774
775 unlock:
776         mutex_unlock(&dev->struct_mutex);
777         return ret;
778 }
779
780 static const struct drm_ioctl_desc msm_ioctls[] = {
781         DRM_IOCTL_DEF_DRV(MSM_GET_PARAM,    msm_ioctl_get_param,    DRM_AUTH|DRM_RENDER_ALLOW),
782         DRM_IOCTL_DEF_DRV(MSM_GEM_NEW,      msm_ioctl_gem_new,      DRM_AUTH|DRM_RENDER_ALLOW),
783         DRM_IOCTL_DEF_DRV(MSM_GEM_INFO,     msm_ioctl_gem_info,     DRM_AUTH|DRM_RENDER_ALLOW),
784         DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
785         DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
786         DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT,   msm_ioctl_gem_submit,   DRM_AUTH|DRM_RENDER_ALLOW),
787         DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE,   msm_ioctl_wait_fence,   DRM_AUTH|DRM_RENDER_ALLOW),
788         DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE,  msm_ioctl_gem_madvise,  DRM_AUTH|DRM_RENDER_ALLOW),
789 };
790
791 static const struct vm_operations_struct vm_ops = {
792         .fault = msm_gem_fault,
793         .open = drm_gem_vm_open,
794         .close = drm_gem_vm_close,
795 };
796
797 static const struct file_operations fops = {
798         .owner              = THIS_MODULE,
799         .open               = drm_open,
800         .release            = drm_release,
801         .unlocked_ioctl     = drm_ioctl,
802         .compat_ioctl       = drm_compat_ioctl,
803         .poll               = drm_poll,
804         .read               = drm_read,
805         .llseek             = no_llseek,
806         .mmap               = msm_gem_mmap,
807 };
808
809 static struct drm_driver msm_driver = {
810         .driver_features    = DRIVER_HAVE_IRQ |
811                                 DRIVER_GEM |
812                                 DRIVER_PRIME |
813                                 DRIVER_RENDER |
814                                 DRIVER_ATOMIC |
815                                 DRIVER_MODESET,
816         .open               = msm_open,
817         .postclose           = msm_postclose,
818         .lastclose          = msm_lastclose,
819         .irq_handler        = msm_irq,
820         .irq_preinstall     = msm_irq_preinstall,
821         .irq_postinstall    = msm_irq_postinstall,
822         .irq_uninstall      = msm_irq_uninstall,
823         .enable_vblank      = msm_enable_vblank,
824         .disable_vblank     = msm_disable_vblank,
825         .gem_free_object    = msm_gem_free_object,
826         .gem_vm_ops         = &vm_ops,
827         .dumb_create        = msm_gem_dumb_create,
828         .dumb_map_offset    = msm_gem_dumb_map_offset,
829         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
830         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
831         .gem_prime_export   = drm_gem_prime_export,
832         .gem_prime_import   = drm_gem_prime_import,
833         .gem_prime_res_obj  = msm_gem_prime_res_obj,
834         .gem_prime_pin      = msm_gem_prime_pin,
835         .gem_prime_unpin    = msm_gem_prime_unpin,
836         .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
837         .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
838         .gem_prime_vmap     = msm_gem_prime_vmap,
839         .gem_prime_vunmap   = msm_gem_prime_vunmap,
840         .gem_prime_mmap     = msm_gem_prime_mmap,
841 #ifdef CONFIG_DEBUG_FS
842         .debugfs_init       = msm_debugfs_init,
843 #endif
844         .ioctls             = msm_ioctls,
845         .num_ioctls         = ARRAY_SIZE(msm_ioctls),
846         .fops               = &fops,
847         .name               = "msm",
848         .desc               = "MSM Snapdragon DRM",
849         .date               = "20130625",
850         .major              = MSM_VERSION_MAJOR,
851         .minor              = MSM_VERSION_MINOR,
852         .patchlevel         = MSM_VERSION_PATCHLEVEL,
853 };
854
855 #ifdef CONFIG_PM_SLEEP
856 static int msm_pm_suspend(struct device *dev)
857 {
858         struct drm_device *ddev = dev_get_drvdata(dev);
859
860         drm_kms_helper_poll_disable(ddev);
861
862         return 0;
863 }
864
865 static int msm_pm_resume(struct device *dev)
866 {
867         struct drm_device *ddev = dev_get_drvdata(dev);
868
869         drm_kms_helper_poll_enable(ddev);
870
871         return 0;
872 }
873 #endif
874
875 #ifdef CONFIG_PM
876 static int msm_runtime_suspend(struct device *dev)
877 {
878         struct drm_device *ddev = dev_get_drvdata(dev);
879         struct msm_drm_private *priv = ddev->dev_private;
880
881         DBG("");
882
883         if (priv->mdss)
884                 return msm_mdss_disable(priv->mdss);
885
886         return 0;
887 }
888
889 static int msm_runtime_resume(struct device *dev)
890 {
891         struct drm_device *ddev = dev_get_drvdata(dev);
892         struct msm_drm_private *priv = ddev->dev_private;
893
894         DBG("");
895
896         if (priv->mdss)
897                 return msm_mdss_enable(priv->mdss);
898
899         return 0;
900 }
901 #endif
902
903 static const struct dev_pm_ops msm_pm_ops = {
904         SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
905         SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
906 };
907
908 /*
909  * Componentized driver support:
910  */
911
912 /*
913  * NOTE: duplication of the same code as exynos or imx (or probably any other).
914  * so probably some room for some helpers
915  */
916 static int compare_of(struct device *dev, void *data)
917 {
918         return dev->of_node == data;
919 }
920
921 /*
922  * Identify what components need to be added by parsing what remote-endpoints
923  * our MDP output ports are connected to. In the case of LVDS on MDP4, there
924  * is no external component that we need to add since LVDS is within MDP4
925  * itself.
926  */
927 static int add_components_mdp(struct device *mdp_dev,
928                               struct component_match **matchptr)
929 {
930         struct device_node *np = mdp_dev->of_node;
931         struct device_node *ep_node;
932         struct device *master_dev;
933
934         /*
935          * on MDP4 based platforms, the MDP platform device is the component
936          * master that adds other display interface components to itself.
937          *
938          * on MDP5 based platforms, the MDSS platform device is the component
939          * master that adds MDP5 and other display interface components to
940          * itself.
941          */
942         if (of_device_is_compatible(np, "qcom,mdp4"))
943                 master_dev = mdp_dev;
944         else
945                 master_dev = mdp_dev->parent;
946
947         for_each_endpoint_of_node(np, ep_node) {
948                 struct device_node *intf;
949                 struct of_endpoint ep;
950                 int ret;
951
952                 ret = of_graph_parse_endpoint(ep_node, &ep);
953                 if (ret) {
954                         dev_err(mdp_dev, "unable to parse port endpoint\n");
955                         of_node_put(ep_node);
956                         return ret;
957                 }
958
959                 /*
960                  * The LCDC/LVDS port on MDP4 is a speacial case where the
961                  * remote-endpoint isn't a component that we need to add
962                  */
963                 if (of_device_is_compatible(np, "qcom,mdp4") &&
964                     ep.port == 0)
965                         continue;
966
967                 /*
968                  * It's okay if some of the ports don't have a remote endpoint
969                  * specified. It just means that the port isn't connected to
970                  * any external interface.
971                  */
972                 intf = of_graph_get_remote_port_parent(ep_node);
973                 if (!intf)
974                         continue;
975
976                 drm_of_component_match_add(master_dev, matchptr, compare_of,
977                                            intf);
978                 of_node_put(intf);
979         }
980
981         return 0;
982 }
983
984 static int compare_name_mdp(struct device *dev, void *data)
985 {
986         return (strstr(dev_name(dev), "mdp") != NULL);
987 }
988
989 static int add_display_components(struct device *dev,
990                                   struct component_match **matchptr)
991 {
992         struct device *mdp_dev;
993         int ret;
994
995         /*
996          * MDP5 based devices don't have a flat hierarchy. There is a top level
997          * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
998          * children devices, find the MDP5 node, and then add the interfaces
999          * to our components list.
1000          */
1001         if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
1002                 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1003                 if (ret) {
1004                         dev_err(dev, "failed to populate children devices\n");
1005                         return ret;
1006                 }
1007
1008                 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1009                 if (!mdp_dev) {
1010                         dev_err(dev, "failed to find MDSS MDP node\n");
1011                         of_platform_depopulate(dev);
1012                         return -ENODEV;
1013                 }
1014
1015                 put_device(mdp_dev);
1016
1017                 /* add the MDP component itself */
1018                 drm_of_component_match_add(dev, matchptr, compare_of,
1019                                            mdp_dev->of_node);
1020         } else {
1021                 /* MDP4 */
1022                 mdp_dev = dev;
1023         }
1024
1025         ret = add_components_mdp(mdp_dev, matchptr);
1026         if (ret)
1027                 of_platform_depopulate(dev);
1028
1029         return ret;
1030 }
1031
1032 /*
1033  * We don't know what's the best binding to link the gpu with the drm device.
1034  * Fow now, we just hunt for all the possible gpus that we support, and add them
1035  * as components.
1036  */
1037 static const struct of_device_id msm_gpu_match[] = {
1038         { .compatible = "qcom,adreno" },
1039         { .compatible = "qcom,adreno-3xx" },
1040         { .compatible = "qcom,kgsl-3d0" },
1041         { },
1042 };
1043
1044 static int add_gpu_components(struct device *dev,
1045                               struct component_match **matchptr)
1046 {
1047         struct device_node *np;
1048
1049         np = of_find_matching_node(NULL, msm_gpu_match);
1050         if (!np)
1051                 return 0;
1052
1053         drm_of_component_match_add(dev, matchptr, compare_of, np);
1054
1055         of_node_put(np);
1056
1057         return 0;
1058 }
1059
1060 static int msm_drm_bind(struct device *dev)
1061 {
1062         return msm_drm_init(dev, &msm_driver);
1063 }
1064
1065 static void msm_drm_unbind(struct device *dev)
1066 {
1067         msm_drm_uninit(dev);
1068 }
1069
1070 static const struct component_master_ops msm_drm_ops = {
1071         .bind = msm_drm_bind,
1072         .unbind = msm_drm_unbind,
1073 };
1074
1075 /*
1076  * Platform driver:
1077  */
1078
1079 static int msm_pdev_probe(struct platform_device *pdev)
1080 {
1081         struct component_match *match = NULL;
1082         int ret;
1083
1084         ret = add_display_components(&pdev->dev, &match);
1085         if (ret)
1086                 return ret;
1087
1088         ret = add_gpu_components(&pdev->dev, &match);
1089         if (ret)
1090                 return ret;
1091
1092         /* on all devices that I am aware of, iommu's which can map
1093          * any address the cpu can see are used:
1094          */
1095         ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1096         if (ret)
1097                 return ret;
1098
1099         return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1100 }
1101
1102 static int msm_pdev_remove(struct platform_device *pdev)
1103 {
1104         component_master_del(&pdev->dev, &msm_drm_ops);
1105         of_platform_depopulate(&pdev->dev);
1106
1107         return 0;
1108 }
1109
1110 static const struct of_device_id dt_match[] = {
1111         { .compatible = "qcom,mdp4", .data = (void *)4 },       /* MDP4 */
1112         { .compatible = "qcom,mdss", .data = (void *)5 },       /* MDP5 MDSS */
1113         {}
1114 };
1115 MODULE_DEVICE_TABLE(of, dt_match);
1116
1117 static struct platform_driver msm_platform_driver = {
1118         .probe      = msm_pdev_probe,
1119         .remove     = msm_pdev_remove,
1120         .driver     = {
1121                 .name   = "msm",
1122                 .of_match_table = dt_match,
1123                 .pm     = &msm_pm_ops,
1124         },
1125 };
1126
1127 static int __init msm_drm_register(void)
1128 {
1129         if (!modeset)
1130                 return -EINVAL;
1131
1132         DBG("init");
1133         msm_mdp_register();
1134         msm_dsi_register();
1135         msm_edp_register();
1136         msm_hdmi_register();
1137         adreno_register();
1138         return platform_driver_register(&msm_platform_driver);
1139 }
1140
1141 static void __exit msm_drm_unregister(void)
1142 {
1143         DBG("fini");
1144         platform_driver_unregister(&msm_platform_driver);
1145         msm_hdmi_unregister();
1146         adreno_unregister();
1147         msm_edp_unregister();
1148         msm_dsi_unregister();
1149         msm_mdp_unregister();
1150 }
1151
1152 module_init(msm_drm_register);
1153 module_exit(msm_drm_unregister);
1154
1155 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1156 MODULE_DESCRIPTION("MSM DRM Driver");
1157 MODULE_LICENSE("GPL");