]> git.saurik.com Git - apple/xnu.git/blob - osfmk/console/ppc/serial_console.c
acb07b0af7b94e6f5a4f43dd6ec334c998d74e31
[apple/xnu.git] / osfmk / console / ppc / serial_console.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * @APPLE_FREE_COPYRIGHT@
35 */
36
37 #include <mach_kdb.h>
38 #include <platforms.h>
39 #include <serial_console_default.h>
40
41 #include <kern/spl.h>
42 #include <machine/machparam.h> /* spl definitions */
43 #include <types.h>
44 #include <console/video_console.h>
45 #include <kern/kalloc.h>
46 #include <kern/thread.h>
47 #include <ppc/misc_protos.h>
48 #include <ppc/serial_io.h>
49 #include <kern/cpu_number.h>
50 #include <ppc/Firmware.h>
51 #include <ppc/proc_reg.h>
52 #include <ppc/cpu_internal.h>
53 #include <ppc/exception.h>
54 #include <pexpert/pexpert.h>
55
56 /*
57 * A machine MUST have a console. In our case
58 * things are a little complicated by the graphic
59 * display: people expect it to be their "console",
60 * but we'd like to be able to live without it.
61 * This is not to be confused with the "rconsole" thing:
62 * that just duplicates the console I/O to
63 * another place (for debugging/logging purposes).
64 */
65
66 const int console_unit = 0;
67 const int console_chan_default = CONSOLE_PORT;
68 #define console_chan (console_chan_default) /* ^ cpu_number()) */
69
70 #define OPS(putc, getc, nosplputc, nosplgetc) putc, getc
71
72 const struct console_ops {
73 int (*putc)(int, int, int);
74 int (*getc)(int, int, boolean_t, boolean_t);
75 } cons_ops[] = {
76 #define SCC_CONS_OPS 0
77 {OPS(scc_putc, scc_getc, no_spl_scputc, no_spl_scgetc)},
78 #define VC_CONS_OPS 1
79 {OPS(vcputc, vcgetc, no_spl_vcputc, no_spl_vcgetc)},
80 };
81 #define NCONSOPS (sizeof cons_ops / sizeof cons_ops[0])
82
83 #if SERIAL_CONSOLE_DEFAULT
84 #define CONS_OPS SCC_CONS_OPS
85 #define CONS_NAME "com"
86 #else
87 #define CONS_OPS VC_CONS_OPS
88 #define CONS_NAME "vc"
89 #endif
90
91 #define MP_SAFE_CONSOLE 1 /* Set this to 1 to allow more than 1 processor to print at once */
92 #if MP_SAFE_CONSOLE
93
94 struct ppcbfr { /* Controls multiple processor output */
95 unsigned int pos; /* Current position in buffer */
96 unsigned int noprompt; /* Set if we skip the prompt */
97 unsigned int echo; /* Control character echoing */
98 char buffer[256]; /* Fairly big buffer */
99 };
100 typedef struct ppcbfr ppcbfr_t;
101
102 ppcbfr_t cbfr_boot_cpu; /* Get one for boot cpu */
103 volatile unsigned int cbfpend; /* A buffer is pending output */
104 volatile unsigned int sconowner=-1; /* Mark who's actually writing */
105
106 #endif
107
108
109 unsigned int cons_ops_index = CONS_OPS;
110 unsigned int killprint = 0;
111 unsigned int debcnputc = 0;
112 extern unsigned int mappingdeb0;
113 extern int debugger_cpu;
114
115 void *console_per_proc_alloc(boolean_t boot_processor)
116 {
117 ppcbfr_t *cbfr_cpu;
118
119 if (boot_processor)
120 cbfr_cpu = &cbfr_boot_cpu;
121 else {
122 cbfr_cpu = (ppcbfr_t *)kalloc(sizeof(ppcbfr_t));
123 if (cbfr_cpu == (ppcbfr_t *)NULL)
124 return (void *)NULL;
125 }
126 bzero((char *)cbfr_cpu, sizeof(ppcbfr_t));
127 return (void *)cbfr_cpu;
128 }
129
130 void console_per_proc_free(void *per_proc_cbfr)
131 {
132 if (per_proc_cbfr == (void *)&cbfr_boot_cpu)
133 return;
134 else
135 kfree(per_proc_cbfr, sizeof(ppcbfr_t));
136 }
137
138
139 static void _cnputc(char c)
140 {
141 cons_ops[cons_ops_index].putc(console_unit, console_chan, c);
142 }
143
144 void cnputcusr(char c) { /* Echo input character directly */
145 struct per_proc_info *procinfo;
146 spl_t s;
147
148 s=splhigh();
149 procinfo = getPerProc();
150
151 hw_atomic_add(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
152
153 _cnputc( c); /* Echo the character */
154 if(c=='\n') _cnputc( '\r'); /* Add a return if we had a new line */
155
156 hw_atomic_sub(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
157 splx(s);
158 return;
159 }
160
161 void
162 cnputc(char c)
163 {
164 unsigned int oldpend, i, cpu, ourbit, sccpu;
165 struct per_proc_info *procinfo;
166 ppcbfr_t *cbfr, *cbfr_cpu;
167 spl_t s;
168
169 #if MP_SAFE_CONSOLE
170
171 /*
172 * Handle multiple CPU console output.
173 * Note: this thing has gotten god-awful complicated. We need a better way.
174 */
175
176
177 if(killprint) {
178 return; /* If printing is disabled, bail... */
179 }
180
181 s=splhigh(); /* Don't bother me */
182 procinfo = getPerProc();
183 cpu = procinfo->cpu_number;
184 cbfr = procinfo->pp_cbfr;
185
186 hw_atomic_add(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
187
188 ourbit = 1 << cpu; /* Make a mask for just us */
189 if(debugger_cpu != -1) { /* Are we in the debugger with empty buffers? */
190
191 while(sconowner != cpu) { /* Anyone but us? */
192 hw_compare_and_store(-1, cpu, (unsigned int *)&sconowner); /* Try to mark it for us if idle */
193 }
194
195 _cnputc( c); /* Yeah, just write it */
196 if(c=='\n') /* Did we just write a new line? */
197 _cnputc( '\r'); /* Yeah, just add a return */
198
199 sconowner=-1; /* Mark it idle */
200 hw_atomic_sub(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
201
202 splx(s);
203 return; /* Leave... */
204 }
205
206
207 while(ourbit&cbfpend); /* We aren't "double buffered," so we'll just wait until the buffers are written */
208 isync(); /* Just in case we had to wait */
209
210 if(c) { /* If the character is not null */
211 cbfr->buffer[cbfr->pos]=c; /* Fill in the buffer for our CPU */
212 cbfr->pos++; /* Up the count */
213 if(cbfr->pos > 253) { /* Is the buffer full? */
214 cbfr->buffer[254]='\n'; /* Yeah, set the second to last as a LF */
215 cbfr->buffer[255]='\r'; /* And the last to a CR */
216 cbfr->pos=256; /* Push the buffer to the end */
217 c='\r'; /* Set character to a CR */
218 }
219 }
220
221 if(c == '\n') { /* Are we finishing a line? */
222 cbfr->buffer[cbfr->pos]='\r'; /* And the last to a CR */
223 cbfr->pos++; /* Up the count */
224 c='\r'; /* Set character to a CR */
225 }
226
227 #if 1
228 if(cbfr->echo == 1) { /* Did we hit an escape last time? */
229 if(c == 'K') { /* Is it a partial clear? */
230 cbfr->echo = 2; /* Yes, enter echo mode */
231 }
232 else cbfr->echo = 0; /* Otherwise reset escape */
233 }
234 else if(cbfr->echo == 0) { /* Not in escape sequence, see if we should enter */
235 cbfr->echo = 1; /* Set that we are in escape sequence */
236 }
237 #endif
238
239 if((c == 0x00) || (c == '\r') || (cbfr->echo == 2)) { /* Try to push out all buffers if we see CR or null */
240
241 while(1) { /* Loop until we see who's doing this */
242 oldpend=cbfpend; /* Get the currentest pending buffer flags */
243 if(hw_compare_and_store(oldpend, oldpend|ourbit, (unsigned int *)&cbfpend)) /* Swap ours on if no change */
244 break; /* Bail the loop if it worked */
245 }
246
247 if(!hw_compare_and_store(-1, cpu, (unsigned int *)&sconowner)) { /* See if someone else has this, and take it if not */
248 procinfo->debugger_holdoff = 0; /* Allow debugger entry (this is a HACK) */
249 splx(s); /* Let's take some 'rupts now */
250 return; /* We leave here, 'cause another processor is already writing the buffers */
251 }
252
253 while(1) { /* Loop to dump out all of the finished buffers */
254 oldpend=cbfpend; /* Get the most current finished buffers */
255 for(sccpu=0; sccpu<real_ncpus; sccpu++) { /* Cycle through all CPUs buffers */
256 if ((PerProcTable[sccpu].ppe_vaddr == 0)
257 || (PerProcTable[sccpu].ppe_vaddr->pp_cbfr == 0))
258 continue;
259
260 cbfr_cpu = PerProcTable[sccpu].ppe_vaddr->pp_cbfr;
261
262 if(oldpend&(1<<sccpu)) { /* Does this guy have a buffer to do? */
263
264 #if 0
265 if(!cbfr_cpu->noprompt) { /* Don't prompt if there was not CR before */
266 _cnputc( '{'); /* Mark CPU number */
267 _cnputc( '0'+sccpu); /* Mark CPU number */
268 _cnputc( '.'); /* (TEST/DEBUG) */
269 _cnputc( '0'+cpu); /* (TEST/DEBUG) */
270 _cnputc( '}'); /* Mark CPU number */
271 _cnputc( ' '); /* Mark CPU number */
272 }
273 #endif
274
275 for(i=0; i<cbfr_cpu->pos; i++) { /* Do the whole buffer */
276 _cnputc(cbfr_cpu->buffer[i]); /* Write it */
277 }
278
279 if(cbfr_cpu->buffer[cbfr_cpu->pos-1]!='\r') { /* Was the last character a return? */
280 cbfr_cpu->noprompt = 1; /* Remember not to prompt */
281 }
282 else { /* Last was a return */
283 cbfr_cpu->noprompt = 0; /* Otherwise remember to prompt */
284 cbfr_cpu->echo = 0; /* And clear echo */
285 }
286
287 cbfr_cpu->pos=0; /* Reset the buffer pointer */
288
289 while(!hw_compare_and_store(cbfpend, cbfpend&~(1<<sccpu), (unsigned int *)&cbfpend)); /* Swap it off */
290 }
291 }
292 sconowner=-1; /* Set the writer to idle */
293 sync(); /* Insure that everything's done */
294 if(hw_compare_and_store(0, 0, (unsigned int *)&cbfpend)) break; /* If there are no new buffers, we are done... */
295 if(!hw_compare_and_store(-1, cpu, (unsigned int *)&sconowner)) break; /* If this isn't idle anymore, we're done */
296
297 }
298 }
299 hw_atomic_sub(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
300 splx(s); /* Let's take some 'rupts now */
301
302 #else /* MP_SAFE_CONSOLE */
303 _cnputc( c);
304 if (c == '\n')
305 _cnputc('\r');
306 #endif /* MP_SAFE_CONSOLE */
307
308 }
309
310 int
311 cngetc()
312 {
313 return cons_ops[cons_ops_index].getc(console_unit, console_chan,
314 TRUE, FALSE);
315 }
316
317 int
318 cnmaygetc()
319 {
320 return cons_ops[cons_ops_index].getc(console_unit, console_chan,
321 FALSE, FALSE);
322 }
323
324 boolean_t console_is_serial()
325 {
326 return cons_ops_index == SCC_CONS_OPS;
327 }
328
329 int
330 switch_to_video_console()
331 {
332 int old_cons_ops = cons_ops_index;
333 cons_ops_index = VC_CONS_OPS;
334 return old_cons_ops;
335 }
336
337 int
338 switch_to_serial_console()
339 {
340 int old_cons_ops = cons_ops_index;
341 cons_ops_index = SCC_CONS_OPS;
342 return old_cons_ops;
343 }
344
345 /* The switch_to_{video,serial,kgdb}_console functions return a cookie that
346 can be used to restore the console to whatever it was before, in the
347 same way that splwhatever() and splx() work. */
348 void
349 switch_to_old_console(int old_console)
350 {
351 static boolean_t squawked;
352 unsigned int ops = old_console;
353
354 if (ops >= NCONSOPS && !squawked) {
355 squawked = TRUE;
356 printf("switch_to_old_console: unknown ops %d\n", ops);
357 } else
358 cons_ops_index = ops;
359 }
360
361
362 int
363 vcgetc(__unused int l,
364 __unused int u,
365 __unused boolean_t wait,
366 __unused boolean_t raw)
367 {
368 char c;
369
370 if( 0 == (*PE_poll_input)( 0, &c))
371 return( c);
372 else
373 return( 0);
374 }