1 // SPDX-License-Identifier: MIT
3 #include <linux/device.h>
5 #include <linux/list.h>
6 #include <linux/mutex.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>
13 #include <drm/drm_aperture.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_print.h>
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.
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.
36 * static const struct drm_driver example_driver = {
40 * static int remove_conflicting_framebuffers(struct pci_dev *pdev)
42 * bool primary = false;
43 * resource_size_t base, size;
46 * base = pci_resource_start(pdev, 0);
47 * size = pci_resource_len(pdev, 0);
49 * primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
52 * return drm_aperture_remove_conflicting_framebuffers(base, size, primary,
56 * static int probe(struct pci_dev *pdev)
60 * // Remove any generic drivers...
61 * ret = remove_conflicting_framebuffers(pdev);
65 * // ... and initialize the hardware.
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.
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.
88 * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
90 * resource_size_t base, size;
92 * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
96 * size = resource_size(mem);
98 * return devm_acquire_aperture_from_firmware(dev, base, size);
101 * static int probe(struct platform_device *pdev)
103 * struct drm_device *dev;
106 * // ... Initialize the device...
107 * dev = devm_drm_dev_alloc();
110 * // ... and acquire ownership of the framebuffer.
111 * ret = acquire_framebuffers(dev, pdev);
115 * drm_dev_register(dev, 0);
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
129 struct drm_aperture {
130 struct drm_device *dev;
131 resource_size_t base;
132 resource_size_t size;
134 void (*detach)(struct drm_device *dev);
137 static LIST_HEAD(drm_apertures);
138 static DEFINE_MUTEX(drm_apertures_lock);
140 static bool overlap(resource_size_t base1, resource_size_t end1,
141 resource_size_t base2, resource_size_t end2)
143 return (base1 < end2) && (end1 > base2);
146 static void devm_aperture_acquire_release(void *data)
148 struct drm_aperture *ap = data;
149 bool detached = !ap->dev;
154 mutex_lock(&drm_apertures_lock);
156 mutex_unlock(&drm_apertures_lock);
159 static int devm_aperture_acquire(struct drm_device *dev,
160 resource_size_t base, resource_size_t size,
161 void (*detach)(struct drm_device *))
163 size_t end = base + size;
164 struct list_head *pos;
165 struct drm_aperture *ap;
167 mutex_lock(&drm_apertures_lock);
169 list_for_each(pos, &drm_apertures) {
170 ap = container_of(pos, struct drm_aperture, lh);
171 if (overlap(base, end, ap->base, ap->base + ap->size)) {
172 mutex_unlock(&drm_apertures_lock);
177 ap = devm_kzalloc(dev->dev, sizeof(*ap), GFP_KERNEL);
179 mutex_unlock(&drm_apertures_lock);
187 INIT_LIST_HEAD(&ap->lh);
189 list_add(&ap->lh, &drm_apertures);
191 mutex_unlock(&drm_apertures_lock);
193 return devm_add_action_or_reset(dev->dev, devm_aperture_acquire_release, ap);
196 static void drm_aperture_detach_firmware(struct drm_device *dev)
198 struct platform_device *pdev = to_platform_device(dev->dev);
201 * Remove the device from the device hierarchy. This is the right thing
202 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
203 * the new driver takes over the hardware, the firmware device's state
206 * For non-platform devices, a new callback would be required.
208 * If the aperture helpers ever need to handle native drivers, this call
209 * would only have to unplug the DRM device, so that the hardware device
210 * stays around after detachment.
212 platform_device_unregister(pdev);
216 * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
217 * on behalf of a DRM driver.
218 * @dev: the DRM device to own the framebuffer memory
219 * @base: the framebuffer's byte offset in physical memory
220 * @size: the framebuffer size in bytes
222 * Installs the given device as the new owner of the framebuffer. The function
223 * expects the framebuffer to be provided by a platform device that has been
224 * set up by firmware. Firmware can be any generic interface, such as EFI,
225 * VESA, VGA, etc. If the native hardware driver takes over ownership of the
226 * framebuffer range, the firmware state gets lost. Aperture helpers will then
227 * unregister the platform device automatically. Acquired apertures are
228 * released automatically if the underlying device goes away.
230 * The function fails if the framebuffer range, or parts of it, is currently
231 * owned by another driver. To evict current owners, callers should use
232 * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
233 * function. The function also fails if the given device is not a platform
237 * 0 on success, or a negative errno value otherwise.
239 int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
240 resource_size_t size)
242 if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
245 return devm_aperture_acquire(dev, base, size, drm_aperture_detach_firmware);
247 EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
249 static void drm_aperture_detach_drivers(resource_size_t base, resource_size_t size)
251 resource_size_t end = base + size;
252 struct list_head *pos, *n;
254 mutex_lock(&drm_apertures_lock);
256 list_for_each_safe(pos, n, &drm_apertures) {
257 struct drm_aperture *ap =
258 container_of(pos, struct drm_aperture, lh);
259 struct drm_device *dev = ap->dev;
261 if (WARN_ON_ONCE(!dev))
264 if (!overlap(base, end, ap->base, ap->base + ap->size))
267 ap->dev = NULL; /* detach from device */
273 mutex_unlock(&drm_apertures_lock);
277 * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
278 * @base: the aperture's base address in physical memory
279 * @size: aperture size in bytes
280 * @primary: also kick vga16fb if present
281 * @req_driver: requesting DRM driver
283 * This function removes graphics device drivers which use memory range described by
287 * 0 on success, or a negative errno code otherwise
289 int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
290 bool primary, const struct drm_driver *req_driver)
292 #if IS_REACHABLE(CONFIG_FB)
293 struct apertures_struct *a;
296 a = alloc_apertures(1);
300 a->ranges[0].base = base;
301 a->ranges[0].size = size;
303 ret = remove_conflicting_framebuffers(a, req_driver->name, primary);
310 drm_aperture_detach_drivers(base, size);
314 EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
317 * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
319 * @req_driver: requesting DRM driver
321 * This function removes graphics device drivers using memory range configured
322 * for any of @pdev's memory bars. The function assumes that PCI device with
323 * shadowed ROM drives a primary display and so kicks out vga16fb.
326 * 0 on success, or a negative errno code otherwise
328 int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
329 const struct drm_driver *req_driver)
331 resource_size_t base, size;
335 * WARNING: Apparently we must kick fbdev drivers before vgacon,
336 * otherwise the vga fbdev driver falls over.
338 #if IS_REACHABLE(CONFIG_FB)
339 ret = remove_conflicting_pci_framebuffers(pdev, req_driver->name);
343 ret = vga_remove_vgacon(pdev);
347 for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
348 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
350 base = pci_resource_start(pdev, bar);
351 size = pci_resource_len(pdev, bar);
352 drm_aperture_detach_drivers(base, size);
357 EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);