]> git.saurik.com Git - apple/xnu.git/blob - osfmk/console/ppc/serial_console.c
xnu-792.13.8.tar.gz
[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 <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>
56
57 /*
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).
65 */
66
67 const int console_unit = 0;
68 const uint32_t console_chan_default = CONSOLE_PORT;
69 #define console_chan (console_chan_default) /* ^ cpu_number()) */
70
71
72 #define MP_SAFE_CONSOLE 1 /* Set this to 1 to allow more than 1 processor to print at once */
73 #if MP_SAFE_CONSOLE
74
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 */
80 };
81 typedef struct ppcbfr ppcbfr_t;
82
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 */
86
87 #endif
88
89 #define OPS(putc, getc, nosplputc, nosplgetc) putc, getc
90
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)},
94 };
95
96 uint32_t nconsops = (sizeof cons_ops / sizeof cons_ops[0]);
97
98 uint32_t cons_ops_index = VC_CONS_OPS;
99
100 unsigned int killprint = 0;
101 unsigned int debcnputc = 0;
102 extern unsigned int mappingdeb0;
103 extern int debugger_cpu;
104
105 void *console_per_proc_alloc(boolean_t boot_processor)
106 {
107 ppcbfr_t *cbfr_cpu;
108
109 if (boot_processor)
110 cbfr_cpu = &cbfr_boot_cpu;
111 else {
112 cbfr_cpu = (ppcbfr_t *)kalloc(sizeof(ppcbfr_t));
113 if (cbfr_cpu == (ppcbfr_t *)NULL)
114 return (void *)NULL;
115 }
116 bzero((char *)cbfr_cpu, sizeof(ppcbfr_t));
117 return (void *)cbfr_cpu;
118 }
119
120 void console_per_proc_free(void *per_proc_cbfr)
121 {
122 if (per_proc_cbfr == (void *)&cbfr_boot_cpu)
123 return;
124 else
125 kfree(per_proc_cbfr, sizeof(ppcbfr_t));
126 }
127
128
129 static void _cnputc(char c)
130 {
131 cons_ops[cons_ops_index].putc(console_unit, console_chan, c);
132 }
133
134 void cnputcusr(char c) { /* Echo input character directly */
135 struct per_proc_info *procinfo;
136 spl_t s;
137
138 s=splhigh();
139 procinfo = getPerProc();
140
141 hw_atomic_add(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
142
143 _cnputc( c); /* Echo the character */
144 if(c=='\n') _cnputc( '\r'); /* Add a return if we had a new line */
145
146 hw_atomic_sub(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
147 splx(s);
148 return;
149 }
150
151 void
152 cnputc(char c)
153 {
154 unsigned int oldpend, i, cpu, ourbit, sccpu;
155 struct per_proc_info *procinfo;
156 ppcbfr_t *cbfr, *cbfr_cpu;
157 spl_t s;
158
159 #if MP_SAFE_CONSOLE
160
161 /*
162 * Handle multiple CPU console output.
163 * Note: this thing has gotten god-awful complicated. We need a better way.
164 */
165
166
167 if(killprint) {
168 return; /* If printing is disabled, bail... */
169 }
170
171 s=splhigh(); /* Don't bother me */
172 procinfo = getPerProc();
173 cpu = procinfo->cpu_number;
174 cbfr = procinfo->pp_cbfr;
175
176 hw_atomic_add(&(procinfo->debugger_holdoff), 1); /* Don't allow debugger entry just now (this is a HACK) */
177
178 ourbit = 1 << cpu; /* Make a mask for just us */
179 if(debugger_cpu != -1) { /* Are we in the debugger with empty buffers? */
180
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 */
183 }
184
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 */
188
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) */
191
192 splx(s);
193 return; /* Leave... */
194 }
195
196
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 */
199
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 */
208 }
209 }
210
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 */
215 }
216
217 #if 1
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 */
221 }
222 else cbfr->echo = 0; /* Otherwise reset escape */
223 }
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 */
226 }
227 #endif
228
229 if((c == 0x00) || (c == '\r') || (cbfr->echo == 2)) { /* Try to push out all buffers if we see CR or null */
230
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 */
235 }
236
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 */
241 }
242
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))
248 continue;
249
250 cbfr_cpu = PerProcTable[sccpu].ppe_vaddr->pp_cbfr;
251
252 if(oldpend&(1<<sccpu)) { /* Does this guy have a buffer to do? */
253
254 #if 0
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 */
262 }
263 #endif
264
265 for(i=0; i<cbfr_cpu->pos; i++) { /* Do the whole buffer */
266 _cnputc(cbfr_cpu->buffer[i]); /* Write it */
267 }
268
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 */
271 }
272 else { /* Last was a return */
273 cbfr_cpu->noprompt = 0; /* Otherwise remember to prompt */
274 cbfr_cpu->echo = 0; /* And clear echo */
275 }
276
277 cbfr_cpu->pos=0; /* Reset the buffer pointer */
278
279 while(!hw_compare_and_store(cbfpend, cbfpend&~(1<<sccpu), (unsigned int *)&cbfpend)); /* Swap it off */
280 }
281 }
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 */
286
287 }
288 }
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 */
291
292 #else /* MP_SAFE_CONSOLE */
293 _cnputc( c);
294 if (c == '\n')
295 _cnputc('\r');
296 #endif /* MP_SAFE_CONSOLE */
297
298 }
299
300 int
301 cngetc()
302 {
303 return cons_ops[cons_ops_index].getc(console_unit, console_chan,
304 TRUE, FALSE);
305 }
306
307 int
308 cnmaygetc()
309 {
310 return cons_ops[cons_ops_index].getc(console_unit, console_chan,
311 FALSE, FALSE);
312 }
313
314
315 int
316 vcgetc(__unused int l,
317 __unused int u,
318 __unused boolean_t wait,
319 __unused boolean_t raw)
320 {
321 char c;
322
323 if( 0 == (*PE_poll_input)( 0, &c))
324 return( c);
325 else
326 return( 0);
327 }