1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016-2018, 2020-2021 The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7 
8 #include <linux/dma-mapping.h>
9 #include <linux/kthread.h>
10 #include <linux/sched/mm.h>
11 #include <linux/uaccess.h>
12 #include <uapi/linux/sched/types.h>
13 
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_drv.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_ioctl.h>
18 #include <drm/drm_prime.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_vblank.h>
21 
22 #include "disp/msm_disp_snapshot.h"
23 #include "msm_drv.h"
24 #include "msm_debugfs.h"
25 #include "msm_fence.h"
26 #include "msm_gem.h"
27 #include "msm_gpu.h"
28 #include "msm_kms.h"
29 #include "adreno/adreno_gpu.h"
30 
31 /*
32  * MSM driver version:
33  * - 1.0.0 - initial interface
34  * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
35  * - 1.2.0 - adds explicit fence support for submit ioctl
36  * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
37  *           SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
38  *           MSM_GEM_INFO ioctl.
39  * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
40  *           GEM object's debug name
41  * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl
42  * - 1.6.0 - Syncobj support
43  * - 1.7.0 - Add MSM_PARAM_SUSPENDS to access suspend count
44  * - 1.8.0 - Add MSM_BO_CACHED_COHERENT for supported GPUs (a6xx)
45  * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN
46  */
47 #define MSM_VERSION_MAJOR	1
48 #define MSM_VERSION_MINOR	9
49 #define MSM_VERSION_PATCHLEVEL	0
50 
51 static const struct drm_mode_config_funcs mode_config_funcs = {
52 	.fb_create = msm_framebuffer_create,
53 	.output_poll_changed = drm_fb_helper_output_poll_changed,
54 	.atomic_check = drm_atomic_helper_check,
55 	.atomic_commit = drm_atomic_helper_commit,
56 };
57 
58 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
59 	.atomic_commit_tail = msm_atomic_commit_tail,
60 };
61 
62 #ifdef CONFIG_DRM_FBDEV_EMULATION
63 static bool fbdev = true;
64 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
65 module_param(fbdev, bool, 0600);
66 #endif
67 
68 static char *vram = "16m";
69 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
70 module_param(vram, charp, 0);
71 
72 bool dumpstate;
73 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
74 module_param(dumpstate, bool, 0600);
75 
76 static bool modeset = true;
77 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
78 module_param(modeset, bool, 0600);
79 
msm_irq(int irq,void * arg)80 static irqreturn_t msm_irq(int irq, void *arg)
81 {
82 	struct drm_device *dev = arg;
83 	struct msm_drm_private *priv = dev->dev_private;
84 	struct msm_kms *kms = priv->kms;
85 
86 	BUG_ON(!kms);
87 
88 	return kms->funcs->irq(kms);
89 }
90 
msm_irq_preinstall(struct drm_device * dev)91 static void msm_irq_preinstall(struct drm_device *dev)
92 {
93 	struct msm_drm_private *priv = dev->dev_private;
94 	struct msm_kms *kms = priv->kms;
95 
96 	BUG_ON(!kms);
97 
98 	kms->funcs->irq_preinstall(kms);
99 }
100 
msm_irq_postinstall(struct drm_device * dev)101 static int msm_irq_postinstall(struct drm_device *dev)
102 {
103 	struct msm_drm_private *priv = dev->dev_private;
104 	struct msm_kms *kms = priv->kms;
105 
106 	BUG_ON(!kms);
107 
108 	if (kms->funcs->irq_postinstall)
109 		return kms->funcs->irq_postinstall(kms);
110 
111 	return 0;
112 }
113 
msm_irq_install(struct drm_device * dev,unsigned int irq)114 static int msm_irq_install(struct drm_device *dev, unsigned int irq)
115 {
116 	struct msm_drm_private *priv = dev->dev_private;
117 	struct msm_kms *kms = priv->kms;
118 	int ret;
119 
120 	if (irq == IRQ_NOTCONNECTED)
121 		return -ENOTCONN;
122 
123 	msm_irq_preinstall(dev);
124 
125 	ret = request_irq(irq, msm_irq, 0, dev->driver->name, dev);
126 	if (ret)
127 		return ret;
128 
129 	kms->irq_requested = true;
130 
131 	ret = msm_irq_postinstall(dev);
132 	if (ret) {
133 		free_irq(irq, dev);
134 		return ret;
135 	}
136 
137 	return 0;
138 }
139 
msm_irq_uninstall(struct drm_device * dev)140 static void msm_irq_uninstall(struct drm_device *dev)
141 {
142 	struct msm_drm_private *priv = dev->dev_private;
143 	struct msm_kms *kms = priv->kms;
144 
145 	kms->funcs->irq_uninstall(kms);
146 	if (kms->irq_requested)
147 		free_irq(kms->irq, dev);
148 }
149 
150 struct msm_vblank_work {
151 	struct work_struct work;
152 	int crtc_id;
153 	bool enable;
154 	struct msm_drm_private *priv;
155 };
156 
vblank_ctrl_worker(struct work_struct * work)157 static void vblank_ctrl_worker(struct work_struct *work)
158 {
159 	struct msm_vblank_work *vbl_work = container_of(work,
160 						struct msm_vblank_work, work);
161 	struct msm_drm_private *priv = vbl_work->priv;
162 	struct msm_kms *kms = priv->kms;
163 
164 	if (vbl_work->enable)
165 		kms->funcs->enable_vblank(kms, priv->crtcs[vbl_work->crtc_id]);
166 	else
167 		kms->funcs->disable_vblank(kms,	priv->crtcs[vbl_work->crtc_id]);
168 
169 	kfree(vbl_work);
170 }
171 
vblank_ctrl_queue_work(struct msm_drm_private * priv,int crtc_id,bool enable)172 static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
173 					int crtc_id, bool enable)
174 {
175 	struct msm_vblank_work *vbl_work;
176 
177 	vbl_work = kzalloc(sizeof(*vbl_work), GFP_ATOMIC);
178 	if (!vbl_work)
179 		return -ENOMEM;
180 
181 	INIT_WORK(&vbl_work->work, vblank_ctrl_worker);
182 
183 	vbl_work->crtc_id = crtc_id;
184 	vbl_work->enable = enable;
185 	vbl_work->priv = priv;
186 
187 	queue_work(priv->wq, &vbl_work->work);
188 
189 	return 0;
190 }
191 
msm_drm_uninit(struct device * dev)192 static int msm_drm_uninit(struct device *dev)
193 {
194 	struct platform_device *pdev = to_platform_device(dev);
195 	struct msm_drm_private *priv = platform_get_drvdata(pdev);
196 	struct drm_device *ddev = priv->dev;
197 	struct msm_kms *kms = priv->kms;
198 	int i;
199 
200 	/*
201 	 * Shutdown the hw if we're far enough along where things might be on.
202 	 * If we run this too early, we'll end up panicking in any variety of
203 	 * places. Since we don't register the drm device until late in
204 	 * msm_drm_init, drm_dev->registered is used as an indicator that the
205 	 * shutdown will be successful.
206 	 */
207 	if (ddev->registered) {
208 		drm_dev_unregister(ddev);
209 		drm_atomic_helper_shutdown(ddev);
210 	}
211 
212 	/* We must cancel and cleanup any pending vblank enable/disable
213 	 * work before msm_irq_uninstall() to avoid work re-enabling an
214 	 * irq after uninstall has disabled it.
215 	 */
216 
217 	flush_workqueue(priv->wq);
218 
219 	/* clean up event worker threads */
220 	for (i = 0; i < priv->num_crtcs; i++) {
221 		if (priv->event_thread[i].worker)
222 			kthread_destroy_worker(priv->event_thread[i].worker);
223 	}
224 
225 	msm_gem_shrinker_cleanup(ddev);
226 
227 	drm_kms_helper_poll_fini(ddev);
228 
229 	msm_perf_debugfs_cleanup(priv);
230 	msm_rd_debugfs_cleanup(priv);
231 
232 #ifdef CONFIG_DRM_FBDEV_EMULATION
233 	if (fbdev && priv->fbdev)
234 		msm_fbdev_free(ddev);
235 #endif
236 
237 	msm_disp_snapshot_destroy(ddev);
238 
239 	drm_mode_config_cleanup(ddev);
240 
241 	for (i = 0; i < priv->num_bridges; i++)
242 		drm_bridge_remove(priv->bridges[i]);
243 
244 	pm_runtime_get_sync(dev);
245 	msm_irq_uninstall(ddev);
246 	pm_runtime_put_sync(dev);
247 
248 	if (kms && kms->funcs)
249 		kms->funcs->destroy(kms);
250 
251 	if (priv->vram.paddr) {
252 		unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
253 		drm_mm_takedown(&priv->vram.mm);
254 		dma_free_attrs(dev, priv->vram.size, NULL,
255 			       priv->vram.paddr, attrs);
256 	}
257 
258 	component_unbind_all(dev, ddev);
259 
260 	ddev->dev_private = NULL;
261 	drm_dev_put(ddev);
262 
263 	destroy_workqueue(priv->wq);
264 
265 	return 0;
266 }
267 
268 #include <linux/of_address.h>
269 
msm_use_mmu(struct drm_device * dev)270 bool msm_use_mmu(struct drm_device *dev)
271 {
272 	struct msm_drm_private *priv = dev->dev_private;
273 
274 	/* a2xx comes with its own MMU */
275 	return priv->is_a2xx || iommu_present(&platform_bus_type);
276 }
277 
msm_init_vram(struct drm_device * dev)278 static int msm_init_vram(struct drm_device *dev)
279 {
280 	struct msm_drm_private *priv = dev->dev_private;
281 	struct device_node *node;
282 	unsigned long size = 0;
283 	int ret = 0;
284 
285 	/* In the device-tree world, we could have a 'memory-region'
286 	 * phandle, which gives us a link to our "vram".  Allocating
287 	 * is all nicely abstracted behind the dma api, but we need
288 	 * to know the entire size to allocate it all in one go. There
289 	 * are two cases:
290 	 *  1) device with no IOMMU, in which case we need exclusive
291 	 *     access to a VRAM carveout big enough for all gpu
292 	 *     buffers
293 	 *  2) device with IOMMU, but where the bootloader puts up
294 	 *     a splash screen.  In this case, the VRAM carveout
295 	 *     need only be large enough for fbdev fb.  But we need
296 	 *     exclusive access to the buffer to avoid the kernel
297 	 *     using those pages for other purposes (which appears
298 	 *     as corruption on screen before we have a chance to
299 	 *     load and do initial modeset)
300 	 */
301 
302 	node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
303 	if (node) {
304 		struct resource r;
305 		ret = of_address_to_resource(node, 0, &r);
306 		of_node_put(node);
307 		if (ret)
308 			return ret;
309 		size = r.end - r.start + 1;
310 		DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
311 
312 		/* if we have no IOMMU, then we need to use carveout allocator.
313 		 * Grab the entire CMA chunk carved out in early startup in
314 		 * mach-msm:
315 		 */
316 	} else if (!msm_use_mmu(dev)) {
317 		DRM_INFO("using %s VRAM carveout\n", vram);
318 		size = memparse(vram, NULL);
319 	}
320 
321 	if (size) {
322 		unsigned long attrs = 0;
323 		void *p;
324 
325 		priv->vram.size = size;
326 
327 		drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
328 		spin_lock_init(&priv->vram.lock);
329 
330 		attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
331 		attrs |= DMA_ATTR_WRITE_COMBINE;
332 
333 		/* note that for no-kernel-mapping, the vaddr returned
334 		 * is bogus, but non-null if allocation succeeded:
335 		 */
336 		p = dma_alloc_attrs(dev->dev, size,
337 				&priv->vram.paddr, GFP_KERNEL, attrs);
338 		if (!p) {
339 			DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
340 			priv->vram.paddr = 0;
341 			return -ENOMEM;
342 		}
343 
344 		DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
345 				(uint32_t)priv->vram.paddr,
346 				(uint32_t)(priv->vram.paddr + size));
347 	}
348 
349 	return ret;
350 }
351 
msm_drm_init(struct device * dev,const struct drm_driver * drv)352 static int msm_drm_init(struct device *dev, const struct drm_driver *drv)
353 {
354 	struct msm_drm_private *priv = dev_get_drvdata(dev);
355 	struct drm_device *ddev;
356 	struct msm_kms *kms;
357 	int ret, i;
358 
359 	if (drm_firmware_drivers_only())
360 		return -ENODEV;
361 
362 	ddev = drm_dev_alloc(drv, dev);
363 	if (IS_ERR(ddev)) {
364 		DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
365 		return PTR_ERR(ddev);
366 	}
367 	ddev->dev_private = priv;
368 	priv->dev = ddev;
369 
370 	priv->wq = alloc_ordered_workqueue("msm", 0);
371 	priv->hangcheck_period = DRM_MSM_HANGCHECK_DEFAULT_PERIOD;
372 
373 	INIT_LIST_HEAD(&priv->objects);
374 	mutex_init(&priv->obj_lock);
375 
376 	INIT_LIST_HEAD(&priv->inactive_willneed);
377 	INIT_LIST_HEAD(&priv->inactive_dontneed);
378 	INIT_LIST_HEAD(&priv->inactive_unpinned);
379 	mutex_init(&priv->mm_lock);
380 
381 	/* Teach lockdep about lock ordering wrt. shrinker: */
382 	fs_reclaim_acquire(GFP_KERNEL);
383 	might_lock(&priv->mm_lock);
384 	fs_reclaim_release(GFP_KERNEL);
385 
386 	drm_mode_config_init(ddev);
387 
388 	ret = msm_init_vram(ddev);
389 	if (ret)
390 		return ret;
391 
392 	/* Bind all our sub-components: */
393 	ret = component_bind_all(dev, ddev);
394 	if (ret)
395 		return ret;
396 
397 	dma_set_max_seg_size(dev, UINT_MAX);
398 
399 	msm_gem_shrinker_init(ddev);
400 
401 	if (priv->kms_init) {
402 		ret = priv->kms_init(ddev);
403 		if (ret) {
404 			DRM_DEV_ERROR(dev, "failed to load kms\n");
405 			priv->kms = NULL;
406 			goto err_msm_uninit;
407 		}
408 		kms = priv->kms;
409 	} else {
410 		/* valid only for the dummy headless case, where of_node=NULL */
411 		WARN_ON(dev->of_node);
412 		kms = NULL;
413 	}
414 
415 	/* Enable normalization of plane zpos */
416 	ddev->mode_config.normalize_zpos = true;
417 
418 	if (kms) {
419 		kms->dev = ddev;
420 		ret = kms->funcs->hw_init(kms);
421 		if (ret) {
422 			DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
423 			goto err_msm_uninit;
424 		}
425 	}
426 
427 	drm_helper_move_panel_connectors_to_head(ddev);
428 
429 	ddev->mode_config.funcs = &mode_config_funcs;
430 	ddev->mode_config.helper_private = &mode_config_helper_funcs;
431 
432 	for (i = 0; i < priv->num_crtcs; i++) {
433 		/* initialize event thread */
434 		priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
435 		priv->event_thread[i].dev = ddev;
436 		priv->event_thread[i].worker = kthread_create_worker(0,
437 			"crtc_event:%d", priv->event_thread[i].crtc_id);
438 		if (IS_ERR(priv->event_thread[i].worker)) {
439 			ret = PTR_ERR(priv->event_thread[i].worker);
440 			DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n");
441 			ret = PTR_ERR(priv->event_thread[i].worker);
442 			goto err_msm_uninit;
443 		}
444 
445 		sched_set_fifo(priv->event_thread[i].worker->task);
446 	}
447 
448 	ret = drm_vblank_init(ddev, priv->num_crtcs);
449 	if (ret < 0) {
450 		DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
451 		goto err_msm_uninit;
452 	}
453 
454 	if (kms) {
455 		pm_runtime_get_sync(dev);
456 		ret = msm_irq_install(ddev, kms->irq);
457 		pm_runtime_put_sync(dev);
458 		if (ret < 0) {
459 			DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
460 			goto err_msm_uninit;
461 		}
462 	}
463 
464 	ret = drm_dev_register(ddev, 0);
465 	if (ret)
466 		goto err_msm_uninit;
467 
468 	if (kms) {
469 		ret = msm_disp_snapshot_init(ddev);
470 		if (ret)
471 			DRM_DEV_ERROR(dev, "msm_disp_snapshot_init failed ret = %d\n", ret);
472 	}
473 	drm_mode_config_reset(ddev);
474 
475 #ifdef CONFIG_DRM_FBDEV_EMULATION
476 	if (kms && fbdev)
477 		priv->fbdev = msm_fbdev_init(ddev);
478 #endif
479 
480 	ret = msm_debugfs_late_init(ddev);
481 	if (ret)
482 		goto err_msm_uninit;
483 
484 	drm_kms_helper_poll_init(ddev);
485 
486 	return 0;
487 
488 err_msm_uninit:
489 	msm_drm_uninit(dev);
490 	return ret;
491 }
492 
493 /*
494  * DRM operations:
495  */
496 
load_gpu(struct drm_device * dev)497 static void load_gpu(struct drm_device *dev)
498 {
499 	static DEFINE_MUTEX(init_lock);
500 	struct msm_drm_private *priv = dev->dev_private;
501 
502 	mutex_lock(&init_lock);
503 
504 	if (!priv->gpu)
505 		priv->gpu = adreno_load_gpu(dev);
506 
507 	mutex_unlock(&init_lock);
508 }
509 
context_init(struct drm_device * dev,struct drm_file * file)510 static int context_init(struct drm_device *dev, struct drm_file *file)
511 {
512 	static atomic_t ident = ATOMIC_INIT(0);
513 	struct msm_drm_private *priv = dev->dev_private;
514 	struct msm_file_private *ctx;
515 
516 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
517 	if (!ctx)
518 		return -ENOMEM;
519 
520 	INIT_LIST_HEAD(&ctx->submitqueues);
521 	rwlock_init(&ctx->queuelock);
522 
523 	kref_init(&ctx->ref);
524 	msm_submitqueue_init(dev, ctx);
525 
526 	ctx->aspace = msm_gpu_create_private_address_space(priv->gpu, current);
527 	file->driver_priv = ctx;
528 
529 	ctx->seqno = atomic_inc_return(&ident);
530 
531 	return 0;
532 }
533 
msm_open(struct drm_device * dev,struct drm_file * file)534 static int msm_open(struct drm_device *dev, struct drm_file *file)
535 {
536 	/* For now, load gpu on open.. to avoid the requirement of having
537 	 * firmware in the initrd.
538 	 */
539 	load_gpu(dev);
540 
541 	return context_init(dev, file);
542 }
543 
context_close(struct msm_file_private * ctx)544 static void context_close(struct msm_file_private *ctx)
545 {
546 	msm_submitqueue_close(ctx);
547 	msm_file_private_put(ctx);
548 }
549 
msm_postclose(struct drm_device * dev,struct drm_file * file)550 static void msm_postclose(struct drm_device *dev, struct drm_file *file)
551 {
552 	struct msm_drm_private *priv = dev->dev_private;
553 	struct msm_file_private *ctx = file->driver_priv;
554 
555 	/*
556 	 * It is not possible to set sysprof param to non-zero if gpu
557 	 * is not initialized:
558 	 */
559 	if (priv->gpu)
560 		msm_file_private_set_sysprof(ctx, priv->gpu, 0);
561 
562 	context_close(ctx);
563 }
564 
msm_crtc_enable_vblank(struct drm_crtc * crtc)565 int msm_crtc_enable_vblank(struct drm_crtc *crtc)
566 {
567 	struct drm_device *dev = crtc->dev;
568 	unsigned int pipe = crtc->index;
569 	struct msm_drm_private *priv = dev->dev_private;
570 	struct msm_kms *kms = priv->kms;
571 	if (!kms)
572 		return -ENXIO;
573 	drm_dbg_vbl(dev, "crtc=%u", pipe);
574 	return vblank_ctrl_queue_work(priv, pipe, true);
575 }
576 
msm_crtc_disable_vblank(struct drm_crtc * crtc)577 void msm_crtc_disable_vblank(struct drm_crtc *crtc)
578 {
579 	struct drm_device *dev = crtc->dev;
580 	unsigned int pipe = crtc->index;
581 	struct msm_drm_private *priv = dev->dev_private;
582 	struct msm_kms *kms = priv->kms;
583 	if (!kms)
584 		return;
585 	drm_dbg_vbl(dev, "crtc=%u", pipe);
586 	vblank_ctrl_queue_work(priv, pipe, false);
587 }
588 
589 /*
590  * DRM ioctls:
591  */
592 
msm_ioctl_get_param(struct drm_device * dev,void * data,struct drm_file * file)593 static int msm_ioctl_get_param(struct drm_device *dev, void *data,
594 		struct drm_file *file)
595 {
596 	struct msm_drm_private *priv = dev->dev_private;
597 	struct drm_msm_param *args = data;
598 	struct msm_gpu *gpu;
599 
600 	/* for now, we just have 3d pipe.. eventually this would need to
601 	 * be more clever to dispatch to appropriate gpu module:
602 	 */
603 	if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0))
604 		return -EINVAL;
605 
606 	gpu = priv->gpu;
607 
608 	if (!gpu)
609 		return -ENXIO;
610 
611 	return gpu->funcs->get_param(gpu, file->driver_priv,
612 				     args->param, &args->value, &args->len);
613 }
614 
msm_ioctl_set_param(struct drm_device * dev,void * data,struct drm_file * file)615 static int msm_ioctl_set_param(struct drm_device *dev, void *data,
616 		struct drm_file *file)
617 {
618 	struct msm_drm_private *priv = dev->dev_private;
619 	struct drm_msm_param *args = data;
620 	struct msm_gpu *gpu;
621 
622 	if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0))
623 		return -EINVAL;
624 
625 	gpu = priv->gpu;
626 
627 	if (!gpu)
628 		return -ENXIO;
629 
630 	return gpu->funcs->set_param(gpu, file->driver_priv,
631 				     args->param, args->value, args->len);
632 }
633 
msm_ioctl_gem_new(struct drm_device * dev,void * data,struct drm_file * file)634 static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
635 		struct drm_file *file)
636 {
637 	struct drm_msm_gem_new *args = data;
638 
639 	if (args->flags & ~MSM_BO_FLAGS) {
640 		DRM_ERROR("invalid flags: %08x\n", args->flags);
641 		return -EINVAL;
642 	}
643 
644 	return msm_gem_new_handle(dev, file, args->size,
645 			args->flags, &args->handle, NULL);
646 }
647 
to_ktime(struct drm_msm_timespec timeout)648 static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
649 {
650 	return ktime_set(timeout.tv_sec, timeout.tv_nsec);
651 }
652 
msm_ioctl_gem_cpu_prep(struct drm_device * dev,void * data,struct drm_file * file)653 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
654 		struct drm_file *file)
655 {
656 	struct drm_msm_gem_cpu_prep *args = data;
657 	struct drm_gem_object *obj;
658 	ktime_t timeout = to_ktime(args->timeout);
659 	int ret;
660 
661 	if (args->op & ~MSM_PREP_FLAGS) {
662 		DRM_ERROR("invalid op: %08x\n", args->op);
663 		return -EINVAL;
664 	}
665 
666 	obj = drm_gem_object_lookup(file, args->handle);
667 	if (!obj)
668 		return -ENOENT;
669 
670 	ret = msm_gem_cpu_prep(obj, args->op, &timeout);
671 
672 	drm_gem_object_put(obj);
673 
674 	return ret;
675 }
676 
msm_ioctl_gem_cpu_fini(struct drm_device * dev,void * data,struct drm_file * file)677 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
678 		struct drm_file *file)
679 {
680 	struct drm_msm_gem_cpu_fini *args = data;
681 	struct drm_gem_object *obj;
682 	int ret;
683 
684 	obj = drm_gem_object_lookup(file, args->handle);
685 	if (!obj)
686 		return -ENOENT;
687 
688 	ret = msm_gem_cpu_fini(obj);
689 
690 	drm_gem_object_put(obj);
691 
692 	return ret;
693 }
694 
msm_ioctl_gem_info_iova(struct drm_device * dev,struct drm_file * file,struct drm_gem_object * obj,uint64_t * iova)695 static int msm_ioctl_gem_info_iova(struct drm_device *dev,
696 		struct drm_file *file, struct drm_gem_object *obj,
697 		uint64_t *iova)
698 {
699 	struct msm_drm_private *priv = dev->dev_private;
700 	struct msm_file_private *ctx = file->driver_priv;
701 
702 	if (!priv->gpu)
703 		return -EINVAL;
704 
705 	/*
706 	 * Don't pin the memory here - just get an address so that userspace can
707 	 * be productive
708 	 */
709 	return msm_gem_get_iova(obj, ctx->aspace, iova);
710 }
711 
msm_ioctl_gem_info_set_iova(struct drm_device * dev,struct drm_file * file,struct drm_gem_object * obj,uint64_t iova)712 static int msm_ioctl_gem_info_set_iova(struct drm_device *dev,
713 		struct drm_file *file, struct drm_gem_object *obj,
714 		uint64_t iova)
715 {
716 	struct msm_drm_private *priv = dev->dev_private;
717 	struct msm_file_private *ctx = file->driver_priv;
718 
719 	if (!priv->gpu)
720 		return -EINVAL;
721 
722 	/* Only supported if per-process address space is supported: */
723 	if (priv->gpu->aspace == ctx->aspace)
724 		return -EOPNOTSUPP;
725 
726 	return msm_gem_set_iova(obj, ctx->aspace, iova);
727 }
728 
msm_ioctl_gem_info(struct drm_device * dev,void * data,struct drm_file * file)729 static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
730 		struct drm_file *file)
731 {
732 	struct drm_msm_gem_info *args = data;
733 	struct drm_gem_object *obj;
734 	struct msm_gem_object *msm_obj;
735 	int i, ret = 0;
736 
737 	if (args->pad)
738 		return -EINVAL;
739 
740 	switch (args->info) {
741 	case MSM_INFO_GET_OFFSET:
742 	case MSM_INFO_GET_IOVA:
743 	case MSM_INFO_SET_IOVA:
744 		/* value returned as immediate, not pointer, so len==0: */
745 		if (args->len)
746 			return -EINVAL;
747 		break;
748 	case MSM_INFO_SET_NAME:
749 	case MSM_INFO_GET_NAME:
750 		break;
751 	default:
752 		return -EINVAL;
753 	}
754 
755 	obj = drm_gem_object_lookup(file, args->handle);
756 	if (!obj)
757 		return -ENOENT;
758 
759 	msm_obj = to_msm_bo(obj);
760 
761 	switch (args->info) {
762 	case MSM_INFO_GET_OFFSET:
763 		args->value = msm_gem_mmap_offset(obj);
764 		break;
765 	case MSM_INFO_GET_IOVA:
766 		ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value);
767 		break;
768 	case MSM_INFO_SET_IOVA:
769 		ret = msm_ioctl_gem_info_set_iova(dev, file, obj, args->value);
770 		break;
771 	case MSM_INFO_SET_NAME:
772 		/* length check should leave room for terminating null: */
773 		if (args->len >= sizeof(msm_obj->name)) {
774 			ret = -EINVAL;
775 			break;
776 		}
777 		if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value),
778 				   args->len)) {
779 			msm_obj->name[0] = '\0';
780 			ret = -EFAULT;
781 			break;
782 		}
783 		msm_obj->name[args->len] = '\0';
784 		for (i = 0; i < args->len; i++) {
785 			if (!isprint(msm_obj->name[i])) {
786 				msm_obj->name[i] = '\0';
787 				break;
788 			}
789 		}
790 		break;
791 	case MSM_INFO_GET_NAME:
792 		if (args->value && (args->len < strlen(msm_obj->name))) {
793 			ret = -EINVAL;
794 			break;
795 		}
796 		args->len = strlen(msm_obj->name);
797 		if (args->value) {
798 			if (copy_to_user(u64_to_user_ptr(args->value),
799 					 msm_obj->name, args->len))
800 				ret = -EFAULT;
801 		}
802 		break;
803 	}
804 
805 	drm_gem_object_put(obj);
806 
807 	return ret;
808 }
809 
wait_fence(struct msm_gpu_submitqueue * queue,uint32_t fence_id,ktime_t timeout)810 static int wait_fence(struct msm_gpu_submitqueue *queue, uint32_t fence_id,
811 		      ktime_t timeout)
812 {
813 	struct dma_fence *fence;
814 	int ret;
815 
816 	if (fence_after(fence_id, queue->last_fence)) {
817 		DRM_ERROR_RATELIMITED("waiting on invalid fence: %u (of %u)\n",
818 				      fence_id, queue->last_fence);
819 		return -EINVAL;
820 	}
821 
822 	/*
823 	 * Map submitqueue scoped "seqno" (which is actually an idr key)
824 	 * back to underlying dma-fence
825 	 *
826 	 * The fence is removed from the fence_idr when the submit is
827 	 * retired, so if the fence is not found it means there is nothing
828 	 * to wait for
829 	 */
830 	ret = mutex_lock_interruptible(&queue->lock);
831 	if (ret)
832 		return ret;
833 	fence = idr_find(&queue->fence_idr, fence_id);
834 	if (fence)
835 		fence = dma_fence_get_rcu(fence);
836 	mutex_unlock(&queue->lock);
837 
838 	if (!fence)
839 		return 0;
840 
841 	ret = dma_fence_wait_timeout(fence, true, timeout_to_jiffies(&timeout));
842 	if (ret == 0) {
843 		ret = -ETIMEDOUT;
844 	} else if (ret != -ERESTARTSYS) {
845 		ret = 0;
846 	}
847 
848 	dma_fence_put(fence);
849 
850 	return ret;
851 }
852 
msm_ioctl_wait_fence(struct drm_device * dev,void * data,struct drm_file * file)853 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
854 		struct drm_file *file)
855 {
856 	struct msm_drm_private *priv = dev->dev_private;
857 	struct drm_msm_wait_fence *args = data;
858 	struct msm_gpu_submitqueue *queue;
859 	int ret;
860 
861 	if (args->pad) {
862 		DRM_ERROR("invalid pad: %08x\n", args->pad);
863 		return -EINVAL;
864 	}
865 
866 	if (!priv->gpu)
867 		return 0;
868 
869 	queue = msm_submitqueue_get(file->driver_priv, args->queueid);
870 	if (!queue)
871 		return -ENOENT;
872 
873 	ret = wait_fence(queue, args->fence, to_ktime(args->timeout));
874 
875 	msm_submitqueue_put(queue);
876 
877 	return ret;
878 }
879 
msm_ioctl_gem_madvise(struct drm_device * dev,void * data,struct drm_file * file)880 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
881 		struct drm_file *file)
882 {
883 	struct drm_msm_gem_madvise *args = data;
884 	struct drm_gem_object *obj;
885 	int ret;
886 
887 	switch (args->madv) {
888 	case MSM_MADV_DONTNEED:
889 	case MSM_MADV_WILLNEED:
890 		break;
891 	default:
892 		return -EINVAL;
893 	}
894 
895 	obj = drm_gem_object_lookup(file, args->handle);
896 	if (!obj) {
897 		return -ENOENT;
898 	}
899 
900 	ret = msm_gem_madvise(obj, args->madv);
901 	if (ret >= 0) {
902 		args->retained = ret;
903 		ret = 0;
904 	}
905 
906 	drm_gem_object_put(obj);
907 
908 	return ret;
909 }
910 
911 
msm_ioctl_submitqueue_new(struct drm_device * dev,void * data,struct drm_file * file)912 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
913 		struct drm_file *file)
914 {
915 	struct drm_msm_submitqueue *args = data;
916 
917 	if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
918 		return -EINVAL;
919 
920 	return msm_submitqueue_create(dev, file->driver_priv, args->prio,
921 		args->flags, &args->id);
922 }
923 
msm_ioctl_submitqueue_query(struct drm_device * dev,void * data,struct drm_file * file)924 static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data,
925 		struct drm_file *file)
926 {
927 	return msm_submitqueue_query(dev, file->driver_priv, data);
928 }
929 
msm_ioctl_submitqueue_close(struct drm_device * dev,void * data,struct drm_file * file)930 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
931 		struct drm_file *file)
932 {
933 	u32 id = *(u32 *) data;
934 
935 	return msm_submitqueue_remove(file->driver_priv, id);
936 }
937 
938 static const struct drm_ioctl_desc msm_ioctls[] = {
939 	DRM_IOCTL_DEF_DRV(MSM_GET_PARAM,    msm_ioctl_get_param,    DRM_RENDER_ALLOW),
940 	DRM_IOCTL_DEF_DRV(MSM_SET_PARAM,    msm_ioctl_set_param,    DRM_RENDER_ALLOW),
941 	DRM_IOCTL_DEF_DRV(MSM_GEM_NEW,      msm_ioctl_gem_new,      DRM_RENDER_ALLOW),
942 	DRM_IOCTL_DEF_DRV(MSM_GEM_INFO,     msm_ioctl_gem_info,     DRM_RENDER_ALLOW),
943 	DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW),
944 	DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW),
945 	DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT,   msm_ioctl_gem_submit,   DRM_RENDER_ALLOW),
946 	DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE,   msm_ioctl_wait_fence,   DRM_RENDER_ALLOW),
947 	DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE,  msm_ioctl_gem_madvise,  DRM_RENDER_ALLOW),
948 	DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW,   msm_ioctl_submitqueue_new,   DRM_RENDER_ALLOW),
949 	DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW),
950 	DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW),
951 };
952 
953 DEFINE_DRM_GEM_FOPS(fops);
954 
955 static const struct drm_driver msm_driver = {
956 	.driver_features    = DRIVER_GEM |
957 				DRIVER_RENDER |
958 				DRIVER_ATOMIC |
959 				DRIVER_MODESET |
960 				DRIVER_SYNCOBJ,
961 	.open               = msm_open,
962 	.postclose           = msm_postclose,
963 	.lastclose          = drm_fb_helper_lastclose,
964 	.dumb_create        = msm_gem_dumb_create,
965 	.dumb_map_offset    = msm_gem_dumb_map_offset,
966 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
967 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
968 	.gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
969 	.gem_prime_mmap     = msm_gem_prime_mmap,
970 #ifdef CONFIG_DEBUG_FS
971 	.debugfs_init       = msm_debugfs_init,
972 #endif
973 	.ioctls             = msm_ioctls,
974 	.num_ioctls         = ARRAY_SIZE(msm_ioctls),
975 	.fops               = &fops,
976 	.name               = "msm",
977 	.desc               = "MSM Snapdragon DRM",
978 	.date               = "20130625",
979 	.major              = MSM_VERSION_MAJOR,
980 	.minor              = MSM_VERSION_MINOR,
981 	.patchlevel         = MSM_VERSION_PATCHLEVEL,
982 };
983 
msm_pm_prepare(struct device * dev)984 int msm_pm_prepare(struct device *dev)
985 {
986 	struct msm_drm_private *priv = dev_get_drvdata(dev);
987 	struct drm_device *ddev = priv ? priv->dev : NULL;
988 
989 	if (!priv || !priv->kms)
990 		return 0;
991 
992 	return drm_mode_config_helper_suspend(ddev);
993 }
994 
msm_pm_complete(struct device * dev)995 void msm_pm_complete(struct device *dev)
996 {
997 	struct msm_drm_private *priv = dev_get_drvdata(dev);
998 	struct drm_device *ddev = priv ? priv->dev : NULL;
999 
1000 	if (!priv || !priv->kms)
1001 		return;
1002 
1003 	drm_mode_config_helper_resume(ddev);
1004 }
1005 
1006 static const struct dev_pm_ops msm_pm_ops = {
1007 	.prepare = msm_pm_prepare,
1008 	.complete = msm_pm_complete,
1009 };
1010 
1011 /*
1012  * Componentized driver support:
1013  */
1014 
1015 /*
1016  * Identify what components need to be added by parsing what remote-endpoints
1017  * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1018  * is no external component that we need to add since LVDS is within MDP4
1019  * itself.
1020  */
add_components_mdp(struct device * master_dev,struct component_match ** matchptr)1021 static int add_components_mdp(struct device *master_dev,
1022 			      struct component_match **matchptr)
1023 {
1024 	struct device_node *np = master_dev->of_node;
1025 	struct device_node *ep_node;
1026 
1027 	for_each_endpoint_of_node(np, ep_node) {
1028 		struct device_node *intf;
1029 		struct of_endpoint ep;
1030 		int ret;
1031 
1032 		ret = of_graph_parse_endpoint(ep_node, &ep);
1033 		if (ret) {
1034 			DRM_DEV_ERROR(master_dev, "unable to parse port endpoint\n");
1035 			of_node_put(ep_node);
1036 			return ret;
1037 		}
1038 
1039 		/*
1040 		 * The LCDC/LVDS port on MDP4 is a speacial case where the
1041 		 * remote-endpoint isn't a component that we need to add
1042 		 */
1043 		if (of_device_is_compatible(np, "qcom,mdp4") &&
1044 		    ep.port == 0)
1045 			continue;
1046 
1047 		/*
1048 		 * It's okay if some of the ports don't have a remote endpoint
1049 		 * specified. It just means that the port isn't connected to
1050 		 * any external interface.
1051 		 */
1052 		intf = of_graph_get_remote_port_parent(ep_node);
1053 		if (!intf)
1054 			continue;
1055 
1056 		if (of_device_is_available(intf))
1057 			drm_of_component_match_add(master_dev, matchptr,
1058 						   component_compare_of, intf);
1059 
1060 		of_node_put(intf);
1061 	}
1062 
1063 	return 0;
1064 }
1065 
1066 /*
1067  * We don't know what's the best binding to link the gpu with the drm device.
1068  * Fow now, we just hunt for all the possible gpus that we support, and add them
1069  * as components.
1070  */
1071 static const struct of_device_id msm_gpu_match[] = {
1072 	{ .compatible = "qcom,adreno" },
1073 	{ .compatible = "qcom,adreno-3xx" },
1074 	{ .compatible = "amd,imageon" },
1075 	{ .compatible = "qcom,kgsl-3d0" },
1076 	{ },
1077 };
1078 
add_gpu_components(struct device * dev,struct component_match ** matchptr)1079 static int add_gpu_components(struct device *dev,
1080 			      struct component_match **matchptr)
1081 {
1082 	struct device_node *np;
1083 
1084 	np = of_find_matching_node(NULL, msm_gpu_match);
1085 	if (!np)
1086 		return 0;
1087 
1088 	if (of_device_is_available(np))
1089 		drm_of_component_match_add(dev, matchptr, component_compare_of, np);
1090 
1091 	of_node_put(np);
1092 
1093 	return 0;
1094 }
1095 
msm_drm_bind(struct device * dev)1096 static int msm_drm_bind(struct device *dev)
1097 {
1098 	return msm_drm_init(dev, &msm_driver);
1099 }
1100 
msm_drm_unbind(struct device * dev)1101 static void msm_drm_unbind(struct device *dev)
1102 {
1103 	msm_drm_uninit(dev);
1104 }
1105 
1106 const struct component_master_ops msm_drm_ops = {
1107 	.bind = msm_drm_bind,
1108 	.unbind = msm_drm_unbind,
1109 };
1110 
msm_drv_probe(struct device * master_dev,int (* kms_init)(struct drm_device * dev))1111 int msm_drv_probe(struct device *master_dev,
1112 	int (*kms_init)(struct drm_device *dev))
1113 {
1114 	struct msm_drm_private *priv;
1115 	struct component_match *match = NULL;
1116 	int ret;
1117 
1118 	priv = devm_kzalloc(master_dev, sizeof(*priv), GFP_KERNEL);
1119 	if (!priv)
1120 		return -ENOMEM;
1121 
1122 	priv->kms_init = kms_init;
1123 	dev_set_drvdata(master_dev, priv);
1124 
1125 	/* Add mdp components if we have KMS. */
1126 	if (kms_init) {
1127 		ret = add_components_mdp(master_dev, &match);
1128 		if (ret)
1129 			return ret;
1130 	}
1131 
1132 	ret = add_gpu_components(master_dev, &match);
1133 	if (ret)
1134 		return ret;
1135 
1136 	/* on all devices that I am aware of, iommu's which can map
1137 	 * any address the cpu can see are used:
1138 	 */
1139 	ret = dma_set_mask_and_coherent(master_dev, ~0);
1140 	if (ret)
1141 		return ret;
1142 
1143 	ret = component_master_add_with_match(master_dev, &msm_drm_ops, match);
1144 	if (ret)
1145 		return ret;
1146 
1147 	return 0;
1148 }
1149 
1150 /*
1151  * Platform driver:
1152  * Used only for headlesss GPU instances
1153  */
1154 
msm_pdev_probe(struct platform_device * pdev)1155 static int msm_pdev_probe(struct platform_device *pdev)
1156 {
1157 	return msm_drv_probe(&pdev->dev, NULL);
1158 }
1159 
msm_pdev_remove(struct platform_device * pdev)1160 static int msm_pdev_remove(struct platform_device *pdev)
1161 {
1162 	component_master_del(&pdev->dev, &msm_drm_ops);
1163 
1164 	return 0;
1165 }
1166 
msm_drv_shutdown(struct platform_device * pdev)1167 void msm_drv_shutdown(struct platform_device *pdev)
1168 {
1169 	struct msm_drm_private *priv = platform_get_drvdata(pdev);
1170 	struct drm_device *drm = priv ? priv->dev : NULL;
1171 
1172 	if (!priv || !priv->kms)
1173 		return;
1174 
1175 	drm_atomic_helper_shutdown(drm);
1176 }
1177 
1178 static struct platform_driver msm_platform_driver = {
1179 	.probe      = msm_pdev_probe,
1180 	.remove     = msm_pdev_remove,
1181 	.shutdown   = msm_drv_shutdown,
1182 	.driver     = {
1183 		.name   = "msm",
1184 		.pm     = &msm_pm_ops,
1185 	},
1186 };
1187 
msm_drm_register(void)1188 static int __init msm_drm_register(void)
1189 {
1190 	if (!modeset)
1191 		return -EINVAL;
1192 
1193 	DBG("init");
1194 	msm_mdp_register();
1195 	msm_dpu_register();
1196 	msm_dsi_register();
1197 	msm_hdmi_register();
1198 	msm_dp_register();
1199 	adreno_register();
1200 	msm_mdp4_register();
1201 	msm_mdss_register();
1202 	return platform_driver_register(&msm_platform_driver);
1203 }
1204 
msm_drm_unregister(void)1205 static void __exit msm_drm_unregister(void)
1206 {
1207 	DBG("fini");
1208 	platform_driver_unregister(&msm_platform_driver);
1209 	msm_mdss_unregister();
1210 	msm_mdp4_unregister();
1211 	msm_dp_unregister();
1212 	msm_hdmi_unregister();
1213 	adreno_unregister();
1214 	msm_dsi_unregister();
1215 	msm_mdp_unregister();
1216 	msm_dpu_unregister();
1217 }
1218 
1219 module_init(msm_drm_register);
1220 module_exit(msm_drm_unregister);
1221 
1222 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1223 MODULE_DESCRIPTION("MSM DRM Driver");
1224 MODULE_LICENSE("GPL");
1225