Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 1 | /** |
Patrick Venture | e84b4dd | 2018-11-01 16:06:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 IBM Corporation |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 16 | #include "cfam_access.hpp" |
| 17 | |
| 18 | #include "targeting.hpp" |
| 19 | |
| 20 | #include <unistd.h> |
| 21 | |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 23 | #include <phosphor-logging/elog.hpp> |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 24 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
| 25 | #include <xyz/openbmc_project/Common/File/error.hpp> |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 26 | |
| 27 | namespace openpower |
| 28 | { |
| 29 | namespace cfam |
| 30 | { |
| 31 | namespace access |
| 32 | { |
| 33 | |
| 34 | constexpr auto cfamRegSize = 4; |
| 35 | |
| 36 | using namespace openpower::targeting; |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 37 | using namespace openpower::util; |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 38 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 39 | namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error; |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Converts the CFAM register address used by the calling |
| 43 | * code (because that's how it is in the spec) to the address |
| 44 | * required by the device driver. |
| 45 | */ |
| 46 | static inline cfam_address_t makeOffset(cfam_address_t address) |
| 47 | { |
| 48 | return (address & 0xFC00) | ((address & 0x03FF) << 2); |
| 49 | } |
| 50 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 51 | void writeReg(const std::unique_ptr<Target>& target, cfam_address_t address, |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 52 | cfam_data_t data) |
| 53 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 54 | using namespace phosphor::logging; |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 55 | int rc = lseek(target->getCFAMFD(), makeOffset(address), SEEK_SET); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 56 | if (rc < 0) |
| 57 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 58 | log<level::ERR>("Failed seeking on a processor CFAM", |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 59 | entry("CFAM_ADDRESS=0x%X", address)); |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 60 | |
| 61 | using metadata = xyz::openbmc_project::Common::File::Seek; |
| 62 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 63 | elog<file_error::Seek>(metadata::OFFSET(makeOffset(address)), |
| 64 | metadata::WHENCE(SEEK_SET), |
| 65 | metadata::ERRNO(errno), |
| 66 | metadata::PATH(target->getCFAMPath().c_str())); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Joel Stanley | afa1d22 | 2019-09-27 15:35:04 +0930 | [diff] [blame] | 69 | data = htobe32(data); |
Edward A. James | 8316b77 | 2017-04-24 14:20:48 -0500 | [diff] [blame] | 70 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 71 | rc = write(target->getCFAMFD(), &data, cfamRegSize); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 72 | if (rc < 0) |
| 73 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 74 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 75 | |
| 76 | elog<device_error::WriteFailure>( |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 77 | metadata::CALLOUT_ERRNO(errno), |
| 78 | metadata::CALLOUT_DEVICE_PATH(target->getCFAMPath().c_str())); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 82 | cfam_data_t readReg(const std::unique_ptr<Target>& target, |
| 83 | cfam_address_t address) |
| 84 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 85 | using namespace phosphor::logging; |
| 86 | |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 87 | cfam_data_t data = 0; |
| 88 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 89 | int rc = lseek(target->getCFAMFD(), makeOffset(address), SEEK_SET); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 90 | if (rc < 0) |
| 91 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 92 | log<level::ERR>("Failed seeking on a processor CFAM", |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 93 | entry("CFAM_ADDRESS=0x%X", address)); |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 94 | |
| 95 | using metadata = xyz::openbmc_project::Common::File::Seek; |
| 96 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 97 | elog<file_error::Seek>(metadata::OFFSET(makeOffset(address)), |
| 98 | metadata::WHENCE(SEEK_SET), |
| 99 | metadata::ERRNO(errno), |
| 100 | metadata::PATH(target->getCFAMPath().c_str())); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 103 | rc = read(target->getCFAMFD(), &data, cfamRegSize); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 104 | if (rc < 0) |
| 105 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 106 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 107 | |
| 108 | elog<device_error::ReadFailure>( |
| 109 | metadata::CALLOUT_ERRNO(errno), |
| 110 | metadata::CALLOUT_DEVICE_PATH(target->getCFAMPath().c_str())); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Joel Stanley | afa1d22 | 2019-09-27 15:35:04 +0930 | [diff] [blame] | 113 | return be32toh(data); |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 114 | } |
| 115 | |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 116 | void writeRegWithMask(const std::unique_ptr<Target>& target, |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 117 | cfam_address_t address, cfam_data_t data, |
Matt Spinler | 0c0eeff | 2017-02-28 10:06:56 -0600 | [diff] [blame] | 118 | cfam_mask_t mask) |
| 119 | { |
| 120 | cfam_data_t readData = readReg(target, address); |
| 121 | |
| 122 | readData &= ~mask; |
| 123 | readData |= (data & mask); |
| 124 | |
| 125 | writeReg(target, address, readData); |
| 126 | } |
| 127 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 128 | } // namespace access |
| 129 | } // namespace cfam |
| 130 | } // namespace openpower |