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"
29 #include "adreno/adreno_gpu.h"
34 * - 1.0.0 - initial interface
35 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
36 * - 1.2.0 - adds explicit fence support for submit ioctl
37 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
38 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
40 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
41 * GEM object's debug name
43 #define MSM_VERSION_MAJOR 1
44 #define MSM_VERSION_MINOR 4
45 #define MSM_VERSION_PATCHLEVEL 0
47 static const struct drm_mode_config_funcs mode_config_funcs
= {
48 .fb_create
= msm_framebuffer_create
,
49 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
50 .atomic_check
= drm_atomic_helper_check
,
51 .atomic_commit
= drm_atomic_helper_commit
,
54 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs
= {
55 .atomic_commit_tail
= msm_atomic_commit_tail
,
58 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
59 static bool reglog
= false;
60 MODULE_PARM_DESC(reglog
, "Enable register read/write logging");
61 module_param(reglog
, bool, 0600);
66 #ifdef CONFIG_DRM_FBDEV_EMULATION
67 static bool fbdev
= true;
68 MODULE_PARM_DESC(fbdev
, "Enable fbdev compat layer");
69 module_param(fbdev
, bool, 0600);
72 static char *vram
= "16m";
73 MODULE_PARM_DESC(vram
, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
74 module_param(vram
, charp
, 0);
76 bool dumpstate
= false;
77 MODULE_PARM_DESC(dumpstate
, "Dump KMS state on errors");
78 module_param(dumpstate
, bool, 0600);
80 static bool modeset
= true;
81 MODULE_PARM_DESC(modeset
, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
82 module_param(modeset
, bool, 0600);
88 int msm_clk_bulk_get(struct device
*dev
, struct clk_bulk_data
**bulk
)
90 struct property
*prop
;
92 struct clk_bulk_data
*local
;
93 int i
= 0, ret
, count
;
95 count
= of_property_count_strings(dev
->of_node
, "clock-names");
99 local
= devm_kcalloc(dev
, sizeof(struct clk_bulk_data
*),
104 of_property_for_each_string(dev
->of_node
, "clock-names", prop
, name
) {
105 local
[i
].id
= devm_kstrdup(dev
, name
, GFP_KERNEL
);
107 devm_kfree(dev
, local
);
114 ret
= devm_clk_bulk_get(dev
, count
, local
);
117 for (i
= 0; i
< count
; i
++)
118 devm_kfree(dev
, (void *) local
[i
].id
);
119 devm_kfree(dev
, local
);
128 struct clk
*msm_clk_bulk_get_clock(struct clk_bulk_data
*bulk
, int count
,
134 snprintf(n
, sizeof(n
), "%s_clk", name
);
136 for (i
= 0; bulk
&& i
< count
; i
++) {
137 if (!strcmp(bulk
[i
].id
, name
) || !strcmp(bulk
[i
].id
, n
))
145 struct clk
*msm_clk_get(struct platform_device
*pdev
, const char *name
)
150 clk
= devm_clk_get(&pdev
->dev
, name
);
151 if (!IS_ERR(clk
) || PTR_ERR(clk
) == -EPROBE_DEFER
)
154 snprintf(name2
, sizeof(name2
), "%s_clk", name
);
156 clk
= devm_clk_get(&pdev
->dev
, name2
);
158 dev_warn(&pdev
->dev
, "Using legacy clk name binding. Use "
159 "\"%s\" instead of \"%s\"\n", name
, name2
);
164 void __iomem
*msm_ioremap(struct platform_device
*pdev
, const char *name
,
167 struct resource
*res
;
172 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
174 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
177 DRM_DEV_ERROR(&pdev
->dev
, "failed to get memory resource: %s\n", name
);
178 return ERR_PTR(-EINVAL
);
181 size
= resource_size(res
);
183 ptr
= devm_ioremap_nocache(&pdev
->dev
, res
->start
, size
);
185 DRM_DEV_ERROR(&pdev
->dev
, "failed to ioremap: %s\n", name
);
186 return ERR_PTR(-ENOMEM
);
190 printk(KERN_DEBUG
"IO:region %s %p %08lx\n", dbgname
, ptr
, size
);
195 void msm_writel(u32 data
, void __iomem
*addr
)
198 printk(KERN_DEBUG
"IO:W %p %08x\n", addr
, data
);
202 u32
msm_readl(const void __iomem
*addr
)
204 u32 val
= readl(addr
);
206 pr_err("IO:R %p %08x\n", addr
, val
);
210 struct vblank_event
{
211 struct list_head node
;
216 static void vblank_ctrl_worker(struct kthread_work
*work
)
218 struct msm_vblank_ctrl
*vbl_ctrl
= container_of(work
,
219 struct msm_vblank_ctrl
, work
);
220 struct msm_drm_private
*priv
= container_of(vbl_ctrl
,
221 struct msm_drm_private
, vblank_ctrl
);
222 struct msm_kms
*kms
= priv
->kms
;
223 struct vblank_event
*vbl_ev
, *tmp
;
226 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
227 list_for_each_entry_safe(vbl_ev
, tmp
, &vbl_ctrl
->event_list
, node
) {
228 list_del(&vbl_ev
->node
);
229 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
232 kms
->funcs
->enable_vblank(kms
,
233 priv
->crtcs
[vbl_ev
->crtc_id
]);
235 kms
->funcs
->disable_vblank(kms
,
236 priv
->crtcs
[vbl_ev
->crtc_id
]);
240 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
243 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
246 static int vblank_ctrl_queue_work(struct msm_drm_private
*priv
,
247 int crtc_id
, bool enable
)
249 struct msm_vblank_ctrl
*vbl_ctrl
= &priv
->vblank_ctrl
;
250 struct vblank_event
*vbl_ev
;
253 vbl_ev
= kzalloc(sizeof(*vbl_ev
), GFP_ATOMIC
);
257 vbl_ev
->crtc_id
= crtc_id
;
258 vbl_ev
->enable
= enable
;
260 spin_lock_irqsave(&vbl_ctrl
->lock
, flags
);
261 list_add_tail(&vbl_ev
->node
, &vbl_ctrl
->event_list
);
262 spin_unlock_irqrestore(&vbl_ctrl
->lock
, flags
);
264 kthread_queue_work(&priv
->disp_thread
[crtc_id
].worker
,
270 static int msm_drm_uninit(struct device
*dev
)
272 struct platform_device
*pdev
= to_platform_device(dev
);
273 struct drm_device
*ddev
= platform_get_drvdata(pdev
);
274 struct msm_drm_private
*priv
= ddev
->dev_private
;
275 struct msm_kms
*kms
= priv
->kms
;
276 struct msm_mdss
*mdss
= priv
->mdss
;
277 struct msm_vblank_ctrl
*vbl_ctrl
= &priv
->vblank_ctrl
;
278 struct vblank_event
*vbl_ev
, *tmp
;
281 /* We must cancel and cleanup any pending vblank enable/disable
282 * work before drm_irq_uninstall() to avoid work re-enabling an
283 * irq after uninstall has disabled it.
285 kthread_flush_work(&vbl_ctrl
->work
);
286 list_for_each_entry_safe(vbl_ev
, tmp
, &vbl_ctrl
->event_list
, node
) {
287 list_del(&vbl_ev
->node
);
291 /* clean up display commit/event worker threads */
292 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
293 if (priv
->disp_thread
[i
].thread
) {
294 kthread_flush_worker(&priv
->disp_thread
[i
].worker
);
295 kthread_stop(priv
->disp_thread
[i
].thread
);
296 priv
->disp_thread
[i
].thread
= NULL
;
299 if (priv
->event_thread
[i
].thread
) {
300 kthread_flush_worker(&priv
->event_thread
[i
].worker
);
301 kthread_stop(priv
->event_thread
[i
].thread
);
302 priv
->event_thread
[i
].thread
= NULL
;
306 msm_gem_shrinker_cleanup(ddev
);
308 drm_kms_helper_poll_fini(ddev
);
310 drm_dev_unregister(ddev
);
312 msm_perf_debugfs_cleanup(priv
);
313 msm_rd_debugfs_cleanup(priv
);
315 #ifdef CONFIG_DRM_FBDEV_EMULATION
316 if (fbdev
&& priv
->fbdev
)
317 msm_fbdev_free(ddev
);
319 drm_atomic_helper_shutdown(ddev
);
320 drm_mode_config_cleanup(ddev
);
322 pm_runtime_get_sync(dev
);
323 drm_irq_uninstall(ddev
);
324 pm_runtime_put_sync(dev
);
326 flush_workqueue(priv
->wq
);
327 destroy_workqueue(priv
->wq
);
329 if (kms
&& kms
->funcs
)
330 kms
->funcs
->destroy(kms
);
332 if (priv
->vram
.paddr
) {
333 unsigned long attrs
= DMA_ATTR_NO_KERNEL_MAPPING
;
334 drm_mm_takedown(&priv
->vram
.mm
);
335 dma_free_attrs(dev
, priv
->vram
.size
, NULL
,
336 priv
->vram
.paddr
, attrs
);
339 component_unbind_all(dev
, ddev
);
341 if (mdss
&& mdss
->funcs
)
342 mdss
->funcs
->destroy(ddev
);
344 ddev
->dev_private
= NULL
;
356 static int get_mdp_ver(struct platform_device
*pdev
)
358 struct device
*dev
= &pdev
->dev
;
360 return (int) (unsigned long) of_device_get_match_data(dev
);
363 #include <linux/of_address.h>
365 bool msm_use_mmu(struct drm_device
*dev
)
367 struct msm_drm_private
*priv
= dev
->dev_private
;
369 /* a2xx comes with its own MMU */
370 return priv
->is_a2xx
|| iommu_present(&platform_bus_type
);
373 static int msm_init_vram(struct drm_device
*dev
)
375 struct msm_drm_private
*priv
= dev
->dev_private
;
376 struct device_node
*node
;
377 unsigned long size
= 0;
380 /* In the device-tree world, we could have a 'memory-region'
381 * phandle, which gives us a link to our "vram". Allocating
382 * is all nicely abstracted behind the dma api, but we need
383 * to know the entire size to allocate it all in one go. There
385 * 1) device with no IOMMU, in which case we need exclusive
386 * access to a VRAM carveout big enough for all gpu
388 * 2) device with IOMMU, but where the bootloader puts up
389 * a splash screen. In this case, the VRAM carveout
390 * need only be large enough for fbdev fb. But we need
391 * exclusive access to the buffer to avoid the kernel
392 * using those pages for other purposes (which appears
393 * as corruption on screen before we have a chance to
394 * load and do initial modeset)
397 node
= of_parse_phandle(dev
->dev
->of_node
, "memory-region", 0);
400 ret
= of_address_to_resource(node
, 0, &r
);
404 size
= r
.end
- r
.start
;
405 DRM_INFO("using VRAM carveout: %lx@%pa\n", size
, &r
.start
);
407 /* if we have no IOMMU, then we need to use carveout allocator.
408 * Grab the entire CMA chunk carved out in early startup in
411 } else if (!msm_use_mmu(dev
)) {
412 DRM_INFO("using %s VRAM carveout\n", vram
);
413 size
= memparse(vram
, NULL
);
417 unsigned long attrs
= 0;
420 priv
->vram
.size
= size
;
422 drm_mm_init(&priv
->vram
.mm
, 0, (size
>> PAGE_SHIFT
) - 1);
423 spin_lock_init(&priv
->vram
.lock
);
425 attrs
|= DMA_ATTR_NO_KERNEL_MAPPING
;
426 attrs
|= DMA_ATTR_WRITE_COMBINE
;
428 /* note that for no-kernel-mapping, the vaddr returned
429 * is bogus, but non-null if allocation succeeded:
431 p
= dma_alloc_attrs(dev
->dev
, size
,
432 &priv
->vram
.paddr
, GFP_KERNEL
, attrs
);
434 DRM_DEV_ERROR(dev
->dev
, "failed to allocate VRAM\n");
435 priv
->vram
.paddr
= 0;
439 DRM_DEV_INFO(dev
->dev
, "VRAM: %08x->%08x\n",
440 (uint32_t)priv
->vram
.paddr
,
441 (uint32_t)(priv
->vram
.paddr
+ size
));
447 static int msm_drm_init(struct device
*dev
, struct drm_driver
*drv
)
449 struct platform_device
*pdev
= to_platform_device(dev
);
450 struct drm_device
*ddev
;
451 struct msm_drm_private
*priv
;
453 struct msm_mdss
*mdss
;
455 struct sched_param param
;
457 ddev
= drm_dev_alloc(drv
, dev
);
459 DRM_DEV_ERROR(dev
, "failed to allocate drm_device\n");
460 return PTR_ERR(ddev
);
463 platform_set_drvdata(pdev
, ddev
);
465 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
468 goto err_put_drm_dev
;
471 ddev
->dev_private
= priv
;
474 switch (get_mdp_ver(pdev
)) {
476 ret
= mdp5_mdss_init(ddev
);
479 ret
= dpu_mdss_init(ddev
);
490 priv
->wq
= alloc_ordered_workqueue("msm", 0);
492 INIT_LIST_HEAD(&priv
->inactive_list
);
493 INIT_LIST_HEAD(&priv
->vblank_ctrl
.event_list
);
494 kthread_init_work(&priv
->vblank_ctrl
.work
, vblank_ctrl_worker
);
495 spin_lock_init(&priv
->vblank_ctrl
.lock
);
497 drm_mode_config_init(ddev
);
499 /* Bind all our sub-components: */
500 ret
= component_bind_all(dev
, ddev
);
502 goto err_destroy_mdss
;
504 ret
= msm_init_vram(ddev
);
508 msm_gem_shrinker_init(ddev
);
510 switch (get_mdp_ver(pdev
)) {
512 kms
= mdp4_kms_init(ddev
);
516 kms
= mdp5_kms_init(ddev
);
519 kms
= dpu_kms_init(ddev
);
523 /* valid only for the dummy headless case, where of_node=NULL */
524 WARN_ON(dev
->of_node
);
530 DRM_DEV_ERROR(dev
, "failed to load kms\n");
536 /* Enable normalization of plane zpos */
537 ddev
->mode_config
.normalize_zpos
= true;
540 ret
= kms
->funcs
->hw_init(kms
);
542 DRM_DEV_ERROR(dev
, "kms hw init failed: %d\n", ret
);
547 ddev
->mode_config
.funcs
= &mode_config_funcs
;
548 ddev
->mode_config
.helper_private
= &mode_config_helper_funcs
;
551 * this priority was found during empiric testing to have appropriate
552 * realtime scheduling to process display updates and interact with
553 * other real time and normal priority task
555 param
.sched_priority
= 16;
556 for (i
= 0; i
< priv
->num_crtcs
; i
++) {
558 /* initialize display thread */
559 priv
->disp_thread
[i
].crtc_id
= priv
->crtcs
[i
]->base
.id
;
560 kthread_init_worker(&priv
->disp_thread
[i
].worker
);
561 priv
->disp_thread
[i
].dev
= ddev
;
562 priv
->disp_thread
[i
].thread
=
563 kthread_run(kthread_worker_fn
,
564 &priv
->disp_thread
[i
].worker
,
565 "crtc_commit:%d", priv
->disp_thread
[i
].crtc_id
);
566 if (IS_ERR(priv
->disp_thread
[i
].thread
)) {
567 DRM_DEV_ERROR(dev
, "failed to create crtc_commit kthread\n");
568 priv
->disp_thread
[i
].thread
= NULL
;
572 ret
= sched_setscheduler(priv
->disp_thread
[i
].thread
,
575 dev_warn(dev
, "disp_thread set priority failed: %d\n",
578 /* initialize event thread */
579 priv
->event_thread
[i
].crtc_id
= priv
->crtcs
[i
]->base
.id
;
580 kthread_init_worker(&priv
->event_thread
[i
].worker
);
581 priv
->event_thread
[i
].dev
= ddev
;
582 priv
->event_thread
[i
].thread
=
583 kthread_run(kthread_worker_fn
,
584 &priv
->event_thread
[i
].worker
,
585 "crtc_event:%d", priv
->event_thread
[i
].crtc_id
);
586 if (IS_ERR(priv
->event_thread
[i
].thread
)) {
587 DRM_DEV_ERROR(dev
, "failed to create crtc_event kthread\n");
588 priv
->event_thread
[i
].thread
= NULL
;
593 * event thread should also run at same priority as disp_thread
594 * because it is handling frame_done events. A lower priority
595 * event thread and higher priority disp_thread can causes
596 * frame_pending counters beyond 2. This can lead to commit
597 * failure at crtc commit level.
599 ret
= sched_setscheduler(priv
->event_thread
[i
].thread
,
602 dev_warn(dev
, "event_thread set priority failed:%d\n",
606 ret
= drm_vblank_init(ddev
, priv
->num_crtcs
);
608 DRM_DEV_ERROR(dev
, "failed to initialize vblank\n");
613 pm_runtime_get_sync(dev
);
614 ret
= drm_irq_install(ddev
, kms
->irq
);
615 pm_runtime_put_sync(dev
);
617 DRM_DEV_ERROR(dev
, "failed to install IRQ handler\n");
622 ret
= drm_dev_register(ddev
, 0);
626 drm_mode_config_reset(ddev
);
628 #ifdef CONFIG_DRM_FBDEV_EMULATION
630 priv
->fbdev
= msm_fbdev_init(ddev
);
633 ret
= msm_debugfs_late_init(ddev
);
637 drm_kms_helper_poll_init(ddev
);
645 if (mdss
&& mdss
->funcs
)
646 mdss
->funcs
->destroy(ddev
);
658 static void load_gpu(struct drm_device
*dev
)
660 static DEFINE_MUTEX(init_lock
);
661 struct msm_drm_private
*priv
= dev
->dev_private
;
663 mutex_lock(&init_lock
);
666 priv
->gpu
= adreno_load_gpu(dev
);
668 mutex_unlock(&init_lock
);
671 static int context_init(struct drm_device
*dev
, struct drm_file
*file
)
673 struct msm_file_private
*ctx
;
675 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
679 msm_submitqueue_init(dev
, ctx
);
681 file
->driver_priv
= ctx
;
686 static int msm_open(struct drm_device
*dev
, struct drm_file
*file
)
688 /* For now, load gpu on open.. to avoid the requirement of having
689 * firmware in the initrd.
693 return context_init(dev
, file
);
696 static void context_close(struct msm_file_private
*ctx
)
698 msm_submitqueue_close(ctx
);
702 static void msm_postclose(struct drm_device
*dev
, struct drm_file
*file
)
704 struct msm_drm_private
*priv
= dev
->dev_private
;
705 struct msm_file_private
*ctx
= file
->driver_priv
;
707 mutex_lock(&dev
->struct_mutex
);
708 if (ctx
== priv
->lastctx
)
709 priv
->lastctx
= NULL
;
710 mutex_unlock(&dev
->struct_mutex
);
715 static irqreturn_t
msm_irq(int irq
, void *arg
)
717 struct drm_device
*dev
= arg
;
718 struct msm_drm_private
*priv
= dev
->dev_private
;
719 struct msm_kms
*kms
= priv
->kms
;
721 return kms
->funcs
->irq(kms
);
724 static void msm_irq_preinstall(struct drm_device
*dev
)
726 struct msm_drm_private
*priv
= dev
->dev_private
;
727 struct msm_kms
*kms
= priv
->kms
;
729 kms
->funcs
->irq_preinstall(kms
);
732 static int msm_irq_postinstall(struct drm_device
*dev
)
734 struct msm_drm_private
*priv
= dev
->dev_private
;
735 struct msm_kms
*kms
= priv
->kms
;
738 if (kms
->funcs
->irq_postinstall
)
739 return kms
->funcs
->irq_postinstall(kms
);
744 static void msm_irq_uninstall(struct drm_device
*dev
)
746 struct msm_drm_private
*priv
= dev
->dev_private
;
747 struct msm_kms
*kms
= priv
->kms
;
749 kms
->funcs
->irq_uninstall(kms
);
752 static int msm_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
754 struct msm_drm_private
*priv
= dev
->dev_private
;
755 struct msm_kms
*kms
= priv
->kms
;
758 DBG("dev=%p, crtc=%u", dev
, pipe
);
759 return vblank_ctrl_queue_work(priv
, pipe
, true);
762 static void msm_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
764 struct msm_drm_private
*priv
= dev
->dev_private
;
765 struct msm_kms
*kms
= priv
->kms
;
768 DBG("dev=%p, crtc=%u", dev
, pipe
);
769 vblank_ctrl_queue_work(priv
, pipe
, false);
776 static int msm_ioctl_get_param(struct drm_device
*dev
, void *data
,
777 struct drm_file
*file
)
779 struct msm_drm_private
*priv
= dev
->dev_private
;
780 struct drm_msm_param
*args
= data
;
783 /* for now, we just have 3d pipe.. eventually this would need to
784 * be more clever to dispatch to appropriate gpu module:
786 if (args
->pipe
!= MSM_PIPE_3D0
)
794 return gpu
->funcs
->get_param(gpu
, args
->param
, &args
->value
);
797 static int msm_ioctl_gem_new(struct drm_device
*dev
, void *data
,
798 struct drm_file
*file
)
800 struct drm_msm_gem_new
*args
= data
;
802 if (args
->flags
& ~MSM_BO_FLAGS
) {
803 DRM_ERROR("invalid flags: %08x\n", args
->flags
);
807 return msm_gem_new_handle(dev
, file
, args
->size
,
808 args
->flags
, &args
->handle
, NULL
);
811 static inline ktime_t
to_ktime(struct drm_msm_timespec timeout
)
813 return ktime_set(timeout
.tv_sec
, timeout
.tv_nsec
);
816 static int msm_ioctl_gem_cpu_prep(struct drm_device
*dev
, void *data
,
817 struct drm_file
*file
)
819 struct drm_msm_gem_cpu_prep
*args
= data
;
820 struct drm_gem_object
*obj
;
821 ktime_t timeout
= to_ktime(args
->timeout
);
824 if (args
->op
& ~MSM_PREP_FLAGS
) {
825 DRM_ERROR("invalid op: %08x\n", args
->op
);
829 obj
= drm_gem_object_lookup(file
, args
->handle
);
833 ret
= msm_gem_cpu_prep(obj
, args
->op
, &timeout
);
835 drm_gem_object_put_unlocked(obj
);
840 static int msm_ioctl_gem_cpu_fini(struct drm_device
*dev
, void *data
,
841 struct drm_file
*file
)
843 struct drm_msm_gem_cpu_fini
*args
= data
;
844 struct drm_gem_object
*obj
;
847 obj
= drm_gem_object_lookup(file
, args
->handle
);
851 ret
= msm_gem_cpu_fini(obj
);
853 drm_gem_object_put_unlocked(obj
);
858 static int msm_ioctl_gem_info_iova(struct drm_device
*dev
,
859 struct drm_gem_object
*obj
, uint64_t *iova
)
861 struct msm_drm_private
*priv
= dev
->dev_private
;
867 * Don't pin the memory here - just get an address so that userspace can
870 return msm_gem_get_iova(obj
, priv
->gpu
->aspace
, iova
);
873 static int msm_ioctl_gem_info(struct drm_device
*dev
, void *data
,
874 struct drm_file
*file
)
876 struct drm_msm_gem_info
*args
= data
;
877 struct drm_gem_object
*obj
;
878 struct msm_gem_object
*msm_obj
;
884 switch (args
->info
) {
885 case MSM_INFO_GET_OFFSET
:
886 case MSM_INFO_GET_IOVA
:
887 /* value returned as immediate, not pointer, so len==0: */
891 case MSM_INFO_SET_NAME
:
892 case MSM_INFO_GET_NAME
:
898 obj
= drm_gem_object_lookup(file
, args
->handle
);
902 msm_obj
= to_msm_bo(obj
);
904 switch (args
->info
) {
905 case MSM_INFO_GET_OFFSET
:
906 args
->value
= msm_gem_mmap_offset(obj
);
908 case MSM_INFO_GET_IOVA
:
909 ret
= msm_ioctl_gem_info_iova(dev
, obj
, &args
->value
);
911 case MSM_INFO_SET_NAME
:
912 /* length check should leave room for terminating null: */
913 if (args
->len
>= sizeof(msm_obj
->name
)) {
917 ret
= copy_from_user(msm_obj
->name
,
918 u64_to_user_ptr(args
->value
), args
->len
);
919 msm_obj
->name
[args
->len
] = '\0';
920 for (i
= 0; i
< args
->len
; i
++) {
921 if (!isprint(msm_obj
->name
[i
])) {
922 msm_obj
->name
[i
] = '\0';
927 case MSM_INFO_GET_NAME
:
928 if (args
->value
&& (args
->len
< strlen(msm_obj
->name
))) {
932 args
->len
= strlen(msm_obj
->name
);
934 ret
= copy_to_user(u64_to_user_ptr(args
->value
),
935 msm_obj
->name
, args
->len
);
940 drm_gem_object_put_unlocked(obj
);
945 static int msm_ioctl_wait_fence(struct drm_device
*dev
, void *data
,
946 struct drm_file
*file
)
948 struct msm_drm_private
*priv
= dev
->dev_private
;
949 struct drm_msm_wait_fence
*args
= data
;
950 ktime_t timeout
= to_ktime(args
->timeout
);
951 struct msm_gpu_submitqueue
*queue
;
952 struct msm_gpu
*gpu
= priv
->gpu
;
956 DRM_ERROR("invalid pad: %08x\n", args
->pad
);
963 queue
= msm_submitqueue_get(file
->driver_priv
, args
->queueid
);
967 ret
= msm_wait_fence(gpu
->rb
[queue
->prio
]->fctx
, args
->fence
, &timeout
,
970 msm_submitqueue_put(queue
);
974 static int msm_ioctl_gem_madvise(struct drm_device
*dev
, void *data
,
975 struct drm_file
*file
)
977 struct drm_msm_gem_madvise
*args
= data
;
978 struct drm_gem_object
*obj
;
981 switch (args
->madv
) {
982 case MSM_MADV_DONTNEED
:
983 case MSM_MADV_WILLNEED
:
989 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
993 obj
= drm_gem_object_lookup(file
, args
->handle
);
999 ret
= msm_gem_madvise(obj
, args
->madv
);
1001 args
->retained
= ret
;
1005 drm_gem_object_put(obj
);
1008 mutex_unlock(&dev
->struct_mutex
);
1013 static int msm_ioctl_submitqueue_new(struct drm_device
*dev
, void *data
,
1014 struct drm_file
*file
)
1016 struct drm_msm_submitqueue
*args
= data
;
1018 if (args
->flags
& ~MSM_SUBMITQUEUE_FLAGS
)
1021 return msm_submitqueue_create(dev
, file
->driver_priv
, args
->prio
,
1022 args
->flags
, &args
->id
);
1026 static int msm_ioctl_submitqueue_close(struct drm_device
*dev
, void *data
,
1027 struct drm_file
*file
)
1029 u32 id
= *(u32
*) data
;
1031 return msm_submitqueue_remove(file
->driver_priv
, id
);
1034 static const struct drm_ioctl_desc msm_ioctls
[] = {
1035 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM
, msm_ioctl_get_param
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1036 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW
, msm_ioctl_gem_new
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1037 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO
, msm_ioctl_gem_info
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1038 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP
, msm_ioctl_gem_cpu_prep
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1039 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI
, msm_ioctl_gem_cpu_fini
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1040 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT
, msm_ioctl_gem_submit
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1041 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE
, msm_ioctl_wait_fence
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1042 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE
, msm_ioctl_gem_madvise
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1043 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW
, msm_ioctl_submitqueue_new
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1044 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE
, msm_ioctl_submitqueue_close
, DRM_AUTH
|DRM_RENDER_ALLOW
),
1047 static const struct vm_operations_struct vm_ops
= {
1048 .fault
= msm_gem_fault
,
1049 .open
= drm_gem_vm_open
,
1050 .close
= drm_gem_vm_close
,
1053 static const struct file_operations fops
= {
1054 .owner
= THIS_MODULE
,
1056 .release
= drm_release
,
1057 .unlocked_ioctl
= drm_ioctl
,
1058 .compat_ioctl
= drm_compat_ioctl
,
1061 .llseek
= no_llseek
,
1062 .mmap
= msm_gem_mmap
,
1065 static struct drm_driver msm_driver
= {
1066 .driver_features
= DRIVER_HAVE_IRQ
|
1073 .postclose
= msm_postclose
,
1074 .lastclose
= drm_fb_helper_lastclose
,
1075 .irq_handler
= msm_irq
,
1076 .irq_preinstall
= msm_irq_preinstall
,
1077 .irq_postinstall
= msm_irq_postinstall
,
1078 .irq_uninstall
= msm_irq_uninstall
,
1079 .enable_vblank
= msm_enable_vblank
,
1080 .disable_vblank
= msm_disable_vblank
,
1081 .gem_free_object
= msm_gem_free_object
,
1082 .gem_vm_ops
= &vm_ops
,
1083 .dumb_create
= msm_gem_dumb_create
,
1084 .dumb_map_offset
= msm_gem_dumb_map_offset
,
1085 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
1086 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
1087 .gem_prime_export
= drm_gem_prime_export
,
1088 .gem_prime_import
= drm_gem_prime_import
,
1089 .gem_prime_res_obj
= msm_gem_prime_res_obj
,
1090 .gem_prime_pin
= msm_gem_prime_pin
,
1091 .gem_prime_unpin
= msm_gem_prime_unpin
,
1092 .gem_prime_get_sg_table
= msm_gem_prime_get_sg_table
,
1093 .gem_prime_import_sg_table
= msm_gem_prime_import_sg_table
,
1094 .gem_prime_vmap
= msm_gem_prime_vmap
,
1095 .gem_prime_vunmap
= msm_gem_prime_vunmap
,
1096 .gem_prime_mmap
= msm_gem_prime_mmap
,
1097 #ifdef CONFIG_DEBUG_FS
1098 .debugfs_init
= msm_debugfs_init
,
1100 .ioctls
= msm_ioctls
,
1101 .num_ioctls
= ARRAY_SIZE(msm_ioctls
),
1104 .desc
= "MSM Snapdragon DRM",
1106 .major
= MSM_VERSION_MAJOR
,
1107 .minor
= MSM_VERSION_MINOR
,
1108 .patchlevel
= MSM_VERSION_PATCHLEVEL
,
1111 #ifdef CONFIG_PM_SLEEP
1112 static int msm_pm_suspend(struct device
*dev
)
1114 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1115 struct msm_drm_private
*priv
= ddev
->dev_private
;
1117 if (WARN_ON(priv
->pm_state
))
1118 drm_atomic_state_put(priv
->pm_state
);
1120 priv
->pm_state
= drm_atomic_helper_suspend(ddev
);
1121 if (IS_ERR(priv
->pm_state
)) {
1122 int ret
= PTR_ERR(priv
->pm_state
);
1123 DRM_ERROR("Failed to suspend dpu, %d\n", ret
);
1130 static int msm_pm_resume(struct device
*dev
)
1132 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1133 struct msm_drm_private
*priv
= ddev
->dev_private
;
1136 if (WARN_ON(!priv
->pm_state
))
1139 ret
= drm_atomic_helper_resume(ddev
, priv
->pm_state
);
1141 priv
->pm_state
= NULL
;
1148 static int msm_runtime_suspend(struct device
*dev
)
1150 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1151 struct msm_drm_private
*priv
= ddev
->dev_private
;
1152 struct msm_mdss
*mdss
= priv
->mdss
;
1156 if (mdss
&& mdss
->funcs
)
1157 return mdss
->funcs
->disable(mdss
);
1162 static int msm_runtime_resume(struct device
*dev
)
1164 struct drm_device
*ddev
= dev_get_drvdata(dev
);
1165 struct msm_drm_private
*priv
= ddev
->dev_private
;
1166 struct msm_mdss
*mdss
= priv
->mdss
;
1170 if (mdss
&& mdss
->funcs
)
1171 return mdss
->funcs
->enable(mdss
);
1177 static const struct dev_pm_ops msm_pm_ops
= {
1178 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend
, msm_pm_resume
)
1179 SET_RUNTIME_PM_OPS(msm_runtime_suspend
, msm_runtime_resume
, NULL
)
1183 * Componentized driver support:
1187 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1188 * so probably some room for some helpers
1190 static int compare_of(struct device
*dev
, void *data
)
1192 return dev
->of_node
== data
;
1196 * Identify what components need to be added by parsing what remote-endpoints
1197 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1198 * is no external component that we need to add since LVDS is within MDP4
1201 static int add_components_mdp(struct device
*mdp_dev
,
1202 struct component_match
**matchptr
)
1204 struct device_node
*np
= mdp_dev
->of_node
;
1205 struct device_node
*ep_node
;
1206 struct device
*master_dev
;
1209 * on MDP4 based platforms, the MDP platform device is the component
1210 * master that adds other display interface components to itself.
1212 * on MDP5 based platforms, the MDSS platform device is the component
1213 * master that adds MDP5 and other display interface components to
1216 if (of_device_is_compatible(np
, "qcom,mdp4"))
1217 master_dev
= mdp_dev
;
1219 master_dev
= mdp_dev
->parent
;
1221 for_each_endpoint_of_node(np
, ep_node
) {
1222 struct device_node
*intf
;
1223 struct of_endpoint ep
;
1226 ret
= of_graph_parse_endpoint(ep_node
, &ep
);
1228 DRM_DEV_ERROR(mdp_dev
, "unable to parse port endpoint\n");
1229 of_node_put(ep_node
);
1234 * The LCDC/LVDS port on MDP4 is a speacial case where the
1235 * remote-endpoint isn't a component that we need to add
1237 if (of_device_is_compatible(np
, "qcom,mdp4") &&
1242 * It's okay if some of the ports don't have a remote endpoint
1243 * specified. It just means that the port isn't connected to
1244 * any external interface.
1246 intf
= of_graph_get_remote_port_parent(ep_node
);
1250 if (of_device_is_available(intf
))
1251 drm_of_component_match_add(master_dev
, matchptr
,
1260 static int compare_name_mdp(struct device
*dev
, void *data
)
1262 return (strstr(dev_name(dev
), "mdp") != NULL
);
1265 static int add_display_components(struct device
*dev
,
1266 struct component_match
**matchptr
)
1268 struct device
*mdp_dev
;
1272 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1273 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1274 * Populate the children devices, find the MDP5/DPU node, and then add
1275 * the interfaces to our components list.
1277 if (of_device_is_compatible(dev
->of_node
, "qcom,mdss") ||
1278 of_device_is_compatible(dev
->of_node
, "qcom,sdm845-mdss")) {
1279 ret
= of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
1281 DRM_DEV_ERROR(dev
, "failed to populate children devices\n");
1285 mdp_dev
= device_find_child(dev
, NULL
, compare_name_mdp
);
1287 DRM_DEV_ERROR(dev
, "failed to find MDSS MDP node\n");
1288 of_platform_depopulate(dev
);
1292 put_device(mdp_dev
);
1294 /* add the MDP component itself */
1295 drm_of_component_match_add(dev
, matchptr
, compare_of
,
1302 ret
= add_components_mdp(mdp_dev
, matchptr
);
1304 of_platform_depopulate(dev
);
1310 * We don't know what's the best binding to link the gpu with the drm device.
1311 * Fow now, we just hunt for all the possible gpus that we support, and add them
1314 static const struct of_device_id msm_gpu_match
[] = {
1315 { .compatible
= "qcom,adreno" },
1316 { .compatible
= "qcom,adreno-3xx" },
1317 { .compatible
= "amd,imageon" },
1318 { .compatible
= "qcom,kgsl-3d0" },
1322 static int add_gpu_components(struct device
*dev
,
1323 struct component_match
**matchptr
)
1325 struct device_node
*np
;
1327 np
= of_find_matching_node(NULL
, msm_gpu_match
);
1331 drm_of_component_match_add(dev
, matchptr
, compare_of
, np
);
1338 static int msm_drm_bind(struct device
*dev
)
1340 return msm_drm_init(dev
, &msm_driver
);
1343 static void msm_drm_unbind(struct device
*dev
)
1345 msm_drm_uninit(dev
);
1348 static const struct component_master_ops msm_drm_ops
= {
1349 .bind
= msm_drm_bind
,
1350 .unbind
= msm_drm_unbind
,
1357 static int msm_pdev_probe(struct platform_device
*pdev
)
1359 struct component_match
*match
= NULL
;
1362 if (get_mdp_ver(pdev
)) {
1363 ret
= add_display_components(&pdev
->dev
, &match
);
1368 ret
= add_gpu_components(&pdev
->dev
, &match
);
1372 /* on all devices that I am aware of, iommu's which can map
1373 * any address the cpu can see are used:
1375 ret
= dma_set_mask_and_coherent(&pdev
->dev
, ~0);
1379 return component_master_add_with_match(&pdev
->dev
, &msm_drm_ops
, match
);
1382 static int msm_pdev_remove(struct platform_device
*pdev
)
1384 component_master_del(&pdev
->dev
, &msm_drm_ops
);
1385 of_platform_depopulate(&pdev
->dev
);
1390 static const struct of_device_id dt_match
[] = {
1391 { .compatible
= "qcom,mdp4", .data
= (void *)KMS_MDP4
},
1392 { .compatible
= "qcom,mdss", .data
= (void *)KMS_MDP5
},
1393 { .compatible
= "qcom,sdm845-mdss", .data
= (void *)KMS_DPU
},
1396 MODULE_DEVICE_TABLE(of
, dt_match
);
1398 static struct platform_driver msm_platform_driver
= {
1399 .probe
= msm_pdev_probe
,
1400 .remove
= msm_pdev_remove
,
1403 .of_match_table
= dt_match
,
1408 static int __init
msm_drm_register(void)
1418 msm_hdmi_register();
1420 return platform_driver_register(&msm_platform_driver
);
1423 static void __exit
msm_drm_unregister(void)
1426 platform_driver_unregister(&msm_platform_driver
);
1427 msm_hdmi_unregister();
1428 adreno_unregister();
1429 msm_edp_unregister();
1430 msm_dsi_unregister();
1431 msm_mdp_unregister();
1432 msm_dpu_unregister();
1435 module_init(msm_drm_register
);
1436 module_exit(msm_drm_unregister
);
1438 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1439 MODULE_DESCRIPTION("MSM DRM Driver");
1440 MODULE_LICENSE("GPL");