xref: /openbmc/linux/drivers/firmware/efi/libstub/gop.c (revision cd33a5c1)
1 // SPDX-License-Identifier: GPL-2.0
2 /* -----------------------------------------------------------------------
3  *
4  *   Copyright 2011 Intel Corporation; author Matt Fleming
5  *
6  * ----------------------------------------------------------------------- */
7 
8 #include <linux/efi.h>
9 #include <linux/screen_info.h>
10 #include <asm/efi.h>
11 #include <asm/setup.h>
12 
13 #include "efistub.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 		 efi_pixel_bitmask_t 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 #define efi_gop_attr(table, attr, instance) \
89 	(efi_table_attr(efi_graphics_output_protocol##table, attr, instance))
90 
91 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
92 			      unsigned long size, void **handles)
93 {
94 	efi_graphics_output_protocol_t *gop, *first_gop;
95 	u16 width, height;
96 	u32 pixels_per_scan_line;
97 	u32 ext_lfb_base;
98 	efi_physical_addr_t fb_base;
99 	efi_pixel_bitmask_t pixel_info;
100 	int pixel_format;
101 	efi_status_t status;
102 	efi_handle_t h;
103 	int i;
104 
105 	first_gop = NULL;
106 	gop = NULL;
107 
108 	for_each_efi_handle(h, handles, size, i) {
109 		efi_graphics_output_protocol_mode_t *mode;
110 		efi_graphics_output_mode_info_t *info = NULL;
111 		efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
112 		bool conout_found = false;
113 		void *dummy = NULL;
114 		efi_physical_addr_t current_fb_base;
115 
116 		status = efi_call_early(handle_protocol, h,
117 					proto, (void **)&gop);
118 		if (status != EFI_SUCCESS)
119 			continue;
120 
121 		status = efi_call_early(handle_protocol, h,
122 					&conout_proto, &dummy);
123 		if (status == EFI_SUCCESS)
124 			conout_found = true;
125 
126 		mode = (void *)(unsigned long)efi_gop_attr(, mode, gop);
127 		info = (void *)(unsigned long)efi_gop_attr(_mode, info, mode);
128 		current_fb_base = efi_gop_attr(_mode, frame_buffer_base, mode);
129 
130 		if ((!first_gop || conout_found) &&
131 		    info->pixel_format != PIXEL_BLT_ONLY) {
132 			/*
133 			 * Systems that use the UEFI Console Splitter may
134 			 * provide multiple GOP devices, not all of which are
135 			 * backed by real hardware. The workaround is to search
136 			 * for a GOP implementing the ConOut protocol, and if
137 			 * one isn't found, to just fall back to the first GOP.
138 			 */
139 			width = info->horizontal_resolution;
140 			height = info->vertical_resolution;
141 			pixel_format = info->pixel_format;
142 			pixel_info = info->pixel_information;
143 			pixels_per_scan_line = info->pixels_per_scan_line;
144 			fb_base = current_fb_base;
145 
146 			/*
147 			 * Once we've found a GOP supporting ConOut,
148 			 * don't bother looking any further.
149 			 */
150 			first_gop = gop;
151 			if (conout_found)
152 				break;
153 		}
154 	}
155 
156 	/* Did we find any GOPs? */
157 	if (!first_gop)
158 		return EFI_NOT_FOUND;
159 
160 	/* EFI framebuffer */
161 	si->orig_video_isVGA = VIDEO_TYPE_EFI;
162 
163 	si->lfb_width = width;
164 	si->lfb_height = height;
165 	si->lfb_base = fb_base;
166 
167 	ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
168 	if (ext_lfb_base) {
169 		si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
170 		si->ext_lfb_base = ext_lfb_base;
171 	}
172 
173 	si->pages = 1;
174 
175 	setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
176 
177 	si->lfb_size = si->lfb_linelength * si->lfb_height;
178 
179 	si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
180 
181 	return EFI_SUCCESS;
182 }
183 
184 /*
185  * See if we have Graphics Output Protocol
186  */
187 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
188 			   unsigned long size)
189 {
190 	efi_status_t status;
191 	void **gop_handle = NULL;
192 
193 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
194 				size, (void **)&gop_handle);
195 	if (status != EFI_SUCCESS)
196 		return status;
197 
198 	status = efi_call_early(locate_handle,
199 				EFI_LOCATE_BY_PROTOCOL,
200 				proto, NULL, &size, gop_handle);
201 	if (status != EFI_SUCCESS)
202 		goto free_handle;
203 
204 	status = setup_gop(si, proto, size, gop_handle);
205 
206 free_handle:
207 	efi_call_early(free_pool, gop_handle);
208 	return status;
209 }
210