GNU Linux-libre 4.9.330-gnu1
[releases.git] / drivers / firmware / efi / libstub / gop.c
1 /* -----------------------------------------------------------------------
2  *
3  *   Copyright 2011 Intel Corporation; author Matt Fleming
4  *
5  *   This file is part of the Linux kernel, and is made available under
6  *   the terms of the GNU General Public License version 2.
7  *
8  * ----------------------------------------------------------------------- */
9
10 #include <linux/efi.h>
11 #include <linux/screen_info.h>
12 #include <asm/efi.h>
13 #include <asm/setup.h>
14
15 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
16 {
17         u8 first, len;
18
19         first = 0;
20         len = 0;
21
22         if (mask) {
23                 while (!(mask & 0x1)) {
24                         mask = mask >> 1;
25                         first++;
26                 }
27
28                 while (mask & 0x1) {
29                         mask = mask >> 1;
30                         len++;
31                 }
32         }
33
34         *pos = first;
35         *size = len;
36 }
37
38 static void
39 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
40                  struct efi_pixel_bitmask pixel_info, int pixel_format)
41 {
42         if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
43                 si->lfb_depth = 32;
44                 si->lfb_linelength = pixels_per_scan_line * 4;
45                 si->red_size = 8;
46                 si->red_pos = 0;
47                 si->green_size = 8;
48                 si->green_pos = 8;
49                 si->blue_size = 8;
50                 si->blue_pos = 16;
51                 si->rsvd_size = 8;
52                 si->rsvd_pos = 24;
53         } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
54                 si->lfb_depth = 32;
55                 si->lfb_linelength = pixels_per_scan_line * 4;
56                 si->red_size = 8;
57                 si->red_pos = 16;
58                 si->green_size = 8;
59                 si->green_pos = 8;
60                 si->blue_size = 8;
61                 si->blue_pos = 0;
62                 si->rsvd_size = 8;
63                 si->rsvd_pos = 24;
64         } else if (pixel_format == PIXEL_BIT_MASK) {
65                 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
66                 find_bits(pixel_info.green_mask, &si->green_pos,
67                           &si->green_size);
68                 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
69                 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
70                           &si->rsvd_size);
71                 si->lfb_depth = si->red_size + si->green_size +
72                         si->blue_size + si->rsvd_size;
73                 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
74         } else {
75                 si->lfb_depth = 4;
76                 si->lfb_linelength = si->lfb_width / 2;
77                 si->red_size = 0;
78                 si->red_pos = 0;
79                 si->green_size = 0;
80                 si->green_pos = 0;
81                 si->blue_size = 0;
82                 si->blue_pos = 0;
83                 si->rsvd_size = 0;
84                 si->rsvd_pos = 0;
85         }
86 }
87
88 static efi_status_t
89 setup_gop32(efi_system_table_t *sys_table_arg, struct screen_info *si,
90             efi_guid_t *proto, unsigned long size, void **gop_handle)
91 {
92         struct efi_graphics_output_protocol_32 *gop32, *first_gop;
93         unsigned long nr_gops;
94         u16 width, height;
95         u32 pixels_per_scan_line;
96         u32 ext_lfb_base;
97         u64 fb_base;
98         struct efi_pixel_bitmask pixel_info;
99         int pixel_format;
100         efi_status_t status;
101         u32 *handles = (u32 *)(unsigned long)gop_handle;
102         int i;
103
104         first_gop = NULL;
105         gop32 = NULL;
106
107         nr_gops = size / sizeof(u32);
108         for (i = 0; i < nr_gops; i++) {
109                 struct efi_graphics_output_protocol_mode_32 *mode;
110                 struct efi_graphics_output_mode_info *info = NULL;
111                 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
112                 bool conout_found = false;
113                 void *dummy = NULL;
114                 efi_handle_t h = (efi_handle_t)(unsigned long)handles[i];
115                 u64 current_fb_base;
116
117                 status = efi_call_early(handle_protocol, h,
118                                         proto, (void **)&gop32);
119                 if (status != EFI_SUCCESS)
120                         continue;
121
122                 status = efi_call_early(handle_protocol, h,
123                                         &conout_proto, &dummy);
124                 if (status == EFI_SUCCESS)
125                         conout_found = true;
126
127                 mode = (void *)(unsigned long)gop32->mode;
128                 info = (void *)(unsigned long)mode->info;
129                 current_fb_base = mode->frame_buffer_base;
130
131                 if ((!first_gop || conout_found) &&
132                     info->pixel_format != PIXEL_BLT_ONLY) {
133                         /*
134                          * Systems that use the UEFI Console Splitter may
135                          * provide multiple GOP devices, not all of which are
136                          * backed by real hardware. The workaround is to search
137                          * for a GOP implementing the ConOut protocol, and if
138                          * one isn't found, to just fall back to the first GOP.
139                          */
140                         width = info->horizontal_resolution;
141                         height = info->vertical_resolution;
142                         pixel_format = info->pixel_format;
143                         pixel_info = info->pixel_information;
144                         pixels_per_scan_line = info->pixels_per_scan_line;
145                         fb_base = current_fb_base;
146
147                         /*
148                          * Once we've found a GOP supporting ConOut,
149                          * don't bother looking any further.
150                          */
151                         first_gop = gop32;
152                         if (conout_found)
153                                 break;
154                 }
155         }
156
157         /* Did we find any GOPs? */
158         if (!first_gop)
159                 return EFI_NOT_FOUND;
160
161         /* EFI framebuffer */
162         si->orig_video_isVGA = VIDEO_TYPE_EFI;
163
164         si->lfb_width = width;
165         si->lfb_height = height;
166         si->lfb_base = fb_base;
167
168         ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
169         if (ext_lfb_base) {
170                 si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
171                 si->ext_lfb_base = ext_lfb_base;
172         }
173
174         si->pages = 1;
175
176         setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
177
178         si->lfb_size = si->lfb_linelength * si->lfb_height;
179
180         si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
181
182         return EFI_SUCCESS;
183 }
184
185 static efi_status_t
186 setup_gop64(efi_system_table_t *sys_table_arg, struct screen_info *si,
187             efi_guid_t *proto, unsigned long size, void **gop_handle)
188 {
189         struct efi_graphics_output_protocol_64 *gop64, *first_gop;
190         unsigned long nr_gops;
191         u16 width, height;
192         u32 pixels_per_scan_line;
193         u32 ext_lfb_base;
194         u64 fb_base;
195         struct efi_pixel_bitmask pixel_info;
196         int pixel_format;
197         efi_status_t status;
198         u64 *handles = (u64 *)(unsigned long)gop_handle;
199         int i;
200
201         first_gop = NULL;
202         gop64 = NULL;
203
204         nr_gops = size / sizeof(u64);
205         for (i = 0; i < nr_gops; i++) {
206                 struct efi_graphics_output_protocol_mode_64 *mode;
207                 struct efi_graphics_output_mode_info *info = NULL;
208                 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
209                 bool conout_found = false;
210                 void *dummy = NULL;
211                 efi_handle_t h = (efi_handle_t)(unsigned long)handles[i];
212                 u64 current_fb_base;
213
214                 status = efi_call_early(handle_protocol, h,
215                                         proto, (void **)&gop64);
216                 if (status != EFI_SUCCESS)
217                         continue;
218
219                 status = efi_call_early(handle_protocol, h,
220                                         &conout_proto, &dummy);
221                 if (status == EFI_SUCCESS)
222                         conout_found = true;
223
224                 mode = (void *)(unsigned long)gop64->mode;
225                 info = (void *)(unsigned long)mode->info;
226                 current_fb_base = mode->frame_buffer_base;
227
228                 if ((!first_gop || conout_found) &&
229                     info->pixel_format != PIXEL_BLT_ONLY) {
230                         /*
231                          * Systems that use the UEFI Console Splitter may
232                          * provide multiple GOP devices, not all of which are
233                          * backed by real hardware. The workaround is to search
234                          * for a GOP implementing the ConOut protocol, and if
235                          * one isn't found, to just fall back to the first GOP.
236                          */
237                         width = info->horizontal_resolution;
238                         height = info->vertical_resolution;
239                         pixel_format = info->pixel_format;
240                         pixel_info = info->pixel_information;
241                         pixels_per_scan_line = info->pixels_per_scan_line;
242                         fb_base = current_fb_base;
243
244                         /*
245                          * Once we've found a GOP supporting ConOut,
246                          * don't bother looking any further.
247                          */
248                         first_gop = gop64;
249                         if (conout_found)
250                                 break;
251                 }
252         }
253
254         /* Did we find any GOPs? */
255         if (!first_gop)
256                 return EFI_NOT_FOUND;
257
258         /* EFI framebuffer */
259         si->orig_video_isVGA = VIDEO_TYPE_EFI;
260
261         si->lfb_width = width;
262         si->lfb_height = height;
263         si->lfb_base = fb_base;
264
265         ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
266         if (ext_lfb_base) {
267                 si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
268                 si->ext_lfb_base = ext_lfb_base;
269         }
270
271         si->pages = 1;
272
273         setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
274
275         si->lfb_size = si->lfb_linelength * si->lfb_height;
276
277         si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
278
279         return EFI_SUCCESS;
280 }
281
282 /*
283  * See if we have Graphics Output Protocol
284  */
285 efi_status_t efi_setup_gop(efi_system_table_t *sys_table_arg,
286                            struct screen_info *si, efi_guid_t *proto,
287                            unsigned long size)
288 {
289         efi_status_t status;
290         void **gop_handle = NULL;
291
292         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
293                                 size, (void **)&gop_handle);
294         if (status != EFI_SUCCESS)
295                 return status;
296
297         status = efi_call_early(locate_handle,
298                                 EFI_LOCATE_BY_PROTOCOL,
299                                 proto, NULL, &size, gop_handle);
300         if (status != EFI_SUCCESS)
301                 goto free_handle;
302
303         if (efi_is_64bit()) {
304                 status = setup_gop64(sys_table_arg, si, proto, size,
305                                      gop_handle);
306         } else {
307                 status = setup_gop32(sys_table_arg, si, proto, size,
308                                      gop_handle);
309         }
310
311 free_handle:
312         efi_call_early(free_pool, gop_handle);
313         return status;
314 }