Home | History | Annotate | Line # | Download | only in include
cpu_counter.h revision 1.8
      1  1.8      matt /*	$NetBSD: cpu_counter.h,v 1.8 2011/01/18 01:02:54 matt Exp $	*/
      2  1.1  nakayama 
      3  1.1  nakayama /*-
      4  1.1  nakayama  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  1.1  nakayama  *
      6  1.1  nakayama  * Redistribution and use in source and binary forms, with or without
      7  1.1  nakayama  * modification, are permitted provided that the following conditions
      8  1.1  nakayama  * are met:
      9  1.1  nakayama  * 1. Redistributions of source code must retain the above copyright
     10  1.1  nakayama  *    notice, this list of conditions and the following disclaimer.
     11  1.1  nakayama  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  nakayama  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  nakayama  *    documentation and/or other materials provided with the distribution.
     14  1.1  nakayama  * 3. The name of the author may not be used to endorse or promote products
     15  1.1  nakayama  *    derived from this software without specific prior written permission.
     16  1.1  nakayama  *
     17  1.1  nakayama  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  nakayama  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  nakayama  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  nakayama  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  nakayama  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  nakayama  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  nakayama  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  nakayama  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  nakayama  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1  nakayama  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  nakayama  */
     28  1.1  nakayama 
     29  1.1  nakayama #ifndef _POWERPC_CPU_COUNTER_H_
     30  1.1  nakayama #define _POWERPC_CPU_COUNTER_H_
     31  1.1  nakayama 
     32  1.1  nakayama /*
     33  1.1  nakayama  * Machine-specific support for CPU counter.
     34  1.1  nakayama  */
     35  1.1  nakayama 
     36  1.1  nakayama #ifdef _KERNEL
     37  1.1  nakayama 
     38  1.1  nakayama #include <powerpc/spr.h>
     39  1.7      matt #ifdef PPC_OEA601
     40  1.7      matt #include <powerpc/oea/spr.h>
     41  1.7      matt #endif
     42  1.1  nakayama 
     43  1.1  nakayama #define cpu_hascounter()	(1)
     44  1.1  nakayama #define cpu_counter()		cpu_counter32()
     45  1.1  nakayama 
     46  1.4     perry static __inline uint32_t
     47  1.1  nakayama cpu_counter32(void)
     48  1.1  nakayama {
     49  1.1  nakayama 	uint32_t rv, rtcu, scratch;
     50  1.1  nakayama 
     51  1.3     perry 	__asm volatile (
     52  1.6   garbled #ifdef PPC_OEA601
     53  1.1  nakayama 	    "mfpvr	%0		\n"
     54  1.1  nakayama 	    "srwi	%0,%0,16	\n"
     55  1.1  nakayama 	    "cmpwi	%0,%3		\n"
     56  1.1  nakayama 	    "bne	1f		\n"	/* branch if not 601 */
     57  1.1  nakayama 	    "lis	%0,0x77		\n"
     58  1.1  nakayama 	    "ori	%0,%0,0x3594	\n"	/* 7.8125e6 */
     59  1.1  nakayama 	    "mfspr	%2,%4		\n"
     60  1.1  nakayama 	    "mullw	%2,%2,%0	\n"
     61  1.1  nakayama 	    "mfspr	%0,%5		\n"
     62  1.1  nakayama 	    "srwi	%0,%0,7		\n"
     63  1.1  nakayama 	    "add	%1,%2,%0	\n"
     64  1.1  nakayama 	    "b		2f		\n"
     65  1.1  nakayama 	    "1:				\n"
     66  1.8      matt #elif defined(PPC_IBM403)
     67  1.2   hannken 	    "mftblo	%1		\n"
     68  1.8      matt #elif defined(PPC_BOOKE)
     69  1.8      matt 	    "mfspr	%1,%3		\n"
     70  1.2   hannken #else
     71  1.1  nakayama 	    "mftb	%1		\n"
     72  1.2   hannken #endif
     73  1.1  nakayama 	    "2:				\n"
     74  1.7      matt 		: "=r"(scratch), "=r"(rv), "=r"(rtcu)
     75  1.7      matt #ifdef PPC_OEA601
     76  1.7      matt 		: "n"(MPC601), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R)
     77  1.8      matt #elif defined(PPC_BOOKE)
     78  1.8      matt 		: "n"(SPR_TBL)
     79  1.7      matt #endif
     80  1.7      matt 	    );
     81  1.1  nakayama 
     82  1.1  nakayama 	return rv;
     83  1.1  nakayama }
     84  1.1  nakayama 
     85  1.5   garbled extern uint32_t ticks_per_sec;
     86  1.5   garbled 
     87  1.4     perry static __inline uint64_t
     88  1.1  nakayama cpu_frequency(struct cpu_info *ci)
     89  1.1  nakayama {
     90  1.5   garbled 	/* XXX this probably only works on 603 and newer */
     91  1.5   garbled 	return ticks_per_sec;
     92  1.1  nakayama }
     93  1.5   garbled 
     94  1.1  nakayama #endif /* _KERNEL */
     95  1.1  nakayama 
     96  1.1  nakayama #endif /* _POWERPC_CPU_COUNTER_H_ */
     97