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> |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 3867926 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 29 | BmcEpoch::BmcEpoch(sdbusplus::bus_t& bus, const char* objPath) : |
George Liu | 261525d | 2022-07-01 17:02:55 +0800 | [diff] [blame] | 30 | EpochBase(bus, objPath) |
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 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 50 | lg2::error("Failed to create timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 51 | elog<InternalFailure>(); |
| 52 | } |
| 53 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 54 | auto r = timerfd_settime( |
| 55 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 56 | if (r != 0) |
| 57 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 58 | lg2::error("Failed to set timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 59 | elog<InternalFailure>(); |
| 60 | } |
| 61 | |
| 62 | sd_event_source* es; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 63 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 64 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 65 | if (r < 0) |
| 66 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 67 | lg2::error("Failed to add event: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 68 | elog<InternalFailure>(); |
| 69 | } |
| 70 | timeChangeEventSource.reset(es); |
| 71 | } |
| 72 | |
| 73 | BmcEpoch::~BmcEpoch() |
| 74 | { |
| 75 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | uint64_t BmcEpoch::elapsed() const |
| 79 | { |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 80 | return getTime().count(); |
| 81 | } |
| 82 | |
| 83 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 84 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 85 | /* |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 86 | Mode | Set BMC Time |
| 87 | ----- | ------------- |
| 88 | NTP | Fail to set |
| 89 | MANUAL| OK |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 90 | */ |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 91 | auto time = microseconds(value); |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 92 | setTime(time); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 93 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 94 | server::EpochTime::elapsed(value); |
| 95 | return value; |
| 96 | } |
| 97 | |
Ratan Gupta | 1e1dc44 | 2021-02-02 05:51:08 -0600 | [diff] [blame] | 98 | int BmcEpoch::onTimeChange(sd_event_source* /* es */, int fd, |
| 99 | uint32_t /* revents */, void* /* userdata */) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 100 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 101 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 102 | |
| 103 | // We are not interested in the data here. |
| 104 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 105 | while (read(fd, time.data(), time.max_size()) > 0) |
| 106 | ; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 107 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 111 | } // namespace time |
| 112 | } // namespace phosphor |