efi_gop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 *bufferp,
  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. efi_uintn_t vid_bpp)
  74. {
  75. struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
  76. efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
  77. u32 *fb32 = gopobj->fb;
  78. u16 *fb16 = gopobj->fb;
  79. struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
  80. if (delta) {
  81. /* Check for 4 byte alignment */
  82. if (delta & 3)
  83. return EFI_INVALID_PARAMETER;
  84. linelen = delta >> 2;
  85. } else {
  86. linelen = width;
  87. }
  88. /* Check source rectangle */
  89. switch (operation) {
  90. case EFI_BLT_VIDEO_FILL:
  91. break;
  92. case EFI_BLT_BUFFER_TO_VIDEO:
  93. if (sx + width > linelen)
  94. return EFI_INVALID_PARAMETER;
  95. break;
  96. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  97. case EFI_BLT_VIDEO_TO_VIDEO:
  98. if (sx + width > gopobj->info.width ||
  99. sy + height > gopobj->info.height)
  100. return EFI_INVALID_PARAMETER;
  101. break;
  102. default:
  103. return EFI_INVALID_PARAMETER;
  104. }
  105. /* Check destination rectangle */
  106. switch (operation) {
  107. case EFI_BLT_VIDEO_FILL:
  108. case EFI_BLT_BUFFER_TO_VIDEO:
  109. case EFI_BLT_VIDEO_TO_VIDEO:
  110. if (dx + width > gopobj->info.width ||
  111. dy + height > gopobj->info.height)
  112. return EFI_INVALID_PARAMETER;
  113. break;
  114. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  115. if (dx + width > linelen)
  116. return EFI_INVALID_PARAMETER;
  117. break;
  118. }
  119. /* Calculate line width */
  120. switch (operation) {
  121. case EFI_BLT_BUFFER_TO_VIDEO:
  122. swidth = linelen;
  123. break;
  124. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  125. case EFI_BLT_VIDEO_TO_VIDEO:
  126. swidth = gopobj->info.width;
  127. if (!vid_bpp)
  128. return EFI_UNSUPPORTED;
  129. break;
  130. case EFI_BLT_VIDEO_FILL:
  131. swidth = 0;
  132. break;
  133. }
  134. switch (operation) {
  135. case EFI_BLT_BUFFER_TO_VIDEO:
  136. case EFI_BLT_VIDEO_FILL:
  137. case EFI_BLT_VIDEO_TO_VIDEO:
  138. dwidth = gopobj->info.width;
  139. if (!vid_bpp)
  140. return EFI_UNSUPPORTED;
  141. break;
  142. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  143. dwidth = linelen;
  144. break;
  145. }
  146. slineoff = swidth * sy;
  147. dlineoff = dwidth * dy;
  148. for (i = 0; i < height; i++) {
  149. for (j = 0; j < width; j++) {
  150. struct efi_gop_pixel pix;
  151. /* Read source pixel */
  152. switch (operation) {
  153. case EFI_BLT_VIDEO_FILL:
  154. pix = *buffer;
  155. break;
  156. case EFI_BLT_BUFFER_TO_VIDEO:
  157. pix = buffer[slineoff + j + sx];
  158. break;
  159. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  160. case EFI_BLT_VIDEO_TO_VIDEO:
  161. if (vid_bpp == 32)
  162. pix = *(struct efi_gop_pixel *)&fb32[
  163. slineoff + j + sx];
  164. else
  165. pix = efi_vid16_to_blt_col(fb16[
  166. slineoff + j + sx]);
  167. break;
  168. }
  169. /* Write destination pixel */
  170. switch (operation) {
  171. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  172. buffer[dlineoff + j + dx] = pix;
  173. break;
  174. case EFI_BLT_BUFFER_TO_VIDEO:
  175. case EFI_BLT_VIDEO_FILL:
  176. case EFI_BLT_VIDEO_TO_VIDEO:
  177. if (vid_bpp == 32)
  178. fb32[dlineoff + j + dx] = *(u32 *)&pix;
  179. else
  180. fb16[dlineoff + j + dx] =
  181. efi_blt_col_to_vid16(&pix);
  182. break;
  183. }
  184. }
  185. slineoff += swidth;
  186. dlineoff += dwidth;
  187. }
  188. return EFI_SUCCESS;
  189. }
  190. static efi_uintn_t gop_get_bpp(struct efi_gop *this)
  191. {
  192. struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
  193. efi_uintn_t vid_bpp = 0;
  194. switch (gopobj->bpix) {
  195. #ifdef CONFIG_DM_VIDEO
  196. case VIDEO_BPP32:
  197. #else
  198. case LCD_COLOR32:
  199. #endif
  200. vid_bpp = 32;
  201. break;
  202. #ifdef CONFIG_DM_VIDEO
  203. case VIDEO_BPP16:
  204. #else
  205. case LCD_COLOR16:
  206. #endif
  207. vid_bpp = 16;
  208. break;
  209. }
  210. return vid_bpp;
  211. }
  212. /*
  213. * Gcc can't optimize our BLT function well, but we need to make sure that
  214. * our 2-dimensional loop gets executed very quickly, otherwise the system
  215. * will feel slow.
  216. *
  217. * By manually putting all obvious branch targets into functions which call
  218. * our generic blt function with constants, the compiler can successfully
  219. * optimize for speed.
  220. */
  221. static efi_status_t gop_blt_video_fill(struct efi_gop *this,
  222. struct efi_gop_pixel *buffer,
  223. u32 foo, efi_uintn_t sx,
  224. efi_uintn_t sy, efi_uintn_t dx,
  225. efi_uintn_t dy, efi_uintn_t width,
  226. efi_uintn_t height, efi_uintn_t delta,
  227. efi_uintn_t vid_bpp)
  228. {
  229. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
  230. dy, width, height, delta, vid_bpp);
  231. }
  232. static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
  233. struct efi_gop_pixel *buffer,
  234. u32 foo, efi_uintn_t sx,
  235. efi_uintn_t sy, efi_uintn_t dx,
  236. efi_uintn_t dy, efi_uintn_t width,
  237. efi_uintn_t height, efi_uintn_t delta)
  238. {
  239. return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
  240. dy, width, height, delta, 16);
  241. }
  242. static efi_status_t gop_blt_buf_to_vid32(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, 32);
  251. }
  252. static efi_status_t gop_blt_vid_to_vid(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. efi_uintn_t vid_bpp)
  259. {
  260. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
  261. dy, width, height, delta, vid_bpp);
  262. }
  263. static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
  264. struct efi_gop_pixel *buffer,
  265. u32 foo, efi_uintn_t sx,
  266. efi_uintn_t sy, efi_uintn_t dx,
  267. efi_uintn_t dy, efi_uintn_t width,
  268. efi_uintn_t height, efi_uintn_t delta,
  269. efi_uintn_t vid_bpp)
  270. {
  271. return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
  272. dx, dy, width, height, delta, vid_bpp);
  273. }
  274. /*
  275. * Copy rectangle.
  276. *
  277. * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
  278. * See the Unified Extensible Firmware Interface (UEFI) specification for
  279. * details.
  280. *
  281. * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
  282. * @buffer: pixel buffer
  283. * @sx: source x-coordinate
  284. * @sy: source y-coordinate
  285. * @dx: destination x-coordinate
  286. * @dy: destination y-coordinate
  287. * @width: width of rectangle
  288. * @height: height of rectangle
  289. * @delta: length in bytes of a line in the pixel buffer (optional)
  290. * @return: status code
  291. */
  292. efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
  293. u32 operation, efi_uintn_t sx,
  294. efi_uintn_t sy, efi_uintn_t dx,
  295. efi_uintn_t dy, efi_uintn_t width,
  296. efi_uintn_t height, efi_uintn_t delta)
  297. {
  298. efi_status_t ret = EFI_INVALID_PARAMETER;
  299. efi_uintn_t vid_bpp;
  300. EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
  301. buffer, operation, sx, sy, dx, dy, width, height, delta);
  302. vid_bpp = gop_get_bpp(this);
  303. /* Allow for compiler optimization */
  304. switch (operation) {
  305. case EFI_BLT_VIDEO_FILL:
  306. ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
  307. dy, width, height, delta, vid_bpp);
  308. break;
  309. case EFI_BLT_BUFFER_TO_VIDEO:
  310. /* This needs to be super-fast, so duplicate for 16/32bpp */
  311. if (vid_bpp == 32)
  312. ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
  313. sy, dx, dy, width, height,
  314. delta);
  315. else
  316. ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
  317. sy, dx, dy, width, height,
  318. delta);
  319. break;
  320. case EFI_BLT_VIDEO_TO_VIDEO:
  321. ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
  322. dy, width, height, delta, vid_bpp);
  323. break;
  324. case EFI_BLT_VIDEO_TO_BLT_BUFFER:
  325. ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
  326. dy, width, height, delta, vid_bpp);
  327. break;
  328. default:
  329. ret = EFI_UNSUPPORTED;
  330. }
  331. if (ret != EFI_SUCCESS)
  332. return EFI_EXIT(ret);
  333. #ifdef CONFIG_DM_VIDEO
  334. video_sync_all();
  335. #else
  336. lcd_sync();
  337. #endif
  338. return EFI_EXIT(EFI_SUCCESS);
  339. }
  340. /*
  341. * Install graphical output protocol.
  342. *
  343. * If no supported video device exists this is not considered as an
  344. * error.
  345. */
  346. efi_status_t efi_gop_register(void)
  347. {
  348. struct efi_gop_obj *gopobj;
  349. u32 bpix, col, row;
  350. u64 fb_base, fb_size;
  351. void *fb;
  352. efi_status_t ret;
  353. #ifdef CONFIG_DM_VIDEO
  354. struct udevice *vdev;
  355. struct video_priv *priv;
  356. /* We only support a single video output device for now */
  357. if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
  358. debug("WARNING: No video device\n");
  359. return EFI_SUCCESS;
  360. }
  361. priv = dev_get_uclass_priv(vdev);
  362. bpix = priv->bpix;
  363. col = video_get_xsize(vdev);
  364. row = video_get_ysize(vdev);
  365. fb_base = (uintptr_t)priv->fb;
  366. fb_size = priv->fb_size;
  367. fb = priv->fb;
  368. #else
  369. int line_len;
  370. bpix = panel_info.vl_bpix;
  371. col = panel_info.vl_col;
  372. row = panel_info.vl_row;
  373. fb_base = gd->fb_base;
  374. fb_size = lcd_get_size(&line_len);
  375. fb = (void*)gd->fb_base;
  376. #endif
  377. switch (bpix) {
  378. #ifdef CONFIG_DM_VIDEO
  379. case VIDEO_BPP16:
  380. case VIDEO_BPP32:
  381. #else
  382. case LCD_COLOR32:
  383. case LCD_COLOR16:
  384. #endif
  385. break;
  386. default:
  387. /* So far, we only work in 16 or 32 bit mode */
  388. debug("WARNING: Unsupported video mode\n");
  389. return EFI_SUCCESS;
  390. }
  391. gopobj = calloc(1, sizeof(*gopobj));
  392. if (!gopobj) {
  393. printf("ERROR: Out of memory\n");
  394. return EFI_OUT_OF_RESOURCES;
  395. }
  396. /* Hook up to the device list */
  397. efi_add_handle(&gopobj->parent);
  398. /* Fill in object data */
  399. ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
  400. &gopobj->ops);
  401. if (ret != EFI_SUCCESS) {
  402. printf("ERROR: Failure adding gop protocol\n");
  403. return ret;
  404. }
  405. gopobj->ops.query_mode = gop_query_mode;
  406. gopobj->ops.set_mode = gop_set_mode;
  407. gopobj->ops.blt = gop_blt;
  408. gopobj->ops.mode = &gopobj->mode;
  409. gopobj->mode.max_mode = 1;
  410. gopobj->mode.info = &gopobj->info;
  411. gopobj->mode.info_size = sizeof(gopobj->info);
  412. #ifdef CONFIG_DM_VIDEO
  413. if (bpix == VIDEO_BPP32)
  414. #else
  415. if (bpix == LCD_COLOR32)
  416. #endif
  417. {
  418. /* With 32bit color space we can directly expose the fb */
  419. gopobj->mode.fb_base = fb_base;
  420. gopobj->mode.fb_size = fb_size;
  421. }
  422. gopobj->info.version = 0;
  423. gopobj->info.width = col;
  424. gopobj->info.height = row;
  425. gopobj->info.pixel_format = EFI_GOT_RGBA8;
  426. gopobj->info.pixels_per_scanline = col;
  427. gopobj->bpix = bpix;
  428. gopobj->fb = fb;
  429. return EFI_SUCCESS;
  430. }