blob: 0c38f34066e5582d0e7cb3f1198bc50c034ecf97 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
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"
Rob Clark7198e6b2013-07-19 12:59:32 -040019#include "msm_gpu.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050020#include "msm_kms.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040021
Rob Clarkc8afe682013-06-26 12:44:06 -040022static void msm_fb_output_poll_changed(struct drm_device *dev)
23{
24 struct msm_drm_private *priv = dev->dev_private;
25 if (priv->fbdev)
26 drm_fb_helper_hotplug_event(priv->fbdev);
27}
28
29static const struct drm_mode_config_funcs mode_config_funcs = {
30 .fb_create = msm_framebuffer_create,
31 .output_poll_changed = msm_fb_output_poll_changed,
Daniel Vetterb4274fb2014-11-26 17:02:18 +010032 .atomic_check = msm_atomic_check,
Rob Clarkcf3a7e42014-11-08 13:21:06 -050033 .atomic_commit = msm_atomic_commit,
Rob Clarkc8afe682013-06-26 12:44:06 -040034};
35
Rob Clark871d8122013-11-16 12:56:06 -050036int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
Rob Clarkc8afe682013-06-26 12:44:06 -040037{
38 struct msm_drm_private *priv = dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -050039 int idx = priv->num_mmus++;
Rob Clarkc8afe682013-06-26 12:44:06 -040040
Rob Clark871d8122013-11-16 12:56:06 -050041 if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
Rob Clarkc8afe682013-06-26 12:44:06 -040042 return -EINVAL;
43
Rob Clark871d8122013-11-16 12:56:06 -050044 priv->mmus[idx] = mmu;
Rob Clarkc8afe682013-06-26 12:44:06 -040045
46 return idx;
47}
48
Rob Clarkc8afe682013-06-26 12:44:06 -040049#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
50static bool reglog = false;
51MODULE_PARM_DESC(reglog, "Enable register read/write logging");
52module_param(reglog, bool, 0600);
53#else
54#define reglog 0
55#endif
56
Rob Clarke90dfec2015-01-30 17:05:41 -050057#ifdef CONFIG_DRM_MSM_FBDEV
58static bool fbdev = true;
59MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
60module_param(fbdev, bool, 0600);
61#endif
62
Rob Clark3a10ba82014-09-08 14:24:57 -040063static char *vram = "16m";
Rob Clark871d8122013-11-16 12:56:06 -050064MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU");
65module_param(vram, charp, 0);
66
Rob Clark060530f2014-03-03 14:19:12 -050067/*
68 * Util/helpers:
69 */
70
Rob Clarkc8afe682013-06-26 12:44:06 -040071void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
72 const char *dbgname)
73{
74 struct resource *res;
75 unsigned long size;
76 void __iomem *ptr;
77
78 if (name)
79 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
80 else
81 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82
83 if (!res) {
84 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
85 return ERR_PTR(-EINVAL);
86 }
87
88 size = resource_size(res);
89
90 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
91 if (!ptr) {
92 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
93 return ERR_PTR(-ENOMEM);
94 }
95
96 if (reglog)
97 printk(KERN_DEBUG "IO:region %s %08x %08lx\n", dbgname, (u32)ptr, size);
98
99 return ptr;
100}
101
102void msm_writel(u32 data, void __iomem *addr)
103{
104 if (reglog)
105 printk(KERN_DEBUG "IO:W %08x %08x\n", (u32)addr, data);
106 writel(data, addr);
107}
108
109u32 msm_readl(const void __iomem *addr)
110{
111 u32 val = readl(addr);
112 if (reglog)
113 printk(KERN_ERR "IO:R %08x %08x\n", (u32)addr, val);
114 return val;
115}
116
117/*
118 * DRM operations:
119 */
120
121static int msm_unload(struct drm_device *dev)
122{
123 struct msm_drm_private *priv = dev->dev_private;
124 struct msm_kms *kms = priv->kms;
Rob Clark7198e6b2013-07-19 12:59:32 -0400125 struct msm_gpu *gpu = priv->gpu;
Rob Clarkc8afe682013-06-26 12:44:06 -0400126
127 drm_kms_helper_poll_fini(dev);
128 drm_mode_config_cleanup(dev);
129 drm_vblank_cleanup(dev);
130
131 pm_runtime_get_sync(dev->dev);
132 drm_irq_uninstall(dev);
133 pm_runtime_put_sync(dev->dev);
134
135 flush_workqueue(priv->wq);
136 destroy_workqueue(priv->wq);
137
138 if (kms) {
139 pm_runtime_disable(dev->dev);
140 kms->funcs->destroy(kms);
141 }
142
Rob Clark7198e6b2013-07-19 12:59:32 -0400143 if (gpu) {
144 mutex_lock(&dev->struct_mutex);
145 gpu->funcs->pm_suspend(gpu);
146 gpu->funcs->destroy(gpu);
147 mutex_unlock(&dev->struct_mutex);
148 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400149
Rob Clark871d8122013-11-16 12:56:06 -0500150 if (priv->vram.paddr) {
151 DEFINE_DMA_ATTRS(attrs);
152 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
153 drm_mm_takedown(&priv->vram.mm);
154 dma_free_attrs(dev->dev, priv->vram.size, NULL,
155 priv->vram.paddr, &attrs);
156 }
157
Rob Clark060530f2014-03-03 14:19:12 -0500158 component_unbind_all(dev->dev, dev);
159
Rob Clarkc8afe682013-06-26 12:44:06 -0400160 dev->dev_private = NULL;
161
162 kfree(priv);
163
164 return 0;
165}
166
Rob Clark06c0dd92013-11-30 17:51:47 -0500167static int get_mdp_ver(struct platform_device *pdev)
168{
169#ifdef CONFIG_OF
Peter Griffin370a4d82014-06-05 18:30:58 +0100170 static const struct of_device_id match_types[] = { {
Rob Clark06c0dd92013-11-30 17:51:47 -0500171 .compatible = "qcom,mdss_mdp",
172 .data = (void *)5,
173 }, {
174 /* end node */
175 } };
176 struct device *dev = &pdev->dev;
177 const struct of_device_id *match;
178 match = of_match_node(match_types, dev->of_node);
179 if (match)
180 return (int)match->data;
181#endif
182 return 4;
183}
184
Rob Clark072f1f92015-03-03 15:04:25 -0500185#include <linux/of_address.h>
186
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500187static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400188{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500189 struct msm_drm_private *priv = dev->dev_private;
Rob Clark072f1f92015-03-03 15:04:25 -0500190 unsigned long size = 0;
191 int ret = 0;
192
193#ifdef CONFIG_OF
194 /* In the device-tree world, we could have a 'memory-region'
195 * phandle, which gives us a link to our "vram". Allocating
196 * is all nicely abstracted behind the dma api, but we need
197 * to know the entire size to allocate it all in one go. There
198 * are two cases:
199 * 1) device with no IOMMU, in which case we need exclusive
200 * access to a VRAM carveout big enough for all gpu
201 * buffers
202 * 2) device with IOMMU, but where the bootloader puts up
203 * a splash screen. In this case, the VRAM carveout
204 * need only be large enough for fbdev fb. But we need
205 * exclusive access to the buffer to avoid the kernel
206 * using those pages for other purposes (which appears
207 * as corruption on screen before we have a chance to
208 * load and do initial modeset)
209 */
210 struct device_node *node;
211
212 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
213 if (node) {
214 struct resource r;
215 ret = of_address_to_resource(node, 0, &r);
216 if (ret)
217 return ret;
218 size = r.end - r.start;
219 DRM_INFO("using VRAM carveout: %lx@%08x\n", size, r.start);
220 } else
221#endif
Rob Clarkc8afe682013-06-26 12:44:06 -0400222
Rob Clark871d8122013-11-16 12:56:06 -0500223 /* if we have no IOMMU, then we need to use carveout allocator.
224 * Grab the entire CMA chunk carved out in early startup in
225 * mach-msm:
226 */
227 if (!iommu_present(&platform_bus_type)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500228 DRM_INFO("using %s VRAM carveout\n", vram);
229 size = memparse(vram, NULL);
230 }
231
232 if (size) {
Rob Clark871d8122013-11-16 12:56:06 -0500233 DEFINE_DMA_ATTRS(attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500234 void *p;
235
Rob Clark871d8122013-11-16 12:56:06 -0500236 priv->vram.size = size;
237
238 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
239
240 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
241 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
242
243 /* note that for no-kernel-mapping, the vaddr returned
244 * is bogus, but non-null if allocation succeeded:
245 */
246 p = dma_alloc_attrs(dev->dev, size,
Rob Clark543d3012014-06-02 07:25:56 -0400247 &priv->vram.paddr, GFP_KERNEL, &attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500248 if (!p) {
249 dev_err(dev->dev, "failed to allocate VRAM\n");
250 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500251 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500252 }
253
254 dev_info(dev->dev, "VRAM: %08x->%08x\n",
255 (uint32_t)priv->vram.paddr,
256 (uint32_t)(priv->vram.paddr + size));
257 }
258
Rob Clark072f1f92015-03-03 15:04:25 -0500259 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500260}
261
262static int msm_load(struct drm_device *dev, unsigned long flags)
263{
264 struct platform_device *pdev = dev->platformdev;
265 struct msm_drm_private *priv;
266 struct msm_kms *kms;
267 int ret;
268
269 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
270 if (!priv) {
271 dev_err(dev->dev, "failed to allocate private data\n");
272 return -ENOMEM;
273 }
274
275 dev->dev_private = priv;
276
277 priv->wq = alloc_ordered_workqueue("msm", 0);
278 init_waitqueue_head(&priv->fence_event);
279 init_waitqueue_head(&priv->pending_crtcs_event);
280
281 INIT_LIST_HEAD(&priv->inactive_list);
282 INIT_LIST_HEAD(&priv->fence_cbs);
283
284 drm_mode_config_init(dev);
285
286 ret = msm_init_vram(dev);
287 if (ret)
288 goto fail;
289
Rob Clark060530f2014-03-03 14:19:12 -0500290 platform_set_drvdata(pdev, dev);
291
292 /* Bind all our sub-components: */
293 ret = component_bind_all(dev->dev, dev);
294 if (ret)
295 return ret;
296
Rob Clark06c0dd92013-11-30 17:51:47 -0500297 switch (get_mdp_ver(pdev)) {
298 case 4:
299 kms = mdp4_kms_init(dev);
300 break;
301 case 5:
302 kms = mdp5_kms_init(dev);
303 break;
304 default:
305 kms = ERR_PTR(-ENODEV);
306 break;
307 }
308
Rob Clarkc8afe682013-06-26 12:44:06 -0400309 if (IS_ERR(kms)) {
310 /*
311 * NOTE: once we have GPU support, having no kms should not
312 * be considered fatal.. ideally we would still support gpu
313 * and (for example) use dmabuf/prime to share buffers with
314 * imx drm driver on iMX5
315 */
316 dev_err(dev->dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200317 ret = PTR_ERR(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400318 goto fail;
319 }
320
321 priv->kms = kms;
322
323 if (kms) {
324 pm_runtime_enable(dev->dev);
325 ret = kms->funcs->hw_init(kms);
326 if (ret) {
327 dev_err(dev->dev, "kms hw init failed: %d\n", ret);
328 goto fail;
329 }
330 }
331
332 dev->mode_config.min_width = 0;
333 dev->mode_config.min_height = 0;
334 dev->mode_config.max_width = 2048;
335 dev->mode_config.max_height = 2048;
336 dev->mode_config.funcs = &mode_config_funcs;
337
Rob Clarkd65bd0e2014-08-06 07:43:12 -0400338 ret = drm_vblank_init(dev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400339 if (ret < 0) {
340 dev_err(dev->dev, "failed to initialize vblank\n");
341 goto fail;
342 }
343
344 pm_runtime_get_sync(dev->dev);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100345 ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
Rob Clarkc8afe682013-06-26 12:44:06 -0400346 pm_runtime_put_sync(dev->dev);
347 if (ret < 0) {
348 dev_err(dev->dev, "failed to install IRQ handler\n");
349 goto fail;
350 }
351
Rob Clarkcf3a7e42014-11-08 13:21:06 -0500352 drm_mode_config_reset(dev);
353
Rob Clarkc8afe682013-06-26 12:44:06 -0400354#ifdef CONFIG_DRM_MSM_FBDEV
Rob Clarke90dfec2015-01-30 17:05:41 -0500355 if (fbdev)
356 priv->fbdev = msm_fbdev_init(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400357#endif
358
Rob Clarka7d3c952014-05-30 14:47:38 -0400359 ret = msm_debugfs_late_init(dev);
360 if (ret)
361 goto fail;
362
Rob Clarkc8afe682013-06-26 12:44:06 -0400363 drm_kms_helper_poll_init(dev);
364
365 return 0;
366
367fail:
368 msm_unload(dev);
369 return ret;
370}
371
Rob Clark7198e6b2013-07-19 12:59:32 -0400372static void load_gpu(struct drm_device *dev)
373{
Rob Clarka1ad3522014-07-11 11:59:22 -0400374 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400375 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400376
Rob Clarka1ad3522014-07-11 11:59:22 -0400377 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400378
Rob Clarke2550b72014-09-05 13:30:27 -0400379 if (!priv->gpu)
380 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400381
Rob Clarka1ad3522014-07-11 11:59:22 -0400382 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400383}
384
385static int msm_open(struct drm_device *dev, struct drm_file *file)
386{
387 struct msm_file_private *ctx;
388
389 /* For now, load gpu on open.. to avoid the requirement of having
390 * firmware in the initrd.
391 */
392 load_gpu(dev);
393
394 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
395 if (!ctx)
396 return -ENOMEM;
397
398 file->driver_priv = ctx;
399
400 return 0;
401}
402
Rob Clarkc8afe682013-06-26 12:44:06 -0400403static void msm_preclose(struct drm_device *dev, struct drm_file *file)
404{
405 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400406 struct msm_file_private *ctx = file->driver_priv;
Rob Clarkc8afe682013-06-26 12:44:06 -0400407 struct msm_kms *kms = priv->kms;
Rob Clark7198e6b2013-07-19 12:59:32 -0400408
Rob Clarkc8afe682013-06-26 12:44:06 -0400409 if (kms)
410 kms->funcs->preclose(kms, file);
Rob Clark7198e6b2013-07-19 12:59:32 -0400411
412 mutex_lock(&dev->struct_mutex);
413 if (ctx == priv->lastctx)
414 priv->lastctx = NULL;
415 mutex_unlock(&dev->struct_mutex);
416
417 kfree(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400418}
419
420static void msm_lastclose(struct drm_device *dev)
421{
422 struct msm_drm_private *priv = dev->dev_private;
Rob Clark5ea1f752014-05-30 12:29:48 -0400423 if (priv->fbdev)
424 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400425}
426
Daniel Vettere9f0d762013-12-11 11:34:42 +0100427static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400428{
429 struct drm_device *dev = arg;
430 struct msm_drm_private *priv = dev->dev_private;
431 struct msm_kms *kms = priv->kms;
432 BUG_ON(!kms);
433 return kms->funcs->irq(kms);
434}
435
436static void msm_irq_preinstall(struct drm_device *dev)
437{
438 struct msm_drm_private *priv = dev->dev_private;
439 struct msm_kms *kms = priv->kms;
440 BUG_ON(!kms);
441 kms->funcs->irq_preinstall(kms);
442}
443
444static int msm_irq_postinstall(struct drm_device *dev)
445{
446 struct msm_drm_private *priv = dev->dev_private;
447 struct msm_kms *kms = priv->kms;
448 BUG_ON(!kms);
449 return kms->funcs->irq_postinstall(kms);
450}
451
452static void msm_irq_uninstall(struct drm_device *dev)
453{
454 struct msm_drm_private *priv = dev->dev_private;
455 struct msm_kms *kms = priv->kms;
456 BUG_ON(!kms);
457 kms->funcs->irq_uninstall(kms);
458}
459
460static int msm_enable_vblank(struct drm_device *dev, int crtc_id)
461{
462 struct msm_drm_private *priv = dev->dev_private;
463 struct msm_kms *kms = priv->kms;
464 if (!kms)
465 return -ENXIO;
466 DBG("dev=%p, crtc=%d", dev, crtc_id);
467 return kms->funcs->enable_vblank(kms, priv->crtcs[crtc_id]);
468}
469
470static void msm_disable_vblank(struct drm_device *dev, int crtc_id)
471{
472 struct msm_drm_private *priv = dev->dev_private;
473 struct msm_kms *kms = priv->kms;
474 if (!kms)
475 return;
476 DBG("dev=%p, crtc=%d", dev, crtc_id);
477 kms->funcs->disable_vblank(kms, priv->crtcs[crtc_id]);
478}
479
480/*
481 * DRM debugfs:
482 */
483
484#ifdef CONFIG_DEBUG_FS
Rob Clark7198e6b2013-07-19 12:59:32 -0400485static int msm_gpu_show(struct drm_device *dev, struct seq_file *m)
486{
487 struct msm_drm_private *priv = dev->dev_private;
488 struct msm_gpu *gpu = priv->gpu;
489
490 if (gpu) {
491 seq_printf(m, "%s Status:\n", gpu->name);
492 gpu->funcs->show(gpu, m);
493 }
494
495 return 0;
496}
497
Rob Clarkc8afe682013-06-26 12:44:06 -0400498static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
499{
500 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400501 struct msm_gpu *gpu = priv->gpu;
Rob Clarkc8afe682013-06-26 12:44:06 -0400502
Rob Clark7198e6b2013-07-19 12:59:32 -0400503 if (gpu) {
504 seq_printf(m, "Active Objects (%s):\n", gpu->name);
505 msm_gem_describe_objects(&gpu->active_list, m);
506 }
507
508 seq_printf(m, "Inactive Objects:\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400509 msm_gem_describe_objects(&priv->inactive_list, m);
510
511 return 0;
512}
513
514static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
515{
Daniel Vetterb04a5902013-12-11 14:24:46 +0100516 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
Rob Clarkc8afe682013-06-26 12:44:06 -0400517}
518
519static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
520{
521 struct msm_drm_private *priv = dev->dev_private;
522 struct drm_framebuffer *fb, *fbdev_fb = NULL;
523
524 if (priv->fbdev) {
525 seq_printf(m, "fbcon ");
526 fbdev_fb = priv->fbdev->fb;
527 msm_framebuffer_describe(fbdev_fb, m);
528 }
529
530 mutex_lock(&dev->mode_config.fb_lock);
531 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
532 if (fb == fbdev_fb)
533 continue;
534
535 seq_printf(m, "user ");
536 msm_framebuffer_describe(fb, m);
537 }
538 mutex_unlock(&dev->mode_config.fb_lock);
539
540 return 0;
541}
542
543static int show_locked(struct seq_file *m, void *arg)
544{
545 struct drm_info_node *node = (struct drm_info_node *) m->private;
546 struct drm_device *dev = node->minor->dev;
547 int (*show)(struct drm_device *dev, struct seq_file *m) =
548 node->info_ent->data;
549 int ret;
550
551 ret = mutex_lock_interruptible(&dev->struct_mutex);
552 if (ret)
553 return ret;
554
555 ret = show(dev, m);
556
557 mutex_unlock(&dev->struct_mutex);
558
559 return ret;
560}
561
562static struct drm_info_list msm_debugfs_list[] = {
Rob Clark7198e6b2013-07-19 12:59:32 -0400563 {"gpu", show_locked, 0, msm_gpu_show},
Rob Clarkc8afe682013-06-26 12:44:06 -0400564 {"gem", show_locked, 0, msm_gem_show},
565 { "mm", show_locked, 0, msm_mm_show },
566 { "fb", show_locked, 0, msm_fb_show },
567};
568
Rob Clarka7d3c952014-05-30 14:47:38 -0400569static int late_init_minor(struct drm_minor *minor)
570{
571 int ret;
572
573 if (!minor)
574 return 0;
575
576 ret = msm_rd_debugfs_init(minor);
577 if (ret) {
578 dev_err(minor->dev->dev, "could not install rd debugfs\n");
579 return ret;
580 }
581
Rob Clark70c70f02014-05-30 14:49:43 -0400582 ret = msm_perf_debugfs_init(minor);
583 if (ret) {
584 dev_err(minor->dev->dev, "could not install perf debugfs\n");
585 return ret;
586 }
587
Rob Clarka7d3c952014-05-30 14:47:38 -0400588 return 0;
589}
590
591int msm_debugfs_late_init(struct drm_device *dev)
592{
593 int ret;
594 ret = late_init_minor(dev->primary);
595 if (ret)
596 return ret;
597 ret = late_init_minor(dev->render);
598 if (ret)
599 return ret;
600 ret = late_init_minor(dev->control);
601 return ret;
602}
603
Rob Clarkc8afe682013-06-26 12:44:06 -0400604static int msm_debugfs_init(struct drm_minor *minor)
605{
606 struct drm_device *dev = minor->dev;
607 int ret;
608
609 ret = drm_debugfs_create_files(msm_debugfs_list,
610 ARRAY_SIZE(msm_debugfs_list),
611 minor->debugfs_root, minor);
612
613 if (ret) {
614 dev_err(dev->dev, "could not install msm_debugfs_list\n");
615 return ret;
616 }
617
Rob Clarka7d3c952014-05-30 14:47:38 -0400618 return 0;
Rob Clarkc8afe682013-06-26 12:44:06 -0400619}
620
621static void msm_debugfs_cleanup(struct drm_minor *minor)
622{
623 drm_debugfs_remove_files(msm_debugfs_list,
624 ARRAY_SIZE(msm_debugfs_list), minor);
Rob Clarka7d3c952014-05-30 14:47:38 -0400625 if (!minor->dev->dev_private)
626 return;
627 msm_rd_debugfs_cleanup(minor);
Rob Clark70c70f02014-05-30 14:49:43 -0400628 msm_perf_debugfs_cleanup(minor);
Rob Clarkc8afe682013-06-26 12:44:06 -0400629}
630#endif
631
Rob Clark7198e6b2013-07-19 12:59:32 -0400632/*
633 * Fences:
634 */
635
636int msm_wait_fence_interruptable(struct drm_device *dev, uint32_t fence,
637 struct timespec *timeout)
638{
639 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400640 int ret;
641
Rob Clarkf816f272013-09-11 17:34:07 -0400642 if (!priv->gpu)
643 return 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400644
Rob Clarkf816f272013-09-11 17:34:07 -0400645 if (fence > priv->gpu->submitted_fence) {
646 DRM_ERROR("waiting on invalid fence: %u (of %u)\n",
647 fence, priv->gpu->submitted_fence);
648 return -EINVAL;
649 }
650
651 if (!timeout) {
652 /* no-wait: */
653 ret = fence_completed(dev, fence) ? 0 : -EBUSY;
654 } else {
655 unsigned long timeout_jiffies = timespec_to_jiffies(timeout);
656 unsigned long start_jiffies = jiffies;
657 unsigned long remaining_jiffies;
658
659 if (time_after(start_jiffies, timeout_jiffies))
660 remaining_jiffies = 0;
661 else
662 remaining_jiffies = timeout_jiffies - start_jiffies;
663
664 ret = wait_event_interruptible_timeout(priv->fence_event,
665 fence_completed(dev, fence),
666 remaining_jiffies);
667
668 if (ret == 0) {
669 DBG("timeout waiting for fence: %u (completed: %u)",
670 fence, priv->completed_fence);
671 ret = -ETIMEDOUT;
672 } else if (ret != -ERESTARTSYS) {
673 ret = 0;
674 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400675 }
676
677 return ret;
678}
679
Rob Clark69193e52014-11-07 18:10:04 -0500680int msm_queue_fence_cb(struct drm_device *dev,
681 struct msm_fence_cb *cb, uint32_t fence)
682{
683 struct msm_drm_private *priv = dev->dev_private;
684 int ret = 0;
685
686 mutex_lock(&dev->struct_mutex);
687 if (!list_empty(&cb->work.entry)) {
688 ret = -EINVAL;
689 } else if (fence > priv->completed_fence) {
690 cb->fence = fence;
691 list_add_tail(&cb->work.entry, &priv->fence_cbs);
692 } else {
693 queue_work(priv->wq, &cb->work);
694 }
695 mutex_unlock(&dev->struct_mutex);
696
697 return ret;
698}
699
Rob Clarkedd4fc62013-09-14 14:01:55 -0400700/* called from workqueue */
Rob Clark7198e6b2013-07-19 12:59:32 -0400701void msm_update_fence(struct drm_device *dev, uint32_t fence)
702{
703 struct msm_drm_private *priv = dev->dev_private;
704
Rob Clarkedd4fc62013-09-14 14:01:55 -0400705 mutex_lock(&dev->struct_mutex);
706 priv->completed_fence = max(fence, priv->completed_fence);
707
708 while (!list_empty(&priv->fence_cbs)) {
709 struct msm_fence_cb *cb;
710
711 cb = list_first_entry(&priv->fence_cbs,
712 struct msm_fence_cb, work.entry);
713
714 if (cb->fence > priv->completed_fence)
715 break;
716
717 list_del_init(&cb->work.entry);
718 queue_work(priv->wq, &cb->work);
Rob Clark7198e6b2013-07-19 12:59:32 -0400719 }
Rob Clarkedd4fc62013-09-14 14:01:55 -0400720
721 mutex_unlock(&dev->struct_mutex);
722
723 wake_up_all(&priv->fence_event);
724}
725
726void __msm_fence_worker(struct work_struct *work)
727{
728 struct msm_fence_cb *cb = container_of(work, struct msm_fence_cb, work);
729 cb->func(cb);
Rob Clark7198e6b2013-07-19 12:59:32 -0400730}
731
732/*
733 * DRM ioctls:
734 */
735
736static int msm_ioctl_get_param(struct drm_device *dev, void *data,
737 struct drm_file *file)
738{
739 struct msm_drm_private *priv = dev->dev_private;
740 struct drm_msm_param *args = data;
741 struct msm_gpu *gpu;
742
743 /* for now, we just have 3d pipe.. eventually this would need to
744 * be more clever to dispatch to appropriate gpu module:
745 */
746 if (args->pipe != MSM_PIPE_3D0)
747 return -EINVAL;
748
749 gpu = priv->gpu;
750
751 if (!gpu)
752 return -ENXIO;
753
754 return gpu->funcs->get_param(gpu, args->param, &args->value);
755}
756
757static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
758 struct drm_file *file)
759{
760 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500761
762 if (args->flags & ~MSM_BO_FLAGS) {
763 DRM_ERROR("invalid flags: %08x\n", args->flags);
764 return -EINVAL;
765 }
766
Rob Clark7198e6b2013-07-19 12:59:32 -0400767 return msm_gem_new_handle(dev, file, args->size,
768 args->flags, &args->handle);
769}
770
771#define TS(t) ((struct timespec){ .tv_sec = (t).tv_sec, .tv_nsec = (t).tv_nsec })
772
773static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
774 struct drm_file *file)
775{
776 struct drm_msm_gem_cpu_prep *args = data;
777 struct drm_gem_object *obj;
778 int ret;
779
Rob Clark93ddb0d2014-03-03 09:42:33 -0500780 if (args->op & ~MSM_PREP_FLAGS) {
781 DRM_ERROR("invalid op: %08x\n", args->op);
782 return -EINVAL;
783 }
784
Rob Clark7198e6b2013-07-19 12:59:32 -0400785 obj = drm_gem_object_lookup(dev, file, args->handle);
786 if (!obj)
787 return -ENOENT;
788
789 ret = msm_gem_cpu_prep(obj, args->op, &TS(args->timeout));
790
791 drm_gem_object_unreference_unlocked(obj);
792
793 return ret;
794}
795
796static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
797 struct drm_file *file)
798{
799 struct drm_msm_gem_cpu_fini *args = data;
800 struct drm_gem_object *obj;
801 int ret;
802
803 obj = drm_gem_object_lookup(dev, file, args->handle);
804 if (!obj)
805 return -ENOENT;
806
807 ret = msm_gem_cpu_fini(obj);
808
809 drm_gem_object_unreference_unlocked(obj);
810
811 return ret;
812}
813
814static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
815 struct drm_file *file)
816{
817 struct drm_msm_gem_info *args = data;
818 struct drm_gem_object *obj;
819 int ret = 0;
820
821 if (args->pad)
822 return -EINVAL;
823
824 obj = drm_gem_object_lookup(dev, file, args->handle);
825 if (!obj)
826 return -ENOENT;
827
828 args->offset = msm_gem_mmap_offset(obj);
829
830 drm_gem_object_unreference_unlocked(obj);
831
832 return ret;
833}
834
835static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
836 struct drm_file *file)
837{
838 struct drm_msm_wait_fence *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500839
840 if (args->pad) {
841 DRM_ERROR("invalid pad: %08x\n", args->pad);
842 return -EINVAL;
843 }
844
845 return msm_wait_fence_interruptable(dev, args->fence,
846 &TS(args->timeout));
Rob Clark7198e6b2013-07-19 12:59:32 -0400847}
848
849static const struct drm_ioctl_desc msm_ioctls[] = {
Rob Clarkb4b15c82013-09-28 12:01:25 -0400850 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
851 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
852 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
853 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
854 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
855 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
856 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -0400857};
858
Rob Clarkc8afe682013-06-26 12:44:06 -0400859static const struct vm_operations_struct vm_ops = {
860 .fault = msm_gem_fault,
861 .open = drm_gem_vm_open,
862 .close = drm_gem_vm_close,
863};
864
865static const struct file_operations fops = {
866 .owner = THIS_MODULE,
867 .open = drm_open,
868 .release = drm_release,
869 .unlocked_ioctl = drm_ioctl,
870#ifdef CONFIG_COMPAT
871 .compat_ioctl = drm_compat_ioctl,
872#endif
873 .poll = drm_poll,
874 .read = drm_read,
875 .llseek = no_llseek,
876 .mmap = msm_gem_mmap,
877};
878
879static struct drm_driver msm_driver = {
Rob Clark05b84912013-09-28 11:28:35 -0400880 .driver_features = DRIVER_HAVE_IRQ |
881 DRIVER_GEM |
882 DRIVER_PRIME |
Rob Clarkb4b15c82013-09-28 12:01:25 -0400883 DRIVER_RENDER |
Rob Clark05b84912013-09-28 11:28:35 -0400884 DRIVER_MODESET,
Rob Clarkc8afe682013-06-26 12:44:06 -0400885 .load = msm_load,
886 .unload = msm_unload,
Rob Clark7198e6b2013-07-19 12:59:32 -0400887 .open = msm_open,
Rob Clarkc8afe682013-06-26 12:44:06 -0400888 .preclose = msm_preclose,
889 .lastclose = msm_lastclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200890 .set_busid = drm_platform_set_busid,
Rob Clarkc8afe682013-06-26 12:44:06 -0400891 .irq_handler = msm_irq,
892 .irq_preinstall = msm_irq_preinstall,
893 .irq_postinstall = msm_irq_postinstall,
894 .irq_uninstall = msm_irq_uninstall,
895 .get_vblank_counter = drm_vblank_count,
896 .enable_vblank = msm_enable_vblank,
897 .disable_vblank = msm_disable_vblank,
898 .gem_free_object = msm_gem_free_object,
899 .gem_vm_ops = &vm_ops,
900 .dumb_create = msm_gem_dumb_create,
901 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark30600a92013-09-28 10:13:04 -0400902 .dumb_destroy = drm_gem_dumb_destroy,
Rob Clark05b84912013-09-28 11:28:35 -0400903 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
904 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
905 .gem_prime_export = drm_gem_prime_export,
906 .gem_prime_import = drm_gem_prime_import,
907 .gem_prime_pin = msm_gem_prime_pin,
908 .gem_prime_unpin = msm_gem_prime_unpin,
909 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
910 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
911 .gem_prime_vmap = msm_gem_prime_vmap,
912 .gem_prime_vunmap = msm_gem_prime_vunmap,
Daniel Thompson77a147e2014-11-12 11:38:14 +0000913 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -0400914#ifdef CONFIG_DEBUG_FS
915 .debugfs_init = msm_debugfs_init,
916 .debugfs_cleanup = msm_debugfs_cleanup,
917#endif
Rob Clark7198e6b2013-07-19 12:59:32 -0400918 .ioctls = msm_ioctls,
919 .num_ioctls = DRM_MSM_NUM_IOCTLS,
Rob Clarkc8afe682013-06-26 12:44:06 -0400920 .fops = &fops,
921 .name = "msm",
922 .desc = "MSM Snapdragon DRM",
923 .date = "20130625",
924 .major = 1,
925 .minor = 0,
926};
927
928#ifdef CONFIG_PM_SLEEP
929static int msm_pm_suspend(struct device *dev)
930{
931 struct drm_device *ddev = dev_get_drvdata(dev);
932
933 drm_kms_helper_poll_disable(ddev);
934
935 return 0;
936}
937
938static int msm_pm_resume(struct device *dev)
939{
940 struct drm_device *ddev = dev_get_drvdata(dev);
941
942 drm_kms_helper_poll_enable(ddev);
943
944 return 0;
945}
946#endif
947
948static const struct dev_pm_ops msm_pm_ops = {
949 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
950};
951
952/*
Rob Clark060530f2014-03-03 14:19:12 -0500953 * Componentized driver support:
954 */
955
956#ifdef CONFIG_OF
957/* NOTE: the CONFIG_OF case duplicates the same code as exynos or imx
958 * (or probably any other).. so probably some room for some helpers
959 */
960static int compare_of(struct device *dev, void *data)
961{
962 return dev->of_node == data;
963}
Rob Clark41e69772013-12-15 16:23:05 -0500964
965static int add_components(struct device *dev, struct component_match **matchptr,
966 const char *name)
967{
968 struct device_node *np = dev->of_node;
969 unsigned i;
970
971 for (i = 0; ; i++) {
972 struct device_node *node;
973
974 node = of_parse_phandle(np, name, i);
975 if (!node)
976 break;
977
978 component_match_add(dev, matchptr, compare_of, node);
979 }
980
981 return 0;
982}
Russell King84448282014-04-19 11:20:42 +0100983#else
984static int compare_dev(struct device *dev, void *data)
Rob Clark060530f2014-03-03 14:19:12 -0500985{
Russell King84448282014-04-19 11:20:42 +0100986 return dev == data;
987}
988#endif
989
990static int msm_drm_bind(struct device *dev)
991{
992 return drm_platform_init(&msm_driver, to_platform_device(dev));
993}
994
995static void msm_drm_unbind(struct device *dev)
996{
997 drm_put_dev(platform_get_drvdata(to_platform_device(dev)));
998}
999
1000static const struct component_master_ops msm_drm_ops = {
1001 .bind = msm_drm_bind,
1002 .unbind = msm_drm_unbind,
1003};
1004
1005/*
1006 * Platform driver:
1007 */
1008
1009static int msm_pdev_probe(struct platform_device *pdev)
1010{
1011 struct component_match *match = NULL;
1012#ifdef CONFIG_OF
Rob Clark41e69772013-12-15 16:23:05 -05001013 add_components(&pdev->dev, &match, "connectors");
1014 add_components(&pdev->dev, &match, "gpus");
Rob Clark060530f2014-03-03 14:19:12 -05001015#else
Rob Clark060530f2014-03-03 14:19:12 -05001016 /* For non-DT case, it kinda sucks. We don't actually have a way
1017 * to know whether or not we are waiting for certain devices (or if
1018 * they are simply not present). But for non-DT we only need to
1019 * care about apq8064/apq8060/etc (all mdp4/a3xx):
1020 */
1021 static const char *devnames[] = {
1022 "hdmi_msm.0", "kgsl-3d0.0",
1023 };
1024 int i;
1025
1026 DBG("Adding components..");
1027
1028 for (i = 0; i < ARRAY_SIZE(devnames); i++) {
1029 struct device *dev;
Rob Clark060530f2014-03-03 14:19:12 -05001030
1031 dev = bus_find_device_by_name(&platform_bus_type,
1032 NULL, devnames[i]);
1033 if (!dev) {
Rob Clark12313c22014-08-04 15:45:16 -04001034 dev_info(&pdev->dev, "still waiting for %s\n", devnames[i]);
Rob Clark060530f2014-03-03 14:19:12 -05001035 return -EPROBE_DEFER;
1036 }
1037
Russell King84448282014-04-19 11:20:42 +01001038 component_match_add(&pdev->dev, &match, compare_dev, dev);
Rob Clark060530f2014-03-03 14:19:12 -05001039 }
Rob Clark060530f2014-03-03 14:19:12 -05001040#endif
1041
Rob Clark871d8122013-11-16 12:56:06 -05001042 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Russell King84448282014-04-19 11:20:42 +01001043 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
Rob Clarkc8afe682013-06-26 12:44:06 -04001044}
1045
1046static int msm_pdev_remove(struct platform_device *pdev)
1047{
Rob Clark060530f2014-03-03 14:19:12 -05001048 component_master_del(&pdev->dev, &msm_drm_ops);
Rob Clarkc8afe682013-06-26 12:44:06 -04001049
1050 return 0;
1051}
1052
1053static const struct platform_device_id msm_id[] = {
1054 { "mdp", 0 },
1055 { }
1056};
1057
Rob Clark06c0dd92013-11-30 17:51:47 -05001058static const struct of_device_id dt_match[] = {
Rob Clark41e69772013-12-15 16:23:05 -05001059 { .compatible = "qcom,mdp" }, /* mdp4 */
1060 { .compatible = "qcom,mdss_mdp" }, /* mdp5 */
Rob Clark06c0dd92013-11-30 17:51:47 -05001061 {}
1062};
1063MODULE_DEVICE_TABLE(of, dt_match);
1064
Rob Clarkc8afe682013-06-26 12:44:06 -04001065static struct platform_driver msm_platform_driver = {
1066 .probe = msm_pdev_probe,
1067 .remove = msm_pdev_remove,
1068 .driver = {
Rob Clarkc8afe682013-06-26 12:44:06 -04001069 .name = "msm",
Rob Clark06c0dd92013-11-30 17:51:47 -05001070 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -04001071 .pm = &msm_pm_ops,
1072 },
1073 .id_table = msm_id,
1074};
1075
1076static int __init msm_drm_register(void)
1077{
1078 DBG("init");
Hai Li00453982014-12-12 14:41:17 -05001079 msm_edp_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001080 hdmi_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001081 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001082 return platform_driver_register(&msm_platform_driver);
1083}
1084
1085static void __exit msm_drm_unregister(void)
1086{
1087 DBG("fini");
1088 platform_driver_unregister(&msm_platform_driver);
1089 hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001090 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -05001091 msm_edp_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -04001092}
1093
1094module_init(msm_drm_register);
1095module_exit(msm_drm_unregister);
1096
1097MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1098MODULE_DESCRIPTION("MSM DRM Driver");
1099MODULE_LICENSE("GPL");