blob: d650aaca60f80ef93d452f624c6437970b824da3 [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 Clarkedcd60c2016-03-16 12:56:12 -040019#include "msm_debugfs.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040020#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040021#include "msm_gpu.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050022#include "msm_kms.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040023
Rob Clarkc8afe682013-06-26 12:44:06 -040024static void msm_fb_output_poll_changed(struct drm_device *dev)
25{
26 struct msm_drm_private *priv = dev->dev_private;
27 if (priv->fbdev)
28 drm_fb_helper_hotplug_event(priv->fbdev);
29}
30
31static const struct drm_mode_config_funcs mode_config_funcs = {
32 .fb_create = msm_framebuffer_create,
33 .output_poll_changed = msm_fb_output_poll_changed,
Daniel Vetterb4274fb2014-11-26 17:02:18 +010034 .atomic_check = msm_atomic_check,
Rob Clarkcf3a7e42014-11-08 13:21:06 -050035 .atomic_commit = msm_atomic_commit,
Rob Clarkc8afe682013-06-26 12:44:06 -040036};
37
Rob Clark871d8122013-11-16 12:56:06 -050038int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
Rob Clarkc8afe682013-06-26 12:44:06 -040039{
40 struct msm_drm_private *priv = dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -050041 int idx = priv->num_mmus++;
Rob Clarkc8afe682013-06-26 12:44:06 -040042
Rob Clark871d8122013-11-16 12:56:06 -050043 if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
Rob Clarkc8afe682013-06-26 12:44:06 -040044 return -EINVAL;
45
Rob Clark871d8122013-11-16 12:56:06 -050046 priv->mmus[idx] = mmu;
Rob Clarkc8afe682013-06-26 12:44:06 -040047
48 return idx;
49}
50
Rob Clarkc8afe682013-06-26 12:44:06 -040051#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
52static bool reglog = false;
53MODULE_PARM_DESC(reglog, "Enable register read/write logging");
54module_param(reglog, bool, 0600);
55#else
56#define reglog 0
57#endif
58
Archit Tanejaa9ee34b2015-07-13 12:12:07 +053059#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarke90dfec2015-01-30 17:05:41 -050060static bool fbdev = true;
61MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
62module_param(fbdev, bool, 0600);
63#endif
64
Rob Clark3a10ba82014-09-08 14:24:57 -040065static char *vram = "16m";
Rob Clark4313c7442016-02-03 14:02:04 -050066MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
Rob Clark871d8122013-11-16 12:56:06 -050067module_param(vram, charp, 0);
68
Rob Clark060530f2014-03-03 14:19:12 -050069/*
70 * Util/helpers:
71 */
72
Rob Clarkc8afe682013-06-26 12:44:06 -040073void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
74 const char *dbgname)
75{
76 struct resource *res;
77 unsigned long size;
78 void __iomem *ptr;
79
80 if (name)
81 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
82 else
83 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84
85 if (!res) {
86 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
87 return ERR_PTR(-EINVAL);
88 }
89
90 size = resource_size(res);
91
92 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
93 if (!ptr) {
94 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
95 return ERR_PTR(-ENOMEM);
96 }
97
98 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +020099 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400100
101 return ptr;
102}
103
104void msm_writel(u32 data, void __iomem *addr)
105{
106 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200107 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
Rob Clarkc8afe682013-06-26 12:44:06 -0400108 writel(data, addr);
109}
110
111u32 msm_readl(const void __iomem *addr)
112{
113 u32 val = readl(addr);
114 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200115 printk(KERN_ERR "IO:R %p %08x\n", addr, val);
Rob Clarkc8afe682013-06-26 12:44:06 -0400116 return val;
117}
118
Hai Li78b1d472015-07-27 13:49:45 -0400119struct vblank_event {
120 struct list_head node;
121 int crtc_id;
122 bool enable;
123};
124
125static void vblank_ctrl_worker(struct work_struct *work)
126{
127 struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
128 struct msm_vblank_ctrl, work);
129 struct msm_drm_private *priv = container_of(vbl_ctrl,
130 struct msm_drm_private, vblank_ctrl);
131 struct msm_kms *kms = priv->kms;
132 struct vblank_event *vbl_ev, *tmp;
133 unsigned long flags;
134
135 spin_lock_irqsave(&vbl_ctrl->lock, flags);
136 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
137 list_del(&vbl_ev->node);
138 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
139
140 if (vbl_ev->enable)
141 kms->funcs->enable_vblank(kms,
142 priv->crtcs[vbl_ev->crtc_id]);
143 else
144 kms->funcs->disable_vblank(kms,
145 priv->crtcs[vbl_ev->crtc_id]);
146
147 kfree(vbl_ev);
148
149 spin_lock_irqsave(&vbl_ctrl->lock, flags);
150 }
151
152 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
153}
154
155static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
156 int crtc_id, bool enable)
157{
158 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
159 struct vblank_event *vbl_ev;
160 unsigned long flags;
161
162 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
163 if (!vbl_ev)
164 return -ENOMEM;
165
166 vbl_ev->crtc_id = crtc_id;
167 vbl_ev->enable = enable;
168
169 spin_lock_irqsave(&vbl_ctrl->lock, flags);
170 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
171 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
172
173 queue_work(priv->wq, &vbl_ctrl->work);
174
175 return 0;
176}
177
Archit Taneja2b669872016-05-02 11:05:54 +0530178static int msm_drm_uninit(struct device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400179{
Archit Taneja2b669872016-05-02 11:05:54 +0530180 struct platform_device *pdev = to_platform_device(dev);
181 struct drm_device *ddev = platform_get_drvdata(pdev);
182 struct msm_drm_private *priv = ddev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400183 struct msm_kms *kms = priv->kms;
Rob Clark7198e6b2013-07-19 12:59:32 -0400184 struct msm_gpu *gpu = priv->gpu;
Hai Li78b1d472015-07-27 13:49:45 -0400185 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
186 struct vblank_event *vbl_ev, *tmp;
187
188 /* We must cancel and cleanup any pending vblank enable/disable
189 * work before drm_irq_uninstall() to avoid work re-enabling an
190 * irq after uninstall has disabled it.
191 */
192 cancel_work_sync(&vbl_ctrl->work);
193 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
194 list_del(&vbl_ev->node);
195 kfree(vbl_ev);
196 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400197
Archit Taneja2b669872016-05-02 11:05:54 +0530198 drm_kms_helper_poll_fini(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530199
Archit Taneja2b669872016-05-02 11:05:54 +0530200 drm_dev_unregister(ddev);
Archit Taneja8208ed92016-05-02 11:05:53 +0530201
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530202#ifdef CONFIG_DRM_FBDEV_EMULATION
203 if (fbdev && priv->fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530204 msm_fbdev_free(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530205#endif
Archit Taneja2b669872016-05-02 11:05:54 +0530206 drm_mode_config_cleanup(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400207
Archit Taneja2b669872016-05-02 11:05:54 +0530208 pm_runtime_get_sync(dev);
209 drm_irq_uninstall(ddev);
210 pm_runtime_put_sync(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400211
212 flush_workqueue(priv->wq);
213 destroy_workqueue(priv->wq);
214
Rob Clarkba00c3f2016-03-16 18:18:17 -0400215 flush_workqueue(priv->atomic_wq);
216 destroy_workqueue(priv->atomic_wq);
217
Rob Clarkc8afe682013-06-26 12:44:06 -0400218 if (kms) {
Archit Taneja2b669872016-05-02 11:05:54 +0530219 pm_runtime_disable(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400220 kms->funcs->destroy(kms);
221 }
222
Rob Clark7198e6b2013-07-19 12:59:32 -0400223 if (gpu) {
Archit Taneja2b669872016-05-02 11:05:54 +0530224 mutex_lock(&ddev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400225 gpu->funcs->pm_suspend(gpu);
Archit Taneja2b669872016-05-02 11:05:54 +0530226 mutex_unlock(&ddev->struct_mutex);
Rob Clark774449e2015-05-15 09:19:36 -0400227 gpu->funcs->destroy(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400228 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400229
Rob Clark871d8122013-11-16 12:56:06 -0500230 if (priv->vram.paddr) {
231 DEFINE_DMA_ATTRS(attrs);
232 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
233 drm_mm_takedown(&priv->vram.mm);
Archit Taneja2b669872016-05-02 11:05:54 +0530234 dma_free_attrs(dev, priv->vram.size, NULL,
235 priv->vram.paddr, &attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500236 }
237
Archit Taneja2b669872016-05-02 11:05:54 +0530238 component_unbind_all(dev, ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500239
Archit Taneja0a6030d2016-05-08 21:36:28 +0530240 msm_mdss_destroy(ddev);
241
Archit Taneja2b669872016-05-02 11:05:54 +0530242 ddev->dev_private = NULL;
243 drm_dev_unref(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400244
245 kfree(priv);
246
247 return 0;
248}
249
Rob Clark06c0dd92013-11-30 17:51:47 -0500250static int get_mdp_ver(struct platform_device *pdev)
251{
Rob Clark06c0dd92013-11-30 17:51:47 -0500252 struct device *dev = &pdev->dev;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530253
254 return (int) (unsigned long) of_device_get_match_data(dev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500255}
256
Rob Clark072f1f92015-03-03 15:04:25 -0500257#include <linux/of_address.h>
258
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500259static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400260{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500261 struct msm_drm_private *priv = dev->dev_private;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530262 struct device_node *node;
Rob Clark072f1f92015-03-03 15:04:25 -0500263 unsigned long size = 0;
264 int ret = 0;
265
Rob Clark072f1f92015-03-03 15:04:25 -0500266 /* In the device-tree world, we could have a 'memory-region'
267 * phandle, which gives us a link to our "vram". Allocating
268 * is all nicely abstracted behind the dma api, but we need
269 * to know the entire size to allocate it all in one go. There
270 * are two cases:
271 * 1) device with no IOMMU, in which case we need exclusive
272 * access to a VRAM carveout big enough for all gpu
273 * buffers
274 * 2) device with IOMMU, but where the bootloader puts up
275 * a splash screen. In this case, the VRAM carveout
276 * need only be large enough for fbdev fb. But we need
277 * exclusive access to the buffer to avoid the kernel
278 * using those pages for other purposes (which appears
279 * as corruption on screen before we have a chance to
280 * load and do initial modeset)
281 */
Rob Clark072f1f92015-03-03 15:04:25 -0500282
283 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
284 if (node) {
285 struct resource r;
286 ret = of_address_to_resource(node, 0, &r);
287 if (ret)
288 return ret;
289 size = r.end - r.start;
Thierry Redingfc99f972015-04-09 16:39:51 +0200290 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
Rob Clarkc8afe682013-06-26 12:44:06 -0400291
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530292 /* if we have no IOMMU, then we need to use carveout allocator.
293 * Grab the entire CMA chunk carved out in early startup in
294 * mach-msm:
295 */
296 } else if (!iommu_present(&platform_bus_type)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500297 DRM_INFO("using %s VRAM carveout\n", vram);
298 size = memparse(vram, NULL);
299 }
300
301 if (size) {
Rob Clark871d8122013-11-16 12:56:06 -0500302 DEFINE_DMA_ATTRS(attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500303 void *p;
304
Rob Clark871d8122013-11-16 12:56:06 -0500305 priv->vram.size = size;
306
307 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
308
309 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs);
310 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &attrs);
311
312 /* note that for no-kernel-mapping, the vaddr returned
313 * is bogus, but non-null if allocation succeeded:
314 */
315 p = dma_alloc_attrs(dev->dev, size,
Rob Clark543d3012014-06-02 07:25:56 -0400316 &priv->vram.paddr, GFP_KERNEL, &attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500317 if (!p) {
318 dev_err(dev->dev, "failed to allocate VRAM\n");
319 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500320 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500321 }
322
323 dev_info(dev->dev, "VRAM: %08x->%08x\n",
324 (uint32_t)priv->vram.paddr,
325 (uint32_t)(priv->vram.paddr + size));
326 }
327
Rob Clark072f1f92015-03-03 15:04:25 -0500328 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500329}
330
Archit Taneja2b669872016-05-02 11:05:54 +0530331static int msm_drm_init(struct device *dev, struct drm_driver *drv)
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500332{
Archit Taneja2b669872016-05-02 11:05:54 +0530333 struct platform_device *pdev = to_platform_device(dev);
334 struct drm_device *ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500335 struct msm_drm_private *priv;
336 struct msm_kms *kms;
337 int ret;
338
Archit Taneja2b669872016-05-02 11:05:54 +0530339 ddev = drm_dev_alloc(drv, dev);
340 if (!ddev) {
341 dev_err(dev, "failed to allocate drm_device\n");
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500342 return -ENOMEM;
343 }
344
Archit Taneja2b669872016-05-02 11:05:54 +0530345 platform_set_drvdata(pdev, ddev);
346 ddev->platformdev = pdev;
347
348 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
349 if (!priv) {
350 drm_dev_unref(ddev);
351 return -ENOMEM;
352 }
353
354 ddev->dev_private = priv;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500355
Archit Taneja0a6030d2016-05-08 21:36:28 +0530356 ret = msm_mdss_init(ddev);
357 if (ret) {
358 kfree(priv);
359 drm_dev_unref(ddev);
360 return ret;
361 }
362
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500363 priv->wq = alloc_ordered_workqueue("msm", 0);
Rob Clarkba00c3f2016-03-16 18:18:17 -0400364 priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500365 init_waitqueue_head(&priv->pending_crtcs_event);
366
367 INIT_LIST_HEAD(&priv->inactive_list);
Hai Li78b1d472015-07-27 13:49:45 -0400368 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
369 INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
370 spin_lock_init(&priv->vblank_ctrl.lock);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500371
Archit Taneja2b669872016-05-02 11:05:54 +0530372 drm_mode_config_init(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500373
374 /* Bind all our sub-components: */
Archit Taneja2b669872016-05-02 11:05:54 +0530375 ret = component_bind_all(dev, ddev);
376 if (ret) {
Archit Taneja0a6030d2016-05-08 21:36:28 +0530377 msm_mdss_destroy(ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530378 kfree(priv);
379 drm_dev_unref(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500380 return ret;
Archit Taneja2b669872016-05-02 11:05:54 +0530381 }
Rob Clark060530f2014-03-03 14:19:12 -0500382
Archit Taneja2b669872016-05-02 11:05:54 +0530383 ret = msm_init_vram(ddev);
Rob Clark13f15562015-05-07 15:20:13 -0400384 if (ret)
385 goto fail;
386
Rob Clark06c0dd92013-11-30 17:51:47 -0500387 switch (get_mdp_ver(pdev)) {
388 case 4:
Archit Taneja2b669872016-05-02 11:05:54 +0530389 kms = mdp4_kms_init(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530390 priv->kms = kms;
Rob Clark06c0dd92013-11-30 17:51:47 -0500391 break;
392 case 5:
Archit Taneja392ae6e2016-06-14 18:24:54 +0530393 kms = mdp5_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500394 break;
395 default:
396 kms = ERR_PTR(-ENODEV);
397 break;
398 }
399
Rob Clarkc8afe682013-06-26 12:44:06 -0400400 if (IS_ERR(kms)) {
401 /*
402 * NOTE: once we have GPU support, having no kms should not
403 * be considered fatal.. ideally we would still support gpu
404 * and (for example) use dmabuf/prime to share buffers with
405 * imx drm driver on iMX5
406 */
Archit Taneja2b669872016-05-02 11:05:54 +0530407 dev_err(dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200408 ret = PTR_ERR(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400409 goto fail;
410 }
411
Rob Clarkc8afe682013-06-26 12:44:06 -0400412 if (kms) {
Archit Taneja2b669872016-05-02 11:05:54 +0530413 pm_runtime_enable(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400414 ret = kms->funcs->hw_init(kms);
415 if (ret) {
Archit Taneja2b669872016-05-02 11:05:54 +0530416 dev_err(dev, "kms hw init failed: %d\n", ret);
Rob Clarkc8afe682013-06-26 12:44:06 -0400417 goto fail;
418 }
419 }
420
Archit Taneja2b669872016-05-02 11:05:54 +0530421 ddev->mode_config.funcs = &mode_config_funcs;
Rob Clarkc8afe682013-06-26 12:44:06 -0400422
Archit Taneja2b669872016-05-02 11:05:54 +0530423 ret = drm_vblank_init(ddev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400424 if (ret < 0) {
Archit Taneja2b669872016-05-02 11:05:54 +0530425 dev_err(dev, "failed to initialize vblank\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400426 goto fail;
427 }
428
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530429 if (kms) {
430 pm_runtime_get_sync(dev);
431 ret = drm_irq_install(ddev, kms->irq);
432 pm_runtime_put_sync(dev);
433 if (ret < 0) {
434 dev_err(dev, "failed to install IRQ handler\n");
435 goto fail;
436 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400437 }
438
Archit Taneja2b669872016-05-02 11:05:54 +0530439 ret = drm_dev_register(ddev, 0);
Rob Clarka7d3c952014-05-30 14:47:38 -0400440 if (ret)
441 goto fail;
442
Archit Taneja2b669872016-05-02 11:05:54 +0530443 drm_mode_config_reset(ddev);
444
445#ifdef CONFIG_DRM_FBDEV_EMULATION
446 if (fbdev)
447 priv->fbdev = msm_fbdev_init(ddev);
448#endif
449
450 ret = msm_debugfs_late_init(ddev);
451 if (ret)
452 goto fail;
453
454 drm_kms_helper_poll_init(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400455
456 return 0;
457
458fail:
Archit Taneja2b669872016-05-02 11:05:54 +0530459 msm_drm_uninit(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400460 return ret;
461}
462
Archit Taneja2b669872016-05-02 11:05:54 +0530463/*
464 * DRM operations:
465 */
466
Rob Clark7198e6b2013-07-19 12:59:32 -0400467static void load_gpu(struct drm_device *dev)
468{
Rob Clarka1ad3522014-07-11 11:59:22 -0400469 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400470 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400471
Rob Clarka1ad3522014-07-11 11:59:22 -0400472 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400473
Rob Clarke2550b72014-09-05 13:30:27 -0400474 if (!priv->gpu)
475 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400476
Rob Clarka1ad3522014-07-11 11:59:22 -0400477 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400478}
479
480static int msm_open(struct drm_device *dev, struct drm_file *file)
481{
482 struct msm_file_private *ctx;
483
484 /* For now, load gpu on open.. to avoid the requirement of having
485 * firmware in the initrd.
486 */
487 load_gpu(dev);
488
489 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
490 if (!ctx)
491 return -ENOMEM;
492
493 file->driver_priv = ctx;
494
495 return 0;
496}
497
Rob Clarkc8afe682013-06-26 12:44:06 -0400498static void msm_preclose(struct drm_device *dev, struct drm_file *file)
499{
500 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400501 struct msm_file_private *ctx = file->driver_priv;
Rob Clark7198e6b2013-07-19 12:59:32 -0400502
Rob Clark7198e6b2013-07-19 12:59:32 -0400503 mutex_lock(&dev->struct_mutex);
504 if (ctx == priv->lastctx)
505 priv->lastctx = NULL;
506 mutex_unlock(&dev->struct_mutex);
507
508 kfree(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400509}
510
511static void msm_lastclose(struct drm_device *dev)
512{
513 struct msm_drm_private *priv = dev->dev_private;
Rob Clark5ea1f752014-05-30 12:29:48 -0400514 if (priv->fbdev)
515 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400516}
517
Daniel Vettere9f0d762013-12-11 11:34:42 +0100518static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400519{
520 struct drm_device *dev = arg;
521 struct msm_drm_private *priv = dev->dev_private;
522 struct msm_kms *kms = priv->kms;
523 BUG_ON(!kms);
524 return kms->funcs->irq(kms);
525}
526
527static void msm_irq_preinstall(struct drm_device *dev)
528{
529 struct msm_drm_private *priv = dev->dev_private;
530 struct msm_kms *kms = priv->kms;
531 BUG_ON(!kms);
532 kms->funcs->irq_preinstall(kms);
533}
534
535static int msm_irq_postinstall(struct drm_device *dev)
536{
537 struct msm_drm_private *priv = dev->dev_private;
538 struct msm_kms *kms = priv->kms;
539 BUG_ON(!kms);
540 return kms->funcs->irq_postinstall(kms);
541}
542
543static void msm_irq_uninstall(struct drm_device *dev)
544{
545 struct msm_drm_private *priv = dev->dev_private;
546 struct msm_kms *kms = priv->kms;
547 BUG_ON(!kms);
548 kms->funcs->irq_uninstall(kms);
549}
550
Thierry Reding88e72712015-09-24 18:35:31 +0200551static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400552{
553 struct msm_drm_private *priv = dev->dev_private;
554 struct msm_kms *kms = priv->kms;
555 if (!kms)
556 return -ENXIO;
Thierry Reding88e72712015-09-24 18:35:31 +0200557 DBG("dev=%p, crtc=%u", dev, pipe);
558 return vblank_ctrl_queue_work(priv, pipe, true);
Rob Clarkc8afe682013-06-26 12:44:06 -0400559}
560
Thierry Reding88e72712015-09-24 18:35:31 +0200561static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400562{
563 struct msm_drm_private *priv = dev->dev_private;
564 struct msm_kms *kms = priv->kms;
565 if (!kms)
566 return;
Thierry Reding88e72712015-09-24 18:35:31 +0200567 DBG("dev=%p, crtc=%u", dev, pipe);
568 vblank_ctrl_queue_work(priv, pipe, false);
Rob Clarkc8afe682013-06-26 12:44:06 -0400569}
570
571/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400572 * DRM ioctls:
573 */
574
575static int msm_ioctl_get_param(struct drm_device *dev, void *data,
576 struct drm_file *file)
577{
578 struct msm_drm_private *priv = dev->dev_private;
579 struct drm_msm_param *args = data;
580 struct msm_gpu *gpu;
581
582 /* for now, we just have 3d pipe.. eventually this would need to
583 * be more clever to dispatch to appropriate gpu module:
584 */
585 if (args->pipe != MSM_PIPE_3D0)
586 return -EINVAL;
587
588 gpu = priv->gpu;
589
590 if (!gpu)
591 return -ENXIO;
592
593 return gpu->funcs->get_param(gpu, args->param, &args->value);
594}
595
596static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
597 struct drm_file *file)
598{
599 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500600
601 if (args->flags & ~MSM_BO_FLAGS) {
602 DRM_ERROR("invalid flags: %08x\n", args->flags);
603 return -EINVAL;
604 }
605
Rob Clark7198e6b2013-07-19 12:59:32 -0400606 return msm_gem_new_handle(dev, file, args->size,
607 args->flags, &args->handle);
608}
609
Rob Clark56c2da82015-05-11 11:50:03 -0400610static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
611{
612 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
613}
Rob Clark7198e6b2013-07-19 12:59:32 -0400614
615static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
616 struct drm_file *file)
617{
618 struct drm_msm_gem_cpu_prep *args = data;
619 struct drm_gem_object *obj;
Rob Clark56c2da82015-05-11 11:50:03 -0400620 ktime_t timeout = to_ktime(args->timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400621 int ret;
622
Rob Clark93ddb0d2014-03-03 09:42:33 -0500623 if (args->op & ~MSM_PREP_FLAGS) {
624 DRM_ERROR("invalid op: %08x\n", args->op);
625 return -EINVAL;
626 }
627
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100628 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400629 if (!obj)
630 return -ENOENT;
631
Rob Clark56c2da82015-05-11 11:50:03 -0400632 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400633
634 drm_gem_object_unreference_unlocked(obj);
635
636 return ret;
637}
638
639static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
640 struct drm_file *file)
641{
642 struct drm_msm_gem_cpu_fini *args = data;
643 struct drm_gem_object *obj;
644 int ret;
645
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100646 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400647 if (!obj)
648 return -ENOENT;
649
650 ret = msm_gem_cpu_fini(obj);
651
652 drm_gem_object_unreference_unlocked(obj);
653
654 return ret;
655}
656
657static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
658 struct drm_file *file)
659{
660 struct drm_msm_gem_info *args = data;
661 struct drm_gem_object *obj;
662 int ret = 0;
663
664 if (args->pad)
665 return -EINVAL;
666
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100667 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400668 if (!obj)
669 return -ENOENT;
670
671 args->offset = msm_gem_mmap_offset(obj);
672
673 drm_gem_object_unreference_unlocked(obj);
674
675 return ret;
676}
677
678static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
679 struct drm_file *file)
680{
Rob Clarkca762a82016-03-15 17:22:13 -0400681 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400682 struct drm_msm_wait_fence *args = data;
Rob Clark56c2da82015-05-11 11:50:03 -0400683 ktime_t timeout = to_ktime(args->timeout);
Rob Clark93ddb0d2014-03-03 09:42:33 -0500684
685 if (args->pad) {
686 DRM_ERROR("invalid pad: %08x\n", args->pad);
687 return -EINVAL;
688 }
689
Rob Clarkca762a82016-03-15 17:22:13 -0400690 if (!priv->gpu)
691 return 0;
692
693 return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
Rob Clark7198e6b2013-07-19 12:59:32 -0400694}
695
696static const struct drm_ioctl_desc msm_ioctls[] = {
Daniel Vetterf8c47142015-09-08 13:56:30 +0200697 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
698 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
699 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
700 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
701 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
702 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
703 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -0400704};
705
Rob Clarkc8afe682013-06-26 12:44:06 -0400706static const struct vm_operations_struct vm_ops = {
707 .fault = msm_gem_fault,
708 .open = drm_gem_vm_open,
709 .close = drm_gem_vm_close,
710};
711
712static const struct file_operations fops = {
713 .owner = THIS_MODULE,
714 .open = drm_open,
715 .release = drm_release,
716 .unlocked_ioctl = drm_ioctl,
717#ifdef CONFIG_COMPAT
718 .compat_ioctl = drm_compat_ioctl,
719#endif
720 .poll = drm_poll,
721 .read = drm_read,
722 .llseek = no_llseek,
723 .mmap = msm_gem_mmap,
724};
725
726static struct drm_driver msm_driver = {
Rob Clark05b84912013-09-28 11:28:35 -0400727 .driver_features = DRIVER_HAVE_IRQ |
728 DRIVER_GEM |
729 DRIVER_PRIME |
Rob Clarkb4b15c82013-09-28 12:01:25 -0400730 DRIVER_RENDER |
Rob Clarka5436e12015-06-04 10:12:22 -0400731 DRIVER_ATOMIC |
Rob Clark05b84912013-09-28 11:28:35 -0400732 DRIVER_MODESET,
Rob Clark7198e6b2013-07-19 12:59:32 -0400733 .open = msm_open,
Rob Clarkc8afe682013-06-26 12:44:06 -0400734 .preclose = msm_preclose,
735 .lastclose = msm_lastclose,
736 .irq_handler = msm_irq,
737 .irq_preinstall = msm_irq_preinstall,
738 .irq_postinstall = msm_irq_postinstall,
739 .irq_uninstall = msm_irq_uninstall,
Ville Syrjäläb44f8402015-09-30 16:46:48 +0300740 .get_vblank_counter = drm_vblank_no_hw_counter,
Rob Clarkc8afe682013-06-26 12:44:06 -0400741 .enable_vblank = msm_enable_vblank,
742 .disable_vblank = msm_disable_vblank,
743 .gem_free_object = msm_gem_free_object,
744 .gem_vm_ops = &vm_ops,
745 .dumb_create = msm_gem_dumb_create,
746 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark30600a92013-09-28 10:13:04 -0400747 .dumb_destroy = drm_gem_dumb_destroy,
Rob Clark05b84912013-09-28 11:28:35 -0400748 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
749 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
750 .gem_prime_export = drm_gem_prime_export,
751 .gem_prime_import = drm_gem_prime_import,
752 .gem_prime_pin = msm_gem_prime_pin,
753 .gem_prime_unpin = msm_gem_prime_unpin,
754 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
755 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
756 .gem_prime_vmap = msm_gem_prime_vmap,
757 .gem_prime_vunmap = msm_gem_prime_vunmap,
Daniel Thompson77a147e2014-11-12 11:38:14 +0000758 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -0400759#ifdef CONFIG_DEBUG_FS
760 .debugfs_init = msm_debugfs_init,
761 .debugfs_cleanup = msm_debugfs_cleanup,
762#endif
Rob Clark7198e6b2013-07-19 12:59:32 -0400763 .ioctls = msm_ioctls,
764 .num_ioctls = DRM_MSM_NUM_IOCTLS,
Rob Clarkc8afe682013-06-26 12:44:06 -0400765 .fops = &fops,
766 .name = "msm",
767 .desc = "MSM Snapdragon DRM",
768 .date = "20130625",
769 .major = 1,
770 .minor = 0,
771};
772
773#ifdef CONFIG_PM_SLEEP
774static int msm_pm_suspend(struct device *dev)
775{
776 struct drm_device *ddev = dev_get_drvdata(dev);
777
778 drm_kms_helper_poll_disable(ddev);
779
780 return 0;
781}
782
783static int msm_pm_resume(struct device *dev)
784{
785 struct drm_device *ddev = dev_get_drvdata(dev);
786
787 drm_kms_helper_poll_enable(ddev);
788
789 return 0;
790}
791#endif
792
793static const struct dev_pm_ops msm_pm_ops = {
794 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
795};
796
797/*
Rob Clark060530f2014-03-03 14:19:12 -0500798 * Componentized driver support:
799 */
800
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530801/*
802 * NOTE: duplication of the same code as exynos or imx (or probably any other).
803 * so probably some room for some helpers
Rob Clark060530f2014-03-03 14:19:12 -0500804 */
805static int compare_of(struct device *dev, void *data)
806{
807 return dev->of_node == data;
808}
Rob Clark41e69772013-12-15 16:23:05 -0500809
810static int add_components(struct device *dev, struct component_match **matchptr,
811 const char *name)
812{
813 struct device_node *np = dev->of_node;
814 unsigned i;
815
816 for (i = 0; ; i++) {
817 struct device_node *node;
818
819 node = of_parse_phandle(np, name, i);
820 if (!node)
821 break;
822
823 component_match_add(dev, matchptr, compare_of, node);
824 }
825
826 return 0;
827}
Russell King84448282014-04-19 11:20:42 +0100828
829static int msm_drm_bind(struct device *dev)
830{
Archit Taneja2b669872016-05-02 11:05:54 +0530831 return msm_drm_init(dev, &msm_driver);
Russell King84448282014-04-19 11:20:42 +0100832}
833
834static void msm_drm_unbind(struct device *dev)
835{
Archit Taneja2b669872016-05-02 11:05:54 +0530836 msm_drm_uninit(dev);
Russell King84448282014-04-19 11:20:42 +0100837}
838
839static const struct component_master_ops msm_drm_ops = {
840 .bind = msm_drm_bind,
841 .unbind = msm_drm_unbind,
842};
843
844/*
845 * Platform driver:
846 */
847
848static int msm_pdev_probe(struct platform_device *pdev)
849{
850 struct component_match *match = NULL;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530851
Rob Clark41e69772013-12-15 16:23:05 -0500852 add_components(&pdev->dev, &match, "connectors");
853 add_components(&pdev->dev, &match, "gpus");
Rob Clark060530f2014-03-03 14:19:12 -0500854
Rob Clark871d8122013-11-16 12:56:06 -0500855 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Russell King84448282014-04-19 11:20:42 +0100856 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
Rob Clarkc8afe682013-06-26 12:44:06 -0400857}
858
859static int msm_pdev_remove(struct platform_device *pdev)
860{
Rob Clark060530f2014-03-03 14:19:12 -0500861 component_master_del(&pdev->dev, &msm_drm_ops);
Rob Clarkc8afe682013-06-26 12:44:06 -0400862
863 return 0;
864}
865
Rob Clark06c0dd92013-11-30 17:51:47 -0500866static const struct of_device_id dt_match[] = {
Archit Tanejad4fc72e2015-11-18 12:28:39 +0530867 { .compatible = "qcom,mdp4", .data = (void *) 4 }, /* mdp4 */
868 { .compatible = "qcom,mdp5", .data = (void *) 5 }, /* mdp5 */
869 /* to support downstream DT files */
870 { .compatible = "qcom,mdss_mdp", .data = (void *) 5 }, /* mdp5 */
Rob Clark06c0dd92013-11-30 17:51:47 -0500871 {}
872};
873MODULE_DEVICE_TABLE(of, dt_match);
874
Rob Clarkc8afe682013-06-26 12:44:06 -0400875static struct platform_driver msm_platform_driver = {
876 .probe = msm_pdev_probe,
877 .remove = msm_pdev_remove,
878 .driver = {
Rob Clarkc8afe682013-06-26 12:44:06 -0400879 .name = "msm",
Rob Clark06c0dd92013-11-30 17:51:47 -0500880 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -0400881 .pm = &msm_pm_ops,
882 },
Rob Clarkc8afe682013-06-26 12:44:06 -0400883};
884
885static int __init msm_drm_register(void)
886{
887 DBG("init");
Archit Taneja1dd0a0b2016-05-30 16:36:50 +0530888 msm_mdp_register();
Hai Lid5af49c2015-03-26 19:25:17 -0400889 msm_dsi_register();
Hai Li00453982014-12-12 14:41:17 -0500890 msm_edp_register();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100891 msm_hdmi_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -0400892 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -0400893 return platform_driver_register(&msm_platform_driver);
894}
895
896static void __exit msm_drm_unregister(void)
897{
898 DBG("fini");
899 platform_driver_unregister(&msm_platform_driver);
Arnd Bergmannfcda50c2016-02-22 22:08:35 +0100900 msm_hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -0400901 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -0500902 msm_edp_unregister();
Hai Lid5af49c2015-03-26 19:25:17 -0400903 msm_dsi_unregister();
Archit Taneja1dd0a0b2016-05-30 16:36:50 +0530904 msm_mdp_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -0400905}
906
907module_init(msm_drm_register);
908module_exit(msm_drm_unregister);
909
910MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
911MODULE_DESCRIPTION("MSM DRM Driver");
912MODULE_LICENSE("GPL");