efi_gop.c 10 KB

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