]> Git Repo - linux.git/blame - drivers/gpu/drm/msm/msm_drv.c
drm/msm: add headless gpu device for imx5
[linux.git] / drivers / gpu / drm / msm / msm_drv.c
CommitLineData
c8afe684 1/*
25fdd593 2 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
c8afe684
RC
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
25fdd593
JS
19#include <linux/kthread.h>
20#include <uapi/linux/sched/types.h>
97ac0e47
RK
21#include <drm/drm_of.h>
22
c8afe684 23#include "msm_drv.h"
edcd60ce 24#include "msm_debugfs.h"
fde5de6c 25#include "msm_fence.h"
f05c83e7 26#include "msm_gem.h"
7198e6b0 27#include "msm_gpu.h"
dd2da6e3 28#include "msm_kms.h"
c2052a4e 29#include "adreno/adreno_gpu.h"
c8afe684 30
a8d854c1
RC
31
32/*
33 * MSM driver version:
34 * - 1.0.0 - initial interface
35 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
7a3bcc0a 36 * - 1.2.0 - adds explicit fence support for submit ioctl
f7de1545
JC
37 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
38 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
39 * MSM_GEM_INFO ioctl.
1fed8df3
RC
40 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
41 * GEM object's debug name
a8d854c1
RC
42 */
43#define MSM_VERSION_MAJOR 1
1fed8df3 44#define MSM_VERSION_MINOR 4
a8d854c1
RC
45#define MSM_VERSION_PATCHLEVEL 0
46
c8afe684
RC
47static const struct drm_mode_config_funcs mode_config_funcs = {
48 .fb_create = msm_framebuffer_create,
4ccbc6e5 49 .output_poll_changed = drm_fb_helper_output_poll_changed,
1f920175 50 .atomic_check = drm_atomic_helper_check,
d14659f5
SP
51 .atomic_commit = drm_atomic_helper_commit,
52};
53
54static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
55 .atomic_commit_tail = msm_atomic_commit_tail,
c8afe684
RC
56};
57
c8afe684
RC
58#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
59static bool reglog = false;
60MODULE_PARM_DESC(reglog, "Enable register read/write logging");
61module_param(reglog, bool, 0600);
62#else
63#define reglog 0
64#endif
65
a9ee34b7 66#ifdef CONFIG_DRM_FBDEV_EMULATION
e90dfec7
RC
67static bool fbdev = true;
68MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
69module_param(fbdev, bool, 0600);
70#endif
71
3a10ba8c 72static char *vram = "16m";
4313c744 73MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
871d812a
RC
74module_param(vram, charp, 0);
75
06d9f56f
RC
76bool dumpstate = false;
77MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
78module_param(dumpstate, bool, 0600);
79
ba4dd718
RC
80static bool modeset = true;
81MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
82module_param(modeset, bool, 0600);
83
060530f1
RC
84/*
85 * Util/helpers:
86 */
87
8e54eea5
JC
88int msm_clk_bulk_get(struct device *dev, struct clk_bulk_data **bulk)
89{
90 struct property *prop;
91 const char *name;
92 struct clk_bulk_data *local;
93 int i = 0, ret, count;
94
95 count = of_property_count_strings(dev->of_node, "clock-names");
96 if (count < 1)
97 return 0;
98
99 local = devm_kcalloc(dev, sizeof(struct clk_bulk_data *),
100 count, GFP_KERNEL);
101 if (!local)
102 return -ENOMEM;
103
104 of_property_for_each_string(dev->of_node, "clock-names", prop, name) {
105 local[i].id = devm_kstrdup(dev, name, GFP_KERNEL);
106 if (!local[i].id) {
107 devm_kfree(dev, local);
108 return -ENOMEM;
109 }
110
111 i++;
112 }
113
114 ret = devm_clk_bulk_get(dev, count, local);
115
116 if (ret) {
117 for (i = 0; i < count; i++)
118 devm_kfree(dev, (void *) local[i].id);
119 devm_kfree(dev, local);
120
121 return ret;
122 }
123
124 *bulk = local;
125 return count;
126}
127
128struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
129 const char *name)
130{
131 int i;
132 char n[32];
133
134 snprintf(n, sizeof(n), "%s_clk", name);
135
136 for (i = 0; bulk && i < count; i++) {
137 if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n))
138 return bulk[i].clk;
139 }
140
141
142 return NULL;
143}
144
720c3bb8
RC
145struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
146{
147 struct clk *clk;
148 char name2[32];
149
150 clk = devm_clk_get(&pdev->dev, name);
151 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
152 return clk;
153
154 snprintf(name2, sizeof(name2), "%s_clk", name);
155
156 clk = devm_clk_get(&pdev->dev, name2);
157 if (!IS_ERR(clk))
158 dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
159 "\"%s\" instead of \"%s\"\n", name, name2);
160
161 return clk;
162}
163
c8afe684
RC
164void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
165 const char *dbgname)
166{
167 struct resource *res;
168 unsigned long size;
169 void __iomem *ptr;
170
171 if (name)
172 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
173 else
174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175
176 if (!res) {
6a41da17 177 DRM_DEV_ERROR(&pdev->dev, "failed to get memory resource: %s\n", name);
c8afe684
RC
178 return ERR_PTR(-EINVAL);
179 }
180
181 size = resource_size(res);
182
183 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
184 if (!ptr) {
6a41da17 185 DRM_DEV_ERROR(&pdev->dev, "failed to ioremap: %s\n", name);
c8afe684
RC
186 return ERR_PTR(-ENOMEM);
187 }
188
189 if (reglog)
fc99f97a 190 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
c8afe684
RC
191
192 return ptr;
193}
194
195void msm_writel(u32 data, void __iomem *addr)
196{
197 if (reglog)
fc99f97a 198 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
c8afe684
RC
199 writel(data, addr);
200}
201
202u32 msm_readl(const void __iomem *addr)
203{
204 u32 val = readl(addr);
205 if (reglog)
8dfe162a 206 pr_err("IO:R %p %08x\n", addr, val);
c8afe684
RC
207 return val;
208}
209
78b1d470
HL
210struct vblank_event {
211 struct list_head node;
212 int crtc_id;
213 bool enable;
214};
215
25fdd593 216static void vblank_ctrl_worker(struct kthread_work *work)
78b1d470
HL
217{
218 struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
219 struct msm_vblank_ctrl, work);
220 struct msm_drm_private *priv = container_of(vbl_ctrl,
221 struct msm_drm_private, vblank_ctrl);
222 struct msm_kms *kms = priv->kms;
223 struct vblank_event *vbl_ev, *tmp;
224 unsigned long flags;
225
226 spin_lock_irqsave(&vbl_ctrl->lock, flags);
227 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
228 list_del(&vbl_ev->node);
229 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
230
231 if (vbl_ev->enable)
232 kms->funcs->enable_vblank(kms,
233 priv->crtcs[vbl_ev->crtc_id]);
234 else
235 kms->funcs->disable_vblank(kms,
236 priv->crtcs[vbl_ev->crtc_id]);
237
238 kfree(vbl_ev);
239
240 spin_lock_irqsave(&vbl_ctrl->lock, flags);
241 }
242
243 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
244}
245
246static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
247 int crtc_id, bool enable)
248{
249 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
250 struct vblank_event *vbl_ev;
251 unsigned long flags;
252
253 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
254 if (!vbl_ev)
255 return -ENOMEM;
256
257 vbl_ev->crtc_id = crtc_id;
258 vbl_ev->enable = enable;
259
260 spin_lock_irqsave(&vbl_ctrl->lock, flags);
261 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
262 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
263
25fdd593
JS
264 kthread_queue_work(&priv->disp_thread[crtc_id].worker,
265 &vbl_ctrl->work);
78b1d470
HL
266
267 return 0;
268}
269
2b669875 270static int msm_drm_uninit(struct device *dev)
c8afe684 271{
2b669875
AT
272 struct platform_device *pdev = to_platform_device(dev);
273 struct drm_device *ddev = platform_get_drvdata(pdev);
274 struct msm_drm_private *priv = ddev->dev_private;
c8afe684 275 struct msm_kms *kms = priv->kms;
bc3220be 276 struct msm_mdss *mdss = priv->mdss;
78b1d470
HL
277 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
278 struct vblank_event *vbl_ev, *tmp;
25fdd593 279 int i;
78b1d470
HL
280
281 /* We must cancel and cleanup any pending vblank enable/disable
282 * work before drm_irq_uninstall() to avoid work re-enabling an
283 * irq after uninstall has disabled it.
284 */
25fdd593 285 kthread_flush_work(&vbl_ctrl->work);
78b1d470
HL
286 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
287 list_del(&vbl_ev->node);
288 kfree(vbl_ev);
289 }
c8afe684 290
25fdd593
JS
291 /* clean up display commit/event worker threads */
292 for (i = 0; i < priv->num_crtcs; i++) {
293 if (priv->disp_thread[i].thread) {
294 kthread_flush_worker(&priv->disp_thread[i].worker);
295 kthread_stop(priv->disp_thread[i].thread);
296 priv->disp_thread[i].thread = NULL;
297 }
298
299 if (priv->event_thread[i].thread) {
300 kthread_flush_worker(&priv->event_thread[i].worker);
301 kthread_stop(priv->event_thread[i].thread);
302 priv->event_thread[i].thread = NULL;
303 }
304 }
305
68209390
RC
306 msm_gem_shrinker_cleanup(ddev);
307
2b669875
AT
308 drm_kms_helper_poll_fini(ddev);
309
2b669875 310 drm_dev_unregister(ddev);
8208ed93 311
85eac470
NT
312 msm_perf_debugfs_cleanup(priv);
313 msm_rd_debugfs_cleanup(priv);
314
1aaa57f5
AT
315#ifdef CONFIG_DRM_FBDEV_EMULATION
316 if (fbdev && priv->fbdev)
2b669875 317 msm_fbdev_free(ddev);
1aaa57f5 318#endif
3ea4b1e1 319 drm_atomic_helper_shutdown(ddev);
2b669875 320 drm_mode_config_cleanup(ddev);
c8afe684 321
2b669875
AT
322 pm_runtime_get_sync(dev);
323 drm_irq_uninstall(ddev);
324 pm_runtime_put_sync(dev);
c8afe684
RC
325
326 flush_workqueue(priv->wq);
327 destroy_workqueue(priv->wq);
328
16976085 329 if (kms && kms->funcs)
c8afe684 330 kms->funcs->destroy(kms);
c8afe684 331
871d812a 332 if (priv->vram.paddr) {
00085f1e 333 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
871d812a 334 drm_mm_takedown(&priv->vram.mm);
2b669875 335 dma_free_attrs(dev, priv->vram.size, NULL,
00085f1e 336 priv->vram.paddr, attrs);
871d812a
RC
337 }
338
2b669875 339 component_unbind_all(dev, ddev);
060530f1 340
bc3220be
RY
341 if (mdss && mdss->funcs)
342 mdss->funcs->destroy(ddev);
0a6030d2 343
2b669875 344 ddev->dev_private = NULL;
4d8dc2df 345 drm_dev_put(ddev);
c8afe684
RC
346
347 kfree(priv);
348
349 return 0;
350}
351
aaded2e3
JS
352#define KMS_MDP4 4
353#define KMS_MDP5 5
25fdd593 354#define KMS_DPU 3
aaded2e3 355
06c0dd96
RC
356static int get_mdp_ver(struct platform_device *pdev)
357{
06c0dd96 358 struct device *dev = &pdev->dev;
e9fbdaf2
AT
359
360 return (int) (unsigned long) of_device_get_match_data(dev);
06c0dd96
RC
361}
362
072f1f91
RC
363#include <linux/of_address.h>
364
c2052a4e
JM
365bool msm_use_mmu(struct drm_device *dev)
366{
367 struct msm_drm_private *priv = dev->dev_private;
368
369 /* a2xx comes with its own MMU */
370 return priv->is_a2xx || iommu_present(&platform_bus_type);
371}
372
5bf9c0b6 373static int msm_init_vram(struct drm_device *dev)
c8afe684 374{
5bf9c0b6 375 struct msm_drm_private *priv = dev->dev_private;
e9fbdaf2 376 struct device_node *node;
072f1f91
RC
377 unsigned long size = 0;
378 int ret = 0;
379
072f1f91
RC
380 /* In the device-tree world, we could have a 'memory-region'
381 * phandle, which gives us a link to our "vram". Allocating
382 * is all nicely abstracted behind the dma api, but we need
383 * to know the entire size to allocate it all in one go. There
384 * are two cases:
385 * 1) device with no IOMMU, in which case we need exclusive
386 * access to a VRAM carveout big enough for all gpu
387 * buffers
388 * 2) device with IOMMU, but where the bootloader puts up
389 * a splash screen. In this case, the VRAM carveout
390 * need only be large enough for fbdev fb. But we need
391 * exclusive access to the buffer to avoid the kernel
392 * using those pages for other purposes (which appears
393 * as corruption on screen before we have a chance to
394 * load and do initial modeset)
395 */
072f1f91
RC
396
397 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
398 if (node) {
399 struct resource r;
400 ret = of_address_to_resource(node, 0, &r);
2ca41c17 401 of_node_put(node);
072f1f91
RC
402 if (ret)
403 return ret;
404 size = r.end - r.start;
fc99f97a 405 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
c8afe684 406
e9fbdaf2
AT
407 /* if we have no IOMMU, then we need to use carveout allocator.
408 * Grab the entire CMA chunk carved out in early startup in
409 * mach-msm:
410 */
c2052a4e 411 } else if (!msm_use_mmu(dev)) {
072f1f91
RC
412 DRM_INFO("using %s VRAM carveout\n", vram);
413 size = memparse(vram, NULL);
414 }
415
416 if (size) {
00085f1e 417 unsigned long attrs = 0;
871d812a
RC
418 void *p;
419
871d812a
RC
420 priv->vram.size = size;
421
422 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
0e08270a 423 spin_lock_init(&priv->vram.lock);
871d812a 424
00085f1e
KK
425 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
426 attrs |= DMA_ATTR_WRITE_COMBINE;
871d812a
RC
427
428 /* note that for no-kernel-mapping, the vaddr returned
429 * is bogus, but non-null if allocation succeeded:
430 */
431 p = dma_alloc_attrs(dev->dev, size,
00085f1e 432 &priv->vram.paddr, GFP_KERNEL, attrs);
871d812a 433 if (!p) {
6a41da17 434 DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
871d812a 435 priv->vram.paddr = 0;
5bf9c0b6 436 return -ENOMEM;
871d812a
RC
437 }
438
6a41da17 439 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
871d812a
RC
440 (uint32_t)priv->vram.paddr,
441 (uint32_t)(priv->vram.paddr + size));
442 }
443
072f1f91 444 return ret;
5bf9c0b6
RC
445}
446
2b669875 447static int msm_drm_init(struct device *dev, struct drm_driver *drv)
5bf9c0b6 448{
2b669875
AT
449 struct platform_device *pdev = to_platform_device(dev);
450 struct drm_device *ddev;
5bf9c0b6
RC
451 struct msm_drm_private *priv;
452 struct msm_kms *kms;
bc3220be 453 struct msm_mdss *mdss;
25fdd593
JS
454 int ret, i;
455 struct sched_param param;
5bf9c0b6 456
2b669875 457 ddev = drm_dev_alloc(drv, dev);
0f288605 458 if (IS_ERR(ddev)) {
6a41da17 459 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
0f288605 460 return PTR_ERR(ddev);
2b669875
AT
461 }
462
463 platform_set_drvdata(pdev, ddev);
2b669875 464
5bf9c0b6
RC
465 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
466 if (!priv) {
77050c3f 467 ret = -ENOMEM;
4d8dc2df 468 goto err_put_drm_dev;
5bf9c0b6
RC
469 }
470
2b669875 471 ddev->dev_private = priv;
68209390 472 priv->dev = ddev;
5bf9c0b6 473
25fdd593
JS
474 switch (get_mdp_ver(pdev)) {
475 case KMS_MDP5:
476 ret = mdp5_mdss_init(ddev);
477 break;
478 case KMS_DPU:
479 ret = dpu_mdss_init(ddev);
480 break;
481 default:
482 ret = 0;
483 break;
484 }
77050c3f
JS
485 if (ret)
486 goto err_free_priv;
0a6030d2 487
bc3220be
RY
488 mdss = priv->mdss;
489
5bf9c0b6 490 priv->wq = alloc_ordered_workqueue("msm", 0);
5bf9c0b6
RC
491
492 INIT_LIST_HEAD(&priv->inactive_list);
78b1d470 493 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
25fdd593 494 kthread_init_work(&priv->vblank_ctrl.work, vblank_ctrl_worker);
78b1d470 495 spin_lock_init(&priv->vblank_ctrl.lock);
5bf9c0b6 496
2b669875 497 drm_mode_config_init(ddev);
060530f1
RC
498
499 /* Bind all our sub-components: */
2b669875 500 ret = component_bind_all(dev, ddev);
77050c3f
JS
501 if (ret)
502 goto err_destroy_mdss;
060530f1 503
2b669875 504 ret = msm_init_vram(ddev);
13f15565 505 if (ret)
77050c3f 506 goto err_msm_uninit;
13f15565 507
68209390
RC
508 msm_gem_shrinker_init(ddev);
509
06c0dd96 510 switch (get_mdp_ver(pdev)) {
aaded2e3 511 case KMS_MDP4:
2b669875 512 kms = mdp4_kms_init(ddev);
0a6030d2 513 priv->kms = kms;
06c0dd96 514 break;
aaded2e3 515 case KMS_MDP5:
392ae6e0 516 kms = mdp5_kms_init(ddev);
06c0dd96 517 break;
25fdd593
JS
518 case KMS_DPU:
519 kms = dpu_kms_init(ddev);
520 priv->kms = kms;
521 break;
06c0dd96 522 default:
e6f6d63e
JM
523 /* valid only for the dummy headless case, where of_node=NULL */
524 WARN_ON(dev->of_node);
525 kms = NULL;
06c0dd96
RC
526 break;
527 }
528
c8afe684 529 if (IS_ERR(kms)) {
6a41da17 530 DRM_DEV_ERROR(dev, "failed to load kms\n");
e4826a94 531 ret = PTR_ERR(kms);
b2ccfdf1 532 priv->kms = NULL;
77050c3f 533 goto err_msm_uninit;
c8afe684
RC
534 }
535
bb676df1
JS
536 /* Enable normalization of plane zpos */
537 ddev->mode_config.normalize_zpos = true;
538
c8afe684 539 if (kms) {
c8afe684
RC
540 ret = kms->funcs->hw_init(kms);
541 if (ret) {
6a41da17 542 DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
77050c3f 543 goto err_msm_uninit;
c8afe684
RC
544 }
545 }
546
2b669875 547 ddev->mode_config.funcs = &mode_config_funcs;
d14659f5 548 ddev->mode_config.helper_private = &mode_config_helper_funcs;
c8afe684 549
25fdd593
JS
550 /**
551 * this priority was found during empiric testing to have appropriate
552 * realtime scheduling to process display updates and interact with
553 * other real time and normal priority task
554 */
555 param.sched_priority = 16;
556 for (i = 0; i < priv->num_crtcs; i++) {
557
558 /* initialize display thread */
559 priv->disp_thread[i].crtc_id = priv->crtcs[i]->base.id;
560 kthread_init_worker(&priv->disp_thread[i].worker);
561 priv->disp_thread[i].dev = ddev;
562 priv->disp_thread[i].thread =
563 kthread_run(kthread_worker_fn,
564 &priv->disp_thread[i].worker,
565 "crtc_commit:%d", priv->disp_thread[i].crtc_id);
566 ret = sched_setscheduler(priv->disp_thread[i].thread,
567 SCHED_FIFO, &param);
568 if (ret)
569 pr_warn("display thread priority update failed: %d\n",
570 ret);
571
572 if (IS_ERR(priv->disp_thread[i].thread)) {
6a41da17 573 DRM_DEV_ERROR(dev, "failed to create crtc_commit kthread\n");
25fdd593
JS
574 priv->disp_thread[i].thread = NULL;
575 }
576
577 /* initialize event thread */
578 priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
579 kthread_init_worker(&priv->event_thread[i].worker);
580 priv->event_thread[i].dev = ddev;
581 priv->event_thread[i].thread =
582 kthread_run(kthread_worker_fn,
583 &priv->event_thread[i].worker,
584 "crtc_event:%d", priv->event_thread[i].crtc_id);
6a41da17 585
25fdd593
JS
586 /**
587 * event thread should also run at same priority as disp_thread
588 * because it is handling frame_done events. A lower priority
589 * event thread and higher priority disp_thread can causes
590 * frame_pending counters beyond 2. This can lead to commit
591 * failure at crtc commit level.
592 */
593 ret = sched_setscheduler(priv->event_thread[i].thread,
594 SCHED_FIFO, &param);
595 if (ret)
596 pr_warn("display event thread priority update failed: %d\n",
597 ret);
598
599 if (IS_ERR(priv->event_thread[i].thread)) {
600 dev_err(dev, "failed to create crtc_event kthread\n");
601 priv->event_thread[i].thread = NULL;
602 }
603
604 if ((!priv->disp_thread[i].thread) ||
605 !priv->event_thread[i].thread) {
606 /* clean up previously created threads if any */
607 for ( ; i >= 0; i--) {
608 if (priv->disp_thread[i].thread) {
609 kthread_stop(
610 priv->disp_thread[i].thread);
611 priv->disp_thread[i].thread = NULL;
612 }
613
614 if (priv->event_thread[i].thread) {
615 kthread_stop(
616 priv->event_thread[i].thread);
617 priv->event_thread[i].thread = NULL;
618 }
619 }
620 goto err_msm_uninit;
621 }
622 }
623
2b669875 624 ret = drm_vblank_init(ddev, priv->num_crtcs);
c8afe684 625 if (ret < 0) {
6a41da17 626 DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
77050c3f 627 goto err_msm_uninit;
c8afe684
RC
628 }
629
a2b3a557
AT
630 if (kms) {
631 pm_runtime_get_sync(dev);
632 ret = drm_irq_install(ddev, kms->irq);
633 pm_runtime_put_sync(dev);
634 if (ret < 0) {
6a41da17 635 DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
77050c3f 636 goto err_msm_uninit;
a2b3a557 637 }
c8afe684
RC
638 }
639
2b669875
AT
640 ret = drm_dev_register(ddev, 0);
641 if (ret)
77050c3f 642 goto err_msm_uninit;
2b669875 643
2b669875 644 drm_mode_config_reset(ddev);
cf3a7e4c 645
a9ee34b7 646#ifdef CONFIG_DRM_FBDEV_EMULATION
e6f6d63e 647 if (kms && fbdev)
2b669875 648 priv->fbdev = msm_fbdev_init(ddev);
c8afe684
RC
649#endif
650
2b669875 651 ret = msm_debugfs_late_init(ddev);
a7d3c950 652 if (ret)
77050c3f 653 goto err_msm_uninit;
a7d3c950 654
2b669875 655 drm_kms_helper_poll_init(ddev);
c8afe684
RC
656
657 return 0;
658
77050c3f 659err_msm_uninit:
2b669875 660 msm_drm_uninit(dev);
c8afe684 661 return ret;
77050c3f
JS
662err_destroy_mdss:
663 if (mdss && mdss->funcs)
664 mdss->funcs->destroy(ddev);
665err_free_priv:
666 kfree(priv);
4d8dc2df
TZ
667err_put_drm_dev:
668 drm_dev_put(ddev);
77050c3f 669 return ret;
c8afe684
RC
670}
671
2b669875
AT
672/*
673 * DRM operations:
674 */
675
7198e6b0
RC
676static void load_gpu(struct drm_device *dev)
677{
a1ad3523 678 static DEFINE_MUTEX(init_lock);
7198e6b0 679 struct msm_drm_private *priv = dev->dev_private;
7198e6b0 680
a1ad3523
RC
681 mutex_lock(&init_lock);
682
e2550b7a
RC
683 if (!priv->gpu)
684 priv->gpu = adreno_load_gpu(dev);
7198e6b0 685
a1ad3523 686 mutex_unlock(&init_lock);
7198e6b0
RC
687}
688
f97decac 689static int context_init(struct drm_device *dev, struct drm_file *file)
7198e6b0
RC
690{
691 struct msm_file_private *ctx;
692
7198e6b0
RC
693 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
694 if (!ctx)
695 return -ENOMEM;
696
f97decac 697 msm_submitqueue_init(dev, ctx);
f7de1545 698
7198e6b0
RC
699 file->driver_priv = ctx;
700
701 return 0;
702}
703
f7de1545
JC
704static int msm_open(struct drm_device *dev, struct drm_file *file)
705{
706 /* For now, load gpu on open.. to avoid the requirement of having
707 * firmware in the initrd.
708 */
709 load_gpu(dev);
710
f97decac 711 return context_init(dev, file);
f7de1545
JC
712}
713
714static void context_close(struct msm_file_private *ctx)
715{
716 msm_submitqueue_close(ctx);
717 kfree(ctx);
718}
719
94df145c 720static void msm_postclose(struct drm_device *dev, struct drm_file *file)
c8afe684
RC
721{
722 struct msm_drm_private *priv = dev->dev_private;
7198e6b0 723 struct msm_file_private *ctx = file->driver_priv;
7198e6b0 724
7198e6b0
RC
725 mutex_lock(&dev->struct_mutex);
726 if (ctx == priv->lastctx)
727 priv->lastctx = NULL;
728 mutex_unlock(&dev->struct_mutex);
729
f7de1545 730 context_close(ctx);
c8afe684
RC
731}
732
e9f0d76f 733static irqreturn_t msm_irq(int irq, void *arg)
c8afe684
RC
734{
735 struct drm_device *dev = arg;
736 struct msm_drm_private *priv = dev->dev_private;
737 struct msm_kms *kms = priv->kms;
738 BUG_ON(!kms);
739 return kms->funcs->irq(kms);
740}
741
742static void msm_irq_preinstall(struct drm_device *dev)
743{
744 struct msm_drm_private *priv = dev->dev_private;
745 struct msm_kms *kms = priv->kms;
746 BUG_ON(!kms);
747 kms->funcs->irq_preinstall(kms);
748}
749
750static int msm_irq_postinstall(struct drm_device *dev)
751{
752 struct msm_drm_private *priv = dev->dev_private;
753 struct msm_kms *kms = priv->kms;
754 BUG_ON(!kms);
755 return kms->funcs->irq_postinstall(kms);
756}
757
758static void msm_irq_uninstall(struct drm_device *dev)
759{
760 struct msm_drm_private *priv = dev->dev_private;
761 struct msm_kms *kms = priv->kms;
762 BUG_ON(!kms);
763 kms->funcs->irq_uninstall(kms);
764}
765
88e72717 766static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
c8afe684
RC
767{
768 struct msm_drm_private *priv = dev->dev_private;
769 struct msm_kms *kms = priv->kms;
770 if (!kms)
771 return -ENXIO;
88e72717
TR
772 DBG("dev=%p, crtc=%u", dev, pipe);
773 return vblank_ctrl_queue_work(priv, pipe, true);
c8afe684
RC
774}
775
88e72717 776static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
c8afe684
RC
777{
778 struct msm_drm_private *priv = dev->dev_private;
779 struct msm_kms *kms = priv->kms;
780 if (!kms)
781 return;
88e72717
TR
782 DBG("dev=%p, crtc=%u", dev, pipe);
783 vblank_ctrl_queue_work(priv, pipe, false);
c8afe684
RC
784}
785
7198e6b0
RC
786/*
787 * DRM ioctls:
788 */
789
790static int msm_ioctl_get_param(struct drm_device *dev, void *data,
791 struct drm_file *file)
792{
793 struct msm_drm_private *priv = dev->dev_private;
794 struct drm_msm_param *args = data;
795 struct msm_gpu *gpu;
796
797 /* for now, we just have 3d pipe.. eventually this would need to
798 * be more clever to dispatch to appropriate gpu module:
799 */
800 if (args->pipe != MSM_PIPE_3D0)
801 return -EINVAL;
802
803 gpu = priv->gpu;
804
805 if (!gpu)
806 return -ENXIO;
807
808 return gpu->funcs->get_param(gpu, args->param, &args->value);
809}
810
811static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
812 struct drm_file *file)
813{
814 struct drm_msm_gem_new *args = data;
93ddb0d3
RC
815
816 if (args->flags & ~MSM_BO_FLAGS) {
817 DRM_ERROR("invalid flags: %08x\n", args->flags);
818 return -EINVAL;
819 }
820
7198e6b0 821 return msm_gem_new_handle(dev, file, args->size,
0815d774 822 args->flags, &args->handle, NULL);
7198e6b0
RC
823}
824
56c2da83
RC
825static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
826{
827 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
828}
7198e6b0
RC
829
830static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
831 struct drm_file *file)
832{
833 struct drm_msm_gem_cpu_prep *args = data;
834 struct drm_gem_object *obj;
56c2da83 835 ktime_t timeout = to_ktime(args->timeout);
7198e6b0
RC
836 int ret;
837
93ddb0d3
RC
838 if (args->op & ~MSM_PREP_FLAGS) {
839 DRM_ERROR("invalid op: %08x\n", args->op);
840 return -EINVAL;
841 }
842
a8ad0bd8 843 obj = drm_gem_object_lookup(file, args->handle);
7198e6b0
RC
844 if (!obj)
845 return -ENOENT;
846
56c2da83 847 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
7198e6b0 848
dc9a9b32 849 drm_gem_object_put_unlocked(obj);
7198e6b0
RC
850
851 return ret;
852}
853
854static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
855 struct drm_file *file)
856{
857 struct drm_msm_gem_cpu_fini *args = data;
858 struct drm_gem_object *obj;
859 int ret;
860
a8ad0bd8 861 obj = drm_gem_object_lookup(file, args->handle);
7198e6b0
RC
862 if (!obj)
863 return -ENOENT;
864
865 ret = msm_gem_cpu_fini(obj);
866
dc9a9b32 867 drm_gem_object_put_unlocked(obj);
7198e6b0
RC
868
869 return ret;
870}
871
49fd08ba
JC
872static int msm_ioctl_gem_info_iova(struct drm_device *dev,
873 struct drm_gem_object *obj, uint64_t *iova)
874{
875 struct msm_drm_private *priv = dev->dev_private;
876
877 if (!priv->gpu)
878 return -EINVAL;
879
9fe041f6
JC
880 /*
881 * Don't pin the memory here - just get an address so that userspace can
882 * be productive
883 */
8bdcd949 884 return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
49fd08ba
JC
885}
886
7198e6b0
RC
887static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
888 struct drm_file *file)
889{
890 struct drm_msm_gem_info *args = data;
891 struct drm_gem_object *obj;
f05c83e7
RC
892 struct msm_gem_object *msm_obj;
893 int i, ret = 0;
7198e6b0 894
789d2e5a 895 if (args->pad)
7198e6b0
RC
896 return -EINVAL;
897
789d2e5a
RC
898 switch (args->info) {
899 case MSM_INFO_GET_OFFSET:
900 case MSM_INFO_GET_IOVA:
901 /* value returned as immediate, not pointer, so len==0: */
902 if (args->len)
903 return -EINVAL;
904 break;
f05c83e7
RC
905 case MSM_INFO_SET_NAME:
906 case MSM_INFO_GET_NAME:
907 break;
789d2e5a
RC
908 default:
909 return -EINVAL;
910 }
911
a8ad0bd8 912 obj = drm_gem_object_lookup(file, args->handle);
7198e6b0
RC
913 if (!obj)
914 return -ENOENT;
915
f05c83e7
RC
916 msm_obj = to_msm_bo(obj);
917
789d2e5a
RC
918 switch (args->info) {
919 case MSM_INFO_GET_OFFSET:
920 args->value = msm_gem_mmap_offset(obj);
921 break;
922 case MSM_INFO_GET_IOVA:
923 ret = msm_ioctl_gem_info_iova(dev, obj, &args->value);
924 break;
f05c83e7
RC
925 case MSM_INFO_SET_NAME:
926 /* length check should leave room for terminating null: */
927 if (args->len >= sizeof(msm_obj->name)) {
928 ret = -EINVAL;
929 break;
930 }
931 ret = copy_from_user(msm_obj->name,
932 u64_to_user_ptr(args->value), args->len);
933 msm_obj->name[args->len] = '\0';
934 for (i = 0; i < args->len; i++) {
935 if (!isprint(msm_obj->name[i])) {
936 msm_obj->name[i] = '\0';
937 break;
938 }
939 }
940 break;
941 case MSM_INFO_GET_NAME:
942 if (args->value && (args->len < strlen(msm_obj->name))) {
943 ret = -EINVAL;
944 break;
945 }
946 args->len = strlen(msm_obj->name);
947 if (args->value) {
948 ret = copy_to_user(u64_to_user_ptr(args->value),
949 msm_obj->name, args->len);
950 }
951 break;
49fd08ba 952 }
7198e6b0 953
dc9a9b32 954 drm_gem_object_put_unlocked(obj);
7198e6b0
RC
955
956 return ret;
957}
958
959static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
960 struct drm_file *file)
961{
ca762a8a 962 struct msm_drm_private *priv = dev->dev_private;
7198e6b0 963 struct drm_msm_wait_fence *args = data;
56c2da83 964 ktime_t timeout = to_ktime(args->timeout);
f97decac
JC
965 struct msm_gpu_submitqueue *queue;
966 struct msm_gpu *gpu = priv->gpu;
967 int ret;
93ddb0d3
RC
968
969 if (args->pad) {
970 DRM_ERROR("invalid pad: %08x\n", args->pad);
971 return -EINVAL;
972 }
973
f97decac 974 if (!gpu)
ca762a8a
RC
975 return 0;
976
f97decac
JC
977 queue = msm_submitqueue_get(file->driver_priv, args->queueid);
978 if (!queue)
979 return -ENOENT;
980
981 ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
982 true);
983
984 msm_submitqueue_put(queue);
985 return ret;
7198e6b0
RC
986}
987
4cd33c48
RC
988static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
989 struct drm_file *file)
990{
991 struct drm_msm_gem_madvise *args = data;
992 struct drm_gem_object *obj;
993 int ret;
994
995 switch (args->madv) {
996 case MSM_MADV_DONTNEED:
997 case MSM_MADV_WILLNEED:
998 break;
999 default:
1000 return -EINVAL;
1001 }
1002
1003 ret = mutex_lock_interruptible(&dev->struct_mutex);
1004 if (ret)
1005 return ret;
1006
1007 obj = drm_gem_object_lookup(file, args->handle);
1008 if (!obj) {
1009 ret = -ENOENT;
1010 goto unlock;
1011 }
1012
1013 ret = msm_gem_madvise(obj, args->madv);
1014 if (ret >= 0) {
1015 args->retained = ret;
1016 ret = 0;
1017 }
1018
dc9a9b32 1019 drm_gem_object_put(obj);
4cd33c48
RC
1020
1021unlock:
1022 mutex_unlock(&dev->struct_mutex);
1023 return ret;
1024}
1025
f7de1545
JC
1026
1027static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
1028 struct drm_file *file)
1029{
1030 struct drm_msm_submitqueue *args = data;
1031
1032 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
1033 return -EINVAL;
1034
f97decac 1035 return msm_submitqueue_create(dev, file->driver_priv, args->prio,
f7de1545
JC
1036 args->flags, &args->id);
1037}
1038
1039
1040static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
1041 struct drm_file *file)
1042{
1043 u32 id = *(u32 *) data;
1044
1045 return msm_submitqueue_remove(file->driver_priv, id);
1046}
1047
7198e6b0 1048static const struct drm_ioctl_desc msm_ioctls[] = {
f8c47144
SV
1049 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
1050 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
1051 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
1052 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
1053 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
1054 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
1055 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
4cd33c48 1056 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
f7de1545
JC
1057 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_AUTH|DRM_RENDER_ALLOW),
1058 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_AUTH|DRM_RENDER_ALLOW),
7198e6b0
RC
1059};
1060
c8afe684
RC
1061static const struct vm_operations_struct vm_ops = {
1062 .fault = msm_gem_fault,
1063 .open = drm_gem_vm_open,
1064 .close = drm_gem_vm_close,
1065};
1066
1067static const struct file_operations fops = {
1068 .owner = THIS_MODULE,
1069 .open = drm_open,
1070 .release = drm_release,
1071 .unlocked_ioctl = drm_ioctl,
c8afe684 1072 .compat_ioctl = drm_compat_ioctl,
c8afe684
RC
1073 .poll = drm_poll,
1074 .read = drm_read,
1075 .llseek = no_llseek,
1076 .mmap = msm_gem_mmap,
1077};
1078
1079static struct drm_driver msm_driver = {
05b84911
RC
1080 .driver_features = DRIVER_HAVE_IRQ |
1081 DRIVER_GEM |
1082 DRIVER_PRIME |
b4b15c86 1083 DRIVER_RENDER |
a5436e1d 1084 DRIVER_ATOMIC |
05b84911 1085 DRIVER_MODESET,
7198e6b0 1086 .open = msm_open,
94df145c 1087 .postclose = msm_postclose,
4ccbc6e5 1088 .lastclose = drm_fb_helper_lastclose,
c8afe684
RC
1089 .irq_handler = msm_irq,
1090 .irq_preinstall = msm_irq_preinstall,
1091 .irq_postinstall = msm_irq_postinstall,
1092 .irq_uninstall = msm_irq_uninstall,
c8afe684
RC
1093 .enable_vblank = msm_enable_vblank,
1094 .disable_vblank = msm_disable_vblank,
1095 .gem_free_object = msm_gem_free_object,
1096 .gem_vm_ops = &vm_ops,
1097 .dumb_create = msm_gem_dumb_create,
1098 .dumb_map_offset = msm_gem_dumb_map_offset,
05b84911
RC
1099 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1100 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1101 .gem_prime_export = drm_gem_prime_export,
1102 .gem_prime_import = drm_gem_prime_import,
43523eba 1103 .gem_prime_res_obj = msm_gem_prime_res_obj,
05b84911
RC
1104 .gem_prime_pin = msm_gem_prime_pin,
1105 .gem_prime_unpin = msm_gem_prime_unpin,
1106 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
1107 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
1108 .gem_prime_vmap = msm_gem_prime_vmap,
1109 .gem_prime_vunmap = msm_gem_prime_vunmap,
77a147e7 1110 .gem_prime_mmap = msm_gem_prime_mmap,
c8afe684
RC
1111#ifdef CONFIG_DEBUG_FS
1112 .debugfs_init = msm_debugfs_init,
c8afe684 1113#endif
7198e6b0 1114 .ioctls = msm_ioctls,
167b606a 1115 .num_ioctls = ARRAY_SIZE(msm_ioctls),
c8afe684
RC
1116 .fops = &fops,
1117 .name = "msm",
1118 .desc = "MSM Snapdragon DRM",
1119 .date = "20130625",
a8d854c1
RC
1120 .major = MSM_VERSION_MAJOR,
1121 .minor = MSM_VERSION_MINOR,
1122 .patchlevel = MSM_VERSION_PATCHLEVEL,
c8afe684
RC
1123};
1124
1125#ifdef CONFIG_PM_SLEEP
1126static int msm_pm_suspend(struct device *dev)
1127{
1128 struct drm_device *ddev = dev_get_drvdata(dev);
ec446d09 1129 struct msm_drm_private *priv = ddev->dev_private;
c8afe684 1130
3750e78c
BW
1131 if (WARN_ON(priv->pm_state))
1132 drm_atomic_state_put(priv->pm_state);
c8afe684 1133
ec446d09
DM
1134 priv->pm_state = drm_atomic_helper_suspend(ddev);
1135 if (IS_ERR(priv->pm_state)) {
3750e78c
BW
1136 int ret = PTR_ERR(priv->pm_state);
1137 DRM_ERROR("Failed to suspend dpu, %d\n", ret);
1138 return ret;
ec446d09
DM
1139 }
1140
c8afe684
RC
1141 return 0;
1142}
1143
1144static int msm_pm_resume(struct device *dev)
1145{
1146 struct drm_device *ddev = dev_get_drvdata(dev);
ec446d09 1147 struct msm_drm_private *priv = ddev->dev_private;
3750e78c 1148 int ret;
036bfeb3 1149
3750e78c
BW
1150 if (WARN_ON(!priv->pm_state))
1151 return -ENOENT;
c8afe684 1152
3750e78c
BW
1153 ret = drm_atomic_helper_resume(ddev, priv->pm_state);
1154 if (!ret)
1155 priv->pm_state = NULL;
c8afe684 1156
3750e78c 1157 return ret;
c8afe684
RC
1158}
1159#endif
1160
774e39ee
AT
1161#ifdef CONFIG_PM
1162static int msm_runtime_suspend(struct device *dev)
1163{
1164 struct drm_device *ddev = dev_get_drvdata(dev);
1165 struct msm_drm_private *priv = ddev->dev_private;
bc3220be 1166 struct msm_mdss *mdss = priv->mdss;
774e39ee
AT
1167
1168 DBG("");
1169
bc3220be
RY
1170 if (mdss && mdss->funcs)
1171 return mdss->funcs->disable(mdss);
774e39ee
AT
1172
1173 return 0;
1174}
1175
1176static int msm_runtime_resume(struct device *dev)
1177{
1178 struct drm_device *ddev = dev_get_drvdata(dev);
1179 struct msm_drm_private *priv = ddev->dev_private;
bc3220be 1180 struct msm_mdss *mdss = priv->mdss;
774e39ee
AT
1181
1182 DBG("");
1183
bc3220be
RY
1184 if (mdss && mdss->funcs)
1185 return mdss->funcs->enable(mdss);
774e39ee
AT
1186
1187 return 0;
1188}
1189#endif
1190
c8afe684
RC
1191static const struct dev_pm_ops msm_pm_ops = {
1192 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
774e39ee 1193 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
c8afe684
RC
1194};
1195
060530f1
RC
1196/*
1197 * Componentized driver support:
1198 */
1199
e9fbdaf2
AT
1200/*
1201 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1202 * so probably some room for some helpers
060530f1
RC
1203 */
1204static int compare_of(struct device *dev, void *data)
1205{
1206 return dev->of_node == data;
1207}
41e69778 1208
812070eb
AT
1209/*
1210 * Identify what components need to be added by parsing what remote-endpoints
1211 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1212 * is no external component that we need to add since LVDS is within MDP4
1213 * itself.
1214 */
1215static int add_components_mdp(struct device *mdp_dev,
1216 struct component_match **matchptr)
1217{
1218 struct device_node *np = mdp_dev->of_node;
1219 struct device_node *ep_node;
54011e26
AT
1220 struct device *master_dev;
1221
1222 /*
1223 * on MDP4 based platforms, the MDP platform device is the component
1224 * master that adds other display interface components to itself.
1225 *
1226 * on MDP5 based platforms, the MDSS platform device is the component
1227 * master that adds MDP5 and other display interface components to
1228 * itself.
1229 */
1230 if (of_device_is_compatible(np, "qcom,mdp4"))
1231 master_dev = mdp_dev;
1232 else
1233 master_dev = mdp_dev->parent;
812070eb
AT
1234
1235 for_each_endpoint_of_node(np, ep_node) {
1236 struct device_node *intf;
1237 struct of_endpoint ep;
1238 int ret;
1239
1240 ret = of_graph_parse_endpoint(ep_node, &ep);
1241 if (ret) {
6a41da17 1242 DRM_DEV_ERROR(mdp_dev, "unable to parse port endpoint\n");
812070eb
AT
1243 of_node_put(ep_node);
1244 return ret;
1245 }
1246
1247 /*
1248 * The LCDC/LVDS port on MDP4 is a speacial case where the
1249 * remote-endpoint isn't a component that we need to add
1250 */
1251 if (of_device_is_compatible(np, "qcom,mdp4") &&
d8dd8052 1252 ep.port == 0)
812070eb 1253 continue;
812070eb
AT
1254
1255 /*
1256 * It's okay if some of the ports don't have a remote endpoint
1257 * specified. It just means that the port isn't connected to
1258 * any external interface.
1259 */
1260 intf = of_graph_get_remote_port_parent(ep_node);
d8dd8052 1261 if (!intf)
812070eb 1262 continue;
812070eb 1263
d1d9d0e1
DA
1264 if (of_device_is_available(intf))
1265 drm_of_component_match_add(master_dev, matchptr,
1266 compare_of, intf);
1267
812070eb 1268 of_node_put(intf);
812070eb
AT
1269 }
1270
1271 return 0;
1272}
1273
54011e26
AT
1274static int compare_name_mdp(struct device *dev, void *data)
1275{
1276 return (strstr(dev_name(dev), "mdp") != NULL);
1277}
1278
7d526fcf
AT
1279static int add_display_components(struct device *dev,
1280 struct component_match **matchptr)
1281{
54011e26
AT
1282 struct device *mdp_dev;
1283 int ret;
1284
1285 /*
25fdd593
JS
1286 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1287 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1288 * Populate the children devices, find the MDP5/DPU node, and then add
1289 * the interfaces to our components list.
54011e26 1290 */
25fdd593
JS
1291 if (of_device_is_compatible(dev->of_node, "qcom,mdss") ||
1292 of_device_is_compatible(dev->of_node, "qcom,sdm845-mdss")) {
54011e26
AT
1293 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1294 if (ret) {
6a41da17 1295 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
54011e26
AT
1296 return ret;
1297 }
1298
1299 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1300 if (!mdp_dev) {
6a41da17 1301 DRM_DEV_ERROR(dev, "failed to find MDSS MDP node\n");
54011e26
AT
1302 of_platform_depopulate(dev);
1303 return -ENODEV;
1304 }
1305
1306 put_device(mdp_dev);
1307
1308 /* add the MDP component itself */
97ac0e47
RK
1309 drm_of_component_match_add(dev, matchptr, compare_of,
1310 mdp_dev->of_node);
54011e26
AT
1311 } else {
1312 /* MDP4 */
1313 mdp_dev = dev;
1314 }
1315
1316 ret = add_components_mdp(mdp_dev, matchptr);
1317 if (ret)
1318 of_platform_depopulate(dev);
1319
1320 return ret;
7d526fcf
AT
1321}
1322
dc3ea265
AT
1323/*
1324 * We don't know what's the best binding to link the gpu with the drm device.
1325 * Fow now, we just hunt for all the possible gpus that we support, and add them
1326 * as components.
1327 */
1328static const struct of_device_id msm_gpu_match[] = {
1db7afa4 1329 { .compatible = "qcom,adreno" },
dc3ea265 1330 { .compatible = "qcom,adreno-3xx" },
e6f6d63e 1331 { .compatible = "amd,imageon" },
dc3ea265
AT
1332 { .compatible = "qcom,kgsl-3d0" },
1333 { },
1334};
1335
7d526fcf
AT
1336static int add_gpu_components(struct device *dev,
1337 struct component_match **matchptr)
1338{
dc3ea265
AT
1339 struct device_node *np;
1340
1341 np = of_find_matching_node(NULL, msm_gpu_match);
1342 if (!np)
1343 return 0;
1344
97ac0e47 1345 drm_of_component_match_add(dev, matchptr, compare_of, np);
dc3ea265
AT
1346
1347 of_node_put(np);
1348
1349 return 0;
7d526fcf
AT
1350}
1351
84448288
RK
1352static int msm_drm_bind(struct device *dev)
1353{
2b669875 1354 return msm_drm_init(dev, &msm_driver);
84448288
RK
1355}
1356
1357static void msm_drm_unbind(struct device *dev)
1358{
2b669875 1359 msm_drm_uninit(dev);
84448288
RK
1360}
1361
1362static const struct component_master_ops msm_drm_ops = {
1363 .bind = msm_drm_bind,
1364 .unbind = msm_drm_unbind,
1365};
1366
1367/*
1368 * Platform driver:
1369 */
060530f1 1370
84448288 1371static int msm_pdev_probe(struct platform_device *pdev)
060530f1 1372{
84448288 1373 struct component_match *match = NULL;
7d526fcf
AT
1374 int ret;
1375
e6f6d63e
JM
1376 if (get_mdp_ver(pdev)) {
1377 ret = add_display_components(&pdev->dev, &match);
1378 if (ret)
1379 return ret;
1380 }
e9fbdaf2 1381
7d526fcf
AT
1382 ret = add_gpu_components(&pdev->dev, &match);
1383 if (ret)
1384 return ret;
060530f1 1385
c83ea576
RC
1386 /* on all devices that I am aware of, iommu's which can map
1387 * any address the cpu can see are used:
1388 */
1389 ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1390 if (ret)
1391 return ret;
1392
84448288 1393 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
c8afe684
RC
1394}
1395
1396static int msm_pdev_remove(struct platform_device *pdev)
1397{
060530f1 1398 component_master_del(&pdev->dev, &msm_drm_ops);
54011e26 1399 of_platform_depopulate(&pdev->dev);
c8afe684
RC
1400
1401 return 0;
1402}
1403
06c0dd96 1404static const struct of_device_id dt_match[] = {
aaded2e3
JS
1405 { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
1406 { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
25fdd593 1407 { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU },
06c0dd96
RC
1408 {}
1409};
1410MODULE_DEVICE_TABLE(of, dt_match);
1411
c8afe684
RC
1412static struct platform_driver msm_platform_driver = {
1413 .probe = msm_pdev_probe,
1414 .remove = msm_pdev_remove,
1415 .driver = {
c8afe684 1416 .name = "msm",
06c0dd96 1417 .of_match_table = dt_match,
c8afe684
RC
1418 .pm = &msm_pm_ops,
1419 },
c8afe684
RC
1420};
1421
1422static int __init msm_drm_register(void)
1423{
ba4dd718
RC
1424 if (!modeset)
1425 return -EINVAL;
1426
c8afe684 1427 DBG("init");
1dd0a0b1 1428 msm_mdp_register();
25fdd593 1429 msm_dpu_register();
d5af49c9 1430 msm_dsi_register();
00453981 1431 msm_edp_register();
fcda50c8 1432 msm_hdmi_register();
bfd28b13 1433 adreno_register();
c8afe684
RC
1434 return platform_driver_register(&msm_platform_driver);
1435}
1436
1437static void __exit msm_drm_unregister(void)
1438{
1439 DBG("fini");
1440 platform_driver_unregister(&msm_platform_driver);
fcda50c8 1441 msm_hdmi_unregister();
bfd28b13 1442 adreno_unregister();
00453981 1443 msm_edp_unregister();
d5af49c9 1444 msm_dsi_unregister();
1dd0a0b1 1445 msm_mdp_unregister();
25fdd593 1446 msm_dpu_unregister();
c8afe684
RC
1447}
1448
1449module_init(msm_drm_register);
1450module_exit(msm_drm_unregister);
1451
1452MODULE_AUTHOR("Rob Clark <[email protected]");
1453MODULE_DESCRIPTION("MSM DRM Driver");
1454MODULE_LICENSE("GPL");
This page took 0.606118 seconds and 4 git commands to generate.