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 | |
Slark Xiao | e2022cb | 2021-11-26 16:19:51 +0530 | [diff] [blame] | 23 | #define MHI_POST_RESET_DELAY_MS 2000 |
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) |
Richard Laing | 5c2c853 | 2021-07-15 09:18:05 +1200 | [diff] [blame] | 35 | * @mru_default: default MRU size for MBIM network packets |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 36 | * @sideband_wake: Devices using dedicated sideband GPIO for wakeup instead |
| 37 | * of inband wake support (such as sdx24) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 38 | */ |
| 39 | struct mhi_pci_dev_info { |
| 40 | const struct mhi_controller_config *config; |
| 41 | const char *name; |
| 42 | const char *fw; |
| 43 | const char *edl; |
| 44 | unsigned int bar_num; |
| 45 | unsigned int dma_data_width; |
Richard Laing | 5c2c853 | 2021-07-15 09:18:05 +1200 | [diff] [blame] | 46 | unsigned int mru_default; |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 47 | bool sideband_wake; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | #define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 51 | { \ |
| 52 | .num = ch_num, \ |
| 53 | .name = ch_name, \ |
| 54 | .num_elements = el_count, \ |
| 55 | .event_ring = ev_ring, \ |
| 56 | .dir = DMA_TO_DEVICE, \ |
| 57 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 58 | .pollcfg = 0, \ |
| 59 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 60 | .lpm_notify = false, \ |
| 61 | .offload_channel = false, \ |
| 62 | .doorbell_mode_switch = false, \ |
| 63 | } \ |
| 64 | |
| 65 | #define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 66 | { \ |
| 67 | .num = ch_num, \ |
| 68 | .name = ch_name, \ |
| 69 | .num_elements = el_count, \ |
| 70 | .event_ring = ev_ring, \ |
| 71 | .dir = DMA_FROM_DEVICE, \ |
| 72 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 73 | .pollcfg = 0, \ |
| 74 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 75 | .lpm_notify = false, \ |
| 76 | .offload_channel = false, \ |
| 77 | .doorbell_mode_switch = false, \ |
| 78 | } |
| 79 | |
Loic Poulain | b8a97f2 | 2021-07-16 13:21:06 +0530 | [diff] [blame] | 80 | #define MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(ch_num, ch_name, el_count, ev_ring) \ |
| 81 | { \ |
| 82 | .num = ch_num, \ |
| 83 | .name = ch_name, \ |
| 84 | .num_elements = el_count, \ |
| 85 | .event_ring = ev_ring, \ |
| 86 | .dir = DMA_FROM_DEVICE, \ |
| 87 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 88 | .pollcfg = 0, \ |
| 89 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 90 | .lpm_notify = false, \ |
| 91 | .offload_channel = false, \ |
| 92 | .doorbell_mode_switch = false, \ |
| 93 | .auto_queue = true, \ |
| 94 | } |
| 95 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 96 | #define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 97 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 98 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 99 | .irq_moderation_ms = 0, \ |
| 100 | .irq = (ev_ring) + 1, \ |
| 101 | .priority = 1, \ |
| 102 | .mode = MHI_DB_BRST_DISABLE, \ |
| 103 | .data_type = MHI_ER_CTRL, \ |
| 104 | .hardware_event = false, \ |
| 105 | .client_managed = false, \ |
| 106 | .offload_channel = false, \ |
| 107 | } |
| 108 | |
Loic Poulain | eb96787 | 2021-01-04 17:14:52 +0100 | [diff] [blame] | 109 | #define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \ |
| 110 | { \ |
| 111 | .num = ch_num, \ |
| 112 | .name = ch_name, \ |
| 113 | .num_elements = el_count, \ |
| 114 | .event_ring = ev_ring, \ |
| 115 | .dir = DMA_TO_DEVICE, \ |
| 116 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 117 | .pollcfg = 0, \ |
| 118 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 119 | .lpm_notify = false, \ |
| 120 | .offload_channel = false, \ |
| 121 | .doorbell_mode_switch = true, \ |
| 122 | } \ |
| 123 | |
| 124 | #define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \ |
| 125 | { \ |
| 126 | .num = ch_num, \ |
| 127 | .name = ch_name, \ |
| 128 | .num_elements = el_count, \ |
| 129 | .event_ring = ev_ring, \ |
| 130 | .dir = DMA_FROM_DEVICE, \ |
| 131 | .ee_mask = BIT(MHI_EE_AMSS), \ |
| 132 | .pollcfg = 0, \ |
| 133 | .doorbell = MHI_DB_BRST_ENABLE, \ |
| 134 | .lpm_notify = false, \ |
| 135 | .offload_channel = false, \ |
| 136 | .doorbell_mode_switch = true, \ |
| 137 | } |
| 138 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 139 | #define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 140 | { \ |
| 141 | .num = ch_num, \ |
| 142 | .name = ch_name, \ |
| 143 | .num_elements = el_count, \ |
| 144 | .event_ring = ev_ring, \ |
| 145 | .dir = DMA_TO_DEVICE, \ |
| 146 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 147 | .pollcfg = 0, \ |
| 148 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 149 | .lpm_notify = false, \ |
| 150 | .offload_channel = false, \ |
| 151 | .doorbell_mode_switch = false, \ |
| 152 | } \ |
| 153 | |
| 154 | #define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \ |
| 155 | { \ |
| 156 | .num = ch_num, \ |
| 157 | .name = ch_name, \ |
| 158 | .num_elements = el_count, \ |
| 159 | .event_ring = ev_ring, \ |
| 160 | .dir = DMA_FROM_DEVICE, \ |
| 161 | .ee_mask = BIT(MHI_EE_SBL), \ |
| 162 | .pollcfg = 0, \ |
| 163 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 164 | .lpm_notify = false, \ |
| 165 | .offload_channel = false, \ |
| 166 | .doorbell_mode_switch = false, \ |
| 167 | } |
| 168 | |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 169 | #define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 170 | { \ |
| 171 | .num = ch_num, \ |
| 172 | .name = ch_name, \ |
| 173 | .num_elements = el_count, \ |
| 174 | .event_ring = ev_ring, \ |
| 175 | .dir = DMA_TO_DEVICE, \ |
| 176 | .ee_mask = BIT(MHI_EE_FP), \ |
| 177 | .pollcfg = 0, \ |
| 178 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 179 | .lpm_notify = false, \ |
| 180 | .offload_channel = false, \ |
| 181 | .doorbell_mode_switch = false, \ |
| 182 | } \ |
| 183 | |
| 184 | #define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \ |
| 185 | { \ |
| 186 | .num = ch_num, \ |
| 187 | .name = ch_name, \ |
| 188 | .num_elements = el_count, \ |
| 189 | .event_ring = ev_ring, \ |
| 190 | .dir = DMA_FROM_DEVICE, \ |
| 191 | .ee_mask = BIT(MHI_EE_FP), \ |
| 192 | .pollcfg = 0, \ |
| 193 | .doorbell = MHI_DB_BRST_DISABLE, \ |
| 194 | .lpm_notify = false, \ |
| 195 | .offload_channel = false, \ |
| 196 | .doorbell_mode_switch = false, \ |
| 197 | } |
| 198 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 199 | #define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 200 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 201 | .num_elements = el_count, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 202 | .irq_moderation_ms = 5, \ |
| 203 | .irq = (ev_ring) + 1, \ |
| 204 | .priority = 1, \ |
| 205 | .mode = MHI_DB_BRST_DISABLE, \ |
| 206 | .data_type = MHI_ER_DATA, \ |
| 207 | .hardware_event = false, \ |
| 208 | .client_managed = false, \ |
| 209 | .offload_channel = false, \ |
| 210 | } |
| 211 | |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 212 | #define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 213 | { \ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 214 | .num_elements = el_count, \ |
Loic Poulain | ec751369 | 2021-01-04 17:14:59 +0100 | [diff] [blame] | 215 | .irq_moderation_ms = 1, \ |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 216 | .irq = (ev_ring) + 1, \ |
| 217 | .priority = 1, \ |
| 218 | .mode = MHI_DB_BRST_DISABLE, \ |
| 219 | .data_type = MHI_ER_DATA, \ |
| 220 | .hardware_event = true, \ |
| 221 | .client_managed = false, \ |
| 222 | .offload_channel = false, \ |
| 223 | .channel = ch_num, \ |
| 224 | } |
| 225 | |
| 226 | static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = { |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 227 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1), |
| 228 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 229 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0), |
| 230 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0), |
| 231 | MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0), |
| 232 | MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0), |
| 233 | MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0), |
Loic Poulain | b8a97f2 | 2021-07-16 13:21:06 +0530 | [diff] [blame] | 234 | MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 8, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 235 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 236 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 237 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2), |
| 238 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 239 | }; |
| 240 | |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 241 | static struct mhi_event_config modem_qcom_v1_mhi_events[] = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 242 | /* first ring is control+data ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 243 | MHI_EVENT_CONFIG_CTRL(0, 64), |
Loic Poulain | 4da3d07 | 2021-01-04 17:14:58 +0100 | [diff] [blame] | 244 | /* DIAG dedicated event ring */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 245 | MHI_EVENT_CONFIG_DATA(1, 128), |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 246 | /* Hardware channels request dedicated hardware event rings */ |
Loic Poulain | 9ea48ef | 2021-03-05 20:16:43 +0100 | [diff] [blame] | 247 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 248 | MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 249 | }; |
| 250 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame] | 251 | static const struct mhi_controller_config modem_qcom_v1_mhiv_config = { |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 252 | .max_channels = 128, |
Loic Poulain | 84026a5 | 2021-01-04 17:14:57 +0100 | [diff] [blame] | 253 | .timeout_ms = 8000, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 254 | .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels), |
| 255 | .ch_cfg = modem_qcom_v1_mhi_channels, |
| 256 | .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events), |
| 257 | .event_cfg = modem_qcom_v1_mhi_events, |
| 258 | }; |
| 259 | |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 260 | static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = { |
| 261 | .name = "qcom-sdx65m", |
| 262 | .fw = "qcom/sdx65m/xbl.elf", |
| 263 | .edl = "qcom/sdx65m/edl.mbn", |
| 264 | .config = &modem_qcom_v1_mhiv_config, |
| 265 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 266 | .dma_data_width = 32, |
| 267 | .sideband_wake = false, |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 270 | static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = { |
| 271 | .name = "qcom-sdx55m", |
| 272 | .fw = "qcom/sdx55m/sbl1.mbn", |
| 273 | .edl = "qcom/sdx55m/edl.mbn", |
| 274 | .config = &modem_qcom_v1_mhiv_config, |
| 275 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
Richard Laing | 5c2c853 | 2021-07-15 09:18:05 +1200 | [diff] [blame] | 276 | .dma_data_width = 32, |
Jakub Kicinski | d2e11fd | 2021-07-31 09:14:46 -0700 | [diff] [blame] | 277 | .mru_default = 32768, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 278 | .sideband_wake = false, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 279 | }; |
| 280 | |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 281 | static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = { |
| 282 | .name = "qcom-sdx24", |
| 283 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 284 | .config = &modem_qcom_v1_mhiv_config, |
| 285 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 286 | .dma_data_width = 32, |
| 287 | .sideband_wake = true, |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 288 | }; |
| 289 | |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 290 | static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = { |
| 291 | MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0), |
| 292 | MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0), |
| 293 | MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0), |
| 294 | MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0), |
| 295 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1), |
| 296 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1), |
| 297 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0), |
| 298 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0), |
| 299 | MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0), |
| 300 | MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0), |
Loic Poulain | 1113439 | 2021-04-07 10:41:00 +0200 | [diff] [blame] | 301 | /* The EDL firmware is a flash-programmer exposing firehose protocol */ |
| 302 | MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0), |
| 303 | MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0), |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 304 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2), |
| 305 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3), |
| 306 | }; |
| 307 | |
| 308 | static struct mhi_event_config mhi_quectel_em1xx_events[] = { |
| 309 | MHI_EVENT_CONFIG_CTRL(0, 128), |
| 310 | MHI_EVENT_CONFIG_DATA(1, 128), |
| 311 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 312 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101) |
| 313 | }; |
| 314 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame] | 315 | static const struct mhi_controller_config modem_quectel_em1xx_config = { |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 316 | .max_channels = 128, |
| 317 | .timeout_ms = 20000, |
| 318 | .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels), |
| 319 | .ch_cfg = mhi_quectel_em1xx_channels, |
| 320 | .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events), |
| 321 | .event_cfg = mhi_quectel_em1xx_events, |
| 322 | }; |
| 323 | |
| 324 | static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = { |
| 325 | .name = "quectel-em1xx", |
| 326 | .edl = "qcom/prog_firehose_sdx24.mbn", |
| 327 | .config = &modem_quectel_em1xx_config, |
| 328 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 329 | .dma_data_width = 32, |
| 330 | .sideband_wake = true, |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 331 | }; |
| 332 | |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 333 | static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = { |
| 334 | MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0), |
| 335 | MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0), |
| 336 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1), |
| 337 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1), |
| 338 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0), |
| 339 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0), |
Jarvis Jiang | c7711c2 | 2021-06-06 21:07:39 +0530 | [diff] [blame] | 340 | MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0), |
| 341 | MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0), |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 342 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2), |
| 343 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3), |
| 344 | }; |
| 345 | |
| 346 | static struct mhi_event_config mhi_foxconn_sdx55_events[] = { |
| 347 | MHI_EVENT_CONFIG_CTRL(0, 128), |
| 348 | MHI_EVENT_CONFIG_DATA(1, 128), |
| 349 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 350 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101) |
| 351 | }; |
| 352 | |
Manivannan Sadhasivam | 5630c10 | 2021-04-08 19:15:29 +0530 | [diff] [blame] | 353 | static const struct mhi_controller_config modem_foxconn_sdx55_config = { |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 354 | .max_channels = 128, |
| 355 | .timeout_ms = 20000, |
| 356 | .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels), |
| 357 | .ch_cfg = mhi_foxconn_sdx55_channels, |
| 358 | .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events), |
| 359 | .event_cfg = mhi_foxconn_sdx55_events, |
| 360 | }; |
| 361 | |
| 362 | static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = { |
| 363 | .name = "foxconn-sdx55", |
| 364 | .fw = "qcom/sdx55m/sbl1.mbn", |
| 365 | .edl = "qcom/sdx55m/edl.mbn", |
| 366 | .config = &modem_foxconn_sdx55_config, |
| 367 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 368 | .dma_data_width = 32, |
Slark Xiao | a0572cea | 2022-02-05 19:27:30 +0530 | [diff] [blame] | 369 | .mru_default = 32768, |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 370 | .sideband_wake = false, |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
ULRICH Thomas | 87693e0 | 2021-08-02 10:42:47 +0530 | [diff] [blame] | 373 | static const struct mhi_channel_config mhi_mv31_channels[] = { |
| 374 | MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 64, 0), |
| 375 | MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 64, 0), |
| 376 | /* MBIM Control Channel */ |
| 377 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 64, 0), |
| 378 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 64, 0), |
| 379 | /* MBIM Data Channel */ |
| 380 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 512, 2), |
| 381 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 512, 3), |
| 382 | }; |
| 383 | |
| 384 | static struct mhi_event_config mhi_mv31_events[] = { |
| 385 | MHI_EVENT_CONFIG_CTRL(0, 256), |
| 386 | MHI_EVENT_CONFIG_DATA(1, 256), |
| 387 | MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100), |
| 388 | MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101), |
| 389 | }; |
| 390 | |
| 391 | static const struct mhi_controller_config modem_mv31_config = { |
| 392 | .max_channels = 128, |
| 393 | .timeout_ms = 20000, |
| 394 | .num_channels = ARRAY_SIZE(mhi_mv31_channels), |
| 395 | .ch_cfg = mhi_mv31_channels, |
| 396 | .num_events = ARRAY_SIZE(mhi_mv31_events), |
| 397 | .event_cfg = mhi_mv31_events, |
| 398 | }; |
| 399 | |
| 400 | static const struct mhi_pci_dev_info mhi_mv31_info = { |
| 401 | .name = "cinterion-mv31", |
| 402 | .config = &modem_mv31_config, |
| 403 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 404 | .dma_data_width = 32, |
Slark Xiao | 05daa80 | 2022-02-05 19:27:31 +0530 | [diff] [blame^] | 405 | .mru_default = 32768, |
ULRICH Thomas | 87693e0 | 2021-08-02 10:42:47 +0530 | [diff] [blame] | 406 | }; |
| 407 | |
Thomas Perrot | 1dba007 | 2021-12-16 13:42:27 +0530 | [diff] [blame] | 408 | static const struct mhi_channel_config mhi_sierra_em919x_channels[] = { |
| 409 | MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0), |
| 410 | MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 256, 0), |
| 411 | MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 0), |
| 412 | MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 0), |
| 413 | MHI_CHANNEL_CONFIG_UL(12, "MBIM", 128, 0), |
| 414 | MHI_CHANNEL_CONFIG_DL(13, "MBIM", 128, 0), |
| 415 | MHI_CHANNEL_CONFIG_UL(14, "QMI", 32, 0), |
| 416 | MHI_CHANNEL_CONFIG_DL(15, "QMI", 32, 0), |
| 417 | MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0), |
| 418 | MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0), |
| 419 | MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 512, 1), |
| 420 | MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 512, 2), |
| 421 | }; |
| 422 | |
| 423 | static struct mhi_event_config modem_sierra_em919x_mhi_events[] = { |
| 424 | /* first ring is control+data and DIAG ring */ |
| 425 | MHI_EVENT_CONFIG_CTRL(0, 2048), |
| 426 | /* Hardware channels request dedicated hardware event rings */ |
| 427 | MHI_EVENT_CONFIG_HW_DATA(1, 2048, 100), |
| 428 | MHI_EVENT_CONFIG_HW_DATA(2, 2048, 101) |
| 429 | }; |
| 430 | |
| 431 | static const struct mhi_controller_config modem_sierra_em919x_config = { |
| 432 | .max_channels = 128, |
| 433 | .timeout_ms = 24000, |
| 434 | .num_channels = ARRAY_SIZE(mhi_sierra_em919x_channels), |
| 435 | .ch_cfg = mhi_sierra_em919x_channels, |
| 436 | .num_events = ARRAY_SIZE(modem_sierra_em919x_mhi_events), |
| 437 | .event_cfg = modem_sierra_em919x_mhi_events, |
| 438 | }; |
| 439 | |
| 440 | static const struct mhi_pci_dev_info mhi_sierra_em919x_info = { |
| 441 | .name = "sierra-em919x", |
| 442 | .config = &modem_sierra_em919x_config, |
| 443 | .bar_num = MHI_PCI_DEFAULT_BAR_NUM, |
| 444 | .dma_data_width = 32, |
| 445 | .sideband_wake = false, |
| 446 | }; |
| 447 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 448 | static const struct pci_device_id mhi_pci_id_table[] = { |
Thomas Perrot | 1dba007 | 2021-12-16 13:42:27 +0530 | [diff] [blame] | 449 | /* EM919x (sdx55), use the same vid:pid as qcom-sdx55m */ |
| 450 | { PCI_DEVICE_SUB(PCI_VENDOR_ID_QCOM, 0x0306, 0x18d7, 0x0200), |
| 451 | .driver_data = (kernel_ulong_t) &mhi_sierra_em919x_info }, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 452 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306), |
| 453 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info }, |
Loic Poulain | 59d05b7 | 2021-03-05 20:16:45 +0100 | [diff] [blame] | 454 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304), |
| 455 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info }, |
Loic Poulain | ac4bf60 | 2021-03-05 20:16:44 +0100 | [diff] [blame] | 456 | { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */ |
| 457 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
| 458 | { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */ |
| 459 | .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info }, |
Bhaumik Bhatt | 49d38eb | 2021-04-02 14:33:19 -0700 | [diff] [blame] | 460 | { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308), |
| 461 | .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info }, |
Jarvis Jiang | aac4265 | 2021-04-08 02:55:24 -0700 | [diff] [blame] | 462 | /* T99W175 (sdx55), Both for eSIM and Non-eSIM */ |
| 463 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab), |
| 464 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
| 465 | /* DW5930e (sdx55), With eSIM, It's also T99W175 */ |
| 466 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0), |
| 467 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
| 468 | /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */ |
| 469 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1), |
| 470 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
Slark Xiao | c9825e6 | 2021-12-16 13:42:18 +0530 | [diff] [blame] | 471 | /* T99W175 (sdx55), Based on Qualcomm new baseline */ |
| 472 | { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0bf), |
| 473 | .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info }, |
ULRICH Thomas | 87693e0 | 2021-08-02 10:42:47 +0530 | [diff] [blame] | 474 | /* MV31-W (Cinterion) */ |
| 475 | { PCI_DEVICE(0x1269, 0x00b3), |
| 476 | .driver_data = (kernel_ulong_t) &mhi_mv31_info }, |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 477 | { } |
| 478 | }; |
| 479 | MODULE_DEVICE_TABLE(pci, mhi_pci_id_table); |
| 480 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 481 | enum mhi_pci_device_status { |
| 482 | MHI_PCI_DEV_STARTED, |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 483 | MHI_PCI_DEV_SUSPENDED, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | struct mhi_pci_device { |
| 487 | struct mhi_controller mhi_cntrl; |
| 488 | struct pci_saved_state *pci_state; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 489 | struct work_struct recovery_work; |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 490 | struct timer_list health_check_timer; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 491 | unsigned long status; |
| 492 | }; |
| 493 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 494 | static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl, |
| 495 | void __iomem *addr, u32 *out) |
| 496 | { |
| 497 | *out = readl(addr); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl, |
| 502 | void __iomem *addr, u32 val) |
| 503 | { |
| 504 | writel(val, addr); |
| 505 | } |
| 506 | |
| 507 | static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl, |
| 508 | enum mhi_callback cb) |
| 509 | { |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 510 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 511 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 512 | /* Nothing to do for now */ |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 513 | switch (cb) { |
| 514 | case MHI_CB_FATAL_ERROR: |
| 515 | case MHI_CB_SYS_ERROR: |
| 516 | dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 517 | pm_runtime_forbid(&pdev->dev); |
| 518 | break; |
| 519 | case MHI_CB_EE_MISSION_MODE: |
| 520 | pm_runtime_allow(&pdev->dev); |
Loic Poulain | 1e2f29b | 2021-02-03 17:39:42 +0100 | [diff] [blame] | 521 | break; |
| 522 | default: |
| 523 | break; |
| 524 | } |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Loic Poulain | e3e5e650 | 2021-03-05 20:16:46 +0100 | [diff] [blame] | 527 | static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force) |
| 528 | { |
| 529 | /* no-op */ |
| 530 | } |
| 531 | |
| 532 | static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override) |
| 533 | { |
| 534 | /* no-op */ |
| 535 | } |
| 536 | |
| 537 | static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl) |
| 538 | { |
| 539 | /* no-op */ |
| 540 | } |
| 541 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 542 | static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl) |
| 543 | { |
| 544 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 545 | u16 vendor = 0; |
| 546 | |
| 547 | if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor)) |
| 548 | return false; |
| 549 | |
| 550 | if (vendor == (u16) ~0 || vendor == 0) |
| 551 | return false; |
| 552 | |
| 553 | return true; |
| 554 | } |
| 555 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 556 | static int mhi_pci_claim(struct mhi_controller *mhi_cntrl, |
| 557 | unsigned int bar_num, u64 dma_mask) |
| 558 | { |
| 559 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 560 | int err; |
| 561 | |
| 562 | err = pci_assign_resource(pdev, bar_num); |
| 563 | if (err) |
| 564 | return err; |
| 565 | |
| 566 | err = pcim_enable_device(pdev); |
| 567 | if (err) { |
| 568 | dev_err(&pdev->dev, "failed to enable pci device: %d\n", err); |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev)); |
| 573 | if (err) { |
| 574 | dev_err(&pdev->dev, "failed to map pci region: %d\n", err); |
| 575 | return err; |
| 576 | } |
| 577 | mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num]; |
Bhaumik Bhatt | 3551a30 | 2021-08-02 10:42:52 +0530 | [diff] [blame] | 578 | mhi_cntrl->reg_len = pci_resource_len(pdev, bar_num); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 579 | |
Christophe JAILLET | f3d1339 | 2021-12-16 13:42:22 +0530 | [diff] [blame] | 580 | err = dma_set_mask_and_coherent(&pdev->dev, dma_mask); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 581 | if (err) { |
| 582 | dev_err(&pdev->dev, "Cannot set proper DMA mask\n"); |
| 583 | return err; |
| 584 | } |
| 585 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 586 | pci_set_master(pdev); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl, |
| 592 | const struct mhi_controller_config *mhi_cntrl_config) |
| 593 | { |
| 594 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 595 | int nr_vectors, i; |
| 596 | int *irq; |
| 597 | |
| 598 | /* |
| 599 | * Alloc one MSI vector for BHI + one vector per event ring, ideally... |
| 600 | * No explicit pci_free_irq_vectors required, done by pcim_release. |
| 601 | */ |
| 602 | mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events; |
| 603 | |
| 604 | nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI); |
| 605 | if (nr_vectors < 0) { |
| 606 | dev_err(&pdev->dev, "Error allocating MSI vectors %d\n", |
| 607 | nr_vectors); |
| 608 | return nr_vectors; |
| 609 | } |
| 610 | |
| 611 | if (nr_vectors < mhi_cntrl->nr_irqs) { |
Loic Poulain | b91c3b3 | 2021-01-05 17:44:36 +0100 | [diff] [blame] | 612 | dev_warn(&pdev->dev, "using shared MSI\n"); |
| 613 | |
| 614 | /* Patch msi vectors, use only one (shared) */ |
| 615 | for (i = 0; i < mhi_cntrl_config->num_events; i++) |
| 616 | mhi_cntrl_config->event_cfg[i].irq = 0; |
| 617 | mhi_cntrl->nr_irqs = 1; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL); |
| 621 | if (!irq) |
| 622 | return -ENOMEM; |
| 623 | |
| 624 | for (i = 0; i < mhi_cntrl->nr_irqs; i++) { |
| 625 | int vector = i >= nr_vectors ? (nr_vectors - 1) : i; |
| 626 | |
| 627 | irq[i] = pci_irq_vector(pdev, vector); |
| 628 | } |
| 629 | |
| 630 | mhi_cntrl->irq = irq; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl) |
| 636 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 637 | /* The runtime_get() MHI callback means: |
| 638 | * Do whatever is requested to leave M3. |
| 639 | */ |
| 640 | return pm_runtime_get(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl) |
| 644 | { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 645 | /* The runtime_put() MHI callback means: |
| 646 | * Device can be moved in M3 state. |
| 647 | */ |
| 648 | pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev); |
| 649 | pm_runtime_put(mhi_cntrl->cntrl_dev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 652 | static void mhi_pci_recovery_work(struct work_struct *work) |
| 653 | { |
| 654 | struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device, |
| 655 | recovery_work); |
| 656 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 657 | struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); |
| 658 | int err; |
| 659 | |
| 660 | dev_warn(&pdev->dev, "device recovery started\n"); |
| 661 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 662 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 663 | pm_runtime_forbid(&pdev->dev); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 664 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 665 | /* Clean up MHI state */ |
| 666 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 667 | mhi_power_down(mhi_cntrl, false); |
| 668 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 669 | } |
| 670 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 671 | pci_set_power_state(pdev, PCI_D0); |
| 672 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 673 | pci_restore_state(pdev); |
| 674 | |
| 675 | if (!mhi_pci_is_alive(mhi_cntrl)) |
| 676 | goto err_try_reset; |
| 677 | |
| 678 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 679 | if (err) |
| 680 | goto err_try_reset; |
| 681 | |
| 682 | err = mhi_sync_power_up(mhi_cntrl); |
| 683 | if (err) |
| 684 | goto err_unprepare; |
| 685 | |
| 686 | dev_dbg(&pdev->dev, "Recovery completed\n"); |
| 687 | |
| 688 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 689 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 690 | return; |
| 691 | |
| 692 | err_unprepare: |
| 693 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 694 | err_try_reset: |
| 695 | if (pci_reset_function(pdev)) |
| 696 | dev_err(&pdev->dev, "Recovery failed\n"); |
| 697 | } |
| 698 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 699 | static void health_check(struct timer_list *t) |
| 700 | { |
| 701 | struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer); |
| 702 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 703 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 704 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 705 | test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 706 | return; |
| 707 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 708 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 709 | dev_err(mhi_cntrl->cntrl_dev, "Device died\n"); |
| 710 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | /* reschedule in two seconds */ |
| 715 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 716 | } |
| 717 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 718 | static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 719 | { |
| 720 | const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data; |
| 721 | const struct mhi_controller_config *mhi_cntrl_config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 722 | struct mhi_pci_device *mhi_pdev; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 723 | struct mhi_controller *mhi_cntrl; |
| 724 | int err; |
| 725 | |
| 726 | dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name); |
| 727 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 728 | /* mhi_pdev.mhi_cntrl must be zero-initialized */ |
| 729 | mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL); |
| 730 | if (!mhi_pdev) |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 731 | return -ENOMEM; |
| 732 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 733 | INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 734 | timer_setup(&mhi_pdev->health_check_timer, health_check, 0); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 735 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 736 | mhi_cntrl_config = info->config; |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 737 | mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 738 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 739 | mhi_cntrl->cntrl_dev = &pdev->dev; |
| 740 | mhi_cntrl->iova_start = 0; |
Loic Poulain | 4ea6fa2 | 2020-12-02 09:12:30 +0100 | [diff] [blame] | 741 | 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] | 742 | mhi_cntrl->fw_image = info->fw; |
| 743 | mhi_cntrl->edl_image = info->edl; |
| 744 | |
| 745 | mhi_cntrl->read_reg = mhi_pci_read_reg; |
| 746 | mhi_cntrl->write_reg = mhi_pci_write_reg; |
| 747 | mhi_cntrl->status_cb = mhi_pci_status_cb; |
| 748 | mhi_cntrl->runtime_get = mhi_pci_runtime_get; |
| 749 | mhi_cntrl->runtime_put = mhi_pci_runtime_put; |
Richard Laing | 5c2c853 | 2021-07-15 09:18:05 +1200 | [diff] [blame] | 750 | mhi_cntrl->mru = info->mru_default; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 751 | |
Bhaumik Bhatt | 56f6f4c | 2021-07-16 13:21:04 +0530 | [diff] [blame] | 752 | if (info->sideband_wake) { |
| 753 | mhi_cntrl->wake_get = mhi_pci_wake_get_nop; |
| 754 | mhi_cntrl->wake_put = mhi_pci_wake_put_nop; |
| 755 | mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop; |
| 756 | } |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 757 | |
| 758 | err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width)); |
| 759 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 760 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 761 | |
| 762 | err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config); |
| 763 | if (err) |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 764 | return err; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 765 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 766 | pci_set_drvdata(pdev, mhi_pdev); |
| 767 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 768 | /* Have stored pci confspace at hand for restore in sudden PCI error. |
| 769 | * cache the state locally and discard the PCI core one. |
| 770 | */ |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 771 | pci_save_state(pdev); |
| 772 | mhi_pdev->pci_state = pci_store_saved_state(pdev); |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 773 | pci_load_saved_state(pdev, NULL); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 774 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 775 | pci_enable_pcie_error_reporting(pdev); |
| 776 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 777 | err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config); |
| 778 | if (err) |
Christophe JAILLET | a25d144 | 2021-06-21 21:46:13 +0530 | [diff] [blame] | 779 | goto err_disable_reporting; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 780 | |
| 781 | /* MHI bus does not power up the controller by default */ |
| 782 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 783 | if (err) { |
| 784 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 785 | goto err_unregister; |
| 786 | } |
| 787 | |
| 788 | err = mhi_sync_power_up(mhi_cntrl); |
| 789 | if (err) { |
| 790 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 791 | goto err_unprepare; |
| 792 | } |
| 793 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 794 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
| 795 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 796 | /* start health check */ |
| 797 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 798 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 799 | /* Only allow runtime-suspend if PME capable (for wakeup) */ |
| 800 | if (pci_pme_capable(pdev, PCI_D3hot)) { |
| 801 | pm_runtime_set_autosuspend_delay(&pdev->dev, 2000); |
| 802 | pm_runtime_use_autosuspend(&pdev->dev); |
| 803 | pm_runtime_mark_last_busy(&pdev->dev); |
| 804 | pm_runtime_put_noidle(&pdev->dev); |
| 805 | } |
| 806 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 807 | return 0; |
| 808 | |
| 809 | err_unprepare: |
| 810 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 811 | err_unregister: |
| 812 | mhi_unregister_controller(mhi_cntrl); |
Christophe JAILLET | a25d144 | 2021-06-21 21:46:13 +0530 | [diff] [blame] | 813 | err_disable_reporting: |
| 814 | pci_disable_pcie_error_reporting(pdev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 815 | |
| 816 | return err; |
| 817 | } |
| 818 | |
| 819 | static void mhi_pci_remove(struct pci_dev *pdev) |
| 820 | { |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 821 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 822 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 823 | |
Wei Yongjun | 0b67808 | 2021-06-06 21:07:40 +0530 | [diff] [blame] | 824 | del_timer_sync(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 825 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 826 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 827 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 828 | mhi_power_down(mhi_cntrl, true); |
| 829 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 830 | } |
| 831 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 832 | /* balancing probe put_noidle */ |
| 833 | if (pci_pme_capable(pdev, PCI_D3hot)) |
| 834 | pm_runtime_get_noresume(&pdev->dev); |
| 835 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 836 | mhi_unregister_controller(mhi_cntrl); |
Christophe JAILLET | a25d144 | 2021-06-21 21:46:13 +0530 | [diff] [blame] | 837 | pci_disable_pcie_error_reporting(pdev); |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 840 | static void mhi_pci_shutdown(struct pci_dev *pdev) |
| 841 | { |
| 842 | mhi_pci_remove(pdev); |
| 843 | pci_set_power_state(pdev, PCI_D3hot); |
| 844 | } |
| 845 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 846 | static void mhi_pci_reset_prepare(struct pci_dev *pdev) |
| 847 | { |
| 848 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 849 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 850 | |
| 851 | dev_info(&pdev->dev, "reset\n"); |
| 852 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 853 | del_timer(&mhi_pdev->health_check_timer); |
| 854 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 855 | /* Clean up MHI state */ |
| 856 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 857 | mhi_power_down(mhi_cntrl, false); |
| 858 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 859 | } |
| 860 | |
| 861 | /* cause internal device reset */ |
| 862 | mhi_soc_reset(mhi_cntrl); |
| 863 | |
| 864 | /* Be sure device reset has been executed */ |
| 865 | msleep(MHI_POST_RESET_DELAY_MS); |
| 866 | } |
| 867 | |
| 868 | static void mhi_pci_reset_done(struct pci_dev *pdev) |
| 869 | { |
| 870 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 871 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 872 | int err; |
| 873 | |
| 874 | /* Restore initial known working PCI state */ |
| 875 | pci_load_saved_state(pdev, mhi_pdev->pci_state); |
| 876 | pci_restore_state(pdev); |
| 877 | |
| 878 | /* Is device status available ? */ |
| 879 | if (!mhi_pci_is_alive(mhi_cntrl)) { |
| 880 | dev_err(&pdev->dev, "reset failed\n"); |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | err = mhi_prepare_for_power_up(mhi_cntrl); |
| 885 | if (err) { |
| 886 | dev_err(&pdev->dev, "failed to prepare MHI controller\n"); |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | err = mhi_sync_power_up(mhi_cntrl); |
| 891 | if (err) { |
| 892 | dev_err(&pdev->dev, "failed to power up MHI controller\n"); |
| 893 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 898 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 901 | static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev, |
| 902 | pci_channel_state_t state) |
| 903 | { |
| 904 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 905 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 906 | |
| 907 | dev_err(&pdev->dev, "PCI error detected, state = %u\n", state); |
| 908 | |
| 909 | if (state == pci_channel_io_perm_failure) |
| 910 | return PCI_ERS_RESULT_DISCONNECT; |
| 911 | |
| 912 | /* Clean up MHI state */ |
| 913 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
| 914 | mhi_power_down(mhi_cntrl, false); |
| 915 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 916 | } else { |
| 917 | /* Nothing to do */ |
| 918 | return PCI_ERS_RESULT_RECOVERED; |
| 919 | } |
| 920 | |
| 921 | pci_disable_device(pdev); |
| 922 | |
| 923 | return PCI_ERS_RESULT_NEED_RESET; |
| 924 | } |
| 925 | |
| 926 | static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev) |
| 927 | { |
| 928 | if (pci_enable_device(pdev)) { |
| 929 | dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n"); |
| 930 | return PCI_ERS_RESULT_DISCONNECT; |
| 931 | } |
| 932 | |
| 933 | return PCI_ERS_RESULT_RECOVERED; |
| 934 | } |
| 935 | |
| 936 | static void mhi_pci_io_resume(struct pci_dev *pdev) |
| 937 | { |
| 938 | struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); |
| 939 | |
| 940 | dev_err(&pdev->dev, "PCI slot reset done\n"); |
| 941 | |
| 942 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 943 | } |
| 944 | |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 945 | static const struct pci_error_handlers mhi_pci_err_handler = { |
Loic Poulain | b012ee6 | 2021-01-04 17:14:55 +0100 | [diff] [blame] | 946 | .error_detected = mhi_pci_error_detected, |
| 947 | .slot_reset = mhi_pci_slot_reset, |
| 948 | .resume = mhi_pci_io_resume, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 949 | .reset_prepare = mhi_pci_reset_prepare, |
| 950 | .reset_done = mhi_pci_reset_done, |
| 951 | }; |
| 952 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 953 | static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 954 | { |
| 955 | struct pci_dev *pdev = to_pci_dev(dev); |
| 956 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 957 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 958 | int err; |
| 959 | |
| 960 | if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 961 | return 0; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 962 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 963 | del_timer(&mhi_pdev->health_check_timer); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 964 | cancel_work_sync(&mhi_pdev->recovery_work); |
| 965 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 966 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 967 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 968 | goto pci_suspend; /* Nothing to do at MHI level */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 969 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 970 | /* Transition to M3 state */ |
| 971 | err = mhi_pm_suspend(mhi_cntrl); |
| 972 | if (err) { |
| 973 | dev_err(&pdev->dev, "failed to suspend device: %d\n", err); |
| 974 | clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status); |
| 975 | return -EBUSY; |
| 976 | } |
| 977 | |
| 978 | pci_suspend: |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 979 | pci_disable_device(pdev); |
| 980 | pci_wake_from_d3(pdev, true); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 985 | static int __maybe_unused mhi_pci_runtime_resume(struct device *dev) |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 986 | { |
| 987 | struct pci_dev *pdev = to_pci_dev(dev); |
| 988 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 989 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 990 | int err; |
| 991 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 992 | if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status)) |
| 993 | return 0; |
| 994 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 995 | err = pci_enable_device(pdev); |
| 996 | if (err) |
| 997 | goto err_recovery; |
| 998 | |
Loic Poulain | e89878a | 2021-03-05 20:16:47 +0100 | [diff] [blame] | 999 | pci_set_master(pdev); |
| 1000 | pci_wake_from_d3(pdev, false); |
| 1001 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1002 | if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) || |
| 1003 | mhi_cntrl->ee != MHI_EE_AMSS) |
| 1004 | return 0; /* Nothing to do at MHI level */ |
| 1005 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1006 | /* Exit M3, transition to M0 state */ |
| 1007 | err = mhi_pm_resume(mhi_cntrl); |
| 1008 | if (err) { |
| 1009 | dev_err(&pdev->dev, "failed to resume device: %d\n", err); |
| 1010 | goto err_recovery; |
| 1011 | } |
| 1012 | |
Loic Poulain | 8562d4f | 2021-01-04 17:14:56 +0100 | [diff] [blame] | 1013 | /* Resume health check */ |
| 1014 | mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD); |
| 1015 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1016 | /* It can be a remote wakeup (no mhi runtime_get), update access time */ |
| 1017 | pm_runtime_mark_last_busy(dev); |
| 1018 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1019 | return 0; |
| 1020 | |
| 1021 | err_recovery: |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1022 | /* Do not fail to not mess up our PCI device state, the device likely |
| 1023 | * lost power (d3cold) and we simply need to reset it from the recovery |
| 1024 | * procedure, trigger the recovery asynchronously to prevent system |
| 1025 | * suspend exit delaying. |
| 1026 | */ |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1027 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1028 | pm_runtime_mark_last_busy(dev); |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1029 | |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | static int __maybe_unused mhi_pci_suspend(struct device *dev) |
| 1034 | { |
| 1035 | pm_runtime_disable(dev); |
| 1036 | return mhi_pci_runtime_suspend(dev); |
| 1037 | } |
| 1038 | |
| 1039 | static int __maybe_unused mhi_pci_resume(struct device *dev) |
| 1040 | { |
| 1041 | int ret; |
| 1042 | |
| 1043 | /* Depending the platform, device may have lost power (d3cold), we need |
| 1044 | * to resume it now to check its state and recover when necessary. |
| 1045 | */ |
| 1046 | ret = mhi_pci_runtime_resume(dev); |
| 1047 | pm_runtime_enable(dev); |
| 1048 | |
| 1049 | return ret; |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Loic Poulain | 5f0c2ee | 2021-06-06 21:07:41 +0530 | [diff] [blame] | 1052 | static int __maybe_unused mhi_pci_freeze(struct device *dev) |
| 1053 | { |
| 1054 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 1055 | struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; |
| 1056 | |
| 1057 | /* We want to stop all operations, hibernation does not guarantee that |
| 1058 | * device will be in the same state as before freezing, especially if |
| 1059 | * the intermediate restore kernel reinitializes MHI device with new |
| 1060 | * context. |
| 1061 | */ |
| 1062 | if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { |
Loic Poulain | f77097e | 2021-12-16 13:42:19 +0530 | [diff] [blame] | 1063 | mhi_power_down(mhi_cntrl, true); |
Loic Poulain | 5f0c2ee | 2021-06-06 21:07:41 +0530 | [diff] [blame] | 1064 | mhi_unprepare_after_power_down(mhi_cntrl); |
| 1065 | } |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | static int __maybe_unused mhi_pci_restore(struct device *dev) |
| 1071 | { |
| 1072 | struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); |
| 1073 | |
| 1074 | /* Reinitialize the device */ |
| 1075 | queue_work(system_long_wq, &mhi_pdev->recovery_work); |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1080 | static const struct dev_pm_ops mhi_pci_pm_ops = { |
Loic Poulain | d3800c1 | 2021-03-05 20:16:48 +0100 | [diff] [blame] | 1081 | SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL) |
Loic Poulain | 5f0c2ee | 2021-06-06 21:07:41 +0530 | [diff] [blame] | 1082 | #ifdef CONFIG_PM_SLEEP |
| 1083 | .suspend = mhi_pci_suspend, |
| 1084 | .resume = mhi_pci_resume, |
| 1085 | .freeze = mhi_pci_freeze, |
| 1086 | .thaw = mhi_pci_restore, |
| 1087 | .restore = mhi_pci_restore, |
| 1088 | #endif |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1089 | }; |
| 1090 | |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 1091 | static struct pci_driver mhi_pci_driver = { |
| 1092 | .name = "mhi-pci-generic", |
| 1093 | .id_table = mhi_pci_id_table, |
| 1094 | .probe = mhi_pci_probe, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 1095 | .remove = mhi_pci_remove, |
Loic Poulain | 757072a | 2021-03-19 16:50:37 +0100 | [diff] [blame] | 1096 | .shutdown = mhi_pci_shutdown, |
Loic Poulain | 8ccc327 | 2021-01-04 17:14:53 +0100 | [diff] [blame] | 1097 | .err_handler = &mhi_pci_err_handler, |
Loic Poulain | 7389337 | 2021-01-04 17:14:54 +0100 | [diff] [blame] | 1098 | .driver.pm = &mhi_pci_pm_ops |
Loic Poulain | 855a70c | 2020-10-21 19:18:19 +0200 | [diff] [blame] | 1099 | }; |
| 1100 | module_pci_driver(mhi_pci_driver); |
| 1101 | |
| 1102 | MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); |
| 1103 | MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver"); |
| 1104 | MODULE_LICENSE("GPL"); |