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