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