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 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 	drm_mode_config_init(drm_dev);
140 
141 	rockchip_drm_mode_config_init(drm_dev);
142 
143 	ret = rockchip_drm_init_iommu(drm_dev);
144 	if (ret)
145 		goto err_config_cleanup;
146 
147 	/* Try to bind all sub drivers. */
148 	ret = component_bind_all(dev, drm_dev);
149 	if (ret)
150 		goto err_iommu_cleanup;
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 
165 	drm_mode_config_reset(drm_dev);
166 
167 	ret = rockchip_drm_fbdev_init(drm_dev);
168 	if (ret)
169 		goto err_vblank_cleanup;
170 
171 	ret = drm_dev_register(drm_dev, 0);
172 	if (ret)
173 		goto err_fbdev_fini;
174 
175 	return 0;
176 err_fbdev_fini:
177 	rockchip_drm_fbdev_fini(drm_dev);
178 err_vblank_cleanup:
179 	drm_vblank_cleanup(drm_dev);
180 err_kms_helper_poll_fini:
181 	drm_kms_helper_poll_fini(drm_dev);
182 	component_unbind_all(dev, drm_dev);
183 err_iommu_cleanup:
184 	rockchip_iommu_cleanup(drm_dev);
185 err_config_cleanup:
186 	drm_mode_config_cleanup(drm_dev);
187 	drm_dev->dev_private = NULL;
188 err_free:
189 	drm_dev_unref(drm_dev);
190 	return ret;
191 }
192 
193 static void rockchip_drm_unbind(struct device *dev)
194 {
195 	struct drm_device *drm_dev = dev_get_drvdata(dev);
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);
201 	rockchip_iommu_cleanup(drm_dev);
202 	drm_mode_config_cleanup(drm_dev);
203 	drm_dev->dev_private = NULL;
204 	drm_dev_unregister(drm_dev);
205 	drm_dev_unref(drm_dev);
206 	dev_set_drvdata(dev, NULL);
207 }
208 
209 static void rockchip_drm_lastclose(struct drm_device *dev)
210 {
211 	struct rockchip_drm_private *priv = dev->dev_private;
212 
213 	drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
214 }
215 
216 static 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,
223 	.compat_ioctl = drm_compat_ioctl,
224 	.release = drm_release,
225 };
226 
227 static struct drm_driver rockchip_drm_driver = {
228 	.driver_features	= DRIVER_MODESET | DRIVER_GEM |
229 				  DRIVER_PRIME | DRIVER_ATOMIC,
230 	.lastclose		= rockchip_drm_lastclose,
231 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
232 	.gem_free_object_unlocked = rockchip_gem_free_object,
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
253 static void rockchip_drm_fb_suspend(struct drm_device *drm)
254 {
255 	struct rockchip_drm_private *priv = drm->dev_private;
256 
257 	console_lock();
258 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
259 	console_unlock();
260 }
261 
262 static void rockchip_drm_fb_resume(struct drm_device *drm)
263 {
264 	struct rockchip_drm_private *priv = drm->dev_private;
265 
266 	console_lock();
267 	drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
268 	console_unlock();
269 }
270 
271 static 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);
284 	}
285 
286 	return 0;
287 }
288 
289 static int rockchip_drm_sys_resume(struct device *dev)
290 {
291 	struct drm_device *drm = dev_get_drvdata(dev);
292 	struct rockchip_drm_private *priv = drm->dev_private;
293 
294 	drm_atomic_helper_resume(drm, priv->state);
295 	rockchip_drm_fb_resume(drm);
296 	drm_kms_helper_poll_enable(drm);
297 
298 	return 0;
299 }
300 #endif
301 
302 static 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 
307 static int compare_of(struct device *dev, void *data)
308 {
309 	struct device_node *np = data;
310 
311 	return dev->of_node == np;
312 }
313 
314 static void rockchip_add_endpoints(struct device *dev,
315 				   struct component_match **match,
316 				   struct device_node *port)
317 {
318 	struct device_node *ep, *remote;
319 
320 	for_each_child_of_node(port, ep) {
321 		remote = of_graph_get_remote_port_parent(ep);
322 		if (!remote || !of_device_is_available(remote)) {
323 			of_node_put(remote);
324 			continue;
325 		} else if (!of_device_is_available(remote->parent)) {
326 			dev_warn(dev, "parent device of %s is not available\n",
327 				 remote->full_name);
328 			of_node_put(remote);
329 			continue;
330 		}
331 
332 		drm_of_component_match_add(dev, match, compare_of, remote);
333 		of_node_put(remote);
334 	}
335 }
336 
337 static const struct component_master_ops rockchip_drm_ops = {
338 	.bind = rockchip_drm_bind,
339 	.unbind = rockchip_drm_unbind,
340 };
341 
342 static int rockchip_drm_platform_probe(struct platform_device *pdev)
343 {
344 	struct device *dev = &pdev->dev;
345 	struct component_match *match = NULL;
346 	struct device_node *np = dev->of_node;
347 	struct device_node *port;
348 	int i;
349 
350 	if (!np)
351 		return -ENODEV;
352 	/*
353 	 * Bind the crtc ports first, so that
354 	 * drm_of_find_possible_crtcs called from encoder .bind callbacks
355 	 * works as expected.
356 	 */
357 	for (i = 0;; i++) {
358 		struct device_node *iommu;
359 
360 		port = of_parse_phandle(np, "ports", i);
361 		if (!port)
362 			break;
363 
364 		if (!of_device_is_available(port->parent)) {
365 			of_node_put(port);
366 			continue;
367 		}
368 
369 		iommu = of_parse_phandle(port->parent, "iommus", 0);
370 		if (!iommu || !of_device_is_available(iommu->parent)) {
371 			dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
372 				port->parent->full_name);
373 			/*
374 			 * if there is a crtc not support iommu, force set all
375 			 * crtc use non-iommu buffer.
376 			 */
377 			is_support_iommu = false;
378 		}
379 
380 		of_node_put(iommu);
381 		drm_of_component_match_add(dev, &match, compare_of,
382 					   port->parent);
383 		of_node_put(port);
384 	}
385 
386 	if (i == 0) {
387 		dev_err(dev, "missing 'ports' property\n");
388 		return -ENODEV;
389 	}
390 
391 	if (!match) {
392 		dev_err(dev, "No available vop found for display-subsystem.\n");
393 		return -ENODEV;
394 	}
395 	/*
396 	 * For each bound crtc, bind the encoders attached to its
397 	 * remote endpoint.
398 	 */
399 	for (i = 0;; i++) {
400 		port = of_parse_phandle(np, "ports", i);
401 		if (!port)
402 			break;
403 
404 		if (!of_device_is_available(port->parent)) {
405 			of_node_put(port);
406 			continue;
407 		}
408 
409 		rockchip_add_endpoints(dev, &match, port);
410 		of_node_put(port);
411 	}
412 
413 	return component_master_add_with_match(dev, &rockchip_drm_ops, match);
414 }
415 
416 static int rockchip_drm_platform_remove(struct platform_device *pdev)
417 {
418 	component_master_del(&pdev->dev, &rockchip_drm_ops);
419 
420 	return 0;
421 }
422 
423 static const struct of_device_id rockchip_drm_dt_ids[] = {
424 	{ .compatible = "rockchip,display-subsystem", },
425 	{ /* sentinel */ },
426 };
427 MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
428 
429 static struct platform_driver rockchip_drm_platform_driver = {
430 	.probe = rockchip_drm_platform_probe,
431 	.remove = rockchip_drm_platform_remove,
432 	.driver = {
433 		.name = "rockchip-drm",
434 		.of_match_table = rockchip_drm_dt_ids,
435 		.pm = &rockchip_drm_pm_ops,
436 	},
437 };
438 
439 module_platform_driver(rockchip_drm_platform_driver);
440 
441 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
442 MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
443 MODULE_LICENSE("GPL v2");
444