xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_drv.c (revision de2bdb3d)
1 /*
2  * Copyright (C) 2014-2015 Broadcom
3  * Copyright (C) 2013 Red Hat
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/component.h>
12 #include <linux/device.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include "drm_fb_cma_helper.h"
19 #include <drm/drm_fb_helper.h>
20 
21 #include "uapi/drm/vc4_drm.h"
22 #include "vc4_drv.h"
23 #include "vc4_regs.h"
24 
25 #define DRIVER_NAME "vc4"
26 #define DRIVER_DESC "Broadcom VC4 graphics"
27 #define DRIVER_DATE "20140616"
28 #define DRIVER_MAJOR 0
29 #define DRIVER_MINOR 0
30 #define DRIVER_PATCHLEVEL 0
31 
32 /* Helper function for mapping the regs on a platform device. */
33 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
34 {
35 	struct resource *res;
36 	void __iomem *map;
37 
38 	res = platform_get_resource(dev, IORESOURCE_MEM, index);
39 	map = devm_ioremap_resource(&dev->dev, res);
40 	if (IS_ERR(map)) {
41 		DRM_ERROR("Failed to map registers: %ld\n", PTR_ERR(map));
42 		return map;
43 	}
44 
45 	return map;
46 }
47 
48 static int vc4_get_param_ioctl(struct drm_device *dev, void *data,
49 			       struct drm_file *file_priv)
50 {
51 	struct vc4_dev *vc4 = to_vc4_dev(dev);
52 	struct drm_vc4_get_param *args = data;
53 	int ret;
54 
55 	if (args->pad != 0)
56 		return -EINVAL;
57 
58 	switch (args->param) {
59 	case DRM_VC4_PARAM_V3D_IDENT0:
60 		ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
61 		if (ret < 0)
62 			return ret;
63 		args->value = V3D_READ(V3D_IDENT0);
64 		pm_runtime_put(&vc4->v3d->pdev->dev);
65 		break;
66 	case DRM_VC4_PARAM_V3D_IDENT1:
67 		ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
68 		if (ret < 0)
69 			return ret;
70 		args->value = V3D_READ(V3D_IDENT1);
71 		pm_runtime_put(&vc4->v3d->pdev->dev);
72 		break;
73 	case DRM_VC4_PARAM_V3D_IDENT2:
74 		ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
75 		if (ret < 0)
76 			return ret;
77 		args->value = V3D_READ(V3D_IDENT2);
78 		pm_runtime_put(&vc4->v3d->pdev->dev);
79 		break;
80 	case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
81 		args->value = true;
82 		break;
83 	default:
84 		DRM_DEBUG("Unknown parameter %d\n", args->param);
85 		return -EINVAL;
86 	}
87 
88 	return 0;
89 }
90 
91 static void vc4_lastclose(struct drm_device *dev)
92 {
93 	struct vc4_dev *vc4 = to_vc4_dev(dev);
94 
95 	drm_fbdev_cma_restore_mode(vc4->fbdev);
96 }
97 
98 static const struct file_operations vc4_drm_fops = {
99 	.owner = THIS_MODULE,
100 	.open = drm_open,
101 	.release = drm_release,
102 	.unlocked_ioctl = drm_ioctl,
103 	.mmap = vc4_mmap,
104 	.poll = drm_poll,
105 	.read = drm_read,
106 	.compat_ioctl = drm_compat_ioctl,
107 	.llseek = noop_llseek,
108 };
109 
110 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
111 	DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, DRM_RENDER_ALLOW),
112 	DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, DRM_RENDER_ALLOW),
113 	DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, DRM_RENDER_ALLOW),
114 	DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, DRM_RENDER_ALLOW),
115 	DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, DRM_RENDER_ALLOW),
116 	DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, DRM_RENDER_ALLOW),
117 	DRM_IOCTL_DEF_DRV(VC4_GET_HANG_STATE, vc4_get_hang_state_ioctl,
118 			  DRM_ROOT_ONLY),
119 	DRM_IOCTL_DEF_DRV(VC4_GET_PARAM, vc4_get_param_ioctl, DRM_RENDER_ALLOW),
120 };
121 
122 static struct drm_driver vc4_drm_driver = {
123 	.driver_features = (DRIVER_MODESET |
124 			    DRIVER_ATOMIC |
125 			    DRIVER_GEM |
126 			    DRIVER_HAVE_IRQ |
127 			    DRIVER_RENDER |
128 			    DRIVER_PRIME),
129 	.lastclose = vc4_lastclose,
130 	.irq_handler = vc4_irq,
131 	.irq_preinstall = vc4_irq_preinstall,
132 	.irq_postinstall = vc4_irq_postinstall,
133 	.irq_uninstall = vc4_irq_uninstall,
134 
135 	.enable_vblank = vc4_enable_vblank,
136 	.disable_vblank = vc4_disable_vblank,
137 	.get_vblank_counter = drm_vblank_no_hw_counter,
138 	.get_scanout_position = vc4_crtc_get_scanoutpos,
139 	.get_vblank_timestamp = vc4_crtc_get_vblank_timestamp,
140 
141 #if defined(CONFIG_DEBUG_FS)
142 	.debugfs_init = vc4_debugfs_init,
143 	.debugfs_cleanup = vc4_debugfs_cleanup,
144 #endif
145 
146 	.gem_create_object = vc4_create_object,
147 	.gem_free_object_unlocked = vc4_free_object,
148 	.gem_vm_ops = &drm_gem_cma_vm_ops,
149 
150 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
151 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
152 	.gem_prime_import = drm_gem_prime_import,
153 	.gem_prime_export = vc4_prime_export,
154 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
155 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
156 	.gem_prime_vmap = vc4_prime_vmap,
157 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
158 	.gem_prime_mmap = vc4_prime_mmap,
159 
160 	.dumb_create = vc4_dumb_create,
161 	.dumb_map_offset = drm_gem_cma_dumb_map_offset,
162 	.dumb_destroy = drm_gem_dumb_destroy,
163 
164 	.ioctls = vc4_drm_ioctls,
165 	.num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
166 	.fops = &vc4_drm_fops,
167 
168 	.name = DRIVER_NAME,
169 	.desc = DRIVER_DESC,
170 	.date = DRIVER_DATE,
171 	.major = DRIVER_MAJOR,
172 	.minor = DRIVER_MINOR,
173 	.patchlevel = DRIVER_PATCHLEVEL,
174 };
175 
176 static int compare_dev(struct device *dev, void *data)
177 {
178 	return dev == data;
179 }
180 
181 static void vc4_match_add_drivers(struct device *dev,
182 				  struct component_match **match,
183 				  struct platform_driver *const *drivers,
184 				  int count)
185 {
186 	int i;
187 
188 	for (i = 0; i < count; i++) {
189 		struct device_driver *drv = &drivers[i]->driver;
190 		struct device *p = NULL, *d;
191 
192 		while ((d = bus_find_device(&platform_bus_type, p, drv,
193 					    (void *)platform_bus_type.match))) {
194 			put_device(p);
195 			component_match_add(dev, match, compare_dev, d);
196 			p = d;
197 		}
198 		put_device(p);
199 	}
200 }
201 
202 static void vc4_kick_out_firmware_fb(void)
203 {
204 	struct apertures_struct *ap;
205 
206 	ap = alloc_apertures(1);
207 	if (!ap)
208 		return;
209 
210 	/* Since VC4 is a UMA device, the simplefb node may have been
211 	 * located anywhere in memory.
212 	 */
213 	ap->ranges[0].base = 0;
214 	ap->ranges[0].size = ~0;
215 
216 	drm_fb_helper_remove_conflicting_framebuffers(ap, "vc4drmfb", false);
217 	kfree(ap);
218 }
219 
220 static int vc4_drm_bind(struct device *dev)
221 {
222 	struct platform_device *pdev = to_platform_device(dev);
223 	struct drm_device *drm;
224 	struct vc4_dev *vc4;
225 	int ret = 0;
226 
227 	dev->coherent_dma_mask = DMA_BIT_MASK(32);
228 
229 	vc4 = devm_kzalloc(dev, sizeof(*vc4), GFP_KERNEL);
230 	if (!vc4)
231 		return -ENOMEM;
232 
233 	drm = drm_dev_alloc(&vc4_drm_driver, dev);
234 	if (IS_ERR(drm))
235 		return PTR_ERR(drm);
236 	platform_set_drvdata(pdev, drm);
237 	vc4->dev = drm;
238 	drm->dev_private = vc4;
239 
240 	vc4_bo_cache_init(drm);
241 
242 	drm_mode_config_init(drm);
243 
244 	vc4_gem_init(drm);
245 
246 	ret = component_bind_all(dev, drm);
247 	if (ret)
248 		goto gem_destroy;
249 
250 	vc4_kick_out_firmware_fb();
251 
252 	ret = drm_dev_register(drm, 0);
253 	if (ret < 0)
254 		goto unbind_all;
255 
256 	vc4_kms_load(drm);
257 
258 	return 0;
259 
260 unbind_all:
261 	component_unbind_all(dev, drm);
262 gem_destroy:
263 	vc4_gem_destroy(drm);
264 	drm_dev_unref(drm);
265 	vc4_bo_cache_destroy(drm);
266 	return ret;
267 }
268 
269 static void vc4_drm_unbind(struct device *dev)
270 {
271 	struct platform_device *pdev = to_platform_device(dev);
272 	struct drm_device *drm = platform_get_drvdata(pdev);
273 	struct vc4_dev *vc4 = to_vc4_dev(drm);
274 
275 	if (vc4->fbdev)
276 		drm_fbdev_cma_fini(vc4->fbdev);
277 
278 	drm_mode_config_cleanup(drm);
279 
280 	drm_put_dev(drm);
281 }
282 
283 static const struct component_master_ops vc4_drm_ops = {
284 	.bind = vc4_drm_bind,
285 	.unbind = vc4_drm_unbind,
286 };
287 
288 static struct platform_driver *const component_drivers[] = {
289 	&vc4_hdmi_driver,
290 	&vc4_dpi_driver,
291 	&vc4_hvs_driver,
292 	&vc4_crtc_driver,
293 	&vc4_v3d_driver,
294 };
295 
296 static int vc4_platform_drm_probe(struct platform_device *pdev)
297 {
298 	struct component_match *match = NULL;
299 	struct device *dev = &pdev->dev;
300 
301 	vc4_match_add_drivers(dev, &match,
302 			      component_drivers, ARRAY_SIZE(component_drivers));
303 
304 	return component_master_add_with_match(dev, &vc4_drm_ops, match);
305 }
306 
307 static int vc4_platform_drm_remove(struct platform_device *pdev)
308 {
309 	component_master_del(&pdev->dev, &vc4_drm_ops);
310 
311 	return 0;
312 }
313 
314 static const struct of_device_id vc4_of_match[] = {
315 	{ .compatible = "brcm,bcm2835-vc4", },
316 	{},
317 };
318 MODULE_DEVICE_TABLE(of, vc4_of_match);
319 
320 static struct platform_driver vc4_platform_driver = {
321 	.probe		= vc4_platform_drm_probe,
322 	.remove		= vc4_platform_drm_remove,
323 	.driver		= {
324 		.name	= "vc4-drm",
325 		.of_match_table = vc4_of_match,
326 	},
327 };
328 
329 static int __init vc4_drm_register(void)
330 {
331 	int i, ret;
332 
333 	for (i = 0; i < ARRAY_SIZE(component_drivers); i++) {
334 		ret = platform_driver_register(component_drivers[i]);
335 		if (ret) {
336 			while (--i >= 0)
337 				platform_driver_unregister(component_drivers[i]);
338 			return ret;
339 		}
340 	}
341 	return platform_driver_register(&vc4_platform_driver);
342 }
343 
344 static void __exit vc4_drm_unregister(void)
345 {
346 	int i;
347 
348 	for (i = ARRAY_SIZE(component_drivers) - 1; i >= 0; i--)
349 		platform_driver_unregister(component_drivers[i]);
350 
351 	platform_driver_unregister(&vc4_platform_driver);
352 }
353 
354 module_init(vc4_drm_register);
355 module_exit(vc4_drm_unregister);
356 
357 MODULE_ALIAS("platform:vc4-drm");
358 MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
359 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
360 MODULE_LICENSE("GPL v2");
361