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 		dev_err(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 int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
81 				 const struct rockchip_crtc_funcs *crtc_funcs)
82 {
83 	int pipe = drm_crtc_index(crtc);
84 	struct rockchip_drm_private *priv = crtc->dev->dev_private;
85 
86 	if (pipe >= ROCKCHIP_MAX_CRTC)
87 		return -EINVAL;
88 
89 	priv->crtc_funcs[pipe] = crtc_funcs;
90 
91 	return 0;
92 }
93 
94 void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
95 {
96 	int pipe = drm_crtc_index(crtc);
97 	struct rockchip_drm_private *priv = crtc->dev->dev_private;
98 
99 	if (pipe >= ROCKCHIP_MAX_CRTC)
100 		return;
101 
102 	priv->crtc_funcs[pipe] = NULL;
103 }
104 
105 static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
106 					   unsigned int pipe)
107 {
108 	struct rockchip_drm_private *priv = dev->dev_private;
109 	struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
110 
111 	if (crtc && priv->crtc_funcs[pipe] &&
112 	    priv->crtc_funcs[pipe]->enable_vblank)
113 		return priv->crtc_funcs[pipe]->enable_vblank(crtc);
114 
115 	return 0;
116 }
117 
118 static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
119 					     unsigned int pipe)
120 {
121 	struct rockchip_drm_private *priv = dev->dev_private;
122 	struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
123 
124 	if (crtc && priv->crtc_funcs[pipe] &&
125 	    priv->crtc_funcs[pipe]->enable_vblank)
126 		priv->crtc_funcs[pipe]->disable_vblank(crtc);
127 }
128 
129 static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
130 {
131 	struct rockchip_drm_private *private = drm_dev->dev_private;
132 	struct iommu_domain_geometry *geometry;
133 	u64 start, end;
134 
135 	if (!is_support_iommu)
136 		return 0;
137 
138 	private->domain = iommu_domain_alloc(&platform_bus_type);
139 	if (!private->domain)
140 		return -ENOMEM;
141 
142 	geometry = &private->domain->geometry;
143 	start = geometry->aperture_start;
144 	end = geometry->aperture_end;
145 
146 	DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
147 		  start, end);
148 	drm_mm_init(&private->mm, start, end - start + 1);
149 	mutex_init(&private->mm_lock);
150 
151 	return 0;
152 }
153 
154 static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
155 {
156 	struct rockchip_drm_private *private = drm_dev->dev_private;
157 
158 	if (!is_support_iommu)
159 		return;
160 
161 	drm_mm_takedown(&private->mm);
162 	iommu_domain_free(private->domain);
163 }
164 
165 static int rockchip_drm_bind(struct device *dev)
166 {
167 	struct drm_device *drm_dev;
168 	struct rockchip_drm_private *private;
169 	int ret;
170 
171 	drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
172 	if (IS_ERR(drm_dev))
173 		return PTR_ERR(drm_dev);
174 
175 	dev_set_drvdata(dev, drm_dev);
176 
177 	private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
178 	if (!private) {
179 		ret = -ENOMEM;
180 		goto err_free;
181 	}
182 
183 	drm_dev->dev_private = private;
184 
185 	INIT_LIST_HEAD(&private->psr_list);
186 	spin_lock_init(&private->psr_list_lock);
187 
188 	drm_mode_config_init(drm_dev);
189 
190 	rockchip_drm_mode_config_init(drm_dev);
191 
192 	ret = rockchip_drm_init_iommu(drm_dev);
193 	if (ret)
194 		goto err_config_cleanup;
195 
196 	/* Try to bind all sub drivers. */
197 	ret = component_bind_all(dev, drm_dev);
198 	if (ret)
199 		goto err_iommu_cleanup;
200 
201 	/* init kms poll for handling hpd */
202 	drm_kms_helper_poll_init(drm_dev);
203 
204 	/*
205 	 * enable drm irq mode.
206 	 * - with irq_enabled = true, we can use the vblank feature.
207 	 */
208 	drm_dev->irq_enabled = true;
209 
210 	ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
211 	if (ret)
212 		goto err_kms_helper_poll_fini;
213 
214 	drm_mode_config_reset(drm_dev);
215 
216 	ret = rockchip_drm_fbdev_init(drm_dev);
217 	if (ret)
218 		goto err_vblank_cleanup;
219 
220 	ret = drm_dev_register(drm_dev, 0);
221 	if (ret)
222 		goto err_fbdev_fini;
223 
224 	return 0;
225 err_fbdev_fini:
226 	rockchip_drm_fbdev_fini(drm_dev);
227 err_vblank_cleanup:
228 	drm_vblank_cleanup(drm_dev);
229 err_kms_helper_poll_fini:
230 	drm_kms_helper_poll_fini(drm_dev);
231 	component_unbind_all(dev, drm_dev);
232 err_iommu_cleanup:
233 	rockchip_iommu_cleanup(drm_dev);
234 err_config_cleanup:
235 	drm_mode_config_cleanup(drm_dev);
236 	drm_dev->dev_private = NULL;
237 err_free:
238 	drm_dev_unref(drm_dev);
239 	return ret;
240 }
241 
242 static void rockchip_drm_unbind(struct device *dev)
243 {
244 	struct drm_device *drm_dev = dev_get_drvdata(dev);
245 
246 	rockchip_drm_fbdev_fini(drm_dev);
247 	drm_vblank_cleanup(drm_dev);
248 	drm_kms_helper_poll_fini(drm_dev);
249 	component_unbind_all(dev, drm_dev);
250 	rockchip_iommu_cleanup(drm_dev);
251 	drm_mode_config_cleanup(drm_dev);
252 	drm_dev->dev_private = NULL;
253 	drm_dev_unregister(drm_dev);
254 	drm_dev_unref(drm_dev);
255 	dev_set_drvdata(dev, NULL);
256 }
257 
258 static void rockchip_drm_lastclose(struct drm_device *dev)
259 {
260 	struct rockchip_drm_private *priv = dev->dev_private;
261 
262 	drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
263 }
264 
265 static const struct file_operations rockchip_drm_driver_fops = {
266 	.owner = THIS_MODULE,
267 	.open = drm_open,
268 	.mmap = rockchip_gem_mmap,
269 	.poll = drm_poll,
270 	.read = drm_read,
271 	.unlocked_ioctl = drm_ioctl,
272 	.compat_ioctl = drm_compat_ioctl,
273 	.release = drm_release,
274 };
275 
276 static struct drm_driver rockchip_drm_driver = {
277 	.driver_features	= DRIVER_MODESET | DRIVER_GEM |
278 				  DRIVER_PRIME | DRIVER_ATOMIC,
279 	.lastclose		= rockchip_drm_lastclose,
280 	.get_vblank_counter	= drm_vblank_no_hw_counter,
281 	.enable_vblank		= rockchip_drm_crtc_enable_vblank,
282 	.disable_vblank		= rockchip_drm_crtc_disable_vblank,
283 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
284 	.gem_free_object_unlocked = rockchip_gem_free_object,
285 	.dumb_create		= rockchip_gem_dumb_create,
286 	.dumb_map_offset	= rockchip_gem_dumb_map_offset,
287 	.dumb_destroy		= drm_gem_dumb_destroy,
288 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
289 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
290 	.gem_prime_import	= drm_gem_prime_import,
291 	.gem_prime_export	= drm_gem_prime_export,
292 	.gem_prime_get_sg_table	= rockchip_gem_prime_get_sg_table,
293 	.gem_prime_vmap		= rockchip_gem_prime_vmap,
294 	.gem_prime_vunmap	= rockchip_gem_prime_vunmap,
295 	.gem_prime_mmap		= rockchip_gem_mmap_buf,
296 	.fops			= &rockchip_drm_driver_fops,
297 	.name	= DRIVER_NAME,
298 	.desc	= DRIVER_DESC,
299 	.date	= DRIVER_DATE,
300 	.major	= DRIVER_MAJOR,
301 	.minor	= DRIVER_MINOR,
302 };
303 
304 #ifdef CONFIG_PM_SLEEP
305 static void rockchip_drm_fb_suspend(struct drm_device *drm)
306 {
307 	struct rockchip_drm_private *priv = drm->dev_private;
308 
309 	console_lock();
310 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
311 	console_unlock();
312 }
313 
314 static void rockchip_drm_fb_resume(struct drm_device *drm)
315 {
316 	struct rockchip_drm_private *priv = drm->dev_private;
317 
318 	console_lock();
319 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
320 	console_unlock();
321 }
322 
323 static int rockchip_drm_sys_suspend(struct device *dev)
324 {
325 	struct drm_device *drm = dev_get_drvdata(dev);
326 	struct rockchip_drm_private *priv = drm->dev_private;
327 
328 	drm_kms_helper_poll_disable(drm);
329 	rockchip_drm_fb_suspend(drm);
330 
331 	priv->state = drm_atomic_helper_suspend(drm);
332 	if (IS_ERR(priv->state)) {
333 		rockchip_drm_fb_resume(drm);
334 		drm_kms_helper_poll_enable(drm);
335 		return PTR_ERR(priv->state);
336 	}
337 
338 	return 0;
339 }
340 
341 static int rockchip_drm_sys_resume(struct device *dev)
342 {
343 	struct drm_device *drm = dev_get_drvdata(dev);
344 	struct rockchip_drm_private *priv = drm->dev_private;
345 
346 	drm_atomic_helper_resume(drm, priv->state);
347 	rockchip_drm_fb_resume(drm);
348 	drm_kms_helper_poll_enable(drm);
349 
350 	return 0;
351 }
352 #endif
353 
354 static const struct dev_pm_ops rockchip_drm_pm_ops = {
355 	SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
356 				rockchip_drm_sys_resume)
357 };
358 
359 static int compare_of(struct device *dev, void *data)
360 {
361 	struct device_node *np = data;
362 
363 	return dev->of_node == np;
364 }
365 
366 static void rockchip_add_endpoints(struct device *dev,
367 				   struct component_match **match,
368 				   struct device_node *port)
369 {
370 	struct device_node *ep, *remote;
371 
372 	for_each_child_of_node(port, ep) {
373 		remote = of_graph_get_remote_port_parent(ep);
374 		if (!remote || !of_device_is_available(remote)) {
375 			of_node_put(remote);
376 			continue;
377 		} else if (!of_device_is_available(remote->parent)) {
378 			dev_warn(dev, "parent device of %s is not available\n",
379 				 remote->full_name);
380 			of_node_put(remote);
381 			continue;
382 		}
383 
384 		drm_of_component_match_add(dev, match, compare_of, remote);
385 		of_node_put(remote);
386 	}
387 }
388 
389 static const struct component_master_ops rockchip_drm_ops = {
390 	.bind = rockchip_drm_bind,
391 	.unbind = rockchip_drm_unbind,
392 };
393 
394 static int rockchip_drm_platform_probe(struct platform_device *pdev)
395 {
396 	struct device *dev = &pdev->dev;
397 	struct component_match *match = NULL;
398 	struct device_node *np = dev->of_node;
399 	struct device_node *port;
400 	int i;
401 
402 	if (!np)
403 		return -ENODEV;
404 	/*
405 	 * Bind the crtc ports first, so that
406 	 * drm_of_find_possible_crtcs called from encoder .bind callbacks
407 	 * works as expected.
408 	 */
409 	for (i = 0;; i++) {
410 		struct device_node *iommu;
411 
412 		port = of_parse_phandle(np, "ports", i);
413 		if (!port)
414 			break;
415 
416 		if (!of_device_is_available(port->parent)) {
417 			of_node_put(port);
418 			continue;
419 		}
420 
421 		iommu = of_parse_phandle(port->parent, "iommus", 0);
422 		if (!iommu || !of_device_is_available(iommu->parent)) {
423 			dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
424 				port->parent->full_name);
425 			/*
426 			 * if there is a crtc not support iommu, force set all
427 			 * crtc use non-iommu buffer.
428 			 */
429 			is_support_iommu = false;
430 		}
431 
432 		of_node_put(iommu);
433 		drm_of_component_match_add(dev, &match, compare_of,
434 					   port->parent);
435 		of_node_put(port);
436 	}
437 
438 	if (i == 0) {
439 		dev_err(dev, "missing 'ports' property\n");
440 		return -ENODEV;
441 	}
442 
443 	if (!match) {
444 		dev_err(dev, "No available vop found for display-subsystem.\n");
445 		return -ENODEV;
446 	}
447 	/*
448 	 * For each bound crtc, bind the encoders attached to its
449 	 * remote endpoint.
450 	 */
451 	for (i = 0;; i++) {
452 		port = of_parse_phandle(np, "ports", i);
453 		if (!port)
454 			break;
455 
456 		if (!of_device_is_available(port->parent)) {
457 			of_node_put(port);
458 			continue;
459 		}
460 
461 		rockchip_add_endpoints(dev, &match, port);
462 		of_node_put(port);
463 	}
464 
465 	return component_master_add_with_match(dev, &rockchip_drm_ops, match);
466 }
467 
468 static int rockchip_drm_platform_remove(struct platform_device *pdev)
469 {
470 	component_master_del(&pdev->dev, &rockchip_drm_ops);
471 
472 	return 0;
473 }
474 
475 static const struct of_device_id rockchip_drm_dt_ids[] = {
476 	{ .compatible = "rockchip,display-subsystem", },
477 	{ /* sentinel */ },
478 };
479 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
480 
481 static struct platform_driver rockchip_drm_platform_driver = {
482 	.probe = rockchip_drm_platform_probe,
483 	.remove = rockchip_drm_platform_remove,
484 	.driver = {
485 		.name = "rockchip-drm",
486 		.of_match_table = rockchip_drm_dt_ids,
487 		.pm = &rockchip_drm_pm_ops,
488 	},
489 };
490 
491 module_platform_driver(rockchip_drm_platform_driver);
492 
493 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
494 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
495 MODULE_LICENSE("GPL v2");
496