blob: b79895810c52f8fc04d9ab4fcc063a012064d5f0 [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
Slark Xiaoe2022cb2021-11-26 16:19:51 +053023#define MHI_POST_RESET_DELAY_MS 2000
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)
Richard Laing5c2c8532021-07-15 09:18:05 +120035 * @mru_default: default MRU size for MBIM network packets
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +053036 * @sideband_wake: Devices using dedicated sideband GPIO for wakeup instead
37 * of inband wake support (such as sdx24)
Loic Poulain855a70c2020-10-21 19:18:19 +020038 */
39struct 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 Laing5c2c8532021-07-15 09:18:05 +120046 unsigned int mru_default;
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +053047 bool sideband_wake;
Loic Poulain855a70c2020-10-21 19:18:19 +020048};
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 Poulainb8a97f22021-07-16 13:21:06 +053080#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 Poulain9ea48ef2021-03-05 20:16:43 +010096#define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +020097 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +010098 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +020099 .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 Poulaineb967872021-01-04 17:14:52 +0100109#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 Poulainac4bf602021-03-05 20:16:44 +0100139#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 Poulain11134392021-04-07 10:41:00 +0200169#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 Poulain9ea48ef2021-03-05 20:16:43 +0100199#define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200200 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100201 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200202 .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 Poulain9ea48ef2021-03-05 20:16:43 +0100212#define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200213 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100214 .num_elements = el_count, \
Loic Poulainec7513692021-01-04 17:14:59 +0100215 .irq_moderation_ms = 1, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200216 .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
226static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = {
Loic Poulain4da3d072021-01-04 17:14:58 +0100227 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1),
228 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1),
Loic Poulain855a70c2020-10-21 19:18:19 +0200229 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 Poulainb8a97f22021-07-16 13:21:06 +0530234 MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 8, 0),
Loic Poulain11134392021-04-07 10:41:00 +0200235 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
236 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
Loic Poulain4da3d072021-01-04 17:14:58 +0100237 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2),
238 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3),
Loic Poulain855a70c2020-10-21 19:18:19 +0200239};
240
Loic Poulainb91c3b32021-01-05 17:44:36 +0100241static struct mhi_event_config modem_qcom_v1_mhi_events[] = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200242 /* first ring is control+data ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100243 MHI_EVENT_CONFIG_CTRL(0, 64),
Loic Poulain4da3d072021-01-04 17:14:58 +0100244 /* DIAG dedicated event ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100245 MHI_EVENT_CONFIG_DATA(1, 128),
Loic Poulain855a70c2020-10-21 19:18:19 +0200246 /* Hardware channels request dedicated hardware event rings */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100247 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
248 MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101)
Loic Poulain855a70c2020-10-21 19:18:19 +0200249};
250
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530251static const struct mhi_controller_config modem_qcom_v1_mhiv_config = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200252 .max_channels = 128,
Loic Poulain84026a52021-01-04 17:14:57 +0100253 .timeout_ms = 8000,
Loic Poulain855a70c2020-10-21 19:18:19 +0200254 .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 Bhatt49d38eb2021-04-02 14:33:19 -0700260static 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 Bhatt56f6f4c2021-07-16 13:21:04 +0530266 .dma_data_width = 32,
267 .sideband_wake = false,
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700268};
269
Loic Poulain855a70c2020-10-21 19:18:19 +0200270static 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 Laing5c2c8532021-07-15 09:18:05 +1200276 .dma_data_width = 32,
Jakub Kicinskid2e11fd2021-07-31 09:14:46 -0700277 .mru_default = 32768,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530278 .sideband_wake = false,
Loic Poulain855a70c2020-10-21 19:18:19 +0200279};
280
Loic Poulain59d05b72021-03-05 20:16:45 +0100281static 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 Bhatt56f6f4c2021-07-16 13:21:04 +0530286 .dma_data_width = 32,
287 .sideband_wake = true,
Loic Poulain59d05b72021-03-05 20:16:45 +0100288};
289
Loic Poulainac4bf602021-03-05 20:16:44 +0100290static 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 Poulain11134392021-04-07 10:41:00 +0200301 /* 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 Poulainac4bf602021-03-05 20:16:44 +0100304 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
308static 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 Sadhasivam5630c102021-04-08 19:15:29 +0530315static const struct mhi_controller_config modem_quectel_em1xx_config = {
Loic Poulainac4bf602021-03-05 20:16:44 +0100316 .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
324static 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 Bhatt56f6f4c2021-07-16 13:21:04 +0530329 .dma_data_width = 32,
330 .sideband_wake = true,
Loic Poulainac4bf602021-03-05 20:16:44 +0100331};
332
Jarvis Jiangaac42652021-04-08 02:55:24 -0700333static 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 Jiangc7711c22021-06-06 21:07:39 +0530340 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
341 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
Jarvis Jiangaac42652021-04-08 02:55:24 -0700342 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
346static 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 Sadhasivam5630c102021-04-08 19:15:29 +0530353static const struct mhi_controller_config modem_foxconn_sdx55_config = {
Jarvis Jiangaac42652021-04-08 02:55:24 -0700354 .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
362static 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 Bhatt56f6f4c2021-07-16 13:21:04 +0530368 .dma_data_width = 32,
Slark Xiaoa0572cea2022-02-05 19:27:30 +0530369 .mru_default = 32768,
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530370 .sideband_wake = false,
Jarvis Jiangaac42652021-04-08 02:55:24 -0700371};
372
ULRICH Thomas87693e02021-08-02 10:42:47 +0530373static 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
384static 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
391static 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
400static 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 Xiao05daa802022-02-05 19:27:31 +0530405 .mru_default = 32768,
ULRICH Thomas87693e02021-08-02 10:42:47 +0530406};
407
Thomas Perrot1dba0072021-12-16 13:42:27 +0530408static 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
423static 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
431static 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
440static 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 Poulain855a70c2020-10-21 19:18:19 +0200448static const struct pci_device_id mhi_pci_id_table[] = {
Thomas Perrot1dba0072021-12-16 13:42:27 +0530449 /* 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 Poulain855a70c2020-10-21 19:18:19 +0200452 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
453 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
Loic Poulain59d05b72021-03-05 20:16:45 +0100454 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
455 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
Loic Poulainac4bf602021-03-05 20:16:44 +0100456 { 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 Bhatt49d38eb2021-04-02 14:33:19 -0700460 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
461 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
Jarvis Jiangaac42652021-04-08 02:55:24 -0700462 /* 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 Xiaoc9825e62021-12-16 13:42:18 +0530471 /* 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 Thomas87693e02021-08-02 10:42:47 +0530474 /* MV31-W (Cinterion) */
475 { PCI_DEVICE(0x1269, 0x00b3),
476 .driver_data = (kernel_ulong_t) &mhi_mv31_info },
Loic Poulain855a70c2020-10-21 19:18:19 +0200477 { }
478};
479MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
480
Loic Poulain8ccc3272021-01-04 17:14:53 +0100481enum mhi_pci_device_status {
482 MHI_PCI_DEV_STARTED,
Loic Poulaind3800c12021-03-05 20:16:48 +0100483 MHI_PCI_DEV_SUSPENDED,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100484};
485
486struct mhi_pci_device {
487 struct mhi_controller mhi_cntrl;
488 struct pci_saved_state *pci_state;
Loic Poulain73893372021-01-04 17:14:54 +0100489 struct work_struct recovery_work;
Loic Poulain8562d4f2021-01-04 17:14:56 +0100490 struct timer_list health_check_timer;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100491 unsigned long status;
492};
493
Loic Poulain855a70c2020-10-21 19:18:19 +0200494static 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
501static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
502 void __iomem *addr, u32 val)
503{
504 writel(val, addr);
505}
506
507static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
508 enum mhi_callback cb)
509{
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100510 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
511
Loic Poulain855a70c2020-10-21 19:18:19 +0200512 /* Nothing to do for now */
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100513 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 Poulaind3800c12021-03-05 20:16:48 +0100517 pm_runtime_forbid(&pdev->dev);
518 break;
519 case MHI_CB_EE_MISSION_MODE:
520 pm_runtime_allow(&pdev->dev);
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100521 break;
522 default:
523 break;
524 }
Loic Poulain855a70c2020-10-21 19:18:19 +0200525}
526
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100527static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
528{
529 /* no-op */
530}
531
532static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
533{
534 /* no-op */
535}
536
537static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
538{
539 /* no-op */
540}
541
Loic Poulain8ccc3272021-01-04 17:14:53 +0100542static 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 Poulain855a70c2020-10-21 19:18:19 +0200556static 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 Bhatt3551a302021-08-02 10:42:52 +0530578 mhi_cntrl->reg_len = pci_resource_len(pdev, bar_num);
Loic Poulain855a70c2020-10-21 19:18:19 +0200579
Christophe JAILLETf3d13392021-12-16 13:42:22 +0530580 err = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
Loic Poulain855a70c2020-10-21 19:18:19 +0200581 if (err) {
582 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
583 return err;
584 }
585
Loic Poulain855a70c2020-10-21 19:18:19 +0200586 pci_set_master(pdev);
587
588 return 0;
589}
590
591static 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 Poulainb91c3b32021-01-05 17:44:36 +0100612 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 Poulain855a70c2020-10-21 19:18:19 +0200618 }
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
635static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
636{
Loic Poulaind3800c12021-03-05 20:16:48 +0100637 /* 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 Poulain855a70c2020-10-21 19:18:19 +0200641}
642
643static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
644{
Loic Poulaind3800c12021-03-05 20:16:48 +0100645 /* 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 Poulain855a70c2020-10-21 19:18:19 +0200650}
651
Loic Poulain73893372021-01-04 17:14:54 +0100652static 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 Poulain8562d4f2021-01-04 17:14:56 +0100662 del_timer(&mhi_pdev->health_check_timer);
Loic Poulaind3800c12021-03-05 20:16:48 +0100663 pm_runtime_forbid(&pdev->dev);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100664
Loic Poulain73893372021-01-04 17:14:54 +0100665 /* 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 Poulain73893372021-01-04 17:14:54 +0100671 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 Poulain8562d4f2021-01-04 17:14:56 +0100689 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain73893372021-01-04 17:14:54 +0100690 return;
691
692err_unprepare:
693 mhi_unprepare_after_power_down(mhi_cntrl);
694err_try_reset:
695 if (pci_reset_function(pdev))
696 dev_err(&pdev->dev, "Recovery failed\n");
697}
698
Loic Poulain8562d4f2021-01-04 17:14:56 +0100699static 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 Poulaind3800c12021-03-05 20:16:48 +0100704 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
705 test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
706 return;
707
Loic Poulain8562d4f2021-01-04 17:14:56 +0100708 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 Poulain855a70c2020-10-21 19:18:19 +0200718static 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 Poulain8ccc3272021-01-04 17:14:53 +0100722 struct mhi_pci_device *mhi_pdev;
Loic Poulain855a70c2020-10-21 19:18:19 +0200723 struct mhi_controller *mhi_cntrl;
724 int err;
725
726 dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name);
727
Loic Poulain8ccc3272021-01-04 17:14:53 +0100728 /* 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 Poulain855a70c2020-10-21 19:18:19 +0200731 return -ENOMEM;
732
Loic Poulain73893372021-01-04 17:14:54 +0100733 INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100734 timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
Loic Poulain73893372021-01-04 17:14:54 +0100735
Loic Poulain855a70c2020-10-21 19:18:19 +0200736 mhi_cntrl_config = info->config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100737 mhi_cntrl = &mhi_pdev->mhi_cntrl;
738
Loic Poulain855a70c2020-10-21 19:18:19 +0200739 mhi_cntrl->cntrl_dev = &pdev->dev;
740 mhi_cntrl->iova_start = 0;
Loic Poulain4ea6fa22020-12-02 09:12:30 +0100741 mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
Loic Poulain855a70c2020-10-21 19:18:19 +0200742 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 Laing5c2c8532021-07-15 09:18:05 +1200750 mhi_cntrl->mru = info->mru_default;
Loic Poulain855a70c2020-10-21 19:18:19 +0200751
Bhaumik Bhatt56f6f4c2021-07-16 13:21:04 +0530752 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 Poulain855a70c2020-10-21 19:18:19 +0200757
758 err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
759 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100760 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200761
762 err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
763 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100764 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200765
Loic Poulain8ccc3272021-01-04 17:14:53 +0100766 pci_set_drvdata(pdev, mhi_pdev);
767
Loic Poulaine89878a2021-03-05 20:16:47 +0100768 /* 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 Poulain8ccc3272021-01-04 17:14:53 +0100771 pci_save_state(pdev);
772 mhi_pdev->pci_state = pci_store_saved_state(pdev);
Loic Poulaine89878a2021-03-05 20:16:47 +0100773 pci_load_saved_state(pdev, NULL);
Loic Poulain855a70c2020-10-21 19:18:19 +0200774
Loic Poulainb012ee62021-01-04 17:14:55 +0100775 pci_enable_pcie_error_reporting(pdev);
776
Loic Poulain855a70c2020-10-21 19:18:19 +0200777 err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
778 if (err)
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530779 goto err_disable_reporting;
Loic Poulain855a70c2020-10-21 19:18:19 +0200780
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 Poulain8ccc3272021-01-04 17:14:53 +0100794 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
795
Loic Poulain8562d4f2021-01-04 17:14:56 +0100796 /* start health check */
797 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
798
Loic Poulaind3800c12021-03-05 20:16:48 +0100799 /* 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 Poulain855a70c2020-10-21 19:18:19 +0200807 return 0;
808
809err_unprepare:
810 mhi_unprepare_after_power_down(mhi_cntrl);
811err_unregister:
812 mhi_unregister_controller(mhi_cntrl);
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530813err_disable_reporting:
814 pci_disable_pcie_error_reporting(pdev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200815
816 return err;
817}
818
819static void mhi_pci_remove(struct pci_dev *pdev)
820{
Loic Poulain8ccc3272021-01-04 17:14:53 +0100821 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
822 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulain855a70c2020-10-21 19:18:19 +0200823
Wei Yongjun0b678082021-06-06 21:07:40 +0530824 del_timer_sync(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100825 cancel_work_sync(&mhi_pdev->recovery_work);
826
Loic Poulain8ccc3272021-01-04 17:14:53 +0100827 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 Poulaind3800c12021-03-05 20:16:48 +0100832 /* balancing probe put_noidle */
833 if (pci_pme_capable(pdev, PCI_D3hot))
834 pm_runtime_get_noresume(&pdev->dev);
835
Loic Poulain855a70c2020-10-21 19:18:19 +0200836 mhi_unregister_controller(mhi_cntrl);
Christophe JAILLETa25d1442021-06-21 21:46:13 +0530837 pci_disable_pcie_error_reporting(pdev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200838}
839
Loic Poulain757072a2021-03-19 16:50:37 +0100840static 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 Poulain8ccc3272021-01-04 17:14:53 +0100846static 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 Poulain8562d4f2021-01-04 17:14:56 +0100853 del_timer(&mhi_pdev->health_check_timer);
854
Loic Poulain8ccc3272021-01-04 17:14:53 +0100855 /* 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
868static 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 Poulain8562d4f2021-01-04 17:14:56 +0100898 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain8ccc3272021-01-04 17:14:53 +0100899}
900
Loic Poulainb012ee62021-01-04 17:14:55 +0100901static 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
926static 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
936static 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 Poulain8ccc3272021-01-04 17:14:53 +0100945static const struct pci_error_handlers mhi_pci_err_handler = {
Loic Poulainb012ee62021-01-04 17:14:55 +0100946 .error_detected = mhi_pci_error_detected,
947 .slot_reset = mhi_pci_slot_reset,
948 .resume = mhi_pci_io_resume,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100949 .reset_prepare = mhi_pci_reset_prepare,
950 .reset_done = mhi_pci_reset_done,
951};
952
Loic Poulaind3800c12021-03-05 20:16:48 +0100953static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100954{
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 Poulaind3800c12021-03-05 20:16:48 +0100958 int err;
959
960 if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
961 return 0;
Loic Poulain73893372021-01-04 17:14:54 +0100962
Loic Poulain8562d4f2021-01-04 17:14:56 +0100963 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100964 cancel_work_sync(&mhi_pdev->recovery_work);
965
Loic Poulaind3800c12021-03-05 20:16:48 +0100966 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 Poulain73893372021-01-04 17:14:54 +0100969
Loic Poulaind3800c12021-03-05 20:16:48 +0100970 /* 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
978pci_suspend:
Loic Poulain73893372021-01-04 17:14:54 +0100979 pci_disable_device(pdev);
980 pci_wake_from_d3(pdev, true);
Loic Poulain73893372021-01-04 17:14:54 +0100981
982 return 0;
983}
984
Loic Poulaind3800c12021-03-05 20:16:48 +0100985static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100986{
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 Poulaind3800c12021-03-05 20:16:48 +0100992 if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
993 return 0;
994
Loic Poulain73893372021-01-04 17:14:54 +0100995 err = pci_enable_device(pdev);
996 if (err)
997 goto err_recovery;
998
Loic Poulaine89878a2021-03-05 20:16:47 +0100999 pci_set_master(pdev);
1000 pci_wake_from_d3(pdev, false);
1001
Loic Poulaind3800c12021-03-05 20:16:48 +01001002 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 Poulain73893372021-01-04 17:14:54 +01001006 /* 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 Poulain8562d4f2021-01-04 17:14:56 +01001013 /* Resume health check */
1014 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
1015
Loic Poulaind3800c12021-03-05 20:16:48 +01001016 /* It can be a remote wakeup (no mhi runtime_get), update access time */
1017 pm_runtime_mark_last_busy(dev);
1018
Loic Poulain73893372021-01-04 17:14:54 +01001019 return 0;
1020
1021err_recovery:
Loic Poulaind3800c12021-03-05 20:16:48 +01001022 /* 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 Poulain73893372021-01-04 17:14:54 +01001027 queue_work(system_long_wq, &mhi_pdev->recovery_work);
Loic Poulaind3800c12021-03-05 20:16:48 +01001028 pm_runtime_mark_last_busy(dev);
Loic Poulain73893372021-01-04 17:14:54 +01001029
Loic Poulaind3800c12021-03-05 20:16:48 +01001030 return 0;
1031}
1032
1033static int __maybe_unused mhi_pci_suspend(struct device *dev)
1034{
1035 pm_runtime_disable(dev);
1036 return mhi_pci_runtime_suspend(dev);
1037}
1038
1039static 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 Poulain73893372021-01-04 17:14:54 +01001050}
1051
Loic Poulain5f0c2ee2021-06-06 21:07:41 +05301052static 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 Poulainf77097e2021-12-16 13:42:19 +05301063 mhi_power_down(mhi_cntrl, true);
Loic Poulain5f0c2ee2021-06-06 21:07:41 +05301064 mhi_unprepare_after_power_down(mhi_cntrl);
1065 }
1066
1067 return 0;
1068}
1069
1070static 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 Poulain73893372021-01-04 17:14:54 +01001080static const struct dev_pm_ops mhi_pci_pm_ops = {
Loic Poulaind3800c12021-03-05 20:16:48 +01001081 SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
Loic Poulain5f0c2ee2021-06-06 21:07:41 +05301082#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 Poulain73893372021-01-04 17:14:54 +01001089};
1090
Loic Poulain855a70c2020-10-21 19:18:19 +02001091static struct pci_driver mhi_pci_driver = {
1092 .name = "mhi-pci-generic",
1093 .id_table = mhi_pci_id_table,
1094 .probe = mhi_pci_probe,
Loic Poulain8ccc3272021-01-04 17:14:53 +01001095 .remove = mhi_pci_remove,
Loic Poulain757072a2021-03-19 16:50:37 +01001096 .shutdown = mhi_pci_shutdown,
Loic Poulain8ccc3272021-01-04 17:14:53 +01001097 .err_handler = &mhi_pci_err_handler,
Loic Poulain73893372021-01-04 17:14:54 +01001098 .driver.pm = &mhi_pci_pm_ops
Loic Poulain855a70c2020-10-21 19:18:19 +02001099};
1100module_pci_driver(mhi_pci_driver);
1101
1102MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1103MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
1104MODULE_LICENSE("GPL");