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.
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.
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]
22 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/machsystm.h>
26 #include <sys/archsystm.h>
29 #include <sys/cpupart.h>
31 #include <sys/bitset.h>
32 #include <sys/reboot.h>
34 #include <sys/bootconf.h>
35 #include <sys/memlist_plat.h>
36 #include <sys/memlist_impl.h>
37 #include <sys/prom_plat.h>
38 #include <sys/prom_isa.h>
39 #include <sys/autoconf.h>
40 #include <sys/intreg.h>
41 #include <sys/ivintr.h>
42 #include <sys/fpu/fpusystm.h>
43 #include <sys/iommutsb.h>
44 #include <vm/vm_dep.h>
45 #include <vm/seg_kmem.h>
46 #include <vm/seg_kpm.h>
47 #include <vm/seg_map.h>
48 #include <vm/seg_kp.h>
49 #include <sys/sysconf.h>
50 #include <vm/hat_sfmmu.h>
52 #include <sys/sun4asi.h>
53 #include <sys/clconf.h>
54 #include <sys/platform_module.h>
55 #include <sys/panic.h>
56 #include <sys/cpu_sgnblk_defs.h>
57 #include <sys/clock.h>
58 #include <sys/fpras_impl.h>
59 #include <sys/prom_debug.h>
60 #include <sys/traptrace.h>
61 #include <sys/memnode.h>
62 #include <sys/mem_cage.h>
65 * fpRAS implementation structures.
67 struct fpras_chkfn
*fpras_chkfnaddrs
[FPRAS_NCOPYOPS
];
68 struct fpras_chkfngrp
*fpras_chkfngrps
;
69 struct fpras_chkfngrp
*fpras_chkfngrps_base
;
70 int fpras_frequency
= -1;
71 int64_t fpras_interval
= -1;
74 * Increase unix symbol table size as a work around for 6828121
76 int alloc_mem_bermuda_triangle
;
79 * Halt idling cpus optimization
81 * This optimation is only enabled in platforms that have
82 * the CPU halt support. The cpu_halt_cpu() support is provided
83 * in the cpu module and it is referenced here with a pragma weak.
84 * The presence of this routine automatically enable the halt idling
85 * cpus functionality if the global switch enable_halt_idle_cpus
86 * is set (default is set).
89 #pragma weak cpu_halt_cpu
90 extern void cpu_halt_cpu();
93 * Defines for the idle_state_transition DTrace probe
95 * The probe fires when the CPU undergoes an idle state change (e.g. halting)
96 * The agument passed is the state to which the CPU is transitioning.
98 * The states are defined here.
100 #define IDLE_STATE_NORMAL 0
101 #define IDLE_STATE_HALTED 1
103 int enable_halt_idle_cpus
= 1; /* global switch */
105 uint_t cp_haltset_fanout
= 3;
108 setup_trap_table(void)
110 intr_init(CPU
); /* init interrupt request free list */
111 setwstate(WSTATE_KERN
);
112 prom_set_traptable(&trap_table
);
118 if (fpras_implemented
&& !fpras_disable
) {
120 struct fpras_chkfngrp
*fcgp
;
121 size_t chkfngrpsallocsz
;
124 * Note that we size off of NCPU and setup for
125 * all those possibilities regardless of whether
126 * the cpu id is present or not. We do this so that
127 * we don't have any construction or destruction
128 * activity to perform at DR time, and it's not
129 * costly in memory. We require block alignment.
131 chkfngrpsallocsz
= NCPU
* sizeof (struct fpras_chkfngrp
);
132 fpras_chkfngrps_base
= kmem_alloc(chkfngrpsallocsz
, KM_SLEEP
);
133 if (IS_P2ALIGNED((uintptr_t)fpras_chkfngrps_base
, 64)) {
134 fpras_chkfngrps
= fpras_chkfngrps_base
;
136 kmem_free(fpras_chkfngrps_base
, chkfngrpsallocsz
);
137 chkfngrpsallocsz
+= 64;
138 fpras_chkfngrps_base
= kmem_alloc(chkfngrpsallocsz
,
140 fpras_chkfngrps
= (struct fpras_chkfngrp
*)
141 P2ROUNDUP((uintptr_t)fpras_chkfngrps_base
, 64);
145 * Copy our check function into place for each copy operation
148 fcgp
= &fpras_chkfngrps
[0];
149 for (i
= 0; i
< FPRAS_NCOPYOPS
; ++i
)
150 bcopy((void *)fpras_chkfn_type1
, &fcgp
->fpras_fn
[i
],
151 sizeof (struct fpras_chkfn
));
152 for (i
= 1; i
< NCPU
; ++i
)
153 *(&fpras_chkfngrps
[i
]) = *fcgp
;
156 * At definition fpras_frequency is set to -1, and it will
157 * still have that value unless changed in /etc/system (not
158 * strictly supported, but not preventable). The following
159 * both sets the default and sanity checks anything from
162 if (fpras_frequency
< 0)
163 fpras_frequency
= FPRAS_DEFAULT_FREQUENCY
;
166 * Now calculate fpras_interval. When fpras_interval
167 * becomes non-negative fpras checks will commence
168 * (copies before this point in boot will bypass fpras).
169 * Our stores of instructions must be visible; no need
170 * to flush as they're never been executed before.
173 fpras_interval
= (fpras_frequency
== 0) ?
174 0 : sys_tick_freq
/ fpras_frequency
;
179 mach_hw_copy_limit(void)
195 * Load tod driver module for the tod part found on this system.
196 * Recompute the cpu frequency/delays based on tod as tod part
197 * tends to keep time more accurately.
199 if (tod_module_name
== NULL
|| modload("tod", tod_module_name
) == -1)
200 halt("Can't load tod module");
207 * Startup memory scrubber, if not running fpu emulation code.
210 #ifndef _HW_MEMSCRUB_SUPPORT
212 if (memscrub_init()) {
214 "Memory scrubber failed to initialize");
217 #endif /* _HW_MEMSCRUB_SUPPORT */
221 * Halt the present CPU until awoken via an interrupt.
222 * This routine should only be invoked if cpu_halt_cpu()
223 * exists and is supported, see mach_cpu_halt_idle()
229 processorid_t cpu_sid
= cpup
->cpu_seqid
;
230 cpupart_t
*cp
= cpup
->cpu_part
;
232 volatile int *p
= &cpup
->cpu_disp
->disp_nrunnable
;
236 * If this CPU is online then we should notate our halting
237 * by adding ourselves to the partition's halted CPU
238 * bitset. This allows other CPUs to find/awaken us when
239 * work becomes available.
241 if (CPU
->cpu_flags
& CPU_OFFLINE
)
245 * Add ourselves to the partition's halted CPUs bitset
246 * and set our HALTED flag, if necessary.
248 * When a thread becomes runnable, it is placed on the queue
249 * and then the halted cpu bitset is checked to determine who
250 * (if anyone) should be awoken. We therefore need to first
251 * add ourselves to the halted bitset, and then check if there
252 * is any work available. The order is important to prevent a race
253 * that can lead to work languishing on a run queue somewhere while
254 * this CPU remains halted.
256 * Either the producing CPU will see we're halted and will awaken us,
257 * or this CPU will see the work available in disp_anywork()
260 cpup
->cpu_disp_flags
|= CPU_DISP_HALTED
;
262 bitset_atomic_add(&cp
->cp_haltset
, cpu_sid
);
266 * Check to make sure there's really nothing to do.
267 * Work destined for this CPU may become available after
268 * this check. We'll be notified through the clearing of our
269 * bit in the halted CPU bitset, and a poke.
271 if (disp_anywork()) {
273 cpup
->cpu_disp_flags
&= ~CPU_DISP_HALTED
;
274 bitset_atomic_del(&cp
->cp_haltset
, cpu_sid
);
280 * We're on our way to being halted. Wait until something becomes
281 * runnable locally or we are awaken (i.e. removed from the halt set).
282 * Note that the call to hv_cpu_yield() can return even if we have
285 * Disable interrupts now, so that we'll awaken immediately
286 * after halting if someone tries to poke us between now and
287 * the time we actually halt.
289 * We check for the presence of our bit after disabling interrupts.
290 * If it's cleared, we'll return. If the bit is cleared after
291 * we check then the poke will pop us out of the halted state.
292 * Also, if the offlined CPU has been brought back on-line, then
295 * The ordering of the poke and the clearing of the bit by cpu_wakeup
297 * cpu_wakeup() must clear, then poke.
298 * cpu_halt() must disable interrupts, then check for the bit.
300 * The check for anything locally runnable is here for performance
301 * and isn't needed for correctness. disp_nrunnable ought to be
302 * in our cache still, so it's inexpensive to check, and if there
303 * is anything runnable we won't have to wait for the poke.
305 * Any interrupt will awaken the cpu from halt. Looping here
306 * will filter spurious interrupts that wake us up, but don't
307 * represent a need for us to head back out to idle(). This
308 * will enable the idle loop to be more efficient and sleep in
309 * the processor pipeline for a larger percent of the time,
310 * which returns useful cycles to the peer hardware strand
311 * that shares the pipeline.
313 s
= disable_vec_intr();
315 ((hset_update
&& bitset_in_set(&cp
->cp_haltset
, cpu_sid
)) ||
316 (!hset_update
&& (CPU
->cpu_flags
& CPU_OFFLINE
)))) {
318 DTRACE_PROBE1(idle__state__transition
,
319 uint_t
, IDLE_STATE_HALTED
);
320 (void) cpu_halt_cpu();
321 DTRACE_PROBE1(idle__state__transition
,
322 uint_t
, IDLE_STATE_NORMAL
);
325 s
= disable_vec_intr();
329 * We're no longer halted
333 cpup
->cpu_disp_flags
&= ~CPU_DISP_HALTED
;
334 bitset_atomic_del(&cp
->cp_haltset
, cpu_sid
);
339 * If "cpu" is halted, then wake it up clearing its halted bit in advance.
340 * Otherwise, see if other CPUs in the cpu partition are halted and need to
341 * be woken up so that they can steal the thread we placed on this CPU.
342 * This function is only used on MP systems.
343 * This function should only be invoked if cpu_halt_cpu()
344 * exists and is supported, see mach_cpu_halt_idle()
347 cpu_wakeup(cpu_t
*cpu
, int bound
)
350 processorid_t cpu_sid
;
354 cpu_sid
= cpu
->cpu_seqid
;
355 if (bitset_in_set(&cp
->cp_haltset
, cpu_sid
)) {
357 * Clear the halted bit for that CPU since it will be
360 bitset_atomic_del(&cp
->cp_haltset
, cpu_sid
);
362 * We may find the current CPU present in the halted cpu bitset
363 * if we're in the context of an interrupt that occurred
364 * before we had a chance to clear our bit in cpu_halt().
365 * Poking ourself is obviously unnecessary, since if
366 * we're here, we're not halted.
369 poke_cpu(cpu
->cpu_id
);
373 * This cpu isn't halted, but it's idle or undergoing a
374 * context switch. No need to awaken anyone else.
376 if (cpu
->cpu_thread
== cpu
->cpu_idle_thread
||
377 cpu
->cpu_disp_flags
& CPU_DISP_DONTSTEAL
)
382 * No need to wake up other CPUs if this is for a bound thread.
388 * The CPU specified for wakeup isn't currently halted, so check
389 * to see if there are any other halted CPUs in the partition,
390 * and if there are then awaken one.
392 * If possible, try to select a CPU close to the target, since this
393 * will likely trigger a migration.
396 cpu_found
= bitset_find(&cp
->cp_haltset
);
397 if (cpu_found
== (uint_t
)-1)
399 } while (bitset_atomic_test_and_del(&cp
->cp_haltset
, cpu_found
) < 0);
401 if (cpu_found
!= CPU
->cpu_seqid
)
402 poke_cpu(cpu_seq
[cpu_found
]->cpu_id
);
406 mach_cpu_halt_idle(void)
408 if (enable_halt_idle_cpus
) {
411 disp_enq_thread
= cpu_wakeup
;
418 cpu_intrq_setup(struct cpu
*cp
)
420 /* Interrupt mondo queues not applicable to sun4u */
426 cpu_intrq_cleanup(struct cpu
*cp
)
428 /* Interrupt mondo queues not applicable to sun4u */
433 cpu_intrq_register(struct cpu
*cp
)
435 /* Interrupt/error queues not applicable to sun4u */
440 mach_htraptrace_setup(int cpuid
)
442 /* Setup hypervisor traptrace buffer, not applicable to sun4u */
447 mach_htraptrace_configure(int cpuid
)
449 /* enable/ disable hypervisor traptracing, not applicable to sun4u */
454 mach_htraptrace_cleanup(int cpuid
)
456 /* cleanup hypervisor traptrace buffer, not applicable to sun4u */
460 mach_descrip_startup_init(void)
464 * Initialize Machine description framework during startup.
468 mach_descrip_startup_fini(void)
472 * Clean up Machine Description framework during startup.
477 mach_descrip_init(void)
481 * Initialize Machine description framework.
488 /* Setup hypervisor services, not applicable to sun4u */
492 load_mach_drivers(void)
494 /* Currently no machine class (sun4u) specific drivers to load */
498 * Return true if the machine we're running on is a Positron.
499 * (Positron is an unsupported developers platform.)
505 const char proto_model
[] = "SUNW,501-2732";
506 pnode_t root
= prom_rootnode();
508 if (prom_getproplen(root
, "model") != sizeof (proto_model
))
511 (void) prom_getprop(root
, "model", model
);
512 if (strcmp(model
, proto_model
) == 0)
518 * Find a physically contiguous area of twice the largest ecache size
519 * to be used while doing displacement flush of ecaches.
522 ecache_flush_address(void)
524 struct memlist
*pmem
;
528 flush_size
= ecache_size
* 2;
529 for (pmem
= phys_install
; pmem
; pmem
= pmem
->ml_next
) {
530 ret_val
= P2ROUNDUP(pmem
->ml_address
, ecache_size
);
531 if (ret_val
+ flush_size
<= pmem
->ml_address
+ pmem
->ml_size
)
534 return ((uint64_t)-1);
538 * Called with the memlist lock held to say that phys_install has
542 phys_install_has_changed(void)
545 * Get the new address into a temporary just in case panicking
546 * involves use of ecache_flushaddr.
550 new_addr
= ecache_flush_address();
551 if (new_addr
== (uint64_t)-1) {
553 "ecache_flush_address(): failed, ecache_size=%x",
557 ecache_flushaddr
= new_addr
;