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