xref: /titanic_50/usr/src/uts/i86pc/os/mp_machdep.c (revision 0a0e9771ca0211c15f3ac4466b661c145feeb9e4)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2009, Intel Corporation.
27  * All rights reserved.
28  */
29 
30 #define	PSMI_1_6
31 #include <sys/smp_impldefs.h>
32 #include <sys/psm.h>
33 #include <sys/psm_modctl.h>
34 #include <sys/pit.h>
35 #include <sys/cmn_err.h>
36 #include <sys/strlog.h>
37 #include <sys/clock.h>
38 #include <sys/debug.h>
39 #include <sys/rtc.h>
40 #include <sys/x86_archext.h>
41 #include <sys/cpupart.h>
42 #include <sys/cpuvar.h>
43 #include <sys/cpu_event.h>
44 #include <sys/cmt.h>
45 #include <sys/cpu.h>
46 #include <sys/disp.h>
47 #include <sys/archsystm.h>
48 #include <sys/machsystm.h>
49 #include <sys/sysmacros.h>
50 #include <sys/memlist.h>
51 #include <sys/param.h>
52 #include <sys/promif.h>
53 #include <sys/cpu_pm.h>
54 #if defined(__xpv)
55 #include <sys/hypervisor.h>
56 #endif
57 #include <sys/mach_intr.h>
58 #include <vm/hat_i86.h>
59 #include <sys/kdi_machimpl.h>
60 #include <sys/sdt.h>
61 #include <sys/hpet.h>
62 #include <sys/sunddi.h>
63 #include <sys/sunndi.h>
64 
65 #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
66 
67 /*
68  *	Local function prototypes
69  */
70 static int mp_disable_intr(processorid_t cpun);
71 static void mp_enable_intr(processorid_t cpun);
72 static void mach_init();
73 static void mach_picinit();
74 static int machhztomhz(uint64_t cpu_freq_hz);
75 static uint64_t mach_getcpufreq(void);
76 static void mach_fixcpufreq(void);
77 static int mach_clkinit(int, int *);
78 static void mach_smpinit(void);
79 static int mach_softlvl_to_vect(int ipl);
80 static void mach_get_platform(int owner);
81 static void mach_construct_info();
82 static int mach_translate_irq(dev_info_t *dip, int irqno);
83 static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
84     psm_intr_op_t, int *);
85 static void mach_notify_error(int level, char *errmsg);
86 static hrtime_t dummy_hrtime(void);
87 static void dummy_scalehrtime(hrtime_t *);
88 void cpu_idle(void);
89 static void cpu_wakeup(cpu_t *, int);
90 #ifndef __xpv
91 void cpu_idle_mwait(void);
92 static void cpu_wakeup_mwait(cpu_t *, int);
93 #endif
94 static int mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp);
95 
96 /*
97  *	External reference functions
98  */
99 extern void return_instr();
100 extern uint64_t freq_tsc(uint32_t *);
101 #if defined(__i386)
102 extern uint64_t freq_notsc(uint32_t *);
103 #endif
104 extern void pc_gethrestime(timestruc_t *);
105 extern int cpuid_get_coreid(cpu_t *);
106 extern int cpuid_get_chipid(cpu_t *);
107 
108 /*
109  *	PSM functions initialization
110  */
111 void (*psm_shutdownf)(int, int)	= (void (*)(int, int))return_instr;
112 void (*psm_preshutdownf)(int, int) = (void (*)(int, int))return_instr;
113 void (*psm_notifyf)(int)	= (void (*)(int))return_instr;
114 void (*psm_set_idle_cpuf)(int)	= (void (*)(int))return_instr;
115 void (*psm_unset_idle_cpuf)(int) = (void (*)(int))return_instr;
116 void (*psminitf)()		= mach_init;
117 void (*picinitf)() 		= return_instr;
118 int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
119 int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
120 void (*send_dirintf)() 		= return_instr;
121 void (*setspl)(int)		= (void (*)(int))return_instr;
122 int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
123 int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
124 void (*kdisetsoftint)(int, struct av_softinfo *)=
125 	(void (*)(int, struct av_softinfo *))return_instr;
126 void (*setsoftint)(int, struct av_softinfo *)=
127 	(void (*)(int, struct av_softinfo *))return_instr;
128 int (*slvltovect)(int)		= (int (*)(int))return_instr;
129 int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
130 void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
131 int (*psm_disable_intr)(int)	= mp_disable_intr;
132 void (*psm_enable_intr)(int)	= mp_enable_intr;
133 hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
134 hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
135 void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
136 int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
137 void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
138 void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
139 int (*psm_get_clockirq)(int) = NULL;
140 int (*psm_get_ipivect)(int, int) = NULL;
141 
142 int (*psm_clkinit)(int) = NULL;
143 void (*psm_timer_reprogram)(hrtime_t) = NULL;
144 void (*psm_timer_enable)(void) = NULL;
145 void (*psm_timer_disable)(void) = NULL;
146 void (*psm_post_cyclic_setup)(void *arg) = NULL;
147 int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
148     int *) = mach_intr_ops;
149 int (*psm_state)(psm_state_request_t *) = (int (*)(psm_state_request_t *))
150     return_instr;
151 
152 void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
153 void (*hrtime_tick)(void)	= return_instr;
154 
155 int (*psm_cpu_create_devinfo)(cpu_t *, dev_info_t **) = mach_cpu_create_devinfo;
156 
157 /*
158  * True if the generic TSC code is our source of hrtime, rather than whatever
159  * the PSM can provide.
160  */
161 #ifdef __xpv
162 int tsc_gethrtime_enable = 0;
163 #else
164 int tsc_gethrtime_enable = 1;
165 #endif
166 int tsc_gethrtime_initted = 0;
167 
168 /*
169  * True if the hrtime implementation is "hires"; namely, better than microdata.
170  */
171 int gethrtime_hires = 0;
172 
173 /*
174  * Local Static Data
175  */
176 static struct psm_ops mach_ops;
177 static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
178 static ushort_t mach_ver[4] = {0, 0, 0, 0};
179 
180 /*
181  * virtualization support for psm
182  */
183 void *psm_vt_ops = NULL;
184 /*
185  * If non-zero, idle cpus will become "halted" when there's
186  * no work to do.
187  */
188 int	idle_cpu_use_hlt = 1;
189 
190 #ifndef __xpv
191 /*
192  * If non-zero, idle cpus will use mwait if available to halt instead of hlt.
193  */
194 int	idle_cpu_prefer_mwait = 1;
195 /*
196  * Set to 0 to avoid MONITOR+CLFLUSH assertion.
197  */
198 int	idle_cpu_assert_cflush_monitor = 1;
199 
200 /*
201  * If non-zero, idle cpus will not use power saving Deep C-States idle loop.
202  */
203 int	idle_cpu_no_deep_c = 0;
204 /*
205  * Non-power saving idle loop and wakeup pointers.
206  * Allows user to toggle Deep Idle power saving feature on/off.
207  */
208 void	(*non_deep_idle_cpu)() = cpu_idle;
209 void	(*non_deep_idle_disp_enq_thread)(cpu_t *, int);
210 
211 /*
212  * Object for the kernel to access the HPET.
213  */
214 hpet_t hpet;
215 
216 #endif	/* ifndef __xpv */
217 
218 /*ARGSUSED*/
219 int
220 pg_plat_hw_shared(cpu_t *cp, pghw_type_t hw)
221 {
222 	switch (hw) {
223 	case PGHW_IPIPE:
224 		if (x86_feature & (X86_HTT)) {
225 			/*
226 			 * Hyper-threading is SMT
227 			 */
228 			return (1);
229 		} else {
230 			return (0);
231 		}
232 	case PGHW_CHIP:
233 		if (x86_feature & (X86_CMP|X86_HTT))
234 			return (1);
235 		else
236 			return (0);
237 	case PGHW_CACHE:
238 		if (cpuid_get_ncpu_sharing_last_cache(cp) > 1)
239 			return (1);
240 		else
241 			return (0);
242 	case PGHW_POW_ACTIVE:
243 		if (cpupm_domain_id(cp, CPUPM_DTYPE_ACTIVE) != (id_t)-1)
244 			return (1);
245 		else
246 			return (0);
247 	case PGHW_POW_IDLE:
248 		if (cpupm_domain_id(cp, CPUPM_DTYPE_IDLE) != (id_t)-1)
249 			return (1);
250 		else
251 			return (0);
252 	default:
253 		return (0);
254 	}
255 }
256 
257 /*
258  * Compare two CPUs and see if they have a pghw_type_t sharing relationship
259  * If pghw_type_t is an unsupported hardware type, then return -1
260  */
261 int
262 pg_plat_cpus_share(cpu_t *cpu_a, cpu_t *cpu_b, pghw_type_t hw)
263 {
264 	id_t pgp_a, pgp_b;
265 
266 	pgp_a = pg_plat_hw_instance_id(cpu_a, hw);
267 	pgp_b = pg_plat_hw_instance_id(cpu_b, hw);
268 
269 	if (pgp_a == -1 || pgp_b == -1)
270 		return (-1);
271 
272 	return (pgp_a == pgp_b);
273 }
274 
275 /*
276  * Return a physical instance identifier for known hardware sharing
277  * relationships
278  */
279 id_t
280 pg_plat_hw_instance_id(cpu_t *cpu, pghw_type_t hw)
281 {
282 	switch (hw) {
283 	case PGHW_IPIPE:
284 		return (cpuid_get_coreid(cpu));
285 	case PGHW_CACHE:
286 		return (cpuid_get_last_lvl_cacheid(cpu));
287 	case PGHW_CHIP:
288 		return (cpuid_get_chipid(cpu));
289 	case PGHW_POW_ACTIVE:
290 		return (cpupm_domain_id(cpu, CPUPM_DTYPE_ACTIVE));
291 	case PGHW_POW_IDLE:
292 		return (cpupm_domain_id(cpu, CPUPM_DTYPE_IDLE));
293 	default:
294 		return (-1);
295 	}
296 }
297 
298 /*
299  * Express preference for optimizing for sharing relationship
300  * hw1 vs hw2
301  */
302 pghw_type_t
303 pg_plat_hw_rank(pghw_type_t hw1, pghw_type_t hw2)
304 {
305 	int i, rank1, rank2;
306 
307 	static pghw_type_t hw_hier[] = {
308 		PGHW_IPIPE,
309 		PGHW_CACHE,
310 		PGHW_CHIP,
311 		PGHW_POW_IDLE,
312 		PGHW_POW_ACTIVE,
313 		PGHW_NUM_COMPONENTS
314 	};
315 
316 	for (i = 0; hw_hier[i] != PGHW_NUM_COMPONENTS; i++) {
317 		if (hw_hier[i] == hw1)
318 			rank1 = i;
319 		if (hw_hier[i] == hw2)
320 			rank2 = i;
321 	}
322 
323 	if (rank1 > rank2)
324 		return (hw1);
325 	else
326 		return (hw2);
327 }
328 
329 /*
330  * Override the default CMT dispatcher policy for the specified
331  * hardware sharing relationship
332  */
333 pg_cmt_policy_t
334 pg_plat_cmt_policy(pghw_type_t hw)
335 {
336 	/*
337 	 * For shared caches, also load balance across them to
338 	 * maximize aggregate cache capacity
339 	 */
340 	switch (hw) {
341 	case PGHW_CACHE:
342 		return (CMT_BALANCE|CMT_AFFINITY);
343 	default:
344 		return (CMT_NO_POLICY);
345 	}
346 }
347 
348 id_t
349 pg_plat_get_core_id(cpu_t *cpu)
350 {
351 	return ((id_t)cpuid_get_coreid(cpu));
352 }
353 
354 void
355 cmp_set_nosteal_interval(void)
356 {
357 	/* Set the nosteal interval (used by disp_getbest()) to 100us */
358 	nosteal_nsec = 100000UL;
359 }
360 
361 /*
362  * Routine to ensure initial callers to hrtime gets 0 as return
363  */
364 static hrtime_t
365 dummy_hrtime(void)
366 {
367 	return (0);
368 }
369 
370 /* ARGSUSED */
371 static void
372 dummy_scalehrtime(hrtime_t *ticks)
373 {}
374 
375 /*
376  * Supports Deep C-State power saving idle loop.
377  */
378 void
379 cpu_idle_adaptive(void)
380 {
381 	(*CPU->cpu_m.mcpu_idle_cpu)();
382 }
383 
384 /*
385  * Function called by CPU idle notification framework to check whether CPU
386  * has been awakened. It will be called with interrupt disabled.
387  * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
388  * notification framework.
389  */
390 /*ARGSUSED*/
391 static void
392 cpu_idle_check_wakeup(void *arg)
393 {
394 	/*
395 	 * Toggle interrupt flag to detect pending interrupts.
396 	 * If interrupt happened, do_interrupt() will notify CPU idle
397 	 * notification framework so no need to call cpu_idle_exit() here.
398 	 */
399 	sti();
400 	SMT_PAUSE();
401 	cli();
402 }
403 
404 /*
405  * Idle the present CPU until wakened via an interrupt
406  */
407 void
408 cpu_idle(void)
409 {
410 	cpu_t		*cpup = CPU;
411 	processorid_t	cpu_sid = cpup->cpu_seqid;
412 	cpupart_t	*cp = cpup->cpu_part;
413 	int		hset_update = 1;
414 
415 	/*
416 	 * If this CPU is online, and there's multiple CPUs
417 	 * in the system, then we should notate our halting
418 	 * by adding ourselves to the partition's halted CPU
419 	 * bitmap. This allows other CPUs to find/awaken us when
420 	 * work becomes available.
421 	 */
422 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
423 		hset_update = 0;
424 
425 	/*
426 	 * Add ourselves to the partition's halted CPUs bitmap
427 	 * and set our HALTED flag, if necessary.
428 	 *
429 	 * When a thread becomes runnable, it is placed on the queue
430 	 * and then the halted CPU bitmap is checked to determine who
431 	 * (if anyone) should be awakened. We therefore need to first
432 	 * add ourselves to the bitmap, and and then check if there
433 	 * is any work available. The order is important to prevent a race
434 	 * that can lead to work languishing on a run queue somewhere while
435 	 * this CPU remains halted.
436 	 *
437 	 * Either the producing CPU will see we're halted and will awaken us,
438 	 * or this CPU will see the work available in disp_anywork().
439 	 *
440 	 * Note that memory barriers after updating the HALTED flag
441 	 * are not necessary since an atomic operation (updating the bitset)
442 	 * immediately follows. On x86 the atomic operation acts as a
443 	 * memory barrier for the update of cpu_disp_flags.
444 	 */
445 	if (hset_update) {
446 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
447 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
448 	}
449 
450 	/*
451 	 * Check to make sure there's really nothing to do.
452 	 * Work destined for this CPU may become available after
453 	 * this check. We'll be notified through the clearing of our
454 	 * bit in the halted CPU bitmap, and a poke.
455 	 */
456 	if (disp_anywork()) {
457 		if (hset_update) {
458 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
459 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
460 		}
461 		return;
462 	}
463 
464 	/*
465 	 * We're on our way to being halted.
466 	 *
467 	 * Disable interrupts now, so that we'll awaken immediately
468 	 * after halting if someone tries to poke us between now and
469 	 * the time we actually halt.
470 	 *
471 	 * We check for the presence of our bit after disabling interrupts.
472 	 * If it's cleared, we'll return. If the bit is cleared after
473 	 * we check then the poke will pop us out of the halted state.
474 	 *
475 	 * This means that the ordering of the poke and the clearing
476 	 * of the bit by cpu_wakeup is important.
477 	 * cpu_wakeup() must clear, then poke.
478 	 * cpu_idle() must disable interrupts, then check for the bit.
479 	 */
480 	cli();
481 
482 	if (hset_update && bitset_in_set(&cp->cp_haltset, cpu_sid) == 0) {
483 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
484 		sti();
485 		return;
486 	}
487 
488 	/*
489 	 * The check for anything locally runnable is here for performance
490 	 * and isn't needed for correctness. disp_nrunnable ought to be
491 	 * in our cache still, so it's inexpensive to check, and if there
492 	 * is anything runnable we won't have to wait for the poke.
493 	 */
494 	if (cpup->cpu_disp->disp_nrunnable != 0) {
495 		if (hset_update) {
496 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
497 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
498 		}
499 		sti();
500 		return;
501 	}
502 
503 	if (cpu_idle_enter(IDLE_STATE_C1, 0,
504 	    cpu_idle_check_wakeup, NULL) == 0) {
505 		mach_cpu_idle();
506 		cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
507 	}
508 
509 	/*
510 	 * We're no longer halted
511 	 */
512 	if (hset_update) {
513 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
514 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
515 	}
516 }
517 
518 
519 /*
520  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
521  * Otherwise, see if other CPUs in the cpu partition are halted and need to
522  * be woken up so that they can steal the thread we placed on this CPU.
523  * This function is only used on MP systems.
524  */
525 static void
526 cpu_wakeup(cpu_t *cpu, int bound)
527 {
528 	uint_t		cpu_found;
529 	processorid_t	cpu_sid;
530 	cpupart_t	*cp;
531 
532 	cp = cpu->cpu_part;
533 	cpu_sid = cpu->cpu_seqid;
534 	if (bitset_in_set(&cp->cp_haltset, cpu_sid)) {
535 		/*
536 		 * Clear the halted bit for that CPU since it will be
537 		 * poked in a moment.
538 		 */
539 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
540 		/*
541 		 * We may find the current CPU present in the halted cpuset
542 		 * if we're in the context of an interrupt that occurred
543 		 * before we had a chance to clear our bit in cpu_idle().
544 		 * Poking ourself is obviously unnecessary, since if
545 		 * we're here, we're not halted.
546 		 */
547 		if (cpu != CPU)
548 			poke_cpu(cpu->cpu_id);
549 		return;
550 	} else {
551 		/*
552 		 * This cpu isn't halted, but it's idle or undergoing a
553 		 * context switch. No need to awaken anyone else.
554 		 */
555 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
556 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
557 			return;
558 	}
559 
560 	/*
561 	 * No need to wake up other CPUs if this is for a bound thread.
562 	 */
563 	if (bound)
564 		return;
565 
566 	/*
567 	 * The CPU specified for wakeup isn't currently halted, so check
568 	 * to see if there are any other halted CPUs in the partition,
569 	 * and if there are then awaken one.
570 	 */
571 	do {
572 		cpu_found = bitset_find(&cp->cp_haltset);
573 		if (cpu_found == (uint_t)-1)
574 			return;
575 	} while (bitset_atomic_test_and_del(&cp->cp_haltset, cpu_found) < 0);
576 
577 	if (cpu_found != CPU->cpu_seqid) {
578 		poke_cpu(cpu_seq[cpu_found]->cpu_id);
579 	}
580 }
581 
582 #ifndef __xpv
583 /*
584  * Function called by CPU idle notification framework to check whether CPU
585  * has been awakened. It will be called with interrupt disabled.
586  * If CPU has been awakened, call cpu_idle_exit() to notify CPU idle
587  * notification framework.
588  */
589 static void
590 cpu_idle_mwait_check_wakeup(void *arg)
591 {
592 	volatile uint32_t *mcpu_mwait = (volatile uint32_t *)arg;
593 
594 	ASSERT(arg != NULL);
595 	if (*mcpu_mwait != MWAIT_HALTED) {
596 		/*
597 		 * CPU has been awakened, notify CPU idle notification system.
598 		 */
599 		cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
600 	} else {
601 		/*
602 		 * Toggle interrupt flag to detect pending interrupts.
603 		 * If interrupt happened, do_interrupt() will notify CPU idle
604 		 * notification framework so no need to call cpu_idle_exit()
605 		 * here.
606 		 */
607 		sti();
608 		SMT_PAUSE();
609 		cli();
610 	}
611 }
612 
613 /*
614  * Idle the present CPU until awakened via touching its monitored line
615  */
616 void
617 cpu_idle_mwait(void)
618 {
619 	volatile uint32_t	*mcpu_mwait = CPU->cpu_m.mcpu_mwait;
620 	cpu_t			*cpup = CPU;
621 	processorid_t		cpu_sid = cpup->cpu_seqid;
622 	cpupart_t		*cp = cpup->cpu_part;
623 	int			hset_update = 1;
624 
625 	/*
626 	 * Set our mcpu_mwait here, so we can tell if anyone tries to
627 	 * wake us between now and when we call mwait.  No other cpu will
628 	 * attempt to set our mcpu_mwait until we add ourself to the halted
629 	 * CPU bitmap.
630 	 */
631 	*mcpu_mwait = MWAIT_HALTED;
632 
633 	/*
634 	 * If this CPU is online, and there's multiple CPUs
635 	 * in the system, then we should note our halting
636 	 * by adding ourselves to the partition's halted CPU
637 	 * bitmap. This allows other CPUs to find/awaken us when
638 	 * work becomes available.
639 	 */
640 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
641 		hset_update = 0;
642 
643 	/*
644 	 * Add ourselves to the partition's halted CPUs bitmap
645 	 * and set our HALTED flag, if necessary.
646 	 *
647 	 * When a thread becomes runnable, it is placed on the queue
648 	 * and then the halted CPU bitmap is checked to determine who
649 	 * (if anyone) should be awakened. We therefore need to first
650 	 * add ourselves to the bitmap, and and then check if there
651 	 * is any work available.
652 	 *
653 	 * Note that memory barriers after updating the HALTED flag
654 	 * are not necessary since an atomic operation (updating the bitmap)
655 	 * immediately follows. On x86 the atomic operation acts as a
656 	 * memory barrier for the update of cpu_disp_flags.
657 	 */
658 	if (hset_update) {
659 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
660 		bitset_atomic_add(&cp->cp_haltset, cpu_sid);
661 	}
662 
663 	/*
664 	 * Check to make sure there's really nothing to do.
665 	 * Work destined for this CPU may become available after
666 	 * this check. We'll be notified through the clearing of our
667 	 * bit in the halted CPU bitmap, and a write to our mcpu_mwait.
668 	 *
669 	 * disp_anywork() checks disp_nrunnable, so we do not have to later.
670 	 */
671 	if (disp_anywork()) {
672 		if (hset_update) {
673 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
674 			bitset_atomic_del(&cp->cp_haltset, cpu_sid);
675 		}
676 		return;
677 	}
678 
679 	/*
680 	 * We're on our way to being halted.
681 	 * To avoid a lost wakeup, arm the monitor before checking if another
682 	 * cpu wrote to mcpu_mwait to wake us up.
683 	 */
684 	i86_monitor(mcpu_mwait, 0, 0);
685 	if (*mcpu_mwait == MWAIT_HALTED) {
686 		if (cpu_idle_enter(IDLE_STATE_C1, 0,
687 		    cpu_idle_mwait_check_wakeup, (void *)mcpu_mwait) == 0) {
688 			if (*mcpu_mwait == MWAIT_HALTED) {
689 				i86_mwait(0, 0);
690 			}
691 			cpu_idle_exit(CPU_IDLE_CB_FLAG_IDLE);
692 		}
693 	}
694 
695 	/*
696 	 * We're no longer halted
697 	 */
698 	if (hset_update) {
699 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
700 		bitset_atomic_del(&cp->cp_haltset, cpu_sid);
701 	}
702 }
703 
704 /*
705  * If "cpu" is halted in mwait, then wake it up clearing its halted bit in
706  * advance.  Otherwise, see if other CPUs in the cpu partition are halted and
707  * need to be woken up so that they can steal the thread we placed on this CPU.
708  * This function is only used on MP systems.
709  */
710 static void
711 cpu_wakeup_mwait(cpu_t *cp, int bound)
712 {
713 	cpupart_t	*cpu_part;
714 	uint_t		cpu_found;
715 	processorid_t	cpu_sid;
716 
717 	cpu_part = cp->cpu_part;
718 	cpu_sid = cp->cpu_seqid;
719 
720 	/*
721 	 * Clear the halted bit for that CPU since it will be woken up
722 	 * in a moment.
723 	 */
724 	if (bitset_in_set(&cpu_part->cp_haltset, cpu_sid)) {
725 		/*
726 		 * Clear the halted bit for that CPU since it will be
727 		 * poked in a moment.
728 		 */
729 		bitset_atomic_del(&cpu_part->cp_haltset, cpu_sid);
730 		/*
731 		 * We may find the current CPU present in the halted cpuset
732 		 * if we're in the context of an interrupt that occurred
733 		 * before we had a chance to clear our bit in cpu_idle().
734 		 * Waking ourself is obviously unnecessary, since if
735 		 * we're here, we're not halted.
736 		 *
737 		 * monitor/mwait wakeup via writing to our cache line is
738 		 * harmless and less expensive than always checking if we
739 		 * are waking ourself which is an uncommon case.
740 		 */
741 		MWAIT_WAKEUP(cp);	/* write to monitored line */
742 		return;
743 	} else {
744 		/*
745 		 * This cpu isn't halted, but it's idle or undergoing a
746 		 * context switch. No need to awaken anyone else.
747 		 */
748 		if (cp->cpu_thread == cp->cpu_idle_thread ||
749 		    cp->cpu_disp_flags & CPU_DISP_DONTSTEAL)
750 			return;
751 	}
752 
753 	/*
754 	 * No need to wake up other CPUs if the thread we just enqueued
755 	 * is bound.
756 	 */
757 	if (bound || ncpus == 1)
758 		return;
759 
760 	/*
761 	 * See if there's any other halted CPUs. If there are, then
762 	 * select one, and awaken it.
763 	 * It's possible that after we find a CPU, somebody else
764 	 * will awaken it before we get the chance.
765 	 * In that case, look again.
766 	 */
767 	do {
768 		cpu_found = bitset_find(&cpu_part->cp_haltset);
769 		if (cpu_found == (uint_t)-1)
770 			return;
771 	} while (bitset_atomic_test_and_del(&cpu_part->cp_haltset,
772 	    cpu_found) < 0);
773 
774 	/*
775 	 * Do not check if cpu_found is ourself as monitor/mwait
776 	 * wakeup is cheap.
777 	 */
778 	MWAIT_WAKEUP(cpu_seq[cpu_found]); /* write to monitored line */
779 }
780 
781 #endif
782 
783 void (*cpu_pause_handler)(volatile char *) = NULL;
784 
785 static int
786 mp_disable_intr(int cpun)
787 {
788 	/*
789 	 * switch to the offline cpu
790 	 */
791 	affinity_set(cpun);
792 	/*
793 	 * raise ipl to just below cross call
794 	 */
795 	splx(XC_SYS_PIL - 1);
796 	/*
797 	 *	set base spl to prevent the next swtch to idle from
798 	 *	lowering back to ipl 0
799 	 */
800 	CPU->cpu_intr_actv |= (1 << (XC_SYS_PIL - 1));
801 	set_base_spl();
802 	affinity_clear();
803 	return (DDI_SUCCESS);
804 }
805 
806 static void
807 mp_enable_intr(int cpun)
808 {
809 	/*
810 	 * switch to the online cpu
811 	 */
812 	affinity_set(cpun);
813 	/*
814 	 * clear the interrupt active mask
815 	 */
816 	CPU->cpu_intr_actv &= ~(1 << (XC_SYS_PIL - 1));
817 	set_base_spl();
818 	(void) spl0();
819 	affinity_clear();
820 }
821 
822 static void
823 mach_get_platform(int owner)
824 {
825 	void		**srv_opsp;
826 	void		**clt_opsp;
827 	int		i;
828 	int		total_ops;
829 
830 	/* fix up psm ops */
831 	srv_opsp = (void **)mach_set[0];
832 	clt_opsp = (void **)mach_set[owner];
833 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
834 		total_ops = sizeof (struct psm_ops_ver01) /
835 		    sizeof (void (*)(void));
836 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
837 		/* no psm_notify_func */
838 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
839 		    sizeof (void (*)(void));
840 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
841 		/* no psm_timer funcs */
842 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
843 		    sizeof (void (*)(void));
844 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
845 		/* no psm_preshutdown function */
846 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
847 		    sizeof (void (*)(void));
848 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
849 		/* no psm_preshutdown function */
850 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
851 		    sizeof (void (*)(void));
852 	else
853 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
854 
855 	/*
856 	 * Save the version of the PSM module, in case we need to
857 	 * behave differently based on version.
858 	 */
859 	mach_ver[0] = mach_ver[owner];
860 
861 	for (i = 0; i < total_ops; i++)
862 		if (clt_opsp[i] != NULL)
863 			srv_opsp[i] = clt_opsp[i];
864 }
865 
866 static void
867 mach_construct_info()
868 {
869 	struct psm_sw *swp;
870 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
871 	int	conflict_owner = 0;
872 
873 	if (psmsw->psw_forw == psmsw)
874 		panic("No valid PSM modules found");
875 	mutex_enter(&psmsw_lock);
876 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
877 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
878 			continue;
879 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
880 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
881 		mach_cnt[swp->psw_infop->p_owner]++;
882 	}
883 	mutex_exit(&psmsw_lock);
884 
885 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
886 
887 	/* check to see are there any conflicts */
888 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
889 		conflict_owner = PSM_OWN_EXCLUSIVE;
890 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
891 		conflict_owner = PSM_OWN_OVERRIDE;
892 	if (conflict_owner) {
893 		/* remove all psm modules except uppc */
894 		cmn_err(CE_WARN,
895 		    "Conflicts detected on the following PSM modules:");
896 		mutex_enter(&psmsw_lock);
897 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
898 			if (swp->psw_infop->p_owner == conflict_owner)
899 				cmn_err(CE_WARN, "%s ",
900 				    swp->psw_infop->p_mach_idstring);
901 		}
902 		mutex_exit(&psmsw_lock);
903 		cmn_err(CE_WARN,
904 		    "Setting the system back to SINGLE processor mode!");
905 		cmn_err(CE_WARN,
906 		    "Please edit /etc/mach to remove the invalid PSM module.");
907 		return;
908 	}
909 
910 	if (mach_set[PSM_OWN_EXCLUSIVE])
911 		mach_get_platform(PSM_OWN_EXCLUSIVE);
912 
913 	if (mach_set[PSM_OWN_OVERRIDE])
914 		mach_get_platform(PSM_OWN_OVERRIDE);
915 }
916 
917 static void
918 mach_init()
919 {
920 	struct psm_ops  *pops;
921 
922 	mach_construct_info();
923 
924 	pops = mach_set[0];
925 
926 	/* register the interrupt and clock initialization rotuines */
927 	picinitf = mach_picinit;
928 	clkinitf = mach_clkinit;
929 	psm_get_clockirq = pops->psm_get_clockirq;
930 
931 	/* register the interrupt setup code */
932 	slvltovect = mach_softlvl_to_vect;
933 	addspl	= pops->psm_addspl;
934 	delspl	= pops->psm_delspl;
935 
936 	if (pops->psm_translate_irq)
937 		psm_translate_irq = pops->psm_translate_irq;
938 	if (pops->psm_intr_ops)
939 		psm_intr_ops = pops->psm_intr_ops;
940 
941 #if defined(PSMI_1_2) || defined(PSMI_1_3) || defined(PSMI_1_4)
942 	/*
943 	 * Time-of-day functionality now handled in TOD modules.
944 	 * (Warn about PSM modules that think that we're going to use
945 	 * their ops vectors.)
946 	 */
947 	if (pops->psm_tod_get)
948 		cmn_err(CE_WARN, "obsolete psm_tod_get op %p",
949 		    (void *)pops->psm_tod_get);
950 
951 	if (pops->psm_tod_set)
952 		cmn_err(CE_WARN, "obsolete psm_tod_set op %p",
953 		    (void *)pops->psm_tod_set);
954 #endif
955 
956 	if (pops->psm_notify_error) {
957 		psm_notify_error = mach_notify_error;
958 		notify_error = pops->psm_notify_error;
959 	}
960 
961 	(*pops->psm_softinit)();
962 
963 	/*
964 	 * Initialize the dispatcher's function hooks to enable CPU halting
965 	 * when idle.  Set both the deep-idle and non-deep-idle hooks.
966 	 *
967 	 * Assume we can use power saving deep-idle loop cpu_idle_adaptive.
968 	 * Platform deep-idle driver will reset our idle loop to
969 	 * non_deep_idle_cpu if power saving deep-idle feature is not available.
970 	 *
971 	 * Do not use monitor/mwait if idle_cpu_use_hlt is not set(spin idle)
972 	 * or idle_cpu_prefer_mwait is not set.
973 	 * Allocate monitor/mwait buffer for cpu0.
974 	 */
975 #ifndef __xpv
976 	non_deep_idle_disp_enq_thread = disp_enq_thread;
977 #endif
978 	if (idle_cpu_use_hlt) {
979 		idle_cpu = cpu_idle_adaptive;
980 		CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
981 #ifndef __xpv
982 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait) {
983 			CPU->cpu_m.mcpu_mwait = cpuid_mwait_alloc(CPU);
984 			/*
985 			 * Protect ourself from insane mwait size.
986 			 */
987 			if (CPU->cpu_m.mcpu_mwait == NULL) {
988 #ifdef DEBUG
989 				cmn_err(CE_NOTE, "Using hlt idle.  Cannot "
990 				    "handle cpu 0 mwait size.");
991 #endif
992 				idle_cpu_prefer_mwait = 0;
993 				CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
994 			} else {
995 				CPU->cpu_m.mcpu_idle_cpu = cpu_idle_mwait;
996 			}
997 		} else {
998 			CPU->cpu_m.mcpu_idle_cpu = cpu_idle;
999 		}
1000 		non_deep_idle_cpu = CPU->cpu_m.mcpu_idle_cpu;
1001 
1002 		/*
1003 		 * Disable power saving deep idle loop?
1004 		 */
1005 		if (idle_cpu_no_deep_c) {
1006 			idle_cpu = non_deep_idle_cpu;
1007 		}
1008 #endif
1009 	}
1010 
1011 	mach_smpinit();
1012 }
1013 
1014 static void
1015 mach_smpinit(void)
1016 {
1017 	struct psm_ops  *pops;
1018 	processorid_t cpu_id;
1019 	int cnt;
1020 	cpuset_t cpumask;
1021 
1022 	pops = mach_set[0];
1023 	CPUSET_ZERO(cpumask);
1024 
1025 	cpu_id = -1;
1026 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
1027 	for (cnt = 0; cpu_id != -1; cnt++) {
1028 		CPUSET_ADD(cpumask, cpu_id);
1029 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
1030 	}
1031 
1032 	mp_cpus = cpumask;
1033 
1034 	/* MP related routines */
1035 	ap_mlsetup = pops->psm_post_cpu_start;
1036 	send_dirintf = pops->psm_send_ipi;
1037 
1038 	/* optional MP related routines */
1039 	if (pops->psm_shutdown)
1040 		psm_shutdownf = pops->psm_shutdown;
1041 	if (pops->psm_preshutdown)
1042 		psm_preshutdownf = pops->psm_preshutdown;
1043 	if (pops->psm_notify_func)
1044 		psm_notifyf = pops->psm_notify_func;
1045 	if (pops->psm_set_idlecpu)
1046 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
1047 	if (pops->psm_unset_idlecpu)
1048 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
1049 
1050 	psm_clkinit = pops->psm_clkinit;
1051 
1052 	if (pops->psm_timer_reprogram)
1053 		psm_timer_reprogram = pops->psm_timer_reprogram;
1054 
1055 	if (pops->psm_timer_enable)
1056 		psm_timer_enable = pops->psm_timer_enable;
1057 
1058 	if (pops->psm_timer_disable)
1059 		psm_timer_disable = pops->psm_timer_disable;
1060 
1061 	if (pops->psm_post_cyclic_setup)
1062 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
1063 
1064 	if (pops->psm_state)
1065 		psm_state = pops->psm_state;
1066 
1067 	/*
1068 	 * Set these vectors here so they can be used by Suspend/Resume
1069 	 * on UP machines.
1070 	 */
1071 	if (pops->psm_disable_intr)
1072 		psm_disable_intr = pops->psm_disable_intr;
1073 	if (pops->psm_enable_intr)
1074 		psm_enable_intr  = pops->psm_enable_intr;
1075 
1076 	/* check for multiple CPUs */
1077 	if (cnt < 2)
1078 		return;
1079 
1080 	/* check for MP platforms */
1081 	if (pops->psm_cpu_start == NULL)
1082 		return;
1083 
1084 	/*
1085 	 * Set the dispatcher hook to enable cpu "wake up"
1086 	 * when a thread becomes runnable.
1087 	 */
1088 	if (idle_cpu_use_hlt) {
1089 		disp_enq_thread = cpu_wakeup;
1090 #ifndef __xpv
1091 		if ((x86_feature & X86_MWAIT) && idle_cpu_prefer_mwait)
1092 			disp_enq_thread = cpu_wakeup_mwait;
1093 		non_deep_idle_disp_enq_thread = disp_enq_thread;
1094 #endif
1095 	}
1096 
1097 	psm_get_ipivect = pops->psm_get_ipivect;
1098 
1099 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_intr",
1100 	    (*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
1101 	    NULL, NULL, NULL, NULL);
1102 
1103 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
1104 }
1105 
1106 static void
1107 mach_picinit()
1108 {
1109 	struct psm_ops  *pops;
1110 
1111 	pops = mach_set[0];
1112 
1113 	/* register the interrupt handlers */
1114 	setlvl = pops->psm_intr_enter;
1115 	setlvlx = pops->psm_intr_exit;
1116 
1117 	/* initialize the interrupt hardware */
1118 	(*pops->psm_picinit)();
1119 
1120 	/* set interrupt mask for current ipl */
1121 	setspl = pops->psm_setspl;
1122 	cli();
1123 	setspl(CPU->cpu_pri);
1124 }
1125 
1126 uint_t	cpu_freq;	/* MHz */
1127 uint64_t cpu_freq_hz;	/* measured (in hertz) */
1128 
1129 #define	MEGA_HZ		1000000
1130 
1131 #ifdef __xpv
1132 
1133 int xpv_cpufreq_workaround = 1;
1134 int xpv_cpufreq_verbose = 0;
1135 
1136 #else	/* __xpv */
1137 
1138 static uint64_t
1139 mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
1140 {
1141 	uint64_t cpu_hz;
1142 
1143 	if ((pit_counter == 0) || (*processor_clks == 0) ||
1144 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
1145 		return (0);
1146 
1147 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
1148 
1149 	return (cpu_hz);
1150 }
1151 
1152 #endif	/* __xpv */
1153 
1154 static uint64_t
1155 mach_getcpufreq(void)
1156 {
1157 #if defined(__xpv)
1158 	vcpu_time_info_t *vti = &CPU->cpu_m.mcpu_vcpu_info->time;
1159 	uint64_t cpu_hz;
1160 
1161 	/*
1162 	 * During dom0 bringup, it was noted that on at least one older
1163 	 * Intel HT machine, the hypervisor initially gives a tsc_to_system_mul
1164 	 * value that is quite wrong (the 3.06GHz clock was reported
1165 	 * as 4.77GHz)
1166 	 *
1167 	 * The curious thing is, that if you stop the kernel at entry,
1168 	 * breakpoint here and inspect the value with kmdb, the value
1169 	 * is correct - but if you don't stop and simply enable the
1170 	 * printf statement (below), you can see the bad value printed
1171 	 * here.  Almost as if something kmdb did caused the hypervisor to
1172 	 * figure it out correctly.  And, note that the hypervisor
1173 	 * eventually -does- figure it out correctly ... if you look at
1174 	 * the field later in the life of dom0, it is correct.
1175 	 *
1176 	 * For now, on dom0, we employ a slightly cheesy workaround of
1177 	 * using the DOM0_PHYSINFO hypercall.
1178 	 */
1179 	if (DOMAIN_IS_INITDOMAIN(xen_info) && xpv_cpufreq_workaround) {
1180 		cpu_hz = 1000 * xpv_cpu_khz();
1181 	} else {
1182 		cpu_hz = (UINT64_C(1000000000) << 32) / vti->tsc_to_system_mul;
1183 
1184 		if (vti->tsc_shift < 0)
1185 			cpu_hz <<= -vti->tsc_shift;
1186 		else
1187 			cpu_hz >>= vti->tsc_shift;
1188 	}
1189 
1190 	if (xpv_cpufreq_verbose)
1191 		printf("mach_getcpufreq: system_mul 0x%x, shift %d, "
1192 		    "cpu_hz %" PRId64 "Hz\n",
1193 		    vti->tsc_to_system_mul, vti->tsc_shift, cpu_hz);
1194 
1195 	return (cpu_hz);
1196 #else	/* __xpv */
1197 	uint32_t pit_counter;
1198 	uint64_t processor_clks;
1199 
1200 	if (x86_feature & X86_TSC) {
1201 		/*
1202 		 * We have a TSC. freq_tsc() knows how to measure the number
1203 		 * of clock cycles sampled against the PIT.
1204 		 */
1205 		ulong_t flags = clear_int_flag();
1206 		processor_clks = freq_tsc(&pit_counter);
1207 		restore_int_flag(flags);
1208 		return (mach_calchz(pit_counter, &processor_clks));
1209 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
1210 #if defined(__amd64)
1211 		panic("mach_getcpufreq: no TSC!");
1212 #elif defined(__i386)
1213 		/*
1214 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
1215 		 * for which freq_notsc() knows how to measure the number of
1216 		 * elapsed clock cycles sampled against the PIT
1217 		 */
1218 		ulong_t flags = clear_int_flag();
1219 		processor_clks = freq_notsc(&pit_counter);
1220 		restore_int_flag(flags);
1221 		return (mach_calchz(pit_counter, &processor_clks));
1222 #endif	/* __i386 */
1223 	}
1224 
1225 	/* We do not know how to calculate cpu frequency for this cpu. */
1226 	return (0);
1227 #endif	/* __xpv */
1228 }
1229 
1230 /*
1231  * If the clock speed of a cpu is found to be reported incorrectly, do not add
1232  * to this array, instead improve the accuracy of the algorithm that determines
1233  * the clock speed of the processor or extend the implementation to support the
1234  * vendor as appropriate. This is here only to support adjusting the speed on
1235  * older slower processors that mach_fixcpufreq() would not be able to account
1236  * for otherwise.
1237  */
1238 static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
1239 
1240 /*
1241  * On fast processors the clock frequency that is measured may be off by
1242  * a few MHz from the value printed on the part. This is a combination of
1243  * the factors that for such fast parts being off by this much is within
1244  * the tolerances for manufacture and because of the difficulties in the
1245  * measurement that can lead to small error. This function uses some
1246  * heuristics in order to tweak the value that was measured to match what
1247  * is most likely printed on the part.
1248  *
1249  * Some examples:
1250  * 	AMD Athlon 1000 mhz measured as 998 mhz
1251  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
1252  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
1253  *
1254  * If in the future this function is no longer sufficient to correct
1255  * for the error in the measurement, then the algorithm used to perform
1256  * the measurement will have to be improved in order to increase accuracy
1257  * rather than adding horrible and questionable kludges here.
1258  *
1259  * This is called after the cyclics subsystem because of the potential
1260  * that the heuristics within may give a worse estimate of the clock
1261  * frequency than the value that was measured.
1262  */
1263 static void
1264 mach_fixcpufreq(void)
1265 {
1266 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
1267 
1268 	freq = (uint32_t)cpu_freq;
1269 
1270 	/*
1271 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
1272 	 * measured speed taking into account that the 667 MHz parts were
1273 	 * the first to round-up.
1274 	 */
1275 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
1276 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
1277 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
1278 
1279 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
1280 	mul = (freq + 25) / 50;
1281 	near50 = mul * 50;
1282 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
1283 
1284 	/* Find the closer of the two */
1285 	if (delta66 < delta50) {
1286 		fixed = near66;
1287 		delta = delta66;
1288 	} else {
1289 		fixed = near50;
1290 		delta = delta50;
1291 	}
1292 
1293 	if (fixed > INT_MAX)
1294 		return;
1295 
1296 	/*
1297 	 * Some older parts have a core clock frequency that is not an
1298 	 * integral multiple of 50 or 66 MHz. Check if one of the old
1299 	 * clock frequencies is closer to the measured value than any
1300 	 * of the integral multiples of 50 an 66, and if so set fixed
1301 	 * and delta appropriately to represent the closest value.
1302 	 */
1303 	i = sizeof (x86_cpu_freq) / sizeof (int);
1304 	while (i > 0) {
1305 		i--;
1306 
1307 		if (x86_cpu_freq[i] <= freq) {
1308 			mul = freq - x86_cpu_freq[i];
1309 
1310 			if (mul < delta) {
1311 				fixed = x86_cpu_freq[i];
1312 				delta = mul;
1313 			}
1314 
1315 			break;
1316 		}
1317 
1318 		mul = x86_cpu_freq[i] - freq;
1319 
1320 		if (mul < delta) {
1321 			fixed = x86_cpu_freq[i];
1322 			delta = mul;
1323 		}
1324 	}
1325 
1326 	/*
1327 	 * Set a reasonable maximum for how much to correct the measured
1328 	 * result by. This check is here to prevent the adjustment made
1329 	 * by this function from being more harm than good. It is entirely
1330 	 * possible that in the future parts will be made that are not
1331 	 * integral multiples of 66 or 50 in clock frequency or that
1332 	 * someone may overclock a part to some odd frequency. If the
1333 	 * measured value is farther from the corrected value than
1334 	 * allowed, then assume the corrected value is in error and use
1335 	 * the measured value.
1336 	 */
1337 	if (6 < delta)
1338 		return;
1339 
1340 	cpu_freq = (int)fixed;
1341 }
1342 
1343 
1344 static int
1345 machhztomhz(uint64_t cpu_freq_hz)
1346 {
1347 	uint64_t cpu_mhz;
1348 
1349 	/* Round to nearest MHZ */
1350 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
1351 
1352 	if (cpu_mhz > INT_MAX)
1353 		return (0);
1354 
1355 	return ((int)cpu_mhz);
1356 
1357 }
1358 
1359 
1360 static int
1361 mach_clkinit(int preferred_mode, int *set_mode)
1362 {
1363 	struct psm_ops  *pops;
1364 	int resolution;
1365 
1366 	pops = mach_set[0];
1367 
1368 	cpu_freq_hz = mach_getcpufreq();
1369 
1370 	cpu_freq = machhztomhz(cpu_freq_hz);
1371 
1372 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
1373 		tsc_gethrtime_enable = 0;
1374 
1375 #ifndef __xpv
1376 	if (tsc_gethrtime_enable) {
1377 		tsc_hrtimeinit(cpu_freq_hz);
1378 	} else
1379 #endif
1380 	{
1381 		if (pops->psm_hrtimeinit)
1382 			(*pops->psm_hrtimeinit)();
1383 		gethrtimef = pops->psm_gethrtime;
1384 		gethrtimeunscaledf = gethrtimef;
1385 		/* scalehrtimef will remain dummy */
1386 	}
1387 
1388 	mach_fixcpufreq();
1389 
1390 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
1391 		if (preferred_mode == TIMER_ONESHOT) {
1392 
1393 			resolution = (*pops->psm_clkinit)(0);
1394 			if (resolution != 0)  {
1395 				*set_mode = TIMER_ONESHOT;
1396 				return (resolution);
1397 			}
1398 		}
1399 
1400 		/*
1401 		 * either periodic mode was requested or could not set to
1402 		 * one-shot mode
1403 		 */
1404 		resolution = (*pops->psm_clkinit)(hz);
1405 		/*
1406 		 * psm should be able to do periodic, so we do not check
1407 		 * for return value of psm_clkinit here.
1408 		 */
1409 		*set_mode = TIMER_PERIODIC;
1410 		return (resolution);
1411 	} else {
1412 		/*
1413 		 * PSMI interface prior to PSMI_3 does not define a return
1414 		 * value for psm_clkinit, so the return value is ignored.
1415 		 */
1416 		(void) (*pops->psm_clkinit)(hz);
1417 		*set_mode = TIMER_PERIODIC;
1418 		return (nsec_per_tick);
1419 	}
1420 }
1421 
1422 
1423 /*ARGSUSED*/
1424 static int
1425 mach_softlvl_to_vect(int ipl)
1426 {
1427 	setsoftint = av_set_softint_pending;
1428 	kdisetsoftint = kdi_av_set_softint_pending;
1429 
1430 	return (PSM_SV_SOFTWARE);
1431 }
1432 
1433 #ifdef DEBUG
1434 /*
1435  * This is here to allow us to simulate cpus that refuse to start.
1436  */
1437 cpuset_t cpufailset;
1438 #endif
1439 
1440 int
1441 mach_cpu_start(struct cpu *cp, void *ctx)
1442 {
1443 	struct psm_ops *pops = mach_set[0];
1444 	processorid_t id = cp->cpu_id;
1445 
1446 #ifdef DEBUG
1447 	if (CPU_IN_SET(cpufailset, id))
1448 		return (0);
1449 #endif
1450 	return ((*pops->psm_cpu_start)(id, ctx));
1451 }
1452 
1453 int
1454 mach_cpuid_start(processorid_t id, void *ctx)
1455 {
1456 	struct psm_ops *pops = mach_set[0];
1457 
1458 #ifdef DEBUG
1459 	if (CPU_IN_SET(cpufailset, id))
1460 		return (0);
1461 #endif
1462 	return ((*pops->psm_cpu_start)(id, ctx));
1463 }
1464 
1465 /*
1466  * Default handler to create device node for CPU.
1467  * One reference count will be held on created device node.
1468  */
1469 static int
1470 mach_cpu_create_devinfo(cpu_t *cp, dev_info_t **dipp)
1471 {
1472 	int rv, circ;
1473 	dev_info_t *dip;
1474 	static kmutex_t cpu_node_lock;
1475 	static dev_info_t *cpu_nex_devi = NULL;
1476 
1477 	ASSERT(cp != NULL);
1478 	ASSERT(dipp != NULL);
1479 	*dipp = NULL;
1480 
1481 	if (cpu_nex_devi == NULL) {
1482 		mutex_enter(&cpu_node_lock);
1483 		/* First check whether cpus exists. */
1484 		cpu_nex_devi = ddi_find_devinfo("cpus", -1, 0);
1485 		/* Create cpus if it doesn't exist. */
1486 		if (cpu_nex_devi == NULL) {
1487 			ndi_devi_enter(ddi_root_node(), &circ);
1488 			rv = ndi_devi_alloc(ddi_root_node(), "cpus",
1489 			    (pnode_t)DEVI_SID_NODEID, &dip);
1490 			if (rv != NDI_SUCCESS) {
1491 				mutex_exit(&cpu_node_lock);
1492 				cmn_err(CE_CONT,
1493 				    "?failed to create cpu nexus device.\n");
1494 				return (PSM_FAILURE);
1495 			}
1496 			ASSERT(dip != NULL);
1497 			(void) ndi_devi_online(dip, 0);
1498 			ndi_devi_exit(ddi_root_node(), circ);
1499 			cpu_nex_devi = dip;
1500 		}
1501 		mutex_exit(&cpu_node_lock);
1502 	}
1503 
1504 	/*
1505 	 * create a child node for cpu identified as 'cpu_id'
1506 	 */
1507 	ndi_devi_enter(cpu_nex_devi, &circ);
1508 	dip = ddi_add_child(cpu_nex_devi, "cpu", DEVI_SID_NODEID, -1);
1509 	if (dip == NULL) {
1510 		cmn_err(CE_CONT,
1511 		    "?failed to create device node for cpu%d.\n", cp->cpu_id);
1512 		rv = PSM_FAILURE;
1513 	} else {
1514 		*dipp = dip;
1515 		(void) ndi_hold_devi(dip);
1516 		rv = PSM_SUCCESS;
1517 	}
1518 	ndi_devi_exit(cpu_nex_devi, circ);
1519 
1520 	return (rv);
1521 }
1522 
1523 /*
1524  * Create cpu device node in device tree and online it.
1525  * Return created dip with reference count held if requested.
1526  */
1527 int
1528 mach_cpu_create_device_node(struct cpu *cp, dev_info_t **dipp)
1529 {
1530 	int rv;
1531 	dev_info_t *dip = NULL;
1532 
1533 	ASSERT(psm_cpu_create_devinfo != NULL);
1534 	rv = psm_cpu_create_devinfo(cp, &dip);
1535 	if (rv == PSM_SUCCESS) {
1536 		cpuid_set_cpu_properties(dip, cp->cpu_id, cp->cpu_m.mcpu_cpi);
1537 		/* Recursively attach driver for parent nexus device. */
1538 		if (i_ddi_attach_node_hierarchy(ddi_get_parent(dip)) ==
1539 		    DDI_SUCCESS) {
1540 			/* Configure cpu itself and descendants. */
1541 			(void) ndi_devi_online(dip,
1542 			    NDI_ONLINE_ATTACH | NDI_CONFIG);
1543 		}
1544 		if (dipp != NULL) {
1545 			*dipp = dip;
1546 		} else {
1547 			(void) ndi_rele_devi(dip);
1548 		}
1549 	}
1550 
1551 	return (rv);
1552 }
1553 
1554 /*ARGSUSED*/
1555 static int
1556 mach_translate_irq(dev_info_t *dip, int irqno)
1557 {
1558 	return (irqno);	/* default to NO translation */
1559 }
1560 
1561 static void
1562 mach_notify_error(int level, char *errmsg)
1563 {
1564 	/*
1565 	 * SL_FATAL is pass in once panicstr is set, deliver it
1566 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
1567 	 * codes for the psmi handler
1568 	 */
1569 	if (level & SL_FATAL)
1570 		(*notify_error)(CE_PANIC, errmsg);
1571 	else if (level & SL_WARN)
1572 		(*notify_error)(CE_WARN, errmsg);
1573 	else if (level & SL_NOTE)
1574 		(*notify_error)(CE_NOTE, errmsg);
1575 	else if (level & SL_CONSOLE)
1576 		(*notify_error)(CE_CONT, errmsg);
1577 }
1578 
1579 /*
1580  * It provides the default basic intr_ops interface for the new DDI
1581  * interrupt framework if the PSM doesn't have one.
1582  *
1583  * Input:
1584  * dip     - pointer to the dev_info structure of the requested device
1585  * hdlp    - pointer to the internal interrupt handle structure for the
1586  *	     requested interrupt
1587  * intr_op - opcode for this call
1588  * result  - pointer to the integer that will hold the result to be
1589  *	     passed back if return value is PSM_SUCCESS
1590  *
1591  * Output:
1592  * return value is either PSM_SUCCESS or PSM_FAILURE
1593  */
1594 static int
1595 mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
1596     psm_intr_op_t intr_op, int *result)
1597 {
1598 	struct intrspec *ispec;
1599 
1600 	switch (intr_op) {
1601 	case PSM_INTR_OP_CHECK_MSI:
1602 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
1603 		    DDI_INTR_TYPE_MSIX);
1604 		break;
1605 	case PSM_INTR_OP_ALLOC_VECTORS:
1606 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
1607 			*result = 1;
1608 		else
1609 			*result = 0;
1610 		break;
1611 	case PSM_INTR_OP_FREE_VECTORS:
1612 		break;
1613 	case PSM_INTR_OP_NAVAIL_VECTORS:
1614 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
1615 			*result = 1;
1616 		else
1617 			*result = 0;
1618 		break;
1619 	case PSM_INTR_OP_XLATE_VECTOR:
1620 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
1621 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
1622 		break;
1623 	case PSM_INTR_OP_GET_CAP:
1624 		*result = 0;
1625 		break;
1626 	case PSM_INTR_OP_GET_PENDING:
1627 	case PSM_INTR_OP_CLEAR_MASK:
1628 	case PSM_INTR_OP_SET_MASK:
1629 	case PSM_INTR_OP_GET_SHARED:
1630 	case PSM_INTR_OP_SET_PRI:
1631 	case PSM_INTR_OP_SET_CAP:
1632 	case PSM_INTR_OP_SET_CPU:
1633 	case PSM_INTR_OP_GET_INTR:
1634 	default:
1635 		return (PSM_FAILURE);
1636 	}
1637 	return (PSM_SUCCESS);
1638 }
1639 /*
1640  * Return 1 if CMT load balancing policies should be
1641  * implemented across instances of the specified hardware
1642  * sharing relationship.
1643  */
1644 int
1645 pg_cmt_load_bal_hw(pghw_type_t hw)
1646 {
1647 	if (hw == PGHW_IPIPE ||
1648 	    hw == PGHW_FPU ||
1649 	    hw == PGHW_CHIP)
1650 		return (1);
1651 	else
1652 		return (0);
1653 }
1654 /*
1655  * Return 1 if thread affinity polices should be implemented
1656  * for instances of the specifed hardware sharing relationship.
1657  */
1658 int
1659 pg_cmt_affinity_hw(pghw_type_t hw)
1660 {
1661 	if (hw == PGHW_CACHE)
1662 		return (1);
1663 	else
1664 		return (0);
1665 }
1666