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