efi_gop.c 12 KB

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