efi_gop.c 12 KB

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