xref: /illumos-gate/usr/src/uts/sun4v/os/mach_startup.c (revision d321a33cdd896e6b211d113a33698dd76e89b861)
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 
46 #ifdef TRAPTRACE
47 int mach_htraptrace_enable = 1;
48 #else
49 int mach_htraptrace_enable = 0;
50 #endif
51 int htrap_tr0_inuse = 0;
52 extern char htrap_tr0[];	/* prealloc buf for boot cpu */
53 
54 caddr_t	mmu_fault_status_area;
55 
56 extern void sfmmu_set_tsbs(void);
57 /*
58  * CPU IDLE optimization variables/routines
59  */
60 static int enable_halt_idle_cpus = 1;
61 
62 #define	SUN4V_CLOCK_TICK_THRESHOLD	64
63 #define	SUN4V_CLOCK_TICK_NCPUS		64
64 
65 extern int	clock_tick_threshold;
66 extern int	clock_tick_ncpus;
67 
68 void
69 setup_trap_table(void)
70 {
71 	caddr_t mmfsa_va;
72 	extern	 caddr_t mmu_fault_status_area;
73 	mmfsa_va =
74 	    mmu_fault_status_area + (MMFSA_SIZE * CPU->cpu_id);
75 
76 	intr_init(CPU);		/* init interrupt request free list */
77 	setwstate(WSTATE_KERN);
78 	set_mmfsa_scratchpad(mmfsa_va);
79 	prom_set_mmfsa_traptable(&trap_table, va_to_pa(mmfsa_va));
80 	sfmmu_set_tsbs();
81 }
82 
83 void
84 phys_install_has_changed(void)
85 {
86 
87 }
88 
89 /*
90  * Halt the present CPU until awoken via an interrupt
91  */
92 static void
93 cpu_halt(void)
94 {
95 	cpu_t *cpup = CPU;
96 	processorid_t cpun = cpup->cpu_id;
97 	cpupart_t *cp = cpup->cpu_part;
98 	int hset_update = 1;
99 	volatile int *p = &cpup->cpu_disp->disp_nrunnable;
100 	uint_t s;
101 
102 	/*
103 	 * If this CPU is online, and there's multiple CPUs
104 	 * in the system, then we should notate our halting
105 	 * by adding ourselves to the partition's halted CPU
106 	 * bitmap. This allows other CPUs to find/awaken us when
107 	 * work becomes available.
108 	 */
109 	if (CPU->cpu_flags & CPU_OFFLINE || ncpus == 1)
110 		hset_update = 0;
111 
112 	/*
113 	 * Add ourselves to the partition's halted CPUs bitmask
114 	 * and set our HALTED flag, if necessary.
115 	 *
116 	 * When a thread becomes runnable, it is placed on the queue
117 	 * and then the halted cpuset is checked to determine who
118 	 * (if anyone) should be awoken. We therefore need to first
119 	 * add ourselves to the halted cpuset, and then check if there
120 	 * is any work available.
121 	 */
122 	if (hset_update) {
123 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
124 		membar_producer();
125 		CPUSET_ATOMIC_ADD(cp->cp_mach->mc_haltset, cpun);
126 	}
127 
128 	/*
129 	 * Check to make sure there's really nothing to do.
130 	 * Work destined for this CPU may become available after
131 	 * this check. We'll be notified through the clearing of our
132 	 * bit in the halted CPU bitmask, and a poke.
133 	 */
134 	if (disp_anywork()) {
135 		if (hset_update) {
136 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
137 			CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
138 		}
139 		return;
140 	}
141 
142 	/*
143 	 * We're on our way to being halted.  Wait until something becomes
144 	 * runnable locally or we are awaken (i.e. removed from the halt set).
145 	 * Note that the call to hv_cpu_yield() can return even if we have
146 	 * nothing to do.
147 	 *
148 	 * Disable interrupts now, so that we'll awaken immediately
149 	 * after halting if someone tries to poke us between now and
150 	 * the time we actually halt.
151 	 *
152 	 * We check for the presence of our bit after disabling interrupts.
153 	 * If it's cleared, we'll return. If the bit is cleared after
154 	 * we check then the poke will pop us out of the halted state.
155 	 * Also, if the offlined CPU has been brought back on-line, then
156 	 * we return as well.
157 	 *
158 	 * The ordering of the poke and the clearing of the bit by cpu_wakeup
159 	 * is important.
160 	 * cpu_wakeup() must clear, then poke.
161 	 * cpu_halt() must disable interrupts, then check for the bit.
162 	 *
163 	 * The check for anything locally runnable is here for performance
164 	 * and isn't needed for correctness. disp_nrunnable ought to be
165 	 * in our cache still, so it's inexpensive to check, and if there
166 	 * is anything runnable we won't have to wait for the poke.
167 	 *
168 	 */
169 	s = disable_vec_intr();
170 	while (*p == 0 &&
171 	    ((hset_update && CPU_IN_SET(cp->cp_mach->mc_haltset, cpun)) ||
172 	    (!hset_update && (CPU->cpu_flags & CPU_OFFLINE)))) {
173 		(void) hv_cpu_yield();
174 		enable_vec_intr(s);
175 		s = disable_vec_intr();
176 	}
177 
178 	/*
179 	 * We're no longer halted
180 	 */
181 	enable_vec_intr(s);
182 	if (hset_update) {
183 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
184 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpun);
185 	}
186 }
187 
188 /*
189  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
190  * Otherwise, see if other CPUs in the cpu partition are halted and need to
191  * be woken up so that they can steal the thread we placed on this CPU.
192  * This function is only used on MP systems.
193  */
194 static void
195 cpu_wakeup(cpu_t *cpu, int bound)
196 {
197 	uint_t		cpu_found;
198 	int		result;
199 	cpupart_t	*cp;
200 
201 	cp = cpu->cpu_part;
202 	if (CPU_IN_SET(cp->cp_mach->mc_haltset, cpu->cpu_id)) {
203 		/*
204 		 * Clear the halted bit for that CPU since it will be
205 		 * poked in a moment.
206 		 */
207 		CPUSET_ATOMIC_DEL(cp->cp_mach->mc_haltset, cpu->cpu_id);
208 		/*
209 		 * We may find the current CPU present in the halted cpuset
210 		 * if we're in the context of an interrupt that occurred
211 		 * before we had a chance to clear our bit in cpu_halt().
212 		 * Poking ourself is obviously unnecessary, since if
213 		 * we're here, we're not halted.
214 		 */
215 		if (cpu != CPU)
216 			poke_cpu(cpu->cpu_id);
217 		return;
218 	} else {
219 		/*
220 		 * This cpu isn't halted, but it's idle or undergoing a
221 		 * context switch. No need to awaken anyone else.
222 		 */
223 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
224 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
225 			return;
226 	}
227 
228 	/*
229 	 * No need to wake up other CPUs if the thread we just enqueued
230 	 * is bound.
231 	 */
232 	if (bound)
233 		return;
234 
235 	/*
236 	 * See if there's any other halted CPUs. If there are, then
237 	 * select one, and awaken it.
238 	 * It's possible that after we find a CPU, somebody else
239 	 * will awaken it before we get the chance.
240 	 * In that case, look again.
241 	 */
242 	do {
243 		CPUSET_FIND(cp->cp_mach->mc_haltset, cpu_found);
244 		if (cpu_found == CPUSET_NOTINSET)
245 			return;
246 
247 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
248 		CPUSET_ATOMIC_XDEL(cp->cp_mach->mc_haltset, cpu_found, result);
249 	} while (result < 0);
250 
251 	if (cpu_found != CPU->cpu_id)
252 		poke_cpu(cpu_found);
253 }
254 
255 void
256 mach_cpu_halt_idle()
257 {
258 	if (enable_halt_idle_cpus) {
259 		idle_cpu = cpu_halt;
260 		disp_enq_thread = cpu_wakeup;
261 	}
262 }
263 
264 int
265 ndata_alloc_mmfsa(struct memlist *ndata)
266 {
267 	size_t	size;
268 
269 	size = MMFSA_SIZE * max_ncpus;
270 	mmu_fault_status_area = ndata_alloc(ndata, size, ecache_alignsize);
271 	if (mmu_fault_status_area == NULL)
272 		return (-1);
273 	return (0);
274 }
275 
276 void
277 mach_memscrub(void)
278 {
279 	/* no memscrub support for sun4v for now */
280 }
281 
282 void
283 mach_fpras()
284 {
285 	/* no fpras support for sun4v for now */
286 }
287 
288 void
289 mach_hw_copy_limit(void)
290 {
291 	/* HW copy limits set by individual CPU module */
292 }
293 
294 /*
295  * We need to enable soft ring functionality on Niagara platform since
296  * one strand can't handle interrupts for a 1Gb NIC. Set the tunable
297  * ip_squeue_soft_ring by default on this platform. We can also set
298  * ip_threads_per_cpu to track number of threads per core. The variables
299  * themselves are defined in space.c and used by IP module
300  */
301 extern uint_t ip_threads_per_cpu;
302 extern boolean_t ip_squeue_soft_ring;
303 void
304 startup_platform(void)
305 {
306 	ip_squeue_soft_ring = B_TRUE;
307 	if (clock_tick_threshold == 0)
308 		clock_tick_threshold = SUN4V_CLOCK_TICK_THRESHOLD;
309 	if (clock_tick_ncpus == 0)
310 		clock_tick_ncpus = SUN4V_CLOCK_TICK_NCPUS;
311 	/* set per-platform constants for mutex_backoff */
312 	mutex_backoff_base = 1;
313 	mutex_cap_factor = 4;
314 	if (l2_cache_node_count() > 1) {
315 		/* VF for example */
316 		mutex_backoff_base = 2;
317 		mutex_cap_factor = 16;
318 	}
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_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_enabled() && modload("misc", "dr_cpu") == -1)
478 		cmn_err(CE_NOTE, "!'dr_cpu' module failed to load");
479 
480 	/*
481 	 * Attempt to attach any virtual device servers. These
482 	 * drivers must be loaded at start of day so that they
483 	 * can respond to any updates to the machine description.
484 	 *
485 	 * Since it is quite likely that a domain will not support
486 	 * one or more of these servers, failures are ignored.
487 	 */
488 
489 	/* virtual disk server */
490 	(void) i_ddi_attach_hw_nodes("vds");
491 
492 	/* virtual network switch */
493 	(void) i_ddi_attach_hw_nodes("vsw");
494 
495 	/* virtual console concentrator */
496 	(void) i_ddi_attach_hw_nodes("vcc");
497 }
498