Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application disk support |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 10 | #include <dm.h> |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 11 | #include <efi_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 65ea46f | 2023-10-01 19:14:36 -0600 | [diff] [blame] | 14 | #include <mapmem.h> |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 15 | #include <video.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Heinrich Schuchardt | 788ad41 | 2019-04-20 07:39:11 +0200 | [diff] [blame] | 20 | static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 21 | |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 22 | /** |
| 23 | * struct efi_gop_obj - graphical output protocol object |
| 24 | * |
| 25 | * @header: EFI object header |
| 26 | * @ops: graphical output protocol interface |
| 27 | * @info: graphical output mode information |
| 28 | * @mode: graphical output mode |
| 29 | * @bpix: bits per pixel |
| 30 | * @fb: frame buffer |
| 31 | */ |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 32 | struct efi_gop_obj { |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 33 | struct efi_object header; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 34 | struct efi_gop ops; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 35 | struct efi_gop_mode_info info; |
| 36 | struct efi_gop_mode mode; |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 37 | /* Fields we only have access to during init */ |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 38 | u32 bpix; |
Rob Clark | 92cec96 | 2017-07-21 15:00:27 -0400 | [diff] [blame] | 39 | void *fb; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number, |
Heinrich Schuchardt | 8b28d5c | 2017-10-26 19:25:51 +0200 | [diff] [blame] | 43 | efi_uintn_t *size_of_info, |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 44 | struct efi_gop_mode_info **info) |
| 45 | { |
| 46 | struct efi_gop_obj *gopobj; |
Heinrich Schuchardt | 5e1dbf7 | 2019-06-15 12:52:35 +0200 | [diff] [blame] | 47 | efi_status_t ret = EFI_SUCCESS; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 48 | |
| 49 | EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info); |
| 50 | |
Heinrich Schuchardt | 5e1dbf7 | 2019-06-15 12:52:35 +0200 | [diff] [blame] | 51 | if (!this || !size_of_info || !info || mode_number) { |
| 52 | ret = EFI_INVALID_PARAMETER; |
| 53 | goto out; |
| 54 | } |
| 55 | |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 56 | gopobj = container_of(this, struct efi_gop_obj, ops); |
Heinrich Schuchardt | 0301e36 | 2019-06-15 14:07:40 +0200 | [diff] [blame] | 57 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info), |
| 58 | (void **)info); |
| 59 | if (ret != EFI_SUCCESS) |
| 60 | goto out; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 61 | *size_of_info = sizeof(gopobj->info); |
Heinrich Schuchardt | 0301e36 | 2019-06-15 14:07:40 +0200 | [diff] [blame] | 62 | memcpy(*info, &gopobj->info, sizeof(gopobj->info)); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 63 | |
Heinrich Schuchardt | 5e1dbf7 | 2019-06-15 12:52:35 +0200 | [diff] [blame] | 64 | out: |
| 65 | return EFI_EXIT(ret); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 68 | static __always_inline struct efi_gop_pixel efi_vid30_to_blt_col(u32 vid) |
| 69 | { |
| 70 | struct efi_gop_pixel blt = { |
| 71 | .reserved = 0, |
| 72 | }; |
| 73 | |
| 74 | blt.blue = (vid & 0x3ff) >> 2; |
| 75 | vid >>= 10; |
| 76 | blt.green = (vid & 0x3ff) >> 2; |
| 77 | vid >>= 10; |
| 78 | blt.red = (vid & 0x3ff) >> 2; |
| 79 | return blt; |
| 80 | } |
| 81 | |
| 82 | static __always_inline u32 efi_blt_col_to_vid30(struct efi_gop_pixel *blt) |
| 83 | { |
| 84 | return (u32)(blt->red << 2) << 20 | |
| 85 | (u32)(blt->green << 2) << 10 | |
| 86 | (u32)(blt->blue << 2); |
| 87 | } |
| 88 | |
Heinrich Schuchardt | 9370d5a | 2018-03-16 19:59:06 +0100 | [diff] [blame] | 89 | static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid) |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 90 | { |
| 91 | struct efi_gop_pixel blt = { |
| 92 | .reserved = 0, |
| 93 | }; |
| 94 | |
| 95 | blt.blue = (vid & 0x1f) << 3; |
| 96 | vid >>= 5; |
| 97 | blt.green = (vid & 0x3f) << 2; |
| 98 | vid >>= 6; |
| 99 | blt.red = (vid & 0x1f) << 3; |
| 100 | return blt; |
| 101 | } |
| 102 | |
Heinrich Schuchardt | 9370d5a | 2018-03-16 19:59:06 +0100 | [diff] [blame] | 103 | static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt) |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 104 | { |
| 105 | return (u16)(blt->red >> 3) << 11 | |
| 106 | (u16)(blt->green >> 2) << 5 | |
| 107 | (u16)(blt->blue >> 3); |
| 108 | } |
| 109 | |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 110 | static __always_inline efi_status_t gop_blt_int(struct efi_gop *this, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 111 | struct efi_gop_pixel *bufferp, |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 112 | u32 operation, efi_uintn_t sx, |
| 113 | efi_uintn_t sy, efi_uintn_t dx, |
| 114 | efi_uintn_t dy, |
| 115 | efi_uintn_t width, |
| 116 | efi_uintn_t height, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 117 | efi_uintn_t delta, |
| 118 | efi_uintn_t vid_bpp) |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 119 | { |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 120 | struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops); |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 121 | efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 122 | u32 *fb32 = gopobj->fb; |
| 123 | u16 *fb16 = gopobj->fb; |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 124 | struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4); |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 125 | |
Heinrich Schuchardt | 5f86d33 | 2018-03-14 19:57:02 +0100 | [diff] [blame] | 126 | if (delta) { |
| 127 | /* Check for 4 byte alignment */ |
| 128 | if (delta & 3) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 129 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 5f86d33 | 2018-03-14 19:57:02 +0100 | [diff] [blame] | 130 | linelen = delta >> 2; |
| 131 | } else { |
| 132 | linelen = width; |
| 133 | } |
| 134 | |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 135 | /* Check source rectangle */ |
| 136 | switch (operation) { |
| 137 | case EFI_BLT_VIDEO_FILL: |
| 138 | break; |
| 139 | case EFI_BLT_BUFFER_TO_VIDEO: |
| 140 | if (sx + width > linelen) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 141 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 142 | break; |
| 143 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 144 | case EFI_BLT_VIDEO_TO_VIDEO: |
| 145 | if (sx + width > gopobj->info.width || |
| 146 | sy + height > gopobj->info.height) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 147 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 148 | break; |
| 149 | default: |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 150 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 151 | } |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 152 | |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 153 | /* Check destination rectangle */ |
| 154 | switch (operation) { |
| 155 | case EFI_BLT_VIDEO_FILL: |
| 156 | case EFI_BLT_BUFFER_TO_VIDEO: |
| 157 | case EFI_BLT_VIDEO_TO_VIDEO: |
| 158 | if (dx + width > gopobj->info.width || |
| 159 | dy + height > gopobj->info.height) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 160 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 161 | break; |
| 162 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 163 | if (dx + width > linelen) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 164 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 165 | break; |
| 166 | } |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 167 | |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 168 | /* Calculate line width */ |
| 169 | switch (operation) { |
| 170 | case EFI_BLT_BUFFER_TO_VIDEO: |
| 171 | swidth = linelen; |
| 172 | break; |
| 173 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 174 | case EFI_BLT_VIDEO_TO_VIDEO: |
| 175 | swidth = gopobj->info.width; |
| 176 | if (!vid_bpp) |
| 177 | return EFI_UNSUPPORTED; |
| 178 | break; |
| 179 | case EFI_BLT_VIDEO_FILL: |
| 180 | swidth = 0; |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | switch (operation) { |
| 185 | case EFI_BLT_BUFFER_TO_VIDEO: |
| 186 | case EFI_BLT_VIDEO_FILL: |
| 187 | case EFI_BLT_VIDEO_TO_VIDEO: |
| 188 | dwidth = gopobj->info.width; |
| 189 | if (!vid_bpp) |
| 190 | return EFI_UNSUPPORTED; |
| 191 | break; |
| 192 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 193 | dwidth = linelen; |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | slineoff = swidth * sy; |
| 198 | dlineoff = dwidth * dy; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 199 | for (i = 0; i < height; i++) { |
| 200 | for (j = 0; j < width; j++) { |
| 201 | struct efi_gop_pixel pix; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 202 | |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 203 | /* Read source pixel */ |
| 204 | switch (operation) { |
| 205 | case EFI_BLT_VIDEO_FILL: |
| 206 | pix = *buffer; |
| 207 | break; |
| 208 | case EFI_BLT_BUFFER_TO_VIDEO: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 209 | pix = buffer[slineoff + j + sx]; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 210 | break; |
| 211 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 212 | case EFI_BLT_VIDEO_TO_VIDEO: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 213 | if (vid_bpp == 32) |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 214 | pix = *(struct efi_gop_pixel *)&fb32[ |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 215 | slineoff + j + sx]; |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 216 | else if (vid_bpp == 30) |
| 217 | pix = efi_vid30_to_blt_col(fb32[ |
| 218 | slineoff + j + sx]); |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 219 | else |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 220 | pix = efi_vid16_to_blt_col(fb16[ |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 221 | slineoff + j + sx]); |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 222 | break; |
| 223 | } |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 224 | |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 225 | /* Write destination pixel */ |
| 226 | switch (operation) { |
| 227 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 228 | buffer[dlineoff + j + dx] = pix; |
Heinrich Schuchardt | a2c715f | 2018-02-07 22:14:22 +0100 | [diff] [blame] | 229 | break; |
| 230 | case EFI_BLT_BUFFER_TO_VIDEO: |
| 231 | case EFI_BLT_VIDEO_FILL: |
| 232 | case EFI_BLT_VIDEO_TO_VIDEO: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 233 | if (vid_bpp == 32) |
| 234 | fb32[dlineoff + j + dx] = *(u32 *)&pix; |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 235 | else if (vid_bpp == 30) |
| 236 | fb32[dlineoff + j + dx] = |
| 237 | efi_blt_col_to_vid30(&pix); |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 238 | else |
| 239 | fb16[dlineoff + j + dx] = |
| 240 | efi_blt_col_to_vid16(&pix); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | slineoff += swidth; |
| 245 | dlineoff += dwidth; |
| 246 | } |
| 247 | |
| 248 | return EFI_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | static efi_uintn_t gop_get_bpp(struct efi_gop *this) |
| 252 | { |
| 253 | struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops); |
| 254 | efi_uintn_t vid_bpp = 0; |
| 255 | |
| 256 | switch (gopobj->bpix) { |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 257 | case VIDEO_BPP32: |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 258 | if (gopobj->info.pixel_format == EFI_GOT_BGRA8) |
| 259 | vid_bpp = 32; |
| 260 | else |
| 261 | vid_bpp = 30; |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 262 | break; |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 263 | case VIDEO_BPP16: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 264 | vid_bpp = 16; |
| 265 | break; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 268 | return vid_bpp; |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 272 | * GCC can't optimize our BLT function well, but we need to make sure that |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 273 | * our 2-dimensional loop gets executed very quickly, otherwise the system |
| 274 | * will feel slow. |
| 275 | * |
| 276 | * By manually putting all obvious branch targets into functions which call |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 277 | * our generic BLT function with constants, the compiler can successfully |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 278 | * optimize for speed. |
| 279 | */ |
| 280 | static efi_status_t gop_blt_video_fill(struct efi_gop *this, |
| 281 | struct efi_gop_pixel *buffer, |
| 282 | u32 foo, efi_uintn_t sx, |
| 283 | efi_uintn_t sy, efi_uintn_t dx, |
| 284 | efi_uintn_t dy, efi_uintn_t width, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 285 | efi_uintn_t height, efi_uintn_t delta, |
| 286 | efi_uintn_t vid_bpp) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 287 | { |
| 288 | return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 289 | dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 292 | static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this, |
| 293 | struct efi_gop_pixel *buffer, |
| 294 | u32 foo, efi_uintn_t sx, |
| 295 | efi_uintn_t sy, efi_uintn_t dx, |
| 296 | efi_uintn_t dy, efi_uintn_t width, |
| 297 | efi_uintn_t height, efi_uintn_t delta) |
| 298 | { |
| 299 | return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx, |
| 300 | dy, width, height, delta, 16); |
| 301 | } |
| 302 | |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 303 | static efi_status_t gop_blt_buf_to_vid30(struct efi_gop *this, |
| 304 | struct efi_gop_pixel *buffer, |
| 305 | u32 foo, efi_uintn_t sx, |
| 306 | efi_uintn_t sy, efi_uintn_t dx, |
| 307 | efi_uintn_t dy, efi_uintn_t width, |
| 308 | efi_uintn_t height, efi_uintn_t delta) |
| 309 | { |
| 310 | return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx, |
| 311 | dy, width, height, delta, 30); |
| 312 | } |
| 313 | |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 314 | static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this, |
| 315 | struct efi_gop_pixel *buffer, |
| 316 | u32 foo, efi_uintn_t sx, |
| 317 | efi_uintn_t sy, efi_uintn_t dx, |
| 318 | efi_uintn_t dy, efi_uintn_t width, |
| 319 | efi_uintn_t height, efi_uintn_t delta) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 320 | { |
| 321 | return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 322 | dy, width, height, delta, 32); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this, |
| 326 | struct efi_gop_pixel *buffer, |
| 327 | u32 foo, efi_uintn_t sx, |
| 328 | efi_uintn_t sy, efi_uintn_t dx, |
| 329 | efi_uintn_t dy, efi_uintn_t width, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 330 | efi_uintn_t height, efi_uintn_t delta, |
| 331 | efi_uintn_t vid_bpp) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 332 | { |
| 333 | return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 334 | dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this, |
| 338 | struct efi_gop_pixel *buffer, |
| 339 | u32 foo, efi_uintn_t sx, |
| 340 | efi_uintn_t sy, efi_uintn_t dx, |
| 341 | efi_uintn_t dy, efi_uintn_t width, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 342 | efi_uintn_t height, efi_uintn_t delta, |
| 343 | efi_uintn_t vid_bpp) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 344 | { |
| 345 | return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 346 | dx, dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Heinrich Schuchardt | e32595b | 2019-06-15 14:52:53 +0200 | [diff] [blame] | 349 | /** |
| 350 | * gop_set_mode() - set graphical output mode |
| 351 | * |
| 352 | * This function implements the SetMode() service. |
| 353 | * |
| 354 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 355 | * details. |
| 356 | * |
| 357 | * @this: the graphical output protocol |
Heinrich Schuchardt | dc305ad | 2019-09-05 20:37:13 +0200 | [diff] [blame] | 358 | * @mode_number: the mode to be set |
Heinrich Schuchardt | e32595b | 2019-06-15 14:52:53 +0200 | [diff] [blame] | 359 | * Return: status code |
| 360 | */ |
| 361 | static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number) |
| 362 | { |
| 363 | struct efi_gop_obj *gopobj; |
| 364 | struct efi_gop_pixel buffer = {0, 0, 0, 0}; |
| 365 | efi_uintn_t vid_bpp; |
| 366 | efi_status_t ret = EFI_SUCCESS; |
| 367 | |
| 368 | EFI_ENTRY("%p, %x", this, mode_number); |
| 369 | |
| 370 | if (!this) { |
| 371 | ret = EFI_INVALID_PARAMETER; |
| 372 | goto out; |
| 373 | } |
| 374 | if (mode_number) { |
| 375 | ret = EFI_UNSUPPORTED; |
| 376 | goto out; |
| 377 | } |
| 378 | gopobj = container_of(this, struct efi_gop_obj, ops); |
| 379 | vid_bpp = gop_get_bpp(this); |
| 380 | ret = gop_blt_video_fill(this, &buffer, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0, |
| 381 | gopobj->info.width, gopobj->info.height, 0, |
| 382 | vid_bpp); |
| 383 | out: |
| 384 | return EFI_EXIT(ret); |
| 385 | } |
| 386 | |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 387 | /* |
| 388 | * Copy rectangle. |
| 389 | * |
| 390 | * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL. |
| 391 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 392 | * details. |
| 393 | * |
| 394 | * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL |
| 395 | * @buffer: pixel buffer |
| 396 | * @sx: source x-coordinate |
| 397 | * @sy: source y-coordinate |
| 398 | * @dx: destination x-coordinate |
| 399 | * @dy: destination y-coordinate |
| 400 | * @width: width of rectangle |
| 401 | * @height: height of rectangle |
| 402 | * @delta: length in bytes of a line in the pixel buffer (optional) |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 403 | * Return: status code |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 404 | */ |
Heinrich Schuchardt | 64a4400 | 2023-02-10 09:05:03 +0100 | [diff] [blame] | 405 | static efi_status_t EFIAPI gop_blt(struct efi_gop *this, |
| 406 | struct efi_gop_pixel *buffer, |
| 407 | u32 operation, efi_uintn_t sx, |
| 408 | efi_uintn_t sy, efi_uintn_t dx, |
| 409 | efi_uintn_t dy, efi_uintn_t width, |
| 410 | efi_uintn_t height, efi_uintn_t delta) |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 411 | { |
| 412 | efi_status_t ret = EFI_INVALID_PARAMETER; |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 413 | efi_uintn_t vid_bpp; |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 414 | |
| 415 | EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this, |
| 416 | buffer, operation, sx, sy, dx, dy, width, height, delta); |
| 417 | |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 418 | vid_bpp = gop_get_bpp(this); |
| 419 | |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 420 | /* Allow for compiler optimization */ |
| 421 | switch (operation) { |
| 422 | case EFI_BLT_VIDEO_FILL: |
| 423 | ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 424 | dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 425 | break; |
| 426 | case EFI_BLT_BUFFER_TO_VIDEO: |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 427 | /* This needs to be super-fast, so duplicate for 16/32bpp */ |
| 428 | if (vid_bpp == 32) |
| 429 | ret = gop_blt_buf_to_vid32(this, buffer, operation, sx, |
| 430 | sy, dx, dy, width, height, |
| 431 | delta); |
Mark Kettenis | d8ab334 | 2021-09-25 22:47:39 +0200 | [diff] [blame] | 432 | else if (vid_bpp == 30) |
| 433 | ret = gop_blt_buf_to_vid30(this, buffer, operation, sx, |
| 434 | sy, dx, dy, width, height, |
| 435 | delta); |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 436 | else |
| 437 | ret = gop_blt_buf_to_vid16(this, buffer, operation, sx, |
| 438 | sy, dx, dy, width, height, |
| 439 | delta); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 440 | break; |
| 441 | case EFI_BLT_VIDEO_TO_VIDEO: |
| 442 | ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 443 | dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 444 | break; |
| 445 | case EFI_BLT_VIDEO_TO_BLT_BUFFER: |
| 446 | ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx, |
Alexander Graf | 18f436c | 2018-03-15 15:02:29 +0100 | [diff] [blame] | 447 | dy, width, height, delta, vid_bpp); |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 448 | break; |
| 449 | default: |
Heinrich Schuchardt | 087c8c4 | 2019-06-15 12:42:46 +0200 | [diff] [blame] | 450 | ret = EFI_INVALID_PARAMETER; |
Alexander Graf | a3800bd | 2018-03-15 15:02:28 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | if (ret != EFI_SUCCESS) |
| 454 | return EFI_EXIT(ret); |
| 455 | |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 456 | video_sync_all(); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 457 | |
| 458 | return EFI_EXIT(EFI_SUCCESS); |
| 459 | } |
| 460 | |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 461 | /* |
| 462 | * Install graphical output protocol. |
| 463 | * |
| 464 | * If no supported video device exists this is not considered as an |
| 465 | * error. |
| 466 | */ |
| 467 | efi_status_t efi_gop_register(void) |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 468 | { |
| 469 | struct efi_gop_obj *gopobj; |
Mark Kettenis | 3e4b3d5 | 2021-09-25 22:47:37 +0200 | [diff] [blame] | 470 | u32 bpix, format, col, row; |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 471 | u64 fb_base, fb_size; |
Heinrich Schuchardt | bde9a9b | 2017-11-26 14:05:14 +0100 | [diff] [blame] | 472 | efi_status_t ret; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 473 | struct udevice *vdev; |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 474 | struct video_priv *priv; |
Simon Glass | 65ea46f | 2023-10-01 19:14:36 -0600 | [diff] [blame] | 475 | struct video_uc_plat *plat; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 476 | |
| 477 | /* We only support a single video output device for now */ |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 478 | if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) { |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 479 | debug("WARNING: No video device\n"); |
| 480 | return EFI_SUCCESS; |
| 481 | } |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 482 | |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 483 | priv = dev_get_uclass_priv(vdev); |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 484 | bpix = priv->bpix; |
Mark Kettenis | 3e4b3d5 | 2021-09-25 22:47:37 +0200 | [diff] [blame] | 485 | format = priv->format; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 486 | col = video_get_xsize(vdev); |
| 487 | row = video_get_ysize(vdev); |
Simon Glass | 65ea46f | 2023-10-01 19:14:36 -0600 | [diff] [blame] | 488 | |
| 489 | plat = dev_get_uclass_plat(vdev); |
| 490 | fb_base = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base; |
| 491 | fb_size = plat->size; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 492 | |
| 493 | switch (bpix) { |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 494 | case VIDEO_BPP16: |
| 495 | case VIDEO_BPP32: |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 496 | break; |
| 497 | default: |
| 498 | /* So far, we only work in 16 or 32 bit mode */ |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 499 | debug("WARNING: Unsupported video mode\n"); |
| 500 | return EFI_SUCCESS; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | gopobj = calloc(1, sizeof(*gopobj)); |
Heinrich Schuchardt | 9aaa35b | 2017-10-26 19:25:45 +0200 | [diff] [blame] | 504 | if (!gopobj) { |
| 505 | printf("ERROR: Out of memory\n"); |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 506 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 9aaa35b | 2017-10-26 19:25:45 +0200 | [diff] [blame] | 507 | } |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 508 | |
Heinrich Schuchardt | bde9a9b | 2017-11-26 14:05:14 +0100 | [diff] [blame] | 509 | /* Hook up to the device list */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 510 | efi_add_handle(&gopobj->header); |
Heinrich Schuchardt | bde9a9b | 2017-11-26 14:05:14 +0100 | [diff] [blame] | 511 | |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 512 | /* Fill in object data */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 513 | ret = efi_add_protocol(&gopobj->header, &efi_gop_guid, |
Heinrich Schuchardt | bde9a9b | 2017-11-26 14:05:14 +0100 | [diff] [blame] | 514 | &gopobj->ops); |
| 515 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 516 | printf("ERROR: Failure adding GOP protocol\n"); |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 517 | return ret; |
Heinrich Schuchardt | bde9a9b | 2017-11-26 14:05:14 +0100 | [diff] [blame] | 518 | } |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 519 | gopobj->ops.query_mode = gop_query_mode; |
| 520 | gopobj->ops.set_mode = gop_set_mode; |
| 521 | gopobj->ops.blt = gop_blt; |
| 522 | gopobj->ops.mode = &gopobj->mode; |
| 523 | |
| 524 | gopobj->mode.max_mode = 1; |
| 525 | gopobj->mode.info = &gopobj->info; |
| 526 | gopobj->mode.info_size = sizeof(gopobj->info); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 527 | |
Heinrich Schuchardt | 6ea1b29 | 2019-06-15 14:52:53 +0200 | [diff] [blame] | 528 | gopobj->mode.fb_base = fb_base; |
| 529 | gopobj->mode.fb_size = fb_size; |
| 530 | |
| 531 | gopobj->info.version = 0; |
| 532 | gopobj->info.width = col; |
| 533 | gopobj->info.height = row; |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 534 | if (bpix == VIDEO_BPP32) |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 535 | { |
Mark Kettenis | 3e4b3d5 | 2021-09-25 22:47:37 +0200 | [diff] [blame] | 536 | if (format == VIDEO_X2R10G10B10) { |
| 537 | gopobj->info.pixel_format = EFI_GOT_BITMASK; |
| 538 | gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */ |
| 539 | gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */ |
| 540 | gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */ |
| 541 | gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */ |
| 542 | } else { |
| 543 | gopobj->info.pixel_format = EFI_GOT_BGRA8; |
| 544 | } |
Heinrich Schuchardt | 6ea1b29 | 2019-06-15 14:52:53 +0200 | [diff] [blame] | 545 | } else { |
| 546 | gopobj->info.pixel_format = EFI_GOT_BITMASK; |
| 547 | gopobj->info.pixel_bitmask[0] = 0xf800; /* red */ |
| 548 | gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */ |
| 549 | gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */ |
Alexander Graf | bc76639 | 2016-06-07 00:57:05 +0200 | [diff] [blame] | 550 | } |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 551 | gopobj->info.pixels_per_scanline = col; |
Alexander Graf | 792284f | 2016-06-05 22:34:31 +0200 | [diff] [blame] | 552 | gopobj->bpix = bpix; |
Simon Glass | 65ea46f | 2023-10-01 19:14:36 -0600 | [diff] [blame] | 553 | gopobj->fb = map_sysmem(fb_base, fb_size); |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 554 | |
Heinrich Schuchardt | b91c084 | 2018-03-03 15:28:55 +0100 | [diff] [blame] | 555 | return EFI_SUCCESS; |
Alexander Graf | d65783d | 2016-03-15 18:38:21 +0100 | [diff] [blame] | 556 | } |