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