efi_gop.c 13 KB

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