1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application disk support
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
14 #include <asm/global_data.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
21 * struct efi_gop_obj - graphical output protocol object
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
31 struct efi_object header;
33 struct efi_gop_mode_info info;
34 struct efi_gop_mode mode;
35 /* Fields we only have access to during init */
40 static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
41 efi_uintn_t *size_of_info,
42 struct efi_gop_mode_info **info)
44 struct efi_gop_obj *gopobj;
45 efi_status_t ret = EFI_SUCCESS;
47 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
49 if (!this || !size_of_info || !info || mode_number) {
50 ret = EFI_INVALID_PARAMETER;
54 gopobj = container_of(this, struct efi_gop_obj, ops);
55 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
57 if (ret != EFI_SUCCESS)
59 *size_of_info = sizeof(gopobj->info);
60 memcpy(*info, &gopobj->info, sizeof(gopobj->info));
66 static __always_inline struct efi_gop_pixel efi_vid30_to_blt_col(u32 vid)
68 struct efi_gop_pixel blt = {
72 blt.blue = (vid & 0x3ff) >> 2;
74 blt.green = (vid & 0x3ff) >> 2;
76 blt.red = (vid & 0x3ff) >> 2;
80 static __always_inline u32 efi_blt_col_to_vid30(struct efi_gop_pixel *blt)
82 return (u32)(blt->red << 2) << 20 |
83 (u32)(blt->green << 2) << 10 |
84 (u32)(blt->blue << 2);
87 static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
89 struct efi_gop_pixel blt = {
93 blt.blue = (vid & 0x1f) << 3;
95 blt.green = (vid & 0x3f) << 2;
97 blt.red = (vid & 0x1f) << 3;
101 static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
103 return (u16)(blt->red >> 3) << 11 |
104 (u16)(blt->green >> 2) << 5 |
105 (u16)(blt->blue >> 3);
108 static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
109 struct efi_gop_pixel *bufferp,
110 u32 operation, efi_uintn_t sx,
111 efi_uintn_t sy, efi_uintn_t dx,
118 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
119 efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
120 u32 *fb32 = gopobj->fb;
121 u16 *fb16 = gopobj->fb;
122 struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
125 /* Check for 4 byte alignment */
127 return EFI_INVALID_PARAMETER;
128 linelen = delta >> 2;
133 /* Check source rectangle */
135 case EFI_BLT_VIDEO_FILL:
137 case EFI_BLT_BUFFER_TO_VIDEO:
138 if (sx + width > linelen)
139 return EFI_INVALID_PARAMETER;
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)
145 return EFI_INVALID_PARAMETER;
148 return EFI_INVALID_PARAMETER;
151 /* Check destination rectangle */
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)
158 return EFI_INVALID_PARAMETER;
160 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
161 if (dx + width > linelen)
162 return EFI_INVALID_PARAMETER;
166 /* Calculate line width */
168 case EFI_BLT_BUFFER_TO_VIDEO:
171 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
172 case EFI_BLT_VIDEO_TO_VIDEO:
173 swidth = gopobj->info.width;
175 return EFI_UNSUPPORTED;
177 case EFI_BLT_VIDEO_FILL:
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;
188 return EFI_UNSUPPORTED;
190 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
195 slineoff = swidth * sy;
196 dlineoff = dwidth * dy;
197 for (i = 0; i < height; i++) {
198 for (j = 0; j < width; j++) {
199 struct efi_gop_pixel pix;
201 /* Read source pixel */
203 case EFI_BLT_VIDEO_FILL:
206 case EFI_BLT_BUFFER_TO_VIDEO:
207 pix = buffer[slineoff + j + sx];
209 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
210 case EFI_BLT_VIDEO_TO_VIDEO:
212 pix = *(struct efi_gop_pixel *)&fb32[
214 else if (vid_bpp == 30)
215 pix = efi_vid30_to_blt_col(fb32[
218 pix = efi_vid16_to_blt_col(fb16[
223 /* Write destination pixel */
225 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
226 buffer[dlineoff + j + dx] = pix;
228 case EFI_BLT_BUFFER_TO_VIDEO:
229 case EFI_BLT_VIDEO_FILL:
230 case EFI_BLT_VIDEO_TO_VIDEO:
232 fb32[dlineoff + j + dx] = *(u32 *)&pix;
233 else if (vid_bpp == 30)
234 fb32[dlineoff + j + dx] =
235 efi_blt_col_to_vid30(&pix);
237 fb16[dlineoff + j + dx] =
238 efi_blt_col_to_vid16(&pix);
249 static efi_uintn_t gop_get_bpp(struct efi_gop *this)
251 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
252 efi_uintn_t vid_bpp = 0;
254 switch (gopobj->bpix) {
256 if (gopobj->info.pixel_format == EFI_GOT_BGRA8)
270 * GCC can't optimize our BLT function well, but we need to make sure that
271 * our 2-dimensional loop gets executed very quickly, otherwise the system
274 * By manually putting all obvious branch targets into functions which call
275 * our generic BLT function with constants, the compiler can successfully
276 * optimize for speed.
278 static 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,
283 efi_uintn_t height, efi_uintn_t delta,
286 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
287 dy, width, height, delta, vid_bpp);
290 static 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)
297 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
298 dy, width, height, delta, 16);
301 static 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)
308 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
309 dy, width, height, delta, 30);
312 static 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)
319 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
320 dy, width, height, delta, 32);
323 static 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,
328 efi_uintn_t height, efi_uintn_t delta,
331 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
332 dy, width, height, delta, vid_bpp);
335 static 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,
340 efi_uintn_t height, efi_uintn_t delta,
343 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
344 dx, dy, width, height, delta, vid_bpp);
348 * gop_set_mode() - set graphical output mode
350 * This function implements the SetMode() service.
352 * See the Unified Extensible Firmware Interface (UEFI) specification for
355 * @this: the graphical output protocol
356 * @mode_number: the mode to be set
357 * Return: status code
359 static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
361 struct efi_gop_obj *gopobj;
362 struct efi_gop_pixel buffer = {0, 0, 0, 0};
364 efi_status_t ret = EFI_SUCCESS;
366 EFI_ENTRY("%p, %x", this, mode_number);
369 ret = EFI_INVALID_PARAMETER;
373 ret = EFI_UNSUPPORTED;
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,
382 return EFI_EXIT(ret);
388 * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
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)
401 * Return: status code
403 static 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)
410 efi_status_t ret = EFI_INVALID_PARAMETER;
413 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
414 buffer, operation, sx, sy, dx, dy, width, height, delta);
416 vid_bpp = gop_get_bpp(this);
418 /* Allow for compiler optimization */
420 case EFI_BLT_VIDEO_FILL:
421 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
422 dy, width, height, delta, vid_bpp);
424 case EFI_BLT_BUFFER_TO_VIDEO:
425 /* This needs to be super-fast, so duplicate for 16/32bpp */
427 ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
428 sy, dx, dy, width, height,
430 else if (vid_bpp == 30)
431 ret = gop_blt_buf_to_vid30(this, buffer, operation, sx,
432 sy, dx, dy, width, height,
435 ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
436 sy, dx, dy, width, height,
439 case EFI_BLT_VIDEO_TO_VIDEO:
440 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
441 dy, width, height, delta, vid_bpp);
443 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
444 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
445 dy, width, height, delta, vid_bpp);
448 ret = EFI_INVALID_PARAMETER;
451 if (ret != EFI_SUCCESS)
452 return EFI_EXIT(ret);
456 return EFI_EXIT(EFI_SUCCESS);
460 * Install graphical output protocol.
462 * If no supported video device exists this is not considered as an
465 efi_status_t efi_gop_register(void)
467 struct efi_gop_obj *gopobj;
468 u32 bpix, format, col, row;
469 u64 fb_base, fb_size;
472 struct udevice *vdev;
473 struct video_priv *priv;
475 /* We only support a single video output device for now */
476 if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) {
477 debug("WARNING: No video device\n");
481 priv = dev_get_uclass_priv(vdev);
483 format = priv->format;
484 col = video_get_xsize(vdev);
485 row = video_get_ysize(vdev);
486 fb_base = (uintptr_t)priv->fb;
487 fb_size = priv->fb_size;
495 /* So far, we only work in 16 or 32 bit mode */
496 debug("WARNING: Unsupported video mode\n");
500 gopobj = calloc(1, sizeof(*gopobj));
502 printf("ERROR: Out of memory\n");
503 return EFI_OUT_OF_RESOURCES;
506 /* Hook up to the device list */
507 efi_add_handle(&gopobj->header);
509 /* Fill in object data */
510 ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
512 if (ret != EFI_SUCCESS) {
513 printf("ERROR: Failure adding GOP protocol\n");
516 gopobj->ops.query_mode = gop_query_mode;
517 gopobj->ops.set_mode = gop_set_mode;
518 gopobj->ops.blt = gop_blt;
519 gopobj->ops.mode = &gopobj->mode;
521 gopobj->mode.max_mode = 1;
522 gopobj->mode.info = &gopobj->info;
523 gopobj->mode.info_size = sizeof(gopobj->info);
525 gopobj->mode.fb_base = fb_base;
526 gopobj->mode.fb_size = fb_size;
528 gopobj->info.version = 0;
529 gopobj->info.width = col;
530 gopobj->info.height = row;
531 if (bpix == VIDEO_BPP32)
533 if (format == VIDEO_X2R10G10B10) {
534 gopobj->info.pixel_format = EFI_GOT_BITMASK;
535 gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */
536 gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */
537 gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */
538 gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */
540 gopobj->info.pixel_format = EFI_GOT_BGRA8;
543 gopobj->info.pixel_format = EFI_GOT_BITMASK;
544 gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
545 gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
546 gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
548 gopobj->info.pixels_per_scanline = col;