xref: /titanic_50/usr/src/uts/sun4v/cpu/common_asm.s (revision d2d5cf7c5d909b74a88d499283e24750a9a52c5d)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#if !defined(lint)
27#include "assym.h"
28#endif
29
30/*
31 * General assembly language routines.
32 * It is the intent of this file to contain routines that are
33 * specific to cpu architecture.
34 */
35
36/*
37 * WARNING: If you add a fast trap handler which can be invoked by a
38 * non-privileged user, you may have to use the FAST_TRAP_DONE macro
39 * instead of "done" instruction to return back to the user mode. See
40 * comments for the "fast_trap_done" entry point for more information.
41 */
42#define	FAST_TRAP_DONE	\
43	ba,a	fast_trap_done
44
45/*
46 * Override GET_NATIVE_TIME for the cpu module code.  This is not
47 * guaranteed to be exactly one instruction, be careful of using
48 * the macro in delay slots.
49 *
50 * Do not use any instruction that modifies condition codes as the
51 * caller may depend on these to remain unchanged across the macro.
52 */
53
54#define	GET_NATIVE_TIME(out, scr1, scr2) \
55	rd	STICK, out
56
57#define	RD_TICKCMPR(out, scr)		\
58	rd	STICK_COMPARE, out
59
60#define	WR_TICKCMPR(in,scr1,scr2,label)		\
61	wr	in, STICK_COMPARE
62
63
64#include <sys/clock.h>
65
66#if defined(lint)
67#include <sys/types.h>
68#include <sys/scb.h>
69#include <sys/systm.h>
70#include <sys/regset.h>
71#include <sys/sunddi.h>
72#include <sys/lockstat.h>
73#endif	/* lint */
74
75
76#include <sys/asm_linkage.h>
77#include <sys/privregs.h>
78#include <vm/hat_sfmmu.h>
79#include <sys/machparam.h>	/* To get SYSBASE and PAGESIZE */
80#include <sys/machthread.h>
81#include <sys/clock.h>
82#include <sys/intreg.h>
83#include <sys/psr_compat.h>
84#include <sys/isa_defs.h>
85#include <sys/dditypes.h>
86#include <sys/intr.h>
87#include <sys/hypervisor_api.h>
88
89#if !defined(lint)
90#include "assym.h"
91#endif
92
93#define	ICACHE_FLUSHSZ	0x20
94
95#if defined(lint)
96/*
97 * Softint generated when counter field of tick reg matches value field
98 * of tick_cmpr reg
99 */
100/*ARGSUSED*/
101void
102tickcmpr_set(uint64_t clock_cycles)
103{}
104
105#else   /* lint */
106
107	ENTRY_NP(tickcmpr_set)
108	! get 64-bit clock_cycles interval
109	mov	%o0, %o2
110	mov	8, %o3			! A reasonable initial step size
1111:
112	WR_TICKCMPR(%o2,%o4,%o5,__LINE__)	! Write to TICK_CMPR
113
114	GET_NATIVE_TIME(%o0, %o4, %o5)	! Read %tick to confirm the
115	sllx	%o0, 1, %o0		!   value we wrote was in the future.
116	srlx	%o0, 1, %o0
117
118	cmp	%o2, %o0		! If the value we wrote was in the
119	bg,pt	%xcc, 2f		!   future, then blow out of here.
120	  sllx	%o3, 1, %o3		! If not, then double our step size,
121	ba,pt	%xcc, 1b		!   and take another lap.
122	  add	%o0, %o3, %o2		!
1232:
124	retl
125	  nop
126	SET_SIZE(tickcmpr_set)
127
128#endif  /* lint */
129
130#if defined(lint)
131
132void
133tickcmpr_disable(void)
134{}
135
136#else
137
138	ENTRY_NP(tickcmpr_disable)
139	mov	1, %g1
140	sllx	%g1, TICKINT_DIS_SHFT, %o0
141	WR_TICKCMPR(%o0,%o4,%o5,__LINE__)	! Write to TICK_CMPR
142	retl
143	  nop
144	SET_SIZE(tickcmpr_disable)
145
146#endif
147
148#if defined(lint)
149
150/*
151 * tick_write_delta() increments %tick by the specified delta.  This should
152 * only be called after a CPR event to assure that gethrtime() continues to
153 * increase monotonically.  Obviously, writing %tick needs to de done very
154 * carefully to avoid introducing unnecessary %tick skew across CPUs.  For
155 * this reason, we make sure we're i-cache hot before actually writing to
156 * %tick.
157 *
158 * NOTE: No provision for this on sun4v right now.
159 */
160
161/*ARGSUSED*/
162void
163tick_write_delta(uint64_t delta)
164{}
165
166#else	/* lint */
167
168	.seg	".text"
169tick_write_delta_panic:
170	.asciz	"tick_write_delta: not supported"
171
172	ENTRY_NP(tick_write_delta)
173	sethi	%hi(tick_write_delta_panic), %o1
174        save    %sp, -SA(MINFRAME), %sp ! get a new window to preserve caller
175	call	panic
176	  or	%i1, %lo(tick_write_delta_panic), %o0
177	/*NOTREACHED*/
178	retl
179	  nop
180#endif
181
182#if defined(lint)
183/*
184 *  return 1 if disabled
185 */
186
187int
188tickcmpr_disabled(void)
189{ return (0); }
190
191#else   /* lint */
192
193	ENTRY_NP(tickcmpr_disabled)
194	RD_TICKCMPR(%g1, %o0)
195	retl
196	  srlx	%g1, TICKINT_DIS_SHFT, %o0
197	SET_SIZE(tickcmpr_disabled)
198
199#endif  /* lint */
200
201/*
202 * Get current tick
203 */
204#if defined(lint)
205
206u_longlong_t
207gettick(void)
208{ return (0); }
209
210#else   /* lint */
211
212	ENTRY(gettick)
213	GET_NATIVE_TIME(%o0, %o2, %o3)
214	retl
215	  nop
216	SET_SIZE(gettick)
217
218#endif  /* lint */
219
220
221/*
222 * Return the counter portion of the tick register.
223 */
224
225#if defined(lint)
226
227uint64_t
228gettick_counter(void)
229{ return(0); }
230
231#else	/* lint */
232
233	ENTRY_NP(gettick_counter)
234	rdpr	%tick, %o0
235	sllx	%o0, 1, %o0
236	retl
237	  srlx	%o0, 1, %o0		! shake off npt bit
238	SET_SIZE(gettick_counter)
239#endif	/* lint */
240
241/*
242 * Provide a C callable interface to the trap that reads the hi-res timer.
243 * Returns 64-bit nanosecond timestamp in %o0 and %o1.
244 */
245
246#if defined(lint)
247
248hrtime_t
249gethrtime(void)
250{
251	return ((hrtime_t)0);
252}
253
254hrtime_t
255gethrtime_unscaled(void)
256{
257	return ((hrtime_t)0);
258}
259
260hrtime_t
261gethrtime_max(void)
262{
263	return ((hrtime_t)0);
264}
265
266void
267scalehrtime(hrtime_t *hrt)
268{
269	*hrt = 0;
270}
271
272void
273gethrestime(timespec_t *tp)
274{
275	tp->tv_sec = 0;
276	tp->tv_nsec = 0;
277}
278
279time_t
280gethrestime_sec(void)
281{
282	return (0);
283}
284
285void
286gethrestime_lasttick(timespec_t *tp)
287{
288	tp->tv_sec = 0;
289	tp->tv_nsec = 0;
290}
291
292/*ARGSUSED*/
293void
294hres_tick(void)
295{
296}
297
298void
299panic_hres_tick(void)
300{
301}
302
303#else	/* lint */
304
305	ENTRY_NP(gethrtime)
306	GET_HRTIME(%g1, %o0, %o1, %o2, %o3, %o4, %o5, %g2)
307							! %g1 = hrtime
308	retl
309	  mov	%g1, %o0
310	SET_SIZE(gethrtime)
311
312	ENTRY_NP(gethrtime_unscaled)
313	GET_NATIVE_TIME(%g1, %o2, %o3)			! %g1 = native time
314	retl
315	  mov	%g1, %o0
316	SET_SIZE(gethrtime_unscaled)
317
318	ENTRY_NP(gethrtime_waitfree)
319	ALTENTRY(dtrace_gethrtime)
320	GET_NATIVE_TIME(%g1, %o2, %o3)			! %g1 = native time
321	NATIVE_TIME_TO_NSEC(%g1, %o2, %o3)
322	retl
323	  mov	%g1, %o0
324	SET_SIZE(dtrace_gethrtime)
325	SET_SIZE(gethrtime_waitfree)
326
327	ENTRY(gethrtime_max)
328	NATIVE_TIME_MAX(%g1)
329	NATIVE_TIME_TO_NSEC(%g1, %o0, %o1)
330
331	! hrtime_t's are signed, max hrtime_t must be positive
332	mov	-1, %o2
333	brlz,a	%g1, 1f
334	  srlx	%o2, 1, %g1
3351:
336	retl
337	  mov	%g1, %o0
338	SET_SIZE(gethrtime_max)
339
340	ENTRY(scalehrtime)
341	ldx	[%o0], %o1
342	NATIVE_TIME_TO_NSEC(%o1, %o2, %o3)
343	retl
344	  stx	%o1, [%o0]
345	SET_SIZE(scalehrtime)
346
347/*
348 * Fast trap to return a timestamp, uses trap window, leaves traps
349 * disabled.  Returns a 64-bit nanosecond timestamp in %o0 and %o1.
350 *
351 * This is the handler for the ST_GETHRTIME trap.
352 */
353
354	ENTRY_NP(get_timestamp)
355	GET_HRTIME(%g1, %g2, %g3, %g4, %g5, %o0, %o1, %o2)	! %g1 = hrtime
356	srlx	%g1, 32, %o0				! %o0 = hi32(%g1)
357	srl	%g1, 0, %o1				! %o1 = lo32(%g1)
358	FAST_TRAP_DONE
359	SET_SIZE(get_timestamp)
360
361/*
362 * Macro to convert GET_HRESTIME() bits into a timestamp.
363 *
364 * We use two separate macros so that the platform-dependent GET_HRESTIME()
365 * can be as small as possible; CONV_HRESTIME() implements the generic part.
366 */
367#define	CONV_HRESTIME(hrestsec, hrestnsec, adj, nslt, nano) \
368	brz,pt	adj, 3f;		/* no adjustments, it's easy */	\
369	add	hrestnsec, nslt, hrestnsec; /* hrest.tv_nsec += nslt */	\
370	brlz,pn	adj, 2f;		/* if hrestime_adj negative */	\
371	  srlx	nslt, ADJ_SHIFT, nslt;	/* delay: nslt >>= 4 */		\
372	subcc	adj, nslt, %g0;		/* hrestime_adj - nslt/16 */	\
373	movg	%xcc, nslt, adj;	/* adj by min(adj, nslt/16) */	\
374	ba	3f;			/* go convert to sec/nsec */	\
375	  add	hrestnsec, adj, hrestnsec; /* delay: apply adjustment */ \
3762:	addcc	adj, nslt, %g0;		/* hrestime_adj + nslt/16 */	\
377	bge,a,pt %xcc, 3f;		/* is adj less negative? */	\
378	  add	hrestnsec, adj, hrestnsec; /* yes: hrest.nsec += adj */	\
379	sub	hrestnsec, nslt, hrestnsec; /* no: hrest.nsec -= nslt/16 */ \
3803:	cmp	hrestnsec, nano;	/* more than a billion? */	\
381	bl,pt	%xcc, 4f;		/* if not, we're done */	\
382	  nop;				/* delay: do nothing :( */	\
383	add	hrestsec, 1, hrestsec;	/* hrest.tv_sec++; */		\
384	sub	hrestnsec, nano, hrestnsec; /* hrest.tv_nsec -= NANOSEC; */	\
385	ba,a	3b;			/* check >= billion again */	\
3864:
387
388	ENTRY_NP(gethrestime)
389	GET_HRESTIME(%o1, %o2, %o3, %o4, %o5, %g1, %g2, %g3, %g4)
390	CONV_HRESTIME(%o1, %o2, %o3, %o4, %o5)
391	stn	%o1, [%o0]
392	retl
393	  stn	%o2, [%o0 + CLONGSIZE]
394	SET_SIZE(gethrestime)
395
396/*
397 * Similar to gethrestime(), but gethrestime_sec() returns current hrestime
398 * seconds.
399 */
400	ENTRY_NP(gethrestime_sec)
401	GET_HRESTIME(%o0, %o2, %o3, %o4, %o5, %g1, %g2, %g3, %g4)
402	CONV_HRESTIME(%o0, %o2, %o3, %o4, %o5)
403	retl					! %o0 current hrestime seconds
404	  nop
405	SET_SIZE(gethrestime_sec)
406
407/*
408 * Returns the hrestime on the last tick.  This is simpler than gethrestime()
409 * and gethrestime_sec():  no conversion is required.  gethrestime_lasttick()
410 * follows the same locking algorithm as GET_HRESTIME and GET_HRTIME,
411 * outlined in detail in clock.h.  (Unlike GET_HRESTIME/GET_HRTIME, we don't
412 * rely on load dependencies to effect the membar #LoadLoad, instead declaring
413 * it explicitly.)
414 */
415	ENTRY_NP(gethrestime_lasttick)
416	sethi	%hi(hres_lock), %o1
4170:
418	lduw	[%o1 + %lo(hres_lock)], %o2	! Load lock value
419	membar	#LoadLoad			! Load of lock must complete
420	andn	%o2, 1, %o2			! Mask off lowest bit
421	ldn	[%o1 + %lo(hrestime)], %g1	! Seconds.
422	add	%o1, %lo(hrestime), %o4
423	ldn	[%o4 + CLONGSIZE], %g2		! Nanoseconds.
424	membar	#LoadLoad			! All loads must complete
425	lduw	[%o1 + %lo(hres_lock)], %o3	! Reload lock value
426	cmp	%o3, %o2			! If lock is locked or has
427	bne	0b				!   changed, retry.
428	  stn	%g1, [%o0]			! Delay: store seconds
429	retl
430	  stn	%g2, [%o0 + CLONGSIZE]		! Delay: store nanoseconds
431	SET_SIZE(gethrestime_lasttick)
432
433/*
434 * Fast trap for gettimeofday().  Returns a timestruc_t in %o0 and %o1.
435 *
436 * This is the handler for the ST_GETHRESTIME trap.
437 */
438
439	ENTRY_NP(get_hrestime)
440	GET_HRESTIME(%o0, %o1, %g1, %g2, %g3, %g4, %g5, %o2, %o3)
441	CONV_HRESTIME(%o0, %o1, %g1, %g2, %g3)
442	FAST_TRAP_DONE
443	SET_SIZE(get_hrestime)
444
445/*
446 * Fast trap to return lwp virtual time, uses trap window, leaves traps
447 * disabled.  Returns a 64-bit number in %o0:%o1, which is the number
448 * of nanoseconds consumed.
449 *
450 * This is the handler for the ST_GETHRVTIME trap.
451 *
452 * Register usage:
453 *	%o0, %o1 = return lwp virtual time
454 * 	%o2 = CPU/thread
455 * 	%o3 = lwp
456 * 	%g1 = scratch
457 * 	%g5 = scratch
458 */
459	ENTRY_NP(get_virtime)
460	GET_NATIVE_TIME(%g5, %g1, %g2)	! %g5 = native time in ticks
461	CPU_ADDR(%g2, %g3)			! CPU struct ptr to %g2
462	ldn	[%g2 + CPU_THREAD], %g2		! thread pointer to %g2
463	ldn	[%g2 + T_LWP], %g3		! lwp pointer to %g3
464
465	/*
466	 * Subtract start time of current microstate from time
467	 * of day to get increment for lwp virtual time.
468	 */
469	ldx	[%g3 + LWP_STATE_START], %g1	! ms_state_start
470	sub	%g5, %g1, %g5
471
472	/*
473	 * Add current value of ms_acct[LMS_USER]
474	 */
475	ldx	[%g3 + LWP_ACCT_USER], %g1	! ms_acct[LMS_USER]
476	add	%g5, %g1, %g5
477	NATIVE_TIME_TO_NSEC(%g5, %g1, %o0)
478
479	srl	%g5, 0, %o1			! %o1 = lo32(%g5)
480	srlx	%g5, 32, %o0			! %o0 = hi32(%g5)
481
482	FAST_TRAP_DONE
483	SET_SIZE(get_virtime)
484
485
486
487	.seg	".text"
488hrtime_base_panic:
489	.asciz	"hrtime_base stepping back"
490
491
492	ENTRY_NP(hres_tick)
493	save	%sp, -SA(MINFRAME), %sp	! get a new window
494
495	sethi	%hi(hrestime), %l4
496	ldstub	[%l4 + %lo(hres_lock + HRES_LOCK_OFFSET)], %l5	! try locking
4977:	tst	%l5
498	bz,pt	%xcc, 8f			! if we got it, drive on
499	  ld	[%l4 + %lo(nsec_scale)], %l5	! delay: %l5 = scaling factor
500	ldub	[%l4 + %lo(hres_lock + HRES_LOCK_OFFSET)], %l5
5019:	tst	%l5
502	bz,a,pn	%xcc, 7b
503	  ldstub	[%l4 + %lo(hres_lock + HRES_LOCK_OFFSET)], %l5
504	ba,pt	%xcc, 9b
505	  ldub	[%l4 + %lo(hres_lock + HRES_LOCK_OFFSET)], %l5
5068:
507	membar	#StoreLoad|#StoreStore
508
509	!
510	! update hres_last_tick.  %l5 has the scaling factor (nsec_scale).
511	!
512	ldx	[%l4 + %lo(hrtime_base)], %g1	! load current hrtime_base
513	GET_NATIVE_TIME(%l0, %l3, %l6)		! current native time
514	stx	%l0, [%l4 + %lo(hres_last_tick)]! prev = current
515	! convert native time to nsecs
516	NATIVE_TIME_TO_NSEC_SCALE(%l0, %l5, %l2, NSEC_SHIFT)
517
518	sub	%l0, %g1, %i1			! get accurate nsec delta
519
520	ldx	[%l4 + %lo(hrtime_base)], %l1
521	cmp	%l1, %l0
522	bg,pn	%xcc, 9f
523	  nop
524
525	stx	%l0, [%l4 + %lo(hrtime_base)]	! update hrtime_base
526
527	!
528	! apply adjustment, if any
529	!
530	ldx	[%l4 + %lo(hrestime_adj)], %l0	! %l0 = hrestime_adj
531	brz	%l0, 2f
532						! hrestime_adj == 0 ?
533						! yes, skip adjustments
534	  clr	%l5				! delay: set adj to zero
535	tst	%l0				! is hrestime_adj >= 0 ?
536	bge,pt	%xcc, 1f			! yes, go handle positive case
537	  srl	%i1, ADJ_SHIFT, %l5		! delay: %l5 = adj
538
539	addcc	%l0, %l5, %g0			! hrestime_adj < -adj ?
540	bl,pt	%xcc, 2f			! yes, use current adj
541	  neg	%l5				! delay: %l5 = -adj
542	ba,pt	%xcc, 2f
543	  mov	%l0, %l5			! no, so set adj = hrestime_adj
5441:
545	subcc	%l0, %l5, %g0			! hrestime_adj < adj ?
546	bl,a,pt	%xcc, 2f			! yes, set adj = hrestime_adj
547	  mov	%l0, %l5			! delay: adj = hrestime_adj
5482:
549	ldx	[%l4 + %lo(timedelta)], %l0	! %l0 = timedelta
550	sub	%l0, %l5, %l0			! timedelta -= adj
551
552	stx	%l0, [%l4 + %lo(timedelta)]	! store new timedelta
553	stx	%l0, [%l4 + %lo(hrestime_adj)]	! hrestime_adj = timedelta
554
555	or	%l4, %lo(hrestime), %l2
556	ldn	[%l2], %i2			! %i2:%i3 = hrestime sec:nsec
557	ldn	[%l2 + CLONGSIZE], %i3
558	add	%i3, %l5, %i3			! hrestime.nsec += adj
559	add	%i3, %i1, %i3			! hrestime.nsec += nslt
560
561	set	NANOSEC, %l5			! %l5 = NANOSEC
562	cmp	%i3, %l5
563	bl,pt	%xcc, 5f			! if hrestime.tv_nsec < NANOSEC
564	  sethi	%hi(one_sec), %i1		! delay
565	add	%i2, 0x1, %i2			! hrestime.tv_sec++
566	sub	%i3, %l5, %i3			! hrestime.tv_nsec - NANOSEC
567	mov	0x1, %l5
568	st	%l5, [%i1 + %lo(one_sec)]
5695:
570	stn	%i2, [%l2]
571	stn	%i3, [%l2 + CLONGSIZE]		! store the new hrestime
572
573	membar	#StoreStore
574
575	ld	[%l4 + %lo(hres_lock)], %i1
576	inc	%i1				! release lock
577	st	%i1, [%l4 + %lo(hres_lock)]	! clear hres_lock
578
579	ret
580	restore
581
5829:
583	!
584	! release hres_lock
585	!
586	ld	[%l4 + %lo(hres_lock)], %i1
587	inc	%i1
588	st	%i1, [%l4 + %lo(hres_lock)]
589
590	sethi	%hi(hrtime_base_panic), %o0
591	call	panic
592	  or	%o0, %lo(hrtime_base_panic), %o0
593
594	SET_SIZE(hres_tick)
595
596#endif	/* lint */
597
598#if !defined(lint) && !defined(__lint)
599
600	.seg	".text"
601kstat_q_panic_msg:
602	.asciz	"kstat_q_exit: qlen == 0"
603
604	ENTRY(kstat_q_panic)
605	save	%sp, -SA(MINFRAME), %sp
606	sethi	%hi(kstat_q_panic_msg), %o0
607	call	panic
608	  or	%o0, %lo(kstat_q_panic_msg), %o0
609	/*NOTREACHED*/
610	SET_SIZE(kstat_q_panic)
611
612#define	BRZPN	brz,pn
613#define	BRZPT	brz,pt
614
615#define	KSTAT_Q_UPDATE(QOP, QBR, QZERO, QRETURN, QTYPE) \
616	ld	[%o0 + QTYPE/**/CNT], %o1;	/* %o1 = old qlen */	\
617	QOP	%o1, 1, %o2;			/* %o2 = new qlen */	\
618	QBR	%o1, QZERO;			/* done if qlen == 0 */	\
619	st	%o2, [%o0 + QTYPE/**/CNT];	/* delay: save qlen */	\
620	ldx	[%o0 + QTYPE/**/LASTUPDATE], %o3;			\
621	ldx	[%o0 + QTYPE/**/TIME], %o4;	/* %o4 = old time */	\
622	ldx	[%o0 + QTYPE/**/LENTIME], %o5;	/* %o5 = old lentime */	\
623	sub	%g1, %o3, %o2;			/* %o2 = time delta */	\
624	mulx	%o1, %o2, %o3;			/* %o3 = cur lentime */	\
625	add	%o4, %o2, %o4;			/* %o4 = new time */	\
626	add	%o5, %o3, %o5;			/* %o5 = new lentime */	\
627	stx	%o4, [%o0 + QTYPE/**/TIME];	/* save time */		\
628	stx	%o5, [%o0 + QTYPE/**/LENTIME];	/* save lentime */	\
629QRETURN;								\
630	stx	%g1, [%o0 + QTYPE/**/LASTUPDATE]; /* lastupdate = now */
631
632	.align 16
633	ENTRY(kstat_waitq_enter)
634	GET_NATIVE_TIME(%g1, %g2, %g3)
635	KSTAT_Q_UPDATE(add, BRZPT, 1f, 1:retl, KSTAT_IO_W)
636	SET_SIZE(kstat_waitq_enter)
637
638	.align 16
639	ENTRY(kstat_waitq_exit)
640	GET_NATIVE_TIME(%g1, %g2, %g3)
641	KSTAT_Q_UPDATE(sub, BRZPN, kstat_q_panic, retl, KSTAT_IO_W)
642	SET_SIZE(kstat_waitq_exit)
643
644	.align 16
645	ENTRY(kstat_runq_enter)
646	GET_NATIVE_TIME(%g1, %g2, %g3)
647	KSTAT_Q_UPDATE(add, BRZPT, 1f, 1:retl, KSTAT_IO_R)
648	SET_SIZE(kstat_runq_enter)
649
650	.align 16
651	ENTRY(kstat_runq_exit)
652	GET_NATIVE_TIME(%g1, %g2, %g3)
653	KSTAT_Q_UPDATE(sub, BRZPN, kstat_q_panic, retl, KSTAT_IO_R)
654	SET_SIZE(kstat_runq_exit)
655
656	.align 16
657	ENTRY(kstat_waitq_to_runq)
658	GET_NATIVE_TIME(%g1, %g2, %g3)
659	KSTAT_Q_UPDATE(sub, BRZPN, kstat_q_panic, 1:, KSTAT_IO_W)
660	KSTAT_Q_UPDATE(add, BRZPT, 1f, 1:retl, KSTAT_IO_R)
661	SET_SIZE(kstat_waitq_to_runq)
662
663	.align 16
664	ENTRY(kstat_runq_back_to_waitq)
665	GET_NATIVE_TIME(%g1, %g2, %g3)
666	KSTAT_Q_UPDATE(sub, BRZPN, kstat_q_panic, 1:, KSTAT_IO_R)
667	KSTAT_Q_UPDATE(add, BRZPT, 1f, 1:retl, KSTAT_IO_W)
668	SET_SIZE(kstat_runq_back_to_waitq)
669
670#endif /* lint */
671
672#ifdef lint
673
674int64_t timedelta;
675hrtime_t hres_last_tick;
676volatile timestruc_t hrestime;
677int64_t hrestime_adj;
678volatile int hres_lock;
679uint_t nsec_scale;
680hrtime_t hrtime_base;
681int traptrace_use_stick;
682
683#else
684	/*
685	 *  -- WARNING --
686	 *
687	 * The following variables MUST be together on a 128-byte boundary.
688	 * In addition to the primary performance motivation (having them all
689	 * on the same cache line(s)), code here and in the GET*TIME() macros
690	 * assumes that they all have the same high 22 address bits (so
691	 * there's only one sethi).
692	 */
693	.seg	".data"
694	.global	timedelta, hres_last_tick, hrestime, hrestime_adj
695	.global	hres_lock, nsec_scale, hrtime_base, traptrace_use_stick
696	.global	nsec_shift, adj_shift
697
698	/* XXX - above comment claims 128-bytes is necessary */
699	.align	64
700timedelta:
701	.word	0, 0		/* int64_t */
702hres_last_tick:
703	.word	0, 0		/* hrtime_t */
704hrestime:
705	.nword	0, 0		/* 2 longs */
706hrestime_adj:
707	.word	0, 0		/* int64_t */
708hres_lock:
709	.word	0
710nsec_scale:
711	.word	0
712hrtime_base:
713	.word	0, 0
714traptrace_use_stick:
715	.word	0
716nsec_shift:
717	.word	NSEC_SHIFT
718adj_shift:
719	.word	ADJ_SHIFT
720
721#endif
722
723
724/*
725 * drv_usecwait(clock_t n)	[DDI/DKI - section 9F]
726 * usec_delay(int n)		[compatibility - should go one day]
727 * Delay by spinning.
728 *
729 * delay for n microseconds.  numbers <= 0 delay 1 usec
730 *
731 * With UltraSPARC-III the combination of supporting mixed-speed CPUs
732 * and variable clock rate for power management requires that we
733 * use %stick to implement this routine.
734 */
735
736#if defined(lint)
737
738/*ARGSUSED*/
739void
740drv_usecwait(clock_t n)
741{}
742
743/*ARGSUSED*/
744void
745usec_delay(int n)
746{}
747
748#else	/* lint */
749
750	ENTRY(drv_usecwait)
751	ALTENTRY(usec_delay)
752	brlez,a,pn %o0, 0f
753	  mov	1, %o0
7540:
755	sethi	%hi(sticks_per_usec), %o1
756	lduw	[%o1 + %lo(sticks_per_usec)], %o1
757	mulx	%o1, %o0, %o1		! Scale usec to ticks
758	inc	%o1			! We don't start on a tick edge
759	GET_NATIVE_TIME(%o2, %o3, %o4)
760	add	%o1, %o2, %o1
761
7621:	cmp	%o1, %o2
763	GET_NATIVE_TIME(%o2, %o3, %o4)
764	bgeu,pt	%xcc, 1b
765	  nop
766	retl
767	  nop
768	SET_SIZE(usec_delay)
769	SET_SIZE(drv_usecwait)
770#endif	/* lint */
771
772#if defined(lint)
773
774/* ARGSUSED */
775void
776pil14_interrupt(int level)
777{}
778
779#else
780
781/*
782 * Level-14 interrupt prologue.
783 */
784	ENTRY_NP(pil14_interrupt)
785	CPU_ADDR(%g1, %g2)
786	rdpr	%pil, %g6			! %g6 = interrupted PIL
787	stn	%g6, [%g1 + CPU_PROFILE_PIL]	! record interrupted PIL
788	rdpr	%tstate, %g6
789	rdpr	%tpc, %g5
790	btst	TSTATE_PRIV, %g6		! trap from supervisor mode?
791	bnz,a,pt %xcc, 1f
792	  stn	%g5, [%g1 + CPU_PROFILE_PC]	! if so, record kernel PC
793	stn	%g5, [%g1 + CPU_PROFILE_UPC]	! if not, record user PC
794	ba	pil_interrupt_common		! must be large-disp branch
795	  stn	%g0, [%g1 + CPU_PROFILE_PC]	! zero kernel PC
7961:	ba	pil_interrupt_common		! must be large-disp branch
797	  stn	%g0, [%g1 + CPU_PROFILE_UPC]	! zero user PC
798	SET_SIZE(pil14_interrupt)
799
800	ENTRY_NP(tick_rtt)
801	!
802	! Load TICK_COMPARE into %o5; if bit 63 is set, then TICK_COMPARE is
803	! disabled.  If TICK_COMPARE is enabled, we know that we need to
804	! reenqueue the interrupt request structure.  We'll then check TICKINT
805	! in SOFTINT; if it's set, then we know that we were in a TICK_COMPARE
806	! interrupt.  In this case, TICK_COMPARE may have been rewritten
807	! recently; we'll compare %o5 to the current time to verify that it's
808	! in the future.
809	!
810	! Note that %o5 is live until after 1f.
811	! XXX - there is a subroutine call while %o5 is live!
812	!
813	RD_TICKCMPR(%o5, %g1)
814	srlx	%o5, TICKINT_DIS_SHFT, %g1
815	brnz,pt	%g1, 2f
816	  nop
817
818	rdpr 	%pstate, %g5
819	andn	%g5, PSTATE_IE, %g1
820	wrpr	%g0, %g1, %pstate		! Disable vec interrupts
821
822	sethi	%hi(cbe_level14_inum), %o1
823	ldx	[%o1 + %lo(cbe_level14_inum)], %o1
824	call	intr_enqueue_req ! preserves %o5 and %g5
825	  mov	PIL_14, %o0
826
827	! Check SOFTINT for TICKINT/STICKINT
828	rd	SOFTINT, %o4
829	set	(TICK_INT_MASK | STICK_INT_MASK), %o0
830	andcc	%o4, %o0, %g0
831	bz,a,pn	%icc, 2f
832	  wrpr	%g0, %g5, %pstate		! Enable vec interrupts
833
834	! clear TICKINT/STICKINT
835	wr	%o0, CLEAR_SOFTINT
836
837	!
838	! Now that we've cleared TICKINT, we can reread %tick and confirm
839	! that the value we programmed is still in the future.  If it isn't,
840	! we need to reprogram TICK_COMPARE to fire as soon as possible.
841	!
842	GET_NATIVE_TIME(%o0, %g1, %g2)		! %o0 = tick
843	sllx	%o0, 1, %o0			! Clear the DIS bit
844	srlx	%o0, 1, %o0
845	cmp	%o5, %o0			! In the future?
846	bg,a,pt	%xcc, 2f			! Yes, drive on.
847	  wrpr	%g0, %g5, %pstate		!   delay: enable vec intr
848
849	!
850	! If we're here, then we have programmed TICK_COMPARE with a %tick
851	! which is in the past; we'll now load an initial step size, and loop
852	! until we've managed to program TICK_COMPARE to fire in the future.
853	!
854	mov	8, %o4				! 8 = arbitrary inital step
8551:	add	%o0, %o4, %o5			! Add the step
856	WR_TICKCMPR(%o5,%g1,%g2,__LINE__)	! Write to TICK_CMPR
857	GET_NATIVE_TIME(%o0, %g1, %g2)		! %o0 = tick
858	sllx	%o0, 1, %o0			! Clear the DIS bit
859	srlx	%o0, 1, %o0
860	cmp	%o5, %o0			! In the future?
861	bg,a,pt	%xcc, 2f			! Yes, drive on.
862	  wrpr	%g0, %g5, %pstate		!    delay: enable vec intr
863	ba	1b				! No, try again.
864	  sllx	%o4, 1, %o4			!    delay: double step size
865
8662:	ba	current_thread_complete
867	  nop
868	SET_SIZE(tick_rtt)
869
870#endif /* lint */
871
872#if defined(lint)
873/*
874 * Prefetch a page_t for write or read, this assumes a linear
875 * scan of sequential page_t's.
876 */
877/*ARGSUSED*/
878void
879prefetch_page_w(void *pp)
880{}
881
882/*ARGSUSED*/
883void
884prefetch_page_r(void *pp)
885{}
886#else	/* lint */
887
888/* XXXQ These should be inline templates, not functions */
889        ENTRY(prefetch_page_w)
890        retl
891	  nop
892        SET_SIZE(prefetch_page_w)
893
894        ENTRY(prefetch_page_r)
895        retl
896	  nop
897        SET_SIZE(prefetch_page_r)
898
899#endif	/* lint */
900
901#if defined(lint)
902/*
903 * Prefetch struct smap for write.
904 */
905/*ARGSUSED*/
906void
907prefetch_smap_w(void *smp)
908{}
909#else	/* lint */
910
911/* XXXQ These should be inline templates, not functions */
912	ENTRY(prefetch_smap_w)
913	retl
914	  nop
915	SET_SIZE(prefetch_smap_w)
916
917#endif	/* lint */
918
919/*
920 * Generic sun4v MMU and Cache operations.
921 */
922
923#if defined(lint)
924
925/*ARGSUSED*/
926void
927vtag_flushpage(caddr_t vaddr, uint64_t sfmmup)
928{}
929
930/*ARGSUSED*/
931void
932vtag_flushall(void)
933{}
934
935/*ARGSUSED*/
936void
937vtag_unmap_perm_tl1(uint64_t vaddr, uint64_t ctxnum)
938{}
939
940/*ARGSUSED*/
941void
942vtag_flushpage_tl1(uint64_t vaddr, uint64_t sfmmup)
943{}
944
945/*ARGSUSED*/
946void
947vtag_flush_pgcnt_tl1(uint64_t vaddr, uint64_t sfmmup_pgcnt)
948{}
949
950/*ARGSUSED*/
951void
952vtag_flushall_tl1(uint64_t dummy1, uint64_t dummy2)
953{}
954
955/*ARGSUSED*/
956void
957vac_flushpage(pfn_t pfnum, int vcolor)
958{}
959
960/*ARGSUSED*/
961void
962vac_flushpage_tl1(uint64_t pfnum, uint64_t vcolor)
963{}
964
965/*ARGSUSED*/
966void
967flush_instr_mem(caddr_t vaddr, size_t len)
968{}
969
970#else	/* lint */
971
972	ENTRY_NP(vtag_flushpage)
973	/*
974	 * flush page from the tlb
975	 *
976	 * %o0 = vaddr
977	 * %o1 = sfmmup
978	 */
979	SFMMU_CPU_CNUM(%o1, %g1, %g2)   /* %g1 = sfmmu cnum on this CPU */
980
981	mov	%g1, %o1
982	mov	MAP_ITLB | MAP_DTLB, %o2
983	ta	MMU_UNMAP_ADDR
984	brz,pt	%o0, 1f
985	  nop
986	ba	panic_bad_hcall
987	  mov	MMU_UNMAP_ADDR, %o1
9881:
989 	retl
990	  nop
991	SET_SIZE(vtag_flushpage)
992
993	ENTRY_NP(vtag_flushall)
994	mov	%g0, %o0	! XXX no cpu list yet
995	mov	%g0, %o1	! XXX no cpu list yet
996	mov	MAP_ITLB | MAP_DTLB, %o2
997	mov	MMU_DEMAP_ALL, %o5
998	ta	FAST_TRAP
999	brz,pt	%o0, 1f
1000	  nop
1001	ba	panic_bad_hcall
1002	  mov	MMU_DEMAP_ALL, %o1
10031:
1004	retl
1005	  nop
1006	SET_SIZE(vtag_flushall)
1007
1008	ENTRY_NP(vtag_unmap_perm_tl1)
1009	/*
1010	 * x-trap to unmap perm map entry
1011	 * %g1 = vaddr
1012	 * %g2 = ctxnum (KCONTEXT only)
1013	 */
1014	mov	%o0, %g3
1015	mov	%o1, %g4
1016	mov	%o2, %g5
1017	mov	%o5, %g6
1018	mov	%g1, %o0
1019	mov	%g2, %o1
1020	mov	MAP_ITLB | MAP_DTLB, %o2
1021	mov	UNMAP_PERM_ADDR, %o5
1022	ta	FAST_TRAP
1023	brz,pt	%o0, 1f
1024	nop
1025
1026	mov	PTL1_BAD_HCALL, %g1
1027
1028	cmp	%o0, H_ENOMAP
1029	move	%xcc, PTL1_BAD_HCALL_UNMAP_PERM_ENOMAP, %g1
1030
1031	cmp	%o0, H_EINVAL
1032	move	%xcc, PTL1_BAD_HCALL_UNMAP_PERM_EINVAL, %g1
1033
1034	ba,a	ptl1_panic
10351:
1036	mov	%g6, %o5
1037	mov	%g5, %o2
1038	mov	%g4, %o1
1039	mov	%g3, %o0
1040	retry
1041	SET_SIZE(vtag_unmap_perm_tl1)
1042
1043	ENTRY_NP(vtag_flushpage_tl1)
1044	/*
1045	 * x-trap to flush page from tlb and tsb
1046	 *
1047	 * %g1 = vaddr, zero-extended on 32-bit kernel
1048	 * %g2 = sfmmup
1049	 *
1050	 * assumes TSBE_TAG = 0
1051	 */
1052	srln	%g1, MMU_PAGESHIFT, %g1
1053	slln	%g1, MMU_PAGESHIFT, %g1			/* g1 = vaddr */
1054	mov	%o0, %g3
1055	mov	%o1, %g4
1056	mov	%o2, %g5
1057	mov	%g1, %o0			/* vaddr */
1058
1059	SFMMU_CPU_CNUM(%g2, %o1, %g6)   /* %o1 = sfmmu cnum on this CPU */
1060
1061	mov	MAP_ITLB | MAP_DTLB, %o2
1062	ta	MMU_UNMAP_ADDR
1063	brz,pt	%o0, 1f
1064	nop
1065	  ba	ptl1_panic
1066	mov	PTL1_BAD_HCALL, %g1
10671:
1068	mov	%g5, %o2
1069	mov	%g4, %o1
1070	mov	%g3, %o0
1071	membar #Sync
1072	retry
1073	SET_SIZE(vtag_flushpage_tl1)
1074
1075	ENTRY_NP(vtag_flush_pgcnt_tl1)
1076	/*
1077	 * x-trap to flush pgcnt MMU_PAGESIZE pages from tlb
1078	 *
1079	 * %g1 = vaddr, zero-extended on 32-bit kernel
1080	 * %g2 = <sfmmup58|pgcnt6>, (pgcnt - 1) is pass'ed in via pgcnt6 bits.
1081	 *
1082	 * NOTE: this handler relies on the fact that no
1083	 *	interrupts or traps can occur during the loop
1084	 *	issuing the TLB_DEMAP operations. It is assumed
1085	 *	that interrupts are disabled and this code is
1086	 *	fetching from the kernel locked text address.
1087	 *
1088	 * assumes TSBE_TAG = 0
1089	 */
1090	srln	%g1, MMU_PAGESHIFT, %g1
1091	slln	%g1, MMU_PAGESHIFT, %g1		/* g1 = vaddr */
1092	mov	%o0, %g3
1093	mov	%o1, %g4
1094	mov	%o2, %g5
1095
1096	and	%g2, SFMMU_PGCNT_MASK, %g7	/* g7 = pgcnt - 1 */
1097	add	%g7, 1, %g7			/* g7 = pgcnt */
1098
1099        andn    %g2, SFMMU_PGCNT_MASK, %o0      /* %o0 = sfmmup */
1100
1101	SFMMU_CPU_CNUM(%o0, %g2, %g6)    /* %g2 = sfmmu cnum on this CPU */
1102
1103	set	MMU_PAGESIZE, %g6		/* g6 = pgsize */
1104
11051:
1106	mov	%g1, %o0			/* vaddr */
1107	mov	%g2, %o1			/* cnum */
1108	mov	MAP_ITLB | MAP_DTLB, %o2
1109	ta	MMU_UNMAP_ADDR
1110	brz,pt	%o0, 2f
1111	  nop
1112	ba	ptl1_panic
1113	  mov	PTL1_BAD_HCALL, %g1
11142:
1115	deccc	%g7				/* decr pgcnt */
1116	bnz,pt	%icc,1b
1117	  add	%g1, %g6, %g1			/* go to nextpage */
1118
1119	mov	%g5, %o2
1120	mov	%g4, %o1
1121	mov	%g3, %o0
1122	membar #Sync
1123	retry
1124	SET_SIZE(vtag_flush_pgcnt_tl1)
1125
1126	! Not implemented on US1/US2
1127	ENTRY_NP(vtag_flushall_tl1)
1128	mov	%o0, %g3
1129	mov	%o1, %g4
1130	mov	%o2, %g5
1131	mov	%o3, %g6	! XXXQ not used?
1132	mov	%o5, %g7
1133	mov	%g0, %o0	! XXX no cpu list yet
1134	mov	%g0, %o1	! XXX no cpu list yet
1135	mov	MAP_ITLB | MAP_DTLB, %o2
1136	mov	MMU_DEMAP_ALL, %o5
1137	ta	FAST_TRAP
1138	brz,pt	%o0, 1f
1139	  nop
1140	ba	ptl1_panic
1141	  mov	PTL1_BAD_HCALL, %g1
11421:
1143	mov	%g7, %o5
1144	mov	%g6, %o3	! XXXQ not used?
1145	mov	%g5, %o2
1146	mov	%g4, %o1
1147	mov	%g3, %o0
1148	retry
1149	SET_SIZE(vtag_flushall_tl1)
1150
1151/*
1152 * flush_instr_mem:
1153 *	Flush a portion of the I-$ starting at vaddr
1154 * 	%o0 vaddr
1155 *	%o1 bytes to be flushed
1156 */
1157
1158	ENTRY(flush_instr_mem)
1159	membar	#StoreStore				! Ensure the stores
1160							! are globally visible
11611:
1162	flush	%o0
1163	subcc	%o1, ICACHE_FLUSHSZ, %o1		! bytes = bytes-0x20
1164	bgu,pt	%ncc, 1b
1165	  add	%o0, ICACHE_FLUSHSZ, %o0		! vaddr = vaddr+0x20
1166
1167	retl
1168	  nop
1169	SET_SIZE(flush_instr_mem)
1170
1171#endif /* !lint */
1172
1173#if !defined(CUSTOM_FPZERO)
1174
1175/*
1176 * fp_zero() - clear all fp data registers and the fsr
1177 */
1178
1179#if defined(lint) || defined(__lint)
1180
1181void
1182fp_zero(void)
1183{}
1184
1185#else	/* lint */
1186
1187.global	fp_zero_zero
1188.align 8
1189fp_zero_zero:
1190	.xword	0
1191
1192	ENTRY_NP(fp_zero)
1193	sethi	%hi(fp_zero_zero), %o0
1194	ldx	[%o0 + %lo(fp_zero_zero)], %fsr
1195	ldd	[%o0 + %lo(fp_zero_zero)], %f0
1196	fmovd	%f0, %f2
1197	fmovd	%f0, %f4
1198	fmovd	%f0, %f6
1199	fmovd	%f0, %f8
1200	fmovd	%f0, %f10
1201	fmovd	%f0, %f12
1202	fmovd	%f0, %f14
1203	fmovd	%f0, %f16
1204	fmovd	%f0, %f18
1205	fmovd	%f0, %f20
1206	fmovd	%f0, %f22
1207	fmovd	%f0, %f24
1208	fmovd	%f0, %f26
1209	fmovd	%f0, %f28
1210	fmovd	%f0, %f30
1211	fmovd	%f0, %f32
1212	fmovd	%f0, %f34
1213	fmovd	%f0, %f36
1214	fmovd	%f0, %f38
1215	fmovd	%f0, %f40
1216	fmovd	%f0, %f42
1217	fmovd	%f0, %f44
1218	fmovd	%f0, %f46
1219	fmovd	%f0, %f48
1220	fmovd	%f0, %f50
1221	fmovd	%f0, %f52
1222	fmovd	%f0, %f54
1223	fmovd	%f0, %f56
1224	fmovd	%f0, %f58
1225	fmovd	%f0, %f60
1226	retl
1227	fmovd	%f0, %f62
1228	SET_SIZE(fp_zero)
1229
1230#endif	/* lint */
1231#endif  /* CUSTOM_FPZERO */
1232