xref: /openbmc/linux/drivers/firmware/efi/libstub/gop.c (revision 8de8788d)
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 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
14 {
15 	u8 first, len;
16 
17 	first = 0;
18 	len = 0;
19 
20 	if (mask) {
21 		while (!(mask & 0x1)) {
22 			mask = mask >> 1;
23 			first++;
24 		}
25 
26 		while (mask & 0x1) {
27 			mask = mask >> 1;
28 			len++;
29 		}
30 	}
31 
32 	*pos = first;
33 	*size = len;
34 }
35 
36 static void
37 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
38 		 efi_pixel_bitmask_t pixel_info, int pixel_format)
39 {
40 	if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
41 		si->lfb_depth = 32;
42 		si->lfb_linelength = pixels_per_scan_line * 4;
43 		si->red_size = 8;
44 		si->red_pos = 0;
45 		si->green_size = 8;
46 		si->green_pos = 8;
47 		si->blue_size = 8;
48 		si->blue_pos = 16;
49 		si->rsvd_size = 8;
50 		si->rsvd_pos = 24;
51 	} else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
52 		si->lfb_depth = 32;
53 		si->lfb_linelength = pixels_per_scan_line * 4;
54 		si->red_size = 8;
55 		si->red_pos = 16;
56 		si->green_size = 8;
57 		si->green_pos = 8;
58 		si->blue_size = 8;
59 		si->blue_pos = 0;
60 		si->rsvd_size = 8;
61 		si->rsvd_pos = 24;
62 	} else if (pixel_format == PIXEL_BIT_MASK) {
63 		find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
64 		find_bits(pixel_info.green_mask, &si->green_pos,
65 			  &si->green_size);
66 		find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
67 		find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
68 			  &si->rsvd_size);
69 		si->lfb_depth = si->red_size + si->green_size +
70 			si->blue_size + si->rsvd_size;
71 		si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
72 	} else {
73 		si->lfb_depth = 4;
74 		si->lfb_linelength = si->lfb_width / 2;
75 		si->red_size = 0;
76 		si->red_pos = 0;
77 		si->green_size = 0;
78 		si->green_pos = 0;
79 		si->blue_size = 0;
80 		si->blue_pos = 0;
81 		si->rsvd_size = 0;
82 		si->rsvd_pos = 0;
83 	}
84 }
85 
86 #define efi_gop_attr(table, attr, instance) \
87 	(efi_table_attr(efi_graphics_output_protocol##table, attr, instance))
88 
89 static efi_status_t
90 setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si,
91 	  efi_guid_t *proto, unsigned long size, void **handles)
92 {
93 	efi_graphics_output_protocol_t *gop, *first_gop;
94 	unsigned long nr_gops;
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 	int i;
103 	bool is64 = efi_is_64bit();
104 
105 	first_gop = NULL;
106 	gop = NULL;
107 
108 	nr_gops = size / (is64 ? sizeof(u64) : sizeof(u32));
109 	for (i = 0; i < nr_gops; i++) {
110 		efi_graphics_output_protocol_mode_t *mode;
111 		efi_graphics_output_mode_info_t *info = NULL;
112 		efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
113 		bool conout_found = false;
114 		void *dummy = NULL;
115 		efi_handle_t h = (efi_handle_t)(unsigned long)
116 				 (is64 ? ((u64 *)handles)[i]
117 				       : ((u32 *)handles)[i]);
118 		efi_physical_addr_t current_fb_base;
119 
120 		status = efi_call_early(handle_protocol, h,
121 					proto, (void **)&gop);
122 		if (status != EFI_SUCCESS)
123 			continue;
124 
125 		status = efi_call_early(handle_protocol, h,
126 					&conout_proto, &dummy);
127 		if (status == EFI_SUCCESS)
128 			conout_found = true;
129 
130 		mode = (void *)(unsigned long)efi_gop_attr(, mode, gop);
131 		info = (void *)(unsigned long)efi_gop_attr(_mode, info, mode);
132 		current_fb_base = efi_gop_attr(_mode, frame_buffer_base, mode);
133 
134 		if ((!first_gop || conout_found) &&
135 		    info->pixel_format != PIXEL_BLT_ONLY) {
136 			/*
137 			 * Systems that use the UEFI Console Splitter may
138 			 * provide multiple GOP devices, not all of which are
139 			 * backed by real hardware. The workaround is to search
140 			 * for a GOP implementing the ConOut protocol, and if
141 			 * one isn't found, to just fall back to the first GOP.
142 			 */
143 			width = info->horizontal_resolution;
144 			height = info->vertical_resolution;
145 			pixel_format = info->pixel_format;
146 			pixel_info = info->pixel_information;
147 			pixels_per_scan_line = info->pixels_per_scan_line;
148 			fb_base = current_fb_base;
149 
150 			/*
151 			 * Once we've found a GOP supporting ConOut,
152 			 * don't bother looking any further.
153 			 */
154 			first_gop = gop;
155 			if (conout_found)
156 				break;
157 		}
158 	}
159 
160 	/* Did we find any GOPs? */
161 	if (!first_gop)
162 		return EFI_NOT_FOUND;
163 
164 	/* EFI framebuffer */
165 	si->orig_video_isVGA = VIDEO_TYPE_EFI;
166 
167 	si->lfb_width = width;
168 	si->lfb_height = height;
169 	si->lfb_base = fb_base;
170 
171 	ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
172 	if (ext_lfb_base) {
173 		si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
174 		si->ext_lfb_base = ext_lfb_base;
175 	}
176 
177 	si->pages = 1;
178 
179 	setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
180 
181 	si->lfb_size = si->lfb_linelength * si->lfb_height;
182 
183 	si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
184 
185 	return EFI_SUCCESS;
186 }
187 
188 /*
189  * See if we have Graphics Output Protocol
190  */
191 efi_status_t efi_setup_gop(efi_system_table_t *sys_table_arg,
192 			   struct screen_info *si, efi_guid_t *proto,
193 			   unsigned long size)
194 {
195 	efi_status_t status;
196 	void **gop_handle = NULL;
197 
198 	status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
199 				size, (void **)&gop_handle);
200 	if (status != EFI_SUCCESS)
201 		return status;
202 
203 	status = efi_call_early(locate_handle,
204 				EFI_LOCATE_BY_PROTOCOL,
205 				proto, NULL, &size, gop_handle);
206 	if (status != EFI_SUCCESS)
207 		goto free_handle;
208 
209 	status = setup_gop(sys_table_arg, si, proto, size, gop_handle);
210 
211 free_handle:
212 	efi_call_early(free_pool, gop_handle);
213 	return status;
214 }
215