Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2013 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 25 | #include <asm/iosf_mbi.h> |
| 26 | |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 27 | #include "i915_drv.h" |
Jani Nikula | 1d455f8 | 2019-08-06 14:39:33 +0300 | [diff] [blame] | 28 | #include "intel_sideband.h" |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 29 | |
Jesse Barnes | d8228d0 | 2013-10-11 12:09:30 -0700 | [diff] [blame] | 30 | /* |
| 31 | * IOSF sideband, see VLV2_SidebandMsg_HAS.docx and |
| 32 | * VLV_VLV2_PUNIT_HAS_0.8.docx |
| 33 | */ |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 34 | |
| 35 | /* Standard MMIO read, non-posted */ |
| 36 | #define SB_MRD_NP 0x00 |
| 37 | /* Standard MMIO write, non-posted */ |
| 38 | #define SB_MWR_NP 0x01 |
| 39 | /* Private register read, double-word addressing, non-posted */ |
| 40 | #define SB_CRRDDA_NP 0x06 |
| 41 | /* Private register write, double-word addressing, non-posted */ |
| 42 | #define SB_CRWRDA_NP 0x07 |
| 43 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 44 | static void ping(void *info) |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 45 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 46 | } |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 47 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 48 | static void __vlv_punit_get(struct drm_i915_private *i915) |
| 49 | { |
| 50 | iosf_mbi_punit_acquire(); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 51 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 52 | /* |
| 53 | * Prevent the cpu from sleeping while we use this sideband, otherwise |
| 54 | * the punit may cause a machine hang. The issue appears to be isolated |
| 55 | * with changing the power state of the CPU package while changing |
| 56 | * the power state via the punit, and we have only observed it |
| 57 | * reliably on 4-core Baytail systems suggesting the issue is in the |
| 58 | * power delivery mechanism and likely to be be board/function |
| 59 | * specific. Hence we presume the workaround needs only be applied |
| 60 | * to the Valleyview P-unit and not all sideband communications. |
| 61 | */ |
| 62 | if (IS_VALLEYVIEW(i915)) { |
Rafael J. Wysocki | 4d4dda4 | 2020-02-12 00:12:10 +0100 | [diff] [blame] | 63 | cpu_latency_qos_update_request(&i915->sb_qos, 0); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 64 | on_each_cpu(ping, NULL, 1); |
| 65 | } |
| 66 | } |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 67 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 68 | static void __vlv_punit_put(struct drm_i915_private *i915) |
| 69 | { |
| 70 | if (IS_VALLEYVIEW(i915)) |
Rafael J. Wysocki | 4d4dda4 | 2020-02-12 00:12:10 +0100 | [diff] [blame] | 71 | cpu_latency_qos_update_request(&i915->sb_qos, |
| 72 | PM_QOS_DEFAULT_VALUE); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 73 | |
| 74 | iosf_mbi_punit_release(); |
| 75 | } |
| 76 | |
Chris Wilson | 221c786 | 2019-04-26 09:17:19 +0100 | [diff] [blame] | 77 | void vlv_iosf_sb_get(struct drm_i915_private *i915, unsigned long ports) |
| 78 | { |
| 79 | if (ports & BIT(VLV_IOSF_SB_PUNIT)) |
| 80 | __vlv_punit_get(i915); |
| 81 | |
| 82 | mutex_lock(&i915->sb_lock); |
| 83 | } |
| 84 | |
| 85 | void vlv_iosf_sb_put(struct drm_i915_private *i915, unsigned long ports) |
| 86 | { |
| 87 | mutex_unlock(&i915->sb_lock); |
| 88 | |
| 89 | if (ports & BIT(VLV_IOSF_SB_PUNIT)) |
| 90 | __vlv_punit_put(i915); |
| 91 | } |
| 92 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 93 | static int vlv_sideband_rw(struct drm_i915_private *i915, |
| 94 | u32 devfn, u32 port, u32 opcode, |
| 95 | u32 addr, u32 *val) |
| 96 | { |
| 97 | struct intel_uncore *uncore = &i915->uncore; |
| 98 | const bool is_read = (opcode == SB_MRD_NP || opcode == SB_CRRDDA_NP); |
| 99 | int err; |
| 100 | |
| 101 | lockdep_assert_held(&i915->sb_lock); |
Chris Wilson | 221c786 | 2019-04-26 09:17:19 +0100 | [diff] [blame] | 102 | if (port == IOSF_PORT_PUNIT) |
| 103 | iosf_mbi_assert_punit_acquired(); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 104 | |
| 105 | /* Flush the previous comms, just in case it failed last time. */ |
| 106 | if (intel_wait_for_register(uncore, |
Chris Wilson | 4ce533b | 2016-06-30 15:33:37 +0100 | [diff] [blame] | 107 | VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0, |
| 108 | 5)) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 109 | drm_dbg(&i915->drm, "IOSF sideband idle wait (%s) timed out\n", |
| 110 | is_read ? "read" : "write"); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 111 | return -EAGAIN; |
| 112 | } |
| 113 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 114 | preempt_disable(); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 115 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 116 | intel_uncore_write_fw(uncore, VLV_IOSF_ADDR, addr); |
| 117 | intel_uncore_write_fw(uncore, VLV_IOSF_DATA, is_read ? 0 : *val); |
| 118 | intel_uncore_write_fw(uncore, VLV_IOSF_DOORBELL_REQ, |
| 119 | (devfn << IOSF_DEVFN_SHIFT) | |
| 120 | (opcode << IOSF_OPCODE_SHIFT) | |
| 121 | (port << IOSF_PORT_SHIFT) | |
| 122 | (0xf << IOSF_BYTE_ENABLES_SHIFT) | |
| 123 | (0 << IOSF_BAR_SHIFT) | |
| 124 | IOSF_SB_BUSY); |
| 125 | |
| 126 | if (__intel_wait_for_register_fw(uncore, |
| 127 | VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0, |
| 128 | 10000, 0, NULL) == 0) { |
| 129 | if (is_read) |
| 130 | *val = intel_uncore_read_fw(uncore, VLV_IOSF_DATA); |
| 131 | err = 0; |
| 132 | } else { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 133 | drm_dbg(&i915->drm, "IOSF sideband finish wait (%s) timed out\n", |
| 134 | is_read ? "read" : "write"); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 135 | err = -ETIMEDOUT; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 136 | } |
| 137 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 138 | preempt_enable(); |
Chris Wilson | 9fcee2f | 2017-01-26 10:19:19 +0000 | [diff] [blame] | 139 | |
| 140 | return err; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 141 | } |
| 142 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 143 | u32 vlv_punit_read(struct drm_i915_private *i915, u32 addr) |
Jesse Barnes | f341915 | 2013-11-04 11:52:44 -0800 | [diff] [blame] | 144 | { |
| 145 | u32 val = 0; |
| 146 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 147 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT, |
| 148 | SB_CRRDDA_NP, addr, &val); |
| 149 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 150 | return val; |
| 151 | } |
| 152 | |
| 153 | int vlv_punit_write(struct drm_i915_private *i915, u32 addr, u32 val) |
| 154 | { |
Chris Wilson | 337fa6e | 2019-04-26 09:17:20 +0100 | [diff] [blame] | 155 | return vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT, |
| 156 | SB_CRWRDA_NP, addr, &val); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | u32 vlv_bunit_read(struct drm_i915_private *i915, u32 reg) |
| 160 | { |
| 161 | u32 val = 0; |
| 162 | |
| 163 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 164 | SB_CRRDDA_NP, reg, &val); |
Jesse Barnes | f341915 | 2013-11-04 11:52:44 -0800 | [diff] [blame] | 165 | |
| 166 | return val; |
| 167 | } |
| 168 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 169 | void vlv_bunit_write(struct drm_i915_private *i915, u32 reg, u32 val) |
Jesse Barnes | f341915 | 2013-11-04 11:52:44 -0800 | [diff] [blame] | 170 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 171 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 172 | SB_CRWRDA_NP, reg, &val); |
Jesse Barnes | f341915 | 2013-11-04 11:52:44 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 175 | u32 vlv_nc_read(struct drm_i915_private *i915, u8 addr) |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 176 | { |
Jani Nikula | 6493625 | 2013-05-22 15:36:20 +0300 | [diff] [blame] | 177 | u32 val = 0; |
Jani Nikula | 5a09ae9f | 2013-05-22 15:36:17 +0300 | [diff] [blame] | 178 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 179 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_NC, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 180 | SB_CRRDDA_NP, addr, &val); |
Jani Nikula | 5a09ae9f | 2013-05-22 15:36:17 +0300 | [diff] [blame] | 181 | |
Jani Nikula | 6493625 | 2013-05-22 15:36:20 +0300 | [diff] [blame] | 182 | return val; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 183 | } |
| 184 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 185 | u32 vlv_iosf_sb_read(struct drm_i915_private *i915, u8 port, u32 reg) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 186 | { |
| 187 | u32 val = 0; |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 188 | |
| 189 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), port, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 190 | SB_CRRDDA_NP, reg, &val); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 191 | |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 192 | return val; |
| 193 | } |
| 194 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 195 | void vlv_iosf_sb_write(struct drm_i915_private *i915, |
Deepak M | dfb19ed | 2016-02-04 18:55:15 +0200 | [diff] [blame] | 196 | u8 port, u32 reg, u32 val) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 197 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 198 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), port, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 199 | SB_CRWRDA_NP, reg, &val); |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 202 | u32 vlv_cck_read(struct drm_i915_private *i915, u32 reg) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 203 | { |
| 204 | u32 val = 0; |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 205 | |
| 206 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 207 | SB_CRRDDA_NP, reg, &val); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 208 | |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 209 | return val; |
| 210 | } |
| 211 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 212 | void vlv_cck_write(struct drm_i915_private *i915, u32 reg, u32 val) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 213 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 214 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 215 | SB_CRWRDA_NP, reg, &val); |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 216 | } |
| 217 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 218 | u32 vlv_ccu_read(struct drm_i915_private *i915, u32 reg) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 219 | { |
| 220 | u32 val = 0; |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 221 | |
| 222 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 223 | SB_CRRDDA_NP, reg, &val); |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 224 | |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 225 | return val; |
| 226 | } |
| 227 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 228 | void vlv_ccu_write(struct drm_i915_private *i915, u32 reg, u32 val) |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 229 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 230 | vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU, |
Imre Deak | cf63e4a | 2014-05-19 11:41:17 +0300 | [diff] [blame] | 231 | SB_CRWRDA_NP, reg, &val); |
Jani Nikula | e9f882a | 2013-08-27 15:12:14 +0300 | [diff] [blame] | 232 | } |
| 233 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 234 | u32 vlv_dpio_read(struct drm_i915_private *i915, enum pipe pipe, int reg) |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 235 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 236 | int port = i915->dpio_phy_iosf_port[DPIO_PHY(pipe)]; |
Jani Nikula | 5a09ae9f | 2013-05-22 15:36:17 +0300 | [diff] [blame] | 237 | u32 val = 0; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 238 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 239 | vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MRD_NP, reg, &val); |
Ville Syrjälä | 0d95e11 | 2014-03-31 18:21:27 +0300 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * FIXME: There might be some registers where all 1's is a valid value, |
| 243 | * so ideally we should check the register offset instead... |
| 244 | */ |
Pankaj Bharadiya | 48a1b8d | 2020-01-15 09:14:53 +0530 | [diff] [blame] | 245 | drm_WARN(&i915->drm, val == 0xffffffff, |
| 246 | "DPIO read pipe %c reg 0x%x == 0x%x\n", |
| 247 | pipe_name(pipe), reg, val); |
Ville Syrjälä | 0d95e11 | 2014-03-31 18:21:27 +0300 | [diff] [blame] | 248 | |
Jani Nikula | 5a09ae9f | 2013-05-22 15:36:17 +0300 | [diff] [blame] | 249 | return val; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 250 | } |
| 251 | |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 252 | void vlv_dpio_write(struct drm_i915_private *i915, |
| 253 | enum pipe pipe, int reg, u32 val) |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 254 | { |
Chris Wilson | a75d035 | 2019-04-26 09:17:18 +0100 | [diff] [blame] | 255 | int port = i915->dpio_phy_iosf_port[DPIO_PHY(pipe)]; |
| 256 | |
| 257 | vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MWR_NP, reg, &val); |
| 258 | } |
| 259 | |
| 260 | u32 vlv_flisdsi_read(struct drm_i915_private *i915, u32 reg) |
| 261 | { |
| 262 | u32 val = 0; |
| 263 | |
| 264 | vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRRDDA_NP, |
| 265 | reg, &val); |
| 266 | return val; |
| 267 | } |
| 268 | |
| 269 | void vlv_flisdsi_write(struct drm_i915_private *i915, u32 reg, u32 val) |
| 270 | { |
| 271 | vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRWRDA_NP, |
| 272 | reg, &val); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* SBI access */ |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 276 | static int intel_sbi_rw(struct drm_i915_private *i915, u16 reg, |
| 277 | enum intel_sbi_destination destination, |
| 278 | u32 *val, bool is_read) |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 279 | { |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 280 | struct intel_uncore *uncore = &i915->uncore; |
| 281 | u32 cmd; |
Chris Wilson | 221c786 | 2019-04-26 09:17:19 +0100 | [diff] [blame] | 282 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 283 | lockdep_assert_held(&i915->sb_lock); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 284 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 285 | if (intel_wait_for_register_fw(uncore, |
| 286 | SBI_CTL_STAT, SBI_BUSY, 0, |
| 287 | 100)) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 288 | drm_err(&i915->drm, |
| 289 | "timeout waiting for SBI to become ready\n"); |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 290 | return -EBUSY; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 291 | } |
| 292 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 293 | intel_uncore_write_fw(uncore, SBI_ADDR, (u32)reg << 16); |
| 294 | intel_uncore_write_fw(uncore, SBI_DATA, is_read ? 0 : *val); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 295 | |
| 296 | if (destination == SBI_ICLK) |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 297 | cmd = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 298 | else |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 299 | cmd = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD; |
| 300 | if (!is_read) |
| 301 | cmd |= BIT(8); |
| 302 | intel_uncore_write_fw(uncore, SBI_CTL_STAT, cmd | SBI_BUSY); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 303 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 304 | if (__intel_wait_for_register_fw(uncore, |
| 305 | SBI_CTL_STAT, SBI_BUSY, 0, |
| 306 | 100, 100, &cmd)) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 307 | drm_err(&i915->drm, |
| 308 | "timeout waiting for SBI to complete read\n"); |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 309 | return -ETIMEDOUT; |
Chris Wilson | b0734f77b | 2017-02-23 14:10:20 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 312 | if (cmd & SBI_RESPONSE_FAIL) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 313 | drm_err(&i915->drm, "error during SBI read of reg %x\n", reg); |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 314 | return -ENXIO; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 315 | } |
| 316 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 317 | if (is_read) |
| 318 | *val = intel_uncore_read_fw(uncore, SBI_DATA); |
| 319 | |
| 320 | return 0; |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 321 | } |
| 322 | |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 323 | u32 intel_sbi_read(struct drm_i915_private *i915, u16 reg, |
| 324 | enum intel_sbi_destination destination) |
| 325 | { |
| 326 | u32 result = 0; |
| 327 | |
| 328 | intel_sbi_rw(i915, reg, destination, &result, true); |
| 329 | |
| 330 | return result; |
| 331 | } |
| 332 | |
| 333 | void intel_sbi_write(struct drm_i915_private *i915, u16 reg, u32 value, |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 334 | enum intel_sbi_destination destination) |
| 335 | { |
Chris Wilson | 7531942 | 2019-04-26 09:17:23 +0100 | [diff] [blame] | 336 | intel_sbi_rw(i915, reg, destination, &value, false); |
Jani Nikula | 59de081 | 2013-05-22 15:36:16 +0300 | [diff] [blame] | 337 | } |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 338 | |
Jani Nikula | 81b55ef | 2020-04-20 17:04:38 +0300 | [diff] [blame^] | 339 | static int gen6_check_mailbox_status(u32 mbox) |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 340 | { |
| 341 | switch (mbox & GEN6_PCODE_ERROR_MASK) { |
| 342 | case GEN6_PCODE_SUCCESS: |
| 343 | return 0; |
| 344 | case GEN6_PCODE_UNIMPLEMENTED_CMD: |
| 345 | return -ENODEV; |
| 346 | case GEN6_PCODE_ILLEGAL_CMD: |
| 347 | return -ENXIO; |
| 348 | case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE: |
| 349 | case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE: |
| 350 | return -EOVERFLOW; |
| 351 | case GEN6_PCODE_TIMEOUT: |
| 352 | return -ETIMEDOUT; |
| 353 | default: |
| 354 | MISSING_CASE(mbox & GEN6_PCODE_ERROR_MASK); |
| 355 | return 0; |
| 356 | } |
| 357 | } |
| 358 | |
Jani Nikula | 81b55ef | 2020-04-20 17:04:38 +0300 | [diff] [blame^] | 359 | static int gen7_check_mailbox_status(u32 mbox) |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 360 | { |
| 361 | switch (mbox & GEN6_PCODE_ERROR_MASK) { |
| 362 | case GEN6_PCODE_SUCCESS: |
| 363 | return 0; |
| 364 | case GEN6_PCODE_ILLEGAL_CMD: |
| 365 | return -ENXIO; |
| 366 | case GEN7_PCODE_TIMEOUT: |
| 367 | return -ETIMEDOUT; |
| 368 | case GEN7_PCODE_ILLEGAL_DATA: |
| 369 | return -EINVAL; |
Matt Roper | f22fd33 | 2020-01-10 17:45:11 -0800 | [diff] [blame] | 370 | case GEN11_PCODE_ILLEGAL_SUBCOMMAND: |
| 371 | return -ENXIO; |
| 372 | case GEN11_PCODE_LOCKED: |
| 373 | return -EBUSY; |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 374 | case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE: |
| 375 | return -EOVERFLOW; |
| 376 | default: |
| 377 | MISSING_CASE(mbox & GEN6_PCODE_ERROR_MASK); |
| 378 | return 0; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static int __sandybridge_pcode_rw(struct drm_i915_private *i915, |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 383 | u32 mbox, u32 *val, u32 *val1, |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 384 | int fast_timeout_us, |
| 385 | int slow_timeout_ms, |
| 386 | bool is_read) |
| 387 | { |
| 388 | struct intel_uncore *uncore = &i915->uncore; |
| 389 | |
| 390 | lockdep_assert_held(&i915->sb_lock); |
| 391 | |
| 392 | /* |
| 393 | * GEN6_PCODE_* are outside of the forcewake domain, we can |
| 394 | * use te fw I915_READ variants to reduce the amount of work |
| 395 | * required when reading/writing. |
| 396 | */ |
| 397 | |
| 398 | if (intel_uncore_read_fw(uncore, GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) |
| 399 | return -EAGAIN; |
| 400 | |
| 401 | intel_uncore_write_fw(uncore, GEN6_PCODE_DATA, *val); |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 402 | intel_uncore_write_fw(uncore, GEN6_PCODE_DATA1, val1 ? *val1 : 0); |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 403 | intel_uncore_write_fw(uncore, |
| 404 | GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox); |
| 405 | |
| 406 | if (__intel_wait_for_register_fw(uncore, |
| 407 | GEN6_PCODE_MAILBOX, |
| 408 | GEN6_PCODE_READY, 0, |
| 409 | fast_timeout_us, |
| 410 | slow_timeout_ms, |
| 411 | &mbox)) |
| 412 | return -ETIMEDOUT; |
| 413 | |
| 414 | if (is_read) |
| 415 | *val = intel_uncore_read_fw(uncore, GEN6_PCODE_DATA); |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 416 | if (is_read && val1) |
| 417 | *val1 = intel_uncore_read_fw(uncore, GEN6_PCODE_DATA1); |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 418 | |
| 419 | if (INTEL_GEN(i915) > 6) |
| 420 | return gen7_check_mailbox_status(mbox); |
| 421 | else |
| 422 | return gen6_check_mailbox_status(mbox); |
| 423 | } |
| 424 | |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 425 | int sandybridge_pcode_read(struct drm_i915_private *i915, u32 mbox, |
| 426 | u32 *val, u32 *val1) |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 427 | { |
| 428 | int err; |
| 429 | |
| 430 | mutex_lock(&i915->sb_lock); |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 431 | err = __sandybridge_pcode_rw(i915, mbox, val, val1, |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 432 | 500, 0, |
| 433 | true); |
| 434 | mutex_unlock(&i915->sb_lock); |
| 435 | |
| 436 | if (err) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 437 | drm_dbg(&i915->drm, |
| 438 | "warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n", |
| 439 | mbox, __builtin_return_address(0), err); |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return err; |
| 443 | } |
| 444 | |
| 445 | int sandybridge_pcode_write_timeout(struct drm_i915_private *i915, |
| 446 | u32 mbox, u32 val, |
| 447 | int fast_timeout_us, |
| 448 | int slow_timeout_ms) |
| 449 | { |
| 450 | int err; |
| 451 | |
| 452 | mutex_lock(&i915->sb_lock); |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 453 | err = __sandybridge_pcode_rw(i915, mbox, &val, NULL, |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 454 | fast_timeout_us, slow_timeout_ms, |
| 455 | false); |
| 456 | mutex_unlock(&i915->sb_lock); |
| 457 | |
| 458 | if (err) { |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 459 | drm_dbg(&i915->drm, |
| 460 | "warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n", |
| 461 | val, mbox, __builtin_return_address(0), err); |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | static bool skl_pcode_try_request(struct drm_i915_private *i915, u32 mbox, |
| 468 | u32 request, u32 reply_mask, u32 reply, |
| 469 | u32 *status) |
| 470 | { |
Ville Syrjälä | d284d51 | 2019-05-21 19:40:24 +0300 | [diff] [blame] | 471 | *status = __sandybridge_pcode_rw(i915, mbox, &request, NULL, |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 472 | 500, 0, |
| 473 | true); |
| 474 | |
| 475 | return *status || ((request & reply_mask) == reply); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * skl_pcode_request - send PCODE request until acknowledgment |
| 480 | * @i915: device private |
| 481 | * @mbox: PCODE mailbox ID the request is targeted for |
| 482 | * @request: request ID |
| 483 | * @reply_mask: mask used to check for request acknowledgment |
| 484 | * @reply: value used to check for request acknowledgment |
| 485 | * @timeout_base_ms: timeout for polling with preemption enabled |
| 486 | * |
| 487 | * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE |
| 488 | * reports an error or an overall timeout of @timeout_base_ms+50 ms expires. |
| 489 | * The request is acknowledged once the PCODE reply dword equals @reply after |
| 490 | * applying @reply_mask. Polling is first attempted with preemption enabled |
| 491 | * for @timeout_base_ms and if this times out for another 50 ms with |
| 492 | * preemption disabled. |
| 493 | * |
| 494 | * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some |
| 495 | * other error as reported by PCODE. |
| 496 | */ |
| 497 | int skl_pcode_request(struct drm_i915_private *i915, u32 mbox, u32 request, |
| 498 | u32 reply_mask, u32 reply, int timeout_base_ms) |
| 499 | { |
| 500 | u32 status; |
| 501 | int ret; |
| 502 | |
| 503 | mutex_lock(&i915->sb_lock); |
| 504 | |
| 505 | #define COND \ |
| 506 | skl_pcode_try_request(i915, mbox, request, reply_mask, reply, &status) |
| 507 | |
| 508 | /* |
| 509 | * Prime the PCODE by doing a request first. Normally it guarantees |
| 510 | * that a subsequent request, at most @timeout_base_ms later, succeeds. |
| 511 | * _wait_for() doesn't guarantee when its passed condition is evaluated |
| 512 | * first, so send the first request explicitly. |
| 513 | */ |
| 514 | if (COND) { |
| 515 | ret = 0; |
| 516 | goto out; |
| 517 | } |
| 518 | ret = _wait_for(COND, timeout_base_ms * 1000, 10, 10); |
| 519 | if (!ret) |
| 520 | goto out; |
| 521 | |
| 522 | /* |
| 523 | * The above can time out if the number of requests was low (2 in the |
| 524 | * worst case) _and_ PCODE was busy for some reason even after a |
| 525 | * (queued) request and @timeout_base_ms delay. As a workaround retry |
| 526 | * the poll with preemption disabled to maximize the number of |
| 527 | * requests. Increase the timeout from @timeout_base_ms to 50ms to |
| 528 | * account for interrupts that could reduce the number of these |
| 529 | * requests, and for any quirks of the PCODE firmware that delays |
| 530 | * the request completion. |
| 531 | */ |
Wambui Karuga | b908af5 | 2020-01-07 18:13:32 +0300 | [diff] [blame] | 532 | drm_dbg_kms(&i915->drm, |
| 533 | "PCODE timeout, retrying with preemption disabled\n"); |
Pankaj Bharadiya | 48a1b8d | 2020-01-15 09:14:53 +0530 | [diff] [blame] | 534 | drm_WARN_ON_ONCE(&i915->drm, timeout_base_ms > 3); |
Chris Wilson | e0516e8 | 2019-04-26 09:17:25 +0100 | [diff] [blame] | 535 | preempt_disable(); |
| 536 | ret = wait_for_atomic(COND, 50); |
| 537 | preempt_enable(); |
| 538 | |
| 539 | out: |
| 540 | mutex_unlock(&i915->sb_lock); |
| 541 | return ret ? ret : status; |
| 542 | #undef COND |
| 543 | } |