xref: /linux/drivers/gpu/drm/drm_aperture.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
129160591SThomas Zimmermann // SPDX-License-Identifier: MIT
229160591SThomas Zimmermann 
37283f862SThomas Zimmermann #include <linux/aperture.h>
47283f862SThomas Zimmermann #include <linux/platform_device.h>
5603dc7edSThomas Zimmermann 
629160591SThomas Zimmermann #include <drm/drm_aperture.h>
7730e7992SThomas Zimmermann #include <drm/drm_drv.h>
8730e7992SThomas Zimmermann #include <drm/drm_print.h>
929160591SThomas Zimmermann 
1029160591SThomas Zimmermann /**
1129160591SThomas Zimmermann  * DOC: overview
1229160591SThomas Zimmermann  *
1329160591SThomas Zimmermann  * A graphics device might be supported by different drivers, but only one
1429160591SThomas Zimmermann  * driver can be active at any given time. Many systems load a generic
1529160591SThomas Zimmermann  * graphics drivers, such as EFI-GOP or VESA, early during the boot process.
1629160591SThomas Zimmermann  * During later boot stages, they replace the generic driver with a dedicated,
1729160591SThomas Zimmermann  * hardware-specific driver. To take over the device the dedicated driver
1829160591SThomas Zimmermann  * first has to remove the generic driver. DRM aperture functions manage
1929160591SThomas Zimmermann  * ownership of DRM framebuffer memory and hand-over between drivers.
2029160591SThomas Zimmermann  *
2129160591SThomas Zimmermann  * DRM drivers should call drm_aperture_remove_conflicting_framebuffers()
2229160591SThomas Zimmermann  * at the top of their probe function. The function removes any generic
2329160591SThomas Zimmermann  * driver that is currently associated with the given framebuffer memory.
2429160591SThomas Zimmermann  * If the framebuffer is located at PCI BAR 0, the rsp code looks as in the
2529160591SThomas Zimmermann  * example given below.
2629160591SThomas Zimmermann  *
2729160591SThomas Zimmermann  * .. code-block:: c
2829160591SThomas Zimmermann  *
2997c9bfe3SThomas Zimmermann  *	static const struct drm_driver example_driver = {
3097c9bfe3SThomas Zimmermann  *		...
3197c9bfe3SThomas Zimmermann  *	};
3297c9bfe3SThomas Zimmermann  *
3329160591SThomas Zimmermann  *	static int remove_conflicting_framebuffers(struct pci_dev *pdev)
3429160591SThomas Zimmermann  *	{
3529160591SThomas Zimmermann  *		resource_size_t base, size;
3629160591SThomas Zimmermann  *		int ret;
3729160591SThomas Zimmermann  *
3829160591SThomas Zimmermann  *		base = pci_resource_start(pdev, 0);
3929160591SThomas Zimmermann  *		size = pci_resource_len(pdev, 0);
4029160591SThomas Zimmermann  *
4162aeaeaaSDaniel Vetter  *		return drm_aperture_remove_conflicting_framebuffers(base, size,
4297c9bfe3SThomas Zimmermann  *		                                                    &example_driver);
4329160591SThomas Zimmermann  *	}
4429160591SThomas Zimmermann  *
4529160591SThomas Zimmermann  *	static int probe(struct pci_dev *pdev)
4629160591SThomas Zimmermann  *	{
4729160591SThomas Zimmermann  *		int ret;
4829160591SThomas Zimmermann  *
4929160591SThomas Zimmermann  *		// Remove any generic drivers...
5029160591SThomas Zimmermann  *		ret = remove_conflicting_framebuffers(pdev);
5129160591SThomas Zimmermann  *		if (ret)
5229160591SThomas Zimmermann  *			return ret;
5329160591SThomas Zimmermann  *
5429160591SThomas Zimmermann  *		// ... and initialize the hardware.
5529160591SThomas Zimmermann  *		...
5629160591SThomas Zimmermann  *
5729160591SThomas Zimmermann  *		drm_dev_register();
5829160591SThomas Zimmermann  *
5929160591SThomas Zimmermann  *		return 0;
6029160591SThomas Zimmermann  *	}
6129160591SThomas Zimmermann  *
6229160591SThomas Zimmermann  * PCI device drivers should call
6329160591SThomas Zimmermann  * drm_aperture_remove_conflicting_pci_framebuffers() and let it detect the
6429160591SThomas Zimmermann  * framebuffer apertures automatically. Device drivers without knowledge of
6529160591SThomas Zimmermann  * the framebuffer's location shall call drm_aperture_remove_framebuffers(),
6629160591SThomas Zimmermann  * which removes all drivers for known framebuffer.
67730e7992SThomas Zimmermann  *
68730e7992SThomas Zimmermann  * Drivers that are susceptible to being removed by other drivers, such as
69730e7992SThomas Zimmermann  * generic EFI or VESA drivers, have to register themselves as owners of their
700ae865efSCai Huoqing  * given framebuffer memory. Ownership of the framebuffer memory is achieved
71730e7992SThomas Zimmermann  * by calling devm_aperture_acquire_from_firmware(). On success, the driver
72730e7992SThomas Zimmermann  * is the owner of the framebuffer range. The function fails if the
73f2912237SThierry Reding  * framebuffer is already owned by another driver. See below for an example.
74730e7992SThomas Zimmermann  *
75730e7992SThomas Zimmermann  * .. code-block:: c
76730e7992SThomas Zimmermann  *
77730e7992SThomas Zimmermann  *	static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
78730e7992SThomas Zimmermann  *	{
79730e7992SThomas Zimmermann  *		resource_size_t base, size;
80730e7992SThomas Zimmermann  *
81730e7992SThomas Zimmermann  *		mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82730e7992SThomas Zimmermann  *		if (!mem)
83730e7992SThomas Zimmermann  *			return -EINVAL;
84730e7992SThomas Zimmermann  *		base = mem->start;
85730e7992SThomas Zimmermann  *		size = resource_size(mem);
86730e7992SThomas Zimmermann  *
87730e7992SThomas Zimmermann  *		return devm_acquire_aperture_from_firmware(dev, base, size);
88730e7992SThomas Zimmermann  *	}
89730e7992SThomas Zimmermann  *
90730e7992SThomas Zimmermann  *	static int probe(struct platform_device *pdev)
91730e7992SThomas Zimmermann  *	{
92730e7992SThomas Zimmermann  *		struct drm_device *dev;
93730e7992SThomas Zimmermann  *		int ret;
94730e7992SThomas Zimmermann  *
95730e7992SThomas Zimmermann  *		// ... Initialize the device...
96730e7992SThomas Zimmermann  *		dev = devm_drm_dev_alloc();
97730e7992SThomas Zimmermann  *		...
98730e7992SThomas Zimmermann  *
99730e7992SThomas Zimmermann  *		// ... and acquire ownership of the framebuffer.
100730e7992SThomas Zimmermann  *		ret = acquire_framebuffers(dev, pdev);
101730e7992SThomas Zimmermann  *		if (ret)
102730e7992SThomas Zimmermann  *			return ret;
103730e7992SThomas Zimmermann  *
104730e7992SThomas Zimmermann  *		drm_dev_register(dev, 0);
105730e7992SThomas Zimmermann  *
106730e7992SThomas Zimmermann  *		return 0;
107730e7992SThomas Zimmermann  *	}
108730e7992SThomas Zimmermann  *
109730e7992SThomas Zimmermann  * The generic driver is now subject to forced removal by other drivers. This
110730e7992SThomas Zimmermann  * only works for platform drivers that support hot unplug.
111f2912237SThierry Reding  * When a driver calls drm_aperture_remove_conflicting_framebuffers() et al.
112730e7992SThomas Zimmermann  * for the registered framebuffer range, the aperture helpers call
113730e7992SThomas Zimmermann  * platform_device_unregister() and the generic driver unloads itself. It
114730e7992SThomas Zimmermann  * may not access the device's registers, framebuffer memory, ROM, etc
115730e7992SThomas Zimmermann  * afterwards.
11629160591SThomas Zimmermann  */
11729160591SThomas Zimmermann 
118730e7992SThomas Zimmermann /**
119730e7992SThomas Zimmermann  * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
120730e7992SThomas Zimmermann  *                                       on behalf of a DRM driver.
121730e7992SThomas Zimmermann  * @dev:	the DRM device to own the framebuffer memory
122730e7992SThomas Zimmermann  * @base:	the framebuffer's byte offset in physical memory
123730e7992SThomas Zimmermann  * @size:	the framebuffer size in bytes
124730e7992SThomas Zimmermann  *
125730e7992SThomas Zimmermann  * Installs the given device as the new owner of the framebuffer. The function
126730e7992SThomas Zimmermann  * expects the framebuffer to be provided by a platform device that has been
127730e7992SThomas Zimmermann  * set up by firmware. Firmware can be any generic interface, such as EFI,
128730e7992SThomas Zimmermann  * VESA, VGA, etc. If the native hardware driver takes over ownership of the
129730e7992SThomas Zimmermann  * framebuffer range, the firmware state gets lost. Aperture helpers will then
130730e7992SThomas Zimmermann  * unregister the platform device automatically. Acquired apertures are
131730e7992SThomas Zimmermann  * released automatically if the underlying device goes away.
132730e7992SThomas Zimmermann  *
133730e7992SThomas Zimmermann  * The function fails if the framebuffer range, or parts of it, is currently
134730e7992SThomas Zimmermann  * owned by another driver. To evict current owners, callers should use
135730e7992SThomas Zimmermann  * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
136730e7992SThomas Zimmermann  * function. The function also fails if the given device is not a platform
137730e7992SThomas Zimmermann  * device.
138730e7992SThomas Zimmermann  *
139730e7992SThomas Zimmermann  * Returns:
140730e7992SThomas Zimmermann  * 0 on success, or a negative errno value otherwise.
141730e7992SThomas Zimmermann  */
devm_aperture_acquire_from_firmware(struct drm_device * dev,resource_size_t base,resource_size_t size)142730e7992SThomas Zimmermann int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
143730e7992SThomas Zimmermann 					resource_size_t size)
144730e7992SThomas Zimmermann {
1457283f862SThomas Zimmermann 	struct platform_device *pdev;
1467283f862SThomas Zimmermann 
147730e7992SThomas Zimmermann 	if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
148730e7992SThomas Zimmermann 		return -EINVAL;
149730e7992SThomas Zimmermann 
1507283f862SThomas Zimmermann 	pdev = to_platform_device(dev->dev);
1517283f862SThomas Zimmermann 
1527283f862SThomas Zimmermann 	return devm_aperture_acquire_for_platform_device(pdev, base, size);
153730e7992SThomas Zimmermann }
154730e7992SThomas Zimmermann EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
155730e7992SThomas Zimmermann 
15629160591SThomas Zimmermann /**
15729160591SThomas Zimmermann  * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
15829160591SThomas Zimmermann  * @base: the aperture's base address in physical memory
15929160591SThomas Zimmermann  * @size: aperture size in bytes
16097c9bfe3SThomas Zimmermann  * @req_driver: requesting DRM driver
16129160591SThomas Zimmermann  *
162f2912237SThierry Reding  * This function removes graphics device drivers which use the memory range described by
16329160591SThomas Zimmermann  * @base and @size.
16429160591SThomas Zimmermann  *
16529160591SThomas Zimmermann  * Returns:
16629160591SThomas Zimmermann  * 0 on success, or a negative errno code otherwise
16729160591SThomas Zimmermann  */
drm_aperture_remove_conflicting_framebuffers(resource_size_t base,resource_size_t size,const struct drm_driver * req_driver)16829160591SThomas Zimmermann int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
16962aeaeaaSDaniel Vetter 						 const struct drm_driver *req_driver)
17029160591SThomas Zimmermann {
171*5fbcc670SDaniel Vetter 	return aperture_remove_conflicting_devices(base, size, req_driver->name);
17229160591SThomas Zimmermann }
17329160591SThomas Zimmermann EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
17429160591SThomas Zimmermann 
17529160591SThomas Zimmermann /**
17629160591SThomas Zimmermann  * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
17729160591SThomas Zimmermann  * @pdev: PCI device
17897c9bfe3SThomas Zimmermann  * @req_driver: requesting DRM driver
17929160591SThomas Zimmermann  *
180f2912237SThierry Reding  * This function removes graphics device drivers using the memory range configured
181f2912237SThierry Reding  * for any of @pdev's memory bars. The function assumes that a PCI device with
18229160591SThomas Zimmermann  * shadowed ROM drives a primary display and so kicks out vga16fb.
18329160591SThomas Zimmermann  *
18429160591SThomas Zimmermann  * Returns:
18529160591SThomas Zimmermann  * 0 on success, or a negative errno code otherwise
18629160591SThomas Zimmermann  */
drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev * pdev,const struct drm_driver * req_driver)18797c9bfe3SThomas Zimmermann int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
18897c9bfe3SThomas Zimmermann 						     const struct drm_driver *req_driver)
18929160591SThomas Zimmermann {
1907283f862SThomas Zimmermann 	return aperture_remove_conflicting_pci_devices(pdev, req_driver->name);
19129160591SThomas Zimmermann }
19229160591SThomas Zimmermann EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);
193