efi_gop.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer,
  48. u32 operation, efi_uintn_t sx,
  49. efi_uintn_t sy, efi_uintn_t dx,
  50. efi_uintn_t dy, efi_uintn_t width,
  51. efi_uintn_t height, efi_uintn_t delta)
  52. {
  53. struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
  54. int i, j, line_len16, line_len32;
  55. void *fb;
  56. EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
  57. buffer, operation, sx, sy, dx, dy, width, height, delta);
  58. if (operation != EFI_BLT_BUFFER_TO_VIDEO)
  59. return EFI_EXIT(EFI_INVALID_PARAMETER);
  60. fb = gopobj->fb;
  61. line_len16 = gopobj->info.width * sizeof(u16);
  62. line_len32 = gopobj->info.width * sizeof(u32);
  63. /* Copy the contents line by line */
  64. switch (gopobj->bpix) {
  65. #ifdef CONFIG_DM_VIDEO
  66. case VIDEO_BPP32:
  67. #else
  68. case LCD_COLOR32:
  69. #endif
  70. for (i = 0; i < height; i++) {
  71. u32 *dest = fb + ((i + dy) * line_len32) +
  72. (dx * sizeof(u32));
  73. u32 *src = buffer + ((i + sy) * line_len32) +
  74. (sx * sizeof(u32));
  75. /* Same color format, just memcpy */
  76. memcpy(dest, src, width * sizeof(u32));
  77. }
  78. break;
  79. #ifdef CONFIG_DM_VIDEO
  80. case VIDEO_BPP16:
  81. #else
  82. case LCD_COLOR16:
  83. #endif
  84. for (i = 0; i < height; i++) {
  85. u16 *dest = fb + ((i + dy) * line_len16) +
  86. (dx * sizeof(u16));
  87. u32 *src = buffer + ((i + sy) * line_len32) +
  88. (sx * sizeof(u32));
  89. /* Convert from rgb888 to rgb565 */
  90. for (j = 0; j < width; j++) {
  91. u32 rgb888 = src[j];
  92. dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) |
  93. (((rgb888 >> (8 + 2)) & 0x3f) << 5) |
  94. (((rgb888 >> (0 + 3)) & 0x1f) << 0));
  95. }
  96. }
  97. break;
  98. }
  99. #ifdef CONFIG_DM_VIDEO
  100. video_sync_all();
  101. #else
  102. lcd_sync();
  103. #endif
  104. return EFI_EXIT(EFI_SUCCESS);
  105. }
  106. /*
  107. * Install graphical output protocol.
  108. *
  109. * If no supported video device exists this is not considered as an
  110. * error.
  111. */
  112. efi_status_t efi_gop_register(void)
  113. {
  114. struct efi_gop_obj *gopobj;
  115. u32 bpix, col, row;
  116. u64 fb_base, fb_size;
  117. void *fb;
  118. efi_status_t ret;
  119. #ifdef CONFIG_DM_VIDEO
  120. struct udevice *vdev;
  121. struct video_priv *priv;
  122. /* We only support a single video output device for now */
  123. if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
  124. debug("WARNING: No video device\n");
  125. return EFI_SUCCESS;
  126. }
  127. priv = dev_get_uclass_priv(vdev);
  128. bpix = priv->bpix;
  129. col = video_get_xsize(vdev);
  130. row = video_get_ysize(vdev);
  131. fb_base = (uintptr_t)priv->fb;
  132. fb_size = priv->fb_size;
  133. fb = priv->fb;
  134. #else
  135. int line_len;
  136. bpix = panel_info.vl_bpix;
  137. col = panel_info.vl_col;
  138. row = panel_info.vl_row;
  139. fb_base = gd->fb_base;
  140. fb_size = lcd_get_size(&line_len);
  141. fb = (void*)gd->fb_base;
  142. #endif
  143. switch (bpix) {
  144. #ifdef CONFIG_DM_VIDEO
  145. case VIDEO_BPP16:
  146. case VIDEO_BPP32:
  147. #else
  148. case LCD_COLOR32:
  149. case LCD_COLOR16:
  150. #endif
  151. break;
  152. default:
  153. /* So far, we only work in 16 or 32 bit mode */
  154. debug("WARNING: Unsupported video mode\n");
  155. return EFI_SUCCESS;
  156. }
  157. gopobj = calloc(1, sizeof(*gopobj));
  158. if (!gopobj) {
  159. printf("ERROR: Out of memory\n");
  160. return EFI_OUT_OF_RESOURCES;
  161. }
  162. /* Hook up to the device list */
  163. efi_add_handle(&gopobj->parent);
  164. /* Fill in object data */
  165. ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
  166. &gopobj->ops);
  167. if (ret != EFI_SUCCESS) {
  168. printf("ERROR: Failure adding gop protocol\n");
  169. return ret;
  170. }
  171. gopobj->ops.query_mode = gop_query_mode;
  172. gopobj->ops.set_mode = gop_set_mode;
  173. gopobj->ops.blt = gop_blt;
  174. gopobj->ops.mode = &gopobj->mode;
  175. gopobj->mode.max_mode = 1;
  176. gopobj->mode.info = &gopobj->info;
  177. gopobj->mode.info_size = sizeof(gopobj->info);
  178. #ifdef CONFIG_DM_VIDEO
  179. if (bpix == VIDEO_BPP32)
  180. #else
  181. if (bpix == LCD_COLOR32)
  182. #endif
  183. {
  184. /* With 32bit color space we can directly expose the fb */
  185. gopobj->mode.fb_base = fb_base;
  186. gopobj->mode.fb_size = fb_size;
  187. }
  188. gopobj->info.version = 0;
  189. gopobj->info.width = col;
  190. gopobj->info.height = row;
  191. gopobj->info.pixel_format = EFI_GOT_RGBA8;
  192. gopobj->info.pixels_per_scanline = col;
  193. gopobj->bpix = bpix;
  194. gopobj->fb = fb;
  195. return EFI_SUCCESS;
  196. }