Linux Audio

Check our new training course

Loading...
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 Intel Corporation; author Matt Fleming
  4 */
  5
  6#include <linux/console.h>
  7#include <linux/efi.h>
  8#include <linux/font.h>
  9#include <linux/io.h>
 10#include <linux/kernel.h>
 11#include <linux/serial_core.h>
 12#include <linux/screen_info.h>
 13#include <linux/string.h>
 14
 15#include <asm/early_ioremap.h>
 16
 17static const struct console *earlycon_console __initdata;
 18static const struct font_desc *font;
 19static u16 cur_line_y, max_line_y;
 20static u32 efi_x_array[1024];
 21static u32 efi_x, efi_y;
 22static u64 fb_base;
 23static bool fb_wb;
 24static void *efi_fb;
 25
 26/*
 27 * EFI earlycon needs to use early_memremap() to map the framebuffer.
 28 * But early_memremap() is not usable for 'earlycon=efifb keep_bootcon',
 29 * memremap() should be used instead. memremap() will be available after
 30 * paging_init() which is earlier than initcall callbacks. Thus adding this
 31 * early initcall function early_efi_map_fb() to map the whole EFI framebuffer.
 32 */
 33static int __init efi_earlycon_remap_fb(void)
 34{
 35	/* bail if there is no bootconsole or it was unregistered already */
 36	if (!earlycon_console || !console_is_registered(earlycon_console))
 37		return 0;
 38
 39	efi_fb = memremap(fb_base, screen_info.lfb_size,
 40			  fb_wb ? MEMREMAP_WB : MEMREMAP_WC);
 41
 42	return efi_fb ? 0 : -ENOMEM;
 43}
 44early_initcall(efi_earlycon_remap_fb);
 45
 46static int __init efi_earlycon_unmap_fb(void)
 47{
 48	/* unmap the bootconsole fb unless keep_bootcon left it registered */
 49	if (efi_fb && !console_is_registered(earlycon_console))
 50		memunmap(efi_fb);
 51	return 0;
 52}
 53late_initcall(efi_earlycon_unmap_fb);
 54
 55static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
 56{
 57	pgprot_t fb_prot;
 58
 59	if (efi_fb)
 60		return efi_fb + start;
 61
 62	fb_prot = fb_wb ? PAGE_KERNEL : pgprot_writecombine(PAGE_KERNEL);
 63	return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
 64}
 65
 66static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
 67{
 68	if (efi_fb)
 69		return;
 70
 71	early_memunmap(addr, len);
 72}
 73
 74static void efi_earlycon_clear_scanline(unsigned int y)
 75{
 76	unsigned long *dst;
 77	u16 len;
 78
 79	len = screen_info.lfb_linelength;
 80	dst = efi_earlycon_map(y*len, len);
 81	if (!dst)
 82		return;
 83
 84	memset(dst, 0, len);
 85	efi_earlycon_unmap(dst, len);
 86}
 87
 88static void efi_earlycon_scroll_up(void)
 89{
 90	unsigned long *dst, *src;
 91	u16 maxlen = 0;
 92	u16 len;
 93	u32 i, height;
 94
 95	/* Find the cached maximum x coordinate */
 96	for (i = 0; i < max_line_y; i++) {
 97		if (efi_x_array[i] > maxlen)
 98			maxlen = efi_x_array[i];
 99	}
100	maxlen *= 4;
101
102	len = screen_info.lfb_linelength;
103	height = screen_info.lfb_height;
104
105	for (i = 0; i < height - font->height; i++) {
106		dst = efi_earlycon_map(i*len, len);
107		if (!dst)
108			return;
109
110		src = efi_earlycon_map((i + font->height) * len, len);
111		if (!src) {
112			efi_earlycon_unmap(dst, len);
113			return;
114		}
115
116		memmove(dst, src, maxlen);
117
118		efi_earlycon_unmap(src, len);
119		efi_earlycon_unmap(dst, len);
120	}
121}
122
123static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h)
124{
125	const u32 color_black = 0x00000000;
126	const u32 color_white = 0x00ffffff;
127	const u8 *src;
128	int m, n, bytes;
129	u8 x;
130
131	bytes = BITS_TO_BYTES(font->width);
132	src = font->data + c * font->height * bytes + h * bytes;
133
134	for (m = 0; m < font->width; m++) {
135		n = m % 8;
136		x = *(src + m / 8);
137		if ((x >> (7 - n)) & 1)
138			*dst = color_white;
139		else
140			*dst = color_black;
141		dst++;
142	}
143}
144
145static void
146efi_earlycon_write(struct console *con, const char *str, unsigned int num)
147{
148	struct screen_info *si;
149	u32 cur_efi_x = efi_x;
150	unsigned int len;
151	const char *s;
152	void *dst;
153
154	si = &screen_info;
155	len = si->lfb_linelength;
156
157	while (num) {
158		unsigned int linemax = (si->lfb_width - efi_x) / font->width;
159		unsigned int h, count;
160
161		count = strnchrnul(str, num, '\n') - str;
162		if (count > linemax)
163			count = linemax;
164
165		for (h = 0; h < font->height; h++) {
166			unsigned int n, x;
167
168			dst = efi_earlycon_map((efi_y + h) * len, len);
169			if (!dst)
170				return;
171
172			s = str;
173			n = count;
174			x = efi_x;
175
176			while (n-- > 0) {
177				efi_earlycon_write_char(dst + x*4, *s, h);
178				x += font->width;
179				s++;
180			}
181
182			efi_earlycon_unmap(dst, len);
183		}
184
185		num -= count;
186		efi_x += count * font->width;
187		str += count;
188
189		if (num > 0 && *s == '\n') {
190			cur_efi_x = efi_x;
191			efi_x = 0;
192			efi_y += font->height;
193			str++;
194			num--;
195		}
196
197		if (efi_x + font->width > si->lfb_width) {
198			cur_efi_x = efi_x;
199			efi_x = 0;
200			efi_y += font->height;
201		}
202
203		if (efi_y + font->height > si->lfb_height) {
204			u32 i;
205
206			efi_x_array[cur_line_y] = cur_efi_x;
207			cur_line_y = (cur_line_y + 1) % max_line_y;
208
209			efi_y -= font->height;
210			efi_earlycon_scroll_up();
211
212			for (i = 0; i < font->height; i++)
213				efi_earlycon_clear_scanline(efi_y + i);
214		}
215	}
216}
217
218static bool __initdata fb_probed;
219
220void __init efi_earlycon_reprobe(void)
221{
222	if (fb_probed)
223		setup_earlycon("efifb");
224}
225
226static int __init efi_earlycon_setup(struct earlycon_device *device,
227				     const char *opt)
228{
229	struct screen_info *si;
230	u16 xres, yres;
231	u32 i;
232
233	fb_wb = opt && !strcmp(opt, "ram");
234
235	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) {
236		fb_probed = true;
237		return -ENODEV;
238	}
239
240	fb_base = screen_info.lfb_base;
241	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
242		fb_base |= (u64)screen_info.ext_lfb_base << 32;
243
244	si = &screen_info;
245	xres = si->lfb_width;
246	yres = si->lfb_height;
247
248	/*
249	 * efi_earlycon_write_char() implicitly assumes a framebuffer with
250	 * 32 bits per pixel.
251	 */
252	if (si->lfb_depth != 32)
253		return -ENODEV;
254
255	font = get_default_font(xres, yres, NULL, NULL);
256	if (!font)
257		return -ENODEV;
258
259	/* Fill the cache with maximum possible value of x coordinate */
260	memset32(efi_x_array, rounddown(xres, font->width), ARRAY_SIZE(efi_x_array));
261	efi_y = rounddown(yres, font->height);
262
263	/* Make sure we have cache for the x coordinate for the full screen */
264	max_line_y = efi_y / font->height + 1;
265	cur_line_y = 0;
266
267	efi_y -= font->height;
268	for (i = 0; i < (yres - efi_y) / font->height; i++)
269		efi_earlycon_scroll_up();
270
271	device->con->write = efi_earlycon_write;
272	earlycon_console = device->con;
273	return 0;
274}
275EARLYCON_DECLARE(efifb, efi_earlycon_setup);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 Intel Corporation; author Matt Fleming
  4 */
  5
  6#include <linux/console.h>
  7#include <linux/efi.h>
  8#include <linux/font.h>
  9#include <linux/io.h>
 10#include <linux/kernel.h>
 11#include <linux/serial_core.h>
 12#include <linux/screen_info.h>
 13#include <linux/string.h>
 14
 15#include <asm/early_ioremap.h>
 16
 17static const struct console *earlycon_console __initdata;
 18static const struct font_desc *font;
 19static u16 cur_line_y, max_line_y;
 20static u32 efi_x_array[1024];
 21static u32 efi_x, efi_y;
 22static u64 fb_base;
 23static bool fb_wb;
 24static void *efi_fb;
 25
 26/*
 27 * EFI earlycon needs to use early_memremap() to map the framebuffer.
 28 * But early_memremap() is not usable for 'earlycon=efifb keep_bootcon',
 29 * memremap() should be used instead. memremap() will be available after
 30 * paging_init() which is earlier than initcall callbacks. Thus adding this
 31 * early initcall function early_efi_map_fb() to map the whole EFI framebuffer.
 32 */
 33static int __init efi_earlycon_remap_fb(void)
 34{
 35	/* bail if there is no bootconsole or it was unregistered already */
 36	if (!earlycon_console || !console_is_registered(earlycon_console))
 37		return 0;
 38
 39	efi_fb = memremap(fb_base, screen_info.lfb_size,
 40			  fb_wb ? MEMREMAP_WB : MEMREMAP_WC);
 41
 42	return efi_fb ? 0 : -ENOMEM;
 43}
 44early_initcall(efi_earlycon_remap_fb);
 45
 46static int __init efi_earlycon_unmap_fb(void)
 47{
 48	/* unmap the bootconsole fb unless keep_bootcon left it registered */
 49	if (efi_fb && !console_is_registered(earlycon_console))
 50		memunmap(efi_fb);
 51	return 0;
 52}
 53late_initcall(efi_earlycon_unmap_fb);
 54
 55static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
 56{
 57	pgprot_t fb_prot;
 58
 59	if (efi_fb)
 60		return efi_fb + start;
 61
 62	fb_prot = fb_wb ? PAGE_KERNEL : pgprot_writecombine(PAGE_KERNEL);
 63	return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
 64}
 65
 66static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
 67{
 68	if (efi_fb)
 69		return;
 70
 71	early_memunmap(addr, len);
 72}
 73
 74static void efi_earlycon_clear_scanline(unsigned int y)
 75{
 76	unsigned long *dst;
 77	u16 len;
 78
 79	len = screen_info.lfb_linelength;
 80	dst = efi_earlycon_map(y*len, len);
 81	if (!dst)
 82		return;
 83
 84	memset(dst, 0, len);
 85	efi_earlycon_unmap(dst, len);
 86}
 87
 88static void efi_earlycon_scroll_up(void)
 89{
 90	unsigned long *dst, *src;
 91	u16 maxlen = 0;
 92	u16 len;
 93	u32 i, height;
 94
 95	/* Find the cached maximum x coordinate */
 96	for (i = 0; i < max_line_y; i++) {
 97		if (efi_x_array[i] > maxlen)
 98			maxlen = efi_x_array[i];
 99	}
100	maxlen *= 4;
101
102	len = screen_info.lfb_linelength;
103	height = screen_info.lfb_height;
104
105	for (i = 0; i < height - font->height; i++) {
106		dst = efi_earlycon_map(i*len, len);
107		if (!dst)
108			return;
109
110		src = efi_earlycon_map((i + font->height) * len, len);
111		if (!src) {
112			efi_earlycon_unmap(dst, len);
113			return;
114		}
115
116		memmove(dst, src, maxlen);
117
118		efi_earlycon_unmap(src, len);
119		efi_earlycon_unmap(dst, len);
120	}
121}
122
123static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h)
124{
125	const u32 color_black = 0x00000000;
126	const u32 color_white = 0x00ffffff;
127	const u8 *src;
128	int m, n, bytes;
129	u8 x;
130
131	bytes = BITS_TO_BYTES(font->width);
132	src = font->data + c * font->height * bytes + h * bytes;
133
134	for (m = 0; m < font->width; m++) {
135		n = m % 8;
136		x = *(src + m / 8);
137		if ((x >> (7 - n)) & 1)
138			*dst = color_white;
139		else
140			*dst = color_black;
141		dst++;
142	}
143}
144
145static void
146efi_earlycon_write(struct console *con, const char *str, unsigned int num)
147{
148	struct screen_info *si;
149	u32 cur_efi_x = efi_x;
150	unsigned int len;
151	const char *s;
152	void *dst;
153
154	si = &screen_info;
155	len = si->lfb_linelength;
156
157	while (num) {
158		unsigned int linemax = (si->lfb_width - efi_x) / font->width;
159		unsigned int h, count;
160
161		count = strnchrnul(str, num, '\n') - str;
162		if (count > linemax)
163			count = linemax;
164
165		for (h = 0; h < font->height; h++) {
166			unsigned int n, x;
167
168			dst = efi_earlycon_map((efi_y + h) * len, len);
169			if (!dst)
170				return;
171
172			s = str;
173			n = count;
174			x = efi_x;
175
176			while (n-- > 0) {
177				efi_earlycon_write_char(dst + x*4, *s, h);
178				x += font->width;
179				s++;
180			}
181
182			efi_earlycon_unmap(dst, len);
183		}
184
185		num -= count;
186		efi_x += count * font->width;
187		str += count;
188
189		if (num > 0 && *s == '\n') {
190			cur_efi_x = efi_x;
191			efi_x = 0;
192			efi_y += font->height;
193			str++;
194			num--;
195		}
196
197		if (efi_x + font->width > si->lfb_width) {
198			cur_efi_x = efi_x;
199			efi_x = 0;
200			efi_y += font->height;
201		}
202
203		if (efi_y + font->height > si->lfb_height) {
204			u32 i;
205
206			efi_x_array[cur_line_y] = cur_efi_x;
207			cur_line_y = (cur_line_y + 1) % max_line_y;
208
209			efi_y -= font->height;
210			efi_earlycon_scroll_up();
211
212			for (i = 0; i < font->height; i++)
213				efi_earlycon_clear_scanline(efi_y + i);
214		}
215	}
216}
217
218static bool __initdata fb_probed;
219
220void __init efi_earlycon_reprobe(void)
221{
222	if (fb_probed)
223		setup_earlycon("efifb");
224}
225
226static int __init efi_earlycon_setup(struct earlycon_device *device,
227				     const char *opt)
228{
229	struct screen_info *si;
230	u16 xres, yres;
231	u32 i;
232
233	fb_wb = opt && !strcmp(opt, "ram");
234
235	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI) {
236		fb_probed = true;
237		return -ENODEV;
238	}
239
240	fb_base = screen_info.lfb_base;
241	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
242		fb_base |= (u64)screen_info.ext_lfb_base << 32;
243
244	si = &screen_info;
245	xres = si->lfb_width;
246	yres = si->lfb_height;
247
248	/*
249	 * efi_earlycon_write_char() implicitly assumes a framebuffer with
250	 * 32 bits per pixel.
251	 */
252	if (si->lfb_depth != 32)
253		return -ENODEV;
254
255	font = get_default_font(xres, yres, NULL, NULL);
256	if (!font)
257		return -ENODEV;
258
259	/* Fill the cache with maximum possible value of x coordinate */
260	memset32(efi_x_array, rounddown(xres, font->width), ARRAY_SIZE(efi_x_array));
261	efi_y = rounddown(yres, font->height);
262
263	/* Make sure we have cache for the x coordinate for the full screen */
264	max_line_y = efi_y / font->height + 1;
265	cur_line_y = 0;
266
267	efi_y -= font->height;
268	for (i = 0; i < (yres - efi_y) / font->height; i++)
269		efi_earlycon_scroll_up();
270
271	device->con->write = efi_earlycon_write;
272	earlycon_console = device->con;
273	return 0;
274}
275EARLYCON_DECLARE(efifb, efi_earlycon_setup);