blob: 28818eb781a1b108903a55fdbaff2e74456720fa [file] [log] [blame]
Alexander Grafd65783d2016-03-15 18:38:21 +01001/*
2 * EFI application disk support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
Alexander Graf792284f2016-06-05 22:34:31 +020010#include <dm.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010011#include <efi_loader.h>
12#include <inttypes.h>
13#include <lcd.h>
14#include <malloc.h>
Alexander Graf792284f2016-06-05 22:34:31 +020015#include <video.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
19static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
20
21struct efi_gop_obj {
22 /* Generic EFI object parent class data */
23 struct efi_object parent;
24 /* EFI Interface callback struct for gop */
25 struct efi_gop ops;
26 /* The only mode we support */
27 struct efi_gop_mode_info info;
28 struct efi_gop_mode mode;
Alexander Graf792284f2016-06-05 22:34:31 +020029 /* Fields we only have acces to during init */
30 u32 bpix;
Rob Clark92cec962017-07-21 15:00:27 -040031 void *fb;
Alexander Grafd65783d2016-03-15 18:38:21 +010032};
33
34static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020035 efi_uintn_t *size_of_info,
Alexander Grafd65783d2016-03-15 18:38:21 +010036 struct efi_gop_mode_info **info)
37{
38 struct efi_gop_obj *gopobj;
39
40 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
41
42 gopobj = container_of(this, struct efi_gop_obj, ops);
43 *size_of_info = sizeof(gopobj->info);
44 *info = &gopobj->info;
45
46 return EFI_EXIT(EFI_SUCCESS);
47}
48
49static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
50{
51 EFI_ENTRY("%p, %x", this, mode_number);
52
53 if (mode_number != 0)
54 return EFI_EXIT(EFI_INVALID_PARAMETER);
55
56 return EFI_EXIT(EFI_SUCCESS);
57}
58
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020059efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer,
60 u32 operation, efi_uintn_t sx,
61 efi_uintn_t sy, efi_uintn_t dx,
62 efi_uintn_t dy, efi_uintn_t width,
63 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafd65783d2016-03-15 18:38:21 +010064{
Alexander Graf792284f2016-06-05 22:34:31 +020065 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
Alexander Grafd65783d2016-03-15 18:38:21 +010066 int i, j, line_len16, line_len32;
67 void *fb;
68
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020069 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
Alexander Grafd65783d2016-03-15 18:38:21 +010070 buffer, operation, sx, sy, dx, dy, width, height, delta);
71
72 if (operation != EFI_BLT_BUFFER_TO_VIDEO)
73 return EFI_EXIT(EFI_INVALID_PARAMETER);
74
Rob Clark92cec962017-07-21 15:00:27 -040075 fb = gopobj->fb;
Alexander Graf792284f2016-06-05 22:34:31 +020076 line_len16 = gopobj->info.width * sizeof(u16);
77 line_len32 = gopobj->info.width * sizeof(u32);
Alexander Grafd65783d2016-03-15 18:38:21 +010078
79 /* Copy the contents line by line */
80
Alexander Graf792284f2016-06-05 22:34:31 +020081 switch (gopobj->bpix) {
82#ifdef CONFIG_DM_VIDEO
83 case VIDEO_BPP32:
84#else
Alexander Grafd65783d2016-03-15 18:38:21 +010085 case LCD_COLOR32:
Alexander Graf792284f2016-06-05 22:34:31 +020086#endif
Alexander Grafd65783d2016-03-15 18:38:21 +010087 for (i = 0; i < height; i++) {
88 u32 *dest = fb + ((i + dy) * line_len32) +
89 (dx * sizeof(u32));
90 u32 *src = buffer + ((i + sy) * line_len32) +
91 (sx * sizeof(u32));
92
93 /* Same color format, just memcpy */
94 memcpy(dest, src, width * sizeof(u32));
95 }
96 break;
Alexander Graf792284f2016-06-05 22:34:31 +020097#ifdef CONFIG_DM_VIDEO
98 case VIDEO_BPP16:
99#else
Alexander Grafd65783d2016-03-15 18:38:21 +0100100 case LCD_COLOR16:
Alexander Graf792284f2016-06-05 22:34:31 +0200101#endif
Alexander Grafd65783d2016-03-15 18:38:21 +0100102 for (i = 0; i < height; i++) {
103 u16 *dest = fb + ((i + dy) * line_len16) +
104 (dx * sizeof(u16));
105 u32 *src = buffer + ((i + sy) * line_len32) +
106 (sx * sizeof(u32));
107
108 /* Convert from rgb888 to rgb565 */
109 for (j = 0; j < width; j++) {
110 u32 rgb888 = src[j];
111 dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) |
112 (((rgb888 >> (8 + 2)) & 0x3f) << 5) |
113 (((rgb888 >> (0 + 3)) & 0x1f) << 0));
114 }
115 }
116 break;
117 }
118
Alexander Graf792284f2016-06-05 22:34:31 +0200119#ifdef CONFIG_DM_VIDEO
120 video_sync_all();
121#else
Alexander Grafd65783d2016-03-15 18:38:21 +0100122 lcd_sync();
Alexander Graf792284f2016-06-05 22:34:31 +0200123#endif
Alexander Grafd65783d2016-03-15 18:38:21 +0100124
125 return EFI_EXIT(EFI_SUCCESS);
126}
127
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100128/*
129 * Install graphical output protocol.
130 *
131 * If no supported video device exists this is not considered as an
132 * error.
133 */
134efi_status_t efi_gop_register(void)
Alexander Grafd65783d2016-03-15 18:38:21 +0100135{
136 struct efi_gop_obj *gopobj;
Alexander Graf792284f2016-06-05 22:34:31 +0200137 u32 bpix, col, row;
Alexander Grafbc766392016-06-07 00:57:05 +0200138 u64 fb_base, fb_size;
Rob Clark92cec962017-07-21 15:00:27 -0400139 void *fb;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100140 efi_status_t ret;
Alexander Grafd65783d2016-03-15 18:38:21 +0100141
Alexander Graf792284f2016-06-05 22:34:31 +0200142#ifdef CONFIG_DM_VIDEO
143 struct udevice *vdev;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100144 struct video_priv *priv;
Alexander Graf792284f2016-06-05 22:34:31 +0200145
146 /* We only support a single video output device for now */
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100147 if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
148 debug("WARNING: No video device\n");
149 return EFI_SUCCESS;
150 }
Alexander Graf792284f2016-06-05 22:34:31 +0200151
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100152 priv = dev_get_uclass_priv(vdev);
Alexander Graf792284f2016-06-05 22:34:31 +0200153 bpix = priv->bpix;
154 col = video_get_xsize(vdev);
155 row = video_get_ysize(vdev);
Alexander Grafbc766392016-06-07 00:57:05 +0200156 fb_base = (uintptr_t)priv->fb;
157 fb_size = priv->fb_size;
Rob Clark92cec962017-07-21 15:00:27 -0400158 fb = priv->fb;
Alexander Graf792284f2016-06-05 22:34:31 +0200159#else
Alexander Grafbc766392016-06-07 00:57:05 +0200160 int line_len;
Alexander Graf792284f2016-06-05 22:34:31 +0200161
162 bpix = panel_info.vl_bpix;
163 col = panel_info.vl_col;
164 row = panel_info.vl_row;
Alexander Grafbc766392016-06-07 00:57:05 +0200165 fb_base = gd->fb_base;
166 fb_size = lcd_get_size(&line_len);
Alexander Grafda29c602017-07-31 09:15:57 +0200167 fb = (void*)gd->fb_base;
Alexander Graf792284f2016-06-05 22:34:31 +0200168#endif
169
170 switch (bpix) {
171#ifdef CONFIG_DM_VIDEO
172 case VIDEO_BPP16:
173 case VIDEO_BPP32:
174#else
Alexander Grafd65783d2016-03-15 18:38:21 +0100175 case LCD_COLOR32:
176 case LCD_COLOR16:
Alexander Graf792284f2016-06-05 22:34:31 +0200177#endif
Alexander Grafd65783d2016-03-15 18:38:21 +0100178 break;
179 default:
180 /* So far, we only work in 16 or 32 bit mode */
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100181 debug("WARNING: Unsupported video mode\n");
182 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100183 }
184
185 gopobj = calloc(1, sizeof(*gopobj));
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200186 if (!gopobj) {
187 printf("ERROR: Out of memory\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100188 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200189 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100190
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100191 /* Hook up to the device list */
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100192 efi_add_handle(&gopobj->parent);
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100193
Alexander Grafd65783d2016-03-15 18:38:21 +0100194 /* Fill in object data */
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100195 ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
196 &gopobj->ops);
197 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100198 printf("ERROR: Failure adding gop protocol\n");
199 return ret;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100200 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100201 gopobj->ops.query_mode = gop_query_mode;
202 gopobj->ops.set_mode = gop_set_mode;
203 gopobj->ops.blt = gop_blt;
204 gopobj->ops.mode = &gopobj->mode;
205
206 gopobj->mode.max_mode = 1;
207 gopobj->mode.info = &gopobj->info;
208 gopobj->mode.info_size = sizeof(gopobj->info);
Alexander Grafd65783d2016-03-15 18:38:21 +0100209
Alexander Grafbc766392016-06-07 00:57:05 +0200210#ifdef CONFIG_DM_VIDEO
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100211 if (bpix == VIDEO_BPP32)
Alexander Grafbc766392016-06-07 00:57:05 +0200212#else
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100213 if (bpix == LCD_COLOR32)
Alexander Grafbc766392016-06-07 00:57:05 +0200214#endif
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100215 {
Alexander Grafbc766392016-06-07 00:57:05 +0200216 /* With 32bit color space we can directly expose the fb */
217 gopobj->mode.fb_base = fb_base;
218 gopobj->mode.fb_size = fb_size;
219 }
220
Alexander Grafd65783d2016-03-15 18:38:21 +0100221 gopobj->info.version = 0;
Alexander Graf792284f2016-06-05 22:34:31 +0200222 gopobj->info.width = col;
223 gopobj->info.height = row;
Alexander Grafd65783d2016-03-15 18:38:21 +0100224 gopobj->info.pixel_format = EFI_GOT_RGBA8;
Alexander Graf792284f2016-06-05 22:34:31 +0200225 gopobj->info.pixels_per_scanline = col;
226
227 gopobj->bpix = bpix;
Rob Clark92cec962017-07-21 15:00:27 -0400228 gopobj->fb = fb;
Alexander Grafd65783d2016-03-15 18:38:21 +0100229
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100230 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100231}