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