]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/rockchip/rockchip_drm_drv.c
drm/rockchip: Refactor the component match logic.
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / rockchip / rockchip_drm_drv.c
CommitLineData
2048e328
MY
1/*
2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author:Mark Yao <mark.yao@rock-chips.com>
4 *
5 * based on exynos_drm_drv.c
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
2048e328
MY
17#include <drm/drmP.h>
18#include <drm/drm_crtc_helper.h>
19#include <drm/drm_fb_helper.h>
80f67cd8 20#include <drm/drm_gem_cma_helper.h>
97ac0e47 21#include <drm/drm_of.h>
2048e328 22#include <linux/dma-mapping.h>
1aa5ca6e 23#include <linux/dma-iommu.h>
2048e328 24#include <linux/pm_runtime.h>
00fe6148 25#include <linux/module.h>
2048e328
MY
26#include <linux/of_graph.h>
27#include <linux/component.h>
5a587383 28#include <linux/console.h>
1aa5ca6e 29#include <linux/iommu.h>
2048e328
MY
30
31#include "rockchip_drm_drv.h"
32#include "rockchip_drm_fb.h"
33#include "rockchip_drm_fbdev.h"
34#include "rockchip_drm_gem.h"
35
36#define DRIVER_NAME "rockchip"
37#define DRIVER_DESC "RockChip Soc DRM"
38#define DRIVER_DATE "20140818"
39#define DRIVER_MAJOR 1
40#define DRIVER_MINOR 0
41
2d90d477 42static bool is_support_iommu = true;
f706974a 43static struct drm_driver rockchip_drm_driver;
2d90d477 44
2048e328
MY
45/*
46 * Attach a (component) device to the shared drm dma mapping from master drm
47 * device. This is used by the VOPs to map GEM buffers to a common DMA
48 * mapping.
49 */
50int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
51 struct device *dev)
52{
1aa5ca6e 53 struct rockchip_drm_private *private = drm_dev->dev_private;
2048e328
MY
54 int ret;
55
2d90d477
MY
56 if (!is_support_iommu)
57 return 0;
58
1aa5ca6e
SZ
59 ret = iommu_attach_device(private->domain, dev);
60 if (ret) {
61 dev_err(dev, "Failed to attach iommu device\n");
2048e328 62 return ret;
1aa5ca6e 63 }
2048e328 64
1aa5ca6e 65 return 0;
2048e328 66}
2048e328
MY
67
68void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
69 struct device *dev)
70{
1aa5ca6e
SZ
71 struct rockchip_drm_private *private = drm_dev->dev_private;
72 struct iommu_domain *domain = private->domain;
73
2d90d477
MY
74 if (!is_support_iommu)
75 return;
76
1aa5ca6e 77 iommu_detach_device(domain, dev);
2048e328 78}
2048e328 79
1aa5ca6e
SZ
80static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
81{
82 struct rockchip_drm_private *private = drm_dev->dev_private;
83 struct iommu_domain_geometry *geometry;
84 u64 start, end;
85
86 if (!is_support_iommu)
87 return 0;
88
89 private->domain = iommu_domain_alloc(&platform_bus_type);
90 if (!private->domain)
91 return -ENOMEM;
92
93 geometry = &private->domain->geometry;
94 start = geometry->aperture_start;
95 end = geometry->aperture_end;
96
97 DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
98 start, end);
99 drm_mm_init(&private->mm, start, end - start + 1);
100 mutex_init(&private->mm_lock);
101
102 return 0;
103}
104
105static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
106{
107 struct rockchip_drm_private *private = drm_dev->dev_private;
108
109 if (!is_support_iommu)
110 return;
111
112 drm_mm_takedown(&private->mm);
113 iommu_domain_free(private->domain);
2048e328 114}
2048e328 115
f706974a 116static int rockchip_drm_bind(struct device *dev)
2048e328 117{
f706974a 118 struct drm_device *drm_dev;
2048e328 119 struct rockchip_drm_private *private;
2048e328
MY
120 int ret;
121
f706974a 122 drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
0f288605
TG
123 if (IS_ERR(drm_dev))
124 return PTR_ERR(drm_dev);
2048e328 125
f706974a
TV
126 dev_set_drvdata(dev, drm_dev);
127
128 private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
129 if (!private) {
130 ret = -ENOMEM;
9127f99c 131 goto err_free;
f706974a
TV
132 }
133
2048e328
MY
134 drm_dev->dev_private = private;
135
5182c1a5 136 INIT_LIST_HEAD(&private->psr_list);
18d8d4d2 137 spin_lock_init(&private->psr_list_lock);
5182c1a5 138
2048e328
MY
139 drm_mode_config_init(drm_dev);
140
141 rockchip_drm_mode_config_init(drm_dev);
142
1aa5ca6e
SZ
143 ret = rockchip_drm_init_iommu(drm_dev);
144 if (ret)
2048e328 145 goto err_config_cleanup;
2048e328
MY
146
147 /* Try to bind all sub drivers. */
148 ret = component_bind_all(dev, drm_dev);
149 if (ret)
1aa5ca6e 150 goto err_iommu_cleanup;
2048e328
MY
151
152 /* init kms poll for handling hpd */
153 drm_kms_helper_poll_init(drm_dev);
154
155 /*
156 * enable drm irq mode.
157 * - with irq_enabled = true, we can use the vblank feature.
158 */
159 drm_dev->irq_enabled = true;
160
161 ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
162 if (ret)
163 goto err_kms_helper_poll_fini;
164
63ebb9fa
MY
165 drm_mode_config_reset(drm_dev);
166
2048e328
MY
167 ret = rockchip_drm_fbdev_init(drm_dev);
168 if (ret)
169 goto err_vblank_cleanup;
170
9127f99c
TF
171 ret = drm_dev_register(drm_dev, 0);
172 if (ret)
173 goto err_fbdev_fini;
174
2048e328 175 return 0;
9127f99c
TF
176err_fbdev_fini:
177 rockchip_drm_fbdev_fini(drm_dev);
2048e328
MY
178err_vblank_cleanup:
179 drm_vblank_cleanup(drm_dev);
180err_kms_helper_poll_fini:
181 drm_kms_helper_poll_fini(drm_dev);
182 component_unbind_all(dev, drm_dev);
1aa5ca6e
SZ
183err_iommu_cleanup:
184 rockchip_iommu_cleanup(drm_dev);
2048e328
MY
185err_config_cleanup:
186 drm_mode_config_cleanup(drm_dev);
187 drm_dev->dev_private = NULL;
f706974a
TV
188err_free:
189 drm_dev_unref(drm_dev);
2048e328
MY
190 return ret;
191}
192
f706974a 193static void rockchip_drm_unbind(struct device *dev)
2048e328 194{
f706974a 195 struct drm_device *drm_dev = dev_get_drvdata(dev);
2048e328
MY
196
197 rockchip_drm_fbdev_fini(drm_dev);
198 drm_vblank_cleanup(drm_dev);
199 drm_kms_helper_poll_fini(drm_dev);
200 component_unbind_all(dev, drm_dev);
1aa5ca6e 201 rockchip_iommu_cleanup(drm_dev);
2048e328
MY
202 drm_mode_config_cleanup(drm_dev);
203 drm_dev->dev_private = NULL;
f706974a
TV
204 drm_dev_unregister(drm_dev);
205 drm_dev_unref(drm_dev);
206 dev_set_drvdata(dev, NULL);
2048e328
MY
207}
208
8ff490ae 209static void rockchip_drm_lastclose(struct drm_device *dev)
2048e328
MY
210{
211 struct rockchip_drm_private *priv = dev->dev_private;
212
213 drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
214}
215
216static const struct file_operations rockchip_drm_driver_fops = {
217 .owner = THIS_MODULE,
218 .open = drm_open,
219 .mmap = rockchip_gem_mmap,
220 .poll = drm_poll,
221 .read = drm_read,
222 .unlocked_ioctl = drm_ioctl,
2048e328 223 .compat_ioctl = drm_compat_ioctl,
2048e328
MY
224 .release = drm_release,
225};
226
2048e328 227static struct drm_driver rockchip_drm_driver = {
63ebb9fa
MY
228 .driver_features = DRIVER_MODESET | DRIVER_GEM |
229 DRIVER_PRIME | DRIVER_ATOMIC,
2048e328 230 .lastclose = rockchip_drm_lastclose,
80f67cd8 231 .gem_vm_ops = &drm_gem_cma_vm_ops,
c2466ac3 232 .gem_free_object_unlocked = rockchip_gem_free_object,
2048e328
MY
233 .dumb_create = rockchip_gem_dumb_create,
234 .dumb_map_offset = rockchip_gem_dumb_map_offset,
235 .dumb_destroy = drm_gem_dumb_destroy,
236 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
237 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
238 .gem_prime_import = drm_gem_prime_import,
239 .gem_prime_export = drm_gem_prime_export,
240 .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
241 .gem_prime_vmap = rockchip_gem_prime_vmap,
242 .gem_prime_vunmap = rockchip_gem_prime_vunmap,
243 .gem_prime_mmap = rockchip_gem_mmap_buf,
244 .fops = &rockchip_drm_driver_fops,
245 .name = DRIVER_NAME,
246 .desc = DRIVER_DESC,
247 .date = DRIVER_DATE,
248 .major = DRIVER_MAJOR,
249 .minor = DRIVER_MINOR,
250};
251
252#ifdef CONFIG_PM_SLEEP
62398177 253static void rockchip_drm_fb_suspend(struct drm_device *drm)
2048e328 254{
5a587383 255 struct rockchip_drm_private *priv = drm->dev_private;
2048e328 256
5a587383
TV
257 console_lock();
258 drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
259 console_unlock();
260}
2048e328 261
62398177 262static void rockchip_drm_fb_resume(struct drm_device *drm)
5a587383
TV
263{
264 struct rockchip_drm_private *priv = drm->dev_private;
2048e328 265
5a587383
TV
266 console_lock();
267 drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
268 console_unlock();
269}
2048e328 270
5a587383
TV
271static int rockchip_drm_sys_suspend(struct device *dev)
272{
273 struct drm_device *drm = dev_get_drvdata(dev);
274 struct rockchip_drm_private *priv = drm->dev_private;
275
276 drm_kms_helper_poll_disable(drm);
277 rockchip_drm_fb_suspend(drm);
278
279 priv->state = drm_atomic_helper_suspend(drm);
280 if (IS_ERR(priv->state)) {
281 rockchip_drm_fb_resume(drm);
282 drm_kms_helper_poll_enable(drm);
283 return PTR_ERR(priv->state);
2048e328 284 }
2048e328
MY
285
286 return 0;
287}
288
289static int rockchip_drm_sys_resume(struct device *dev)
290{
291 struct drm_device *drm = dev_get_drvdata(dev);
5a587383 292 struct rockchip_drm_private *priv = drm->dev_private;
2048e328 293
5a587383
TV
294 drm_atomic_helper_resume(drm, priv->state);
295 rockchip_drm_fb_resume(drm);
296 drm_kms_helper_poll_enable(drm);
2048e328
MY
297
298 return 0;
299}
300#endif
301
302static const struct dev_pm_ops rockchip_drm_pm_ops = {
303 SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
304 rockchip_drm_sys_resume)
305};
306
8820b68b
JC
307#define MAX_ROCKCHIP_SUB_DRIVERS 16
308static struct platform_driver *rockchip_sub_drivers[MAX_ROCKCHIP_SUB_DRIVERS];
309static int num_rockchip_sub_drivers;
2048e328 310
8820b68b
JC
311static int compare_dev(struct device *dev, void *data)
312{
313 return dev == (struct device *)data;
2048e328
MY
314}
315
8820b68b 316static struct component_match *rockchip_drm_match_add(struct device *dev)
5bad7d29 317{
8820b68b
JC
318 struct component_match *match = NULL;
319 int i;
5bad7d29 320
8820b68b
JC
321 for (i = 0; i < num_rockchip_sub_drivers; i++) {
322 struct platform_driver *drv = rockchip_sub_drivers[i];
323 struct device *p = NULL, *d;
324
325 do {
326 d = bus_find_device(&platform_bus_type, p, &drv->driver,
327 (void *)platform_bus_type.match);
328 put_device(p);
329 p = d;
5bad7d29 330
8820b68b
JC
331 if (!d)
332 break;
333 component_match_add(dev, &match, compare_dev, d);
334 } while (true);
5bad7d29 335 }
8820b68b
JC
336
337 return match ?: ERR_PTR(-ENODEV);
5bad7d29
MY
338}
339
2048e328
MY
340static const struct component_master_ops rockchip_drm_ops = {
341 .bind = rockchip_drm_bind,
342 .unbind = rockchip_drm_unbind,
343};
344
8820b68b 345static int rockchip_drm_platform_of_probe(struct device *dev)
2048e328 346{
5bad7d29
MY
347 struct device_node *np = dev->of_node;
348 struct device_node *port;
8820b68b 349 bool found = false;
5bad7d29 350 int i;
2048e328 351
5bad7d29 352 if (!np)
2048e328 353 return -ENODEV;
8820b68b 354
5bad7d29 355 for (i = 0;; i++) {
2d90d477
MY
356 struct device_node *iommu;
357
5bad7d29
MY
358 port = of_parse_phandle(np, "ports", i);
359 if (!port)
360 break;
361
362 if (!of_device_is_available(port->parent)) {
363 of_node_put(port);
364 continue;
365 }
2048e328 366
2d90d477
MY
367 iommu = of_parse_phandle(port->parent, "iommus", 0);
368 if (!iommu || !of_device_is_available(iommu->parent)) {
369 dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
370 port->parent->full_name);
371 /*
372 * if there is a crtc not support iommu, force set all
373 * crtc use non-iommu buffer.
374 */
375 is_support_iommu = false;
376 }
377
8820b68b
JC
378 found = true;
379
6d5fa28c 380 of_node_put(iommu);
5bad7d29
MY
381 of_node_put(port);
382 }
383
384 if (i == 0) {
385 dev_err(dev, "missing 'ports' property\n");
386 return -ENODEV;
387 }
388
8820b68b 389 if (!found) {
5bad7d29
MY
390 dev_err(dev, "No available vop found for display-subsystem.\n");
391 return -ENODEV;
392 }
5bad7d29 393
8820b68b
JC
394 return 0;
395}
5bad7d29 396
8820b68b
JC
397static int rockchip_drm_platform_probe(struct platform_device *pdev)
398{
399 struct device *dev = &pdev->dev;
400 struct component_match *match = NULL;
401 int ret;
402
403 ret = rockchip_drm_platform_of_probe(dev);
404 if (ret)
405 return ret;
406
407 match = rockchip_drm_match_add(dev);
408 if (IS_ERR(match))
409 return PTR_ERR(match);
5bad7d29
MY
410
411 return component_master_add_with_match(dev, &rockchip_drm_ops, match);
2048e328
MY
412}
413
414static int rockchip_drm_platform_remove(struct platform_device *pdev)
415{
416 component_master_del(&pdev->dev, &rockchip_drm_ops);
417
418 return 0;
419}
420
421static const struct of_device_id rockchip_drm_dt_ids[] = {
422 { .compatible = "rockchip,display-subsystem", },
423 { /* sentinel */ },
424};
425MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
426
427static struct platform_driver rockchip_drm_platform_driver = {
428 .probe = rockchip_drm_platform_probe,
429 .remove = rockchip_drm_platform_remove,
430 .driver = {
2048e328
MY
431 .name = "rockchip-drm",
432 .of_match_table = rockchip_drm_dt_ids,
433 .pm = &rockchip_drm_pm_ops,
434 },
435};
436
8820b68b
JC
437#define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
438 if (IS_ENABLED(cond) && \
439 !WARN_ON(num_rockchip_sub_drivers >= MAX_ROCKCHIP_SUB_DRIVERS)) \
440 rockchip_sub_drivers[num_rockchip_sub_drivers++] = &drv; \
441}
442
443static int __init rockchip_drm_init(void)
444{
445 int ret;
446
447 num_rockchip_sub_drivers = 0;
448 ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_DRM_ROCKCHIP);
449 ADD_ROCKCHIP_SUB_DRIVER(rockchip_dp_driver,
450 CONFIG_ROCKCHIP_ANALOGIX_DP);
451 ADD_ROCKCHIP_SUB_DRIVER(cdn_dp_driver, CONFIG_ROCKCHIP_CDN_DP);
452 ADD_ROCKCHIP_SUB_DRIVER(dw_hdmi_rockchip_pltfm_driver,
453 CONFIG_ROCKCHIP_DW_HDMI);
454 ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi_driver,
455 CONFIG_ROCKCHIP_DW_MIPI_DSI);
456 ADD_ROCKCHIP_SUB_DRIVER(inno_hdmi_driver, CONFIG_ROCKCHIP_INNO_HDMI);
457
458 ret = platform_register_drivers(rockchip_sub_drivers,
459 num_rockchip_sub_drivers);
460 if (ret)
461 return ret;
462
463 ret = platform_driver_register(&rockchip_drm_platform_driver);
464 if (ret)
465 goto err_unreg_drivers;
466
467 return 0;
468
469err_unreg_drivers:
470 platform_unregister_drivers(rockchip_sub_drivers,
471 num_rockchip_sub_drivers);
472 return ret;
473}
474
475static void __exit rockchip_drm_fini(void)
476{
477 platform_driver_unregister(&rockchip_drm_platform_driver);
478
479 platform_unregister_drivers(rockchip_sub_drivers,
480 num_rockchip_sub_drivers);
481}
482
483module_init(rockchip_drm_init);
484module_exit(rockchip_drm_fini);
2048e328
MY
485
486MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
487MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
488MODULE_LICENSE("GPL v2");