blob: a09db31eb46513a6a19d2e48c51ad6b507734070 [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
8#include <common.h>
Alexander Graf792284f2016-06-05 22:34:31 +02009#include <dm.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010010#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010012#include <malloc.h>
Simon Glass65ea46f2023-10-01 19:14:36 -060013#include <mapmem.h>
Alexander Graf792284f2016-06-05 22:34:31 +020014#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020019static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
Alexander Grafd65783d2016-03-15 18:38:21 +010020
Heinrich Schuchardt72928722018-09-26 05:27:56 +020021/**
22 * struct efi_gop_obj - graphical output protocol object
23 *
24 * @header: EFI object header
25 * @ops: graphical output protocol interface
26 * @info: graphical output mode information
27 * @mode: graphical output mode
28 * @bpix: bits per pixel
29 * @fb: frame buffer
30 */
Alexander Grafd65783d2016-03-15 18:38:21 +010031struct efi_gop_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020032 struct efi_object header;
Alexander Grafd65783d2016-03-15 18:38:21 +010033 struct efi_gop ops;
Alexander Grafd65783d2016-03-15 18:38:21 +010034 struct efi_gop_mode_info info;
35 struct efi_gop_mode mode;
Heinrich Schuchardt72928722018-09-26 05:27:56 +020036 /* Fields we only have access to during init */
Alexander Graf792284f2016-06-05 22:34:31 +020037 u32 bpix;
Rob Clark92cec962017-07-21 15:00:27 -040038 void *fb;
Alexander Grafd65783d2016-03-15 18:38:21 +010039};
40
41static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020042 efi_uintn_t *size_of_info,
Alexander Grafd65783d2016-03-15 18:38:21 +010043 struct efi_gop_mode_info **info)
44{
45 struct efi_gop_obj *gopobj;
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020046 efi_status_t ret = EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +010047
48 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
49
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020050 if (!this || !size_of_info || !info || mode_number) {
51 ret = EFI_INVALID_PARAMETER;
52 goto out;
53 }
54
Alexander Grafd65783d2016-03-15 18:38:21 +010055 gopobj = container_of(this, struct efi_gop_obj, ops);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020056 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
57 (void **)info);
58 if (ret != EFI_SUCCESS)
59 goto out;
Alexander Grafd65783d2016-03-15 18:38:21 +010060 *size_of_info = sizeof(gopobj->info);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020061 memcpy(*info, &gopobj->info, sizeof(gopobj->info));
Alexander Grafd65783d2016-03-15 18:38:21 +010062
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020063out:
64 return EFI_EXIT(ret);
Alexander Grafd65783d2016-03-15 18:38:21 +010065}
66
Mark Kettenisd8ab3342021-09-25 22:47:39 +020067static __always_inline struct efi_gop_pixel efi_vid30_to_blt_col(u32 vid)
68{
69 struct efi_gop_pixel blt = {
70 .reserved = 0,
71 };
72
73 blt.blue = (vid & 0x3ff) >> 2;
74 vid >>= 10;
75 blt.green = (vid & 0x3ff) >> 2;
76 vid >>= 10;
77 blt.red = (vid & 0x3ff) >> 2;
78 return blt;
79}
80
81static __always_inline u32 efi_blt_col_to_vid30(struct efi_gop_pixel *blt)
82{
83 return (u32)(blt->red << 2) << 20 |
84 (u32)(blt->green << 2) << 10 |
85 (u32)(blt->blue << 2);
86}
87
Heinrich Schuchardt9370d5a2018-03-16 19:59:06 +010088static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +010089{
90 struct efi_gop_pixel blt = {
91 .reserved = 0,
92 };
93
94 blt.blue = (vid & 0x1f) << 3;
95 vid >>= 5;
96 blt.green = (vid & 0x3f) << 2;
97 vid >>= 6;
98 blt.red = (vid & 0x1f) << 3;
99 return blt;
100}
101
Heinrich Schuchardt9370d5a2018-03-16 19:59:06 +0100102static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100103{
104 return (u16)(blt->red >> 3) << 11 |
105 (u16)(blt->green >> 2) << 5 |
106 (u16)(blt->blue >> 3);
107}
108
Alexander Grafa3800bd2018-03-15 15:02:28 +0100109static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
Alexander Graf18f436c2018-03-15 15:02:29 +0100110 struct efi_gop_pixel *bufferp,
Alexander Grafa3800bd2018-03-15 15:02:28 +0100111 u32 operation, efi_uintn_t sx,
112 efi_uintn_t sy, efi_uintn_t dx,
113 efi_uintn_t dy,
114 efi_uintn_t width,
115 efi_uintn_t height,
Alexander Graf18f436c2018-03-15 15:02:29 +0100116 efi_uintn_t delta,
117 efi_uintn_t vid_bpp)
Alexander Grafd65783d2016-03-15 18:38:21 +0100118{
Alexander Graf792284f2016-06-05 22:34:31 +0200119 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
Alexander Graf18f436c2018-03-15 15:02:29 +0100120 efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100121 u32 *fb32 = gopobj->fb;
122 u16 *fb16 = gopobj->fb;
Alexander Graf18f436c2018-03-15 15:02:29 +0100123 struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100124
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100125 if (delta) {
126 /* Check for 4 byte alignment */
127 if (delta & 3)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100128 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100129 linelen = delta >> 2;
130 } else {
131 linelen = width;
132 }
133
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100134 /* Check source rectangle */
135 switch (operation) {
136 case EFI_BLT_VIDEO_FILL:
137 break;
138 case EFI_BLT_BUFFER_TO_VIDEO:
139 if (sx + width > linelen)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100140 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100141 break;
142 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
143 case EFI_BLT_VIDEO_TO_VIDEO:
144 if (sx + width > gopobj->info.width ||
145 sy + height > gopobj->info.height)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100146 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100147 break;
148 default:
Alexander Grafa3800bd2018-03-15 15:02:28 +0100149 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100150 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100151
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100152 /* Check destination rectangle */
153 switch (operation) {
154 case EFI_BLT_VIDEO_FILL:
155 case EFI_BLT_BUFFER_TO_VIDEO:
156 case EFI_BLT_VIDEO_TO_VIDEO:
157 if (dx + width > gopobj->info.width ||
158 dy + height > gopobj->info.height)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100159 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100160 break;
161 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
162 if (dx + width > linelen)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100163 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100164 break;
165 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100166
Alexander Graf18f436c2018-03-15 15:02:29 +0100167 /* Calculate line width */
168 switch (operation) {
169 case EFI_BLT_BUFFER_TO_VIDEO:
170 swidth = linelen;
171 break;
172 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
173 case EFI_BLT_VIDEO_TO_VIDEO:
174 swidth = gopobj->info.width;
175 if (!vid_bpp)
176 return EFI_UNSUPPORTED;
177 break;
178 case EFI_BLT_VIDEO_FILL:
179 swidth = 0;
180 break;
181 }
182
183 switch (operation) {
184 case EFI_BLT_BUFFER_TO_VIDEO:
185 case EFI_BLT_VIDEO_FILL:
186 case EFI_BLT_VIDEO_TO_VIDEO:
187 dwidth = gopobj->info.width;
188 if (!vid_bpp)
189 return EFI_UNSUPPORTED;
190 break;
191 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
192 dwidth = linelen;
193 break;
194 }
195
196 slineoff = swidth * sy;
197 dlineoff = dwidth * dy;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100198 for (i = 0; i < height; i++) {
199 for (j = 0; j < width; j++) {
200 struct efi_gop_pixel pix;
Alexander Grafd65783d2016-03-15 18:38:21 +0100201
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100202 /* Read source pixel */
203 switch (operation) {
204 case EFI_BLT_VIDEO_FILL:
205 pix = *buffer;
206 break;
207 case EFI_BLT_BUFFER_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100208 pix = buffer[slineoff + j + sx];
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100209 break;
210 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
211 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100212 if (vid_bpp == 32)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100213 pix = *(struct efi_gop_pixel *)&fb32[
Alexander Graf18f436c2018-03-15 15:02:29 +0100214 slineoff + j + sx];
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200215 else if (vid_bpp == 30)
216 pix = efi_vid30_to_blt_col(fb32[
217 slineoff + j + sx]);
Alexander Graf18f436c2018-03-15 15:02:29 +0100218 else
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100219 pix = efi_vid16_to_blt_col(fb16[
Alexander Graf18f436c2018-03-15 15:02:29 +0100220 slineoff + j + sx]);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100221 break;
222 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100223
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100224 /* Write destination pixel */
225 switch (operation) {
226 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
Alexander Graf18f436c2018-03-15 15:02:29 +0100227 buffer[dlineoff + j + dx] = pix;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100228 break;
229 case EFI_BLT_BUFFER_TO_VIDEO:
230 case EFI_BLT_VIDEO_FILL:
231 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100232 if (vid_bpp == 32)
233 fb32[dlineoff + j + dx] = *(u32 *)&pix;
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200234 else if (vid_bpp == 30)
235 fb32[dlineoff + j + dx] =
236 efi_blt_col_to_vid30(&pix);
Alexander Graf18f436c2018-03-15 15:02:29 +0100237 else
238 fb16[dlineoff + j + dx] =
239 efi_blt_col_to_vid16(&pix);
240 break;
241 }
242 }
243 slineoff += swidth;
244 dlineoff += dwidth;
245 }
246
247 return EFI_SUCCESS;
248}
249
250static efi_uintn_t gop_get_bpp(struct efi_gop *this)
251{
252 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
253 efi_uintn_t vid_bpp = 0;
254
255 switch (gopobj->bpix) {
Alexander Graf18f436c2018-03-15 15:02:29 +0100256 case VIDEO_BPP32:
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200257 if (gopobj->info.pixel_format == EFI_GOT_BGRA8)
258 vid_bpp = 32;
259 else
260 vid_bpp = 30;
Alexander Graf18f436c2018-03-15 15:02:29 +0100261 break;
Alexander Graf18f436c2018-03-15 15:02:29 +0100262 case VIDEO_BPP16:
Alexander Graf18f436c2018-03-15 15:02:29 +0100263 vid_bpp = 16;
264 break;
Alexander Grafd65783d2016-03-15 18:38:21 +0100265 }
266
Alexander Graf18f436c2018-03-15 15:02:29 +0100267 return vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100268}
269
270/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200271 * GCC can't optimize our BLT function well, but we need to make sure that
Alexander Grafa3800bd2018-03-15 15:02:28 +0100272 * our 2-dimensional loop gets executed very quickly, otherwise the system
273 * will feel slow.
274 *
275 * By manually putting all obvious branch targets into functions which call
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200276 * our generic BLT function with constants, the compiler can successfully
Alexander Grafa3800bd2018-03-15 15:02:28 +0100277 * optimize for speed.
278 */
279static efi_status_t gop_blt_video_fill(struct efi_gop *this,
280 struct efi_gop_pixel *buffer,
281 u32 foo, efi_uintn_t sx,
282 efi_uintn_t sy, efi_uintn_t dx,
283 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100284 efi_uintn_t height, efi_uintn_t delta,
285 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100286{
287 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100288 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100289}
290
Alexander Graf18f436c2018-03-15 15:02:29 +0100291static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
292 struct efi_gop_pixel *buffer,
293 u32 foo, efi_uintn_t sx,
294 efi_uintn_t sy, efi_uintn_t dx,
295 efi_uintn_t dy, efi_uintn_t width,
296 efi_uintn_t height, efi_uintn_t delta)
297{
298 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
299 dy, width, height, delta, 16);
300}
301
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200302static efi_status_t gop_blt_buf_to_vid30(struct efi_gop *this,
303 struct efi_gop_pixel *buffer,
304 u32 foo, efi_uintn_t sx,
305 efi_uintn_t sy, efi_uintn_t dx,
306 efi_uintn_t dy, efi_uintn_t width,
307 efi_uintn_t height, efi_uintn_t delta)
308{
309 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
310 dy, width, height, delta, 30);
311}
312
Alexander Graf18f436c2018-03-15 15:02:29 +0100313static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this,
314 struct efi_gop_pixel *buffer,
315 u32 foo, efi_uintn_t sx,
316 efi_uintn_t sy, efi_uintn_t dx,
317 efi_uintn_t dy, efi_uintn_t width,
318 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100319{
320 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100321 dy, width, height, delta, 32);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100322}
323
324static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this,
325 struct efi_gop_pixel *buffer,
326 u32 foo, efi_uintn_t sx,
327 efi_uintn_t sy, efi_uintn_t dx,
328 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100329 efi_uintn_t height, efi_uintn_t delta,
330 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100331{
332 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100333 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100334}
335
336static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
337 struct efi_gop_pixel *buffer,
338 u32 foo, efi_uintn_t sx,
339 efi_uintn_t sy, efi_uintn_t dx,
340 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100341 efi_uintn_t height, efi_uintn_t delta,
342 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100343{
344 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
Alexander Graf18f436c2018-03-15 15:02:29 +0100345 dx, dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100346}
347
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200348/**
349 * gop_set_mode() - set graphical output mode
350 *
351 * This function implements the SetMode() service.
352 *
353 * See the Unified Extensible Firmware Interface (UEFI) specification for
354 * details.
355 *
356 * @this: the graphical output protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200357 * @mode_number: the mode to be set
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200358 * Return: status code
359 */
360static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
361{
362 struct efi_gop_obj *gopobj;
363 struct efi_gop_pixel buffer = {0, 0, 0, 0};
364 efi_uintn_t vid_bpp;
365 efi_status_t ret = EFI_SUCCESS;
366
367 EFI_ENTRY("%p, %x", this, mode_number);
368
369 if (!this) {
370 ret = EFI_INVALID_PARAMETER;
371 goto out;
372 }
373 if (mode_number) {
374 ret = EFI_UNSUPPORTED;
375 goto out;
376 }
377 gopobj = container_of(this, struct efi_gop_obj, ops);
378 vid_bpp = gop_get_bpp(this);
379 ret = gop_blt_video_fill(this, &buffer, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0,
380 gopobj->info.width, gopobj->info.height, 0,
381 vid_bpp);
382out:
383 return EFI_EXIT(ret);
384}
385
Alexander Grafa3800bd2018-03-15 15:02:28 +0100386/*
387 * Copy rectangle.
388 *
389 * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
390 * See the Unified Extensible Firmware Interface (UEFI) specification for
391 * details.
392 *
393 * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
394 * @buffer: pixel buffer
395 * @sx: source x-coordinate
396 * @sy: source y-coordinate
397 * @dx: destination x-coordinate
398 * @dy: destination y-coordinate
399 * @width: width of rectangle
400 * @height: height of rectangle
401 * @delta: length in bytes of a line in the pixel buffer (optional)
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100402 * Return: status code
Alexander Grafa3800bd2018-03-15 15:02:28 +0100403 */
Heinrich Schuchardt64a44002023-02-10 09:05:03 +0100404static efi_status_t EFIAPI gop_blt(struct efi_gop *this,
405 struct efi_gop_pixel *buffer,
406 u32 operation, efi_uintn_t sx,
407 efi_uintn_t sy, efi_uintn_t dx,
408 efi_uintn_t dy, efi_uintn_t width,
409 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100410{
411 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf18f436c2018-03-15 15:02:29 +0100412 efi_uintn_t vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100413
414 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
415 buffer, operation, sx, sy, dx, dy, width, height, delta);
416
Alexander Graf18f436c2018-03-15 15:02:29 +0100417 vid_bpp = gop_get_bpp(this);
418
Alexander Grafa3800bd2018-03-15 15:02:28 +0100419 /* Allow for compiler optimization */
420 switch (operation) {
421 case EFI_BLT_VIDEO_FILL:
422 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100423 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100424 break;
425 case EFI_BLT_BUFFER_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100426 /* This needs to be super-fast, so duplicate for 16/32bpp */
427 if (vid_bpp == 32)
428 ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
429 sy, dx, dy, width, height,
430 delta);
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200431 else if (vid_bpp == 30)
432 ret = gop_blt_buf_to_vid30(this, buffer, operation, sx,
433 sy, dx, dy, width, height,
434 delta);
Alexander Graf18f436c2018-03-15 15:02:29 +0100435 else
436 ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
437 sy, dx, dy, width, height,
438 delta);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100439 break;
440 case EFI_BLT_VIDEO_TO_VIDEO:
441 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100442 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100443 break;
444 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
445 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100446 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100447 break;
448 default:
Heinrich Schuchardt087c8c42019-06-15 12:42:46 +0200449 ret = EFI_INVALID_PARAMETER;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100450 }
451
452 if (ret != EFI_SUCCESS)
453 return EFI_EXIT(ret);
454
Alexander Graf792284f2016-06-05 22:34:31 +0200455 video_sync_all();
Alexander Grafd65783d2016-03-15 18:38:21 +0100456
457 return EFI_EXIT(EFI_SUCCESS);
458}
459
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100460/*
461 * Install graphical output protocol.
462 *
463 * If no supported video device exists this is not considered as an
464 * error.
465 */
466efi_status_t efi_gop_register(void)
Alexander Grafd65783d2016-03-15 18:38:21 +0100467{
468 struct efi_gop_obj *gopobj;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200469 u32 bpix, format, col, row;
Alexander Grafbc766392016-06-07 00:57:05 +0200470 u64 fb_base, fb_size;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100471 efi_status_t ret;
Alexander Graf792284f2016-06-05 22:34:31 +0200472 struct udevice *vdev;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100473 struct video_priv *priv;
Simon Glass65ea46f2023-10-01 19:14:36 -0600474 struct video_uc_plat *plat;
Alexander Graf792284f2016-06-05 22:34:31 +0200475
476 /* We only support a single video output device for now */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200477 if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) {
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100478 debug("WARNING: No video device\n");
479 return EFI_SUCCESS;
480 }
Alexander Graf792284f2016-06-05 22:34:31 +0200481
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100482 priv = dev_get_uclass_priv(vdev);
Alexander Graf792284f2016-06-05 22:34:31 +0200483 bpix = priv->bpix;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200484 format = priv->format;
Alexander Graf792284f2016-06-05 22:34:31 +0200485 col = video_get_xsize(vdev);
486 row = video_get_ysize(vdev);
Simon Glass65ea46f2023-10-01 19:14:36 -0600487
488 plat = dev_get_uclass_plat(vdev);
489 fb_base = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
490 fb_size = plat->size;
Alexander Graf792284f2016-06-05 22:34:31 +0200491
492 switch (bpix) {
Alexander Graf792284f2016-06-05 22:34:31 +0200493 case VIDEO_BPP16:
494 case VIDEO_BPP32:
Alexander Grafd65783d2016-03-15 18:38:21 +0100495 break;
496 default:
497 /* So far, we only work in 16 or 32 bit mode */
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100498 debug("WARNING: Unsupported video mode\n");
499 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100500 }
501
502 gopobj = calloc(1, sizeof(*gopobj));
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200503 if (!gopobj) {
504 printf("ERROR: Out of memory\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100505 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200506 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100507
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100508 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200509 efi_add_handle(&gopobj->header);
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100510
Alexander Grafd65783d2016-03-15 18:38:21 +0100511 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200512 ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100513 &gopobj->ops);
514 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200515 printf("ERROR: Failure adding GOP protocol\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100516 return ret;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100517 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100518 gopobj->ops.query_mode = gop_query_mode;
519 gopobj->ops.set_mode = gop_set_mode;
520 gopobj->ops.blt = gop_blt;
521 gopobj->ops.mode = &gopobj->mode;
522
523 gopobj->mode.max_mode = 1;
524 gopobj->mode.info = &gopobj->info;
525 gopobj->mode.info_size = sizeof(gopobj->info);
Alexander Grafd65783d2016-03-15 18:38:21 +0100526
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200527 gopobj->mode.fb_base = fb_base;
528 gopobj->mode.fb_size = fb_size;
529
530 gopobj->info.version = 0;
531 gopobj->info.width = col;
532 gopobj->info.height = row;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100533 if (bpix == VIDEO_BPP32)
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100534 {
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200535 if (format == VIDEO_X2R10G10B10) {
536 gopobj->info.pixel_format = EFI_GOT_BITMASK;
537 gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */
538 gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */
539 gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */
540 gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */
541 } else {
542 gopobj->info.pixel_format = EFI_GOT_BGRA8;
543 }
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200544 } else {
545 gopobj->info.pixel_format = EFI_GOT_BITMASK;
546 gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
547 gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
548 gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
Alexander Grafbc766392016-06-07 00:57:05 +0200549 }
Alexander Graf792284f2016-06-05 22:34:31 +0200550 gopobj->info.pixels_per_scanline = col;
Alexander Graf792284f2016-06-05 22:34:31 +0200551 gopobj->bpix = bpix;
Simon Glass65ea46f2023-10-01 19:14:36 -0600552 gopobj->fb = map_sysmem(fb_base, fb_size);
Alexander Grafd65783d2016-03-15 18:38:21 +0100553
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100554 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100555}