xref: /freebsd-src/sys/compat/linuxkpi/common/src/linux_aperture.c (revision c89d94ad5d95fd15e891b2723caae8a6104ee153)
111d79c47SEmmanuel Vadot // SPDX-License-Identifier: MIT
211d79c47SEmmanuel Vadot 
311d79c47SEmmanuel Vadot #include <linux/aperture.h>
411d79c47SEmmanuel Vadot #include <linux/device.h>
511d79c47SEmmanuel Vadot #include <linux/list.h>
611d79c47SEmmanuel Vadot #include <linux/mutex.h>
711d79c47SEmmanuel Vadot #include <linux/pci.h>
811d79c47SEmmanuel Vadot #include <linux/platform_device.h>
911d79c47SEmmanuel Vadot #include <linux/slab.h>
1011d79c47SEmmanuel Vadot #include <linux/sysfb.h>
1111d79c47SEmmanuel Vadot #include <linux/types.h>
1211d79c47SEmmanuel Vadot #include <linux/vgaarb.h>
1311d79c47SEmmanuel Vadot 
1411d79c47SEmmanuel Vadot #include <video/vga.h>
1511d79c47SEmmanuel Vadot 
1611d79c47SEmmanuel Vadot /**
1711d79c47SEmmanuel Vadot  * DOC: overview
1811d79c47SEmmanuel Vadot  *
1911d79c47SEmmanuel Vadot  * A graphics device might be supported by different drivers, but only one
2011d79c47SEmmanuel Vadot  * driver can be active at any given time. Many systems load a generic
2111d79c47SEmmanuel Vadot  * graphics drivers, such as EFI-GOP or VESA, early during the boot process.
2211d79c47SEmmanuel Vadot  * During later boot stages, they replace the generic driver with a dedicated,
23*c89d94adSVladimir Kondratyev  * hardware-specific driver. To take over the device, the dedicated driver
2411d79c47SEmmanuel Vadot  * first has to remove the generic driver. Aperture functions manage
2511d79c47SEmmanuel Vadot  * ownership of framebuffer memory and hand-over between drivers.
2611d79c47SEmmanuel Vadot  *
2711d79c47SEmmanuel Vadot  * Graphics drivers should call aperture_remove_conflicting_devices()
2811d79c47SEmmanuel Vadot  * at the top of their probe function. The function removes any generic
2911d79c47SEmmanuel Vadot  * driver that is currently associated with the given framebuffer memory.
3011d79c47SEmmanuel Vadot  * An example for a graphics device on the platform bus is shown below.
3111d79c47SEmmanuel Vadot  *
3211d79c47SEmmanuel Vadot  * .. code-block:: c
3311d79c47SEmmanuel Vadot  *
3411d79c47SEmmanuel Vadot  *	static int example_probe(struct platform_device *pdev)
3511d79c47SEmmanuel Vadot  *	{
3611d79c47SEmmanuel Vadot  *		struct resource *mem;
3711d79c47SEmmanuel Vadot  *		resource_size_t base, size;
3811d79c47SEmmanuel Vadot  *		int ret;
3911d79c47SEmmanuel Vadot  *
4011d79c47SEmmanuel Vadot  *		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4111d79c47SEmmanuel Vadot  *		if (!mem)
4211d79c47SEmmanuel Vadot  *			return -ENODEV;
4311d79c47SEmmanuel Vadot  *		base = mem->start;
4411d79c47SEmmanuel Vadot  *		size = resource_size(mem);
4511d79c47SEmmanuel Vadot  *
46*c89d94adSVladimir Kondratyev  *		ret = aperture_remove_conflicting_devices(base, size, "example");
4711d79c47SEmmanuel Vadot  *		if (ret)
4811d79c47SEmmanuel Vadot  *			return ret;
4911d79c47SEmmanuel Vadot  *
5011d79c47SEmmanuel Vadot  *		// Initialize the hardware
5111d79c47SEmmanuel Vadot  *		...
5211d79c47SEmmanuel Vadot  *
5311d79c47SEmmanuel Vadot  *		return 0;
5411d79c47SEmmanuel Vadot  *	}
5511d79c47SEmmanuel Vadot  *
5611d79c47SEmmanuel Vadot  *	static const struct platform_driver example_driver = {
5711d79c47SEmmanuel Vadot  *		.probe = example_probe,
5811d79c47SEmmanuel Vadot  *		...
5911d79c47SEmmanuel Vadot  *	};
6011d79c47SEmmanuel Vadot  *
6111d79c47SEmmanuel Vadot  * The given example reads the platform device's I/O-memory range from the
6211d79c47SEmmanuel Vadot  * device instance. An active framebuffer will be located within this range.
6311d79c47SEmmanuel Vadot  * The call to aperture_remove_conflicting_devices() releases drivers that
6411d79c47SEmmanuel Vadot  * have previously claimed ownership of the range and are currently driving
6511d79c47SEmmanuel Vadot  * output on the framebuffer. If successful, the new driver can take over
6611d79c47SEmmanuel Vadot  * the device.
6711d79c47SEmmanuel Vadot  *
6811d79c47SEmmanuel Vadot  * While the given example uses a platform device, the aperture helpers work
6911d79c47SEmmanuel Vadot  * with every bus that has an addressable framebuffer. In the case of PCI,
7011d79c47SEmmanuel Vadot  * device drivers can also call aperture_remove_conflicting_pci_devices() and
7111d79c47SEmmanuel Vadot  * let the function detect the apertures automatically. Device drivers without
7211d79c47SEmmanuel Vadot  * knowledge of the framebuffer's location can call
7311d79c47SEmmanuel Vadot  * aperture_remove_all_conflicting_devices(), which removes all known devices.
7411d79c47SEmmanuel Vadot  *
7511d79c47SEmmanuel Vadot  * Drivers that are susceptible to being removed by other drivers, such as
7611d79c47SEmmanuel Vadot  * generic EFI or VESA drivers, have to register themselves as owners of their
7711d79c47SEmmanuel Vadot  * framebuffer apertures. Ownership of the framebuffer memory is achieved
7811d79c47SEmmanuel Vadot  * by calling devm_aperture_acquire_for_platform_device(). If successful, the
7911d79c47SEmmanuel Vadot  * driver is the owner of the framebuffer range. The function fails if the
8011d79c47SEmmanuel Vadot  * framebuffer is already owned by another driver. See below for an example.
8111d79c47SEmmanuel Vadot  *
8211d79c47SEmmanuel Vadot  * .. code-block:: c
8311d79c47SEmmanuel Vadot  *
8411d79c47SEmmanuel Vadot  *	static int generic_probe(struct platform_device *pdev)
8511d79c47SEmmanuel Vadot  *	{
8611d79c47SEmmanuel Vadot  *		struct resource *mem;
8711d79c47SEmmanuel Vadot  *		resource_size_t base, size;
8811d79c47SEmmanuel Vadot  *
8911d79c47SEmmanuel Vadot  *		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9011d79c47SEmmanuel Vadot  *		if (!mem)
9111d79c47SEmmanuel Vadot  *			return -ENODEV;
9211d79c47SEmmanuel Vadot  *		base = mem->start;
9311d79c47SEmmanuel Vadot  *		size = resource_size(mem);
9411d79c47SEmmanuel Vadot  *
9511d79c47SEmmanuel Vadot  *		ret = devm_aperture_acquire_for_platform_device(pdev, base, size);
9611d79c47SEmmanuel Vadot  *		if (ret)
9711d79c47SEmmanuel Vadot  *			return ret;
9811d79c47SEmmanuel Vadot  *
9911d79c47SEmmanuel Vadot  *		// Initialize the hardware
10011d79c47SEmmanuel Vadot  *		...
10111d79c47SEmmanuel Vadot  *
10211d79c47SEmmanuel Vadot  *		return 0;
10311d79c47SEmmanuel Vadot  *	}
10411d79c47SEmmanuel Vadot  *
10511d79c47SEmmanuel Vadot  *	static int generic_remove(struct platform_device *)
10611d79c47SEmmanuel Vadot  *	{
10711d79c47SEmmanuel Vadot  *		// Hot-unplug the device
10811d79c47SEmmanuel Vadot  *		...
10911d79c47SEmmanuel Vadot  *
11011d79c47SEmmanuel Vadot  *		return 0;
11111d79c47SEmmanuel Vadot  *	}
11211d79c47SEmmanuel Vadot  *
11311d79c47SEmmanuel Vadot  *	static const struct platform_driver generic_driver = {
11411d79c47SEmmanuel Vadot  *		.probe = generic_probe,
11511d79c47SEmmanuel Vadot  *		.remove = generic_remove,
11611d79c47SEmmanuel Vadot  *		...
11711d79c47SEmmanuel Vadot  *	};
11811d79c47SEmmanuel Vadot  *
11911d79c47SEmmanuel Vadot  * The similar to the previous example, the generic driver claims ownership
12011d79c47SEmmanuel Vadot  * of the framebuffer memory from its probe function. This will fail if the
12111d79c47SEmmanuel Vadot  * memory range, or parts of it, is already owned by another driver.
12211d79c47SEmmanuel Vadot  *
12311d79c47SEmmanuel Vadot  * If successful, the generic driver is now subject to forced removal by
12411d79c47SEmmanuel Vadot  * another driver. This only works for platform drivers that support hot
12511d79c47SEmmanuel Vadot  * unplugging. When a driver calls aperture_remove_conflicting_devices()
12611d79c47SEmmanuel Vadot  * et al for the registered framebuffer range, the aperture helpers call
12711d79c47SEmmanuel Vadot  * platform_device_unregister() and the generic driver unloads itself. The
12811d79c47SEmmanuel Vadot  * generic driver also has to provide a remove function to make this work.
12911d79c47SEmmanuel Vadot  * Once hot unplugged from hardware, it may not access the device's
13011d79c47SEmmanuel Vadot  * registers, framebuffer memory, ROM, etc afterwards.
13111d79c47SEmmanuel Vadot  */
13211d79c47SEmmanuel Vadot 
13311d79c47SEmmanuel Vadot struct aperture_range {
13411d79c47SEmmanuel Vadot 	struct device *dev;
13511d79c47SEmmanuel Vadot 	resource_size_t base;
13611d79c47SEmmanuel Vadot 	resource_size_t size;
13711d79c47SEmmanuel Vadot 	struct list_head lh;
13811d79c47SEmmanuel Vadot 	void (*detach)(struct device *dev);
13911d79c47SEmmanuel Vadot };
14011d79c47SEmmanuel Vadot 
14111d79c47SEmmanuel Vadot static LIST_HEAD(apertures);
14211d79c47SEmmanuel Vadot static DEFINE_MUTEX(apertures_lock);
14311d79c47SEmmanuel Vadot 
14411d79c47SEmmanuel Vadot static bool overlap(resource_size_t base1, resource_size_t end1,
14511d79c47SEmmanuel Vadot 		    resource_size_t base2, resource_size_t end2)
14611d79c47SEmmanuel Vadot {
14711d79c47SEmmanuel Vadot 	return (base1 < end2) && (end1 > base2);
14811d79c47SEmmanuel Vadot }
14911d79c47SEmmanuel Vadot 
15011d79c47SEmmanuel Vadot static void devm_aperture_acquire_release(void *data)
15111d79c47SEmmanuel Vadot {
15211d79c47SEmmanuel Vadot 	struct aperture_range *ap = data;
15311d79c47SEmmanuel Vadot 	bool detached = !ap->dev;
15411d79c47SEmmanuel Vadot 
15511d79c47SEmmanuel Vadot 	if (detached)
15611d79c47SEmmanuel Vadot 		return;
15711d79c47SEmmanuel Vadot 
15811d79c47SEmmanuel Vadot 	mutex_lock(&apertures_lock);
15911d79c47SEmmanuel Vadot 	list_del(&ap->lh);
16011d79c47SEmmanuel Vadot 	mutex_unlock(&apertures_lock);
16111d79c47SEmmanuel Vadot }
16211d79c47SEmmanuel Vadot 
16311d79c47SEmmanuel Vadot static int devm_aperture_acquire(struct device *dev,
16411d79c47SEmmanuel Vadot 				 resource_size_t base, resource_size_t size,
16511d79c47SEmmanuel Vadot 				 void (*detach)(struct device *))
16611d79c47SEmmanuel Vadot {
16711d79c47SEmmanuel Vadot 	size_t end = base + size;
16811d79c47SEmmanuel Vadot 	struct list_head *pos;
16911d79c47SEmmanuel Vadot 	struct aperture_range *ap;
17011d79c47SEmmanuel Vadot 
17111d79c47SEmmanuel Vadot 	mutex_lock(&apertures_lock);
17211d79c47SEmmanuel Vadot 
17311d79c47SEmmanuel Vadot 	list_for_each(pos, &apertures) {
17411d79c47SEmmanuel Vadot 		ap = container_of(pos, struct aperture_range, lh);
17511d79c47SEmmanuel Vadot 		if (overlap(base, end, ap->base, ap->base + ap->size)) {
17611d79c47SEmmanuel Vadot 			mutex_unlock(&apertures_lock);
17711d79c47SEmmanuel Vadot 			return -EBUSY;
17811d79c47SEmmanuel Vadot 		}
17911d79c47SEmmanuel Vadot 	}
18011d79c47SEmmanuel Vadot 
18111d79c47SEmmanuel Vadot 	ap = devm_kzalloc(dev, sizeof(*ap), GFP_KERNEL);
18211d79c47SEmmanuel Vadot 	if (!ap) {
18311d79c47SEmmanuel Vadot 		mutex_unlock(&apertures_lock);
18411d79c47SEmmanuel Vadot 		return -ENOMEM;
18511d79c47SEmmanuel Vadot 	}
18611d79c47SEmmanuel Vadot 
18711d79c47SEmmanuel Vadot 	ap->dev = dev;
18811d79c47SEmmanuel Vadot 	ap->base = base;
18911d79c47SEmmanuel Vadot 	ap->size = size;
19011d79c47SEmmanuel Vadot 	ap->detach = detach;
19111d79c47SEmmanuel Vadot 	INIT_LIST_HEAD(&ap->lh);
19211d79c47SEmmanuel Vadot 
19311d79c47SEmmanuel Vadot 	list_add(&ap->lh, &apertures);
19411d79c47SEmmanuel Vadot 
19511d79c47SEmmanuel Vadot 	mutex_unlock(&apertures_lock);
19611d79c47SEmmanuel Vadot 
19711d79c47SEmmanuel Vadot 	return devm_add_action_or_reset(dev, devm_aperture_acquire_release, ap);
19811d79c47SEmmanuel Vadot }
19911d79c47SEmmanuel Vadot 
20011d79c47SEmmanuel Vadot static void aperture_detach_platform_device(struct device *dev)
20111d79c47SEmmanuel Vadot {
20211d79c47SEmmanuel Vadot 	struct platform_device *pdev = to_platform_device(dev);
20311d79c47SEmmanuel Vadot 
20411d79c47SEmmanuel Vadot 	/*
20511d79c47SEmmanuel Vadot 	 * Remove the device from the device hierarchy. This is the right thing
206*c89d94adSVladimir Kondratyev 	 * to do for firmware-based fb drivers, such as EFI, VESA or VGA. After
20711d79c47SEmmanuel Vadot 	 * the new driver takes over the hardware, the firmware device's state
20811d79c47SEmmanuel Vadot 	 * will be lost.
20911d79c47SEmmanuel Vadot 	 *
21011d79c47SEmmanuel Vadot 	 * For non-platform devices, a new callback would be required.
21111d79c47SEmmanuel Vadot 	 *
21211d79c47SEmmanuel Vadot 	 * If the aperture helpers ever need to handle native drivers, this call
21311d79c47SEmmanuel Vadot 	 * would only have to unplug the DRM device, so that the hardware device
21411d79c47SEmmanuel Vadot 	 * stays around after detachment.
21511d79c47SEmmanuel Vadot 	 */
21611d79c47SEmmanuel Vadot 	platform_device_unregister(pdev);
21711d79c47SEmmanuel Vadot }
21811d79c47SEmmanuel Vadot 
21911d79c47SEmmanuel Vadot /**
22011d79c47SEmmanuel Vadot  * devm_aperture_acquire_for_platform_device - Acquires ownership of an aperture
22111d79c47SEmmanuel Vadot  *                                             on behalf of a platform device.
22211d79c47SEmmanuel Vadot  * @pdev:	the platform device to own the aperture
22311d79c47SEmmanuel Vadot  * @base:	the aperture's byte offset in physical memory
22411d79c47SEmmanuel Vadot  * @size:	the aperture size in bytes
22511d79c47SEmmanuel Vadot  *
22611d79c47SEmmanuel Vadot  * Installs the given device as the new owner of the aperture. The function
22711d79c47SEmmanuel Vadot  * expects the aperture to be provided by a platform device. If another
22811d79c47SEmmanuel Vadot  * driver takes over ownership of the aperture, aperture helpers will then
22911d79c47SEmmanuel Vadot  * unregister the platform device automatically. All acquired apertures are
23011d79c47SEmmanuel Vadot  * released automatically when the underlying device goes away.
23111d79c47SEmmanuel Vadot  *
23211d79c47SEmmanuel Vadot  * The function fails if the aperture, or parts of it, is currently
23311d79c47SEmmanuel Vadot  * owned by another device. To evict current owners, callers should use
23411d79c47SEmmanuel Vadot  * remove_conflicting_devices() et al. before calling this function.
23511d79c47SEmmanuel Vadot  *
23611d79c47SEmmanuel Vadot  * Returns:
23711d79c47SEmmanuel Vadot  * 0 on success, or a negative errno value otherwise.
23811d79c47SEmmanuel Vadot  */
23911d79c47SEmmanuel Vadot int devm_aperture_acquire_for_platform_device(struct platform_device *pdev,
24011d79c47SEmmanuel Vadot 					      resource_size_t base,
24111d79c47SEmmanuel Vadot 					      resource_size_t size)
24211d79c47SEmmanuel Vadot {
24311d79c47SEmmanuel Vadot 	return devm_aperture_acquire(&pdev->dev, base, size, aperture_detach_platform_device);
24411d79c47SEmmanuel Vadot }
24511d79c47SEmmanuel Vadot EXPORT_SYMBOL(devm_aperture_acquire_for_platform_device);
24611d79c47SEmmanuel Vadot 
24711d79c47SEmmanuel Vadot static void aperture_detach_devices(resource_size_t base, resource_size_t size)
24811d79c47SEmmanuel Vadot {
24911d79c47SEmmanuel Vadot 	resource_size_t end = base + size;
25011d79c47SEmmanuel Vadot 	struct list_head *pos, *n;
25111d79c47SEmmanuel Vadot 
25211d79c47SEmmanuel Vadot 	mutex_lock(&apertures_lock);
25311d79c47SEmmanuel Vadot 
25411d79c47SEmmanuel Vadot 	list_for_each_safe(pos, n, &apertures) {
25511d79c47SEmmanuel Vadot 		struct aperture_range *ap = container_of(pos, struct aperture_range, lh);
25611d79c47SEmmanuel Vadot 		struct device *dev = ap->dev;
25711d79c47SEmmanuel Vadot 
25811d79c47SEmmanuel Vadot 		if (WARN_ON_ONCE(!dev))
25911d79c47SEmmanuel Vadot 			continue;
26011d79c47SEmmanuel Vadot 
26111d79c47SEmmanuel Vadot 		if (!overlap(base, end, ap->base, ap->base + ap->size))
26211d79c47SEmmanuel Vadot 			continue;
26311d79c47SEmmanuel Vadot 
26411d79c47SEmmanuel Vadot 		ap->dev = NULL; /* detach from device */
26511d79c47SEmmanuel Vadot 		list_del(&ap->lh);
26611d79c47SEmmanuel Vadot 
26711d79c47SEmmanuel Vadot 		ap->detach(dev);
26811d79c47SEmmanuel Vadot 	}
26911d79c47SEmmanuel Vadot 
27011d79c47SEmmanuel Vadot 	mutex_unlock(&apertures_lock);
27111d79c47SEmmanuel Vadot }
27211d79c47SEmmanuel Vadot 
27311d79c47SEmmanuel Vadot /**
27411d79c47SEmmanuel Vadot  * aperture_remove_conflicting_devices - remove devices in the given range
27511d79c47SEmmanuel Vadot  * @base: the aperture's base address in physical memory
27611d79c47SEmmanuel Vadot  * @size: aperture size in bytes
27711d79c47SEmmanuel Vadot  * @name: a descriptive name of the requesting driver
27811d79c47SEmmanuel Vadot  *
27911d79c47SEmmanuel Vadot  * This function removes devices that own apertures within @base and @size.
28011d79c47SEmmanuel Vadot  *
28111d79c47SEmmanuel Vadot  * Returns:
28211d79c47SEmmanuel Vadot  * 0 on success, or a negative errno code otherwise
28311d79c47SEmmanuel Vadot  */
28411d79c47SEmmanuel Vadot int aperture_remove_conflicting_devices(resource_size_t base, resource_size_t size,
285*c89d94adSVladimir Kondratyev 					const char *name)
28611d79c47SEmmanuel Vadot {
28711d79c47SEmmanuel Vadot 	/*
28811d79c47SEmmanuel Vadot 	 * If a driver asked to unregister a platform device registered by
28911d79c47SEmmanuel Vadot 	 * sysfb, then can be assumed that this is a driver for a display
29011d79c47SEmmanuel Vadot 	 * that is set up by the system firmware and has a generic driver.
29111d79c47SEmmanuel Vadot 	 *
29211d79c47SEmmanuel Vadot 	 * Drivers for devices that don't have a generic driver will never
29311d79c47SEmmanuel Vadot 	 * ask for this, so let's assume that a real driver for the display
29411d79c47SEmmanuel Vadot 	 * was already probed and prevent sysfb to register devices later.
29511d79c47SEmmanuel Vadot 	 */
29611d79c47SEmmanuel Vadot #ifdef __linux__
29711d79c47SEmmanuel Vadot 	sysfb_disable();
29811d79c47SEmmanuel Vadot #endif
29911d79c47SEmmanuel Vadot 
30011d79c47SEmmanuel Vadot 	aperture_detach_devices(base, size);
30111d79c47SEmmanuel Vadot 
30211d79c47SEmmanuel Vadot 	return 0;
30311d79c47SEmmanuel Vadot }
30411d79c47SEmmanuel Vadot EXPORT_SYMBOL(aperture_remove_conflicting_devices);
30511d79c47SEmmanuel Vadot 
30611d79c47SEmmanuel Vadot /**
307*c89d94adSVladimir Kondratyev  * __aperture_remove_legacy_vga_devices - remove legacy VGA devices of a PCI devices
308*c89d94adSVladimir Kondratyev  * @pdev: PCI device
309*c89d94adSVladimir Kondratyev  *
310*c89d94adSVladimir Kondratyev  * This function removes VGA devices provided by @pdev, such as a VGA
311*c89d94adSVladimir Kondratyev  * framebuffer or a console. This is useful if you have a VGA-compatible
312*c89d94adSVladimir Kondratyev  * PCI graphics device with framebuffers in non-BAR locations. Drivers
313*c89d94adSVladimir Kondratyev  * should acquire ownership of those memory areas and afterwards call
314*c89d94adSVladimir Kondratyev  * this helper to release remaining VGA devices.
315*c89d94adSVladimir Kondratyev  *
316*c89d94adSVladimir Kondratyev  * If your hardware has its framebuffers accessible via PCI BARS, use
317*c89d94adSVladimir Kondratyev  * aperture_remove_conflicting_pci_devices() instead. The function will
318*c89d94adSVladimir Kondratyev  * release any VGA devices automatically.
319*c89d94adSVladimir Kondratyev  *
320*c89d94adSVladimir Kondratyev  * WARNING: Apparently we must remove graphics drivers before calling
321*c89d94adSVladimir Kondratyev  *          this helper. Otherwise the vga fbdev driver falls over if
322*c89d94adSVladimir Kondratyev  *          we have vgacon configured.
323*c89d94adSVladimir Kondratyev  *
324*c89d94adSVladimir Kondratyev  * Returns:
325*c89d94adSVladimir Kondratyev  * 0 on success, or a negative errno code otherwise
326*c89d94adSVladimir Kondratyev  */
327*c89d94adSVladimir Kondratyev int __aperture_remove_legacy_vga_devices(struct pci_dev *pdev)
328*c89d94adSVladimir Kondratyev {
329*c89d94adSVladimir Kondratyev 	/* VGA framebuffer */
330*c89d94adSVladimir Kondratyev 	aperture_detach_devices(VGA_FB_PHYS_BASE, VGA_FB_PHYS_SIZE);
331*c89d94adSVladimir Kondratyev 
332*c89d94adSVladimir Kondratyev 	/* VGA textmode console */
333*c89d94adSVladimir Kondratyev #ifdef __linux__
334*c89d94adSVladimir Kondratyev 	return vga_remove_vgacon(pdev);
335*c89d94adSVladimir Kondratyev #elif defined(__FreeBSD__)
336*c89d94adSVladimir Kondratyev 	return 0;
337*c89d94adSVladimir Kondratyev #endif
338*c89d94adSVladimir Kondratyev }
339*c89d94adSVladimir Kondratyev EXPORT_SYMBOL(__aperture_remove_legacy_vga_devices);
340*c89d94adSVladimir Kondratyev 
341*c89d94adSVladimir Kondratyev /**
34211d79c47SEmmanuel Vadot  * aperture_remove_conflicting_pci_devices - remove existing framebuffers for PCI devices
34311d79c47SEmmanuel Vadot  * @pdev: PCI device
34411d79c47SEmmanuel Vadot  * @name: a descriptive name of the requesting driver
34511d79c47SEmmanuel Vadot  *
34611d79c47SEmmanuel Vadot  * This function removes devices that own apertures within any of @pdev's
34711d79c47SEmmanuel Vadot  * memory bars. The function assumes that PCI device with shadowed ROM
34811d79c47SEmmanuel Vadot  * drives a primary display and therefore kicks out vga16fb as well.
34911d79c47SEmmanuel Vadot  *
35011d79c47SEmmanuel Vadot  * Returns:
35111d79c47SEmmanuel Vadot  * 0 on success, or a negative errno code otherwise
35211d79c47SEmmanuel Vadot  */
35311d79c47SEmmanuel Vadot int aperture_remove_conflicting_pci_devices(struct pci_dev *pdev, const char *name)
35411d79c47SEmmanuel Vadot {
35511d79c47SEmmanuel Vadot 	bool primary = false;
35611d79c47SEmmanuel Vadot 	resource_size_t base, size;
357*c89d94adSVladimir Kondratyev 	int bar, ret = 0;
35811d79c47SEmmanuel Vadot 
35911d79c47SEmmanuel Vadot #ifdef __linux__
360*c89d94adSVladimir Kondratyev 	if (pdev == vga_default_device())
361*c89d94adSVladimir Kondratyev 		primary = true;
362*c89d94adSVladimir Kondratyev 
363*c89d94adSVladimir Kondratyev 	if (primary)
364*c89d94adSVladimir Kondratyev 		sysfb_disable();
36511d79c47SEmmanuel Vadot #endif
36611d79c47SEmmanuel Vadot 
36711d79c47SEmmanuel Vadot 	for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
36811d79c47SEmmanuel Vadot 		if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
36911d79c47SEmmanuel Vadot 			continue;
37011d79c47SEmmanuel Vadot 
37111d79c47SEmmanuel Vadot 		base = pci_resource_start(pdev, bar);
37211d79c47SEmmanuel Vadot 		size = pci_resource_len(pdev, bar);
373*c89d94adSVladimir Kondratyev 		aperture_detach_devices(base, size);
37411d79c47SEmmanuel Vadot 	}
37511d79c47SEmmanuel Vadot 
37611d79c47SEmmanuel Vadot 	/*
377*c89d94adSVladimir Kondratyev 	 * If this is the primary adapter, there could be a VGA device
378*c89d94adSVladimir Kondratyev 	 * that consumes the VGA framebuffer I/O range. Remove this
379*c89d94adSVladimir Kondratyev 	 * device as well.
38011d79c47SEmmanuel Vadot 	 */
381*c89d94adSVladimir Kondratyev 	if (primary)
382*c89d94adSVladimir Kondratyev 		ret = __aperture_remove_legacy_vga_devices(pdev);
38311d79c47SEmmanuel Vadot 
384*c89d94adSVladimir Kondratyev 	return ret;
38511d79c47SEmmanuel Vadot 
38611d79c47SEmmanuel Vadot }
38711d79c47SEmmanuel Vadot EXPORT_SYMBOL(aperture_remove_conflicting_pci_devices);
388