efi_gop.c 13 KB

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