cpu_counter.h revision 1.6.30.1 1 1.6.30.1 uebayasi /* $NetBSD: cpu_counter.h,v 1.6.30.1 2010/04/30 14:39:43 uebayasi 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.6.30.1 uebayasi #ifdef PPC_OEA601
40 1.6.30.1 uebayasi #include <powerpc/oea/spr.h>
41 1.6.30.1 uebayasi #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.6 garbled #endif /* PPC_OEA601 */
67 1.2 hannken #ifdef PPC_IBM403
68 1.2 hannken "mftblo %1 \n"
69 1.2 hannken #else
70 1.1 nakayama "mftb %1 \n"
71 1.2 hannken #endif
72 1.1 nakayama "2: \n"
73 1.6.30.1 uebayasi : "=r"(scratch), "=r"(rv), "=r"(rtcu)
74 1.6.30.1 uebayasi #ifdef PPC_OEA601
75 1.6.30.1 uebayasi : "n"(MPC601), "n"(SPR_RTCU_R), "n"(SPR_RTCL_R)
76 1.6.30.1 uebayasi #endif
77 1.6.30.1 uebayasi );
78 1.1 nakayama
79 1.1 nakayama return rv;
80 1.1 nakayama }
81 1.1 nakayama
82 1.5 garbled extern uint32_t ticks_per_sec;
83 1.5 garbled
84 1.4 perry static __inline uint64_t
85 1.1 nakayama cpu_frequency(struct cpu_info *ci)
86 1.1 nakayama {
87 1.5 garbled /* XXX this probably only works on 603 and newer */
88 1.5 garbled return ticks_per_sec;
89 1.1 nakayama }
90 1.5 garbled
91 1.1 nakayama #endif /* _KERNEL */
92 1.1 nakayama
93 1.1 nakayama #endif /* _POWERPC_CPU_COUNTER_H_ */
94