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