1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
#ifndef __CYCLETIMERHELPER_H__ |
---|
24 |
#define __CYCLETIMERHELPER_H__ |
---|
25 |
|
---|
26 |
/** |
---|
27 |
* Implements a DLL based mechanism to track the cycle timer |
---|
28 |
* register of the Ieee1394Service pointed to by the 'parent'. |
---|
29 |
* |
---|
30 |
* A DLL mechanism is performance-wise better suited, since it |
---|
31 |
* does not require an OS call. Hence we run a thread to update |
---|
32 |
* the DLL at regular time intervals, and then use the DLL to |
---|
33 |
* generate a cycle timer estimate for the parent to pass on |
---|
34 |
* to it's clients. |
---|
35 |
* |
---|
36 |
* The idea is to make reading the cycle timer real-time safe, |
---|
37 |
* which isn't (necessarily)the case for the direct raw1394 call, |
---|
38 |
* since it's a kernel call that could block (although the current |
---|
39 |
* implementation is RT safe). |
---|
40 |
* |
---|
41 |
* This also allows us to run on systems not having the |
---|
42 |
* raw1394_read_cycle_timer kernel call. We can always do a normal |
---|
43 |
* read of our own cycle timer register, but that's not very accurate. |
---|
44 |
* The accuracy is improved by this DLL mechanism. Still not as good |
---|
45 |
* as when using the raw1394_read_cycle_timer call, but anyway. |
---|
46 |
* |
---|
47 |
* On the long run this code will also allow us to map system time |
---|
48 |
* on to 1394 time for the current host controller, hence enabling |
---|
49 |
* different clock domains to operate together. |
---|
50 |
*/ |
---|
51 |
|
---|
52 |
#include "libutil/Thread.h" |
---|
53 |
#include "libutil/SystemTimeSource.h" |
---|
54 |
#include "cycletimer.h" |
---|
55 |
|
---|
56 |
#include "libutil/Functors.h" |
---|
57 |
#include "libutil/Mutex.h" |
---|
58 |
|
---|
59 |
#include "debugmodule/debugmodule.h" |
---|
60 |
|
---|
61 |
class Ieee1394Service; |
---|
62 |
|
---|
63 |
class CycleTimerHelper : public Util::RunnableInterface |
---|
64 |
{ |
---|
65 |
public: |
---|
66 |
CycleTimerHelper(Ieee1394Service &, unsigned int); |
---|
67 |
CycleTimerHelper(Ieee1394Service &, unsigned int, bool rt, int prio); |
---|
68 |
~CycleTimerHelper(); |
---|
69 |
|
---|
70 |
virtual bool Init(); |
---|
71 |
virtual bool Execute(); |
---|
72 |
|
---|
73 |
bool setThreadParameters(bool rt, int priority); |
---|
74 |
bool Start(); |
---|
75 |
|
---|
76 |
/** |
---|
77 |
* @brief get the current cycle timer value (in ticks) |
---|
78 |
* @note thread safe |
---|
79 |
*/ |
---|
80 |
uint32_t getCycleTimerTicks(); |
---|
81 |
|
---|
82 |
/** |
---|
83 |
* @brief get the cycle timer value for a specific time instant (in ticks) |
---|
84 |
* @note thread safe |
---|
85 |
*/ |
---|
86 |
uint32_t getCycleTimerTicks(uint64_t now); |
---|
87 |
|
---|
88 |
/** |
---|
89 |
* @brief get the current cycle timer value (in CTR format) |
---|
90 |
* @note thread safe |
---|
91 |
*/ |
---|
92 |
uint32_t getCycleTimer(); |
---|
93 |
|
---|
94 |
/** |
---|
95 |
* @brief get the cycle timer value for a specific time instant (in CTR format) |
---|
96 |
* @note thread safe |
---|
97 |
*/ |
---|
98 |
uint32_t getCycleTimer(uint64_t now); |
---|
99 |
|
---|
100 |
/** |
---|
101 |
* @brief get the system time for a specific cycle timer value (in ticks) |
---|
102 |
* @note thread safe |
---|
103 |
*/ |
---|
104 |
uint64_t getSystemTimeForCycleTimerTicks(uint32_t ticks); |
---|
105 |
|
---|
106 |
/** |
---|
107 |
* @brief get the system time for a specific cycle timer value (in CTR format) |
---|
108 |
* @note thread safe |
---|
109 |
*/ |
---|
110 |
uint64_t getSystemTimeForCycleTimer(uint32_t ctr); |
---|
111 |
|
---|
112 |
float getRate(); |
---|
113 |
float getNominalRate(); |
---|
114 |
|
---|
115 |
/** |
---|
116 |
* @brief handle a bus reset |
---|
117 |
*/ |
---|
118 |
void busresetHandler(); |
---|
119 |
|
---|
120 |
void setVerboseLevel(int l); |
---|
121 |
|
---|
122 |
private: |
---|
123 |
bool readCycleTimerWithRetry(uint32_t *cycle_timer, uint64_t *local_time, int ntries); |
---|
124 |
bool initValues(); |
---|
125 |
|
---|
126 |
#if IEEE1394SERVICE_USE_CYCLETIMER_DLL |
---|
127 |
bool initDLL(); |
---|
128 |
#endif |
---|
129 |
|
---|
130 |
Ieee1394Service &m_Parent; |
---|
131 |
// parameters |
---|
132 |
uint32_t m_ticks_per_update; |
---|
133 |
uint32_t m_usecs_per_update; |
---|
134 |
|
---|
135 |
float m_avg_wakeup_delay; |
---|
136 |
|
---|
137 |
// state variables |
---|
138 |
double m_dll_e2; |
---|
139 |
|
---|
140 |
double m_current_time_usecs; |
---|
141 |
double m_next_time_usecs; |
---|
142 |
double m_current_time_ticks; |
---|
143 |
double m_next_time_ticks; |
---|
144 |
bool m_first_run; |
---|
145 |
ffado_microsecs_t m_sleep_until; |
---|
146 |
|
---|
147 |
uint32_t m_cycle_timer_prev; |
---|
148 |
uint64_t m_cycle_timer_ticks_prev; |
---|
149 |
|
---|
150 |
double m_dll_coeff_b; |
---|
151 |
double m_dll_coeff_c; |
---|
152 |
|
---|
153 |
// cached vars used for computation |
---|
154 |
struct compute_vars { |
---|
155 |
uint64_t usecs; |
---|
156 |
uint64_t ticks; |
---|
157 |
double rate; |
---|
158 |
}; |
---|
159 |
|
---|
160 |
#define CTRHELPER_NB_SHADOW_VARS 8 |
---|
161 |
struct compute_vars m_shadow_vars[CTRHELPER_NB_SHADOW_VARS]; |
---|
162 |
volatile unsigned int m_current_shadow_idx; |
---|
163 |
|
---|
164 |
// Threading |
---|
165 |
Util::Thread * m_Thread; |
---|
166 |
bool m_realtime; |
---|
167 |
unsigned int m_priority; |
---|
168 |
Util::Mutex* m_update_lock; |
---|
169 |
|
---|
170 |
// busreset handling |
---|
171 |
Util::Functor* m_busreset_functor; |
---|
172 |
bool m_unhandled_busreset; |
---|
173 |
|
---|
174 |
#ifdef DEBUG |
---|
175 |
uint64_t m_last_loop_entry; |
---|
176 |
int m_successive_short_loops; |
---|
177 |
#endif |
---|
178 |
|
---|
179 |
// debug stuff |
---|
180 |
DECLARE_DEBUG_MODULE; |
---|
181 |
}; |
---|
182 |
#endif |
---|