blob: 41e12fa72460a68fca676d484d6669f73da61fc2 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafd65783d2016-03-15 18:38:21 +01002/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafd65783d2016-03-15 18:38:21 +01006 */
7
Alexander Graf792284f2016-06-05 22:34:31 +02008#include <dm.h>
Alexander Grafd65783d2016-03-15 18:38:21 +01009#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010011#include <malloc.h>
Simon Glass65ea46f2023-10-01 19:14:36 -060012#include <mapmem.h>
Alexander Graf792284f2016-06-05 22:34:31 +020013#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010015
16DECLARE_GLOBAL_DATA_PTR;
17
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020018static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
Alexander Grafd65783d2016-03-15 18:38:21 +010019
Heinrich Schuchardt72928722018-09-26 05:27:56 +020020/**
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 Grafd65783d2016-03-15 18:38:21 +010030struct efi_gop_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020031 struct efi_object header;
Alexander Grafd65783d2016-03-15 18:38:21 +010032 struct efi_gop ops;
Alexander Grafd65783d2016-03-15 18:38:21 +010033 struct efi_gop_mode_info info;
34 struct efi_gop_mode mode;
Heinrich Schuchardt72928722018-09-26 05:27:56 +020035 /* Fields we only have access to during init */
Alexander Graf792284f2016-06-05 22:34:31 +020036 u32 bpix;
Rob Clark92cec962017-07-21 15:00:27 -040037 void *fb;
Alexander Grafd65783d2016-03-15 18:38:21 +010038};
39
40static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020041 efi_uintn_t *size_of_info,
Alexander Grafd65783d2016-03-15 18:38:21 +010042 struct efi_gop_mode_info **info)
43{
44 struct efi_gop_obj *gopobj;
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020045 efi_status_t ret = EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +010046
47 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
48
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020049 if (!this || !size_of_info || !info || mode_number) {
50 ret = EFI_INVALID_PARAMETER;
51 goto out;
52 }
53
Alexander Grafd65783d2016-03-15 18:38:21 +010054 gopobj = container_of(this, struct efi_gop_obj, ops);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020055 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
56 (void **)info);
57 if (ret != EFI_SUCCESS)
58 goto out;
Alexander Grafd65783d2016-03-15 18:38:21 +010059 *size_of_info = sizeof(gopobj->info);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020060 memcpy(*info, &gopobj->info, sizeof(gopobj->info));
Alexander Grafd65783d2016-03-15 18:38:21 +010061
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020062out:
63 return EFI_EXIT(ret);
Alexander Grafd65783d2016-03-15 18:38:21 +010064}
65
Mark Kettenisd8ab3342021-09-25 22:47:39 +020066static __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
80static __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 Schuchardt9370d5a2018-03-16 19:59:06 +010087static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +010088{
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 Schuchardt9370d5a2018-03-16 19:59:06 +0100101static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100102{
103 return (u16)(blt->red >> 3) << 11 |
104 (u16)(blt->green >> 2) << 5 |
105 (u16)(blt->blue >> 3);
106}
107
Alexander Grafa3800bd2018-03-15 15:02:28 +0100108static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
Alexander Graf18f436c2018-03-15 15:02:29 +0100109 struct efi_gop_pixel *bufferp,
Alexander Grafa3800bd2018-03-15 15:02:28 +0100110 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 Graf18f436c2018-03-15 15:02:29 +0100115 efi_uintn_t delta,
116 efi_uintn_t vid_bpp)
Alexander Grafd65783d2016-03-15 18:38:21 +0100117{
Alexander Graf792284f2016-06-05 22:34:31 +0200118 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
Alexander Graf18f436c2018-03-15 15:02:29 +0100119 efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100120 u32 *fb32 = gopobj->fb;
121 u16 *fb16 = gopobj->fb;
Alexander Graf18f436c2018-03-15 15:02:29 +0100122 struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100123
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100124 if (delta) {
125 /* Check for 4 byte alignment */
126 if (delta & 3)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100127 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100128 linelen = delta >> 2;
129 } else {
130 linelen = width;
131 }
132
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100133 /* 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 Grafa3800bd2018-03-15 15:02:28 +0100139 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100140 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 Grafa3800bd2018-03-15 15:02:28 +0100145 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100146 break;
147 default:
Alexander Grafa3800bd2018-03-15 15:02:28 +0100148 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100149 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100150
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100151 /* 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 Grafa3800bd2018-03-15 15:02:28 +0100158 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100159 break;
160 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
161 if (dx + width > linelen)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100162 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100163 break;
164 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100165
Alexander Graf18f436c2018-03-15 15:02:29 +0100166 /* 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 Schuchardta2c715f2018-02-07 22:14:22 +0100197 for (i = 0; i < height; i++) {
198 for (j = 0; j < width; j++) {
199 struct efi_gop_pixel pix;
Alexander Grafd65783d2016-03-15 18:38:21 +0100200
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100201 /* 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 Graf18f436c2018-03-15 15:02:29 +0100207 pix = buffer[slineoff + j + sx];
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100208 break;
209 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
210 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100211 if (vid_bpp == 32)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100212 pix = *(struct efi_gop_pixel *)&fb32[
Alexander Graf18f436c2018-03-15 15:02:29 +0100213 slineoff + j + sx];
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200214 else if (vid_bpp == 30)
215 pix = efi_vid30_to_blt_col(fb32[
216 slineoff + j + sx]);
Alexander Graf18f436c2018-03-15 15:02:29 +0100217 else
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100218 pix = efi_vid16_to_blt_col(fb16[
Alexander Graf18f436c2018-03-15 15:02:29 +0100219 slineoff + j + sx]);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100220 break;
221 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100222
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100223 /* Write destination pixel */
224 switch (operation) {
225 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
Alexander Graf18f436c2018-03-15 15:02:29 +0100226 buffer[dlineoff + j + dx] = pix;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100227 break;
228 case EFI_BLT_BUFFER_TO_VIDEO:
229 case EFI_BLT_VIDEO_FILL:
230 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100231 if (vid_bpp == 32)
232 fb32[dlineoff + j + dx] = *(u32 *)&pix;
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200233 else if (vid_bpp == 30)
234 fb32[dlineoff + j + dx] =
235 efi_blt_col_to_vid30(&pix);
Alexander Graf18f436c2018-03-15 15:02:29 +0100236 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
249static 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) {
Alexander Graf18f436c2018-03-15 15:02:29 +0100255 case VIDEO_BPP32:
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200256 if (gopobj->info.pixel_format == EFI_GOT_BGRA8)
257 vid_bpp = 32;
258 else
259 vid_bpp = 30;
Alexander Graf18f436c2018-03-15 15:02:29 +0100260 break;
Alexander Graf18f436c2018-03-15 15:02:29 +0100261 case VIDEO_BPP16:
Alexander Graf18f436c2018-03-15 15:02:29 +0100262 vid_bpp = 16;
263 break;
Alexander Grafd65783d2016-03-15 18:38:21 +0100264 }
265
Alexander Graf18f436c2018-03-15 15:02:29 +0100266 return vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100267}
268
269/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200270 * GCC can't optimize our BLT function well, but we need to make sure that
Alexander Grafa3800bd2018-03-15 15:02:28 +0100271 * our 2-dimensional loop gets executed very quickly, otherwise the system
272 * will feel slow.
273 *
274 * By manually putting all obvious branch targets into functions which call
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200275 * our generic BLT function with constants, the compiler can successfully
Alexander Grafa3800bd2018-03-15 15:02:28 +0100276 * optimize for speed.
277 */
278static efi_status_t gop_blt_video_fill(struct efi_gop *this,
279 struct efi_gop_pixel *buffer,
280 u32 foo, efi_uintn_t sx,
281 efi_uintn_t sy, efi_uintn_t dx,
282 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100283 efi_uintn_t height, efi_uintn_t delta,
284 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100285{
286 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100287 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100288}
289
Alexander Graf18f436c2018-03-15 15:02:29 +0100290static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
291 struct efi_gop_pixel *buffer,
292 u32 foo, efi_uintn_t sx,
293 efi_uintn_t sy, efi_uintn_t dx,
294 efi_uintn_t dy, efi_uintn_t width,
295 efi_uintn_t height, efi_uintn_t delta)
296{
297 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
298 dy, width, height, delta, 16);
299}
300
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200301static efi_status_t gop_blt_buf_to_vid30(struct efi_gop *this,
302 struct efi_gop_pixel *buffer,
303 u32 foo, efi_uintn_t sx,
304 efi_uintn_t sy, efi_uintn_t dx,
305 efi_uintn_t dy, efi_uintn_t width,
306 efi_uintn_t height, efi_uintn_t delta)
307{
308 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
309 dy, width, height, delta, 30);
310}
311
Alexander Graf18f436c2018-03-15 15:02:29 +0100312static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this,
313 struct efi_gop_pixel *buffer,
314 u32 foo, efi_uintn_t sx,
315 efi_uintn_t sy, efi_uintn_t dx,
316 efi_uintn_t dy, efi_uintn_t width,
317 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100318{
319 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100320 dy, width, height, delta, 32);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100321}
322
323static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this,
324 struct efi_gop_pixel *buffer,
325 u32 foo, efi_uintn_t sx,
326 efi_uintn_t sy, efi_uintn_t dx,
327 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100328 efi_uintn_t height, efi_uintn_t delta,
329 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100330{
331 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100332 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100333}
334
335static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
336 struct efi_gop_pixel *buffer,
337 u32 foo, efi_uintn_t sx,
338 efi_uintn_t sy, efi_uintn_t dx,
339 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100340 efi_uintn_t height, efi_uintn_t delta,
341 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100342{
343 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
Alexander Graf18f436c2018-03-15 15:02:29 +0100344 dx, dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100345}
346
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200347/**
348 * gop_set_mode() - set graphical output mode
349 *
350 * This function implements the SetMode() service.
351 *
352 * See the Unified Extensible Firmware Interface (UEFI) specification for
353 * details.
354 *
355 * @this: the graphical output protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200356 * @mode_number: the mode to be set
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200357 * Return: status code
358 */
359static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
360{
361 struct efi_gop_obj *gopobj;
362 struct efi_gop_pixel buffer = {0, 0, 0, 0};
363 efi_uintn_t vid_bpp;
364 efi_status_t ret = EFI_SUCCESS;
365
366 EFI_ENTRY("%p, %x", this, mode_number);
367
368 if (!this) {
369 ret = EFI_INVALID_PARAMETER;
370 goto out;
371 }
372 if (mode_number) {
373 ret = EFI_UNSUPPORTED;
374 goto out;
375 }
376 gopobj = container_of(this, struct efi_gop_obj, ops);
377 vid_bpp = gop_get_bpp(this);
378 ret = gop_blt_video_fill(this, &buffer, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0,
379 gopobj->info.width, gopobj->info.height, 0,
380 vid_bpp);
381out:
382 return EFI_EXIT(ret);
383}
384
Alexander Grafa3800bd2018-03-15 15:02:28 +0100385/*
386 * Copy rectangle.
387 *
388 * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
390 * details.
391 *
392 * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
393 * @buffer: pixel buffer
394 * @sx: source x-coordinate
395 * @sy: source y-coordinate
396 * @dx: destination x-coordinate
397 * @dy: destination y-coordinate
398 * @width: width of rectangle
399 * @height: height of rectangle
400 * @delta: length in bytes of a line in the pixel buffer (optional)
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100401 * Return: status code
Alexander Grafa3800bd2018-03-15 15:02:28 +0100402 */
Heinrich Schuchardt64a44002023-02-10 09:05:03 +0100403static efi_status_t EFIAPI gop_blt(struct efi_gop *this,
404 struct efi_gop_pixel *buffer,
405 u32 operation, efi_uintn_t sx,
406 efi_uintn_t sy, efi_uintn_t dx,
407 efi_uintn_t dy, efi_uintn_t width,
408 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100409{
410 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf18f436c2018-03-15 15:02:29 +0100411 efi_uintn_t vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100412
413 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
414 buffer, operation, sx, sy, dx, dy, width, height, delta);
415
Alexander Graf18f436c2018-03-15 15:02:29 +0100416 vid_bpp = gop_get_bpp(this);
417
Alexander Grafa3800bd2018-03-15 15:02:28 +0100418 /* Allow for compiler optimization */
419 switch (operation) {
420 case EFI_BLT_VIDEO_FILL:
421 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100422 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100423 break;
424 case EFI_BLT_BUFFER_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100425 /* This needs to be super-fast, so duplicate for 16/32bpp */
426 if (vid_bpp == 32)
427 ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
428 sy, dx, dy, width, height,
429 delta);
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200430 else if (vid_bpp == 30)
431 ret = gop_blt_buf_to_vid30(this, buffer, operation, sx,
432 sy, dx, dy, width, height,
433 delta);
Alexander Graf18f436c2018-03-15 15:02:29 +0100434 else
435 ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
436 sy, dx, dy, width, height,
437 delta);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100438 break;
439 case EFI_BLT_VIDEO_TO_VIDEO:
440 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100441 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100442 break;
443 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
444 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100445 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100446 break;
447 default:
Heinrich Schuchardt087c8c42019-06-15 12:42:46 +0200448 ret = EFI_INVALID_PARAMETER;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100449 }
450
451 if (ret != EFI_SUCCESS)
452 return EFI_EXIT(ret);
453
Alexander Graf792284f2016-06-05 22:34:31 +0200454 video_sync_all();
Alexander Grafd65783d2016-03-15 18:38:21 +0100455
456 return EFI_EXIT(EFI_SUCCESS);
457}
458
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100459/*
460 * Install graphical output protocol.
461 *
462 * If no supported video device exists this is not considered as an
463 * error.
464 */
465efi_status_t efi_gop_register(void)
Alexander Grafd65783d2016-03-15 18:38:21 +0100466{
467 struct efi_gop_obj *gopobj;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200468 u32 bpix, format, col, row;
Alexander Grafbc766392016-06-07 00:57:05 +0200469 u64 fb_base, fb_size;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100470 efi_status_t ret;
Alexander Graf792284f2016-06-05 22:34:31 +0200471 struct udevice *vdev;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100472 struct video_priv *priv;
Simon Glass65ea46f2023-10-01 19:14:36 -0600473 struct video_uc_plat *plat;
Alexander Graf792284f2016-06-05 22:34:31 +0200474
475 /* We only support a single video output device for now */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200476 if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) {
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100477 debug("WARNING: No video device\n");
478 return EFI_SUCCESS;
479 }
Alexander Graf792284f2016-06-05 22:34:31 +0200480
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100481 priv = dev_get_uclass_priv(vdev);
Alexander Graf792284f2016-06-05 22:34:31 +0200482 bpix = priv->bpix;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200483 format = priv->format;
Alexander Graf792284f2016-06-05 22:34:31 +0200484 col = video_get_xsize(vdev);
485 row = video_get_ysize(vdev);
Simon Glass65ea46f2023-10-01 19:14:36 -0600486
487 plat = dev_get_uclass_plat(vdev);
488 fb_base = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
489 fb_size = plat->size;
Alexander Graf792284f2016-06-05 22:34:31 +0200490
491 switch (bpix) {
Alexander Graf792284f2016-06-05 22:34:31 +0200492 case VIDEO_BPP16:
493 case VIDEO_BPP32:
Alexander Grafd65783d2016-03-15 18:38:21 +0100494 break;
495 default:
496 /* So far, we only work in 16 or 32 bit mode */
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100497 debug("WARNING: Unsupported video mode\n");
498 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100499 }
500
501 gopobj = calloc(1, sizeof(*gopobj));
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200502 if (!gopobj) {
503 printf("ERROR: Out of memory\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100504 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200505 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100506
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100507 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200508 efi_add_handle(&gopobj->header);
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100509
Alexander Grafd65783d2016-03-15 18:38:21 +0100510 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200511 ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100512 &gopobj->ops);
513 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200514 printf("ERROR: Failure adding GOP protocol\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100515 return ret;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100516 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100517 gopobj->ops.query_mode = gop_query_mode;
518 gopobj->ops.set_mode = gop_set_mode;
519 gopobj->ops.blt = gop_blt;
520 gopobj->ops.mode = &gopobj->mode;
521
522 gopobj->mode.max_mode = 1;
523 gopobj->mode.info = &gopobj->info;
524 gopobj->mode.info_size = sizeof(gopobj->info);
Alexander Grafd65783d2016-03-15 18:38:21 +0100525
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200526 gopobj->mode.fb_base = fb_base;
527 gopobj->mode.fb_size = fb_size;
528
529 gopobj->info.version = 0;
530 gopobj->info.width = col;
531 gopobj->info.height = row;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100532 if (bpix == VIDEO_BPP32)
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100533 {
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200534 if (format == VIDEO_X2R10G10B10) {
535 gopobj->info.pixel_format = EFI_GOT_BITMASK;
536 gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */
537 gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */
538 gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */
539 gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */
540 } else {
541 gopobj->info.pixel_format = EFI_GOT_BGRA8;
542 }
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200543 } else {
544 gopobj->info.pixel_format = EFI_GOT_BITMASK;
545 gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
546 gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
547 gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
Alexander Grafbc766392016-06-07 00:57:05 +0200548 }
Alexander Graf792284f2016-06-05 22:34:31 +0200549 gopobj->info.pixels_per_scanline = col;
Alexander Graf792284f2016-06-05 22:34:31 +0200550 gopobj->bpix = bpix;
Simon Glass65ea46f2023-10-01 19:14:36 -0600551 gopobj->fb = map_sysmem(fb_base, fb_size);
Alexander Grafd65783d2016-03-15 18:38:21 +0100552
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100553 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100554}