]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/console/ppc/serial_console.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * @APPLE_FREE_COPYRIGHT@
38 #include <platforms.h>
39 #include <serial_console_default.h>
42 #include <machine/machparam.h> /* spl definitions */
44 #include <console/video_console.h>
45 #include <console/serial_protos.h>
46 #include <kern/kalloc.h>
47 #include <kern/thread.h>
48 #include <ppc/misc_protos.h>
49 #include <ppc/serial_io.h>
50 #include <kern/cpu_number.h>
51 #include <ppc/Firmware.h>
52 #include <ppc/proc_reg.h>
53 #include <ppc/cpu_internal.h>
54 #include <ppc/exception.h>
55 #include <pexpert/pexpert.h>
58 * A machine MUST have a console. In our case
59 * things are a little complicated by the graphic
60 * display: people expect it to be their "console",
61 * but we'd like to be able to live without it.
62 * This is not to be confused with the "rconsole" thing:
63 * that just duplicates the console I/O to
64 * another place (for debugging/logging purposes).
67 const int console_unit
= 0;
68 const uint32_t console_chan_default
= CONSOLE_PORT
;
69 #define console_chan (console_chan_default) /* ^ cpu_number()) */
72 #define MP_SAFE_CONSOLE 1 /* Set this to 1 to allow more than 1 processor to print at once */
75 struct ppcbfr
{ /* Controls multiple processor output */
76 unsigned int pos
; /* Current position in buffer */
77 unsigned int noprompt
; /* Set if we skip the prompt */
78 unsigned int echo
; /* Control character echoing */
79 char buffer
[256]; /* Fairly big buffer */
81 typedef struct ppcbfr ppcbfr_t
;
83 ppcbfr_t cbfr_boot_cpu
; /* Get one for boot cpu */
84 volatile unsigned int cbfpend
; /* A buffer is pending output */
85 volatile unsigned int sconowner
=-1; /* Mark who's actually writing */
89 #define OPS(putc, getc, nosplputc, nosplgetc) putc, getc
91 console_ops_t cons_ops
[] = {
92 {OPS(scc_putc
, scc_getc
, no_spl_scputc
, no_spl_scgetc
)},
93 {OPS(vcputc
, vcgetc
, no_spl_vcputc
, no_spl_vcgetc
)},
96 uint32_t nconsops
= (sizeof cons_ops
/ sizeof cons_ops
[0]);
98 uint32_t cons_ops_index
= VC_CONS_OPS
;
100 unsigned int killprint
= 0;
101 unsigned int debcnputc
= 0;
102 extern unsigned int mappingdeb0
;
103 extern int debugger_cpu
;
105 void *console_per_proc_alloc(boolean_t boot_processor
)
110 cbfr_cpu
= &cbfr_boot_cpu
;
112 cbfr_cpu
= (ppcbfr_t
*)kalloc(sizeof(ppcbfr_t
));
113 if (cbfr_cpu
== (ppcbfr_t
*)NULL
)
116 bzero((char *)cbfr_cpu
, sizeof(ppcbfr_t
));
117 return (void *)cbfr_cpu
;
120 void console_per_proc_free(void *per_proc_cbfr
)
122 if (per_proc_cbfr
== (void *)&cbfr_boot_cpu
)
125 kfree(per_proc_cbfr
, sizeof(ppcbfr_t
));
129 static void _cnputc(char c
)
131 cons_ops
[cons_ops_index
].putc(console_unit
, console_chan
, c
);
134 void cnputcusr(char c
) { /* Echo input character directly */
135 struct per_proc_info
*procinfo
;
139 procinfo
= getPerProc();
141 hw_atomic_add(&(procinfo
->debugger_holdoff
), 1); /* Don't allow debugger entry just now (this is a HACK) */
143 _cnputc( c
); /* Echo the character */
144 if(c
=='\n') _cnputc( '\r'); /* Add a return if we had a new line */
146 hw_atomic_sub(&(procinfo
->debugger_holdoff
), 1); /* Don't allow debugger entry just now (this is a HACK) */
154 unsigned int oldpend
, i
, cpu
, ourbit
, sccpu
;
155 struct per_proc_info
*procinfo
;
156 ppcbfr_t
*cbfr
, *cbfr_cpu
;
162 * Handle multiple CPU console output.
163 * Note: this thing has gotten god-awful complicated. We need a better way.
168 return; /* If printing is disabled, bail... */
171 s
=splhigh(); /* Don't bother me */
172 procinfo
= getPerProc();
173 cpu
= procinfo
->cpu_number
;
174 cbfr
= procinfo
->pp_cbfr
;
176 hw_atomic_add(&(procinfo
->debugger_holdoff
), 1); /* Don't allow debugger entry just now (this is a HACK) */
178 ourbit
= 1 << cpu
; /* Make a mask for just us */
179 if(debugger_cpu
!= -1) { /* Are we in the debugger with empty buffers? */
181 while(sconowner
!= cpu
) { /* Anyone but us? */
182 hw_compare_and_store(-1, cpu
, (unsigned int *)&sconowner
); /* Try to mark it for us if idle */
185 _cnputc( c
); /* Yeah, just write it */
186 if(c
=='\n') /* Did we just write a new line? */
187 _cnputc( '\r'); /* Yeah, just add a return */
189 sconowner
=-1; /* Mark it idle */
190 hw_atomic_sub(&(procinfo
->debugger_holdoff
), 1); /* Don't allow debugger entry just now (this is a HACK) */
193 return; /* Leave... */
197 while(ourbit
&cbfpend
); /* We aren't "double buffered," so we'll just wait until the buffers are written */
198 isync(); /* Just in case we had to wait */
200 if(c
) { /* If the character is not null */
201 cbfr
->buffer
[cbfr
->pos
]=c
; /* Fill in the buffer for our CPU */
202 cbfr
->pos
++; /* Up the count */
203 if(cbfr
->pos
> 253) { /* Is the buffer full? */
204 cbfr
->buffer
[254]='\n'; /* Yeah, set the second to last as a LF */
205 cbfr
->buffer
[255]='\r'; /* And the last to a CR */
206 cbfr
->pos
=256; /* Push the buffer to the end */
207 c
='\r'; /* Set character to a CR */
211 if(c
== '\n') { /* Are we finishing a line? */
212 cbfr
->buffer
[cbfr
->pos
]='\r'; /* And the last to a CR */
213 cbfr
->pos
++; /* Up the count */
214 c
='\r'; /* Set character to a CR */
218 if(cbfr
->echo
== 1) { /* Did we hit an escape last time? */
219 if(c
== 'K') { /* Is it a partial clear? */
220 cbfr
->echo
= 2; /* Yes, enter echo mode */
222 else cbfr
->echo
= 0; /* Otherwise reset escape */
224 else if(cbfr
->echo
== 0) { /* Not in escape sequence, see if we should enter */
225 cbfr
->echo
= 1; /* Set that we are in escape sequence */
229 if((c
== 0x00) || (c
== '\r') || (cbfr
->echo
== 2)) { /* Try to push out all buffers if we see CR or null */
231 while(1) { /* Loop until we see who's doing this */
232 oldpend
=cbfpend
; /* Get the currentest pending buffer flags */
233 if(hw_compare_and_store(oldpend
, oldpend
|ourbit
, (unsigned int *)&cbfpend
)) /* Swap ours on if no change */
234 break; /* Bail the loop if it worked */
237 if(!hw_compare_and_store(-1, cpu
, (unsigned int *)&sconowner
)) { /* See if someone else has this, and take it if not */
238 procinfo
->debugger_holdoff
= 0; /* Allow debugger entry (this is a HACK) */
239 splx(s
); /* Let's take some 'rupts now */
240 return; /* We leave here, 'cause another processor is already writing the buffers */
243 while(1) { /* Loop to dump out all of the finished buffers */
244 oldpend
=cbfpend
; /* Get the most current finished buffers */
245 for(sccpu
=0; sccpu
<real_ncpus
; sccpu
++) { /* Cycle through all CPUs buffers */
246 if ((PerProcTable
[sccpu
].ppe_vaddr
== 0)
247 || (PerProcTable
[sccpu
].ppe_vaddr
->pp_cbfr
== 0))
250 cbfr_cpu
= PerProcTable
[sccpu
].ppe_vaddr
->pp_cbfr
;
252 if(oldpend
&(1<<sccpu
)) { /* Does this guy have a buffer to do? */
255 if(!cbfr_cpu
->noprompt
) { /* Don't prompt if there was not CR before */
256 _cnputc( '{'); /* Mark CPU number */
257 _cnputc( '0'+sccpu
); /* Mark CPU number */
258 _cnputc( '.'); /* (TEST/DEBUG) */
259 _cnputc( '0'+cpu
); /* (TEST/DEBUG) */
260 _cnputc( '}'); /* Mark CPU number */
261 _cnputc( ' '); /* Mark CPU number */
265 for(i
=0; i
<cbfr_cpu
->pos
; i
++) { /* Do the whole buffer */
266 _cnputc(cbfr_cpu
->buffer
[i
]); /* Write it */
269 if(cbfr_cpu
->buffer
[cbfr_cpu
->pos
-1]!='\r') { /* Was the last character a return? */
270 cbfr_cpu
->noprompt
= 1; /* Remember not to prompt */
272 else { /* Last was a return */
273 cbfr_cpu
->noprompt
= 0; /* Otherwise remember to prompt */
274 cbfr_cpu
->echo
= 0; /* And clear echo */
277 cbfr_cpu
->pos
=0; /* Reset the buffer pointer */
279 while(!hw_compare_and_store(cbfpend
, cbfpend
&~(1<<sccpu
), (unsigned int *)&cbfpend
)); /* Swap it off */
282 sconowner
=-1; /* Set the writer to idle */
283 sync(); /* Insure that everything's done */
284 if(hw_compare_and_store(0, 0, (unsigned int *)&cbfpend
)) break; /* If there are no new buffers, we are done... */
285 if(!hw_compare_and_store(-1, cpu
, (unsigned int *)&sconowner
)) break; /* If this isn't idle anymore, we're done */
289 hw_atomic_sub(&(procinfo
->debugger_holdoff
), 1); /* Don't allow debugger entry just now (this is a HACK) */
290 splx(s
); /* Let's take some 'rupts now */
292 #else /* MP_SAFE_CONSOLE */
296 #endif /* MP_SAFE_CONSOLE */
303 return cons_ops
[cons_ops_index
].getc(console_unit
, console_chan
,
310 return cons_ops
[cons_ops_index
].getc(console_unit
, console_chan
,
316 vcgetc(__unused
int l
,
318 __unused boolean_t wait
,
319 __unused boolean_t raw
)
323 if( 0 == (*PE_poll_input
)( 0, &c
))