blob: c3f14f3e6a15b81f27bbf53fcd14eaa4943a57d6 [file] [log] [blame]
Uwe Hermannca7f0e42007-09-09 20:02:45 +00001/*
2 * This file is part of the flashrom project.
3 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +00005 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Uwe Hermannca7f0e42007-09-09 20:02:45 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Uwe Hermannca7f0e42007-09-09 20:02:45 +000016 */
17
Patrick Georgia9095a92010-09-30 17:03:32 +000018#ifndef __LIBPAYLOAD__
19
Nico Huber8624e8c2012-11-05 21:46:33 +010020#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000021#include <unistd.h>
Nico Huber8624e8c2012-11-05 21:46:33 +010022#include <errno.h>
Stefan Tauner839db6d2015-11-14 02:55:22 +000023#include <time.h>
Ollie Lhocbbf1252004-03-17 22:22:08 +000024#include <sys/time.h>
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000025#include <stdlib.h>
26#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000027#include "flash.h"
Ollie Lhocbbf1252004-03-17 22:22:08 +000028
Nico Huber8624e8c2012-11-05 21:46:33 +010029static bool use_clock_gettime = false;
30
31#if HAVE_CLOCK_GETTIME == 1
32
33#ifdef _POSIX_MONOTONIC_CLOCK
34static clockid_t clock_id = CLOCK_MONOTONIC;
35#else
36static clockid_t clock_id = CLOCK_REALTIME;
37#endif
38
39static void clock_usec_delay(int usecs)
40{
41 struct timespec now;
42 clock_gettime(clock_id, &now);
43
44 const long end_nsec = now.tv_nsec + usecs * 1000L;
45 const struct timespec end = {
46 end_nsec / (1000 * 1000 * 1000) + now.tv_sec,
47 end_nsec % (1000 * 1000 * 1000)
48 };
49 do {
50 clock_gettime(clock_id, &now);
51 } while (now.tv_sec < end.tv_sec || (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
52}
53
54static int clock_check_res(void)
55{
56 struct timespec res;
57 if (!clock_getres(clock_id, &res)) {
58 if (res.tv_sec == 0 && res.tv_nsec <= 100) {
59 msg_pinfo("Using clock_gettime for delay loops (clk_id: %d, resolution: %ldns).\n",
60 (int)clock_id, res.tv_nsec);
61 use_clock_gettime = true;
62 return 1;
63 }
64 } else if (clock_id != CLOCK_REALTIME && errno == EINVAL) {
65 /* Try again with CLOCK_REALTIME. */
66 clock_id = CLOCK_REALTIME;
67 return clock_check_res();
68 }
69 return 0;
70}
71#else
72
73static inline void clock_usec_delay(int usecs) {}
74static inline int clock_check_res(void) { return 0; }
75
76#endif /* HAVE_CLOCK_GETTIME == 1 */
77
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +000078/* loops per microsecond */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000079static unsigned long micro = 1;
Ollie Lhocbbf1252004-03-17 22:22:08 +000080
Stefan Taunerf80419c2014-05-02 15:41:42 +000081__attribute__ ((noinline)) void myusec_delay(unsigned int usecs)
Ollie Lhocbbf1252004-03-17 22:22:08 +000082{
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +000083 unsigned long i;
84 for (i = 0; i < usecs * micro; i++) {
85 /* Make sure the compiler doesn't optimize the loop away. */
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000086 __asm__ volatile ("" : : "rm" (i) );
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +000087 }
Ollie Lhocbbf1252004-03-17 22:22:08 +000088}
89
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000090static unsigned long measure_os_delay_resolution(void)
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +000091{
92 unsigned long timeusec;
93 struct timeval start, end;
94 unsigned long counter = 0;
95
Peter Huewe73f8ec82011-01-24 19:15:51 +000096 gettimeofday(&start, NULL);
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +000097 timeusec = 0;
98
99 while (!timeusec && (++counter < 1000000000)) {
Peter Huewe73f8ec82011-01-24 19:15:51 +0000100 gettimeofday(&end, NULL);
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000101 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
102 (end.tv_usec - start.tv_usec);
103 /* Protect against time going forward too much. */
104 if ((end.tv_sec > start.tv_sec) &&
105 ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1))
106 timeusec = 0;
107 /* Protect against time going backwards during leap seconds. */
108 if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX))
109 timeusec = 0;
110 }
111 return timeusec;
112}
113
Stefan Taunerf80419c2014-05-02 15:41:42 +0000114static unsigned long measure_delay(unsigned int usecs)
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000115{
116 unsigned long timeusec;
117 struct timeval start, end;
118
Peter Huewe73f8ec82011-01-24 19:15:51 +0000119 gettimeofday(&start, NULL);
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000120 myusec_delay(usecs);
Peter Huewe73f8ec82011-01-24 19:15:51 +0000121 gettimeofday(&end, NULL);
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000122 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
123 (end.tv_usec - start.tv_usec);
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000124 /* Protect against time going forward too much. */
125 if ((end.tv_sec > start.tv_sec) &&
126 ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1))
127 timeusec = LONG_MAX;
128 /* Protect against time going backwards during leap seconds. */
129 if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX))
130 timeusec = 1;
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000131
132 return timeusec;
133}
134
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000135void myusec_calibrate_delay(void)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000136{
Nico Huber8624e8c2012-11-05 21:46:33 +0100137 if (clock_check_res())
138 return;
139
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000140 unsigned long count = 1000;
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000141 unsigned long timeusec, resolution;
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000142 int i, tries = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000143
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000144 msg_pinfo("Calibrating delay loop... ");
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000145 resolution = measure_os_delay_resolution();
146 if (resolution) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000147 msg_pdbg("OS timer resolution is %lu usecs, ", resolution);
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000148 } else {
149 msg_pinfo("OS timer resolution is unusable. ");
150 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000151
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000152recalibrate:
Urja Rannikkof6404012010-04-09 00:02:38 +0000153 count = 1000;
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000154 while (1) {
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000155 timeusec = measure_delay(count);
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000156 if (timeusec > 1000000 / 4)
157 break;
158 if (count >= ULONG_MAX / 2) {
159 msg_pinfo("timer loop overflow, reduced precision. ");
160 break;
161 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000162 count *= 2;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000163 }
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000164 tries ++;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000165
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000166 /* Avoid division by zero, but in that case the loop is shot anyway. */
167 if (!timeusec)
168 timeusec = 1;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200169
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000170 /* Compute rounded up number of loops per microsecond. */
171 micro = (count * micro) / timeusec + 1;
172 msg_pdbg("%luM loops per second, ", micro);
173
174 /* Did we try to recalibrate less than 5 times? */
175 if (tries < 5) {
176 /* Recheck our timing to make sure we weren't just hitting
177 * a scheduler delay or something similar.
178 */
179 for (i = 0; i < 4; i++) {
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000180 if (resolution && (resolution < 10)) {
181 timeusec = measure_delay(100);
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200182 } else if (resolution &&
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000183 (resolution < ULONG_MAX / 200)) {
184 timeusec = measure_delay(resolution * 10) *
185 100 / (resolution * 10);
186 } else {
187 /* This workaround should be active for broken
188 * OS and maybe libpayload. The criterion
189 * here is horrible or non-measurable OS timer
190 * resolution which will result in
191 * measure_delay(100)=0 whereas a longer delay
192 * (1000 ms) may be sufficient
193 * to get a nonzero time measurement.
194 */
195 timeusec = measure_delay(1000000) / 10000;
196 }
197 if (timeusec < 90) {
198 msg_pdbg("delay more than 10%% too short (got "
199 "%lu%% of expected delay), "
200 "recalculating... ", timeusec);
Carl-Daniel Hailfinger253101e2010-03-31 23:55:06 +0000201 goto recalibrate;
202 }
203 }
204 } else {
205 msg_perr("delay loop is unreliable, trying to continue ");
206 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000207
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000208 /* We're interested in the actual precision. */
209 timeusec = measure_delay(10);
210 msg_pdbg("10 myus = %ld us, ", timeusec);
211 timeusec = measure_delay(100);
212 msg_pdbg("100 myus = %ld us, ", timeusec);
213 timeusec = measure_delay(1000);
214 msg_pdbg("1000 myus = %ld us, ", timeusec);
215 timeusec = measure_delay(10000);
216 msg_pdbg("10000 myus = %ld us, ", timeusec);
Carl-Daniel Hailfingerb929d112010-06-03 21:48:13 +0000217 timeusec = measure_delay(resolution * 4);
218 msg_pdbg("%ld myus = %ld us, ", resolution * 4, timeusec);
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +0000219
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000220 msg_pinfo("OK.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000221}
Carl-Daniel Hailfinger36cc1c82009-12-24 03:11:55 +0000222
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000223/* Not very precise sleep. */
Stefan Taunerf80419c2014-05-02 15:41:42 +0000224void internal_sleep(unsigned int usecs)
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000225{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000226#if IS_WINDOWS
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000227 Sleep((usecs + 999) / 1000);
Stefan Tauner839db6d2015-11-14 02:55:22 +0000228#elif defined(__DJGPP__)
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000229 sleep(usecs / 1000000);
230 usleep(usecs % 1000000);
Stefan Tauner839db6d2015-11-14 02:55:22 +0000231#else
232 nanosleep(&(struct timespec){usecs / 1000000, (usecs * 1000) % 1000000000UL}, NULL);
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000233#endif
234}
235
236/* Precise delay. */
Stefan Taunerf80419c2014-05-02 15:41:42 +0000237void internal_delay(unsigned int usecs)
Carl-Daniel Hailfinger36cc1c82009-12-24 03:11:55 +0000238{
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000239 /* If the delay is >1 s, use internal_sleep because timing does not need to be so precise. */
Carl-Daniel Hailfinger36cc1c82009-12-24 03:11:55 +0000240 if (usecs > 1000000) {
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000241 internal_sleep(usecs);
Nico Huber8624e8c2012-11-05 21:46:33 +0100242 } else if (use_clock_gettime) {
243 clock_usec_delay(usecs);
Carl-Daniel Hailfinger36cc1c82009-12-24 03:11:55 +0000244 } else {
245 myusec_delay(usecs);
246 }
247}
248
Elyes HAOUAS1b365932018-04-25 16:03:51 +0200249#else
Patrick Georgi97bc95c2011-03-08 07:17:44 +0000250#include <libpayload.h>
Patrick Georgia9095a92010-09-30 17:03:32 +0000251
252void myusec_calibrate_delay(void)
253{
254 get_cpu_speed();
255}
256
Stefan Taunerf80419c2014-05-02 15:41:42 +0000257void internal_delay(unsigned int usecs)
Patrick Georgia9095a92010-09-30 17:03:32 +0000258{
259 udelay(usecs);
260}
261#endif