9f6d9b4b5c3ed1837414588a34c7e2a151c2e3b9
[ardour.git] / libs / ardour / ardour / cycles.h
1 /*
2     Copyright (C) 2001 Paul Davis
3     Code derived from various headers from the Linux kernel
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_cycles_h__
22 #define __ardour_cycles_h__
23
24 #include <stdint.h>
25
26 #if defined(__i386__) || defined(__x86_64__)
27
28 /*
29  * Standard way to access the cycle counter on i586+ CPUs.
30  * Currently only used on SMP.
31  *
32  * If you really have a SMP machine with i486 chips or older,
33  * compile for that, and this will just always return zero.
34  * That's ok, it just means that the nicer scheduling heuristics
35  * won't work for you.
36  *
37  * We only use the low 32 bits, and we'd simply better make sure
38  * that we reschedule before that wraps. Scheduling at least every
39  * four billion cycles just basically sounds like a good idea,
40  * regardless of how fast the machine is.
41  */
42 typedef uint64_t cycles_t;
43
44 extern cycles_t cacheflush_time;
45
46 #define rdtscll(val) \
47      __asm__ __volatile__("rdtsc" : "=A" (val))
48
49 static inline cycles_t get_cycles (void)
50 {
51         cycles_t ret;
52
53         rdtscll(ret);
54         return ret & 0xffffffff;
55 }
56
57 #elif defined(__powerpc__)
58
59 #define CPU_FTR_601                     0x00000100
60
61 typedef uint32_t cycles_t;
62
63 /*
64  * For the "cycle" counter we use the timebase lower half.
65  * Currently only used on SMP.
66  */
67
68 extern cycles_t cacheflush_time;
69
70 static inline cycles_t get_cycles(void)
71 {
72         cycles_t ret = 0;
73
74         __asm__ __volatile__(
75                 "98:    mftb %0\n"
76                 "99:\n"
77                 ".section __ftr_fixup,\"a\"\n"
78                 "       .long %1\n"
79                 "       .long 0\n"
80                 "       .long 98b\n"
81                 "       .long 99b\n"
82                 ".previous"
83                 : "=r" (ret) : "i" (CPU_FTR_601));
84         return ret;
85 }
86
87 #elif defined(__ia64__)
88 /* ia64 */
89
90 typedef uint32_t cycles_t;
91 static inline cycles_t
92 get_cycles (void)
93 {
94         cycles_t ret;
95         __asm__ __volatile__ ("mov %0=ar.itc" : "=r"(ret));
96         return ret;
97 }
98
99 #elif defined(__alpha__)
100 /* alpha */
101
102 /*
103  * Standard way to access the cycle counter.
104  * Currently only used on SMP for scheduling.
105  *
106  * Only the low 32 bits are available as a continuously counting entity.
107  * But this only means we'll force a reschedule every 8 seconds or so,
108  * which isn't an evil thing.
109  */
110
111 typedef uint32_t cycles_t;
112 static inline cycles_t get_cycles (void)
113 {
114         cycles_t ret;
115         __asm__ __volatile__ ("rpcc %0" : "=r"(ret));
116         return ret;
117 }
118
119 #elif defined(__s390__)
120 /* s390 */
121
122 typedef uint32_t long cycles_t;
123 static inline cycles_t get_cycles(void)
124 {
125         cycles_t cycles;
126         __asm__("stck 0(%0)" : : "a" (&(cycles)) : "memory", "cc");
127         return cycles >> 2;
128 }
129
130 #elif defined(__hppa__)
131 /* hppa/parisc */
132
133 #define mfctl(reg)      ({              \
134         uint32_t cr;               \
135         __asm__ __volatile__(           \
136                 "mfctl " #reg ",%0" :   \
137                  "=r" (cr)              \
138         );                              \
139         cr;                             \
140 })
141
142 typedef uint32_t cycles_t;
143 static inline cycles_t get_cycles (void)
144 {
145         return mfctl(16);
146 }
147
148 #elif defined(__mips__)
149 /* mips/mipsel */
150
151 /*
152  * Standard way to access the cycle counter.
153  * Currently only used on SMP for scheduling.
154  *
155  * Only the low 32 bits are available as a continuously counting entity.
156  * But this only means we'll force a reschedule every 8 seconds or so,
157  * which isn't an evil thing.
158  *
159  * We know that all SMP capable CPUs have cycle counters.
160  */
161
162 #define __read_32bit_c0_register(source, sel)                           \
163 ({ int __res;                                                           \
164         if (sel == 0)                                                   \
165                 __asm__ __volatile__(                                   \
166                         "mfc0\t%0, " #source "\n\t"                     \
167                         : "=r" (__res));                                \
168         else                                                            \
169                 __asm__ __volatile__(                                   \
170                         ".set\tmips32\n\t"                              \
171                         "mfc0\t%0, " #source ", " #sel "\n\t"           \
172                         ".set\tmips0\n\t"                               \
173                         : "=r" (__res));                                \
174         __res;                                                          \
175 })
176
177 /* #define CP0_COUNT $9 */
178 #define read_c0_count()         __read_32bit_c0_register($9, 0)
179
180 typedef uint32_t cycles_t;
181 static inline cycles_t get_cycles (void)
182 {
183         return read_c0_count();
184 }
185
186 /* begin mach */
187 #elif defined(__APPLE__)
188
189 #include <CoreAudio/HostTime.h>
190
191 typedef UInt64 cycles_t;
192 static inline cycles_t get_cycles (void)
193 {
194        UInt64 time = AudioGetCurrentHostTime();
195        return AudioConvertHostTimeToNanos(time);
196 }
197 /* end mach  */
198
199 #else
200
201 /* debian: sparc, arm, m68k */
202
203 #warning You are compiling libardour on a platform for which ardour/cycles.h needs work
204
205 #include <sys/time.h>
206
207 typedef long cycles_t;
208
209 extern cycles_t cacheflush_time;
210
211 static inline cycles_t get_cycles(void)
212 {
213        struct timeval tv;
214        gettimeofday (&tv, NULL);
215
216        return tv.tv_usec;
217 }
218
219 #endif
220
221 #endif /* __ardour_cycles_h__ */