blob: a61cbf73a179dec6a4bf55595c3d5906057c3c38 [file] [log] [blame]
Alex Dewar97870c32019-08-25 10:49:18 +01001// SPDX-License-Identifier: GPL-2.0
Gennady Sharapovcff65c42006-01-18 17:42:42 -08002/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00003 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5 * Copyright (C) 2012-2014 Cisco Systems
Jeff Dike4c9e1382007-10-16 01:26:54 -07006 * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
Gennady Sharapovcff65c42006-01-18 17:42:42 -08007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Jeff Dike4c9e1382007-10-16 01:26:54 -07009#include <stddef.h>
Johannes Berg49da38a2020-12-02 20:58:04 +010010#include <unistd.h>
Jeff Dike4c9e1382007-10-16 01:26:54 -070011#include <errno.h>
12#include <signal.h>
Gennady Sharapovcff65c42006-01-18 17:42:42 -080013#include <time.h>
14#include <sys/time.h>
Al Viro37185b32012-10-08 03:27:32 +010015#include <kern_util.h>
16#include <os.h>
Anton Ivanov2eb5f312015-11-02 16:16:37 +000017#include <string.h>
Gennady Sharapovcff65c42006-01-18 17:42:42 -080018
Anton Ivanov2eb5f312015-11-02 16:16:37 +000019static timer_t event_high_res_timer = 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080020
Jeff Dike5f734612007-10-16 01:27:27 -070021static inline long long timeval_to_ns(const struct timeval *tv)
22{
23 return ((long long) tv->tv_sec * UM_NSEC_PER_SEC) +
24 tv->tv_usec * UM_NSEC_PER_USEC;
25}
26
Anton Ivanov2eb5f312015-11-02 16:16:37 +000027static inline long long timespec_to_ns(const struct timespec *ts)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080028{
Johannes Berg56fc1872019-05-06 14:39:38 +020029 return ((long long) ts->tv_sec * UM_NSEC_PER_SEC) + ts->tv_nsec;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000030}
Jeff Dike4c9e1382007-10-16 01:26:54 -070031
Johannes Berg56fc1872019-05-06 14:39:38 +020032long long os_persistent_clock_emulation(void)
33{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000034 struct timespec realtime_tp;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080035
Anton Ivanov2eb5f312015-11-02 16:16:37 +000036 clock_gettime(CLOCK_REALTIME, &realtime_tp);
37 return timespec_to_ns(&realtime_tp);
38}
Jeff Dikefe2cc532008-05-12 14:02:00 -070039
Anton Ivanov2eb5f312015-11-02 16:16:37 +000040/**
41 * os_timer_create() - create an new posix (interval) timer
42 */
Johannes Berg56fc1872019-05-06 14:39:38 +020043int os_timer_create(void)
44{
45 timer_t *t = &event_high_res_timer;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000046
Johannes Berg56fc1872019-05-06 14:39:38 +020047 if (timer_create(CLOCK_MONOTONIC, NULL, t) == -1)
Anton Ivanov2eb5f312015-11-02 16:16:37 +000048 return -1;
Johannes Berg56fc1872019-05-06 14:39:38 +020049
Anton Ivanov2eb5f312015-11-02 16:16:37 +000050 return 0;
51}
52
Johannes Bergc7c6f3b2019-05-27 10:34:26 +020053int os_timer_set_interval(unsigned long long nsecs)
Anton Ivanov2eb5f312015-11-02 16:16:37 +000054{
55 struct itimerspec its;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000056
Johannes Bergc7c6f3b2019-05-27 10:34:26 +020057 its.it_value.tv_sec = nsecs / UM_NSEC_PER_SEC;
58 its.it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000059
Johannes Bergc7c6f3b2019-05-27 10:34:26 +020060 its.it_interval.tv_sec = nsecs / UM_NSEC_PER_SEC;
61 its.it_interval.tv_nsec = nsecs % UM_NSEC_PER_SEC;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000062
Johannes Berg56fc1872019-05-06 14:39:38 +020063 if (timer_settime(event_high_res_timer, 0, &its, NULL) == -1)
Anton Ivanov2eb5f312015-11-02 16:16:37 +000064 return -errno;
Anton Ivanov2eb5f312015-11-02 16:16:37 +000065
66 return 0;
67}
68
Johannes Bergc7c6f3b2019-05-27 10:34:26 +020069int os_timer_one_shot(unsigned long long nsecs)
Anton Ivanov2eb5f312015-11-02 16:16:37 +000070{
Johannes Berg56fc1872019-05-06 14:39:38 +020071 struct itimerspec its = {
Johannes Bergc7c6f3b2019-05-27 10:34:26 +020072 .it_value.tv_sec = nsecs / UM_NSEC_PER_SEC,
73 .it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000074
Johannes Berg56fc1872019-05-06 14:39:38 +020075 .it_interval.tv_sec = 0,
76 .it_interval.tv_nsec = 0, // we cheat here
77 };
Anton Ivanov2eb5f312015-11-02 16:16:37 +000078
79 timer_settime(event_high_res_timer, 0, &its, NULL);
80 return 0;
81}
82
83/**
84 * os_timer_disable() - disable the posix (interval) timer
Anton Ivanov2eb5f312015-11-02 16:16:37 +000085 */
Johannes Berg56fc1872019-05-06 14:39:38 +020086void os_timer_disable(void)
Anton Ivanov2eb5f312015-11-02 16:16:37 +000087{
88 struct itimerspec its;
89
90 memset(&its, 0, sizeof(struct itimerspec));
Johannes Berg56fc1872019-05-06 14:39:38 +020091 timer_settime(event_high_res_timer, 0, &its, NULL);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080092}
93
Jeff Dike5f734612007-10-16 01:27:27 -070094long long os_nsecs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000096 struct timespec ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Anton Ivanov2eb5f312015-11-02 16:16:37 +000098 clock_gettime(CLOCK_MONOTONIC,&ts);
99 return timespec_to_ns(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000102/**
Johannes Berg49da38a2020-12-02 20:58:04 +0100103 * os_idle_sleep() - sleep until interrupted
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000104 */
Johannes Berg49da38a2020-12-02 20:58:04 +0100105void os_idle_sleep(void)
Jeff Dikeb160fb62007-10-16 01:27:26 -0700106{
Johannes Berg49da38a2020-12-02 20:58:04 +0100107 pause();
Gennady Sharapovcff65c42006-01-18 17:42:42 -0800108}