xref: /OK3568_Linux_fs/kernel/drivers/firmware/google/memconsole-x86-legacy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * memconsole-x86-legacy.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * EBDA specific parts of the memory based BIOS console.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright 2017 Google Inc.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/dmi.h>
13*4882a593Smuzhiyun #include <linux/mm.h>
14*4882a593Smuzhiyun #include <asm/bios_ebda.h>
15*4882a593Smuzhiyun #include <linux/acpi.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "memconsole.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define BIOS_MEMCONSOLE_V1_MAGIC	0xDEADBABE
20*4882a593Smuzhiyun #define BIOS_MEMCONSOLE_V2_MAGIC	(('M')|('C'<<8)|('O'<<16)|('N'<<24))
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct biosmemcon_ebda {
23*4882a593Smuzhiyun 	u32 signature;
24*4882a593Smuzhiyun 	union {
25*4882a593Smuzhiyun 		struct {
26*4882a593Smuzhiyun 			u8  enabled;
27*4882a593Smuzhiyun 			u32 buffer_addr;
28*4882a593Smuzhiyun 			u16 start;
29*4882a593Smuzhiyun 			u16 end;
30*4882a593Smuzhiyun 			u16 num_chars;
31*4882a593Smuzhiyun 			u8  wrapped;
32*4882a593Smuzhiyun 		} __packed v1;
33*4882a593Smuzhiyun 		struct {
34*4882a593Smuzhiyun 			u32 buffer_addr;
35*4882a593Smuzhiyun 			/* Misdocumented as number of pages! */
36*4882a593Smuzhiyun 			u16 num_bytes;
37*4882a593Smuzhiyun 			u16 start;
38*4882a593Smuzhiyun 			u16 end;
39*4882a593Smuzhiyun 		} __packed v2;
40*4882a593Smuzhiyun 	};
41*4882a593Smuzhiyun } __packed;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static char *memconsole_baseaddr;
44*4882a593Smuzhiyun static size_t memconsole_length;
45*4882a593Smuzhiyun 
memconsole_read(char * buf,loff_t pos,size_t count)46*4882a593Smuzhiyun static ssize_t memconsole_read(char *buf, loff_t pos, size_t count)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr,
49*4882a593Smuzhiyun 				       memconsole_length);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
found_v1_header(struct biosmemcon_ebda * hdr)52*4882a593Smuzhiyun static void found_v1_header(struct biosmemcon_ebda *hdr)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	pr_info("memconsole: BIOS console v1 EBDA structure found at %p\n",
55*4882a593Smuzhiyun 		hdr);
56*4882a593Smuzhiyun 	pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num = %d\n",
57*4882a593Smuzhiyun 		hdr->v1.buffer_addr, hdr->v1.start,
58*4882a593Smuzhiyun 		hdr->v1.end, hdr->v1.num_chars);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr);
61*4882a593Smuzhiyun 	memconsole_length = hdr->v1.num_chars;
62*4882a593Smuzhiyun 	memconsole_setup(memconsole_read);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
found_v2_header(struct biosmemcon_ebda * hdr)65*4882a593Smuzhiyun static void found_v2_header(struct biosmemcon_ebda *hdr)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	pr_info("memconsole: BIOS console v2 EBDA structure found at %p\n",
68*4882a593Smuzhiyun 		hdr);
69*4882a593Smuzhiyun 	pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num_bytes = %d\n",
70*4882a593Smuzhiyun 		hdr->v2.buffer_addr, hdr->v2.start,
71*4882a593Smuzhiyun 		hdr->v2.end, hdr->v2.num_bytes);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr + hdr->v2.start);
74*4882a593Smuzhiyun 	memconsole_length = hdr->v2.end - hdr->v2.start;
75*4882a593Smuzhiyun 	memconsole_setup(memconsole_read);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * Search through the EBDA for the BIOS Memory Console, and
80*4882a593Smuzhiyun  * set the global variables to point to it.  Return true if found.
81*4882a593Smuzhiyun  */
memconsole_ebda_init(void)82*4882a593Smuzhiyun static bool memconsole_ebda_init(void)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	unsigned int address;
85*4882a593Smuzhiyun 	size_t length, cur;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	address = get_bios_ebda();
88*4882a593Smuzhiyun 	if (!address) {
89*4882a593Smuzhiyun 		pr_info("memconsole: BIOS EBDA non-existent.\n");
90*4882a593Smuzhiyun 		return false;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* EBDA length is byte 0 of EBDA (in KB) */
94*4882a593Smuzhiyun 	length = *(u8 *)phys_to_virt(address);
95*4882a593Smuzhiyun 	length <<= 10; /* convert to bytes */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/*
98*4882a593Smuzhiyun 	 * Search through EBDA for BIOS memory console structure
99*4882a593Smuzhiyun 	 * note: signature is not necessarily dword-aligned
100*4882a593Smuzhiyun 	 */
101*4882a593Smuzhiyun 	for (cur = 0; cur < length; cur++) {
102*4882a593Smuzhiyun 		struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 		/* memconsole v1 */
105*4882a593Smuzhiyun 		if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
106*4882a593Smuzhiyun 			found_v1_header(hdr);
107*4882a593Smuzhiyun 			return true;
108*4882a593Smuzhiyun 		}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 		/* memconsole v2 */
111*4882a593Smuzhiyun 		if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
112*4882a593Smuzhiyun 			found_v2_header(hdr);
113*4882a593Smuzhiyun 			return true;
114*4882a593Smuzhiyun 		}
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	pr_info("memconsole: BIOS console EBDA structure not found!\n");
118*4882a593Smuzhiyun 	return false;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun static const struct dmi_system_id memconsole_dmi_table[] __initconst = {
122*4882a593Smuzhiyun 	{
123*4882a593Smuzhiyun 		.ident = "Google Board",
124*4882a593Smuzhiyun 		.matches = {
125*4882a593Smuzhiyun 			DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
126*4882a593Smuzhiyun 		},
127*4882a593Smuzhiyun 	},
128*4882a593Smuzhiyun 	{}
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
131*4882a593Smuzhiyun 
memconsole_find(void)132*4882a593Smuzhiyun static bool __init memconsole_find(void)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	if (!dmi_check_system(memconsole_dmi_table))
135*4882a593Smuzhiyun 		return false;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	return memconsole_ebda_init();
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
memconsole_x86_init(void)140*4882a593Smuzhiyun static int __init memconsole_x86_init(void)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	if (!memconsole_find())
143*4882a593Smuzhiyun 		return -ENODEV;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return memconsole_sysfs_init();
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
memconsole_x86_exit(void)148*4882a593Smuzhiyun static void __exit memconsole_x86_exit(void)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	memconsole_exit();
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun module_init(memconsole_x86_init);
154*4882a593Smuzhiyun module_exit(memconsole_x86_exit);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun MODULE_AUTHOR("Google, Inc.");
157*4882a593Smuzhiyun MODULE_LICENSE("GPL");
158