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))
171 ap = devm_kzalloc(dev->dev, sizeof(*ap), GFP_KERNEL);
179 INIT_LIST_HEAD(&ap->lh);
181 list_add(&ap->lh, &drm_apertures);
183 mutex_unlock(&drm_apertures_lock);
185 return devm_add_action_or_reset(dev->dev, devm_aperture_acquire_release, ap);
188 static void drm_aperture_detach_firmware(struct drm_device *dev)
190 struct platform_device *pdev = to_platform_device(dev->dev);
193 * Remove the device from the device hierarchy. This is the right thing
194 * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
195 * the new driver takes over the hardware, the firmware device's state
198 * For non-platform devices, a new callback would be required.
200 * If the aperture helpers ever need to handle native drivers, this call
201 * would only have to unplug the DRM device, so that the hardware device
202 * stays around after detachment.
204 platform_device_unregister(pdev);
208 * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
209 * on behalf of a DRM driver.
210 * @dev: the DRM device to own the framebuffer memory
211 * @base: the framebuffer's byte offset in physical memory
212 * @size: the framebuffer size in bytes
214 * Installs the given device as the new owner of the framebuffer. The function
215 * expects the framebuffer to be provided by a platform device that has been
216 * set up by firmware. Firmware can be any generic interface, such as EFI,
217 * VESA, VGA, etc. If the native hardware driver takes over ownership of the
218 * framebuffer range, the firmware state gets lost. Aperture helpers will then
219 * unregister the platform device automatically. Acquired apertures are
220 * released automatically if the underlying device goes away.
222 * The function fails if the framebuffer range, or parts of it, is currently
223 * owned by another driver. To evict current owners, callers should use
224 * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
225 * function. The function also fails if the given device is not a platform
229 * 0 on success, or a negative errno value otherwise.
231 int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
232 resource_size_t size)
234 if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
237 return devm_aperture_acquire(dev, base, size, drm_aperture_detach_firmware);
239 EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
241 static void drm_aperture_detach_drivers(resource_size_t base, resource_size_t size)
243 resource_size_t end = base + size;
244 struct list_head *pos, *n;
246 mutex_lock(&drm_apertures_lock);
248 list_for_each_safe(pos, n, &drm_apertures) {
249 struct drm_aperture *ap =
250 container_of(pos, struct drm_aperture, lh);
251 struct drm_device *dev = ap->dev;
253 if (WARN_ON_ONCE(!dev))
256 if (!overlap(base, end, ap->base, ap->base + ap->size))
259 ap->dev = NULL; /* detach from device */
265 mutex_unlock(&drm_apertures_lock);
269 * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
270 * @base: the aperture's base address in physical memory
271 * @size: aperture size in bytes
272 * @primary: also kick vga16fb if present
273 * @name: requesting driver name
275 * This function removes graphics device drivers which use memory range described by
279 * 0 on success, or a negative errno code otherwise
281 int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
282 bool primary, const char *name)
284 #if IS_REACHABLE(CONFIG_FB)
285 struct apertures_struct *a;
288 a = alloc_apertures(1);
292 a->ranges[0].base = base;
293 a->ranges[0].size = size;
295 ret = remove_conflicting_framebuffers(a, name, primary);
302 drm_aperture_detach_drivers(base, size);
306 EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
309 * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
311 * @name: requesting driver name
313 * This function removes graphics device drivers using memory range configured
314 * for any of @pdev's memory bars. The function assumes that PCI device with
315 * shadowed ROM drives a primary display and so kicks out vga16fb.
318 * 0 on success, or a negative errno code otherwise
320 int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, const char *name)
322 resource_size_t base, size;
325 for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
326 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
328 base = pci_resource_start(pdev, bar);
329 size = pci_resource_len(pdev, bar);
330 drm_aperture_detach_drivers(base, size);
334 * WARNING: Apparently we must kick fbdev drivers before vgacon,
335 * otherwise the vga fbdev driver falls over.
337 #if IS_REACHABLE(CONFIG_FB)
338 ret = remove_conflicting_pci_framebuffers(pdev, name);
341 ret = vga_remove_vgacon(pdev);
344 EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);