cpu_counter.h revision 1.8
155682Smarkm/*	$NetBSD: cpu_counter.h,v 1.8 2011/01/18 01:02:54 matt Exp $	*/
2233294Sstas
355682Smarkm/*-
455682Smarkm * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
5233294Sstas *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
9233294Sstas * 1. Redistributions of source code must retain the above copyright
1055682Smarkm *    notice, this list of conditions and the following disclaimer.
1155682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12233294Sstas *    notice, this list of conditions and the following disclaimer in the
1355682Smarkm *    documentation and/or other materials provided with the distribution.
1455682Smarkm * 3. The name of the author may not be used to endorse or promote products
1555682Smarkm *    derived from this software without specific prior written permission.
16233294Sstas *
1755682Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1855682Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1955682Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20233294Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2155682Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2255682Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2355682Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2455682Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2555682Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2655682Smarkm * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2755682Smarkm */
2855682Smarkm
2955682Smarkm#ifndef _POWERPC_CPU_COUNTER_H_
3055682Smarkm#define _POWERPC_CPU_COUNTER_H_
3155682Smarkm
3255682Smarkm/*
3355682Smarkm * Machine-specific support for CPU counter.
3455682Smarkm */
3555682Smarkm
36233294Sstas#ifdef _KERNEL
3755682Smarkm
3855682Smarkm#include <powerpc/spr.h>
3955682Smarkm#ifdef PPC_OEA601
4055682Smarkm#include <powerpc/oea/spr.h>
4155682Smarkm#endif
4255682Smarkm
4355682Smarkm#define cpu_hascounter()	(1)
4455682Smarkm#define cpu_counter()		cpu_counter32()
4555682Smarkm
4655682Smarkmstatic __inline uint32_t
4755682Smarkmcpu_counter32(void)
4855682Smarkm{
4955682Smarkm	uint32_t rv, rtcu, scratch;
5055682Smarkm
5155682Smarkm	__asm volatile (
5255682Smarkm#ifdef PPC_OEA601
5355682Smarkm	    "mfpvr	%0		\n"
5455682Smarkm	    "srwi	%0,%0,16	\n"
5555682Smarkm	    "cmpwi	%0,%3		\n"
5655682Smarkm	    "bne	1f		\n"	/* branch if not 601 */
5755682Smarkm	    "lis	%0,0x77		\n"
5855682Smarkm	    "ori	%0,%0,0x3594	\n"	/* 7.8125e6 */
5955682Smarkm	    "mfspr	%2,%4		\n"
6055682Smarkm	    "mullw	%2,%2,%0	\n"
6155682Smarkm	    "mfspr	%0,%5		\n"
6255682Smarkm	    "srwi	%0,%0,7		\n"
6355682Smarkm	    "add	%1,%2,%0	\n"
6455682Smarkm	    "b		2f		\n"
6555682Smarkm	    "1:				\n"
6655682Smarkm#elif defined(PPC_IBM403)
6755682Smarkm	    "mftblo	%1		\n"
6855682Smarkm#elif defined(PPC_BOOKE)
6955682Smarkm	    "mfspr	%1,%3		\n"
7055682Smarkm#else
7155682Smarkm	    "mftb	%1		\n"
7255682Smarkm#endif
7355682Smarkm	    "2:				\n"
7455682Smarkm		: "=r"(scratch), "=r"(rv), "=r"(rtcu)
7555682Smarkm#ifdef PPC_OEA601
7655682Smarkm		: "n"(MPC601), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R)
7755682Smarkm#elif defined(PPC_BOOKE)
7855682Smarkm		: "n"(SPR_TBL)
7955682Smarkm#endif
8055682Smarkm	    );
8155682Smarkm
8255682Smarkm	return rv;
8355682Smarkm}
8455682Smarkm
8555682Smarkmextern uint32_t ticks_per_sec;
8655682Smarkm
8755682Smarkmstatic __inline uint64_t
8855682Smarkmcpu_frequency(struct cpu_info *ci)
8955682Smarkm{
9055682Smarkm	/* XXX this probably only works on 603 and newer */
9155682Smarkm	return ticks_per_sec;
9255682Smarkm}
9355682Smarkm
9455682Smarkm#endif /* _KERNEL */
9555682Smarkm
9655682Smarkm#endif /* _POWERPC_CPU_COUNTER_H_ */
9755682Smarkm