Skip to content

Commit 730e799

Browse files
committed
drm/aperture: Add infrastructure for aperture ownership
Platform devices might operate on firmware framebuffers, such as VESA or EFI. Before a native driver for the graphics hardware can take over the device, it has to remove any platform driver that operates on the firmware framebuffer. Aperture helpers provide the infrastructure for platform drivers to acquire firmware framebuffers, and for native drivers to remove them later on. It works similar to the related fbdev mechanism. During initialization, the platform driver acquires the firmware framebuffer's I/O memory and provides a callback to be removed. The native driver later uses this information to remove any platform driver for it's framebuffer I/O memory. The aperture removal code is integrated into the existing code for removing conflicting framebuffers, so native drivers use it automatically. v5: * fix build error introduced by rebasing v4 * fix typo in documentation v4: * hide detach callback in implementation (Daniel) * documentation fixes v3: * rebase onto existing aperture infrastructure * release aperture from list during detach; fix dangling apertures * don't export struct drm_aperture * document struct drm_aperture_funcs v2: * rename plaform helpers to aperture helpers * tie to device lifetime with devm_ functions * removed unsued remove() callback * rename kickout to detach * make struct drm_aperture private * rebase onto existing drm_aperture.h header file * use MIT license only for simplicity * documentation Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch> Acked-by: Maxime Ripard <maxime@cerno.tech> Tested-by: nerdopolis <bluescreen_avenger@verizon.net> Link: https://patchwork.freedesktop.org/patch/msgid/20210430105840.30515-4-tzimmermann@suse.de
1 parent 900d3e4 commit 730e799

2 files changed

Lines changed: 221 additions & 4 deletions

File tree

‎drivers/gpu/drm/drm_aperture.c‎

Lines changed: 217 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// SPDX-License-Identifier: MIT
22

3+
#include <linux/device.h>
34
#include <linux/fb.h>
5+
#include <linux/list.h>
6+
#include <linux/mutex.h>
7+
#include <linux/pci.h>
8+
#include <linux/platform_device.h> /* for firmware helpers */
9+
#include <linux/slab.h>
10+
#include <linux/types.h>
411
#include <linux/vgaarb.h>
512

613
#include <drm/drm_aperture.h>
14+
#include <drm/drm_drv.h>
15+
#include <drm/drm_print.h>
716

817
/**
918
* DOC: overview
@@ -62,8 +71,200 @@
6271
* framebuffer apertures automatically. Device drivers without knowledge of
6372
* the framebuffer's location shall call drm_aperture_remove_framebuffers(),
6473
* which removes all drivers for known framebuffer.
74+
*
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.
81+
*
82+
* .. code-block:: c
83+
*
84+
* static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
85+
* {
86+
* resource_size_t base, size;
87+
*
88+
* mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
89+
* if (!mem)
90+
* return -EINVAL;
91+
* base = mem->start;
92+
* size = resource_size(mem);
93+
*
94+
* return devm_acquire_aperture_from_firmware(dev, base, size);
95+
* }
96+
*
97+
* static int probe(struct platform_device *pdev)
98+
* {
99+
* struct drm_device *dev;
100+
* int ret;
101+
*
102+
* // ... Initialize the device...
103+
* dev = devm_drm_dev_alloc();
104+
* ...
105+
*
106+
* // ... and acquire ownership of the framebuffer.
107+
* ret = acquire_framebuffers(dev, pdev);
108+
* if (ret)
109+
* return ret;
110+
*
111+
* drm_dev_register(dev, 0);
112+
*
113+
* return 0;
114+
* }
115+
*
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
122+
* afterwards.
65123
*/
66124

125+
struct drm_aperture {
126+
struct drm_device *dev;
127+
resource_size_t base;
128+
resource_size_t size;
129+
struct list_head lh;
130+
void (*detach)(struct drm_device *dev);
131+
};
132+
133+
static LIST_HEAD(drm_apertures);
134+
static DEFINE_MUTEX(drm_apertures_lock);
135+
136+
static bool overlap(resource_size_t base1, resource_size_t end1,
137+
resource_size_t base2, resource_size_t end2)
138+
{
139+
return (base1 < end2) && (end1 > base2);
140+
}
141+
142+
static void devm_aperture_acquire_release(void *data)
143+
{
144+
struct drm_aperture *ap = data;
145+
bool detached = !ap->dev;
146+
147+
if (detached)
148+
return;
149+
150+
mutex_lock(&drm_apertures_lock);
151+
list_del(&ap->lh);
152+
mutex_unlock(&drm_apertures_lock);
153+
}
154+
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 *))
158+
{
159+
size_t end = base + size;
160+
struct list_head *pos;
161+
struct drm_aperture *ap;
162+
163+
mutex_lock(&drm_apertures_lock);
164+
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+
return -EBUSY;
169+
}
170+
171+
ap = devm_kzalloc(dev->dev, sizeof(*ap), GFP_KERNEL);
172+
if (!ap)
173+
return -ENOMEM;
174+
175+
ap->dev = dev;
176+
ap->base = base;
177+
ap->size = size;
178+
ap->detach = detach;
179+
INIT_LIST_HEAD(&ap->lh);
180+
181+
list_add(&ap->lh, &drm_apertures);
182+
183+
mutex_unlock(&drm_apertures_lock);
184+
185+
return devm_add_action_or_reset(dev->dev, devm_aperture_acquire_release, ap);
186+
}
187+
188+
static void drm_aperture_detach_firmware(struct drm_device *dev)
189+
{
190+
struct platform_device *pdev = to_platform_device(dev->dev);
191+
192+
/*
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
196+
* will be lost.
197+
*
198+
* For non-platform devices, a new callback would be required.
199+
*
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.
203+
*/
204+
platform_device_unregister(pdev);
205+
}
206+
207+
/**
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
213+
*
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.
221+
*
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
226+
* device.
227+
*
228+
* Returns:
229+
* 0 on success, or a negative errno value otherwise.
230+
*/
231+
int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
232+
resource_size_t size)
233+
{
234+
if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
235+
return -EINVAL;
236+
237+
return devm_aperture_acquire(dev, base, size, drm_aperture_detach_firmware);
238+
}
239+
EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
240+
241+
static void drm_aperture_detach_drivers(resource_size_t base, resource_size_t size)
242+
{
243+
resource_size_t end = base + size;
244+
struct list_head *pos, *n;
245+
246+
mutex_lock(&drm_apertures_lock);
247+
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;
252+
253+
if (WARN_ON_ONCE(!dev))
254+
continue;
255+
256+
if (!overlap(base, end, ap->base, ap->base + ap->size))
257+
continue;
258+
259+
ap->dev = NULL; /* detach from device */
260+
list_del(&ap->lh);
261+
262+
ap->detach(dev);
263+
}
264+
265+
mutex_unlock(&drm_apertures_lock);
266+
}
267+
67268
/**
68269
* drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
69270
* @base: the aperture's base address in physical memory
@@ -94,10 +295,13 @@ int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_
94295
ret = remove_conflicting_framebuffers(a, name, primary);
95296
kfree(a);
96297

97-
return ret;
98-
#else
99-
return 0;
298+
if (ret)
299+
return ret;
100300
#endif
301+
302+
drm_aperture_detach_drivers(base, size);
303+
304+
return 0;
101305
}
102306
EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
103307

@@ -115,7 +319,16 @@ EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
115319
*/
116320
int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, const char *name)
117321
{
118-
int ret = 0;
322+
resource_size_t base, size;
323+
int bar, ret = 0;
324+
325+
for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
326+
if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
327+
continue;
328+
base = pci_resource_start(pdev, bar);
329+
size = pci_resource_len(pdev, bar);
330+
drm_aperture_detach_drivers(base, size);
331+
}
119332

120333
/*
121334
* WARNING: Apparently we must kick fbdev drivers before vgacon,

‎include/drm/drm_aperture.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55

66
#include <linux/types.h>
77

8+
struct drm_device;
89
struct pci_dev;
910

11+
int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
12+
resource_size_t size);
13+
1014
int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
1115
bool primary, const char *name);
1216

0 commit comments

Comments
 (0)