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> |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Time/error.hpp> |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 13 | |
Gunnar Mills | 75710b6 | 2018-04-08 14:50:11 -0500 | [diff] [blame] | 14 | // Need to do this since its not exported outside of the kernel. |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 15 | // Refer : https://gist.github.com/lethean/446cea944b7441228298 |
| 16 | #ifndef TFD_TIMER_CANCEL_ON_SET |
| 17 | #define TFD_TIMER_CANCEL_ON_SET (1 << 1) |
| 18 | #endif |
| 19 | |
| 20 | // Needed to make sure timerfd does not misfire even though we set CANCEL_ON_SET |
| 21 | #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] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace time |
| 26 | { |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 27 | namespace // anonymous |
| 28 | { |
| 29 | constexpr auto SYSTEMD_TIME_SERVICE = "org.freedesktop.timedate1"; |
| 30 | constexpr auto SYSTEMD_TIME_PATH = "/org/freedesktop/timedate1"; |
| 31 | constexpr auto SYSTEMD_TIME_INTERFACE = "org.freedesktop.timedate1"; |
| 32 | constexpr auto METHOD_SET_TIME = "SetTime"; |
| 33 | } // namespace |
| 34 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 35 | namespace server = sdbusplus::xyz::openbmc_project::Time::server; |
| 36 | using namespace phosphor::logging; |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 37 | using FailedError = sdbusplus::xyz::openbmc_project::Time::Error::Failed; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 38 | |
| 39 | void BmcEpoch::initialize() |
| 40 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 41 | using InternalFailure = |
| 42 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 43 | |
| 44 | // Subscribe time change event |
| 45 | // Choose the MAX time that is possible to avoid mis fires. |
| 46 | constexpr itimerspec maxTime = { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 47 | {0, 0}, // it_interval |
| 48 | {TIME_T_MAX, 0}, // it_value |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | timeFd = timerfd_create(CLOCK_REALTIME, 0); |
| 52 | if (timeFd == -1) |
| 53 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 54 | lg2::error("Failed to create timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 55 | elog<InternalFailure>(); |
| 56 | } |
| 57 | |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 58 | auto r = timerfd_settime( |
| 59 | timeFd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &maxTime, nullptr); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 60 | if (r != 0) |
| 61 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 62 | lg2::error("Failed to set timerfd: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 63 | elog<InternalFailure>(); |
| 64 | } |
| 65 | |
| 66 | sd_event_source* es; |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 67 | r = sd_event_add_io(bus.get_event(), &es, timeFd, EPOLLIN, onTimeChange, |
| 68 | this); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 69 | if (r < 0) |
| 70 | { |
George Liu | 947b534 | 2022-07-01 16:12:18 +0800 | [diff] [blame] | 71 | lg2::error("Failed to add event: {ERRNO}", "ERRNO", errno); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 72 | elog<InternalFailure>(); |
| 73 | } |
| 74 | timeChangeEventSource.reset(es); |
| 75 | } |
| 76 | |
| 77 | BmcEpoch::~BmcEpoch() |
| 78 | { |
| 79 | close(timeFd); |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | uint64_t BmcEpoch::elapsed() const |
| 83 | { |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 84 | return getTime().count(); |
| 85 | } |
| 86 | |
| 87 | uint64_t BmcEpoch::elapsed(uint64_t value) |
| 88 | { |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 89 | /* |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 90 | Mode | Set BMC Time |
| 91 | ----- | ------------- |
| 92 | NTP | Fail to set |
| 93 | MANUAL| OK |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 94 | */ |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 95 | auto time = microseconds(value); |
George Liu | 3c2f449 | 2020-04-12 11:35:57 +0800 | [diff] [blame] | 96 | setTime(time); |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 97 | |
Lei YU | 9623282 | 2017-01-20 14:05:46 +0800 | [diff] [blame] | 98 | server::EpochTime::elapsed(value); |
| 99 | return value; |
| 100 | } |
| 101 | |
Ratan Gupta | 1e1dc44 | 2021-02-02 05:51:08 -0600 | [diff] [blame] | 102 | int BmcEpoch::onTimeChange(sd_event_source* /* es */, int fd, |
| 103 | uint32_t /* revents */, void* /* userdata */) |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 104 | { |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 105 | std::array<char, 64> time{}; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 106 | |
| 107 | // We are not interested in the data here. |
| 108 | // So read until there is no new data here in the FD |
Gunnar Mills | ab4cc6a | 2018-09-14 14:42:39 -0500 | [diff] [blame] | 109 | while (read(fd, time.data(), time.max_size()) > 0) |
| 110 | ; |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 111 | |
Lei YU | 7b21879 | 2017-02-09 12:10:13 +0800 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
George Liu | f1d54ae | 2022-09-01 16:54:57 +0800 | [diff] [blame] | 115 | void BmcEpoch::onModeChanged(Mode mode) |
| 116 | { |
| 117 | manager.setTimeMode(mode); |
| 118 | } |
| 119 | |
| 120 | bool BmcEpoch::setTime(const microseconds& usec) |
| 121 | { |
| 122 | auto method = bus.new_method_call(SYSTEMD_TIME_SERVICE, SYSTEMD_TIME_PATH, |
| 123 | SYSTEMD_TIME_INTERFACE, METHOD_SET_TIME); |
| 124 | method.append(static_cast<int64_t>(usec.count()), |
| 125 | false, // relative |
| 126 | false); // user_interaction |
| 127 | |
| 128 | try |
| 129 | { |
| 130 | bus.call_noreply(method); |
| 131 | } |
| 132 | catch (const sdbusplus::exception_t& ex) |
| 133 | { |
| 134 | lg2::error("Error in setting system time: {ERROR}", "ERROR", ex); |
| 135 | using namespace xyz::openbmc_project::Time; |
| 136 | elog<FailedError>(Failed::REASON(ex.what())); |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | microseconds BmcEpoch::getTime() const |
| 142 | { |
| 143 | auto now = system_clock::now(); |
| 144 | return duration_cast<microseconds>(now.time_since_epoch()); |
| 145 | } |
| 146 | |
Lei YU | af5abc5 | 2017-03-07 17:49:17 +0800 | [diff] [blame] | 147 | } // namespace time |
| 148 | } // namespace phosphor |