Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Generic System Framebuffers
0004  * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
0005  *
0006  * EFI Quirks Copyright (c) 2006 Edgar Hucek <gimli@dark-green.com>
0007  */
0008 
0009 /*
0010  * EFI Quirks
0011  * Several EFI systems do not correctly advertise their boot framebuffers.
0012  * Hence, we use this static table of known broken machines and fix up the
0013  * information so framebuffer drivers can load correctly.
0014  */
0015 
0016 #include <linux/dmi.h>
0017 #include <linux/err.h>
0018 #include <linux/efi.h>
0019 #include <linux/init.h>
0020 #include <linux/kernel.h>
0021 #include <linux/mm.h>
0022 #include <linux/of_address.h>
0023 #include <linux/pci.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/screen_info.h>
0026 #include <linux/sysfb.h>
0027 #include <video/vga.h>
0028 
0029 enum {
0030     OVERRIDE_NONE = 0x0,
0031     OVERRIDE_BASE = 0x1,
0032     OVERRIDE_STRIDE = 0x2,
0033     OVERRIDE_HEIGHT = 0x4,
0034     OVERRIDE_WIDTH = 0x8,
0035 };
0036 
0037 struct efifb_dmi_info efifb_dmi_list[] = {
0038     [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
0039     [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE }, /* guess */
0040     [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE },
0041     [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, /* guess */
0042     [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
0043     [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080, OVERRIDE_NONE },
0044     [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440, OVERRIDE_NONE },
0045     [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768, OVERRIDE_NONE },
0046     [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768, OVERRIDE_NONE },
0047     [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
0048     [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0049     [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0050     [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0051     [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0052     [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0053     /* 11" Macbook Air 3,1 passes the wrong stride */
0054     [M_MBA_3] = { "mba3", 0, 2048 * 4, 0, 0, OVERRIDE_STRIDE },
0055     [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
0056     [M_MBP_2] = { "mbp2", 0, 0, 0, 0, OVERRIDE_NONE }, /* placeholder */
0057     [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
0058     [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
0059     [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
0060     [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
0061     [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
0062     [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE },
0063     [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200, OVERRIDE_NONE },
0064     [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050, OVERRIDE_NONE },
0065     [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800, OVERRIDE_NONE },
0066     [M_MBP_8_2] = { "mbp82", 0x90010000, 1472 * 4, 1440, 900, OVERRIDE_NONE },
0067     [M_UNKNOWN] = { NULL, 0, 0, 0, 0, OVERRIDE_NONE }
0068 };
0069 
0070 void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
0071 {
0072     int i;
0073 
0074     for (i = 0; i < M_UNKNOWN; i++) {
0075         if (efifb_dmi_list[i].base != 0 &&
0076             !strcmp(opt, efifb_dmi_list[i].optname)) {
0077             si->lfb_base = efifb_dmi_list[i].base;
0078             si->lfb_linelength = efifb_dmi_list[i].stride;
0079             si->lfb_width = efifb_dmi_list[i].width;
0080             si->lfb_height = efifb_dmi_list[i].height;
0081         }
0082     }
0083 }
0084 
0085 #define choose_value(dmivalue, fwvalue, field, flags) ({    \
0086         typeof(fwvalue) _ret_ = fwvalue;        \
0087         if ((flags) & (field))              \
0088             _ret_ = dmivalue;           \
0089         else if ((fwvalue) == 0)            \
0090             _ret_ = dmivalue;           \
0091         _ret_;                      \
0092     })
0093 
0094 static int __init efifb_set_system(const struct dmi_system_id *id)
0095 {
0096     struct efifb_dmi_info *info = id->driver_data;
0097 
0098     if (info->base == 0 && info->height == 0 && info->width == 0 &&
0099         info->stride == 0)
0100         return 0;
0101 
0102     /* Trust the bootloader over the DMI tables */
0103     if (screen_info.lfb_base == 0) {
0104 #if defined(CONFIG_PCI)
0105         struct pci_dev *dev = NULL;
0106         int found_bar = 0;
0107 #endif
0108         if (info->base) {
0109             screen_info.lfb_base = choose_value(info->base,
0110                 screen_info.lfb_base, OVERRIDE_BASE,
0111                 info->flags);
0112 
0113 #if defined(CONFIG_PCI)
0114             /* make sure that the address in the table is actually
0115              * on a VGA device's PCI BAR */
0116 
0117             for_each_pci_dev(dev) {
0118                 int i;
0119                 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
0120                     continue;
0121                 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
0122                     resource_size_t start, end;
0123                     unsigned long flags;
0124 
0125                     flags = pci_resource_flags(dev, i);
0126                     if (!(flags & IORESOURCE_MEM))
0127                         continue;
0128 
0129                     if (flags & IORESOURCE_UNSET)
0130                         continue;
0131 
0132                     if (pci_resource_len(dev, i) == 0)
0133                         continue;
0134 
0135                     start = pci_resource_start(dev, i);
0136                     end = pci_resource_end(dev, i);
0137                     if (screen_info.lfb_base >= start &&
0138                         screen_info.lfb_base < end) {
0139                         found_bar = 1;
0140                         break;
0141                     }
0142                 }
0143             }
0144             if (!found_bar)
0145                 screen_info.lfb_base = 0;
0146 #endif
0147         }
0148     }
0149     if (screen_info.lfb_base) {
0150         screen_info.lfb_linelength = choose_value(info->stride,
0151             screen_info.lfb_linelength, OVERRIDE_STRIDE,
0152             info->flags);
0153         screen_info.lfb_width = choose_value(info->width,
0154             screen_info.lfb_width, OVERRIDE_WIDTH,
0155             info->flags);
0156         screen_info.lfb_height = choose_value(info->height,
0157             screen_info.lfb_height, OVERRIDE_HEIGHT,
0158             info->flags);
0159         if (screen_info.orig_video_isVGA == 0)
0160             screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
0161     } else {
0162         screen_info.lfb_linelength = 0;
0163         screen_info.lfb_width = 0;
0164         screen_info.lfb_height = 0;
0165         screen_info.orig_video_isVGA = 0;
0166         return 0;
0167     }
0168 
0169     printk(KERN_INFO "efifb: dmi detected %s - framebuffer at 0x%08x "
0170              "(%dx%d, stride %d)\n", id->ident,
0171              screen_info.lfb_base, screen_info.lfb_width,
0172              screen_info.lfb_height, screen_info.lfb_linelength);
0173 
0174     return 1;
0175 }
0176 
0177 #define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid)       \
0178     {                           \
0179         efifb_set_system,               \
0180         name,                       \
0181         {                       \
0182             DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
0183             DMI_MATCH(DMI_PRODUCT_NAME, name)   \
0184         },                      \
0185         &efifb_dmi_list[enumid]             \
0186     }
0187 
0188 static const struct dmi_system_id efifb_dmi_system_table[] __initconst = {
0189     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
0190     /* At least one of these two will be right; maybe both? */
0191     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
0192     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
0193     /* At least one of these two will be right; maybe both? */
0194     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
0195     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
0196     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
0197     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac8,1", M_I24_8_1),
0198     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac10,1", M_I24_10_1),
0199     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac11,1", M_I27_11_1),
0200     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
0201     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini3,1", M_MINI_3_1),
0202     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "Macmini4,1", M_MINI_4_1),
0203     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
0204     /* At least one of these two will be right; maybe both? */
0205     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
0206     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
0207     /* At least one of these two will be right; maybe both? */
0208     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
0209     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
0210     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
0211     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook5,1", M_MB_5_1),
0212     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1),
0213     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1),
0214     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
0215     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir3,1", M_MBA_3),
0216     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
0217     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
0218     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2),
0219     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
0220     EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
0221     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
0222     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
0223     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
0224     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,2", M_MBP_5_2),
0225     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,3", M_MBP_5_3),
0226     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,1", M_MBP_6_1),
0227     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro6,2", M_MBP_6_2),
0228     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro7,1", M_MBP_7_1),
0229     EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro8,2", M_MBP_8_2),
0230     {},
0231 };
0232 
0233 /*
0234  * Some devices have a portrait LCD but advertise a landscape resolution (and
0235  * pitch). We simply swap width and height for these devices so that we can
0236  * correctly deal with some of them coming with multiple resolutions.
0237  */
0238 static const struct dmi_system_id efifb_dmi_swap_width_height[] __initconst = {
0239     {
0240         /*
0241          * Lenovo MIIX310-10ICR, only some batches have the troublesome
0242          * 800x1280 portrait screen. Luckily the portrait version has
0243          * its own BIOS version, so we match on that.
0244          */
0245         .matches = {
0246             DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0247             DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "MIIX 310-10ICR"),
0248             DMI_EXACT_MATCH(DMI_BIOS_VERSION, "1HCN44WW"),
0249         },
0250     },
0251     {
0252         /* Lenovo MIIX 320-10ICR with 800x1280 portrait screen */
0253         .matches = {
0254             DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0255             DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
0256                     "Lenovo MIIX 320-10ICR"),
0257         },
0258     },
0259     {
0260         /* Lenovo D330 with 800x1280 or 1200x1920 portrait screen */
0261         .matches = {
0262             DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
0263             DMI_EXACT_MATCH(DMI_PRODUCT_VERSION,
0264                     "Lenovo ideapad D330-10IGM"),
0265         },
0266     },
0267     {},
0268 };
0269 
0270 static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
0271 {
0272     u64 fb_base = screen_info.lfb_base;
0273 
0274     if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
0275         fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
0276 
0277     return fb_base >= range->cpu_addr &&
0278            fb_base < (range->cpu_addr + range->size);
0279 }
0280 
0281 static struct device_node *find_pci_overlap_node(void)
0282 {
0283     struct device_node *np;
0284 
0285     for_each_node_by_type(np, "pci") {
0286         struct of_pci_range_parser parser;
0287         struct of_pci_range range;
0288         int err;
0289 
0290         err = of_pci_range_parser_init(&parser, np);
0291         if (err) {
0292             pr_warn("of_pci_range_parser_init() failed: %d\n", err);
0293             continue;
0294         }
0295 
0296         for_each_of_pci_range(&parser, &range)
0297             if (efifb_overlaps_pci_range(&range))
0298                 return np;
0299     }
0300     return NULL;
0301 }
0302 
0303 /*
0304  * If the efifb framebuffer is backed by a PCI graphics controller, we have
0305  * to ensure that this relation is expressed using a device link when
0306  * running in DT mode, or the probe order may be reversed, resulting in a
0307  * resource reservation conflict on the memory window that the efifb
0308  * framebuffer steals from the PCIe host bridge.
0309  */
0310 static int efifb_add_links(struct fwnode_handle *fwnode)
0311 {
0312     struct device_node *sup_np;
0313 
0314     sup_np = find_pci_overlap_node();
0315 
0316     /*
0317      * If there's no PCI graphics controller backing the efifb, we are
0318      * done here.
0319      */
0320     if (!sup_np)
0321         return 0;
0322 
0323     fwnode_link_add(fwnode, of_fwnode_handle(sup_np));
0324     of_node_put(sup_np);
0325 
0326     return 0;
0327 }
0328 
0329 static const struct fwnode_operations efifb_fwnode_ops = {
0330     .add_links = efifb_add_links,
0331 };
0332 
0333 #ifdef CONFIG_EFI
0334 static struct fwnode_handle efifb_fwnode;
0335 
0336 __init void sysfb_apply_efi_quirks(struct platform_device *pd)
0337 {
0338     if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI ||
0339         !(screen_info.capabilities & VIDEO_CAPABILITY_SKIP_QUIRKS))
0340         dmi_check_system(efifb_dmi_system_table);
0341 
0342     if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
0343         dmi_check_system(efifb_dmi_swap_width_height)) {
0344         u16 temp = screen_info.lfb_width;
0345 
0346         screen_info.lfb_width = screen_info.lfb_height;
0347         screen_info.lfb_height = temp;
0348         screen_info.lfb_linelength = 4 * screen_info.lfb_width;
0349     }
0350 
0351     if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI && IS_ENABLED(CONFIG_PCI)) {
0352         fwnode_init(&efifb_fwnode, &efifb_fwnode_ops);
0353         pd->dev.fwnode = &efifb_fwnode;
0354     }
0355 }
0356 #endif