1 /******************************************************************************
3 * Copyright (c) 2019 Intel.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
20 * @brief This file provides implementation to Timing for XRAN.
23 * @ingroup group_lte_source_xran
24 * @author Intel Corporation
33 #include "xran_timer.h"
34 #include "xran_printf.h"
35 #include "xran_mlog_lnx.h"
36 #include "xran_lib_mlog_tasks_id.h"
38 #include "xran_fh_o_du.h"
39 #include "xran_common.h"
41 #define NSEC_PER_SEC 1000000000L
42 #define NSEC_PER_USEC 1000L
43 #define THRESHOLD 35 /**< the avg cost of clock_gettime() in ns */
44 #define TIMECOMPENSATION 2 /**< time compensation in us, avg latency of clock_nanosleep */
46 #define SEC_MOD_STOP (60)
48 static struct timespec started_time;
49 static struct timespec last_time;
50 static struct timespec cur_time;
52 static uint64_t curr_tick;
53 static uint64_t last_tick;
55 static struct timespec* p_cur_time = &cur_time;
56 static struct timespec* p_last_time = &last_time;
59 static struct timespec* p_temp_time;
61 static struct timespec sleeptime = {.tv_nsec = 1E3 }; /* 1 us */
63 static unsigned long current_second = 0;
64 static unsigned long started_second = 0;
65 static uint8_t numerlogy = 0;
66 extern uint32_t xran_lib_ota_sym;
67 extern uint32_t xran_lib_ota_tti;
68 extern uint32_t xran_lib_ota_sym_idx;
70 static int debugStop = 0;
71 static int debugStopCount = 0;
73 static long fine_tuning[5][2] =
75 {71428L, 71429L}, /* mu = 0 */
76 {35714L, 35715L}, /* mu = 1 */
77 {0, 0}, /* mu = 2 not supported */
78 {8928L, 8929L}, /* mu = 3 */
79 {0,0 } /* mu = 4 not supported */
82 static uint8_t slots_per_subframe[4] =
90 uint64_t timing_get_current_second(void)
92 return current_second;
95 int timing_set_numerology(uint8_t value)
101 int timing_set_debug_stop(int value, int count)
104 debugStopCount = count;
107 clock_gettime(CLOCK_REALTIME, &started_time);
108 started_second =started_time.tv_sec;
113 int timing_get_debug_stop(void)
118 void timing_adjust_gps_second(struct timespec* p_time)
120 struct xran_device_ctx * p_xran_dev_ctx = xran_dev_get_ctx();
122 if (p_time->tv_nsec >= p_xran_dev_ctx->offset_nsec)
124 p_time->tv_nsec -= p_xran_dev_ctx->offset_nsec;
125 p_time->tv_sec -= p_xran_dev_ctx->offset_sec;
129 p_time->tv_nsec += 1e9 - p_xran_dev_ctx->offset_nsec;
130 p_time->tv_sec -= p_xran_dev_ctx->offset_sec + 1;
135 uint64_t xran_tick(void)
138 __asm volatile ("rdtsc" : "=a"(lo), "=d"(hi));
139 return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
142 unsigned long get_ticks_diff(unsigned long curr_tick, unsigned long last_tick)
144 if (curr_tick >= last_tick)
145 return (unsigned long)(curr_tick - last_tick);
147 return (unsigned long)(0xFFFFFFFFFFFFFFFF - last_tick + curr_tick);
150 long poll_next_tick(long interval_ns, unsigned long *used_tick)
152 struct xran_device_ctx * p_xran_dev_ctx = xran_dev_get_ctx();
153 struct xran_common_counters* pCnt = &p_xran_dev_ctx->fh_counters;
157 static int counter = 0;
158 static long sym_acc = 0;
159 static long sym_cnt = 0;
162 clock_gettime(CLOCK_REALTIME, p_last_time);
163 last_tick = MLogTick();
164 if(unlikely(p_xran_dev_ctx->offset_sec || p_xran_dev_ctx->offset_nsec))
165 timing_adjust_gps_second(p_last_time);
166 current_second = p_last_time->tv_sec;
170 target_time = (p_last_time->tv_sec * NSEC_PER_SEC + p_last_time->tv_nsec + interval_ns);
173 clock_gettime(CLOCK_REALTIME, p_cur_time);
174 curr_tick = MLogTick();
175 if(unlikely(p_xran_dev_ctx->offset_sec || p_xran_dev_ctx->offset_nsec))
176 timing_adjust_gps_second(p_cur_time);
177 delta = (p_cur_time->tv_sec * NSEC_PER_SEC + p_cur_time->tv_nsec) - target_time;
178 if(delta > 0 || (delta < 0 && abs(delta) < THRESHOLD)) {
179 if (debugStop &&(debugStopCount > 0) && (pCnt->tx_counter >= debugStopCount)){
181 printf("STOP:[%ld.%09ld], debugStopCount %d, tx_counter %ld\n", p_cur_time->tv_sec, p_cur_time->tv_nsec, debugStopCount, pCnt->tx_counter);
184 MLogTask(PID_TIME_SYSTIME_STOP, t1, MLogTick());
185 xran_if_current_state = XRAN_STOPPED;
187 if(current_second != p_cur_time->tv_sec){
188 current_second = p_cur_time->tv_sec;
189 xran_updateSfnSecStart();
190 xran_lib_ota_sym_idx = 0;
191 xran_lib_ota_tti = 0;
192 xran_lib_ota_sym = 0;
195 print_dbg("ToS:C Sync timestamp: [%ld.%09ld]\n", p_cur_time->tv_sec, p_cur_time->tv_nsec);
197 if(p_cur_time->tv_sec > started_second && ((p_cur_time->tv_sec % SEC_MOD_STOP) == 0)){
199 printf("STOP:[%ld.%09ld]\n", p_cur_time->tv_sec, p_cur_time->tv_nsec);
202 MLogTask(PID_TIME_SYSTIME_STOP, t1, MLogTick());
203 xran_if_current_state = XRAN_STOPPED;
206 p_cur_time->tv_nsec = 0; // adjust to 1pps
208 xran_lib_ota_sym_idx = XranIncrementSymIdx(xran_lib_ota_sym_idx, XRAN_NUM_OF_SYMBOL_PER_SLOT*slots_per_subframe[numerlogy]);
209 /* adjust to sym boundary */
211 sym_acc += fine_tuning[numerlogy][0];
213 sym_acc += fine_tuning[numerlogy][1];
214 /* fine tune to second boundary */
215 if(sym_cnt % 13 == 0)
218 p_cur_time->tv_nsec = sym_acc;
223 if(debugStop && delta < interval_ns*10)
224 MLogTask(PID_TIME_SYSTIME_POLL, (p_last_time->tv_sec * NSEC_PER_SEC + p_last_time->tv_nsec), (p_cur_time->tv_sec * NSEC_PER_SEC + p_cur_time->tv_nsec));
226 MLogTask(PID_TIME_SYSTIME_POLL, last_tick, curr_tick);
227 last_tick = curr_tick;
231 p_temp_time = p_last_time;
232 p_last_time = p_cur_time;
233 p_cur_time = p_temp_time;
236 if( likely(xran_if_current_state == XRAN_RUNNING)){
240 if(p_xran_dev_ctx->fh_init.io_cfg.pkt_proc_core == 0)
241 ring_processing_func();
245 /* work around for some kernel */
246 if(p_xran_dev_ctx->fh_init.io_cfg.io_sleep)
247 nanosleep(&sleeptime,NULL);
250 *used_tick += get_ticks_diff(t2, t1);
259 long sleep_next_tick(long interval)
261 struct timespec start_time;
262 struct timespec cur_time;
263 //struct timespec target_time_convert;
264 struct timespec sleep_target_time_convert;
266 long sleep_target_time;
269 clock_gettime(CLOCK_REALTIME, &start_time);
270 target_time = (start_time.tv_sec * NSEC_PER_SEC + start_time.tv_nsec + interval * NSEC_PER_USEC) / (interval * NSEC_PER_USEC) * interval;
271 //printf("target: %ld, current: %ld, %ld\n", target_time, start_time.tv_sec, start_time.tv_nsec);
272 sleep_target_time = target_time - TIMECOMPENSATION;
273 sleep_target_time_convert.tv_sec = sleep_target_time * NSEC_PER_USEC / NSEC_PER_SEC;
274 sleep_target_time_convert.tv_nsec = (sleep_target_time * NSEC_PER_USEC) % NSEC_PER_SEC;
276 //target_time_convert.tv_sec = target_time * NSEC_PER_USEC / NSEC_PER_SEC;
277 //target_time_convert.tv_nsec = (target_time * NSEC_PER_USEC) % NSEC_PER_SEC;
279 clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &sleep_target_time_convert, NULL);
281 clock_gettime(CLOCK_REALTIME, &cur_time);
283 delta = (cur_time.tv_sec * NSEC_PER_SEC + cur_time.tv_nsec) - target_time * NSEC_PER_USEC;