sysfb_efi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic System Framebuffers
  4. * Copyright (c) 2012-2013 David Herrmann <[email protected]>
  5. *
  6. * EFI Quirks Copyright (c) 2006 Edgar Hucek <[email protected]>
  7. */
  8. /*
  9. * EFI Quirks
  10. * Several EFI systems do not correctly advertise their boot framebuffers.
  11. * Hence, we use this static table of known broken machines and fix up the
  12. * information so framebuffer drivers can load correctly.
  13. */
  14. #include <linux/dmi.h>
  15. #include <linux/err.h>
  16. #include <linux/efi.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/of_address.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/screen_info.h>
  24. #include <linux/sysfb.h>
  25. #include <video/vga.h>
  26. enum {
  27. OVERRIDE_NONE = 0x0,
  28. OVERRIDE_BASE = 0x1,
  29. OVERRIDE_STRIDE = 0x2,
  30. OVERRIDE_HEIGHT = 0x4,
  31. OVERRIDE_WIDTH = 0x8,
  32. };
  33. struct efifb_dmi_info efifb_dmi_list[] = {
  34. [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
  35. [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE }, /* guess */
  36. [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE },
  37. [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, /* guess */
  38. [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
  39. [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080, OVERRIDE_NONE },
  40. [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440, OVERRIDE_NONE },
  41. [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768, OVERRIDE_NONE },
  42. [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768, OVERRIDE_NONE },
  43. [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
  44. [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  45. [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  46. [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  47. [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  48. [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  49. /* 11" Macbook Air 3,1 passes the wrong stride */
  50. [M_MBA_3] = { "mba3", 0, 2048 * 4, 0, 0, OVERRIDE_STRIDE },
  51. [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
  52. [M_MBP_2] = { "mbp2", 0, 0, 0, 0, OVERRIDE_NONE }, /* placeholder */
  53. [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
  54. [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
  55. [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
  56. [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
  57. [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
  58. [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
  59. [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
  60. [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050, OVERRIDE_NONE },
  61. [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
  62. [M_MBP_8_2] = { "mbp82", 0x90010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
  63. [M_UNKNOWN] = { NULL, 0, 0, 0, 0, OVERRIDE_NONE }
  64. };
  65. void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
  66. {
  67. int i;
  68. for (i = 0; i < M_UNKNOWN; i++) {
  69. if (efifb_dmi_list[i].base != 0 &&
  70. !strcmp(opt, efifb_dmi_list[i].optname)) {
  71. si->lfb_base = efifb_dmi_list[i].base;
  72. si->lfb_linelength = efifb_dmi_list[i].stride;
  73. si->lfb_width = efifb_dmi_list[i].width;
  74. si->lfb_height = efifb_dmi_list[i].height;
  75. }
  76. }
  77. }
  78. #define choose_value(dmivalue, fwvalue, field, flags) ({ \
  79. typeof(fwvalue) _ret_ = fwvalue; \
  80. if ((flags) & (field)) \
  81. _ret_ = dmivalue; \
  82. else if ((fwvalue) == 0) \
  83. _ret_ = dmivalue; \
  84. _ret_; \
  85. })
  86. static int __init efifb_set_system(const struct dmi_system_id *id)
  87. {
  88. struct efifb_dmi_info *info = id->driver_data;
  89. if (info->base == 0 && info->height == 0 && info->width == 0 &&
  90. info->stride == 0)
  91. return 0;
  92. /* Trust the bootloader over the DMI tables */
  93. if (screen_info.lfb_base == 0) {
  94. #if defined(CONFIG_PCI)
  95. struct pci_dev *dev = NULL;
  96. int found_bar = 0;
  97. #endif
  98. if (info->base) {
  99. screen_info.lfb_base = choose_value(info->base,
  100. screen_info.lfb_base, OVERRIDE_BASE,
  101. info->flags);
  102. #if defined(CONFIG_PCI)
  103. /* make sure that the address in the table is actually
  104. * on a VGA device's PCI BAR */
  105. for_each_pci_dev(dev) {
  106. int i;
  107. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  108. continue;
  109. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  110. resource_size_t start, end;
  111. unsigned long flags;
  112. flags = pci_resource_flags(dev, i);
  113. if (!(flags & IORESOURCE_MEM))
  114. continue;
  115. if (flags & IORESOURCE_UNSET)
  116. continue;
  117. if (pci_resource_len(dev, i) == 0)
  118. continue;
  119. start = pci_resource_start(dev, i);
  120. end = pci_resource_end(dev, i);
  121. if (screen_info.lfb_base >= start &&
  122. screen_info.lfb_base < end) {
  123. found_bar = 1;
  124. break;
  125. }
  126. }
  127. }
  128. if (!found_bar)
  129. screen_info.lfb_base = 0;
  130. #endif
  131. }
  132. }
  133. if (screen_info.lfb_base) {
  134. screen_info.lfb_linelength = choose_value(info->stride,
  135. screen_info.lfb_linelength, OVERRIDE_STRIDE,
  136. info->flags);
  137. screen_info.lfb_width = choose_value(info->width,
  138. screen_info.lfb_width, OVERRIDE_WIDTH,
  139. info->flags);
  140. screen_info.lfb_height = choose_value(info->height,
  141. screen_info.lfb_height, OVERRIDE_HEIGHT,
  142. info->flags);
  143. if (screen_info.orig_video_isVGA == 0)
  144. screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
  145. } else {
  146. screen_info.lfb_linelength = 0;
  147. screen_info.lfb_width = 0;
  148. screen_info.lfb_height = 0;
  149. screen_info.orig_video_isVGA = 0;
  150. return 0;
  151. }
  152. printk(KERN_INFO "efifb: dmi detected %s - framebuffer at 0x%08x "
  153. "(%dx%d, stride %d)\n", id->ident,
  154. screen_info.lfb_base, screen_info.lfb_width,
  155. screen_info.lfb_height, screen_info.lfb_linelength);
  156. return 1;
  157. }
  158. #define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
  159. { \
  160. efifb_set_system, \
  161. name, \
  162. { \
  163. DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
  164. DMI_MATCH(DMI_PRODUCT_NAME, name) \
  165. }, \
  166. &efifb_dmi_list[enumid] \
  167. }
  168. static const struct dmi_system_id efifb_dmi_system_table[] __initconst = {
  169. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
  170. /* At least one of these two will be right; maybe both? */
  171. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
  172. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
  173. /* At least one of these two will be right; maybe both? */
  174. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
  175. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
  176. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
  177. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac8,1", M_I24_8_1),
  178. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac10,1", M_I24_10_1),
  179. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac11,1", M_I27_11_1),
  180. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
  181. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini3,1", M_MINI_3_1),
  182. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini4,1", M_MINI_4_1),
  183. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
  184. /* At least one of these two will be right; maybe both? */
  185. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
  186. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
  187. /* At least one of these two will be right; maybe both? */
  188. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
  189. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
  190. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
  191. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook5,1", M_MB_5_1),
  192. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1),
  193. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1),
  194. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
  195. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir3,1", M_MBA_3),
  196. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
  197. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
  198. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2),
  199. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
  200. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
  201. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
  202. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
  203. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
  204. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,2", M_MBP_5_2),
  205. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,3", M_MBP_5_3),
  206. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,1", M_MBP_6_1),
  207. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,2", M_MBP_6_2),
  208. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro7,1", M_MBP_7_1),
  209. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro8,2", M_MBP_8_2),
  210. {},
  211. };
  212. /*
  213. * Some devices have a portrait LCD but advertise a landscape resolution (and
  214. * pitch). We simply swap width and height for these devices so that we can
  215. * correctly deal with some of them coming with multiple resolutions.
  216. */
  217. static const struct dmi_system_id efifb_dmi_swap_width_height[] __initconst = {
  218. {
  219. /*
  220. * Lenovo MIIX310-10ICR, only some batches have the troublesome
  221. * 800x1280 portrait screen. Luckily the portrait version has
  222. * its own BIOS version, so we match on that.
  223. */
  224. .matches = {
  225. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  226. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10ICR"),
  227. DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1HCN44WW"),
  228. },
  229. },
  230. {
  231. /* Lenovo MIIX 320-10ICR with 800x1280 portrait screen */
  232. .matches = {
  233. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  234. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  235. "Lenovo MIIX 320-10ICR"),
  236. },
  237. },
  238. {
  239. /* Lenovo D330 with 800x1280 or 1200x1920 portrait screen */
  240. .matches = {
  241. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  242. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  243. "Lenovo ideapad D330-10IGM"),
  244. },
  245. },
  246. {
  247. /* Lenovo IdeaPad Duet 3 10IGL5 with 1200x1920 portrait screen */
  248. .matches = {
  249. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  250. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  251. "IdeaPad Duet 3 10IGL5"),
  252. },
  253. },
  254. {
  255. /* Lenovo Yoga Book X91F / X91L */
  256. .matches = {
  257. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  258. /* Non exact match to match F + L versions */
  259. DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
  260. },
  261. },
  262. {},
  263. };
  264. static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
  265. {
  266. u64 fb_base = screen_info.lfb_base;
  267. if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  268. fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
  269. return fb_base >= range->cpu_addr &&
  270. fb_base < (range->cpu_addr + range->size);
  271. }
  272. static struct device_node *find_pci_overlap_node(void)
  273. {
  274. struct device_node *np;
  275. for_each_node_by_type(np, "pci") {
  276. struct of_pci_range_parser parser;
  277. struct of_pci_range range;
  278. int err;
  279. err = of_pci_range_parser_init(&parser, np);
  280. if (err) {
  281. pr_warn("of_pci_range_parser_init() failed: %d\n", err);
  282. continue;
  283. }
  284. for_each_of_pci_range(&parser, &range)
  285. if (efifb_overlaps_pci_range(&range))
  286. return np;
  287. }
  288. return NULL;
  289. }
  290. /*
  291. * If the efifb framebuffer is backed by a PCI graphics controller, we have
  292. * to ensure that this relation is expressed using a device link when
  293. * running in DT mode, or the probe order may be reversed, resulting in a
  294. * resource reservation conflict on the memory window that the efifb
  295. * framebuffer steals from the PCIe host bridge.
  296. */
  297. static int efifb_add_links(struct fwnode_handle *fwnode)
  298. {
  299. struct device_node *sup_np;
  300. sup_np = find_pci_overlap_node();
  301. /*
  302. * If there's no PCI graphics controller backing the efifb, we are
  303. * done here.
  304. */
  305. if (!sup_np)
  306. return 0;
  307. fwnode_link_add(fwnode, of_fwnode_handle(sup_np));
  308. of_node_put(sup_np);
  309. return 0;
  310. }
  311. static const struct fwnode_operations efifb_fwnode_ops = {
  312. .add_links = efifb_add_links,
  313. };
  314. #ifdef CONFIG_EFI
  315. static struct fwnode_handle efifb_fwnode;
  316. __init void sysfb_apply_efi_quirks(void)
  317. {
  318. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI ||
  319. !(screen_info.capabilities & VIDEO_CAPABILITY_SKIP_QUIRKS))
  320. dmi_check_system(efifb_dmi_system_table);
  321. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
  322. dmi_check_system(efifb_dmi_swap_width_height)) {
  323. u16 temp = screen_info.lfb_width;
  324. screen_info.lfb_width = screen_info.lfb_height;
  325. screen_info.lfb_height = temp;
  326. screen_info.lfb_linelength = 4 * screen_info.lfb_width;
  327. }
  328. }
  329. __init void sysfb_set_efifb_fwnode(struct platform_device *pd)
  330. {
  331. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI && IS_ENABLED(CONFIG_PCI)) {
  332. fwnode_init(&efifb_fwnode, &efifb_fwnode_ops);
  333. pd->dev.fwnode = &efifb_fwnode;
  334. }
  335. }
  336. #endif