drm/msm/mdp5: Add support for msm8x74v1
[linux-2.6-block.git] / drivers / gpu / drm / msm / msm_drv.c
CommitLineData
c8afe684
RC
1/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "msm_drv.h"
7198e6b0 19#include "msm_gpu.h"
dd2da6e3 20#include "msm_kms.h"
c8afe684 21
c8afe684
RC
22static void msm_fb_output_poll_changed(struct drm_device *dev)
23{
a2ca7789 24#ifdef CONFIG_DRM_MSM_FBDEV
c8afe684
RC
25 struct msm_drm_private *priv = dev->dev_private;
26 if (priv->fbdev)
27 drm_fb_helper_hotplug_event(priv->fbdev);
a2ca7789 28#endif
c8afe684
RC
29}
30
31static const struct drm_mode_config_funcs mode_config_funcs = {
32 .fb_create = msm_framebuffer_create,
33 .output_poll_changed = msm_fb_output_poll_changed,
b4274fbe 34 .atomic_check = msm_atomic_check,
cf3a7e4c 35 .atomic_commit = msm_atomic_commit,
c8afe684
RC
36};
37
871d812a 38int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
c8afe684
RC
39{
40 struct msm_drm_private *priv = dev->dev_private;
871d812a 41 int idx = priv->num_mmus++;
c8afe684 42
871d812a 43 if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
c8afe684
RC
44 return -EINVAL;
45
871d812a 46 priv->mmus[idx] = mmu;
c8afe684
RC
47
48 return idx;
49}
50
c8afe684
RC
51#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
52static bool reglog = false;
53MODULE_PARM_DESC(reglog, "Enable register read/write logging");
54module_param(reglog, bool, 0600);
55#else
56#define reglog 0
57#endif
58
e90dfec7
RC
59#ifdef CONFIG_DRM_MSM_FBDEV
60static bool fbdev = true;
61MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
62module_param(fbdev, bool, 0600);
63#endif
64
3a10ba8c 65static char *vram = "16m";
871d812a
RC
66MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU");
67module_param(vram, charp, 0);
68
060530f1
RC
69/*
70 * Util/helpers:
71 */
72
c8afe684
RC
73void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
74 const char *dbgname)
75{
76 struct resource *res;
77 unsigned long size;
78 void __iomem *ptr;
79
80 if (name)
81 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
82 else
83 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84
85 if (!res) {
86 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
87 return ERR_PTR(-EINVAL);
88 }
89
90 size = resource_size(res);
91
92 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
93 if (!ptr) {
94 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
95 return ERR_PTR(-ENOMEM);
96 }
97
98 if (reglog)
fc99f97a 99 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
c8afe684
RC
100
101 return ptr;
102}
103
104void msm_writel(u32 data, void __iomem *addr)
105{
106 if (reglog)
fc99f97a 107 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
c8afe684
RC
108 writel(data, addr);
109}
110
111u32 msm_readl(const void __iomem *addr)
112{
113 u32 val = readl(addr);
114 if (reglog)
fc99f97a 115 printk(KERN_ERR "IO:R %p %08x\n", addr, val);
c8afe684
RC
116 return val;
117}
118
119/*
120 * DRM operations:
121 */
122
123static int msm_unload(struct drm_device *dev)
124{
125 struct msm_drm_private *priv = dev->dev_private;
126 struct msm_kms *kms = priv->kms;
7198e6b0 127 struct msm_gpu *gpu = priv->gpu;
c8afe684
RC
128
129 drm_kms_helper_poll_fini(dev);
130 drm_mode_config_cleanup(dev);
131 drm_vblank_cleanup(dev);
132
133 pm_runtime_get_sync(dev->dev);
134 drm_irq_uninstall(dev);
135 pm_runtime_put_sync(dev->dev);
136
137 flush_workqueue(priv->wq);
138 destroy_workqueue(priv->wq);
139
140 if (kms) {
141 pm_runtime_disable(dev->dev);
142 kms->funcs->destroy(kms);
143 }
144
7198e6b0
RC
145 if (gpu) {
146 mutex_lock(&dev->struct_mutex);
147 gpu->funcs->pm_suspend(gpu);
7198e6b0 148 mutex_unlock(&dev->struct_mutex);
774449eb 149 gpu->funcs->destroy(gpu);
7198e6b0 150 }
c8afe684 151
871d812a
RC
152 if (priv->vram.paddr) {
153 DEFINE_DMA_ATTRS(attrs);
154 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
155 drm_mm_takedown(&priv->vram.mm);
156 dma_free_attrs(dev->dev, priv->vram.size, NULL,
157 priv->vram.paddr, &attrs);
158 }
159
060530f1
RC
160 component_unbind_all(dev->dev, dev);
161
c8afe684
RC
162 dev->dev_private = NULL;
163
164 kfree(priv);
165
166 return 0;
167}
168
06c0dd96
RC
169static int get_mdp_ver(struct platform_device *pdev)
170{
171#ifdef CONFIG_OF
370a4d8a 172 static const struct of_device_id match_types[] = { {
06c0dd96
RC
173 .compatible = "qcom,mdss_mdp",
174 .data = (void *)5,
175 }, {
176 /* end node */
177 } };
178 struct device *dev = &pdev->dev;
179 const struct of_device_id *match;
180 match = of_match_node(match_types, dev->of_node);
181 if (match)
fc99f97a 182 return (int)(unsigned long)match->data;
06c0dd96
RC
183#endif
184 return 4;
185}
186
072f1f91
RC
187#include <linux/of_address.h>
188
5bf9c0b6 189static int msm_init_vram(struct drm_device *dev)
c8afe684 190{
5bf9c0b6 191 struct msm_drm_private *priv = dev->dev_private;
072f1f91
RC
192 unsigned long size = 0;
193 int ret = 0;
194
195#ifdef CONFIG_OF
196 /* In the device-tree world, we could have a 'memory-region'
197 * phandle, which gives us a link to our "vram". Allocating
198 * is all nicely abstracted behind the dma api, but we need
199 * to know the entire size to allocate it all in one go. There
200 * are two cases:
201 * 1) device with no IOMMU, in which case we need exclusive
202 * access to a VRAM carveout big enough for all gpu
203 * buffers
204 * 2) device with IOMMU, but where the bootloader puts up
205 * a splash screen. In this case, the VRAM carveout
206 * need only be large enough for fbdev fb. But we need
207 * exclusive access to the buffer to avoid the kernel
208 * using those pages for other purposes (which appears
209 * as corruption on screen before we have a chance to
210 * load and do initial modeset)
211 */
212 struct device_node *node;
213
214 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
215 if (node) {
216 struct resource r;
217 ret = of_address_to_resource(node, 0, &r);
218 if (ret)
219 return ret;
220 size = r.end - r.start;
fc99f97a 221 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
072f1f91
RC
222 } else
223#endif
c8afe684 224
871d812a
RC
225 /* if we have no IOMMU, then we need to use carveout allocator.
226 * Grab the entire CMA chunk carved out in early startup in
227 * mach-msm:
228 */
229 if (!iommu_present(&platform_bus_type)) {
072f1f91
RC
230 DRM_INFO("using %s VRAM carveout\n", vram);
231 size = memparse(vram, NULL);
232 }
233
234 if (size) {
871d812a 235 DEFINE_DMA_ATTRS(attrs);
871d812a
RC
236 void *p;
237
871d812a
RC
238 priv->vram.size = size;
239
240 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
241
242 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
243 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
244
245 /* note that for no-kernel-mapping, the vaddr returned
246 * is bogus, but non-null if allocation succeeded:
247 */
248 p = dma_alloc_attrs(dev->dev, size,
543d3011 249 &priv->vram.paddr, GFP_KERNEL, &attrs);
871d812a
RC
250 if (!p) {
251 dev_err(dev->dev, "failed to allocate VRAM\n");
252 priv->vram.paddr = 0;
5bf9c0b6 253 return -ENOMEM;
871d812a
RC
254 }
255
256 dev_info(dev->dev, "VRAM: %08x->%08x\n",
257 (uint32_t)priv->vram.paddr,
258 (uint32_t)(priv->vram.paddr + size));
259 }
260
072f1f91 261 return ret;
5bf9c0b6
RC
262}
263
264static int msm_load(struct drm_device *dev, unsigned long flags)
265{
266 struct platform_device *pdev = dev->platformdev;
267 struct msm_drm_private *priv;
268 struct msm_kms *kms;
269 int ret;
270
271 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
272 if (!priv) {
273 dev_err(dev->dev, "failed to allocate private data\n");
274 return -ENOMEM;
275 }
276
277 dev->dev_private = priv;
278
279 priv->wq = alloc_ordered_workqueue("msm", 0);
280 init_waitqueue_head(&priv->fence_event);
281 init_waitqueue_head(&priv->pending_crtcs_event);
282
283 INIT_LIST_HEAD(&priv->inactive_list);
284 INIT_LIST_HEAD(&priv->fence_cbs);
285
286 drm_mode_config_init(dev);
287
060530f1
RC
288 platform_set_drvdata(pdev, dev);
289
290 /* Bind all our sub-components: */
291 ret = component_bind_all(dev->dev, dev);
292 if (ret)
293 return ret;
294
13f15565
RC
295 ret = msm_init_vram(dev);
296 if (ret)
297 goto fail;
298
06c0dd96
RC
299 switch (get_mdp_ver(pdev)) {
300 case 4:
301 kms = mdp4_kms_init(dev);
302 break;
303 case 5:
304 kms = mdp5_kms_init(dev);
305 break;
306 default:
307 kms = ERR_PTR(-ENODEV);
308 break;
309 }
310
c8afe684
RC
311 if (IS_ERR(kms)) {
312 /*
313 * NOTE: once we have GPU support, having no kms should not
314 * be considered fatal.. ideally we would still support gpu
315 * and (for example) use dmabuf/prime to share buffers with
316 * imx drm driver on iMX5
317 */
318 dev_err(dev->dev, "failed to load kms\n");
e4826a94 319 ret = PTR_ERR(kms);
c8afe684
RC
320 goto fail;
321 }
322
323 priv->kms = kms;
324
325 if (kms) {
326 pm_runtime_enable(dev->dev);
327 ret = kms->funcs->hw_init(kms);
328 if (ret) {
329 dev_err(dev->dev, "kms hw init failed: %d\n", ret);
330 goto fail;
331 }
332 }
333
c8afe684
RC
334 dev->mode_config.funcs = &mode_config_funcs;
335
d65bd0e4 336 ret = drm_vblank_init(dev, priv->num_crtcs);
c8afe684
RC
337 if (ret < 0) {
338 dev_err(dev->dev, "failed to initialize vblank\n");
339 goto fail;
340 }
341
342 pm_runtime_get_sync(dev->dev);
bb0f1b5c 343 ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
c8afe684
RC
344 pm_runtime_put_sync(dev->dev);
345 if (ret < 0) {
346 dev_err(dev->dev, "failed to install IRQ handler\n");
347 goto fail;
348 }
349
cf3a7e4c
RC
350 drm_mode_config_reset(dev);
351
c8afe684 352#ifdef CONFIG_DRM_MSM_FBDEV
e90dfec7
RC
353 if (fbdev)
354 priv->fbdev = msm_fbdev_init(dev);
c8afe684
RC
355#endif
356
a7d3c950
RC
357 ret = msm_debugfs_late_init(dev);
358 if (ret)
359 goto fail;
360
c8afe684
RC
361 drm_kms_helper_poll_init(dev);
362
363 return 0;
364
365fail:
366 msm_unload(dev);
367 return ret;
368}
369
7198e6b0
RC
370static void load_gpu(struct drm_device *dev)
371{
a1ad3523 372 static DEFINE_MUTEX(init_lock);
7198e6b0 373 struct msm_drm_private *priv = dev->dev_private;
7198e6b0 374
a1ad3523
RC
375 mutex_lock(&init_lock);
376
e2550b7a
RC
377 if (!priv->gpu)
378 priv->gpu = adreno_load_gpu(dev);
7198e6b0 379
a1ad3523 380 mutex_unlock(&init_lock);
7198e6b0
RC
381}
382
383static int msm_open(struct drm_device *dev, struct drm_file *file)
384{
385 struct msm_file_private *ctx;
386
387 /* For now, load gpu on open.. to avoid the requirement of having
388 * firmware in the initrd.
389 */
390 load_gpu(dev);
391
392 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
393 if (!ctx)
394 return -ENOMEM;
395
396 file->driver_priv = ctx;
397
398 return 0;
399}
400
c8afe684
RC
401static void msm_preclose(struct drm_device *dev, struct drm_file *file)
402{
403 struct msm_drm_private *priv = dev->dev_private;
7198e6b0 404 struct msm_file_private *ctx = file->driver_priv;
c8afe684 405 struct msm_kms *kms = priv->kms;
7198e6b0 406
c8afe684
RC
407 if (kms)
408 kms->funcs->preclose(kms, file);
7198e6b0
RC
409
410 mutex_lock(&dev->struct_mutex);
411 if (ctx == priv->lastctx)
412 priv->lastctx = NULL;
413 mutex_unlock(&dev->struct_mutex);
414
415 kfree(ctx);
c8afe684
RC
416}
417
418static void msm_lastclose(struct drm_device *dev)
419{
a2ca7789 420#ifdef CONFIG_DRM_MSM_FBDEV
c8afe684 421 struct msm_drm_private *priv = dev->dev_private;
5ea1f752
RC
422 if (priv->fbdev)
423 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
a2ca7789 424#endif
c8afe684
RC
425}
426
e9f0d76f 427static irqreturn_t msm_irq(int irq, void *arg)
c8afe684
RC
428{
429 struct drm_device *dev = arg;
430 struct msm_drm_private *priv = dev->dev_private;
431 struct msm_kms *kms = priv->kms;
432 BUG_ON(!kms);
433 return kms->funcs->irq(kms);
434}
435
436static void msm_irq_preinstall(struct drm_device *dev)
437{
438 struct msm_drm_private *priv = dev->dev_private;
439 struct msm_kms *kms = priv->kms;
440 BUG_ON(!kms);
441 kms->funcs->irq_preinstall(kms);
442}
443
444static int msm_irq_postinstall(struct drm_device *dev)
445{
446 struct msm_drm_private *priv = dev->dev_private;
447 struct msm_kms *kms = priv->kms;
448 BUG_ON(!kms);
449 return kms->funcs->irq_postinstall(kms);
450}
451
452static void msm_irq_uninstall(struct drm_device *dev)
453{
454 struct msm_drm_private *priv = dev->dev_private;
455 struct msm_kms *kms = priv->kms;
456 BUG_ON(!kms);
457 kms->funcs->irq_uninstall(kms);
458}
459
460static int msm_enable_vblank(struct drm_device *dev, int crtc_id)
461{
462 struct msm_drm_private *priv = dev->dev_private;
463 struct msm_kms *kms = priv->kms;
464 if (!kms)
465 return -ENXIO;
466 DBG("dev=%p, crtc=%d", dev, crtc_id);
467 return kms->funcs->enable_vblank(kms, priv->crtcs[crtc_id]);
468}
469
470static void msm_disable_vblank(struct drm_device *dev, int crtc_id)
471{
472 struct msm_drm_private *priv = dev->dev_private;
473 struct msm_kms *kms = priv->kms;
474 if (!kms)
475 return;
476 DBG("dev=%p, crtc=%d", dev, crtc_id);
477 kms->funcs->disable_vblank(kms, priv->crtcs[crtc_id]);
478}
479
480/*
481 * DRM debugfs:
482 */
483
484#ifdef CONFIG_DEBUG_FS
7198e6b0
RC
485static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
486{
487 struct msm_drm_private *priv = dev->dev_private;
488 struct msm_gpu *gpu = priv->gpu;
489
490 if (gpu) {
491 seq_printf(m, "%s Status:\n", gpu->name);
492 gpu->funcs->show(gpu, m);
493 }
494
495 return 0;
496}
497
c8afe684
RC
498static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
499{
500 struct msm_drm_private *priv = dev->dev_private;
7198e6b0
RC
501 struct msm_gpu *gpu = priv->gpu;
502
503 if (gpu) {
504 seq_printf(m, "Active Objects (%s):\n", gpu->name);
505 msm_gem_describe_objects(&gpu->active_list, m);
506 }
c8afe684 507
7198e6b0 508 seq_printf(m, "Inactive Objects:\n");
c8afe684
RC
509 msm_gem_describe_objects(&priv->inactive_list, m);
510
511 return 0;
512}
513
514static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
515{
b04a5906 516 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
c8afe684
RC
517}
518
519static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
520{
521 struct msm_drm_private *priv = dev->dev_private;
522 struct drm_framebuffer *fb, *fbdev_fb = NULL;
523
524 if (priv->fbdev) {
525 seq_printf(m, "fbcon ");
526 fbdev_fb = priv->fbdev->fb;
527 msm_framebuffer_describe(fbdev_fb, m);
528 }
529
530 mutex_lock(&dev->mode_config.fb_lock);
531 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
532 if (fb == fbdev_fb)
533 continue;
534
535 seq_printf(m, "user ");
536 msm_framebuffer_describe(fb, m);
537 }
538 mutex_unlock(&dev->mode_config.fb_lock);
539
540 return 0;
541}
542
543static int show_locked(struct seq_file *m, void *arg)
544{
545 struct drm_info_node *node = (struct drm_info_node *) m->private;
546 struct drm_device *dev = node->minor->dev;
547 int (*show)(struct drm_device *dev, struct seq_file *m) =
548 node->info_ent->data;
549 int ret;
550
551 ret = mutex_lock_interruptible(&dev->struct_mutex);
552 if (ret)
553 return ret;
554
555 ret = show(dev, m);
556
557 mutex_unlock(&dev->struct_mutex);
558
559 return ret;
560}
561
562static struct drm_info_list msm_debugfs_list[] = {
7198e6b0 563 {"gpu", show_locked, 0, msm_gpu_show},
c8afe684
RC
564 {"gem", show_locked, 0, msm_gem_show},
565 { "mm", show_locked, 0, msm_mm_show },
566 { "fb", show_locked, 0, msm_fb_show },
567};
568
a7d3c950
RC
569static int late_init_minor(struct drm_minor *minor)
570{
571 int ret;
572
573 if (!minor)
574 return 0;
575
576 ret = msm_rd_debugfs_init(minor);
577 if (ret) {
578 dev_err(minor->dev->dev, "could not install rd debugfs\n");
579 return ret;
580 }
581
70c70f09
RC
582 ret = msm_perf_debugfs_init(minor);
583 if (ret) {
584 dev_err(minor->dev->dev, "could not install perf debugfs\n");
585 return ret;
586 }
587
a7d3c950
RC
588 return 0;
589}
590
591int msm_debugfs_late_init(struct drm_device *dev)
592{
593 int ret;
594 ret = late_init_minor(dev->primary);
595 if (ret)
596 return ret;
597 ret = late_init_minor(dev->render);
598 if (ret)
599 return ret;
600 ret = late_init_minor(dev->control);
601 return ret;
602}
603
c8afe684
RC
604static int msm_debugfs_init(struct drm_minor *minor)
605{
606 struct drm_device *dev = minor->dev;
607 int ret;
608
609 ret = drm_debugfs_create_files(msm_debugfs_list,
610 ARRAY_SIZE(msm_debugfs_list),
611 minor->debugfs_root, minor);
612
613 if (ret) {
614 dev_err(dev->dev, "could not install msm_debugfs_list\n");
615 return ret;
616 }
617
a7d3c950 618 return 0;
c8afe684
RC
619}
620
621static void msm_debugfs_cleanup(struct drm_minor *minor)
622{
623 drm_debugfs_remove_files(msm_debugfs_list,
624 ARRAY_SIZE(msm_debugfs_list), minor);
a7d3c950
RC
625 if (!minor->dev->dev_private)
626 return;
627 msm_rd_debugfs_cleanup(minor);
70c70f09 628 msm_perf_debugfs_cleanup(minor);
c8afe684
RC
629}
630#endif
631
7198e6b0
RC
632/*
633 * Fences:
634 */
635
a9702ca2
WX
636int msm_wait_fence(struct drm_device *dev, uint32_t fence,
637 ktime_t *timeout , bool interruptible)
7198e6b0
RC
638{
639 struct msm_drm_private *priv = dev->dev_private;
7198e6b0
RC
640 int ret;
641
f816f272
RC
642 if (!priv->gpu)
643 return 0;
644
645 if (fence > priv->gpu->submitted_fence) {
646 DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
647 fence, priv->gpu->submitted_fence);
648 return -EINVAL;
649 }
650
651 if (!timeout) {
652 /* no-wait: */
653 ret = fence_completed(dev, fence) ? 0 : -EBUSY;
654 } else {
56c2da83 655 ktime_t now = ktime_get();
f816f272
RC
656 unsigned long remaining_jiffies;
657
56c2da83 658 if (ktime_compare(*timeout, now) < 0) {
f816f272 659 remaining_jiffies = 0;
56c2da83
RC
660 } else {
661 ktime_t rem = ktime_sub(*timeout, now);
662 struct timespec ts = ktime_to_timespec(rem);
663 remaining_jiffies = timespec_to_jiffies(&ts);
664 }
f816f272 665
a9702ca2
WX
666 if (interruptible)
667 ret = wait_event_interruptible_timeout(priv->fence_event,
668 fence_completed(dev, fence),
669 remaining_jiffies);
670 else
671 ret = wait_event_timeout(priv->fence_event,
f816f272
RC
672 fence_completed(dev, fence),
673 remaining_jiffies);
674
675 if (ret == 0) {
676 DBG("timeout waiting for fence: %u (completed: %u)",
677 fence, priv->completed_fence);
678 ret = -ETIMEDOUT;
679 } else if (ret != -ERESTARTSYS) {
680 ret = 0;
681 }
7198e6b0
RC
682 }
683
684 return ret;
685}
686
69193e50
RC
687int msm_queue_fence_cb(struct drm_device *dev,
688 struct msm_fence_cb *cb, uint32_t fence)
689{
690 struct msm_drm_private *priv = dev->dev_private;
691 int ret = 0;
692
693 mutex_lock(&dev->struct_mutex);
694 if (!list_empty(&cb->work.entry)) {
695 ret = -EINVAL;
696 } else if (fence > priv->completed_fence) {
697 cb->fence = fence;
698 list_add_tail(&cb->work.entry, &priv->fence_cbs);
699 } else {
700 queue_work(priv->wq, &cb->work);
701 }
702 mutex_unlock(&dev->struct_mutex);
703
704 return ret;
705}
706
edd4fc63 707/* called from workqueue */
7198e6b0
RC
708void msm_update_fence(struct drm_device *dev, uint32_t fence)
709{
710 struct msm_drm_private *priv = dev->dev_private;
711
edd4fc63
RC
712 mutex_lock(&dev->struct_mutex);
713 priv->completed_fence = max(fence, priv->completed_fence);
714
715 while (!list_empty(&priv->fence_cbs)) {
716 struct msm_fence_cb *cb;
717
718 cb = list_first_entry(&priv->fence_cbs,
719 struct msm_fence_cb, work.entry);
720
721 if (cb->fence > priv->completed_fence)
722 break;
723
724 list_del_init(&cb->work.entry);
725 queue_work(priv->wq, &cb->work);
7198e6b0 726 }
edd4fc63
RC
727
728 mutex_unlock(&dev->struct_mutex);
729
730 wake_up_all(&priv->fence_event);
731}
732
733void __msm_fence_worker(struct work_struct *work)
734{
735 struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
736 cb->func(cb);
7198e6b0
RC
737}
738
739/*
740 * DRM ioctls:
741 */
742
743static int msm_ioctl_get_param(struct drm_device *dev, void *data,
744 struct drm_file *file)
745{
746 struct msm_drm_private *priv = dev->dev_private;
747 struct drm_msm_param *args = data;
748 struct msm_gpu *gpu;
749
750 /* for now, we just have 3d pipe.. eventually this would need to
751 * be more clever to dispatch to appropriate gpu module:
752 */
753 if (args->pipe != MSM_PIPE_3D0)
754 return -EINVAL;
755
756 gpu = priv->gpu;
757
758 if (!gpu)
759 return -ENXIO;
760
761 return gpu->funcs->get_param(gpu, args->param, &args->value);
762}
763
764static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
765 struct drm_file *file)
766{
767 struct drm_msm_gem_new *args = data;
93ddb0d3
RC
768
769 if (args->flags & ~MSM_BO_FLAGS) {
770 DRM_ERROR("invalid flags: %08x\n", args->flags);
771 return -EINVAL;
772 }
773
7198e6b0
RC
774 return msm_gem_new_handle(dev, file, args->size,
775 args->flags, &args->handle);
776}
777
56c2da83
RC
778static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
779{
780 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
781}
7198e6b0
RC
782
783static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
784 struct drm_file *file)
785{
786 struct drm_msm_gem_cpu_prep *args = data;
787 struct drm_gem_object *obj;
56c2da83 788 ktime_t timeout = to_ktime(args->timeout);
7198e6b0
RC
789 int ret;
790
93ddb0d3
RC
791 if (args->op & ~MSM_PREP_FLAGS) {
792 DRM_ERROR("invalid op: %08x\n", args->op);
793 return -EINVAL;
794 }
795
7198e6b0
RC
796 obj = drm_gem_object_lookup(dev, file, args->handle);
797 if (!obj)
798 return -ENOENT;
799
56c2da83 800 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
7198e6b0
RC
801
802 drm_gem_object_unreference_unlocked(obj);
803
804 return ret;
805}
806
807static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
808 struct drm_file *file)
809{
810 struct drm_msm_gem_cpu_fini *args = data;
811 struct drm_gem_object *obj;
812 int ret;
813
814 obj = drm_gem_object_lookup(dev, file, args->handle);
815 if (!obj)
816 return -ENOENT;
817
818 ret = msm_gem_cpu_fini(obj);
819
820 drm_gem_object_unreference_unlocked(obj);
821
822 return ret;
823}
824
825static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
826 struct drm_file *file)
827{
828 struct drm_msm_gem_info *args = data;
829 struct drm_gem_object *obj;
830 int ret = 0;
831
832 if (args->pad)
833 return -EINVAL;
834
835 obj = drm_gem_object_lookup(dev, file, args->handle);
836 if (!obj)
837 return -ENOENT;
838
839 args->offset = msm_gem_mmap_offset(obj);
840
841 drm_gem_object_unreference_unlocked(obj);
842
843 return ret;
844}
845
846static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
847 struct drm_file *file)
848{
849 struct drm_msm_wait_fence *args = data;
56c2da83 850 ktime_t timeout = to_ktime(args->timeout);
93ddb0d3
RC
851
852 if (args->pad) {
853 DRM_ERROR("invalid pad: %08x\n", args->pad);
854 return -EINVAL;
855 }
856
a9702ca2 857 return msm_wait_fence(dev, args->fence, &timeout, true);
7198e6b0
RC
858}
859
860static const struct drm_ioctl_desc msm_ioctls[] = {
b4b15c86
RC
861 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
862 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
863 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
864 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
865 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
866 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
867 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
7198e6b0
RC
868};
869
c8afe684
RC
870static const struct vm_operations_struct vm_ops = {
871 .fault = msm_gem_fault,
872 .open = drm_gem_vm_open,
873 .close = drm_gem_vm_close,
874};
875
876static const struct file_operations fops = {
877 .owner = THIS_MODULE,
878 .open = drm_open,
879 .release = drm_release,
880 .unlocked_ioctl = drm_ioctl,
881#ifdef CONFIG_COMPAT
882 .compat_ioctl = drm_compat_ioctl,
883#endif
884 .poll = drm_poll,
885 .read = drm_read,
886 .llseek = no_llseek,
887 .mmap = msm_gem_mmap,
888};
889
890static struct drm_driver msm_driver = {
05b84911
RC
891 .driver_features = DRIVER_HAVE_IRQ |
892 DRIVER_GEM |
893 DRIVER_PRIME |
b4b15c86 894 DRIVER_RENDER |
a5436e1d 895 DRIVER_ATOMIC |
05b84911 896 DRIVER_MODESET,
c8afe684
RC
897 .load = msm_load,
898 .unload = msm_unload,
7198e6b0 899 .open = msm_open,
c8afe684
RC
900 .preclose = msm_preclose,
901 .lastclose = msm_lastclose,
915b4d11 902 .set_busid = drm_platform_set_busid,
c8afe684
RC
903 .irq_handler = msm_irq,
904 .irq_preinstall = msm_irq_preinstall,
905 .irq_postinstall = msm_irq_postinstall,
906 .irq_uninstall = msm_irq_uninstall,
907 .get_vblank_counter = drm_vblank_count,
908 .enable_vblank = msm_enable_vblank,
909 .disable_vblank = msm_disable_vblank,
910 .gem_free_object = msm_gem_free_object,
911 .gem_vm_ops = &vm_ops,
912 .dumb_create = msm_gem_dumb_create,
913 .dumb_map_offset = msm_gem_dumb_map_offset,
30600a90 914 .dumb_destroy = drm_gem_dumb_destroy,
05b84911
RC
915 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
916 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
917 .gem_prime_export = drm_gem_prime_export,
918 .gem_prime_import = drm_gem_prime_import,
919 .gem_prime_pin = msm_gem_prime_pin,
920 .gem_prime_unpin = msm_gem_prime_unpin,
921 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
922 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
923 .gem_prime_vmap = msm_gem_prime_vmap,
924 .gem_prime_vunmap = msm_gem_prime_vunmap,
77a147e7 925 .gem_prime_mmap = msm_gem_prime_mmap,
c8afe684
RC
926#ifdef CONFIG_DEBUG_FS
927 .debugfs_init = msm_debugfs_init,
928 .debugfs_cleanup = msm_debugfs_cleanup,
929#endif
7198e6b0
RC
930 .ioctls = msm_ioctls,
931 .num_ioctls = DRM_MSM_NUM_IOCTLS,
c8afe684
RC
932 .fops = &fops,
933 .name = "msm",
934 .desc = "MSM Snapdragon DRM",
935 .date = "20130625",
936 .major = 1,
937 .minor = 0,
938};
939
940#ifdef CONFIG_PM_SLEEP
941static int msm_pm_suspend(struct device *dev)
942{
943 struct drm_device *ddev = dev_get_drvdata(dev);
944
945 drm_kms_helper_poll_disable(ddev);
946
947 return 0;
948}
949
950static int msm_pm_resume(struct device *dev)
951{
952 struct drm_device *ddev = dev_get_drvdata(dev);
953
954 drm_kms_helper_poll_enable(ddev);
955
956 return 0;
957}
958#endif
959
960static const struct dev_pm_ops msm_pm_ops = {
961 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
962};
963
060530f1
RC
964/*
965 * Componentized driver support:
966 */
967
968#ifdef CONFIG_OF
969/* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
970 * (or probably any other).. so probably some room for some helpers
971 */
972static int compare_of(struct device *dev, void *data)
973{
974 return dev->of_node == data;
975}
41e69778
RC
976
977static int add_components(struct device *dev, struct component_match **matchptr,
978 const char *name)
979{
980 struct device_node *np = dev->of_node;
981 unsigned i;
982
983 for (i = 0; ; i++) {
984 struct device_node *node;
985
986 node = of_parse_phandle(np, name, i);
987 if (!node)
988 break;
989
990 component_match_add(dev, matchptr, compare_of, node);
991 }
992
993 return 0;
994}
84448288
RK
995#else
996static int compare_dev(struct device *dev, void *data)
997{
998 return dev == data;
999}
1000#endif
1001
1002static int msm_drm_bind(struct device *dev)
1003{
1004 return drm_platform_init(&msm_driver, to_platform_device(dev));
1005}
1006
1007static void msm_drm_unbind(struct device *dev)
1008{
1009 drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
1010}
1011
1012static const struct component_master_ops msm_drm_ops = {
1013 .bind = msm_drm_bind,
1014 .unbind = msm_drm_unbind,
1015};
1016
1017/*
1018 * Platform driver:
1019 */
060530f1 1020
84448288 1021static int msm_pdev_probe(struct platform_device *pdev)
060530f1 1022{
84448288
RK
1023 struct component_match *match = NULL;
1024#ifdef CONFIG_OF
41e69778
RC
1025 add_components(&pdev->dev, &match, "connectors");
1026 add_components(&pdev->dev, &match, "gpus");
060530f1 1027#else
060530f1
RC
1028 /* For non-DT case, it kinda sucks. We don't actually have a way
1029 * to know whether or not we are waiting for certain devices (or if
1030 * they are simply not present). But for non-DT we only need to
1031 * care about apq8064/apq8060/etc (all mdp4/a3xx):
1032 */
1033 static const char *devnames[] = {
1034 "hdmi_msm.0", "kgsl-3d0.0",
1035 };
1036 int i;
1037
1038 DBG("Adding components..");
1039
1040 for (i = 0; i < ARRAY_SIZE(devnames); i++) {
1041 struct device *dev;
060530f1
RC
1042
1043 dev = bus_find_device_by_name(&platform_bus_type,
1044 NULL, devnames[i]);
1045 if (!dev) {
12313c2a 1046 dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
060530f1
RC
1047 return -EPROBE_DEFER;
1048 }
1049
84448288 1050 component_match_add(&pdev->dev, &match, compare_dev, dev);
060530f1 1051 }
060530f1
RC
1052#endif
1053
871d812a 1054 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
84448288 1055 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
c8afe684
RC
1056}
1057
1058static int msm_pdev_remove(struct platform_device *pdev)
1059{
060530f1 1060 component_master_del(&pdev->dev, &msm_drm_ops);
c8afe684
RC
1061
1062 return 0;
1063}
1064
1065static const struct platform_device_id msm_id[] = {
1066 { "mdp", 0 },
1067 { }
1068};
1069
06c0dd96 1070static const struct of_device_id dt_match[] = {
41e69778
RC
1071 { .compatible = "qcom,mdp" }, /* mdp4 */
1072 { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
06c0dd96
RC
1073 {}
1074};
1075MODULE_DEVICE_TABLE(of, dt_match);
1076
c8afe684
RC
1077static struct platform_driver msm_platform_driver = {
1078 .probe = msm_pdev_probe,
1079 .remove = msm_pdev_remove,
1080 .driver = {
c8afe684 1081 .name = "msm",
06c0dd96 1082 .of_match_table = dt_match,
c8afe684
RC
1083 .pm = &msm_pm_ops,
1084 },
1085 .id_table = msm_id,
1086};
1087
1088static int __init msm_drm_register(void)
1089{
1090 DBG("init");
d5af49c9 1091 msm_dsi_register();
00453981 1092 msm_edp_register();
c8afe684 1093 hdmi_register();
bfd28b13 1094 adreno_register();
c8afe684
RC
1095 return platform_driver_register(&msm_platform_driver);
1096}
1097
1098static void __exit msm_drm_unregister(void)
1099{
1100 DBG("fini");
1101 platform_driver_unregister(&msm_platform_driver);
1102 hdmi_unregister();
bfd28b13 1103 adreno_unregister();
00453981 1104 msm_edp_unregister();
d5af49c9 1105 msm_dsi_unregister();
c8afe684
RC
1106}
1107
1108module_init(msm_drm_register);
1109module_exit(msm_drm_unregister);
1110
1111MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1112MODULE_DESCRIPTION("MSM DRM Driver");
1113MODULE_LICENSE("GPL");