1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2015 Broadcom
4 * Copyright (C) 2013 Red Hat
8 * DOC: Broadcom VC4 Graphics Driver
10 * The Broadcom VideoCore 4 (present in the Raspberry Pi) contains a
11 * OpenGL ES 2.0-compatible 3D engine called V3D, and a highly
12 * configurable display output pipeline that supports HDMI, DSI, DPI,
13 * and Composite TV output.
15 * The 3D engine also has an interface for submitting arbitrary
16 * compute shader-style jobs using the same shader processor as is
17 * used for vertex and fragment shaders in GLES 2.0. However, given
18 * that the hardware isn't able to expose any standard interfaces like
19 * OpenGL compute shaders or OpenCL, it isn't supported by this
23 #include <linux/clk.h>
24 #include <linux/component.h>
25 #include <linux/device.h>
26 #include <linux/dma-mapping.h>
28 #include <linux/module.h>
29 #include <linux/of_platform.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
33 #include <drm/drm_aperture.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_fb_cma_helper.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_vblank.h>
40 #include <soc/bcm2835/raspberrypi-firmware.h>
42 #include "uapi/drm/vc4_drm.h"
47 #define DRIVER_NAME "vc4"
48 #define DRIVER_DESC "Broadcom VC4 graphics"
49 #define DRIVER_DATE "20140616"
50 #define DRIVER_MAJOR 0
51 #define DRIVER_MINOR 0
52 #define DRIVER_PATCHLEVEL 0
54 /* Helper function for mapping the regs on a platform device. */
55 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
60 res = platform_get_resource(dev, IORESOURCE_MEM, index);
61 map = devm_ioremap_resource(&dev->dev, res);
68 int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args)
70 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
72 if (args->pitch < min_pitch)
73 args->pitch = min_pitch;
75 if (args->size < args->pitch * args->height)
76 args->size = args->pitch * args->height;
81 static int vc4_dumb_create(struct drm_file *file_priv,
82 struct drm_device *dev,
83 struct drm_mode_create_dumb *args)
87 ret = vc4_dumb_fixup_args(args);
91 return drm_gem_cma_dumb_create_internal(file_priv, dev, args);
94 static int vc4_get_param_ioctl(struct drm_device *dev, void *data,
95 struct drm_file *file_priv)
97 struct vc4_dev *vc4 = to_vc4_dev(dev);
98 struct drm_vc4_get_param *args = data;
107 switch (args->param) {
108 case DRM_VC4_PARAM_V3D_IDENT0:
109 ret = vc4_v3d_pm_get(vc4);
112 args->value = V3D_READ(V3D_IDENT0);
115 case DRM_VC4_PARAM_V3D_IDENT1:
116 ret = vc4_v3d_pm_get(vc4);
119 args->value = V3D_READ(V3D_IDENT1);
122 case DRM_VC4_PARAM_V3D_IDENT2:
123 ret = vc4_v3d_pm_get(vc4);
126 args->value = V3D_READ(V3D_IDENT2);
129 case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
130 case DRM_VC4_PARAM_SUPPORTS_ETC1:
131 case DRM_VC4_PARAM_SUPPORTS_THREADED_FS:
132 case DRM_VC4_PARAM_SUPPORTS_FIXED_RCL_ORDER:
133 case DRM_VC4_PARAM_SUPPORTS_MADVISE:
134 case DRM_VC4_PARAM_SUPPORTS_PERFMON:
138 DRM_DEBUG("Unknown parameter %d\n", args->param);
145 static int vc4_open(struct drm_device *dev, struct drm_file *file)
147 struct vc4_file *vc4file;
149 vc4file = kzalloc(sizeof(*vc4file), GFP_KERNEL);
153 vc4_perfmon_open_file(vc4file);
154 file->driver_priv = vc4file;
158 static void vc4_close(struct drm_device *dev, struct drm_file *file)
160 struct vc4_dev *vc4 = to_vc4_dev(dev);
161 struct vc4_file *vc4file = file->driver_priv;
163 if (vc4file->bin_bo_used)
164 vc4_v3d_bin_bo_put(vc4);
166 vc4_perfmon_close_file(vc4file);
170 DEFINE_DRM_GEM_FOPS(vc4_drm_fops);
172 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
173 DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, DRM_RENDER_ALLOW),
174 DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, DRM_RENDER_ALLOW),
175 DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, DRM_RENDER_ALLOW),
176 DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, DRM_RENDER_ALLOW),
177 DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, DRM_RENDER_ALLOW),
178 DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, DRM_RENDER_ALLOW),
179 DRM_IOCTL_DEF_DRV(VC4_GET_HANG_STATE, vc4_get_hang_state_ioctl,
181 DRM_IOCTL_DEF_DRV(VC4_GET_PARAM, vc4_get_param_ioctl, DRM_RENDER_ALLOW),
182 DRM_IOCTL_DEF_DRV(VC4_SET_TILING, vc4_set_tiling_ioctl, DRM_RENDER_ALLOW),
183 DRM_IOCTL_DEF_DRV(VC4_GET_TILING, vc4_get_tiling_ioctl, DRM_RENDER_ALLOW),
184 DRM_IOCTL_DEF_DRV(VC4_LABEL_BO, vc4_label_bo_ioctl, DRM_RENDER_ALLOW),
185 DRM_IOCTL_DEF_DRV(VC4_GEM_MADVISE, vc4_gem_madvise_ioctl, DRM_RENDER_ALLOW),
186 DRM_IOCTL_DEF_DRV(VC4_PERFMON_CREATE, vc4_perfmon_create_ioctl, DRM_RENDER_ALLOW),
187 DRM_IOCTL_DEF_DRV(VC4_PERFMON_DESTROY, vc4_perfmon_destroy_ioctl, DRM_RENDER_ALLOW),
188 DRM_IOCTL_DEF_DRV(VC4_PERFMON_GET_VALUES, vc4_perfmon_get_values_ioctl, DRM_RENDER_ALLOW),
191 static const struct drm_driver vc4_drm_driver = {
192 .driver_features = (DRIVER_MODESET |
198 .postclose = vc4_close,
200 #if defined(CONFIG_DEBUG_FS)
201 .debugfs_init = vc4_debugfs_init,
204 .gem_create_object = vc4_create_object,
206 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(vc4_bo_dumb_create),
208 .ioctls = vc4_drm_ioctls,
209 .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
210 .fops = &vc4_drm_fops,
215 .major = DRIVER_MAJOR,
216 .minor = DRIVER_MINOR,
217 .patchlevel = DRIVER_PATCHLEVEL,
220 static const struct drm_driver vc5_drm_driver = {
221 .driver_features = (DRIVER_MODESET |
225 #if defined(CONFIG_DEBUG_FS)
226 .debugfs_init = vc4_debugfs_init,
229 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(vc4_dumb_create),
231 .fops = &vc4_drm_fops,
236 .major = DRIVER_MAJOR,
237 .minor = DRIVER_MINOR,
238 .patchlevel = DRIVER_PATCHLEVEL,
241 static int compare_dev(struct device *dev, void *data)
246 static void vc4_match_add_drivers(struct device *dev,
247 struct component_match **match,
248 struct platform_driver *const *drivers,
253 for (i = 0; i < count; i++) {
254 struct device_driver *drv = &drivers[i]->driver;
255 struct device *p = NULL, *d;
257 while ((d = platform_find_device_by_driver(p, drv))) {
259 component_match_add(dev, match, compare_dev, d);
266 const struct of_device_id vc4_dma_range_matches[] = {
267 { .compatible = "brcm,bcm2835-hvs" },
268 { .compatible = "brcm,bcm2711-hvs" },
269 { .compatible = "raspberrypi,rpi-firmware-kms" },
270 { .compatible = "brcm,bcm2835-v3d" },
271 { .compatible = "brcm,cygnus-v3d" },
272 { .compatible = "brcm,vc4-v3d" },
277 * we need this helper function for determining presence of fkms
278 * before it's been bound
280 static bool firmware_kms(void)
282 return of_device_is_available(of_find_compatible_node(NULL, NULL,
283 "raspberrypi,rpi-firmware-kms")) ||
284 of_device_is_available(of_find_compatible_node(NULL, NULL,
285 "raspberrypi,rpi-firmware-kms-2711"));
288 static int vc4_drm_bind(struct device *dev)
290 struct platform_device *pdev = to_platform_device(dev);
291 const struct drm_driver *driver;
292 struct rpi_firmware *firmware = NULL;
293 struct drm_device *drm;
295 struct device_node *node;
296 struct drm_crtc *crtc;
300 dev->coherent_dma_mask = DMA_BIT_MASK(32);
302 is_vc5 = of_device_is_compatible(dev->of_node, "brcm,bcm2711-vc5");
304 driver = &vc5_drm_driver;
306 driver = &vc4_drm_driver;
308 node = of_find_matching_node_and_match(NULL, vc4_dma_range_matches,
311 ret = of_dma_configure(dev, node, true);
318 vc4 = devm_drm_dev_alloc(dev, driver, struct vc4_dev, base);
321 vc4->is_vc5 = is_vc5;
324 platform_set_drvdata(pdev, drm);
325 INIT_LIST_HEAD(&vc4->debugfs_list);
328 mutex_init(&vc4->bin_bo_lock);
330 ret = vc4_bo_cache_init(drm);
335 ret = drmm_mode_config_init(drm);
340 ret = vc4_gem_init(drm);
345 node = of_find_compatible_node(NULL, NULL, "raspberrypi,bcm2835-firmware");
347 firmware = rpi_firmware_get(node);
351 return -EPROBE_DEFER;
354 ret = drm_aperture_remove_framebuffers(false, driver);
358 if (firmware && !firmware_kms()) {
359 ret = rpi_firmware_property(firmware,
360 RPI_FIRMWARE_NOTIFY_DISPLAY_DONE,
363 drm_warn(drm, "Couldn't stop firmware display driver: %d\n", ret);
365 rpi_firmware_put(firmware);
368 ret = component_bind_all(dev, drm);
372 if (!vc4->firmware_kms) {
373 ret = vc4_plane_create_additional_planes(drm);
378 ret = vc4_kms_load(drm);
382 if (!vc4->firmware_kms) {
383 drm_for_each_crtc(crtc, drm)
384 vc4_crtc_disable_at_boot(crtc);
387 ret = drm_dev_register(drm, 0);
391 drm_fbdev_generic_setup(drm, 16);
396 component_unbind_all(dev, drm);
401 static void vc4_drm_unbind(struct device *dev)
403 struct drm_device *drm = dev_get_drvdata(dev);
405 drm_dev_unregister(drm);
407 drm_atomic_helper_shutdown(drm);
410 static const struct component_master_ops vc4_drm_ops = {
411 .bind = vc4_drm_bind,
412 .unbind = vc4_drm_unbind,
416 * This list determines the binding order of our components, and we have
418 * - The TXP driver needs to be bound before the PixelValves (CRTC)
419 * but after the HVS to set the possible_crtc field properly
420 * - The HDMI driver needs to be bound after the HVS so that we can
421 * lookup the HVS maximum core clock rate and figure out if we
422 * support 4kp60 or not.
424 static struct platform_driver *const component_drivers[] = {
432 &vc4_firmware_kms_driver,
436 static int vc4_platform_drm_probe(struct platform_device *pdev)
438 struct component_match *match = NULL;
439 struct device *dev = &pdev->dev;
441 vc4_match_add_drivers(dev, &match,
442 component_drivers, ARRAY_SIZE(component_drivers));
444 return component_master_add_with_match(dev, &vc4_drm_ops, match);
447 static int vc4_platform_drm_remove(struct platform_device *pdev)
449 component_master_del(&pdev->dev, &vc4_drm_ops);
454 static const struct of_device_id vc4_of_match[] = {
455 { .compatible = "brcm,bcm2711-vc5", },
456 { .compatible = "brcm,bcm2835-vc4", },
457 { .compatible = "brcm,cygnus-vc4", },
460 MODULE_DEVICE_TABLE(of, vc4_of_match);
462 static struct platform_driver vc4_platform_driver = {
463 .probe = vc4_platform_drm_probe,
464 .remove = vc4_platform_drm_remove,
467 .of_match_table = vc4_of_match,
471 static int __init vc4_drm_register(void)
475 ret = platform_register_drivers(component_drivers,
476 ARRAY_SIZE(component_drivers));
480 return platform_driver_register(&vc4_platform_driver);
483 static void __exit vc4_drm_unregister(void)
485 platform_unregister_drivers(component_drivers,
486 ARRAY_SIZE(component_drivers));
487 platform_driver_unregister(&vc4_platform_driver);
490 module_init(vc4_drm_register);
491 module_exit(vc4_drm_unregister);
493 MODULE_ALIAS("platform:vc4-drm");
494 MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
495 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
496 MODULE_LICENSE("GPL v2");