Home | History | Annotate | Line # | Download | only in booke
booke_machdep.c revision 1.21.2.3
      1  1.21.2.3     skrll /*	$NetBSD: booke_machdep.c,v 1.21.2.3 2017/02/05 13:40:18 skrll Exp $	*/
      2       1.2      matt /*-
      3       1.2      matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4       1.2      matt  * All rights reserved.
      5       1.2      matt  *
      6       1.2      matt  * This code is derived from software contributed to The NetBSD Foundation
      7       1.2      matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8       1.2      matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9       1.2      matt  *
     10       1.2      matt  * This material is based upon work supported by the Defense Advanced Research
     11       1.2      matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12       1.2      matt  * Contract No. N66001-09-C-2073.
     13       1.2      matt  * Approved for Public Release, Distribution Unlimited
     14       1.2      matt  *
     15       1.2      matt  * Redistribution and use in source and binary forms, with or without
     16       1.2      matt  * modification, are permitted provided that the following conditions
     17       1.2      matt  * are met:
     18       1.2      matt  * 1. Redistributions of source code must retain the above copyright
     19       1.2      matt  *    notice, this list of conditions and the following disclaimer.
     20       1.2      matt  * 2. Redistributions in binary form must reproduce the above copyright
     21       1.2      matt  *    notice, this list of conditions and the following disclaimer in the
     22       1.2      matt  *    documentation and/or other materials provided with the distribution.
     23       1.2      matt  *
     24       1.2      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25       1.2      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26       1.2      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27       1.2      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28       1.2      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29       1.2      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30       1.2      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31       1.2      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32       1.2      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33       1.2      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34       1.2      matt  * POSSIBILITY OF SUCH DAMAGE.
     35       1.2      matt  */
     36       1.2      matt 
     37       1.2      matt #define	__INTR_PRIVATE
     38       1.2      matt #define	_POWERPC_BUS_DMA_PRIVATE
     39       1.2      matt 
     40       1.2      matt #include <sys/cdefs.h>
     41  1.21.2.3     skrll __KERNEL_RCSID(0, "$NetBSD: booke_machdep.c,v 1.21.2.3 2017/02/05 13:40:18 skrll Exp $");
     42       1.2      matt 
     43       1.9      matt #include "opt_modular.h"
     44       1.9      matt 
     45       1.2      matt #include <sys/param.h>
     46       1.2      matt #include <sys/cpu.h>
     47       1.2      matt #include <sys/device.h>
     48       1.2      matt #include <sys/intr.h>
     49       1.2      matt #include <sys/mount.h>
     50       1.2      matt #include <sys/msgbuf.h>
     51       1.2      matt #include <sys/kernel.h>
     52       1.2      matt #include <sys/reboot.h>
     53       1.2      matt #include <sys/bus.h>
     54      1.20  christos #include <sys/cpu.h>
     55       1.2      matt 
     56       1.2      matt #include <uvm/uvm_extern.h>
     57       1.2      matt 
     58  1.21.2.3     skrll #include <dev/cons.h>
     59  1.21.2.3     skrll 
     60      1.12      matt #include <powerpc/pcb.h>
     61       1.4      matt #include <powerpc/spr.h>
     62       1.4      matt #include <powerpc/booke/spr.h>
     63       1.4      matt #include <powerpc/booke/cpuvar.h>
     64       1.2      matt 
     65       1.2      matt /*
     66       1.2      matt  * Global variables used here and there
     67       1.2      matt  */
     68       1.2      matt paddr_t msgbuf_paddr;
     69       1.2      matt psize_t pmemsize;
     70       1.2      matt struct vm_map *phys_map;
     71       1.2      matt 
     72       1.9      matt #ifdef MODULAR
     73       1.9      matt register_t cpu_psluserset = PSL_USERSET;
     74       1.9      matt register_t cpu_pslusermod = PSL_USERMOD;
     75       1.9      matt register_t cpu_pslusermask = PSL_USERMASK;
     76       1.9      matt #endif
     77       1.9      matt 
     78       1.2      matt static bus_addr_t booke_dma_phys_to_bus_mem(bus_dma_tag_t, bus_addr_t);
     79       1.2      matt static bus_addr_t booke_dma_bus_mem_to_phys(bus_dma_tag_t, bus_addr_t);
     80       1.2      matt 
     81       1.2      matt 
     82       1.2      matt struct powerpc_bus_dma_tag booke_bus_dma_tag = {
     83       1.2      matt 	._dmamap_create = _bus_dmamap_create,
     84       1.2      matt 	._dmamap_destroy = _bus_dmamap_destroy,
     85       1.2      matt 	._dmamap_load = _bus_dmamap_load,
     86       1.2      matt 	._dmamap_load_mbuf = _bus_dmamap_load_mbuf,
     87       1.2      matt 	._dmamap_load_uio = _bus_dmamap_load_uio,
     88       1.2      matt 	._dmamap_load_raw = _bus_dmamap_load_raw,
     89       1.2      matt 	._dmamap_unload = _bus_dmamap_unload,
     90      1.17      matt 	/*
     91      1.17      matt 	 * The caches on BookE are coherent so we don't need to do any special
     92      1.17      matt 	 * cache synchronization.
     93      1.17      matt 	 */
     94      1.17      matt 	//._dmamap_sync = _bus_dmamap_sync,
     95       1.2      matt 	._dmamem_alloc = _bus_dmamem_alloc,
     96       1.2      matt 	._dmamem_free = _bus_dmamem_free,
     97       1.2      matt 	._dmamem_map = _bus_dmamem_map,
     98       1.2      matt 	._dmamem_unmap = _bus_dmamem_unmap,
     99       1.2      matt 	._dmamem_mmap = _bus_dmamem_mmap,
    100       1.2      matt 	._dma_phys_to_bus_mem = booke_dma_phys_to_bus_mem,
    101       1.2      matt 	._dma_bus_mem_to_phys = booke_dma_bus_mem_to_phys,
    102       1.2      matt };
    103       1.2      matt 
    104       1.2      matt static bus_addr_t
    105       1.2      matt booke_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t a)
    106       1.2      matt {
    107       1.2      matt 	return a;
    108       1.2      matt }
    109       1.2      matt 
    110       1.2      matt static bus_addr_t
    111       1.2      matt booke_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t a)
    112       1.2      matt {
    113       1.2      matt 	return a;
    114       1.2      matt }
    115       1.2      matt 
    116       1.2      matt struct cpu_md_ops cpu_md_ops;
    117       1.2      matt 
    118       1.6      matt struct cpu_softc cpu_softc[] = {
    119       1.2      matt 	[0] = {
    120       1.6      matt 		.cpu_ci = &cpu_info[0],
    121       1.6      matt 	},
    122       1.6      matt #ifdef MULTIPROCESSOR
    123       1.6      matt 	[CPU_MAXNUM-1] = {
    124       1.6      matt 		.cpu_ci = &cpu_info[CPU_MAXNUM-1],
    125       1.2      matt 	},
    126       1.6      matt #endif
    127       1.2      matt };
    128       1.6      matt struct cpu_info cpu_info[] = {
    129       1.2      matt 	[0] = {
    130       1.2      matt 		.ci_curlwp = &lwp0,
    131       1.2      matt 		.ci_tlb_info = &pmap_tlb0_info,
    132       1.6      matt 		.ci_softc = &cpu_softc[0],
    133       1.6      matt 		.ci_cpl = IPL_HIGH,
    134      1.12      matt 		.ci_idepth = -1,
    135  1.21.2.2     skrll 		.ci_pmap_kern_segtab = &pmap_kern_segtab,
    136       1.6      matt 	},
    137       1.6      matt #ifdef MULTIPROCESSOR
    138       1.6      matt 	[CPU_MAXNUM-1] = {
    139       1.6      matt 		.ci_curlwp = NULL,
    140       1.6      matt 		.ci_tlb_info = &pmap_tlb0_info,
    141       1.6      matt 		.ci_softc = &cpu_softc[CPU_MAXNUM-1],
    142       1.2      matt 		.ci_cpl = IPL_HIGH,
    143      1.12      matt 		.ci_idepth = -1,
    144  1.21.2.2     skrll 		.ci_pmap_kern_segtab = &pmap_kern_segtab,
    145       1.6      matt 	},
    146       1.2      matt #endif
    147       1.2      matt };
    148      1.12      matt __CTASSERT(__arraycount(cpu_info) == __arraycount(cpu_softc));
    149       1.2      matt 
    150       1.2      matt /*
    151       1.2      matt  * This should probably be in autoconf!				XXX
    152       1.2      matt  */
    153       1.2      matt char machine[] = MACHINE;		/* from <machine/param.h> */
    154       1.2      matt char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    155       1.2      matt 
    156       1.2      matt char bootpath[256];
    157       1.2      matt 
    158       1.2      matt #if NKSYMS || defined(DDB) || defined(MODULAR)
    159       1.2      matt void *startsym, *endsym;
    160       1.2      matt #endif
    161       1.2      matt 
    162      1.12      matt #if defined(MULTIPROCESSOR)
    163      1.12      matt volatile struct cpu_hatch_data cpu_hatch_data __cacheline_aligned;
    164      1.12      matt #endif
    165      1.12      matt 
    166       1.2      matt int fake_mapiodev = 1;
    167       1.2      matt 
    168       1.2      matt void
    169       1.2      matt booke_cpu_startup(const char *model)
    170       1.2      matt {
    171       1.2      matt 	vaddr_t 	minaddr, maxaddr;
    172       1.2      matt 	char 		pbuf[9];
    173       1.2      matt 
    174      1.20  christos 	cpu_setmodel("%s", model);
    175       1.2      matt 
    176       1.2      matt 	printf("%s%s", copyright, version);
    177       1.2      matt 
    178       1.5      matt 	format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem));
    179       1.2      matt 	printf("total memory = %s\n", pbuf);
    180       1.2      matt 
    181       1.2      matt 	minaddr = 0;
    182       1.2      matt 	/*
    183       1.2      matt 	 * Allocate a submap for physio
    184       1.2      matt 	 */
    185       1.2      matt 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    186       1.2      matt 				 VM_PHYS_SIZE, 0, false, NULL);
    187       1.2      matt 
    188       1.2      matt 	/*
    189       1.2      matt 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    190       1.2      matt 	 * are allocated via the pool allocator, and we use direct-mapped
    191       1.2      matt 	 * pool pages.
    192       1.2      matt 	 */
    193       1.2      matt 
    194       1.2      matt 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    195       1.2      matt 	printf("avail memory = %s\n", pbuf);
    196       1.2      matt 
    197       1.2      matt 	/*
    198      1.11      matt 	 * Register the tlb's evcnts
    199      1.11      matt 	 */
    200      1.11      matt 	pmap_tlb_info_evcnt_attach(curcpu()->ci_tlb_info);
    201      1.11      matt 
    202      1.11      matt 	/*
    203       1.2      matt 	 * Set up the board properties database.
    204       1.2      matt 	 */
    205       1.2      matt 	board_info_init();
    206       1.2      matt 
    207       1.2      matt 	/*
    208       1.2      matt 	 * Now that we have VM, malloc()s are OK in bus_space.
    209       1.2      matt 	 */
    210       1.2      matt 	bus_space_mallocok();
    211       1.2      matt 	fake_mapiodev = 0;
    212      1.12      matt 
    213      1.12      matt #ifdef MULTIPROCESSOR
    214  1.21.2.1     skrll 	pmap_kernel()->pm_active = kcpuset_running;
    215  1.21.2.1     skrll 	pmap_kernel()->pm_onproc = kcpuset_running;
    216  1.21.2.1     skrll 
    217      1.12      matt 	for (size_t i = 1; i < __arraycount(cpu_info); i++) {
    218      1.12      matt 		struct cpu_info * const ci = &cpu_info[i];
    219      1.12      matt 		struct cpu_softc * const cpu = &cpu_softc[i];
    220      1.12      matt 		cpu->cpu_ci = ci;
    221      1.12      matt 		cpu->cpu_bst = cpu_softc[0].cpu_bst;
    222      1.12      matt 		cpu->cpu_le_bst = cpu_softc[0].cpu_le_bst;
    223      1.12      matt 		cpu->cpu_bsh = cpu_softc[0].cpu_bsh;
    224      1.12      matt 		cpu->cpu_highmem = cpu_softc[0].cpu_highmem;
    225      1.12      matt 		ci->ci_softc = cpu;
    226      1.12      matt 		ci->ci_tlb_info = &pmap_tlb0_info;
    227      1.12      matt 		ci->ci_cpl = IPL_HIGH;
    228      1.12      matt 		ci->ci_idepth = -1;
    229      1.12      matt 		ci->ci_pmap_kern_segtab = curcpu()->ci_pmap_kern_segtab;
    230      1.12      matt 	}
    231      1.18      matt 
    232      1.18      matt 	kcpuset_create(&cpuset_info.cpus_running, true);
    233      1.18      matt 	kcpuset_create(&cpuset_info.cpus_hatched, true);
    234      1.18      matt 	kcpuset_create(&cpuset_info.cpus_paused, true);
    235      1.18      matt 	kcpuset_create(&cpuset_info.cpus_resumed, true);
    236      1.18      matt 	kcpuset_create(&cpuset_info.cpus_halted, true);
    237  1.21.2.1     skrll 
    238  1.21.2.1     skrll 	kcpuset_set(cpuset_info.cpus_running, cpu_number());
    239      1.12      matt #endif /* MULTIPROCESSOR */
    240       1.2      matt }
    241       1.2      matt 
    242       1.2      matt static void
    243       1.2      matt dumpsys(void)
    244       1.2      matt {
    245       1.2      matt 
    246       1.2      matt 	printf("dumpsys: TBD\n");
    247       1.2      matt }
    248       1.2      matt 
    249       1.2      matt /*
    250       1.2      matt  * Halt or reboot the machine after syncing/dumping according to howto.
    251       1.2      matt  */
    252       1.2      matt void
    253       1.2      matt cpu_reboot(int howto, char *what)
    254       1.2      matt {
    255       1.2      matt 	static int syncing;
    256       1.2      matt 	static char str[256];
    257       1.2      matt 	char *ap = str, *ap1 = ap;
    258       1.2      matt 
    259       1.2      matt 	boothowto = howto;
    260       1.2      matt 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    261       1.2      matt 		syncing = 1;
    262       1.2      matt 		vfs_shutdown();		/* sync */
    263       1.2      matt 		resettodr();		/* set wall clock */
    264       1.2      matt 	}
    265       1.2      matt 
    266       1.2      matt 	splhigh();
    267       1.2      matt 
    268       1.2      matt 	if (!cold && (howto & RB_DUMP))
    269       1.2      matt 		dumpsys();
    270       1.2      matt 
    271       1.2      matt 	doshutdownhooks();
    272       1.2      matt 
    273       1.2      matt 	pmf_system_shutdown(boothowto);
    274       1.2      matt 
    275       1.2      matt 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    276       1.2      matt 	  /* Power off here if we know how...*/
    277       1.2      matt 	}
    278       1.2      matt 
    279       1.2      matt 	if (howto & RB_HALT) {
    280  1.21.2.3     skrll 		printf("The operating system has halted.\n"
    281  1.21.2.3     skrll 		    "Press any key to reboot.\n\n");
    282  1.21.2.3     skrll 		cnpollc(1);	/* For proper keyboard command handling */
    283  1.21.2.3     skrll 		cngetc();
    284  1.21.2.3     skrll 		cnpollc(0);
    285       1.2      matt 
    286  1.21.2.3     skrll 		printf("rebooting...\n\n");
    287       1.2      matt 		goto reboot;	/* XXX for now... */
    288       1.2      matt 
    289       1.2      matt #ifdef DDB
    290       1.2      matt 		printf("dropping to debugger\n");
    291       1.2      matt 		while(1)
    292       1.2      matt 			Debugger();
    293       1.2      matt #endif
    294       1.2      matt 	}
    295       1.2      matt 
    296       1.2      matt 	printf("rebooting\n\n");
    297       1.2      matt 	if (what && *what) {
    298       1.2      matt 		if (strlen(what) > sizeof str - 5)
    299       1.2      matt 			printf("boot string too large, ignored\n");
    300       1.2      matt 		else {
    301       1.2      matt 			strcpy(str, what);
    302       1.2      matt 			ap1 = ap = str + strlen(str);
    303       1.2      matt 			*ap++ = ' ';
    304       1.2      matt 		}
    305       1.2      matt 	}
    306       1.2      matt 	*ap++ = '-';
    307       1.2      matt 	if (howto & RB_SINGLE)
    308       1.2      matt 		*ap++ = 's';
    309       1.2      matt 	if (howto & RB_KDB)
    310       1.2      matt 		*ap++ = 'd';
    311       1.2      matt 	*ap++ = 0;
    312       1.2      matt 	if (ap[-2] == '-')
    313       1.2      matt 		*ap1 = 0;
    314       1.2      matt 
    315       1.2      matt 	/* flush cache for msgbuf */
    316       1.2      matt 	dcache_wb(msgbuf_paddr, round_page(MSGBUFSIZE));
    317       1.2      matt 
    318       1.2      matt  reboot:
    319       1.2      matt 	__asm volatile("msync; isync");
    320       1.2      matt 	(*cpu_md_ops.md_cpu_reset)();
    321       1.2      matt 
    322       1.2      matt 	printf("%s: md_cpu_reset() failed!\n", __func__);
    323       1.2      matt #ifdef DDB
    324       1.2      matt 	for (;;)
    325       1.2      matt 		Debugger();
    326       1.2      matt #else
    327       1.2      matt 	for (;;)
    328       1.2      matt 		/* nothing */;
    329       1.2      matt #endif
    330       1.2      matt }
    331       1.2      matt 
    332       1.2      matt /*
    333       1.2      matt  * mapiodev:
    334       1.2      matt  *
    335       1.2      matt  * 	Allocate vm space and mapin the I/O address. Use reserved TLB
    336       1.2      matt  * 	mapping if one is found.
    337       1.2      matt  */
    338       1.2      matt void *
    339      1.14      matt mapiodev(paddr_t pa, psize_t len, bool prefetchable)
    340       1.2      matt {
    341       1.2      matt 	const vsize_t off = pa & PAGE_MASK;
    342       1.2      matt 
    343       1.2      matt 	/*
    344       1.2      matt 	 * See if we have reserved TLB entry for the pa. This needs to be
    345       1.2      matt 	 * true for console as we can't use uvm during early bootstrap.
    346       1.2      matt 	 */
    347      1.14      matt 	void * const p = tlb_mapiodev(pa, len, prefetchable);
    348       1.2      matt 	if (p != NULL)
    349       1.2      matt 		return p;
    350       1.2      matt 
    351       1.2      matt 	if (fake_mapiodev)
    352       1.2      matt 		panic("mapiodev: no TLB entry reserved for %llx+%llx",
    353       1.2      matt 		    (long long)pa, (long long)len);
    354       1.2      matt 
    355      1.15      matt 	const paddr_t orig_pa = pa;
    356      1.15      matt 	const psize_t orig_len = len;
    357      1.15      matt 	vsize_t align = 0;
    358       1.2      matt 	pa = trunc_page(pa);
    359       1.2      matt 	len = round_page(off + len);
    360      1.15      matt 	/*
    361      1.15      matt 	 * If we are allocating a large amount (>= 1MB) try to get an
    362      1.15      matt 	 * aligned VA region for it so try to do a large mapping for it.
    363      1.15      matt 	 */
    364      1.15      matt 	if ((len & (len - 1)) == 0 && len >= 0x100000)
    365      1.15      matt 		align = len;
    366      1.15      matt 
    367      1.15      matt 	vaddr_t va = uvm_km_alloc(kernel_map, len, align, UVM_KMF_VAONLY);
    368       1.2      matt 
    369      1.15      matt 	if (va == 0 && align > 0) {
    370      1.15      matt 		/*
    371      1.15      matt 		 * Large aligned request failed.  Let's just get anything.
    372      1.15      matt 		 */
    373      1.15      matt 		align = 0;
    374      1.15      matt 		va = uvm_km_alloc(kernel_map, len, align, UVM_KMF_VAONLY);
    375      1.15      matt 	}
    376       1.2      matt 	if (va == 0)
    377       1.2      matt 		return NULL;
    378       1.2      matt 
    379      1.15      matt 	if (align) {
    380      1.15      matt 		/*
    381      1.15      matt 		 * Now try to map that via one big TLB entry.
    382      1.15      matt 		 */
    383      1.15      matt 		pt_entry_t pte = pte_make_kenter_pa(pa, NULL,
    384      1.15      matt 		    VM_PROT_READ|VM_PROT_WRITE,
    385      1.15      matt 		    prefetchable ? 0 : PMAP_NOCACHE);
    386      1.15      matt 		if (!tlb_ioreserve(va, len, pte)) {
    387      1.15      matt 			void * const p0 = tlb_mapiodev(orig_pa, orig_len,
    388      1.15      matt 			    prefetchable);
    389      1.15      matt 			KASSERT(p0 != NULL);
    390      1.15      matt 			return p0;
    391      1.15      matt 		}
    392      1.15      matt 	}
    393      1.15      matt 
    394       1.2      matt 	for (va += len, pa += len; len > 0; len -= PAGE_SIZE) {
    395       1.2      matt 		va -= PAGE_SIZE;
    396       1.2      matt 		pa -= PAGE_SIZE;
    397       1.2      matt 		pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE,
    398      1.14      matt 		    prefetchable ? 0 : PMAP_NOCACHE);
    399       1.2      matt 	}
    400       1.2      matt 	pmap_update(pmap_kernel());
    401       1.2      matt 	return (void *)(va + off);
    402       1.2      matt }
    403       1.2      matt 
    404       1.2      matt void
    405       1.2      matt unmapiodev(vaddr_t va, vsize_t len)
    406       1.2      matt {
    407       1.2      matt 	/* Nothing to do for reserved (ie. not uvm_km_alloc'd) mappings. */
    408       1.2      matt 	if (va < VM_MIN_KERNEL_ADDRESS || va > VM_MAX_KERNEL_ADDRESS) {
    409       1.2      matt 		tlb_unmapiodev(va, len);
    410       1.2      matt 		return;
    411       1.2      matt 	}
    412       1.2      matt 
    413       1.2      matt 	len = round_page((va & PAGE_MASK) + len);
    414       1.2      matt 	va = trunc_page(va);
    415       1.2      matt 
    416       1.2      matt 	pmap_kremove(va, len);
    417       1.2      matt 	uvm_km_free(kernel_map, va, len, UVM_KMF_VAONLY);
    418       1.2      matt }
    419       1.2      matt 
    420       1.2      matt void
    421       1.2      matt cpu_evcnt_attach(struct cpu_info *ci)
    422       1.2      matt {
    423       1.2      matt 	struct cpu_softc * const cpu = ci->ci_softc;
    424      1.12      matt 	const char * const xname = ci->ci_data.cpu_name;
    425       1.2      matt 
    426       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_clock, EVCNT_TYPE_INTR,
    427       1.2      matt 		NULL, xname, "clock");
    428       1.2      matt 	evcnt_attach_dynamic_nozero(&cpu->cpu_ev_late_clock, EVCNT_TYPE_INTR,
    429       1.2      matt 		NULL, xname, "late clock");
    430       1.2      matt 	evcnt_attach_dynamic_nozero(&cpu->cpu_ev_exec_trap_sync, EVCNT_TYPE_TRAP,
    431       1.2      matt 		NULL, xname, "exec pages synced (trap)");
    432       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_traps, EVCNT_TYPE_TRAP,
    433       1.2      matt 		NULL, xname, "traps");
    434       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_kdsi, EVCNT_TYPE_TRAP,
    435       1.2      matt 		&ci->ci_ev_traps, xname, "kernel DSI traps");
    436       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi, EVCNT_TYPE_TRAP,
    437       1.2      matt 		&ci->ci_ev_traps, xname, "user DSI traps");
    438       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_udsi_fatal, EVCNT_TYPE_TRAP,
    439       1.2      matt 		&ci->ci_ev_udsi, xname, "user DSI failures");
    440       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_kisi, EVCNT_TYPE_TRAP,
    441       1.2      matt 		&ci->ci_ev_traps, xname, "kernel ISI traps");
    442       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_isi, EVCNT_TYPE_TRAP,
    443       1.2      matt 		&ci->ci_ev_traps, xname, "user ISI traps");
    444       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_isi_fatal, EVCNT_TYPE_TRAP,
    445       1.2      matt 		&ci->ci_ev_isi, xname, "user ISI failures");
    446       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_scalls, EVCNT_TYPE_TRAP,
    447       1.2      matt 		&ci->ci_ev_traps, xname, "system call traps");
    448       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_pgm, EVCNT_TYPE_TRAP,
    449       1.2      matt 		&ci->ci_ev_traps, xname, "PGM traps");
    450       1.3      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_debug, EVCNT_TYPE_TRAP,
    451       1.3      matt 		&ci->ci_ev_traps, xname, "debug traps");
    452       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_fpu, EVCNT_TYPE_TRAP,
    453       1.2      matt 		&ci->ci_ev_traps, xname, "FPU unavailable traps");
    454       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_fpusw, EVCNT_TYPE_MISC,
    455       1.2      matt 		&ci->ci_ev_fpu, xname, "FPU context switches");
    456       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_ali, EVCNT_TYPE_TRAP,
    457       1.2      matt 		&ci->ci_ev_traps, xname, "user alignment traps");
    458       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_ali_fatal, EVCNT_TYPE_TRAP,
    459       1.2      matt 		&ci->ci_ev_ali, xname, "user alignment traps");
    460       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_umchk, EVCNT_TYPE_TRAP,
    461       1.2      matt 		&ci->ci_ev_umchk, xname, "user MCHK failures");
    462       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_vec, EVCNT_TYPE_TRAP,
    463       1.2      matt 		&ci->ci_ev_traps, xname, "SPE unavailable");
    464       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_vecsw, EVCNT_TYPE_MISC,
    465       1.2      matt 	    &ci->ci_ev_vec, xname, "SPE context switches");
    466       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_ipi, EVCNT_TYPE_INTR,
    467       1.2      matt 		NULL, xname, "IPIs");
    468       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_tlbmiss_soft, EVCNT_TYPE_TRAP,
    469       1.2      matt 		&ci->ci_ev_traps, xname, "soft tlb misses");
    470       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_dtlbmiss_hard, EVCNT_TYPE_TRAP,
    471       1.2      matt 		&ci->ci_ev_traps, xname, "data tlb misses");
    472       1.2      matt 	evcnt_attach_dynamic_nozero(&ci->ci_ev_itlbmiss_hard, EVCNT_TYPE_TRAP,
    473       1.2      matt 		&ci->ci_ev_traps, xname, "inst tlb misses");
    474       1.2      matt }
    475       1.2      matt 
    476      1.12      matt #ifdef MULTIPROCESSOR
    477      1.12      matt register_t
    478      1.12      matt cpu_hatch(void)
    479      1.12      matt {
    480      1.18      matt 	struct cpuset_info * const csi = &cpuset_info;
    481      1.12      matt 	const size_t id = cpu_number();
    482      1.12      matt 
    483      1.12      matt 	/*
    484      1.12      matt 	 * We've hatched so tell the spinup code.
    485      1.12      matt 	 */
    486      1.18      matt 	kcpuset_set(csi->cpus_hatched, id);
    487      1.12      matt 
    488      1.12      matt 	/*
    489      1.12      matt 	 * Loop until running bit for this cpu is set.
    490      1.12      matt 	 */
    491      1.18      matt 	while (!kcpuset_isset(csi->cpus_running, id)) {
    492      1.12      matt 		continue;
    493      1.12      matt 	}
    494      1.12      matt 
    495      1.12      matt 	/*
    496      1.12      matt 	 * Now that we are active, start the clocks.
    497      1.12      matt 	 */
    498      1.12      matt 	cpu_initclocks();
    499      1.12      matt 
    500      1.12      matt 	/*
    501      1.12      matt 	 * Return sp of the idlelwp.  Which we should be already using but ...
    502      1.12      matt 	 */
    503      1.12      matt 	return curcpu()->ci_curpcb->pcb_sp;
    504      1.12      matt }
    505      1.12      matt 
    506      1.12      matt void
    507      1.12      matt cpu_boot_secondary_processors(void)
    508      1.12      matt {
    509      1.12      matt 	volatile struct cpuset_info * const csi = &cpuset_info;
    510      1.12      matt 	CPU_INFO_ITERATOR cii;
    511      1.12      matt 	struct cpu_info *ci;
    512      1.18      matt 	kcpuset_t *running;
    513      1.18      matt 
    514      1.18      matt 	kcpuset_create(&running, true);
    515      1.12      matt 
    516      1.12      matt 	for (CPU_INFO_FOREACH(cii, ci)) {
    517      1.12      matt 		/*
    518      1.12      matt 		 * Skip this CPU if it didn't sucessfully hatch.
    519      1.12      matt 		 */
    520      1.18      matt 		if (!kcpuset_isset(csi->cpus_hatched, cpu_index(ci)))
    521      1.12      matt 			continue;
    522      1.12      matt 
    523      1.12      matt 		KASSERT(!CPU_IS_PRIMARY(ci));
    524      1.12      matt 		KASSERT(ci->ci_data.cpu_idlelwp);
    525      1.12      matt 
    526      1.18      matt 		kcpuset_set(running, cpu_index(ci));
    527      1.12      matt 	}
    528      1.18      matt 	KASSERT(kcpuset_match(csi->cpus_hatched, running));
    529      1.18      matt 	if (!kcpuset_iszero(running)) {
    530      1.18      matt 		kcpuset_merge(csi->cpus_running, running);
    531      1.12      matt 	}
    532      1.18      matt 	kcpuset_destroy(running);
    533      1.12      matt }
    534      1.12      matt #endif
    535      1.12      matt 
    536       1.2      matt uint32_t
    537       1.2      matt cpu_read_4(bus_addr_t a)
    538       1.2      matt {
    539       1.2      matt 	struct cpu_softc * const cpu = curcpu()->ci_softc;
    540       1.2      matt //	printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a);
    541       1.2      matt 	return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh, a);
    542       1.2      matt }
    543       1.2      matt 
    544       1.2      matt uint8_t
    545       1.2      matt cpu_read_1(bus_addr_t a)
    546       1.2      matt {
    547       1.2      matt 	struct cpu_softc * const cpu = curcpu()->ci_softc;
    548       1.2      matt //	printf(" %s(%p, %x, %x)", __func__, cpu->cpu_bst, cpu->cpu_bsh, a);
    549       1.2      matt 	return bus_space_read_1(cpu->cpu_bst, cpu->cpu_bsh, a);
    550       1.2      matt }
    551       1.2      matt 
    552       1.2      matt void
    553       1.2      matt cpu_write_4(bus_addr_t a, uint32_t v)
    554       1.2      matt {
    555       1.2      matt 	struct cpu_softc * const cpu = curcpu()->ci_softc;
    556       1.2      matt 	bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh, a, v);
    557       1.2      matt }
    558       1.2      matt 
    559       1.2      matt void
    560       1.2      matt cpu_write_1(bus_addr_t a, uint8_t v)
    561       1.2      matt {
    562       1.2      matt 	struct cpu_softc * const cpu = curcpu()->ci_softc;
    563       1.2      matt 	bus_space_write_1(cpu->cpu_bst, cpu->cpu_bsh, a, v);
    564       1.2      matt }
    565       1.4      matt 
    566       1.4      matt void
    567       1.4      matt booke_sstep(struct trapframe *tf)
    568       1.4      matt {
    569       1.4      matt 	KASSERT(tf->tf_srr1 & PSL_DE);
    570       1.4      matt 	const uint32_t insn = ufetch_32((const void *)tf->tf_srr0);
    571       1.4      matt 	register_t dbcr0 = DBCR0_IAC1 | DBCR0_IDM;
    572       1.4      matt 	register_t dbcr1 = DBCR1_IAC1US_USER | DBCR1_IAC1ER_DS1;
    573       1.4      matt 	if ((insn >> 28) == 4) {
    574       1.4      matt 		uint32_t iac2 = 0;
    575       1.4      matt 		if ((insn >> 26) == 0x12) {
    576       1.4      matt 			const int32_t off = (((int32_t)insn << 6) >> 6) & ~3;
    577       1.4      matt 			iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off;
    578       1.4      matt 			dbcr0 |= DBCR0_IAC2;
    579       1.4      matt 		} else if ((insn >> 26) == 0x10) {
    580       1.4      matt 			const int16_t off = insn & ~3;
    581       1.4      matt 			iac2 = ((insn & 2) ? 0 : tf->tf_srr0) + off;
    582       1.4      matt 			dbcr0 |= DBCR0_IAC2;
    583      1.21      matt 		} else if ((insn & 0xfc00fffe) == 0x4c000420) {
    584       1.4      matt 			iac2 = tf->tf_ctr;
    585       1.4      matt 			dbcr0 |= DBCR0_IAC2;
    586      1.21      matt 		} else if ((insn & 0xfc00fffe) == 0x4c000020) {
    587       1.4      matt 			iac2 = tf->tf_lr;
    588       1.4      matt 			dbcr0 |= DBCR0_IAC2;
    589       1.4      matt 		}
    590       1.4      matt 		if (dbcr0 & DBCR0_IAC2) {
    591       1.4      matt 			dbcr1 |= DBCR1_IAC2US_USER | DBCR1_IAC2ER_DS1;
    592       1.4      matt 			mtspr(SPR_IAC2, iac2);
    593       1.4      matt 		}
    594       1.4      matt 	}
    595       1.4      matt 	mtspr(SPR_IAC1, tf->tf_srr0 + 4);
    596       1.4      matt 	mtspr(SPR_DBCR1, dbcr1);
    597       1.4      matt 	mtspr(SPR_DBCR0, dbcr0);
    598       1.4      matt }
    599      1.16      matt 
    600      1.16      matt #ifdef DIAGNOSTIC
    601      1.16      matt static inline void
    602      1.16      matt swap_data(uint64_t *data, size_t a, size_t b)
    603      1.16      matt {
    604      1.16      matt 	uint64_t swap = data[a];
    605      1.16      matt 	data[a] = data[b];
    606      1.16      matt 	data[b] = swap;
    607      1.16      matt }
    608      1.16      matt 
    609      1.16      matt static void
    610      1.16      matt sort_data(uint64_t *data, size_t count)
    611      1.16      matt {
    612      1.16      matt #if 0
    613      1.16      matt 	/*
    614      1.16      matt 	 * Mostly classic bubble sort
    615      1.16      matt 	 */
    616      1.16      matt 	do {
    617      1.16      matt 		size_t new_count = 0;
    618      1.16      matt 		for (size_t i = 1; i < count; i++) {
    619      1.16      matt 			if (tbs[i - 1] > tbs[i]) {
    620      1.16      matt 				swap_tbs(tbs, i - 1, i);
    621      1.16      matt 				new_count = i;
    622      1.16      matt 			}
    623      1.16      matt 		}
    624      1.16      matt 		count = new_count;
    625      1.16      matt 	} while (count > 0);
    626      1.16      matt #else
    627      1.16      matt 	/*
    628      1.16      matt 	 * Comb sort
    629      1.16      matt 	 */
    630      1.16      matt 	size_t gap = count;
    631      1.16      matt 	bool swapped = false;
    632      1.16      matt 	while (gap > 1 || swapped) {
    633      1.16      matt 		if (gap > 1) {
    634      1.16      matt 			/*
    635      1.16      matt 			 * phi = (1 + sqrt(5)) / 2 [golden ratio]
    636      1.16      matt 			 * N = 1 / (1 - e^-phi)) = 1.247330950103979
    637      1.16      matt 			 *
    638      1.16      matt 			 * We want to but can't use floating point to calculate
    639      1.16      matt 			 *	gap = (size_t)((double)gap / N)
    640      1.16      matt 			 *
    641      1.16      matt 			 * So we will use the multicative inverse of N
    642      1.16      matt 			 * (module 65536) to achieve the division.
    643      1.16      matt 			 *
    644      1.16      matt 			 * iN = 2^16 / 1.24733... = 52540
    645  1.21.2.2     skrll 			 * x / N == (x * iN) / 65536
    646      1.16      matt 			 */
    647      1.16      matt 			gap = (gap * 52540) / 65536;
    648      1.16      matt 		}
    649      1.16      matt 
    650      1.16      matt 		swapped = false;
    651      1.16      matt 
    652      1.16      matt 		for (size_t i = 0; gap + i < count; i++) {
    653      1.16      matt 			if (data[i] > data[i + gap]) {
    654      1.16      matt 				swap_data(data, i, i + gap);
    655      1.16      matt 				swapped = true;
    656      1.16      matt 			}
    657      1.16      matt 		}
    658      1.16      matt 	}
    659      1.16      matt #endif
    660      1.16      matt }
    661      1.16      matt #endif
    662      1.16      matt 
    663      1.16      matt void
    664      1.16      matt dump_splhist(struct cpu_info *ci, void (*pr)(const char *, ...))
    665      1.16      matt {
    666      1.16      matt #ifdef DIAGNOSTIC
    667      1.16      matt 	struct cpu_softc * const cpu = ci->ci_softc;
    668      1.16      matt 	uint64_t tbs[NIPL*NIPL];
    669      1.16      matt 	size_t ntbs = 0;
    670      1.16      matt 	for (size_t to = 0; to < NIPL; to++) {
    671      1.16      matt 		for (size_t from = 0; from < NIPL; from++) {
    672      1.16      matt 			uint64_t tb = cpu->cpu_spl_tb[to][from];
    673      1.16      matt 			if (tb == 0)
    674      1.16      matt 				continue;
    675      1.16      matt 			tbs[ntbs++] = (tb << 8) | (to << 4) | from;
    676      1.16      matt 		}
    677      1.16      matt 	}
    678      1.16      matt 	sort_data(tbs, ntbs);
    679      1.16      matt 
    680      1.16      matt 	if (pr == NULL)
    681      1.16      matt 		pr = printf;
    682      1.16      matt 	uint64_t last_tb = 0;
    683      1.16      matt 	for (size_t i = 0; i < ntbs; i++) {
    684      1.16      matt 		uint64_t tb = tbs[i];
    685      1.16      matt 		size_t from = tb & 15;
    686      1.16      matt 		size_t to = (tb >> 4) & 15;
    687      1.16      matt 		tb >>= 8;
    688      1.16      matt 		(*pr)("%s(%zu) from %zu at %"PRId64"",
    689      1.16      matt 		     from < to ? "splraise" : "splx",
    690      1.16      matt 		     to, from, tb);
    691      1.16      matt 		if (last_tb && from != IPL_NONE)
    692      1.16      matt 			(*pr)(" (+%"PRId64")", tb - last_tb);
    693      1.16      matt 		(*pr)("\n");
    694      1.16      matt 		last_tb = tb;
    695      1.16      matt 	}
    696      1.16      matt #endif
    697      1.16      matt }
    698