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

source code of linux/drivers/firmware/efi/sysfb_efi.c