xref: /titanic_51/usr/src/uts/sun4v/os/mach_startup.c (revision 20d159bb22d487d2a4feb5d283a2b7d832bff8d5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/machsystm.h>
30 #include <sys/archsystm.h>
31 #include <sys/prom_plat.h>
32 #include <sys/promif.h>
33 #include <sys/vm.h>
34 #include <sys/cpu.h>
35 #include <sys/atomic.h>
36 #include <sys/cpupart.h>
37 #include <sys/disp.h>
38 #include <sys/hypervisor_api.h>
39 #include <sys/traptrace.h>
40 #include <sys/modctl.h>
41 #include <sys/ldoms.h>
42 
43 #ifdef TRAPTRACE
44 int mach_htraptrace_enable = 1;
45 #else
46 int mach_htraptrace_enable = 0;
47 #endif
48 int htrap_tr0_inuse = 0;
49 extern char htrap_tr0[];	/* prealloc buf for boot cpu */
50 
51 caddr_t	mmu_fault_status_area;
52 
53 extern void sfmmu_set_tsbs(void);
54 /*
55  * CPU IDLE optimization variables/routines
56  */
57 static int enable_halt_idle_cpus = 1;
58 
59 void
60 setup_trap_table(void)
61 {
62 	caddr_t mmfsa_va;
63 	extern	 caddr_t mmu_fault_status_area;
64 	mmfsa_va =
65 	    mmu_fault_status_area + (MMFSA_SIZE * CPU->cpu_id);
66 
67 	intr_init(CPU);		/* init interrupt request free list */
68 	setwstate(WSTATE_KERN);
69 	set_mmfsa_scratchpad(mmfsa_va);
70 	prom_set_mmfsa_traptable(&trap_table, va_to_pa(mmfsa_va));
71 	sfmmu_set_tsbs();
72 }
73 
74 void
75 phys_install_has_changed(void)
76 {
77 
78 }
79 
80 #ifdef N2_IDLE_WORKAROUND
81 /*
82  * Tuneable to control enabling of IDLE loop workaround on Niagara2 1.x parts.
83  * This workaround will be removed before the RR.
84  */
85 int	n2_idle_workaround;
86 #endif
87 
88 /*
89  * Halt the present CPU until awoken via an interrupt
90  */
91 static void
92 cpu_halt(void)
93 {
94 	cpu_t *cpup = CPU;
95 	processorid_t cpun = cpup->cpu_id;
96 	cpupart_t *cp = cpup->cpu_part;
97 	int hset_update = 1;
98 	volatile int *p = &cpup->cpu_disp->disp_nrunnable;
99 	uint_t s;
100 
101 	/*
102 	 * If this CPU is online, and there's multiple CPUs
103 	 * in the system, then we should notate our halting
104 	 * by adding ourselves to the partition's halted CPU
105 	 * bitmap. This allows other CPUs to find/awaken us when
106 	 * work becomes available.
107 	 */
108 	if (CPU->cpu_flags & CPU_OFFLINE || ncpus == 1)
109 		hset_update = 0;
110 
111 	/*
112 	 * Add ourselves to the partition's halted CPUs bitmask
113 	 * and set our HALTED flag, if necessary.
114 	 *
115 	 * When a thread becomes runnable, it is placed on the queue
116 	 * and then the halted cpuset is checked to determine who
117 	 * (if anyone) should be awoken. We therefore need to first
118 	 * add ourselves to the halted cpuset, and then check if there
119 	 * is any work available.
120 	 */
121 	if (hset_update) {
122 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
123 		membar_producer();
124 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
125 	}
126 
127 	/*
128 	 * Check to make sure there's really nothing to do.
129 	 * Work destined for this CPU may become available after
130 	 * this check. We'll be notified through the clearing of our
131 	 * bit in the halted CPU bitmask, and a poke.
132 	 */
133 	if (disp_anywork()) {
134 		if (hset_update) {
135 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
136 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
137 		}
138 		return;
139 	}
140 
141 #ifdef N2_IDLE_WORKAROUND
142 	/*
143 	 * The following workaround for Niagara2, when enabled, forces the
144 	 * IDLE CPU to wait in a tight loop until something becomes runnable
145 	 * locally, minimizing the overall CPU usage on an IDLE CPU.
146 	 */
147 	if (n2_idle_workaround) {
148 		while (cpup->cpu_disp->disp_nrunnable == 0) {
149 			(void) hv_cpu_yield();
150 		}
151 	}
152 #endif
153 
154 	/*
155 	 * We're on our way to being halted.  Wait until something becomes
156 	 * runnable locally or we are awaken (i.e. removed from the halt set).
157 	 * Note that the call to hv_cpu_yield() can return even if we have
158 	 * nothing to do.
159 	 *
160 	 * Disable interrupts now, so that we'll awaken immediately
161 	 * after halting if someone tries to poke us between now and
162 	 * the time we actually halt.
163 	 *
164 	 * We check for the presence of our bit after disabling interrupts.
165 	 * If it's cleared, we'll return. If the bit is cleared after
166 	 * we check then the poke will pop us out of the halted state.
167 	 * Also, if the offlined CPU has been brought back on-line, then
168 	 * we return as well.
169 	 *
170 	 * The ordering of the poke and the clearing of the bit by cpu_wakeup
171 	 * is important.
172 	 * cpu_wakeup() must clear, then poke.
173 	 * cpu_halt() must disable interrupts, then check for the bit.
174 	 *
175 	 * The check for anything locally runnable is here for performance
176 	 * and isn't needed for correctness. disp_nrunnable ought to be
177 	 * in our cache still, so it's inexpensive to check, and if there
178 	 * is anything runnable we won't have to wait for the poke.
179 	 *
180 	 */
181 	s = disable_vec_intr();
182 	while (*p == 0 &&
183 	    ((hset_update && CPU_IN_SET(cp->cp_mach->mc_haltset, cpun)) ||
184 	    (!hset_update && (CPU->cpu_flags & CPU_OFFLINE)))) {
185 		(void) hv_cpu_yield();
186 		enable_vec_intr(s);
187 		s = disable_vec_intr();
188 	}
189 
190 	/*
191 	 * We're no longer halted
192 	 */
193 	enable_vec_intr(s);
194 	if (hset_update) {
195 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
196 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
197 	}
198 }
199 
200 /*
201  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
202  * Otherwise, see if other CPUs in the cpu partition are halted and need to
203  * be woken up so that they can steal the thread we placed on this CPU.
204  * This function is only used on MP systems.
205  */
206 static void
207 cpu_wakeup(cpu_t *cpu, int bound)
208 {
209 	uint_t		cpu_found;
210 	int		result;
211 	cpupart_t	*cp;
212 
213 	cp = cpu->cpu_part;
214 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
215 		/*
216 		 * Clear the halted bit for that CPU since it will be
217 		 * poked in a moment.
218 		 */
219 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
220 		/*
221 		 * We may find the current CPU present in the halted cpuset
222 		 * if we're in the context of an interrupt that occurred
223 		 * before we had a chance to clear our bit in cpu_halt().
224 		 * Poking ourself is obviously unnecessary, since if
225 		 * we're here, we're not halted.
226 		 */
227 		if (cpu != CPU)
228 			poke_cpu(cpu->cpu_id);
229 		return;
230 	} else {
231 		/*
232 		 * This cpu isn't halted, but it's idle or undergoing a
233 		 * context switch. No need to awaken anyone else.
234 		 */
235 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
236 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
237 			return;
238 	}
239 
240 	/*
241 	 * No need to wake up other CPUs if the thread we just enqueued
242 	 * is bound.
243 	 */
244 	if (bound)
245 		return;
246 
247 	/*
248 	 * See if there's any other halted CPUs. If there are, then
249 	 * select one, and awaken it.
250 	 * It's possible that after we find a CPU, somebody else
251 	 * will awaken it before we get the chance.
252 	 * In that case, look again.
253 	 */
254 	do {
255 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
256 		if (cpu_found == CPUSET_NOTINSET)
257 			return;
258 
259 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
260 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
261 	} while (result < 0);
262 
263 	if (cpu_found != CPU->cpu_id)
264 		poke_cpu(cpu_found);
265 }
266 
267 void
268 mach_cpu_halt_idle()
269 {
270 	if (enable_halt_idle_cpus) {
271 		idle_cpu = cpu_halt;
272 		disp_enq_thread = cpu_wakeup;
273 	}
274 }
275 
276 int
277 ndata_alloc_mmfsa(struct memlist *ndata)
278 {
279 	size_t	size;
280 
281 	size = MMFSA_SIZE * max_ncpus;
282 	mmu_fault_status_area = ndata_alloc(ndata, size, ecache_alignsize);
283 	if (mmu_fault_status_area == NULL)
284 		return (-1);
285 	return (0);
286 }
287 
288 void
289 mach_memscrub(void)
290 {
291 	/* no memscrub support for sun4v for now */
292 }
293 
294 void
295 mach_fpras()
296 {
297 	/* no fpras support for sun4v for now */
298 }
299 
300 void
301 mach_hw_copy_limit(void)
302 {
303 	/* HW copy limits set by individual CPU module */
304 }
305 
306 /*
307  * We need to enable soft ring functionality on Niagara platform since
308  * one strand can't handle interrupts for a 1Gb NIC. Set the tunable
309  * ip_squeue_soft_ring by default on this platform. We can also set
310  * ip_threads_per_cpu to track number of threads per core. The variables
311  * themselves are defined in space.c and used by IP module
312  */
313 extern uint_t ip_threads_per_cpu;
314 extern boolean_t ip_squeue_soft_ring;
315 void
316 startup_platform(void)
317 {
318 	ip_squeue_soft_ring = B_TRUE;
319 }
320 
321 /*
322  * This function sets up hypervisor traptrace buffer
323  * This routine is called by the boot cpu only
324  */
325 void
326 mach_htraptrace_setup(int cpuid)
327 {
328 	TRAP_TRACE_CTL	*ctlp;
329 	int bootcpuid = getprocessorid(); /* invoked on boot cpu only */
330 
331 	if (mach_htraptrace_enable && ((cpuid != bootcpuid) ||
332 	    !htrap_tr0_inuse)) {
333 		ctlp = &trap_trace_ctl[cpuid];
334 		ctlp->d.hvaddr_base = (cpuid == bootcpuid) ? htrap_tr0 :
335 		    contig_mem_alloc_align(HTRAP_TSIZE, HTRAP_TSIZE);
336 		if (ctlp->d.hvaddr_base == NULL) {
337 			ctlp->d.hlimit = 0;
338 			ctlp->d.hpaddr_base = NULL;
339 			cmn_err(CE_WARN, "!cpu%d: failed to allocate HV "
340 			    "traptrace buffer", cpuid);
341 		} else {
342 			ctlp->d.hlimit = HTRAP_TSIZE;
343 			ctlp->d.hpaddr_base = va_to_pa(ctlp->d.hvaddr_base);
344 		}
345 	}
346 }
347 
348 /*
349  * This function enables or disables the hypervisor traptracing
350  */
351 void
352 mach_htraptrace_configure(int cpuid)
353 {
354 	uint64_t ret;
355 	uint64_t prev_buf, prev_bufsize;
356 	uint64_t prev_enable;
357 	uint64_t size;
358 	TRAP_TRACE_CTL	*ctlp;
359 
360 	ctlp = &trap_trace_ctl[cpuid];
361 	if (mach_htraptrace_enable) {
362 		if ((ctlp->d.hvaddr_base != NULL) &&
363 		    ((ctlp->d.hvaddr_base != htrap_tr0) ||
364 		    (!htrap_tr0_inuse))) {
365 			ret = hv_ttrace_buf_info(&prev_buf, &prev_bufsize);
366 			if ((ret == H_EOK) && (prev_bufsize != 0)) {
367 				cmn_err(CE_CONT,
368 				    "!cpu%d: previous HV traptrace buffer of "
369 				    "size 0x%lx at address 0x%lx", cpuid,
370 				    prev_bufsize, prev_buf);
371 			}
372 
373 			ret = hv_ttrace_buf_conf(ctlp->d.hpaddr_base,
374 			    ctlp->d.hlimit /
375 			    (sizeof (struct htrap_trace_record)), &size);
376 			if (ret == H_EOK) {
377 				ret = hv_ttrace_enable(\
378 				    (uint64_t)TRAP_TENABLE_ALL, &prev_enable);
379 				if (ret != H_EOK) {
380 					cmn_err(CE_WARN,
381 					    "!cpu%d: HV traptracing not "
382 					    "enabled, ta: 0x%x returned error: "
383 					    "%ld", cpuid, TTRACE_ENABLE, ret);
384 				} else {
385 					if (ctlp->d.hvaddr_base == htrap_tr0)
386 						htrap_tr0_inuse = 1;
387 				}
388 			} else {
389 				cmn_err(CE_WARN,
390 				    "!cpu%d: HV traptrace buffer not "
391 				    "configured, ta: 0x%x returned error: %ld",
392 				    cpuid, TTRACE_BUF_CONF, ret);
393 			}
394 			/*
395 			 * set hvaddr_base to NULL when traptrace buffer
396 			 * registration fails
397 			 */
398 			if (ret != H_EOK) {
399 				ctlp->d.hvaddr_base = NULL;
400 				ctlp->d.hlimit = 0;
401 				ctlp->d.hpaddr_base = NULL;
402 			}
403 		}
404 	} else {
405 		ret = hv_ttrace_buf_info(&prev_buf, &prev_bufsize);
406 		if ((ret == H_EOK) && (prev_bufsize != 0)) {
407 			ret = hv_ttrace_enable((uint64_t)TRAP_TDISABLE_ALL,
408 			    &prev_enable);
409 			if (ret == H_EOK) {
410 				if (ctlp->d.hvaddr_base == htrap_tr0)
411 					htrap_tr0_inuse = 0;
412 				ctlp->d.hvaddr_base = NULL;
413 				ctlp->d.hlimit = 0;
414 				ctlp->d.hpaddr_base = NULL;
415 			} else
416 				cmn_err(CE_WARN,
417 				    "!cpu%d: HV traptracing is not disabled, "
418 				    "ta: 0x%x returned error: %ld",
419 				    cpuid, TTRACE_ENABLE, ret);
420 		}
421 	}
422 }
423 
424 /*
425  * This function cleans up the hypervisor traptrace buffer
426  */
427 void
428 mach_htraptrace_cleanup(int cpuid)
429 {
430 	if (mach_htraptrace_enable) {
431 		TRAP_TRACE_CTL *ctlp;
432 		caddr_t httrace_buf_va;
433 
434 		ASSERT(cpuid < max_ncpus);
435 		ctlp = &trap_trace_ctl[cpuid];
436 		httrace_buf_va = ctlp->d.hvaddr_base;
437 		if (httrace_buf_va == htrap_tr0) {
438 			bzero(httrace_buf_va, HTRAP_TSIZE);
439 		} else if (httrace_buf_va != NULL) {
440 			contig_mem_free(httrace_buf_va, HTRAP_TSIZE);
441 		}
442 		ctlp->d.hvaddr_base = NULL;
443 		ctlp->d.hlimit = 0;
444 		ctlp->d.hpaddr_base = NULL;
445 	}
446 }
447 
448 /*
449  * Load any required machine class (sun4v) specific drivers.
450  */
451 void
452 load_mach_drivers(void)
453 {
454 	/*
455 	 * We don't want to load these LDOMs-specific
456 	 * modules if domaining is not supported.  Also,
457 	 * we must be able to run on non-LDOMs firmware.
458 	 */
459 	if (!(domaining_capabilities & DOMAINING_SUPPORTED))
460 		return;
461 
462 	/*
463 	 * Load the core domain services module
464 	 */
465 	if (modload("misc", "ds") == -1)
466 		cmn_err(CE_NOTE, "!'ds' module failed to load");
467 
468 	/*
469 	 * Load the rest of the domain services
470 	 */
471 	if (modload("misc", "fault_iso") == -1)
472 		cmn_err(CE_NOTE, "!'fault_iso' module failed to load");
473 
474 	if (modload("misc", "platsvc") == -1)
475 		cmn_err(CE_NOTE, "!'platsvc' module failed to load");
476 
477 	if ((domaining_capabilities & DOMAINING_ENABLED) &&
478 	    modload("misc", "dr_cpu") == -1)
479 		cmn_err(CE_NOTE, "!'dr_cpu' module failed to load");
480 
481 	/*
482 	 * Attempt to attach any virtual device servers. These
483 	 * drivers must be loaded at start of day so that they
484 	 * can respond to any updates to the machine description.
485 	 *
486 	 * Since it is quite likely that a domain will not support
487 	 * one or more of these servers, failures are ignored.
488 	 */
489 
490 	/* virtual disk server */
491 	(void) i_ddi_attach_hw_nodes("vds");
492 
493 	/* virtual network switch */
494 	(void) i_ddi_attach_hw_nodes("vsw");
495 
496 	/* virtual console concentrator */
497 	(void) i_ddi_attach_hw_nodes("vcc");
498 }
499