xref: /linux/drivers/gpu/drm/exynos/exynos_drm_drv.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Authors:
5  *	Inki Dae <inki.dae@samsung.com>
6  *	Joonyoung Shim <jy0922.shim@samsung.com>
7  *	Seung-Woo Kim <sw0312.kim@samsung.com>
8  */
9 
10 #include <linux/pm_runtime.h>
11 #include <drm/drmP.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_probe_helper.h>
16 
17 #include <linux/component.h>
18 
19 #include <drm/exynos_drm.h>
20 
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fbdev.h"
23 #include "exynos_drm_fb.h"
24 #include "exynos_drm_gem.h"
25 #include "exynos_drm_plane.h"
26 #include "exynos_drm_ipp.h"
27 #include "exynos_drm_vidi.h"
28 #include "exynos_drm_g2d.h"
29 
30 #define DRIVER_NAME	"exynos"
31 #define DRIVER_DESC	"Samsung SoC DRM"
32 #define DRIVER_DATE	"20180330"
33 
34 /*
35  * Interface history:
36  *
37  * 1.0 - Original version
38  * 1.1 - Upgrade IPP driver to version 2.0
39  */
40 #define DRIVER_MAJOR	1
41 #define DRIVER_MINOR	1
42 
43 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
44 {
45 	struct drm_exynos_file_private *file_priv;
46 	int ret;
47 
48 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
49 	if (!file_priv)
50 		return -ENOMEM;
51 
52 	file->driver_priv = file_priv;
53 	ret = g2d_open(dev, file);
54 	if (ret)
55 		goto err_file_priv_free;
56 
57 	return ret;
58 
59 err_file_priv_free:
60 	kfree(file_priv);
61 	file->driver_priv = NULL;
62 	return ret;
63 }
64 
65 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
66 {
67 	g2d_close(dev, file);
68 	kfree(file->driver_priv);
69 	file->driver_priv = NULL;
70 }
71 
72 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
73 	.fault = exynos_drm_gem_fault,
74 	.open = drm_gem_vm_open,
75 	.close = drm_gem_vm_close,
76 };
77 
78 static const struct drm_ioctl_desc exynos_ioctls[] = {
79 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
80 			DRM_AUTH | DRM_RENDER_ALLOW),
81 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP, exynos_drm_gem_map_ioctl,
82 			DRM_AUTH | DRM_RENDER_ALLOW),
83 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET, exynos_drm_gem_get_ioctl,
84 			DRM_RENDER_ALLOW),
85 	DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION, vidi_connection_ioctl,
86 			DRM_AUTH),
87 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER, exynos_g2d_get_ver_ioctl,
88 			DRM_AUTH | DRM_RENDER_ALLOW),
89 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST, exynos_g2d_set_cmdlist_ioctl,
90 			DRM_AUTH | DRM_RENDER_ALLOW),
91 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC, exynos_g2d_exec_ioctl,
92 			DRM_AUTH | DRM_RENDER_ALLOW),
93 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_RESOURCES,
94 			exynos_drm_ipp_get_res_ioctl,
95 			DRM_AUTH | DRM_RENDER_ALLOW),
96 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_CAPS, exynos_drm_ipp_get_caps_ioctl,
97 			DRM_AUTH | DRM_RENDER_ALLOW),
98 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_LIMITS,
99 			exynos_drm_ipp_get_limits_ioctl,
100 			DRM_AUTH | DRM_RENDER_ALLOW),
101 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_COMMIT, exynos_drm_ipp_commit_ioctl,
102 			DRM_AUTH | DRM_RENDER_ALLOW),
103 };
104 
105 static const struct file_operations exynos_drm_driver_fops = {
106 	.owner		= THIS_MODULE,
107 	.open		= drm_open,
108 	.mmap		= exynos_drm_gem_mmap,
109 	.poll		= drm_poll,
110 	.read		= drm_read,
111 	.unlocked_ioctl	= drm_ioctl,
112 	.compat_ioctl = drm_compat_ioctl,
113 	.release	= drm_release,
114 };
115 
116 static struct drm_driver exynos_drm_driver = {
117 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME
118 				  | DRIVER_ATOMIC | DRIVER_RENDER,
119 	.open			= exynos_drm_open,
120 	.lastclose		= drm_fb_helper_lastclose,
121 	.postclose		= exynos_drm_postclose,
122 	.gem_free_object_unlocked = exynos_drm_gem_free_object,
123 	.gem_vm_ops		= &exynos_drm_gem_vm_ops,
124 	.dumb_create		= exynos_drm_gem_dumb_create,
125 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
126 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
127 	.gem_prime_export	= drm_gem_prime_export,
128 	.gem_prime_import	= exynos_drm_gem_prime_import,
129 	.gem_prime_get_sg_table	= exynos_drm_gem_prime_get_sg_table,
130 	.gem_prime_import_sg_table	= exynos_drm_gem_prime_import_sg_table,
131 	.gem_prime_vmap		= exynos_drm_gem_prime_vmap,
132 	.gem_prime_vunmap	= exynos_drm_gem_prime_vunmap,
133 	.gem_prime_mmap		= exynos_drm_gem_prime_mmap,
134 	.ioctls			= exynos_ioctls,
135 	.num_ioctls		= ARRAY_SIZE(exynos_ioctls),
136 	.fops			= &exynos_drm_driver_fops,
137 	.name	= DRIVER_NAME,
138 	.desc	= DRIVER_DESC,
139 	.date	= DRIVER_DATE,
140 	.major	= DRIVER_MAJOR,
141 	.minor	= DRIVER_MINOR,
142 };
143 
144 static int exynos_drm_suspend(struct device *dev)
145 {
146 	struct drm_device *drm_dev = dev_get_drvdata(dev);
147 
148 	return  drm_mode_config_helper_suspend(drm_dev);
149 }
150 
151 static void exynos_drm_resume(struct device *dev)
152 {
153 	struct drm_device *drm_dev = dev_get_drvdata(dev);
154 
155 	drm_mode_config_helper_resume(drm_dev);
156 }
157 
158 static const struct dev_pm_ops exynos_drm_pm_ops = {
159 	.prepare = exynos_drm_suspend,
160 	.complete = exynos_drm_resume,
161 };
162 
163 /* forward declaration */
164 static struct platform_driver exynos_drm_platform_driver;
165 
166 struct exynos_drm_driver_info {
167 	struct platform_driver *driver;
168 	unsigned int flags;
169 };
170 
171 #define DRM_COMPONENT_DRIVER	BIT(0)	/* supports component framework */
172 #define DRM_VIRTUAL_DEVICE	BIT(1)	/* create virtual platform device */
173 #define DRM_FIMC_DEVICE		BIT(2)	/* devices shared with V4L2 subsystem */
174 
175 #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
176 
177 /*
178  * Connector drivers should not be placed before associated crtc drivers,
179  * because connector requires pipe number of its crtc during initialization.
180  */
181 static struct exynos_drm_driver_info exynos_drm_drivers[] = {
182 	{
183 		DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
184 		DRM_COMPONENT_DRIVER
185 	}, {
186 		DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
187 		DRM_COMPONENT_DRIVER
188 	}, {
189 		DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
190 		DRM_COMPONENT_DRIVER
191 	}, {
192 		DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
193 		DRM_COMPONENT_DRIVER
194 	}, {
195 		DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
196 		DRM_COMPONENT_DRIVER
197 	}, {
198 		DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
199 		DRM_COMPONENT_DRIVER
200 	}, {
201 		DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
202 		DRM_COMPONENT_DRIVER
203 	}, {
204 		DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
205 		DRM_COMPONENT_DRIVER
206 	}, {
207 		DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
208 		DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
209 	}, {
210 		DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
211 		DRM_COMPONENT_DRIVER
212 	}, {
213 		DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
214 		DRM_COMPONENT_DRIVER | DRM_FIMC_DEVICE,
215 	}, {
216 		DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
217 		DRM_COMPONENT_DRIVER
218 	}, {
219 		DRV_PTR(scaler_driver, CONFIG_DRM_EXYNOS_SCALER),
220 		DRM_COMPONENT_DRIVER
221 	}, {
222 		DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
223 		DRM_COMPONENT_DRIVER
224 	}, {
225 		&exynos_drm_platform_driver,
226 		DRM_VIRTUAL_DEVICE
227 	}
228 };
229 
230 static int compare_dev(struct device *dev, void *data)
231 {
232 	return dev == (struct device *)data;
233 }
234 
235 static struct component_match *exynos_drm_match_add(struct device *dev)
236 {
237 	struct component_match *match = NULL;
238 	int i;
239 
240 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
241 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
242 		struct device *p = NULL, *d;
243 
244 		if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
245 			continue;
246 
247 		while ((d = bus_find_device(&platform_bus_type, p,
248 					    &info->driver->driver,
249 					    (void *)platform_bus_type.match))) {
250 			put_device(p);
251 
252 			if (!(info->flags & DRM_FIMC_DEVICE) ||
253 			    exynos_drm_check_fimc_device(d) == 0)
254 				component_match_add(dev, &match,
255 						    compare_dev, d);
256 			p = d;
257 		}
258 		put_device(p);
259 	}
260 
261 	return match ?: ERR_PTR(-ENODEV);
262 }
263 
264 static int exynos_drm_bind(struct device *dev)
265 {
266 	struct exynos_drm_private *private;
267 	struct drm_encoder *encoder;
268 	struct drm_device *drm;
269 	unsigned int clone_mask;
270 	int cnt, ret;
271 
272 	drm = drm_dev_alloc(&exynos_drm_driver, dev);
273 	if (IS_ERR(drm))
274 		return PTR_ERR(drm);
275 
276 	private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
277 	if (!private) {
278 		ret = -ENOMEM;
279 		goto err_free_drm;
280 	}
281 
282 	init_waitqueue_head(&private->wait);
283 	spin_lock_init(&private->lock);
284 
285 	dev_set_drvdata(dev, drm);
286 	drm->dev_private = (void *)private;
287 
288 	drm_mode_config_init(drm);
289 
290 	exynos_drm_mode_config_init(drm);
291 
292 	/* setup possible_clones. */
293 	cnt = 0;
294 	clone_mask = 0;
295 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
296 		clone_mask |= (1 << (cnt++));
297 
298 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
299 		encoder->possible_clones = clone_mask;
300 
301 	/* Try to bind all sub drivers. */
302 	ret = component_bind_all(drm->dev, drm);
303 	if (ret)
304 		goto err_mode_config_cleanup;
305 
306 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
307 	if (ret)
308 		goto err_unbind_all;
309 
310 	drm_mode_config_reset(drm);
311 
312 	/*
313 	 * enable drm irq mode.
314 	 * - with irq_enabled = true, we can use the vblank feature.
315 	 *
316 	 * P.S. note that we wouldn't use drm irq handler but
317 	 *	just specific driver own one instead because
318 	 *	drm framework supports only one irq handler.
319 	 */
320 	drm->irq_enabled = true;
321 
322 	/* init kms poll for handling hpd */
323 	drm_kms_helper_poll_init(drm);
324 
325 	ret = exynos_drm_fbdev_init(drm);
326 	if (ret)
327 		goto err_cleanup_poll;
328 
329 	/* register the DRM device */
330 	ret = drm_dev_register(drm, 0);
331 	if (ret < 0)
332 		goto err_cleanup_fbdev;
333 
334 	return 0;
335 
336 err_cleanup_fbdev:
337 	exynos_drm_fbdev_fini(drm);
338 err_cleanup_poll:
339 	drm_kms_helper_poll_fini(drm);
340 err_unbind_all:
341 	component_unbind_all(drm->dev, drm);
342 err_mode_config_cleanup:
343 	drm_mode_config_cleanup(drm);
344 	exynos_drm_cleanup_dma(drm);
345 	kfree(private);
346 err_free_drm:
347 	drm_dev_put(drm);
348 
349 	return ret;
350 }
351 
352 static void exynos_drm_unbind(struct device *dev)
353 {
354 	struct drm_device *drm = dev_get_drvdata(dev);
355 
356 	drm_dev_unregister(drm);
357 
358 	exynos_drm_fbdev_fini(drm);
359 	drm_kms_helper_poll_fini(drm);
360 
361 	component_unbind_all(drm->dev, drm);
362 	drm_mode_config_cleanup(drm);
363 	exynos_drm_cleanup_dma(drm);
364 
365 	kfree(drm->dev_private);
366 	drm->dev_private = NULL;
367 	dev_set_drvdata(dev, NULL);
368 
369 	drm_dev_put(drm);
370 }
371 
372 static const struct component_master_ops exynos_drm_ops = {
373 	.bind		= exynos_drm_bind,
374 	.unbind		= exynos_drm_unbind,
375 };
376 
377 static int exynos_drm_platform_probe(struct platform_device *pdev)
378 {
379 	struct component_match *match;
380 
381 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
382 
383 	match = exynos_drm_match_add(&pdev->dev);
384 	if (IS_ERR(match))
385 		return PTR_ERR(match);
386 
387 	return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
388 					       match);
389 }
390 
391 static int exynos_drm_platform_remove(struct platform_device *pdev)
392 {
393 	component_master_del(&pdev->dev, &exynos_drm_ops);
394 	return 0;
395 }
396 
397 static struct platform_driver exynos_drm_platform_driver = {
398 	.probe	= exynos_drm_platform_probe,
399 	.remove	= exynos_drm_platform_remove,
400 	.driver	= {
401 		.name	= "exynos-drm",
402 		.pm	= &exynos_drm_pm_ops,
403 	},
404 };
405 
406 static void exynos_drm_unregister_devices(void)
407 {
408 	int i;
409 
410 	for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
411 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
412 		struct device *dev;
413 
414 		if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
415 			continue;
416 
417 		while ((dev = bus_find_device(&platform_bus_type, NULL,
418 					    &info->driver->driver,
419 					    (void *)platform_bus_type.match))) {
420 			put_device(dev);
421 			platform_device_unregister(to_platform_device(dev));
422 		}
423 	}
424 }
425 
426 static int exynos_drm_register_devices(void)
427 {
428 	struct platform_device *pdev;
429 	int i;
430 
431 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
432 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
433 
434 		if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
435 			continue;
436 
437 		pdev = platform_device_register_simple(
438 					info->driver->driver.name, -1, NULL, 0);
439 		if (IS_ERR(pdev))
440 			goto fail;
441 	}
442 
443 	return 0;
444 fail:
445 	exynos_drm_unregister_devices();
446 	return PTR_ERR(pdev);
447 }
448 
449 static void exynos_drm_unregister_drivers(void)
450 {
451 	int i;
452 
453 	for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
454 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
455 
456 		if (!info->driver)
457 			continue;
458 
459 		platform_driver_unregister(info->driver);
460 	}
461 }
462 
463 static int exynos_drm_register_drivers(void)
464 {
465 	int i, ret;
466 
467 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
468 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
469 
470 		if (!info->driver)
471 			continue;
472 
473 		ret = platform_driver_register(info->driver);
474 		if (ret)
475 			goto fail;
476 	}
477 	return 0;
478 fail:
479 	exynos_drm_unregister_drivers();
480 	return ret;
481 }
482 
483 static int exynos_drm_init(void)
484 {
485 	int ret;
486 
487 	ret = exynos_drm_register_devices();
488 	if (ret)
489 		return ret;
490 
491 	ret = exynos_drm_register_drivers();
492 	if (ret)
493 		goto err_unregister_pdevs;
494 
495 	return 0;
496 
497 err_unregister_pdevs:
498 	exynos_drm_unregister_devices();
499 
500 	return ret;
501 }
502 
503 static void exynos_drm_exit(void)
504 {
505 	exynos_drm_unregister_drivers();
506 	exynos_drm_unregister_devices();
507 }
508 
509 module_init(exynos_drm_init);
510 module_exit(exynos_drm_exit);
511 
512 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
513 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
514 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
515 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
516 MODULE_LICENSE("GPL");
517