1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application disk support
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
21 /* Generic EFI object parent class data */
22 struct efi_object parent;
23 /* EFI Interface callback struct for gop */
25 /* The only mode we support */
26 struct efi_gop_mode_info info;
27 struct efi_gop_mode mode;
28 /* Fields we only have acces to during init */
33 static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
34 efi_uintn_t *size_of_info,
35 struct efi_gop_mode_info **info)
37 struct efi_gop_obj *gopobj;
39 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
41 gopobj = container_of(this, struct efi_gop_obj, ops);
42 *size_of_info = sizeof(gopobj->info);
43 *info = &gopobj->info;
45 return EFI_EXIT(EFI_SUCCESS);
48 static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
50 EFI_ENTRY("%p, %x", this, mode_number);
53 return EFI_EXIT(EFI_INVALID_PARAMETER);
55 return EFI_EXIT(EFI_SUCCESS);
58 static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
60 struct efi_gop_pixel blt = {
64 blt.blue = (vid & 0x1f) << 3;
66 blt.green = (vid & 0x3f) << 2;
68 blt.red = (vid & 0x1f) << 3;
72 static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
74 return (u16)(blt->red >> 3) << 11 |
75 (u16)(blt->green >> 2) << 5 |
76 (u16)(blt->blue >> 3);
79 static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
80 struct efi_gop_pixel *bufferp,
81 u32 operation, efi_uintn_t sx,
82 efi_uintn_t sy, efi_uintn_t dx,
89 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
90 efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
91 u32 *fb32 = gopobj->fb;
92 u16 *fb16 = gopobj->fb;
93 struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
96 /* Check for 4 byte alignment */
98 return EFI_INVALID_PARAMETER;
104 /* Check source rectangle */
106 case EFI_BLT_VIDEO_FILL:
108 case EFI_BLT_BUFFER_TO_VIDEO:
109 if (sx + width > linelen)
110 return EFI_INVALID_PARAMETER;
112 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
113 case EFI_BLT_VIDEO_TO_VIDEO:
114 if (sx + width > gopobj->info.width ||
115 sy + height > gopobj->info.height)
116 return EFI_INVALID_PARAMETER;
119 return EFI_INVALID_PARAMETER;
122 /* Check destination rectangle */
124 case EFI_BLT_VIDEO_FILL:
125 case EFI_BLT_BUFFER_TO_VIDEO:
126 case EFI_BLT_VIDEO_TO_VIDEO:
127 if (dx + width > gopobj->info.width ||
128 dy + height > gopobj->info.height)
129 return EFI_INVALID_PARAMETER;
131 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
132 if (dx + width > linelen)
133 return EFI_INVALID_PARAMETER;
137 /* Calculate line width */
139 case EFI_BLT_BUFFER_TO_VIDEO:
142 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
143 case EFI_BLT_VIDEO_TO_VIDEO:
144 swidth = gopobj->info.width;
146 return EFI_UNSUPPORTED;
148 case EFI_BLT_VIDEO_FILL:
154 case EFI_BLT_BUFFER_TO_VIDEO:
155 case EFI_BLT_VIDEO_FILL:
156 case EFI_BLT_VIDEO_TO_VIDEO:
157 dwidth = gopobj->info.width;
159 return EFI_UNSUPPORTED;
161 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
166 slineoff = swidth * sy;
167 dlineoff = dwidth * dy;
168 for (i = 0; i < height; i++) {
169 for (j = 0; j < width; j++) {
170 struct efi_gop_pixel pix;
172 /* Read source pixel */
174 case EFI_BLT_VIDEO_FILL:
177 case EFI_BLT_BUFFER_TO_VIDEO:
178 pix = buffer[slineoff + j + sx];
180 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
181 case EFI_BLT_VIDEO_TO_VIDEO:
183 pix = *(struct efi_gop_pixel *)&fb32[
186 pix = efi_vid16_to_blt_col(fb16[
191 /* Write destination pixel */
193 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
194 buffer[dlineoff + j + dx] = pix;
196 case EFI_BLT_BUFFER_TO_VIDEO:
197 case EFI_BLT_VIDEO_FILL:
198 case EFI_BLT_VIDEO_TO_VIDEO:
200 fb32[dlineoff + j + dx] = *(u32 *)&pix;
202 fb16[dlineoff + j + dx] =
203 efi_blt_col_to_vid16(&pix);
214 static efi_uintn_t gop_get_bpp(struct efi_gop *this)
216 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
217 efi_uintn_t vid_bpp = 0;
219 switch (gopobj->bpix) {
220 #ifdef CONFIG_DM_VIDEO
227 #ifdef CONFIG_DM_VIDEO
240 * Gcc can't optimize our BLT function well, but we need to make sure that
241 * our 2-dimensional loop gets executed very quickly, otherwise the system
244 * By manually putting all obvious branch targets into functions which call
245 * our generic blt function with constants, the compiler can successfully
246 * optimize for speed.
248 static efi_status_t gop_blt_video_fill(struct efi_gop *this,
249 struct efi_gop_pixel *buffer,
250 u32 foo, efi_uintn_t sx,
251 efi_uintn_t sy, efi_uintn_t dx,
252 efi_uintn_t dy, efi_uintn_t width,
253 efi_uintn_t height, efi_uintn_t delta,
256 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
257 dy, width, height, delta, vid_bpp);
260 static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
261 struct efi_gop_pixel *buffer,
262 u32 foo, efi_uintn_t sx,
263 efi_uintn_t sy, efi_uintn_t dx,
264 efi_uintn_t dy, efi_uintn_t width,
265 efi_uintn_t height, efi_uintn_t delta)
267 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
268 dy, width, height, delta, 16);
271 static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this,
272 struct efi_gop_pixel *buffer,
273 u32 foo, efi_uintn_t sx,
274 efi_uintn_t sy, efi_uintn_t dx,
275 efi_uintn_t dy, efi_uintn_t width,
276 efi_uintn_t height, efi_uintn_t delta)
278 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
279 dy, width, height, delta, 32);
282 static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this,
283 struct efi_gop_pixel *buffer,
284 u32 foo, efi_uintn_t sx,
285 efi_uintn_t sy, efi_uintn_t dx,
286 efi_uintn_t dy, efi_uintn_t width,
287 efi_uintn_t height, efi_uintn_t delta,
290 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
291 dy, width, height, delta, vid_bpp);
294 static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
295 struct efi_gop_pixel *buffer,
296 u32 foo, efi_uintn_t sx,
297 efi_uintn_t sy, efi_uintn_t dx,
298 efi_uintn_t dy, efi_uintn_t width,
299 efi_uintn_t height, efi_uintn_t delta,
302 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
303 dx, dy, width, height, delta, vid_bpp);
309 * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
310 * See the Unified Extensible Firmware Interface (UEFI) specification for
313 * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
314 * @buffer: pixel buffer
315 * @sx: source x-coordinate
316 * @sy: source y-coordinate
317 * @dx: destination x-coordinate
318 * @dy: destination y-coordinate
319 * @width: width of rectangle
320 * @height: height of rectangle
321 * @delta: length in bytes of a line in the pixel buffer (optional)
322 * @return: status code
324 efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
325 u32 operation, 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)
330 efi_status_t ret = EFI_INVALID_PARAMETER;
333 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
334 buffer, operation, sx, sy, dx, dy, width, height, delta);
336 vid_bpp = gop_get_bpp(this);
338 /* Allow for compiler optimization */
340 case EFI_BLT_VIDEO_FILL:
341 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
342 dy, width, height, delta, vid_bpp);
344 case EFI_BLT_BUFFER_TO_VIDEO:
345 /* This needs to be super-fast, so duplicate for 16/32bpp */
347 ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
348 sy, dx, dy, width, height,
351 ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
352 sy, dx, dy, width, height,
355 case EFI_BLT_VIDEO_TO_VIDEO:
356 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
357 dy, width, height, delta, vid_bpp);
359 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
360 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
361 dy, width, height, delta, vid_bpp);
364 ret = EFI_UNSUPPORTED;
367 if (ret != EFI_SUCCESS)
368 return EFI_EXIT(ret);
370 #ifdef CONFIG_DM_VIDEO
376 return EFI_EXIT(EFI_SUCCESS);
380 * Install graphical output protocol.
382 * If no supported video device exists this is not considered as an
385 efi_status_t efi_gop_register(void)
387 struct efi_gop_obj *gopobj;
389 u64 fb_base, fb_size;
393 #ifdef CONFIG_DM_VIDEO
394 struct udevice *vdev;
395 struct video_priv *priv;
397 /* We only support a single video output device for now */
398 if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
399 debug("WARNING: No video device\n");
403 priv = dev_get_uclass_priv(vdev);
405 col = video_get_xsize(vdev);
406 row = video_get_ysize(vdev);
407 fb_base = (uintptr_t)priv->fb;
408 fb_size = priv->fb_size;
413 bpix = panel_info.vl_bpix;
414 col = panel_info.vl_col;
415 row = panel_info.vl_row;
416 fb_base = gd->fb_base;
417 fb_size = lcd_get_size(&line_len);
418 fb = (void*)gd->fb_base;
422 #ifdef CONFIG_DM_VIDEO
431 /* So far, we only work in 16 or 32 bit mode */
432 debug("WARNING: Unsupported video mode\n");
436 gopobj = calloc(1, sizeof(*gopobj));
438 printf("ERROR: Out of memory\n");
439 return EFI_OUT_OF_RESOURCES;
442 /* Hook up to the device list */
443 efi_add_handle(&gopobj->parent);
445 /* Fill in object data */
446 ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
448 if (ret != EFI_SUCCESS) {
449 printf("ERROR: Failure adding gop protocol\n");
452 gopobj->ops.query_mode = gop_query_mode;
453 gopobj->ops.set_mode = gop_set_mode;
454 gopobj->ops.blt = gop_blt;
455 gopobj->ops.mode = &gopobj->mode;
457 gopobj->mode.max_mode = 1;
458 gopobj->mode.info = &gopobj->info;
459 gopobj->mode.info_size = sizeof(gopobj->info);
461 #ifdef CONFIG_DM_VIDEO
462 if (bpix == VIDEO_BPP32)
464 if (bpix == LCD_COLOR32)
467 /* With 32bit color space we can directly expose the fb */
468 gopobj->mode.fb_base = fb_base;
469 gopobj->mode.fb_size = fb_size;
472 gopobj->info.version = 0;
473 gopobj->info.width = col;
474 gopobj->info.height = row;
475 gopobj->info.pixel_format = EFI_GOT_BGRA8;
476 gopobj->info.pixels_per_scanline = col;