blob: 6265be8f4119304c5a2799775f0274e8bfc3b3a6 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04002 * Copyright (c) 2016-2018, 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
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -040019#include <linux/kthread.h>
20#include <uapi/linux/sched/types.h>
Russell King97ac0e42016-10-19 11:28:27 +010021#include <drm/drm_of.h>
22
Rob Clarkc8afe682013-06-26 12:44:06 -040023#include "msm_drv.h"
Rob Clarkedcd60c2016-03-16 12:56:12 -040024#include "msm_debugfs.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040025#include "msm_fence.h"
Rob Clarkf05c83e2018-11-29 10:27:22 -050026#include "msm_gem.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040027#include "msm_gpu.h"
Rob Clarkdd2da6e2013-11-30 16:12:10 -050028#include "msm_kms.h"
Jonathan Marekc2052a42018-11-14 17:08:04 -050029#include "adreno/adreno_gpu.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040030
Rob Clarka8d854c2016-06-01 14:02:02 -040031
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
Rob Clark7a3bcc02016-09-16 18:37:44 -040036 * - 1.2.0 - adds explicit fence support for submit ioctl
Jordan Crousef7de1542017-10-20 11:06:55 -060037 * - 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.
Rob Clark1fed8df2018-11-29 10:30:04 -050040 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
41 * GEM object's debug name
Rob Clarka8d854c2016-06-01 14:02:02 -040042 */
43#define MSM_VERSION_MAJOR 1
Rob Clark1fed8df2018-11-29 10:30:04 -050044#define MSM_VERSION_MINOR 4
Rob Clarka8d854c2016-06-01 14:02:02 -040045#define MSM_VERSION_PATCHLEVEL 0
46
Rob Clarkc8afe682013-06-26 12:44:06 -040047static const struct drm_mode_config_funcs mode_config_funcs = {
48 .fb_create = msm_framebuffer_create,
Noralf Trønnes4ccbc6e2017-12-05 19:24:59 +010049 .output_poll_changed = drm_fb_helper_output_poll_changed,
Rob Clark1f920172017-10-25 12:30:51 -040050 .atomic_check = drm_atomic_helper_check,
Sean Pauld14659f2018-02-28 14:19:05 -050051 .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,
Rob Clarkc8afe682013-06-26 12:44:06 -040056};
57
Rob Clarkc8afe682013-06-26 12:44:06 -040058#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
Archit Tanejaa9ee34b2015-07-13 12:12:07 +053066#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarke90dfec2015-01-30 17:05:41 -050067static bool fbdev = true;
68MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
69module_param(fbdev, bool, 0600);
70#endif
71
Rob Clark3a10ba82014-09-08 14:24:57 -040072static char *vram = "16m";
Rob Clark4313c7442016-02-03 14:02:04 -050073MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
Rob Clark871d8122013-11-16 12:56:06 -050074module_param(vram, charp, 0);
75
Rob Clark06d9f562016-11-05 11:08:12 -040076bool dumpstate = false;
77MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
78module_param(dumpstate, bool, 0600);
79
Rob Clarkba4dd712017-07-06 16:33:44 -040080static bool modeset = true;
81MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
82module_param(modeset, bool, 0600);
83
Rob Clark060530f2014-03-03 14:19:12 -050084/*
85 * Util/helpers:
86 */
87
Jordan Crouse8e54eea2018-08-06 11:33:21 -060088int 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
Rob Clark720c3bb2017-01-30 11:30:58 -0500145struct 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
Rob Clarkc8afe682013-06-26 12:44:06 -0400164void __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) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530177 DRM_DEV_ERROR(&pdev->dev, "failed to get memory resource: %s\n", name);
Rob Clarkc8afe682013-06-26 12:44:06 -0400178 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) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530185 DRM_DEV_ERROR(&pdev->dev, "failed to ioremap: %s\n", name);
Rob Clarkc8afe682013-06-26 12:44:06 -0400186 return ERR_PTR(-ENOMEM);
187 }
188
189 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200190 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400191
192 return ptr;
193}
194
195void msm_writel(u32 data, void __iomem *addr)
196{
197 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200198 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
Rob Clarkc8afe682013-06-26 12:44:06 -0400199 writel(data, addr);
200}
201
202u32 msm_readl(const void __iomem *addr)
203{
204 u32 val = readl(addr);
205 if (reglog)
Joe Perches8dfe1622017-02-28 04:55:54 -0800206 pr_err("IO:R %p %08x\n", addr, val);
Rob Clarkc8afe682013-06-26 12:44:06 -0400207 return val;
208}
209
Hai Li78b1d472015-07-27 13:49:45 -0400210struct vblank_event {
211 struct list_head node;
212 int crtc_id;
213 bool enable;
214};
215
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400216static void vblank_ctrl_worker(struct kthread_work *work)
Hai Li78b1d472015-07-27 13:49:45 -0400217{
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
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400264 kthread_queue_work(&priv->disp_thread[crtc_id].worker,
265 &vbl_ctrl->work);
Hai Li78b1d472015-07-27 13:49:45 -0400266
267 return 0;
268}
269
Archit Taneja2b669872016-05-02 11:05:54 +0530270static int msm_drm_uninit(struct device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400271{
Archit Taneja2b669872016-05-02 11:05:54 +0530272 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;
Rob Clarkc8afe682013-06-26 12:44:06 -0400275 struct msm_kms *kms = priv->kms;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400276 struct msm_mdss *mdss = priv->mdss;
Hai Li78b1d472015-07-27 13:49:45 -0400277 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl;
278 struct vblank_event *vbl_ev, *tmp;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400279 int i;
Hai Li78b1d472015-07-27 13:49:45 -0400280
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 */
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400285 kthread_flush_work(&vbl_ctrl->work);
Hai Li78b1d472015-07-27 13:49:45 -0400286 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) {
287 list_del(&vbl_ev->node);
288 kfree(vbl_ev);
289 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400290
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400291 /* 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
Rob Clark68209392016-05-17 16:19:32 -0400306 msm_gem_shrinker_cleanup(ddev);
307
Archit Taneja2b669872016-05-02 11:05:54 +0530308 drm_kms_helper_poll_fini(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530309
Archit Taneja2b669872016-05-02 11:05:54 +0530310 drm_dev_unregister(ddev);
Archit Taneja8208ed92016-05-02 11:05:53 +0530311
Noralf Trønnes85eac472017-03-07 21:49:22 +0100312 msm_perf_debugfs_cleanup(priv);
313 msm_rd_debugfs_cleanup(priv);
314
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530315#ifdef CONFIG_DRM_FBDEV_EMULATION
316 if (fbdev && priv->fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530317 msm_fbdev_free(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530318#endif
Daniel Vetter3ea4b1e2018-10-04 22:24:36 +0200319 drm_atomic_helper_shutdown(ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530320 drm_mode_config_cleanup(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400321
Archit Taneja2b669872016-05-02 11:05:54 +0530322 pm_runtime_get_sync(dev);
323 drm_irq_uninstall(ddev);
324 pm_runtime_put_sync(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400325
326 flush_workqueue(priv->wq);
327 destroy_workqueue(priv->wq);
328
Archit Taneja16976082016-11-03 17:36:18 +0530329 if (kms && kms->funcs)
Rob Clarkc8afe682013-06-26 12:44:06 -0400330 kms->funcs->destroy(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400331
Rob Clark871d8122013-11-16 12:56:06 -0500332 if (priv->vram.paddr) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700333 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
Rob Clark871d8122013-11-16 12:56:06 -0500334 drm_mm_takedown(&priv->vram.mm);
Archit Taneja2b669872016-05-02 11:05:54 +0530335 dma_free_attrs(dev, priv->vram.size, NULL,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700336 priv->vram.paddr, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500337 }
338
Archit Taneja2b669872016-05-02 11:05:54 +0530339 component_unbind_all(dev, ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500340
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400341 if (mdss && mdss->funcs)
342 mdss->funcs->destroy(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530343
Archit Taneja2b669872016-05-02 11:05:54 +0530344 ddev->dev_private = NULL;
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200345 drm_dev_put(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400346
347 kfree(priv);
348
349 return 0;
350}
351
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400352#define KMS_MDP4 4
353#define KMS_MDP5 5
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400354#define KMS_DPU 3
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400355
Rob Clark06c0dd92013-11-30 17:51:47 -0500356static int get_mdp_ver(struct platform_device *pdev)
357{
Rob Clark06c0dd92013-11-30 17:51:47 -0500358 struct device *dev = &pdev->dev;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530359
360 return (int) (unsigned long) of_device_get_match_data(dev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500361}
362
Rob Clark072f1f92015-03-03 15:04:25 -0500363#include <linux/of_address.h>
364
Jonathan Marekc2052a42018-11-14 17:08:04 -0500365bool 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
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500373static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400374{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500375 struct msm_drm_private *priv = dev->dev_private;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530376 struct device_node *node;
Rob Clark072f1f92015-03-03 15:04:25 -0500377 unsigned long size = 0;
378 int ret = 0;
379
Rob Clark072f1f92015-03-03 15:04:25 -0500380 /* 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 */
Rob Clark072f1f92015-03-03 15:04:25 -0500396
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);
Peter Chen2ca41c172016-07-04 16:49:50 +0800401 of_node_put(node);
Rob Clark072f1f92015-03-03 15:04:25 -0500402 if (ret)
403 return ret;
404 size = r.end - r.start;
Thierry Redingfc99f972015-04-09 16:39:51 +0200405 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
Rob Clarkc8afe682013-06-26 12:44:06 -0400406
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530407 /* 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 */
Jonathan Marekc2052a42018-11-14 17:08:04 -0500411 } else if (!msm_use_mmu(dev)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500412 DRM_INFO("using %s VRAM carveout\n", vram);
413 size = memparse(vram, NULL);
414 }
415
416 if (size) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700417 unsigned long attrs = 0;
Rob Clark871d8122013-11-16 12:56:06 -0500418 void *p;
419
Rob Clark871d8122013-11-16 12:56:06 -0500420 priv->vram.size = size;
421
422 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600423 spin_lock_init(&priv->vram.lock);
Rob Clark871d8122013-11-16 12:56:06 -0500424
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700425 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
426 attrs |= DMA_ATTR_WRITE_COMBINE;
Rob Clark871d8122013-11-16 12:56:06 -0500427
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,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700432 &priv->vram.paddr, GFP_KERNEL, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500433 if (!p) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530434 DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
Rob Clark871d8122013-11-16 12:56:06 -0500435 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500436 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500437 }
438
Mamta Shukla6a41da12018-10-20 23:19:26 +0530439 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
Rob Clark871d8122013-11-16 12:56:06 -0500440 (uint32_t)priv->vram.paddr,
441 (uint32_t)(priv->vram.paddr + size));
442 }
443
Rob Clark072f1f92015-03-03 15:04:25 -0500444 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500445}
446
Archit Taneja2b669872016-05-02 11:05:54 +0530447static int msm_drm_init(struct device *dev, struct drm_driver *drv)
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500448{
Archit Taneja2b669872016-05-02 11:05:54 +0530449 struct platform_device *pdev = to_platform_device(dev);
450 struct drm_device *ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500451 struct msm_drm_private *priv;
452 struct msm_kms *kms;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400453 struct msm_mdss *mdss;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400454 int ret, i;
455 struct sched_param param;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500456
Archit Taneja2b669872016-05-02 11:05:54 +0530457 ddev = drm_dev_alloc(drv, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200458 if (IS_ERR(ddev)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530459 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
Tom Gundersen0f288602016-09-21 16:59:19 +0200460 return PTR_ERR(ddev);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500461 }
462
Archit Taneja2b669872016-05-02 11:05:54 +0530463 platform_set_drvdata(pdev, ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530464
465 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
466 if (!priv) {
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400467 ret = -ENOMEM;
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200468 goto err_put_drm_dev;
Archit Taneja2b669872016-05-02 11:05:54 +0530469 }
470
471 ddev->dev_private = priv;
Rob Clark68209392016-05-17 16:19:32 -0400472 priv->dev = ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500473
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400474 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 }
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400485 if (ret)
486 goto err_free_priv;
Archit Taneja0a6030d2016-05-08 21:36:28 +0530487
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400488 mdss = priv->mdss;
489
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500490 priv->wq = alloc_ordered_workqueue("msm", 0);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500491
492 INIT_LIST_HEAD(&priv->inactive_list);
Hai Li78b1d472015-07-27 13:49:45 -0400493 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list);
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400494 kthread_init_work(&priv->vblank_ctrl.work, vblank_ctrl_worker);
Hai Li78b1d472015-07-27 13:49:45 -0400495 spin_lock_init(&priv->vblank_ctrl.lock);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500496
Archit Taneja2b669872016-05-02 11:05:54 +0530497 drm_mode_config_init(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500498
499 /* Bind all our sub-components: */
Archit Taneja2b669872016-05-02 11:05:54 +0530500 ret = component_bind_all(dev, ddev);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400501 if (ret)
502 goto err_destroy_mdss;
Rob Clark060530f2014-03-03 14:19:12 -0500503
Archit Taneja2b669872016-05-02 11:05:54 +0530504 ret = msm_init_vram(ddev);
Rob Clark13f15562015-05-07 15:20:13 -0400505 if (ret)
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400506 goto err_msm_uninit;
Rob Clark13f15562015-05-07 15:20:13 -0400507
Rob Clark68209392016-05-17 16:19:32 -0400508 msm_gem_shrinker_init(ddev);
509
Rob Clark06c0dd92013-11-30 17:51:47 -0500510 switch (get_mdp_ver(pdev)) {
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400511 case KMS_MDP4:
Archit Taneja2b669872016-05-02 11:05:54 +0530512 kms = mdp4_kms_init(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530513 priv->kms = kms;
Rob Clark06c0dd92013-11-30 17:51:47 -0500514 break;
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400515 case KMS_MDP5:
Archit Taneja392ae6e2016-06-14 18:24:54 +0530516 kms = mdp5_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500517 break;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400518 case KMS_DPU:
519 kms = dpu_kms_init(ddev);
520 priv->kms = kms;
521 break;
Rob Clark06c0dd92013-11-30 17:51:47 -0500522 default:
Jonathan Mareke6f6d632018-12-04 10:16:58 -0500523 /* valid only for the dummy headless case, where of_node=NULL */
524 WARN_ON(dev->of_node);
525 kms = NULL;
Rob Clark06c0dd92013-11-30 17:51:47 -0500526 break;
527 }
528
Rob Clarkc8afe682013-06-26 12:44:06 -0400529 if (IS_ERR(kms)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530530 DRM_DEV_ERROR(dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200531 ret = PTR_ERR(kms);
Jonathan Marekb2ccfdf2018-11-21 20:52:35 -0500532 priv->kms = NULL;
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400533 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400534 }
535
Jeykumar Sankaranbb676df2018-06-11 14:13:20 -0700536 /* Enable normalization of plane zpos */
537 ddev->mode_config.normalize_zpos = true;
538
Rob Clarkc8afe682013-06-26 12:44:06 -0400539 if (kms) {
Rob Clarkc8afe682013-06-26 12:44:06 -0400540 ret = kms->funcs->hw_init(kms);
541 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530542 DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400543 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400544 }
545 }
546
Archit Taneja2b669872016-05-02 11:05:54 +0530547 ddev->mode_config.funcs = &mode_config_funcs;
Sean Pauld14659f2018-02-28 14:19:05 -0500548 ddev->mode_config.helper_private = &mode_config_helper_funcs;
Rob Clarkc8afe682013-06-26 12:44:06 -0400549
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400550 /**
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)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530573 DRM_DEV_ERROR(dev, "failed to create crtc_commit kthread\n");
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400574 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);
Mamta Shukla6a41da12018-10-20 23:19:26 +0530585
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400586 /**
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
Archit Taneja2b669872016-05-02 11:05:54 +0530624 ret = drm_vblank_init(ddev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400625 if (ret < 0) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530626 DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400627 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400628 }
629
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530630 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) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530635 DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400636 goto err_msm_uninit;
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530637 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400638 }
639
Archit Taneja2b669872016-05-02 11:05:54 +0530640 ret = drm_dev_register(ddev, 0);
Rob Clarka7d3c952014-05-30 14:47:38 -0400641 if (ret)
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400642 goto err_msm_uninit;
Rob Clarka7d3c952014-05-30 14:47:38 -0400643
Archit Taneja2b669872016-05-02 11:05:54 +0530644 drm_mode_config_reset(ddev);
645
646#ifdef CONFIG_DRM_FBDEV_EMULATION
Jonathan Mareke6f6d632018-12-04 10:16:58 -0500647 if (kms && fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530648 priv->fbdev = msm_fbdev_init(ddev);
649#endif
650
651 ret = msm_debugfs_late_init(ddev);
652 if (ret)
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400653 goto err_msm_uninit;
Archit Taneja2b669872016-05-02 11:05:54 +0530654
655 drm_kms_helper_poll_init(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400656
657 return 0;
658
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400659err_msm_uninit:
Archit Taneja2b669872016-05-02 11:05:54 +0530660 msm_drm_uninit(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400661 return ret;
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400662err_destroy_mdss:
663 if (mdss && mdss->funcs)
664 mdss->funcs->destroy(ddev);
665err_free_priv:
666 kfree(priv);
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200667err_put_drm_dev:
668 drm_dev_put(ddev);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400669 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400670}
671
Archit Taneja2b669872016-05-02 11:05:54 +0530672/*
673 * DRM operations:
674 */
675
Rob Clark7198e6b2013-07-19 12:59:32 -0400676static void load_gpu(struct drm_device *dev)
677{
Rob Clarka1ad3522014-07-11 11:59:22 -0400678 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400679 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400680
Rob Clarka1ad3522014-07-11 11:59:22 -0400681 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400682
Rob Clarke2550b72014-09-05 13:30:27 -0400683 if (!priv->gpu)
684 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400685
Rob Clarka1ad3522014-07-11 11:59:22 -0400686 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400687}
688
Jordan Crousef97deca2017-10-20 11:06:57 -0600689static int context_init(struct drm_device *dev, struct drm_file *file)
Rob Clark7198e6b2013-07-19 12:59:32 -0400690{
691 struct msm_file_private *ctx;
692
Rob Clark7198e6b2013-07-19 12:59:32 -0400693 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
694 if (!ctx)
695 return -ENOMEM;
696
Jordan Crousef97deca2017-10-20 11:06:57 -0600697 msm_submitqueue_init(dev, ctx);
Jordan Crousef7de1542017-10-20 11:06:55 -0600698
Rob Clark7198e6b2013-07-19 12:59:32 -0400699 file->driver_priv = ctx;
700
701 return 0;
702}
703
Jordan Crousef7de1542017-10-20 11:06:55 -0600704static 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
Jordan Crousef97deca2017-10-20 11:06:57 -0600711 return context_init(dev, file);
Jordan Crousef7de1542017-10-20 11:06:55 -0600712}
713
714static void context_close(struct msm_file_private *ctx)
715{
716 msm_submitqueue_close(ctx);
717 kfree(ctx);
718}
719
Daniel Vetter94df1452017-03-08 15:12:46 +0100720static void msm_postclose(struct drm_device *dev, struct drm_file *file)
Rob Clarkc8afe682013-06-26 12:44:06 -0400721{
722 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400723 struct msm_file_private *ctx = file->driver_priv;
Rob Clark7198e6b2013-07-19 12:59:32 -0400724
Rob Clark7198e6b2013-07-19 12:59:32 -0400725 mutex_lock(&dev->struct_mutex);
726 if (ctx == priv->lastctx)
727 priv->lastctx = NULL;
728 mutex_unlock(&dev->struct_mutex);
729
Jordan Crousef7de1542017-10-20 11:06:55 -0600730 context_close(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400731}
732
Daniel Vettere9f0d762013-12-11 11:34:42 +0100733static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400734{
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);
Jordan Crouseab07e0c2018-12-03 15:47:19 -0700755
756 if (kms->funcs->irq_postinstall)
757 return kms->funcs->irq_postinstall(kms);
758
759 return 0;
Rob Clarkc8afe682013-06-26 12:44:06 -0400760}
761
762static void msm_irq_uninstall(struct drm_device *dev)
763{
764 struct msm_drm_private *priv = dev->dev_private;
765 struct msm_kms *kms = priv->kms;
766 BUG_ON(!kms);
767 kms->funcs->irq_uninstall(kms);
768}
769
Thierry Reding88e72712015-09-24 18:35:31 +0200770static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400771{
772 struct msm_drm_private *priv = dev->dev_private;
773 struct msm_kms *kms = priv->kms;
774 if (!kms)
775 return -ENXIO;
Thierry Reding88e72712015-09-24 18:35:31 +0200776 DBG("dev=%p, crtc=%u", dev, pipe);
777 return vblank_ctrl_queue_work(priv, pipe, true);
Rob Clarkc8afe682013-06-26 12:44:06 -0400778}
779
Thierry Reding88e72712015-09-24 18:35:31 +0200780static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clarkc8afe682013-06-26 12:44:06 -0400781{
782 struct msm_drm_private *priv = dev->dev_private;
783 struct msm_kms *kms = priv->kms;
784 if (!kms)
785 return;
Thierry Reding88e72712015-09-24 18:35:31 +0200786 DBG("dev=%p, crtc=%u", dev, pipe);
787 vblank_ctrl_queue_work(priv, pipe, false);
Rob Clarkc8afe682013-06-26 12:44:06 -0400788}
789
790/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400791 * DRM ioctls:
792 */
793
794static int msm_ioctl_get_param(struct drm_device *dev, void *data,
795 struct drm_file *file)
796{
797 struct msm_drm_private *priv = dev->dev_private;
798 struct drm_msm_param *args = data;
799 struct msm_gpu *gpu;
800
801 /* for now, we just have 3d pipe.. eventually this would need to
802 * be more clever to dispatch to appropriate gpu module:
803 */
804 if (args->pipe != MSM_PIPE_3D0)
805 return -EINVAL;
806
807 gpu = priv->gpu;
808
809 if (!gpu)
810 return -ENXIO;
811
812 return gpu->funcs->get_param(gpu, args->param, &args->value);
813}
814
815static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
816 struct drm_file *file)
817{
818 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500819
820 if (args->flags & ~MSM_BO_FLAGS) {
821 DRM_ERROR("invalid flags: %08x\n", args->flags);
822 return -EINVAL;
823 }
824
Rob Clark7198e6b2013-07-19 12:59:32 -0400825 return msm_gem_new_handle(dev, file, args->size,
Jordan Crouse0815d772018-11-07 15:35:52 -0700826 args->flags, &args->handle, NULL);
Rob Clark7198e6b2013-07-19 12:59:32 -0400827}
828
Rob Clark56c2da82015-05-11 11:50:03 -0400829static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
830{
831 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
832}
Rob Clark7198e6b2013-07-19 12:59:32 -0400833
834static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
835 struct drm_file *file)
836{
837 struct drm_msm_gem_cpu_prep *args = data;
838 struct drm_gem_object *obj;
Rob Clark56c2da82015-05-11 11:50:03 -0400839 ktime_t timeout = to_ktime(args->timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400840 int ret;
841
Rob Clark93ddb0d2014-03-03 09:42:33 -0500842 if (args->op & ~MSM_PREP_FLAGS) {
843 DRM_ERROR("invalid op: %08x\n", args->op);
844 return -EINVAL;
845 }
846
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100847 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400848 if (!obj)
849 return -ENOENT;
850
Rob Clark56c2da82015-05-11 11:50:03 -0400851 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400852
Steve Kowalikdc9a9b32018-01-26 14:55:54 +1100853 drm_gem_object_put_unlocked(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400854
855 return ret;
856}
857
858static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
859 struct drm_file *file)
860{
861 struct drm_msm_gem_cpu_fini *args = data;
862 struct drm_gem_object *obj;
863 int ret;
864
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100865 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400866 if (!obj)
867 return -ENOENT;
868
869 ret = msm_gem_cpu_fini(obj);
870
Steve Kowalikdc9a9b32018-01-26 14:55:54 +1100871 drm_gem_object_put_unlocked(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400872
873 return ret;
874}
875
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600876static int msm_ioctl_gem_info_iova(struct drm_device *dev,
877 struct drm_gem_object *obj, uint64_t *iova)
878{
879 struct msm_drm_private *priv = dev->dev_private;
880
881 if (!priv->gpu)
882 return -EINVAL;
883
Jordan Crouse9fe041f2018-11-07 15:35:50 -0700884 /*
885 * Don't pin the memory here - just get an address so that userspace can
886 * be productive
887 */
Rob Clark8bdcd942017-06-13 11:07:08 -0400888 return msm_gem_get_iova(obj, priv->gpu->aspace, iova);
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600889}
890
Rob Clark7198e6b2013-07-19 12:59:32 -0400891static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
892 struct drm_file *file)
893{
894 struct drm_msm_gem_info *args = data;
895 struct drm_gem_object *obj;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500896 struct msm_gem_object *msm_obj;
897 int i, ret = 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400898
Rob Clark789d2e52018-11-29 09:54:42 -0500899 if (args->pad)
Rob Clark7198e6b2013-07-19 12:59:32 -0400900 return -EINVAL;
901
Rob Clark789d2e52018-11-29 09:54:42 -0500902 switch (args->info) {
903 case MSM_INFO_GET_OFFSET:
904 case MSM_INFO_GET_IOVA:
905 /* value returned as immediate, not pointer, so len==0: */
906 if (args->len)
907 return -EINVAL;
908 break;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500909 case MSM_INFO_SET_NAME:
910 case MSM_INFO_GET_NAME:
911 break;
Rob Clark789d2e52018-11-29 09:54:42 -0500912 default:
913 return -EINVAL;
914 }
915
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100916 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400917 if (!obj)
918 return -ENOENT;
919
Rob Clarkf05c83e2018-11-29 10:27:22 -0500920 msm_obj = to_msm_bo(obj);
921
Rob Clark789d2e52018-11-29 09:54:42 -0500922 switch (args->info) {
923 case MSM_INFO_GET_OFFSET:
924 args->value = msm_gem_mmap_offset(obj);
925 break;
926 case MSM_INFO_GET_IOVA:
927 ret = msm_ioctl_gem_info_iova(dev, obj, &args->value);
928 break;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500929 case MSM_INFO_SET_NAME:
930 /* length check should leave room for terminating null: */
931 if (args->len >= sizeof(msm_obj->name)) {
932 ret = -EINVAL;
933 break;
934 }
935 ret = copy_from_user(msm_obj->name,
936 u64_to_user_ptr(args->value), args->len);
937 msm_obj->name[args->len] = '\0';
938 for (i = 0; i < args->len; i++) {
939 if (!isprint(msm_obj->name[i])) {
940 msm_obj->name[i] = '\0';
941 break;
942 }
943 }
944 break;
945 case MSM_INFO_GET_NAME:
946 if (args->value && (args->len < strlen(msm_obj->name))) {
947 ret = -EINVAL;
948 break;
949 }
950 args->len = strlen(msm_obj->name);
951 if (args->value) {
952 ret = copy_to_user(u64_to_user_ptr(args->value),
953 msm_obj->name, args->len);
954 }
955 break;
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600956 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400957
Steve Kowalikdc9a9b32018-01-26 14:55:54 +1100958 drm_gem_object_put_unlocked(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400959
960 return ret;
961}
962
963static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
964 struct drm_file *file)
965{
Rob Clarkca762a82016-03-15 17:22:13 -0400966 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400967 struct drm_msm_wait_fence *args = data;
Rob Clark56c2da82015-05-11 11:50:03 -0400968 ktime_t timeout = to_ktime(args->timeout);
Jordan Crousef97deca2017-10-20 11:06:57 -0600969 struct msm_gpu_submitqueue *queue;
970 struct msm_gpu *gpu = priv->gpu;
971 int ret;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500972
973 if (args->pad) {
974 DRM_ERROR("invalid pad: %08x\n", args->pad);
975 return -EINVAL;
976 }
977
Jordan Crousef97deca2017-10-20 11:06:57 -0600978 if (!gpu)
Rob Clarkca762a82016-03-15 17:22:13 -0400979 return 0;
980
Jordan Crousef97deca2017-10-20 11:06:57 -0600981 queue = msm_submitqueue_get(file->driver_priv, args->queueid);
982 if (!queue)
983 return -ENOENT;
984
985 ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
986 true);
987
988 msm_submitqueue_put(queue);
989 return ret;
Rob Clark7198e6b2013-07-19 12:59:32 -0400990}
991
Rob Clark4cd33c42016-05-17 15:44:49 -0400992static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
993 struct drm_file *file)
994{
995 struct drm_msm_gem_madvise *args = data;
996 struct drm_gem_object *obj;
997 int ret;
998
999 switch (args->madv) {
1000 case MSM_MADV_DONTNEED:
1001 case MSM_MADV_WILLNEED:
1002 break;
1003 default:
1004 return -EINVAL;
1005 }
1006
1007 ret = mutex_lock_interruptible(&dev->struct_mutex);
1008 if (ret)
1009 return ret;
1010
1011 obj = drm_gem_object_lookup(file, args->handle);
1012 if (!obj) {
1013 ret = -ENOENT;
1014 goto unlock;
1015 }
1016
1017 ret = msm_gem_madvise(obj, args->madv);
1018 if (ret >= 0) {
1019 args->retained = ret;
1020 ret = 0;
1021 }
1022
Steve Kowalikdc9a9b32018-01-26 14:55:54 +11001023 drm_gem_object_put(obj);
Rob Clark4cd33c42016-05-17 15:44:49 -04001024
1025unlock:
1026 mutex_unlock(&dev->struct_mutex);
1027 return ret;
1028}
1029
Jordan Crousef7de1542017-10-20 11:06:55 -06001030
1031static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
1032 struct drm_file *file)
1033{
1034 struct drm_msm_submitqueue *args = data;
1035
1036 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
1037 return -EINVAL;
1038
Jordan Crousef97deca2017-10-20 11:06:57 -06001039 return msm_submitqueue_create(dev, file->driver_priv, args->prio,
Jordan Crousef7de1542017-10-20 11:06:55 -06001040 args->flags, &args->id);
1041}
1042
1043
1044static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
1045 struct drm_file *file)
1046{
1047 u32 id = *(u32 *) data;
1048
1049 return msm_submitqueue_remove(file->driver_priv, id);
1050}
1051
Rob Clark7198e6b2013-07-19 12:59:32 -04001052static const struct drm_ioctl_desc msm_ioctls[] = {
Daniel Vetterf8c47142015-09-08 13:56:30 +02001053 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW),
1054 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW),
1055 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW),
1056 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
1057 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
1058 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW),
1059 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark4cd33c42016-05-17 15:44:49 -04001060 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW),
Jordan Crousef7de1542017-10-20 11:06:55 -06001061 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_AUTH|DRM_RENDER_ALLOW),
1062 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_AUTH|DRM_RENDER_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -04001063};
1064
Rob Clarkc8afe682013-06-26 12:44:06 -04001065static const struct vm_operations_struct vm_ops = {
1066 .fault = msm_gem_fault,
1067 .open = drm_gem_vm_open,
1068 .close = drm_gem_vm_close,
1069};
1070
1071static const struct file_operations fops = {
1072 .owner = THIS_MODULE,
1073 .open = drm_open,
1074 .release = drm_release,
1075 .unlocked_ioctl = drm_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -04001076 .compat_ioctl = drm_compat_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -04001077 .poll = drm_poll,
1078 .read = drm_read,
1079 .llseek = no_llseek,
1080 .mmap = msm_gem_mmap,
1081};
1082
1083static struct drm_driver msm_driver = {
Rob Clark05b84912013-09-28 11:28:35 -04001084 .driver_features = DRIVER_HAVE_IRQ |
1085 DRIVER_GEM |
1086 DRIVER_PRIME |
Rob Clarkb4b15c82013-09-28 12:01:25 -04001087 DRIVER_RENDER |
Rob Clarka5436e12015-06-04 10:12:22 -04001088 DRIVER_ATOMIC |
Rob Clark05b84912013-09-28 11:28:35 -04001089 DRIVER_MODESET,
Rob Clark7198e6b2013-07-19 12:59:32 -04001090 .open = msm_open,
Daniel Vetter94df1452017-03-08 15:12:46 +01001091 .postclose = msm_postclose,
Noralf Trønnes4ccbc6e2017-12-05 19:24:59 +01001092 .lastclose = drm_fb_helper_lastclose,
Rob Clarkc8afe682013-06-26 12:44:06 -04001093 .irq_handler = msm_irq,
1094 .irq_preinstall = msm_irq_preinstall,
1095 .irq_postinstall = msm_irq_postinstall,
1096 .irq_uninstall = msm_irq_uninstall,
Rob Clarkc8afe682013-06-26 12:44:06 -04001097 .enable_vblank = msm_enable_vblank,
1098 .disable_vblank = msm_disable_vblank,
1099 .gem_free_object = msm_gem_free_object,
1100 .gem_vm_ops = &vm_ops,
1101 .dumb_create = msm_gem_dumb_create,
1102 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark05b84912013-09-28 11:28:35 -04001103 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1104 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1105 .gem_prime_export = drm_gem_prime_export,
1106 .gem_prime_import = drm_gem_prime_import,
Eric Anholt43523eb2017-04-12 12:11:58 -07001107 .gem_prime_res_obj = msm_gem_prime_res_obj,
Rob Clark05b84912013-09-28 11:28:35 -04001108 .gem_prime_pin = msm_gem_prime_pin,
1109 .gem_prime_unpin = msm_gem_prime_unpin,
1110 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table,
1111 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
1112 .gem_prime_vmap = msm_gem_prime_vmap,
1113 .gem_prime_vunmap = msm_gem_prime_vunmap,
Daniel Thompson77a147e2014-11-12 11:38:14 +00001114 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -04001115#ifdef CONFIG_DEBUG_FS
1116 .debugfs_init = msm_debugfs_init,
Rob Clarkc8afe682013-06-26 12:44:06 -04001117#endif
Rob Clark7198e6b2013-07-19 12:59:32 -04001118 .ioctls = msm_ioctls,
Jordan Crouse167b6062017-05-08 14:34:59 -06001119 .num_ioctls = ARRAY_SIZE(msm_ioctls),
Rob Clarkc8afe682013-06-26 12:44:06 -04001120 .fops = &fops,
1121 .name = "msm",
1122 .desc = "MSM Snapdragon DRM",
1123 .date = "20130625",
Rob Clarka8d854c2016-06-01 14:02:02 -04001124 .major = MSM_VERSION_MAJOR,
1125 .minor = MSM_VERSION_MINOR,
1126 .patchlevel = MSM_VERSION_PATCHLEVEL,
Rob Clarkc8afe682013-06-26 12:44:06 -04001127};
1128
1129#ifdef CONFIG_PM_SLEEP
1130static int msm_pm_suspend(struct device *dev)
1131{
1132 struct drm_device *ddev = dev_get_drvdata(dev);
Daniel Mackec446d02018-05-28 21:53:38 +02001133 struct msm_drm_private *priv = ddev->dev_private;
Jeykumar Sankaran036bfeb2018-06-27 15:24:17 -04001134
Bruce Wang3750e782018-10-05 17:04:01 -04001135 if (WARN_ON(priv->pm_state))
1136 drm_atomic_state_put(priv->pm_state);
Rob Clarkc8afe682013-06-26 12:44:06 -04001137
Daniel Mackec446d02018-05-28 21:53:38 +02001138 priv->pm_state = drm_atomic_helper_suspend(ddev);
1139 if (IS_ERR(priv->pm_state)) {
Bruce Wang3750e782018-10-05 17:04:01 -04001140 int ret = PTR_ERR(priv->pm_state);
1141 DRM_ERROR("Failed to suspend dpu, %d\n", ret);
1142 return ret;
Daniel Mackec446d02018-05-28 21:53:38 +02001143 }
1144
Rob Clarkc8afe682013-06-26 12:44:06 -04001145 return 0;
1146}
1147
1148static int msm_pm_resume(struct device *dev)
1149{
1150 struct drm_device *ddev = dev_get_drvdata(dev);
Daniel Mackec446d02018-05-28 21:53:38 +02001151 struct msm_drm_private *priv = ddev->dev_private;
Bruce Wang3750e782018-10-05 17:04:01 -04001152 int ret;
Jeykumar Sankaran036bfeb2018-06-27 15:24:17 -04001153
Bruce Wang3750e782018-10-05 17:04:01 -04001154 if (WARN_ON(!priv->pm_state))
1155 return -ENOENT;
Rob Clarkc8afe682013-06-26 12:44:06 -04001156
Bruce Wang3750e782018-10-05 17:04:01 -04001157 ret = drm_atomic_helper_resume(ddev, priv->pm_state);
1158 if (!ret)
1159 priv->pm_state = NULL;
Rob Clarkc8afe682013-06-26 12:44:06 -04001160
Bruce Wang3750e782018-10-05 17:04:01 -04001161 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -04001162}
1163#endif
1164
Archit Taneja774e39e2017-07-28 16:17:07 +05301165#ifdef CONFIG_PM
1166static int msm_runtime_suspend(struct device *dev)
1167{
1168 struct drm_device *ddev = dev_get_drvdata(dev);
1169 struct msm_drm_private *priv = ddev->dev_private;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001170 struct msm_mdss *mdss = priv->mdss;
Archit Taneja774e39e2017-07-28 16:17:07 +05301171
1172 DBG("");
1173
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001174 if (mdss && mdss->funcs)
1175 return mdss->funcs->disable(mdss);
Archit Taneja774e39e2017-07-28 16:17:07 +05301176
1177 return 0;
1178}
1179
1180static int msm_runtime_resume(struct device *dev)
1181{
1182 struct drm_device *ddev = dev_get_drvdata(dev);
1183 struct msm_drm_private *priv = ddev->dev_private;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001184 struct msm_mdss *mdss = priv->mdss;
Archit Taneja774e39e2017-07-28 16:17:07 +05301185
1186 DBG("");
1187
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001188 if (mdss && mdss->funcs)
1189 return mdss->funcs->enable(mdss);
Archit Taneja774e39e2017-07-28 16:17:07 +05301190
1191 return 0;
1192}
1193#endif
1194
Rob Clarkc8afe682013-06-26 12:44:06 -04001195static const struct dev_pm_ops msm_pm_ops = {
1196 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
Archit Taneja774e39e2017-07-28 16:17:07 +05301197 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
Rob Clarkc8afe682013-06-26 12:44:06 -04001198};
1199
1200/*
Rob Clark060530f2014-03-03 14:19:12 -05001201 * Componentized driver support:
1202 */
1203
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301204/*
1205 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1206 * so probably some room for some helpers
Rob Clark060530f2014-03-03 14:19:12 -05001207 */
1208static int compare_of(struct device *dev, void *data)
1209{
1210 return dev->of_node == data;
1211}
Rob Clark41e69772013-12-15 16:23:05 -05001212
Archit Taneja812070e2016-05-19 10:38:39 +05301213/*
1214 * Identify what components need to be added by parsing what remote-endpoints
1215 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1216 * is no external component that we need to add since LVDS is within MDP4
1217 * itself.
1218 */
1219static int add_components_mdp(struct device *mdp_dev,
1220 struct component_match **matchptr)
1221{
1222 struct device_node *np = mdp_dev->of_node;
1223 struct device_node *ep_node;
Archit Taneja54011e22016-06-06 13:45:34 +05301224 struct device *master_dev;
1225
1226 /*
1227 * on MDP4 based platforms, the MDP platform device is the component
1228 * master that adds other display interface components to itself.
1229 *
1230 * on MDP5 based platforms, the MDSS platform device is the component
1231 * master that adds MDP5 and other display interface components to
1232 * itself.
1233 */
1234 if (of_device_is_compatible(np, "qcom,mdp4"))
1235 master_dev = mdp_dev;
1236 else
1237 master_dev = mdp_dev->parent;
Archit Taneja812070e2016-05-19 10:38:39 +05301238
1239 for_each_endpoint_of_node(np, ep_node) {
1240 struct device_node *intf;
1241 struct of_endpoint ep;
1242 int ret;
1243
1244 ret = of_graph_parse_endpoint(ep_node, &ep);
1245 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301246 DRM_DEV_ERROR(mdp_dev, "unable to parse port endpoint\n");
Archit Taneja812070e2016-05-19 10:38:39 +05301247 of_node_put(ep_node);
1248 return ret;
1249 }
1250
1251 /*
1252 * The LCDC/LVDS port on MDP4 is a speacial case where the
1253 * remote-endpoint isn't a component that we need to add
1254 */
1255 if (of_device_is_compatible(np, "qcom,mdp4") &&
Archit Tanejad8dd8052016-11-17 12:12:03 +05301256 ep.port == 0)
Archit Taneja812070e2016-05-19 10:38:39 +05301257 continue;
Archit Taneja812070e2016-05-19 10:38:39 +05301258
1259 /*
1260 * It's okay if some of the ports don't have a remote endpoint
1261 * specified. It just means that the port isn't connected to
1262 * any external interface.
1263 */
1264 intf = of_graph_get_remote_port_parent(ep_node);
Archit Tanejad8dd8052016-11-17 12:12:03 +05301265 if (!intf)
Archit Taneja812070e2016-05-19 10:38:39 +05301266 continue;
Archit Taneja812070e2016-05-19 10:38:39 +05301267
Douglas Andersond1d9d0e2018-12-04 10:04:41 -08001268 if (of_device_is_available(intf))
1269 drm_of_component_match_add(master_dev, matchptr,
1270 compare_of, intf);
1271
Archit Taneja812070e2016-05-19 10:38:39 +05301272 of_node_put(intf);
Archit Taneja812070e2016-05-19 10:38:39 +05301273 }
1274
1275 return 0;
1276}
1277
Archit Taneja54011e22016-06-06 13:45:34 +05301278static int compare_name_mdp(struct device *dev, void *data)
1279{
1280 return (strstr(dev_name(dev), "mdp") != NULL);
1281}
1282
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301283static int add_display_components(struct device *dev,
1284 struct component_match **matchptr)
1285{
Archit Taneja54011e22016-06-06 13:45:34 +05301286 struct device *mdp_dev;
1287 int ret;
1288
1289 /*
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001290 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1291 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1292 * Populate the children devices, find the MDP5/DPU node, and then add
1293 * the interfaces to our components list.
Archit Taneja54011e22016-06-06 13:45:34 +05301294 */
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001295 if (of_device_is_compatible(dev->of_node, "qcom,mdss") ||
1296 of_device_is_compatible(dev->of_node, "qcom,sdm845-mdss")) {
Archit Taneja54011e22016-06-06 13:45:34 +05301297 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1298 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301299 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
Archit Taneja54011e22016-06-06 13:45:34 +05301300 return ret;
1301 }
1302
1303 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1304 if (!mdp_dev) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301305 DRM_DEV_ERROR(dev, "failed to find MDSS MDP node\n");
Archit Taneja54011e22016-06-06 13:45:34 +05301306 of_platform_depopulate(dev);
1307 return -ENODEV;
1308 }
1309
1310 put_device(mdp_dev);
1311
1312 /* add the MDP component itself */
Russell King97ac0e42016-10-19 11:28:27 +01001313 drm_of_component_match_add(dev, matchptr, compare_of,
1314 mdp_dev->of_node);
Archit Taneja54011e22016-06-06 13:45:34 +05301315 } else {
1316 /* MDP4 */
1317 mdp_dev = dev;
1318 }
1319
1320 ret = add_components_mdp(mdp_dev, matchptr);
1321 if (ret)
1322 of_platform_depopulate(dev);
1323
1324 return ret;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301325}
1326
Archit Tanejadc3ea262016-05-19 13:33:52 +05301327/*
1328 * We don't know what's the best binding to link the gpu with the drm device.
1329 * Fow now, we just hunt for all the possible gpus that we support, and add them
1330 * as components.
1331 */
1332static const struct of_device_id msm_gpu_match[] = {
Rob Clark1db7afa2017-01-30 11:02:27 -05001333 { .compatible = "qcom,adreno" },
Archit Tanejadc3ea262016-05-19 13:33:52 +05301334 { .compatible = "qcom,adreno-3xx" },
Jonathan Mareke6f6d632018-12-04 10:16:58 -05001335 { .compatible = "amd,imageon" },
Archit Tanejadc3ea262016-05-19 13:33:52 +05301336 { .compatible = "qcom,kgsl-3d0" },
1337 { },
1338};
1339
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301340static int add_gpu_components(struct device *dev,
1341 struct component_match **matchptr)
1342{
Archit Tanejadc3ea262016-05-19 13:33:52 +05301343 struct device_node *np;
1344
1345 np = of_find_matching_node(NULL, msm_gpu_match);
1346 if (!np)
1347 return 0;
1348
Russell King97ac0e42016-10-19 11:28:27 +01001349 drm_of_component_match_add(dev, matchptr, compare_of, np);
Archit Tanejadc3ea262016-05-19 13:33:52 +05301350
1351 of_node_put(np);
1352
1353 return 0;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301354}
1355
Russell King84448282014-04-19 11:20:42 +01001356static int msm_drm_bind(struct device *dev)
1357{
Archit Taneja2b669872016-05-02 11:05:54 +05301358 return msm_drm_init(dev, &msm_driver);
Russell King84448282014-04-19 11:20:42 +01001359}
1360
1361static void msm_drm_unbind(struct device *dev)
1362{
Archit Taneja2b669872016-05-02 11:05:54 +05301363 msm_drm_uninit(dev);
Russell King84448282014-04-19 11:20:42 +01001364}
1365
1366static const struct component_master_ops msm_drm_ops = {
1367 .bind = msm_drm_bind,
1368 .unbind = msm_drm_unbind,
1369};
1370
1371/*
1372 * Platform driver:
1373 */
1374
1375static int msm_pdev_probe(struct platform_device *pdev)
1376{
1377 struct component_match *match = NULL;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301378 int ret;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301379
Jonathan Mareke6f6d632018-12-04 10:16:58 -05001380 if (get_mdp_ver(pdev)) {
1381 ret = add_display_components(&pdev->dev, &match);
1382 if (ret)
1383 return ret;
1384 }
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301385
1386 ret = add_gpu_components(&pdev->dev, &match);
1387 if (ret)
1388 return ret;
Rob Clark060530f2014-03-03 14:19:12 -05001389
Rob Clarkc83ea572016-11-07 13:31:30 -05001390 /* on all devices that I am aware of, iommu's which can map
1391 * any address the cpu can see are used:
1392 */
1393 ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1394 if (ret)
1395 return ret;
1396
Russell King84448282014-04-19 11:20:42 +01001397 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
Rob Clarkc8afe682013-06-26 12:44:06 -04001398}
1399
1400static int msm_pdev_remove(struct platform_device *pdev)
1401{
Rob Clark060530f2014-03-03 14:19:12 -05001402 component_master_del(&pdev->dev, &msm_drm_ops);
Archit Taneja54011e22016-06-06 13:45:34 +05301403 of_platform_depopulate(&pdev->dev);
Rob Clarkc8afe682013-06-26 12:44:06 -04001404
1405 return 0;
1406}
1407
Rob Clark06c0dd92013-11-30 17:51:47 -05001408static const struct of_device_id dt_match[] = {
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -04001409 { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
1410 { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001411 { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU },
Rob Clark06c0dd92013-11-30 17:51:47 -05001412 {}
1413};
1414MODULE_DEVICE_TABLE(of, dt_match);
1415
Rob Clarkc8afe682013-06-26 12:44:06 -04001416static struct platform_driver msm_platform_driver = {
1417 .probe = msm_pdev_probe,
1418 .remove = msm_pdev_remove,
1419 .driver = {
Rob Clarkc8afe682013-06-26 12:44:06 -04001420 .name = "msm",
Rob Clark06c0dd92013-11-30 17:51:47 -05001421 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -04001422 .pm = &msm_pm_ops,
1423 },
Rob Clarkc8afe682013-06-26 12:44:06 -04001424};
1425
1426static int __init msm_drm_register(void)
1427{
Rob Clarkba4dd712017-07-06 16:33:44 -04001428 if (!modeset)
1429 return -EINVAL;
1430
Rob Clarkc8afe682013-06-26 12:44:06 -04001431 DBG("init");
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301432 msm_mdp_register();
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001433 msm_dpu_register();
Hai Lid5af49c2015-03-26 19:25:17 -04001434 msm_dsi_register();
Hai Li00453982014-12-12 14:41:17 -05001435 msm_edp_register();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001436 msm_hdmi_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001437 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001438 return platform_driver_register(&msm_platform_driver);
1439}
1440
1441static void __exit msm_drm_unregister(void)
1442{
1443 DBG("fini");
1444 platform_driver_unregister(&msm_platform_driver);
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001445 msm_hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001446 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -05001447 msm_edp_unregister();
Hai Lid5af49c2015-03-26 19:25:17 -04001448 msm_dsi_unregister();
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301449 msm_mdp_unregister();
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001450 msm_dpu_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -04001451}
1452
1453module_init(msm_drm_register);
1454module_exit(msm_drm_unregister);
1455
1456MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1457MODULE_DESCRIPTION("MSM DRM Driver");
1458MODULE_LICENSE("GPL");