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