drm_aperture.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * resource_size_t base, size;
  33. * int ret;
  34. *
  35. * base = pci_resource_start(pdev, 0);
  36. * size = pci_resource_len(pdev, 0);
  37. *
  38. * return drm_aperture_remove_conflicting_framebuffers(base, size,
  39. * &example_driver);
  40. * }
  41. *
  42. * static int probe(struct pci_dev *pdev)
  43. * {
  44. * int ret;
  45. *
  46. * // Remove any generic drivers...
  47. * ret = remove_conflicting_framebuffers(pdev);
  48. * if (ret)
  49. * return ret;
  50. *
  51. * // ... and initialize the hardware.
  52. * ...
  53. *
  54. * drm_dev_register();
  55. *
  56. * return 0;
  57. * }
  58. *
  59. * PCI device drivers should call
  60. * drm_aperture_remove_conflicting_pci_framebuffers() and let it detect the
  61. * framebuffer apertures automatically. Device drivers without knowledge of
  62. * the framebuffer's location shall call drm_aperture_remove_framebuffers(),
  63. * which removes all drivers for known framebuffer.
  64. *
  65. * Drivers that are susceptible to being removed by other drivers, such as
  66. * generic EFI or VESA drivers, have to register themselves as owners of their
  67. * given framebuffer memory. Ownership of the framebuffer memory is achieved
  68. * by calling devm_aperture_acquire_from_firmware(). On success, the driver
  69. * is the owner of the framebuffer range. The function fails if the
  70. * framebuffer is already owned by another driver. See below for an example.
  71. *
  72. * .. code-block:: c
  73. *
  74. * static int acquire_framebuffers(struct drm_device *dev, struct platform_device *pdev)
  75. * {
  76. * resource_size_t base, size;
  77. *
  78. * mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  79. * if (!mem)
  80. * return -EINVAL;
  81. * base = mem->start;
  82. * size = resource_size(mem);
  83. *
  84. * return devm_acquire_aperture_from_firmware(dev, base, size);
  85. * }
  86. *
  87. * static int probe(struct platform_device *pdev)
  88. * {
  89. * struct drm_device *dev;
  90. * int ret;
  91. *
  92. * // ... Initialize the device...
  93. * dev = devm_drm_dev_alloc();
  94. * ...
  95. *
  96. * // ... and acquire ownership of the framebuffer.
  97. * ret = acquire_framebuffers(dev, pdev);
  98. * if (ret)
  99. * return ret;
  100. *
  101. * drm_dev_register(dev, 0);
  102. *
  103. * return 0;
  104. * }
  105. *
  106. * The generic driver is now subject to forced removal by other drivers. This
  107. * only works for platform drivers that support hot unplug.
  108. * When a driver calls drm_aperture_remove_conflicting_framebuffers() et al.
  109. * for the registered framebuffer range, the aperture helpers call
  110. * platform_device_unregister() and the generic driver unloads itself. It
  111. * may not access the device's registers, framebuffer memory, ROM, etc
  112. * afterwards.
  113. */
  114. /**
  115. * devm_aperture_acquire_from_firmware - Acquires ownership of a firmware framebuffer
  116. * on behalf of a DRM driver.
  117. * @dev: the DRM device to own the framebuffer memory
  118. * @base: the framebuffer's byte offset in physical memory
  119. * @size: the framebuffer size in bytes
  120. *
  121. * Installs the given device as the new owner of the framebuffer. The function
  122. * expects the framebuffer to be provided by a platform device that has been
  123. * set up by firmware. Firmware can be any generic interface, such as EFI,
  124. * VESA, VGA, etc. If the native hardware driver takes over ownership of the
  125. * framebuffer range, the firmware state gets lost. Aperture helpers will then
  126. * unregister the platform device automatically. Acquired apertures are
  127. * released automatically if the underlying device goes away.
  128. *
  129. * The function fails if the framebuffer range, or parts of it, is currently
  130. * owned by another driver. To evict current owners, callers should use
  131. * drm_aperture_remove_conflicting_framebuffers() et al. before calling this
  132. * function. The function also fails if the given device is not a platform
  133. * device.
  134. *
  135. * Returns:
  136. * 0 on success, or a negative errno value otherwise.
  137. */
  138. int devm_aperture_acquire_from_firmware(struct drm_device *dev, resource_size_t base,
  139. resource_size_t size)
  140. {
  141. struct platform_device *pdev;
  142. if (drm_WARN_ON(dev, !dev_is_platform(dev->dev)))
  143. return -EINVAL;
  144. pdev = to_platform_device(dev->dev);
  145. return devm_aperture_acquire_for_platform_device(pdev, base, size);
  146. }
  147. EXPORT_SYMBOL(devm_aperture_acquire_from_firmware);
  148. /**
  149. * drm_aperture_remove_conflicting_framebuffers - remove existing framebuffers in the given range
  150. * @base: the aperture's base address in physical memory
  151. * @size: aperture size in bytes
  152. * @req_driver: requesting DRM driver
  153. *
  154. * This function removes graphics device drivers which use the memory range described by
  155. * @base and @size.
  156. *
  157. * Returns:
  158. * 0 on success, or a negative errno code otherwise
  159. */
  160. int drm_aperture_remove_conflicting_framebuffers(resource_size_t base, resource_size_t size,
  161. const struct drm_driver *req_driver)
  162. {
  163. return aperture_remove_conflicting_devices(base, size, req_driver->name);
  164. }
  165. EXPORT_SYMBOL(drm_aperture_remove_conflicting_framebuffers);
  166. /**
  167. * drm_aperture_remove_conflicting_pci_framebuffers - remove existing framebuffers for PCI devices
  168. * @pdev: PCI device
  169. * @req_driver: requesting DRM driver
  170. *
  171. * This function removes graphics device drivers using the memory range configured
  172. * for any of @pdev's memory bars. The function assumes that a PCI device with
  173. * shadowed ROM drives a primary display and so kicks out vga16fb.
  174. *
  175. * Returns:
  176. * 0 on success, or a negative errno code otherwise
  177. */
  178. int drm_aperture_remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
  179. const struct drm_driver *req_driver)
  180. {
  181. return aperture_remove_conflicting_pci_devices(pdev, req_driver->name);
  182. }
  183. EXPORT_SYMBOL(drm_aperture_remove_conflicting_pci_framebuffers);