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 | |
George Liu | cb42109 | 2022-08-16 17:02:31 +0800 | [diff] [blame^] | 29 | BmcEpoch::BmcEpoch(sdbusplus::bus_t& bus, const char* objPath, |
| 30 | Manager& manager) : |
| 31 | EpochBase(bus, objPath, manager) |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 32 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 33 | initialize(); |
| 34 | } |
| 35 | |
| 36 | void BmcEpoch::initialize() |
| 37 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 38 | using InternalFailure = |
| 39 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 40 | |
| 41 | // Subscribe time change event |
| 42 | // Choose the MAX time that is possible to avoid mis fires. |
| 43 | constexpr itimerspec maxTime = { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 44 | {0, 0}, // it_interval |
| 45 | {TIME_T_MAX, 0}, // it_value |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 49 | if (timeFd == -1) |
| 50 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 51 | lg2::error("Failed to create timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 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 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 59 | lg2::error("Failed to set timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 60 | elog<InternalFailure>(); |
| 61 | } |
| 62 | |
| 63 | sd_event_source* es; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 64 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 65 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 66 | if (r < 0) |
| 67 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 68 | lg2::error("Failed to add event: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 69 | elog<InternalFailure>(); |
| 70 | } |
| 71 | timeChangeEventSource.reset(es); |
| 72 | } |
| 73 | |
| 74 | BmcEpoch::~BmcEpoch() |
| 75 | { |
| 76 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | uint64_t BmcEpoch::elapsed() const |
| 80 | { |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 81 | return getTime().count(); |
| 82 | } |
| 83 | |
| 84 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 85 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 86 | /* |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 87 | Mode | Set BMC Time |
| 88 | ----- | ------------- |
| 89 | NTP | Fail to set |
| 90 | MANUAL| OK |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 91 | */ |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 92 | auto time = microseconds(value); |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 93 | setTime(time); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 94 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 95 | server::EpochTime::elapsed(value); |
| 96 | return value; |
| 97 | } |
| 98 | |
Ratan Gupta | 1e1dc44 | 2021-02-02 05:51:08 -0600 | [diff] [blame] | 99 | int BmcEpoch::onTimeChange(sd_event_source* /* es */, int fd, |
| 100 | uint32_t /* revents */, void* /* userdata */) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 101 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 102 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 103 | |
| 104 | // We are not interested in the data here. |
| 105 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 106 | while (read(fd, time.data(), time.max_size()) > 0) |
| 107 | ; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 108 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 112 | } // namespace time |
| 113 | } // namespace phosphor |