booke_machdep.c revision 1.6 1 /*-
2 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
7 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
8 *
9 * This material is based upon work supported by the Defense Advanced Research
10 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
11 * Contract No. N66001-09-C-2073.
12 * Approved for Public Release, Distribution Unlimited
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #define __INTR_PRIVATE
37 #define _POWERPC_BUS_DMA_PRIVATE
38
39 #include <sys/cdefs.h>
40
41 #include <sys/param.h>
42 #include <sys/cpu.h>
43 #include <sys/device.h>
44 #include <sys/intr.h>
45 #include <sys/mount.h>
46 #include <sys/msgbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/reboot.h>
49 #include <sys/bus.h>
50
51 #include <uvm/uvm_extern.h>
52
53 #include <powerpc/spr.h>
54 #include <powerpc/booke/spr.h>
55 #include <powerpc/booke/cpuvar.h>
56
57 /*
58 * Global variables used here and there
59 */
60 paddr_t msgbuf_paddr;
61 psize_t pmemsize;
62 struct vm_map *phys_map;
63
64 static bus_addr_t booke_dma_phys_to_bus_mem(bus_dma_tag_t, bus_addr_t);
65 static bus_addr_t booke_dma_bus_mem_to_phys(bus_dma_tag_t, bus_addr_t);
66
67
68 struct powerpc_bus_dma_tag booke_bus_dma_tag = {
69 ._dmamap_create = _bus_dmamap_create,
70 ._dmamap_destroy = _bus_dmamap_destroy,
71 ._dmamap_load = _bus_dmamap_load,
72 ._dmamap_load_mbuf = _bus_dmamap_load_mbuf,
73 ._dmamap_load_uio = _bus_dmamap_load_uio,
74 ._dmamap_load_raw = _bus_dmamap_load_raw,
75 ._dmamap_unload = _bus_dmamap_unload,
76 ._dmamap_sync = _bus_dmamap_sync,
77 ._dmamem_alloc = _bus_dmamem_alloc,
78 ._dmamem_free = _bus_dmamem_free,
79 ._dmamem_map = _bus_dmamem_map,
80 ._dmamem_unmap = _bus_dmamem_unmap,
81 ._dmamem_mmap = _bus_dmamem_mmap,
82 ._dma_phys_to_bus_mem = booke_dma_phys_to_bus_mem,
83 ._dma_bus_mem_to_phys = booke_dma_bus_mem_to_phys,
84 };
85
86 static bus_addr_t
87 booke_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t a)
88 {
89 return a;
90 }
91
92 static bus_addr_t
93 booke_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t a)
94 {
95 return a;
96 }
97
98 static int
99 null_splraise(int ipl)
100 {
101 int cpl = curcpu()->ci_cpl;
102 curcpu()->ci_cpl = ipl;
103 return cpl;
104 }
105
106 static void
107 null_splx(int ipl)
108 {
109 curcpu()->ci_cpl = ipl;
110 }
111
112 static const struct intrsw null_intrsw = {
113 .intrsw_splraise = null_splraise,
114 .intrsw_splx = null_splx,
115 };
116
117 const struct intrsw *powerpc_intrsw = &null_intrsw;
118 struct cpu_md_ops cpu_md_ops;
119
120 struct cpu_softc cpu_softc[] = {
121 [0] = {
122 .cpu_ci = &cpu_info[0],
123 },
124 #ifdef MULTIPROCESSOR
125 [CPU_MAXNUM-1] = {
126 .cpu_ci = &cpu_info[CPU_MAXNUM-1],
127 },
128 #endif
129 };
130 struct cpu_info cpu_info[] = {
131 [0] = {
132 .ci_curlwp = &lwp0,
133 .ci_tlb_info = &pmap_tlb0_info,
134 .ci_softc = &cpu_softc[0],
135 .ci_cpl = IPL_HIGH,
136 },
137 #ifdef MULTIPROCESSOR
138 [CPU_MAXNUM-1] = {
139 .ci_curlwp = NULL,
140 .ci_tlb_info = &pmap_tlb0_info,
141 .ci_softc = &cpu_softc[CPU_MAXNUM-1],
142 .ci_cpl = IPL_HIGH,
143 },
144 #endif
145 };
146
147 /*
148 * This should probably be in autoconf! XXX
149 */
150 char cpu_model[80];
151 char machine[] = MACHINE; /* from <machine/param.h> */
152 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
153
154 char bootpath[256];
155
156 #if NKSYMS || defined(DDB) || defined(MODULAR)
157 void *startsym, *endsym;
158 #endif
159
160 int fake_mapiodev = 1;
161
162 void lcsplx(int);
163
164 void
165 booke_cpu_startup(const char *model)
166 {
167 vaddr_t minaddr, maxaddr;
168 char pbuf[9];
169
170 strlcpy(cpu_model, model, sizeof(cpu_model));
171
172 printf("%s%s", copyright, version);
173
174 format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem));
175 printf("total memory = %s\n", pbuf);
176
177 minaddr = 0;
178 /*
179 * Allocate a submap for physio
180 */
181 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
182 VM_PHYS_SIZE, 0, false, NULL);
183
184 /*
185 * No need to allocate an mbuf cluster submap. Mbuf clusters
186 * are allocated via the pool allocator, and we use direct-mapped
187 * pool pages.
188 */
189
190 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
191 printf("avail memory = %s\n", pbuf);
192
193 /*
194 * Set up the board properties database.
195 */
196 board_info_init();
197
198 /*
199 * Now that we have VM, malloc()s are OK in bus_space.
200 */
201 bus_space_mallocok();
202 fake_mapiodev = 0;
203 }
204
205 static void
206 dumpsys(void)
207 {
208
209 printf("dumpsys: TBD\n");
210 }
211
212 /*
213 * Halt or reboot the machine after syncing/dumping according to howto.
214 */
215 void
216 cpu_reboot(int howto, char *what)
217 {
218 static int syncing;
219 static char str[256];
220 char *ap = str, *ap1 = ap;
221
222 boothowto = howto;
223 if (!cold && !(howto & RB_NOSYNC) && !syncing) {
224 syncing = 1;
225 vfs_shutdown(); /* sync */
226 resettodr(); /* set wall clock */
227 }
228
229 splhigh();
230
231 if (!cold && (howto & RB_DUMP))
232 dumpsys();
233
234 doshutdownhooks();
235
236 pmf_system_shutdown(boothowto);
237
238 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
239 /* Power off here if we know how...*/
240 }
241
242 if (howto & RB_HALT) {
243 printf("halted\n\n");
244
245 goto reboot; /* XXX for now... */
246
247 #ifdef DDB
248 printf("dropping to debugger\n");
249 while(1)
250 Debugger();
251 #endif
252 }
253
254 printf("rebooting\n\n");
255 if (what && *what) {
256 if (strlen(what) > sizeof str - 5)
257 printf("boot string too large, ignored\n");
258 else {
259 strcpy(str, what);
260 ap1 = ap = str + strlen(str);
261 *ap++ = ' ';
262 }
263 }
264 *ap++ = '-';
265 if (howto & RB_SINGLE)
266 *ap++ = 's';
267 if (howto & RB_KDB)
268 *ap++ = 'd';
269 *ap++ = 0;
270 if (ap[-2] == '-')
271 *ap1 = 0;
272
273 /* flush cache for msgbuf */
274 dcache_wb(msgbuf_paddr, round_page(MSGBUFSIZE));
275
276 reboot:
277 __asm volatile("msync; isync");
278 (*cpu_md_ops.md_cpu_reset)();
279
280 printf("%s: md_cpu_reset() failed!\n", __func__);
281 #ifdef DDB
282 for (;;)
283 Debugger();
284 #else
285 for (;;)
286 /* nothing */;
287 #endif
288 }
289 void
290 lcsplx(int spl)
291 {
292 splx(spl);
293 }
294
295 /*
296 * mapiodev:
297 *
298 * Allocate vm space and mapin the I/O address. Use reserved TLB
299 * mapping if one is found.
300 */
301 void *
302 mapiodev(paddr_t pa, psize_t len)
303 {
304 const vsize_t off = pa & PAGE_MASK;
305
306 /*
307 * See if we have reserved TLB entry for the pa. This needs to be
308 * true for console as we can't use uvm during early bootstrap.
309 */
310 void * const p = tlb_mapiodev(pa, len);
311 if (p != NULL)
312 return p;
313
314 if (fake_mapiodev)
315 panic("mapiodev: no TLB entry reserved for %llx+%llx",
316 (long long)pa, (long long)len);
317
318 pa = trunc_page(pa);
319 len = round_page(off + len);
320 vaddr_t va = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY);
321
322 if (va == 0)
323 return NULL;
324
325 for (va += len, pa += len; len > 0; len -= PAGE_SIZE) {
326 va -= PAGE_SIZE;
327 pa -= PAGE_SIZE;
328 pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE,
329 PMAP_NOCACHE);
330 }
331 pmap_update(pmap_kernel());
332 return (void *)(va + off);
333 }
334
335 void
336 unmapiodev(vaddr_t va, vsize_t len)
337 {
338 /* Nothing to do for reserved (ie. not uvm_km_alloc'd) mappings. */
339 if (va < VM_MIN_KERNEL_ADDRESS || va > VM_MAX_KERNEL_ADDRESS) {
340 tlb_unmapiodev(va, len);
341 return;
342 }
343
344 len = round_page((va & PAGE_MASK) + len);
345 va = trunc_page(va);
346
347 pmap_kremove(va, len);
348 uvm_km_free(kernel_map, va, len, UVM_KMF_VAONLY);
349 }
350
351 void
352 cpu_evcnt_attach(struct cpu_info *ci)
353 {
354 struct cpu_softc * const cpu = ci->ci_softc;
355 const char * const xname = device_xname(ci->ci_dev);
356
357 evcnt_attach_dynamic_nozero(&ci->ci_ev_clock, EVCNT_TYPE_INTR,
358 NULL, xname, "clock");
359 evcnt_attach_dynamic_nozero(&cpu->cpu_ev_late_clock, EVCNT_TYPE_INTR,
360 NULL, xname, "late clock");
361 evcnt_attach_dynamic_nozero(&cpu->cpu_ev_exec_trap_sync, EVCNT_TYPE_TRAP,
362 NULL, xname, "exec pages synced (trap)");
363 #ifndef __HAVE_FAST_SOFTINTS
364 evcnt_attach_dynamic_nozero(&ci->ci_ev_softclock, EVCNT_TYPE_INTR,
365 NULL, xname, "soft clock");
366 evcnt_attach_dynamic_nozero(&ci->ci_ev_softnet, EVCNT_TYPE_INTR,
367 NULL, xname, "soft net");
368 evcnt_attach_dynamic_nozero(&ci->ci_ev_softserial, EVCNT_TYPE_INTR,
369 NULL, xname, "soft serial");
370 #endif
371 evcnt_attach_dynamic_nozero(&ci->ci_ev_traps, EVCNT_TYPE_TRAP,
372 NULL, xname, "traps");
373 evcnt_attach_dynamic_nozero(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP,
374 &ci->ci_ev_traps, xname, "kernel DSI traps");
375 evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP,
376 &ci->ci_ev_traps, xname, "user DSI traps");
377 evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP,
378 &ci->ci_ev_udsi, xname, "user DSI failures");
379 evcnt_attach_dynamic_nozero(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP,
380 &ci->ci_ev_traps, xname, "kernel ISI traps");
381 evcnt_attach_dynamic_nozero(&ci->ci_ev_isi, EVCNT_TYPE_TRAP,
382 &ci->ci_ev_traps, xname, "user ISI traps");
383 evcnt_attach_dynamic_nozero(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
384 &ci->ci_ev_isi, xname, "user ISI failures");
385 evcnt_attach_dynamic_nozero(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
386 &ci->ci_ev_traps, xname, "system call traps");
387 evcnt_attach_dynamic_nozero(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
388 &ci->ci_ev_traps, xname, "PGM traps");
389 evcnt_attach_dynamic_nozero(&ci->ci_ev_debug, EVCNT_TYPE_TRAP,
390 &ci->ci_ev_traps, xname, "debug traps");
391 evcnt_attach_dynamic_nozero(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
392 &ci->ci_ev_traps, xname, "FPU unavailable traps");
393 evcnt_attach_dynamic_nozero(&ci->ci_ev_fpusw, EVCNT_TYPE_MISC,
394 &ci->ci_ev_fpu, xname, "FPU context switches");
395 evcnt_attach_dynamic_nozero(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
396 &ci->ci_ev_traps, xname, "user alignment traps");
397 evcnt_attach_dynamic_nozero(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
398 &ci->ci_ev_ali, xname, "user alignment traps");
399 evcnt_attach_dynamic_nozero(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
400 &ci->ci_ev_umchk, xname, "user MCHK failures");
401 evcnt_attach_dynamic_nozero(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
402 &ci->ci_ev_traps, xname, "SPE unavailable");
403 evcnt_attach_dynamic_nozero(&ci->ci_ev_vecsw, EVCNT_TYPE_MISC,
404 &ci->ci_ev_vec, xname, "SPE context switches");
405 evcnt_attach_dynamic_nozero(&ci->ci_ev_ipi, EVCNT_TYPE_INTR,
406 NULL, xname, "IPIs");
407 evcnt_attach_dynamic_nozero(&ci->ci_ev_tlbmiss_soft, EVCNT_TYPE_TRAP,
408 &ci->ci_ev_traps, xname, "soft tlb misses");
409 evcnt_attach_dynamic_nozero(&ci->ci_ev_dtlbmiss_hard, EVCNT_TYPE_TRAP,
410 &ci->ci_ev_traps, xname, "data tlb misses");
411 evcnt_attach_dynamic_nozero(&ci->ci_ev_itlbmiss_hard, EVCNT_TYPE_TRAP,
412 &ci->ci_ev_traps, xname, "inst tlb misses");
413 }
414
415 uint32_t
416 cpu_read_4(bus_addr_t a)
417 {
418 struct cpu_softc * const cpu = curcpu()->ci_softc;
419 // printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a);
420 return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh, a);
421 }
422
423 uint8_t
424 cpu_read_1(bus_addr_t a)
425 {
426 struct cpu_softc * const cpu = curcpu()->ci_softc;
427 // printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a);
428 return bus_space_read_1(cpu->cpu_bst, cpu->cpu_bsh, a);
429 }
430
431 void
432 cpu_write_4(bus_addr_t a, uint32_t v)
433 {
434 struct cpu_softc * const cpu = curcpu()->ci_softc;
435 bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh, a, v);
436 }
437
438 void
439 cpu_write_1(bus_addr_t a, uint8_t v)
440 {
441 struct cpu_softc * const cpu = curcpu()->ci_softc;
442 bus_space_write_1(cpu->cpu_bst, cpu->cpu_bsh, a, v);
443 }
444
445 void
446 booke_sstep(struct trapframe *tf)
447 {
448 KASSERT(tf->tf_srr1 & PSL_DE);
449 const uint32_t insn = ufetch_32((const void *)tf->tf_srr0);
450 register_t dbcr0 = DBCR0_IAC1 | DBCR0_IDM;
451 register_t dbcr1 = DBCR1_IAC1US_USER | DBCR1_IAC1ER_DS1;
452 if ((insn >> 28) == 4) {
453 uint32_t iac2 = 0;
454 if ((insn >> 26) == 0x12) {
455 const int32_t off = (((int32_t)insn << 6) >> 6) & ~3;
456 iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off;
457 dbcr0 |= DBCR0_IAC2;
458 } else if ((insn >> 26) == 0x10) {
459 const int16_t off = insn & ~3;
460 iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off;
461 dbcr0 |= DBCR0_IAC2;
462 } else if ((insn & 0xfc00ffde) == 0x4c000420) {
463 iac2 = tf->tf_ctr;
464 dbcr0 |= DBCR0_IAC2;
465 } else if ((insn & 0xfc00ffde) == 0x4c000020) {
466 iac2 = tf->tf_lr;
467 dbcr0 |= DBCR0_IAC2;
468 }
469 if (dbcr0 & DBCR0_IAC2) {
470 dbcr1 |= DBCR1_IAC2US_USER | DBCR1_IAC2ER_DS1;
471 mtspr(SPR_IAC2, iac2);
472 }
473 }
474 mtspr(SPR_IAC1, tf->tf_srr0 + 4);
475 mtspr(SPR_DBCR1, dbcr1);
476 mtspr(SPR_DBCR0, dbcr0);
477 }
478