xref: /netbsd-src/sys/arch/hppa/dev/cpu.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: cpu.c,v 1.1 2014/02/24 07:23:42 skrll Exp $	*/
2 
3 /*	$OpenBSD: cpu.c,v 1.29 2009/02/08 18:33:28 miod Exp $	*/
4 
5 /*
6  * Copyright (c) 1998-2003 Michael Shalayeff
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1 2014/02/24 07:23:42 skrll Exp $");
33 
34 #include "opt_multiprocessor.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/atomic.h>
40 #include <sys/reboot.h>
41 
42 #include <uvm/uvm.h>
43 
44 #include <machine/cpufunc.h>
45 #include <machine/pdc.h>
46 #include <machine/iomod.h>
47 #include <machine/autoconf.h>
48 
49 #include <hppa/hppa/cpuvar.h>
50 #include <hppa/hppa/machdep.h>
51 #include <hppa/dev/cpudevs.h>
52 
53 #ifdef MULTIPROCESSOR
54 
55 int hppa_ncpu;
56 
57 struct cpu_info *cpu_hatch_info;
58 static volatile int start_secondary_cpu;
59 #endif
60 
61 int	cpumatch(device_t, cfdata_t, void *);
62 void	cpuattach(device_t, device_t, void *);
63 
64 CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
65     cpumatch, cpuattach, NULL, NULL);
66 
67 int
68 cpumatch(device_t parent, cfdata_t cf, void *aux)
69 {
70 	struct confargs *ca = aux;
71 
72 	/* probe any 1.0, 1.1 or 2.0 */
73 	if (ca->ca_type.iodc_type != HPPA_TYPE_NPROC ||
74 	    ca->ca_type.iodc_sv_model != HPPA_NPROC_HPPA)
75 		return 0;
76 
77 	return 1;
78 }
79 
80 void
81 cpuattach(device_t parent, device_t self, void *aux)
82 {
83 	/* machdep.c */
84 	extern struct pdc_cache pdc_cache;
85 	extern struct pdc_btlb pdc_btlb;
86 	extern struct pdc_model pdc_model;
87 	extern u_int cpu_ticksnum, cpu_ticksdenom;
88 
89 	struct cpu_softc *sc = device_private(self);
90 	struct confargs *ca = aux;
91 	static const char lvls[4][4] = { "0", "1", "1.5", "2" };
92 	struct hppa_interrupt_register *ir;
93 	struct cpu_info *ci;
94 	u_int mhz = 100 * cpu_ticksnum / cpu_ticksdenom;
95 	int cpuno = device_unit(self);
96 
97 #ifdef MULTIPROCESSOR
98 	struct pglist mlist;
99 	struct vm_page *m;
100 	int error;
101 #endif
102 
103 	sc->sc_dev = self;
104 
105 	/* Print the CPU chip name, nickname, and rev. */
106 	aprint_normal(": %s", hppa_cpu_info->hci_chip_name);
107 	if (hppa_cpu_info->hci_chip_nickname != NULL)
108 		aprint_normal(" (%s)", hppa_cpu_info->hci_chip_nickname);
109 	aprint_normal(" rev %d", cpu_revision);
110 
111 	/* sanity against luser amongst config editors */
112 	if (ca->ca_irq != 31) {
113 		aprint_error_dev(self, "bad irq number %d\n", ca->ca_irq);
114 		return;
115 	}
116 
117 	/* Print the CPU type, spec, level, category, and speed. */
118 	aprint_normal("\n%s: %s, PA-RISC %s", device_xname(self),
119 	    hppa_cpu_info->hci_chip_type,
120 	    hppa_cpu_info->hci_chip_spec);
121 	aprint_normal(", lev %s, cat %c, ",
122 	    lvls[pdc_model.pa_lvl], "AB"[pdc_model.mc]);
123 
124 	aprint_normal("%d", mhz / 100);
125 	if (mhz % 100 > 9)
126 		aprint_normal(".%02d", mhz % 100);
127 
128 	aprint_normal(" MHz clk\n%s: %s", device_xname(self),
129 	    pdc_model.sh? "shadows, ": "");
130 
131 	if (pdc_cache.dc_conf.cc_fsel)
132 		aprint_normal("%uK cache", pdc_cache.dc_size / 1024);
133 	else
134 		aprint_normal("%uK/%uK D/I caches", pdc_cache.dc_size / 1024,
135 		    pdc_cache.ic_size / 1024);
136 	if (pdc_cache.dt_conf.tc_sh)
137 		aprint_normal(", %u shared TLB", pdc_cache.dt_size);
138 	else
139 		aprint_normal(", %u/%u D/I TLBs", pdc_cache.dt_size,
140 		    pdc_cache.it_size);
141 
142 	if (pdc_btlb.finfo.num_c)
143 		aprint_normal(", %u shared BTLB", pdc_btlb.finfo.num_c);
144 	else {
145 		aprint_normal(", %u/%u D/I BTLBs", pdc_btlb.finfo.num_i,
146 		    pdc_btlb.finfo.num_d);
147 	}
148 	aprint_normal("\n");
149 
150 	/*
151 	 * Describe the floating-point support.
152 	 */
153 	KASSERT(fpu_present);
154 	aprint_normal("%s: %s floating point, rev %d\n", device_xname(self),
155 	    hppa_mod_info(HPPA_TYPE_FPU, (fpu_version >> 16) & 0x1f),
156 	    (fpu_version >> 11) & 0x1f);
157 
158 	if (cpuno >= HPPA_MAXCPUS) {
159 		aprint_normal_dev(self, "not started\n");
160 		return;
161 	}
162 
163 	ci = &cpus[cpuno];
164 	ci->ci_cpuid = cpuno;
165 	ci->ci_hpa = ca->ca_hpa;
166 
167 	hppa_intr_initialise(ci);
168 
169 	ir = &ci->ci_ir;
170 	hppa_interrupt_register_establish(ci, ir);
171 	ir->ir_iscpu = true;
172 	ir->ir_ci = ci;
173 	ir->ir_name = device_xname(self);
174 
175 	sc->sc_ihclk = hppa_intr_establish(IPL_CLOCK, clock_intr,
176 	    NULL /*clockframe*/, &ci->ci_ir, 31);
177 #ifdef MULTIPROCESSOR
178 	sc->sc_ihipi = hppa_intr_establish(IPL_HIGH, hppa_ipi_intr,
179 	    NULL /*clockframe*/, &ci->ci_ir, 30);
180 #endif
181 
182 	/*
183 	 * Reserve some bits for chips that don't like to be moved
184 	 * around, e.g. lasi and asp.
185 	 */
186 	ir->ir_rbits = ((1 << 28) | (1 << 27));
187 	ir->ir_bits &= ~ir->ir_rbits;
188 
189 #ifdef MULTIPROCESSOR
190 	/* Allocate stack for spin up and FPU emulation. */
191 	TAILQ_INIT(&mlist);
192 	error = uvm_pglistalloc(PAGE_SIZE, 0, -1L, PAGE_SIZE, 0, &mlist, 1, 0);
193 
194 	if (error) {
195 		aprint_error(": unable to allocate CPU stack!\n");
196 		return;
197 	}
198 	m = TAILQ_FIRST(&mlist);
199 	ci->ci_stack = VM_PAGE_TO_PHYS(m);
200 	ci->ci_softc = sc;
201 
202 	if (ci->ci_hpa == hppa_mcpuhpa) {
203 		ci->ci_flags |= CPUF_PRIMARY|CPUF_RUNNING;
204 	} else {
205 		int err;
206 
207 		err = mi_cpu_attach(ci);
208 		if (err) {
209 			aprint_error_dev(self,
210 			    "mi_cpu_attach failed with %d\n", err);
211 			return;
212 		}
213 	}
214 	hppa_ncpu++;
215 	hppa_ipi_init(ci);
216 #endif
217 	KASSERT(ci->ci_cpl == -1);
218 }
219 
220 #ifdef MULTIPROCESSOR
221 void
222 cpu_boot_secondary_processors(void)
223 {
224 	struct cpu_info *ci;
225 	struct iomod *cpu;
226 	int i, j;
227 
228 	for (i = 0; i < HPPA_MAXCPUS; i++) {
229 
230 		ci = &cpus[i];
231 		if (ci->ci_cpuid == 0)
232 			continue;
233 
234 		if (ci->ci_data.cpu_idlelwp == NULL)
235 			continue;
236 
237 		if (ci->ci_flags & CPUF_PRIMARY)
238 			continue;
239 
240 		/* Release the specified CPU by triggering an EIR{0}. */
241 		cpu_hatch_info = ci;
242 		cpu = (struct iomod *)(ci->ci_hpa);
243 		cpu->io_eir = 0;
244 		membar_sync();
245 
246 		/* Wait for CPU to wake up... */
247 		j = 0;
248 		while (!(ci->ci_flags & CPUF_RUNNING) && j++ < 10000)
249 			delay(1000);
250 		if (!(ci->ci_flags & CPUF_RUNNING))
251 			printf("failed to hatch cpu %i!\n", ci->ci_cpuid);
252 	}
253 
254 	/* Release secondary CPUs. */
255 	start_secondary_cpu = 1;
256 	membar_sync();
257 }
258 
259 void
260 cpu_hw_init(void)
261 {
262 	struct cpu_info *ci = curcpu();
263 
264 	/* Purge TLB and flush caches. */
265 	ptlball();
266 	fcacheall();
267 
268 	/* Enable address translations. */
269 	ci->ci_psw = PSW_I | PSW_Q | PSW_P | PSW_C | PSW_D;
270 	ci->ci_psw |= (cpus[0].ci_psw & PSW_O);
271 
272 	ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
273 }
274 
275 void
276 cpu_hatch(void)
277 {
278 	struct cpu_info *ci = curcpu();
279 
280 	ci->ci_flags |= CPUF_RUNNING;
281 
282 	/* Wait for additional CPUs to spinup. */
283 	while (!start_secondary_cpu)
284 		;
285 
286 	/* Spin for now */
287 	for (;;)
288 		;
289 
290 }
291 #endif
292