blob: beb4f6b3ac70e20f0bceadc692b2640c9b446aa6 [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
Russell King97ac0e42016-10-19 11:28:27 +010018#include <drm/drm_of.h>
19
Rob Clarkc8afe682013-06-26 12:44:06 -040020#include "msm_drv.h"
Rob Clarkedcd60c2016-03-16 12:56:12 -040021#include "msm_debugfs.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040022#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040023#include "msm_gpu.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050024#include "msm_kms.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040025
Rob Clarka8d854c2016-06-01 14:02:02 -040026
27/*
28 * MSM driver version:
29 * - 1.0.0 - initial interface
30 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
Rob Clark7a3bcc02016-09-16 18:37:44 -040031 * - 1.2.0 - adds explicit fence support for submit ioctl
Rob Clarka8d854c2016-06-01 14:02:02 -040032 */
33#define MSM_VERSION_MAJOR 1
Rob Clark7a3bcc02016-09-16 18:37:44 -040034#define MSM_VERSION_MINOR 2
Rob Clarka8d854c2016-06-01 14:02:02 -040035#define MSM_VERSION_PATCHLEVEL 0
36
Rob Clarkc8afe682013-06-26 12:44:06 -040037static void msm_fb_output_poll_changed(struct drm_device *dev)
38{
39 struct msm_drm_private *priv = dev->dev_private;
40 if (priv->fbdev)
41 drm_fb_helper_hotplug_event(priv->fbdev);
42}
43
44static const struct drm_mode_config_funcs mode_config_funcs = {
45 .fb_create = msm_framebuffer_create,
46 .output_poll_changed = msm_fb_output_poll_changed,
Daniel Vetterb4274fb2014-11-26 17:02:18 +010047 .atomic_check = msm_atomic_check,
Rob Clarkcf3a7e42014-11-08 13:21:06 -050048 .atomic_commit = msm_atomic_commit,
Rob Clark870d7382016-11-04 13:51:42 -040049 .atomic_state_alloc = msm_atomic_state_alloc,
50 .atomic_state_clear = msm_atomic_state_clear,
51 .atomic_state_free = msm_atomic_state_free,
Rob Clarkc8afe682013-06-26 12:44:06 -040052};
53
Rob Clark667ce332016-09-28 19:58:32 -040054int msm_register_address_space(struct drm_device *dev,
55 struct msm_gem_address_space *aspace)
Rob Clarkc8afe682013-06-26 12:44:06 -040056{
57 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -040058
Jordan Crouse36849cc2017-03-07 09:50:28 -070059 if (WARN_ON(priv->num_aspaces >= ARRAY_SIZE(priv->aspace)))
Rob Clarkc8afe682013-06-26 12:44:06 -040060 return -EINVAL;
61
Jordan Crouse36849cc2017-03-07 09:50:28 -070062 priv->aspace[priv->num_aspaces] = aspace;
Rob Clarkc8afe682013-06-26 12:44:06 -040063
Jordan Crouse36849cc2017-03-07 09:50:28 -070064 return priv->num_aspaces++;
Rob Clarkc8afe682013-06-26 12:44:06 -040065}
66
Rob Clarkc8afe682013-06-26 12:44:06 -040067#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
68static bool reglog = false;
69MODULE_PARM_DESC(reglog, "Enable register read/write logging");
70module_param(reglog, bool, 0600);
71#else
72#define reglog 0
73#endif
74
Archit Tanejaa9ee34b2015-07-13 12:12:07 +053075#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarke90dfec2015-01-30 17:05:41 -050076static bool fbdev = true;
77MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
78module_param(fbdev, bool, 0600);
79#endif
80
Rob Clark3a10ba82014-09-08 14:24:57 -040081static char *vram = "16m";
Rob Clark4313c7442016-02-03 14:02:04 -050082MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
Rob Clark871d8122013-11-16 12:56:06 -050083module_param(vram, charp, 0);
84
Rob Clark06d9f562016-11-05 11:08:12 -040085bool dumpstate = false;
86MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
87module_param(dumpstate, bool, 0600);
88
Rob Clark060530f2014-03-03 14:19:12 -050089/*
90 * Util/helpers:
91 */
92
Rob Clark720c3bb2017-01-30 11:30:58 -050093struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
94{
95 struct clk *clk;
96 char name2[32];
97
98 clk = devm_clk_get(&pdev->dev, name);
99 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
100 return clk;
101
102 snprintf(name2, sizeof(name2), "%s_clk", name);
103
104 clk = devm_clk_get(&pdev->dev, name2);
105 if (!IS_ERR(clk))
106 dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
107 "\"%s\" instead of \"%s\"\n", name, name2);
108
109 return clk;
110}
111
Rob Clarkc8afe682013-06-26 12:44:06 -0400112void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
113 const char *dbgname)
114{
115 struct resource *res;
116 unsigned long size;
117 void __iomem *ptr;
118
119 if (name)
120 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
121 else
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123
124 if (!res) {
125 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
126 return ERR_PTR(-EINVAL);
127 }
128
129 size = resource_size(res);
130
131 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
132 if (!ptr) {
133 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
134 return ERR_PTR(-ENOMEM);
135 }
136
137 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200138 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400139
140 return ptr;
141}
142
143void msm_writel(u32 data, void __iomem *addr)
144{
145 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200146 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
Rob Clarkc8afe682013-06-26 12:44:06 -0400147 writel(data, addr);
148}
149
150u32 msm_readl(const void __iomem *addr)
151{
152 u32 val = readl(addr);
153 if (reglog)
Joe Perches8dfe1622017-02-28 04:55:54 -0800154 pr_err("IO:R %p %08x\n", addr, val);
Rob Clarkc8afe682013-06-26 12:44:06 -0400155 return val;
156}
157
Hai Li78b1d472015-07-27 13:49:45 -0400158struct vblank_event {
159 struct list_head node;
160 int crtc_id;
161 bool enable;
162};
163
164static void vblank_ctrl_worker(struct work_struct *work)
165{
166 struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
167 struct msm_vblank_ctrl, work);
168 struct msm_drm_private *priv = container_of(vbl_ctrl,
169 struct msm_drm_private, vblank_ctrl);
170 struct msm_kms *kms = priv->kms;
171 struct vblank_event *vbl_ev, *tmp;
172 unsigned long flags;
173
174 spin_lock_irqsave(&vbl_ctrl->lock, flags);
175 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
176 list_del(&vbl_ev->node);
177 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
178
179 if (vbl_ev->enable)
180 kms->funcs->enable_vblank(kms,
181 priv->crtcs[vbl_ev->crtc_id]);
182 else
183 kms->funcs->disable_vblank(kms,
184 priv->crtcs[vbl_ev->crtc_id]);
185
186 kfree(vbl_ev);
187
188 spin_lock_irqsave(&vbl_ctrl->lock, flags);
189 }
190
191 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
192}
193
194static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
195 int crtc_id, bool enable)
196{
197 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
198 struct vblank_event *vbl_ev;
199 unsigned long flags;
200
201 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
202 if (!vbl_ev)
203 return -ENOMEM;
204
205 vbl_ev->crtc_id = crtc_id;
206 vbl_ev->enable = enable;
207
208 spin_lock_irqsave(&vbl_ctrl->lock, flags);
209 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
210 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
211
212 queue_work(priv->wq, &vbl_ctrl->work);
213
214 return 0;
215}
216
Archit Taneja2b669872016-05-02 11:05:54 +0530217static int msm_drm_uninit(struct device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400218{
Archit Taneja2b669872016-05-02 11:05:54 +0530219 struct platform_device *pdev = to_platform_device(dev);
220 struct drm_device *ddev = platform_get_drvdata(pdev);
221 struct msm_drm_private *priv = ddev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400222 struct msm_kms *kms = priv->kms;
Rob Clark7198e6b2013-07-19 12:59:32 -0400223 struct msm_gpu *gpu = priv->gpu;
Hai Li78b1d472015-07-27 13:49:45 -0400224 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
225 struct vblank_event *vbl_ev, *tmp;
226
227 /* We must cancel and cleanup any pending vblank enable/disable
228 * work before drm_irq_uninstall() to avoid work re-enabling an
229 * irq after uninstall has disabled it.
230 */
231 cancel_work_sync(&vbl_ctrl->work);
232 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
233 list_del(&vbl_ev->node);
234 kfree(vbl_ev);
235 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400236
Rob Clark68209392016-05-17 16:19:32 -0400237 msm_gem_shrinker_cleanup(ddev);
238
Archit Taneja2b669872016-05-02 11:05:54 +0530239 drm_kms_helper_poll_fini(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530240
Archit Taneja2b669872016-05-02 11:05:54 +0530241 drm_dev_unregister(ddev);
Archit Taneja8208ed92016-05-02 11:05:53 +0530242
Noralf Trønnes85eac472017-03-07 21:49:22 +0100243 msm_perf_debugfs_cleanup(priv);
244 msm_rd_debugfs_cleanup(priv);
245
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530246#ifdef CONFIG_DRM_FBDEV_EMULATION
247 if (fbdev && priv->fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530248 msm_fbdev_free(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530249#endif
Archit Taneja2b669872016-05-02 11:05:54 +0530250 drm_mode_config_cleanup(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400251
Archit Taneja2b669872016-05-02 11:05:54 +0530252 pm_runtime_get_sync(dev);
253 drm_irq_uninstall(ddev);
254 pm_runtime_put_sync(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400255
256 flush_workqueue(priv->wq);
257 destroy_workqueue(priv->wq);
258
Rob Clarkba00c3f2016-03-16 18:18:17 -0400259 flush_workqueue(priv->atomic_wq);
260 destroy_workqueue(priv->atomic_wq);
261
Archit Taneja16976082016-11-03 17:36:18 +0530262 if (kms && kms->funcs)
Rob Clarkc8afe682013-06-26 12:44:06 -0400263 kms->funcs->destroy(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400264
Rob Clark7198e6b2013-07-19 12:59:32 -0400265 if (gpu) {
Archit Taneja2b669872016-05-02 11:05:54 +0530266 mutex_lock(&ddev->struct_mutex);
Rob Clarkeeb75472017-02-10 15:36:33 -0500267 // XXX what do we do here?
268 //pm_runtime_enable(&pdev->dev);
Rob Clark7198e6b2013-07-19 12:59:32 -0400269 gpu->funcs->pm_suspend(gpu);
Archit Taneja2b669872016-05-02 11:05:54 +0530270 mutex_unlock(&ddev->struct_mutex);
Rob Clark774449e2015-05-15 09:19:36 -0400271 gpu->funcs->destroy(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400272 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400273
Rob Clark871d8122013-11-16 12:56:06 -0500274 if (priv->vram.paddr) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700275 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
Rob Clark871d8122013-11-16 12:56:06 -0500276 drm_mm_takedown(&priv->vram.mm);
Archit Taneja2b669872016-05-02 11:05:54 +0530277 dma_free_attrs(dev, priv->vram.size, NULL,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700278 priv->vram.paddr, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500279 }
280
Archit Taneja2b669872016-05-02 11:05:54 +0530281 component_unbind_all(dev, ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500282
Archit Taneja0a6030d2016-05-08 21:36:28 +0530283 msm_mdss_destroy(ddev);
284
Archit Taneja2b669872016-05-02 11:05:54 +0530285 ddev->dev_private = NULL;
286 drm_dev_unref(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400287
288 kfree(priv);
289
290 return 0;
291}
292
Rob Clark06c0dd92013-11-30 17:51:47 -0500293static int get_mdp_ver(struct platform_device *pdev)
294{
Rob Clark06c0dd92013-11-30 17:51:47 -0500295 struct device *dev = &pdev->dev;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530296
297 return (int) (unsigned long) of_device_get_match_data(dev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500298}
299
Rob Clark072f1f92015-03-03 15:04:25 -0500300#include <linux/of_address.h>
301
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500302static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400303{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500304 struct msm_drm_private *priv = dev->dev_private;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530305 struct device_node *node;
Rob Clark072f1f92015-03-03 15:04:25 -0500306 unsigned long size = 0;
307 int ret = 0;
308
Rob Clark072f1f92015-03-03 15:04:25 -0500309 /* In the device-tree world, we could have a 'memory-region'
310 * phandle, which gives us a link to our "vram". Allocating
311 * is all nicely abstracted behind the dma api, but we need
312 * to know the entire size to allocate it all in one go. There
313 * are two cases:
314 * 1) device with no IOMMU, in which case we need exclusive
315 * access to a VRAM carveout big enough for all gpu
316 * buffers
317 * 2) device with IOMMU, but where the bootloader puts up
318 * a splash screen. In this case, the VRAM carveout
319 * need only be large enough for fbdev fb. But we need
320 * exclusive access to the buffer to avoid the kernel
321 * using those pages for other purposes (which appears
322 * as corruption on screen before we have a chance to
323 * load and do initial modeset)
324 */
Rob Clark072f1f92015-03-03 15:04:25 -0500325
326 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
327 if (node) {
328 struct resource r;
329 ret = of_address_to_resource(node, 0, &r);
Peter Chen2ca41c172016-07-04 16:49:50 +0800330 of_node_put(node);
Rob Clark072f1f92015-03-03 15:04:25 -0500331 if (ret)
332 return ret;
333 size = r.end - r.start;
Thierry Redingfc99f972015-04-09 16:39:51 +0200334 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
Rob Clarkc8afe682013-06-26 12:44:06 -0400335
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530336 /* if we have no IOMMU, then we need to use carveout allocator.
337 * Grab the entire CMA chunk carved out in early startup in
338 * mach-msm:
339 */
340 } else if (!iommu_present(&platform_bus_type)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500341 DRM_INFO("using %s VRAM carveout\n", vram);
342 size = memparse(vram, NULL);
343 }
344
345 if (size) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700346 unsigned long attrs = 0;
Rob Clark871d8122013-11-16 12:56:06 -0500347 void *p;
348
Rob Clark871d8122013-11-16 12:56:06 -0500349 priv->vram.size = size;
350
351 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
352
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700353 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
354 attrs |= DMA_ATTR_WRITE_COMBINE;
Rob Clark871d8122013-11-16 12:56:06 -0500355
356 /* note that for no-kernel-mapping, the vaddr returned
357 * is bogus, but non-null if allocation succeeded:
358 */
359 p = dma_alloc_attrs(dev->dev, size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700360 &priv->vram.paddr, GFP_KERNEL, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500361 if (!p) {
362 dev_err(dev->dev, "failed to allocate VRAM\n");
363 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500364 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500365 }
366
367 dev_info(dev->dev, "VRAM: %08x->%08x\n",
368 (uint32_t)priv->vram.paddr,
369 (uint32_t)(priv->vram.paddr + size));
370 }
371
Rob Clark072f1f92015-03-03 15:04:25 -0500372 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500373}
374
Archit Taneja2b669872016-05-02 11:05:54 +0530375static int msm_drm_init(struct device *dev, struct drm_driver *drv)
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500376{
Archit Taneja2b669872016-05-02 11:05:54 +0530377 struct platform_device *pdev = to_platform_device(dev);
378 struct drm_device *ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500379 struct msm_drm_private *priv;
380 struct msm_kms *kms;
381 int ret;
382
Archit Taneja2b669872016-05-02 11:05:54 +0530383 ddev = drm_dev_alloc(drv, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200384 if (IS_ERR(ddev)) {
Archit Taneja2b669872016-05-02 11:05:54 +0530385 dev_err(dev, "failed to allocate drm_device\n");
Tom Gundersen0f288602016-09-21 16:59:19 +0200386 return PTR_ERR(ddev);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500387 }
388
Archit Taneja2b669872016-05-02 11:05:54 +0530389 platform_set_drvdata(pdev, ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530390
391 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
392 if (!priv) {
393 drm_dev_unref(ddev);
394 return -ENOMEM;
395 }
396
397 ddev->dev_private = priv;
Rob Clark68209392016-05-17 16:19:32 -0400398 priv->dev = ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500399
Archit Taneja0a6030d2016-05-08 21:36:28 +0530400 ret = msm_mdss_init(ddev);
401 if (ret) {
402 kfree(priv);
403 drm_dev_unref(ddev);
404 return ret;
405 }
406
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500407 priv->wq = alloc_ordered_workqueue("msm", 0);
Rob Clarkba00c3f2016-03-16 18:18:17 -0400408 priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500409 init_waitqueue_head(&priv->pending_crtcs_event);
410
411 INIT_LIST_HEAD(&priv->inactive_list);
Hai Li78b1d472015-07-27 13:49:45 -0400412 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
413 INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker);
414 spin_lock_init(&priv->vblank_ctrl.lock);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500415
Archit Taneja2b669872016-05-02 11:05:54 +0530416 drm_mode_config_init(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500417
418 /* Bind all our sub-components: */
Archit Taneja2b669872016-05-02 11:05:54 +0530419 ret = component_bind_all(dev, ddev);
420 if (ret) {
Archit Taneja0a6030d2016-05-08 21:36:28 +0530421 msm_mdss_destroy(ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530422 kfree(priv);
423 drm_dev_unref(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500424 return ret;
Archit Taneja2b669872016-05-02 11:05:54 +0530425 }
Rob Clark060530f2014-03-03 14:19:12 -0500426
Archit Taneja2b669872016-05-02 11:05:54 +0530427 ret = msm_init_vram(ddev);
Rob Clark13f15562015-05-07 15:20:13 -0400428 if (ret)
429 goto fail;
430
Rob Clark68209392016-05-17 16:19:32 -0400431 msm_gem_shrinker_init(ddev);
432
Rob Clark06c0dd92013-11-30 17:51:47 -0500433 switch (get_mdp_ver(pdev)) {
434 case 4:
Archit Taneja2b669872016-05-02 11:05:54 +0530435 kms = mdp4_kms_init(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530436 priv->kms = kms;
Rob Clark06c0dd92013-11-30 17:51:47 -0500437 break;
438 case 5:
Archit Taneja392ae6e2016-06-14 18:24:54 +0530439 kms = mdp5_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500440 break;
441 default:
442 kms = ERR_PTR(-ENODEV);
443 break;
444 }
445
Rob Clarkc8afe682013-06-26 12:44:06 -0400446 if (IS_ERR(kms)) {
447 /*
448 * NOTE: once we have GPU support, having no kms should not
449 * be considered fatal.. ideally we would still support gpu
450 * and (for example) use dmabuf/prime to share buffers with
451 * imx drm driver on iMX5
452 */
Archit Taneja2b669872016-05-02 11:05:54 +0530453 dev_err(dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200454 ret = PTR_ERR(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400455 goto fail;
456 }
457
Rob Clarkc8afe682013-06-26 12:44:06 -0400458 if (kms) {
Rob Clarkc8afe682013-06-26 12:44:06 -0400459 ret = kms->funcs->hw_init(kms);
460 if (ret) {
Archit Taneja2b669872016-05-02 11:05:54 +0530461 dev_err(dev, "kms hw init failed: %d\n", ret);
Rob Clarkc8afe682013-06-26 12:44:06 -0400462 goto fail;
463 }
464 }
465
Archit Taneja2b669872016-05-02 11:05:54 +0530466 ddev->mode_config.funcs = &mode_config_funcs;
Rob Clarkc8afe682013-06-26 12:44:06 -0400467
Archit Taneja2b669872016-05-02 11:05:54 +0530468 ret = drm_vblank_init(ddev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400469 if (ret < 0) {
Archit Taneja2b669872016-05-02 11:05:54 +0530470 dev_err(dev, "failed to initialize vblank\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400471 goto fail;
472 }
473
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530474 if (kms) {
475 pm_runtime_get_sync(dev);
476 ret = drm_irq_install(ddev, kms->irq);
477 pm_runtime_put_sync(dev);
478 if (ret < 0) {
479 dev_err(dev, "failed to install IRQ handler\n");
480 goto fail;
481 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400482 }
483
Archit Taneja2b669872016-05-02 11:05:54 +0530484 ret = drm_dev_register(ddev, 0);
Rob Clarka7d3c952014-05-30 14:47:38 -0400485 if (ret)
486 goto fail;
487
Archit Taneja2b669872016-05-02 11:05:54 +0530488 drm_mode_config_reset(ddev);
489
490#ifdef CONFIG_DRM_FBDEV_EMULATION
491 if (fbdev)
492 priv->fbdev = msm_fbdev_init(ddev);
493#endif
494
495 ret = msm_debugfs_late_init(ddev);
496 if (ret)
497 goto fail;
498
499 drm_kms_helper_poll_init(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400500
501 return 0;
502
503fail:
Archit Taneja2b669872016-05-02 11:05:54 +0530504 msm_drm_uninit(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400505 return ret;
506}
507
Archit Taneja2b669872016-05-02 11:05:54 +0530508/*
509 * DRM operations:
510 */
511
Rob Clark7198e6b2013-07-19 12:59:32 -0400512static void load_gpu(struct drm_device *dev)
513{
Rob Clarka1ad3522014-07-11 11:59:22 -0400514 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400515 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400516
Rob Clarka1ad3522014-07-11 11:59:22 -0400517 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400518
Rob Clarke2550b72014-09-05 13:30:27 -0400519 if (!priv->gpu)
520 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400521
Rob Clarka1ad3522014-07-11 11:59:22 -0400522 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400523}
524
525static int msm_open(struct drm_device *dev, struct drm_file *file)
526{
527 struct msm_file_private *ctx;
528
529 /* For now, load gpu on open.. to avoid the requirement of having
530 * firmware in the initrd.
531 */
532 load_gpu(dev);
533
534 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
535 if (!ctx)
536 return -ENOMEM;
537
538 file->driver_priv = ctx;
539
540 return 0;
541}
542
Daniel Vetter94df1452017-03-08 15:12:46 +0100543static void msm_postclose(struct drm_device *dev, struct drm_file *file)
Rob Clarkc8afe682013-06-26 12:44:06 -0400544{
545 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400546 struct msm_file_private *ctx = file->driver_priv;
Rob Clark7198e6b2013-07-19 12:59:32 -0400547
Rob Clark7198e6b2013-07-19 12:59:32 -0400548 mutex_lock(&dev->struct_mutex);
549 if (ctx == priv->lastctx)
550 priv->lastctx = NULL;
551 mutex_unlock(&dev->struct_mutex);
552
553 kfree(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400554}
555
556static void msm_lastclose(struct drm_device *dev)
557{
558 struct msm_drm_private *priv = dev->dev_private;
Rob Clark5ea1f752014-05-30 12:29:48 -0400559 if (priv->fbdev)
560 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400561}
562
Daniel Vettere9f0d762013-12-11 11:34:42 +0100563static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400564{
565 struct drm_device *dev = arg;
566 struct msm_drm_private *priv = dev->dev_private;
567 struct msm_kms *kms = priv->kms;
568 BUG_ON(!kms);
569 return kms->funcs->irq(kms);
570}
571
572static void msm_irq_preinstall(struct drm_device *dev)
573{
574 struct msm_drm_private *priv = dev->dev_private;
575 struct msm_kms *kms = priv->kms;
576 BUG_ON(!kms);
577 kms->funcs->irq_preinstall(kms);
578}
579
580static int msm_irq_postinstall(struct drm_device *dev)
581{
582 struct msm_drm_private *priv = dev->dev_private;
583 struct msm_kms *kms = priv->kms;
584 BUG_ON(!kms);
585 return kms->funcs->irq_postinstall(kms);
586}
587
588static void msm_irq_uninstall(struct drm_device *dev)
589{
590 struct msm_drm_private *priv = dev->dev_private;
591 struct msm_kms *kms = priv->kms;
592 BUG_ON(!kms);
593 kms->funcs->irq_uninstall(kms);
594}
595
Thierry Reding88e72712015-09-24 18:35:31 +0200596static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400597{
598 struct msm_drm_private *priv = dev->dev_private;
599 struct msm_kms *kms = priv->kms;
600 if (!kms)
601 return -ENXIO;
Thierry Reding88e72712015-09-24 18:35:31 +0200602 DBG("dev=%p, crtc=%u", dev, pipe);
603 return vblank_ctrl_queue_work(priv, pipe, true);
Rob Clarkc8afe682013-06-26 12:44:06 -0400604}
605
Thierry Reding88e72712015-09-24 18:35:31 +0200606static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400607{
608 struct msm_drm_private *priv = dev->dev_private;
609 struct msm_kms *kms = priv->kms;
610 if (!kms)
611 return;
Thierry Reding88e72712015-09-24 18:35:31 +0200612 DBG("dev=%p, crtc=%u", dev, pipe);
613 vblank_ctrl_queue_work(priv, pipe, false);
Rob Clarkc8afe682013-06-26 12:44:06 -0400614}
615
616/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400617 * DRM ioctls:
618 */
619
620static int msm_ioctl_get_param(struct drm_device *dev, void *data,
621 struct drm_file *file)
622{
623 struct msm_drm_private *priv = dev->dev_private;
624 struct drm_msm_param *args = data;
625 struct msm_gpu *gpu;
626
627 /* for now, we just have 3d pipe.. eventually this would need to
628 * be more clever to dispatch to appropriate gpu module:
629 */
630 if (args->pipe != MSM_PIPE_3D0)
631 return -EINVAL;
632
633 gpu = priv->gpu;
634
635 if (!gpu)
636 return -ENXIO;
637
638 return gpu->funcs->get_param(gpu, args->param, &args->value);
639}
640
641static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
642 struct drm_file *file)
643{
644 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500645
646 if (args->flags & ~MSM_BO_FLAGS) {
647 DRM_ERROR("invalid flags: %08x\n", args->flags);
648 return -EINVAL;
649 }
650
Rob Clark7198e6b2013-07-19 12:59:32 -0400651 return msm_gem_new_handle(dev, file, args->size,
652 args->flags, &args->handle);
653}
654
Rob Clark56c2da82015-05-11 11:50:03 -0400655static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
656{
657 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
658}
Rob Clark7198e6b2013-07-19 12:59:32 -0400659
660static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
661 struct drm_file *file)
662{
663 struct drm_msm_gem_cpu_prep *args = data;
664 struct drm_gem_object *obj;
Rob Clark56c2da82015-05-11 11:50:03 -0400665 ktime_t timeout = to_ktime(args->timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400666 int ret;
667
Rob Clark93ddb0d2014-03-03 09:42:33 -0500668 if (args->op & ~MSM_PREP_FLAGS) {
669 DRM_ERROR("invalid op: %08x\n", args->op);
670 return -EINVAL;
671 }
672
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100673 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400674 if (!obj)
675 return -ENOENT;
676
Rob Clark56c2da82015-05-11 11:50:03 -0400677 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400678
679 drm_gem_object_unreference_unlocked(obj);
680
681 return ret;
682}
683
684static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
685 struct drm_file *file)
686{
687 struct drm_msm_gem_cpu_fini *args = data;
688 struct drm_gem_object *obj;
689 int ret;
690
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100691 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400692 if (!obj)
693 return -ENOENT;
694
695 ret = msm_gem_cpu_fini(obj);
696
697 drm_gem_object_unreference_unlocked(obj);
698
699 return ret;
700}
701
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600702static int msm_ioctl_gem_info_iova(struct drm_device *dev,
703 struct drm_gem_object *obj, uint64_t *iova)
704{
705 struct msm_drm_private *priv = dev->dev_private;
706
707 if (!priv->gpu)
708 return -EINVAL;
709
710 return msm_gem_get_iova(obj, priv->gpu->id, iova);
711}
712
Rob Clark7198e6b2013-07-19 12:59:32 -0400713static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
714 struct drm_file *file)
715{
716 struct drm_msm_gem_info *args = data;
717 struct drm_gem_object *obj;
718 int ret = 0;
719
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600720 if (args->flags & ~MSM_INFO_FLAGS)
Rob Clark7198e6b2013-07-19 12:59:32 -0400721 return -EINVAL;
722
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100723 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400724 if (!obj)
725 return -ENOENT;
726
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600727 if (args->flags & MSM_INFO_IOVA) {
728 uint64_t iova;
729
730 ret = msm_ioctl_gem_info_iova(dev, obj, &iova);
731 if (!ret)
732 args->offset = iova;
733 } else {
734 args->offset = msm_gem_mmap_offset(obj);
735 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400736
737 drm_gem_object_unreference_unlocked(obj);
738
739 return ret;
740}
741
742static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
743 struct drm_file *file)
744{
Rob Clarkca762a82016-03-15 17:22:13 -0400745 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400746 struct drm_msm_wait_fence *args = data;
Rob Clark56c2da82015-05-11 11:50:03 -0400747 ktime_t timeout = to_ktime(args->timeout);
Rob Clark93ddb0d2014-03-03 09:42:33 -0500748
749 if (args->pad) {
750 DRM_ERROR("invalid pad: %08x\n", args->pad);
751 return -EINVAL;
752 }
753
Rob Clarkca762a82016-03-15 17:22:13 -0400754 if (!priv->gpu)
755 return 0;
756
757 return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
Rob Clark7198e6b2013-07-19 12:59:32 -0400758}
759
Rob Clark4cd33c42016-05-17 15:44:49 -0400760static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
761 struct drm_file *file)
762{
763 struct drm_msm_gem_madvise *args = data;
764 struct drm_gem_object *obj;
765 int ret;
766
767 switch (args->madv) {
768 case MSM_MADV_DONTNEED:
769 case MSM_MADV_WILLNEED:
770 break;
771 default:
772 return -EINVAL;
773 }
774
775 ret = mutex_lock_interruptible(&dev->struct_mutex);
776 if (ret)
777 return ret;
778
779 obj = drm_gem_object_lookup(file, args->handle);
780 if (!obj) {
781 ret = -ENOENT;
782 goto unlock;
783 }
784
785 ret = msm_gem_madvise(obj, args->madv);
786 if (ret >= 0) {
787 args->retained = ret;
788 ret = 0;
789 }
790
791 drm_gem_object_unreference(obj);
792
793unlock:
794 mutex_unlock(&dev->struct_mutex);
795 return ret;
796}
797
Rob Clark7198e6b2013-07-19 12:59:32 -0400798static const struct drm_ioctl_desc msm_ioctls[] = {
Daniel Vetterf8c47142015-09-08 13:56:30 +0200799 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
800 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
801 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
802 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
803 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
804 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
805 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark4cd33c42016-05-17 15:44:49 -0400806 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -0400807};
808
Rob Clarkc8afe682013-06-26 12:44:06 -0400809static const struct vm_operations_struct vm_ops = {
810 .fault = msm_gem_fault,
811 .open = drm_gem_vm_open,
812 .close = drm_gem_vm_close,
813};
814
815static const struct file_operations fops = {
816 .owner = THIS_MODULE,
817 .open = drm_open,
818 .release = drm_release,
819 .unlocked_ioctl = drm_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -0400820 .compat_ioctl = drm_compat_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -0400821 .poll = drm_poll,
822 .read = drm_read,
823 .llseek = no_llseek,
824 .mmap = msm_gem_mmap,
825};
826
827static struct drm_driver msm_driver = {
Rob Clark05b84912013-09-28 11:28:35 -0400828 .driver_features = DRIVER_HAVE_IRQ |
829 DRIVER_GEM |
830 DRIVER_PRIME |
Rob Clarkb4b15c82013-09-28 12:01:25 -0400831 DRIVER_RENDER |
Rob Clarka5436e12015-06-04 10:12:22 -0400832 DRIVER_ATOMIC |
Rob Clark05b84912013-09-28 11:28:35 -0400833 DRIVER_MODESET,
Rob Clark7198e6b2013-07-19 12:59:32 -0400834 .open = msm_open,
Daniel Vetter94df1452017-03-08 15:12:46 +0100835 .postclose = msm_postclose,
Rob Clarkc8afe682013-06-26 12:44:06 -0400836 .lastclose = msm_lastclose,
837 .irq_handler = msm_irq,
838 .irq_preinstall = msm_irq_preinstall,
839 .irq_postinstall = msm_irq_postinstall,
840 .irq_uninstall = msm_irq_uninstall,
Rob Clarkc8afe682013-06-26 12:44:06 -0400841 .enable_vblank = msm_enable_vblank,
842 .disable_vblank = msm_disable_vblank,
843 .gem_free_object = msm_gem_free_object,
844 .gem_vm_ops = &vm_ops,
845 .dumb_create = msm_gem_dumb_create,
846 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark30600a92013-09-28 10:13:04 -0400847 .dumb_destroy = drm_gem_dumb_destroy,
Rob Clark05b84912013-09-28 11:28:35 -0400848 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
849 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
850 .gem_prime_export = drm_gem_prime_export,
851 .gem_prime_import = drm_gem_prime_import,
Eric Anholt43523eb2017-04-12 12:11:58 -0700852 .gem_prime_res_obj = msm_gem_prime_res_obj,
Rob Clark05b84912013-09-28 11:28:35 -0400853 .gem_prime_pin = msm_gem_prime_pin,
854 .gem_prime_unpin = msm_gem_prime_unpin,
855 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
856 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
857 .gem_prime_vmap = msm_gem_prime_vmap,
858 .gem_prime_vunmap = msm_gem_prime_vunmap,
Daniel Thompson77a147e2014-11-12 11:38:14 +0000859 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -0400860#ifdef CONFIG_DEBUG_FS
861 .debugfs_init = msm_debugfs_init,
Rob Clarkc8afe682013-06-26 12:44:06 -0400862#endif
Rob Clark7198e6b2013-07-19 12:59:32 -0400863 .ioctls = msm_ioctls,
Jordan Crouse167b6062017-05-08 14:34:59 -0600864 .num_ioctls = ARRAY_SIZE(msm_ioctls),
Rob Clarkc8afe682013-06-26 12:44:06 -0400865 .fops = &fops,
866 .name = "msm",
867 .desc = "MSM Snapdragon DRM",
868 .date = "20130625",
Rob Clarka8d854c2016-06-01 14:02:02 -0400869 .major = MSM_VERSION_MAJOR,
870 .minor = MSM_VERSION_MINOR,
871 .patchlevel = MSM_VERSION_PATCHLEVEL,
Rob Clarkc8afe682013-06-26 12:44:06 -0400872};
873
874#ifdef CONFIG_PM_SLEEP
875static int msm_pm_suspend(struct device *dev)
876{
877 struct drm_device *ddev = dev_get_drvdata(dev);
878
879 drm_kms_helper_poll_disable(ddev);
880
881 return 0;
882}
883
884static int msm_pm_resume(struct device *dev)
885{
886 struct drm_device *ddev = dev_get_drvdata(dev);
887
888 drm_kms_helper_poll_enable(ddev);
889
890 return 0;
891}
892#endif
893
894static const struct dev_pm_ops msm_pm_ops = {
895 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
896};
897
898/*
Rob Clark060530f2014-03-03 14:19:12 -0500899 * Componentized driver support:
900 */
901
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530902/*
903 * NOTE: duplication of the same code as exynos or imx (or probably any other).
904 * so probably some room for some helpers
Rob Clark060530f2014-03-03 14:19:12 -0500905 */
906static int compare_of(struct device *dev, void *data)
907{
908 return dev->of_node == data;
909}
Rob Clark41e69772013-12-15 16:23:05 -0500910
Archit Taneja812070e2016-05-19 10:38:39 +0530911/*
912 * Identify what components need to be added by parsing what remote-endpoints
913 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
914 * is no external component that we need to add since LVDS is within MDP4
915 * itself.
916 */
917static int add_components_mdp(struct device *mdp_dev,
918 struct component_match **matchptr)
919{
920 struct device_node *np = mdp_dev->of_node;
921 struct device_node *ep_node;
Archit Taneja54011e22016-06-06 13:45:34 +0530922 struct device *master_dev;
923
924 /*
925 * on MDP4 based platforms, the MDP platform device is the component
926 * master that adds other display interface components to itself.
927 *
928 * on MDP5 based platforms, the MDSS platform device is the component
929 * master that adds MDP5 and other display interface components to
930 * itself.
931 */
932 if (of_device_is_compatible(np, "qcom,mdp4"))
933 master_dev = mdp_dev;
934 else
935 master_dev = mdp_dev->parent;
Archit Taneja812070e2016-05-19 10:38:39 +0530936
937 for_each_endpoint_of_node(np, ep_node) {
938 struct device_node *intf;
939 struct of_endpoint ep;
940 int ret;
941
942 ret = of_graph_parse_endpoint(ep_node, &ep);
943 if (ret) {
944 dev_err(mdp_dev, "unable to parse port endpoint\n");
945 of_node_put(ep_node);
946 return ret;
947 }
948
949 /*
950 * The LCDC/LVDS port on MDP4 is a speacial case where the
951 * remote-endpoint isn't a component that we need to add
952 */
953 if (of_device_is_compatible(np, "qcom,mdp4") &&
Archit Tanejad8dd8052016-11-17 12:12:03 +0530954 ep.port == 0)
Archit Taneja812070e2016-05-19 10:38:39 +0530955 continue;
Archit Taneja812070e2016-05-19 10:38:39 +0530956
957 /*
958 * It's okay if some of the ports don't have a remote endpoint
959 * specified. It just means that the port isn't connected to
960 * any external interface.
961 */
962 intf = of_graph_get_remote_port_parent(ep_node);
Archit Tanejad8dd8052016-11-17 12:12:03 +0530963 if (!intf)
Archit Taneja812070e2016-05-19 10:38:39 +0530964 continue;
Archit Taneja812070e2016-05-19 10:38:39 +0530965
Russell King97ac0e42016-10-19 11:28:27 +0100966 drm_of_component_match_add(master_dev, matchptr, compare_of,
967 intf);
Archit Taneja812070e2016-05-19 10:38:39 +0530968 of_node_put(intf);
Archit Taneja812070e2016-05-19 10:38:39 +0530969 }
970
971 return 0;
972}
973
Archit Taneja54011e22016-06-06 13:45:34 +0530974static int compare_name_mdp(struct device *dev, void *data)
975{
976 return (strstr(dev_name(dev), "mdp") != NULL);
977}
978
Archit Taneja7d526fcf2016-05-19 10:33:57 +0530979static int add_display_components(struct device *dev,
980 struct component_match **matchptr)
981{
Archit Taneja54011e22016-06-06 13:45:34 +0530982 struct device *mdp_dev;
983 int ret;
984
985 /*
986 * MDP5 based devices don't have a flat hierarchy. There is a top level
987 * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
988 * children devices, find the MDP5 node, and then add the interfaces
989 * to our components list.
990 */
991 if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
992 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
993 if (ret) {
994 dev_err(dev, "failed to populate children devices\n");
995 return ret;
996 }
997
998 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
999 if (!mdp_dev) {
1000 dev_err(dev, "failed to find MDSS MDP node\n");
1001 of_platform_depopulate(dev);
1002 return -ENODEV;
1003 }
1004
1005 put_device(mdp_dev);
1006
1007 /* add the MDP component itself */
Russell King97ac0e42016-10-19 11:28:27 +01001008 drm_of_component_match_add(dev, matchptr, compare_of,
1009 mdp_dev->of_node);
Archit Taneja54011e22016-06-06 13:45:34 +05301010 } else {
1011 /* MDP4 */
1012 mdp_dev = dev;
1013 }
1014
1015 ret = add_components_mdp(mdp_dev, matchptr);
1016 if (ret)
1017 of_platform_depopulate(dev);
1018
1019 return ret;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301020}
1021
Archit Tanejadc3ea262016-05-19 13:33:52 +05301022/*
1023 * We don't know what's the best binding to link the gpu with the drm device.
1024 * Fow now, we just hunt for all the possible gpus that we support, and add them
1025 * as components.
1026 */
1027static const struct of_device_id msm_gpu_match[] = {
Rob Clark1db7afa2017-01-30 11:02:27 -05001028 { .compatible = "qcom,adreno" },
Archit Tanejadc3ea262016-05-19 13:33:52 +05301029 { .compatible = "qcom,adreno-3xx" },
1030 { .compatible = "qcom,kgsl-3d0" },
1031 { },
1032};
1033
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301034static int add_gpu_components(struct device *dev,
1035 struct component_match **matchptr)
1036{
Archit Tanejadc3ea262016-05-19 13:33:52 +05301037 struct device_node *np;
1038
1039 np = of_find_matching_node(NULL, msm_gpu_match);
1040 if (!np)
1041 return 0;
1042
Russell King97ac0e42016-10-19 11:28:27 +01001043 drm_of_component_match_add(dev, matchptr, compare_of, np);
Archit Tanejadc3ea262016-05-19 13:33:52 +05301044
1045 of_node_put(np);
1046
1047 return 0;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301048}
1049
Russell King84448282014-04-19 11:20:42 +01001050static int msm_drm_bind(struct device *dev)
1051{
Archit Taneja2b669872016-05-02 11:05:54 +05301052 return msm_drm_init(dev, &msm_driver);
Russell King84448282014-04-19 11:20:42 +01001053}
1054
1055static void msm_drm_unbind(struct device *dev)
1056{
Archit Taneja2b669872016-05-02 11:05:54 +05301057 msm_drm_uninit(dev);
Russell King84448282014-04-19 11:20:42 +01001058}
1059
1060static const struct component_master_ops msm_drm_ops = {
1061 .bind = msm_drm_bind,
1062 .unbind = msm_drm_unbind,
1063};
1064
1065/*
1066 * Platform driver:
1067 */
1068
1069static int msm_pdev_probe(struct platform_device *pdev)
1070{
1071 struct component_match *match = NULL;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301072 int ret;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301073
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301074 ret = add_display_components(&pdev->dev, &match);
1075 if (ret)
1076 return ret;
1077
1078 ret = add_gpu_components(&pdev->dev, &match);
1079 if (ret)
1080 return ret;
Rob Clark060530f2014-03-03 14:19:12 -05001081
Rob Clarkc83ea572016-11-07 13:31:30 -05001082 /* on all devices that I am aware of, iommu's which can map
1083 * any address the cpu can see are used:
1084 */
1085 ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1086 if (ret)
1087 return ret;
1088
Russell King84448282014-04-19 11:20:42 +01001089 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
Rob Clarkc8afe682013-06-26 12:44:06 -04001090}
1091
1092static int msm_pdev_remove(struct platform_device *pdev)
1093{
Rob Clark060530f2014-03-03 14:19:12 -05001094 component_master_del(&pdev->dev, &msm_drm_ops);
Archit Taneja54011e22016-06-06 13:45:34 +05301095 of_platform_depopulate(&pdev->dev);
Rob Clarkc8afe682013-06-26 12:44:06 -04001096
1097 return 0;
1098}
1099
Rob Clark06c0dd92013-11-30 17:51:47 -05001100static const struct of_device_id dt_match[] = {
Archit Taneja96a611b2016-05-30 17:02:00 +05301101 { .compatible = "qcom,mdp4", .data = (void *)4 }, /* MDP4 */
1102 { .compatible = "qcom,mdss", .data = (void *)5 }, /* MDP5 MDSS */
Rob Clark06c0dd92013-11-30 17:51:47 -05001103 {}
1104};
1105MODULE_DEVICE_TABLE(of, dt_match);
1106
Rob Clarkc8afe682013-06-26 12:44:06 -04001107static struct platform_driver msm_platform_driver = {
1108 .probe = msm_pdev_probe,
1109 .remove = msm_pdev_remove,
1110 .driver = {
Rob Clarkc8afe682013-06-26 12:44:06 -04001111 .name = "msm",
Rob Clark06c0dd92013-11-30 17:51:47 -05001112 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -04001113 .pm = &msm_pm_ops,
1114 },
Rob Clarkc8afe682013-06-26 12:44:06 -04001115};
1116
1117static int __init msm_drm_register(void)
1118{
1119 DBG("init");
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301120 msm_mdp_register();
Hai Lid5af49c2015-03-26 19:25:17 -04001121 msm_dsi_register();
Hai Li00453982014-12-12 14:41:17 -05001122 msm_edp_register();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001123 msm_hdmi_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001124 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001125 return platform_driver_register(&msm_platform_driver);
1126}
1127
1128static void __exit msm_drm_unregister(void)
1129{
1130 DBG("fini");
1131 platform_driver_unregister(&msm_platform_driver);
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001132 msm_hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001133 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -05001134 msm_edp_unregister();
Hai Lid5af49c2015-03-26 19:25:17 -04001135 msm_dsi_unregister();
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301136 msm_mdp_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -04001137}
1138
1139module_init(msm_drm_register);
1140module_exit(msm_drm_unregister);
1141
1142MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1143MODULE_DESCRIPTION("MSM DRM Driver");
1144MODULE_LICENSE("GPL");