sysfb.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic System Framebuffers
  4. * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
  5. */
  6. /*
  7. * Simple-Framebuffer support
  8. * Create a platform-device for any available boot framebuffer. The
  9. * simple-framebuffer platform device is already available on DT systems, so
  10. * this module parses the global "screen_info" object and creates a suitable
  11. * platform device compatible with the "simple-framebuffer" DT object. If
  12. * the framebuffer is incompatible, we instead create a legacy
  13. * "vesa-framebuffer", "efi-framebuffer" or "platform-framebuffer" device and
  14. * pass the screen_info as platform_data. This allows legacy drivers
  15. * to pick these devices up without messing with simple-framebuffer drivers.
  16. * The global "screen_info" is still valid at all times.
  17. *
  18. * If CONFIG_SYSFB_SIMPLEFB is not selected, never register "simple-framebuffer"
  19. * platform devices, but only use legacy framebuffer devices for
  20. * backwards compatibility.
  21. *
  22. * TODO: We set the dev_id field of all platform-devices to 0. This allows
  23. * other OF/DT parsers to create such devices, too. However, they must
  24. * start at offset 1 for this to work.
  25. */
  26. #include <linux/err.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/mm.h>
  30. #include <linux/pci.h>
  31. #include <linux/platform_data/simplefb.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/screen_info.h>
  34. #include <linux/sysfb.h>
  35. static struct platform_device *pd;
  36. static DEFINE_MUTEX(disable_lock);
  37. static bool disabled;
  38. static struct device *sysfb_parent_dev(const struct screen_info *si);
  39. static bool sysfb_unregister(void)
  40. {
  41. if (IS_ERR_OR_NULL(pd))
  42. return false;
  43. platform_device_unregister(pd);
  44. pd = NULL;
  45. return true;
  46. }
  47. /**
  48. * sysfb_disable() - disable the Generic System Framebuffers support
  49. * @dev: the device to check if non-NULL
  50. *
  51. * This disables the registration of system framebuffer devices that match the
  52. * generic drivers that make use of the system framebuffer set up by firmware.
  53. *
  54. * It also unregisters a device if this was already registered by sysfb_init().
  55. *
  56. * Context: The function can sleep. A @disable_lock mutex is acquired to serialize
  57. * against sysfb_init(), that registers a system framebuffer device.
  58. */
  59. void sysfb_disable(struct device *dev)
  60. {
  61. struct screen_info *si = &sysfb_primary_display.screen;
  62. struct device *parent;
  63. mutex_lock(&disable_lock);
  64. parent = sysfb_parent_dev(si);
  65. if (!dev || !parent || dev == parent) {
  66. sysfb_unregister();
  67. disabled = true;
  68. }
  69. mutex_unlock(&disable_lock);
  70. }
  71. EXPORT_SYMBOL_GPL(sysfb_disable);
  72. /**
  73. * sysfb_handles_screen_info() - reports if sysfb handles the global screen_info
  74. *
  75. * Callers can use sysfb_handles_screen_info() to determine whether the Generic
  76. * System Framebuffers (sysfb) can handle the global screen_info data structure
  77. * or not. Drivers might need this information to know if they have to setup the
  78. * system framebuffer, or if they have to delegate this action to sysfb instead.
  79. *
  80. * Returns:
  81. * True if sysfb handles the global screen_info data structure.
  82. */
  83. bool sysfb_handles_screen_info(void)
  84. {
  85. const struct screen_info *si = &sysfb_primary_display.screen;
  86. return !!screen_info_video_type(si);
  87. }
  88. EXPORT_SYMBOL_GPL(sysfb_handles_screen_info);
  89. #if defined(CONFIG_PCI)
  90. static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)
  91. {
  92. /*
  93. * TODO: Try to integrate this code into the PCI subsystem
  94. */
  95. int ret;
  96. u16 command;
  97. ret = pci_read_config_word(pdev, PCI_COMMAND, &command);
  98. if (ret != PCIBIOS_SUCCESSFUL)
  99. return false;
  100. if (!(command & PCI_COMMAND_MEMORY))
  101. return false;
  102. return true;
  103. }
  104. #else
  105. static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)
  106. {
  107. return false;
  108. }
  109. #endif
  110. static struct device *sysfb_parent_dev(const struct screen_info *si)
  111. {
  112. struct pci_dev *pdev;
  113. pdev = screen_info_pci_dev(si);
  114. if (IS_ERR(pdev)) {
  115. return ERR_CAST(pdev);
  116. } else if (pdev) {
  117. if (!sysfb_pci_dev_is_enabled(pdev)) {
  118. pci_dev_put(pdev);
  119. return ERR_PTR(-ENODEV);
  120. }
  121. return &pdev->dev;
  122. }
  123. return NULL;
  124. }
  125. static __init int sysfb_init(void)
  126. {
  127. struct sysfb_display_info *dpy = &sysfb_primary_display;
  128. struct screen_info *si = &dpy->screen;
  129. struct device *parent;
  130. unsigned int type;
  131. struct simplefb_platform_data mode;
  132. const char *name;
  133. bool compatible;
  134. int ret = 0;
  135. screen_info_apply_fixups();
  136. mutex_lock(&disable_lock);
  137. if (disabled)
  138. goto unlock_mutex;
  139. sysfb_apply_efi_quirks(si);
  140. parent = sysfb_parent_dev(si);
  141. if (IS_ERR(parent)) {
  142. ret = PTR_ERR(parent);
  143. goto unlock_mutex;
  144. }
  145. /* try to create a simple-framebuffer device */
  146. compatible = sysfb_parse_mode(si, &mode);
  147. if (compatible) {
  148. pd = sysfb_create_simplefb(si, &mode, parent);
  149. if (!IS_ERR(pd))
  150. goto put_device;
  151. }
  152. type = screen_info_video_type(si);
  153. /* if the FB is incompatible, create a legacy framebuffer device */
  154. switch (type) {
  155. case VIDEO_TYPE_EGAC:
  156. name = "ega-framebuffer";
  157. break;
  158. case VIDEO_TYPE_VGAC:
  159. name = "vga-framebuffer";
  160. break;
  161. case VIDEO_TYPE_VLFB:
  162. name = "vesa-framebuffer";
  163. break;
  164. case VIDEO_TYPE_EFI:
  165. name = "efi-framebuffer";
  166. break;
  167. default:
  168. name = "platform-framebuffer";
  169. break;
  170. }
  171. pd = platform_device_alloc(name, 0);
  172. if (!pd) {
  173. ret = -ENOMEM;
  174. goto put_device;
  175. }
  176. pd->dev.parent = parent;
  177. sysfb_set_efifb_fwnode(si, pd);
  178. ret = platform_device_add_data(pd, dpy, sizeof(*dpy));
  179. if (ret)
  180. goto err;
  181. ret = platform_device_add(pd);
  182. if (ret)
  183. goto err;
  184. goto put_device;
  185. err:
  186. platform_device_put(pd);
  187. put_device:
  188. put_device(parent);
  189. unlock_mutex:
  190. mutex_unlock(&disable_lock);
  191. return ret;
  192. }
  193. /* must execute after PCI subsystem for EFI quirks */
  194. device_initcall(sysfb_init);