sysfb_efi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. * EFI Quirks Copyright (c) 2006 Edgar Hucek <gimli@dark-green.com>
  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. #ifdef CONFIG_EFI
  87. static int __init efifb_set_system(struct screen_info *si, const struct dmi_system_id *id)
  88. {
  89. struct efifb_dmi_info *info = id->driver_data;
  90. if (info->base == 0 && info->height == 0 && info->width == 0 &&
  91. info->stride == 0)
  92. return 0;
  93. /* Trust the bootloader over the DMI tables */
  94. if (si->lfb_base == 0) {
  95. #if defined(CONFIG_PCI)
  96. struct pci_dev *dev = NULL;
  97. int found_bar = 0;
  98. #endif
  99. if (info->base) {
  100. si->lfb_base = choose_value(info->base,
  101. si->lfb_base, OVERRIDE_BASE,
  102. info->flags);
  103. #if defined(CONFIG_PCI)
  104. /* make sure that the address in the table is actually
  105. * on a VGA device's PCI BAR */
  106. for_each_pci_dev(dev) {
  107. int i;
  108. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  109. continue;
  110. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  111. resource_size_t start, end;
  112. unsigned long flags;
  113. flags = pci_resource_flags(dev, i);
  114. if (!(flags & IORESOURCE_MEM))
  115. continue;
  116. if (flags & IORESOURCE_UNSET)
  117. continue;
  118. if (pci_resource_len(dev, i) == 0)
  119. continue;
  120. start = pci_resource_start(dev, i);
  121. end = pci_resource_end(dev, i);
  122. if (si->lfb_base >= start && si->lfb_base < end) {
  123. found_bar = 1;
  124. break;
  125. }
  126. }
  127. }
  128. if (!found_bar)
  129. si->lfb_base = 0;
  130. #endif
  131. }
  132. }
  133. if (si->lfb_base) {
  134. si->lfb_linelength = choose_value(info->stride,
  135. si->lfb_linelength, OVERRIDE_STRIDE,
  136. info->flags);
  137. si->lfb_width = choose_value(info->width,
  138. si->lfb_width, OVERRIDE_WIDTH,
  139. info->flags);
  140. si->lfb_height = choose_value(info->height,
  141. si->lfb_height, OVERRIDE_HEIGHT,
  142. info->flags);
  143. if (si->orig_video_isVGA == 0)
  144. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  145. } else {
  146. si->lfb_linelength = 0;
  147. si->lfb_width = 0;
  148. si->lfb_height = 0;
  149. si->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. si->lfb_base, si->lfb_width,
  155. si->lfb_height, si->lfb_linelength);
  156. return 1;
  157. }
  158. static int __init efifb_set_system_callback(const struct dmi_system_id *id)
  159. {
  160. return efifb_set_system(&sysfb_primary_display.screen, id);
  161. }
  162. #define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
  163. { \
  164. efifb_set_system_callback, \
  165. name, \
  166. { \
  167. DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
  168. DMI_MATCH(DMI_PRODUCT_NAME, name) \
  169. }, \
  170. &efifb_dmi_list[enumid] \
  171. }
  172. static const struct dmi_system_id efifb_dmi_system_table[] __initconst = {
  173. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
  174. /* At least one of these two will be right; maybe both? */
  175. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
  176. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
  177. /* At least one of these two will be right; maybe both? */
  178. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
  179. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
  180. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
  181. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac8,1", M_I24_8_1),
  182. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac10,1", M_I24_10_1),
  183. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac11,1", M_I27_11_1),
  184. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
  185. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini3,1", M_MINI_3_1),
  186. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini4,1", M_MINI_4_1),
  187. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
  188. /* At least one of these two will be right; maybe both? */
  189. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
  190. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
  191. /* At least one of these two will be right; maybe both? */
  192. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
  193. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
  194. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
  195. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook5,1", M_MB_5_1),
  196. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1),
  197. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1),
  198. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
  199. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir3,1", M_MBA_3),
  200. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
  201. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
  202. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2),
  203. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
  204. EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
  205. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
  206. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
  207. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
  208. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,2", M_MBP_5_2),
  209. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,3", M_MBP_5_3),
  210. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,1", M_MBP_6_1),
  211. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,2", M_MBP_6_2),
  212. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro7,1", M_MBP_7_1),
  213. EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro8,2", M_MBP_8_2),
  214. {},
  215. };
  216. static int __init efifb_swap_width_height(const struct dmi_system_id *id)
  217. {
  218. struct screen_info *si = &sysfb_primary_display.screen;
  219. u32 bpp = __screen_info_lfb_bits_per_pixel(si);
  220. swap(si->lfb_width, si->lfb_height);
  221. si->lfb_linelength = bpp * si->lfb_width / BITS_PER_BYTE;
  222. return 1;
  223. }
  224. struct efifb_mode_fixup {
  225. unsigned int width;
  226. unsigned int height;
  227. unsigned int linelength;
  228. };
  229. static int __init
  230. efifb_check_and_swap_width_height(const struct dmi_system_id *id)
  231. {
  232. const struct efifb_mode_fixup *data = id->driver_data;
  233. struct screen_info *si = &sysfb_primary_display.screen;
  234. if (data->width == si->lfb_width && data->height == si->lfb_height) {
  235. swap(si->lfb_width, si->lfb_height);
  236. si->lfb_linelength = data->linelength;
  237. si->lfb_size = data->linelength * data->width;
  238. }
  239. return 1;
  240. }
  241. static const struct efifb_mode_fixup efifb_steamdeck_mode_fixup __initconst = {
  242. .width = 1280,
  243. .height = 800,
  244. .linelength = 3328,
  245. };
  246. /*
  247. * Some devices have a portrait LCD but advertise a landscape resolution (and
  248. * pitch). We simply swap width and height for these devices so that we can
  249. * correctly deal with some of them coming with multiple resolutions.
  250. */
  251. static const struct dmi_system_id efifb_dmi_swap_width_height[] __initconst = {
  252. {
  253. /*
  254. * Lenovo MIIX310-10ICR, only some batches have the troublesome
  255. * 800x1280 portrait screen. Luckily the portrait version has
  256. * its own BIOS version, so we match on that.
  257. */
  258. .matches = {
  259. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  260. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10ICR"),
  261. DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1HCN44WW"),
  262. },
  263. .callback = efifb_swap_width_height,
  264. },
  265. {
  266. /* Lenovo MIIX 320-10ICR with 800x1280 portrait screen */
  267. .matches = {
  268. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  269. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  270. "Lenovo MIIX 320-10ICR"),
  271. },
  272. .callback = efifb_swap_width_height,
  273. },
  274. {
  275. /* Lenovo D330 with 800x1280 or 1200x1920 portrait screen */
  276. .matches = {
  277. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  278. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  279. "Lenovo ideapad D330-10IGM"),
  280. },
  281. .callback = efifb_swap_width_height,
  282. },
  283. {
  284. /* Lenovo IdeaPad Duet 3 10IGL5 with 1200x1920 portrait screen */
  285. .matches = {
  286. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  287. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
  288. "IdeaPad Duet 3 10IGL5"),
  289. },
  290. .callback = efifb_swap_width_height,
  291. },
  292. {
  293. /* Lenovo Yoga Book X91F / X91L */
  294. .matches = {
  295. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  296. /* Non exact match to match F + L versions */
  297. DMI_MATCH(DMI_PRODUCT_NAME, "Lenovo YB1-X91"),
  298. },
  299. .callback = efifb_swap_width_height,
  300. },
  301. {
  302. /* Valve Steam Deck (Jupiter) */
  303. .matches = {
  304. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Valve"),
  305. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"),
  306. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "1"),
  307. },
  308. .callback = efifb_check_and_swap_width_height,
  309. .driver_data = (void *)&efifb_steamdeck_mode_fixup,
  310. },
  311. {
  312. /* Valve Steam Deck (Galileo) */
  313. .matches = {
  314. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Valve"),
  315. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Galileo"),
  316. DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "1"),
  317. },
  318. .callback = efifb_check_and_swap_width_height,
  319. .driver_data = (void *)&efifb_steamdeck_mode_fixup,
  320. },
  321. {},
  322. };
  323. static bool efifb_overlaps_pci_range(const struct screen_info *si,
  324. const struct of_pci_range *range)
  325. {
  326. u64 fb_base = si->lfb_base;
  327. if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  328. fb_base |= (u64)(unsigned long)si->ext_lfb_base << 32;
  329. return fb_base >= range->cpu_addr &&
  330. fb_base < (range->cpu_addr + range->size);
  331. }
  332. static struct device_node *find_pci_overlap_node(void)
  333. {
  334. struct device_node *np;
  335. for_each_node_by_type(np, "pci") {
  336. struct of_pci_range_parser parser;
  337. struct of_pci_range range;
  338. int err;
  339. err = of_pci_range_parser_init(&parser, np);
  340. if (err) {
  341. pr_warn("of_pci_range_parser_init() failed: %d\n", err);
  342. continue;
  343. }
  344. for_each_of_pci_range(&parser, &range)
  345. if (efifb_overlaps_pci_range(&sysfb_primary_display.screen, &range))
  346. return np;
  347. }
  348. return NULL;
  349. }
  350. /*
  351. * If the efifb framebuffer is backed by a PCI graphics controller, we have
  352. * to ensure that this relation is expressed using a device link when
  353. * running in DT mode, or the probe order may be reversed, resulting in a
  354. * resource reservation conflict on the memory window that the efifb
  355. * framebuffer steals from the PCIe host bridge.
  356. */
  357. static int efifb_add_links(struct fwnode_handle *fwnode)
  358. {
  359. struct device_node *sup_np;
  360. sup_np = find_pci_overlap_node();
  361. /*
  362. * If there's no PCI graphics controller backing the efifb, we are
  363. * done here.
  364. */
  365. if (!sup_np)
  366. return 0;
  367. fwnode_link_add(fwnode, of_fwnode_handle(sup_np), 0);
  368. of_node_put(sup_np);
  369. return 0;
  370. }
  371. static const struct fwnode_operations efifb_fwnode_ops = {
  372. .add_links = efifb_add_links,
  373. };
  374. static struct fwnode_handle efifb_fwnode;
  375. __init void sysfb_apply_efi_quirks(struct screen_info *si)
  376. {
  377. if (si->orig_video_isVGA != VIDEO_TYPE_EFI ||
  378. !(si->capabilities & VIDEO_CAPABILITY_SKIP_QUIRKS))
  379. dmi_check_system(efifb_dmi_system_table);
  380. if (si->orig_video_isVGA == VIDEO_TYPE_EFI)
  381. dmi_check_system(efifb_dmi_swap_width_height);
  382. }
  383. __init void sysfb_set_efifb_fwnode(const struct screen_info *si, struct platform_device *pd)
  384. {
  385. if (si->orig_video_isVGA == VIDEO_TYPE_EFI && IS_ENABLED(CONFIG_PCI)) {
  386. fwnode_init(&efifb_fwnode, &efifb_fwnode_ops);
  387. pd->dev.fwnode = &efifb_fwnode;
  388. }
  389. }
  390. #endif