Merge tag 'devicetree-fixes-for-5.13-2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / msm_drv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7
8 #include <linux/dma-mapping.h>
9 #include <linux/kthread.h>
10 #include <linux/sched/mm.h>
11 #include <linux/uaccess.h>
12 #include <uapi/linux/sched/types.h>
13
14 #include <drm/drm_drv.h>
15 #include <drm/drm_file.h>
16 #include <drm/drm_ioctl.h>
17 #include <drm/drm_irq.h>
18 #include <drm/drm_prime.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_vblank.h>
21
22 #include "msm_drv.h"
23 #include "msm_debugfs.h"
24 #include "msm_fence.h"
25 #include "msm_gem.h"
26 #include "msm_gpu.h"
27 #include "msm_kms.h"
28 #include "adreno/adreno_gpu.h"
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  * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
39  *           GEM object's debug name
40  * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl
41  * - 1.6.0 - Syncobj support
42  * - 1.7.0 - Add MSM_PARAM_SUSPENDS to access suspend count
43  */
44 #define MSM_VERSION_MAJOR       1
45 #define MSM_VERSION_MINOR       7
46 #define MSM_VERSION_PATCHLEVEL  0
47
48 static const struct drm_mode_config_funcs mode_config_funcs = {
49         .fb_create = msm_framebuffer_create,
50         .output_poll_changed = drm_fb_helper_output_poll_changed,
51         .atomic_check = drm_atomic_helper_check,
52         .atomic_commit = drm_atomic_helper_commit,
53 };
54
55 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
56         .atomic_commit_tail = msm_atomic_commit_tail,
57 };
58
59 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
60 static bool reglog = false;
61 MODULE_PARM_DESC(reglog, "Enable register read/write logging");
62 module_param(reglog, bool, 0600);
63 #else
64 #define reglog 0
65 #endif
66
67 #ifdef CONFIG_DRM_FBDEV_EMULATION
68 static bool fbdev = true;
69 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
70 module_param(fbdev, bool, 0600);
71 #endif
72
73 static char *vram = "16m";
74 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
75 module_param(vram, charp, 0);
76
77 bool dumpstate = false;
78 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
79 module_param(dumpstate, bool, 0600);
80
81 static bool modeset = true;
82 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
83 module_param(modeset, bool, 0600);
84
85 /*
86  * Util/helpers:
87  */
88
89 struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
90                 const char *name)
91 {
92         int i;
93         char n[32];
94
95         snprintf(n, sizeof(n), "%s_clk", name);
96
97         for (i = 0; bulk && i < count; i++) {
98                 if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n))
99                         return bulk[i].clk;
100         }
101
102
103         return NULL;
104 }
105
106 struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
107 {
108         struct clk *clk;
109         char name2[32];
110
111         clk = devm_clk_get(&pdev->dev, name);
112         if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
113                 return clk;
114
115         snprintf(name2, sizeof(name2), "%s_clk", name);
116
117         clk = devm_clk_get(&pdev->dev, name2);
118         if (!IS_ERR(clk))
119                 dev_warn(&pdev->dev, "Using legacy clk name binding.  Use "
120                                 "\"%s\" instead of \"%s\"\n", name, name2);
121
122         return clk;
123 }
124
125 static void __iomem *_msm_ioremap(struct platform_device *pdev, const char *name,
126                                   const char *dbgname, bool quiet)
127 {
128         struct resource *res;
129         unsigned long size;
130         void __iomem *ptr;
131
132         if (name)
133                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
134         else
135                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
136
137         if (!res) {
138                 if (!quiet)
139                         DRM_DEV_ERROR(&pdev->dev, "failed to get memory resource: %s\n", name);
140                 return ERR_PTR(-EINVAL);
141         }
142
143         size = resource_size(res);
144
145         ptr = devm_ioremap(&pdev->dev, res->start, size);
146         if (!ptr) {
147                 if (!quiet)
148                         DRM_DEV_ERROR(&pdev->dev, "failed to ioremap: %s\n", name);
149                 return ERR_PTR(-ENOMEM);
150         }
151
152         if (reglog)
153                 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
154
155         return ptr;
156 }
157
158 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
159                           const char *dbgname)
160 {
161         return _msm_ioremap(pdev, name, dbgname, false);
162 }
163
164 void __iomem *msm_ioremap_quiet(struct platform_device *pdev, const char *name,
165                                 const char *dbgname)
166 {
167         return _msm_ioremap(pdev, name, dbgname, true);
168 }
169
170 void msm_writel(u32 data, void __iomem *addr)
171 {
172         if (reglog)
173                 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
174         writel(data, addr);
175 }
176
177 u32 msm_readl(const void __iomem *addr)
178 {
179         u32 val = readl(addr);
180         if (reglog)
181                 pr_err("IO:R %p %08x\n", addr, val);
182         return val;
183 }
184
185 void msm_rmw(void __iomem *addr, u32 mask, u32 or)
186 {
187         u32 val = msm_readl(addr);
188
189         val &= ~mask;
190         msm_writel(val | or, addr);
191 }
192
193 struct msm_vblank_work {
194         struct work_struct work;
195         int crtc_id;
196         bool enable;
197         struct msm_drm_private *priv;
198 };
199
200 static void vblank_ctrl_worker(struct work_struct *work)
201 {
202         struct msm_vblank_work *vbl_work = container_of(work,
203                                                 struct msm_vblank_work, work);
204         struct msm_drm_private *priv = vbl_work->priv;
205         struct msm_kms *kms = priv->kms;
206
207         if (vbl_work->enable)
208                 kms->funcs->enable_vblank(kms, priv->crtcs[vbl_work->crtc_id]);
209         else
210                 kms->funcs->disable_vblank(kms, priv->crtcs[vbl_work->crtc_id]);
211
212         kfree(vbl_work);
213 }
214
215 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
216                                         int crtc_id, bool enable)
217 {
218         struct msm_vblank_work *vbl_work;
219
220         vbl_work = kzalloc(sizeof(*vbl_work), GFP_ATOMIC);
221         if (!vbl_work)
222                 return -ENOMEM;
223
224         INIT_WORK(&vbl_work->work, vblank_ctrl_worker);
225
226         vbl_work->crtc_id = crtc_id;
227         vbl_work->enable = enable;
228         vbl_work->priv = priv;
229
230         queue_work(priv->wq, &vbl_work->work);
231
232         return 0;
233 }
234
235 static int msm_drm_uninit(struct device *dev)
236 {
237         struct platform_device *pdev = to_platform_device(dev);
238         struct drm_device *ddev = platform_get_drvdata(pdev);
239         struct msm_drm_private *priv = ddev->dev_private;
240         struct msm_kms *kms = priv->kms;
241         struct msm_mdss *mdss = priv->mdss;
242         int i;
243
244         /*
245          * Shutdown the hw if we're far enough along where things might be on.
246          * If we run this too early, we'll end up panicking in any variety of
247          * places. Since we don't register the drm device until late in
248          * msm_drm_init, drm_dev->registered is used as an indicator that the
249          * shutdown will be successful.
250          */
251         if (ddev->registered) {
252                 drm_dev_unregister(ddev);
253                 drm_atomic_helper_shutdown(ddev);
254         }
255
256         /* We must cancel and cleanup any pending vblank enable/disable
257          * work before drm_irq_uninstall() to avoid work re-enabling an
258          * irq after uninstall has disabled it.
259          */
260
261         flush_workqueue(priv->wq);
262
263         /* clean up event worker threads */
264         for (i = 0; i < priv->num_crtcs; i++) {
265                 if (priv->event_thread[i].worker)
266                         kthread_destroy_worker(priv->event_thread[i].worker);
267         }
268
269         msm_gem_shrinker_cleanup(ddev);
270
271         drm_kms_helper_poll_fini(ddev);
272
273         msm_perf_debugfs_cleanup(priv);
274         msm_rd_debugfs_cleanup(priv);
275
276 #ifdef CONFIG_DRM_FBDEV_EMULATION
277         if (fbdev && priv->fbdev)
278                 msm_fbdev_free(ddev);
279 #endif
280
281         drm_mode_config_cleanup(ddev);
282
283         pm_runtime_get_sync(dev);
284         drm_irq_uninstall(ddev);
285         pm_runtime_put_sync(dev);
286
287         if (kms && kms->funcs)
288                 kms->funcs->destroy(kms);
289
290         if (priv->vram.paddr) {
291                 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
292                 drm_mm_takedown(&priv->vram.mm);
293                 dma_free_attrs(dev, priv->vram.size, NULL,
294                                priv->vram.paddr, attrs);
295         }
296
297         component_unbind_all(dev, ddev);
298
299         if (mdss && mdss->funcs)
300                 mdss->funcs->destroy(ddev);
301
302         ddev->dev_private = NULL;
303         drm_dev_put(ddev);
304
305         destroy_workqueue(priv->wq);
306         kfree(priv);
307
308         return 0;
309 }
310
311 #define KMS_MDP4 4
312 #define KMS_MDP5 5
313 #define KMS_DPU  3
314
315 static int get_mdp_ver(struct platform_device *pdev)
316 {
317         struct device *dev = &pdev->dev;
318
319         return (int) (unsigned long) of_device_get_match_data(dev);
320 }
321
322 #include <linux/of_address.h>
323
324 bool msm_use_mmu(struct drm_device *dev)
325 {
326         struct msm_drm_private *priv = dev->dev_private;
327
328         /* a2xx comes with its own MMU */
329         return priv->is_a2xx || iommu_present(&platform_bus_type);
330 }
331
332 static int msm_init_vram(struct drm_device *dev)
333 {
334         struct msm_drm_private *priv = dev->dev_private;
335         struct device_node *node;
336         unsigned long size = 0;
337         int ret = 0;
338
339         /* In the device-tree world, we could have a 'memory-region'
340          * phandle, which gives us a link to our "vram".  Allocating
341          * is all nicely abstracted behind the dma api, but we need
342          * to know the entire size to allocate it all in one go. There
343          * are two cases:
344          *  1) device with no IOMMU, in which case we need exclusive
345          *     access to a VRAM carveout big enough for all gpu
346          *     buffers
347          *  2) device with IOMMU, but where the bootloader puts up
348          *     a splash screen.  In this case, the VRAM carveout
349          *     need only be large enough for fbdev fb.  But we need
350          *     exclusive access to the buffer to avoid the kernel
351          *     using those pages for other purposes (which appears
352          *     as corruption on screen before we have a chance to
353          *     load and do initial modeset)
354          */
355
356         node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
357         if (node) {
358                 struct resource r;
359                 ret = of_address_to_resource(node, 0, &r);
360                 of_node_put(node);
361                 if (ret)
362                         return ret;
363                 size = r.end - r.start;
364                 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
365
366                 /* if we have no IOMMU, then we need to use carveout allocator.
367                  * Grab the entire CMA chunk carved out in early startup in
368                  * mach-msm:
369                  */
370         } else if (!msm_use_mmu(dev)) {
371                 DRM_INFO("using %s VRAM carveout\n", vram);
372                 size = memparse(vram, NULL);
373         }
374
375         if (size) {
376                 unsigned long attrs = 0;
377                 void *p;
378
379                 priv->vram.size = size;
380
381                 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
382                 spin_lock_init(&priv->vram.lock);
383
384                 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
385                 attrs |= DMA_ATTR_WRITE_COMBINE;
386
387                 /* note that for no-kernel-mapping, the vaddr returned
388                  * is bogus, but non-null if allocation succeeded:
389                  */
390                 p = dma_alloc_attrs(dev->dev, size,
391                                 &priv->vram.paddr, GFP_KERNEL, attrs);
392                 if (!p) {
393                         DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
394                         priv->vram.paddr = 0;
395                         return -ENOMEM;
396                 }
397
398                 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
399                                 (uint32_t)priv->vram.paddr,
400                                 (uint32_t)(priv->vram.paddr + size));
401         }
402
403         return ret;
404 }
405
406 static int msm_drm_init(struct device *dev, const struct drm_driver *drv)
407 {
408         struct platform_device *pdev = to_platform_device(dev);
409         struct drm_device *ddev;
410         struct msm_drm_private *priv;
411         struct msm_kms *kms;
412         struct msm_mdss *mdss;
413         int ret, i;
414
415         ddev = drm_dev_alloc(drv, dev);
416         if (IS_ERR(ddev)) {
417                 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
418                 return PTR_ERR(ddev);
419         }
420
421         platform_set_drvdata(pdev, ddev);
422
423         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
424         if (!priv) {
425                 ret = -ENOMEM;
426                 goto err_put_drm_dev;
427         }
428
429         ddev->dev_private = priv;
430         priv->dev = ddev;
431
432         switch (get_mdp_ver(pdev)) {
433         case KMS_MDP5:
434                 ret = mdp5_mdss_init(ddev);
435                 break;
436         case KMS_DPU:
437                 ret = dpu_mdss_init(ddev);
438                 break;
439         default:
440                 ret = 0;
441                 break;
442         }
443         if (ret)
444                 goto err_free_priv;
445
446         mdss = priv->mdss;
447
448         priv->wq = alloc_ordered_workqueue("msm", 0);
449
450         INIT_LIST_HEAD(&priv->objects);
451         mutex_init(&priv->obj_lock);
452
453         INIT_LIST_HEAD(&priv->inactive_willneed);
454         INIT_LIST_HEAD(&priv->inactive_dontneed);
455         INIT_LIST_HEAD(&priv->inactive_unpinned);
456         mutex_init(&priv->mm_lock);
457
458         /* Teach lockdep about lock ordering wrt. shrinker: */
459         fs_reclaim_acquire(GFP_KERNEL);
460         might_lock(&priv->mm_lock);
461         fs_reclaim_release(GFP_KERNEL);
462
463         drm_mode_config_init(ddev);
464
465         ret = msm_init_vram(ddev);
466         if (ret)
467                 goto err_destroy_mdss;
468
469         /* Bind all our sub-components: */
470         ret = component_bind_all(dev, ddev);
471         if (ret)
472                 goto err_destroy_mdss;
473
474         dma_set_max_seg_size(dev, UINT_MAX);
475
476         msm_gem_shrinker_init(ddev);
477
478         switch (get_mdp_ver(pdev)) {
479         case KMS_MDP4:
480                 kms = mdp4_kms_init(ddev);
481                 priv->kms = kms;
482                 break;
483         case KMS_MDP5:
484                 kms = mdp5_kms_init(ddev);
485                 break;
486         case KMS_DPU:
487                 kms = dpu_kms_init(ddev);
488                 priv->kms = kms;
489                 break;
490         default:
491                 /* valid only for the dummy headless case, where of_node=NULL */
492                 WARN_ON(dev->of_node);
493                 kms = NULL;
494                 break;
495         }
496
497         if (IS_ERR(kms)) {
498                 DRM_DEV_ERROR(dev, "failed to load kms\n");
499                 ret = PTR_ERR(kms);
500                 priv->kms = NULL;
501                 goto err_msm_uninit;
502         }
503
504         /* Enable normalization of plane zpos */
505         ddev->mode_config.normalize_zpos = true;
506
507         if (kms) {
508                 kms->dev = ddev;
509                 ret = kms->funcs->hw_init(kms);
510                 if (ret) {
511                         DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
512                         goto err_msm_uninit;
513                 }
514         }
515
516         ddev->mode_config.funcs = &mode_config_funcs;
517         ddev->mode_config.helper_private = &mode_config_helper_funcs;
518
519         for (i = 0; i < priv->num_crtcs; i++) {
520                 /* initialize event thread */
521                 priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
522                 priv->event_thread[i].dev = ddev;
523                 priv->event_thread[i].worker = kthread_create_worker(0,
524                         "crtc_event:%d", priv->event_thread[i].crtc_id);
525                 if (IS_ERR(priv->event_thread[i].worker)) {
526                         DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n");
527                         goto err_msm_uninit;
528                 }
529
530                 sched_set_fifo(priv->event_thread[i].worker->task);
531         }
532
533         ret = drm_vblank_init(ddev, priv->num_crtcs);
534         if (ret < 0) {
535                 DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
536                 goto err_msm_uninit;
537         }
538
539         if (kms) {
540                 pm_runtime_get_sync(dev);
541                 ret = drm_irq_install(ddev, kms->irq);
542                 pm_runtime_put_sync(dev);
543                 if (ret < 0) {
544                         DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
545                         goto err_msm_uninit;
546                 }
547         }
548
549         ret = drm_dev_register(ddev, 0);
550         if (ret)
551                 goto err_msm_uninit;
552
553         drm_mode_config_reset(ddev);
554
555 #ifdef CONFIG_DRM_FBDEV_EMULATION
556         if (kms && fbdev)
557                 priv->fbdev = msm_fbdev_init(ddev);
558 #endif
559
560         ret = msm_debugfs_late_init(ddev);
561         if (ret)
562                 goto err_msm_uninit;
563
564         drm_kms_helper_poll_init(ddev);
565
566         return 0;
567
568 err_msm_uninit:
569         msm_drm_uninit(dev);
570         return ret;
571 err_destroy_mdss:
572         if (mdss && mdss->funcs)
573                 mdss->funcs->destroy(ddev);
574 err_free_priv:
575         kfree(priv);
576 err_put_drm_dev:
577         drm_dev_put(ddev);
578         platform_set_drvdata(pdev, NULL);
579         return ret;
580 }
581
582 /*
583  * DRM operations:
584  */
585
586 static void load_gpu(struct drm_device *dev)
587 {
588         static DEFINE_MUTEX(init_lock);
589         struct msm_drm_private *priv = dev->dev_private;
590
591         mutex_lock(&init_lock);
592
593         if (!priv->gpu)
594                 priv->gpu = adreno_load_gpu(dev);
595
596         mutex_unlock(&init_lock);
597 }
598
599 static int context_init(struct drm_device *dev, struct drm_file *file)
600 {
601         struct msm_drm_private *priv = dev->dev_private;
602         struct msm_file_private *ctx;
603
604         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
605         if (!ctx)
606                 return -ENOMEM;
607
608         kref_init(&ctx->ref);
609         msm_submitqueue_init(dev, ctx);
610
611         ctx->aspace = msm_gpu_create_private_address_space(priv->gpu, current);
612         file->driver_priv = ctx;
613
614         return 0;
615 }
616
617 static int msm_open(struct drm_device *dev, struct drm_file *file)
618 {
619         /* For now, load gpu on open.. to avoid the requirement of having
620          * firmware in the initrd.
621          */
622         load_gpu(dev);
623
624         return context_init(dev, file);
625 }
626
627 static void context_close(struct msm_file_private *ctx)
628 {
629         msm_submitqueue_close(ctx);
630         msm_file_private_put(ctx);
631 }
632
633 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
634 {
635         struct msm_drm_private *priv = dev->dev_private;
636         struct msm_file_private *ctx = file->driver_priv;
637
638         mutex_lock(&dev->struct_mutex);
639         if (ctx == priv->lastctx)
640                 priv->lastctx = NULL;
641         mutex_unlock(&dev->struct_mutex);
642
643         context_close(ctx);
644 }
645
646 static irqreturn_t msm_irq(int irq, void *arg)
647 {
648         struct drm_device *dev = arg;
649         struct msm_drm_private *priv = dev->dev_private;
650         struct msm_kms *kms = priv->kms;
651         BUG_ON(!kms);
652         return kms->funcs->irq(kms);
653 }
654
655 static void msm_irq_preinstall(struct drm_device *dev)
656 {
657         struct msm_drm_private *priv = dev->dev_private;
658         struct msm_kms *kms = priv->kms;
659         BUG_ON(!kms);
660         kms->funcs->irq_preinstall(kms);
661 }
662
663 static int msm_irq_postinstall(struct drm_device *dev)
664 {
665         struct msm_drm_private *priv = dev->dev_private;
666         struct msm_kms *kms = priv->kms;
667         BUG_ON(!kms);
668
669         if (kms->funcs->irq_postinstall)
670                 return kms->funcs->irq_postinstall(kms);
671
672         return 0;
673 }
674
675 static void msm_irq_uninstall(struct drm_device *dev)
676 {
677         struct msm_drm_private *priv = dev->dev_private;
678         struct msm_kms *kms = priv->kms;
679         BUG_ON(!kms);
680         kms->funcs->irq_uninstall(kms);
681 }
682
683 int msm_crtc_enable_vblank(struct drm_crtc *crtc)
684 {
685         struct drm_device *dev = crtc->dev;
686         unsigned int pipe = crtc->index;
687         struct msm_drm_private *priv = dev->dev_private;
688         struct msm_kms *kms = priv->kms;
689         if (!kms)
690                 return -ENXIO;
691         DBG("dev=%p, crtc=%u", dev, pipe);
692         return vblank_ctrl_queue_work(priv, pipe, true);
693 }
694
695 void msm_crtc_disable_vblank(struct drm_crtc *crtc)
696 {
697         struct drm_device *dev = crtc->dev;
698         unsigned int pipe = crtc->index;
699         struct msm_drm_private *priv = dev->dev_private;
700         struct msm_kms *kms = priv->kms;
701         if (!kms)
702                 return;
703         DBG("dev=%p, crtc=%u", dev, pipe);
704         vblank_ctrl_queue_work(priv, pipe, false);
705 }
706
707 /*
708  * DRM ioctls:
709  */
710
711 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
712                 struct drm_file *file)
713 {
714         struct msm_drm_private *priv = dev->dev_private;
715         struct drm_msm_param *args = data;
716         struct msm_gpu *gpu;
717
718         /* for now, we just have 3d pipe.. eventually this would need to
719          * be more clever to dispatch to appropriate gpu module:
720          */
721         if (args->pipe != MSM_PIPE_3D0)
722                 return -EINVAL;
723
724         gpu = priv->gpu;
725
726         if (!gpu)
727                 return -ENXIO;
728
729         return gpu->funcs->get_param(gpu, args->param, &args->value);
730 }
731
732 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
733                 struct drm_file *file)
734 {
735         struct drm_msm_gem_new *args = data;
736
737         if (args->flags & ~MSM_BO_FLAGS) {
738                 DRM_ERROR("invalid flags: %08x\n", args->flags);
739                 return -EINVAL;
740         }
741
742         return msm_gem_new_handle(dev, file, args->size,
743                         args->flags, &args->handle, NULL);
744 }
745
746 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
747 {
748         return ktime_set(timeout.tv_sec, timeout.tv_nsec);
749 }
750
751 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
752                 struct drm_file *file)
753 {
754         struct drm_msm_gem_cpu_prep *args = data;
755         struct drm_gem_object *obj;
756         ktime_t timeout = to_ktime(args->timeout);
757         int ret;
758
759         if (args->op & ~MSM_PREP_FLAGS) {
760                 DRM_ERROR("invalid op: %08x\n", args->op);
761                 return -EINVAL;
762         }
763
764         obj = drm_gem_object_lookup(file, args->handle);
765         if (!obj)
766                 return -ENOENT;
767
768         ret = msm_gem_cpu_prep(obj, args->op, &timeout);
769
770         drm_gem_object_put(obj);
771
772         return ret;
773 }
774
775 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
776                 struct drm_file *file)
777 {
778         struct drm_msm_gem_cpu_fini *args = data;
779         struct drm_gem_object *obj;
780         int ret;
781
782         obj = drm_gem_object_lookup(file, args->handle);
783         if (!obj)
784                 return -ENOENT;
785
786         ret = msm_gem_cpu_fini(obj);
787
788         drm_gem_object_put(obj);
789
790         return ret;
791 }
792
793 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
794                 struct drm_file *file, struct drm_gem_object *obj,
795                 uint64_t *iova)
796 {
797         struct msm_drm_private *priv = dev->dev_private;
798         struct msm_file_private *ctx = file->driver_priv;
799
800         if (!priv->gpu)
801                 return -EINVAL;
802
803         /*
804          * Don't pin the memory here - just get an address so that userspace can
805          * be productive
806          */
807         return msm_gem_get_iova(obj, ctx->aspace, iova);
808 }
809
810 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
811                 struct drm_file *file)
812 {
813         struct drm_msm_gem_info *args = data;
814         struct drm_gem_object *obj;
815         struct msm_gem_object *msm_obj;
816         int i, ret = 0;
817
818         if (args->pad)
819                 return -EINVAL;
820
821         switch (args->info) {
822         case MSM_INFO_GET_OFFSET:
823         case MSM_INFO_GET_IOVA:
824                 /* value returned as immediate, not pointer, so len==0: */
825                 if (args->len)
826                         return -EINVAL;
827                 break;
828         case MSM_INFO_SET_NAME:
829         case MSM_INFO_GET_NAME:
830                 break;
831         default:
832                 return -EINVAL;
833         }
834
835         obj = drm_gem_object_lookup(file, args->handle);
836         if (!obj)
837                 return -ENOENT;
838
839         msm_obj = to_msm_bo(obj);
840
841         switch (args->info) {
842         case MSM_INFO_GET_OFFSET:
843                 args->value = msm_gem_mmap_offset(obj);
844                 break;
845         case MSM_INFO_GET_IOVA:
846                 ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value);
847                 break;
848         case MSM_INFO_SET_NAME:
849                 /* length check should leave room for terminating null: */
850                 if (args->len >= sizeof(msm_obj->name)) {
851                         ret = -EINVAL;
852                         break;
853                 }
854                 if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value),
855                                    args->len)) {
856                         msm_obj->name[0] = '\0';
857                         ret = -EFAULT;
858                         break;
859                 }
860                 msm_obj->name[args->len] = '\0';
861                 for (i = 0; i < args->len; i++) {
862                         if (!isprint(msm_obj->name[i])) {
863                                 msm_obj->name[i] = '\0';
864                                 break;
865                         }
866                 }
867                 break;
868         case MSM_INFO_GET_NAME:
869                 if (args->value && (args->len < strlen(msm_obj->name))) {
870                         ret = -EINVAL;
871                         break;
872                 }
873                 args->len = strlen(msm_obj->name);
874                 if (args->value) {
875                         if (copy_to_user(u64_to_user_ptr(args->value),
876                                          msm_obj->name, args->len))
877                                 ret = -EFAULT;
878                 }
879                 break;
880         }
881
882         drm_gem_object_put(obj);
883
884         return ret;
885 }
886
887 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
888                 struct drm_file *file)
889 {
890         struct msm_drm_private *priv = dev->dev_private;
891         struct drm_msm_wait_fence *args = data;
892         ktime_t timeout = to_ktime(args->timeout);
893         struct msm_gpu_submitqueue *queue;
894         struct msm_gpu *gpu = priv->gpu;
895         int ret;
896
897         if (args->pad) {
898                 DRM_ERROR("invalid pad: %08x\n", args->pad);
899                 return -EINVAL;
900         }
901
902         if (!gpu)
903                 return 0;
904
905         queue = msm_submitqueue_get(file->driver_priv, args->queueid);
906         if (!queue)
907                 return -ENOENT;
908
909         ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
910                 true);
911
912         msm_submitqueue_put(queue);
913         return ret;
914 }
915
916 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
917                 struct drm_file *file)
918 {
919         struct drm_msm_gem_madvise *args = data;
920         struct drm_gem_object *obj;
921         int ret;
922
923         switch (args->madv) {
924         case MSM_MADV_DONTNEED:
925         case MSM_MADV_WILLNEED:
926                 break;
927         default:
928                 return -EINVAL;
929         }
930
931         obj = drm_gem_object_lookup(file, args->handle);
932         if (!obj) {
933                 return -ENOENT;
934         }
935
936         ret = msm_gem_madvise(obj, args->madv);
937         if (ret >= 0) {
938                 args->retained = ret;
939                 ret = 0;
940         }
941
942         drm_gem_object_put(obj);
943
944         return ret;
945 }
946
947
948 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
949                 struct drm_file *file)
950 {
951         struct drm_msm_submitqueue *args = data;
952
953         if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
954                 return -EINVAL;
955
956         return msm_submitqueue_create(dev, file->driver_priv, args->prio,
957                 args->flags, &args->id);
958 }
959
960 static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data,
961                 struct drm_file *file)
962 {
963         return msm_submitqueue_query(dev, file->driver_priv, data);
964 }
965
966 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
967                 struct drm_file *file)
968 {
969         u32 id = *(u32 *) data;
970
971         return msm_submitqueue_remove(file->driver_priv, id);
972 }
973
974 static const struct drm_ioctl_desc msm_ioctls[] = {
975         DRM_IOCTL_DEF_DRV(MSM_GET_PARAM,    msm_ioctl_get_param,    DRM_RENDER_ALLOW),
976         DRM_IOCTL_DEF_DRV(MSM_GEM_NEW,      msm_ioctl_gem_new,      DRM_RENDER_ALLOW),
977         DRM_IOCTL_DEF_DRV(MSM_GEM_INFO,     msm_ioctl_gem_info,     DRM_RENDER_ALLOW),
978         DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW),
979         DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW),
980         DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT,   msm_ioctl_gem_submit,   DRM_RENDER_ALLOW),
981         DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE,   msm_ioctl_wait_fence,   DRM_RENDER_ALLOW),
982         DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE,  msm_ioctl_gem_madvise,  DRM_RENDER_ALLOW),
983         DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW,   msm_ioctl_submitqueue_new,   DRM_RENDER_ALLOW),
984         DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW),
985         DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW),
986 };
987
988 static const struct file_operations fops = {
989         .owner              = THIS_MODULE,
990         .open               = drm_open,
991         .release            = drm_release,
992         .unlocked_ioctl     = drm_ioctl,
993         .compat_ioctl       = drm_compat_ioctl,
994         .poll               = drm_poll,
995         .read               = drm_read,
996         .llseek             = no_llseek,
997         .mmap               = msm_gem_mmap,
998 };
999
1000 static const struct drm_driver msm_driver = {
1001         .driver_features    = DRIVER_GEM |
1002                                 DRIVER_RENDER |
1003                                 DRIVER_ATOMIC |
1004                                 DRIVER_MODESET |
1005                                 DRIVER_SYNCOBJ,
1006         .open               = msm_open,
1007         .postclose           = msm_postclose,
1008         .lastclose          = drm_fb_helper_lastclose,
1009         .irq_handler        = msm_irq,
1010         .irq_preinstall     = msm_irq_preinstall,
1011         .irq_postinstall    = msm_irq_postinstall,
1012         .irq_uninstall      = msm_irq_uninstall,
1013         .dumb_create        = msm_gem_dumb_create,
1014         .dumb_map_offset    = msm_gem_dumb_map_offset,
1015         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1016         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1017         .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
1018         .gem_prime_mmap     = msm_gem_prime_mmap,
1019 #ifdef CONFIG_DEBUG_FS
1020         .debugfs_init       = msm_debugfs_init,
1021 #endif
1022         .ioctls             = msm_ioctls,
1023         .num_ioctls         = ARRAY_SIZE(msm_ioctls),
1024         .fops               = &fops,
1025         .name               = "msm",
1026         .desc               = "MSM Snapdragon DRM",
1027         .date               = "20130625",
1028         .major              = MSM_VERSION_MAJOR,
1029         .minor              = MSM_VERSION_MINOR,
1030         .patchlevel         = MSM_VERSION_PATCHLEVEL,
1031 };
1032
1033 static int __maybe_unused msm_runtime_suspend(struct device *dev)
1034 {
1035         struct drm_device *ddev = dev_get_drvdata(dev);
1036         struct msm_drm_private *priv = ddev->dev_private;
1037         struct msm_mdss *mdss = priv->mdss;
1038
1039         DBG("");
1040
1041         if (mdss && mdss->funcs)
1042                 return mdss->funcs->disable(mdss);
1043
1044         return 0;
1045 }
1046
1047 static int __maybe_unused msm_runtime_resume(struct device *dev)
1048 {
1049         struct drm_device *ddev = dev_get_drvdata(dev);
1050         struct msm_drm_private *priv = ddev->dev_private;
1051         struct msm_mdss *mdss = priv->mdss;
1052
1053         DBG("");
1054
1055         if (mdss && mdss->funcs)
1056                 return mdss->funcs->enable(mdss);
1057
1058         return 0;
1059 }
1060
1061 static int __maybe_unused msm_pm_suspend(struct device *dev)
1062 {
1063
1064         if (pm_runtime_suspended(dev))
1065                 return 0;
1066
1067         return msm_runtime_suspend(dev);
1068 }
1069
1070 static int __maybe_unused msm_pm_resume(struct device *dev)
1071 {
1072         if (pm_runtime_suspended(dev))
1073                 return 0;
1074
1075         return msm_runtime_resume(dev);
1076 }
1077
1078 static int __maybe_unused msm_pm_prepare(struct device *dev)
1079 {
1080         struct drm_device *ddev = dev_get_drvdata(dev);
1081         struct msm_drm_private *priv = ddev ? ddev->dev_private : NULL;
1082
1083         if (!priv || !priv->kms)
1084                 return 0;
1085
1086         return drm_mode_config_helper_suspend(ddev);
1087 }
1088
1089 static void __maybe_unused msm_pm_complete(struct device *dev)
1090 {
1091         struct drm_device *ddev = dev_get_drvdata(dev);
1092         struct msm_drm_private *priv = ddev ? ddev->dev_private : NULL;
1093
1094         if (!priv || !priv->kms)
1095                 return;
1096
1097         drm_mode_config_helper_resume(ddev);
1098 }
1099
1100 static const struct dev_pm_ops msm_pm_ops = {
1101         SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
1102         SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
1103         .prepare = msm_pm_prepare,
1104         .complete = msm_pm_complete,
1105 };
1106
1107 /*
1108  * Componentized driver support:
1109  */
1110
1111 /*
1112  * NOTE: duplication of the same code as exynos or imx (or probably any other).
1113  * so probably some room for some helpers
1114  */
1115 static int compare_of(struct device *dev, void *data)
1116 {
1117         return dev->of_node == data;
1118 }
1119
1120 /*
1121  * Identify what components need to be added by parsing what remote-endpoints
1122  * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1123  * is no external component that we need to add since LVDS is within MDP4
1124  * itself.
1125  */
1126 static int add_components_mdp(struct device *mdp_dev,
1127                               struct component_match **matchptr)
1128 {
1129         struct device_node *np = mdp_dev->of_node;
1130         struct device_node *ep_node;
1131         struct device *master_dev;
1132
1133         /*
1134          * on MDP4 based platforms, the MDP platform device is the component
1135          * master that adds other display interface components to itself.
1136          *
1137          * on MDP5 based platforms, the MDSS platform device is the component
1138          * master that adds MDP5 and other display interface components to
1139          * itself.
1140          */
1141         if (of_device_is_compatible(np, "qcom,mdp4"))
1142                 master_dev = mdp_dev;
1143         else
1144                 master_dev = mdp_dev->parent;
1145
1146         for_each_endpoint_of_node(np, ep_node) {
1147                 struct device_node *intf;
1148                 struct of_endpoint ep;
1149                 int ret;
1150
1151                 ret = of_graph_parse_endpoint(ep_node, &ep);
1152                 if (ret) {
1153                         DRM_DEV_ERROR(mdp_dev, "unable to parse port endpoint\n");
1154                         of_node_put(ep_node);
1155                         return ret;
1156                 }
1157
1158                 /*
1159                  * The LCDC/LVDS port on MDP4 is a speacial case where the
1160                  * remote-endpoint isn't a component that we need to add
1161                  */
1162                 if (of_device_is_compatible(np, "qcom,mdp4") &&
1163                     ep.port == 0)
1164                         continue;
1165
1166                 /*
1167                  * It's okay if some of the ports don't have a remote endpoint
1168                  * specified. It just means that the port isn't connected to
1169                  * any external interface.
1170                  */
1171                 intf = of_graph_get_remote_port_parent(ep_node);
1172                 if (!intf)
1173                         continue;
1174
1175                 if (of_device_is_available(intf))
1176                         drm_of_component_match_add(master_dev, matchptr,
1177                                                    compare_of, intf);
1178
1179                 of_node_put(intf);
1180         }
1181
1182         return 0;
1183 }
1184
1185 static int compare_name_mdp(struct device *dev, void *data)
1186 {
1187         return (strstr(dev_name(dev), "mdp") != NULL);
1188 }
1189
1190 static int add_display_components(struct platform_device *pdev,
1191                                   struct component_match **matchptr)
1192 {
1193         struct device *mdp_dev;
1194         struct device *dev = &pdev->dev;
1195         int ret;
1196
1197         /*
1198          * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1199          * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1200          * Populate the children devices, find the MDP5/DPU node, and then add
1201          * the interfaces to our components list.
1202          */
1203         switch (get_mdp_ver(pdev)) {
1204         case KMS_MDP5:
1205         case KMS_DPU:
1206                 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1207                 if (ret) {
1208                         DRM_DEV_ERROR(dev, "failed to populate children devices\n");
1209                         return ret;
1210                 }
1211
1212                 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1213                 if (!mdp_dev) {
1214                         DRM_DEV_ERROR(dev, "failed to find MDSS MDP node\n");
1215                         of_platform_depopulate(dev);
1216                         return -ENODEV;
1217                 }
1218
1219                 put_device(mdp_dev);
1220
1221                 /* add the MDP component itself */
1222                 drm_of_component_match_add(dev, matchptr, compare_of,
1223                                            mdp_dev->of_node);
1224                 break;
1225         case KMS_MDP4:
1226                 /* MDP4 */
1227                 mdp_dev = dev;
1228                 break;
1229         }
1230
1231         ret = add_components_mdp(mdp_dev, matchptr);
1232         if (ret)
1233                 of_platform_depopulate(dev);
1234
1235         return ret;
1236 }
1237
1238 /*
1239  * We don't know what's the best binding to link the gpu with the drm device.
1240  * Fow now, we just hunt for all the possible gpus that we support, and add them
1241  * as components.
1242  */
1243 static const struct of_device_id msm_gpu_match[] = {
1244         { .compatible = "qcom,adreno" },
1245         { .compatible = "qcom,adreno-3xx" },
1246         { .compatible = "amd,imageon" },
1247         { .compatible = "qcom,kgsl-3d0" },
1248         { },
1249 };
1250
1251 static int add_gpu_components(struct device *dev,
1252                               struct component_match **matchptr)
1253 {
1254         struct device_node *np;
1255
1256         np = of_find_matching_node(NULL, msm_gpu_match);
1257         if (!np)
1258                 return 0;
1259
1260         if (of_device_is_available(np))
1261                 drm_of_component_match_add(dev, matchptr, compare_of, np);
1262
1263         of_node_put(np);
1264
1265         return 0;
1266 }
1267
1268 static int msm_drm_bind(struct device *dev)
1269 {
1270         return msm_drm_init(dev, &msm_driver);
1271 }
1272
1273 static void msm_drm_unbind(struct device *dev)
1274 {
1275         msm_drm_uninit(dev);
1276 }
1277
1278 static const struct component_master_ops msm_drm_ops = {
1279         .bind = msm_drm_bind,
1280         .unbind = msm_drm_unbind,
1281 };
1282
1283 /*
1284  * Platform driver:
1285  */
1286
1287 static int msm_pdev_probe(struct platform_device *pdev)
1288 {
1289         struct component_match *match = NULL;
1290         int ret;
1291
1292         if (get_mdp_ver(pdev)) {
1293                 ret = add_display_components(pdev, &match);
1294                 if (ret)
1295                         return ret;
1296         }
1297
1298         ret = add_gpu_components(&pdev->dev, &match);
1299         if (ret)
1300                 goto fail;
1301
1302         /* on all devices that I am aware of, iommu's which can map
1303          * any address the cpu can see are used:
1304          */
1305         ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1306         if (ret)
1307                 goto fail;
1308
1309         ret = component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1310         if (ret)
1311                 goto fail;
1312
1313         return 0;
1314
1315 fail:
1316         of_platform_depopulate(&pdev->dev);
1317         return ret;
1318 }
1319
1320 static int msm_pdev_remove(struct platform_device *pdev)
1321 {
1322         component_master_del(&pdev->dev, &msm_drm_ops);
1323         of_platform_depopulate(&pdev->dev);
1324
1325         return 0;
1326 }
1327
1328 static void msm_pdev_shutdown(struct platform_device *pdev)
1329 {
1330         struct drm_device *drm = platform_get_drvdata(pdev);
1331         struct msm_drm_private *priv = drm ? drm->dev_private : NULL;
1332
1333         if (!priv || !priv->kms)
1334                 return;
1335
1336         drm_atomic_helper_shutdown(drm);
1337 }
1338
1339 static const struct of_device_id dt_match[] = {
1340         { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
1341         { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
1342         { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU },
1343         { .compatible = "qcom,sc7180-mdss", .data = (void *)KMS_DPU },
1344         { .compatible = "qcom,sc7280-mdss", .data = (void *)KMS_DPU },
1345         { .compatible = "qcom,sm8150-mdss", .data = (void *)KMS_DPU },
1346         { .compatible = "qcom,sm8250-mdss", .data = (void *)KMS_DPU },
1347         {}
1348 };
1349 MODULE_DEVICE_TABLE(of, dt_match);
1350
1351 static struct platform_driver msm_platform_driver = {
1352         .probe      = msm_pdev_probe,
1353         .remove     = msm_pdev_remove,
1354         .shutdown   = msm_pdev_shutdown,
1355         .driver     = {
1356                 .name   = "msm",
1357                 .of_match_table = dt_match,
1358                 .pm     = &msm_pm_ops,
1359         },
1360 };
1361
1362 static int __init msm_drm_register(void)
1363 {
1364         if (!modeset)
1365                 return -EINVAL;
1366
1367         DBG("init");
1368         msm_mdp_register();
1369         msm_dpu_register();
1370         msm_dsi_register();
1371         msm_edp_register();
1372         msm_hdmi_register();
1373         msm_dp_register();
1374         adreno_register();
1375         return platform_driver_register(&msm_platform_driver);
1376 }
1377
1378 static void __exit msm_drm_unregister(void)
1379 {
1380         DBG("fini");
1381         platform_driver_unregister(&msm_platform_driver);
1382         msm_dp_unregister();
1383         msm_hdmi_unregister();
1384         adreno_unregister();
1385         msm_edp_unregister();
1386         msm_dsi_unregister();
1387         msm_mdp_unregister();
1388         msm_dpu_unregister();
1389 }
1390
1391 module_init(msm_drm_register);
1392 module_exit(msm_drm_unregister);
1393
1394 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1395 MODULE_DESCRIPTION("MSM DRM Driver");
1396 MODULE_LICENSE("GPL");