drm_aperture.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: MIT
  2. #include <linux/aperture.h>
  3. #include <linux/platform_device.h>
  4. #include <drm/drm_aperture.h>
  5. #include <drm/drm_drv.h>
  6. #include <drm/drm_print.h>
  7. /**
  8. * DOC: overview
  9. *
  10. * A graphics device might be supported by different drivers, but only one
  11. * driver can be active at any given time. Many systems load a generic
  12. * graphics drivers, such as EFI-GOP or VESA, early during the boot process.
  13. * During later boot stages, they replace the generic driver with a dedicated,
  14. * hardware-specific driver. To take over the device the dedicated driver
  15. * first has to remove the generic driver. DRM aperture functions manage
  16. * ownership of DRM framebuffer memory and hand-over between drivers.
  17. *
  18. * DRM drivers should call drm_aperture_remove_conflicting_framebuffers()
  19. * at the top of their probe function. The function removes any generic
  20. * driver that is currently associated with the given framebuffer memory.
  21. * If the framebuffer is located at PCI BAR 0, the rsp code looks as in the
  22. * example given below.
  23. *
  24. * .. code-block:: c
  25. *
  26. * static const struct drm_driver example_driver = {
  27. * ...
  28. * };
  29. *
  30. * static int remove_conflicting_framebuffers(struct pci_dev *pdev)
  31. * {
  32. * bool primary = false;
  33. * resource_size_t base, size;
  34. * int ret;
  35. *
  36. * base = pci_resource_start(pdev, 0);
  37. * size = pci_resource_len(pdev, 0);
  38. * #ifdef CONFIG_X86
  39. * primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  40. * #endif
  41. *
  42. * return drm_aperture_remove_conflicting_framebuffers(base, size, primary,
  43. * &example_driver);
  44. * }
  45. *
  46. * static int probe(struct pci_dev *pdev)
  47. * {
  48. * int ret;
  49. *
  50. * // Remove any generic drivers...
  51. * ret = remove_conflicting_framebuffers(pdev);
  52. * if (ret)
  53. * return ret;
  54. *
  55. * // ... and initialize the hardware.
  56. * ...
  57. *
  58. * drm_dev_register();
  59. *
  60. * return 0;
  61. * }
  62. *
  63. * PCI device drivers should call
  64. * drm_aperture_remove_conflicting_pci_framebuffers() and let it detect the
  65. * framebuffer apertures automatically. Device drivers without knowledge of
  66. * the framebuffer's location shall call drm_aperture_remove_framebuffers(),
  67. * which removes all drivers for known framebuffer.
  68. *
  69. * Drivers that are susceptible to being removed by other drivers, such as
  70. * generic EFI or VESA drivers, have to register themselves as owners of their
  71. * given framebuffer memory. Ownership of the framebuffer memory is achieved
  72. * by calling devm_aperture_acquire_from_firmware(). On success, the driver
  73. * is the owner of the framebuffer range. The function fails if the
  74. * framebuffer is already owned by another driver. See below for an example.
  75. *
  76. * .. code-block:: c
  77. *
  78. * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
  79. * {
  80. * resource_size_t base, size;
  81. *
  82. * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. * if (!mem)
  84. * return -EINVAL;
  85. * base = mem->start;
  86. * size = resource_size(mem);
  87. *
  88. * return devm_acquire_aperture_from_firmware(dev, base, size);
  89. * }
  90. *
  91. * static int probe(struct platform_device *pdev)
  92. * {
  93. * struct drm_device *dev;
  94. * int ret;
  95. *
  96. * // ... Initialize the device...
  97. * dev = devm_drm_dev_alloc();
  98. * ...
  99. *
  100. * // ... and acquire ownership of the framebuffer.
  101. * ret = acquire_framebuffers(dev, pdev);
  102. * if (ret)
  103. * return ret;
  104. *
  105. * drm_dev_register(dev, 0);
  106. *
  107. * return 0;
  108. * }
  109. *
  110. * The generic driver is now subject to forced removal by other drivers. This
  111. * only works for platform drivers that support hot unplug.
  112. * When a driver calls drm_aperture_remove_conflicting_framebuffers() et al.
  113. * for the registered framebuffer range, the aperture helpers call
  114. * platform_device_unregister() and the generic driver unloads itself. It
  115. * may not access the device's registers, framebuffer memory, ROM, etc
  116. * afterwards.
  117. */
  118. /**
  119. * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
  120. * on behalf of a DRM driver.
  121. * @dev: the DRM device to own the framebuffer memory
  122. * @base: the framebuffer's byte offset in physical memory
  123. * @size: the framebuffer size in bytes
  124. *
  125. * Installs the given device as the new owner of the framebuffer. The function
  126. * expects the framebuffer to be provided by a platform device that has been
  127. * set up by firmware. Firmware can be any generic interface, such as EFI,
  128. * VESA, VGA, etc. If the native hardware driver takes over ownership of the
  129. * framebuffer range, the firmware state gets lost. Aperture helpers will then
  130. * unregister the platform device automatically. Acquired apertures are
  131. * released automatically if the underlying device goes away.
  132. *
  133. * The function fails if the framebuffer range, or parts of it, is currently
  134. * owned by another driver. To evict current owners, callers should use
  135. * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
  136. * function. The function also fails if the given device is not a platform
  137. * device.
  138. *
  139. * Returns:
  140. * 0 on success, or a negative errno value otherwise.
  141. */
  142. int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
  143. resource_size_t size)
  144. {
  145. struct platform_device *pdev;
  146. if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
  147. return -EINVAL;
  148. pdev = to_platform_device(dev->dev);
  149. return devm_aperture_acquire_for_platform_device(pdev, base, size);
  150. }
  151. EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
  152. /**
  153. * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
  154. * @base: the aperture's base address in physical memory
  155. * @size: aperture size in bytes
  156. * @primary: also kick vga16fb if present
  157. * @req_driver: requesting DRM driver
  158. *
  159. * This function removes graphics device drivers which use the memory range described by
  160. * @base and @size.
  161. *
  162. * Returns:
  163. * 0 on success, or a negative errno code otherwise
  164. */
  165. int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
  166. bool primary, const struct drm_driver *req_driver)
  167. {
  168. return aperture_remove_conflicting_devices(base, size, primary, req_driver->name);
  169. }
  170. EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
  171. /**
  172. * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
  173. * @pdev: PCI device
  174. * @req_driver: requesting DRM driver
  175. *
  176. * This function removes graphics device drivers using the memory range configured
  177. * for any of @pdev's memory bars. The function assumes that a PCI device with
  178. * shadowed ROM drives a primary display and so kicks out vga16fb.
  179. *
  180. * Returns:
  181. * 0 on success, or a negative errno code otherwise
  182. */
  183. int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
  184. const struct drm_driver *req_driver)
  185. {
  186. return aperture_remove_conflicting_pci_devices(pdev, req_driver->name);
  187. }
  188. EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);