1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/console.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/string.h>
6 #include <linux/screen_info.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/pci_regs.h>
9 #include <linux/pci_ids.h>
10 #include <linux/errno.h>
11 #include <linux/pgtable.h>
12 #include <asm/io.h>
13 #include <asm/processor.h>
14 #include <asm/fcntl.h>
15 #include <asm/setup.h>
16 #include <xen/hvc-console.h>
17 #include <asm/pci-direct.h>
18 #include <asm/fixmap.h>
19 #include <linux/usb/ehci_def.h>
20 #include <linux/usb/xhci-dbgp.h>
21 #include <asm/pci_x86.h>
22 #include <linux/static_call.h>
23
24 /* Simple VGA output */
25 #define VGABASE (__ISA_IO_base + 0xb8000)
26
27 static int max_ypos = 25, max_xpos = 80;
28 static int current_ypos = 25, current_xpos;
29
early_vga_write(struct console * con,const char * str,unsigned n)30 static void early_vga_write(struct console *con, const char *str, unsigned n)
31 {
32 char c;
33 int i, k, j;
34
35 while ((c = *str++) != '\0' && n-- > 0) {
36 if (current_ypos >= max_ypos) {
37 /* scroll 1 line up */
38 for (k = 1, j = 0; k < max_ypos; k++, j++) {
39 for (i = 0; i < max_xpos; i++) {
40 writew(readw(VGABASE+2*(max_xpos*k+i)),
41 VGABASE + 2*(max_xpos*j + i));
42 }
43 }
44 for (i = 0; i < max_xpos; i++)
45 writew(0x720, VGABASE + 2*(max_xpos*j + i));
46 current_ypos = max_ypos-1;
47 }
48 #ifdef CONFIG_KGDB_KDB
49 if (c == '\b') {
50 if (current_xpos > 0)
51 current_xpos--;
52 } else if (c == '\r') {
53 current_xpos = 0;
54 } else
55 #endif
56 if (c == '\n') {
57 current_xpos = 0;
58 current_ypos++;
59 } else if (c != '\r') {
60 writew(((0x7 << 8) | (unsigned short) c),
61 VGABASE + 2*(max_xpos*current_ypos +
62 current_xpos++));
63 if (current_xpos >= max_xpos) {
64 current_xpos = 0;
65 current_ypos++;
66 }
67 }
68 }
69 }
70
71 static struct console early_vga_console = {
72 .name = "earlyvga",
73 .write = early_vga_write,
74 .flags = CON_PRINTBUFFER,
75 .index = -1,
76 };
77
78 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
79
80 static unsigned long early_serial_base = 0x3f8; /* ttyS0 */
81
82 #define XMTRDY 0x20
83
84 #define DLAB 0x80
85
86 #define TXR 0 /* Transmit register (WRITE) */
87 #define RXR 0 /* Receive register (READ) */
88 #define IER 1 /* Interrupt Enable */
89 #define IIR 2 /* Interrupt ID */
90 #define FCR 2 /* FIFO control */
91 #define LCR 3 /* Line control */
92 #define MCR 4 /* Modem control */
93 #define LSR 5 /* Line Status */
94 #define MSR 6 /* Modem Status */
95 #define DLL 0 /* Divisor Latch Low */
96 #define DLH 1 /* Divisor latch High */
97
io_serial_in(unsigned long addr,int offset)98 static __noendbr unsigned int io_serial_in(unsigned long addr, int offset)
99 {
100 return inb(addr + offset);
101 }
102 ANNOTATE_NOENDBR_SYM(io_serial_in);
103
io_serial_out(unsigned long addr,int offset,int value)104 static __noendbr void io_serial_out(unsigned long addr, int offset, int value)
105 {
106 outb(value, addr + offset);
107 }
108 ANNOTATE_NOENDBR_SYM(io_serial_out);
109
110 DEFINE_STATIC_CALL(serial_in, io_serial_in);
111 DEFINE_STATIC_CALL(serial_out, io_serial_out);
112
early_serial_putc(unsigned char ch)113 static int early_serial_putc(unsigned char ch)
114 {
115 unsigned timeout = 0xffff;
116
117 while ((static_call(serial_in)(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
118 cpu_relax();
119 static_call(serial_out)(early_serial_base, TXR, ch);
120 return timeout ? 0 : -1;
121 }
122
early_serial_write(struct console * con,const char * s,unsigned n)123 static void early_serial_write(struct console *con, const char *s, unsigned n)
124 {
125 while (*s && n-- > 0) {
126 if (*s == '\n')
127 early_serial_putc('\r');
128 early_serial_putc(*s);
129 s++;
130 }
131 }
132
early_serial_hw_init(unsigned divisor)133 static __init void early_serial_hw_init(unsigned divisor)
134 {
135 unsigned char c;
136
137 static_call(serial_out)(early_serial_base, LCR, 0x3); /* 8n1 */
138 static_call(serial_out)(early_serial_base, IER, 0); /* no interrupt */
139 static_call(serial_out)(early_serial_base, FCR, 0); /* no fifo */
140 static_call(serial_out)(early_serial_base, MCR, 0x3); /* DTR + RTS */
141
142 c = static_call(serial_in)(early_serial_base, LCR);
143 static_call(serial_out)(early_serial_base, LCR, c | DLAB);
144 static_call(serial_out)(early_serial_base, DLL, divisor & 0xff);
145 static_call(serial_out)(early_serial_base, DLH, (divisor >> 8) & 0xff);
146 static_call(serial_out)(early_serial_base, LCR, c & ~DLAB);
147 }
148
149 #define DEFAULT_BAUD 9600
150
early_serial_init(char * s)151 static __init void early_serial_init(char *s)
152 {
153 unsigned divisor;
154 unsigned long baud = DEFAULT_BAUD;
155 char *e;
156
157 if (*s == ',')
158 ++s;
159
160 if (*s) {
161 unsigned port;
162 if (!strncmp(s, "0x", 2)) {
163 early_serial_base = simple_strtoul(s, &e, 16);
164 } else {
165 static const int __initconst bases[] = { 0x3f8, 0x2f8 };
166
167 if (!strncmp(s, "ttyS", 4))
168 s += 4;
169 port = simple_strtoul(s, &e, 10);
170 if (port > 1 || s == e)
171 port = 0;
172 early_serial_base = bases[port];
173 }
174 s += strcspn(s, ",");
175 if (*s == ',')
176 s++;
177 }
178
179 if (*s) {
180 baud = simple_strtoull(s, &e, 0);
181
182 if (baud == 0 || s == e)
183 baud = DEFAULT_BAUD;
184 }
185
186 /* Convert from baud to divisor value */
187 divisor = 115200 / baud;
188
189 /* Set up the HW */
190 early_serial_hw_init(divisor);
191 }
192
mem32_serial_out(unsigned long addr,int offset,int value)193 static __noendbr void mem32_serial_out(unsigned long addr, int offset, int value)
194 {
195 u32 __iomem *vaddr = (u32 __iomem *)addr;
196 /* shift implied by pointer type */
197 writel(value, vaddr + offset);
198 }
199 ANNOTATE_NOENDBR_SYM(mem32_serial_out);
200
mem32_serial_in(unsigned long addr,int offset)201 static __noendbr unsigned int mem32_serial_in(unsigned long addr, int offset)
202 {
203 u32 __iomem *vaddr = (u32 __iomem *)addr;
204 /* shift implied by pointer type */
205 return readl(vaddr + offset);
206 }
207 ANNOTATE_NOENDBR_SYM(mem32_serial_in);
208
209 /*
210 * early_mmio_serial_init() - Initialize MMIO-based early serial console.
211 * @s: MMIO-based serial specification.
212 */
early_mmio_serial_init(char * s)213 static __init void early_mmio_serial_init(char *s)
214 {
215 unsigned long baudrate;
216 unsigned long membase;
217 char *e;
218
219 if (*s == ',')
220 s++;
221
222 if (!strncmp(s, "0x", 2)) {
223 /* NB: only 32-bit addresses are supported. */
224 membase = simple_strtoul(s, &e, 16);
225 early_serial_base = (unsigned long)early_ioremap(membase, PAGE_SIZE);
226
227 static_call_update(serial_in, mem32_serial_in);
228 static_call_update(serial_out, mem32_serial_out);
229
230 s += strcspn(s, ",");
231 if (*s == ',')
232 s++;
233 }
234
235 if (!strncmp(s, "nocfg", 5)) {
236 baudrate = 0;
237 } else {
238 baudrate = simple_strtoul(s, &e, 0);
239 if (baudrate == 0 || s == e)
240 baudrate = DEFAULT_BAUD;
241 }
242
243 if (baudrate)
244 early_serial_hw_init(115200 / baudrate);
245 }
246
247 #ifdef CONFIG_PCI
248 /*
249 * early_pci_serial_init()
250 *
251 * This function is invoked when the early_printk param starts with "pciserial"
252 * The rest of the param should be "[force],B:D.F,baud", where B, D & F describe
253 * the location of a PCI device that must be a UART device. "force" is optional
254 * and overrides the use of an UART device with a wrong PCI class code.
255 */
early_pci_serial_init(char * s)256 static __init void early_pci_serial_init(char *s)
257 {
258 unsigned divisor;
259 unsigned long baud = DEFAULT_BAUD;
260 u8 bus, slot, func;
261 u32 classcode, bar0;
262 u16 cmdreg;
263 char *e;
264 int force = 0;
265
266 if (*s == ',')
267 ++s;
268
269 if (*s == 0)
270 return;
271
272 /* Force the use of an UART device with wrong class code */
273 if (!strncmp(s, "force,", 6)) {
274 force = 1;
275 s += 6;
276 }
277
278 /*
279 * Part the param to get the BDF values
280 */
281 bus = (u8)simple_strtoul(s, &e, 16);
282 s = e;
283 if (*s != ':')
284 return;
285 ++s;
286 slot = (u8)simple_strtoul(s, &e, 16);
287 s = e;
288 if (*s != '.')
289 return;
290 ++s;
291 func = (u8)simple_strtoul(s, &e, 16);
292 s = e;
293
294 /* A baud might be following */
295 if (*s == ',')
296 s++;
297
298 /*
299 * Find the device from the BDF
300 */
301 cmdreg = read_pci_config(bus, slot, func, PCI_COMMAND);
302 classcode = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
303 bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
304
305 /*
306 * Verify it is a 16550-UART type device
307 */
308 if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
309 (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
310 (((classcode >> 8) & 0xff) != PCI_SERIAL_16550_COMPATIBLE)) {
311 if (!force)
312 return;
313 }
314
315 /*
316 * Determine if it is IO or memory mapped
317 */
318 if ((bar0 & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
319 /* it is IO mapped */
320 early_serial_base = bar0 & PCI_BASE_ADDRESS_IO_MASK;
321 write_pci_config(bus, slot, func, PCI_COMMAND,
322 cmdreg|PCI_COMMAND_IO);
323 } else {
324 /* It is memory mapped - assume 32-bit alignment */
325 static_call_update(serial_in, mem32_serial_in);
326 static_call_update(serial_out, mem32_serial_out);
327 /* WARNING! assuming the address is always in the first 4G */
328 early_serial_base =
329 (unsigned long)early_ioremap(bar0 & PCI_BASE_ADDRESS_MEM_MASK, 0x10);
330 write_pci_config(bus, slot, func, PCI_COMMAND,
331 cmdreg|PCI_COMMAND_MEMORY);
332 }
333
334 /*
335 * Initialize the hardware
336 */
337 if (*s) {
338 if (strcmp(s, "nocfg") == 0)
339 /* Sometimes, we want to leave the UART alone
340 * and assume the BIOS has set it up correctly.
341 * "nocfg" tells us this is the case, and we
342 * should do no more setup.
343 */
344 return;
345 if (kstrtoul(s, 0, &baud) < 0 || baud == 0)
346 baud = DEFAULT_BAUD;
347 }
348
349 /* Convert from baud to divisor value */
350 divisor = 115200 / baud;
351
352 /* Set up the HW */
353 early_serial_hw_init(divisor);
354 }
355 #endif
356
357 static struct console early_serial_console = {
358 .name = "earlyser",
359 .write = early_serial_write,
360 .flags = CON_PRINTBUFFER,
361 .index = -1,
362 };
363
early_console_register(struct console * con,int keep_early)364 static void early_console_register(struct console *con, int keep_early)
365 {
366 if (con->index != -1) {
367 printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
368 con->name);
369 return;
370 }
371 early_console = con;
372 if (keep_early)
373 early_console->flags &= ~CON_BOOT;
374 else
375 early_console->flags |= CON_BOOT;
376 register_console(early_console);
377 }
378
setup_early_printk(char * buf)379 static int __init setup_early_printk(char *buf)
380 {
381 int keep;
382
383 if (!buf)
384 return 0;
385
386 if (early_console)
387 return 0;
388
389 keep = (strstr(buf, "keep") != NULL);
390
391 while (*buf != '\0') {
392 if (!strncmp(buf, "mmio32", 6)) {
393 buf += 6;
394 early_mmio_serial_init(buf);
395 early_console_register(&early_serial_console, keep);
396 }
397 if (!strncmp(buf, "serial", 6)) {
398 buf += 6;
399 early_serial_init(buf);
400 early_console_register(&early_serial_console, keep);
401 if (!strncmp(buf, ",ttyS", 5))
402 buf += 5;
403 }
404 if (!strncmp(buf, "ttyS", 4)) {
405 early_serial_init(buf + 4);
406 early_console_register(&early_serial_console, keep);
407 }
408 #ifdef CONFIG_PCI
409 if (!strncmp(buf, "pciserial", 9)) {
410 buf += 9; /* Keep from match the above "pciserial" */
411 early_pci_serial_init(buf);
412 early_console_register(&early_serial_console, keep);
413 }
414 #endif
415 if (!strncmp(buf, "vga", 3) &&
416 boot_params.screen_info.orig_video_isVGA == 1) {
417 max_xpos = boot_params.screen_info.orig_video_cols;
418 max_ypos = boot_params.screen_info.orig_video_lines;
419 current_ypos = boot_params.screen_info.orig_y;
420 early_console_register(&early_vga_console, keep);
421 }
422 #ifdef CONFIG_EARLY_PRINTK_DBGP
423 if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
424 early_console_register(&early_dbgp_console, keep);
425 #endif
426 #ifdef CONFIG_HVC_XEN
427 if (!strncmp(buf, "xen", 3))
428 early_console_register(&xenboot_console, keep);
429 #endif
430 #ifdef CONFIG_EARLY_PRINTK_USB_XDBC
431 if (!strncmp(buf, "xdbc", 4))
432 early_xdbc_parse_parameter(buf + 4, keep);
433 #endif
434
435 buf++;
436 }
437 return 0;
438 }
439
440 early_param("earlyprintk", setup_early_printk);
441