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