1 // SPDX-License-Identifier: GPL-2.0
2 /* -----------------------------------------------------------------------
4 * Copyright 2011 Intel Corporation; author Matt Fleming
6 * ----------------------------------------------------------------------- */
8 #include <linux/bitops.h>
9 #include <linux/ctype.h>
10 #include <linux/efi.h>
11 #include <linux/screen_info.h>
12 #include <linux/string.h>
14 #include <asm/setup.h>
18 enum efi_cmdline_option {
27 enum efi_cmdline_option option;
36 } cmdline = { .option = EFI_CMDLINE_NONE };
38 static bool parse_modenum(char *option, char **next)
42 if (!strstarts(option, "mode="))
44 option += strlen("mode=");
45 m = simple_strtoull(option, &option, 0);
46 if (*option && *option++ != ',')
48 cmdline.option = EFI_CMDLINE_MODE_NUM;
55 static bool parse_res(char *option, char **next)
60 if (!isdigit(*option))
62 w = simple_strtoull(option, &option, 10);
63 if (*option++ != 'x' || !isdigit(*option))
65 h = simple_strtoull(option, &option, 10);
68 if (strstarts(option, "rgb")) {
69 option += strlen("rgb");
70 pf = PIXEL_RGB_RESERVED_8BIT_PER_COLOR;
71 } else if (strstarts(option, "bgr")) {
72 option += strlen("bgr");
73 pf = PIXEL_BGR_RESERVED_8BIT_PER_COLOR;
74 } else if (isdigit(*option))
75 d = simple_strtoull(option, &option, 10);
79 if (*option && *option++ != ',')
81 cmdline.option = EFI_CMDLINE_RES;
82 cmdline.res.width = w;
83 cmdline.res.height = h;
84 cmdline.res.format = pf;
85 cmdline.res.depth = d;
91 static bool parse_auto(char *option, char **next)
93 if (!strstarts(option, "auto"))
95 option += strlen("auto");
96 if (*option && *option++ != ',')
98 cmdline.option = EFI_CMDLINE_AUTO;
104 static bool parse_list(char *option, char **next)
106 if (!strstarts(option, "list"))
108 option += strlen("list");
109 if (*option && *option++ != ',')
111 cmdline.option = EFI_CMDLINE_LIST;
117 void efi_parse_option_graphics(char *option)
120 if (parse_modenum(option, &option))
122 if (parse_res(option, &option))
124 if (parse_auto(option, &option))
126 if (parse_list(option, &option))
129 while (*option && *option++ != ',')
134 static u32 choose_mode_modenum(efi_graphics_output_protocol_t *gop)
138 efi_graphics_output_protocol_mode_t *mode;
139 efi_graphics_output_mode_info_t *info;
140 unsigned long info_size;
142 u32 max_mode, cur_mode;
145 mode = efi_table_attr(gop, mode);
147 cur_mode = efi_table_attr(mode, mode);
148 if (cmdline.mode == cur_mode)
151 max_mode = efi_table_attr(mode, max_mode);
152 if (cmdline.mode >= max_mode) {
153 efi_err("Requested mode is invalid\n");
157 status = efi_call_proto(gop, query_mode, cmdline.mode,
159 if (status != EFI_SUCCESS) {
160 efi_err("Couldn't get mode information\n");
164 pf = info->pixel_format;
166 efi_bs_call(free_pool, info);
168 if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX) {
169 efi_err("Invalid PixelFormat\n");
176 static u8 pixel_bpp(int pixel_format, efi_pixel_bitmask_t pixel_info)
178 if (pixel_format == PIXEL_BIT_MASK) {
179 u32 mask = pixel_info.red_mask | pixel_info.green_mask |
180 pixel_info.blue_mask | pixel_info.reserved_mask;
183 return __fls(mask) - __ffs(mask) + 1;
188 static u32 choose_mode_res(efi_graphics_output_protocol_t *gop)
192 efi_graphics_output_protocol_mode_t *mode;
193 efi_graphics_output_mode_info_t *info;
194 unsigned long info_size;
196 u32 max_mode, cur_mode;
198 efi_pixel_bitmask_t pi;
201 mode = efi_table_attr(gop, mode);
203 cur_mode = efi_table_attr(mode, mode);
204 info = efi_table_attr(mode, info);
205 pf = info->pixel_format;
206 pi = info->pixel_information;
207 w = info->horizontal_resolution;
208 h = info->vertical_resolution;
210 if (w == cmdline.res.width && h == cmdline.res.height &&
211 (cmdline.res.format < 0 || cmdline.res.format == pf) &&
212 (!cmdline.res.depth || cmdline.res.depth == pixel_bpp(pf, pi)))
215 max_mode = efi_table_attr(mode, max_mode);
217 for (m = 0; m < max_mode; m++) {
221 status = efi_call_proto(gop, query_mode, m,
223 if (status != EFI_SUCCESS)
226 pf = info->pixel_format;
227 pi = info->pixel_information;
228 w = info->horizontal_resolution;
229 h = info->vertical_resolution;
231 efi_bs_call(free_pool, info);
233 if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
235 if (w == cmdline.res.width && h == cmdline.res.height &&
236 (cmdline.res.format < 0 || cmdline.res.format == pf) &&
237 (!cmdline.res.depth || cmdline.res.depth == pixel_bpp(pf, pi)))
241 efi_err("Couldn't find requested mode\n");
246 static u32 choose_mode_auto(efi_graphics_output_protocol_t *gop)
250 efi_graphics_output_protocol_mode_t *mode;
251 efi_graphics_output_mode_info_t *info;
252 unsigned long info_size;
254 u32 max_mode, cur_mode, best_mode, area;
257 efi_pixel_bitmask_t pi;
261 mode = efi_table_attr(gop, mode);
263 cur_mode = efi_table_attr(mode, mode);
264 max_mode = efi_table_attr(mode, max_mode);
266 info = efi_table_attr(mode, info);
268 pf = info->pixel_format;
269 pi = info->pixel_information;
270 w = info->horizontal_resolution;
271 h = info->vertical_resolution;
273 best_mode = cur_mode;
275 depth = pixel_bpp(pf, pi);
277 for (m = 0; m < max_mode; m++) {
281 status = efi_call_proto(gop, query_mode, m,
283 if (status != EFI_SUCCESS)
286 pf = info->pixel_format;
287 pi = info->pixel_information;
288 w = info->horizontal_resolution;
289 h = info->vertical_resolution;
291 efi_bs_call(free_pool, info);
293 if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
298 d = pixel_bpp(pf, pi);
299 if (a > area || d > depth) {
309 static u32 choose_mode_list(efi_graphics_output_protocol_t *gop)
313 efi_graphics_output_protocol_mode_t *mode;
314 efi_graphics_output_mode_info_t *info;
315 unsigned long info_size;
317 u32 max_mode, cur_mode;
319 efi_pixel_bitmask_t pi;
326 mode = efi_table_attr(gop, mode);
328 cur_mode = efi_table_attr(mode, mode);
329 max_mode = efi_table_attr(mode, max_mode);
331 efi_printk("Available graphics modes are 0-%u\n", max_mode-1);
332 efi_puts(" * = current mode\n"
333 " - = unusable mode\n");
334 for (m = 0; m < max_mode; m++) {
335 status = efi_call_proto(gop, query_mode, m,
337 if (status != EFI_SUCCESS)
340 pf = info->pixel_format;
341 pi = info->pixel_information;
342 w = info->horizontal_resolution;
343 h = info->vertical_resolution;
345 efi_bs_call(free_pool, info);
347 valid = !(pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX);
350 case PIXEL_RGB_RESERVED_8BIT_PER_COLOR:
353 case PIXEL_BGR_RESERVED_8BIT_PER_COLOR:
358 d = pixel_bpp(pf, pi);
368 efi_printk("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n",
370 m == cur_mode ? '*' : ' ',
375 efi_puts("\nPress any key to continue (or wait 10 seconds)\n");
376 status = efi_wait_for_key(10 * EFI_USEC_PER_SEC, &key);
377 if (status != EFI_SUCCESS && status != EFI_TIMEOUT) {
378 efi_err("Unable to read key, continuing in 10 seconds\n");
379 efi_bs_call(stall, 10 * EFI_USEC_PER_SEC);
385 static void set_mode(efi_graphics_output_protocol_t *gop)
387 efi_graphics_output_protocol_mode_t *mode;
388 u32 cur_mode, new_mode;
390 switch (cmdline.option) {
391 case EFI_CMDLINE_MODE_NUM:
392 new_mode = choose_mode_modenum(gop);
394 case EFI_CMDLINE_RES:
395 new_mode = choose_mode_res(gop);
397 case EFI_CMDLINE_AUTO:
398 new_mode = choose_mode_auto(gop);
400 case EFI_CMDLINE_LIST:
401 new_mode = choose_mode_list(gop);
407 mode = efi_table_attr(gop, mode);
408 cur_mode = efi_table_attr(mode, mode);
410 if (new_mode == cur_mode)
413 if (efi_call_proto(gop, set_mode, new_mode) != EFI_SUCCESS)
414 efi_err("Failed to set requested mode\n");
417 static void find_bits(u32 mask, u8 *pos, u8 *size)
424 /* UEFI spec guarantees that the set bits are contiguous */
426 *size = __fls(mask) - *pos + 1;
430 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
431 efi_pixel_bitmask_t pixel_info, int pixel_format)
433 if (pixel_format == PIXEL_BIT_MASK) {
434 find_bits(pixel_info.red_mask,
435 &si->red_pos, &si->red_size);
436 find_bits(pixel_info.green_mask,
437 &si->green_pos, &si->green_size);
438 find_bits(pixel_info.blue_mask,
439 &si->blue_pos, &si->blue_size);
440 find_bits(pixel_info.reserved_mask,
441 &si->rsvd_pos, &si->rsvd_size);
442 si->lfb_depth = si->red_size + si->green_size +
443 si->blue_size + si->rsvd_size;
444 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
446 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
449 } else /* PIXEL_BGR_RESERVED_8BIT_PER_COLOR */ {
456 si->red_size = si->green_size =
457 si->blue_size = si->rsvd_size = 8;
460 si->lfb_linelength = pixels_per_scan_line * 4;
464 static efi_graphics_output_protocol_t *
465 find_gop(efi_guid_t *proto, unsigned long size, void **handles)
467 efi_graphics_output_protocol_t *first_gop;
473 for_each_efi_handle(h, handles, size, i) {
476 efi_graphics_output_protocol_t *gop;
477 efi_graphics_output_protocol_mode_t *mode;
478 efi_graphics_output_mode_info_t *info;
480 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
483 status = efi_bs_call(handle_protocol, h, proto, (void **)&gop);
484 if (status != EFI_SUCCESS)
487 mode = efi_table_attr(gop, mode);
488 info = efi_table_attr(mode, info);
489 if (info->pixel_format == PIXEL_BLT_ONLY ||
490 info->pixel_format >= PIXEL_FORMAT_MAX)
494 * Systems that use the UEFI Console Splitter may
495 * provide multiple GOP devices, not all of which are
496 * backed by real hardware. The workaround is to search
497 * for a GOP implementing the ConOut protocol, and if
498 * one isn't found, to just fall back to the first GOP.
500 * Once we've found a GOP supporting ConOut,
501 * don't bother looking any further.
503 status = efi_bs_call(handle_protocol, h, &conout_proto, &dummy);
504 if (status == EFI_SUCCESS)
514 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
515 unsigned long size, void **handles)
517 efi_graphics_output_protocol_t *gop;
518 efi_graphics_output_protocol_mode_t *mode;
519 efi_graphics_output_mode_info_t *info;
521 gop = find_gop(proto, size, handles);
523 /* Did we find any GOPs? */
525 return EFI_NOT_FOUND;
527 /* Change mode if requested */
530 /* EFI framebuffer */
531 mode = efi_table_attr(gop, mode);
532 info = efi_table_attr(mode, info);
534 si->orig_video_isVGA = VIDEO_TYPE_EFI;
536 si->lfb_width = info->horizontal_resolution;
537 si->lfb_height = info->vertical_resolution;
539 efi_set_u64_split(efi_table_attr(mode, frame_buffer_base),
540 &si->lfb_base, &si->ext_lfb_base);
541 if (si->ext_lfb_base)
542 si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
546 setup_pixel_info(si, info->pixels_per_scan_line,
547 info->pixel_information, info->pixel_format);
549 si->lfb_size = si->lfb_linelength * si->lfb_height;
551 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
557 * See if we have Graphics Output Protocol
559 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
563 void **gop_handle = NULL;
565 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
566 (void **)&gop_handle);
567 if (status != EFI_SUCCESS)
570 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, proto, NULL,
572 if (status != EFI_SUCCESS)
575 status = setup_gop(si, proto, size, gop_handle);
578 efi_bs_call(free_pool, gop_handle);