xref: /titanic_51/usr/src/uts/i86pc/os/mp_machdep.c (revision 42487ff1899d230ad6c2d55cf9573489ca5eb770)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
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 #define	PSMI_1_5
30 #include <sys/smp_impldefs.h>
31 #include <sys/psm.h>
32 #include <sys/psm_modctl.h>
33 #include <sys/pit.h>
34 #include <sys/cmn_err.h>
35 #include <sys/strlog.h>
36 #include <sys/clock.h>
37 #include <sys/debug.h>
38 #include <sys/rtc.h>
39 #include <sys/x86_archext.h>
40 #include <sys/cpupart.h>
41 #include <sys/cpuvar.h>
42 #include <sys/chip.h>
43 #include <sys/disp.h>
44 #include <sys/cpu.h>
45 #include <sys/archsystm.h>
46 #include <sys/mach_intr.h>
47 
48 #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
49 
50 /*
51  *	Local function prototypes
52  */
53 static int mp_disable_intr(processorid_t cpun);
54 static void mp_enable_intr(processorid_t cpun);
55 static void mach_init();
56 static void mach_picinit();
57 static uint64_t mach_calchz(uint32_t pit_counter, uint64_t *processor_clks);
58 static int machhztomhz(uint64_t cpu_freq_hz);
59 static uint64_t mach_getcpufreq(void);
60 static void mach_fixcpufreq(void);
61 static int mach_clkinit(int, int *);
62 static void mach_smpinit(void);
63 static void mach_set_softintr(int ipl, struct av_softinfo *);
64 static void mach_cpu_start(int cpun);
65 static int mach_softlvl_to_vect(int ipl);
66 static void mach_get_platform(int owner);
67 static void mach_construct_info();
68 static int mach_translate_irq(dev_info_t *dip, int irqno);
69 static int mach_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *,
70     psm_intr_op_t, int *);
71 static timestruc_t mach_tod_get(void);
72 static void mach_tod_set(timestruc_t ts);
73 static void mach_notify_error(int level, char *errmsg);
74 static hrtime_t dummy_hrtime(void);
75 static void dummy_scalehrtime(hrtime_t *);
76 static void cpu_halt(void);
77 static void cpu_wakeup(cpu_t *, int);
78 /*
79  *	External reference functions
80  */
81 extern void return_instr();
82 extern timestruc_t (*todgetf)(void);
83 extern void (*todsetf)(timestruc_t);
84 extern long gmt_lag;
85 extern uint64_t freq_tsc(uint32_t *);
86 #if defined(__i386)
87 extern uint64_t freq_notsc(uint32_t *);
88 #endif
89 extern void pc_gethrestime(timestruc_t *);
90 
91 /*
92  *	PSM functions initialization
93  */
94 void (*psm_shutdownf)(int, int)	= return_instr;
95 void (*psm_preshutdownf)(int, int) = return_instr;
96 void (*psm_notifyf)(int)	= return_instr;
97 void (*psm_set_idle_cpuf)(int)	= return_instr;
98 void (*psm_unset_idle_cpuf)(int) = return_instr;
99 void (*psminitf)()		= mach_init;
100 void (*picinitf)() 		= return_instr;
101 int (*clkinitf)(int, int *) 	= (int (*)(int, int *))return_instr;
102 void (*cpu_startf)() 		= return_instr;
103 int (*ap_mlsetup)() 		= (int (*)(void))return_instr;
104 void (*send_dirintf)() 		= return_instr;
105 void (*setspl)(int)		= return_instr;
106 int (*addspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
107 int (*delspl)(int, int, int, int) = (int (*)(int, int, int, int))return_instr;
108 void (*setsoftint)(int, struct av_softinfo *)=
109 	(void (*)(int, struct av_softinfo *))return_instr;
110 int (*slvltovect)(int)		= (int (*)(int))return_instr;
111 int (*setlvl)(int, int *)	= (int (*)(int, int *))return_instr;
112 void (*setlvlx)(int, int)	= (void (*)(int, int))return_instr;
113 int (*psm_disable_intr)(int)	= mp_disable_intr;
114 void (*psm_enable_intr)(int)	= mp_enable_intr;
115 hrtime_t (*gethrtimef)(void)	= dummy_hrtime;
116 hrtime_t (*gethrtimeunscaledf)(void)	= dummy_hrtime;
117 void (*scalehrtimef)(hrtime_t *)	= dummy_scalehrtime;
118 int (*psm_translate_irq)(dev_info_t *, int) = mach_translate_irq;
119 void (*gethrestimef)(timestruc_t *) = pc_gethrestime;
120 int (*psm_todgetf)(todinfo_t *) = (int (*)(todinfo_t *))return_instr;
121 int (*psm_todsetf)(todinfo_t *) = (int (*)(todinfo_t *))return_instr;
122 void (*psm_notify_error)(int, char *) = (void (*)(int, char *))NULL;
123 int (*psm_get_clockirq)(int) = NULL;
124 int (*psm_get_ipivect)(int, int) = NULL;
125 
126 int (*psm_clkinit)(int) = NULL;
127 void (*psm_timer_reprogram)(hrtime_t) = NULL;
128 void (*psm_timer_enable)(void) = NULL;
129 void (*psm_timer_disable)(void) = NULL;
130 void (*psm_post_cyclic_setup)(void *arg) = NULL;
131 int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, psm_intr_op_t,
132     int *) = mach_intr_ops;
133 
134 void (*notify_error)(int, char *) = (void (*)(int, char *))return_instr;
135 void (*hrtime_tick)(void)	= return_instr;
136 
137 int tsc_gethrtime_enable = 1;
138 int tsc_gethrtime_initted = 0;
139 
140 /*
141  * Local Static Data
142  */
143 static struct psm_ops mach_ops;
144 static struct psm_ops *mach_set[4] = {&mach_ops, NULL, NULL, NULL};
145 static ushort_t mach_ver[4] = {0, 0, 0, 0};
146 
147 /*
148  * If non-zero, idle cpus will "halted" when there's
149  * no work to do.
150  */
151 int	halt_idle_cpus = 1;
152 
153 #if defined(__amd64)
154 /*
155  * If non-zero, will use cr8 for interrupt priority masking
156  * We declare this here since install_spl is called from here
157  * (where this is checked).
158  */
159 int	intpri_use_cr8 = 0;
160 #endif	/* __amd64 */
161 
162 #ifdef	_SIMULATOR_SUPPORT
163 
164 int simulator_run = 0;	/* patch to non-zero if running under simics */
165 
166 #endif	/* _SIMULATOR_SUPPORT */
167 
168 /* ARGSUSED */
169 void
170 chip_plat_define_chip(cpu_t *cp, chip_def_t *cd)
171 {
172 	if ((x86_feature & (X86_HTT|X86_CMP)) == X86_HTT) {
173 		/*
174 		 * Single-core Pentiums with Hyper-Threading enabled.
175 		 */
176 		cd->chipd_type = CHIP_SMT;
177 	} else if ((x86_feature & (X86_HTT|X86_CMP)) == X86_CMP) {
178 		/*
179 		 * Multi-core Opterons or Multi-core Pentiums with
180 		 * Hyper-Threading disabled.
181 		 */
182 		cd->chipd_type = CHIP_CMP_SPLIT_CACHE;
183 	} else if ((x86_feature & (X86_HTT|X86_CMP)) == (X86_HTT|X86_CMP)) {
184 		/*
185 		 * Multi-core Pentiums with Hyper-Threading enabled.
186 		 */
187 		cd->chipd_type = CHIP_CMT;
188 	} else {
189 		/*
190 		 * Single-core/single-threaded chips.
191 		 */
192 		cd->chipd_type = CHIP_DEFAULT;
193 	}
194 
195 	cd->chipd_rechoose_adj = 0;
196 }
197 
198 /*
199  * Routine to ensure initial callers to hrtime gets 0 as return
200  */
201 static hrtime_t
202 dummy_hrtime(void)
203 {
204 	return (0);
205 }
206 
207 /* ARGSUSED */
208 static void
209 dummy_scalehrtime(hrtime_t *ticks)
210 {}
211 
212 /*
213  * Halt the present CPU until awoken via an interrupt
214  */
215 static void
216 cpu_halt(void)
217 {
218 	cpu_t		*cpup = CPU;
219 	processorid_t	cpun = cpup->cpu_id;
220 	cpupart_t	*cp = cpup->cpu_part;
221 	int		hset_update = 1;
222 
223 	/*
224 	 * If this CPU is online, and there's multiple CPUs
225 	 * in the system, then we should notate our halting
226 	 * by adding ourselves to the partition's halted CPU
227 	 * bitmap. This allows other CPUs to find/awaken us when
228 	 * work becomes available.
229 	 */
230 	if (cpup->cpu_flags & CPU_OFFLINE || ncpus == 1)
231 		hset_update = 0;
232 
233 	/*
234 	 * Add ourselves to the partition's halted CPUs bitmask
235 	 * and set our HALTED flag, if necessary.
236 	 *
237 	 * When a thread becomes runnable, it is placed on the queue
238 	 * and then the halted cpuset is checked to determine who
239 	 * (if anyone) should be awoken. We therefore need to first
240 	 * add ourselves to the halted cpuset, and and then check if there
241 	 * is any work available.
242 	 *
243 	 * Note that memory barriers after updating the HALTED flag
244 	 * are not necessary since an atomic operation (updating the bitmap)
245 	 * immediately follows. On x86 the atomic operation acts as a
246 	 * memory barrier for the update of cpu_disp_flags.
247 	 */
248 	if (hset_update) {
249 		cpup->cpu_disp_flags |= CPU_DISP_HALTED;
250 		CPUSET_ATOMIC_ADD(cp->cp_haltset, cpun);
251 	}
252 
253 	/*
254 	 * Check to make sure there's really nothing to do.
255 	 * Work destined for this CPU may become available after
256 	 * this check. We'll be notified through the clearing of our
257 	 * bit in the halted CPU bitmask, and a poke.
258 	 */
259 	if (disp_anywork()) {
260 		if (hset_update) {
261 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
262 			CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun);
263 		}
264 		return;
265 	}
266 
267 	/*
268 	 * We're on our way to being halted.
269 	 *
270 	 * Disable interrupts now, so that we'll awaken immediately
271 	 * after halting if someone tries to poke us between now and
272 	 * the time we actually halt.
273 	 *
274 	 * We check for the presence of our bit after disabling interrupts.
275 	 * If it's cleared, we'll return. If the bit is cleared after
276 	 * we check then the poke will pop us out of the halted state.
277 	 *
278 	 * This means that the ordering of the poke and the clearing
279 	 * of the bit by cpu_wakeup is important.
280 	 * cpu_wakeup() must clear, then poke.
281 	 * cpu_halt() must disable interrupts, then check for the bit.
282 	 */
283 	cli();
284 
285 	if (hset_update && !CPU_IN_SET(cp->cp_haltset, cpun)) {
286 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
287 		sti();
288 		return;
289 	}
290 
291 	/*
292 	 * The check for anything locally runnable is here for performance
293 	 * and isn't needed for correctness. disp_nrunnable ought to be
294 	 * in our cache still, so it's inexpensive to check, and if there
295 	 * is anything runnable we won't have to wait for the poke.
296 	 */
297 	if (cpup->cpu_disp->disp_nrunnable != 0) {
298 		if (hset_update) {
299 			cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
300 			CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun);
301 		}
302 		sti();
303 		return;
304 	}
305 
306 	/*
307 	 * Call the halt sequence:
308 	 * sti
309 	 * hlt
310 	 */
311 	i86_halt();
312 
313 	/*
314 	 * We're no longer halted
315 	 */
316 	if (hset_update) {
317 		cpup->cpu_disp_flags &= ~CPU_DISP_HALTED;
318 		CPUSET_ATOMIC_DEL(cp->cp_haltset, cpun);
319 	}
320 }
321 
322 
323 /*
324  * If "cpu" is halted, then wake it up clearing its halted bit in advance.
325  * Otherwise, see if other CPUs in the cpu partition are halted and need to
326  * be woken up so that they can steal the thread we placed on this CPU.
327  * This function is only used on MP systems.
328  */
329 static void
330 cpu_wakeup(cpu_t *cpu, int bound)
331 {
332 	uint_t		cpu_found;
333 	int		result;
334 	cpupart_t	*cp;
335 
336 	cp = cpu->cpu_part;
337 	if (CPU_IN_SET(cp->cp_haltset, cpu->cpu_id)) {
338 		/*
339 		 * Clear the halted bit for that CPU since it will be
340 		 * poked in a moment.
341 		 */
342 		CPUSET_ATOMIC_DEL(cp->cp_haltset, cpu->cpu_id);
343 		/*
344 		 * We may find the current CPU present in the halted cpuset
345 		 * if we're in the context of an interrupt that occurred
346 		 * before we had a chance to clear our bit in cpu_halt().
347 		 * Poking ourself is obviously unnecessary, since if
348 		 * we're here, we're not halted.
349 		 */
350 		if (cpu != CPU)
351 			poke_cpu(cpu->cpu_id);
352 		return;
353 	} else {
354 		/*
355 		 * This cpu isn't halted, but it's idle or undergoing a
356 		 * context switch. No need to awaken anyone else.
357 		 */
358 		if (cpu->cpu_thread == cpu->cpu_idle_thread ||
359 		    cpu->cpu_disp_flags & CPU_DISP_DONTSTEAL)
360 			return;
361 	}
362 
363 	/*
364 	 * No need to wake up other CPUs if the thread we just enqueued
365 	 * is bound.
366 	 */
367 	if (bound)
368 		return;
369 
370 
371 	/*
372 	 * See if there's any other halted CPUs. If there are, then
373 	 * select one, and awaken it.
374 	 * It's possible that after we find a CPU, somebody else
375 	 * will awaken it before we get the chance.
376 	 * In that case, look again.
377 	 */
378 	do {
379 		CPUSET_FIND(cp->cp_haltset, cpu_found);
380 		if (cpu_found == CPUSET_NOTINSET)
381 			return;
382 
383 		ASSERT(cpu_found >= 0 && cpu_found < NCPU);
384 		CPUSET_ATOMIC_XDEL(cp->cp_haltset, cpu_found, result);
385 	} while (result < 0);
386 
387 	if (cpu_found != CPU->cpu_id)
388 		poke_cpu(cpu_found);
389 }
390 
391 static int
392 mp_disable_intr(int cpun)
393 {
394 	/*
395 	 * switch to the offline cpu
396 	 */
397 	affinity_set(cpun);
398 	/*
399 	 * raise ipl to just below cross call
400 	 */
401 	splx(XC_MED_PIL-1);
402 	/*
403 	 *	set base spl to prevent the next swtch to idle from
404 	 *	lowering back to ipl 0
405 	 */
406 	CPU->cpu_intr_actv |= (1 << (XC_MED_PIL-1));
407 	set_base_spl();
408 	affinity_clear();
409 	return (DDI_SUCCESS);
410 }
411 
412 static void
413 mp_enable_intr(int cpun)
414 {
415 	/*
416 	 * switch to the online cpu
417 	 */
418 	affinity_set(cpun);
419 	/*
420 	 * clear the interrupt active mask
421 	 */
422 	CPU->cpu_intr_actv &= ~(1 << (XC_MED_PIL-1));
423 	set_base_spl();
424 	(void) spl0();
425 	affinity_clear();
426 }
427 
428 static void
429 mach_get_platform(int owner)
430 {
431 	void		**srv_opsp;
432 	void		**clt_opsp;
433 	int		i;
434 	int		total_ops;
435 
436 	/* fix up psm ops */
437 	srv_opsp = (void **)mach_set[0];
438 	clt_opsp = (void **)mach_set[owner];
439 	if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01)
440 		total_ops = sizeof (struct psm_ops_ver01) /
441 				sizeof (void (*)(void));
442 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_1)
443 		/* no psm_notify_func */
444 		total_ops = OFFSETOF(struct psm_ops, psm_notify_func) /
445 		    sizeof (void (*)(void));
446 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_2)
447 		/* no psm_timer funcs */
448 		total_ops = OFFSETOF(struct psm_ops, psm_timer_reprogram) /
449 		    sizeof (void (*)(void));
450 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_3)
451 		/* no psm_preshutdown function */
452 		total_ops = OFFSETOF(struct psm_ops, psm_preshutdown) /
453 		    sizeof (void (*)(void));
454 	else if (mach_ver[owner] == (ushort_t)PSM_INFO_VER01_4)
455 		/* no psm_preshutdown function */
456 		total_ops = OFFSETOF(struct psm_ops, psm_intr_ops) /
457 		    sizeof (void (*)(void));
458 	else
459 		total_ops = sizeof (struct psm_ops) / sizeof (void (*)(void));
460 
461 	/*
462 	 * Save the version of the PSM module, in case we need to
463 	 * bahave differently based on version.
464 	 */
465 	mach_ver[0] = mach_ver[owner];
466 
467 	for (i = 0; i < total_ops; i++)
468 		if (clt_opsp[i] != NULL)
469 			srv_opsp[i] = clt_opsp[i];
470 }
471 
472 static void
473 mach_construct_info()
474 {
475 	register struct psm_sw *swp;
476 	int	mach_cnt[PSM_OWN_OVERRIDE+1] = {0};
477 	int	conflict_owner = 0;
478 
479 	if (psmsw->psw_forw == psmsw)
480 		panic("No valid PSM modules found");
481 	mutex_enter(&psmsw_lock);
482 	for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
483 		if (!(swp->psw_flag & PSM_MOD_IDENTIFY))
484 			continue;
485 		mach_set[swp->psw_infop->p_owner] = swp->psw_infop->p_ops;
486 		mach_ver[swp->psw_infop->p_owner] = swp->psw_infop->p_version;
487 		mach_cnt[swp->psw_infop->p_owner]++;
488 	}
489 	mutex_exit(&psmsw_lock);
490 
491 	mach_get_platform(PSM_OWN_SYS_DEFAULT);
492 
493 	/* check to see are there any conflicts */
494 	if (mach_cnt[PSM_OWN_EXCLUSIVE] > 1)
495 		conflict_owner = PSM_OWN_EXCLUSIVE;
496 	if (mach_cnt[PSM_OWN_OVERRIDE] > 1)
497 		conflict_owner = PSM_OWN_OVERRIDE;
498 	if (conflict_owner) {
499 		/* remove all psm modules except uppc */
500 		cmn_err(CE_WARN,
501 			"Conflicts detected on the following PSM modules:");
502 		mutex_enter(&psmsw_lock);
503 		for (swp = psmsw->psw_forw; swp != psmsw; swp = swp->psw_forw) {
504 			if (swp->psw_infop->p_owner == conflict_owner)
505 				cmn_err(CE_WARN, "%s ",
506 					swp->psw_infop->p_mach_idstring);
507 		}
508 		mutex_exit(&psmsw_lock);
509 		cmn_err(CE_WARN,
510 			"Setting the system back to SINGLE processor mode!");
511 		cmn_err(CE_WARN,
512 		    "Please edit /etc/mach to remove the invalid PSM module.");
513 		return;
514 	}
515 
516 	if (mach_set[PSM_OWN_EXCLUSIVE])
517 		mach_get_platform(PSM_OWN_EXCLUSIVE);
518 
519 	if (mach_set[PSM_OWN_OVERRIDE])
520 		mach_get_platform(PSM_OWN_OVERRIDE);
521 }
522 
523 static void
524 mach_init()
525 {
526 	register struct psm_ops  *pops;
527 
528 	mach_construct_info();
529 
530 	pops = mach_set[0];
531 
532 	/* register the interrupt and clock initialization rotuines */
533 	picinitf = mach_picinit;
534 	clkinitf = mach_clkinit;
535 	psm_get_clockirq = pops->psm_get_clockirq;
536 
537 	/* register the interrupt setup code */
538 	slvltovect = mach_softlvl_to_vect;
539 	addspl	= pops->psm_addspl;
540 	delspl	= pops->psm_delspl;
541 
542 	if (pops->psm_translate_irq)
543 		psm_translate_irq = pops->psm_translate_irq;
544 	if (pops->psm_intr_ops)
545 		psm_intr_ops = pops->psm_intr_ops;
546 	if (pops->psm_tod_get) {
547 		todgetf = mach_tod_get;
548 		psm_todgetf = pops->psm_tod_get;
549 	}
550 	if (pops->psm_tod_set) {
551 		todsetf = mach_tod_set;
552 		psm_todsetf = pops->psm_tod_set;
553 	}
554 	if (pops->psm_notify_error) {
555 		psm_notify_error = mach_notify_error;
556 		notify_error = pops->psm_notify_error;
557 	}
558 
559 	(*pops->psm_softinit)();
560 
561 	/*
562 	 * Initialize the dispatcher's function hooks
563 	 * to enable CPU halting when idle
564 	 */
565 #if defined(_SIMULATOR_SUPPORT)
566 	if (halt_idle_cpus && !simulator_run)
567 		idle_cpu = cpu_halt;
568 #else
569 	if (halt_idle_cpus)
570 		idle_cpu = cpu_halt;
571 #endif	/* _SIMULATOR_SUPPORT */
572 
573 	mach_smpinit();
574 }
575 
576 static void
577 mach_smpinit(void)
578 {
579 	register struct psm_ops  *pops;
580 	register processorid_t cpu_id;
581 	int	 cnt;
582 	int	 cpumask;
583 
584 	pops = mach_set[0];
585 
586 	cpu_id = -1;
587 	cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
588 	for (cnt = 0, cpumask = 0; cpu_id != -1; cnt++) {
589 		cpumask |= 1 << cpu_id;
590 		cpu_id = (*pops->psm_get_next_processorid)(cpu_id);
591 	}
592 
593 	mp_cpus = cpumask;
594 
595 	/* MP related routines */
596 	cpu_startf = mach_cpu_start;
597 	ap_mlsetup = pops->psm_post_cpu_start;
598 	send_dirintf = pops->psm_send_ipi;
599 
600 	/* optional MP related routines */
601 	if (pops->psm_shutdown)
602 		psm_shutdownf = pops->psm_shutdown;
603 	if (pops->psm_preshutdown)
604 		psm_preshutdownf = pops->psm_preshutdown;
605 	if (pops->psm_notify_func)
606 		psm_notifyf = pops->psm_notify_func;
607 	if (pops->psm_set_idlecpu)
608 		psm_set_idle_cpuf = pops->psm_set_idlecpu;
609 	if (pops->psm_unset_idlecpu)
610 		psm_unset_idle_cpuf = pops->psm_unset_idlecpu;
611 
612 	psm_clkinit = pops->psm_clkinit;
613 
614 	if (pops->psm_timer_reprogram)
615 		psm_timer_reprogram = pops->psm_timer_reprogram;
616 
617 	if (pops->psm_timer_enable)
618 		psm_timer_enable = pops->psm_timer_enable;
619 
620 	if (pops->psm_timer_disable)
621 		psm_timer_disable = pops->psm_timer_disable;
622 
623 	if (pops->psm_post_cyclic_setup)
624 		psm_post_cyclic_setup = pops->psm_post_cyclic_setup;
625 
626 	/* check for multiple cpu's */
627 	if (cnt < 2)
628 		return;
629 
630 	/* check for MP platforms */
631 	if (pops->psm_cpu_start == NULL)
632 		return;
633 
634 	/*
635 	 * Set the dispatcher hook to enable cpu "wake up"
636 	 * when a thread becomes runnable.
637 	 */
638 #if defined(_SIMULATOR_SUPPORT)
639 	if (halt_idle_cpus && !simulator_run) {
640 		disp_enq_thread = cpu_wakeup;
641 	}
642 #else
643 	if (halt_idle_cpus) {
644 		disp_enq_thread = cpu_wakeup;
645 	}
646 #endif	/* _SIMULATOR_SUPPORT */
647 
648 	if (pops->psm_disable_intr)
649 		psm_disable_intr = pops->psm_disable_intr;
650 	if (pops->psm_enable_intr)
651 		psm_enable_intr  = pops->psm_enable_intr;
652 
653 	psm_get_ipivect = pops->psm_get_ipivect;
654 
655 	(void) add_avintr((void *)NULL, XC_HI_PIL, xc_serv, "xc_hi_intr",
656 		(*pops->psm_get_ipivect)(XC_HI_PIL, PSM_INTR_IPI_HI),
657 		(caddr_t)X_CALL_HIPRI, NULL, NULL, NULL);
658 	(void) add_avintr((void *)NULL, XC_MED_PIL, xc_serv, "xc_med_intr",
659 		(*pops->psm_get_ipivect)(XC_MED_PIL, PSM_INTR_IPI_LO),
660 		(caddr_t)X_CALL_MEDPRI, NULL, NULL, NULL);
661 
662 	(void) (*pops->psm_get_ipivect)(XC_CPUPOKE_PIL, PSM_INTR_POKE);
663 }
664 
665 static void
666 mach_picinit()
667 {
668 	register struct psm_ops  *pops;
669 	extern void install_spl(void);	/* XXX: belongs in a header file */
670 #if defined(__amd64) && defined(DEBUG)
671 	extern void *spl_patch, *slow_spl, *setsplhi_patch, *slow_setsplhi;
672 #endif
673 
674 	pops = mach_set[0];
675 
676 	/* register the interrupt handlers */
677 	setlvl = pops->psm_intr_enter;
678 	setlvlx = pops->psm_intr_exit;
679 
680 	/* initialize the interrupt hardware */
681 	(*pops->psm_picinit)();
682 
683 	/* set interrupt mask for current ipl */
684 	setspl = pops->psm_setspl;
685 	setspl(CPU->cpu_pri);
686 
687 	/* Install proper spl routine now that we can Program the PIC   */
688 #if defined(__amd64)
689 	/*
690 	 * It would be better if we could check this at compile time
691 	 */
692 	ASSERT(((uintptr_t)&slow_setsplhi - (uintptr_t)&setsplhi_patch < 128) &&
693 		((uintptr_t)&slow_spl - (uintptr_t)&spl_patch < 128));
694 #endif
695 	install_spl();
696 }
697 
698 uint_t	cpu_freq;	/* MHz */
699 uint64_t cpu_freq_hz;	/* measured (in hertz) */
700 
701 #define	MEGA_HZ		1000000
702 
703 static uint64_t
704 mach_calchz(uint32_t pit_counter, uint64_t *processor_clks)
705 {
706 	uint64_t cpu_hz;
707 
708 	if ((pit_counter == 0) || (*processor_clks == 0) ||
709 	    (*processor_clks > (((uint64_t)-1) / PIT_HZ)))
710 		return (0);
711 
712 	cpu_hz = ((uint64_t)PIT_HZ * *processor_clks) / pit_counter;
713 
714 	return (cpu_hz);
715 }
716 
717 static uint64_t
718 mach_getcpufreq(void)
719 {
720 	uint32_t pit_counter;
721 	uint64_t processor_clks;
722 
723 	if (x86_feature & X86_TSC) {
724 		/*
725 		 * We have a TSC. freq_tsc() knows how to measure the number
726 		 * of clock cycles sampled against the PIT.
727 		 */
728 		processor_clks = freq_tsc(&pit_counter);
729 		return (mach_calchz(pit_counter, &processor_clks));
730 	} else if (x86_vendor == X86_VENDOR_Cyrix || x86_type == X86_TYPE_P5) {
731 #if defined(__amd64)
732 		panic("mach_getcpufreq: no TSC!");
733 #elif defined(__i386)
734 		/*
735 		 * We are a Cyrix based on a 6x86 core or an Intel Pentium
736 		 * for which freq_notsc() knows how to measure the number of
737 		 * elapsed clock cycles sampled against the PIT
738 		 */
739 		processor_clks = freq_notsc(&pit_counter);
740 		return (mach_calchz(pit_counter, &processor_clks));
741 #endif	/* __i386 */
742 	}
743 
744 	/* We do not know how to calculate cpu frequency for this cpu. */
745 	return (0);
746 }
747 
748 /*
749  * If the clock speed of a cpu is found to be reported incorrectly, do not add
750  * to this array, instead improve the accuracy of the algorithm that determines
751  * the clock speed of the processor or extend the implementation to support the
752  * vendor as appropriate. This is here only to support adjusting the speed on
753  * older slower processors that mach_fixcpufreq() would not be able to account
754  * for otherwise.
755  */
756 static int x86_cpu_freq[] = { 60, 75, 80, 90, 120, 160, 166, 175, 180, 233 };
757 
758 /*
759  * On fast processors the clock frequency that is measured may be off by
760  * a few MHz from the value printed on the part. This is a combination of
761  * the factors that for such fast parts being off by this much is within
762  * the tolerances for manufacture and because of the difficulties in the
763  * measurement that can lead to small error. This function uses some
764  * heuristics in order to tweak the value that was measured to match what
765  * is most likely printed on the part.
766  *
767  * Some examples:
768  * 	AMD Athlon 1000 mhz measured as 998 mhz
769  * 	Intel Pentium III Xeon 733 mhz measured as 731 mhz
770  * 	Intel Pentium IV 1500 mhz measured as 1495mhz
771  *
772  * If in the future this function is no longer sufficient to correct
773  * for the error in the measurement, then the algorithm used to perform
774  * the measurement will have to be improved in order to increase accuracy
775  * rather than adding horrible and questionable kludges here.
776  *
777  * This is called after the cyclics subsystem because of the potential
778  * that the heuristics within may give a worse estimate of the clock
779  * frequency than the value that was measured.
780  */
781 static void
782 mach_fixcpufreq(void)
783 {
784 	uint32_t freq, mul, near66, delta66, near50, delta50, fixed, delta, i;
785 
786 	freq = (uint32_t)cpu_freq;
787 
788 	/*
789 	 * Find the nearest integer multiple of 200/3 (about 66) MHz to the
790 	 * measured speed taking into account that the 667 MHz parts were
791 	 * the first to round-up.
792 	 */
793 	mul = (uint32_t)((3 * (uint64_t)freq + 100) / 200);
794 	near66 = (uint32_t)((200 * (uint64_t)mul + ((mul >= 10) ? 1 : 0)) / 3);
795 	delta66 = (near66 > freq) ? (near66 - freq) : (freq - near66);
796 
797 	/* Find the nearest integer multiple of 50 MHz to the measured speed */
798 	mul = (freq + 25) / 50;
799 	near50 = mul * 50;
800 	delta50 = (near50 > freq) ? (near50 - freq) : (freq - near50);
801 
802 	/* Find the closer of the two */
803 	if (delta66 < delta50) {
804 		fixed = near66;
805 		delta = delta66;
806 	} else {
807 		fixed = near50;
808 		delta = delta50;
809 	}
810 
811 	if (fixed > INT_MAX)
812 		return;
813 
814 	/*
815 	 * Some older parts have a core clock frequency that is not an
816 	 * integral multiple of 50 or 66 MHz. Check if one of the old
817 	 * clock frequencies is closer to the measured value than any
818 	 * of the integral multiples of 50 an 66, and if so set fixed
819 	 * and delta appropriately to represent the closest value.
820 	 */
821 	i = sizeof (x86_cpu_freq) / sizeof (int);
822 	while (i > 0) {
823 		i--;
824 
825 		if (x86_cpu_freq[i] <= freq) {
826 			mul = freq - x86_cpu_freq[i];
827 
828 			if (mul < delta) {
829 				fixed = x86_cpu_freq[i];
830 				delta = mul;
831 			}
832 
833 			break;
834 		}
835 
836 		mul = x86_cpu_freq[i] - freq;
837 
838 		if (mul < delta) {
839 			fixed = x86_cpu_freq[i];
840 			delta = mul;
841 		}
842 	}
843 
844 	/*
845 	 * Set a reasonable maximum for how much to correct the measured
846 	 * result by. This check is here to prevent the adjustment made
847 	 * by this function from being more harm than good. It is entirely
848 	 * possible that in the future parts will be made that are not
849 	 * integral multiples of 66 or 50 in clock frequency or that
850 	 * someone may overclock a part to some odd frequency. If the
851 	 * measured value is farther from the corrected value than
852 	 * allowed, then assume the corrected value is in error and use
853 	 * the measured value.
854 	 */
855 	if (6 < delta)
856 		return;
857 
858 	cpu_freq = (int)fixed;
859 }
860 
861 
862 static int
863 machhztomhz(uint64_t cpu_freq_hz)
864 {
865 	uint64_t cpu_mhz;
866 
867 	/* Round to nearest MHZ */
868 	cpu_mhz = (cpu_freq_hz + (MEGA_HZ / 2)) / MEGA_HZ;
869 
870 	if (cpu_mhz > INT_MAX)
871 		return (0);
872 
873 	return ((int)cpu_mhz);
874 
875 }
876 
877 
878 static int
879 mach_clkinit(int preferred_mode, int *set_mode)
880 {
881 	register struct psm_ops  *pops;
882 	int resolution;
883 
884 	pops = mach_set[0];
885 
886 #ifdef	_SIMULATOR_SUPPORT
887 	if (!simulator_run)
888 		cpu_freq_hz = mach_getcpufreq();
889 	else
890 		cpu_freq_hz = 40000000; /* use 40 Mhz (hack for simulator) */
891 #else
892 	cpu_freq_hz = mach_getcpufreq();
893 #endif	/* _SIMULATOR_SUPPORT */
894 
895 	cpu_freq = machhztomhz(cpu_freq_hz);
896 
897 	if (!(x86_feature & X86_TSC) || (cpu_freq == 0))
898 		tsc_gethrtime_enable = 0;
899 
900 	if (tsc_gethrtime_enable) {
901 		tsc_hrtimeinit(cpu_freq_hz);
902 		gethrtimef = tsc_gethrtime;
903 		gethrtimeunscaledf = tsc_gethrtimeunscaled;
904 		scalehrtimef = tsc_scalehrtime;
905 		hrtime_tick = tsc_tick;
906 		tsc_gethrtime_initted = 1;
907 	} else {
908 		if (pops->psm_hrtimeinit)
909 			(*pops->psm_hrtimeinit)();
910 		gethrtimef = pops->psm_gethrtime;
911 		gethrtimeunscaledf = gethrtimef;
912 		/* scalehrtimef will remain dummy */
913 	}
914 
915 	mach_fixcpufreq();
916 
917 	if (mach_ver[0] >= PSM_INFO_VER01_3) {
918 		if ((preferred_mode == TIMER_ONESHOT) &&
919 		    (tsc_gethrtime_enable)) {
920 
921 			resolution = (*pops->psm_clkinit)(0);
922 			if (resolution != 0)  {
923 				*set_mode = TIMER_ONESHOT;
924 				return (resolution);
925 			}
926 
927 		}
928 
929 		/*
930 		 * either periodic mode was requested or could not set to
931 		 * one-shot mode
932 		 */
933 		resolution = (*pops->psm_clkinit)(hz);
934 		/*
935 		 * psm should be able to do periodic, so we do not check
936 		 * for return value of psm_clkinit here.
937 		 */
938 		*set_mode = TIMER_PERIODIC;
939 		return (resolution);
940 	} else {
941 		/*
942 		 * PSMI interface prior to PSMI_3 does not define a return
943 		 * value for psm_clkinit, so the return value is ignored.
944 		 */
945 		(void) (*pops->psm_clkinit)(hz);
946 		*set_mode = TIMER_PERIODIC;
947 		return (nsec_per_tick);
948 	}
949 }
950 
951 /*ARGSUSED*/
952 static void
953 mach_psm_set_softintr(int ipl, struct av_softinfo *pending)
954 {
955 	register struct psm_ops  *pops;
956 
957 	/* invoke hardware interrupt					*/
958 	pops = mach_set[0];
959 	(*pops->psm_set_softintr)(ipl);
960 }
961 
962 static int
963 mach_softlvl_to_vect(register int ipl)
964 {
965 	register int softvect;
966 	register struct psm_ops  *pops;
967 
968 	pops = mach_set[0];
969 
970 	/* check for null handler for set soft interrupt call		*/
971 	if (pops->psm_set_softintr == NULL) {
972 		setsoftint = av_set_softint_pending;
973 		return (PSM_SV_SOFTWARE);
974 	}
975 
976 	softvect = (*pops->psm_softlvl_to_irq)(ipl);
977 	/* check for hardware scheme					*/
978 	if (softvect > PSM_SV_SOFTWARE) {
979 		setsoftint = mach_psm_set_softintr;
980 		return (softvect);
981 	}
982 
983 	if (softvect == PSM_SV_SOFTWARE)
984 		setsoftint = av_set_softint_pending;
985 	else	/* hardware and software mixed scheme			*/
986 		setsoftint = mach_set_softintr;
987 
988 	return (PSM_SV_SOFTWARE);
989 }
990 
991 static void
992 mach_set_softintr(register int ipl, struct av_softinfo *pending)
993 {
994 	register struct psm_ops  *pops;
995 
996 	/* set software pending bits					*/
997 	av_set_softint_pending(ipl, pending);
998 
999 	/*	check if dosoftint will be called at the end of intr	*/
1000 	if (CPU_ON_INTR(CPU) || (curthread->t_intr))
1001 		return;
1002 
1003 	/* invoke hardware interrupt					*/
1004 	pops = mach_set[0];
1005 	(*pops->psm_set_softintr)(ipl);
1006 }
1007 
1008 static void
1009 mach_cpu_start(register int cpun)
1010 {
1011 	register struct psm_ops  *pops;
1012 	int	i;
1013 
1014 	pops = mach_set[0];
1015 
1016 	(*pops->psm_cpu_start)(cpun, rm_platter_va);
1017 
1018 	/* wait for the auxillary cpu to be ready			*/
1019 	for (i = 20000; i; i--) {
1020 		if (cpu[cpun]->cpu_flags & CPU_READY)
1021 			return;
1022 		drv_usecwait(100);
1023 	}
1024 }
1025 
1026 /*ARGSUSED*/
1027 static int
1028 mach_translate_irq(dev_info_t *dip, int irqno)
1029 {
1030 	return (irqno);	/* default to NO translation */
1031 }
1032 
1033 static timestruc_t
1034 mach_tod_get(void)
1035 {
1036 	timestruc_t ts;
1037 	todinfo_t tod;
1038 	static int mach_range_warn = 1;	/* warn only once */
1039 
1040 	ASSERT(MUTEX_HELD(&tod_lock));
1041 
1042 	/* The year returned from is the last 2 digit only */
1043 	if ((*psm_todgetf)(&tod)) {
1044 		ts.tv_sec = 0;
1045 		ts.tv_nsec = 0;
1046 		tod_fault_reset();
1047 		return (ts);
1048 	}
1049 
1050 	/* assume that we wrap the rtc year back to zero at 2000 */
1051 	if (tod.tod_year < 69) {
1052 		if (mach_range_warn && tod.tod_year > 38) {
1053 			cmn_err(CE_WARN, "hardware real-time clock is out "
1054 				"of range -- time needs to be reset");
1055 			mach_range_warn = 0;
1056 		}
1057 		tod.tod_year += 100;
1058 	}
1059 
1060 	/* tod_to_utc uses 1900 as base for the year */
1061 	ts.tv_sec = tod_to_utc(tod) + gmt_lag;
1062 	ts.tv_nsec = 0;
1063 
1064 	return (ts);
1065 }
1066 
1067 static void
1068 mach_tod_set(timestruc_t ts)
1069 {
1070 	todinfo_t tod = utc_to_tod(ts.tv_sec - gmt_lag);
1071 
1072 	ASSERT(MUTEX_HELD(&tod_lock));
1073 
1074 	if (tod.tod_year >= 100)
1075 		tod.tod_year -= 100;
1076 
1077 	(*psm_todsetf)(&tod);
1078 }
1079 
1080 static void
1081 mach_notify_error(int level, char *errmsg)
1082 {
1083 	/*
1084 	 * SL_FATAL is pass in once panicstr is set, deliver it
1085 	 * as CE_PANIC.  Also, translate SL_ codes back to CE_
1086 	 * codes for the psmi handler
1087 	 */
1088 	if (level & SL_FATAL)
1089 		(*notify_error)(CE_PANIC, errmsg);
1090 	else if (level & SL_WARN)
1091 		(*notify_error)(CE_WARN, errmsg);
1092 	else if (level & SL_NOTE)
1093 		(*notify_error)(CE_NOTE, errmsg);
1094 	else if (level & SL_CONSOLE)
1095 		(*notify_error)(CE_CONT, errmsg);
1096 }
1097 
1098 /*
1099  * It provides the default basic intr_ops interface for the new DDI
1100  * interrupt framework if the PSM doesn't have one.
1101  *
1102  * Input:
1103  * dip     - pointer to the dev_info structure of the requested device
1104  * hdlp    - pointer to the internal interrupt handle structure for the
1105  *	     requested interrupt
1106  * intr_op - opcode for this call
1107  * result  - pointer to the integer that will hold the result to be
1108  *	     passed back if return value is PSM_SUCCESS
1109  *
1110  * Output:
1111  * return value is either PSM_SUCCESS or PSM_FAILURE
1112  */
1113 static int
1114 mach_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
1115     psm_intr_op_t intr_op, int *result)
1116 {
1117 	struct intrspec *ispec;
1118 
1119 	switch (intr_op) {
1120 	case PSM_INTR_OP_CHECK_MSI:
1121 		*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
1122 			    DDI_INTR_TYPE_MSIX);
1123 		break;
1124 	case PSM_INTR_OP_ALLOC_VECTORS:
1125 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
1126 			*result = 1;
1127 		else
1128 			*result = 0;
1129 		break;
1130 	case PSM_INTR_OP_FREE_VECTORS:
1131 		break;
1132 	case PSM_INTR_OP_NAVAIL_VECTORS:
1133 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
1134 			*result = 1;
1135 		else
1136 			*result = 0;
1137 		break;
1138 	case PSM_INTR_OP_XLATE_VECTOR:
1139 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
1140 		*result = psm_translate_irq(dip, ispec->intrspec_vec);
1141 		break;
1142 	case PSM_INTR_OP_GET_CAP:
1143 		*result = 0;
1144 		break;
1145 	case PSM_INTR_OP_GET_PENDING:
1146 	case PSM_INTR_OP_CLEAR_MASK:
1147 	case PSM_INTR_OP_SET_MASK:
1148 	case PSM_INTR_OP_GET_SHARED:
1149 	case PSM_INTR_OP_SET_PRI:
1150 	case PSM_INTR_OP_SET_CAP:
1151 	case PSM_INTR_OP_SET_CPU:
1152 	case PSM_INTR_OP_GET_INTR:
1153 	default:
1154 		return (PSM_FAILURE);
1155 	}
1156 	return (PSM_SUCCESS);
1157 }
1158