efi_gop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application disk support
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <efi_loader.h>
  10. #include <lcd.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <video.h>
  14. #include <asm/global_data.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  17. /**
  18. * struct efi_gop_obj - graphical output protocol object
  19. *
  20. * @header: EFI object header
  21. * @ops: graphical output protocol interface
  22. * @info: graphical output mode information
  23. * @mode: graphical output mode
  24. * @bpix: bits per pixel
  25. * @fb: frame buffer
  26. */
  27. struct efi_gop_obj {
  28. struct efi_object header;
  29. struct efi_gop ops;
  30. struct efi_gop_mode_info info;
  31. struct efi_gop_mode mode;
  32. /* Fields we only have access to during init */
  33. u32 bpix;
  34. void *fb;
  35. };
  36. static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
  37. efi_uintn_t *size_of_info,
  38. struct efi_gop_mode_info **info)
  39. {
  40. struct efi_gop_obj *gopobj;
  41. efi_status_t ret = EFI_SUCCESS;
  42. EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
  43. if (!this || !size_of_info || !info || mode_number) {
  44. ret = EFI_INVALID_PARAMETER;
  45. goto out;
  46. }
  47. gopobj = container_of(this, struct efi_gop_obj, ops);
  48. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
  49. (void **)info);
  50. if (ret != EFI_SUCCESS)
  51. goto out;
  52. *size_of_info = sizeof(gopobj->info);
  53. memcpy(*info, &gopobj->info, sizeof(gopobj->info));
  54. out:
  55. return EFI_EXIT(ret);
  56. }
  57. static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
  58. {
  59. struct efi_gop_pixel blt = {
  60. .reserved = 0,
  61. };
  62. blt.blue = (vid & 0x1f) << 3;
  63. vid >>= 5;
  64. blt.green = (vid & 0x3f) << 2;
  65. vid >>= 6;
  66. blt.red = (vid & 0x1f) << 3;
  67. return blt;
  68. }
  69. static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
  70. {
  71. return (u16)(blt->red >> 3) << 11 |
  72. (u16)(blt->green >> 2) << 5 |
  73. (u16)(blt->blue >> 3);
  74. }
  75. static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
  76. struct efi_gop_pixel *bufferp,
  77. u32 operation, efi_uintn_t sx,
  78. efi_uintn_t sy, efi_uintn_t dx,
  79. efi_uintn_t dy,
  80. efi_uintn_t width,
  81. efi_uintn_t height,
  82. efi_uintn_t delta,
  83. efi_uintn_t vid_bpp)
  84. {
  85. struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
  86. efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
  87. u32 *fb32 = gopobj->fb;
  88. u16 *fb16 = gopobj->fb;
  89. struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
  90. if (delta) {
  91. /* Check for 4 byte alignment */
  92. if (delta & 3)
  93. return EFI_INVALID_PARAMETER;
  94. linelen = delta >> 2;
  95. } else {
  96. linelen = width;
  97. }
  98. /* Check source rectangle */
  99. switch (operation) {
  100. case EFI_BLT_VIDEO_FILL:
  101. break;
  102. case EFI_BLT_BUFFER_TO_VIDEO:
  103. if (sx + width > linelen)
  104. return EFI_INVALID_PARAMETER;
  105. break;
  106. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  107. case EFI_BLT_VIDEO_TO_VIDEO:
  108. if (sx + width > gopobj->info.width ||
  109. sy + height > gopobj->info.height)
  110. return EFI_INVALID_PARAMETER;
  111. break;
  112. default:
  113. return EFI_INVALID_PARAMETER;
  114. }
  115. /* Check destination rectangle */
  116. switch (operation) {
  117. case EFI_BLT_VIDEO_FILL:
  118. case EFI_BLT_BUFFER_TO_VIDEO:
  119. case EFI_BLT_VIDEO_TO_VIDEO:
  120. if (dx + width > gopobj->info.width ||
  121. dy + height > gopobj->info.height)
  122. return EFI_INVALID_PARAMETER;
  123. break;
  124. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  125. if (dx + width > linelen)
  126. return EFI_INVALID_PARAMETER;
  127. break;
  128. }
  129. /* Calculate line width */
  130. switch (operation) {
  131. case EFI_BLT_BUFFER_TO_VIDEO:
  132. swidth = linelen;
  133. break;
  134. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  135. case EFI_BLT_VIDEO_TO_VIDEO:
  136. swidth = gopobj->info.width;
  137. if (!vid_bpp)
  138. return EFI_UNSUPPORTED;
  139. break;
  140. case EFI_BLT_VIDEO_FILL:
  141. swidth = 0;
  142. break;
  143. }
  144. switch (operation) {
  145. case EFI_BLT_BUFFER_TO_VIDEO:
  146. case EFI_BLT_VIDEO_FILL:
  147. case EFI_BLT_VIDEO_TO_VIDEO:
  148. dwidth = gopobj->info.width;
  149. if (!vid_bpp)
  150. return EFI_UNSUPPORTED;
  151. break;
  152. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  153. dwidth = linelen;
  154. break;
  155. }
  156. slineoff = swidth * sy;
  157. dlineoff = dwidth * dy;
  158. for (i = 0; i < height; i++) {
  159. for (j = 0; j < width; j++) {
  160. struct efi_gop_pixel pix;
  161. /* Read source pixel */
  162. switch (operation) {
  163. case EFI_BLT_VIDEO_FILL:
  164. pix = *buffer;
  165. break;
  166. case EFI_BLT_BUFFER_TO_VIDEO:
  167. pix = buffer[slineoff + j + sx];
  168. break;
  169. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  170. case EFI_BLT_VIDEO_TO_VIDEO:
  171. if (vid_bpp == 32)
  172. pix = *(struct efi_gop_pixel *)&fb32[
  173. slineoff + j + sx];
  174. else
  175. pix = efi_vid16_to_blt_col(fb16[
  176. slineoff + j + sx]);
  177. break;
  178. }
  179. /* Write destination pixel */
  180. switch (operation) {
  181. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  182. buffer[dlineoff + j + dx] = pix;
  183. break;
  184. case EFI_BLT_BUFFER_TO_VIDEO:
  185. case EFI_BLT_VIDEO_FILL:
  186. case EFI_BLT_VIDEO_TO_VIDEO:
  187. if (vid_bpp == 32)
  188. fb32[dlineoff + j + dx] = *(u32 *)&pix;
  189. else
  190. fb16[dlineoff + j + dx] =
  191. efi_blt_col_to_vid16(&pix);
  192. break;
  193. }
  194. }
  195. slineoff += swidth;
  196. dlineoff += dwidth;
  197. }
  198. return EFI_SUCCESS;
  199. }
  200. static efi_uintn_t gop_get_bpp(struct efi_gop *this)
  201. {
  202. struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
  203. efi_uintn_t vid_bpp = 0;
  204. switch (gopobj->bpix) {
  205. #ifdef CONFIG_DM_VIDEO
  206. case VIDEO_BPP32:
  207. #else
  208. case LCD_COLOR32:
  209. #endif
  210. vid_bpp = 32;
  211. break;
  212. #ifdef CONFIG_DM_VIDEO
  213. case VIDEO_BPP16:
  214. #else
  215. case LCD_COLOR16:
  216. #endif
  217. vid_bpp = 16;
  218. break;
  219. }
  220. return vid_bpp;
  221. }
  222. /*
  223. * GCC can't optimize our BLT function well, but we need to make sure that
  224. * our 2-dimensional loop gets executed very quickly, otherwise the system
  225. * will feel slow.
  226. *
  227. * By manually putting all obvious branch targets into functions which call
  228. * our generic BLT function with constants, the compiler can successfully
  229. * optimize for speed.
  230. */
  231. static efi_status_t gop_blt_video_fill(struct efi_gop *this,
  232. struct efi_gop_pixel *buffer,
  233. u32 foo, efi_uintn_t sx,
  234. efi_uintn_t sy, efi_uintn_t dx,
  235. efi_uintn_t dy, efi_uintn_t width,
  236. efi_uintn_t height, efi_uintn_t delta,
  237. efi_uintn_t vid_bpp)
  238. {
  239. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
  240. dy, width, height, delta, vid_bpp);
  241. }
  242. static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
  243. struct efi_gop_pixel *buffer,
  244. u32 foo, efi_uintn_t sx,
  245. efi_uintn_t sy, efi_uintn_t dx,
  246. efi_uintn_t dy, efi_uintn_t width,
  247. efi_uintn_t height, efi_uintn_t delta)
  248. {
  249. return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
  250. dy, width, height, delta, 16);
  251. }
  252. static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this,
  253. struct efi_gop_pixel *buffer,
  254. u32 foo, efi_uintn_t sx,
  255. efi_uintn_t sy, efi_uintn_t dx,
  256. efi_uintn_t dy, efi_uintn_t width,
  257. efi_uintn_t height, efi_uintn_t delta)
  258. {
  259. return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
  260. dy, width, height, delta, 32);
  261. }
  262. static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this,
  263. struct efi_gop_pixel *buffer,
  264. u32 foo, efi_uintn_t sx,
  265. efi_uintn_t sy, efi_uintn_t dx,
  266. efi_uintn_t dy, efi_uintn_t width,
  267. efi_uintn_t height, efi_uintn_t delta,
  268. efi_uintn_t vid_bpp)
  269. {
  270. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
  271. dy, width, height, delta, vid_bpp);
  272. }
  273. static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
  274. struct efi_gop_pixel *buffer,
  275. u32 foo, efi_uintn_t sx,
  276. efi_uintn_t sy, efi_uintn_t dx,
  277. efi_uintn_t dy, efi_uintn_t width,
  278. efi_uintn_t height, efi_uintn_t delta,
  279. efi_uintn_t vid_bpp)
  280. {
  281. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
  282. dx, dy, width, height, delta, vid_bpp);
  283. }
  284. /**
  285. * gop_set_mode() - set graphical output mode
  286. *
  287. * This function implements the SetMode() service.
  288. *
  289. * See the Unified Extensible Firmware Interface (UEFI) specification for
  290. * details.
  291. *
  292. * @this: the graphical output protocol
  293. * @mode_number: the mode to be set
  294. * Return: status code
  295. */
  296. static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
  297. {
  298. struct efi_gop_obj *gopobj;
  299. struct efi_gop_pixel buffer = {0, 0, 0, 0};
  300. efi_uintn_t vid_bpp;
  301. efi_status_t ret = EFI_SUCCESS;
  302. EFI_ENTRY("%p, %x", this, mode_number);
  303. if (!this) {
  304. ret = EFI_INVALID_PARAMETER;
  305. goto out;
  306. }
  307. if (mode_number) {
  308. ret = EFI_UNSUPPORTED;
  309. goto out;
  310. }
  311. gopobj = container_of(this, struct efi_gop_obj, ops);
  312. vid_bpp = gop_get_bpp(this);
  313. ret = gop_blt_video_fill(this, &buffer, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0,
  314. gopobj->info.width, gopobj->info.height, 0,
  315. vid_bpp);
  316. out:
  317. return EFI_EXIT(ret);
  318. }
  319. /*
  320. * Copy rectangle.
  321. *
  322. * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
  323. * See the Unified Extensible Firmware Interface (UEFI) specification for
  324. * details.
  325. *
  326. * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
  327. * @buffer: pixel buffer
  328. * @sx: source x-coordinate
  329. * @sy: source y-coordinate
  330. * @dx: destination x-coordinate
  331. * @dy: destination y-coordinate
  332. * @width: width of rectangle
  333. * @height: height of rectangle
  334. * @delta: length in bytes of a line in the pixel buffer (optional)
  335. * @return: status code
  336. */
  337. efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
  338. u32 operation, efi_uintn_t sx,
  339. efi_uintn_t sy, efi_uintn_t dx,
  340. efi_uintn_t dy, efi_uintn_t width,
  341. efi_uintn_t height, efi_uintn_t delta)
  342. {
  343. efi_status_t ret = EFI_INVALID_PARAMETER;
  344. efi_uintn_t vid_bpp;
  345. EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
  346. buffer, operation, sx, sy, dx, dy, width, height, delta);
  347. vid_bpp = gop_get_bpp(this);
  348. /* Allow for compiler optimization */
  349. switch (operation) {
  350. case EFI_BLT_VIDEO_FILL:
  351. ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
  352. dy, width, height, delta, vid_bpp);
  353. break;
  354. case EFI_BLT_BUFFER_TO_VIDEO:
  355. /* This needs to be super-fast, so duplicate for 16/32bpp */
  356. if (vid_bpp == 32)
  357. ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
  358. sy, dx, dy, width, height,
  359. delta);
  360. else
  361. ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
  362. sy, dx, dy, width, height,
  363. delta);
  364. break;
  365. case EFI_BLT_VIDEO_TO_VIDEO:
  366. ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
  367. dy, width, height, delta, vid_bpp);
  368. break;
  369. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  370. ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
  371. dy, width, height, delta, vid_bpp);
  372. break;
  373. default:
  374. ret = EFI_INVALID_PARAMETER;
  375. }
  376. if (ret != EFI_SUCCESS)
  377. return EFI_EXIT(ret);
  378. #ifdef CONFIG_DM_VIDEO
  379. video_sync_all();
  380. #else
  381. lcd_sync();
  382. #endif
  383. return EFI_EXIT(EFI_SUCCESS);
  384. }
  385. /*
  386. * Install graphical output protocol.
  387. *
  388. * If no supported video device exists this is not considered as an
  389. * error.
  390. */
  391. efi_status_t efi_gop_register(void)
  392. {
  393. struct efi_gop_obj *gopobj;
  394. u32 bpix, col, row;
  395. u64 fb_base, fb_size;
  396. void *fb;
  397. efi_status_t ret;
  398. #ifdef CONFIG_DM_VIDEO
  399. struct udevice *vdev;
  400. struct video_priv *priv;
  401. /* We only support a single video output device for now */
  402. if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
  403. debug("WARNING: No video device\n");
  404. return EFI_SUCCESS;
  405. }
  406. priv = dev_get_uclass_priv(vdev);
  407. bpix = priv->bpix;
  408. col = video_get_xsize(vdev);
  409. row = video_get_ysize(vdev);
  410. fb_base = (uintptr_t)priv->fb;
  411. fb_size = priv->fb_size;
  412. fb = priv->fb;
  413. #else
  414. int line_len;
  415. bpix = panel_info.vl_bpix;
  416. col = panel_info.vl_col;
  417. row = panel_info.vl_row;
  418. fb_base = gd->fb_base;
  419. fb_size = lcd_get_size(&line_len);
  420. fb = (void*)gd->fb_base;
  421. #endif
  422. switch (bpix) {
  423. #ifdef CONFIG_DM_VIDEO
  424. case VIDEO_BPP16:
  425. case VIDEO_BPP32:
  426. #else
  427. case LCD_COLOR32:
  428. case LCD_COLOR16:
  429. #endif
  430. break;
  431. default:
  432. /* So far, we only work in 16 or 32 bit mode */
  433. debug("WARNING: Unsupported video mode\n");
  434. return EFI_SUCCESS;
  435. }
  436. gopobj = calloc(1, sizeof(*gopobj));
  437. if (!gopobj) {
  438. printf("ERROR: Out of memory\n");
  439. return EFI_OUT_OF_RESOURCES;
  440. }
  441. /* Hook up to the device list */
  442. efi_add_handle(&gopobj->header);
  443. /* Fill in object data */
  444. ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
  445. &gopobj->ops);
  446. if (ret != EFI_SUCCESS) {
  447. printf("ERROR: Failure adding GOP protocol\n");
  448. return ret;
  449. }
  450. gopobj->ops.query_mode = gop_query_mode;
  451. gopobj->ops.set_mode = gop_set_mode;
  452. gopobj->ops.blt = gop_blt;
  453. gopobj->ops.mode = &gopobj->mode;
  454. gopobj->mode.max_mode = 1;
  455. gopobj->mode.info = &gopobj->info;
  456. gopobj->mode.info_size = sizeof(gopobj->info);
  457. gopobj->mode.fb_base = fb_base;
  458. gopobj->mode.fb_size = fb_size;
  459. gopobj->info.version = 0;
  460. gopobj->info.width = col;
  461. gopobj->info.height = row;
  462. #ifdef CONFIG_DM_VIDEO
  463. if (bpix == VIDEO_BPP32)
  464. #else
  465. if (bpix == LCD_COLOR32)
  466. #endif
  467. {
  468. gopobj->info.pixel_format = EFI_GOT_BGRA8;
  469. } else {
  470. gopobj->info.pixel_format = EFI_GOT_BITMASK;
  471. gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
  472. gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
  473. gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
  474. }
  475. gopobj->info.pixels_per_scanline = col;
  476. gopobj->bpix = bpix;
  477. gopobj->fb = fb;
  478. return EFI_SUCCESS;
  479. }