aperture.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: MIT
  2. #include <linux/aperture.h>
  3. #include <linux/device.h>
  4. #include <linux/list.h>
  5. #include <linux/mutex.h>
  6. #include <linux/pci.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <linux/sysfb.h>
  10. #include <linux/types.h>
  11. #include <linux/vgaarb.h>
  12. #include <video/vga.h>
  13. /**
  14. * DOC: overview
  15. *
  16. * A graphics device might be supported by different drivers, but only one
  17. * driver can be active at any given time. Many systems load a generic
  18. * graphics drivers, such as EFI-GOP or VESA, early during the boot process.
  19. * During later boot stages, they replace the generic driver with a dedicated,
  20. * hardware-specific driver. To take over the device the dedicated driver
  21. * first has to remove the generic driver. Aperture functions manage
  22. * ownership of framebuffer memory and hand-over between drivers.
  23. *
  24. * Graphics drivers should call aperture_remove_conflicting_devices()
  25. * at the top of their probe function. The function removes any generic
  26. * driver that is currently associated with the given framebuffer memory.
  27. * An example for a graphics device on the platform bus is shown below.
  28. *
  29. * .. code-block:: c
  30. *
  31. * static int example_probe(struct platform_device *pdev)
  32. * {
  33. * struct resource *mem;
  34. * resource_size_t base, size;
  35. * int ret;
  36. *
  37. * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38. * if (!mem)
  39. * return -ENODEV;
  40. * base = mem->start;
  41. * size = resource_size(mem);
  42. *
  43. * ret = aperture_remove_conflicting_devices(base, size, false, "example");
  44. * if (ret)
  45. * return ret;
  46. *
  47. * // Initialize the hardware
  48. * ...
  49. *
  50. * return 0;
  51. * }
  52. *
  53. * static const struct platform_driver example_driver = {
  54. * .probe = example_probe,
  55. * ...
  56. * };
  57. *
  58. * The given example reads the platform device's I/O-memory range from the
  59. * device instance. An active framebuffer will be located within this range.
  60. * The call to aperture_remove_conflicting_devices() releases drivers that
  61. * have previously claimed ownership of the range and are currently driving
  62. * output on the framebuffer. If successful, the new driver can take over
  63. * the device.
  64. *
  65. * While the given example uses a platform device, the aperture helpers work
  66. * with every bus that has an addressable framebuffer. In the case of PCI,
  67. * device drivers can also call aperture_remove_conflicting_pci_devices() and
  68. * let the function detect the apertures automatically. Device drivers without
  69. * knowledge of the framebuffer's location can call
  70. * aperture_remove_all_conflicting_devices(), which removes all known devices.
  71. *
  72. * Drivers that are susceptible to being removed by other drivers, such as
  73. * generic EFI or VESA drivers, have to register themselves as owners of their
  74. * framebuffer apertures. Ownership of the framebuffer memory is achieved
  75. * by calling devm_aperture_acquire_for_platform_device(). If successful, the
  76. * driveris the owner of the framebuffer range. The function fails if the
  77. * framebuffer is already owned by another driver. See below for an example.
  78. *
  79. * .. code-block:: c
  80. *
  81. * static int generic_probe(struct platform_device *pdev)
  82. * {
  83. * struct resource *mem;
  84. * resource_size_t base, size;
  85. *
  86. * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87. * if (!mem)
  88. * return -ENODEV;
  89. * base = mem->start;
  90. * size = resource_size(mem);
  91. *
  92. * ret = devm_aperture_acquire_for_platform_device(pdev, base, size);
  93. * if (ret)
  94. * return ret;
  95. *
  96. * // Initialize the hardware
  97. * ...
  98. *
  99. * return 0;
  100. * }
  101. *
  102. * static int generic_remove(struct platform_device *)
  103. * {
  104. * // Hot-unplug the device
  105. * ...
  106. *
  107. * return 0;
  108. * }
  109. *
  110. * static const struct platform_driver generic_driver = {
  111. * .probe = generic_probe,
  112. * .remove = generic_remove,
  113. * ...
  114. * };
  115. *
  116. * The similar to the previous example, the generic driver claims ownership
  117. * of the framebuffer memory from its probe function. This will fail if the
  118. * memory range, or parts of it, is already owned by another driver.
  119. *
  120. * If successful, the generic driver is now subject to forced removal by
  121. * another driver. This only works for platform drivers that support hot
  122. * unplugging. When a driver calls aperture_remove_conflicting_devices()
  123. * et al for the registered framebuffer range, the aperture helpers call
  124. * platform_device_unregister() and the generic driver unloads itself. The
  125. * generic driver also has to provide a remove function to make this work.
  126. * Once hot unplugged fro mhardware, it may not access the device's
  127. * registers, framebuffer memory, ROM, etc afterwards.
  128. */
  129. struct aperture_range {
  130. struct device *dev;
  131. resource_size_t base;
  132. resource_size_t size;
  133. struct list_head lh;
  134. void (*detach)(struct device *dev);
  135. };
  136. static LIST_HEAD(apertures);
  137. static DEFINE_MUTEX(apertures_lock);
  138. static bool overlap(resource_size_t base1, resource_size_t end1,
  139. resource_size_t base2, resource_size_t end2)
  140. {
  141. return (base1 < end2) && (end1 > base2);
  142. }
  143. static void devm_aperture_acquire_release(void *data)
  144. {
  145. struct aperture_range *ap = data;
  146. bool detached = !ap->dev;
  147. if (detached)
  148. return;
  149. mutex_lock(&apertures_lock);
  150. list_del(&ap->lh);
  151. mutex_unlock(&apertures_lock);
  152. }
  153. static int devm_aperture_acquire(struct device *dev,
  154. resource_size_t base, resource_size_t size,
  155. void (*detach)(struct device *))
  156. {
  157. size_t end = base + size;
  158. struct list_head *pos;
  159. struct aperture_range *ap;
  160. mutex_lock(&apertures_lock);
  161. list_for_each(pos, &apertures) {
  162. ap = container_of(pos, struct aperture_range, lh);
  163. if (overlap(base, end, ap->base, ap->base + ap->size)) {
  164. mutex_unlock(&apertures_lock);
  165. return -EBUSY;
  166. }
  167. }
  168. ap = devm_kzalloc(dev, sizeof(*ap), GFP_KERNEL);
  169. if (!ap) {
  170. mutex_unlock(&apertures_lock);
  171. return -ENOMEM;
  172. }
  173. ap->dev = dev;
  174. ap->base = base;
  175. ap->size = size;
  176. ap->detach = detach;
  177. INIT_LIST_HEAD(&ap->lh);
  178. list_add(&ap->lh, &apertures);
  179. mutex_unlock(&apertures_lock);
  180. return devm_add_action_or_reset(dev, devm_aperture_acquire_release, ap);
  181. }
  182. static void aperture_detach_platform_device(struct device *dev)
  183. {
  184. struct platform_device *pdev = to_platform_device(dev);
  185. /*
  186. * Remove the device from the device hierarchy. This is the right thing
  187. * to do for firmware-based DRM drivers, such as EFI, VESA or VGA. After
  188. * the new driver takes over the hardware, the firmware device's state
  189. * will be lost.
  190. *
  191. * For non-platform devices, a new callback would be required.
  192. *
  193. * If the aperture helpers ever need to handle native drivers, this call
  194. * would only have to unplug the DRM device, so that the hardware device
  195. * stays around after detachment.
  196. */
  197. platform_device_unregister(pdev);
  198. }
  199. /**
  200. * devm_aperture_acquire_for_platform_device - Acquires ownership of an aperture
  201. * on behalf of a platform device.
  202. * @pdev: the platform device to own the aperture
  203. * @base: the aperture's byte offset in physical memory
  204. * @size: the aperture size in bytes
  205. *
  206. * Installs the given device as the new owner of the aperture. The function
  207. * expects the aperture to be provided by a platform device. If another
  208. * driver takes over ownership of the aperture, aperture helpers will then
  209. * unregister the platform device automatically. All acquired apertures are
  210. * released automatically when the underlying device goes away.
  211. *
  212. * The function fails if the aperture, or parts of it, is currently
  213. * owned by another device. To evict current owners, callers should use
  214. * remove_conflicting_devices() et al. before calling this function.
  215. *
  216. * Returns:
  217. * 0 on success, or a negative errno value otherwise.
  218. */
  219. int devm_aperture_acquire_for_platform_device(struct platform_device *pdev,
  220. resource_size_t base,
  221. resource_size_t size)
  222. {
  223. return devm_aperture_acquire(&pdev->dev, base, size, aperture_detach_platform_device);
  224. }
  225. EXPORT_SYMBOL(devm_aperture_acquire_for_platform_device);
  226. static void aperture_detach_devices(resource_size_t base, resource_size_t size)
  227. {
  228. resource_size_t end = base + size;
  229. struct list_head *pos, *n;
  230. mutex_lock(&apertures_lock);
  231. list_for_each_safe(pos, n, &apertures) {
  232. struct aperture_range *ap = container_of(pos, struct aperture_range, lh);
  233. struct device *dev = ap->dev;
  234. if (WARN_ON_ONCE(!dev))
  235. continue;
  236. if (!overlap(base, end, ap->base, ap->base + ap->size))
  237. continue;
  238. ap->dev = NULL; /* detach from device */
  239. list_del(&ap->lh);
  240. ap->detach(dev);
  241. }
  242. mutex_unlock(&apertures_lock);
  243. }
  244. /**
  245. * aperture_remove_conflicting_devices - remove devices in the given range
  246. * @base: the aperture's base address in physical memory
  247. * @size: aperture size in bytes
  248. * @primary: also kick vga16fb if present; only relevant for VGA devices
  249. * @name: a descriptive name of the requesting driver
  250. *
  251. * This function removes devices that own apertures within @base and @size.
  252. *
  253. * Returns:
  254. * 0 on success, or a negative errno code otherwise
  255. */
  256. int aperture_remove_conflicting_devices(resource_size_t base, resource_size_t size,
  257. bool primary, const char *name)
  258. {
  259. /*
  260. * If a driver asked to unregister a platform device registered by
  261. * sysfb, then can be assumed that this is a driver for a display
  262. * that is set up by the system firmware and has a generic driver.
  263. *
  264. * Drivers for devices that don't have a generic driver will never
  265. * ask for this, so let's assume that a real driver for the display
  266. * was already probed and prevent sysfb to register devices later.
  267. */
  268. sysfb_disable();
  269. aperture_detach_devices(base, size);
  270. /*
  271. * If this is the primary adapter, there could be a VGA device
  272. * that consumes the VGA framebuffer I/O range. Remove this device
  273. * as well.
  274. */
  275. if (primary)
  276. aperture_detach_devices(VGA_FB_PHYS_BASE, VGA_FB_PHYS_SIZE);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL(aperture_remove_conflicting_devices);
  280. /**
  281. * aperture_remove_conflicting_pci_devices - remove existing framebuffers for PCI devices
  282. * @pdev: PCI device
  283. * @name: a descriptive name of the requesting driver
  284. *
  285. * This function removes devices that own apertures within any of @pdev's
  286. * memory bars. The function assumes that PCI device with shadowed ROM
  287. * drives a primary display and therefore kicks out vga16fb as well.
  288. *
  289. * Returns:
  290. * 0 on success, or a negative errno code otherwise
  291. */
  292. int aperture_remove_conflicting_pci_devices(struct pci_dev *pdev, const char *name)
  293. {
  294. bool primary = false;
  295. resource_size_t base, size;
  296. int bar, ret;
  297. #ifdef CONFIG_X86
  298. primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  299. #endif
  300. for (bar = 0; bar < PCI_STD_NUM_BARS; ++bar) {
  301. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM))
  302. continue;
  303. base = pci_resource_start(pdev, bar);
  304. size = pci_resource_len(pdev, bar);
  305. ret = aperture_remove_conflicting_devices(base, size, primary, name);
  306. if (ret)
  307. return ret;
  308. }
  309. /*
  310. * WARNING: Apparently we must kick fbdev drivers before vgacon,
  311. * otherwise the vga fbdev driver falls over.
  312. */
  313. ret = vga_remove_vgacon(pdev);
  314. if (ret)
  315. return ret;
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(aperture_remove_conflicting_pci_devices);