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 int remove_conflicting_framebuffers(struct pci_dev *pdev)
38 * bool primary = false;
39 * resource_size_t base, size;
42 * base = pci_resource_start(pdev, 0);
43 * size = pci_resource_len(pdev, 0);
45 * primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
48 * return drm_aperture_remove_conflicting_framebuffers(base, size, primary,
52 * static int probe(struct pci_dev *pdev)
56 * // Remove any generic drivers...
57 * ret = remove_conflicting_framebuffers(pdev);
61 * // ... and initialize the hardware.
69 * PCI device drivers should call
70 * drm_aperture_remove_conflicting_pci_framebuffers() and let it detect the
71 * framebuffer apertures automatically. Device drivers without knowledge of
72 * the framebuffer's location shall call drm_aperture_remove_framebuffers(),
73 * which removes all drivers for known framebuffer.
75 * Drivers that are susceptible to being removed by other drivers, such as
76 * generic EFI or VESA drivers, have to register themselves as owners of their
77 * given framebuffer memory. Ownership of the framebuffer memory is achived
78 * by calling devm_aperture_acquire_from_firmware(). On success, the driver
79 * is the owner of the framebuffer range. The function fails if the
80 * framebuffer is already by another driver. See below for an example.
84 * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
86 * resource_size_t base, size;
88 * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
92 * size = resource_size(mem);
94 * return devm_acquire_aperture_from_firmware(dev, base, size);
97 * static int probe(struct platform_device *pdev)
99 * struct drm_device *dev;
102 * // ... Initialize the device...
103 * dev = devm_drm_dev_alloc();
106 * // ... and acquire ownership of the framebuffer.
107 * ret = acquire_framebuffers(dev, pdev);
111 * drm_dev_register(dev, 0);
116 * The generic driver is now subject to forced removal by other drivers. This
117 * only works for platform drivers that support hot unplug.
118 * When a driver calls drm_aperture_remove_conflicting_framebuffers() et al
119 * for the registered framebuffer range, the aperture helpers call
120 * platform_device_unregister() and the generic driver unloads itself. It
121 * may not access the device's registers, framebuffer memory, ROM, etc
125 struct drm_aperture {
126 struct drm_device *dev;
127 resource_size_t base;
128 resource_size_t size;
130 void (*detach)(struct drm_device *dev);
133 static LIST_HEAD(drm_apertures);
134 static DEFINE_MUTEX(drm_apertures_lock);
136 static bool overlap(resource_size_t base1, resource_size_t end1,
137 resource_size_t base2, resource_size_t end2)
139 return (base1 < end2) && (end1 > base2);
142 static void devm_aperture_acquire_release(void *data)
144 struct drm_aperture *ap = data;
145 bool detached = !ap->dev;
150 mutex_lock(&drm_apertures_lock);
152 mutex_unlock(&drm_apertures_lock);
155 static int devm_aperture_acquire(struct drm_device *dev,
156 resource_size_t base, resource_size_t size,
157 void (*detach)(struct drm_device *))
159 size_t end = base + size;
160 struct list_head *pos;
161 struct drm_aperture *ap;
163 mutex_lock(&drm_apertures_lock);
165 list_for_each(pos, &drm_apertures) {
166 ap = container_of(pos, struct drm_aperture, lh);
167 if (overlap(base, end, ap->base, ap->base + ap->size)) {
168 mutex_unlock(&drm_apertures_lock);
173 ap = devm_kzalloc(dev->dev, sizeof(*ap), GFP_KERNEL);
175 mutex_unlock(&drm_apertures_lock);
183 INIT_LIST_HEAD(&ap->lh);
185 list_add(&ap->lh, &drm_apertures);
187 mutex_unlock(&drm_apertures_lock);
189 return devm_add_action_or_reset(dev->dev, devm_aperture_acquire_release, ap);
192 static void drm_aperture_detach_firmware(struct drm_device *dev)
194 struct platform_device *pdev = to_platform_device(dev->dev);
197 * Remove the device from the device hierarchy. This is the right thing
198 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
199 * the new driver takes over the hardware, the firmware device's state
202 * For non-platform devices, a new callback would be required.
204 * If the aperture helpers ever need to handle native drivers, this call
205 * would only have to unplug the DRM device, so that the hardware device
206 * stays around after detachment.
208 platform_device_unregister(pdev);
212 * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
213 * on behalf of a DRM driver.
214 * @dev: the DRM device to own the framebuffer memory
215 * @base: the framebuffer's byte offset in physical memory
216 * @size: the framebuffer size in bytes
218 * Installs the given device as the new owner of the framebuffer. The function
219 * expects the framebuffer to be provided by a platform device that has been
220 * set up by firmware. Firmware can be any generic interface, such as EFI,
221 * VESA, VGA, etc. If the native hardware driver takes over ownership of the
222 * framebuffer range, the firmware state gets lost. Aperture helpers will then
223 * unregister the platform device automatically. Acquired apertures are
224 * released automatically if the underlying device goes away.
226 * The function fails if the framebuffer range, or parts of it, is currently
227 * owned by another driver. To evict current owners, callers should use
228 * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
229 * function. The function also fails if the given device is not a platform
233 * 0 on success, or a negative errno value otherwise.
235 int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
236 resource_size_t size)
238 if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
241 return devm_aperture_acquire(dev, base, size, drm_aperture_detach_firmware);
243 EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
245 static void drm_aperture_detach_drivers(resource_size_t base, resource_size_t size)
247 resource_size_t end = base + size;
248 struct list_head *pos, *n;
250 mutex_lock(&drm_apertures_lock);
252 list_for_each_safe(pos, n, &drm_apertures) {
253 struct drm_aperture *ap =
254 container_of(pos, struct drm_aperture, lh);
255 struct drm_device *dev = ap->dev;
257 if (WARN_ON_ONCE(!dev))
260 if (!overlap(base, end, ap->base, ap->base + ap->size))
263 ap->dev = NULL; /* detach from device */
269 mutex_unlock(&drm_apertures_lock);
273 * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
274 * @base: the aperture's base address in physical memory
275 * @size: aperture size in bytes
276 * @primary: also kick vga16fb if present
277 * @name: requesting driver name
279 * This function removes graphics device drivers which use memory range described by
283 * 0 on success, or a negative errno code otherwise
285 int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
286 bool primary, const char *name)
288 #if IS_REACHABLE(CONFIG_FB)
289 struct apertures_struct *a;
292 a = alloc_apertures(1);
296 a->ranges[0].base = base;
297 a->ranges[0].size = size;
299 ret = remove_conflicting_framebuffers(a, name, primary);
306 drm_aperture_detach_drivers(base, size);
310 EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
313 * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
315 * @name: requesting driver name
317 * This function removes graphics device drivers using memory range configured
318 * for any of @pdev's memory bars. The function assumes that PCI device with
319 * shadowed ROM drives a primary display and so kicks out vga16fb.
322 * 0 on success, or a negative errno code otherwise
324 int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, const char *name)
326 resource_size_t base, size;
329 for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
330 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
332 base = pci_resource_start(pdev, bar);
333 size = pci_resource_len(pdev, bar);
334 drm_aperture_detach_drivers(base, size);
338 * WARNING: Apparently we must kick fbdev drivers before vgacon,
339 * otherwise the vga fbdev driver falls over.
341 #if IS_REACHABLE(CONFIG_FB)
342 ret = remove_conflicting_pci_framebuffers(pdev, name);
345 ret = vga_remove_vgacon(pdev);
348 EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);