10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 511172SHaik.Aftandilian@Sun.COM * Common Development and Distribution License (the "License"). 611172SHaik.Aftandilian@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*12235Sprashanth.sreenivasa@oracle.com * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate #ifndef _SYS_MACHCLOCK_H 260Sstevel@tonic-gate #define _SYS_MACHCLOCK_H 270Sstevel@tonic-gate 2811172SHaik.Aftandilian@Sun.COM #include <sys/intreg.h> 290Sstevel@tonic-gate 300Sstevel@tonic-gate #ifdef __cplusplus 310Sstevel@tonic-gate extern "C" { 320Sstevel@tonic-gate #endif 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 3511172SHaik.Aftandilian@Sun.COM * Tick/Stick Register Access 3611172SHaik.Aftandilian@Sun.COM * 3711172SHaik.Aftandilian@Sun.COM * The following assembly language macros are defined for reading 3811172SHaik.Aftandilian@Sun.COM * the %tick and %stick registers as well as reading and writing 3911172SHaik.Aftandilian@Sun.COM * the stick compare register. With the exception of trapstat, reads 4011172SHaik.Aftandilian@Sun.COM * and writes of these registers all take into account an offset 4111172SHaik.Aftandilian@Sun.COM * value which is added to the hardware counter. By default, this 4211172SHaik.Aftandilian@Sun.COM * offset is zero. The offsets can only be modified when CPUs are 4311172SHaik.Aftandilian@Sun.COM * paused and are only intended to be modified during an OS suspend 4411172SHaik.Aftandilian@Sun.COM * operation. 4511172SHaik.Aftandilian@Sun.COM * 4611172SHaik.Aftandilian@Sun.COM * Since the read of the %tick or %stick is not an atomic operation, 4711172SHaik.Aftandilian@Sun.COM * it is possible for a suspend operation to occur between the read 4811172SHaik.Aftandilian@Sun.COM * of the hardware register and its offset variable. The default 4911172SHaik.Aftandilian@Sun.COM * macros here take this into account by comparing the value of the 5011172SHaik.Aftandilian@Sun.COM * offset variable before and after reading the hardware register. 5111172SHaik.Aftandilian@Sun.COM * Callers that need to read the %tick register and can guarantee 5211172SHaik.Aftandilian@Sun.COM * they will not be preempted can use the RD_TICK_NO_SUSPEND_CHECK 5311172SHaik.Aftandilian@Sun.COM * which does not check for native_tick_offset changing. 5411172SHaik.Aftandilian@Sun.COM */ 5511172SHaik.Aftandilian@Sun.COM #define RD_STICK(out, scr1, scr2, label) \ 5611172SHaik.Aftandilian@Sun.COM .rd_stick.label: \ 5711172SHaik.Aftandilian@Sun.COM sethi %hi(native_stick_offset), scr1; \ 5811172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr2; \ 5911172SHaik.Aftandilian@Sun.COM rd STICK, out; \ 6011172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr1; \ 6111172SHaik.Aftandilian@Sun.COM sub scr1, scr2, scr2; \ 6211172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 6311172SHaik.Aftandilian@Sun.COM brnz,pn scr2, .rd_stick.label; \ 6411172SHaik.Aftandilian@Sun.COM sllx out, 1, out; \ 6511172SHaik.Aftandilian@Sun.COM srlx out, 1, out; \ 6611172SHaik.Aftandilian@Sun.COM add out, scr1, out 6711172SHaik.Aftandilian@Sun.COM 68*12235Sprashanth.sreenivasa@oracle.com /* 69*12235Sprashanth.sreenivasa@oracle.com * These macros on sun4v read the %stick register, because : 70*12235Sprashanth.sreenivasa@oracle.com * 71*12235Sprashanth.sreenivasa@oracle.com * For sun4v platforms %tick can change dynamically *without* kernel 72*12235Sprashanth.sreenivasa@oracle.com * knowledge, due to SP side power & thermal management cases, 73*12235Sprashanth.sreenivasa@oracle.com * which is triggered externally by SP and handled by Hypervisor. 74*12235Sprashanth.sreenivasa@oracle.com * 75*12235Sprashanth.sreenivasa@oracle.com * The frequency of %tick cannot be relied upon by kernel code, 76*12235Sprashanth.sreenivasa@oracle.com * since it changes dynamically without the kernel being aware. 77*12235Sprashanth.sreenivasa@oracle.com * So, always use the constant-frequency %stick on sun4v. 78*12235Sprashanth.sreenivasa@oracle.com */ 79*12235Sprashanth.sreenivasa@oracle.com #define RD_CLOCK_TICK(out, scr1, scr2, label) \ 80*12235Sprashanth.sreenivasa@oracle.com /* CSTYLED */ \ 81*12235Sprashanth.sreenivasa@oracle.com RD_STICK(out,scr1,scr2,label) 82*12235Sprashanth.sreenivasa@oracle.com 83*12235Sprashanth.sreenivasa@oracle.com #define RD_STICK_NO_SUSPEND_CHECK(out, scr1) \ 84*12235Sprashanth.sreenivasa@oracle.com sethi %hi(native_stick_offset), scr1; \ 85*12235Sprashanth.sreenivasa@oracle.com ldx [scr1 + %lo(native_stick_offset)], scr1; \ 86*12235Sprashanth.sreenivasa@oracle.com rd STICK, out; \ 87*12235Sprashanth.sreenivasa@oracle.com sllx out, 1, out; \ 88*12235Sprashanth.sreenivasa@oracle.com srlx out, 1, out; \ 89*12235Sprashanth.sreenivasa@oracle.com add out, scr1, out 90*12235Sprashanth.sreenivasa@oracle.com 91*12235Sprashanth.sreenivasa@oracle.com #define RD_CLOCK_TICK_NO_SUSPEND_CHECK(out, scr1) \ 92*12235Sprashanth.sreenivasa@oracle.com /* CSTYLED */ \ 93*12235Sprashanth.sreenivasa@oracle.com RD_STICK_NO_SUSPEND_CHECK(out,scr1) 94*12235Sprashanth.sreenivasa@oracle.com 95*12235Sprashanth.sreenivasa@oracle.com #ifndef _ASM 96*12235Sprashanth.sreenivasa@oracle.com #ifdef _KERNEL 97*12235Sprashanth.sreenivasa@oracle.com extern u_longlong_t gettick(void); 98*12235Sprashanth.sreenivasa@oracle.com #define CLOCK_TICK_COUNTER() gettick() /* returns %stick */ 99*12235Sprashanth.sreenivasa@oracle.com #endif /* _KERNEL */ 100*12235Sprashanth.sreenivasa@oracle.com #endif /* _ASM */ 101*12235Sprashanth.sreenivasa@oracle.com 102*12235Sprashanth.sreenivasa@oracle.com 10311172SHaik.Aftandilian@Sun.COM #define RD_TICK(out, scr1, scr2, label) \ 10411172SHaik.Aftandilian@Sun.COM .rd_tick.label: \ 10511172SHaik.Aftandilian@Sun.COM sethi %hi(native_tick_offset), scr1; \ 10611172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_tick_offset)], scr2; \ 10711172SHaik.Aftandilian@Sun.COM rd %tick, out; \ 10811172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_tick_offset)], scr1; \ 10911172SHaik.Aftandilian@Sun.COM sub scr1, scr2, scr2; \ 11011172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 11111172SHaik.Aftandilian@Sun.COM brnz,pn scr2, .rd_tick.label; \ 11211172SHaik.Aftandilian@Sun.COM sllx out, 1, out; \ 11311172SHaik.Aftandilian@Sun.COM srlx out, 1, out; \ 11411172SHaik.Aftandilian@Sun.COM add out, scr1, out 11511172SHaik.Aftandilian@Sun.COM 11611172SHaik.Aftandilian@Sun.COM #define RD_TICK_NO_SUSPEND_CHECK(out, scr1) \ 11711172SHaik.Aftandilian@Sun.COM sethi %hi(native_tick_offset), scr1; \ 11811172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_tick_offset)], scr1; \ 11911172SHaik.Aftandilian@Sun.COM rd %tick, out; \ 12011172SHaik.Aftandilian@Sun.COM sllx out, 1, out; \ 12111172SHaik.Aftandilian@Sun.COM srlx out, 1, out; \ 12211172SHaik.Aftandilian@Sun.COM add out, scr1, out 12311172SHaik.Aftandilian@Sun.COM 12411172SHaik.Aftandilian@Sun.COM /* 12511172SHaik.Aftandilian@Sun.COM * Read the %stick register without taking the native_stick_offset 12611172SHaik.Aftandilian@Sun.COM * into account. 12711172SHaik.Aftandilian@Sun.COM */ 12811172SHaik.Aftandilian@Sun.COM #define RD_STICK_PHYSICAL(out) \ 12911172SHaik.Aftandilian@Sun.COM rd %stick, out 13011172SHaik.Aftandilian@Sun.COM 13111172SHaik.Aftandilian@Sun.COM /* 13211172SHaik.Aftandilian@Sun.COM * Read the %tick register without taking the native_tick_offset 13311172SHaik.Aftandilian@Sun.COM * into account. Required to be a single instruction, usable in a 13411172SHaik.Aftandilian@Sun.COM * delay slot. 13511172SHaik.Aftandilian@Sun.COM */ 13611172SHaik.Aftandilian@Sun.COM #define RD_TICK_PHYSICAL(out) \ 13711172SHaik.Aftandilian@Sun.COM rd %tick, out 13811172SHaik.Aftandilian@Sun.COM 13911172SHaik.Aftandilian@Sun.COM /* 14011172SHaik.Aftandilian@Sun.COM * For traptrace, which requires either the %tick or %stick 14111172SHaik.Aftandilian@Sun.COM * counter depending on the value of a global variable. 14211172SHaik.Aftandilian@Sun.COM * If the kernel variable passed in as 'use_stick' is non-zero, 14311172SHaik.Aftandilian@Sun.COM * read the %stick counter into the 'out' register, otherwise, 14411172SHaik.Aftandilian@Sun.COM * read the %tick counter. Note the label-less branches. 14511172SHaik.Aftandilian@Sun.COM * We do not check for the tick or stick offset variables changing 14611172SHaik.Aftandilian@Sun.COM * during the course of the macro's execution and as a result 14711172SHaik.Aftandilian@Sun.COM * if a suspend operation occurs between the time the offset 14811172SHaik.Aftandilian@Sun.COM * variable is read and the hardware register is read, we will 14911172SHaik.Aftandilian@Sun.COM * use an inaccurate traptrace timestamp. 15011172SHaik.Aftandilian@Sun.COM */ 15111172SHaik.Aftandilian@Sun.COM #define RD_TICKSTICK_FLAG(out, scr1, use_stick) \ 15211172SHaik.Aftandilian@Sun.COM sethi %hi(use_stick), scr1; \ 15311172SHaik.Aftandilian@Sun.COM lduw [scr1 + %lo(use_stick)], scr1; \ 15411172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 15511172SHaik.Aftandilian@Sun.COM brz,a scr1, .+24; \ 15611172SHaik.Aftandilian@Sun.COM rd %tick, out; \ 15711172SHaik.Aftandilian@Sun.COM sethi %hi(native_stick_offset), scr1; \ 15811172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr1; \ 15911172SHaik.Aftandilian@Sun.COM ba .+16; \ 16011172SHaik.Aftandilian@Sun.COM rd STICK, out; \ 16111172SHaik.Aftandilian@Sun.COM sethi %hi(native_tick_offset), scr1; \ 16211172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_tick_offset)], scr1; \ 16311172SHaik.Aftandilian@Sun.COM sllx out, 1, out; \ 16411172SHaik.Aftandilian@Sun.COM srlx out, 1, out; \ 16511172SHaik.Aftandilian@Sun.COM add out, scr1, out; 16611172SHaik.Aftandilian@Sun.COM 16711172SHaik.Aftandilian@Sun.COM #define RD_TICKCMPR(out, scr1, scr2, label) \ 16811172SHaik.Aftandilian@Sun.COM .rd_stickcmpr.label: \ 16911172SHaik.Aftandilian@Sun.COM sethi %hi(native_stick_offset), scr1; \ 17011172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr2; \ 17111172SHaik.Aftandilian@Sun.COM rd STICK_COMPARE, out; \ 17211172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr1; \ 17311172SHaik.Aftandilian@Sun.COM sub scr1, scr2, scr2; \ 17411172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 17511172SHaik.Aftandilian@Sun.COM brnz,pn scr2, .rd_stickcmpr.label; \ 17611172SHaik.Aftandilian@Sun.COM add out, scr1, out 17711172SHaik.Aftandilian@Sun.COM 17811172SHaik.Aftandilian@Sun.COM #define WR_TICKCMPR(in, scr1, scr2, label) \ 17911172SHaik.Aftandilian@Sun.COM sethi %hi(native_stick_offset), scr1; \ 18011172SHaik.Aftandilian@Sun.COM ldx [scr1 + %lo(native_stick_offset)], scr1; \ 18111172SHaik.Aftandilian@Sun.COM sub in, scr1, scr1; \ 18211172SHaik.Aftandilian@Sun.COM wr scr1, STICK_COMPARE 18311172SHaik.Aftandilian@Sun.COM 18411172SHaik.Aftandilian@Sun.COM #define GET_NATIVE_TIME(out, scr1, scr2, label) \ 18511172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 18611172SHaik.Aftandilian@Sun.COM RD_STICK(out,scr1,scr2,label) 18711172SHaik.Aftandilian@Sun.COM 18811172SHaik.Aftandilian@Sun.COM /* 1890Sstevel@tonic-gate * Sun4v processors come up with NPT cleared and there is no need to 1900Sstevel@tonic-gate * clear it again. Also, clearing of the NPT cannot be done atomically 1910Sstevel@tonic-gate * on a CMT processor. 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate #define CLEARTICKNPT 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate #if defined(CPU_MODULE) 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* 1980Sstevel@tonic-gate * Constants used to convert hi-res timestamps into nanoseconds 1990Sstevel@tonic-gate * (see <sys/clock.h> file for more information) 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * At least 62.5 MHz, for faster %tick-based systems. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate #define NSEC_SHIFT 4 20611172SHaik.Aftandilian@Sun.COM 20711172SHaik.Aftandilian@Sun.COM /* 20811172SHaik.Aftandilian@Sun.COM * NOTE: the macros below assume that the various time-related variables 20911172SHaik.Aftandilian@Sun.COM * (hrestime, hrestime_adj, hres_last_tick, timedelta, nsec_scale, etc) 21011172SHaik.Aftandilian@Sun.COM * are all stored together on a 64-byte boundary. The primary motivation 21111172SHaik.Aftandilian@Sun.COM * is cache performance, but we also take advantage of a convenient side 21211172SHaik.Aftandilian@Sun.COM * effect: these variables all have the same high 22 address bits, so only 21311172SHaik.Aftandilian@Sun.COM * one sethi is needed to access them all. 21411172SHaik.Aftandilian@Sun.COM */ 21511172SHaik.Aftandilian@Sun.COM 21611172SHaik.Aftandilian@Sun.COM /* 21711172SHaik.Aftandilian@Sun.COM * GET_HRESTIME() returns the value of hrestime, hrestime_adj and the 21811172SHaik.Aftandilian@Sun.COM * number of nanoseconds since the last clock tick ('nslt'). It also 21911172SHaik.Aftandilian@Sun.COM * sets 'nano' to the value NANOSEC (one billion). 22011172SHaik.Aftandilian@Sun.COM * 22111172SHaik.Aftandilian@Sun.COM * This macro assumes that all registers are globals or outs so they can 22211172SHaik.Aftandilian@Sun.COM * safely contain 64-bit data, and that it's safe to use the label "5:". 22311172SHaik.Aftandilian@Sun.COM * Further, this macro calls the NATIVE_TIME_TO_NSEC_SCALE which in turn 22411172SHaik.Aftandilian@Sun.COM * uses the labels "6:" and "7:"; labels "5:", "6:" and "7:" must not 22511172SHaik.Aftandilian@Sun.COM * be used across invocations of this macro. 22611172SHaik.Aftandilian@Sun.COM */ 22711172SHaik.Aftandilian@Sun.COM #define GET_HRESTIME(hrestsec, hrestnsec, adj, nslt, nano, scr, hrlock, \ 22811172SHaik.Aftandilian@Sun.COM gnt1, gnt2, label) \ 22911172SHaik.Aftandilian@Sun.COM 5: sethi %hi(hres_lock), scr; \ 23011172SHaik.Aftandilian@Sun.COM lduw [scr + %lo(hres_lock)], hrlock; /* load clock lock */ \ 23111172SHaik.Aftandilian@Sun.COM lduw [scr + %lo(nsec_scale)], nano; /* tick-to-ns factor */ \ 23211172SHaik.Aftandilian@Sun.COM andn hrlock, 1, hrlock; /* see comments above! */ \ 23311172SHaik.Aftandilian@Sun.COM ldx [scr + %lo(hres_last_tick)], nslt; \ 23411172SHaik.Aftandilian@Sun.COM ldn [scr + %lo(hrestime)], hrestsec; /* load hrestime.sec */\ 23511172SHaik.Aftandilian@Sun.COM add scr, %lo(hrestime), hrestnsec; \ 23611172SHaik.Aftandilian@Sun.COM ldn [hrestnsec + CLONGSIZE], hrestnsec; \ 23711172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 23811172SHaik.Aftandilian@Sun.COM GET_NATIVE_TIME(adj,gnt1,gnt2,label); /* get current %stick */ \ 23911172SHaik.Aftandilian@Sun.COM subcc adj, nslt, nslt; /* nslt = ticks since last clockint */ \ 24011172SHaik.Aftandilian@Sun.COM movneg %xcc, %g0, nslt; /* ignore neg delta from tick skew */ \ 24111172SHaik.Aftandilian@Sun.COM ldx [scr + %lo(hrestime_adj)], adj; /* load hrestime_adj */ \ 24211172SHaik.Aftandilian@Sun.COM /* membar #LoadLoad; (see comment (2) above) */ \ 24311172SHaik.Aftandilian@Sun.COM lduw [scr + %lo(hres_lock)], scr; /* load clock lock */ \ 24411172SHaik.Aftandilian@Sun.COM NATIVE_TIME_TO_NSEC_SCALE(nslt, nano, gnt1, NSEC_SHIFT); \ 24511172SHaik.Aftandilian@Sun.COM sethi %hi(NANOSEC), nano; \ 24611172SHaik.Aftandilian@Sun.COM xor hrlock, scr, scr; \ 24711172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 24811172SHaik.Aftandilian@Sun.COM brnz,pn scr, 5b; \ 24911172SHaik.Aftandilian@Sun.COM or nano, %lo(NANOSEC), nano; 25011172SHaik.Aftandilian@Sun.COM 25111172SHaik.Aftandilian@Sun.COM /* 25211172SHaik.Aftandilian@Sun.COM * Similar to above, but returns current gethrtime() value in 'base'. 25311172SHaik.Aftandilian@Sun.COM */ 25411172SHaik.Aftandilian@Sun.COM #define GET_HRTIME(base, now, nslt, scale, scr, hrlock, gnt1, gnt2, label) \ 25511172SHaik.Aftandilian@Sun.COM 5: sethi %hi(hres_lock), scr; \ 25611172SHaik.Aftandilian@Sun.COM lduw [scr + %lo(hres_lock)], hrlock; /* load clock lock */ \ 25711172SHaik.Aftandilian@Sun.COM lduw [scr + %lo(nsec_scale)], scale; /* tick-to-ns factor */ \ 25811172SHaik.Aftandilian@Sun.COM andn hrlock, 1, hrlock; /* see comments above! */ \ 25911172SHaik.Aftandilian@Sun.COM ldx [scr + %lo(hres_last_tick)], nslt; \ 26011172SHaik.Aftandilian@Sun.COM ldx [scr + %lo(hrtime_base)], base; /* load hrtime_base */ \ 26111172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 26211172SHaik.Aftandilian@Sun.COM GET_NATIVE_TIME(now,gnt1,gnt2,label); /* get current %stick */ \ 26311172SHaik.Aftandilian@Sun.COM subcc now, nslt, nslt; /* nslt = ticks since last clockint */ \ 26411172SHaik.Aftandilian@Sun.COM movneg %xcc, %g0, nslt; /* ignore neg delta from tick skew */ \ 26511172SHaik.Aftandilian@Sun.COM /* membar #LoadLoad; (see comment (2) above) */ \ 26611172SHaik.Aftandilian@Sun.COM ld [scr + %lo(hres_lock)], scr; /* load clock lock */ \ 26711172SHaik.Aftandilian@Sun.COM NATIVE_TIME_TO_NSEC_SCALE(nslt, scale, gnt1, NSEC_SHIFT); \ 26811172SHaik.Aftandilian@Sun.COM xor hrlock, scr, scr; \ 26911172SHaik.Aftandilian@Sun.COM /* CSTYLED */ \ 27011172SHaik.Aftandilian@Sun.COM brnz,pn scr, 5b; \ 27111172SHaik.Aftandilian@Sun.COM add base, nslt, base; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate #endif /* CPU_MODULE */ 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate #ifdef __cplusplus 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate #endif 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate #endif /* !_SYS_MACHCLOCK_H */ 280