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