gop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* -----------------------------------------------------------------------
  3. *
  4. * Copyright 2011 Intel Corporation; author Matt Fleming
  5. *
  6. * ----------------------------------------------------------------------- */
  7. #include <linux/bitops.h>
  8. #include <linux/ctype.h>
  9. #include <linux/efi.h>
  10. #include <linux/screen_info.h>
  11. #include <linux/string.h>
  12. #include <asm/efi.h>
  13. #include <asm/setup.h>
  14. #include "efistub.h"
  15. enum efi_cmdline_option {
  16. EFI_CMDLINE_NONE,
  17. EFI_CMDLINE_MODE_NUM,
  18. EFI_CMDLINE_RES,
  19. EFI_CMDLINE_AUTO,
  20. EFI_CMDLINE_LIST
  21. };
  22. static struct {
  23. enum efi_cmdline_option option;
  24. union {
  25. u32 mode;
  26. struct {
  27. u32 width, height;
  28. int format;
  29. u8 depth;
  30. } res;
  31. };
  32. } cmdline = { .option = EFI_CMDLINE_NONE };
  33. static bool parse_modenum(char *option, char **next)
  34. {
  35. u32 m;
  36. if (!strstarts(option, "mode="))
  37. return false;
  38. option += strlen("mode=");
  39. m = simple_strtoull(option, &option, 0);
  40. if (*option && *option++ != ',')
  41. return false;
  42. cmdline.option = EFI_CMDLINE_MODE_NUM;
  43. cmdline.mode = m;
  44. *next = option;
  45. return true;
  46. }
  47. static bool parse_res(char *option, char **next)
  48. {
  49. u32 w, h, d = 0;
  50. int pf = -1;
  51. if (!isdigit(*option))
  52. return false;
  53. w = simple_strtoull(option, &option, 10);
  54. if (*option++ != 'x' || !isdigit(*option))
  55. return false;
  56. h = simple_strtoull(option, &option, 10);
  57. if (*option == '-') {
  58. option++;
  59. if (strstarts(option, "rgb")) {
  60. option += strlen("rgb");
  61. pf = PIXEL_RGB_RESERVED_8BIT_PER_COLOR;
  62. } else if (strstarts(option, "bgr")) {
  63. option += strlen("bgr");
  64. pf = PIXEL_BGR_RESERVED_8BIT_PER_COLOR;
  65. } else if (isdigit(*option))
  66. d = simple_strtoull(option, &option, 10);
  67. else
  68. return false;
  69. }
  70. if (*option && *option++ != ',')
  71. return false;
  72. cmdline.option = EFI_CMDLINE_RES;
  73. cmdline.res.width = w;
  74. cmdline.res.height = h;
  75. cmdline.res.format = pf;
  76. cmdline.res.depth = d;
  77. *next = option;
  78. return true;
  79. }
  80. static bool parse_auto(char *option, char **next)
  81. {
  82. if (!strstarts(option, "auto"))
  83. return false;
  84. option += strlen("auto");
  85. if (*option && *option++ != ',')
  86. return false;
  87. cmdline.option = EFI_CMDLINE_AUTO;
  88. *next = option;
  89. return true;
  90. }
  91. static bool parse_list(char *option, char **next)
  92. {
  93. if (!strstarts(option, "list"))
  94. return false;
  95. option += strlen("list");
  96. if (*option && *option++ != ',')
  97. return false;
  98. cmdline.option = EFI_CMDLINE_LIST;
  99. *next = option;
  100. return true;
  101. }
  102. void efi_parse_option_graphics(char *option)
  103. {
  104. while (*option) {
  105. if (parse_modenum(option, &option))
  106. continue;
  107. if (parse_res(option, &option))
  108. continue;
  109. if (parse_auto(option, &option))
  110. continue;
  111. if (parse_list(option, &option))
  112. continue;
  113. while (*option && *option++ != ',')
  114. ;
  115. }
  116. }
  117. static u32 choose_mode_modenum(efi_graphics_output_protocol_t *gop)
  118. {
  119. efi_status_t status;
  120. efi_graphics_output_protocol_mode_t *mode;
  121. efi_graphics_output_mode_info_t *info;
  122. unsigned long info_size;
  123. u32 max_mode, cur_mode;
  124. int pf;
  125. mode = efi_table_attr(gop, mode);
  126. cur_mode = efi_table_attr(mode, mode);
  127. if (cmdline.mode == cur_mode)
  128. return cur_mode;
  129. max_mode = efi_table_attr(mode, max_mode);
  130. if (cmdline.mode >= max_mode) {
  131. efi_err("Requested mode is invalid\n");
  132. return cur_mode;
  133. }
  134. status = efi_call_proto(gop, query_mode, cmdline.mode,
  135. &info_size, &info);
  136. if (status != EFI_SUCCESS) {
  137. efi_err("Couldn't get mode information\n");
  138. return cur_mode;
  139. }
  140. pf = info->pixel_format;
  141. efi_bs_call(free_pool, info);
  142. if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX) {
  143. efi_err("Invalid PixelFormat\n");
  144. return cur_mode;
  145. }
  146. return cmdline.mode;
  147. }
  148. static u8 pixel_bpp(int pixel_format, efi_pixel_bitmask_t pixel_info)
  149. {
  150. if (pixel_format == PIXEL_BIT_MASK) {
  151. u32 mask = pixel_info.red_mask | pixel_info.green_mask |
  152. pixel_info.blue_mask | pixel_info.reserved_mask;
  153. if (!mask)
  154. return 0;
  155. return __fls(mask) - __ffs(mask) + 1;
  156. } else
  157. return 32;
  158. }
  159. static u32 choose_mode_res(efi_graphics_output_protocol_t *gop)
  160. {
  161. efi_status_t status;
  162. efi_graphics_output_protocol_mode_t *mode;
  163. efi_graphics_output_mode_info_t *info;
  164. unsigned long info_size;
  165. u32 max_mode, cur_mode;
  166. int pf;
  167. efi_pixel_bitmask_t pi;
  168. u32 m, w, h;
  169. mode = efi_table_attr(gop, mode);
  170. cur_mode = efi_table_attr(mode, mode);
  171. info = efi_table_attr(mode, info);
  172. pf = info->pixel_format;
  173. pi = info->pixel_information;
  174. w = info->horizontal_resolution;
  175. h = info->vertical_resolution;
  176. if (w == cmdline.res.width && h == cmdline.res.height &&
  177. (cmdline.res.format < 0 || cmdline.res.format == pf) &&
  178. (!cmdline.res.depth || cmdline.res.depth == pixel_bpp(pf, pi)))
  179. return cur_mode;
  180. max_mode = efi_table_attr(mode, max_mode);
  181. for (m = 0; m < max_mode; m++) {
  182. if (m == cur_mode)
  183. continue;
  184. status = efi_call_proto(gop, query_mode, m,
  185. &info_size, &info);
  186. if (status != EFI_SUCCESS)
  187. continue;
  188. pf = info->pixel_format;
  189. pi = info->pixel_information;
  190. w = info->horizontal_resolution;
  191. h = info->vertical_resolution;
  192. efi_bs_call(free_pool, info);
  193. if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
  194. continue;
  195. if (w == cmdline.res.width && h == cmdline.res.height &&
  196. (cmdline.res.format < 0 || cmdline.res.format == pf) &&
  197. (!cmdline.res.depth || cmdline.res.depth == pixel_bpp(pf, pi)))
  198. return m;
  199. }
  200. efi_err("Couldn't find requested mode\n");
  201. return cur_mode;
  202. }
  203. static u32 choose_mode_auto(efi_graphics_output_protocol_t *gop)
  204. {
  205. efi_status_t status;
  206. efi_graphics_output_protocol_mode_t *mode;
  207. efi_graphics_output_mode_info_t *info;
  208. unsigned long info_size;
  209. u32 max_mode, cur_mode, best_mode, area;
  210. u8 depth;
  211. int pf;
  212. efi_pixel_bitmask_t pi;
  213. u32 m, w, h, a;
  214. u8 d;
  215. mode = efi_table_attr(gop, mode);
  216. cur_mode = efi_table_attr(mode, mode);
  217. max_mode = efi_table_attr(mode, max_mode);
  218. info = efi_table_attr(mode, info);
  219. pf = info->pixel_format;
  220. pi = info->pixel_information;
  221. w = info->horizontal_resolution;
  222. h = info->vertical_resolution;
  223. best_mode = cur_mode;
  224. area = w * h;
  225. depth = pixel_bpp(pf, pi);
  226. for (m = 0; m < max_mode; m++) {
  227. if (m == cur_mode)
  228. continue;
  229. status = efi_call_proto(gop, query_mode, m,
  230. &info_size, &info);
  231. if (status != EFI_SUCCESS)
  232. continue;
  233. pf = info->pixel_format;
  234. pi = info->pixel_information;
  235. w = info->horizontal_resolution;
  236. h = info->vertical_resolution;
  237. efi_bs_call(free_pool, info);
  238. if (pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX)
  239. continue;
  240. a = w * h;
  241. if (a < area)
  242. continue;
  243. d = pixel_bpp(pf, pi);
  244. if (a > area || d > depth) {
  245. best_mode = m;
  246. area = a;
  247. depth = d;
  248. }
  249. }
  250. return best_mode;
  251. }
  252. static u32 choose_mode_list(efi_graphics_output_protocol_t *gop)
  253. {
  254. efi_status_t status;
  255. efi_graphics_output_protocol_mode_t *mode;
  256. efi_graphics_output_mode_info_t *info;
  257. unsigned long info_size;
  258. u32 max_mode, cur_mode;
  259. int pf;
  260. efi_pixel_bitmask_t pi;
  261. u32 m, w, h;
  262. u8 d;
  263. const char *dstr;
  264. bool valid;
  265. efi_input_key_t key;
  266. mode = efi_table_attr(gop, mode);
  267. cur_mode = efi_table_attr(mode, mode);
  268. max_mode = efi_table_attr(mode, max_mode);
  269. efi_printk("Available graphics modes are 0-%u\n", max_mode-1);
  270. efi_puts(" * = current mode\n"
  271. " - = unusable mode\n");
  272. for (m = 0; m < max_mode; m++) {
  273. status = efi_call_proto(gop, query_mode, m,
  274. &info_size, &info);
  275. if (status != EFI_SUCCESS)
  276. continue;
  277. pf = info->pixel_format;
  278. pi = info->pixel_information;
  279. w = info->horizontal_resolution;
  280. h = info->vertical_resolution;
  281. efi_bs_call(free_pool, info);
  282. valid = !(pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX);
  283. d = 0;
  284. switch (pf) {
  285. case PIXEL_RGB_RESERVED_8BIT_PER_COLOR:
  286. dstr = "rgb";
  287. break;
  288. case PIXEL_BGR_RESERVED_8BIT_PER_COLOR:
  289. dstr = "bgr";
  290. break;
  291. case PIXEL_BIT_MASK:
  292. dstr = "";
  293. d = pixel_bpp(pf, pi);
  294. break;
  295. case PIXEL_BLT_ONLY:
  296. dstr = "blt";
  297. break;
  298. default:
  299. dstr = "xxx";
  300. break;
  301. }
  302. efi_printk("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n",
  303. m,
  304. m == cur_mode ? '*' : ' ',
  305. !valid ? '-' : ' ',
  306. w, h, dstr, d);
  307. }
  308. efi_puts("\nPress any key to continue (or wait 10 seconds)\n");
  309. status = efi_wait_for_key(10 * EFI_USEC_PER_SEC, &key);
  310. if (status != EFI_SUCCESS && status != EFI_TIMEOUT) {
  311. efi_err("Unable to read key, continuing in 10 seconds\n");
  312. efi_bs_call(stall, 10 * EFI_USEC_PER_SEC);
  313. }
  314. return cur_mode;
  315. }
  316. static void set_mode(efi_graphics_output_protocol_t *gop)
  317. {
  318. efi_graphics_output_protocol_mode_t *mode;
  319. u32 cur_mode, new_mode;
  320. switch (cmdline.option) {
  321. case EFI_CMDLINE_MODE_NUM:
  322. new_mode = choose_mode_modenum(gop);
  323. break;
  324. case EFI_CMDLINE_RES:
  325. new_mode = choose_mode_res(gop);
  326. break;
  327. case EFI_CMDLINE_AUTO:
  328. new_mode = choose_mode_auto(gop);
  329. break;
  330. case EFI_CMDLINE_LIST:
  331. new_mode = choose_mode_list(gop);
  332. break;
  333. default:
  334. return;
  335. }
  336. mode = efi_table_attr(gop, mode);
  337. cur_mode = efi_table_attr(mode, mode);
  338. if (new_mode == cur_mode)
  339. return;
  340. if (efi_call_proto(gop, set_mode, new_mode) != EFI_SUCCESS)
  341. efi_err("Failed to set requested mode\n");
  342. }
  343. static void find_bits(u32 mask, u8 *pos, u8 *size)
  344. {
  345. if (!mask) {
  346. *pos = *size = 0;
  347. return;
  348. }
  349. /* UEFI spec guarantees that the set bits are contiguous */
  350. *pos = __ffs(mask);
  351. *size = __fls(mask) - *pos + 1;
  352. }
  353. static void
  354. setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
  355. efi_pixel_bitmask_t pixel_info, int pixel_format)
  356. {
  357. if (pixel_format == PIXEL_BIT_MASK) {
  358. find_bits(pixel_info.red_mask,
  359. &si->red_pos, &si->red_size);
  360. find_bits(pixel_info.green_mask,
  361. &si->green_pos, &si->green_size);
  362. find_bits(pixel_info.blue_mask,
  363. &si->blue_pos, &si->blue_size);
  364. find_bits(pixel_info.reserved_mask,
  365. &si->rsvd_pos, &si->rsvd_size);
  366. si->lfb_depth = si->red_size + si->green_size +
  367. si->blue_size + si->rsvd_size;
  368. si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
  369. } else {
  370. if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
  371. si->red_pos = 0;
  372. si->blue_pos = 16;
  373. } else /* PIXEL_BGR_RESERVED_8BIT_PER_COLOR */ {
  374. si->blue_pos = 0;
  375. si->red_pos = 16;
  376. }
  377. si->green_pos = 8;
  378. si->rsvd_pos = 24;
  379. si->red_size = si->green_size =
  380. si->blue_size = si->rsvd_size = 8;
  381. si->lfb_depth = 32;
  382. si->lfb_linelength = pixels_per_scan_line * 4;
  383. }
  384. }
  385. static efi_graphics_output_protocol_t *
  386. find_gop(efi_guid_t *proto, unsigned long size, void **handles)
  387. {
  388. efi_graphics_output_protocol_t *first_gop;
  389. efi_handle_t h;
  390. int i;
  391. first_gop = NULL;
  392. for_each_efi_handle(h, handles, size, i) {
  393. efi_status_t status;
  394. efi_graphics_output_protocol_t *gop;
  395. efi_graphics_output_protocol_mode_t *mode;
  396. efi_graphics_output_mode_info_t *info;
  397. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  398. void *dummy = NULL;
  399. status = efi_bs_call(handle_protocol, h, proto, (void **)&gop);
  400. if (status != EFI_SUCCESS)
  401. continue;
  402. mode = efi_table_attr(gop, mode);
  403. info = efi_table_attr(mode, info);
  404. if (info->pixel_format == PIXEL_BLT_ONLY ||
  405. info->pixel_format >= PIXEL_FORMAT_MAX)
  406. continue;
  407. /*
  408. * Systems that use the UEFI Console Splitter may
  409. * provide multiple GOP devices, not all of which are
  410. * backed by real hardware. The workaround is to search
  411. * for a GOP implementing the ConOut protocol, and if
  412. * one isn't found, to just fall back to the first GOP.
  413. *
  414. * Once we've found a GOP supporting ConOut,
  415. * don't bother looking any further.
  416. */
  417. status = efi_bs_call(handle_protocol, h, &conout_proto, &dummy);
  418. if (status == EFI_SUCCESS)
  419. return gop;
  420. if (!first_gop)
  421. first_gop = gop;
  422. }
  423. return first_gop;
  424. }
  425. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  426. unsigned long size, void **handles)
  427. {
  428. efi_graphics_output_protocol_t *gop;
  429. efi_graphics_output_protocol_mode_t *mode;
  430. efi_graphics_output_mode_info_t *info;
  431. gop = find_gop(proto, size, handles);
  432. /* Did we find any GOPs? */
  433. if (!gop)
  434. return EFI_NOT_FOUND;
  435. /* Change mode if requested */
  436. set_mode(gop);
  437. /* EFI framebuffer */
  438. mode = efi_table_attr(gop, mode);
  439. info = efi_table_attr(mode, info);
  440. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  441. si->lfb_width = info->horizontal_resolution;
  442. si->lfb_height = info->vertical_resolution;
  443. efi_set_u64_split(efi_table_attr(mode, frame_buffer_base),
  444. &si->lfb_base, &si->ext_lfb_base);
  445. if (si->ext_lfb_base)
  446. si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
  447. si->pages = 1;
  448. setup_pixel_info(si, info->pixels_per_scan_line,
  449. info->pixel_information, info->pixel_format);
  450. si->lfb_size = si->lfb_linelength * si->lfb_height;
  451. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  452. return EFI_SUCCESS;
  453. }
  454. /*
  455. * See if we have Graphics Output Protocol
  456. */
  457. efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
  458. unsigned long size)
  459. {
  460. efi_status_t status;
  461. void **gop_handle = NULL;
  462. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  463. (void **)&gop_handle);
  464. if (status != EFI_SUCCESS)
  465. return status;
  466. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, proto, NULL,
  467. &size, gop_handle);
  468. if (status != EFI_SUCCESS)
  469. goto free_handle;
  470. status = setup_gop(si, proto, size, gop_handle);
  471. free_handle:
  472. efi_bs_call(free_pool, gop_handle);
  473. return status;
  474. }