sysfb.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = &screen_info;
  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. #if defined(CONFIG_PCI)
  73. static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)
  74. {
  75. /*
  76. * TODO: Try to integrate this code into the PCI subsystem
  77. */
  78. int ret;
  79. u16 command;
  80. ret = pci_read_config_word(pdev, PCI_COMMAND, &command);
  81. if (ret != PCIBIOS_SUCCESSFUL)
  82. return false;
  83. if (!(command & PCI_COMMAND_MEMORY))
  84. return false;
  85. return true;
  86. }
  87. #else
  88. static bool sysfb_pci_dev_is_enabled(struct pci_dev *pdev)
  89. {
  90. return false;
  91. }
  92. #endif
  93. static struct device *sysfb_parent_dev(const struct screen_info *si)
  94. {
  95. struct pci_dev *pdev;
  96. pdev = screen_info_pci_dev(si);
  97. if (IS_ERR(pdev)) {
  98. return ERR_CAST(pdev);
  99. } else if (pdev) {
  100. if (!sysfb_pci_dev_is_enabled(pdev)) {
  101. pci_dev_put(pdev);
  102. return ERR_PTR(-ENODEV);
  103. }
  104. return &pdev->dev;
  105. }
  106. return NULL;
  107. }
  108. static __init int sysfb_init(void)
  109. {
  110. struct screen_info *si = &screen_info;
  111. struct device *parent;
  112. unsigned int type;
  113. struct simplefb_platform_data mode;
  114. const char *name;
  115. bool compatible;
  116. int ret = 0;
  117. screen_info_apply_fixups();
  118. mutex_lock(&disable_lock);
  119. if (disabled)
  120. goto unlock_mutex;
  121. sysfb_apply_efi_quirks();
  122. parent = sysfb_parent_dev(si);
  123. if (IS_ERR(parent)) {
  124. ret = PTR_ERR(parent);
  125. goto unlock_mutex;
  126. }
  127. /* try to create a simple-framebuffer device */
  128. compatible = sysfb_parse_mode(si, &mode);
  129. if (compatible) {
  130. pd = sysfb_create_simplefb(si, &mode, parent);
  131. if (!IS_ERR(pd))
  132. goto put_device;
  133. }
  134. type = screen_info_video_type(si);
  135. /* if the FB is incompatible, create a legacy framebuffer device */
  136. switch (type) {
  137. case VIDEO_TYPE_EGAC:
  138. name = "ega-framebuffer";
  139. break;
  140. case VIDEO_TYPE_VGAC:
  141. name = "vga-framebuffer";
  142. break;
  143. case VIDEO_TYPE_VLFB:
  144. name = "vesa-framebuffer";
  145. break;
  146. case VIDEO_TYPE_EFI:
  147. name = "efi-framebuffer";
  148. break;
  149. default:
  150. name = "platform-framebuffer";
  151. break;
  152. }
  153. pd = platform_device_alloc(name, 0);
  154. if (!pd) {
  155. ret = -ENOMEM;
  156. goto put_device;
  157. }
  158. pd->dev.parent = parent;
  159. sysfb_set_efifb_fwnode(pd);
  160. ret = platform_device_add_data(pd, si, sizeof(*si));
  161. if (ret)
  162. goto err;
  163. ret = platform_device_add(pd);
  164. if (ret)
  165. goto err;
  166. goto put_device;
  167. err:
  168. platform_device_put(pd);
  169. put_device:
  170. put_device(parent);
  171. unlock_mutex:
  172. mutex_unlock(&disable_lock);
  173. return ret;
  174. }
  175. /* must execute after PCI subsystem for EFI quirks */
  176. device_initcall(sysfb_init);