1 // SPDX-License-Identifier: MIT 2 3 #include <linux/device.h> 4 #include <linux/fb.h> 5 #include <linux/list.h> 6 #include <linux/mutex.h> 7 #include <linux/pci.h> 8 #include <linux/platform_device.h> /* for firmware helpers */ 9 #include <linux/slab.h> 10 #include <linux/types.h> 11 #include <linux/vgaarb.h> 12 13 #include <drm/drm_aperture.h> 14 #include <drm/drm_drv.h> 15 #include <drm/drm_print.h> 16 17 /** 18 * DOC: overview 19 * 20 * A graphics device might be supported by different drivers, but only one 21 * driver can be active at any given time. Many systems load a generic 22 * graphics drivers, such as EFI-GOP or VESA, early during the boot process. 23 * During later boot stages, they replace the generic driver with a dedicated, 24 * hardware-specific driver. To take over the device the dedicated driver 25 * first has to remove the generic driver. DRM aperture functions manage 26 * ownership of DRM framebuffer memory and hand-over between drivers. 27 * 28 * DRM drivers should call drm_aperture_remove_conflicting_framebuffers() 29 * at the top of their probe function. The function removes any generic 30 * driver that is currently associated with the given framebuffer memory. 31 * If the framebuffer is located at PCI BAR 0, the rsp code looks as in the 32 * example given below. 33 * 34 * .. code-block:: c 35 * 36 * static const struct drm_driver example_driver = { 37 * ... 38 * }; 39 * 40 * static int remove_conflicting_framebuffers(struct pci_dev *pdev) 41 * { 42 * bool primary = false; 43 * resource_size_t base, size; 44 * int ret; 45 * 46 * base = pci_resource_start(pdev, 0); 47 * size = pci_resource_len(pdev, 0); 48 * #ifdef CONFIG_X86 49 * primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW; 50 * #endif 51 * 52 * return drm_aperture_remove_conflicting_framebuffers(base, size, primary, 53 * &example_driver); 54 * } 55 * 56 * static int probe(struct pci_dev *pdev) 57 * { 58 * int ret; 59 * 60 * // Remove any generic drivers... 61 * ret = remove_conflicting_framebuffers(pdev); 62 * if (ret) 63 * return ret; 64 * 65 * // ... and initialize the hardware. 66 * ... 67 * 68 * drm_dev_register(); 69 * 70 * return 0; 71 * } 72 * 73 * PCI device drivers should call 74 * drm_aperture_remove_conflicting_pci_framebuffers() and let it detect the 75 * framebuffer apertures automatically. Device drivers without knowledge of 76 * the framebuffer's location shall call drm_aperture_remove_framebuffers(), 77 * which removes all drivers for known framebuffer. 78 * 79 * Drivers that are susceptible to being removed by other drivers, such as 80 * generic EFI or VESA drivers, have to register themselves as owners of their 81 * given framebuffer memory. Ownership of the framebuffer memory is achieved 82 * by calling devm_aperture_acquire_from_firmware(). On success, the driver 83 * is the owner of the framebuffer range. The function fails if the 84 * framebuffer is already by another driver. See below for an example. 85 * 86 * .. code-block:: c 87 * 88 * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev) 89 * { 90 * resource_size_t base, size; 91 * 92 * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 93 * if (!mem) 94 * return -EINVAL; 95 * base = mem->start; 96 * size = resource_size(mem); 97 * 98 * return devm_acquire_aperture_from_firmware(dev, base, size); 99 * } 100 * 101 * static int probe(struct platform_device *pdev) 102 * { 103 * struct drm_device *dev; 104 * int ret; 105 * 106 * // ... Initialize the device... 107 * dev = devm_drm_dev_alloc(); 108 * ... 109 * 110 * // ... and acquire ownership of the framebuffer. 111 * ret = acquire_framebuffers(dev, pdev); 112 * if (ret) 113 * return ret; 114 * 115 * drm_dev_register(dev, 0); 116 * 117 * return 0; 118 * } 119 * 120 * The generic driver is now subject to forced removal by other drivers. This 121 * only works for platform drivers that support hot unplug. 122 * When a driver calls drm_aperture_remove_conflicting_framebuffers() et al 123 * for the registered framebuffer range, the aperture helpers call 124 * platform_device_unregister() and the generic driver unloads itself. It 125 * may not access the device's registers, framebuffer memory, ROM, etc 126 * afterwards. 127 */ 128 129 struct drm_aperture { 130 struct drm_device *dev; 131 resource_size_t base; 132 resource_size_t size; 133 struct list_head lh; 134 void (*detach)(struct drm_device *dev); 135 }; 136 137 static DRM_LIST_HEAD(drm_apertures); 138 static DEFINE_MUTEX(drm_apertures_lock); 139 140 static bool overlap(resource_size_t base1, resource_size_t end1, 141 resource_size_t base2, resource_size_t end2) 142 { 143 return (base1 < end2) && (end1 > base2); 144 } 145 146 #ifdef notyet 147 148 static void devm_aperture_acquire_release(void *data) 149 { 150 struct drm_aperture *ap = data; 151 bool detached = !ap->dev; 152 153 if (detached) 154 return; 155 156 mutex_lock(&drm_apertures_lock); 157 list_del(&ap->lh); 158 mutex_unlock(&drm_apertures_lock); 159 } 160 161 static int devm_aperture_acquire(struct drm_device *dev, 162 resource_size_t base, resource_size_t size, 163 void (*detach)(struct drm_device *)) 164 { 165 size_t end = base + size; 166 struct list_head *pos; 167 struct drm_aperture *ap; 168 169 mutex_lock(&drm_apertures_lock); 170 171 list_for_each(pos, &drm_apertures) { 172 ap = container_of(pos, struct drm_aperture, lh); 173 if (overlap(base, end, ap->base, ap->base + ap->size)) { 174 mutex_unlock(&drm_apertures_lock); 175 return -EBUSY; 176 } 177 } 178 179 ap = devm_kzalloc(dev->dev, sizeof(*ap), GFP_KERNEL); 180 if (!ap) { 181 mutex_unlock(&drm_apertures_lock); 182 return -ENOMEM; 183 } 184 185 ap->dev = dev; 186 ap->base = base; 187 ap->size = size; 188 ap->detach = detach; 189 INIT_LIST_HEAD(&ap->lh); 190 191 list_add(&ap->lh, &drm_apertures); 192 193 mutex_unlock(&drm_apertures_lock); 194 195 return devm_add_action_or_reset(dev->dev, devm_aperture_acquire_release, ap); 196 } 197 198 static void drm_aperture_detach_firmware(struct drm_device *dev) 199 { 200 struct platform_device *pdev = to_platform_device(dev->dev); 201 202 /* 203 * Remove the device from the device hierarchy. This is the right thing 204 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After 205 * the new driver takes over the hardware, the firmware device's state 206 * will be lost. 207 * 208 * For non-platform devices, a new callback would be required. 209 * 210 * If the aperture helpers ever need to handle native drivers, this call 211 * would only have to unplug the DRM device, so that the hardware device 212 * stays around after detachment. 213 */ 214 platform_device_unregister(pdev); 215 } 216 217 #endif /* notyet */ 218 219 /** 220 * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer 221 * on behalf of a DRM driver. 222 * @dev: the DRM device to own the framebuffer memory 223 * @base: the framebuffer's byte offset in physical memory 224 * @size: the framebuffer size in bytes 225 * 226 * Installs the given device as the new owner of the framebuffer. The function 227 * expects the framebuffer to be provided by a platform device that has been 228 * set up by firmware. Firmware can be any generic interface, such as EFI, 229 * VESA, VGA, etc. If the native hardware driver takes over ownership of the 230 * framebuffer range, the firmware state gets lost. Aperture helpers will then 231 * unregister the platform device automatically. Acquired apertures are 232 * released automatically if the underlying device goes away. 233 * 234 * The function fails if the framebuffer range, or parts of it, is currently 235 * owned by another driver. To evict current owners, callers should use 236 * drm_aperture_remove_conflicting_framebuffers() et al. before calling this 237 * function. The function also fails if the given device is not a platform 238 * device. 239 * 240 * Returns: 241 * 0 on success, or a negative errno value otherwise. 242 */ 243 int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base, 244 resource_size_t size) 245 { 246 STUB(); 247 return -ENOSYS; 248 #ifdef notyet 249 if (drm_WARN_ON(dev, !dev_is_platform(dev->dev))) 250 return -EINVAL; 251 252 return devm_aperture_acquire(dev, base, size, drm_aperture_detach_firmware); 253 #endif 254 } 255 EXPORT_SYMBOL(devm_aperture_acquire_from_firmware); 256 257 static void drm_aperture_detach_drivers(resource_size_t base, resource_size_t size) 258 { 259 resource_size_t end = base + size; 260 struct list_head *pos, *n; 261 262 mutex_lock(&drm_apertures_lock); 263 264 list_for_each_safe(pos, n, &drm_apertures) { 265 struct drm_aperture *ap = 266 container_of(pos, struct drm_aperture, lh); 267 struct drm_device *dev = ap->dev; 268 269 if (WARN_ON_ONCE(!dev)) 270 continue; 271 272 if (!overlap(base, end, ap->base, ap->base + ap->size)) 273 continue; 274 275 ap->dev = NULL; /* detach from device */ 276 list_del(&ap->lh); 277 278 ap->detach(dev); 279 } 280 281 mutex_unlock(&drm_apertures_lock); 282 } 283 284 /** 285 * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range 286 * @base: the aperture's base address in physical memory 287 * @size: aperture size in bytes 288 * @primary: also kick vga16fb if present 289 * @req_driver: requesting DRM driver 290 * 291 * This function removes graphics device drivers which use memory range described by 292 * @base and @size. 293 * 294 * Returns: 295 * 0 on success, or a negative errno code otherwise 296 */ 297 int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size, 298 bool primary, const struct drm_driver *req_driver) 299 { 300 #if IS_REACHABLE(CONFIG_FB) 301 struct apertures_struct *a; 302 int ret; 303 304 a = alloc_apertures(1); 305 if (!a) 306 return -ENOMEM; 307 308 a->ranges[0].base = base; 309 a->ranges[0].size = size; 310 311 ret = remove_conflicting_framebuffers(a, req_driver->name, primary); 312 kfree(a); 313 314 if (ret) 315 return ret; 316 #endif 317 318 drm_aperture_detach_drivers(base, size); 319 320 return 0; 321 } 322 EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers); 323 324 /** 325 * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices 326 * @pdev: PCI device 327 * @req_driver: requesting DRM driver 328 * 329 * This function removes graphics device drivers using memory range configured 330 * for any of @pdev's memory bars. The function assumes that PCI device with 331 * shadowed ROM drives a primary display and so kicks out vga16fb. 332 * 333 * Returns: 334 * 0 on success, or a negative errno code otherwise 335 */ 336 int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, 337 const struct drm_driver *req_driver) 338 { 339 return 0; 340 #ifdef notyet 341 resource_size_t base, size; 342 int bar, ret; 343 344 /* 345 * WARNING: Apparently we must kick fbdev drivers before vgacon, 346 * otherwise the vga fbdev driver falls over. 347 */ 348 #if IS_REACHABLE(CONFIG_FB) 349 ret = remove_conflicting_pci_framebuffers(pdev, req_driver->name); 350 if (ret) 351 return ret; 352 #endif 353 ret = vga_remove_vgacon(pdev); 354 if (ret) 355 return ret; 356 357 for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) { 358 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) 359 continue; 360 base = pci_resource_start(pdev, bar); 361 size = pci_resource_len(pdev, bar); 362 drm_aperture_detach_drivers(base, size); 363 } 364 365 return 0; 366 #endif 367 } 368 EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers); 369