efi: Drop old LCD code
[platform/kernel/u-boot.git] / lib / efi_loader / efi_gop.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI application disk support
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <efi_loader.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <video.h>
14 #include <asm/global_data.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
19
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  */
30 struct efi_gop_obj {
31         struct efi_object header;
32         struct efi_gop ops;
33         struct efi_gop_mode_info info;
34         struct efi_gop_mode mode;
35         /* Fields we only have access to during init */
36         u32 bpix;
37         void *fb;
38 };
39
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)
43 {
44         struct efi_gop_obj *gopobj;
45         efi_status_t ret = EFI_SUCCESS;
46
47         EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
48
49         if (!this || !size_of_info || !info || mode_number) {
50                 ret = EFI_INVALID_PARAMETER;
51                 goto out;
52         }
53
54         gopobj = container_of(this, struct efi_gop_obj, ops);
55         ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
56                                 (void **)info);
57         if (ret != EFI_SUCCESS)
58                 goto out;
59         *size_of_info = sizeof(gopobj->info);
60         memcpy(*info, &gopobj->info, sizeof(gopobj->info));
61
62 out:
63         return EFI_EXIT(ret);
64 }
65
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
87 static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
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
101 static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
102 {
103         return (u16)(blt->red   >> 3) << 11 |
104                (u16)(blt->green >> 2) <<  5 |
105                (u16)(blt->blue  >> 3);
106 }
107
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,
112                                                 efi_uintn_t dy,
113                                                 efi_uintn_t width,
114                                                 efi_uintn_t height,
115                                                 efi_uintn_t delta,
116                                                 efi_uintn_t vid_bpp)
117 {
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);
123
124         if (delta) {
125                 /* Check for 4 byte alignment */
126                 if (delta & 3)
127                         return EFI_INVALID_PARAMETER;
128                 linelen = delta >> 2;
129         } else {
130                 linelen = width;
131         }
132
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)
139                         return EFI_INVALID_PARAMETER;
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)
145                         return EFI_INVALID_PARAMETER;
146                 break;
147         default:
148                 return EFI_INVALID_PARAMETER;
149         }
150
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)
158                         return EFI_INVALID_PARAMETER;
159                 break;
160         case EFI_BLT_VIDEO_TO_BLT_BUFFER:
161                 if (dx + width > linelen)
162                         return EFI_INVALID_PARAMETER;
163                 break;
164         }
165
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;
197         for (i = 0; i < height; i++) {
198                 for (j = 0; j < width; j++) {
199                         struct efi_gop_pixel pix;
200
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:
207                                 pix = buffer[slineoff + j + sx];
208                                 break;
209                         case EFI_BLT_VIDEO_TO_BLT_BUFFER:
210                         case EFI_BLT_VIDEO_TO_VIDEO:
211                                 if (vid_bpp == 32)
212                                         pix = *(struct efi_gop_pixel *)&fb32[
213                                                 slineoff + j + sx];
214                                 else if (vid_bpp == 30)
215                                         pix = efi_vid30_to_blt_col(fb32[
216                                                 slineoff + j + sx]);
217                                 else
218                                         pix = efi_vid16_to_blt_col(fb16[
219                                                 slineoff + j + sx]);
220                                 break;
221                         }
222
223                         /* Write destination pixel */
224                         switch (operation) {
225                         case EFI_BLT_VIDEO_TO_BLT_BUFFER:
226                                 buffer[dlineoff + j + dx] = pix;
227                                 break;
228                         case EFI_BLT_BUFFER_TO_VIDEO:
229                         case EFI_BLT_VIDEO_FILL:
230                         case EFI_BLT_VIDEO_TO_VIDEO:
231                                 if (vid_bpp == 32)
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);
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) {
255         case VIDEO_BPP32:
256                 if (gopobj->info.pixel_format == EFI_GOT_BGRA8)
257                         vid_bpp = 32;
258                 else
259                         vid_bpp = 30;
260                 break;
261         case VIDEO_BPP16:
262                 vid_bpp = 16;
263                 break;
264         }
265
266         return vid_bpp;
267 }
268
269 /*
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
272  * will feel slow.
273  *
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.
277  */
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,
284                                        efi_uintn_t vid_bpp)
285 {
286         return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
287                            dy, width, height, delta, vid_bpp);
288 }
289
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)
296 {
297         return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
298                            dy, width, height, delta, 16);
299 }
300
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)
307 {
308         return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
309                            dy, width, height, delta, 30);
310 }
311
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)
318 {
319         return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
320                            dy, width, height, delta, 32);
321 }
322
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,
329                                        efi_uintn_t vid_bpp)
330 {
331         return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
332                            dy, width, height, delta, vid_bpp);
333 }
334
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,
341                                        efi_uintn_t vid_bpp)
342 {
343         return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
344                            dx, dy, width, height, delta, vid_bpp);
345 }
346
347 /**
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
356  * @mode_number:        the mode to be set
357  * Return:              status code
358  */
359 static 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);
381 out:
382         return EFI_EXIT(ret);
383 }
384
385 /*
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)
401  * Return:      status code
402  */
403 efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
404                             u32 operation, efi_uintn_t sx,
405                             efi_uintn_t sy, efi_uintn_t dx,
406                             efi_uintn_t dy, efi_uintn_t width,
407                             efi_uintn_t height, efi_uintn_t delta)
408 {
409         efi_status_t ret = EFI_INVALID_PARAMETER;
410         efi_uintn_t vid_bpp;
411
412         EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
413                   buffer, operation, sx, sy, dx, dy, width, height, delta);
414
415         vid_bpp = gop_get_bpp(this);
416
417         /* Allow for compiler optimization */
418         switch (operation) {
419         case EFI_BLT_VIDEO_FILL:
420                 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
421                                          dy, width, height, delta, vid_bpp);
422                 break;
423         case EFI_BLT_BUFFER_TO_VIDEO:
424                 /* This needs to be super-fast, so duplicate for 16/32bpp */
425                 if (vid_bpp == 32)
426                         ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
427                                                    sy, dx, dy, width, height,
428                                                    delta);
429                 else if (vid_bpp == 30)
430                         ret = gop_blt_buf_to_vid30(this, buffer, operation, sx,
431                                                    sy, dx, dy, width, height,
432                                                    delta);
433                 else
434                         ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
435                                                    sy, dx, dy, width, height,
436                                                    delta);
437                 break;
438         case EFI_BLT_VIDEO_TO_VIDEO:
439                 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
440                                          dy, width, height, delta, vid_bpp);
441                 break;
442         case EFI_BLT_VIDEO_TO_BLT_BUFFER:
443                 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
444                                          dy, width, height, delta, vid_bpp);
445                 break;
446         default:
447                 ret = EFI_INVALID_PARAMETER;
448         }
449
450         if (ret != EFI_SUCCESS)
451                 return EFI_EXIT(ret);
452
453         video_sync_all();
454
455         return EFI_EXIT(EFI_SUCCESS);
456 }
457
458 /*
459  * Install graphical output protocol.
460  *
461  * If no supported video device exists this is not considered as an
462  * error.
463  */
464 efi_status_t efi_gop_register(void)
465 {
466         struct efi_gop_obj *gopobj;
467         u32 bpix, format, col, row;
468         u64 fb_base, fb_size;
469         void *fb;
470         efi_status_t ret;
471         struct udevice *vdev;
472         struct video_priv *priv;
473
474         /* We only support a single video output device for now */
475         if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) {
476                 debug("WARNING: No video device\n");
477                 return EFI_SUCCESS;
478         }
479
480         priv = dev_get_uclass_priv(vdev);
481         bpix = priv->bpix;
482         format = priv->format;
483         col = video_get_xsize(vdev);
484         row = video_get_ysize(vdev);
485         fb_base = (uintptr_t)priv->fb;
486         fb_size = priv->fb_size;
487         fb = priv->fb;
488
489         switch (bpix) {
490         case VIDEO_BPP16:
491         case VIDEO_BPP32:
492                 break;
493         default:
494                 /* So far, we only work in 16 or 32 bit mode */
495                 debug("WARNING: Unsupported video mode\n");
496                 return EFI_SUCCESS;
497         }
498
499         gopobj = calloc(1, sizeof(*gopobj));
500         if (!gopobj) {
501                 printf("ERROR: Out of memory\n");
502                 return EFI_OUT_OF_RESOURCES;
503         }
504
505         /* Hook up to the device list */
506         efi_add_handle(&gopobj->header);
507
508         /* Fill in object data */
509         ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
510                                &gopobj->ops);
511         if (ret != EFI_SUCCESS) {
512                 printf("ERROR: Failure adding GOP protocol\n");
513                 return ret;
514         }
515         gopobj->ops.query_mode = gop_query_mode;
516         gopobj->ops.set_mode = gop_set_mode;
517         gopobj->ops.blt = gop_blt;
518         gopobj->ops.mode = &gopobj->mode;
519
520         gopobj->mode.max_mode = 1;
521         gopobj->mode.info = &gopobj->info;
522         gopobj->mode.info_size = sizeof(gopobj->info);
523
524         gopobj->mode.fb_base = fb_base;
525         gopobj->mode.fb_size = fb_size;
526
527         gopobj->info.version = 0;
528         gopobj->info.width = col;
529         gopobj->info.height = row;
530         if (bpix == VIDEO_BPP32)
531         {
532                 if (format == VIDEO_X2R10G10B10) {
533                         gopobj->info.pixel_format = EFI_GOT_BITMASK;
534                         gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */
535                         gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */
536                         gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */
537                         gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */
538                 } else {
539                         gopobj->info.pixel_format = EFI_GOT_BGRA8;
540                 }
541         } else {
542                 gopobj->info.pixel_format = EFI_GOT_BITMASK;
543                 gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
544                 gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
545                 gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
546         }
547         gopobj->info.pixels_per_scanline = col;
548         gopobj->bpix = bpix;
549         gopobj->fb = fb;
550
551         return EFI_SUCCESS;
552 }