fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / x86_64 / kernel / early_printk.c
1 #include <linux/console.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/string.h>
5 #include <linux/screen_info.h>
6 #include <asm/io.h>
7 #include <asm/processor.h>
8 #include <asm/fcntl.h>
9
10 /* Simple VGA output */
11
12 #ifdef __i386__
13 #include <asm/setup.h>
14 #define VGABASE         (__ISA_IO_base + 0xb8000)
15 #else
16 #include <asm/bootsetup.h>
17 #define VGABASE         ((void __iomem *)0xffffffff800b8000UL)
18 #endif
19
20 static int max_ypos = 25, max_xpos = 80;
21 static int current_ypos = 25, current_xpos = 0;
22
23 #ifndef CONFIG_XEN
24 static void early_vga_write(struct console *con, const char *str, unsigned n)
25 {
26         char c;
27         int  i, k, j;
28
29         while ((c = *str++) != '\0' && n-- > 0) {
30                 if (current_ypos >= max_ypos) {
31                         /* scroll 1 line up */
32                         for (k = 1, j = 0; k < max_ypos; k++, j++) {
33                                 for (i = 0; i < max_xpos; i++) {
34                                         writew(readw(VGABASE+2*(max_xpos*k+i)),
35                                                VGABASE + 2*(max_xpos*j + i));
36                                 }
37                         }
38                         for (i = 0; i < max_xpos; i++)
39                                 writew(0x720, VGABASE + 2*(max_xpos*j + i));
40                         current_ypos = max_ypos-1;
41                 }
42                 if (c == '\n') {
43                         current_xpos = 0;
44                         current_ypos++;
45                 } else if (c != '\r')  {
46                         writew(((0x7 << 8) | (unsigned short) c),
47                                VGABASE + 2*(max_xpos*current_ypos +
48                                                 current_xpos++));
49                         if (current_xpos >= max_xpos) {
50                                 current_xpos = 0;
51                                 current_ypos++;
52                         }
53                 }
54         }
55 }
56
57 static struct console early_vga_console = {
58         .name =         "earlyvga",
59         .write =        early_vga_write,
60         .flags =        CON_PRINTBUFFER,
61         .index =        -1,
62 };
63
64 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
65
66 static int early_serial_base = 0x3f8;  /* ttyS0 */
67
68 #define XMTRDY          0x20
69
70 #define DLAB            0x80
71
72 #define TXR             0       /*  Transmit register (WRITE) */
73 #define RXR             0       /*  Receive register  (READ)  */
74 #define IER             1       /*  Interrupt Enable          */
75 #define IIR             2       /*  Interrupt ID              */
76 #define FCR             2       /*  FIFO control              */
77 #define LCR             3       /*  Line control              */
78 #define MCR             4       /*  Modem control             */
79 #define LSR             5       /*  Line Status               */
80 #define MSR             6       /*  Modem Status              */
81 #define DLL             0       /*  Divisor Latch Low         */
82 #define DLH             1       /*  Divisor latch High        */
83
84 static int early_serial_putc(unsigned char ch)
85 {
86         unsigned timeout = 0xffff;
87         while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
88                 cpu_relax();
89         outb(ch, early_serial_base + TXR);
90         return timeout ? 0 : -1;
91 }
92
93 static void early_serial_write(struct console *con, const char *s, unsigned n)
94 {
95         while (*s && n-- > 0) {
96                 early_serial_putc(*s);
97                 if (*s == '\n')
98                         early_serial_putc('\r');
99                 s++;
100         }
101 }
102
103 #define DEFAULT_BAUD 9600
104
105 static __init void early_serial_init(char *s)
106 {
107         unsigned char c;
108         unsigned divisor;
109         unsigned baud = DEFAULT_BAUD;
110         char *e;
111
112         if (*s == ',')
113                 ++s;
114
115         if (*s) {
116                 unsigned port;
117                 if (!strncmp(s,"0x",2)) {
118                         early_serial_base = simple_strtoul(s, &e, 16);
119                 } else {
120                         static int bases[] = { 0x3f8, 0x2f8 };
121
122                         if (!strncmp(s,"ttyS",4))
123                                 s += 4;
124                         port = simple_strtoul(s, &e, 10);
125                         if (port > 1 || s == e)
126                                 port = 0;
127                         early_serial_base = bases[port];
128                 }
129                 s += strcspn(s, ",");
130                 if (*s == ',')
131                         s++;
132         }
133
134         outb(0x3, early_serial_base + LCR);     /* 8n1 */
135         outb(0, early_serial_base + IER);       /* no interrupt */
136         outb(0, early_serial_base + FCR);       /* no fifo */
137         outb(0x3, early_serial_base + MCR);     /* DTR + RTS */
138
139         if (*s) {
140                 baud = simple_strtoul(s, &e, 0);
141                 if (baud == 0 || s == e)
142                         baud = DEFAULT_BAUD;
143         }
144
145         divisor = 115200 / baud;
146         c = inb(early_serial_base + LCR);
147         outb(c | DLAB, early_serial_base + LCR);
148         outb(divisor & 0xff, early_serial_base + DLL);
149         outb((divisor >> 8) & 0xff, early_serial_base + DLH);
150         outb(c & ~DLAB, early_serial_base + LCR);
151 }
152
153 #else /* CONFIG_XEN */
154
155 #undef SCREEN_INFO
156 #define SCREEN_INFO screen_info
157 extern struct screen_info screen_info;
158
159 static void
160 early_serial_write(struct console *con, const char *s, unsigned count)
161 {
162         int n;
163
164         while (count > 0) {
165                 n = HYPERVISOR_console_io(CONSOLEIO_write, count, (char *)s);
166                 if (n <= 0)
167                         break;
168                 count -= n;
169                 s += n;
170         }
171
172
173 static __init void early_serial_init(char *s)
174 {
175         current_xpos = 0;
176 }
177
178 /*
179  * No early VGA console on Xen, as we do not have convenient ISA-space
180  * mappings. Someone should fix this for domain 0. For now, use fake serial.
181  */
182 #define early_vga_console early_serial_console
183
184 #endif
185
186 static struct console early_serial_console = {
187         .name =         "earlyser",
188         .write =        early_serial_write,
189         .flags =        CON_PRINTBUFFER,
190         .index =        -1,
191 };
192
193 /* Console interface to a host file on AMD's SimNow! */
194
195 static int simnow_fd;
196
197 enum {
198         MAGIC1 = 0xBACCD00A,
199         MAGIC2 = 0xCA110000,
200         XOPEN = 5,
201         XWRITE = 4,
202 };
203
204 static noinline long simnow(long cmd, long a, long b, long c)
205 {
206         long ret;
207         asm volatile("cpuid" :
208                      "=a" (ret) :
209                      "b" (a), "c" (b), "d" (c), "0" (MAGIC1), "D" (cmd + MAGIC2));
210         return ret;
211 }
212
213 void __init simnow_init(char *str)
214 {
215         char *fn = "klog";
216         if (*str == '=')
217                 fn = ++str;
218         /* error ignored */
219         simnow_fd = simnow(XOPEN, (unsigned long)fn, O_WRONLY|O_APPEND|O_CREAT, 0644);
220 }
221
222 static void simnow_write(struct console *con, const char *s, unsigned n)
223 {
224         simnow(XWRITE, simnow_fd, (unsigned long)s, n);
225 }
226
227 static struct console simnow_console = {
228         .name =         "simnow",
229         .write =        simnow_write,
230         .flags =        CON_PRINTBUFFER,
231         .index =        -1,
232 };
233
234 /* Direct interface for emergencies */
235 struct console *early_console = &early_vga_console;
236 static int early_console_initialized = 0;
237
238 void early_printk(const char *fmt, ...)
239 {
240         char buf[512];
241         int n;
242         va_list ap;
243
244         va_start(ap,fmt);
245         n = vscnprintf(buf,512,fmt,ap);
246         early_console->write(early_console,buf,n);
247         va_end(ap);
248 }
249
250 static int __initdata keep_early;
251
252 static int __init setup_early_printk(char *buf)
253 {
254         if (!buf)
255                 return 0;
256
257         if (early_console_initialized)
258                 return 0;
259         early_console_initialized = 1;
260
261         if (strstr(buf, "keep"))
262                 keep_early = 1;
263
264         if (!strncmp(buf, "serial", 6)) {
265                 early_serial_init(buf + 6);
266                 early_console = &early_serial_console;
267         } else if (!strncmp(buf, "ttyS", 4)) {
268                 early_serial_init(buf);
269                 early_console = &early_serial_console;
270         } else if (!strncmp(buf, "vga", 3)
271                    && SCREEN_INFO.orig_video_isVGA == 1) {
272                 max_xpos = SCREEN_INFO.orig_video_cols;
273                 max_ypos = SCREEN_INFO.orig_video_lines;
274                 current_ypos = SCREEN_INFO.orig_y;
275                 early_console = &early_vga_console;
276         } else if (!strncmp(buf, "simnow", 6)) {
277                 simnow_init(buf + 6);
278                 early_console = &simnow_console;
279                 keep_early = 1;
280         }
281         register_console(early_console);
282         return 0;
283 }
284
285 early_param("earlyprintk", setup_early_printk);
286
287 void __init disable_early_printk(void)
288 {
289         if (!early_console_initialized || !early_console)
290                 return;
291         if (!keep_early) {
292                 printk("disabling early console\n");
293                 unregister_console(early_console);
294                 early_console_initialized = 0;
295         } else {
296                 printk("keeping early console\n");
297         }
298 }
299