drm/aperture: Add infrastructure for aperture ownership
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_aperture.c
1 // SPDX-License-Identifier: MIT
2
3 #include <linux/device.h>
4 #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>
11 #include <linux/vgaarb.h>
12
13 #include <drm/drm_aperture.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_print.h>
16
17 /**
18  * DOC: overview
19  *
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.
27  *
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.
33  *
34  * .. code-block:: c
35  *
36  *      static int remove_conflicting_framebuffers(struct pci_dev *pdev)
37  *      {
38  *              bool primary = false;
39  *              resource_size_t base, size;
40  *              int ret;
41  *
42  *              base = pci_resource_start(pdev, 0);
43  *              size = pci_resource_len(pdev, 0);
44  *      #ifdef CONFIG_X86
45  *              primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
46  *      #endif
47  *
48  *              return drm_aperture_remove_conflicting_framebuffers(base, size, primary,
49  *                                                                  "example driver");
50  *      }
51  *
52  *      static int probe(struct pci_dev *pdev)
53  *      {
54  *              int ret;
55  *
56  *              // Remove any generic drivers...
57  *              ret = remove_conflicting_framebuffers(pdev);
58  *              if (ret)
59  *                      return ret;
60  *
61  *              // ... and initialize the hardware.
62  *              ...
63  *
64  *              drm_dev_register();
65  *
66  *              return 0;
67  *      }
68  *
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.
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.
123  */
124
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
268 /**
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
274  *
275  * This function removes graphics device drivers which use memory range described by
276  * @base and @size.
277  *
278  * Returns:
279  * 0 on success, or a negative errno code otherwise
280  */
281 int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
282                                                  bool primary, const char *name)
283 {
284 #if IS_REACHABLE(CONFIG_FB)
285         struct apertures_struct *a;
286         int ret;
287
288         a = alloc_apertures(1);
289         if (!a)
290                 return -ENOMEM;
291
292         a->ranges[0].base = base;
293         a->ranges[0].size = size;
294
295         ret = remove_conflicting_framebuffers(a, name, primary);
296         kfree(a);
297
298         if (ret)
299                 return ret;
300 #endif
301
302         drm_aperture_detach_drivers(base, size);
303
304         return 0;
305 }
306 EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
307
308 /**
309  * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
310  * @pdev: PCI device
311  * @name: requesting driver name
312  *
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.
316  *
317  * Returns:
318  * 0 on success, or a negative errno code otherwise
319  */
320 int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, const char *name)
321 {
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         }
332
333         /*
334          * WARNING: Apparently we must kick fbdev drivers before vgacon,
335          * otherwise the vga fbdev driver falls over.
336          */
337 #if IS_REACHABLE(CONFIG_FB)
338         ret = remove_conflicting_pci_framebuffers(pdev, name);
339 #endif
340         if (ret == 0)
341                 ret = vga_remove_vgacon(pdev);
342         return ret;
343 }
344 EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);