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