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 
17 #include <drm/drmP.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_of.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/component.h>
28 #include <linux/console.h>
29 #include <linux/iommu.h>
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 
42 static bool is_support_iommu = true;
43 static struct drm_driver rockchip_drm_driver;
44 
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  */
50 int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
51 				   struct device *dev)
52 {
53 	struct rockchip_drm_private *private = drm_dev->dev_private;
54 	int ret;
55 
56 	if (!is_support_iommu)
57 		return 0;
58 
59 	ret = iommu_attach_device(private->domain, dev);
60 	if (ret) {
61 		DRM_DEV_ERROR(dev, "Failed to attach iommu device\n");
62 		return ret;
63 	}
64 
65 	return 0;
66 }
67 
68 void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
69 				    struct device *dev)
70 {
71 	struct rockchip_drm_private *private = drm_dev->dev_private;
72 	struct iommu_domain *domain = private->domain;
73 
74 	if (!is_support_iommu)
75 		return;
76 
77 	iommu_detach_device(domain, dev);
78 }
79 
80 static 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 
105 static 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);
114 }
115 
116 static int rockchip_drm_bind(struct device *dev)
117 {
118 	struct drm_device *drm_dev;
119 	struct rockchip_drm_private *private;
120 	int ret;
121 
122 	drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
123 	if (IS_ERR(drm_dev))
124 		return PTR_ERR(drm_dev);
125 
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;
131 		goto err_free;
132 	}
133 
134 	drm_dev->dev_private = private;
135 
136 	INIT_LIST_HEAD(&private->psr_list);
137 	spin_lock_init(&private->psr_list_lock);
138 
139 	ret = rockchip_drm_init_iommu(drm_dev);
140 	if (ret)
141 		goto err_free;
142 
143 	drm_mode_config_init(drm_dev);
144 
145 	rockchip_drm_mode_config_init(drm_dev);
146 
147 	/* Try to bind all sub drivers. */
148 	ret = component_bind_all(dev, drm_dev);
149 	if (ret)
150 		goto err_mode_config_cleanup;
151 
152 	ret = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
153 	if (ret)
154 		goto err_unbind_all;
155 
156 	drm_mode_config_reset(drm_dev);
157 
158 	/*
159 	 * enable drm irq mode.
160 	 * - with irq_enabled = true, we can use the vblank feature.
161 	 */
162 	drm_dev->irq_enabled = true;
163 
164 	ret = rockchip_drm_fbdev_init(drm_dev);
165 	if (ret)
166 		goto err_unbind_all;
167 
168 	/* init kms poll for handling hpd */
169 	drm_kms_helper_poll_init(drm_dev);
170 
171 	ret = drm_dev_register(drm_dev, 0);
172 	if (ret)
173 		goto err_kms_helper_poll_fini;
174 
175 	return 0;
176 err_kms_helper_poll_fini:
177 	drm_kms_helper_poll_fini(drm_dev);
178 	rockchip_drm_fbdev_fini(drm_dev);
179 err_unbind_all:
180 	component_unbind_all(dev, drm_dev);
181 err_mode_config_cleanup:
182 	drm_mode_config_cleanup(drm_dev);
183 	rockchip_iommu_cleanup(drm_dev);
184 err_free:
185 	drm_dev->dev_private = NULL;
186 	dev_set_drvdata(dev, NULL);
187 	drm_dev_unref(drm_dev);
188 	return ret;
189 }
190 
191 static void rockchip_drm_unbind(struct device *dev)
192 {
193 	struct drm_device *drm_dev = dev_get_drvdata(dev);
194 
195 	drm_dev_unregister(drm_dev);
196 
197 	rockchip_drm_fbdev_fini(drm_dev);
198 	drm_kms_helper_poll_fini(drm_dev);
199 
200 	drm_atomic_helper_shutdown(drm_dev);
201 	component_unbind_all(dev, drm_dev);
202 	drm_mode_config_cleanup(drm_dev);
203 	rockchip_iommu_cleanup(drm_dev);
204 
205 	drm_dev->dev_private = NULL;
206 	dev_set_drvdata(dev, NULL);
207 	drm_dev_unref(drm_dev);
208 }
209 
210 static const struct file_operations rockchip_drm_driver_fops = {
211 	.owner = THIS_MODULE,
212 	.open = drm_open,
213 	.mmap = rockchip_gem_mmap,
214 	.poll = drm_poll,
215 	.read = drm_read,
216 	.unlocked_ioctl = drm_ioctl,
217 	.compat_ioctl = drm_compat_ioctl,
218 	.release = drm_release,
219 };
220 
221 static struct drm_driver rockchip_drm_driver = {
222 	.driver_features	= DRIVER_MODESET | DRIVER_GEM |
223 				  DRIVER_PRIME | DRIVER_ATOMIC,
224 	.lastclose		= drm_fb_helper_lastclose,
225 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
226 	.gem_free_object_unlocked = rockchip_gem_free_object,
227 	.dumb_create		= rockchip_gem_dumb_create,
228 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
229 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
230 	.gem_prime_import	= drm_gem_prime_import,
231 	.gem_prime_export	= drm_gem_prime_export,
232 	.gem_prime_get_sg_table	= rockchip_gem_prime_get_sg_table,
233 	.gem_prime_import_sg_table	= rockchip_gem_prime_import_sg_table,
234 	.gem_prime_vmap		= rockchip_gem_prime_vmap,
235 	.gem_prime_vunmap	= rockchip_gem_prime_vunmap,
236 	.gem_prime_mmap		= rockchip_gem_mmap_buf,
237 	.fops			= &rockchip_drm_driver_fops,
238 	.name	= DRIVER_NAME,
239 	.desc	= DRIVER_DESC,
240 	.date	= DRIVER_DATE,
241 	.major	= DRIVER_MAJOR,
242 	.minor	= DRIVER_MINOR,
243 };
244 
245 #ifdef CONFIG_PM_SLEEP
246 static void rockchip_drm_fb_suspend(struct drm_device *drm)
247 {
248 	struct rockchip_drm_private *priv = drm->dev_private;
249 
250 	console_lock();
251 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
252 	console_unlock();
253 }
254 
255 static void rockchip_drm_fb_resume(struct drm_device *drm)
256 {
257 	struct rockchip_drm_private *priv = drm->dev_private;
258 
259 	console_lock();
260 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
261 	console_unlock();
262 }
263 
264 static int rockchip_drm_sys_suspend(struct device *dev)
265 {
266 	struct drm_device *drm = dev_get_drvdata(dev);
267 	struct rockchip_drm_private *priv;
268 
269 	if (!drm)
270 		return 0;
271 
272 	drm_kms_helper_poll_disable(drm);
273 	rockchip_drm_fb_suspend(drm);
274 
275 	priv = drm->dev_private;
276 	priv->state = drm_atomic_helper_suspend(drm);
277 	if (IS_ERR(priv->state)) {
278 		rockchip_drm_fb_resume(drm);
279 		drm_kms_helper_poll_enable(drm);
280 		return PTR_ERR(priv->state);
281 	}
282 
283 	return 0;
284 }
285 
286 static int rockchip_drm_sys_resume(struct device *dev)
287 {
288 	struct drm_device *drm = dev_get_drvdata(dev);
289 	struct rockchip_drm_private *priv;
290 
291 	if (!drm)
292 		return 0;
293 
294 	priv = drm->dev_private;
295 	drm_atomic_helper_resume(drm, priv->state);
296 	rockchip_drm_fb_resume(drm);
297 	drm_kms_helper_poll_enable(drm);
298 
299 	return 0;
300 }
301 #endif
302 
303 static const struct dev_pm_ops rockchip_drm_pm_ops = {
304 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
305 				rockchip_drm_sys_resume)
306 };
307 
308 #define MAX_ROCKCHIP_SUB_DRIVERS 16
309 static struct platform_driver *rockchip_sub_drivers[MAX_ROCKCHIP_SUB_DRIVERS];
310 static int num_rockchip_sub_drivers;
311 
312 static int compare_dev(struct device *dev, void *data)
313 {
314 	return dev == (struct device *)data;
315 }
316 
317 static void rockchip_drm_match_remove(struct device *dev)
318 {
319 	struct device_link *link;
320 
321 	list_for_each_entry(link, &dev->links.consumers, s_node)
322 		device_link_del(link);
323 }
324 
325 static struct component_match *rockchip_drm_match_add(struct device *dev)
326 {
327 	struct component_match *match = NULL;
328 	int i;
329 
330 	for (i = 0; i < num_rockchip_sub_drivers; i++) {
331 		struct platform_driver *drv = rockchip_sub_drivers[i];
332 		struct device *p = NULL, *d;
333 
334 		do {
335 			d = bus_find_device(&platform_bus_type, p, &drv->driver,
336 					    (void *)platform_bus_type.match);
337 			put_device(p);
338 			p = d;
339 
340 			if (!d)
341 				break;
342 
343 			device_link_add(dev, d, DL_FLAG_STATELESS);
344 			component_match_add(dev, &match, compare_dev, d);
345 		} while (true);
346 	}
347 
348 	if (IS_ERR(match))
349 		rockchip_drm_match_remove(dev);
350 
351 	return match ?: ERR_PTR(-ENODEV);
352 }
353 
354 static const struct component_master_ops rockchip_drm_ops = {
355 	.bind = rockchip_drm_bind,
356 	.unbind = rockchip_drm_unbind,
357 };
358 
359 static int rockchip_drm_platform_of_probe(struct device *dev)
360 {
361 	struct device_node *np = dev->of_node;
362 	struct device_node *port;
363 	bool found = false;
364 	int i;
365 
366 	if (!np)
367 		return -ENODEV;
368 
369 	for (i = 0;; i++) {
370 		struct device_node *iommu;
371 
372 		port = of_parse_phandle(np, "ports", i);
373 		if (!port)
374 			break;
375 
376 		if (!of_device_is_available(port->parent)) {
377 			of_node_put(port);
378 			continue;
379 		}
380 
381 		iommu = of_parse_phandle(port->parent, "iommus", 0);
382 		if (!iommu || !of_device_is_available(iommu->parent)) {
383 			DRM_DEV_DEBUG(dev,
384 				      "no iommu attached for %pOF, using non-iommu buffers\n",
385 				      port->parent);
386 			/*
387 			 * if there is a crtc not support iommu, force set all
388 			 * crtc use non-iommu buffer.
389 			 */
390 			is_support_iommu = false;
391 		}
392 
393 		found = true;
394 
395 		of_node_put(iommu);
396 		of_node_put(port);
397 	}
398 
399 	if (i == 0) {
400 		DRM_DEV_ERROR(dev, "missing 'ports' property\n");
401 		return -ENODEV;
402 	}
403 
404 	if (!found) {
405 		DRM_DEV_ERROR(dev,
406 			      "No available vop found for display-subsystem.\n");
407 		return -ENODEV;
408 	}
409 
410 	return 0;
411 }
412 
413 static int rockchip_drm_platform_probe(struct platform_device *pdev)
414 {
415 	struct device *dev = &pdev->dev;
416 	struct component_match *match = NULL;
417 	int ret;
418 
419 	ret = rockchip_drm_platform_of_probe(dev);
420 	if (ret)
421 		return ret;
422 
423 	match = rockchip_drm_match_add(dev);
424 	if (IS_ERR(match))
425 		return PTR_ERR(match);
426 
427 	ret = component_master_add_with_match(dev, &rockchip_drm_ops, match);
428 	if (ret < 0) {
429 		rockchip_drm_match_remove(dev);
430 		return ret;
431 	}
432 
433 	return 0;
434 }
435 
436 static int rockchip_drm_platform_remove(struct platform_device *pdev)
437 {
438 	component_master_del(&pdev->dev, &rockchip_drm_ops);
439 
440 	rockchip_drm_match_remove(&pdev->dev);
441 
442 	return 0;
443 }
444 
445 static const struct of_device_id rockchip_drm_dt_ids[] = {
446 	{ .compatible = "rockchip,display-subsystem", },
447 	{ /* sentinel */ },
448 };
449 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
450 
451 static struct platform_driver rockchip_drm_platform_driver = {
452 	.probe = rockchip_drm_platform_probe,
453 	.remove = rockchip_drm_platform_remove,
454 	.driver = {
455 		.name = "rockchip-drm",
456 		.of_match_table = rockchip_drm_dt_ids,
457 		.pm = &rockchip_drm_pm_ops,
458 	},
459 };
460 
461 #define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
462 	if (IS_ENABLED(cond) && \
463 	    !WARN_ON(num_rockchip_sub_drivers >= MAX_ROCKCHIP_SUB_DRIVERS)) \
464 		rockchip_sub_drivers[num_rockchip_sub_drivers++] = &drv; \
465 }
466 
467 static int __init rockchip_drm_init(void)
468 {
469 	int ret;
470 
471 	num_rockchip_sub_drivers = 0;
472 	ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_DRM_ROCKCHIP);
473 	ADD_ROCKCHIP_SUB_DRIVER(rockchip_lvds_driver,
474 				CONFIG_ROCKCHIP_LVDS);
475 	ADD_ROCKCHIP_SUB_DRIVER(rockchip_dp_driver,
476 				CONFIG_ROCKCHIP_ANALOGIX_DP);
477 	ADD_ROCKCHIP_SUB_DRIVER(cdn_dp_driver, CONFIG_ROCKCHIP_CDN_DP);
478 	ADD_ROCKCHIP_SUB_DRIVER(dw_hdmi_rockchip_pltfm_driver,
479 				CONFIG_ROCKCHIP_DW_HDMI);
480 	ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi_driver,
481 				CONFIG_ROCKCHIP_DW_MIPI_DSI);
482 	ADD_ROCKCHIP_SUB_DRIVER(inno_hdmi_driver, CONFIG_ROCKCHIP_INNO_HDMI);
483 
484 	ret = platform_register_drivers(rockchip_sub_drivers,
485 					num_rockchip_sub_drivers);
486 	if (ret)
487 		return ret;
488 
489 	ret = platform_driver_register(&rockchip_drm_platform_driver);
490 	if (ret)
491 		goto err_unreg_drivers;
492 
493 	return 0;
494 
495 err_unreg_drivers:
496 	platform_unregister_drivers(rockchip_sub_drivers,
497 				    num_rockchip_sub_drivers);
498 	return ret;
499 }
500 
501 static void __exit rockchip_drm_fini(void)
502 {
503 	platform_driver_unregister(&rockchip_drm_platform_driver);
504 
505 	platform_unregister_drivers(rockchip_sub_drivers,
506 				    num_rockchip_sub_drivers);
507 }
508 
509 module_init(rockchip_drm_init);
510 module_exit(rockchip_drm_fini);
511 
512 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
513 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
514 MODULE_LICENSE("GPL v2");
515