46876bc8b7077e9ef8ed7f7d116ae2f8a99f78fc
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / msm_drv.c
1 /*
2  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include <linux/kthread.h>
20 #include <uapi/linux/sched/types.h>
21 #include <drm/drm_of.h>
22
23 #include "msm_drv.h"
24 #include "msm_debugfs.h"
25 #include "msm_fence.h"
26 #include "msm_gpu.h"
27 #include "msm_kms.h"
28
29
30 /*
31  * MSM driver version:
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
37  *           MSM_GEM_INFO ioctl.
38  */
39 #define MSM_VERSION_MAJOR       1
40 #define MSM_VERSION_MINOR       3
41 #define MSM_VERSION_PATCHLEVEL  0
42
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,
48 };
49
50 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
51         .atomic_commit_tail = msm_atomic_commit_tail,
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 kthread_work *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         kthread_queue_work(&priv->disp_thread[crtc_id].worker,
204                         &vbl_ctrl->work);
205
206         return 0;
207 }
208
209 static int msm_drm_uninit(struct device *dev)
210 {
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;
218         int i;
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         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);
227                 kfree(vbl_ev);
228         }
229
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;
236                 }
237
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;
242                 }
243         }
244
245         msm_gem_shrinker_cleanup(ddev);
246
247         drm_kms_helper_poll_fini(ddev);
248
249         drm_dev_unregister(ddev);
250
251         msm_perf_debugfs_cleanup(priv);
252         msm_rd_debugfs_cleanup(priv);
253
254 #ifdef CONFIG_DRM_FBDEV_EMULATION
255         if (fbdev && priv->fbdev)
256                 msm_fbdev_free(ddev);
257 #endif
258         drm_mode_config_cleanup(ddev);
259
260         pm_runtime_get_sync(dev);
261         drm_irq_uninstall(ddev);
262         pm_runtime_put_sync(dev);
263
264         flush_workqueue(priv->wq);
265         destroy_workqueue(priv->wq);
266
267         if (kms && kms->funcs)
268                 kms->funcs->destroy(kms);
269
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);
275         }
276
277         component_unbind_all(dev, ddev);
278
279         if (mdss && mdss->funcs)
280                 mdss->funcs->destroy(ddev);
281
282         ddev->dev_private = NULL;
283         drm_dev_unref(ddev);
284
285         kfree(priv);
286
287         return 0;
288 }
289
290 #define KMS_MDP4 4
291 #define KMS_MDP5 5
292 #define KMS_DPU  3
293
294 static int get_mdp_ver(struct platform_device *pdev)
295 {
296         struct device *dev = &pdev->dev;
297
298         return (int) (unsigned long) of_device_get_match_data(dev);
299 }
300
301 #include <linux/of_address.h>
302
303 static int msm_init_vram(struct drm_device *dev)
304 {
305         struct msm_drm_private *priv = dev->dev_private;
306         struct device_node *node;
307         unsigned long size = 0;
308         int ret = 0;
309
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
314          * are two cases:
315          *  1) device with no IOMMU, in which case we need exclusive
316          *     access to a VRAM carveout big enough for all gpu
317          *     buffers
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)
325          */
326
327         node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
328         if (node) {
329                 struct resource r;
330                 ret = of_address_to_resource(node, 0, &r);
331                 of_node_put(node);
332                 if (ret)
333                         return ret;
334                 size = r.end - r.start;
335                 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
336
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
339                  * mach-msm:
340                  */
341         } else if (!iommu_present(&platform_bus_type)) {
342                 DRM_INFO("using %s VRAM carveout\n", vram);
343                 size = memparse(vram, NULL);
344         }
345
346         if (size) {
347                 unsigned long attrs = 0;
348                 void *p;
349
350                 priv->vram.size = size;
351
352                 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
353                 spin_lock_init(&priv->vram.lock);
354
355                 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
356                 attrs |= DMA_ATTR_WRITE_COMBINE;
357
358                 /* note that for no-kernel-mapping, the vaddr returned
359                  * is bogus, but non-null if allocation succeeded:
360                  */
361                 p = dma_alloc_attrs(dev->dev, size,
362                                 &priv->vram.paddr, GFP_KERNEL, attrs);
363                 if (!p) {
364                         dev_err(dev->dev, "failed to allocate VRAM\n");
365                         priv->vram.paddr = 0;
366                         return -ENOMEM;
367                 }
368
369                 dev_info(dev->dev, "VRAM: %08x->%08x\n",
370                                 (uint32_t)priv->vram.paddr,
371                                 (uint32_t)(priv->vram.paddr + size));
372         }
373
374         return ret;
375 }
376
377 static int msm_drm_init(struct device *dev, struct drm_driver *drv)
378 {
379         struct platform_device *pdev = to_platform_device(dev);
380         struct drm_device *ddev;
381         struct msm_drm_private *priv;
382         struct msm_kms *kms;
383         struct msm_mdss *mdss;
384         int ret, i;
385         struct sched_param param;
386
387         ddev = drm_dev_alloc(drv, dev);
388         if (IS_ERR(ddev)) {
389                 dev_err(dev, "failed to allocate drm_device\n");
390                 return PTR_ERR(ddev);
391         }
392
393         platform_set_drvdata(pdev, ddev);
394
395         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
396         if (!priv) {
397                 ret = -ENOMEM;
398                 goto err_unref_drm_dev;
399         }
400
401         ddev->dev_private = priv;
402         priv->dev = ddev;
403
404         switch (get_mdp_ver(pdev)) {
405         case KMS_MDP5:
406                 ret = mdp5_mdss_init(ddev);
407                 break;
408         case KMS_DPU:
409                 ret = dpu_mdss_init(ddev);
410                 break;
411         default:
412                 ret = 0;
413                 break;
414         }
415         if (ret)
416                 goto err_free_priv;
417
418         mdss = priv->mdss;
419
420         priv->wq = alloc_ordered_workqueue("msm", 0);
421
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);
426
427         drm_mode_config_init(ddev);
428
429         /* Bind all our sub-components: */
430         ret = component_bind_all(dev, ddev);
431         if (ret)
432                 goto err_destroy_mdss;
433
434         ret = msm_init_vram(ddev);
435         if (ret)
436                 goto err_msm_uninit;
437
438         msm_gem_shrinker_init(ddev);
439
440         switch (get_mdp_ver(pdev)) {
441         case KMS_MDP4:
442                 kms = mdp4_kms_init(ddev);
443                 priv->kms = kms;
444                 break;
445         case KMS_MDP5:
446                 kms = mdp5_kms_init(ddev);
447                 break;
448         case KMS_DPU:
449                 kms = dpu_kms_init(ddev);
450                 priv->kms = kms;
451                 break;
452         default:
453                 kms = ERR_PTR(-ENODEV);
454                 break;
455         }
456
457         if (IS_ERR(kms)) {
458                 /*
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
463                  */
464                 dev_err(dev, "failed to load kms\n");
465                 ret = PTR_ERR(kms);
466                 goto err_msm_uninit;
467         }
468
469         /* Enable normalization of plane zpos */
470         ddev->mode_config.normalize_zpos = true;
471
472         if (kms) {
473                 ret = kms->funcs->hw_init(kms);
474                 if (ret) {
475                         dev_err(dev, "kms hw init failed: %d\n", ret);
476                         goto err_msm_uninit;
477                 }
478         }
479
480         ddev->mode_config.funcs = &mode_config_funcs;
481         ddev->mode_config.helper_private = &mode_config_helper_funcs;
482
483         /**
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
487          */
488         param.sched_priority = 16;
489         for (i = 0; i < priv->num_crtcs; i++) {
490
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,
500                                                         SCHED_FIFO, &param);
501                 if (ret)
502                         pr_warn("display thread priority update failed: %d\n",
503                                                                         ret);
504
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;
508                 }
509
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);
518                 /**
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.
524                  */
525                 ret = sched_setscheduler(priv->event_thread[i].thread,
526                                                         SCHED_FIFO, &param);
527                 if (ret)
528                         pr_warn("display event thread priority update failed: %d\n",
529                                                                         ret);
530
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;
534                 }
535
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) {
541                                         kthread_stop(
542                                                 priv->disp_thread[i].thread);
543                                         priv->disp_thread[i].thread = NULL;
544                                 }
545
546                                 if (priv->event_thread[i].thread) {
547                                         kthread_stop(
548                                                 priv->event_thread[i].thread);
549                                         priv->event_thread[i].thread = NULL;
550                                 }
551                         }
552                         goto err_msm_uninit;
553                 }
554         }
555
556         ret = drm_vblank_init(ddev, priv->num_crtcs);
557         if (ret < 0) {
558                 dev_err(dev, "failed to initialize vblank\n");
559                 goto err_msm_uninit;
560         }
561
562         if (kms) {
563                 pm_runtime_get_sync(dev);
564                 ret = drm_irq_install(ddev, kms->irq);
565                 pm_runtime_put_sync(dev);
566                 if (ret < 0) {
567                         dev_err(dev, "failed to install IRQ handler\n");
568                         goto err_msm_uninit;
569                 }
570         }
571
572         ret = drm_dev_register(ddev, 0);
573         if (ret)
574                 goto err_msm_uninit;
575
576         drm_mode_config_reset(ddev);
577
578 #ifdef CONFIG_DRM_FBDEV_EMULATION
579         if (fbdev)
580                 priv->fbdev = msm_fbdev_init(ddev);
581 #endif
582
583         ret = msm_debugfs_late_init(ddev);
584         if (ret)
585                 goto err_msm_uninit;
586
587         drm_kms_helper_poll_init(ddev);
588
589         return 0;
590
591 err_msm_uninit:
592         msm_drm_uninit(dev);
593         return ret;
594 err_destroy_mdss:
595         if (mdss && mdss->funcs)
596                 mdss->funcs->destroy(ddev);
597 err_free_priv:
598         kfree(priv);
599 err_unref_drm_dev:
600         drm_dev_unref(ddev);
601         return ret;
602 }
603
604 /*
605  * DRM operations:
606  */
607
608 static void load_gpu(struct drm_device *dev)
609 {
610         static DEFINE_MUTEX(init_lock);
611         struct msm_drm_private *priv = dev->dev_private;
612
613         mutex_lock(&init_lock);
614
615         if (!priv->gpu)
616                 priv->gpu = adreno_load_gpu(dev);
617
618         mutex_unlock(&init_lock);
619 }
620
621 static int context_init(struct drm_device *dev, struct drm_file *file)
622 {
623         struct msm_file_private *ctx;
624
625         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
626         if (!ctx)
627                 return -ENOMEM;
628
629         msm_submitqueue_init(dev, ctx);
630
631         file->driver_priv = ctx;
632
633         return 0;
634 }
635
636 static int msm_open(struct drm_device *dev, struct drm_file *file)
637 {
638         /* For now, load gpu on open.. to avoid the requirement of having
639          * firmware in the initrd.
640          */
641         load_gpu(dev);
642
643         return context_init(dev, file);
644 }
645
646 static void context_close(struct msm_file_private *ctx)
647 {
648         msm_submitqueue_close(ctx);
649         kfree(ctx);
650 }
651
652 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
653 {
654         struct msm_drm_private *priv = dev->dev_private;
655         struct msm_file_private *ctx = file->driver_priv;
656
657         mutex_lock(&dev->struct_mutex);
658         if (ctx == priv->lastctx)
659                 priv->lastctx = NULL;
660         mutex_unlock(&dev->struct_mutex);
661
662         context_close(ctx);
663 }
664
665 static irqreturn_t msm_irq(int irq, void *arg)
666 {
667         struct drm_device *dev = arg;
668         struct msm_drm_private *priv = dev->dev_private;
669         struct msm_kms *kms = priv->kms;
670         BUG_ON(!kms);
671         return kms->funcs->irq(kms);
672 }
673
674 static void msm_irq_preinstall(struct drm_device *dev)
675 {
676         struct msm_drm_private *priv = dev->dev_private;
677         struct msm_kms *kms = priv->kms;
678         BUG_ON(!kms);
679         kms->funcs->irq_preinstall(kms);
680 }
681
682 static int msm_irq_postinstall(struct drm_device *dev)
683 {
684         struct msm_drm_private *priv = dev->dev_private;
685         struct msm_kms *kms = priv->kms;
686         BUG_ON(!kms);
687         return kms->funcs->irq_postinstall(kms);
688 }
689
690 static void msm_irq_uninstall(struct drm_device *dev)
691 {
692         struct msm_drm_private *priv = dev->dev_private;
693         struct msm_kms *kms = priv->kms;
694         BUG_ON(!kms);
695         kms->funcs->irq_uninstall(kms);
696 }
697
698 static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
699 {
700         struct msm_drm_private *priv = dev->dev_private;
701         struct msm_kms *kms = priv->kms;
702         if (!kms)
703                 return -ENXIO;
704         DBG("dev=%p, crtc=%u", dev, pipe);
705         return vblank_ctrl_queue_work(priv, pipe, true);
706 }
707
708 static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
709 {
710         struct msm_drm_private *priv = dev->dev_private;
711         struct msm_kms *kms = priv->kms;
712         if (!kms)
713                 return;
714         DBG("dev=%p, crtc=%u", dev, pipe);
715         vblank_ctrl_queue_work(priv, pipe, false);
716 }
717
718 /*
719  * DRM ioctls:
720  */
721
722 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
723                 struct drm_file *file)
724 {
725         struct msm_drm_private *priv = dev->dev_private;
726         struct drm_msm_param *args = data;
727         struct msm_gpu *gpu;
728
729         /* for now, we just have 3d pipe.. eventually this would need to
730          * be more clever to dispatch to appropriate gpu module:
731          */
732         if (args->pipe != MSM_PIPE_3D0)
733                 return -EINVAL;
734
735         gpu = priv->gpu;
736
737         if (!gpu)
738                 return -ENXIO;
739
740         return gpu->funcs->get_param(gpu, args->param, &args->value);
741 }
742
743 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
744                 struct drm_file *file)
745 {
746         struct drm_msm_gem_new *args = data;
747
748         if (args->flags & ~MSM_BO_FLAGS) {
749                 DRM_ERROR("invalid flags: %08x\n", args->flags);
750                 return -EINVAL;
751         }
752
753         return msm_gem_new_handle(dev, file, args->size,
754                         args->flags, &args->handle);
755 }
756
757 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
758 {
759         return ktime_set(timeout.tv_sec, timeout.tv_nsec);
760 }
761
762 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
763                 struct drm_file *file)
764 {
765         struct drm_msm_gem_cpu_prep *args = data;
766         struct drm_gem_object *obj;
767         ktime_t timeout = to_ktime(args->timeout);
768         int ret;
769
770         if (args->op & ~MSM_PREP_FLAGS) {
771                 DRM_ERROR("invalid op: %08x\n", args->op);
772                 return -EINVAL;
773         }
774
775         obj = drm_gem_object_lookup(file, args->handle);
776         if (!obj)
777                 return -ENOENT;
778
779         ret = msm_gem_cpu_prep(obj, args->op, &timeout);
780
781         drm_gem_object_put_unlocked(obj);
782
783         return ret;
784 }
785
786 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
787                 struct drm_file *file)
788 {
789         struct drm_msm_gem_cpu_fini *args = data;
790         struct drm_gem_object *obj;
791         int ret;
792
793         obj = drm_gem_object_lookup(file, args->handle);
794         if (!obj)
795                 return -ENOENT;
796
797         ret = msm_gem_cpu_fini(obj);
798
799         drm_gem_object_put_unlocked(obj);
800
801         return ret;
802 }
803
804 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
805                 struct drm_gem_object *obj, uint64_t *iova)
806 {
807         struct msm_drm_private *priv = dev->dev_private;
808
809         if (!priv->gpu)
810                 return -EINVAL;
811
812         return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
813 }
814
815 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
816                 struct drm_file *file)
817 {
818         struct drm_msm_gem_info *args = data;
819         struct drm_gem_object *obj;
820         int ret = 0;
821
822         if (args->flags & ~MSM_INFO_FLAGS)
823                 return -EINVAL;
824
825         obj = drm_gem_object_lookup(file, args->handle);
826         if (!obj)
827                 return -ENOENT;
828
829         if (args->flags & MSM_INFO_IOVA) {
830                 uint64_t iova;
831
832                 ret = msm_ioctl_gem_info_iova(dev, obj, &iova);
833                 if (!ret)
834                         args->offset = iova;
835         } else {
836                 args->offset = msm_gem_mmap_offset(obj);
837         }
838
839         drm_gem_object_put_unlocked(obj);
840
841         return ret;
842 }
843
844 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
845                 struct drm_file *file)
846 {
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;
852         int ret;
853
854         if (args->pad) {
855                 DRM_ERROR("invalid pad: %08x\n", args->pad);
856                 return -EINVAL;
857         }
858
859         if (!gpu)
860                 return 0;
861
862         queue = msm_submitqueue_get(file->driver_priv, args->queueid);
863         if (!queue)
864                 return -ENOENT;
865
866         ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
867                 true);
868
869         msm_submitqueue_put(queue);
870         return ret;
871 }
872
873 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
874                 struct drm_file *file)
875 {
876         struct drm_msm_gem_madvise *args = data;
877         struct drm_gem_object *obj;
878         int ret;
879
880         switch (args->madv) {
881         case MSM_MADV_DONTNEED:
882         case MSM_MADV_WILLNEED:
883                 break;
884         default:
885                 return -EINVAL;
886         }
887
888         ret = mutex_lock_interruptible(&dev->struct_mutex);
889         if (ret)
890                 return ret;
891
892         obj = drm_gem_object_lookup(file, args->handle);
893         if (!obj) {
894                 ret = -ENOENT;
895                 goto unlock;
896         }
897
898         ret = msm_gem_madvise(obj, args->madv);
899         if (ret >= 0) {
900                 args->retained = ret;
901                 ret = 0;
902         }
903
904         drm_gem_object_put(obj);
905
906 unlock:
907         mutex_unlock(&dev->struct_mutex);
908         return ret;
909 }
910
911
912 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
913                 struct drm_file *file)
914 {
915         struct drm_msm_submitqueue *args = data;
916
917         if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
918                 return -EINVAL;
919
920         return msm_submitqueue_create(dev, file->driver_priv, args->prio,
921                 args->flags, &args->id);
922 }
923
924
925 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
926                 struct drm_file *file)
927 {
928         u32 id = *(u32 *) data;
929
930         return msm_submitqueue_remove(file->driver_priv, id);
931 }
932
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),
944 };
945
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,
950 };
951
952 static const struct file_operations fops = {
953         .owner              = THIS_MODULE,
954         .open               = drm_open,
955         .release            = drm_release,
956         .unlocked_ioctl     = drm_ioctl,
957         .compat_ioctl       = drm_compat_ioctl,
958         .poll               = drm_poll,
959         .read               = drm_read,
960         .llseek             = no_llseek,
961         .mmap               = msm_gem_mmap,
962 };
963
964 static struct drm_driver msm_driver = {
965         .driver_features    = DRIVER_HAVE_IRQ |
966                                 DRIVER_GEM |
967                                 DRIVER_PRIME |
968                                 DRIVER_RENDER |
969                                 DRIVER_ATOMIC |
970                                 DRIVER_MODESET,
971         .open               = msm_open,
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,
998 #endif
999         .ioctls             = msm_ioctls,
1000         .num_ioctls         = ARRAY_SIZE(msm_ioctls),
1001         .fops               = &fops,
1002         .name               = "msm",
1003         .desc               = "MSM Snapdragon DRM",
1004         .date               = "20130625",
1005         .major              = MSM_VERSION_MAJOR,
1006         .minor              = MSM_VERSION_MINOR,
1007         .patchlevel         = MSM_VERSION_PATCHLEVEL,
1008 };
1009
1010 #ifdef CONFIG_PM_SLEEP
1011 static int msm_pm_suspend(struct device *dev)
1012 {
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;
1016
1017         /* TODO: Use atomic helper suspend/resume */
1018         if (kms && kms->funcs && kms->funcs->pm_suspend)
1019                 return kms->funcs->pm_suspend(dev);
1020
1021         drm_kms_helper_poll_disable(ddev);
1022
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);
1027         }
1028
1029         return 0;
1030 }
1031
1032 static int msm_pm_resume(struct device *dev)
1033 {
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;
1037
1038         /* TODO: Use atomic helper suspend/resume */
1039         if (kms && kms->funcs && kms->funcs->pm_resume)
1040                 return kms->funcs->pm_resume(dev);
1041
1042         drm_atomic_helper_resume(ddev, priv->pm_state);
1043         drm_kms_helper_poll_enable(ddev);
1044
1045         return 0;
1046 }
1047 #endif
1048
1049 #ifdef CONFIG_PM
1050 static int msm_runtime_suspend(struct device *dev)
1051 {
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;
1055
1056         DBG("");
1057
1058         if (mdss && mdss->funcs)
1059                 return mdss->funcs->disable(mdss);
1060
1061         return 0;
1062 }
1063
1064 static int msm_runtime_resume(struct device *dev)
1065 {
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;
1069
1070         DBG("");
1071
1072         if (mdss && mdss->funcs)
1073                 return mdss->funcs->enable(mdss);
1074
1075         return 0;
1076 }
1077 #endif
1078
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)
1082 };
1083
1084 /*
1085  * Componentized driver support:
1086  */
1087
1088 /*
1089  * NOTE: duplication of the same code as exynos or imx (or probably any other).
1090  * so probably some room for some helpers
1091  */
1092 static int compare_of(struct device *dev, void *data)
1093 {
1094         return dev->of_node == data;
1095 }
1096
1097 /*
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
1101  * itself.
1102  */
1103 static int add_components_mdp(struct device *mdp_dev,
1104                               struct component_match **matchptr)
1105 {
1106         struct device_node *np = mdp_dev->of_node;
1107         struct device_node *ep_node;
1108         struct device *master_dev;
1109
1110         /*
1111          * on MDP4 based platforms, the MDP platform device is the component
1112          * master that adds other display interface components to itself.
1113          *
1114          * on MDP5 based platforms, the MDSS platform device is the component
1115          * master that adds MDP5 and other display interface components to
1116          * itself.
1117          */
1118         if (of_device_is_compatible(np, "qcom,mdp4"))
1119                 master_dev = mdp_dev;
1120         else
1121                 master_dev = mdp_dev->parent;
1122
1123         for_each_endpoint_of_node(np, ep_node) {
1124                 struct device_node *intf;
1125                 struct of_endpoint ep;
1126                 int ret;
1127
1128                 ret = of_graph_parse_endpoint(ep_node, &ep);
1129                 if (ret) {
1130                         dev_err(mdp_dev, "unable to parse port endpoint\n");
1131                         of_node_put(ep_node);
1132                         return ret;
1133                 }
1134
1135                 /*
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
1138                  */
1139                 if (of_device_is_compatible(np, "qcom,mdp4") &&
1140                     ep.port == 0)
1141                         continue;
1142
1143                 /*
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.
1147                  */
1148                 intf = of_graph_get_remote_port_parent(ep_node);
1149                 if (!intf)
1150                         continue;
1151
1152                 drm_of_component_match_add(master_dev, matchptr, compare_of,
1153                                            intf);
1154                 of_node_put(intf);
1155         }
1156
1157         return 0;
1158 }
1159
1160 static int compare_name_mdp(struct device *dev, void *data)
1161 {
1162         return (strstr(dev_name(dev), "mdp") != NULL);
1163 }
1164
1165 static int add_display_components(struct device *dev,
1166                                   struct component_match **matchptr)
1167 {
1168         struct device *mdp_dev;
1169         int ret;
1170
1171         /*
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.
1176          */
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);
1180                 if (ret) {
1181                         dev_err(dev, "failed to populate children devices\n");
1182                         return ret;
1183                 }
1184
1185                 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1186                 if (!mdp_dev) {
1187                         dev_err(dev, "failed to find MDSS MDP node\n");
1188                         of_platform_depopulate(dev);
1189                         return -ENODEV;
1190                 }
1191
1192                 put_device(mdp_dev);
1193
1194                 /* add the MDP component itself */
1195                 drm_of_component_match_add(dev, matchptr, compare_of,
1196                                            mdp_dev->of_node);
1197         } else {
1198                 /* MDP4 */
1199                 mdp_dev = dev;
1200         }
1201
1202         ret = add_components_mdp(mdp_dev, matchptr);
1203         if (ret)
1204                 of_platform_depopulate(dev);
1205
1206         return ret;
1207 }
1208
1209 /*
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
1212  * as components.
1213  */
1214 static const struct of_device_id msm_gpu_match[] = {
1215         { .compatible = "qcom,adreno" },
1216         { .compatible = "qcom,adreno-3xx" },
1217         { .compatible = "qcom,kgsl-3d0" },
1218         { },
1219 };
1220
1221 static int add_gpu_components(struct device *dev,
1222                               struct component_match **matchptr)
1223 {
1224         struct device_node *np;
1225
1226         np = of_find_matching_node(NULL, msm_gpu_match);
1227         if (!np)
1228                 return 0;
1229
1230         drm_of_component_match_add(dev, matchptr, compare_of, np);
1231
1232         of_node_put(np);
1233
1234         return 0;
1235 }
1236
1237 static int msm_drm_bind(struct device *dev)
1238 {
1239         return msm_drm_init(dev, &msm_driver);
1240 }
1241
1242 static void msm_drm_unbind(struct device *dev)
1243 {
1244         msm_drm_uninit(dev);
1245 }
1246
1247 static const struct component_master_ops msm_drm_ops = {
1248         .bind = msm_drm_bind,
1249         .unbind = msm_drm_unbind,
1250 };
1251
1252 /*
1253  * Platform driver:
1254  */
1255
1256 static int msm_pdev_probe(struct platform_device *pdev)
1257 {
1258         struct component_match *match = NULL;
1259         int ret;
1260
1261         ret = add_display_components(&pdev->dev, &match);
1262         if (ret)
1263                 return ret;
1264
1265         ret = add_gpu_components(&pdev->dev, &match);
1266         if (ret)
1267                 return ret;
1268
1269         /* on all devices that I am aware of, iommu's which can map
1270          * any address the cpu can see are used:
1271          */
1272         ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1273         if (ret)
1274                 return ret;
1275
1276         return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1277 }
1278
1279 static int msm_pdev_remove(struct platform_device *pdev)
1280 {
1281         component_master_del(&pdev->dev, &msm_drm_ops);
1282         of_platform_depopulate(&pdev->dev);
1283
1284         return 0;
1285 }
1286
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 },
1291         {}
1292 };
1293 MODULE_DEVICE_TABLE(of, dt_match);
1294
1295 static struct platform_driver msm_platform_driver = {
1296         .probe      = msm_pdev_probe,
1297         .remove     = msm_pdev_remove,
1298         .driver     = {
1299                 .name   = "msm",
1300                 .of_match_table = dt_match,
1301                 .pm     = &msm_pm_ops,
1302         },
1303 };
1304
1305 static int __init msm_drm_register(void)
1306 {
1307         if (!modeset)
1308                 return -EINVAL;
1309
1310         DBG("init");
1311         msm_mdp_register();
1312         msm_dpu_register();
1313         msm_dsi_register();
1314         msm_edp_register();
1315         msm_hdmi_register();
1316         adreno_register();
1317         return platform_driver_register(&msm_platform_driver);
1318 }
1319
1320 static void __exit msm_drm_unregister(void)
1321 {
1322         DBG("fini");
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();
1330 }
1331
1332 module_init(msm_drm_register);
1333 module_exit(msm_drm_unregister);
1334
1335 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1336 MODULE_DESCRIPTION("MSM DRM Driver");
1337 MODULE_LICENSE("GPL");