Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 1 | #include "bmc_epoch.hpp" |
| 2 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 3 | #include "utils.hpp" |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 4 | |
| 5 | #include <sys/timerfd.h> |
| 6 | #include <unistd.h> |
| 7 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | #include <xyz/openbmc_project/Common/error.hpp> |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 12 | |
Gunnar Mills | 75710b6 | 2018-04-08 14:50:11 -0500 | [diff] [blame] | 13 | // Need to do this since its not exported outside of the kernel. |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 14 | // Refer : https://gist.github.com/lethean/446cea944b7441228298 |
| 15 | #ifndef TFD_TIMER_CANCEL_ON_SET |
| 16 | #define TFD_TIMER_CANCEL_ON_SET (1 << 1) |
| 17 | #endif |
| 18 | |
| 19 | // Needed to make sure timerfd does not misfire even though we set CANCEL_ON_SET |
| 20 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace time |
| 25 | { |
| 26 | namespace server = sdbusplus::xyz::openbmc_project::Time::server; |
| 27 | using namespace phosphor::logging; |
Lei YU | f6fad82 | 2018-07-13 16:35:45 +0800 | [diff] [blame] | 28 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 29 | BmcEpoch::BmcEpoch(sdbusplus::bus::bus& bus, const char* objPath) : |
| 30 | EpochBase(bus, objPath), bus(bus) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 31 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 32 | initialize(); |
| 33 | } |
| 34 | |
| 35 | void BmcEpoch::initialize() |
| 36 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 37 | using InternalFailure = |
| 38 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 39 | |
| 40 | // Subscribe time change event |
| 41 | // Choose the MAX time that is possible to avoid mis fires. |
| 42 | constexpr itimerspec maxTime = { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 43 | {0, 0}, // it_interval |
| 44 | {TIME_T_MAX, 0}, // it_value |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 48 | if (timeFd == -1) |
| 49 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 50 | log<level::ERR>("Failed to create timerfd", entry("ERRNO=%d", errno), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 51 | entry("ERR=%s", strerror(errno))); |
| 52 | elog<InternalFailure>(); |
| 53 | } |
| 54 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 55 | auto r = timerfd_settime( |
| 56 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 57 | if (r != 0) |
| 58 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 59 | log<level::ERR>("Failed to set timerfd", entry("ERRNO=%d", errno), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 60 | entry("ERR=%s", strerror(errno))); |
| 61 | elog<InternalFailure>(); |
| 62 | } |
| 63 | |
| 64 | sd_event_source* es; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 65 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 66 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 67 | if (r < 0) |
| 68 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 69 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", -r), |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 70 | entry("ERR=%s", strerror(-r))); |
| 71 | elog<InternalFailure>(); |
| 72 | } |
| 73 | timeChangeEventSource.reset(es); |
| 74 | } |
| 75 | |
| 76 | BmcEpoch::~BmcEpoch() |
| 77 | { |
| 78 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uint64_t BmcEpoch::elapsed() const |
| 82 | { |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 83 | return getTime().count(); |
| 84 | } |
| 85 | |
| 86 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 87 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 88 | /* |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 89 | Mode | Set BMC Time |
| 90 | ----- | ------------- |
| 91 | NTP | Fail to set |
| 92 | MANUAL| OK |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 93 | */ |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 94 | auto time = microseconds(value); |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 95 | setTime(time); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 96 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 97 | server::EpochTime::elapsed(value); |
| 98 | return value; |
| 99 | } |
| 100 | |
Ratan Gupta | 1e1dc44 | 2021-02-02 05:51:08 -0600 | [diff] [blame^] | 101 | int BmcEpoch::onTimeChange(sd_event_source* /* es */, int fd, |
| 102 | uint32_t /* revents */, void* /* userdata */) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 103 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 104 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 105 | |
| 106 | // We are not interested in the data here. |
| 107 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 108 | while (read(fd, time.data(), time.max_size()) > 0) |
| 109 | ; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 110 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 114 | } // namespace time |
| 115 | } // namespace phosphor |