xref: /linux/drivers/gpu/drm/msm/msm_drv.c (revision e08a1d97d33e2ac05cd368b955f9fdc2823f15fd)
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 <drm/drm_of.h>
19 
20 #include "msm_drv.h"
21 #include "msm_debugfs.h"
22 #include "msm_fence.h"
23 #include "msm_gpu.h"
24 #include "msm_kms.h"
25 
26 
27 /*
28  * MSM driver version:
29  * - 1.0.0 - initial interface
30  * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
31  * - 1.2.0 - adds explicit fence support for submit ioctl
32  */
33 #define MSM_VERSION_MAJOR	1
34 #define MSM_VERSION_MINOR	2
35 #define MSM_VERSION_PATCHLEVEL	0
36 
37 static void msm_fb_output_poll_changed(struct drm_device *dev)
38 {
39 	struct msm_drm_private *priv = dev->dev_private;
40 	if (priv->fbdev)
41 		drm_fb_helper_hotplug_event(priv->fbdev);
42 }
43 
44 static const struct drm_mode_config_funcs mode_config_funcs = {
45 	.fb_create = msm_framebuffer_create,
46 	.output_poll_changed = msm_fb_output_poll_changed,
47 	.atomic_check = msm_atomic_check,
48 	.atomic_commit = msm_atomic_commit,
49 };
50 
51 int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
52 {
53 	struct msm_drm_private *priv = dev->dev_private;
54 	int idx = priv->num_mmus++;
55 
56 	if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
57 		return -EINVAL;
58 
59 	priv->mmus[idx] = mmu;
60 
61 	return idx;
62 }
63 
64 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
65 static bool reglog = false;
66 MODULE_PARM_DESC(reglog, "Enable register read/write logging");
67 module_param(reglog, bool, 0600);
68 #else
69 #define reglog 0
70 #endif
71 
72 #ifdef CONFIG_DRM_FBDEV_EMULATION
73 static bool fbdev = true;
74 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
75 module_param(fbdev, bool, 0600);
76 #endif
77 
78 static char *vram = "16m";
79 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
80 module_param(vram, charp, 0);
81 
82 /*
83  * Util/helpers:
84  */
85 
86 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
87 		const char *dbgname)
88 {
89 	struct resource *res;
90 	unsigned long size;
91 	void __iomem *ptr;
92 
93 	if (name)
94 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
95 	else
96 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
97 
98 	if (!res) {
99 		dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
100 		return ERR_PTR(-EINVAL);
101 	}
102 
103 	size = resource_size(res);
104 
105 	ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
106 	if (!ptr) {
107 		dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
108 		return ERR_PTR(-ENOMEM);
109 	}
110 
111 	if (reglog)
112 		printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
113 
114 	return ptr;
115 }
116 
117 void msm_writel(u32 data, void __iomem *addr)
118 {
119 	if (reglog)
120 		printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
121 	writel(data, addr);
122 }
123 
124 u32 msm_readl(const void __iomem *addr)
125 {
126 	u32 val = readl(addr);
127 	if (reglog)
128 		printk(KERN_ERR "IO:R %p %08x\n", addr, val);
129 	return val;
130 }
131 
132 struct vblank_event {
133 	struct list_head node;
134 	int crtc_id;
135 	bool enable;
136 };
137 
138 static void vblank_ctrl_worker(struct work_struct *work)
139 {
140 	struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
141 						struct msm_vblank_ctrl, work);
142 	struct msm_drm_private *priv = container_of(vbl_ctrl,
143 					struct msm_drm_private, vblank_ctrl);
144 	struct msm_kms *kms = priv->kms;
145 	struct vblank_event *vbl_ev, *tmp;
146 	unsigned long flags;
147 
148 	spin_lock_irqsave(&vbl_ctrl->lock, flags);
149 	list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
150 		list_del(&vbl_ev->node);
151 		spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
152 
153 		if (vbl_ev->enable)
154 			kms->funcs->enable_vblank(kms,
155 						priv->crtcs[vbl_ev->crtc_id]);
156 		else
157 			kms->funcs->disable_vblank(kms,
158 						priv->crtcs[vbl_ev->crtc_id]);
159 
160 		kfree(vbl_ev);
161 
162 		spin_lock_irqsave(&vbl_ctrl->lock, flags);
163 	}
164 
165 	spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
166 }
167 
168 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
169 					int crtc_id, bool enable)
170 {
171 	struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
172 	struct vblank_event *vbl_ev;
173 	unsigned long flags;
174 
175 	vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
176 	if (!vbl_ev)
177 		return -ENOMEM;
178 
179 	vbl_ev->crtc_id = crtc_id;
180 	vbl_ev->enable = enable;
181 
182 	spin_lock_irqsave(&vbl_ctrl->lock, flags);
183 	list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
184 	spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
185 
186 	queue_work(priv->wq, &vbl_ctrl->work);
187 
188 	return 0;
189 }
190 
191 static int msm_drm_uninit(struct device *dev)
192 {
193 	struct platform_device *pdev = to_platform_device(dev);
194 	struct drm_device *ddev = platform_get_drvdata(pdev);
195 	struct msm_drm_private *priv = ddev->dev_private;
196 	struct msm_kms *kms = priv->kms;
197 	struct msm_gpu *gpu = priv->gpu;
198 	struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
199 	struct vblank_event *vbl_ev, *tmp;
200 
201 	/* We must cancel and cleanup any pending vblank enable/disable
202 	 * work before drm_irq_uninstall() to avoid work re-enabling an
203 	 * irq after uninstall has disabled it.
204 	 */
205 	cancel_work_sync(&vbl_ctrl->work);
206 	list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
207 		list_del(&vbl_ev->node);
208 		kfree(vbl_ev);
209 	}
210 
211 	msm_gem_shrinker_cleanup(ddev);
212 
213 	drm_kms_helper_poll_fini(ddev);
214 
215 	drm_dev_unregister(ddev);
216 
217 #ifdef CONFIG_DRM_FBDEV_EMULATION
218 	if (fbdev && priv->fbdev)
219 		msm_fbdev_free(ddev);
220 #endif
221 	drm_mode_config_cleanup(ddev);
222 
223 	pm_runtime_get_sync(dev);
224 	drm_irq_uninstall(ddev);
225 	pm_runtime_put_sync(dev);
226 
227 	flush_workqueue(priv->wq);
228 	destroy_workqueue(priv->wq);
229 
230 	flush_workqueue(priv->atomic_wq);
231 	destroy_workqueue(priv->atomic_wq);
232 
233 	if (kms)
234 		kms->funcs->destroy(kms);
235 
236 	if (gpu) {
237 		mutex_lock(&ddev->struct_mutex);
238 		gpu->funcs->pm_suspend(gpu);
239 		mutex_unlock(&ddev->struct_mutex);
240 		gpu->funcs->destroy(gpu);
241 	}
242 
243 	if (priv->vram.paddr) {
244 		unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
245 		drm_mm_takedown(&priv->vram.mm);
246 		dma_free_attrs(dev, priv->vram.size, NULL,
247 			       priv->vram.paddr, attrs);
248 	}
249 
250 	component_unbind_all(dev, ddev);
251 
252 	msm_mdss_destroy(ddev);
253 
254 	ddev->dev_private = NULL;
255 	drm_dev_unref(ddev);
256 
257 	kfree(priv);
258 
259 	return 0;
260 }
261 
262 static int get_mdp_ver(struct platform_device *pdev)
263 {
264 	struct device *dev = &pdev->dev;
265 
266 	return (int) (unsigned long) of_device_get_match_data(dev);
267 }
268 
269 #include <linux/of_address.h>
270 
271 static int msm_init_vram(struct drm_device *dev)
272 {
273 	struct msm_drm_private *priv = dev->dev_private;
274 	struct device_node *node;
275 	unsigned long size = 0;
276 	int ret = 0;
277 
278 	/* In the device-tree world, we could have a 'memory-region'
279 	 * phandle, which gives us a link to our "vram".  Allocating
280 	 * is all nicely abstracted behind the dma api, but we need
281 	 * to know the entire size to allocate it all in one go. There
282 	 * are two cases:
283 	 *  1) device with no IOMMU, in which case we need exclusive
284 	 *     access to a VRAM carveout big enough for all gpu
285 	 *     buffers
286 	 *  2) device with IOMMU, but where the bootloader puts up
287 	 *     a splash screen.  In this case, the VRAM carveout
288 	 *     need only be large enough for fbdev fb.  But we need
289 	 *     exclusive access to the buffer to avoid the kernel
290 	 *     using those pages for other purposes (which appears
291 	 *     as corruption on screen before we have a chance to
292 	 *     load and do initial modeset)
293 	 */
294 
295 	node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
296 	if (node) {
297 		struct resource r;
298 		ret = of_address_to_resource(node, 0, &r);
299 		of_node_put(node);
300 		if (ret)
301 			return ret;
302 		size = r.end - r.start;
303 		DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
304 
305 		/* if we have no IOMMU, then we need to use carveout allocator.
306 		 * Grab the entire CMA chunk carved out in early startup in
307 		 * mach-msm:
308 		 */
309 	} else if (!iommu_present(&platform_bus_type)) {
310 		DRM_INFO("using %s VRAM carveout\n", vram);
311 		size = memparse(vram, NULL);
312 	}
313 
314 	if (size) {
315 		unsigned long attrs = 0;
316 		void *p;
317 
318 		priv->vram.size = size;
319 
320 		drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
321 
322 		attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
323 		attrs |= DMA_ATTR_WRITE_COMBINE;
324 
325 		/* note that for no-kernel-mapping, the vaddr returned
326 		 * is bogus, but non-null if allocation succeeded:
327 		 */
328 		p = dma_alloc_attrs(dev->dev, size,
329 				&priv->vram.paddr, GFP_KERNEL, attrs);
330 		if (!p) {
331 			dev_err(dev->dev, "failed to allocate VRAM\n");
332 			priv->vram.paddr = 0;
333 			return -ENOMEM;
334 		}
335 
336 		dev_info(dev->dev, "VRAM: %08x->%08x\n",
337 				(uint32_t)priv->vram.paddr,
338 				(uint32_t)(priv->vram.paddr + size));
339 	}
340 
341 	return ret;
342 }
343 
344 static int msm_drm_init(struct device *dev, struct drm_driver *drv)
345 {
346 	struct platform_device *pdev = to_platform_device(dev);
347 	struct drm_device *ddev;
348 	struct msm_drm_private *priv;
349 	struct msm_kms *kms;
350 	int ret;
351 
352 	ddev = drm_dev_alloc(drv, dev);
353 	if (IS_ERR(ddev)) {
354 		dev_err(dev, "failed to allocate drm_device\n");
355 		return PTR_ERR(ddev);
356 	}
357 
358 	platform_set_drvdata(pdev, ddev);
359 	ddev->platformdev = pdev;
360 
361 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
362 	if (!priv) {
363 		drm_dev_unref(ddev);
364 		return -ENOMEM;
365 	}
366 
367 	ddev->dev_private = priv;
368 	priv->dev = ddev;
369 
370 	ret = msm_mdss_init(ddev);
371 	if (ret) {
372 		kfree(priv);
373 		drm_dev_unref(ddev);
374 		return ret;
375 	}
376 
377 	priv->wq = alloc_ordered_workqueue("msm", 0);
378 	priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
379 	init_waitqueue_head(&priv->pending_crtcs_event);
380 
381 	INIT_LIST_HEAD(&priv->inactive_list);
382 	INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
383 	INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
384 	spin_lock_init(&priv->vblank_ctrl.lock);
385 
386 	drm_mode_config_init(ddev);
387 
388 	/* Bind all our sub-components: */
389 	ret = component_bind_all(dev, ddev);
390 	if (ret) {
391 		msm_mdss_destroy(ddev);
392 		kfree(priv);
393 		drm_dev_unref(ddev);
394 		return ret;
395 	}
396 
397 	ret = msm_init_vram(ddev);
398 	if (ret)
399 		goto fail;
400 
401 	msm_gem_shrinker_init(ddev);
402 
403 	switch (get_mdp_ver(pdev)) {
404 	case 4:
405 		kms = mdp4_kms_init(ddev);
406 		priv->kms = kms;
407 		break;
408 	case 5:
409 		kms = mdp5_kms_init(ddev);
410 		break;
411 	default:
412 		kms = ERR_PTR(-ENODEV);
413 		break;
414 	}
415 
416 	if (IS_ERR(kms)) {
417 		/*
418 		 * NOTE: once we have GPU support, having no kms should not
419 		 * be considered fatal.. ideally we would still support gpu
420 		 * and (for example) use dmabuf/prime to share buffers with
421 		 * imx drm driver on iMX5
422 		 */
423 		dev_err(dev, "failed to load kms\n");
424 		ret = PTR_ERR(kms);
425 		goto fail;
426 	}
427 
428 	if (kms) {
429 		ret = kms->funcs->hw_init(kms);
430 		if (ret) {
431 			dev_err(dev, "kms hw init failed: %d\n", ret);
432 			goto fail;
433 		}
434 	}
435 
436 	ddev->mode_config.funcs = &mode_config_funcs;
437 
438 	ret = drm_vblank_init(ddev, priv->num_crtcs);
439 	if (ret < 0) {
440 		dev_err(dev, "failed to initialize vblank\n");
441 		goto fail;
442 	}
443 
444 	if (kms) {
445 		pm_runtime_get_sync(dev);
446 		ret = drm_irq_install(ddev, kms->irq);
447 		pm_runtime_put_sync(dev);
448 		if (ret < 0) {
449 			dev_err(dev, "failed to install IRQ handler\n");
450 			goto fail;
451 		}
452 	}
453 
454 	ret = drm_dev_register(ddev, 0);
455 	if (ret)
456 		goto fail;
457 
458 	drm_mode_config_reset(ddev);
459 
460 #ifdef CONFIG_DRM_FBDEV_EMULATION
461 	if (fbdev)
462 		priv->fbdev = msm_fbdev_init(ddev);
463 #endif
464 
465 	ret = msm_debugfs_late_init(ddev);
466 	if (ret)
467 		goto fail;
468 
469 	drm_kms_helper_poll_init(ddev);
470 
471 	return 0;
472 
473 fail:
474 	msm_drm_uninit(dev);
475 	return ret;
476 }
477 
478 /*
479  * DRM operations:
480  */
481 
482 static void load_gpu(struct drm_device *dev)
483 {
484 	static DEFINE_MUTEX(init_lock);
485 	struct msm_drm_private *priv = dev->dev_private;
486 
487 	mutex_lock(&init_lock);
488 
489 	if (!priv->gpu)
490 		priv->gpu = adreno_load_gpu(dev);
491 
492 	mutex_unlock(&init_lock);
493 }
494 
495 static int msm_open(struct drm_device *dev, struct drm_file *file)
496 {
497 	struct msm_file_private *ctx;
498 
499 	/* For now, load gpu on open.. to avoid the requirement of having
500 	 * firmware in the initrd.
501 	 */
502 	load_gpu(dev);
503 
504 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
505 	if (!ctx)
506 		return -ENOMEM;
507 
508 	file->driver_priv = ctx;
509 
510 	return 0;
511 }
512 
513 static void msm_preclose(struct drm_device *dev, struct drm_file *file)
514 {
515 	struct msm_drm_private *priv = dev->dev_private;
516 	struct msm_file_private *ctx = file->driver_priv;
517 
518 	mutex_lock(&dev->struct_mutex);
519 	if (ctx == priv->lastctx)
520 		priv->lastctx = NULL;
521 	mutex_unlock(&dev->struct_mutex);
522 
523 	kfree(ctx);
524 }
525 
526 static void msm_lastclose(struct drm_device *dev)
527 {
528 	struct msm_drm_private *priv = dev->dev_private;
529 	if (priv->fbdev)
530 		drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
531 }
532 
533 static irqreturn_t msm_irq(int irq, void *arg)
534 {
535 	struct drm_device *dev = arg;
536 	struct msm_drm_private *priv = dev->dev_private;
537 	struct msm_kms *kms = priv->kms;
538 	BUG_ON(!kms);
539 	return kms->funcs->irq(kms);
540 }
541 
542 static void msm_irq_preinstall(struct drm_device *dev)
543 {
544 	struct msm_drm_private *priv = dev->dev_private;
545 	struct msm_kms *kms = priv->kms;
546 	BUG_ON(!kms);
547 	kms->funcs->irq_preinstall(kms);
548 }
549 
550 static int msm_irq_postinstall(struct drm_device *dev)
551 {
552 	struct msm_drm_private *priv = dev->dev_private;
553 	struct msm_kms *kms = priv->kms;
554 	BUG_ON(!kms);
555 	return kms->funcs->irq_postinstall(kms);
556 }
557 
558 static void msm_irq_uninstall(struct drm_device *dev)
559 {
560 	struct msm_drm_private *priv = dev->dev_private;
561 	struct msm_kms *kms = priv->kms;
562 	BUG_ON(!kms);
563 	kms->funcs->irq_uninstall(kms);
564 }
565 
566 static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
567 {
568 	struct msm_drm_private *priv = dev->dev_private;
569 	struct msm_kms *kms = priv->kms;
570 	if (!kms)
571 		return -ENXIO;
572 	DBG("dev=%p, crtc=%u", dev, pipe);
573 	return vblank_ctrl_queue_work(priv, pipe, true);
574 }
575 
576 static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
577 {
578 	struct msm_drm_private *priv = dev->dev_private;
579 	struct msm_kms *kms = priv->kms;
580 	if (!kms)
581 		return;
582 	DBG("dev=%p, crtc=%u", dev, pipe);
583 	vblank_ctrl_queue_work(priv, pipe, false);
584 }
585 
586 /*
587  * DRM ioctls:
588  */
589 
590 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
591 		struct drm_file *file)
592 {
593 	struct msm_drm_private *priv = dev->dev_private;
594 	struct drm_msm_param *args = data;
595 	struct msm_gpu *gpu;
596 
597 	/* for now, we just have 3d pipe.. eventually this would need to
598 	 * be more clever to dispatch to appropriate gpu module:
599 	 */
600 	if (args->pipe != MSM_PIPE_3D0)
601 		return -EINVAL;
602 
603 	gpu = priv->gpu;
604 
605 	if (!gpu)
606 		return -ENXIO;
607 
608 	return gpu->funcs->get_param(gpu, args->param, &args->value);
609 }
610 
611 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
612 		struct drm_file *file)
613 {
614 	struct drm_msm_gem_new *args = data;
615 
616 	if (args->flags & ~MSM_BO_FLAGS) {
617 		DRM_ERROR("invalid flags: %08x\n", args->flags);
618 		return -EINVAL;
619 	}
620 
621 	return msm_gem_new_handle(dev, file, args->size,
622 			args->flags, &args->handle);
623 }
624 
625 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
626 {
627 	return ktime_set(timeout.tv_sec, timeout.tv_nsec);
628 }
629 
630 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
631 		struct drm_file *file)
632 {
633 	struct drm_msm_gem_cpu_prep *args = data;
634 	struct drm_gem_object *obj;
635 	ktime_t timeout = to_ktime(args->timeout);
636 	int ret;
637 
638 	if (args->op & ~MSM_PREP_FLAGS) {
639 		DRM_ERROR("invalid op: %08x\n", args->op);
640 		return -EINVAL;
641 	}
642 
643 	obj = drm_gem_object_lookup(file, args->handle);
644 	if (!obj)
645 		return -ENOENT;
646 
647 	ret = msm_gem_cpu_prep(obj, args->op, &timeout);
648 
649 	drm_gem_object_unreference_unlocked(obj);
650 
651 	return ret;
652 }
653 
654 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
655 		struct drm_file *file)
656 {
657 	struct drm_msm_gem_cpu_fini *args = data;
658 	struct drm_gem_object *obj;
659 	int ret;
660 
661 	obj = drm_gem_object_lookup(file, args->handle);
662 	if (!obj)
663 		return -ENOENT;
664 
665 	ret = msm_gem_cpu_fini(obj);
666 
667 	drm_gem_object_unreference_unlocked(obj);
668 
669 	return ret;
670 }
671 
672 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
673 		struct drm_file *file)
674 {
675 	struct drm_msm_gem_info *args = data;
676 	struct drm_gem_object *obj;
677 	int ret = 0;
678 
679 	if (args->pad)
680 		return -EINVAL;
681 
682 	obj = drm_gem_object_lookup(file, args->handle);
683 	if (!obj)
684 		return -ENOENT;
685 
686 	args->offset = msm_gem_mmap_offset(obj);
687 
688 	drm_gem_object_unreference_unlocked(obj);
689 
690 	return ret;
691 }
692 
693 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
694 		struct drm_file *file)
695 {
696 	struct msm_drm_private *priv = dev->dev_private;
697 	struct drm_msm_wait_fence *args = data;
698 	ktime_t timeout = to_ktime(args->timeout);
699 
700 	if (args->pad) {
701 		DRM_ERROR("invalid pad: %08x\n", args->pad);
702 		return -EINVAL;
703 	}
704 
705 	if (!priv->gpu)
706 		return 0;
707 
708 	return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
709 }
710 
711 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
712 		struct drm_file *file)
713 {
714 	struct drm_msm_gem_madvise *args = data;
715 	struct drm_gem_object *obj;
716 	int ret;
717 
718 	switch (args->madv) {
719 	case MSM_MADV_DONTNEED:
720 	case MSM_MADV_WILLNEED:
721 		break;
722 	default:
723 		return -EINVAL;
724 	}
725 
726 	ret = mutex_lock_interruptible(&dev->struct_mutex);
727 	if (ret)
728 		return ret;
729 
730 	obj = drm_gem_object_lookup(file, args->handle);
731 	if (!obj) {
732 		ret = -ENOENT;
733 		goto unlock;
734 	}
735 
736 	ret = msm_gem_madvise(obj, args->madv);
737 	if (ret >= 0) {
738 		args->retained = ret;
739 		ret = 0;
740 	}
741 
742 	drm_gem_object_unreference(obj);
743 
744 unlock:
745 	mutex_unlock(&dev->struct_mutex);
746 	return ret;
747 }
748 
749 static const struct drm_ioctl_desc msm_ioctls[] = {
750 	DRM_IOCTL_DEF_DRV(MSM_GET_PARAM,    msm_ioctl_get_param,    DRM_AUTH|DRM_RENDER_ALLOW),
751 	DRM_IOCTL_DEF_DRV(MSM_GEM_NEW,      msm_ioctl_gem_new,      DRM_AUTH|DRM_RENDER_ALLOW),
752 	DRM_IOCTL_DEF_DRV(MSM_GEM_INFO,     msm_ioctl_gem_info,     DRM_AUTH|DRM_RENDER_ALLOW),
753 	DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
754 	DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
755 	DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT,   msm_ioctl_gem_submit,   DRM_AUTH|DRM_RENDER_ALLOW),
756 	DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE,   msm_ioctl_wait_fence,   DRM_AUTH|DRM_RENDER_ALLOW),
757 	DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE,  msm_ioctl_gem_madvise,  DRM_AUTH|DRM_RENDER_ALLOW),
758 };
759 
760 static const struct vm_operations_struct vm_ops = {
761 	.fault = msm_gem_fault,
762 	.open = drm_gem_vm_open,
763 	.close = drm_gem_vm_close,
764 };
765 
766 static const struct file_operations fops = {
767 	.owner              = THIS_MODULE,
768 	.open               = drm_open,
769 	.release            = drm_release,
770 	.unlocked_ioctl     = drm_ioctl,
771 #ifdef CONFIG_COMPAT
772 	.compat_ioctl       = drm_compat_ioctl,
773 #endif
774 	.poll               = drm_poll,
775 	.read               = drm_read,
776 	.llseek             = no_llseek,
777 	.mmap               = msm_gem_mmap,
778 };
779 
780 static struct drm_driver msm_driver = {
781 	.driver_features    = DRIVER_HAVE_IRQ |
782 				DRIVER_GEM |
783 				DRIVER_PRIME |
784 				DRIVER_RENDER |
785 				DRIVER_ATOMIC |
786 				DRIVER_MODESET,
787 	.open               = msm_open,
788 	.preclose           = msm_preclose,
789 	.lastclose          = msm_lastclose,
790 	.irq_handler        = msm_irq,
791 	.irq_preinstall     = msm_irq_preinstall,
792 	.irq_postinstall    = msm_irq_postinstall,
793 	.irq_uninstall      = msm_irq_uninstall,
794 	.get_vblank_counter = drm_vblank_no_hw_counter,
795 	.enable_vblank      = msm_enable_vblank,
796 	.disable_vblank     = msm_disable_vblank,
797 	.gem_free_object    = msm_gem_free_object,
798 	.gem_vm_ops         = &vm_ops,
799 	.dumb_create        = msm_gem_dumb_create,
800 	.dumb_map_offset    = msm_gem_dumb_map_offset,
801 	.dumb_destroy       = drm_gem_dumb_destroy,
802 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
803 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
804 	.gem_prime_export   = drm_gem_prime_export,
805 	.gem_prime_import   = drm_gem_prime_import,
806 	.gem_prime_pin      = msm_gem_prime_pin,
807 	.gem_prime_unpin    = msm_gem_prime_unpin,
808 	.gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
809 	.gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
810 	.gem_prime_vmap     = msm_gem_prime_vmap,
811 	.gem_prime_vunmap   = msm_gem_prime_vunmap,
812 	.gem_prime_mmap     = msm_gem_prime_mmap,
813 #ifdef CONFIG_DEBUG_FS
814 	.debugfs_init       = msm_debugfs_init,
815 	.debugfs_cleanup    = msm_debugfs_cleanup,
816 #endif
817 	.ioctls             = msm_ioctls,
818 	.num_ioctls         = DRM_MSM_NUM_IOCTLS,
819 	.fops               = &fops,
820 	.name               = "msm",
821 	.desc               = "MSM Snapdragon DRM",
822 	.date               = "20130625",
823 	.major              = MSM_VERSION_MAJOR,
824 	.minor              = MSM_VERSION_MINOR,
825 	.patchlevel         = MSM_VERSION_PATCHLEVEL,
826 };
827 
828 #ifdef CONFIG_PM_SLEEP
829 static int msm_pm_suspend(struct device *dev)
830 {
831 	struct drm_device *ddev = dev_get_drvdata(dev);
832 
833 	drm_kms_helper_poll_disable(ddev);
834 
835 	return 0;
836 }
837 
838 static int msm_pm_resume(struct device *dev)
839 {
840 	struct drm_device *ddev = dev_get_drvdata(dev);
841 
842 	drm_kms_helper_poll_enable(ddev);
843 
844 	return 0;
845 }
846 #endif
847 
848 static const struct dev_pm_ops msm_pm_ops = {
849 	SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
850 };
851 
852 /*
853  * Componentized driver support:
854  */
855 
856 /*
857  * NOTE: duplication of the same code as exynos or imx (or probably any other).
858  * so probably some room for some helpers
859  */
860 static int compare_of(struct device *dev, void *data)
861 {
862 	return dev->of_node == data;
863 }
864 
865 /*
866  * Identify what components need to be added by parsing what remote-endpoints
867  * our MDP output ports are connected to. In the case of LVDS on MDP4, there
868  * is no external component that we need to add since LVDS is within MDP4
869  * itself.
870  */
871 static int add_components_mdp(struct device *mdp_dev,
872 			      struct component_match **matchptr)
873 {
874 	struct device_node *np = mdp_dev->of_node;
875 	struct device_node *ep_node;
876 	struct device *master_dev;
877 
878 	/*
879 	 * on MDP4 based platforms, the MDP platform device is the component
880 	 * master that adds other display interface components to itself.
881 	 *
882 	 * on MDP5 based platforms, the MDSS platform device is the component
883 	 * master that adds MDP5 and other display interface components to
884 	 * itself.
885 	 */
886 	if (of_device_is_compatible(np, "qcom,mdp4"))
887 		master_dev = mdp_dev;
888 	else
889 		master_dev = mdp_dev->parent;
890 
891 	for_each_endpoint_of_node(np, ep_node) {
892 		struct device_node *intf;
893 		struct of_endpoint ep;
894 		int ret;
895 
896 		ret = of_graph_parse_endpoint(ep_node, &ep);
897 		if (ret) {
898 			dev_err(mdp_dev, "unable to parse port endpoint\n");
899 			of_node_put(ep_node);
900 			return ret;
901 		}
902 
903 		/*
904 		 * The LCDC/LVDS port on MDP4 is a speacial case where the
905 		 * remote-endpoint isn't a component that we need to add
906 		 */
907 		if (of_device_is_compatible(np, "qcom,mdp4") &&
908 		    ep.port == 0) {
909 			of_node_put(ep_node);
910 			continue;
911 		}
912 
913 		/*
914 		 * It's okay if some of the ports don't have a remote endpoint
915 		 * specified. It just means that the port isn't connected to
916 		 * any external interface.
917 		 */
918 		intf = of_graph_get_remote_port_parent(ep_node);
919 		if (!intf) {
920 			of_node_put(ep_node);
921 			continue;
922 		}
923 
924 		drm_of_component_match_add(master_dev, matchptr, compare_of,
925 					   intf);
926 		of_node_put(intf);
927 		of_node_put(ep_node);
928 	}
929 
930 	return 0;
931 }
932 
933 static int compare_name_mdp(struct device *dev, void *data)
934 {
935 	return (strstr(dev_name(dev), "mdp") != NULL);
936 }
937 
938 static int add_display_components(struct device *dev,
939 				  struct component_match **matchptr)
940 {
941 	struct device *mdp_dev;
942 	int ret;
943 
944 	/*
945 	 * MDP5 based devices don't have a flat hierarchy. There is a top level
946 	 * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
947 	 * children devices, find the MDP5 node, and then add the interfaces
948 	 * to our components list.
949 	 */
950 	if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
951 		ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
952 		if (ret) {
953 			dev_err(dev, "failed to populate children devices\n");
954 			return ret;
955 		}
956 
957 		mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
958 		if (!mdp_dev) {
959 			dev_err(dev, "failed to find MDSS MDP node\n");
960 			of_platform_depopulate(dev);
961 			return -ENODEV;
962 		}
963 
964 		put_device(mdp_dev);
965 
966 		/* add the MDP component itself */
967 		drm_of_component_match_add(dev, matchptr, compare_of,
968 					   mdp_dev->of_node);
969 	} else {
970 		/* MDP4 */
971 		mdp_dev = dev;
972 	}
973 
974 	ret = add_components_mdp(mdp_dev, matchptr);
975 	if (ret)
976 		of_platform_depopulate(dev);
977 
978 	return ret;
979 }
980 
981 /*
982  * We don't know what's the best binding to link the gpu with the drm device.
983  * Fow now, we just hunt for all the possible gpus that we support, and add them
984  * as components.
985  */
986 static const struct of_device_id msm_gpu_match[] = {
987 	{ .compatible = "qcom,adreno-3xx" },
988 	{ .compatible = "qcom,kgsl-3d0" },
989 	{ },
990 };
991 
992 static int add_gpu_components(struct device *dev,
993 			      struct component_match **matchptr)
994 {
995 	struct device_node *np;
996 
997 	np = of_find_matching_node(NULL, msm_gpu_match);
998 	if (!np)
999 		return 0;
1000 
1001 	drm_of_component_match_add(dev, matchptr, compare_of, np);
1002 
1003 	of_node_put(np);
1004 
1005 	return 0;
1006 }
1007 
1008 static int msm_drm_bind(struct device *dev)
1009 {
1010 	return msm_drm_init(dev, &msm_driver);
1011 }
1012 
1013 static void msm_drm_unbind(struct device *dev)
1014 {
1015 	msm_drm_uninit(dev);
1016 }
1017 
1018 static const struct component_master_ops msm_drm_ops = {
1019 	.bind = msm_drm_bind,
1020 	.unbind = msm_drm_unbind,
1021 };
1022 
1023 /*
1024  * Platform driver:
1025  */
1026 
1027 static int msm_pdev_probe(struct platform_device *pdev)
1028 {
1029 	struct component_match *match = NULL;
1030 	int ret;
1031 
1032 	ret = add_display_components(&pdev->dev, &match);
1033 	if (ret)
1034 		return ret;
1035 
1036 	ret = add_gpu_components(&pdev->dev, &match);
1037 	if (ret)
1038 		return ret;
1039 
1040 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1041 	return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1042 }
1043 
1044 static int msm_pdev_remove(struct platform_device *pdev)
1045 {
1046 	component_master_del(&pdev->dev, &msm_drm_ops);
1047 	of_platform_depopulate(&pdev->dev);
1048 
1049 	return 0;
1050 }
1051 
1052 static const struct of_device_id dt_match[] = {
1053 	{ .compatible = "qcom,mdp4", .data = (void *)4 },	/* MDP4 */
1054 	{ .compatible = "qcom,mdss", .data = (void *)5 },	/* MDP5 MDSS */
1055 	{}
1056 };
1057 MODULE_DEVICE_TABLE(of, dt_match);
1058 
1059 static struct platform_driver msm_platform_driver = {
1060 	.probe      = msm_pdev_probe,
1061 	.remove     = msm_pdev_remove,
1062 	.driver     = {
1063 		.name   = "msm",
1064 		.of_match_table = dt_match,
1065 		.pm     = &msm_pm_ops,
1066 	},
1067 };
1068 
1069 static int __init msm_drm_register(void)
1070 {
1071 	DBG("init");
1072 	msm_mdp_register();
1073 	msm_dsi_register();
1074 	msm_edp_register();
1075 	msm_hdmi_register();
1076 	adreno_register();
1077 	return platform_driver_register(&msm_platform_driver);
1078 }
1079 
1080 static void __exit msm_drm_unregister(void)
1081 {
1082 	DBG("fini");
1083 	platform_driver_unregister(&msm_platform_driver);
1084 	msm_hdmi_unregister();
1085 	adreno_unregister();
1086 	msm_edp_unregister();
1087 	msm_dsi_unregister();
1088 	msm_mdp_unregister();
1089 }
1090 
1091 module_init(msm_drm_register);
1092 module_exit(msm_drm_unregister);
1093 
1094 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1095 MODULE_DESCRIPTION("MSM DRM Driver");
1096 MODULE_LICENSE("GPL");
1097