blob: 74e5e5ac82d05787d0ab19c5c92b37bb2750a0e9 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkc8afe682013-06-26 12:44:06 -04002/*
Abhinav Kumar98659482021-04-16 13:57:20 -07003 * Copyright (c) 2016-2018, 2020-2021 The Linux Foundation. All rights reserved.
Rob Clarkc8afe682013-06-26 12:44:06 -04004 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
Rob Clarkc8afe682013-06-26 12:44:06 -04006 */
7
Sam Ravnborgfeea39a2019-08-04 08:55:51 +02008#include <linux/dma-mapping.h>
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04009#include <linux/kthread.h>
Rob Clarkd9844572020-10-23 09:51:14 -070010#include <linux/sched/mm.h>
Sam Ravnborgfeea39a2019-08-04 08:55:51 +020011#include <linux/uaccess.h>
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -040012#include <uapi/linux/sched/types.h>
Sam Ravnborgfeea39a2019-08-04 08:55:51 +020013
14#include <drm/drm_drv.h>
15#include <drm/drm_file.h>
16#include <drm/drm_ioctl.h>
17#include <drm/drm_irq.h>
18#include <drm/drm_prime.h>
Russell King97ac0e42016-10-19 11:28:27 +010019#include <drm/drm_of.h>
Sam Ravnborgfeea39a2019-08-04 08:55:51 +020020#include <drm/drm_vblank.h>
Russell King97ac0e42016-10-19 11:28:27 +010021
Abhinav Kumar98659482021-04-16 13:57:20 -070022#include "disp/msm_disp_snapshot.h"
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 * MSM driver version:
33 * - 1.0.0 - initial interface
34 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers
Rob Clark7a3bcc02016-09-16 18:37:44 -040035 * - 1.2.0 - adds explicit fence support for submit ioctl
Jordan Crousef7de1542017-10-20 11:06:55 -060036 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW +
37 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for
38 * MSM_GEM_INFO ioctl.
Rob Clark1fed8df2018-11-29 10:30:04 -050039 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get
40 * GEM object's debug name
Jordan Crouseb0fb6602019-03-22 14:21:22 -060041 * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl
Bas Nieuwenhuizenab723b72020-01-24 00:57:10 +010042 * - 1.6.0 - Syncobj support
Rob Clark3ab1c5c2021-03-24 18:23:53 -070043 * - 1.7.0 - Add MSM_PARAM_SUSPENDS to access suspend count
Rob Clarka8d854c2016-06-01 14:02:02 -040044 */
45#define MSM_VERSION_MAJOR 1
Jonathan Marek02ded132021-04-23 15:04:20 -040046#define MSM_VERSION_MINOR 7
Rob Clarka8d854c2016-06-01 14:02:02 -040047#define MSM_VERSION_PATCHLEVEL 0
48
Rob Clarkc8afe682013-06-26 12:44:06 -040049static const struct drm_mode_config_funcs mode_config_funcs = {
50 .fb_create = msm_framebuffer_create,
Noralf Trønnes4ccbc6e2017-12-05 19:24:59 +010051 .output_poll_changed = drm_fb_helper_output_poll_changed,
Rob Clark1f920172017-10-25 12:30:51 -040052 .atomic_check = drm_atomic_helper_check,
Sean Pauld14659f2018-02-28 14:19:05 -050053 .atomic_commit = drm_atomic_helper_commit,
54};
55
56static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
57 .atomic_commit_tail = msm_atomic_commit_tail,
Rob Clarkc8afe682013-06-26 12:44:06 -040058};
59
Rob Clarkc8afe682013-06-26 12:44:06 -040060#ifdef CONFIG_DRM_MSM_REGISTER_LOGGING
61static bool reglog = false;
62MODULE_PARM_DESC(reglog, "Enable register read/write logging");
63module_param(reglog, bool, 0600);
64#else
65#define reglog 0
66#endif
67
Archit Tanejaa9ee34b2015-07-13 12:12:07 +053068#ifdef CONFIG_DRM_FBDEV_EMULATION
Rob Clarke90dfec2015-01-30 17:05:41 -050069static bool fbdev = true;
70MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer");
71module_param(fbdev, bool, 0600);
72#endif
73
Rob Clark3a10ba82014-09-08 14:24:57 -040074static char *vram = "16m";
Rob Clark4313c7442016-02-03 14:02:04 -050075MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)");
Rob Clark871d8122013-11-16 12:56:06 -050076module_param(vram, charp, 0);
77
Rob Clark06d9f562016-11-05 11:08:12 -040078bool dumpstate = false;
79MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors");
80module_param(dumpstate, bool, 0600);
81
Rob Clarkba4dd712017-07-06 16:33:44 -040082static bool modeset = true;
83MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)");
84module_param(modeset, bool, 0600);
85
Rob Clark060530f2014-03-03 14:19:12 -050086/*
87 * Util/helpers:
88 */
89
Jordan Crouse8e54eea2018-08-06 11:33:21 -060090struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count,
91 const char *name)
92{
93 int i;
94 char n[32];
95
96 snprintf(n, sizeof(n), "%s_clk", name);
97
98 for (i = 0; bulk && i < count; i++) {
99 if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n))
100 return bulk[i].clk;
101 }
102
103
104 return NULL;
105}
106
Rob Clark720c3bb2017-01-30 11:30:58 -0500107struct clk *msm_clk_get(struct platform_device *pdev, const char *name)
108{
109 struct clk *clk;
110 char name2[32];
111
112 clk = devm_clk_get(&pdev->dev, name);
113 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
114 return clk;
115
116 snprintf(name2, sizeof(name2), "%s_clk", name);
117
118 clk = devm_clk_get(&pdev->dev, name2);
119 if (!IS_ERR(clk))
120 dev_warn(&pdev->dev, "Using legacy clk name binding. Use "
121 "\"%s\" instead of \"%s\"\n", name, name2);
122
123 return clk;
124}
125
Lee Jonesea8742c2020-11-23 11:19:17 +0000126static void __iomem *_msm_ioremap(struct platform_device *pdev, const char *name,
127 const char *dbgname, bool quiet)
Rob Clarkc8afe682013-06-26 12:44:06 -0400128{
129 struct resource *res;
130 unsigned long size;
131 void __iomem *ptr;
132
133 if (name)
134 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
135 else
136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
137
138 if (!res) {
Eric Anholt62a35e82020-06-29 11:19:21 -0700139 if (!quiet)
140 DRM_DEV_ERROR(&pdev->dev, "failed to get memory resource: %s\n", name);
Rob Clarkc8afe682013-06-26 12:44:06 -0400141 return ERR_PTR(-EINVAL);
142 }
143
144 size = resource_size(res);
145
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100146 ptr = devm_ioremap(&pdev->dev, res->start, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400147 if (!ptr) {
Eric Anholt62a35e82020-06-29 11:19:21 -0700148 if (!quiet)
149 DRM_DEV_ERROR(&pdev->dev, "failed to ioremap: %s\n", name);
Rob Clarkc8afe682013-06-26 12:44:06 -0400150 return ERR_PTR(-ENOMEM);
151 }
152
153 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200154 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400155
156 return ptr;
157}
158
Eric Anholt62a35e82020-06-29 11:19:21 -0700159void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
160 const char *dbgname)
161{
162 return _msm_ioremap(pdev, name, dbgname, false);
163}
164
165void __iomem *msm_ioremap_quiet(struct platform_device *pdev, const char *name,
166 const char *dbgname)
167{
168 return _msm_ioremap(pdev, name, dbgname, true);
169}
170
Abhinav Kumar98659482021-04-16 13:57:20 -0700171unsigned long msm_iomap_size(struct platform_device *pdev, const char *name)
172{
173 struct resource *res;
174
175 if (name)
176 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
177 else
178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179
180 if (!res) {
181 dev_dbg(&pdev->dev, "failed to get memory resource: %s\n",
182 name);
183 return 0;
184 }
185
186 return resource_size(res);
187}
188
Rob Clarkc8afe682013-06-26 12:44:06 -0400189void msm_writel(u32 data, void __iomem *addr)
190{
191 if (reglog)
Thierry Redingfc99f972015-04-09 16:39:51 +0200192 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data);
Rob Clarkc8afe682013-06-26 12:44:06 -0400193 writel(data, addr);
194}
195
196u32 msm_readl(const void __iomem *addr)
197{
198 u32 val = readl(addr);
199 if (reglog)
Joe Perches8dfe1622017-02-28 04:55:54 -0800200 pr_err("IO:R %p %08x\n", addr, val);
Rob Clarkc8afe682013-06-26 12:44:06 -0400201 return val;
202}
203
Sharat Masetty40a72b02020-11-25 12:30:14 +0530204void msm_rmw(void __iomem *addr, u32 mask, u32 or)
205{
206 u32 val = msm_readl(addr);
207
208 val &= ~mask;
209 msm_writel(val | or, addr);
210}
211
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800212struct msm_vblank_work {
213 struct work_struct work;
Hai Li78b1d472015-07-27 13:49:45 -0400214 int crtc_id;
215 bool enable;
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800216 struct msm_drm_private *priv;
Hai Li78b1d472015-07-27 13:49:45 -0400217};
218
Jeykumar Sankaran5aeb6652018-12-14 15:57:52 -0800219static void vblank_ctrl_worker(struct work_struct *work)
Hai Li78b1d472015-07-27 13:49:45 -0400220{
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800221 struct msm_vblank_work *vbl_work = container_of(work,
222 struct msm_vblank_work, work);
223 struct msm_drm_private *priv = vbl_work->priv;
Hai Li78b1d472015-07-27 13:49:45 -0400224 struct msm_kms *kms = priv->kms;
Hai Li78b1d472015-07-27 13:49:45 -0400225
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800226 if (vbl_work->enable)
227 kms->funcs->enable_vblank(kms, priv->crtcs[vbl_work->crtc_id]);
228 else
229 kms->funcs->disable_vblank(kms, priv->crtcs[vbl_work->crtc_id]);
Hai Li78b1d472015-07-27 13:49:45 -0400230
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800231 kfree(vbl_work);
Hai Li78b1d472015-07-27 13:49:45 -0400232}
233
234static int vblank_ctrl_queue_work(struct msm_drm_private *priv,
235 int crtc_id, bool enable)
236{
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800237 struct msm_vblank_work *vbl_work;
Hai Li78b1d472015-07-27 13:49:45 -0400238
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800239 vbl_work = kzalloc(sizeof(*vbl_work), GFP_ATOMIC);
240 if (!vbl_work)
Hai Li78b1d472015-07-27 13:49:45 -0400241 return -ENOMEM;
242
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800243 INIT_WORK(&vbl_work->work, vblank_ctrl_worker);
Hai Li78b1d472015-07-27 13:49:45 -0400244
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800245 vbl_work->crtc_id = crtc_id;
246 vbl_work->enable = enable;
247 vbl_work->priv = priv;
Hai Li78b1d472015-07-27 13:49:45 -0400248
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800249 queue_work(priv->wq, &vbl_work->work);
Hai Li78b1d472015-07-27 13:49:45 -0400250
251 return 0;
252}
253
Archit Taneja2b669872016-05-02 11:05:54 +0530254static int msm_drm_uninit(struct device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400255{
Archit Taneja2b669872016-05-02 11:05:54 +0530256 struct platform_device *pdev = to_platform_device(dev);
257 struct drm_device *ddev = platform_get_drvdata(pdev);
258 struct msm_drm_private *priv = ddev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400259 struct msm_kms *kms = priv->kms;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400260 struct msm_mdss *mdss = priv->mdss;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400261 int i;
Hai Li78b1d472015-07-27 13:49:45 -0400262
Sean Paul2aa31762019-05-24 16:29:13 -0400263 /*
264 * Shutdown the hw if we're far enough along where things might be on.
265 * If we run this too early, we'll end up panicking in any variety of
266 * places. Since we don't register the drm device until late in
267 * msm_drm_init, drm_dev->registered is used as an indicator that the
268 * shutdown will be successful.
269 */
270 if (ddev->registered) {
271 drm_dev_unregister(ddev);
272 drm_atomic_helper_shutdown(ddev);
273 }
274
Hai Li78b1d472015-07-27 13:49:45 -0400275 /* We must cancel and cleanup any pending vblank enable/disable
276 * work before drm_irq_uninstall() to avoid work re-enabling an
277 * irq after uninstall has disabled it.
278 */
Rob Clarkc8afe682013-06-26 12:44:06 -0400279
Jeykumar Sankaran48d1d282018-12-14 15:57:55 -0800280 flush_workqueue(priv->wq);
Rob Clarkc8afe682013-06-26 12:44:06 -0400281
Jeykumar Sankarand9db30c2018-12-14 15:57:54 -0800282 /* clean up event worker threads */
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400283 for (i = 0; i < priv->num_crtcs; i++) {
Bernard1041dee2020-07-21 09:33:03 +0800284 if (priv->event_thread[i].worker)
285 kthread_destroy_worker(priv->event_thread[i].worker);
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400286 }
287
Rob Clark68209392016-05-17 16:19:32 -0400288 msm_gem_shrinker_cleanup(ddev);
289
Archit Taneja2b669872016-05-02 11:05:54 +0530290 drm_kms_helper_poll_fini(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530291
Noralf Trønnes85eac472017-03-07 21:49:22 +0100292 msm_perf_debugfs_cleanup(priv);
293 msm_rd_debugfs_cleanup(priv);
294
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530295#ifdef CONFIG_DRM_FBDEV_EMULATION
296 if (fbdev && priv->fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530297 msm_fbdev_free(ddev);
Archit Taneja1aaa57f2016-02-25 11:19:45 +0530298#endif
Sean Paul2aa31762019-05-24 16:29:13 -0400299
Abhinav Kumar98659482021-04-16 13:57:20 -0700300 msm_disp_snapshot_destroy(ddev);
301
Archit Taneja2b669872016-05-02 11:05:54 +0530302 drm_mode_config_cleanup(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400303
Archit Taneja2b669872016-05-02 11:05:54 +0530304 pm_runtime_get_sync(dev);
305 drm_irq_uninstall(ddev);
306 pm_runtime_put_sync(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400307
Archit Taneja16976082016-11-03 17:36:18 +0530308 if (kms && kms->funcs)
Rob Clarkc8afe682013-06-26 12:44:06 -0400309 kms->funcs->destroy(kms);
Rob Clarkc8afe682013-06-26 12:44:06 -0400310
Rob Clark871d8122013-11-16 12:56:06 -0500311 if (priv->vram.paddr) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700312 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING;
Rob Clark871d8122013-11-16 12:56:06 -0500313 drm_mm_takedown(&priv->vram.mm);
Archit Taneja2b669872016-05-02 11:05:54 +0530314 dma_free_attrs(dev, priv->vram.size, NULL,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700315 priv->vram.paddr, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500316 }
317
Archit Taneja2b669872016-05-02 11:05:54 +0530318 component_unbind_all(dev, ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500319
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400320 if (mdss && mdss->funcs)
321 mdss->funcs->destroy(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530322
Archit Taneja2b669872016-05-02 11:05:54 +0530323 ddev->dev_private = NULL;
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200324 drm_dev_put(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400325
Sean Paul2aa31762019-05-24 16:29:13 -0400326 destroy_workqueue(priv->wq);
Rob Clarkc8afe682013-06-26 12:44:06 -0400327 kfree(priv);
328
329 return 0;
330}
331
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400332#define KMS_MDP4 4
333#define KMS_MDP5 5
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400334#define KMS_DPU 3
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400335
Rob Clark06c0dd92013-11-30 17:51:47 -0500336static int get_mdp_ver(struct platform_device *pdev)
337{
Rob Clark06c0dd92013-11-30 17:51:47 -0500338 struct device *dev = &pdev->dev;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530339
340 return (int) (unsigned long) of_device_get_match_data(dev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500341}
342
Rob Clark072f1f92015-03-03 15:04:25 -0500343#include <linux/of_address.h>
344
Jonathan Marekc2052a42018-11-14 17:08:04 -0500345bool msm_use_mmu(struct drm_device *dev)
346{
347 struct msm_drm_private *priv = dev->dev_private;
348
349 /* a2xx comes with its own MMU */
350 return priv->is_a2xx || iommu_present(&platform_bus_type);
351}
352
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500353static int msm_init_vram(struct drm_device *dev)
Rob Clarkc8afe682013-06-26 12:44:06 -0400354{
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500355 struct msm_drm_private *priv = dev->dev_private;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530356 struct device_node *node;
Rob Clark072f1f92015-03-03 15:04:25 -0500357 unsigned long size = 0;
358 int ret = 0;
359
Rob Clark072f1f92015-03-03 15:04:25 -0500360 /* In the device-tree world, we could have a 'memory-region'
361 * phandle, which gives us a link to our "vram". Allocating
362 * is all nicely abstracted behind the dma api, but we need
363 * to know the entire size to allocate it all in one go. There
364 * are two cases:
365 * 1) device with no IOMMU, in which case we need exclusive
366 * access to a VRAM carveout big enough for all gpu
367 * buffers
368 * 2) device with IOMMU, but where the bootloader puts up
369 * a splash screen. In this case, the VRAM carveout
370 * need only be large enough for fbdev fb. But we need
371 * exclusive access to the buffer to avoid the kernel
372 * using those pages for other purposes (which appears
373 * as corruption on screen before we have a chance to
374 * load and do initial modeset)
375 */
Rob Clark072f1f92015-03-03 15:04:25 -0500376
377 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0);
378 if (node) {
379 struct resource r;
380 ret = of_address_to_resource(node, 0, &r);
Peter Chen2ca41c172016-07-04 16:49:50 +0800381 of_node_put(node);
Rob Clark072f1f92015-03-03 15:04:25 -0500382 if (ret)
383 return ret;
384 size = r.end - r.start;
Thierry Redingfc99f972015-04-09 16:39:51 +0200385 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start);
Rob Clarkc8afe682013-06-26 12:44:06 -0400386
Archit Tanejae9fbdaf2015-11-18 12:15:14 +0530387 /* if we have no IOMMU, then we need to use carveout allocator.
388 * Grab the entire CMA chunk carved out in early startup in
389 * mach-msm:
390 */
Jonathan Marekc2052a42018-11-14 17:08:04 -0500391 } else if (!msm_use_mmu(dev)) {
Rob Clark072f1f92015-03-03 15:04:25 -0500392 DRM_INFO("using %s VRAM carveout\n", vram);
393 size = memparse(vram, NULL);
394 }
395
396 if (size) {
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700397 unsigned long attrs = 0;
Rob Clark871d8122013-11-16 12:56:06 -0500398 void *p;
399
Rob Clark871d8122013-11-16 12:56:06 -0500400 priv->vram.size = size;
401
402 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600403 spin_lock_init(&priv->vram.lock);
Rob Clark871d8122013-11-16 12:56:06 -0500404
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700405 attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
406 attrs |= DMA_ATTR_WRITE_COMBINE;
Rob Clark871d8122013-11-16 12:56:06 -0500407
408 /* note that for no-kernel-mapping, the vaddr returned
409 * is bogus, but non-null if allocation succeeded:
410 */
411 p = dma_alloc_attrs(dev->dev, size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700412 &priv->vram.paddr, GFP_KERNEL, attrs);
Rob Clark871d8122013-11-16 12:56:06 -0500413 if (!p) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530414 DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n");
Rob Clark871d8122013-11-16 12:56:06 -0500415 priv->vram.paddr = 0;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500416 return -ENOMEM;
Rob Clark871d8122013-11-16 12:56:06 -0500417 }
418
Mamta Shukla6a41da12018-10-20 23:19:26 +0530419 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n",
Rob Clark871d8122013-11-16 12:56:06 -0500420 (uint32_t)priv->vram.paddr,
421 (uint32_t)(priv->vram.paddr + size));
422 }
423
Rob Clark072f1f92015-03-03 15:04:25 -0500424 return ret;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500425}
426
Daniel Vetter70a59dd2020-11-04 11:04:24 +0100427static int msm_drm_init(struct device *dev, const struct drm_driver *drv)
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500428{
Archit Taneja2b669872016-05-02 11:05:54 +0530429 struct platform_device *pdev = to_platform_device(dev);
430 struct drm_device *ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500431 struct msm_drm_private *priv;
432 struct msm_kms *kms;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400433 struct msm_mdss *mdss;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400434 int ret, i;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500435
Archit Taneja2b669872016-05-02 11:05:54 +0530436 ddev = drm_dev_alloc(drv, dev);
Tom Gundersen0f288602016-09-21 16:59:19 +0200437 if (IS_ERR(ddev)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530438 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n");
Tom Gundersen0f288602016-09-21 16:59:19 +0200439 return PTR_ERR(ddev);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500440 }
441
Archit Taneja2b669872016-05-02 11:05:54 +0530442 platform_set_drvdata(pdev, ddev);
Archit Taneja2b669872016-05-02 11:05:54 +0530443
444 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
445 if (!priv) {
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400446 ret = -ENOMEM;
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200447 goto err_put_drm_dev;
Archit Taneja2b669872016-05-02 11:05:54 +0530448 }
449
450 ddev->dev_private = priv;
Rob Clark68209392016-05-17 16:19:32 -0400451 priv->dev = ddev;
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500452
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400453 switch (get_mdp_ver(pdev)) {
454 case KMS_MDP5:
455 ret = mdp5_mdss_init(ddev);
456 break;
457 case KMS_DPU:
458 ret = dpu_mdss_init(ddev);
459 break;
460 default:
461 ret = 0;
462 break;
463 }
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400464 if (ret)
465 goto err_free_priv;
Archit Taneja0a6030d2016-05-08 21:36:28 +0530466
Rajesh Yadavbc3220b2018-06-21 16:06:10 -0400467 mdss = priv->mdss;
468
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500469 priv->wq = alloc_ordered_workqueue("msm", 0);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500470
Rob Clark6ed08972021-03-31 18:27:20 -0700471 INIT_LIST_HEAD(&priv->objects);
472 mutex_init(&priv->obj_lock);
473
Rob Clark3edfa302020-11-16 09:48:51 -0800474 INIT_LIST_HEAD(&priv->inactive_willneed);
475 INIT_LIST_HEAD(&priv->inactive_dontneed);
Rob Clark64fcbde2021-04-05 10:45:29 -0700476 INIT_LIST_HEAD(&priv->inactive_unpinned);
Rob Clarkd9844572020-10-23 09:51:14 -0700477 mutex_init(&priv->mm_lock);
Kristian H. Kristensen48e7f182019-03-20 10:09:08 -0700478
Rob Clarkd9844572020-10-23 09:51:14 -0700479 /* Teach lockdep about lock ordering wrt. shrinker: */
480 fs_reclaim_acquire(GFP_KERNEL);
481 might_lock(&priv->mm_lock);
482 fs_reclaim_release(GFP_KERNEL);
Rob Clark5bf9c0b2015-03-03 15:04:24 -0500483
Archit Taneja2b669872016-05-02 11:05:54 +0530484 drm_mode_config_init(ddev);
Rob Clark060530f2014-03-03 14:19:12 -0500485
Craig Tatlord863f0c2020-12-30 17:29:42 +0200486 ret = msm_init_vram(ddev);
487 if (ret)
488 goto err_destroy_mdss;
489
Rob Clark060530f2014-03-03 14:19:12 -0500490 /* Bind all our sub-components: */
Archit Taneja2b669872016-05-02 11:05:54 +0530491 ret = component_bind_all(dev, ddev);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400492 if (ret)
493 goto err_destroy_mdss;
Rob Clark060530f2014-03-03 14:19:12 -0500494
Robin Murphyd5653a92020-09-03 22:04:03 +0100495 dma_set_max_seg_size(dev, UINT_MAX);
Sean Pauldb735fc2020-01-21 11:18:48 -0800496
Rob Clark68209392016-05-17 16:19:32 -0400497 msm_gem_shrinker_init(ddev);
498
Rob Clark06c0dd92013-11-30 17:51:47 -0500499 switch (get_mdp_ver(pdev)) {
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400500 case KMS_MDP4:
Archit Taneja2b669872016-05-02 11:05:54 +0530501 kms = mdp4_kms_init(ddev);
Archit Taneja0a6030d2016-05-08 21:36:28 +0530502 priv->kms = kms;
Rob Clark06c0dd92013-11-30 17:51:47 -0500503 break;
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -0400504 case KMS_MDP5:
Archit Taneja392ae6e2016-06-14 18:24:54 +0530505 kms = mdp5_kms_init(ddev);
Rob Clark06c0dd92013-11-30 17:51:47 -0500506 break;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400507 case KMS_DPU:
508 kms = dpu_kms_init(ddev);
509 priv->kms = kms;
510 break;
Rob Clark06c0dd92013-11-30 17:51:47 -0500511 default:
Jonathan Mareke6f6d632018-12-04 10:16:58 -0500512 /* valid only for the dummy headless case, where of_node=NULL */
513 WARN_ON(dev->of_node);
514 kms = NULL;
Rob Clark06c0dd92013-11-30 17:51:47 -0500515 break;
516 }
517
Rob Clarkc8afe682013-06-26 12:44:06 -0400518 if (IS_ERR(kms)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530519 DRM_DEV_ERROR(dev, "failed to load kms\n");
Thomas Meyere4826a92013-09-16 23:19:54 +0200520 ret = PTR_ERR(kms);
Jonathan Marekb2ccfdf2018-11-21 20:52:35 -0500521 priv->kms = NULL;
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400522 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400523 }
524
Jeykumar Sankaranbb676df2018-06-11 14:13:20 -0700525 /* Enable normalization of plane zpos */
526 ddev->mode_config.normalize_zpos = true;
527
Rob Clarkc8afe682013-06-26 12:44:06 -0400528 if (kms) {
Rob Clark2d99ced2019-08-29 09:45:16 -0700529 kms->dev = ddev;
Rob Clarkc8afe682013-06-26 12:44:06 -0400530 ret = kms->funcs->hw_init(kms);
531 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530532 DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400533 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400534 }
535 }
536
Archit Taneja2b669872016-05-02 11:05:54 +0530537 ddev->mode_config.funcs = &mode_config_funcs;
Sean Pauld14659f2018-02-28 14:19:05 -0500538 ddev->mode_config.helper_private = &mode_config_helper_funcs;
Rob Clarkc8afe682013-06-26 12:44:06 -0400539
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400540 for (i = 0; i < priv->num_crtcs; i++) {
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400541 /* initialize event thread */
542 priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id;
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400543 priv->event_thread[i].dev = ddev;
Bernard1041dee2020-07-21 09:33:03 +0800544 priv->event_thread[i].worker = kthread_create_worker(0,
545 "crtc_event:%d", priv->event_thread[i].crtc_id);
546 if (IS_ERR(priv->event_thread[i].worker)) {
Linus Torvalds4971f092018-12-25 11:48:26 -0800547 DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n");
Jeykumar Sankaran7f9743a2018-10-10 14:11:16 -0700548 goto err_msm_uninit;
549 }
550
Linus Torvalds6d2b84a2020-08-06 11:55:43 -0700551 sched_set_fifo(priv->event_thread[i].worker->task);
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -0400552 }
553
Archit Taneja2b669872016-05-02 11:05:54 +0530554 ret = drm_vblank_init(ddev, priv->num_crtcs);
Rob Clarkc8afe682013-06-26 12:44:06 -0400555 if (ret < 0) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530556 DRM_DEV_ERROR(dev, "failed to initialize vblank\n");
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400557 goto err_msm_uninit;
Rob Clarkc8afe682013-06-26 12:44:06 -0400558 }
559
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530560 if (kms) {
561 pm_runtime_get_sync(dev);
562 ret = drm_irq_install(ddev, kms->irq);
563 pm_runtime_put_sync(dev);
564 if (ret < 0) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530565 DRM_DEV_ERROR(dev, "failed to install IRQ handler\n");
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400566 goto err_msm_uninit;
Archit Tanejaa2b3a552016-05-18 15:06:03 +0530567 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400568 }
569
Archit Taneja2b669872016-05-02 11:05:54 +0530570 ret = drm_dev_register(ddev, 0);
Rob Clarka7d3c952014-05-30 14:47:38 -0400571 if (ret)
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400572 goto err_msm_uninit;
Rob Clarka7d3c952014-05-30 14:47:38 -0400573
Abhinav Kumar98659482021-04-16 13:57:20 -0700574 ret = msm_disp_snapshot_init(ddev);
575 if (ret)
576 DRM_DEV_ERROR(dev, "msm_disp_snapshot_init failed ret = %d\n", ret);
577
Archit Taneja2b669872016-05-02 11:05:54 +0530578 drm_mode_config_reset(ddev);
579
580#ifdef CONFIG_DRM_FBDEV_EMULATION
Jonathan Mareke6f6d632018-12-04 10:16:58 -0500581 if (kms && fbdev)
Archit Taneja2b669872016-05-02 11:05:54 +0530582 priv->fbdev = msm_fbdev_init(ddev);
583#endif
584
585 ret = msm_debugfs_late_init(ddev);
586 if (ret)
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400587 goto err_msm_uninit;
Archit Taneja2b669872016-05-02 11:05:54 +0530588
589 drm_kms_helper_poll_init(ddev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400590
591 return 0;
592
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400593err_msm_uninit:
Archit Taneja2b669872016-05-02 11:05:54 +0530594 msm_drm_uninit(dev);
Rob Clarkc8afe682013-06-26 12:44:06 -0400595 return ret;
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400596err_destroy_mdss:
597 if (mdss && mdss->funcs)
598 mdss->funcs->destroy(ddev);
599err_free_priv:
600 kfree(priv);
Thomas Zimmermann4d8dc2d2018-09-26 13:48:59 +0200601err_put_drm_dev:
602 drm_dev_put(ddev);
Stephen Boyd5620b132021-03-25 14:28:22 -0700603 platform_set_drvdata(pdev, NULL);
Jeykumar Sankaran77050c32018-06-27 14:35:28 -0400604 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400605}
606
Archit Taneja2b669872016-05-02 11:05:54 +0530607/*
608 * DRM operations:
609 */
610
Rob Clark7198e6b2013-07-19 12:59:32 -0400611static void load_gpu(struct drm_device *dev)
612{
Rob Clarka1ad3522014-07-11 11:59:22 -0400613 static DEFINE_MUTEX(init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400614 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400615
Rob Clarka1ad3522014-07-11 11:59:22 -0400616 mutex_lock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400617
Rob Clarke2550b72014-09-05 13:30:27 -0400618 if (!priv->gpu)
619 priv->gpu = adreno_load_gpu(dev);
Rob Clarka1ad3522014-07-11 11:59:22 -0400620
Rob Clarka1ad3522014-07-11 11:59:22 -0400621 mutex_unlock(&init_lock);
Rob Clark7198e6b2013-07-19 12:59:32 -0400622}
623
Jordan Crousef97deca2017-10-20 11:06:57 -0600624static int context_init(struct drm_device *dev, struct drm_file *file)
Rob Clark7198e6b2013-07-19 12:59:32 -0400625{
Jordan Crouse295b22a2019-05-07 12:02:07 -0600626 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400627 struct msm_file_private *ctx;
628
Rob Clark7198e6b2013-07-19 12:59:32 -0400629 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
630 if (!ctx)
631 return -ENOMEM;
632
Jordan Crousecf655d62020-08-17 15:01:36 -0700633 kref_init(&ctx->ref);
Jordan Crousef97deca2017-10-20 11:06:57 -0600634 msm_submitqueue_init(dev, ctx);
Jordan Crousef7de1542017-10-20 11:06:55 -0600635
Rob Clark25faf2f2020-08-17 15:01:45 -0700636 ctx->aspace = msm_gpu_create_private_address_space(priv->gpu, current);
Rob Clark7198e6b2013-07-19 12:59:32 -0400637 file->driver_priv = ctx;
638
639 return 0;
640}
641
Jordan Crousef7de1542017-10-20 11:06:55 -0600642static int msm_open(struct drm_device *dev, struct drm_file *file)
643{
644 /* For now, load gpu on open.. to avoid the requirement of having
645 * firmware in the initrd.
646 */
647 load_gpu(dev);
648
Jordan Crousef97deca2017-10-20 11:06:57 -0600649 return context_init(dev, file);
Jordan Crousef7de1542017-10-20 11:06:55 -0600650}
651
652static void context_close(struct msm_file_private *ctx)
653{
654 msm_submitqueue_close(ctx);
Jordan Crousecf655d62020-08-17 15:01:36 -0700655 msm_file_private_put(ctx);
Jordan Crousef7de1542017-10-20 11:06:55 -0600656}
657
Daniel Vetter94df1452017-03-08 15:12:46 +0100658static void msm_postclose(struct drm_device *dev, struct drm_file *file)
Rob Clarkc8afe682013-06-26 12:44:06 -0400659{
660 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400661 struct msm_file_private *ctx = file->driver_priv;
Rob Clark7198e6b2013-07-19 12:59:32 -0400662
Rob Clark7198e6b2013-07-19 12:59:32 -0400663 mutex_lock(&dev->struct_mutex);
664 if (ctx == priv->lastctx)
665 priv->lastctx = NULL;
666 mutex_unlock(&dev->struct_mutex);
667
Jordan Crousef7de1542017-10-20 11:06:55 -0600668 context_close(ctx);
Rob Clarkc8afe682013-06-26 12:44:06 -0400669}
670
Daniel Vettere9f0d762013-12-11 11:34:42 +0100671static irqreturn_t msm_irq(int irq, void *arg)
Rob Clarkc8afe682013-06-26 12:44:06 -0400672{
673 struct drm_device *dev = arg;
674 struct msm_drm_private *priv = dev->dev_private;
675 struct msm_kms *kms = priv->kms;
676 BUG_ON(!kms);
677 return kms->funcs->irq(kms);
678}
679
680static void msm_irq_preinstall(struct drm_device *dev)
681{
682 struct msm_drm_private *priv = dev->dev_private;
683 struct msm_kms *kms = priv->kms;
684 BUG_ON(!kms);
685 kms->funcs->irq_preinstall(kms);
686}
687
688static int msm_irq_postinstall(struct drm_device *dev)
689{
690 struct msm_drm_private *priv = dev->dev_private;
691 struct msm_kms *kms = priv->kms;
692 BUG_ON(!kms);
Jordan Crouseab07e0c2018-12-03 15:47:19 -0700693
694 if (kms->funcs->irq_postinstall)
695 return kms->funcs->irq_postinstall(kms);
696
697 return 0;
Rob Clarkc8afe682013-06-26 12:44:06 -0400698}
699
700static void msm_irq_uninstall(struct drm_device *dev)
701{
702 struct msm_drm_private *priv = dev->dev_private;
703 struct msm_kms *kms = priv->kms;
704 BUG_ON(!kms);
705 kms->funcs->irq_uninstall(kms);
706}
707
Thomas Zimmermann76e8cfd2020-01-23 14:59:34 +0100708int msm_crtc_enable_vblank(struct drm_crtc *crtc)
Rob Clarkc8afe682013-06-26 12:44:06 -0400709{
Thomas Zimmermann76e8cfd2020-01-23 14:59:34 +0100710 struct drm_device *dev = crtc->dev;
711 unsigned int pipe = crtc->index;
Rob Clarkc8afe682013-06-26 12:44:06 -0400712 struct msm_drm_private *priv = dev->dev_private;
713 struct msm_kms *kms = priv->kms;
714 if (!kms)
715 return -ENXIO;
Thierry Reding88e72712015-09-24 18:35:31 +0200716 DBG("dev=%p, crtc=%u", dev, pipe);
717 return vblank_ctrl_queue_work(priv, pipe, true);
Rob Clarkc8afe682013-06-26 12:44:06 -0400718}
719
Thomas Zimmermann76e8cfd2020-01-23 14:59:34 +0100720void msm_crtc_disable_vblank(struct drm_crtc *crtc)
Rob Clarkc8afe682013-06-26 12:44:06 -0400721{
Thomas Zimmermann76e8cfd2020-01-23 14:59:34 +0100722 struct drm_device *dev = crtc->dev;
723 unsigned int pipe = crtc->index;
Rob Clarkc8afe682013-06-26 12:44:06 -0400724 struct msm_drm_private *priv = dev->dev_private;
725 struct msm_kms *kms = priv->kms;
726 if (!kms)
727 return;
Thierry Reding88e72712015-09-24 18:35:31 +0200728 DBG("dev=%p, crtc=%u", dev, pipe);
729 vblank_ctrl_queue_work(priv, pipe, false);
Rob Clarkc8afe682013-06-26 12:44:06 -0400730}
731
732/*
Rob Clark7198e6b2013-07-19 12:59:32 -0400733 * DRM ioctls:
734 */
735
736static int msm_ioctl_get_param(struct drm_device *dev, void *data,
737 struct drm_file *file)
738{
739 struct msm_drm_private *priv = dev->dev_private;
740 struct drm_msm_param *args = data;
741 struct msm_gpu *gpu;
742
743 /* for now, we just have 3d pipe.. eventually this would need to
744 * be more clever to dispatch to appropriate gpu module:
745 */
746 if (args->pipe != MSM_PIPE_3D0)
747 return -EINVAL;
748
749 gpu = priv->gpu;
750
751 if (!gpu)
752 return -ENXIO;
753
754 return gpu->funcs->get_param(gpu, args->param, &args->value);
755}
756
757static int msm_ioctl_gem_new(struct drm_device *dev, void *data,
758 struct drm_file *file)
759{
760 struct drm_msm_gem_new *args = data;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500761
762 if (args->flags & ~MSM_BO_FLAGS) {
763 DRM_ERROR("invalid flags: %08x\n", args->flags);
764 return -EINVAL;
765 }
766
Rob Clark7198e6b2013-07-19 12:59:32 -0400767 return msm_gem_new_handle(dev, file, args->size,
Jordan Crouse0815d772018-11-07 15:35:52 -0700768 args->flags, &args->handle, NULL);
Rob Clark7198e6b2013-07-19 12:59:32 -0400769}
770
Rob Clark56c2da82015-05-11 11:50:03 -0400771static inline ktime_t to_ktime(struct drm_msm_timespec timeout)
772{
773 return ktime_set(timeout.tv_sec, timeout.tv_nsec);
774}
Rob Clark7198e6b2013-07-19 12:59:32 -0400775
776static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data,
777 struct drm_file *file)
778{
779 struct drm_msm_gem_cpu_prep *args = data;
780 struct drm_gem_object *obj;
Rob Clark56c2da82015-05-11 11:50:03 -0400781 ktime_t timeout = to_ktime(args->timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400782 int ret;
783
Rob Clark93ddb0d2014-03-03 09:42:33 -0500784 if (args->op & ~MSM_PREP_FLAGS) {
785 DRM_ERROR("invalid op: %08x\n", args->op);
786 return -EINVAL;
787 }
788
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100789 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400790 if (!obj)
791 return -ENOENT;
792
Rob Clark56c2da82015-05-11 11:50:03 -0400793 ret = msm_gem_cpu_prep(obj, args->op, &timeout);
Rob Clark7198e6b2013-07-19 12:59:32 -0400794
Emil Velikovf7d33952020-05-15 10:51:04 +0100795 drm_gem_object_put(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400796
797 return ret;
798}
799
800static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data,
801 struct drm_file *file)
802{
803 struct drm_msm_gem_cpu_fini *args = data;
804 struct drm_gem_object *obj;
805 int ret;
806
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100807 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400808 if (!obj)
809 return -ENOENT;
810
811 ret = msm_gem_cpu_fini(obj);
812
Emil Velikovf7d33952020-05-15 10:51:04 +0100813 drm_gem_object_put(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400814
815 return ret;
816}
817
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600818static int msm_ioctl_gem_info_iova(struct drm_device *dev,
Jordan Crouse933415e2020-08-17 15:01:40 -0700819 struct drm_file *file, struct drm_gem_object *obj,
820 uint64_t *iova)
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600821{
Iskren Chernev6cefa312021-01-02 22:24:37 +0200822 struct msm_drm_private *priv = dev->dev_private;
Jordan Crouse933415e2020-08-17 15:01:40 -0700823 struct msm_file_private *ctx = file->driver_priv;
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600824
Iskren Chernev6cefa312021-01-02 22:24:37 +0200825 if (!priv->gpu)
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600826 return -EINVAL;
827
Jordan Crouse9fe041f2018-11-07 15:35:50 -0700828 /*
829 * Don't pin the memory here - just get an address so that userspace can
830 * be productive
831 */
Jordan Crouse933415e2020-08-17 15:01:40 -0700832 return msm_gem_get_iova(obj, ctx->aspace, iova);
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600833}
834
Rob Clark7198e6b2013-07-19 12:59:32 -0400835static int msm_ioctl_gem_info(struct drm_device *dev, void *data,
836 struct drm_file *file)
837{
838 struct drm_msm_gem_info *args = data;
839 struct drm_gem_object *obj;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500840 struct msm_gem_object *msm_obj;
841 int i, ret = 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400842
Rob Clark789d2e52018-11-29 09:54:42 -0500843 if (args->pad)
Rob Clark7198e6b2013-07-19 12:59:32 -0400844 return -EINVAL;
845
Rob Clark789d2e52018-11-29 09:54:42 -0500846 switch (args->info) {
847 case MSM_INFO_GET_OFFSET:
848 case MSM_INFO_GET_IOVA:
849 /* value returned as immediate, not pointer, so len==0: */
850 if (args->len)
851 return -EINVAL;
852 break;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500853 case MSM_INFO_SET_NAME:
854 case MSM_INFO_GET_NAME:
855 break;
Rob Clark789d2e52018-11-29 09:54:42 -0500856 default:
857 return -EINVAL;
858 }
859
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100860 obj = drm_gem_object_lookup(file, args->handle);
Rob Clark7198e6b2013-07-19 12:59:32 -0400861 if (!obj)
862 return -ENOENT;
863
Rob Clarkf05c83e2018-11-29 10:27:22 -0500864 msm_obj = to_msm_bo(obj);
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600865
Rob Clark789d2e52018-11-29 09:54:42 -0500866 switch (args->info) {
867 case MSM_INFO_GET_OFFSET:
868 args->value = msm_gem_mmap_offset(obj);
869 break;
870 case MSM_INFO_GET_IOVA:
Jordan Crouse933415e2020-08-17 15:01:40 -0700871 ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value);
Rob Clark789d2e52018-11-29 09:54:42 -0500872 break;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500873 case MSM_INFO_SET_NAME:
874 /* length check should leave room for terminating null: */
875 if (args->len >= sizeof(msm_obj->name)) {
876 ret = -EINVAL;
877 break;
878 }
Dan Carpenter7cce8e42019-02-14 10:19:27 +0300879 if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value),
Jordan Crouse860433e2019-02-19 11:40:19 -0700880 args->len)) {
881 msm_obj->name[0] = '\0';
Dan Carpenter7cce8e42019-02-14 10:19:27 +0300882 ret = -EFAULT;
Jordan Crouse860433e2019-02-19 11:40:19 -0700883 break;
884 }
Rob Clarkf05c83e2018-11-29 10:27:22 -0500885 msm_obj->name[args->len] = '\0';
886 for (i = 0; i < args->len; i++) {
887 if (!isprint(msm_obj->name[i])) {
888 msm_obj->name[i] = '\0';
889 break;
890 }
891 }
892 break;
893 case MSM_INFO_GET_NAME:
894 if (args->value && (args->len < strlen(msm_obj->name))) {
895 ret = -EINVAL;
896 break;
897 }
898 args->len = strlen(msm_obj->name);
899 if (args->value) {
Dan Carpenter7cce8e42019-02-14 10:19:27 +0300900 if (copy_to_user(u64_to_user_ptr(args->value),
901 msm_obj->name, args->len))
902 ret = -EFAULT;
Rob Clarkf05c83e2018-11-29 10:27:22 -0500903 }
904 break;
Jordan Crouse49fd08b2017-05-08 14:35:01 -0600905 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400906
Emil Velikovf7d33952020-05-15 10:51:04 +0100907 drm_gem_object_put(obj);
Rob Clark7198e6b2013-07-19 12:59:32 -0400908
909 return ret;
910}
911
912static int msm_ioctl_wait_fence(struct drm_device *dev, void *data,
913 struct drm_file *file)
914{
Rob Clarkca762a82016-03-15 17:22:13 -0400915 struct msm_drm_private *priv = dev->dev_private;
Rob Clark7198e6b2013-07-19 12:59:32 -0400916 struct drm_msm_wait_fence *args = data;
Rob Clark56c2da82015-05-11 11:50:03 -0400917 ktime_t timeout = to_ktime(args->timeout);
Jordan Crousef97deca2017-10-20 11:06:57 -0600918 struct msm_gpu_submitqueue *queue;
919 struct msm_gpu *gpu = priv->gpu;
920 int ret;
Rob Clark93ddb0d2014-03-03 09:42:33 -0500921
922 if (args->pad) {
923 DRM_ERROR("invalid pad: %08x\n", args->pad);
924 return -EINVAL;
925 }
926
Jordan Crousef97deca2017-10-20 11:06:57 -0600927 if (!gpu)
Rob Clarkca762a82016-03-15 17:22:13 -0400928 return 0;
929
Jordan Crousef97deca2017-10-20 11:06:57 -0600930 queue = msm_submitqueue_get(file->driver_priv, args->queueid);
931 if (!queue)
932 return -ENOENT;
933
934 ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout,
935 true);
936
937 msm_submitqueue_put(queue);
938 return ret;
Rob Clark7198e6b2013-07-19 12:59:32 -0400939}
940
Rob Clark4cd33c42016-05-17 15:44:49 -0400941static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data,
942 struct drm_file *file)
943{
944 struct drm_msm_gem_madvise *args = data;
945 struct drm_gem_object *obj;
946 int ret;
947
948 switch (args->madv) {
949 case MSM_MADV_DONTNEED:
950 case MSM_MADV_WILLNEED:
951 break;
952 default:
953 return -EINVAL;
954 }
955
Rob Clark4cd33c42016-05-17 15:44:49 -0400956 obj = drm_gem_object_lookup(file, args->handle);
957 if (!obj) {
Rob Clarkf92f0262020-10-23 09:51:22 -0700958 return -ENOENT;
Rob Clark4cd33c42016-05-17 15:44:49 -0400959 }
960
961 ret = msm_gem_madvise(obj, args->madv);
962 if (ret >= 0) {
963 args->retained = ret;
964 ret = 0;
965 }
966
Rob Clarkf92f0262020-10-23 09:51:22 -0700967 drm_gem_object_put(obj);
Rob Clark4cd33c42016-05-17 15:44:49 -0400968
Rob Clark4cd33c42016-05-17 15:44:49 -0400969 return ret;
970}
971
Jordan Crousef7de1542017-10-20 11:06:55 -0600972
973static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data,
974 struct drm_file *file)
975{
976 struct drm_msm_submitqueue *args = data;
977
978 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS)
979 return -EINVAL;
980
Jordan Crousef97deca2017-10-20 11:06:57 -0600981 return msm_submitqueue_create(dev, file->driver_priv, args->prio,
Jordan Crousef7de1542017-10-20 11:06:55 -0600982 args->flags, &args->id);
983}
984
Jordan Crouseb0fb6602019-03-22 14:21:22 -0600985static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data,
986 struct drm_file *file)
987{
988 return msm_submitqueue_query(dev, file->driver_priv, data);
989}
Jordan Crousef7de1542017-10-20 11:06:55 -0600990
991static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data,
992 struct drm_file *file)
993{
994 u32 id = *(u32 *) data;
995
996 return msm_submitqueue_remove(file->driver_priv, id);
997}
998
Rob Clark7198e6b2013-07-19 12:59:32 -0400999static const struct drm_ioctl_desc msm_ioctls[] = {
Emil Velikov34127c72019-05-27 09:17:35 +01001000 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_RENDER_ALLOW),
1001 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_RENDER_ALLOW),
1002 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_RENDER_ALLOW),
1003 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW),
1004 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW),
1005 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_RENDER_ALLOW),
1006 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_RENDER_ALLOW),
1007 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_RENDER_ALLOW),
1008 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_RENDER_ALLOW),
1009 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW),
1010 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW),
Rob Clark7198e6b2013-07-19 12:59:32 -04001011};
1012
Rob Clarkc8afe682013-06-26 12:44:06 -04001013static const struct file_operations fops = {
1014 .owner = THIS_MODULE,
1015 .open = drm_open,
1016 .release = drm_release,
1017 .unlocked_ioctl = drm_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -04001018 .compat_ioctl = drm_compat_ioctl,
Rob Clarkc8afe682013-06-26 12:44:06 -04001019 .poll = drm_poll,
1020 .read = drm_read,
1021 .llseek = no_llseek,
1022 .mmap = msm_gem_mmap,
1023};
1024
Daniel Vetter70a59dd2020-11-04 11:04:24 +01001025static const struct drm_driver msm_driver = {
Daniel Vetter5b38e742019-01-29 11:42:46 +01001026 .driver_features = DRIVER_GEM |
Rob Clarkb4b15c82013-09-28 12:01:25 -04001027 DRIVER_RENDER |
Rob Clarka5436e12015-06-04 10:12:22 -04001028 DRIVER_ATOMIC |
Bas Nieuwenhuizenab723b72020-01-24 00:57:10 +01001029 DRIVER_MODESET |
1030 DRIVER_SYNCOBJ,
Rob Clark7198e6b2013-07-19 12:59:32 -04001031 .open = msm_open,
Daniel Vetter94df1452017-03-08 15:12:46 +01001032 .postclose = msm_postclose,
Noralf Trønnes4ccbc6e2017-12-05 19:24:59 +01001033 .lastclose = drm_fb_helper_lastclose,
Rob Clarkc8afe682013-06-26 12:44:06 -04001034 .irq_handler = msm_irq,
1035 .irq_preinstall = msm_irq_preinstall,
1036 .irq_postinstall = msm_irq_postinstall,
1037 .irq_uninstall = msm_irq_uninstall,
Rob Clarkc8afe682013-06-26 12:44:06 -04001038 .dumb_create = msm_gem_dumb_create,
1039 .dumb_map_offset = msm_gem_dumb_map_offset,
Rob Clark05b84912013-09-28 11:28:35 -04001040 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1041 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Rob Clark05b84912013-09-28 11:28:35 -04001042 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table,
Daniel Thompson77a147e2014-11-12 11:38:14 +00001043 .gem_prime_mmap = msm_gem_prime_mmap,
Rob Clarkc8afe682013-06-26 12:44:06 -04001044#ifdef CONFIG_DEBUG_FS
1045 .debugfs_init = msm_debugfs_init,
Rob Clarkc8afe682013-06-26 12:44:06 -04001046#endif
Rob Clark7198e6b2013-07-19 12:59:32 -04001047 .ioctls = msm_ioctls,
Jordan Crouse167b6062017-05-08 14:34:59 -06001048 .num_ioctls = ARRAY_SIZE(msm_ioctls),
Rob Clarkc8afe682013-06-26 12:44:06 -04001049 .fops = &fops,
1050 .name = "msm",
1051 .desc = "MSM Snapdragon DRM",
1052 .date = "20130625",
Rob Clarka8d854c2016-06-01 14:02:02 -04001053 .major = MSM_VERSION_MAJOR,
1054 .minor = MSM_VERSION_MINOR,
1055 .patchlevel = MSM_VERSION_PATCHLEVEL,
Rob Clarkc8afe682013-06-26 12:44:06 -04001056};
1057
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301058static int __maybe_unused msm_runtime_suspend(struct device *dev)
Archit Taneja774e39e2017-07-28 16:17:07 +05301059{
1060 struct drm_device *ddev = dev_get_drvdata(dev);
1061 struct msm_drm_private *priv = ddev->dev_private;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001062 struct msm_mdss *mdss = priv->mdss;
Archit Taneja774e39e2017-07-28 16:17:07 +05301063
1064 DBG("");
1065
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001066 if (mdss && mdss->funcs)
1067 return mdss->funcs->disable(mdss);
Archit Taneja774e39e2017-07-28 16:17:07 +05301068
1069 return 0;
1070}
1071
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301072static int __maybe_unused msm_runtime_resume(struct device *dev)
Archit Taneja774e39e2017-07-28 16:17:07 +05301073{
1074 struct drm_device *ddev = dev_get_drvdata(dev);
1075 struct msm_drm_private *priv = ddev->dev_private;
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001076 struct msm_mdss *mdss = priv->mdss;
Archit Taneja774e39e2017-07-28 16:17:07 +05301077
1078 DBG("");
1079
Rajesh Yadavbc3220b2018-06-21 16:06:10 -04001080 if (mdss && mdss->funcs)
1081 return mdss->funcs->enable(mdss);
Archit Taneja774e39e2017-07-28 16:17:07 +05301082
1083 return 0;
1084}
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301085
1086static int __maybe_unused msm_pm_suspend(struct device *dev)
1087{
1088
1089 if (pm_runtime_suspended(dev))
1090 return 0;
1091
1092 return msm_runtime_suspend(dev);
1093}
1094
1095static int __maybe_unused msm_pm_resume(struct device *dev)
1096{
1097 if (pm_runtime_suspended(dev))
1098 return 0;
1099
1100 return msm_runtime_resume(dev);
1101}
1102
1103static int __maybe_unused msm_pm_prepare(struct device *dev)
1104{
1105 struct drm_device *ddev = dev_get_drvdata(dev);
Fabio Estevama9748132021-03-20 08:56:03 -03001106 struct msm_drm_private *priv = ddev ? ddev->dev_private : NULL;
1107
1108 if (!priv || !priv->kms)
1109 return 0;
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301110
1111 return drm_mode_config_helper_suspend(ddev);
1112}
1113
1114static void __maybe_unused msm_pm_complete(struct device *dev)
1115{
1116 struct drm_device *ddev = dev_get_drvdata(dev);
Fabio Estevama9748132021-03-20 08:56:03 -03001117 struct msm_drm_private *priv = ddev ? ddev->dev_private : NULL;
1118
1119 if (!priv || !priv->kms)
1120 return;
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301121
1122 drm_mode_config_helper_resume(ddev);
1123}
Archit Taneja774e39e2017-07-28 16:17:07 +05301124
Rob Clarkc8afe682013-06-26 12:44:06 -04001125static const struct dev_pm_ops msm_pm_ops = {
1126 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume)
Archit Taneja774e39e2017-07-28 16:17:07 +05301127 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL)
Kalyan Thotaca8199f2020-06-18 19:31:24 +05301128 .prepare = msm_pm_prepare,
1129 .complete = msm_pm_complete,
Rob Clarkc8afe682013-06-26 12:44:06 -04001130};
1131
1132/*
Rob Clark060530f2014-03-03 14:19:12 -05001133 * Componentized driver support:
1134 */
1135
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301136/*
1137 * NOTE: duplication of the same code as exynos or imx (or probably any other).
1138 * so probably some room for some helpers
Rob Clark060530f2014-03-03 14:19:12 -05001139 */
1140static int compare_of(struct device *dev, void *data)
1141{
1142 return dev->of_node == data;
1143}
Rob Clark41e69772013-12-15 16:23:05 -05001144
Archit Taneja812070e2016-05-19 10:38:39 +05301145/*
1146 * Identify what components need to be added by parsing what remote-endpoints
1147 * our MDP output ports are connected to. In the case of LVDS on MDP4, there
1148 * is no external component that we need to add since LVDS is within MDP4
1149 * itself.
1150 */
1151static int add_components_mdp(struct device *mdp_dev,
1152 struct component_match **matchptr)
1153{
1154 struct device_node *np = mdp_dev->of_node;
1155 struct device_node *ep_node;
Archit Taneja54011e22016-06-06 13:45:34 +05301156 struct device *master_dev;
1157
1158 /*
1159 * on MDP4 based platforms, the MDP platform device is the component
1160 * master that adds other display interface components to itself.
1161 *
1162 * on MDP5 based platforms, the MDSS platform device is the component
1163 * master that adds MDP5 and other display interface components to
1164 * itself.
1165 */
1166 if (of_device_is_compatible(np, "qcom,mdp4"))
1167 master_dev = mdp_dev;
1168 else
1169 master_dev = mdp_dev->parent;
Archit Taneja812070e2016-05-19 10:38:39 +05301170
1171 for_each_endpoint_of_node(np, ep_node) {
1172 struct device_node *intf;
1173 struct of_endpoint ep;
1174 int ret;
1175
1176 ret = of_graph_parse_endpoint(ep_node, &ep);
1177 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301178 DRM_DEV_ERROR(mdp_dev, "unable to parse port endpoint\n");
Archit Taneja812070e2016-05-19 10:38:39 +05301179 of_node_put(ep_node);
1180 return ret;
1181 }
1182
1183 /*
1184 * The LCDC/LVDS port on MDP4 is a speacial case where the
1185 * remote-endpoint isn't a component that we need to add
1186 */
1187 if (of_device_is_compatible(np, "qcom,mdp4") &&
Archit Tanejad8dd8052016-11-17 12:12:03 +05301188 ep.port == 0)
Archit Taneja812070e2016-05-19 10:38:39 +05301189 continue;
Archit Taneja812070e2016-05-19 10:38:39 +05301190
1191 /*
1192 * It's okay if some of the ports don't have a remote endpoint
1193 * specified. It just means that the port isn't connected to
1194 * any external interface.
1195 */
1196 intf = of_graph_get_remote_port_parent(ep_node);
Archit Tanejad8dd8052016-11-17 12:12:03 +05301197 if (!intf)
Archit Taneja812070e2016-05-19 10:38:39 +05301198 continue;
Archit Taneja812070e2016-05-19 10:38:39 +05301199
Douglas Andersond1d9d0e2018-12-04 10:04:41 -08001200 if (of_device_is_available(intf))
1201 drm_of_component_match_add(master_dev, matchptr,
1202 compare_of, intf);
1203
Archit Taneja812070e2016-05-19 10:38:39 +05301204 of_node_put(intf);
Archit Taneja812070e2016-05-19 10:38:39 +05301205 }
1206
1207 return 0;
1208}
1209
Archit Taneja54011e22016-06-06 13:45:34 +05301210static int compare_name_mdp(struct device *dev, void *data)
1211{
1212 return (strstr(dev_name(dev), "mdp") != NULL);
1213}
1214
Bjorn Andersson84240842021-03-16 19:56:34 -07001215static int add_display_components(struct platform_device *pdev,
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301216 struct component_match **matchptr)
1217{
Archit Taneja54011e22016-06-06 13:45:34 +05301218 struct device *mdp_dev;
Bjorn Andersson84240842021-03-16 19:56:34 -07001219 struct device *dev = &pdev->dev;
Archit Taneja54011e22016-06-06 13:45:34 +05301220 int ret;
1221
1222 /*
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001223 * MDP5/DPU based devices don't have a flat hierarchy. There is a top
1224 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc.
1225 * Populate the children devices, find the MDP5/DPU node, and then add
1226 * the interfaces to our components list.
Archit Taneja54011e22016-06-06 13:45:34 +05301227 */
Bjorn Andersson84240842021-03-16 19:56:34 -07001228 switch (get_mdp_ver(pdev)) {
1229 case KMS_MDP5:
1230 case KMS_DPU:
Archit Taneja54011e22016-06-06 13:45:34 +05301231 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
1232 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301233 DRM_DEV_ERROR(dev, "failed to populate children devices\n");
Archit Taneja54011e22016-06-06 13:45:34 +05301234 return ret;
1235 }
1236
1237 mdp_dev = device_find_child(dev, NULL, compare_name_mdp);
1238 if (!mdp_dev) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301239 DRM_DEV_ERROR(dev, "failed to find MDSS MDP node\n");
Archit Taneja54011e22016-06-06 13:45:34 +05301240 of_platform_depopulate(dev);
1241 return -ENODEV;
1242 }
1243
1244 put_device(mdp_dev);
1245
1246 /* add the MDP component itself */
Russell King97ac0e42016-10-19 11:28:27 +01001247 drm_of_component_match_add(dev, matchptr, compare_of,
1248 mdp_dev->of_node);
Bjorn Andersson84240842021-03-16 19:56:34 -07001249 break;
1250 case KMS_MDP4:
Archit Taneja54011e22016-06-06 13:45:34 +05301251 /* MDP4 */
1252 mdp_dev = dev;
Bjorn Andersson84240842021-03-16 19:56:34 -07001253 break;
Archit Taneja54011e22016-06-06 13:45:34 +05301254 }
1255
1256 ret = add_components_mdp(mdp_dev, matchptr);
1257 if (ret)
1258 of_platform_depopulate(dev);
1259
1260 return ret;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301261}
1262
Archit Tanejadc3ea262016-05-19 13:33:52 +05301263/*
1264 * We don't know what's the best binding to link the gpu with the drm device.
1265 * Fow now, we just hunt for all the possible gpus that we support, and add them
1266 * as components.
1267 */
1268static const struct of_device_id msm_gpu_match[] = {
Rob Clark1db7afa2017-01-30 11:02:27 -05001269 { .compatible = "qcom,adreno" },
Archit Tanejadc3ea262016-05-19 13:33:52 +05301270 { .compatible = "qcom,adreno-3xx" },
Jonathan Mareke6f6d632018-12-04 10:16:58 -05001271 { .compatible = "amd,imageon" },
Archit Tanejadc3ea262016-05-19 13:33:52 +05301272 { .compatible = "qcom,kgsl-3d0" },
1273 { },
1274};
1275
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301276static int add_gpu_components(struct device *dev,
1277 struct component_match **matchptr)
1278{
Archit Tanejadc3ea262016-05-19 13:33:52 +05301279 struct device_node *np;
1280
1281 np = of_find_matching_node(NULL, msm_gpu_match);
1282 if (!np)
1283 return 0;
1284
Jeffrey Hugo9ca7ad62019-06-26 11:00:15 -07001285 if (of_device_is_available(np))
1286 drm_of_component_match_add(dev, matchptr, compare_of, np);
Archit Tanejadc3ea262016-05-19 13:33:52 +05301287
1288 of_node_put(np);
1289
1290 return 0;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301291}
1292
Russell King84448282014-04-19 11:20:42 +01001293static int msm_drm_bind(struct device *dev)
1294{
Archit Taneja2b669872016-05-02 11:05:54 +05301295 return msm_drm_init(dev, &msm_driver);
Russell King84448282014-04-19 11:20:42 +01001296}
1297
1298static void msm_drm_unbind(struct device *dev)
1299{
Archit Taneja2b669872016-05-02 11:05:54 +05301300 msm_drm_uninit(dev);
Russell King84448282014-04-19 11:20:42 +01001301}
1302
1303static const struct component_master_ops msm_drm_ops = {
1304 .bind = msm_drm_bind,
1305 .unbind = msm_drm_unbind,
1306};
1307
1308/*
1309 * Platform driver:
1310 */
1311
1312static int msm_pdev_probe(struct platform_device *pdev)
1313{
1314 struct component_match *match = NULL;
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301315 int ret;
Archit Tanejae9fbdaf2015-11-18 12:15:14 +05301316
Jonathan Mareke6f6d632018-12-04 10:16:58 -05001317 if (get_mdp_ver(pdev)) {
Bjorn Andersson84240842021-03-16 19:56:34 -07001318 ret = add_display_components(pdev, &match);
Jonathan Mareke6f6d632018-12-04 10:16:58 -05001319 if (ret)
1320 return ret;
1321 }
Archit Taneja7d526fcf2016-05-19 10:33:57 +05301322
1323 ret = add_gpu_components(&pdev->dev, &match);
1324 if (ret)
Sean Paul4368a152019-06-17 16:12:51 -04001325 goto fail;
Rob Clark060530f2014-03-03 14:19:12 -05001326
Rob Clarkc83ea572016-11-07 13:31:30 -05001327 /* on all devices that I am aware of, iommu's which can map
1328 * any address the cpu can see are used:
1329 */
1330 ret = dma_set_mask_and_coherent(&pdev->dev, ~0);
1331 if (ret)
Sean Paul4368a152019-06-17 16:12:51 -04001332 goto fail;
Rob Clarkc83ea572016-11-07 13:31:30 -05001333
Sean Paul4368a152019-06-17 16:12:51 -04001334 ret = component_master_add_with_match(&pdev->dev, &msm_drm_ops, match);
1335 if (ret)
1336 goto fail;
1337
1338 return 0;
1339
1340fail:
1341 of_platform_depopulate(&pdev->dev);
1342 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -04001343}
1344
1345static int msm_pdev_remove(struct platform_device *pdev)
1346{
Rob Clark060530f2014-03-03 14:19:12 -05001347 component_master_del(&pdev->dev, &msm_drm_ops);
Archit Taneja54011e22016-06-06 13:45:34 +05301348 of_platform_depopulate(&pdev->dev);
Rob Clarkc8afe682013-06-26 12:44:06 -04001349
1350 return 0;
1351}
1352
Krishna Manikandan9d5cbf52020-06-01 16:33:22 +05301353static void msm_pdev_shutdown(struct platform_device *pdev)
1354{
1355 struct drm_device *drm = platform_get_drvdata(pdev);
Dmitry Baryshkov623f2792021-03-20 08:56:02 -03001356 struct msm_drm_private *priv = drm ? drm->dev_private : NULL;
1357
1358 if (!priv || !priv->kms)
1359 return;
Krishna Manikandan9d5cbf52020-06-01 16:33:22 +05301360
1361 drm_atomic_helper_shutdown(drm);
1362}
1363
Rob Clark06c0dd92013-11-30 17:51:47 -05001364static const struct of_device_id dt_match[] = {
Jeykumar Sankaranaaded2e2018-06-27 14:26:24 -04001365 { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 },
1366 { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 },
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001367 { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU },
Kalyan Thota7bdc0c42019-11-25 17:29:27 +05301368 { .compatible = "qcom,sc7180-mdss", .data = (void *)KMS_DPU },
Krishna Manikandan591e34a2021-04-06 10:39:49 +05301369 { .compatible = "qcom,sc7280-mdss", .data = (void *)KMS_DPU },
Jonathan Marek0ba17e72021-03-29 15:00:50 +03001370 { .compatible = "qcom,sm8150-mdss", .data = (void *)KMS_DPU },
1371 { .compatible = "qcom,sm8250-mdss", .data = (void *)KMS_DPU },
Rob Clark06c0dd92013-11-30 17:51:47 -05001372 {}
1373};
1374MODULE_DEVICE_TABLE(of, dt_match);
1375
Rob Clarkc8afe682013-06-26 12:44:06 -04001376static struct platform_driver msm_platform_driver = {
1377 .probe = msm_pdev_probe,
1378 .remove = msm_pdev_remove,
Krishna Manikandan9d5cbf52020-06-01 16:33:22 +05301379 .shutdown = msm_pdev_shutdown,
Rob Clarkc8afe682013-06-26 12:44:06 -04001380 .driver = {
Rob Clarkc8afe682013-06-26 12:44:06 -04001381 .name = "msm",
Rob Clark06c0dd92013-11-30 17:51:47 -05001382 .of_match_table = dt_match,
Rob Clarkc8afe682013-06-26 12:44:06 -04001383 .pm = &msm_pm_ops,
1384 },
Rob Clarkc8afe682013-06-26 12:44:06 -04001385};
1386
1387static int __init msm_drm_register(void)
1388{
Rob Clarkba4dd712017-07-06 16:33:44 -04001389 if (!modeset)
1390 return -EINVAL;
1391
Rob Clarkc8afe682013-06-26 12:44:06 -04001392 DBG("init");
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301393 msm_mdp_register();
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001394 msm_dpu_register();
Hai Lid5af49c2015-03-26 19:25:17 -04001395 msm_dsi_register();
Hai Li00453982014-12-12 14:41:17 -05001396 msm_edp_register();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001397 msm_hdmi_register();
Chandan Uddarajuc943b492020-08-27 14:16:55 -07001398 msm_dp_register();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001399 adreno_register();
Rob Clarkc8afe682013-06-26 12:44:06 -04001400 return platform_driver_register(&msm_platform_driver);
1401}
1402
1403static void __exit msm_drm_unregister(void)
1404{
1405 DBG("fini");
1406 platform_driver_unregister(&msm_platform_driver);
Chandan Uddarajuc943b492020-08-27 14:16:55 -07001407 msm_dp_unregister();
Arnd Bergmannfcda50c2016-02-22 22:08:35 +01001408 msm_hdmi_unregister();
Rob Clarkbfd28b12014-09-05 13:06:37 -04001409 adreno_unregister();
Hai Li00453982014-12-12 14:41:17 -05001410 msm_edp_unregister();
Hai Lid5af49c2015-03-26 19:25:17 -04001411 msm_dsi_unregister();
Archit Taneja1dd0a0b2016-05-30 16:36:50 +05301412 msm_mdp_unregister();
Jeykumar Sankaran25fdd592018-06-27 15:26:09 -04001413 msm_dpu_unregister();
Rob Clarkc8afe682013-06-26 12:44:06 -04001414}
1415
1416module_init(msm_drm_register);
1417module_exit(msm_drm_unregister);
1418
1419MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1420MODULE_DESCRIPTION("MSM DRM Driver");
1421MODULE_LICENSE("GPL");