2 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
19 #include <linux/kthread.h>
20 #include <uapi/linux/sched/types.h>
21 #include <drm/drm_of.h>
24 #include "msm_debugfs.h"
25 #include "msm_fence.h"
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
39 #define MSM_VERSION_MAJOR 1
40 #define MSM_VERSION_MINOR 3
41 #define MSM_VERSION_PATCHLEVEL 0
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
,
50 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs
= {
51 .atomic_commit_tail
= msm_atomic_commit_tail
,
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);
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);
68 static char *vram
= "16m";
69 MODULE_PARM_DESC(vram
, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
70 module_param(vram
, charp
, 0);
72 bool dumpstate
= false;
73 MODULE_PARM_DESC(dumpstate
, "Dump KMS state on errors");
74 module_param(dumpstate
, bool, 0600);
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);
84 int msm_clk_bulk_get(struct device
*dev
, struct clk_bulk_data
**bulk
)
86 struct property
*prop
;
88 struct clk_bulk_data
*local
;
89 int i
= 0, ret
, count
;
91 count
= of_property_count_strings(dev
->of_node
, "clock-names");
95 local
= devm_kcalloc(dev
, sizeof(struct clk_bulk_data
*),
100 of_property_for_each_string(dev
->of_node
, "clock-names", prop
, name
) {
101 local
[i
].id
= devm_kstrdup(dev
, name
, GFP_KERNEL
);
103 devm_kfree(dev
, local
);
110 ret
= devm_clk_bulk_get(dev
, count
, local
);
113 for (i
= 0; i
< count
; i
++)
114 devm_kfree(dev
, (void *) local
[i
].id
);
115 devm_kfree(dev
, local
);
124 struct clk
*msm_clk_bulk_get_clock(struct clk_bulk_data
*bulk
, int count
,
130 snprintf(n
, sizeof(n
), "%s_clk", name
);
132 for (i
= 0; bulk
&& i
< count
; i
++) {
133 if (!strcmp(bulk
[i
].id
, name
) || !strcmp(bulk
[i
].id
, n
))
141 struct clk
*msm_clk_get(struct platform_device
*pdev
, const char *name
)
146 clk
= devm_clk_get(&pdev
->dev
, name
);
147 if (!IS_ERR(clk
) || PTR_ERR(clk
) == -EPROBE_DEFER
)
150 snprintf(name2
, sizeof(name2
), "%s_clk", name
);
152 clk
= devm_clk_get(&pdev
->dev
, name2
);
154 dev_warn(&pdev
->dev
, "Using legacy clk name binding. Use "
155 "\"%s\" instead of \"%s\"\n", name
, name2
);
160 void __iomem
*msm_ioremap(struct platform_device
*pdev
, const char *name
,
163 struct resource
*res
;
168 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
170 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
173 dev_err(&pdev
->dev
, "failed to get memory resource: %s\n", name
);
174 return ERR_PTR(-EINVAL
);
177 size
= resource_size(res
);
179 ptr
= devm_ioremap_nocache(&pdev
->dev
, res
->start
, size
);
181 dev_err(&pdev
->dev
, "failed to ioremap: %s\n", name
);
182 return ERR_PTR(-ENOMEM
);
186 printk(KERN_DEBUG
"IO:region %s %p %08lx\n", dbgname
, ptr
, size
);
191 void msm_writel(u32 data
, void __iomem
*addr
)
194 printk(KERN_DEBUG
"IO:W %p %08x\n", addr
, data
);
198 u32
msm_readl(const void __iomem
*addr
)
200 u32 val
= readl(addr
);
202 pr_err("IO:R %p %08x\n", addr
, val
);
206 struct vblank_event
{
207 struct list_head node
;
212 static void vblank_ctrl_worker(struct kthread_work
*work
)
214 struct msm_vblank_ctrl
*vbl_ctrl
= container_of(work
,
215 struct msm_vblank_ctrl
, work
);
216 struct msm_drm_private
*priv
= container_of(vbl_ctrl
,
217 struct msm_drm_private
, vblank_ctrl
);
218 struct msm_kms
*kms
= priv
->kms
;
219 struct vblank_event
*vbl_ev
, *tmp
;
222 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
223 list_for_each_entry_safe(vbl_ev
, tmp
, &vbl_ctrl
->event_list
, node
) {
224 list_del(&vbl_ev
->node
);
225 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
228 kms
->funcs
->enable_vblank(kms
,
229 priv
->crtcs
[vbl_ev
->crtc_id
]);
231 kms
->funcs
->disable_vblank(kms
,
232 priv
->crtcs
[vbl_ev
->crtc_id
]);
236 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
239 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
242 static int vblank_ctrl_queue_work(struct msm_drm_private
*priv
,
243 int crtc_id
, bool enable
)
245 struct msm_vblank_ctrl
*vbl_ctrl
= &priv
->vblank_ctrl
;
246 struct vblank_event
*vbl_ev
;
249 vbl_ev
= kzalloc(sizeof(*vbl_ev
), GFP_ATOMIC
);
253 vbl_ev
->crtc_id
= crtc_id
;
254 vbl_ev
->enable
= enable
;
256 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
257 list_add_tail(&vbl_ev
->node
, &vbl_ctrl
->event_list
);
258 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
260 kthread_queue_work(&priv
->disp_thread
[crtc_id
].worker
,
266 static int msm_drm_uninit(struct device
*dev
)
268 struct platform_device
*pdev
= to_platform_device(dev
);
269 struct drm_device
*ddev
= platform_get_drvdata(pdev
);
270 struct msm_drm_private
*priv
= ddev
->dev_private
;
271 struct msm_kms
*kms
= priv
->kms
;
272 struct msm_mdss
*mdss
= priv
->mdss
;
273 struct msm_vblank_ctrl
*vbl_ctrl
= &priv
->vblank_ctrl
;
274 struct vblank_event
*vbl_ev
, *tmp
;
277 /* We must cancel and cleanup any pending vblank enable/disable
278 * work before drm_irq_uninstall() to avoid work re-enabling an
279 * irq after uninstall has disabled it.
281 kthread_flush_work(&vbl_ctrl
->work
);
282 list_for_each_entry_safe(vbl_ev
, tmp
, &vbl_ctrl
->event_list
, node
) {
283 list_del(&vbl_ev
->node
);
287 /* clean up display commit/event worker threads */
288 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
289 if (priv
->disp_thread
[i
].thread
) {
290 kthread_flush_worker(&priv
->disp_thread
[i
].worker
);
291 kthread_stop(priv
->disp_thread
[i
].thread
);
292 priv
->disp_thread
[i
].thread
= NULL
;
295 if (priv
->event_thread
[i
].thread
) {
296 kthread_flush_worker(&priv
->event_thread
[i
].worker
);
297 kthread_stop(priv
->event_thread
[i
].thread
);
298 priv
->event_thread
[i
].thread
= NULL
;
302 msm_gem_shrinker_cleanup(ddev
);
304 drm_kms_helper_poll_fini(ddev
);
306 drm_dev_unregister(ddev
);
308 msm_perf_debugfs_cleanup(priv
);
309 msm_rd_debugfs_cleanup(priv
);
311 #ifdef CONFIG_DRM_FBDEV_EMULATION
312 if (fbdev
&& priv
->fbdev
)
313 msm_fbdev_free(ddev
);
315 drm_mode_config_cleanup(ddev
);
317 pm_runtime_get_sync(dev
);
318 drm_irq_uninstall(ddev
);
319 pm_runtime_put_sync(dev
);
321 flush_workqueue(priv
->wq
);
322 destroy_workqueue(priv
->wq
);
324 if (kms
&& kms
->funcs
)
325 kms
->funcs
->destroy(kms
);
327 if (priv
->vram
.paddr
) {
328 unsigned long attrs
= DMA_ATTR_NO_KERNEL_MAPPING
;
329 drm_mm_takedown(&priv
->vram
.mm
);
330 dma_free_attrs(dev
, priv
->vram
.size
, NULL
,
331 priv
->vram
.paddr
, attrs
);
334 component_unbind_all(dev
, ddev
);
336 if (mdss
&& mdss
->funcs
)
337 mdss
->funcs
->destroy(ddev
);
339 ddev
->dev_private
= NULL
;
351 static int get_mdp_ver(struct platform_device
*pdev
)
353 struct device
*dev
= &pdev
->dev
;
355 return (int) (unsigned long) of_device_get_match_data(dev
);
358 #include <linux/of_address.h>
360 static int msm_init_vram(struct drm_device
*dev
)
362 struct msm_drm_private
*priv
= dev
->dev_private
;
363 struct device_node
*node
;
364 unsigned long size
= 0;
367 /* In the device-tree world, we could have a 'memory-region'
368 * phandle, which gives us a link to our "vram". Allocating
369 * is all nicely abstracted behind the dma api, but we need
370 * to know the entire size to allocate it all in one go. There
372 * 1) device with no IOMMU, in which case we need exclusive
373 * access to a VRAM carveout big enough for all gpu
375 * 2) device with IOMMU, but where the bootloader puts up
376 * a splash screen. In this case, the VRAM carveout
377 * need only be large enough for fbdev fb. But we need
378 * exclusive access to the buffer to avoid the kernel
379 * using those pages for other purposes (which appears
380 * as corruption on screen before we have a chance to
381 * load and do initial modeset)
384 node
= of_parse_phandle(dev
->dev
->of_node
, "memory-region", 0);
387 ret
= of_address_to_resource(node
, 0, &r
);
391 size
= r
.end
- r
.start
;
392 DRM_INFO("using VRAM carveout: %lx@%pa\n", size
, &r
.start
);
394 /* if we have no IOMMU, then we need to use carveout allocator.
395 * Grab the entire CMA chunk carved out in early startup in
398 } else if (!iommu_present(&platform_bus_type
)) {
399 DRM_INFO("using %s VRAM carveout\n", vram
);
400 size
= memparse(vram
, NULL
);
404 unsigned long attrs
= 0;
407 priv
->vram
.size
= size
;
409 drm_mm_init(&priv
->vram
.mm
, 0, (size
>> PAGE_SHIFT
) - 1);
410 spin_lock_init(&priv
->vram
.lock
);
412 attrs
|= DMA_ATTR_NO_KERNEL_MAPPING
;
413 attrs
|= DMA_ATTR_WRITE_COMBINE
;
415 /* note that for no-kernel-mapping, the vaddr returned
416 * is bogus, but non-null if allocation succeeded:
418 p
= dma_alloc_attrs(dev
->dev
, size
,
419 &priv
->vram
.paddr
, GFP_KERNEL
, attrs
);
421 dev_err(dev
->dev
, "failed to allocate VRAM\n");
422 priv
->vram
.paddr
= 0;
426 dev_info(dev
->dev
, "VRAM: %08x->%08x\n",
427 (uint32_t)priv
->vram
.paddr
,
428 (uint32_t)(priv
->vram
.paddr
+ size
));
434 static int msm_drm_init(struct device
*dev
, struct drm_driver
*drv
)
436 struct platform_device
*pdev
= to_platform_device(dev
);
437 struct drm_device
*ddev
;
438 struct msm_drm_private
*priv
;
440 struct msm_mdss
*mdss
;
442 struct sched_param param
;
444 ddev
= drm_dev_alloc(drv
, dev
);
446 dev_err(dev
, "failed to allocate drm_device\n");
447 return PTR_ERR(ddev
);
450 platform_set_drvdata(pdev
, ddev
);
452 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
455 goto err_unref_drm_dev
;
458 ddev
->dev_private
= priv
;
461 switch (get_mdp_ver(pdev
)) {
463 ret
= mdp5_mdss_init(ddev
);
466 ret
= dpu_mdss_init(ddev
);
477 priv
->wq
= alloc_ordered_workqueue("msm", 0);
479 INIT_LIST_HEAD(&priv
->inactive_list
);
480 INIT_LIST_HEAD(&priv
->vblank_ctrl
.event_list
);
481 kthread_init_work(&priv
->vblank_ctrl
.work
, vblank_ctrl_worker
);
482 spin_lock_init(&priv
->vblank_ctrl
.lock
);
484 drm_mode_config_init(ddev
);
486 /* Bind all our sub-components: */
487 ret
= component_bind_all(dev
, ddev
);
489 goto err_destroy_mdss
;
491 ret
= msm_init_vram(ddev
);
495 if (!dev
->dma_parms
) {
496 dev
->dma_parms
= devm_kzalloc(dev
, sizeof(*dev
->dma_parms
),
501 dma_set_max_seg_size(dev
, DMA_BIT_MASK(32));
503 msm_gem_shrinker_init(ddev
);
505 switch (get_mdp_ver(pdev
)) {
507 kms
= mdp4_kms_init(ddev
);
511 kms
= mdp5_kms_init(ddev
);
514 kms
= dpu_kms_init(ddev
);
518 kms
= ERR_PTR(-ENODEV
);
524 * NOTE: once we have GPU support, having no kms should not
525 * be considered fatal.. ideally we would still support gpu
526 * and (for example) use dmabuf/prime to share buffers with
527 * imx drm driver on iMX5
529 dev_err(dev
, "failed to load kms\n");
534 /* Enable normalization of plane zpos */
535 ddev
->mode_config
.normalize_zpos
= true;
538 ret
= kms
->funcs
->hw_init(kms
);
540 dev_err(dev
, "kms hw init failed: %d\n", ret
);
545 ddev
->mode_config
.funcs
= &mode_config_funcs
;
546 ddev
->mode_config
.helper_private
= &mode_config_helper_funcs
;
549 * this priority was found during empiric testing to have appropriate
550 * realtime scheduling to process display updates and interact with
551 * other real time and normal priority task
553 param
.sched_priority
= 16;
554 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
556 /* initialize display thread */
557 priv
->disp_thread
[i
].crtc_id
= priv
->crtcs
[i
]->base
.id
;
558 kthread_init_worker(&priv
->disp_thread
[i
].worker
);
559 priv
->disp_thread
[i
].dev
= ddev
;
560 priv
->disp_thread
[i
].thread
=
561 kthread_run(kthread_worker_fn
,
562 &priv
->disp_thread
[i
].worker
,
563 "crtc_commit:%d", priv
->disp_thread
[i
].crtc_id
);
564 ret
= sched_setscheduler(priv
->disp_thread
[i
].thread
,
567 pr_warn("display thread priority update failed: %d\n",
570 if (IS_ERR(priv
->disp_thread
[i
].thread
)) {
571 dev_err(dev
, "failed to create crtc_commit kthread\n");
572 priv
->disp_thread
[i
].thread
= NULL
;
575 /* initialize event thread */
576 priv
->event_thread
[i
].crtc_id
= priv
->crtcs
[i
]->base
.id
;
577 kthread_init_worker(&priv
->event_thread
[i
].worker
);
578 priv
->event_thread
[i
].dev
= ddev
;
579 priv
->event_thread
[i
].thread
=
580 kthread_run(kthread_worker_fn
,
581 &priv
->event_thread
[i
].worker
,
582 "crtc_event:%d", priv
->event_thread
[i
].crtc_id
);
584 * event thread should also run at same priority as disp_thread
585 * because it is handling frame_done events. A lower priority
586 * event thread and higher priority disp_thread can causes
587 * frame_pending counters beyond 2. This can lead to commit
588 * failure at crtc commit level.
590 ret
= sched_setscheduler(priv
->event_thread
[i
].thread
,
593 pr_warn("display event thread priority update failed: %d\n",
596 if (IS_ERR(priv
->event_thread
[i
].thread
)) {
597 dev_err(dev
, "failed to create crtc_event kthread\n");
598 priv
->event_thread
[i
].thread
= NULL
;
601 if ((!priv
->disp_thread
[i
].thread
) ||
602 !priv
->event_thread
[i
].thread
) {
603 /* clean up previously created threads if any */
604 for ( ; i
>= 0; i
--) {
605 if (priv
->disp_thread
[i
].thread
) {
607 priv
->disp_thread
[i
].thread
);
608 priv
->disp_thread
[i
].thread
= NULL
;
611 if (priv
->event_thread
[i
].thread
) {
613 priv
->event_thread
[i
].thread
);
614 priv
->event_thread
[i
].thread
= NULL
;
621 ret
= drm_vblank_init(ddev
, priv
->num_crtcs
);
623 dev_err(dev
, "failed to initialize vblank\n");
628 pm_runtime_get_sync(dev
);
629 ret
= drm_irq_install(ddev
, kms
->irq
);
630 pm_runtime_put_sync(dev
);
632 dev_err(dev
, "failed to install IRQ handler\n");
637 ret
= drm_dev_register(ddev
, 0);
641 drm_mode_config_reset(ddev
);
643 #ifdef CONFIG_DRM_FBDEV_EMULATION
645 priv
->fbdev
= msm_fbdev_init(ddev
);
648 ret
= msm_debugfs_late_init(ddev
);
652 drm_kms_helper_poll_init(ddev
);
660 if (mdss
&& mdss
->funcs
)
661 mdss
->funcs
->destroy(ddev
);
673 static void load_gpu(struct drm_device
*dev
)
675 static DEFINE_MUTEX(init_lock
);
676 struct msm_drm_private
*priv
= dev
->dev_private
;
678 mutex_lock(&init_lock
);
681 priv
->gpu
= adreno_load_gpu(dev
);
683 mutex_unlock(&init_lock
);
686 static int context_init(struct drm_device
*dev
, struct drm_file
*file
)
688 struct msm_file_private
*ctx
;
690 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
694 msm_submitqueue_init(dev
, ctx
);
696 file
->driver_priv
= ctx
;
701 static int msm_open(struct drm_device
*dev
, struct drm_file
*file
)
703 /* For now, load gpu on open.. to avoid the requirement of having
704 * firmware in the initrd.
708 return context_init(dev
, file
);
711 static void context_close(struct msm_file_private
*ctx
)
713 msm_submitqueue_close(ctx
);
717 static void msm_postclose(struct drm_device
*dev
, struct drm_file
*file
)
719 struct msm_drm_private
*priv
= dev
->dev_private
;
720 struct msm_file_private
*ctx
= file
->driver_priv
;
722 mutex_lock(&dev
->struct_mutex
);
723 if (ctx
== priv
->lastctx
)
724 priv
->lastctx
= NULL
;
725 mutex_unlock(&dev
->struct_mutex
);
730 static irqreturn_t
msm_irq(int irq
, void *arg
)
732 struct drm_device
*dev
= arg
;
733 struct msm_drm_private
*priv
= dev
->dev_private
;
734 struct msm_kms
*kms
= priv
->kms
;
736 return kms
->funcs
->irq(kms
);
739 static void msm_irq_preinstall(struct drm_device
*dev
)
741 struct msm_drm_private
*priv
= dev
->dev_private
;
742 struct msm_kms
*kms
= priv
->kms
;
744 kms
->funcs
->irq_preinstall(kms
);
747 static int msm_irq_postinstall(struct drm_device
*dev
)
749 struct msm_drm_private
*priv
= dev
->dev_private
;
750 struct msm_kms
*kms
= priv
->kms
;
752 return kms
->funcs
->irq_postinstall(kms
);
755 static void msm_irq_uninstall(struct drm_device
*dev
)
757 struct msm_drm_private
*priv
= dev
->dev_private
;
758 struct msm_kms
*kms
= priv
->kms
;
760 kms
->funcs
->irq_uninstall(kms
);
763 static int msm_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
765 struct msm_drm_private
*priv
= dev
->dev_private
;
766 struct msm_kms
*kms
= priv
->kms
;
769 DBG("dev=%p, crtc=%u", dev
, pipe
);
770 return vblank_ctrl_queue_work(priv
, pipe
, true);
773 static void msm_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
775 struct msm_drm_private
*priv
= dev
->dev_private
;
776 struct msm_kms
*kms
= priv
->kms
;
779 DBG("dev=%p, crtc=%u", dev
, pipe
);
780 vblank_ctrl_queue_work(priv
, pipe
, false);
787 static int msm_ioctl_get_param(struct drm_device
*dev
, void *data
,
788 struct drm_file
*file
)
790 struct msm_drm_private
*priv
= dev
->dev_private
;
791 struct drm_msm_param
*args
= data
;
794 /* for now, we just have 3d pipe.. eventually this would need to
795 * be more clever to dispatch to appropriate gpu module:
797 if (args
->pipe
!= MSM_PIPE_3D0
)
805 return gpu
->funcs
->get_param(gpu
, args
->param
, &args
->value
);
808 static int msm_ioctl_gem_new(struct drm_device
*dev
, void *data
,
809 struct drm_file
*file
)
811 struct drm_msm_gem_new
*args
= data
;
813 if (args
->flags
& ~MSM_BO_FLAGS
) {
814 DRM_ERROR("invalid flags: %08x\n", args
->flags
);
818 return msm_gem_new_handle(dev
, file
, args
->size
,
819 args
->flags
, &args
->handle
);
822 static inline ktime_t
to_ktime(struct drm_msm_timespec timeout
)
824 return ktime_set(timeout
.tv_sec
, timeout
.tv_nsec
);
827 static int msm_ioctl_gem_cpu_prep(struct drm_device
*dev
, void *data
,
828 struct drm_file
*file
)
830 struct drm_msm_gem_cpu_prep
*args
= data
;
831 struct drm_gem_object
*obj
;
832 ktime_t timeout
= to_ktime(args
->timeout
);
835 if (args
->op
& ~MSM_PREP_FLAGS
) {
836 DRM_ERROR("invalid op: %08x\n", args
->op
);
840 obj
= drm_gem_object_lookup(file
, args
->handle
);
844 ret
= msm_gem_cpu_prep(obj
, args
->op
, &timeout
);
846 drm_gem_object_put_unlocked(obj
);
851 static int msm_ioctl_gem_cpu_fini(struct drm_device
*dev
, void *data
,
852 struct drm_file
*file
)
854 struct drm_msm_gem_cpu_fini
*args
= data
;
855 struct drm_gem_object
*obj
;
858 obj
= drm_gem_object_lookup(file
, args
->handle
);
862 ret
= msm_gem_cpu_fini(obj
);
864 drm_gem_object_put_unlocked(obj
);
869 static int msm_ioctl_gem_info_iova(struct drm_device
*dev
,
870 struct drm_gem_object
*obj
, uint64_t *iova
)
872 struct msm_drm_private
*priv
= dev
->dev_private
;
877 return msm_gem_get_iova(obj
, priv
->gpu
->aspace
, iova
);
880 static int msm_ioctl_gem_info(struct drm_device
*dev
, void *data
,
881 struct drm_file
*file
)
883 struct drm_msm_gem_info
*args
= data
;
884 struct drm_gem_object
*obj
;
887 if (args
->flags
& ~MSM_INFO_FLAGS
)
890 obj
= drm_gem_object_lookup(file
, args
->handle
);
894 if (args
->flags
& MSM_INFO_IOVA
) {
897 ret
= msm_ioctl_gem_info_iova(dev
, obj
, &iova
);
901 args
->offset
= msm_gem_mmap_offset(obj
);
904 drm_gem_object_put_unlocked(obj
);
909 static int msm_ioctl_wait_fence(struct drm_device
*dev
, void *data
,
910 struct drm_file
*file
)
912 struct msm_drm_private
*priv
= dev
->dev_private
;
913 struct drm_msm_wait_fence
*args
= data
;
914 ktime_t timeout
= to_ktime(args
->timeout
);
915 struct msm_gpu_submitqueue
*queue
;
916 struct msm_gpu
*gpu
= priv
->gpu
;
920 DRM_ERROR("invalid pad: %08x\n", args
->pad
);
927 queue
= msm_submitqueue_get(file
->driver_priv
, args
->queueid
);
931 ret
= msm_wait_fence(gpu
->rb
[queue
->prio
]->fctx
, args
->fence
, &timeout
,
934 msm_submitqueue_put(queue
);
938 static int msm_ioctl_gem_madvise(struct drm_device
*dev
, void *data
,
939 struct drm_file
*file
)
941 struct drm_msm_gem_madvise
*args
= data
;
942 struct drm_gem_object
*obj
;
945 switch (args
->madv
) {
946 case MSM_MADV_DONTNEED
:
947 case MSM_MADV_WILLNEED
:
953 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
957 obj
= drm_gem_object_lookup(file
, args
->handle
);
963 ret
= msm_gem_madvise(obj
, args
->madv
);
965 args
->retained
= ret
;
969 drm_gem_object_put(obj
);
972 mutex_unlock(&dev
->struct_mutex
);
977 static int msm_ioctl_submitqueue_new(struct drm_device
*dev
, void *data
,
978 struct drm_file
*file
)
980 struct drm_msm_submitqueue
*args
= data
;
982 if (args
->flags
& ~MSM_SUBMITQUEUE_FLAGS
)
985 return msm_submitqueue_create(dev
, file
->driver_priv
, args
->prio
,
986 args
->flags
, &args
->id
);
990 static int msm_ioctl_submitqueue_close(struct drm_device
*dev
, void *data
,
991 struct drm_file
*file
)
993 u32 id
= *(u32
*) data
;
995 return msm_submitqueue_remove(file
->driver_priv
, id
);
998 static const struct drm_ioctl_desc msm_ioctls
[] = {
999 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM
, msm_ioctl_get_param
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1000 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW
, msm_ioctl_gem_new
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1001 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO
, msm_ioctl_gem_info
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1002 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP
, msm_ioctl_gem_cpu_prep
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1003 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI
, msm_ioctl_gem_cpu_fini
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1004 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT
, msm_ioctl_gem_submit
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1005 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE
, msm_ioctl_wait_fence
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1006 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE
, msm_ioctl_gem_madvise
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1007 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW
, msm_ioctl_submitqueue_new
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1008 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE
, msm_ioctl_submitqueue_close
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1011 static const struct vm_operations_struct vm_ops
= {
1012 .fault
= msm_gem_fault
,
1013 .open
= drm_gem_vm_open
,
1014 .close
= drm_gem_vm_close
,
1017 static const struct file_operations fops
= {
1018 .owner
= THIS_MODULE
,
1020 .release
= drm_release
,
1021 .unlocked_ioctl
= drm_ioctl
,
1022 .compat_ioctl
= drm_compat_ioctl
,
1025 .llseek
= no_llseek
,
1026 .mmap
= msm_gem_mmap
,
1029 static struct drm_driver msm_driver
= {
1030 .driver_features
= DRIVER_HAVE_IRQ
|
1037 .postclose
= msm_postclose
,
1038 .lastclose
= drm_fb_helper_lastclose
,
1039 .irq_handler
= msm_irq
,
1040 .irq_preinstall
= msm_irq_preinstall
,
1041 .irq_postinstall
= msm_irq_postinstall
,
1042 .irq_uninstall
= msm_irq_uninstall
,
1043 .enable_vblank
= msm_enable_vblank
,
1044 .disable_vblank
= msm_disable_vblank
,
1045 .gem_free_object
= msm_gem_free_object
,
1046 .gem_vm_ops
= &vm_ops
,
1047 .dumb_create
= msm_gem_dumb_create
,
1048 .dumb_map_offset
= msm_gem_dumb_map_offset
,
1049 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
1050 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
1051 .gem_prime_export
= drm_gem_prime_export
,
1052 .gem_prime_import
= drm_gem_prime_import
,
1053 .gem_prime_res_obj
= msm_gem_prime_res_obj
,
1054 .gem_prime_pin
= msm_gem_prime_pin
,
1055 .gem_prime_unpin
= msm_gem_prime_unpin
,
1056 .gem_prime_get_sg_table
= msm_gem_prime_get_sg_table
,
1057 .gem_prime_import_sg_table
= msm_gem_prime_import_sg_table
,
1058 .gem_prime_vmap
= msm_gem_prime_vmap
,
1059 .gem_prime_vunmap
= msm_gem_prime_vunmap
,
1060 .gem_prime_mmap
= msm_gem_prime_mmap
,
1061 #ifdef CONFIG_DEBUG_FS
1062 .debugfs_init
= msm_debugfs_init
,
1064 .ioctls
= msm_ioctls
,
1065 .num_ioctls
= ARRAY_SIZE(msm_ioctls
),
1068 .desc
= "MSM Snapdragon DRM",
1070 .major
= MSM_VERSION_MAJOR
,
1071 .minor
= MSM_VERSION_MINOR
,
1072 .patchlevel
= MSM_VERSION_PATCHLEVEL
,
1075 #ifdef CONFIG_PM_SLEEP
1076 static int msm_pm_suspend(struct device
*dev
)
1078 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1079 struct msm_drm_private
*priv
= ddev
->dev_private
;
1080 struct msm_kms
*kms
= priv
->kms
;
1082 /* TODO: Use atomic helper suspend/resume */
1083 if (kms
&& kms
->funcs
&& kms
->funcs
->pm_suspend
)
1084 return kms
->funcs
->pm_suspend(dev
);
1086 drm_kms_helper_poll_disable(ddev
);
1088 priv
->pm_state
= drm_atomic_helper_suspend(ddev
);
1089 if (IS_ERR(priv
->pm_state
)) {
1090 drm_kms_helper_poll_enable(ddev
);
1091 return PTR_ERR(priv
->pm_state
);
1097 static int msm_pm_resume(struct device
*dev
)
1099 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1100 struct msm_drm_private
*priv
= ddev
->dev_private
;
1101 struct msm_kms
*kms
= priv
->kms
;
1103 /* TODO: Use atomic helper suspend/resume */
1104 if (kms
&& kms
->funcs
&& kms
->funcs
->pm_resume
)
1105 return kms
->funcs
->pm_resume(dev
);
1107 drm_atomic_helper_resume(ddev
, priv
->pm_state
);
1108 drm_kms_helper_poll_enable(ddev
);
1115 static int msm_runtime_suspend(struct device
*dev
)
1117 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1118 struct msm_drm_private
*priv
= ddev
->dev_private
;
1119 struct msm_mdss
*mdss
= priv
->mdss
;
1123 if (mdss
&& mdss
->funcs
)
1124 return mdss
->funcs
->disable(mdss
);
1129 static int msm_runtime_resume(struct device
*dev
)
1131 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1132 struct msm_drm_private
*priv
= ddev
->dev_private
;
1133 struct msm_mdss
*mdss
= priv
->mdss
;
1137 if (mdss
&& mdss
->funcs
)
1138 return mdss
->funcs
->enable(mdss
);
1144 static const struct dev_pm_ops msm_pm_ops
= {
1145 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend
, msm_pm_resume
)
1146 SET_RUNTIME_PM_OPS(msm_runtime_suspend
, msm_runtime_resume
, NULL
)
1150 * Componentized driver support:
1154 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1155 * so probably some room for some helpers
1157 static int compare_of(struct device
*dev
, void *data
)
1159 return dev
->of_node
== data
;
1163 * Identify what components need to be added by parsing what remote-endpoints
1164 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1165 * is no external component that we need to add since LVDS is within MDP4
1168 static int add_components_mdp(struct device
*mdp_dev
,
1169 struct component_match
**matchptr
)
1171 struct device_node
*np
= mdp_dev
->of_node
;
1172 struct device_node
*ep_node
;
1173 struct device
*master_dev
;
1176 * on MDP4 based platforms, the MDP platform device is the component
1177 * master that adds other display interface components to itself.
1179 * on MDP5 based platforms, the MDSS platform device is the component
1180 * master that adds MDP5 and other display interface components to
1183 if (of_device_is_compatible(np
, "qcom,mdp4"))
1184 master_dev
= mdp_dev
;
1186 master_dev
= mdp_dev
->parent
;
1188 for_each_endpoint_of_node(np
, ep_node
) {
1189 struct device_node
*intf
;
1190 struct of_endpoint ep
;
1193 ret
= of_graph_parse_endpoint(ep_node
, &ep
);
1195 dev_err(mdp_dev
, "unable to parse port endpoint\n");
1196 of_node_put(ep_node
);
1201 * The LCDC/LVDS port on MDP4 is a speacial case where the
1202 * remote-endpoint isn't a component that we need to add
1204 if (of_device_is_compatible(np
, "qcom,mdp4") &&
1209 * It's okay if some of the ports don't have a remote endpoint
1210 * specified. It just means that the port isn't connected to
1211 * any external interface.
1213 intf
= of_graph_get_remote_port_parent(ep_node
);
1217 drm_of_component_match_add(master_dev
, matchptr
, compare_of
,
1225 static int compare_name_mdp(struct device
*dev
, void *data
)
1227 return (strstr(dev_name(dev
), "mdp") != NULL
);
1230 static int add_display_components(struct device
*dev
,
1231 struct component_match
**matchptr
)
1233 struct device
*mdp_dev
;
1237 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1238 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1239 * Populate the children devices, find the MDP5/DPU node, and then add
1240 * the interfaces to our components list.
1242 if (of_device_is_compatible(dev
->of_node
, "qcom,mdss") ||
1243 of_device_is_compatible(dev
->of_node
, "qcom,sdm845-mdss")) {
1244 ret
= of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
1246 dev_err(dev
, "failed to populate children devices\n");
1250 mdp_dev
= device_find_child(dev
, NULL
, compare_name_mdp
);
1252 dev_err(dev
, "failed to find MDSS MDP node\n");
1253 of_platform_depopulate(dev
);
1257 put_device(mdp_dev
);
1259 /* add the MDP component itself */
1260 drm_of_component_match_add(dev
, matchptr
, compare_of
,
1267 ret
= add_components_mdp(mdp_dev
, matchptr
);
1269 of_platform_depopulate(dev
);
1275 * We don't know what's the best binding to link the gpu with the drm device.
1276 * Fow now, we just hunt for all the possible gpus that we support, and add them
1279 static const struct of_device_id msm_gpu_match
[] = {
1280 { .compatible
= "qcom,adreno" },
1281 { .compatible
= "qcom,adreno-3xx" },
1282 { .compatible
= "qcom,kgsl-3d0" },
1286 static int add_gpu_components(struct device
*dev
,
1287 struct component_match
**matchptr
)
1289 struct device_node
*np
;
1291 np
= of_find_matching_node(NULL
, msm_gpu_match
);
1295 if (of_device_is_available(np
))
1296 drm_of_component_match_add(dev
, matchptr
, compare_of
, np
);
1303 static int msm_drm_bind(struct device
*dev
)
1305 return msm_drm_init(dev
, &msm_driver
);
1308 static void msm_drm_unbind(struct device
*dev
)
1310 msm_drm_uninit(dev
);
1313 static const struct component_master_ops msm_drm_ops
= {
1314 .bind
= msm_drm_bind
,
1315 .unbind
= msm_drm_unbind
,
1322 static int msm_pdev_probe(struct platform_device
*pdev
)
1324 struct component_match
*match
= NULL
;
1327 ret
= add_display_components(&pdev
->dev
, &match
);
1331 ret
= add_gpu_components(&pdev
->dev
, &match
);
1335 /* on all devices that I am aware of, iommu's which can map
1336 * any address the cpu can see are used:
1338 ret
= dma_set_mask_and_coherent(&pdev
->dev
, ~0);
1342 ret
= component_master_add_with_match(&pdev
->dev
, &msm_drm_ops
, match
);
1349 of_platform_depopulate(&pdev
->dev
);
1353 static int msm_pdev_remove(struct platform_device
*pdev
)
1355 component_master_del(&pdev
->dev
, &msm_drm_ops
);
1356 of_platform_depopulate(&pdev
->dev
);
1361 static const struct of_device_id dt_match
[] = {
1362 { .compatible
= "qcom,mdp4", .data
= (void *)KMS_MDP4
},
1363 { .compatible
= "qcom,mdss", .data
= (void *)KMS_MDP5
},
1364 { .compatible
= "qcom,sdm845-mdss", .data
= (void *)KMS_DPU
},
1367 MODULE_DEVICE_TABLE(of
, dt_match
);
1369 static struct platform_driver msm_platform_driver
= {
1370 .probe
= msm_pdev_probe
,
1371 .remove
= msm_pdev_remove
,
1374 .of_match_table
= dt_match
,
1379 static int __init
msm_drm_register(void)
1389 msm_hdmi_register();
1391 return platform_driver_register(&msm_platform_driver
);
1394 static void __exit
msm_drm_unregister(void)
1397 platform_driver_unregister(&msm_platform_driver
);
1398 msm_hdmi_unregister();
1399 adreno_unregister();
1400 msm_edp_unregister();
1401 msm_dsi_unregister();
1402 msm_mdp_unregister();
1403 msm_dpu_unregister();
1406 module_init(msm_drm_register
);
1407 module_exit(msm_drm_unregister
);
1409 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1410 MODULE_DESCRIPTION("MSM DRM Driver");
1411 MODULE_LICENSE("GPL");