blob: 5872c42927b16b5e6b36574ec7bff4ba6103b825 [file] [log] [blame]
Loic Poulain855a70c2020-10-21 19:18:19 +02001// 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 Poulainb012ee62021-01-04 17:14:55 +010011#include <linux/aer.h>
Loic Poulain8ccc3272021-01-04 17:14:53 +010012#include <linux/delay.h>
Loic Poulain855a70c2020-10-21 19:18:19 +020013#include <linux/device.h>
14#include <linux/mhi.h>
15#include <linux/module.h>
16#include <linux/pci.h>
Loic Poulaind3800c12021-03-05 20:16:48 +010017#include <linux/pm_runtime.h>
Loic Poulain8562d4f2021-01-04 17:14:56 +010018#include <linux/timer.h>
Loic Poulain73893372021-01-04 17:14:54 +010019#include <linux/workqueue.h>
Loic Poulain855a70c2020-10-21 19:18:19 +020020
21#define MHI_PCI_DEFAULT_BAR_NUM 0
22
Loic Poulain8ccc3272021-01-04 17:14:53 +010023#define MHI_POST_RESET_DELAY_MS 500
Loic Poulain8562d4f2021-01-04 17:14:56 +010024
25#define HEALTH_CHECK_PERIOD (HZ * 2)
26
Loic Poulain855a70c2020-10-21 19:18:19 +020027/**
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)
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +053035 * @sideband_wake: Devices using dedicated sideband GPIO for wakeup instead
36 * of inband wake support (such as sdx24)
Loic Poulain855a70c2020-10-21 19:18:19 +020037 */
38struct mhi_pci_dev_info {
39 const struct mhi_controller_config *config;
40 const char *name;
41 const char *fw;
42 const char *edl;
43 unsigned int bar_num;
44 unsigned int dma_data_width;
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +053045 bool sideband_wake;
Loic Poulain855a70c2020-10-21 19:18:19 +020046};
47
48#define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \
49 { \
50 .num = ch_num, \
51 .name = ch_name, \
52 .num_elements = el_count, \
53 .event_ring = ev_ring, \
54 .dir = DMA_TO_DEVICE, \
55 .ee_mask = BIT(MHI_EE_AMSS), \
56 .pollcfg = 0, \
57 .doorbell = MHI_DB_BRST_DISABLE, \
58 .lpm_notify = false, \
59 .offload_channel = false, \
60 .doorbell_mode_switch = false, \
61 } \
62
63#define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \
64 { \
65 .num = ch_num, \
66 .name = ch_name, \
67 .num_elements = el_count, \
68 .event_ring = ev_ring, \
69 .dir = DMA_FROM_DEVICE, \
70 .ee_mask = BIT(MHI_EE_AMSS), \
71 .pollcfg = 0, \
72 .doorbell = MHI_DB_BRST_DISABLE, \
73 .lpm_notify = false, \
74 .offload_channel = false, \
75 .doorbell_mode_switch = false, \
76 }
77
Loic Poulainb8a97f22021-07-16 13:21:06 +053078#define MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(ch_num, ch_name, el_count, ev_ring) \
79 { \
80 .num = ch_num, \
81 .name = ch_name, \
82 .num_elements = el_count, \
83 .event_ring = ev_ring, \
84 .dir = DMA_FROM_DEVICE, \
85 .ee_mask = BIT(MHI_EE_AMSS), \
86 .pollcfg = 0, \
87 .doorbell = MHI_DB_BRST_DISABLE, \
88 .lpm_notify = false, \
89 .offload_channel = false, \
90 .doorbell_mode_switch = false, \
91 .auto_queue = true, \
92 }
93
Loic Poulain9ea48ef2021-03-05 20:16:43 +010094#define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +020095 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +010096 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +020097 .irq_moderation_ms = 0, \
98 .irq = (ev_ring) + 1, \
99 .priority = 1, \
100 .mode = MHI_DB_BRST_DISABLE, \
101 .data_type = MHI_ER_CTRL, \
102 .hardware_event = false, \
103 .client_managed = false, \
104 .offload_channel = false, \
105 }
106
Loic Poulaineb967872021-01-04 17:14:52 +0100107#define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \
108 { \
109 .num = ch_num, \
110 .name = ch_name, \
111 .num_elements = el_count, \
112 .event_ring = ev_ring, \
113 .dir = DMA_TO_DEVICE, \
114 .ee_mask = BIT(MHI_EE_AMSS), \
115 .pollcfg = 0, \
116 .doorbell = MHI_DB_BRST_ENABLE, \
117 .lpm_notify = false, \
118 .offload_channel = false, \
119 .doorbell_mode_switch = true, \
120 } \
121
122#define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \
123 { \
124 .num = ch_num, \
125 .name = ch_name, \
126 .num_elements = el_count, \
127 .event_ring = ev_ring, \
128 .dir = DMA_FROM_DEVICE, \
129 .ee_mask = BIT(MHI_EE_AMSS), \
130 .pollcfg = 0, \
131 .doorbell = MHI_DB_BRST_ENABLE, \
132 .lpm_notify = false, \
133 .offload_channel = false, \
134 .doorbell_mode_switch = true, \
135 }
136
Loic Poulainac4bf602021-03-05 20:16:44 +0100137#define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \
138 { \
139 .num = ch_num, \
140 .name = ch_name, \
141 .num_elements = el_count, \
142 .event_ring = ev_ring, \
143 .dir = DMA_TO_DEVICE, \
144 .ee_mask = BIT(MHI_EE_SBL), \
145 .pollcfg = 0, \
146 .doorbell = MHI_DB_BRST_DISABLE, \
147 .lpm_notify = false, \
148 .offload_channel = false, \
149 .doorbell_mode_switch = false, \
150 } \
151
152#define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \
153 { \
154 .num = ch_num, \
155 .name = ch_name, \
156 .num_elements = el_count, \
157 .event_ring = ev_ring, \
158 .dir = DMA_FROM_DEVICE, \
159 .ee_mask = BIT(MHI_EE_SBL), \
160 .pollcfg = 0, \
161 .doorbell = MHI_DB_BRST_DISABLE, \
162 .lpm_notify = false, \
163 .offload_channel = false, \
164 .doorbell_mode_switch = false, \
165 }
166
Loic Poulain11134392021-04-07 10:41:00 +0200167#define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \
168 { \
169 .num = ch_num, \
170 .name = ch_name, \
171 .num_elements = el_count, \
172 .event_ring = ev_ring, \
173 .dir = DMA_TO_DEVICE, \
174 .ee_mask = BIT(MHI_EE_FP), \
175 .pollcfg = 0, \
176 .doorbell = MHI_DB_BRST_DISABLE, \
177 .lpm_notify = false, \
178 .offload_channel = false, \
179 .doorbell_mode_switch = false, \
180 } \
181
182#define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \
183 { \
184 .num = ch_num, \
185 .name = ch_name, \
186 .num_elements = el_count, \
187 .event_ring = ev_ring, \
188 .dir = DMA_FROM_DEVICE, \
189 .ee_mask = BIT(MHI_EE_FP), \
190 .pollcfg = 0, \
191 .doorbell = MHI_DB_BRST_DISABLE, \
192 .lpm_notify = false, \
193 .offload_channel = false, \
194 .doorbell_mode_switch = false, \
195 }
196
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100197#define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200198 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100199 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200200 .irq_moderation_ms = 5, \
201 .irq = (ev_ring) + 1, \
202 .priority = 1, \
203 .mode = MHI_DB_BRST_DISABLE, \
204 .data_type = MHI_ER_DATA, \
205 .hardware_event = false, \
206 .client_managed = false, \
207 .offload_channel = false, \
208 }
209
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100210#define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200211 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100212 .num_elements = el_count, \
Loic Poulainec7513692021-01-04 17:14:59 +0100213 .irq_moderation_ms = 1, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200214 .irq = (ev_ring) + 1, \
215 .priority = 1, \
216 .mode = MHI_DB_BRST_DISABLE, \
217 .data_type = MHI_ER_DATA, \
218 .hardware_event = true, \
219 .client_managed = false, \
220 .offload_channel = false, \
221 .channel = ch_num, \
222 }
223
224static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = {
Loic Poulain4da3d072021-01-04 17:14:58 +0100225 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1),
226 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1),
Loic Poulain855a70c2020-10-21 19:18:19 +0200227 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0),
228 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0),
229 MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0),
230 MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0),
231 MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0),
Loic Poulainb8a97f22021-07-16 13:21:06 +0530232 MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 8, 0),
Loic Poulain11134392021-04-07 10:41:00 +0200233 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
234 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
Loic Poulain4da3d072021-01-04 17:14:58 +0100235 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2),
236 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3),
Loic Poulain855a70c2020-10-21 19:18:19 +0200237};
238
Loic Poulainb91c3b32021-01-05 17:44:36 +0100239static struct mhi_event_config modem_qcom_v1_mhi_events[] = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200240 /* first ring is control+data ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100241 MHI_EVENT_CONFIG_CTRL(0, 64),
Loic Poulain4da3d072021-01-04 17:14:58 +0100242 /* DIAG dedicated event ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100243 MHI_EVENT_CONFIG_DATA(1, 128),
Loic Poulain855a70c2020-10-21 19:18:19 +0200244 /* Hardware channels request dedicated hardware event rings */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100245 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
246 MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101)
Loic Poulain855a70c2020-10-21 19:18:19 +0200247};
248
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530249static const struct mhi_controller_config modem_qcom_v1_mhiv_config = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200250 .max_channels = 128,
Loic Poulain84026a52021-01-04 17:14:57 +0100251 .timeout_ms = 8000,
Loic Poulain855a70c2020-10-21 19:18:19 +0200252 .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels),
253 .ch_cfg = modem_qcom_v1_mhi_channels,
254 .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events),
255 .event_cfg = modem_qcom_v1_mhi_events,
256};
257
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700258static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = {
259 .name = "qcom-sdx65m",
260 .fw = "qcom/sdx65m/xbl.elf",
261 .edl = "qcom/sdx65m/edl.mbn",
262 .config = &modem_qcom_v1_mhiv_config,
263 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530264 .dma_data_width = 32,
265 .sideband_wake = false,
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700266};
267
Loic Poulain855a70c2020-10-21 19:18:19 +0200268static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = {
269 .name = "qcom-sdx55m",
270 .fw = "qcom/sdx55m/sbl1.mbn",
271 .edl = "qcom/sdx55m/edl.mbn",
272 .config = &modem_qcom_v1_mhiv_config,
273 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530274 .dma_data_width = 32,
275 .sideband_wake = false,
Loic Poulain855a70c2020-10-21 19:18:19 +0200276};
277
Loic Poulain59d05b72021-03-05 20:16:45 +0100278static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = {
279 .name = "qcom-sdx24",
280 .edl = "qcom/prog_firehose_sdx24.mbn",
281 .config = &modem_qcom_v1_mhiv_config,
282 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530283 .dma_data_width = 32,
284 .sideband_wake = true,
Loic Poulain59d05b72021-03-05 20:16:45 +0100285};
286
Loic Poulainac4bf602021-03-05 20:16:44 +0100287static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = {
288 MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0),
289 MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0),
290 MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
291 MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0),
292 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
293 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
294 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
295 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
296 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
297 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
Loic Poulain11134392021-04-07 10:41:00 +0200298 /* The EDL firmware is a flash-programmer exposing firehose protocol */
299 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
300 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
Loic Poulainac4bf602021-03-05 20:16:44 +0100301 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
302 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
303};
304
305static struct mhi_event_config mhi_quectel_em1xx_events[] = {
306 MHI_EVENT_CONFIG_CTRL(0, 128),
307 MHI_EVENT_CONFIG_DATA(1, 128),
308 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
309 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
310};
311
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530312static const struct mhi_controller_config modem_quectel_em1xx_config = {
Loic Poulainac4bf602021-03-05 20:16:44 +0100313 .max_channels = 128,
314 .timeout_ms = 20000,
315 .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels),
316 .ch_cfg = mhi_quectel_em1xx_channels,
317 .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events),
318 .event_cfg = mhi_quectel_em1xx_events,
319};
320
321static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = {
322 .name = "quectel-em1xx",
323 .edl = "qcom/prog_firehose_sdx24.mbn",
324 .config = &modem_quectel_em1xx_config,
325 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530326 .dma_data_width = 32,
327 .sideband_wake = true,
Loic Poulainac4bf602021-03-05 20:16:44 +0100328};
329
Jarvis Jiangaac42652021-04-08 02:55:24 -0700330static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = {
331 MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0),
332 MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0),
333 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
334 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
335 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
336 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
Jarvis Jiangc7711c22021-06-06 21:07:39 +0530337 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
338 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
Jarvis Jiangaac42652021-04-08 02:55:24 -0700339 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
340 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
341};
342
343static struct mhi_event_config mhi_foxconn_sdx55_events[] = {
344 MHI_EVENT_CONFIG_CTRL(0, 128),
345 MHI_EVENT_CONFIG_DATA(1, 128),
346 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
347 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
348};
349
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530350static const struct mhi_controller_config modem_foxconn_sdx55_config = {
Jarvis Jiangaac42652021-04-08 02:55:24 -0700351 .max_channels = 128,
352 .timeout_ms = 20000,
353 .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels),
354 .ch_cfg = mhi_foxconn_sdx55_channels,
355 .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events),
356 .event_cfg = mhi_foxconn_sdx55_events,
357};
358
359static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = {
360 .name = "foxconn-sdx55",
361 .fw = "qcom/sdx55m/sbl1.mbn",
362 .edl = "qcom/sdx55m/edl.mbn",
363 .config = &modem_foxconn_sdx55_config,
364 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530365 .dma_data_width = 32,
366 .sideband_wake = false,
Jarvis Jiangaac42652021-04-08 02:55:24 -0700367};
368
ULRICH Thomas87693e02021-08-02 10:42:47 +0530369static const struct mhi_channel_config mhi_mv31_channels[] = {
370 MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 64, 0),
371 MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 64, 0),
372 /* MBIM Control Channel */
373 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 64, 0),
374 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 64, 0),
375 /* MBIM Data Channel */
376 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 512, 2),
377 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 512, 3),
378};
379
380static struct mhi_event_config mhi_mv31_events[] = {
381 MHI_EVENT_CONFIG_CTRL(0, 256),
382 MHI_EVENT_CONFIG_DATA(1, 256),
383 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
384 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101),
385};
386
387static const struct mhi_controller_config modem_mv31_config = {
388 .max_channels = 128,
389 .timeout_ms = 20000,
390 .num_channels = ARRAY_SIZE(mhi_mv31_channels),
391 .ch_cfg = mhi_mv31_channels,
392 .num_events = ARRAY_SIZE(mhi_mv31_events),
393 .event_cfg = mhi_mv31_events,
394};
395
396static const struct mhi_pci_dev_info mhi_mv31_info = {
397 .name = "cinterion-mv31",
398 .config = &modem_mv31_config,
399 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
400 .dma_data_width = 32,
401};
402
Loic Poulain855a70c2020-10-21 19:18:19 +0200403static const struct pci_device_id mhi_pci_id_table[] = {
404 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
405 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
Loic Poulain59d05b72021-03-05 20:16:45 +0100406 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
407 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
Loic Poulainac4bf602021-03-05 20:16:44 +0100408 { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */
409 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
410 { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */
411 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700412 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
413 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
Jarvis Jiangaac42652021-04-08 02:55:24 -0700414 /* T99W175 (sdx55), Both for eSIM and Non-eSIM */
415 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab),
416 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
417 /* DW5930e (sdx55), With eSIM, It's also T99W175 */
418 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0),
419 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
420 /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */
421 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1),
422 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
ULRICH Thomas87693e02021-08-02 10:42:47 +0530423 /* MV31-W (Cinterion) */
424 { PCI_DEVICE(0x1269, 0x00b3),
425 .driver_data = (kernel_ulong_t) &mhi_mv31_info },
Loic Poulain855a70c2020-10-21 19:18:19 +0200426 { }
427};
428MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
429
Loic Poulain8ccc3272021-01-04 17:14:53 +0100430enum mhi_pci_device_status {
431 MHI_PCI_DEV_STARTED,
Loic Poulaind3800c12021-03-05 20:16:48 +0100432 MHI_PCI_DEV_SUSPENDED,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100433};
434
435struct mhi_pci_device {
436 struct mhi_controller mhi_cntrl;
437 struct pci_saved_state *pci_state;
Loic Poulain73893372021-01-04 17:14:54 +0100438 struct work_struct recovery_work;
Loic Poulain8562d4f2021-01-04 17:14:56 +0100439 struct timer_list health_check_timer;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100440 unsigned long status;
441};
442
Loic Poulain855a70c2020-10-21 19:18:19 +0200443static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl,
444 void __iomem *addr, u32 *out)
445{
446 *out = readl(addr);
447 return 0;
448}
449
450static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
451 void __iomem *addr, u32 val)
452{
453 writel(val, addr);
454}
455
456static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
457 enum mhi_callback cb)
458{
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100459 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
460
Loic Poulain855a70c2020-10-21 19:18:19 +0200461 /* Nothing to do for now */
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100462 switch (cb) {
463 case MHI_CB_FATAL_ERROR:
464 case MHI_CB_SYS_ERROR:
465 dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb);
Loic Poulaind3800c12021-03-05 20:16:48 +0100466 pm_runtime_forbid(&pdev->dev);
467 break;
468 case MHI_CB_EE_MISSION_MODE:
469 pm_runtime_allow(&pdev->dev);
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100470 break;
471 default:
472 break;
473 }
Loic Poulain855a70c2020-10-21 19:18:19 +0200474}
475
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100476static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
477{
478 /* no-op */
479}
480
481static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
482{
483 /* no-op */
484}
485
486static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
487{
488 /* no-op */
489}
490
Loic Poulain8ccc3272021-01-04 17:14:53 +0100491static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl)
492{
493 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
494 u16 vendor = 0;
495
496 if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor))
497 return false;
498
499 if (vendor == (u16) ~0 || vendor == 0)
500 return false;
501
502 return true;
503}
504
Loic Poulain855a70c2020-10-21 19:18:19 +0200505static int mhi_pci_claim(struct mhi_controller *mhi_cntrl,
506 unsigned int bar_num, u64 dma_mask)
507{
508 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
509 int err;
510
511 err = pci_assign_resource(pdev, bar_num);
512 if (err)
513 return err;
514
515 err = pcim_enable_device(pdev);
516 if (err) {
517 dev_err(&pdev->dev, "failed to enable pci device: %d\n", err);
518 return err;
519 }
520
521 err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev));
522 if (err) {
523 dev_err(&pdev->dev, "failed to map pci region: %d\n", err);
524 return err;
525 }
526 mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num];
527
528 err = pci_set_dma_mask(pdev, dma_mask);
529 if (err) {
530 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
531 return err;
532 }
533
534 err = pci_set_consistent_dma_mask(pdev, dma_mask);
535 if (err) {
536 dev_err(&pdev->dev, "set consistent dma mask failed\n");
537 return err;
538 }
539
540 pci_set_master(pdev);
541
542 return 0;
543}
544
545static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl,
546 const struct mhi_controller_config *mhi_cntrl_config)
547{
548 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
549 int nr_vectors, i;
550 int *irq;
551
552 /*
553 * Alloc one MSI vector for BHI + one vector per event ring, ideally...
554 * No explicit pci_free_irq_vectors required, done by pcim_release.
555 */
556 mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events;
557
558 nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI);
559 if (nr_vectors < 0) {
560 dev_err(&pdev->dev, "Error allocating MSI vectors %d\n",
561 nr_vectors);
562 return nr_vectors;
563 }
564
565 if (nr_vectors < mhi_cntrl->nr_irqs) {
Loic Poulainb91c3b32021-01-05 17:44:36 +0100566 dev_warn(&pdev->dev, "using shared MSI\n");
567
568 /* Patch msi vectors, use only one (shared) */
569 for (i = 0; i < mhi_cntrl_config->num_events; i++)
570 mhi_cntrl_config->event_cfg[i].irq = 0;
571 mhi_cntrl->nr_irqs = 1;
Loic Poulain855a70c2020-10-21 19:18:19 +0200572 }
573
574 irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL);
575 if (!irq)
576 return -ENOMEM;
577
578 for (i = 0; i < mhi_cntrl->nr_irqs; i++) {
579 int vector = i >= nr_vectors ? (nr_vectors - 1) : i;
580
581 irq[i] = pci_irq_vector(pdev, vector);
582 }
583
584 mhi_cntrl->irq = irq;
585
586 return 0;
587}
588
589static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
590{
Loic Poulaind3800c12021-03-05 20:16:48 +0100591 /* The runtime_get() MHI callback means:
592 * Do whatever is requested to leave M3.
593 */
594 return pm_runtime_get(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200595}
596
597static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
598{
Loic Poulaind3800c12021-03-05 20:16:48 +0100599 /* The runtime_put() MHI callback means:
600 * Device can be moved in M3 state.
601 */
602 pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev);
603 pm_runtime_put(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200604}
605
Loic Poulain73893372021-01-04 17:14:54 +0100606static void mhi_pci_recovery_work(struct work_struct *work)
607{
608 struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device,
609 recovery_work);
610 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
611 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
612 int err;
613
614 dev_warn(&pdev->dev, "device recovery started\n");
615
Loic Poulain8562d4f2021-01-04 17:14:56 +0100616 del_timer(&mhi_pdev->health_check_timer);
Loic Poulaind3800c12021-03-05 20:16:48 +0100617 pm_runtime_forbid(&pdev->dev);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100618
Loic Poulain73893372021-01-04 17:14:54 +0100619 /* Clean up MHI state */
620 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
621 mhi_power_down(mhi_cntrl, false);
622 mhi_unprepare_after_power_down(mhi_cntrl);
623 }
624
Loic Poulain73893372021-01-04 17:14:54 +0100625 pci_set_power_state(pdev, PCI_D0);
626 pci_load_saved_state(pdev, mhi_pdev->pci_state);
627 pci_restore_state(pdev);
628
629 if (!mhi_pci_is_alive(mhi_cntrl))
630 goto err_try_reset;
631
632 err = mhi_prepare_for_power_up(mhi_cntrl);
633 if (err)
634 goto err_try_reset;
635
636 err = mhi_sync_power_up(mhi_cntrl);
637 if (err)
638 goto err_unprepare;
639
640 dev_dbg(&pdev->dev, "Recovery completed\n");
641
642 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100643 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain73893372021-01-04 17:14:54 +0100644 return;
645
646err_unprepare:
647 mhi_unprepare_after_power_down(mhi_cntrl);
648err_try_reset:
649 if (pci_reset_function(pdev))
650 dev_err(&pdev->dev, "Recovery failed\n");
651}
652
Loic Poulain8562d4f2021-01-04 17:14:56 +0100653static void health_check(struct timer_list *t)
654{
655 struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer);
656 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
657
Loic Poulaind3800c12021-03-05 20:16:48 +0100658 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
659 test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
660 return;
661
Loic Poulain8562d4f2021-01-04 17:14:56 +0100662 if (!mhi_pci_is_alive(mhi_cntrl)) {
663 dev_err(mhi_cntrl->cntrl_dev, "Device died\n");
664 queue_work(system_long_wq, &mhi_pdev->recovery_work);
665 return;
666 }
667
668 /* reschedule in two seconds */
669 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
670}
671
Loic Poulain855a70c2020-10-21 19:18:19 +0200672static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
673{
674 const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data;
675 const struct mhi_controller_config *mhi_cntrl_config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100676 struct mhi_pci_device *mhi_pdev;
Loic Poulain855a70c2020-10-21 19:18:19 +0200677 struct mhi_controller *mhi_cntrl;
678 int err;
679
680 dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name);
681
Loic Poulain8ccc3272021-01-04 17:14:53 +0100682 /* mhi_pdev.mhi_cntrl must be zero-initialized */
683 mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL);
684 if (!mhi_pdev)
Loic Poulain855a70c2020-10-21 19:18:19 +0200685 return -ENOMEM;
686
Loic Poulain73893372021-01-04 17:14:54 +0100687 INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100688 timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
Loic Poulain73893372021-01-04 17:14:54 +0100689
Loic Poulain855a70c2020-10-21 19:18:19 +0200690 mhi_cntrl_config = info->config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100691 mhi_cntrl = &mhi_pdev->mhi_cntrl;
692
Loic Poulain855a70c2020-10-21 19:18:19 +0200693 mhi_cntrl->cntrl_dev = &pdev->dev;
694 mhi_cntrl->iova_start = 0;
Loic Poulain4ea6fa22020-12-02 09:12:30 +0100695 mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
Loic Poulain855a70c2020-10-21 19:18:19 +0200696 mhi_cntrl->fw_image = info->fw;
697 mhi_cntrl->edl_image = info->edl;
698
699 mhi_cntrl->read_reg = mhi_pci_read_reg;
700 mhi_cntrl->write_reg = mhi_pci_write_reg;
701 mhi_cntrl->status_cb = mhi_pci_status_cb;
702 mhi_cntrl->runtime_get = mhi_pci_runtime_get;
703 mhi_cntrl->runtime_put = mhi_pci_runtime_put;
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530704
705 if (info->sideband_wake) {
706 mhi_cntrl->wake_get = mhi_pci_wake_get_nop;
707 mhi_cntrl->wake_put = mhi_pci_wake_put_nop;
708 mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop;
709 }
Loic Poulain855a70c2020-10-21 19:18:19 +0200710
711 err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
712 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100713 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200714
715 err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
716 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100717 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200718
Loic Poulain8ccc3272021-01-04 17:14:53 +0100719 pci_set_drvdata(pdev, mhi_pdev);
720
Loic Poulaine89878a2021-03-05 20:16:47 +0100721 /* Have stored pci confspace at hand for restore in sudden PCI error.
722 * cache the state locally and discard the PCI core one.
723 */
Loic Poulain8ccc3272021-01-04 17:14:53 +0100724 pci_save_state(pdev);
725 mhi_pdev->pci_state = pci_store_saved_state(pdev);
Loic Poulaine89878a2021-03-05 20:16:47 +0100726 pci_load_saved_state(pdev, NULL);
Loic Poulain855a70c2020-10-21 19:18:19 +0200727
Loic Poulainb012ee62021-01-04 17:14:55 +0100728 pci_enable_pcie_error_reporting(pdev);
729
Loic Poulain855a70c2020-10-21 19:18:19 +0200730 err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
731 if (err)
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530732 goto err_disable_reporting;
Loic Poulain855a70c2020-10-21 19:18:19 +0200733
734 /* MHI bus does not power up the controller by default */
735 err = mhi_prepare_for_power_up(mhi_cntrl);
736 if (err) {
737 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
738 goto err_unregister;
739 }
740
741 err = mhi_sync_power_up(mhi_cntrl);
742 if (err) {
743 dev_err(&pdev->dev, "failed to power up MHI controller\n");
744 goto err_unprepare;
745 }
746
Loic Poulain8ccc3272021-01-04 17:14:53 +0100747 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
748
Loic Poulain8562d4f2021-01-04 17:14:56 +0100749 /* start health check */
750 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
751
Loic Poulaind3800c12021-03-05 20:16:48 +0100752 /* Only allow runtime-suspend if PME capable (for wakeup) */
753 if (pci_pme_capable(pdev, PCI_D3hot)) {
754 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
755 pm_runtime_use_autosuspend(&pdev->dev);
756 pm_runtime_mark_last_busy(&pdev->dev);
757 pm_runtime_put_noidle(&pdev->dev);
758 }
759
Loic Poulain855a70c2020-10-21 19:18:19 +0200760 return 0;
761
762err_unprepare:
763 mhi_unprepare_after_power_down(mhi_cntrl);
764err_unregister:
765 mhi_unregister_controller(mhi_cntrl);
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530766err_disable_reporting:
767 pci_disable_pcie_error_reporting(pdev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200768
769 return err;
770}
771
772static void mhi_pci_remove(struct pci_dev *pdev)
773{
Loic Poulain8ccc3272021-01-04 17:14:53 +0100774 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
775 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulain855a70c2020-10-21 19:18:19 +0200776
Wei Yongjun0b678082021-06-06 21:07:40 +0530777 del_timer_sync(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100778 cancel_work_sync(&mhi_pdev->recovery_work);
779
Loic Poulain8ccc3272021-01-04 17:14:53 +0100780 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
781 mhi_power_down(mhi_cntrl, true);
782 mhi_unprepare_after_power_down(mhi_cntrl);
783 }
784
Loic Poulaind3800c12021-03-05 20:16:48 +0100785 /* balancing probe put_noidle */
786 if (pci_pme_capable(pdev, PCI_D3hot))
787 pm_runtime_get_noresume(&pdev->dev);
788
Loic Poulain855a70c2020-10-21 19:18:19 +0200789 mhi_unregister_controller(mhi_cntrl);
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530790 pci_disable_pcie_error_reporting(pdev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200791}
792
Loic Poulain757072a2021-03-19 16:50:37 +0100793static void mhi_pci_shutdown(struct pci_dev *pdev)
794{
795 mhi_pci_remove(pdev);
796 pci_set_power_state(pdev, PCI_D3hot);
797}
798
Loic Poulain8ccc3272021-01-04 17:14:53 +0100799static void mhi_pci_reset_prepare(struct pci_dev *pdev)
800{
801 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
802 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
803
804 dev_info(&pdev->dev, "reset\n");
805
Loic Poulain8562d4f2021-01-04 17:14:56 +0100806 del_timer(&mhi_pdev->health_check_timer);
807
Loic Poulain8ccc3272021-01-04 17:14:53 +0100808 /* Clean up MHI state */
809 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
810 mhi_power_down(mhi_cntrl, false);
811 mhi_unprepare_after_power_down(mhi_cntrl);
812 }
813
814 /* cause internal device reset */
815 mhi_soc_reset(mhi_cntrl);
816
817 /* Be sure device reset has been executed */
818 msleep(MHI_POST_RESET_DELAY_MS);
819}
820
821static void mhi_pci_reset_done(struct pci_dev *pdev)
822{
823 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
824 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
825 int err;
826
827 /* Restore initial known working PCI state */
828 pci_load_saved_state(pdev, mhi_pdev->pci_state);
829 pci_restore_state(pdev);
830
831 /* Is device status available ? */
832 if (!mhi_pci_is_alive(mhi_cntrl)) {
833 dev_err(&pdev->dev, "reset failed\n");
834 return;
835 }
836
837 err = mhi_prepare_for_power_up(mhi_cntrl);
838 if (err) {
839 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
840 return;
841 }
842
843 err = mhi_sync_power_up(mhi_cntrl);
844 if (err) {
845 dev_err(&pdev->dev, "failed to power up MHI controller\n");
846 mhi_unprepare_after_power_down(mhi_cntrl);
847 return;
848 }
849
850 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100851 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain8ccc3272021-01-04 17:14:53 +0100852}
853
Loic Poulainb012ee62021-01-04 17:14:55 +0100854static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev,
855 pci_channel_state_t state)
856{
857 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
858 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
859
860 dev_err(&pdev->dev, "PCI error detected, state = %u\n", state);
861
862 if (state == pci_channel_io_perm_failure)
863 return PCI_ERS_RESULT_DISCONNECT;
864
865 /* Clean up MHI state */
866 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
867 mhi_power_down(mhi_cntrl, false);
868 mhi_unprepare_after_power_down(mhi_cntrl);
869 } else {
870 /* Nothing to do */
871 return PCI_ERS_RESULT_RECOVERED;
872 }
873
874 pci_disable_device(pdev);
875
876 return PCI_ERS_RESULT_NEED_RESET;
877}
878
879static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev)
880{
881 if (pci_enable_device(pdev)) {
882 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n");
883 return PCI_ERS_RESULT_DISCONNECT;
884 }
885
886 return PCI_ERS_RESULT_RECOVERED;
887}
888
889static void mhi_pci_io_resume(struct pci_dev *pdev)
890{
891 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
892
893 dev_err(&pdev->dev, "PCI slot reset done\n");
894
895 queue_work(system_long_wq, &mhi_pdev->recovery_work);
896}
897
Loic Poulain8ccc3272021-01-04 17:14:53 +0100898static const struct pci_error_handlers mhi_pci_err_handler = {
Loic Poulainb012ee62021-01-04 17:14:55 +0100899 .error_detected = mhi_pci_error_detected,
900 .slot_reset = mhi_pci_slot_reset,
901 .resume = mhi_pci_io_resume,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100902 .reset_prepare = mhi_pci_reset_prepare,
903 .reset_done = mhi_pci_reset_done,
904};
905
Loic Poulaind3800c12021-03-05 20:16:48 +0100906static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100907{
908 struct pci_dev *pdev = to_pci_dev(dev);
909 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
910 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulaind3800c12021-03-05 20:16:48 +0100911 int err;
912
913 if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
914 return 0;
Loic Poulain73893372021-01-04 17:14:54 +0100915
Loic Poulain8562d4f2021-01-04 17:14:56 +0100916 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100917 cancel_work_sync(&mhi_pdev->recovery_work);
918
Loic Poulaind3800c12021-03-05 20:16:48 +0100919 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
920 mhi_cntrl->ee != MHI_EE_AMSS)
921 goto pci_suspend; /* Nothing to do at MHI level */
Loic Poulain73893372021-01-04 17:14:54 +0100922
Loic Poulaind3800c12021-03-05 20:16:48 +0100923 /* Transition to M3 state */
924 err = mhi_pm_suspend(mhi_cntrl);
925 if (err) {
926 dev_err(&pdev->dev, "failed to suspend device: %d\n", err);
927 clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status);
928 return -EBUSY;
929 }
930
931pci_suspend:
Loic Poulain73893372021-01-04 17:14:54 +0100932 pci_disable_device(pdev);
933 pci_wake_from_d3(pdev, true);
Loic Poulain73893372021-01-04 17:14:54 +0100934
935 return 0;
936}
937
Loic Poulaind3800c12021-03-05 20:16:48 +0100938static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100939{
940 struct pci_dev *pdev = to_pci_dev(dev);
941 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
942 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
943 int err;
944
Loic Poulaind3800c12021-03-05 20:16:48 +0100945 if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
946 return 0;
947
Loic Poulain73893372021-01-04 17:14:54 +0100948 err = pci_enable_device(pdev);
949 if (err)
950 goto err_recovery;
951
Loic Poulaine89878a2021-03-05 20:16:47 +0100952 pci_set_master(pdev);
953 pci_wake_from_d3(pdev, false);
954
Loic Poulaind3800c12021-03-05 20:16:48 +0100955 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
956 mhi_cntrl->ee != MHI_EE_AMSS)
957 return 0; /* Nothing to do at MHI level */
958
Loic Poulain73893372021-01-04 17:14:54 +0100959 /* Exit M3, transition to M0 state */
960 err = mhi_pm_resume(mhi_cntrl);
961 if (err) {
962 dev_err(&pdev->dev, "failed to resume device: %d\n", err);
963 goto err_recovery;
964 }
965
Loic Poulain8562d4f2021-01-04 17:14:56 +0100966 /* Resume health check */
967 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
968
Loic Poulaind3800c12021-03-05 20:16:48 +0100969 /* It can be a remote wakeup (no mhi runtime_get), update access time */
970 pm_runtime_mark_last_busy(dev);
971
Loic Poulain73893372021-01-04 17:14:54 +0100972 return 0;
973
974err_recovery:
Loic Poulaind3800c12021-03-05 20:16:48 +0100975 /* Do not fail to not mess up our PCI device state, the device likely
976 * lost power (d3cold) and we simply need to reset it from the recovery
977 * procedure, trigger the recovery asynchronously to prevent system
978 * suspend exit delaying.
979 */
Loic Poulain73893372021-01-04 17:14:54 +0100980 queue_work(system_long_wq, &mhi_pdev->recovery_work);
Loic Poulaind3800c12021-03-05 20:16:48 +0100981 pm_runtime_mark_last_busy(dev);
Loic Poulain73893372021-01-04 17:14:54 +0100982
Loic Poulaind3800c12021-03-05 20:16:48 +0100983 return 0;
984}
985
986static int __maybe_unused mhi_pci_suspend(struct device *dev)
987{
988 pm_runtime_disable(dev);
989 return mhi_pci_runtime_suspend(dev);
990}
991
992static int __maybe_unused mhi_pci_resume(struct device *dev)
993{
994 int ret;
995
996 /* Depending the platform, device may have lost power (d3cold), we need
997 * to resume it now to check its state and recover when necessary.
998 */
999 ret = mhi_pci_runtime_resume(dev);
1000 pm_runtime_enable(dev);
1001
1002 return ret;
Loic Poulain73893372021-01-04 17:14:54 +01001003}
1004
Loic Poulain5f0c2ee2021-06-06 21:07:41 +05301005static int __maybe_unused mhi_pci_freeze(struct device *dev)
1006{
1007 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1008 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
1009
1010 /* We want to stop all operations, hibernation does not guarantee that
1011 * device will be in the same state as before freezing, especially if
1012 * the intermediate restore kernel reinitializes MHI device with new
1013 * context.
1014 */
1015 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
1016 mhi_power_down(mhi_cntrl, false);
1017 mhi_unprepare_after_power_down(mhi_cntrl);
1018 }
1019
1020 return 0;
1021}
1022
1023static int __maybe_unused mhi_pci_restore(struct device *dev)
1024{
1025 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
1026
1027 /* Reinitialize the device */
1028 queue_work(system_long_wq, &mhi_pdev->recovery_work);
1029
1030 return 0;
1031}
1032
Loic Poulain73893372021-01-04 17:14:54 +01001033static const struct dev_pm_ops mhi_pci_pm_ops = {
Loic Poulaind3800c12021-03-05 20:16:48 +01001034 SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
Loic Poulain5f0c2ee2021-06-06 21:07:41 +05301035#ifdef CONFIG_PM_SLEEP
1036 .suspend = mhi_pci_suspend,
1037 .resume = mhi_pci_resume,
1038 .freeze = mhi_pci_freeze,
1039 .thaw = mhi_pci_restore,
1040 .restore = mhi_pci_restore,
1041#endif
Loic Poulain73893372021-01-04 17:14:54 +01001042};
1043
Loic Poulain855a70c2020-10-21 19:18:19 +02001044static struct pci_driver mhi_pci_driver = {
1045 .name = "mhi-pci-generic",
1046 .id_table = mhi_pci_id_table,
1047 .probe = mhi_pci_probe,
Loic Poulain8ccc3272021-01-04 17:14:53 +01001048 .remove = mhi_pci_remove,
Loic Poulain757072a2021-03-19 16:50:37 +01001049 .shutdown = mhi_pci_shutdown,
Loic Poulain8ccc3272021-01-04 17:14:53 +01001050 .err_handler = &mhi_pci_err_handler,
Loic Poulain73893372021-01-04 17:14:54 +01001051 .driver.pm = &mhi_pci_pm_ops
Loic Poulain855a70c2020-10-21 19:18:19 +02001052};
1053module_pci_driver(mhi_pci_driver);
1054
1055MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1056MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
1057MODULE_LICENSE("GPL");