xref: /titanic_44/usr/src/uts/sun4v/os/mach_startup.c (revision f6acbf7c6543dc0aab58cae1df419f0147c46d77)
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 2008 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 #include <sys/cpu_module.h>
43 #include <sys/mutex_impl.h>
44 #include <vm/vm_dep.h>
45 #include <sys/sdt.h>
46 
47 #ifdef TRAPTRACE
48 int mach_htraptrace_enable = 1;
49 #else
50 int mach_htraptrace_enable = 0;
51 #endif
52 int htrap_tr0_inuse = 0;
53 extern char htrap_tr0[];	/* prealloc buf for boot cpu */
54 
55 caddr_t	mmu_fault_status_area;
56 
57 extern void sfmmu_set_tsbs(void);
58 /*
59  * CPU IDLE optimization variables/routines
60  */
61 static int enable_halt_idle_cpus = 1;
62 
63 /*
64  * Defines for the idle_state_transition DTrace probe
65  *
66  * The probe fires when the CPU undergoes an idle state change (e.g. hv yield)
67  * The agument passed is the state to which the CPU is transitioning.
68  *
69  * The states are defined here.
70  */
71 #define	IDLE_STATE_NORMAL 0
72 #define	IDLE_STATE_YIELDED 1
73 
74 #define	SUN4V_CLOCK_TICK_THRESHOLD	64
75 #define	SUN4V_CLOCK_TICK_NCPUS		64
76 
77 extern int	clock_tick_threshold;
78 extern int	clock_tick_ncpus;
79 
80 void
81 setup_trap_table(void)
82 {
83 	caddr_t mmfsa_va;
84 	extern	 caddr_t mmu_fault_status_area;
85 	mmfsa_va =
86 	    mmu_fault_status_area + (MMFSA_SIZE * CPU->cpu_id);
87 
88 	intr_init(CPU);		/* init interrupt request free list */
89 	setwstate(WSTATE_KERN);
90 	set_mmfsa_scratchpad(mmfsa_va);
91 	prom_set_mmfsa_traptable(&trap_table, va_to_pa(mmfsa_va));
92 	sfmmu_set_tsbs();
93 }
94 
95 void
96 phys_install_has_changed(void)
97 {
98 
99 }
100 
101 /*
102  * Halt the present CPU until awoken via an interrupt
103  */
104 static void
105 cpu_halt(void)
106 {
107 	cpu_t *cpup = CPU;
108 	processorid_t cpun = cpup->cpu_id;
109 	cpupart_t *cp = cpup->cpu_part;
110 	int hset_update = 1;
111 	volatile int *p = &cpup->cpu_disp->disp_nrunnable;
112 	uint_t s;
113 
114 	/*
115 	 * If this CPU is online, and there's multiple CPUs
116 	 * in the system, then we should notate our halting
117 	 * by adding ourselves to the partition's halted CPU
118 	 * bitmap. This allows other CPUs to find/awaken us when
119 	 * work becomes available.
120 	 */
121 	if (CPU->cpu_flags & CPU_OFFLINE || ncpus == 1)
122 		hset_update = 0;
123 
124 	/*
125 	 * Add ourselves to the partition's halted CPUs bitmask
126 	 * and set our HALTED flag, if necessary.
127 	 *
128 	 * When a thread becomes runnable, it is placed on the queue
129 	 * and then the halted cpuset is checked to determine who
130 	 * (if anyone) should be awoken. We therefore need to first
131 	 * add ourselves to the halted cpuset, and then check if there
132 	 * is any work available.
133 	 */
134 	if (hset_update) {
135 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
136 		membar_producer();
137 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
138 	}
139 
140 	/*
141 	 * Check to make sure there's really nothing to do.
142 	 * Work destined for this CPU may become available after
143 	 * this check. We'll be notified through the clearing of our
144 	 * bit in the halted CPU bitmask, and a poke.
145 	 */
146 	if (disp_anywork()) {
147 		if (hset_update) {
148 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
149 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
150 		}
151 		return;
152 	}
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 
186 		DTRACE_PROBE1(idle__state__transition,
187 		    uint_t, IDLE_STATE_YIELDED);
188 		(void) hv_cpu_yield();
189 		DTRACE_PROBE1(idle__state__transition,
190 		    uint_t, IDLE_STATE_NORMAL);
191 
192 		enable_vec_intr(s);
193 		s = disable_vec_intr();
194 	}
195 
196 	/*
197 	 * We're no longer halted
198 	 */
199 	enable_vec_intr(s);
200 	if (hset_update) {
201 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
202 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
203 	}
204 }
205 
206 /*
207  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
208  * Otherwise, see if other CPUs in the cpu partition are halted and need to
209  * be woken up so that they can steal the thread we placed on this CPU.
210  * This function is only used on MP systems.
211  */
212 static void
213 cpu_wakeup(cpu_t *cpu, int bound)
214 {
215 	uint_t		cpu_found;
216 	int		result;
217 	cpupart_t	*cp;
218 
219 	cp = cpu->cpu_part;
220 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
221 		/*
222 		 * Clear the halted bit for that CPU since it will be
223 		 * poked in a moment.
224 		 */
225 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
226 		/*
227 		 * We may find the current CPU present in the halted cpuset
228 		 * if we're in the context of an interrupt that occurred
229 		 * before we had a chance to clear our bit in cpu_halt().
230 		 * Poking ourself is obviously unnecessary, since if
231 		 * we're here, we're not halted.
232 		 */
233 		if (cpu != CPU)
234 			poke_cpu(cpu->cpu_id);
235 		return;
236 	} else {
237 		/*
238 		 * This cpu isn't halted, but it's idle or undergoing a
239 		 * context switch. No need to awaken anyone else.
240 		 */
241 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
242 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
243 			return;
244 	}
245 
246 	/*
247 	 * No need to wake up other CPUs if the thread we just enqueued
248 	 * is bound.
249 	 */
250 	if (bound)
251 		return;
252 
253 	/*
254 	 * See if there's any other halted CPUs. If there are, then
255 	 * select one, and awaken it.
256 	 * It's possible that after we find a CPU, somebody else
257 	 * will awaken it before we get the chance.
258 	 * In that case, look again.
259 	 */
260 	do {
261 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
262 		if (cpu_found == CPUSET_NOTINSET)
263 			return;
264 
265 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
266 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
267 	} while (result < 0);
268 
269 	if (cpu_found != CPU->cpu_id)
270 		poke_cpu(cpu_found);
271 }
272 
273 void
274 mach_cpu_halt_idle()
275 {
276 	if (enable_halt_idle_cpus) {
277 		idle_cpu = cpu_halt;
278 		disp_enq_thread = cpu_wakeup;
279 	}
280 }
281 
282 int
283 ndata_alloc_mmfsa(struct memlist *ndata)
284 {
285 	size_t	size;
286 
287 	size = MMFSA_SIZE * max_ncpus;
288 	mmu_fault_status_area = ndata_alloc(ndata, size, ecache_alignsize);
289 	if (mmu_fault_status_area == NULL)
290 		return (-1);
291 	return (0);
292 }
293 
294 void
295 mach_memscrub(void)
296 {
297 	/* no memscrub support for sun4v for now */
298 }
299 
300 void
301 mach_fpras()
302 {
303 	/* no fpras support for sun4v for now */
304 }
305 
306 void
307 mach_hw_copy_limit(void)
308 {
309 	/* HW copy limits set by individual CPU module */
310 }
311 
312 /*
313  * We need to enable soft ring functionality on Niagara platform since
314  * one strand can't handle interrupts for a 1Gb NIC. Set the tunable
315  * ip_squeue_soft_ring by default on this platform. We can also set
316  * ip_threads_per_cpu to track number of threads per core. The variables
317  * themselves are defined in space.c and used by IP module
318  */
319 extern uint_t ip_threads_per_cpu;
320 extern boolean_t ip_squeue_soft_ring;
321 void
322 startup_platform(void)
323 {
324 	ip_squeue_soft_ring = B_TRUE;
325 	if (clock_tick_threshold == 0)
326 		clock_tick_threshold = SUN4V_CLOCK_TICK_THRESHOLD;
327 	if (clock_tick_ncpus == 0)
328 		clock_tick_ncpus = SUN4V_CLOCK_TICK_NCPUS;
329 	/* set per-platform constants for mutex_backoff */
330 	mutex_backoff_base = 1;
331 	mutex_cap_factor = 4;
332 	if (l2_cache_node_count() > 1) {
333 		/* VF for example */
334 		mutex_backoff_base = 2;
335 		mutex_cap_factor = 16;
336 	}
337 }
338 
339 /*
340  * This function sets up hypervisor traptrace buffer
341  * This routine is called by the boot cpu only
342  */
343 void
344 mach_htraptrace_setup(int cpuid)
345 {
346 	TRAP_TRACE_CTL	*ctlp;
347 	int bootcpuid = getprocessorid(); /* invoked on boot cpu only */
348 
349 	if (mach_htraptrace_enable && ((cpuid != bootcpuid) ||
350 	    !htrap_tr0_inuse)) {
351 		ctlp = &trap_trace_ctl[cpuid];
352 		ctlp->d.hvaddr_base = (cpuid == bootcpuid) ? htrap_tr0 :
353 		    contig_mem_alloc_align(HTRAP_TSIZE, HTRAP_TSIZE);
354 		if (ctlp->d.hvaddr_base == NULL) {
355 			ctlp->d.hlimit = 0;
356 			ctlp->d.hpaddr_base = NULL;
357 			cmn_err(CE_WARN, "!cpu%d: failed to allocate HV "
358 			    "traptrace buffer", cpuid);
359 		} else {
360 			ctlp->d.hlimit = HTRAP_TSIZE;
361 			ctlp->d.hpaddr_base = va_to_pa(ctlp->d.hvaddr_base);
362 		}
363 	}
364 }
365 
366 /*
367  * This function enables or disables the hypervisor traptracing
368  */
369 void
370 mach_htraptrace_configure(int cpuid)
371 {
372 	uint64_t ret;
373 	uint64_t prev_buf, prev_bufsize;
374 	uint64_t prev_enable;
375 	uint64_t size;
376 	TRAP_TRACE_CTL	*ctlp;
377 
378 	ctlp = &trap_trace_ctl[cpuid];
379 	if (mach_htraptrace_enable) {
380 		if ((ctlp->d.hvaddr_base != NULL) &&
381 		    ((ctlp->d.hvaddr_base != htrap_tr0) ||
382 		    (!htrap_tr0_inuse))) {
383 			ret = hv_ttrace_buf_info(&prev_buf, &prev_bufsize);
384 			if ((ret == H_EOK) && (prev_bufsize != 0)) {
385 				cmn_err(CE_CONT,
386 				    "!cpu%d: previous HV traptrace buffer of "
387 				    "size 0x%lx at address 0x%lx", cpuid,
388 				    prev_bufsize, prev_buf);
389 			}
390 
391 			ret = hv_ttrace_buf_conf(ctlp->d.hpaddr_base,
392 			    ctlp->d.hlimit /
393 			    (sizeof (struct htrap_trace_record)), &size);
394 			if (ret == H_EOK) {
395 				ret = hv_ttrace_enable(\
396 				    (uint64_t)TRAP_TENABLE_ALL, &prev_enable);
397 				if (ret != H_EOK) {
398 					cmn_err(CE_WARN,
399 					    "!cpu%d: HV traptracing not "
400 					    "enabled, ta: 0x%x returned error: "
401 					    "%ld", cpuid, TTRACE_ENABLE, ret);
402 				} else {
403 					if (ctlp->d.hvaddr_base == htrap_tr0)
404 						htrap_tr0_inuse = 1;
405 				}
406 			} else {
407 				cmn_err(CE_WARN,
408 				    "!cpu%d: HV traptrace buffer not "
409 				    "configured, ta: 0x%x returned error: %ld",
410 				    cpuid, TTRACE_BUF_CONF, ret);
411 			}
412 			/*
413 			 * set hvaddr_base to NULL when traptrace buffer
414 			 * registration fails
415 			 */
416 			if (ret != H_EOK) {
417 				ctlp->d.hvaddr_base = NULL;
418 				ctlp->d.hlimit = 0;
419 				ctlp->d.hpaddr_base = NULL;
420 			}
421 		}
422 	} else {
423 		ret = hv_ttrace_buf_info(&prev_buf, &prev_bufsize);
424 		if ((ret == H_EOK) && (prev_bufsize != 0)) {
425 			ret = hv_ttrace_enable((uint64_t)TRAP_TDISABLE_ALL,
426 			    &prev_enable);
427 			if (ret == H_EOK) {
428 				if (ctlp->d.hvaddr_base == htrap_tr0)
429 					htrap_tr0_inuse = 0;
430 				ctlp->d.hvaddr_base = NULL;
431 				ctlp->d.hlimit = 0;
432 				ctlp->d.hpaddr_base = NULL;
433 			} else
434 				cmn_err(CE_WARN,
435 				    "!cpu%d: HV traptracing is not disabled, "
436 				    "ta: 0x%x returned error: %ld",
437 				    cpuid, TTRACE_ENABLE, ret);
438 		}
439 	}
440 }
441 
442 /*
443  * This function cleans up the hypervisor traptrace buffer
444  */
445 void
446 mach_htraptrace_cleanup(int cpuid)
447 {
448 	if (mach_htraptrace_enable) {
449 		TRAP_TRACE_CTL *ctlp;
450 		caddr_t httrace_buf_va;
451 
452 		ASSERT(cpuid < max_ncpus);
453 		ctlp = &trap_trace_ctl[cpuid];
454 		httrace_buf_va = ctlp->d.hvaddr_base;
455 		if (httrace_buf_va == htrap_tr0) {
456 			bzero(httrace_buf_va, HTRAP_TSIZE);
457 		} else if (httrace_buf_va != NULL) {
458 			contig_mem_free(httrace_buf_va, HTRAP_TSIZE);
459 		}
460 		ctlp->d.hvaddr_base = NULL;
461 		ctlp->d.hlimit = 0;
462 		ctlp->d.hpaddr_base = NULL;
463 	}
464 }
465 
466 /*
467  * Load any required machine class (sun4v) specific drivers.
468  */
469 void
470 load_mach_drivers(void)
471 {
472 	/*
473 	 * We don't want to load these LDOMs-specific
474 	 * modules if domaining is not supported.  Also,
475 	 * we must be able to run on non-LDOMs firmware.
476 	 */
477 	if (!domaining_supported())
478 		return;
479 
480 	/*
481 	 * Load the core domain services module
482 	 */
483 	if (modload("misc", "ds") == -1)
484 		cmn_err(CE_NOTE, "!'ds' module failed to load");
485 
486 	/*
487 	 * Load the rest of the domain services
488 	 */
489 	if (modload("misc", "fault_iso") == -1)
490 		cmn_err(CE_NOTE, "!'fault_iso' module failed to load");
491 
492 	if (modload("misc", "platsvc") == -1)
493 		cmn_err(CE_NOTE, "!'platsvc' module failed to load");
494 
495 	if (domaining_enabled() && modload("misc", "dr_cpu") == -1)
496 		cmn_err(CE_NOTE, "!'dr_cpu' module failed to load");
497 
498 	/*
499 	 * Attempt to attach any virtual device servers. These
500 	 * drivers must be loaded at start of day so that they
501 	 * can respond to any updates to the machine description.
502 	 *
503 	 * Since it is quite likely that a domain will not support
504 	 * one or more of these servers, failures are ignored.
505 	 */
506 
507 	/* virtual disk server */
508 	(void) i_ddi_attach_hw_nodes("vds");
509 
510 	/* virtual network switch */
511 	(void) i_ddi_attach_hw_nodes("vsw");
512 
513 	/* virtual console concentrator */
514 	(void) i_ddi_attach_hw_nodes("vcc");
515 }
516