blob: ced015f240581e188be499ba803f33f746e0ee88 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
Dhaval Patel14d46ce2017-01-17 16:28:12 -08002 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Rob Clarkc8afe682013-06-26 12:44:06 -04003 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
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
Dhaval Patel3949f032016-06-20 16:24:33 -070019#include <linux/of_address.h>
Dhaval Patel5200c602017-01-17 15:53:37 -080020#include <linux/kthread.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040021#include "msm_drv.h"
Rob Clarkedcd60c2016-03-16 12:56:12 -040022#include "msm_debugfs.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040023#include "msm_fence.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040024#include "msm_gpu.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050025#include "msm_kms.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040026#include "sde_wb.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040027
Rob Clarka8d854c2016-06-01 14:02:02 -040028/*
29 * MSM driver version:
30 * - 1.0.0 - initial interface
31 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
Rob Clark7a3bcc02016-09-16 18:37:44 -040032 * - 1.2.0 - adds explicit fence support for submit ioctl
Rob Clarka8d854c2016-06-01 14:02:02 -040033 */
34#define MSM_VERSION_MAJOR 1
Rob Clark7a3bcc02016-09-16 18:37:44 -040035#define MSM_VERSION_MINOR 2
Rob Clarka8d854c2016-06-01 14:02:02 -040036#define MSM_VERSION_PATCHLEVEL 0
37
Lloyd Atkinson5217336c2016-09-15 18:21:18 -040038#define TEARDOWN_DEADLOCK_RETRY_MAX 5
39
Rob Clarkc8afe682013-06-26 12:44:06 -040040static void msm_fb_output_poll_changed(struct drm_device *dev)
41{
42 struct msm_drm_private *priv = dev->dev_private;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -040043
Rob Clarkc8afe682013-06-26 12:44:06 -040044 if (priv->fbdev)
45 drm_fb_helper_hotplug_event(priv->fbdev);
46}
47
Clarence Ipa65cba52017-03-17 15:18:29 -040048int msm_atomic_check(struct drm_device *dev,
49 struct drm_atomic_state *state)
50{
51 if (msm_is_suspend_blocked(dev)) {
52 DRM_DEBUG("rejecting commit during suspend\n");
53 return -EBUSY;
54 }
55 return drm_atomic_helper_check(dev, state);
56}
57
Rob Clarkc8afe682013-06-26 12:44:06 -040058static const struct drm_mode_config_funcs mode_config_funcs = {
59 .fb_create = msm_framebuffer_create,
60 .output_poll_changed = msm_fb_output_poll_changed,
Clarence Ipa65cba52017-03-17 15:18:29 -040061 .atomic_check = msm_atomic_check,
Rob Clarkcf3a7e42014-11-08 13:21:06 -050062 .atomic_commit = msm_atomic_commit,
Rob Clarkc8afe682013-06-26 12:44:06 -040063};
64
Rob Clark871d8122013-11-16 12:56:06 -050065int msm_register_mmu(struct drm_device *dev, struct msm_mmu *mmu)
Rob Clarkc8afe682013-06-26 12:44:06 -040066{
67 struct msm_drm_private *priv = dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -050068 int idx = priv->num_mmus++;
Rob Clarkc8afe682013-06-26 12:44:06 -040069
Rob Clark871d8122013-11-16 12:56:06 -050070 if (WARN_ON(idx >= ARRAY_SIZE(priv->mmus)))
Rob Clarkc8afe682013-06-26 12:44:06 -040071 return -EINVAL;
72
Rob Clark871d8122013-11-16 12:56:06 -050073 priv->mmus[idx] = mmu;
Rob Clarkc8afe682013-06-26 12:44:06 -040074
75 return idx;
76}
77
Lloyd Atkinson1e2497e2016-09-26 17:55:48 -040078void msm_unregister_mmu(struct drm_device *dev, struct msm_mmu *mmu)
79{
80 struct msm_drm_private *priv = dev->dev_private;
81 int idx;
82
83 if (priv->num_mmus <= 0) {
84 dev_err(dev->dev, "invalid num mmus %d\n", priv->num_mmus);
85 return;
86 }
87
88 idx = priv->num_mmus - 1;
89
90 /* only support reverse-order deallocation */
91 if (priv->mmus[idx] != mmu) {
92 dev_err(dev->dev, "unexpected mmu at idx %d\n", idx);
93 return;
94 }
95
96 --priv->num_mmus;
97 priv->mmus[idx] = 0;
98}
99
100
Rob Clarkc8afe682013-06-26 12:44:06 -0400101#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
102static bool reglog = false;
103MODULE_PARM_DESC(reglog, "Enable register read/write logging");
104module_param(reglog, bool, 0600);
105#else
106#define reglog 0
107#endif
108
Archit Tanejaa9ee34b2015-07-13 12:12:07 +0530109#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarke90dfec2015-01-30 17:05:41 -0500110static bool fbdev = true;
111MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
112module_param(fbdev, bool, 0600);
113#endif
114
Rob Clark3a10ba82014-09-08 14:24:57 -0400115static char *vram = "16m";
Rob Clark4313c742016-02-03 14:02:04 -0500116MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
Rob Clark871d8122013-11-16 12:56:06 -0500117module_param(vram, charp, 0);
118
Rob Clark060530f2014-03-03 14:19:12 -0500119/*
120 * Util/helpers:
121 */
122
Rob Clarkc8afe682013-06-26 12:44:06 -0400123void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
124 const char *dbgname)
125{
126 struct resource *res;
127 unsigned long size;
128 void __iomem *ptr;
129
130 if (name)
131 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
132 else
133 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134
135 if (!res) {
136 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name);
137 return ERR_PTR(-EINVAL);
138 }
139
140 size = resource_size(res);
141
142 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size);
143 if (!ptr) {
144 dev_err(&pdev->dev, "failed to ioremap: %s\n", name);
145 return ERR_PTR(-ENOMEM);
146 }
147
148 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200149 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400150
151 return ptr;
152}
153
Lloyd Atkinson1a0c9172016-10-04 10:01:24 -0400154void msm_iounmap(struct platform_device *pdev, void __iomem *addr)
155{
156 devm_iounmap(&pdev->dev, addr);
157}
158
Rob Clarkc8afe682013-06-26 12:44:06 -0400159void msm_writel(u32 data, void __iomem *addr)
160{
161 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200162 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
Rob Clarkc8afe682013-06-26 12:44:06 -0400163 writel(data, addr);
164}
165
166u32 msm_readl(const void __iomem *addr)
167{
168 u32 val = readl(addr);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400169
Rob Clarkc8afe682013-06-26 12:44:06 -0400170 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200171 printk(KERN_ERR "IO:R %p %08x\n", addr, val);
Rob Clarkc8afe682013-06-26 12:44:06 -0400172 return val;
173}
174
Hai Li78b1d472015-07-27 13:49:45 -0400175struct vblank_event {
176 struct list_head node;
177 int crtc_id;
178 bool enable;
179};
180
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530181static void vblank_ctrl_worker(struct kthread_work *work)
Hai Li78b1d472015-07-27 13:49:45 -0400182{
183 struct msm_vblank_ctrl *vbl_ctrl = container_of(work,
184 struct msm_vblank_ctrl, work);
185 struct msm_drm_private *priv = container_of(vbl_ctrl,
186 struct msm_drm_private, vblank_ctrl);
187 struct msm_kms *kms = priv->kms;
188 struct vblank_event *vbl_ev, *tmp;
189 unsigned long flags;
190
191 spin_lock_irqsave(&vbl_ctrl->lock, flags);
192 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
193 list_del(&vbl_ev->node);
194 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
195
196 if (vbl_ev->enable)
197 kms->funcs->enable_vblank(kms,
198 priv->crtcs[vbl_ev->crtc_id]);
199 else
200 kms->funcs->disable_vblank(kms,
201 priv->crtcs[vbl_ev->crtc_id]);
202
203 kfree(vbl_ev);
204
205 spin_lock_irqsave(&vbl_ctrl->lock, flags);
206 }
207
208 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
209}
210
211static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
212 int crtc_id, bool enable)
213{
214 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
215 struct vblank_event *vbl_ev;
216 unsigned long flags;
217
218 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC);
219 if (!vbl_ev)
220 return -ENOMEM;
221
222 vbl_ev->crtc_id = crtc_id;
223 vbl_ev->enable = enable;
224
225 spin_lock_irqsave(&vbl_ctrl->lock, flags);
226 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list);
227 spin_unlock_irqrestore(&vbl_ctrl->lock, flags);
228
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530229 kthread_queue_work(&priv->disp_thread[crtc_id].worker, &vbl_ctrl->work);
Hai Li78b1d472015-07-27 13:49:45 -0400230
231 return 0;
232}
233
Archit Taneja2b669872016-05-02 11:05:54 +0530234static int msm_drm_uninit(struct device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400235{
Dhaval Patel5200c602017-01-17 15:53:37 -0800236 struct platform_device *pdev = to_platform_device(dev);
237 struct drm_device *ddev = platform_get_drvdata(pdev);
238 struct msm_drm_private *priv = ddev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400239 struct msm_kms *kms = priv->kms;
Rob Clark7198e6b2013-07-19 12:59:32 -0400240 struct msm_gpu *gpu = priv->gpu;
Hai Li78b1d472015-07-27 13:49:45 -0400241 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
242 struct vblank_event *vbl_ev, *tmp;
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530243 int i;
Hai Li78b1d472015-07-27 13:49:45 -0400244
245 /* We must cancel and cleanup any pending vblank enable/disable
246 * work before drm_irq_uninstall() to avoid work re-enabling an
247 * irq after uninstall has disabled it.
248 */
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530249 kthread_flush_work(&vbl_ctrl->work);
Hai Li78b1d472015-07-27 13:49:45 -0400250 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
251 list_del(&vbl_ev->node);
252 kfree(vbl_ev);
253 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400254
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530255 /* clean up display commit worker threads */
256 for (i = 0; i < priv->num_crtcs; i++) {
257 if (priv->disp_thread[i].thread) {
258 kthread_flush_worker(&priv->disp_thread[i].worker);
259 kthread_stop(priv->disp_thread[i].thread);
260 priv->disp_thread[i].thread = NULL;
261 }
262 }
263
Rob Clark68209392016-05-17 16:19:32 -0400264 msm_gem_shrinker_cleanup(ddev);
265
Archit Taneja2b669872016-05-02 11:05:54 +0530266 drm_kms_helper_poll_fini(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530267
Dhaval Patel5200c602017-01-17 15:53:37 -0800268 drm_mode_config_cleanup(ddev);
269 drm_vblank_cleanup(ddev);
270
Lloyd Atkinsonab3dd302017-02-13 10:44:55 -0800271 if (priv->registered) {
272 drm_dev_unregister(ddev);
273 priv->registered = false;
274 }
Archit Taneja8208ed92016-05-02 11:05:53 +0530275
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530276#ifdef CONFIG_DRM_FBDEV_EMULATION
277 if (fbdev && priv->fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530278 msm_fbdev_free(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530279#endif
Archit Taneja2b669872016-05-02 11:05:54 +0530280 drm_mode_config_cleanup(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400281
Archit Taneja2b669872016-05-02 11:05:54 +0530282 pm_runtime_get_sync(dev);
283 drm_irq_uninstall(ddev);
284 pm_runtime_put_sync(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400285
286 flush_workqueue(priv->wq);
287 destroy_workqueue(priv->wq);
288
Archit Taneja16976082016-11-03 17:36:18 +0530289 if (kms && kms->funcs)
Rob Clarkc8afe682013-06-26 12:44:06 -0400290 kms->funcs->destroy(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400291
Rob Clark7198e6b2013-07-19 12:59:32 -0400292 if (gpu) {
Archit Taneja2b669872016-05-02 11:05:54 +0530293 mutex_lock(&ddev->struct_mutex);
Rob Clark7198e6b2013-07-19 12:59:32 -0400294 gpu->funcs->pm_suspend(gpu);
Archit Taneja2b669872016-05-02 11:05:54 +0530295 mutex_unlock(&ddev->struct_mutex);
Rob Clark774449e2015-05-15 09:19:36 -0400296 gpu->funcs->destroy(gpu);
Rob Clark7198e6b2013-07-19 12:59:32 -0400297 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400298
Rob Clark871d8122013-11-16 12:56:06 -0500299 if (priv->vram.paddr) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700300 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400301
Rob Clark871d8122013-11-16 12:56:06 -0500302 drm_mm_takedown(&priv->vram.mm);
Archit Taneja2b669872016-05-02 11:05:54 +0530303 dma_free_attrs(dev, priv->vram.size, NULL,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700304 priv->vram.paddr, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500305 }
306
Lloyd Atkinson113aefd2016-10-23 13:15:18 -0400307 sde_dbg_destroy();
Dhaval Patel6c666622017-03-21 23:02:59 -0700308 debugfs_remove_recursive(priv->debug_root);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400309
Dhaval Patel5200c602017-01-17 15:53:37 -0800310 component_unbind_all(dev, ddev);
Dhaval Patel5398f602017-03-25 18:25:18 -0700311 sde_power_client_destroy(&priv->phandle, priv->pclient);
Dhaval Patel5200c602017-01-17 15:53:37 -0800312 sde_power_resource_deinit(pdev, &priv->phandle);
Rob Clark060530f2014-03-03 14:19:12 -0500313
Archit Taneja0a6030d2016-05-08 21:36:28 +0530314 msm_mdss_destroy(ddev);
315
Archit Taneja2b669872016-05-02 11:05:54 +0530316 ddev->dev_private = NULL;
Rob Clarkc8afe682013-06-26 12:44:06 -0400317 kfree(priv);
318
Dhaval Patel5200c602017-01-17 15:53:37 -0800319 drm_dev_unref(ddev);
320
Rob Clarkc8afe682013-06-26 12:44:06 -0400321 return 0;
322}
323
Dhaval Patel5200c602017-01-17 15:53:37 -0800324#define KMS_MDP4 4
325#define KMS_MDP5 5
326#define KMS_SDE 3
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700327
Rob Clark06c0dd92013-11-30 17:51:47 -0500328static int get_mdp_ver(struct platform_device *pdev)
329{
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700330#ifdef CONFIG_OF
331 static const struct of_device_id match_types[] = { {
332 .compatible = "qcom,mdss_mdp",
333 .data = (void *)KMS_MDP5,
334 },
335 {
336 .compatible = "qcom,sde-kms",
337 .data = (void *)KMS_SDE,
338 /* end node */
339 } };
Rob Clark06c0dd92013-11-30 17:51:47 -0500340 struct device *dev = &pdev->dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700341 const struct of_device_id *match;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530342
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700343 match = of_match_node(match_types, dev->of_node);
344 if (match)
345 return (int)(unsigned long)match->data;
346#endif
347 return KMS_MDP4;
Rob Clark06c0dd92013-11-30 17:51:47 -0500348}
349
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500350static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400351{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500352 struct msm_drm_private *priv = dev->dev_private;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530353 struct device_node *node;
Rob Clark072f1f92015-03-03 15:04:25 -0500354 unsigned long size = 0;
355 int ret = 0;
356
Rob Clark072f1f92015-03-03 15:04:25 -0500357 /* In the device-tree world, we could have a 'memory-region'
358 * phandle, which gives us a link to our "vram". Allocating
359 * is all nicely abstracted behind the dma api, but we need
360 * to know the entire size to allocate it all in one go. There
361 * are two cases:
362 * 1) device with no IOMMU, in which case we need exclusive
363 * access to a VRAM carveout big enough for all gpu
364 * buffers
365 * 2) device with IOMMU, but where the bootloader puts up
366 * a splash screen. In this case, the VRAM carveout
367 * need only be large enough for fbdev fb. But we need
368 * exclusive access to the buffer to avoid the kernel
369 * using those pages for other purposes (which appears
370 * as corruption on screen before we have a chance to
371 * load and do initial modeset)
372 */
Rob Clark072f1f92015-03-03 15:04:25 -0500373
374 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
375 if (node) {
376 struct resource r;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400377
Rob Clark072f1f92015-03-03 15:04:25 -0500378 ret = of_address_to_resource(node, 0, &r);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400379
Peter Chen2ca41c172016-07-04 16:49:50 +0800380 of_node_put(node);
Rob Clark072f1f92015-03-03 15:04:25 -0500381 if (ret)
382 return ret;
383 size = r.end - r.start;
Thierry Redingfc99f972015-04-09 16:39:51 +0200384 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
Rob Clarkc8afe682013-06-26 12:44:06 -0400385
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530386 /* if we have no IOMMU, then we need to use carveout allocator.
387 * Grab the entire CMA chunk carved out in early startup in
388 * mach-msm:
389 */
390 } else if (!iommu_present(&platform_bus_type)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500391 DRM_INFO("using %s VRAM carveout\n", vram);
392 size = memparse(vram, NULL);
393 }
394
395 if (size) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700396 unsigned long attrs = 0;
Rob Clark871d8122013-11-16 12:56:06 -0500397 void *p;
398
Rob Clark871d8122013-11-16 12:56:06 -0500399 priv->vram.size = size;
400
401 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
402
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700403 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
404 attrs |= DMA_ATTR_WRITE_COMBINE;
Rob Clark871d8122013-11-16 12:56:06 -0500405
406 /* note that for no-kernel-mapping, the vaddr returned
407 * is bogus, but non-null if allocation succeeded:
408 */
409 p = dma_alloc_attrs(dev->dev, size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700410 &priv->vram.paddr, GFP_KERNEL, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500411 if (!p) {
412 dev_err(dev->dev, "failed to allocate VRAM\n");
413 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500414 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500415 }
416
417 dev_info(dev->dev, "VRAM: %08x->%08x\n",
418 (uint32_t)priv->vram.paddr,
419 (uint32_t)(priv->vram.paddr + size));
420 }
421
Rob Clark072f1f92015-03-03 15:04:25 -0500422 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500423}
424
Dhaval Patel3949f032016-06-20 16:24:33 -0700425#ifdef CONFIG_OF
426static int msm_component_bind_all(struct device *dev,
427 struct drm_device *drm_dev)
428{
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400429 int ret;
430
431 ret = component_bind_all(dev, drm_dev);
432 if (ret)
433 DRM_ERROR("component_bind_all failed: %d\n", ret);
434
435 return ret;
Dhaval Patel3949f032016-06-20 16:24:33 -0700436}
437#else
438static int msm_component_bind_all(struct device *dev,
439 struct drm_device *drm_dev)
440{
441 return 0;
442}
443#endif
444
Lloyd Atkinson113aefd2016-10-23 13:15:18 -0400445static int msm_power_enable_wrapper(void *handle, void *client, bool enable)
446{
447 return sde_power_resource_enable(handle, client, enable);
448}
449
Archit Taneja2b669872016-05-02 11:05:54 +0530450static int msm_drm_init(struct device *dev, struct drm_driver *drv)
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500451{
Archit Taneja2b669872016-05-02 11:05:54 +0530452 struct platform_device *pdev = to_platform_device(dev);
453 struct drm_device *ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500454 struct msm_drm_private *priv;
455 struct msm_kms *kms;
Lloyd Atkinson113aefd2016-10-23 13:15:18 -0400456 struct sde_dbg_power_ctrl dbg_power_ctrl = { 0 };
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530457 int ret, i;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500458
Dhaval Patel5200c602017-01-17 15:53:37 -0800459 ddev = drm_dev_alloc(drv, dev);
460 if (!ddev) {
461 dev_err(dev, "failed to allocate drm_device\n");
462 return -ENOMEM;
463 }
464
465 drm_mode_config_init(ddev);
466 platform_set_drvdata(pdev, ddev);
467 ddev->platformdev = pdev;
468
Archit Taneja2b669872016-05-02 11:05:54 +0530469 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
470 if (!priv) {
Dhaval Patel5200c602017-01-17 15:53:37 -0800471 ret = -ENOMEM;
472 goto priv_alloc_fail;
Archit Taneja2b669872016-05-02 11:05:54 +0530473 }
474
475 ddev->dev_private = priv;
Rob Clark68209392016-05-17 16:19:32 -0400476 priv->dev = ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500477
Dhaval Patel5200c602017-01-17 15:53:37 -0800478 ret = msm_mdss_init(ddev);
479 if (ret)
480 goto mdss_init_fail;
481
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400482 priv->wq = alloc_ordered_workqueue("msm_drm", 0);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400483 init_waitqueue_head(&priv->pending_crtcs_event);
484
485 INIT_LIST_HEAD(&priv->client_event_list);
486 INIT_LIST_HEAD(&priv->inactive_list);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400487 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530488 kthread_init_work(&priv->vblank_ctrl.work, vblank_ctrl_worker);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400489 spin_lock_init(&priv->vblank_ctrl.lock);
490
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400491 ret = sde_power_resource_init(pdev, &priv->phandle);
492 if (ret) {
493 pr_err("sde power resource init failed\n");
Dhaval Patel5398f602017-03-25 18:25:18 -0700494 goto power_init_fail;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400495 }
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500496
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400497 priv->pclient = sde_power_client_create(&priv->phandle, "sde");
498 if (IS_ERR_OR_NULL(priv->pclient)) {
499 pr_err("sde power client create failed\n");
500 ret = -EINVAL;
Dhaval Patel5398f602017-03-25 18:25:18 -0700501 goto power_client_fail;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400502 }
Rob Clark060530f2014-03-03 14:19:12 -0500503
504 /* Bind all our sub-components: */
Dhaval Patel5200c602017-01-17 15:53:37 -0800505 ret = msm_component_bind_all(dev, ddev);
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400506 if (ret)
Dhaval Patel5398f602017-03-25 18:25:18 -0700507 goto bind_fail;
Rob Clark060530f2014-03-03 14:19:12 -0500508
Archit Taneja2b669872016-05-02 11:05:54 +0530509 ret = msm_init_vram(ddev);
Rob Clark13f15562015-05-07 15:20:13 -0400510 if (ret)
511 goto fail;
512
Lloyd Atkinson113aefd2016-10-23 13:15:18 -0400513 dbg_power_ctrl.handle = &priv->phandle;
514 dbg_power_ctrl.client = priv->pclient;
515 dbg_power_ctrl.enable_fn = msm_power_enable_wrapper;
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -0700516 ret = sde_dbg_init(&pdev->dev, &dbg_power_ctrl);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400517 if (ret) {
Lloyd Atkinson113aefd2016-10-23 13:15:18 -0400518 dev_err(dev, "failed to init sde dbg: %d\n", ret);
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400519 goto fail;
520 }
Rob Clark68209392016-05-17 16:19:32 -0400521
Rob Clark06c0dd92013-11-30 17:51:47 -0500522 switch (get_mdp_ver(pdev)) {
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700523 case KMS_MDP4:
Dhaval Patel5200c602017-01-17 15:53:37 -0800524 kms = mdp4_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500525 break;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700526 case KMS_MDP5:
Dhaval Patel5200c602017-01-17 15:53:37 -0800527 kms = mdp5_kms_init(ddev);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700528 break;
529 case KMS_SDE:
Dhaval Patel5200c602017-01-17 15:53:37 -0800530 kms = sde_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500531 break;
532 default:
533 kms = ERR_PTR(-ENODEV);
534 break;
535 }
536
Rob Clarkc8afe682013-06-26 12:44:06 -0400537 if (IS_ERR(kms)) {
538 /*
539 * NOTE: once we have GPU support, having no kms should not
540 * be considered fatal.. ideally we would still support gpu
541 * and (for example) use dmabuf/prime to share buffers with
542 * imx drm driver on iMX5
543 */
Lloyd Atkinson1e2497e2016-09-26 17:55:48 -0400544 priv->kms = NULL;
Dhaval Patel5200c602017-01-17 15:53:37 -0800545 dev_err(dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200546 ret = PTR_ERR(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400547 goto fail;
548 }
Dhaval Patel5200c602017-01-17 15:53:37 -0800549 priv->kms = kms;
550 pm_runtime_enable(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400551
Alan Kwong29946282017-02-01 21:55:56 -0800552 if (kms) {
553 ret = kms->funcs->hw_init(kms);
554 if (ret) {
555 dev_err(dev, "kms hw init failed: %d\n", ret);
556 goto fail;
557 }
558 }
559 ddev->mode_config.funcs = &mode_config_funcs;
560
Sandeep Pandaf48c46a2016-10-24 09:48:50 +0530561 for (i = 0; i < priv->num_crtcs; i++) {
562 priv->disp_thread[i].crtc_id = priv->crtcs[i]->base.id;
563 kthread_init_worker(&priv->disp_thread[i].worker);
564 priv->disp_thread[i].dev = ddev;
565 priv->disp_thread[i].thread =
566 kthread_run(kthread_worker_fn,
567 &priv->disp_thread[i].worker,
568 "crtc_commit:%d",
569 priv->disp_thread[i].crtc_id);
570
571 if (IS_ERR(priv->disp_thread[i].thread)) {
572 dev_err(dev, "failed to create kthread\n");
573 priv->disp_thread[i].thread = NULL;
574 /* clean up previously created threads if any */
575 for (i -= 1; i >= 0; i--) {
576 kthread_stop(priv->disp_thread[i].thread);
577 priv->disp_thread[i].thread = NULL;
578 }
579 goto fail;
580 }
581 }
582
Archit Taneja2b669872016-05-02 11:05:54 +0530583 ret = drm_vblank_init(ddev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400584 if (ret < 0) {
Archit Taneja2b669872016-05-02 11:05:54 +0530585 dev_err(dev, "failed to initialize vblank\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400586 goto fail;
587 }
588
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530589 if (kms) {
590 pm_runtime_get_sync(dev);
Dhaval Patel5200c602017-01-17 15:53:37 -0800591 ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530592 pm_runtime_put_sync(dev);
593 if (ret < 0) {
594 dev_err(dev, "failed to install IRQ handler\n");
595 goto fail;
596 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400597 }
598
Lloyd Atkinsonab3dd302017-02-13 10:44:55 -0800599 ret = drm_dev_register(ddev, 0);
600 if (ret)
601 goto fail;
602 priv->registered = true;
Rob Clarka7d3c952014-05-30 14:47:38 -0400603
Archit Taneja2b669872016-05-02 11:05:54 +0530604 drm_mode_config_reset(ddev);
605
606#ifdef CONFIG_DRM_FBDEV_EMULATION
607 if (fbdev)
608 priv->fbdev = msm_fbdev_init(ddev);
609#endif
610
611 ret = msm_debugfs_late_init(ddev);
612 if (ret)
613 goto fail;
614
Dhaval Patel6c666622017-03-21 23:02:59 -0700615 priv->debug_root = debugfs_create_dir("debug",
616 ddev->primary->debugfs_root);
617 if (IS_ERR_OR_NULL(priv->debug_root)) {
618 pr_err("debugfs_root create_dir fail, error %ld\n",
619 PTR_ERR(priv->debug_root));
620 priv->debug_root = NULL;
621 goto fail;
622 }
623
624 ret = sde_dbg_debugfs_register(priv->debug_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -0700625 if (ret) {
626 dev_err(dev, "failed to reg sde dbg debugfs: %d\n", ret);
627 goto fail;
628 }
629
Alan Kwong5a3ac752016-10-16 01:02:35 -0400630 /* perform subdriver post initialization */
631 if (kms && kms->funcs && kms->funcs->postinit) {
632 ret = kms->funcs->postinit(kms);
633 if (ret) {
Dhaval Patel5200c602017-01-17 15:53:37 -0800634 pr_err("kms post init failed: %d\n", ret);
Alan Kwong5a3ac752016-10-16 01:02:35 -0400635 goto fail;
636 }
637 }
638
Dhaval Patel5200c602017-01-17 15:53:37 -0800639 drm_kms_helper_poll_init(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400640
641 return 0;
642
643fail:
Archit Taneja2b669872016-05-02 11:05:54 +0530644 msm_drm_uninit(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400645 return ret;
Dhaval Patel5398f602017-03-25 18:25:18 -0700646bind_fail:
647 sde_power_client_destroy(&priv->phandle, priv->pclient);
648power_client_fail:
649 sde_power_resource_deinit(pdev, &priv->phandle);
650power_init_fail:
651 msm_mdss_destroy(ddev);
Dhaval Patel5200c602017-01-17 15:53:37 -0800652mdss_init_fail:
653 kfree(priv);
654priv_alloc_fail:
655 drm_dev_unref(ddev);
656 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400657}
658
Archit Taneja2b669872016-05-02 11:05:54 +0530659/*
660 * DRM operations:
661 */
662
Stephane Viau32f13f62015-04-29 15:57:29 -0400663#ifdef CONFIG_QCOM_KGSL
664static void load_gpu(struct drm_device *dev)
665{
666}
667#else
Rob Clark7198e6b2013-07-19 12:59:32 -0400668static void load_gpu(struct drm_device *dev)
669{
Rob Clarka1ad3522014-07-11 11:59:22 -0400670 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400671 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400672
Rob Clarka1ad3522014-07-11 11:59:22 -0400673 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400674
Rob Clarke2550b72014-09-05 13:30:27 -0400675 if (!priv->gpu)
676 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400677
Rob Clarka1ad3522014-07-11 11:59:22 -0400678 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400679}
Stephane Viau32f13f62015-04-29 15:57:29 -0400680#endif
Rob Clark7198e6b2013-07-19 12:59:32 -0400681
682static int msm_open(struct drm_device *dev, struct drm_file *file)
683{
684 struct msm_file_private *ctx;
685
686 /* For now, load gpu on open.. to avoid the requirement of having
687 * firmware in the initrd.
688 */
689 load_gpu(dev);
690
691 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
692 if (!ctx)
693 return -ENOMEM;
694
695 file->driver_priv = ctx;
696
Clarence Ip0e19a5d2016-08-10 16:36:50 -0400697 if (dev && dev->dev_private) {
698 struct msm_drm_private *priv = dev->dev_private;
699 struct msm_kms *kms;
700
701 kms = priv->kms;
702 if (kms && kms->funcs && kms->funcs->postopen)
703 kms->funcs->postopen(kms, file);
704 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400705 return 0;
706}
707
Rob Clarkc8afe682013-06-26 12:44:06 -0400708static void msm_preclose(struct drm_device *dev, struct drm_file *file)
709{
710 struct msm_drm_private *priv = dev->dev_private;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400711 struct msm_kms *kms = priv->kms;
712
713 if (kms && kms->funcs && kms->funcs->preclose)
714 kms->funcs->preclose(kms, file);
715}
716
717static void msm_postclose(struct drm_device *dev, struct drm_file *file)
718{
719 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400720 struct msm_file_private *ctx = file->driver_priv;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400721 struct msm_kms *kms = priv->kms;
722
723 if (kms && kms->funcs && kms->funcs->postclose)
724 kms->funcs->postclose(kms, file);
Rob Clark7198e6b2013-07-19 12:59:32 -0400725
Rob Clark7198e6b2013-07-19 12:59:32 -0400726 mutex_lock(&dev->struct_mutex);
727 if (ctx == priv->lastctx)
728 priv->lastctx = NULL;
729 mutex_unlock(&dev->struct_mutex);
730
731 kfree(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400732}
733
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400734static int msm_disable_all_modes_commit(
735 struct drm_device *dev,
736 struct drm_atomic_state *state)
737{
738 struct drm_plane *plane;
739 struct drm_crtc *crtc;
740 unsigned int plane_mask;
741 int ret;
742
743 plane_mask = 0;
744 drm_for_each_plane(plane, dev) {
745 struct drm_plane_state *plane_state;
746
747 plane_state = drm_atomic_get_plane_state(state, plane);
748 if (IS_ERR(plane_state)) {
749 ret = PTR_ERR(plane_state);
750 goto fail;
751 }
752
Alan Kwong76c9d182016-12-14 14:39:17 -0800753 plane_state->rotation = 0;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400754
755 plane->old_fb = plane->fb;
756 plane_mask |= 1 << drm_plane_index(plane);
757
758 /* disable non-primary: */
759 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
760 continue;
761
762 DRM_DEBUG("disabling plane %d\n", plane->base.id);
763
764 ret = __drm_atomic_helper_disable_plane(plane, plane_state);
765 if (ret != 0)
766 DRM_ERROR("error %d disabling plane %d\n", ret,
767 plane->base.id);
768 }
769
770 drm_for_each_crtc(crtc, dev) {
771 struct drm_mode_set mode_set;
772
773 memset(&mode_set, 0, sizeof(struct drm_mode_set));
774 mode_set.crtc = crtc;
775
776 DRM_DEBUG("disabling crtc %d\n", crtc->base.id);
777
778 ret = __drm_atomic_helper_set_config(&mode_set, state);
779 if (ret != 0)
780 DRM_ERROR("error %d disabling crtc %d\n", ret,
781 crtc->base.id);
782 }
783
784 DRM_DEBUG("committing disables\n");
785 ret = drm_atomic_commit(state);
786
787fail:
788 drm_atomic_clean_old_fb(dev, plane_mask, ret);
789 DRM_DEBUG("disables result %d\n", ret);
790 return ret;
791}
792
793/**
794 * msm_clear_all_modes - disables all planes and crtcs via an atomic commit
795 * based on restore_fbdev_mode_atomic in drm_fb_helper.c
796 * @dev: device pointer
797 * @Return: 0 on success, otherwise -error
798 */
799static int msm_disable_all_modes(struct drm_device *dev)
800{
801 struct drm_atomic_state *state;
802 int ret, i;
803
804 state = drm_atomic_state_alloc(dev);
805 if (!state)
806 return -ENOMEM;
807
808 state->acquire_ctx = dev->mode_config.acquire_ctx;
809
810 for (i = 0; i < TEARDOWN_DEADLOCK_RETRY_MAX; i++) {
811 ret = msm_disable_all_modes_commit(dev, state);
812 if (ret != -EDEADLK)
813 break;
814 drm_atomic_state_clear(state);
815 drm_atomic_legacy_backoff(state);
816 }
817
818 /* on successful atomic commit state ownership transfers to framework */
819 if (ret != 0)
820 drm_atomic_state_free(state);
821
822 return ret;
823}
824
Rob Clarkc8afe682013-06-26 12:44:06 -0400825static void msm_lastclose(struct drm_device *dev)
826{
827 struct msm_drm_private *priv = dev->dev_private;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400828 struct msm_kms *kms = priv->kms;
Alan Kwong5a3ac752016-10-16 01:02:35 -0400829 int i;
830
831 /*
832 * clean up vblank disable immediately as this is the last close.
833 */
834 for (i = 0; i < dev->num_crtcs; i++) {
835 struct drm_vblank_crtc *vblank = &dev->vblank[i];
836 struct timer_list *disable_timer = &vblank->disable_timer;
837
838 if (del_timer_sync(disable_timer))
839 disable_timer->function(disable_timer->data);
840 }
841
842 /* wait for pending vblank requests to be executed by worker thread */
843 flush_workqueue(priv->wq);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400844
845 if (priv->fbdev) {
Rob Clark5ea1f752014-05-30 12:29:48 -0400846 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -0400847 } else {
848 drm_modeset_lock_all(dev);
849 msm_disable_all_modes(dev);
850 drm_modeset_unlock_all(dev);
851 if (kms && kms->funcs && kms->funcs->lastclose)
852 kms->funcs->lastclose(kms);
853 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400854}
855
Daniel Vettere9f0d762013-12-11 11:34:42 +0100856static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400857{
858 struct drm_device *dev = arg;
859 struct msm_drm_private *priv = dev->dev_private;
860 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400861
Rob Clarkc8afe682013-06-26 12:44:06 -0400862 BUG_ON(!kms);
863 return kms->funcs->irq(kms);
864}
865
866static void msm_irq_preinstall(struct drm_device *dev)
867{
868 struct msm_drm_private *priv = dev->dev_private;
869 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400870
Rob Clarkc8afe682013-06-26 12:44:06 -0400871 BUG_ON(!kms);
872 kms->funcs->irq_preinstall(kms);
873}
874
875static int msm_irq_postinstall(struct drm_device *dev)
876{
877 struct msm_drm_private *priv = dev->dev_private;
878 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400879
Rob Clarkc8afe682013-06-26 12:44:06 -0400880 BUG_ON(!kms);
881 return kms->funcs->irq_postinstall(kms);
882}
883
884static void msm_irq_uninstall(struct drm_device *dev)
885{
886 struct msm_drm_private *priv = dev->dev_private;
887 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400888
Rob Clarkc8afe682013-06-26 12:44:06 -0400889 BUG_ON(!kms);
890 kms->funcs->irq_uninstall(kms);
891}
892
Thierry Reding88e72712015-09-24 18:35:31 +0200893static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400894{
895 struct msm_drm_private *priv = dev->dev_private;
896 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400897
Rob Clarkc8afe682013-06-26 12:44:06 -0400898 if (!kms)
899 return -ENXIO;
Thierry Reding88e72712015-09-24 18:35:31 +0200900 DBG("dev=%p, crtc=%u", dev, pipe);
901 return vblank_ctrl_queue_work(priv, pipe, true);
Rob Clarkc8afe682013-06-26 12:44:06 -0400902}
903
Thierry Reding88e72712015-09-24 18:35:31 +0200904static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400905{
906 struct msm_drm_private *priv = dev->dev_private;
907 struct msm_kms *kms = priv->kms;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -0400908
Rob Clarkc8afe682013-06-26 12:44:06 -0400909 if (!kms)
910 return;
Thierry Reding88e72712015-09-24 18:35:31 +0200911 DBG("dev=%p, crtc=%u", dev, pipe);
912 vblank_ctrl_queue_work(priv, pipe, false);
Rob Clarkc8afe682013-06-26 12:44:06 -0400913}
914
915/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400916 * DRM ioctls:
917 */
918
919static int msm_ioctl_get_param(struct drm_device *dev, void *data,
920 struct drm_file *file)
921{
922 struct msm_drm_private *priv = dev->dev_private;
923 struct drm_msm_param *args = data;
924 struct msm_gpu *gpu;
925
926 /* for now, we just have 3d pipe.. eventually this would need to
927 * be more clever to dispatch to appropriate gpu module:
928 */
929 if (args->pipe != MSM_PIPE_3D0)
930 return -EINVAL;
931
932 gpu = priv->gpu;
933
934 if (!gpu)
935 return -ENXIO;
936
937 return gpu->funcs->get_param(gpu, args->param, &args->value);
938}
939
940static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
941 struct drm_file *file)
942{
943 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500944
945 if (args->flags & ~MSM_BO_FLAGS) {
946 DRM_ERROR("invalid flags: %08x\n", args->flags);
947 return -EINVAL;
948 }
949
Rob Clark7198e6b2013-07-19 12:59:32 -0400950 return msm_gem_new_handle(dev, file, args->size,
951 args->flags, &args->handle);
952}
953
Rob Clark56c2da82015-05-11 11:50:03 -0400954static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
955{
956 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
957}
Rob Clark7198e6b2013-07-19 12:59:32 -0400958
959static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
960 struct drm_file *file)
961{
962 struct drm_msm_gem_cpu_prep *args = data;
963 struct drm_gem_object *obj;
Rob Clark56c2da82015-05-11 11:50:03 -0400964 ktime_t timeout = to_ktime(args->timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400965 int ret;
966
Rob Clark93ddb0d2014-03-03 09:42:33 -0500967 if (args->op & ~MSM_PREP_FLAGS) {
968 DRM_ERROR("invalid op: %08x\n", args->op);
969 return -EINVAL;
970 }
971
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100972 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400973 if (!obj)
974 return -ENOENT;
975
Rob Clark56c2da82015-05-11 11:50:03 -0400976 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400977
978 drm_gem_object_unreference_unlocked(obj);
979
980 return ret;
981}
982
983static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
984 struct drm_file *file)
985{
986 struct drm_msm_gem_cpu_fini *args = data;
987 struct drm_gem_object *obj;
988 int ret;
989
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100990 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400991 if (!obj)
992 return -ENOENT;
993
994 ret = msm_gem_cpu_fini(obj);
995
996 drm_gem_object_unreference_unlocked(obj);
997
998 return ret;
999}
1000
1001static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
1002 struct drm_file *file)
1003{
1004 struct drm_msm_gem_info *args = data;
1005 struct drm_gem_object *obj;
1006 int ret = 0;
1007
1008 if (args->pad)
1009 return -EINVAL;
1010
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001011 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -04001012 if (!obj)
1013 return -ENOENT;
1014
1015 args->offset = msm_gem_mmap_offset(obj);
1016
1017 drm_gem_object_unreference_unlocked(obj);
1018
1019 return ret;
1020}
1021
1022static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
1023 struct drm_file *file)
1024{
Rob Clarkca762a82016-03-15 17:22:13 -04001025 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -04001026 struct drm_msm_wait_fence *args = data;
Rob Clark56c2da82015-05-11 11:50:03 -04001027 ktime_t timeout = to_ktime(args->timeout);
Rob Clark93ddb0d2014-03-03 09:42:33 -05001028
1029 if (args->pad) {
1030 DRM_ERROR("invalid pad: %08x\n", args->pad);
1031 return -EINVAL;
1032 }
1033
Rob Clarkca762a82016-03-15 17:22:13 -04001034 if (!priv->gpu)
1035 return 0;
1036
1037 return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true);
Rob Clark7198e6b2013-07-19 12:59:32 -04001038}
1039
Rob Clark4cd33c42016-05-17 15:44:49 -04001040static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
1041 struct drm_file *file)
1042{
1043 struct drm_msm_gem_madvise *args = data;
1044 struct drm_gem_object *obj;
1045 int ret;
1046
1047 switch (args->madv) {
1048 case MSM_MADV_DONTNEED:
1049 case MSM_MADV_WILLNEED:
1050 break;
1051 default:
1052 return -EINVAL;
1053 }
1054
1055 ret = mutex_lock_interruptible(&dev->struct_mutex);
1056 if (ret)
1057 return ret;
1058
1059 obj = drm_gem_object_lookup(file, args->handle);
1060 if (!obj) {
1061 ret = -ENOENT;
1062 goto unlock;
1063 }
1064
1065 ret = msm_gem_madvise(obj, args->madv);
1066 if (ret >= 0) {
1067 args->retained = ret;
1068 ret = 0;
1069 }
1070
1071 drm_gem_object_unreference(obj);
1072
1073unlock:
1074 mutex_unlock(&dev->struct_mutex);
1075 return ret;
1076}
1077
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07001078static int msm_drm_object_supports_event(struct drm_device *dev,
1079 struct drm_msm_event_req *req)
1080{
1081 int ret = -EINVAL;
1082 struct drm_mode_object *arg_obj;
1083
1084 arg_obj = drm_mode_object_find(dev, req->object_id, req->object_type);
1085 if (!arg_obj)
1086 return -ENOENT;
1087
1088 switch (arg_obj->type) {
1089 case DRM_MODE_OBJECT_CRTC:
1090 case DRM_MODE_OBJECT_CONNECTOR:
1091 ret = 0;
1092 break;
1093 default:
1094 ret = -EOPNOTSUPP;
1095 break;
1096 }
1097
1098 return ret;
1099}
1100
1101static int msm_register_event(struct drm_device *dev,
1102 struct drm_msm_event_req *req, struct drm_file *file, bool en)
1103{
1104 int ret = -EINVAL;
1105 struct msm_drm_private *priv = dev->dev_private;
1106 struct msm_kms *kms = priv->kms;
1107 struct drm_mode_object *arg_obj;
1108
1109 arg_obj = drm_mode_object_find(dev, req->object_id, req->object_type);
1110 if (!arg_obj)
1111 return -ENOENT;
1112
1113 ret = kms->funcs->register_events(kms, arg_obj, req->event, en);
1114 return ret;
1115}
1116
1117static int msm_event_client_count(struct drm_device *dev,
1118 struct drm_msm_event_req *req_event, bool locked)
1119{
1120 struct msm_drm_private *priv = dev->dev_private;
1121 unsigned long flag = 0;
1122 struct msm_drm_event *node;
1123 int count = 0;
1124
1125 if (!locked)
1126 spin_lock_irqsave(&dev->event_lock, flag);
1127 list_for_each_entry(node, &priv->client_event_list, base.link) {
1128 if (node->event.type == req_event->event &&
1129 node->info.object_id == req_event->object_id)
1130 count++;
1131 }
1132 if (!locked)
1133 spin_unlock_irqrestore(&dev->event_lock, flag);
1134
1135 return count;
1136}
1137
1138static int msm_ioctl_register_event(struct drm_device *dev, void *data,
1139 struct drm_file *file)
1140{
1141 struct msm_drm_private *priv = dev->dev_private;
1142 struct drm_msm_event_req *req_event = data;
1143 struct msm_drm_event *client, *node;
1144 unsigned long flag = 0;
1145 bool dup_request = false;
1146 int ret = 0, count = 0;
1147
1148 ret = msm_drm_object_supports_event(dev, req_event);
1149 if (ret) {
1150 DRM_ERROR("unsupported event %x object %x object id %d\n",
1151 req_event->event, req_event->object_type,
1152 req_event->object_id);
1153 return ret;
1154 }
1155
1156 spin_lock_irqsave(&dev->event_lock, flag);
1157 list_for_each_entry(node, &priv->client_event_list, base.link) {
1158 if (node->base.file_priv != file)
1159 continue;
1160 if (node->event.type == req_event->event &&
1161 node->info.object_id == req_event->object_id) {
1162 DRM_DEBUG("duplicate request for event %x obj id %d\n",
1163 node->event.type, node->info.object_id);
1164 dup_request = true;
1165 break;
1166 }
1167 }
1168 spin_unlock_irqrestore(&dev->event_lock, flag);
1169
1170 if (dup_request)
1171 return -EALREADY;
1172
1173 client = kzalloc(sizeof(*client), GFP_KERNEL);
1174 if (!client)
1175 return -ENOMEM;
1176
1177 client->base.file_priv = file;
1178 client->base.pid = current->pid;
1179 client->base.event = &client->event;
1180 client->event.type = req_event->event;
1181 memcpy(&client->info, req_event, sizeof(client->info));
1182
1183 /* Get the count of clients that have registered for event.
1184 * Event should be enabled for first client, for subsequent enable
1185 * calls add to client list and return.
1186 */
1187 count = msm_event_client_count(dev, req_event, false);
1188 /* Add current client to list */
1189 spin_lock_irqsave(&dev->event_lock, flag);
1190 list_add_tail(&client->base.link, &priv->client_event_list);
1191 spin_unlock_irqrestore(&dev->event_lock, flag);
1192
1193 if (count)
1194 return 0;
1195
1196 ret = msm_register_event(dev, req_event, file, true);
1197 if (ret) {
1198 DRM_ERROR("failed to enable event %x object %x object id %d\n",
1199 req_event->event, req_event->object_type,
1200 req_event->object_id);
1201 spin_lock_irqsave(&dev->event_lock, flag);
1202 list_del(&client->base.link);
1203 spin_unlock_irqrestore(&dev->event_lock, flag);
1204 kfree(client);
1205 }
1206 return ret;
1207}
1208
1209static int msm_ioctl_deregister_event(struct drm_device *dev, void *data,
1210 struct drm_file *file)
1211{
1212 struct msm_drm_private *priv = dev->dev_private;
1213 struct drm_msm_event_req *req_event = data;
1214 struct msm_drm_event *client = NULL, *node, *temp;
1215 unsigned long flag = 0;
1216 int count = 0;
1217 bool found = false;
1218 int ret = 0;
1219
1220 ret = msm_drm_object_supports_event(dev, req_event);
1221 if (ret) {
1222 DRM_ERROR("unsupported event %x object %x object id %d\n",
1223 req_event->event, req_event->object_type,
1224 req_event->object_id);
1225 return ret;
1226 }
1227
1228 spin_lock_irqsave(&dev->event_lock, flag);
1229 list_for_each_entry_safe(node, temp, &priv->client_event_list,
1230 base.link) {
1231 if (node->event.type == req_event->event &&
1232 node->info.object_id == req_event->object_id &&
1233 node->base.file_priv == file) {
1234 client = node;
1235 list_del(&client->base.link);
1236 found = true;
1237 kfree(client);
1238 break;
1239 }
1240 }
1241 spin_unlock_irqrestore(&dev->event_lock, flag);
1242
1243 if (!found)
1244 return -ENOENT;
1245
1246 count = msm_event_client_count(dev, req_event, false);
1247 if (!count)
1248 ret = msm_register_event(dev, req_event, file, false);
1249
1250 return ret;
1251}
1252
1253void msm_send_crtc_notification(struct drm_crtc *crtc,
1254 struct drm_event *event, u8 *payload)
1255{
1256 struct drm_device *dev = NULL;
1257 struct msm_drm_private *priv = NULL;
1258 unsigned long flags;
1259 struct msm_drm_event *notify, *node;
1260 int len = 0, ret;
1261
1262 if (!crtc || !event || !event->length || !payload) {
1263 DRM_ERROR("err param crtc %pK event %pK len %d payload %pK\n",
1264 crtc, event, ((event) ? (event->length) : -1),
1265 payload);
1266 return;
1267 }
1268 dev = crtc->dev;
1269 priv = (dev) ? dev->dev_private : NULL;
1270 if (!dev || !priv) {
1271 DRM_ERROR("invalid dev %pK priv %pK\n", dev, priv);
1272 return;
1273 }
1274
1275 spin_lock_irqsave(&dev->event_lock, flags);
1276 list_for_each_entry(node, &priv->client_event_list, base.link) {
1277 if (node->event.type != event->type ||
1278 crtc->base.id != node->info.object_id)
1279 continue;
1280 len = event->length + sizeof(struct drm_msm_event_resp);
1281 if (node->base.file_priv->event_space < len) {
1282 DRM_ERROR("Insufficient space to notify\n");
1283 continue;
1284 }
1285 notify = kzalloc(len, GFP_ATOMIC);
1286 if (!notify)
1287 continue;
1288 notify->base.file_priv = node->base.file_priv;
1289 notify->base.event = &notify->event;
1290 notify->base.pid = node->base.pid;
1291 notify->event.type = node->event.type;
1292 notify->event.length = len;
1293 memcpy(&notify->info, &node->info, sizeof(notify->info));
1294 memcpy(notify->data, payload, event->length);
1295 ret = drm_event_reserve_init_locked(dev, node->base.file_priv,
1296 &notify->base, &notify->event);
1297 if (ret) {
1298 kfree(notify);
1299 continue;
1300 }
1301 drm_send_event_locked(dev, &notify->base);
1302 }
1303 spin_unlock_irqrestore(&dev->event_lock, flags);
1304}
1305
1306static int msm_release(struct inode *inode, struct file *filp)
1307{
1308 struct drm_file *file_priv = filp->private_data;
1309 struct drm_minor *minor = file_priv->minor;
1310 struct drm_device *dev = minor->dev;
1311 struct msm_drm_private *priv = dev->dev_private;
1312 struct msm_drm_event *node, *temp;
1313 u32 count;
1314 unsigned long flags;
1315
1316 spin_lock_irqsave(&dev->event_lock, flags);
1317 list_for_each_entry_safe(node, temp, &priv->client_event_list,
1318 base.link) {
1319 if (node->base.file_priv != file_priv)
1320 continue;
1321 list_del(&node->base.link);
1322 spin_unlock_irqrestore(&dev->event_lock, flags);
1323 count = msm_event_client_count(dev, &node->info, true);
1324 if (!count)
1325 msm_register_event(dev, &node->info, file_priv, false);
1326 kfree(node);
1327 spin_lock_irqsave(&dev->event_lock, flags);
1328 }
1329 spin_unlock_irqrestore(&dev->event_lock, flags);
1330
1331 return drm_release(inode, filp);
1332}
1333
Rob Clark7198e6b2013-07-19 12:59:32 -04001334static const struct drm_ioctl_desc msm_ioctls[] = {
Daniel Vetterf8c47142015-09-08 13:56:30 +02001335 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
1336 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
1337 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
1338 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
1339 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
1340 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
1341 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark4cd33c42016-05-17 15:44:49 -04001342 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
Alan Kwongbb27c092016-07-20 16:41:25 -04001343 DRM_IOCTL_DEF_DRV(SDE_WB_CONFIG, sde_wb_config, DRM_UNLOCKED|DRM_AUTH),
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07001344 DRM_IOCTL_DEF_DRV(MSM_REGISTER_EVENT, msm_ioctl_register_event,
1345 DRM_UNLOCKED|DRM_CONTROL_ALLOW),
1346 DRM_IOCTL_DEF_DRV(MSM_DEREGISTER_EVENT, msm_ioctl_deregister_event,
1347 DRM_UNLOCKED|DRM_CONTROL_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -04001348};
1349
Rob Clarkc8afe682013-06-26 12:44:06 -04001350static const struct vm_operations_struct vm_ops = {
1351 .fault = msm_gem_fault,
1352 .open = drm_gem_vm_open,
1353 .close = drm_gem_vm_close,
1354};
1355
1356static const struct file_operations fops = {
1357 .owner = THIS_MODULE,
1358 .open = drm_open,
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07001359 .release = msm_release,
Rob Clarkc8afe682013-06-26 12:44:06 -04001360 .unlocked_ioctl = drm_ioctl,
1361#ifdef CONFIG_COMPAT
1362 .compat_ioctl = drm_compat_ioctl,
1363#endif
1364 .poll = drm_poll,
1365 .read = drm_read,
1366 .llseek = no_llseek,
1367 .mmap = msm_gem_mmap,
1368};
1369
1370static struct drm_driver msm_driver = {
Rob Clark05b84912013-09-28 11:28:35 -04001371 .driver_features = DRIVER_HAVE_IRQ |
1372 DRIVER_GEM |
1373 DRIVER_PRIME |
Rob Clarkb4b15c82013-09-28 12:01:25 -04001374 DRIVER_RENDER |
Rob Clarka5436e12015-06-04 10:12:22 -04001375 DRIVER_ATOMIC |
Rob Clark05b84912013-09-28 11:28:35 -04001376 DRIVER_MODESET,
Rob Clark7198e6b2013-07-19 12:59:32 -04001377 .open = msm_open,
Rob Clarkc8afe682013-06-26 12:44:06 -04001378 .preclose = msm_preclose,
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04001379 .postclose = msm_postclose,
Rob Clarkc8afe682013-06-26 12:44:06 -04001380 .lastclose = msm_lastclose,
1381 .irq_handler = msm_irq,
1382 .irq_preinstall = msm_irq_preinstall,
1383 .irq_postinstall = msm_irq_postinstall,
1384 .irq_uninstall = msm_irq_uninstall,
Ville Syrjäläb44f8402015-09-30 16:46:48 +03001385 .get_vblank_counter = drm_vblank_no_hw_counter,
Rob Clarkc8afe682013-06-26 12:44:06 -04001386 .enable_vblank = msm_enable_vblank,
1387 .disable_vblank = msm_disable_vblank,
1388 .gem_free_object = msm_gem_free_object,
1389 .gem_vm_ops = &vm_ops,
1390 .dumb_create = msm_gem_dumb_create,
1391 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark30600a9092013-09-28 10:13:04 -04001392 .dumb_destroy = drm_gem_dumb_destroy,
Rob Clark05b84912013-09-28 11:28:35 -04001393 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1394 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1395 .gem_prime_export = drm_gem_prime_export,
1396 .gem_prime_import = drm_gem_prime_import,
1397 .gem_prime_pin = msm_gem_prime_pin,
1398 .gem_prime_unpin = msm_gem_prime_unpin,
1399 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
1400 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
1401 .gem_prime_vmap = msm_gem_prime_vmap,
1402 .gem_prime_vunmap = msm_gem_prime_vunmap,
Daniel Thompson77a147e2014-11-12 11:38:14 +00001403 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -04001404#ifdef CONFIG_DEBUG_FS
1405 .debugfs_init = msm_debugfs_init,
1406 .debugfs_cleanup = msm_debugfs_cleanup,
1407#endif
Rob Clark7198e6b2013-07-19 12:59:32 -04001408 .ioctls = msm_ioctls,
Jordan Crouse1023e9b2017-03-07 11:14:04 -07001409 .num_ioctls = ARRAY_SIZE(msm_ioctls),
Rob Clarkc8afe682013-06-26 12:44:06 -04001410 .fops = &fops,
Stephane Viauaa6ed8b2016-07-19 12:59:42 -04001411 .name = "msm_drm",
Rob Clarkc8afe682013-06-26 12:44:06 -04001412 .desc = "MSM Snapdragon DRM",
1413 .date = "20130625",
Rob Clarka8d854c2016-06-01 14:02:02 -04001414 .major = MSM_VERSION_MAJOR,
1415 .minor = MSM_VERSION_MINOR,
1416 .patchlevel = MSM_VERSION_PATCHLEVEL,
Rob Clarkc8afe682013-06-26 12:44:06 -04001417};
1418
1419#ifdef CONFIG_PM_SLEEP
1420static int msm_pm_suspend(struct device *dev)
1421{
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001422 struct drm_device *ddev;
1423 struct drm_modeset_acquire_ctx ctx;
1424 struct drm_connector *conn;
1425 struct drm_atomic_state *state;
1426 struct drm_crtc_state *crtc_state;
1427 struct msm_drm_private *priv;
1428 int ret = 0;
Rob Clarkc8afe682013-06-26 12:44:06 -04001429
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001430 if (!dev)
1431 return -EINVAL;
1432
1433 ddev = dev_get_drvdata(dev);
1434 if (!ddev || !ddev->dev_private)
1435 return -EINVAL;
1436
1437 priv = ddev->dev_private;
1438 SDE_EVT32(0);
1439
1440 /* acquire modeset lock(s) */
1441 drm_modeset_acquire_init(&ctx, 0);
1442
1443retry:
1444 ret = drm_modeset_lock_all_ctx(ddev, &ctx);
1445 if (ret)
1446 goto unlock;
1447
1448 /* save current state for resume */
1449 if (priv->suspend_state)
1450 drm_atomic_state_free(priv->suspend_state);
1451 priv->suspend_state = drm_atomic_helper_duplicate_state(ddev, &ctx);
1452 if (IS_ERR_OR_NULL(priv->suspend_state)) {
1453 DRM_ERROR("failed to back up suspend state\n");
1454 priv->suspend_state = NULL;
1455 goto unlock;
1456 }
1457
1458 /* create atomic state to disable all CRTCs */
1459 state = drm_atomic_state_alloc(ddev);
1460 if (IS_ERR_OR_NULL(state)) {
1461 DRM_ERROR("failed to allocate crtc disable state\n");
1462 goto unlock;
1463 }
1464
1465 state->acquire_ctx = &ctx;
1466 drm_for_each_connector(conn, ddev) {
1467
1468 if (!conn->state || !conn->state->crtc ||
1469 conn->dpms != DRM_MODE_DPMS_ON)
1470 continue;
1471
1472 /* force CRTC to be inactive */
1473 crtc_state = drm_atomic_get_crtc_state(state,
1474 conn->state->crtc);
1475 if (IS_ERR_OR_NULL(crtc_state)) {
1476 DRM_ERROR("failed to get crtc %d state\n",
1477 conn->state->crtc->base.id);
1478 drm_atomic_state_free(state);
1479 goto unlock;
1480 }
1481 crtc_state->active = false;
1482 }
1483
1484 /* commit the "disable all" state */
1485 ret = drm_atomic_commit(state);
1486 if (ret < 0) {
1487 DRM_ERROR("failed to disable crtcs, %d\n", ret);
1488 drm_atomic_state_free(state);
Clarence Ipa65cba52017-03-17 15:18:29 -04001489 } else {
1490 priv->suspend_block = true;
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001491 }
1492
1493unlock:
1494 if (ret == -EDEADLK) {
1495 drm_modeset_backoff(&ctx);
1496 goto retry;
1497 }
1498 drm_modeset_drop_locks(&ctx);
1499 drm_modeset_acquire_fini(&ctx);
1500
1501 /* disable hot-plug polling */
Rob Clarkc8afe682013-06-26 12:44:06 -04001502 drm_kms_helper_poll_disable(ddev);
1503
1504 return 0;
1505}
1506
1507static int msm_pm_resume(struct device *dev)
1508{
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001509 struct drm_device *ddev;
1510 struct msm_drm_private *priv;
1511 int ret;
Rob Clarkc8afe682013-06-26 12:44:06 -04001512
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001513 if (!dev)
1514 return -EINVAL;
1515
1516 ddev = dev_get_drvdata(dev);
1517 if (!ddev || !ddev->dev_private)
1518 return -EINVAL;
1519
1520 priv = ddev->dev_private;
1521
1522 SDE_EVT32(priv->suspend_state != NULL);
1523
1524 drm_mode_config_reset(ddev);
1525
1526 drm_modeset_lock_all(ddev);
1527
Clarence Ipa65cba52017-03-17 15:18:29 -04001528 priv->suspend_block = false;
1529
Clarence Ipe5f1f4c2016-11-19 18:02:23 -05001530 if (priv->suspend_state) {
1531 priv->suspend_state->acquire_ctx =
1532 ddev->mode_config.acquire_ctx;
1533 ret = drm_atomic_commit(priv->suspend_state);
1534 if (ret < 0) {
1535 DRM_ERROR("failed to restore state, %d\n", ret);
1536 drm_atomic_state_free(priv->suspend_state);
1537 }
1538 priv->suspend_state = NULL;
1539 }
1540 drm_modeset_unlock_all(ddev);
1541
1542 /* enable hot-plug polling */
Rob Clarkc8afe682013-06-26 12:44:06 -04001543 drm_kms_helper_poll_enable(ddev);
1544
1545 return 0;
1546}
1547#endif
1548
1549static const struct dev_pm_ops msm_pm_ops = {
1550 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
1551};
1552
1553/*
Rob Clark060530f2014-03-03 14:19:12 -05001554 * Componentized driver support:
1555 */
1556
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301557/*
1558 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1559 * so probably some room for some helpers
Rob Clark060530f2014-03-03 14:19:12 -05001560 */
1561static int compare_of(struct device *dev, void *data)
1562{
1563 return dev->of_node == data;
1564}
Rob Clark41e69772013-12-15 16:23:05 -05001565
Archit Taneja812070e2016-05-19 10:38:39 +05301566/*
1567 * Identify what components need to be added by parsing what remote-endpoints
1568 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1569 * is no external component that we need to add since LVDS is within MDP4
1570 * itself.
1571 */
1572static int add_components_mdp(struct device *mdp_dev,
1573 struct component_match **matchptr)
1574{
1575 struct device_node *np = mdp_dev->of_node;
1576 struct device_node *ep_node;
Archit Taneja54011e22016-06-06 13:45:34 +05301577 struct device *master_dev;
1578
1579 /*
1580 * on MDP4 based platforms, the MDP platform device is the component
1581 * master that adds other display interface components to itself.
1582 *
1583 * on MDP5 based platforms, the MDSS platform device is the component
1584 * master that adds MDP5 and other display interface components to
1585 * itself.
1586 */
1587 if (of_device_is_compatible(np, "qcom,mdp4"))
1588 master_dev = mdp_dev;
1589 else
1590 master_dev = mdp_dev->parent;
Archit Taneja812070e2016-05-19 10:38:39 +05301591
1592 for_each_endpoint_of_node(np, ep_node) {
1593 struct device_node *intf;
1594 struct of_endpoint ep;
1595 int ret;
1596
1597 ret = of_graph_parse_endpoint(ep_node, &ep);
1598 if (ret) {
1599 dev_err(mdp_dev, "unable to parse port endpoint\n");
1600 of_node_put(ep_node);
1601 return ret;
1602 }
1603
1604 /*
1605 * The LCDC/LVDS port on MDP4 is a speacial case where the
1606 * remote-endpoint isn't a component that we need to add
1607 */
1608 if (of_device_is_compatible(np, "qcom,mdp4") &&
1609 ep.port == 0) {
1610 of_node_put(ep_node);
1611 continue;
1612 }
1613
1614 /*
1615 * It's okay if some of the ports don't have a remote endpoint
1616 * specified. It just means that the port isn't connected to
1617 * any external interface.
1618 */
1619 intf = of_graph_get_remote_port_parent(ep_node);
1620 if (!intf) {
1621 of_node_put(ep_node);
1622 continue;
1623 }
1624
Archit Taneja54011e22016-06-06 13:45:34 +05301625 component_match_add(master_dev, matchptr, compare_of, intf);
Archit Taneja812070e2016-05-19 10:38:39 +05301626
1627 of_node_put(intf);
1628 of_node_put(ep_node);
1629 }
1630
1631 return 0;
1632}
1633
Archit Taneja54011e22016-06-06 13:45:34 +05301634static int compare_name_mdp(struct device *dev, void *data)
1635{
Dhaval Patel5200c602017-01-17 15:53:37 -08001636 return (strnstr(dev_name(dev), "mdp", strlen("mdp")) != NULL);
1637}
1638
1639static int add_display_components(struct device *dev,
1640 struct component_match **matchptr)
1641{
1642 struct device *mdp_dev = NULL;
Archit Taneja54011e22016-06-06 13:45:34 +05301643 int ret;
1644
Dhaval Patel5200c602017-01-17 15:53:37 -08001645 if (of_device_is_compatible(dev->of_node, "qcom,sde-kms")) {
1646 struct device_node *np = dev->of_node;
1647 unsigned int i;
1648
1649 for (i = 0; ; i++) {
1650 struct device_node *node;
1651
1652 node = of_parse_phandle(np, "connectors", i);
1653 if (!node)
1654 break;
1655
1656 component_match_add(dev, matchptr, compare_of, node);
1657 }
1658 return 0;
1659 }
1660
1661 /*
1662 * MDP5 based devices don't have a flat hierarchy. There is a top level
1663 * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the
1664 * children devices, find the MDP5 node, and then add the interfaces
1665 * to our components list.
1666 */
1667 if (of_device_is_compatible(dev->of_node, "qcom,mdss")) {
1668 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1669 if (ret) {
1670 dev_err(dev, "failed to populate children devices\n");
1671 return ret;
1672 }
1673
1674 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1675 if (!mdp_dev) {
1676 dev_err(dev, "failed to find MDSS MDP node\n");
1677 of_platform_depopulate(dev);
1678 return -ENODEV;
1679 }
1680
1681 put_device(mdp_dev);
1682
1683 /* add the MDP component itself */
1684 component_match_add(dev, matchptr, compare_of,
1685 mdp_dev->of_node);
1686 } else {
1687 /* MDP4 */
1688 mdp_dev = dev;
1689 }
1690
1691 ret = add_components_mdp(mdp_dev, matchptr);
Archit Taneja54011e22016-06-06 13:45:34 +05301692 if (ret)
Dhaval Patel5200c602017-01-17 15:53:37 -08001693 of_platform_depopulate(dev);
Archit Taneja54011e22016-06-06 13:45:34 +05301694
1695 return ret;
Archit Taneja7d526fc2016-05-19 10:33:57 +05301696}
1697
Archit Tanejadc3ea262016-05-19 13:33:52 +05301698/*
1699 * We don't know what's the best binding to link the gpu with the drm device.
1700 * Fow now, we just hunt for all the possible gpus that we support, and add them
1701 * as components.
1702 */
1703static const struct of_device_id msm_gpu_match[] = {
1704 { .compatible = "qcom,adreno-3xx" },
1705 { .compatible = "qcom,kgsl-3d0" },
1706 { },
1707};
1708
Dhaval Patel169bf3a2017-04-11 11:00:57 -07001709#ifdef CONFIG_QCOM_KGSL
1710static int add_gpu_components(struct device *dev,
1711 struct component_match **matchptr)
1712{
1713 return 0;
1714}
1715#else
Archit Taneja7d526fc2016-05-19 10:33:57 +05301716static int add_gpu_components(struct device *dev,
1717 struct component_match **matchptr)
1718{
Archit Tanejadc3ea262016-05-19 13:33:52 +05301719 struct device_node *np;
1720
1721 np = of_find_matching_node(NULL, msm_gpu_match);
1722 if (!np)
1723 return 0;
1724
1725 component_match_add(dev, matchptr, compare_of, np);
1726
1727 of_node_put(np);
1728
1729 return 0;
Archit Taneja7d526fc2016-05-19 10:33:57 +05301730}
Dhaval Patel169bf3a2017-04-11 11:00:57 -07001731#endif
Archit Taneja7d526fc2016-05-19 10:33:57 +05301732
Dhaval Patel5200c602017-01-17 15:53:37 -08001733static int msm_drm_bind(struct device *dev)
Russell King84448282014-04-19 11:20:42 +01001734{
Archit Taneja2b669872016-05-02 11:05:54 +05301735 return msm_drm_init(dev, &msm_driver);
Russell King84448282014-04-19 11:20:42 +01001736}
1737
Dhaval Patel5200c602017-01-17 15:53:37 -08001738static void msm_drm_unbind(struct device *dev)
Russell King84448282014-04-19 11:20:42 +01001739{
Archit Taneja2b669872016-05-02 11:05:54 +05301740 msm_drm_uninit(dev);
Russell King84448282014-04-19 11:20:42 +01001741}
Dhaval Patel5200c602017-01-17 15:53:37 -08001742
1743static const struct component_master_ops msm_drm_ops = {
1744 .bind = msm_drm_bind,
1745 .unbind = msm_drm_unbind,
1746};
Russell King84448282014-04-19 11:20:42 +01001747
1748/*
1749 * Platform driver:
1750 */
1751
1752static int msm_pdev_probe(struct platform_device *pdev)
1753{
Dhaval Patel3949f032016-06-20 16:24:33 -07001754 int ret;
Russell King84448282014-04-19 11:20:42 +01001755 struct component_match *match = NULL;
Lloyd Atkinson6f74f402016-10-04 10:07:36 -04001756
Archit Taneja7d526fc2016-05-19 10:33:57 +05301757 ret = add_display_components(&pdev->dev, &match);
1758 if (ret)
1759 return ret;
1760
Dhaval Patel5200c602017-01-17 15:53:37 -08001761 ret = add_gpu_components(&pdev->dev, &match);
1762 if (ret)
1763 return ret;
Dhaval Patel3949f032016-06-20 16:24:33 -07001764
Dhaval Patel5200c602017-01-17 15:53:37 -08001765 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1766 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
Rob Clarkc8afe682013-06-26 12:44:06 -04001767}
1768
1769static int msm_pdev_remove(struct platform_device *pdev)
1770{
Lloyd Atkinson6f74f402016-10-04 10:07:36 -04001771 component_master_del(&pdev->dev, &msm_drm_ops);
Dhaval Patel5200c602017-01-17 15:53:37 -08001772 of_platform_depopulate(&pdev->dev);
1773
1774 msm_drm_unbind(&pdev->dev);
Rob Clarkc8afe682013-06-26 12:44:06 -04001775 return 0;
1776}
1777
Rob Clark06c0dd92013-11-30 17:51:47 -05001778static const struct of_device_id dt_match[] = {
Dhaval Patel5200c602017-01-17 15:53:37 -08001779 { .compatible = "qcom,mdp4", .data = (void *)4 }, /* MDP4 */
1780 { .compatible = "qcom,mdss", .data = (void *)5 }, /* MDP5 MDSS */
1781 { .compatible = "qcom,sde-kms", .data = (void *)3 }, /* sde */
Rob Clark06c0dd92013-11-30 17:51:47 -05001782 {}
1783};
1784MODULE_DEVICE_TABLE(of, dt_match);
1785
Rob Clarkc8afe682013-06-26 12:44:06 -04001786static struct platform_driver msm_platform_driver = {
1787 .probe = msm_pdev_probe,
1788 .remove = msm_pdev_remove,
1789 .driver = {
Stephane Viauaa6ed8b2016-07-19 12:59:42 -04001790 .name = "msm_drm",
Rob Clark06c0dd92013-11-30 17:51:47 -05001791 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -04001792 .pm = &msm_pm_ops,
1793 },
Rob Clarkc8afe682013-06-26 12:44:06 -04001794};
1795
Stephane Viau32f13f62015-04-29 15:57:29 -04001796#ifdef CONFIG_QCOM_KGSL
1797void __init adreno_register(void)
1798{
1799}
1800
1801void __exit adreno_unregister(void)
1802{
1803}
1804#endif
1805
Rob Clarkc8afe682013-06-26 12:44:06 -04001806static int __init msm_drm_register(void)
1807{
1808 DBG("init");
Hai Lid5af49c2015-03-26 19:25:17 -04001809 msm_dsi_register();
Hai Li00453982014-12-12 14:41:17 -05001810 msm_edp_register();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001811 msm_hdmi_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001812 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001813 return platform_driver_register(&msm_platform_driver);
1814}
1815
1816static void __exit msm_drm_unregister(void)
1817{
1818 DBG("fini");
1819 platform_driver_unregister(&msm_platform_driver);
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001820 msm_hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001821 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -05001822 msm_edp_unregister();
Hai Lid5af49c2015-03-26 19:25:17 -04001823 msm_dsi_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -04001824}
1825
1826module_init(msm_drm_register);
1827module_exit(msm_drm_unregister);
1828
1829MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1830MODULE_DESCRIPTION("MSM DRM Driver");
1831MODULE_LICENSE("GPL");