Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * MHI PCI driver - MHI over PCI controller driver |
| 4 | * |
| 5 | * This module is a generic driver for registering MHI-over-PCI devices, |
| 6 | * such as PCIe QCOM modems. |
| 7 | * |
| 8 | * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org> |
| 9 | */ |
| 10 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 11 | #include <linux/aer.h> |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 12 | #include <linux/delay.h> |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/mhi.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/pci.h> |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 18 | #include <linux/timer.h> |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 20 | |
| 21 | #define MHI_PCI_DEFAULT_BAR_NUM 0 |
| 22 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 23 | #define MHI_POST_RESET_DELAY_MS 500 |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 24 | |
| 25 | #define HEALTH_CHECK_PERIOD (HZ * 2) |
| 26 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 27 | /** |
| 28 | * struct mhi_pci_dev_info - MHI PCI device specific information |
| 29 | * @config: MHI controller configuration |
| 30 | * @name: name of the PCI module |
| 31 | * @fw: firmware path (if any) |
| 32 | * @edl: emergency download mode firmware path (if any) |
| 33 | * @bar_num: PCI base address register to use for MHI MMIO register space |
| 34 | * @dma_data_width: DMA transfer word size (32 or 64 bits) |
| 35 | */ |
| 36 | struct mhi_pci_dev_info { |
| 37 | const struct mhi_controller_config *config; |
| 38 | const char *name; |
| 39 | const char *fw; |
| 40 | const char *edl; |
| 41 | unsigned int bar_num; |
| 42 | unsigned int dma_data_width; |
| 43 | }; |
| 44 | |
| 45 | #define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 46 | { \ |
| 47 | .num = ch_num, \ |
| 48 | .name = ch_name, \ |
| 49 | .num_elements = el_count, \ |
| 50 | .event_ring = ev_ring, \ |
| 51 | .dir = DMA_TO_DEVICE, \ |
| 52 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 53 | .pollcfg = 0, \ |
| 54 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 55 | .lpm_notify = false, \ |
| 56 | .offload_channel = false, \ |
| 57 | .doorbell_mode_switch = false, \ |
| 58 | } \ |
| 59 | |
| 60 | #define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 61 | { \ |
| 62 | .num = ch_num, \ |
| 63 | .name = ch_name, \ |
| 64 | .num_elements = el_count, \ |
| 65 | .event_ring = ev_ring, \ |
| 66 | .dir = DMA_FROM_DEVICE, \ |
| 67 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 68 | .pollcfg = 0, \ |
| 69 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 70 | .lpm_notify = false, \ |
| 71 | .offload_channel = false, \ |
| 72 | .doorbell_mode_switch = false, \ |
| 73 | } |
| 74 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 75 | #define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 76 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 77 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 78 | .irq_moderation_ms = 0, \ |
| 79 | .irq = (ev_ring) + 1, \ |
| 80 | .priority = 1, \ |
| 81 | .mode = MHI_DB_BRST_DISABLE, \ |
| 82 | .data_type = MHI_ER_CTRL, \ |
| 83 | .hardware_event = false, \ |
| 84 | .client_managed = false, \ |
| 85 | .offload_channel = false, \ |
| 86 | } |
| 87 | |
Loic Poulain | eb96787 | 2021-01-04 17:14:52 +0100 | [diff] [blame] | 88 | #define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 89 | { \ |
| 90 | .num = ch_num, \ |
| 91 | .name = ch_name, \ |
| 92 | .num_elements = el_count, \ |
| 93 | .event_ring = ev_ring, \ |
| 94 | .dir = DMA_TO_DEVICE, \ |
| 95 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 96 | .pollcfg = 0, \ |
| 97 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 98 | .lpm_notify = false, \ |
| 99 | .offload_channel = false, \ |
| 100 | .doorbell_mode_switch = true, \ |
| 101 | } \ |
| 102 | |
| 103 | #define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 104 | { \ |
| 105 | .num = ch_num, \ |
| 106 | .name = ch_name, \ |
| 107 | .num_elements = el_count, \ |
| 108 | .event_ring = ev_ring, \ |
| 109 | .dir = DMA_FROM_DEVICE, \ |
| 110 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 111 | .pollcfg = 0, \ |
| 112 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 113 | .lpm_notify = false, \ |
| 114 | .offload_channel = false, \ |
| 115 | .doorbell_mode_switch = true, \ |
| 116 | } |
| 117 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 118 | #define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 119 | { \ |
| 120 | .num = ch_num, \ |
| 121 | .name = ch_name, \ |
| 122 | .num_elements = el_count, \ |
| 123 | .event_ring = ev_ring, \ |
| 124 | .dir = DMA_TO_DEVICE, \ |
| 125 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 126 | .pollcfg = 0, \ |
| 127 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 128 | .lpm_notify = false, \ |
| 129 | .offload_channel = false, \ |
| 130 | .doorbell_mode_switch = false, \ |
| 131 | } \ |
| 132 | |
| 133 | #define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 134 | { \ |
| 135 | .num = ch_num, \ |
| 136 | .name = ch_name, \ |
| 137 | .num_elements = el_count, \ |
| 138 | .event_ring = ev_ring, \ |
| 139 | .dir = DMA_FROM_DEVICE, \ |
| 140 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 141 | .pollcfg = 0, \ |
| 142 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 143 | .lpm_notify = false, \ |
| 144 | .offload_channel = false, \ |
| 145 | .doorbell_mode_switch = false, \ |
| 146 | } |
| 147 | |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame^] | 148 | #define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 149 | { \ |
| 150 | .num = ch_num, \ |
| 151 | .name = ch_name, \ |
| 152 | .num_elements = el_count, \ |
| 153 | .event_ring = ev_ring, \ |
| 154 | .dir = DMA_TO_DEVICE, \ |
| 155 | .ee_mask = BIT(MHI_EE_FP), \ |
| 156 | .pollcfg = 0, \ |
| 157 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 158 | .lpm_notify = false, \ |
| 159 | .offload_channel = false, \ |
| 160 | .doorbell_mode_switch = false, \ |
| 161 | } \ |
| 162 | |
| 163 | #define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 164 | { \ |
| 165 | .num = ch_num, \ |
| 166 | .name = ch_name, \ |
| 167 | .num_elements = el_count, \ |
| 168 | .event_ring = ev_ring, \ |
| 169 | .dir = DMA_FROM_DEVICE, \ |
| 170 | .ee_mask = BIT(MHI_EE_FP), \ |
| 171 | .pollcfg = 0, \ |
| 172 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 173 | .lpm_notify = false, \ |
| 174 | .offload_channel = false, \ |
| 175 | .doorbell_mode_switch = false, \ |
| 176 | } |
| 177 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 178 | #define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 179 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 180 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 181 | .irq_moderation_ms = 5, \ |
| 182 | .irq = (ev_ring) + 1, \ |
| 183 | .priority = 1, \ |
| 184 | .mode = MHI_DB_BRST_DISABLE, \ |
| 185 | .data_type = MHI_ER_DATA, \ |
| 186 | .hardware_event = false, \ |
| 187 | .client_managed = false, \ |
| 188 | .offload_channel = false, \ |
| 189 | } |
| 190 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 191 | #define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 192 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 193 | .num_elements = el_count, \ |
Loic Poulain | ec751369 | 2021-01-04 17:14:59 +0100 | [diff] [blame] | 194 | .irq_moderation_ms = 1, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 195 | .irq = (ev_ring) + 1, \ |
| 196 | .priority = 1, \ |
| 197 | .mode = MHI_DB_BRST_DISABLE, \ |
| 198 | .data_type = MHI_ER_DATA, \ |
| 199 | .hardware_event = true, \ |
| 200 | .client_managed = false, \ |
| 201 | .offload_channel = false, \ |
| 202 | .channel = ch_num, \ |
| 203 | } |
| 204 | |
| 205 | static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = { |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 206 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1), |
| 207 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 208 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0), |
| 209 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0), |
| 210 | MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0), |
| 211 | MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0), |
| 212 | MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0), |
| 213 | MHI_CHANNEL_CONFIG_DL(21, "IPCR", 8, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame^] | 214 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 215 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 216 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2), |
| 217 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 218 | }; |
| 219 | |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 220 | static struct mhi_event_config modem_qcom_v1_mhi_events[] = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 221 | /* first ring is control+data ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 222 | MHI_EVENT_CONFIG_CTRL(0, 64), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 223 | /* DIAG dedicated event ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 224 | MHI_EVENT_CONFIG_DATA(1, 128), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 225 | /* Hardware channels request dedicated hardware event rings */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 226 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 227 | MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 228 | }; |
| 229 | |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 230 | static struct mhi_controller_config modem_qcom_v1_mhiv_config = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 231 | .max_channels = 128, |
Loic Poulain | 84026a5 | 2021-01-04 17:14:57 +0100 | [diff] [blame] | 232 | .timeout_ms = 8000, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 233 | .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels), |
| 234 | .ch_cfg = modem_qcom_v1_mhi_channels, |
| 235 | .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events), |
| 236 | .event_cfg = modem_qcom_v1_mhi_events, |
| 237 | }; |
| 238 | |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 239 | static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = { |
| 240 | .name = "qcom-sdx65m", |
| 241 | .fw = "qcom/sdx65m/xbl.elf", |
| 242 | .edl = "qcom/sdx65m/edl.mbn", |
| 243 | .config = &modem_qcom_v1_mhiv_config, |
| 244 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 245 | .dma_data_width = 32 |
| 246 | }; |
| 247 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 248 | static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = { |
| 249 | .name = "qcom-sdx55m", |
| 250 | .fw = "qcom/sdx55m/sbl1.mbn", |
| 251 | .edl = "qcom/sdx55m/edl.mbn", |
| 252 | .config = &modem_qcom_v1_mhiv_config, |
| 253 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 254 | .dma_data_width = 32 |
| 255 | }; |
| 256 | |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 257 | static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = { |
| 258 | .name = "qcom-sdx24", |
| 259 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 260 | .config = &modem_qcom_v1_mhiv_config, |
| 261 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 262 | .dma_data_width = 32 |
| 263 | }; |
| 264 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 265 | static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = { |
| 266 | MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0), |
| 267 | MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0), |
| 268 | MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0), |
| 269 | MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0), |
| 270 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1), |
| 271 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1), |
| 272 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0), |
| 273 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0), |
| 274 | MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0), |
| 275 | MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame^] | 276 | /* The EDL firmware is a flash-programmer exposing firehose protocol */ |
| 277 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 278 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 279 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2), |
| 280 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3), |
| 281 | }; |
| 282 | |
| 283 | static struct mhi_event_config mhi_quectel_em1xx_events[] = { |
| 284 | MHI_EVENT_CONFIG_CTRL(0, 128), |
| 285 | MHI_EVENT_CONFIG_DATA(1, 128), |
| 286 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 287 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101) |
| 288 | }; |
| 289 | |
| 290 | static struct mhi_controller_config modem_quectel_em1xx_config = { |
| 291 | .max_channels = 128, |
| 292 | .timeout_ms = 20000, |
| 293 | .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels), |
| 294 | .ch_cfg = mhi_quectel_em1xx_channels, |
| 295 | .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events), |
| 296 | .event_cfg = mhi_quectel_em1xx_events, |
| 297 | }; |
| 298 | |
| 299 | static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = { |
| 300 | .name = "quectel-em1xx", |
| 301 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 302 | .config = &modem_quectel_em1xx_config, |
| 303 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 304 | .dma_data_width = 32 |
| 305 | }; |
| 306 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 307 | static const struct pci_device_id mhi_pci_id_table[] = { |
| 308 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306), |
| 309 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info }, |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 310 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304), |
| 311 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info }, |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 312 | { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */ |
| 313 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
| 314 | { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */ |
| 315 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 316 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308), |
| 317 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info }, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 318 | { } |
| 319 | }; |
| 320 | MODULE_DEVICE_TABLE(pci, mhi_pci_id_table); |
| 321 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 322 | enum mhi_pci_device_status { |
| 323 | MHI_PCI_DEV_STARTED, |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 324 | MHI_PCI_DEV_SUSPENDED, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | struct mhi_pci_device { |
| 328 | struct mhi_controller mhi_cntrl; |
| 329 | struct pci_saved_state *pci_state; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 330 | struct work_struct recovery_work; |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 331 | struct timer_list health_check_timer; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 332 | unsigned long status; |
| 333 | }; |
| 334 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 335 | static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl, |
| 336 | void __iomem *addr, u32 *out) |
| 337 | { |
| 338 | *out = readl(addr); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl, |
| 343 | void __iomem *addr, u32 val) |
| 344 | { |
| 345 | writel(val, addr); |
| 346 | } |
| 347 | |
| 348 | static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl, |
| 349 | enum mhi_callback cb) |
| 350 | { |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 351 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 352 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 353 | /* Nothing to do for now */ |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 354 | switch (cb) { |
| 355 | case MHI_CB_FATAL_ERROR: |
| 356 | case MHI_CB_SYS_ERROR: |
| 357 | dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 358 | pm_runtime_forbid(&pdev->dev); |
| 359 | break; |
| 360 | case MHI_CB_EE_MISSION_MODE: |
| 361 | pm_runtime_allow(&pdev->dev); |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 362 | break; |
| 363 | default: |
| 364 | break; |
| 365 | } |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Loic Poulain | e3e5e650 | 2021-03-05 20:16:46 +0100 | [diff] [blame] | 368 | static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force) |
| 369 | { |
| 370 | /* no-op */ |
| 371 | } |
| 372 | |
| 373 | static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override) |
| 374 | { |
| 375 | /* no-op */ |
| 376 | } |
| 377 | |
| 378 | static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl) |
| 379 | { |
| 380 | /* no-op */ |
| 381 | } |
| 382 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 383 | static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl) |
| 384 | { |
| 385 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 386 | u16 vendor = 0; |
| 387 | |
| 388 | if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor)) |
| 389 | return false; |
| 390 | |
| 391 | if (vendor == (u16) ~0 || vendor == 0) |
| 392 | return false; |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 397 | static int mhi_pci_claim(struct mhi_controller *mhi_cntrl, |
| 398 | unsigned int bar_num, u64 dma_mask) |
| 399 | { |
| 400 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 401 | int err; |
| 402 | |
| 403 | err = pci_assign_resource(pdev, bar_num); |
| 404 | if (err) |
| 405 | return err; |
| 406 | |
| 407 | err = pcim_enable_device(pdev); |
| 408 | if (err) { |
| 409 | dev_err(&pdev->dev, "failed to enable pci device: %d\n", err); |
| 410 | return err; |
| 411 | } |
| 412 | |
| 413 | err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev)); |
| 414 | if (err) { |
| 415 | dev_err(&pdev->dev, "failed to map pci region: %d\n", err); |
| 416 | return err; |
| 417 | } |
| 418 | mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num]; |
| 419 | |
| 420 | err = pci_set_dma_mask(pdev, dma_mask); |
| 421 | if (err) { |
| 422 | dev_err(&pdev->dev, "Cannot set proper DMA mask\n"); |
| 423 | return err; |
| 424 | } |
| 425 | |
| 426 | err = pci_set_consistent_dma_mask(pdev, dma_mask); |
| 427 | if (err) { |
| 428 | dev_err(&pdev->dev, "set consistent dma mask failed\n"); |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | pci_set_master(pdev); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl, |
| 438 | const struct mhi_controller_config *mhi_cntrl_config) |
| 439 | { |
| 440 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 441 | int nr_vectors, i; |
| 442 | int *irq; |
| 443 | |
| 444 | /* |
| 445 | * Alloc one MSI vector for BHI + one vector per event ring, ideally... |
| 446 | * No explicit pci_free_irq_vectors required, done by pcim_release. |
| 447 | */ |
| 448 | mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events; |
| 449 | |
| 450 | nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI); |
| 451 | if (nr_vectors < 0) { |
| 452 | dev_err(&pdev->dev, "Error allocating MSI vectors %d\n", |
| 453 | nr_vectors); |
| 454 | return nr_vectors; |
| 455 | } |
| 456 | |
| 457 | if (nr_vectors < mhi_cntrl->nr_irqs) { |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 458 | dev_warn(&pdev->dev, "using shared MSI\n"); |
| 459 | |
| 460 | /* Patch msi vectors, use only one (shared) */ |
| 461 | for (i = 0; i < mhi_cntrl_config->num_events; i++) |
| 462 | mhi_cntrl_config->event_cfg[i].irq = 0; |
| 463 | mhi_cntrl->nr_irqs = 1; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL); |
| 467 | if (!irq) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | for (i = 0; i < mhi_cntrl->nr_irqs; i++) { |
| 471 | int vector = i >= nr_vectors ? (nr_vectors - 1) : i; |
| 472 | |
| 473 | irq[i] = pci_irq_vector(pdev, vector); |
| 474 | } |
| 475 | |
| 476 | mhi_cntrl->irq = irq; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl) |
| 482 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 483 | /* The runtime_get() MHI callback means: |
| 484 | * Do whatever is requested to leave M3. |
| 485 | */ |
| 486 | return pm_runtime_get(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl) |
| 490 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 491 | /* The runtime_put() MHI callback means: |
| 492 | * Device can be moved in M3 state. |
| 493 | */ |
| 494 | pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev); |
| 495 | pm_runtime_put(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 498 | static void mhi_pci_recovery_work(struct work_struct *work) |
| 499 | { |
| 500 | struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device, |
| 501 | recovery_work); |
| 502 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 503 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 504 | int err; |
| 505 | |
| 506 | dev_warn(&pdev->dev, "device recovery started\n"); |
| 507 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 508 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 509 | pm_runtime_forbid(&pdev->dev); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 510 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 511 | /* Clean up MHI state */ |
| 512 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 513 | mhi_power_down(mhi_cntrl, false); |
| 514 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 515 | } |
| 516 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 517 | pci_set_power_state(pdev, PCI_D0); |
| 518 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 519 | pci_restore_state(pdev); |
| 520 | |
| 521 | if (!mhi_pci_is_alive(mhi_cntrl)) |
| 522 | goto err_try_reset; |
| 523 | |
| 524 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 525 | if (err) |
| 526 | goto err_try_reset; |
| 527 | |
| 528 | err = mhi_sync_power_up(mhi_cntrl); |
| 529 | if (err) |
| 530 | goto err_unprepare; |
| 531 | |
| 532 | dev_dbg(&pdev->dev, "Recovery completed\n"); |
| 533 | |
| 534 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 535 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 536 | return; |
| 537 | |
| 538 | err_unprepare: |
| 539 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 540 | err_try_reset: |
| 541 | if (pci_reset_function(pdev)) |
| 542 | dev_err(&pdev->dev, "Recovery failed\n"); |
| 543 | } |
| 544 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 545 | static void health_check(struct timer_list *t) |
| 546 | { |
| 547 | struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer); |
| 548 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 549 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 550 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 551 | test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 552 | return; |
| 553 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 554 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 555 | dev_err(mhi_cntrl->cntrl_dev, "Device died\n"); |
| 556 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | /* reschedule in two seconds */ |
| 561 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 562 | } |
| 563 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 564 | static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 565 | { |
| 566 | const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data; |
| 567 | const struct mhi_controller_config *mhi_cntrl_config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 568 | struct mhi_pci_device *mhi_pdev; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 569 | struct mhi_controller *mhi_cntrl; |
| 570 | int err; |
| 571 | |
| 572 | dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name); |
| 573 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 574 | /* mhi_pdev.mhi_cntrl must be zero-initialized */ |
| 575 | mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL); |
| 576 | if (!mhi_pdev) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 577 | return -ENOMEM; |
| 578 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 579 | INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 580 | timer_setup(&mhi_pdev->health_check_timer, health_check, 0); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 581 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 582 | mhi_cntrl_config = info->config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 583 | mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 584 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 585 | mhi_cntrl->cntrl_dev = &pdev->dev; |
| 586 | mhi_cntrl->iova_start = 0; |
Loic Poulain | 4ea6fa2 | 2020-12-02 09:12:30 +0100 | [diff] [blame] | 587 | mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 588 | mhi_cntrl->fw_image = info->fw; |
| 589 | mhi_cntrl->edl_image = info->edl; |
| 590 | |
| 591 | mhi_cntrl->read_reg = mhi_pci_read_reg; |
| 592 | mhi_cntrl->write_reg = mhi_pci_write_reg; |
| 593 | mhi_cntrl->status_cb = mhi_pci_status_cb; |
| 594 | mhi_cntrl->runtime_get = mhi_pci_runtime_get; |
| 595 | mhi_cntrl->runtime_put = mhi_pci_runtime_put; |
Loic Poulain | e3e5e650 | 2021-03-05 20:16:46 +0100 | [diff] [blame] | 596 | mhi_cntrl->wake_get = mhi_pci_wake_get_nop; |
| 597 | mhi_cntrl->wake_put = mhi_pci_wake_put_nop; |
| 598 | mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 599 | |
| 600 | err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width)); |
| 601 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 602 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 603 | |
| 604 | err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config); |
| 605 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 606 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 607 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 608 | pci_set_drvdata(pdev, mhi_pdev); |
| 609 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 610 | /* Have stored pci confspace at hand for restore in sudden PCI error. |
| 611 | * cache the state locally and discard the PCI core one. |
| 612 | */ |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 613 | pci_save_state(pdev); |
| 614 | mhi_pdev->pci_state = pci_store_saved_state(pdev); |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 615 | pci_load_saved_state(pdev, NULL); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 616 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 617 | pci_enable_pcie_error_reporting(pdev); |
| 618 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 619 | err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config); |
| 620 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 621 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 622 | |
| 623 | /* MHI bus does not power up the controller by default */ |
| 624 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 625 | if (err) { |
| 626 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 627 | goto err_unregister; |
| 628 | } |
| 629 | |
| 630 | err = mhi_sync_power_up(mhi_cntrl); |
| 631 | if (err) { |
| 632 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 633 | goto err_unprepare; |
| 634 | } |
| 635 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 636 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
| 637 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 638 | /* start health check */ |
| 639 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 640 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 641 | /* Only allow runtime-suspend if PME capable (for wakeup) */ |
| 642 | if (pci_pme_capable(pdev, PCI_D3hot)) { |
| 643 | pm_runtime_set_autosuspend_delay(&pdev->dev, 2000); |
| 644 | pm_runtime_use_autosuspend(&pdev->dev); |
| 645 | pm_runtime_mark_last_busy(&pdev->dev); |
| 646 | pm_runtime_put_noidle(&pdev->dev); |
| 647 | } |
| 648 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 649 | return 0; |
| 650 | |
| 651 | err_unprepare: |
| 652 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 653 | err_unregister: |
| 654 | mhi_unregister_controller(mhi_cntrl); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 655 | |
| 656 | return err; |
| 657 | } |
| 658 | |
| 659 | static void mhi_pci_remove(struct pci_dev *pdev) |
| 660 | { |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 661 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 662 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 663 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 664 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 665 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 666 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 667 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 668 | mhi_power_down(mhi_cntrl, true); |
| 669 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 670 | } |
| 671 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 672 | /* balancing probe put_noidle */ |
| 673 | if (pci_pme_capable(pdev, PCI_D3hot)) |
| 674 | pm_runtime_get_noresume(&pdev->dev); |
| 675 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 676 | mhi_unregister_controller(mhi_cntrl); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 679 | static void mhi_pci_shutdown(struct pci_dev *pdev) |
| 680 | { |
| 681 | mhi_pci_remove(pdev); |
| 682 | pci_set_power_state(pdev, PCI_D3hot); |
| 683 | } |
| 684 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 685 | static void mhi_pci_reset_prepare(struct pci_dev *pdev) |
| 686 | { |
| 687 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 688 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 689 | |
| 690 | dev_info(&pdev->dev, "reset\n"); |
| 691 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 692 | del_timer(&mhi_pdev->health_check_timer); |
| 693 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 694 | /* Clean up MHI state */ |
| 695 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 696 | mhi_power_down(mhi_cntrl, false); |
| 697 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 698 | } |
| 699 | |
| 700 | /* cause internal device reset */ |
| 701 | mhi_soc_reset(mhi_cntrl); |
| 702 | |
| 703 | /* Be sure device reset has been executed */ |
| 704 | msleep(MHI_POST_RESET_DELAY_MS); |
| 705 | } |
| 706 | |
| 707 | static void mhi_pci_reset_done(struct pci_dev *pdev) |
| 708 | { |
| 709 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 710 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 711 | int err; |
| 712 | |
| 713 | /* Restore initial known working PCI state */ |
| 714 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 715 | pci_restore_state(pdev); |
| 716 | |
| 717 | /* Is device status available ? */ |
| 718 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 719 | dev_err(&pdev->dev, "reset failed\n"); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 724 | if (err) { |
| 725 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | err = mhi_sync_power_up(mhi_cntrl); |
| 730 | if (err) { |
| 731 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 732 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 737 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 740 | static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev, |
| 741 | pci_channel_state_t state) |
| 742 | { |
| 743 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 744 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 745 | |
| 746 | dev_err(&pdev->dev, "PCI error detected, state = %u\n", state); |
| 747 | |
| 748 | if (state == pci_channel_io_perm_failure) |
| 749 | return PCI_ERS_RESULT_DISCONNECT; |
| 750 | |
| 751 | /* Clean up MHI state */ |
| 752 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 753 | mhi_power_down(mhi_cntrl, false); |
| 754 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 755 | } else { |
| 756 | /* Nothing to do */ |
| 757 | return PCI_ERS_RESULT_RECOVERED; |
| 758 | } |
| 759 | |
| 760 | pci_disable_device(pdev); |
| 761 | |
| 762 | return PCI_ERS_RESULT_NEED_RESET; |
| 763 | } |
| 764 | |
| 765 | static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev) |
| 766 | { |
| 767 | if (pci_enable_device(pdev)) { |
| 768 | dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n"); |
| 769 | return PCI_ERS_RESULT_DISCONNECT; |
| 770 | } |
| 771 | |
| 772 | return PCI_ERS_RESULT_RECOVERED; |
| 773 | } |
| 774 | |
| 775 | static void mhi_pci_io_resume(struct pci_dev *pdev) |
| 776 | { |
| 777 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 778 | |
| 779 | dev_err(&pdev->dev, "PCI slot reset done\n"); |
| 780 | |
| 781 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 782 | } |
| 783 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 784 | static const struct pci_error_handlers mhi_pci_err_handler = { |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 785 | .error_detected = mhi_pci_error_detected, |
| 786 | .slot_reset = mhi_pci_slot_reset, |
| 787 | .resume = mhi_pci_io_resume, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 788 | .reset_prepare = mhi_pci_reset_prepare, |
| 789 | .reset_done = mhi_pci_reset_done, |
| 790 | }; |
| 791 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 792 | static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 793 | { |
| 794 | struct pci_dev *pdev = to_pci_dev(dev); |
| 795 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 796 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 797 | int err; |
| 798 | |
| 799 | if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 800 | return 0; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 801 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 802 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 803 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 804 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 805 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 806 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 807 | goto pci_suspend; /* Nothing to do at MHI level */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 808 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 809 | /* Transition to M3 state */ |
| 810 | err = mhi_pm_suspend(mhi_cntrl); |
| 811 | if (err) { |
| 812 | dev_err(&pdev->dev, "failed to suspend device: %d\n", err); |
| 813 | clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status); |
| 814 | return -EBUSY; |
| 815 | } |
| 816 | |
| 817 | pci_suspend: |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 818 | pci_disable_device(pdev); |
| 819 | pci_wake_from_d3(pdev, true); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 824 | static int __maybe_unused mhi_pci_runtime_resume(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 825 | { |
| 826 | struct pci_dev *pdev = to_pci_dev(dev); |
| 827 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 828 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 829 | int err; |
| 830 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 831 | if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 832 | return 0; |
| 833 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 834 | err = pci_enable_device(pdev); |
| 835 | if (err) |
| 836 | goto err_recovery; |
| 837 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 838 | pci_set_master(pdev); |
| 839 | pci_wake_from_d3(pdev, false); |
| 840 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 841 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 842 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 843 | return 0; /* Nothing to do at MHI level */ |
| 844 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 845 | /* Exit M3, transition to M0 state */ |
| 846 | err = mhi_pm_resume(mhi_cntrl); |
| 847 | if (err) { |
| 848 | dev_err(&pdev->dev, "failed to resume device: %d\n", err); |
| 849 | goto err_recovery; |
| 850 | } |
| 851 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 852 | /* Resume health check */ |
| 853 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 854 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 855 | /* It can be a remote wakeup (no mhi runtime_get), update access time */ |
| 856 | pm_runtime_mark_last_busy(dev); |
| 857 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 858 | return 0; |
| 859 | |
| 860 | err_recovery: |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 861 | /* Do not fail to not mess up our PCI device state, the device likely |
| 862 | * lost power (d3cold) and we simply need to reset it from the recovery |
| 863 | * procedure, trigger the recovery asynchronously to prevent system |
| 864 | * suspend exit delaying. |
| 865 | */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 866 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 867 | pm_runtime_mark_last_busy(dev); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 868 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static int __maybe_unused mhi_pci_suspend(struct device *dev) |
| 873 | { |
| 874 | pm_runtime_disable(dev); |
| 875 | return mhi_pci_runtime_suspend(dev); |
| 876 | } |
| 877 | |
| 878 | static int __maybe_unused mhi_pci_resume(struct device *dev) |
| 879 | { |
| 880 | int ret; |
| 881 | |
| 882 | /* Depending the platform, device may have lost power (d3cold), we need |
| 883 | * to resume it now to check its state and recover when necessary. |
| 884 | */ |
| 885 | ret = mhi_pci_runtime_resume(dev); |
| 886 | pm_runtime_enable(dev); |
| 887 | |
| 888 | return ret; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static const struct dev_pm_ops mhi_pci_pm_ops = { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 892 | SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 893 | SET_SYSTEM_SLEEP_PM_OPS(mhi_pci_suspend, mhi_pci_resume) |
| 894 | }; |
| 895 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 896 | static struct pci_driver mhi_pci_driver = { |
| 897 | .name = "mhi-pci-generic", |
| 898 | .id_table = mhi_pci_id_table, |
| 899 | .probe = mhi_pci_probe, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 900 | .remove = mhi_pci_remove, |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 901 | .shutdown = mhi_pci_shutdown, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 902 | .err_handler = &mhi_pci_err_handler, |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 903 | .driver.pm = &mhi_pci_pm_ops |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 904 | }; |
| 905 | module_pci_driver(mhi_pci_driver); |
| 906 | |
| 907 | MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); |
| 908 | MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver"); |
| 909 | MODULE_LICENSE("GPL"); |