efi_gop.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /* This gets called from do_bootefi_exec(). */
  107. int efi_gop_register(void)
  108. {
  109. struct efi_gop_obj *gopobj;
  110. u32 bpix, col, row;
  111. u64 fb_base, fb_size;
  112. void *fb;
  113. efi_status_t ret;
  114. #ifdef CONFIG_DM_VIDEO
  115. struct udevice *vdev;
  116. /* We only support a single video output device for now */
  117. if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev)
  118. return -1;
  119. struct video_priv *priv = dev_get_uclass_priv(vdev);
  120. bpix = priv->bpix;
  121. col = video_get_xsize(vdev);
  122. row = video_get_ysize(vdev);
  123. fb_base = (uintptr_t)priv->fb;
  124. fb_size = priv->fb_size;
  125. fb = priv->fb;
  126. #else
  127. int line_len;
  128. bpix = panel_info.vl_bpix;
  129. col = panel_info.vl_col;
  130. row = panel_info.vl_row;
  131. fb_base = gd->fb_base;
  132. fb_size = lcd_get_size(&line_len);
  133. fb = (void*)gd->fb_base;
  134. #endif
  135. switch (bpix) {
  136. #ifdef CONFIG_DM_VIDEO
  137. case VIDEO_BPP16:
  138. case VIDEO_BPP32:
  139. #else
  140. case LCD_COLOR32:
  141. case LCD_COLOR16:
  142. #endif
  143. break;
  144. default:
  145. /* So far, we only work in 16 or 32 bit mode */
  146. return -1;
  147. }
  148. gopobj = calloc(1, sizeof(*gopobj));
  149. if (!gopobj) {
  150. printf("ERROR: Out of memory\n");
  151. return 1;
  152. }
  153. /* Hook up to the device list */
  154. efi_add_handle(&gopobj->parent);
  155. /* Fill in object data */
  156. ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
  157. &gopobj->ops);
  158. if (ret != EFI_SUCCESS) {
  159. printf("ERROR: Out of memory\n");
  160. return 1;
  161. }
  162. gopobj->ops.query_mode = gop_query_mode;
  163. gopobj->ops.set_mode = gop_set_mode;
  164. gopobj->ops.blt = gop_blt;
  165. gopobj->ops.mode = &gopobj->mode;
  166. gopobj->mode.max_mode = 1;
  167. gopobj->mode.info = &gopobj->info;
  168. gopobj->mode.info_size = sizeof(gopobj->info);
  169. #ifdef CONFIG_DM_VIDEO
  170. if (bpix == VIDEO_BPP32) {
  171. #else
  172. if (bpix == LCD_COLOR32) {
  173. #endif
  174. /* With 32bit color space we can directly expose the fb */
  175. gopobj->mode.fb_base = fb_base;
  176. gopobj->mode.fb_size = fb_size;
  177. }
  178. gopobj->info.version = 0;
  179. gopobj->info.width = col;
  180. gopobj->info.height = row;
  181. gopobj->info.pixel_format = EFI_GOT_RGBA8;
  182. gopobj->info.pixels_per_scanline = col;
  183. gopobj->bpix = bpix;
  184. gopobj->fb = fb;
  185. return 0;
  186. }