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