gop.c 4.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11 12
/* -----------------------------------------------------------------------
 *
 *   Copyright 2011 Intel Corporation; author Matt Fleming
 *
 * ----------------------------------------------------------------------- */

#include <linux/efi.h>
#include <linux/screen_info.h>
#include <asm/efi.h>
#include <asm/setup.h>

13 14
#include "efistub.h"

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
static void find_bits(unsigned long mask, u8 *pos, u8 *size)
{
	u8 first, len;

	first = 0;
	len = 0;

	if (mask) {
		while (!(mask & 0x1)) {
			mask = mask >> 1;
			first++;
		}

		while (mask & 0x1) {
			mask = mask >> 1;
			len++;
		}
	}

	*pos = first;
	*size = len;
}

static void
setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
40
		 efi_pixel_bitmask_t pixel_info, int pixel_format)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
		si->lfb_depth = 32;
		si->lfb_linelength = pixels_per_scan_line * 4;
		si->red_size = 8;
		si->red_pos = 0;
		si->green_size = 8;
		si->green_pos = 8;
		si->blue_size = 8;
		si->blue_pos = 16;
		si->rsvd_size = 8;
		si->rsvd_pos = 24;
	} else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
		si->lfb_depth = 32;
		si->lfb_linelength = pixels_per_scan_line * 4;
		si->red_size = 8;
		si->red_pos = 16;
		si->green_size = 8;
		si->green_pos = 8;
		si->blue_size = 8;
		si->blue_pos = 0;
		si->rsvd_size = 8;
		si->rsvd_pos = 24;
	} else if (pixel_format == PIXEL_BIT_MASK) {
		find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
		find_bits(pixel_info.green_mask, &si->green_pos,
			  &si->green_size);
		find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
		find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
			  &si->rsvd_size);
		si->lfb_depth = si->red_size + si->green_size +
			si->blue_size + si->rsvd_size;
		si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
	} else {
		si->lfb_depth = 4;
		si->lfb_linelength = si->lfb_width / 2;
		si->red_size = 0;
		si->red_pos = 0;
		si->green_size = 0;
		si->green_pos = 0;
		si->blue_size = 0;
		si->blue_pos = 0;
		si->rsvd_size = 0;
		si->rsvd_pos = 0;
	}
}

88 89
static efi_graphics_output_protocol_t *
find_gop(efi_guid_t *proto, unsigned long size, void **handles)
90
{
A
Arvind Sankar 已提交
91
	efi_graphics_output_protocol_t *gop, *first_gop;
92 93
	efi_graphics_output_protocol_mode_t *mode;
	efi_graphics_output_mode_info_t *info = NULL;
94
	efi_status_t status;
95
	efi_handle_t h;
96 97 98 99
	int i;

	first_gop = NULL;

100
	for_each_efi_handle(h, handles, size, i) {
101 102 103
		efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
		void *dummy = NULL;

104
		status = efi_bs_call(handle_protocol, h, proto, (void **)&gop);
105 106 107
		if (status != EFI_SUCCESS)
			continue;

108 109 110 111 112
		mode = efi_table_attr(gop, mode);
		info = efi_table_attr(mode, info);
		if (info->pixel_format == PIXEL_BLT_ONLY)
			continue;

113 114 115 116 117 118 119 120 121 122
		/*
		 * Systems that use the UEFI Console Splitter may
		 * provide multiple GOP devices, not all of which are
		 * backed by real hardware. The workaround is to search
		 * for a GOP implementing the ConOut protocol, and if
		 * one isn't found, to just fall back to the first GOP.
		 *
		 * Once we've found a GOP supporting ConOut,
		 * don't bother looking any further.
		 */
123
		status = efi_bs_call(handle_protocol, h, &conout_proto, &dummy);
124
		if (status == EFI_SUCCESS)
125 126 127
			return gop;

		if (!first_gop)
A
Arvind Sankar 已提交
128
			first_gop = gop;
129 130
	}

131 132 133 134 135 136 137 138 139 140 141 142 143
	return first_gop;
}

static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
			      unsigned long size, void **handles)
{
	efi_graphics_output_protocol_t *gop;
	efi_graphics_output_protocol_mode_t *mode;
	efi_graphics_output_mode_info_t *info = NULL;
	efi_physical_addr_t fb_base;

	gop = find_gop(proto, size, handles);

144
	/* Did we find any GOPs? */
145
	if (!gop)
146
		return EFI_NOT_FOUND;
147 148

	/* EFI framebuffer */
149
	mode = efi_table_attr(gop, mode);
150 151
	info = efi_table_attr(mode, info);

152 153
	si->orig_video_isVGA = VIDEO_TYPE_EFI;

154 155
	si->lfb_width  = info->horizontal_resolution;
	si->lfb_height = info->vertical_resolution;
156

157 158 159 160
	fb_base		 = efi_table_attr(mode, frame_buffer_base);
	si->lfb_base	 = fb_base;
	si->ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
	if (si->ext_lfb_base)
161 162 163 164
		si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;

	si->pages = 1;

165 166
	setup_pixel_info(si, info->pixels_per_scan_line,
			     info->pixel_information, info->pixel_format);
167 168 169 170

	si->lfb_size = si->lfb_linelength * si->lfb_height;

	si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
171

172
	return EFI_SUCCESS;
173 174 175 176 177
}

/*
 * See if we have Graphics Output Protocol
 */
178
efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
179 180 181 182 183
			   unsigned long size)
{
	efi_status_t status;
	void **gop_handle = NULL;

184 185
	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
			     (void **)&gop_handle);
186 187 188
	if (status != EFI_SUCCESS)
		return status;

189 190
	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, proto, NULL,
			     &size, gop_handle);
191 192 193
	if (status != EFI_SUCCESS)
		goto free_handle;

194
	status = setup_gop(si, proto, size, gop_handle);
195 196

free_handle:
197
	efi_bs_call(free_pool, gop_handle);
198 199
	return status;
}