blob: 493bb580a4138ba6b572d9a285e3e88830e9576d [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
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>
Derek Foreman48666d52015-07-02 11:19:54 -050017#include "drm_fb_cma_helper.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080018
Eric Anholtd5bc60f2015-01-18 09:33:17 +130019#include "uapi/drm/vc4_drm.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080020#include "vc4_drv.h"
21#include "vc4_regs.h"
22
23#define DRIVER_NAME "vc4"
24#define DRIVER_DESC "Broadcom VC4 graphics"
25#define DRIVER_DATE "20140616"
26#define DRIVER_MAJOR 0
27#define DRIVER_MINOR 0
28#define DRIVER_PATCHLEVEL 0
29
30/* Helper function for mapping the regs on a platform device. */
31void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
32{
33 struct resource *res;
34 void __iomem *map;
35
36 res = platform_get_resource(dev, IORESOURCE_MEM, index);
37 map = devm_ioremap_resource(&dev->dev, res);
38 if (IS_ERR(map)) {
39 DRM_ERROR("Failed to map registers: %ld\n", PTR_ERR(map));
40 return map;
41 }
42
43 return map;
44}
45
Derek Foreman48666d52015-07-02 11:19:54 -050046static void vc4_lastclose(struct drm_device *dev)
47{
48 struct vc4_dev *vc4 = to_vc4_dev(dev);
49
Markus Elfring63fe9bb2016-07-15 21:15:37 +020050 drm_fbdev_cma_restore_mode(vc4->fbdev);
Derek Foreman48666d52015-07-02 11:19:54 -050051}
52
Eric Anholtc8b75bc2015-03-02 13:01:12 -080053static const struct file_operations vc4_drm_fops = {
54 .owner = THIS_MODULE,
55 .open = drm_open,
56 .release = drm_release,
57 .unlocked_ioctl = drm_ioctl,
Eric Anholt463873d2015-11-30 11:41:40 -080058 .mmap = vc4_mmap,
Eric Anholtc8b75bc2015-03-02 13:01:12 -080059 .poll = drm_poll,
60 .read = drm_read,
61#ifdef CONFIG_COMPAT
62 .compat_ioctl = drm_compat_ioctl,
63#endif
64 .llseek = noop_llseek,
65};
66
67static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
Eric Anholtd5b1a782015-11-30 12:13:37 -080068 DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, 0),
69 DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, 0),
70 DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, 0),
Eric Anholtd5bc60f2015-01-18 09:33:17 +130071 DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, 0),
72 DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, 0),
Eric Anholt463873d2015-11-30 11:41:40 -080073 DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, 0),
Eric Anholt21461362015-10-30 10:09:02 -070074 DRM_IOCTL_DEF_DRV(VC4_GET_HANG_STATE, vc4_get_hang_state_ioctl,
75 DRM_ROOT_ONLY),
Eric Anholtc8b75bc2015-03-02 13:01:12 -080076};
77
78static struct drm_driver vc4_drm_driver = {
79 .driver_features = (DRIVER_MODESET |
80 DRIVER_ATOMIC |
81 DRIVER_GEM |
Eric Anholtd5b1a782015-11-30 12:13:37 -080082 DRIVER_HAVE_IRQ |
Eric Anholt0cd3e272016-04-14 23:16:05 -070083 DRIVER_RENDER |
Eric Anholtc8b75bc2015-03-02 13:01:12 -080084 DRIVER_PRIME),
Derek Foreman48666d52015-07-02 11:19:54 -050085 .lastclose = vc4_lastclose,
Eric Anholtd5b1a782015-11-30 12:13:37 -080086 .irq_handler = vc4_irq,
87 .irq_preinstall = vc4_irq_preinstall,
88 .irq_postinstall = vc4_irq_postinstall,
89 .irq_uninstall = vc4_irq_uninstall,
90
Eric Anholtc8b75bc2015-03-02 13:01:12 -080091 .enable_vblank = vc4_enable_vblank,
92 .disable_vblank = vc4_disable_vblank,
93 .get_vblank_counter = drm_vblank_count,
94
95#if defined(CONFIG_DEBUG_FS)
96 .debugfs_init = vc4_debugfs_init,
97 .debugfs_cleanup = vc4_debugfs_cleanup,
98#endif
99
Eric Anholtc826a6e2015-10-09 20:25:07 -0700100 .gem_create_object = vc4_create_object,
Daniel Vetterb82caaf2016-05-30 19:53:07 +0200101 .gem_free_object_unlocked = vc4_free_object,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800102 .gem_vm_ops = &drm_gem_cma_vm_ops,
103
104 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
105 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
106 .gem_prime_import = drm_gem_prime_import,
Eric Anholt463873d2015-11-30 11:41:40 -0800107 .gem_prime_export = vc4_prime_export,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800108 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
109 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
Eric Anholt463873d2015-11-30 11:41:40 -0800110 .gem_prime_vmap = vc4_prime_vmap,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800111 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
Eric Anholt463873d2015-11-30 11:41:40 -0800112 .gem_prime_mmap = vc4_prime_mmap,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800113
114 .dumb_create = vc4_dumb_create,
115 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
116 .dumb_destroy = drm_gem_dumb_destroy,
117
118 .ioctls = vc4_drm_ioctls,
119 .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
120 .fops = &vc4_drm_fops,
121
122 .name = DRIVER_NAME,
123 .desc = DRIVER_DESC,
124 .date = DRIVER_DATE,
125 .major = DRIVER_MAJOR,
126 .minor = DRIVER_MINOR,
127 .patchlevel = DRIVER_PATCHLEVEL,
128};
129
130static int compare_dev(struct device *dev, void *data)
131{
132 return dev == data;
133}
134
135static void vc4_match_add_drivers(struct device *dev,
136 struct component_match **match,
137 struct platform_driver *const *drivers,
138 int count)
139{
140 int i;
141
142 for (i = 0; i < count; i++) {
143 struct device_driver *drv = &drivers[i]->driver;
144 struct device *p = NULL, *d;
145
146 while ((d = bus_find_device(&platform_bus_type, p, drv,
147 (void *)platform_bus_type.match))) {
148 put_device(p);
149 component_match_add(dev, match, compare_dev, d);
150 p = d;
151 }
152 put_device(p);
153 }
154}
155
Eric Anholtb3a15f62016-04-19 13:24:14 -0700156static void vc4_kick_out_firmware_fb(void)
157{
158 struct apertures_struct *ap;
159
160 ap = alloc_apertures(1);
161 if (!ap)
162 return;
163
164 /* Since VC4 is a UMA device, the simplefb node may have been
165 * located anywhere in memory.
166 */
167 ap->ranges[0].base = 0;
168 ap->ranges[0].size = ~0;
169
170 remove_conflicting_framebuffers(ap, "vc4drmfb", false);
171 kfree(ap);
172}
173
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800174static int vc4_drm_bind(struct device *dev)
175{
176 struct platform_device *pdev = to_platform_device(dev);
177 struct drm_device *drm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800178 struct vc4_dev *vc4;
179 int ret = 0;
180
181 dev->coherent_dma_mask = DMA_BIT_MASK(32);
182
183 vc4 = devm_kzalloc(dev, sizeof(*vc4), GFP_KERNEL);
184 if (!vc4)
185 return -ENOMEM;
186
187 drm = drm_dev_alloc(&vc4_drm_driver, dev);
188 if (!drm)
189 return -ENOMEM;
190 platform_set_drvdata(pdev, drm);
191 vc4->dev = drm;
192 drm->dev_private = vc4;
193
Eric Anholtc826a6e2015-10-09 20:25:07 -0700194 vc4_bo_cache_init(drm);
195
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800196 drm_mode_config_init(drm);
197 if (ret)
198 goto unref;
199
Eric Anholtd5b1a782015-11-30 12:13:37 -0800200 vc4_gem_init(drm);
201
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800202 ret = component_bind_all(dev, drm);
203 if (ret)
Eric Anholtd5b1a782015-11-30 12:13:37 -0800204 goto gem_destroy;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800205
Eric Anholtb3a15f62016-04-19 13:24:14 -0700206 vc4_kick_out_firmware_fb();
207
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800208 ret = drm_dev_register(drm, 0);
209 if (ret < 0)
210 goto unbind_all;
211
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800212 vc4_kms_load(drm);
213
214 return 0;
215
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800216unbind_all:
217 component_unbind_all(dev, drm);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800218gem_destroy:
219 vc4_gem_destroy(drm);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800220unref:
221 drm_dev_unref(drm);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700222 vc4_bo_cache_destroy(drm);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800223 return ret;
224}
225
226static void vc4_drm_unbind(struct device *dev)
227{
228 struct platform_device *pdev = to_platform_device(dev);
229 struct drm_device *drm = platform_get_drvdata(pdev);
Derek Foreman48666d52015-07-02 11:19:54 -0500230 struct vc4_dev *vc4 = to_vc4_dev(drm);
231
232 if (vc4->fbdev)
233 drm_fbdev_cma_fini(vc4->fbdev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800234
235 drm_mode_config_cleanup(drm);
236
237 drm_put_dev(drm);
238}
239
240static const struct component_master_ops vc4_drm_ops = {
241 .bind = vc4_drm_bind,
242 .unbind = vc4_drm_unbind,
243};
244
245static struct platform_driver *const component_drivers[] = {
246 &vc4_hdmi_driver,
Eric Anholt08302c32016-02-10 11:42:32 -0800247 &vc4_dpi_driver,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800248 &vc4_crtc_driver,
249 &vc4_hvs_driver,
Eric Anholtd3f51682015-03-02 13:01:12 -0800250 &vc4_v3d_driver,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800251};
252
253static int vc4_platform_drm_probe(struct platform_device *pdev)
254{
255 struct component_match *match = NULL;
256 struct device *dev = &pdev->dev;
257
258 vc4_match_add_drivers(dev, &match,
259 component_drivers, ARRAY_SIZE(component_drivers));
260
261 return component_master_add_with_match(dev, &vc4_drm_ops, match);
262}
263
264static int vc4_platform_drm_remove(struct platform_device *pdev)
265{
266 component_master_del(&pdev->dev, &vc4_drm_ops);
267
268 return 0;
269}
270
271static const struct of_device_id vc4_of_match[] = {
272 { .compatible = "brcm,bcm2835-vc4", },
273 {},
274};
275MODULE_DEVICE_TABLE(of, vc4_of_match);
276
277static struct platform_driver vc4_platform_driver = {
278 .probe = vc4_platform_drm_probe,
279 .remove = vc4_platform_drm_remove,
280 .driver = {
281 .name = "vc4-drm",
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800282 .of_match_table = vc4_of_match,
283 },
284};
285
286static int __init vc4_drm_register(void)
287{
288 int i, ret;
289
290 for (i = 0; i < ARRAY_SIZE(component_drivers); i++) {
291 ret = platform_driver_register(component_drivers[i]);
292 if (ret) {
293 while (--i >= 0)
294 platform_driver_unregister(component_drivers[i]);
295 return ret;
296 }
297 }
298 return platform_driver_register(&vc4_platform_driver);
299}
300
301static void __exit vc4_drm_unregister(void)
302{
303 int i;
304
305 for (i = ARRAY_SIZE(component_drivers) - 1; i >= 0; i--)
306 platform_driver_unregister(component_drivers[i]);
307
308 platform_driver_unregister(&vc4_platform_driver);
309}
310
311module_init(vc4_drm_register);
312module_exit(vc4_drm_unregister);
313
314MODULE_ALIAS("platform:vc4-drm");
315MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
316MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
317MODULE_LICENSE("GPL v2");