1 // SPDX-License-Identifier: MIT
3 #include <linux/aperture.h>
4 #include <linux/device.h>
5 #include <linux/fb.h> /* for old fbdev helpers */
6 #include <linux/list.h>
7 #include <linux/mutex.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/sysfb.h>
12 #include <linux/types.h>
13 #include <linux/vgaarb.h>
15 #include <video/vga.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. Aperture functions manage
26 * ownership of framebuffer memory and hand-over between drivers.
28 * Graphics drivers should call aperture_remove_conflicting_devices()
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 * An example for a graphics device on the platform bus is shown below.
35 * static int example_probe(struct platform_device *pdev)
37 * struct resource *mem;
38 * resource_size_t base, size;
41 * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
45 * size = resource_size(mem);
47 * ret = aperture_remove_conflicting_devices(base, size, false, "example");
51 * // Initialize the hardware
57 * static const struct platform_driver example_driver = {
58 * .probe = example_probe,
62 * The given example reads the platform device's I/O-memory range from the
63 * device instance. An active framebuffer will be located within this range.
64 * The call to aperture_remove_conflicting_devices() releases drivers that
65 * have previously claimed ownership of the range and are currently driving
66 * output on the framebuffer. If successful, the new driver can take over
69 * While the given example uses a platform device, the aperture helpers work
70 * with every bus that has an addressable framebuffer. In the case of PCI,
71 * device drivers can also call aperture_remove_conflicting_pci_devices() and
72 * let the function detect the apertures automatically. Device drivers without
73 * knowledge of the framebuffer's location can call
74 * aperture_remove_all_conflicting_devices(), which removes all known devices.
76 * Drivers that are susceptible to being removed by other drivers, such as
77 * generic EFI or VESA drivers, have to register themselves as owners of their
78 * framebuffer apertures. Ownership of the framebuffer memory is achieved
79 * by calling devm_aperture_acquire_for_platform_device(). If successful, the
80 * driveris the owner of the framebuffer range. The function fails if the
81 * framebuffer is already owned by another driver. See below for an example.
85 * static int generic_probe(struct platform_device *pdev)
87 * struct resource *mem;
88 * resource_size_t base, size;
90 * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 * size = resource_size(mem);
96 * ret = devm_aperture_acquire_for_platform_device(pdev, base, size);
100 * // Initialize the hardware
106 * static int generic_remove(struct platform_device *)
108 * // Hot-unplug the device
114 * static const struct platform_driver generic_driver = {
115 * .probe = generic_probe,
116 * .remove = generic_remove,
120 * The similar to the previous example, the generic driver claims ownership
121 * of the framebuffer memory from its probe function. This will fail if the
122 * memory range, or parts of it, is already owned by another driver.
124 * If successful, the generic driver is now subject to forced removal by
125 * another driver. This only works for platform drivers that support hot
126 * unplugging. When a driver calls aperture_remove_conflicting_devices()
127 * et al for the registered framebuffer range, the aperture helpers call
128 * platform_device_unregister() and the generic driver unloads itself. The
129 * generic driver also has to provide a remove function to make this work.
130 * Once hot unplugged fro mhardware, it may not access the device's
131 * registers, framebuffer memory, ROM, etc afterwards.
134 struct aperture_range {
136 resource_size_t base;
137 resource_size_t size;
139 void (*detach)(struct device *dev);
142 static LIST_HEAD(apertures);
143 static DEFINE_MUTEX(apertures_lock);
145 static bool overlap(resource_size_t base1, resource_size_t end1,
146 resource_size_t base2, resource_size_t end2)
148 return (base1 < end2) && (end1 > base2);
151 static void devm_aperture_acquire_release(void *data)
153 struct aperture_range *ap = data;
154 bool detached = !ap->dev;
159 mutex_lock(&apertures_lock);
161 mutex_unlock(&apertures_lock);
164 static int devm_aperture_acquire(struct device *dev,
165 resource_size_t base, resource_size_t size,
166 void (*detach)(struct device *))
168 size_t end = base + size;
169 struct list_head *pos;
170 struct aperture_range *ap;
172 mutex_lock(&apertures_lock);
174 list_for_each(pos, &apertures) {
175 ap = container_of(pos, struct aperture_range, lh);
176 if (overlap(base, end, ap->base, ap->base + ap->size)) {
177 mutex_unlock(&apertures_lock);
182 ap = devm_kzalloc(dev, sizeof(*ap), GFP_KERNEL);
184 mutex_unlock(&apertures_lock);
192 INIT_LIST_HEAD(&ap->lh);
194 list_add(&ap->lh, &apertures);
196 mutex_unlock(&apertures_lock);
198 return devm_add_action_or_reset(dev, devm_aperture_acquire_release, ap);
201 static void aperture_detach_platform_device(struct device *dev)
203 struct platform_device *pdev = to_platform_device(dev);
206 * Remove the device from the device hierarchy. This is the right thing
207 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
208 * the new driver takes over the hardware, the firmware device's state
211 * For non-platform devices, a new callback would be required.
213 * If the aperture helpers ever need to handle native drivers, this call
214 * would only have to unplug the DRM device, so that the hardware device
215 * stays around after detachment.
217 platform_device_unregister(pdev);
221 * devm_aperture_acquire_for_platform_device - Acquires ownership of an aperture
222 * on behalf of a platform device.
223 * @pdev: the platform device to own the aperture
224 * @base: the aperture's byte offset in physical memory
225 * @size: the aperture size in bytes
227 * Installs the given device as the new owner of the aperture. The function
228 * expects the aperture to be provided by a platform device. If another
229 * driver takes over ownership of the aperture, aperture helpers will then
230 * unregister the platform device automatically. All acquired apertures are
231 * released automatically when the underlying device goes away.
233 * The function fails if the aperture, or parts of it, is currently
234 * owned by another device. To evict current owners, callers should use
235 * remove_conflicting_devices() et al. before calling this function.
238 * 0 on success, or a negative errno value otherwise.
240 int devm_aperture_acquire_for_platform_device(struct platform_device *pdev,
241 resource_size_t base,
242 resource_size_t size)
244 return devm_aperture_acquire(&pdev->dev, base, size, aperture_detach_platform_device);
246 EXPORT_SYMBOL(devm_aperture_acquire_for_platform_device);
248 static void aperture_detach_devices(resource_size_t base, resource_size_t size)
250 resource_size_t end = base + size;
251 struct list_head *pos, *n;
253 mutex_lock(&apertures_lock);
255 list_for_each_safe(pos, n, &apertures) {
256 struct aperture_range *ap = container_of(pos, struct aperture_range, lh);
257 struct device *dev = ap->dev;
259 if (WARN_ON_ONCE(!dev))
262 if (!overlap(base, end, ap->base, ap->base + ap->size))
265 ap->dev = NULL; /* detach from device */
271 mutex_unlock(&apertures_lock);
275 * aperture_remove_conflicting_devices - remove devices in the given range
276 * @base: the aperture's base address in physical memory
277 * @size: aperture size in bytes
278 * @primary: also kick vga16fb if present; only relevant for VGA devices
279 * @name: a descriptive name of the requesting driver
281 * This function removes devices that own apertures within @base and @size.
284 * 0 on success, or a negative errno code otherwise
286 int aperture_remove_conflicting_devices(resource_size_t base, resource_size_t size,
287 bool primary, const char *name)
289 #if IS_REACHABLE(CONFIG_FB)
290 struct apertures_struct *a;
295 * If a driver asked to unregister a platform device registered by
296 * sysfb, then can be assumed that this is a driver for a display
297 * that is set up by the system firmware and has a generic driver.
299 * Drivers for devices that don't have a generic driver will never
300 * ask for this, so let's assume that a real driver for the display
301 * was already probed and prevent sysfb to register devices later.
305 aperture_detach_devices(base, size);
308 * If this is the primary adapter, there could be a VGA device
309 * that consumes the VGA framebuffer I/O range. Remove this device
313 aperture_detach_devices(VGA_FB_PHYS_BASE, VGA_FB_PHYS_SIZE);
315 #if IS_REACHABLE(CONFIG_FB)
316 a = alloc_apertures(1);
320 a->ranges[0].base = base;
321 a->ranges[0].size = size;
323 ret = remove_conflicting_framebuffers(a, name, primary);
332 EXPORT_SYMBOL(aperture_remove_conflicting_devices);
335 * aperture_remove_conflicting_pci_devices - remove existing framebuffers for PCI devices
337 * @name: a descriptive name of the requesting driver
339 * This function removes devices that own apertures within any of @pdev's
340 * memory bars. The function assumes that PCI device with shadowed ROM
341 * drives a primary display and therefore kicks out vga16fb as well.
344 * 0 on success, or a negative errno code otherwise
346 int aperture_remove_conflicting_pci_devices(struct pci_dev *pdev, const char *name)
348 bool primary = false;
349 resource_size_t base, size;
353 primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
356 for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
357 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
360 base = pci_resource_start(pdev, bar);
361 size = pci_resource_len(pdev, bar);
362 ret = aperture_remove_conflicting_devices(base, size, primary, name);
371 * WARNING: Apparently we must kick fbdev drivers before vgacon,
372 * otherwise the vga fbdev driver falls over.
374 ret = vga_remove_vgacon(pdev);
381 EXPORT_SYMBOL(aperture_remove_conflicting_pci_devices);