blob: 7c810f02a2ef4d164f959e3a02a7aa6684bfe32b [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)
35 */
36struct mhi_pci_dev_info {
37 const struct mhi_controller_config *config;
38 const char *name;
39 const char *fw;
40 const char *edl;
41 unsigned int bar_num;
42 unsigned int dma_data_width;
43};
44
45#define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \
46 { \
47 .num = ch_num, \
48 .name = ch_name, \
49 .num_elements = el_count, \
50 .event_ring = ev_ring, \
51 .dir = DMA_TO_DEVICE, \
52 .ee_mask = BIT(MHI_EE_AMSS), \
53 .pollcfg = 0, \
54 .doorbell = MHI_DB_BRST_DISABLE, \
55 .lpm_notify = false, \
56 .offload_channel = false, \
57 .doorbell_mode_switch = false, \
58 } \
59
60#define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \
61 { \
62 .num = ch_num, \
63 .name = ch_name, \
64 .num_elements = el_count, \
65 .event_ring = ev_ring, \
66 .dir = DMA_FROM_DEVICE, \
67 .ee_mask = BIT(MHI_EE_AMSS), \
68 .pollcfg = 0, \
69 .doorbell = MHI_DB_BRST_DISABLE, \
70 .lpm_notify = false, \
71 .offload_channel = false, \
72 .doorbell_mode_switch = false, \
73 }
74
Loic Poulain9ea48ef2021-03-05 20:16:43 +010075#define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +020076 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +010077 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +020078 .irq_moderation_ms = 0, \
79 .irq = (ev_ring) + 1, \
80 .priority = 1, \
81 .mode = MHI_DB_BRST_DISABLE, \
82 .data_type = MHI_ER_CTRL, \
83 .hardware_event = false, \
84 .client_managed = false, \
85 .offload_channel = false, \
86 }
87
Loic Poulaineb967872021-01-04 17:14:52 +010088#define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \
89 { \
90 .num = ch_num, \
91 .name = ch_name, \
92 .num_elements = el_count, \
93 .event_ring = ev_ring, \
94 .dir = DMA_TO_DEVICE, \
95 .ee_mask = BIT(MHI_EE_AMSS), \
96 .pollcfg = 0, \
97 .doorbell = MHI_DB_BRST_ENABLE, \
98 .lpm_notify = false, \
99 .offload_channel = false, \
100 .doorbell_mode_switch = true, \
101 } \
102
103#define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \
104 { \
105 .num = ch_num, \
106 .name = ch_name, \
107 .num_elements = el_count, \
108 .event_ring = ev_ring, \
109 .dir = DMA_FROM_DEVICE, \
110 .ee_mask = BIT(MHI_EE_AMSS), \
111 .pollcfg = 0, \
112 .doorbell = MHI_DB_BRST_ENABLE, \
113 .lpm_notify = false, \
114 .offload_channel = false, \
115 .doorbell_mode_switch = true, \
116 }
117
Loic Poulainac4bf602021-03-05 20:16:44 +0100118#define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \
119 { \
120 .num = ch_num, \
121 .name = ch_name, \
122 .num_elements = el_count, \
123 .event_ring = ev_ring, \
124 .dir = DMA_TO_DEVICE, \
125 .ee_mask = BIT(MHI_EE_SBL), \
126 .pollcfg = 0, \
127 .doorbell = MHI_DB_BRST_DISABLE, \
128 .lpm_notify = false, \
129 .offload_channel = false, \
130 .doorbell_mode_switch = false, \
131 } \
132
133#define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \
134 { \
135 .num = ch_num, \
136 .name = ch_name, \
137 .num_elements = el_count, \
138 .event_ring = ev_ring, \
139 .dir = DMA_FROM_DEVICE, \
140 .ee_mask = BIT(MHI_EE_SBL), \
141 .pollcfg = 0, \
142 .doorbell = MHI_DB_BRST_DISABLE, \
143 .lpm_notify = false, \
144 .offload_channel = false, \
145 .doorbell_mode_switch = false, \
146 }
147
Loic Poulain11134392021-04-07 10:41:00 +0200148#define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \
149 { \
150 .num = ch_num, \
151 .name = ch_name, \
152 .num_elements = el_count, \
153 .event_ring = ev_ring, \
154 .dir = DMA_TO_DEVICE, \
155 .ee_mask = BIT(MHI_EE_FP), \
156 .pollcfg = 0, \
157 .doorbell = MHI_DB_BRST_DISABLE, \
158 .lpm_notify = false, \
159 .offload_channel = false, \
160 .doorbell_mode_switch = false, \
161 } \
162
163#define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \
164 { \
165 .num = ch_num, \
166 .name = ch_name, \
167 .num_elements = el_count, \
168 .event_ring = ev_ring, \
169 .dir = DMA_FROM_DEVICE, \
170 .ee_mask = BIT(MHI_EE_FP), \
171 .pollcfg = 0, \
172 .doorbell = MHI_DB_BRST_DISABLE, \
173 .lpm_notify = false, \
174 .offload_channel = false, \
175 .doorbell_mode_switch = false, \
176 }
177
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100178#define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200179 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100180 .num_elements = el_count, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200181 .irq_moderation_ms = 5, \
182 .irq = (ev_ring) + 1, \
183 .priority = 1, \
184 .mode = MHI_DB_BRST_DISABLE, \
185 .data_type = MHI_ER_DATA, \
186 .hardware_event = false, \
187 .client_managed = false, \
188 .offload_channel = false, \
189 }
190
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100191#define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \
Loic Poulain855a70c2020-10-21 19:18:19 +0200192 { \
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100193 .num_elements = el_count, \
Loic Poulainec7513692021-01-04 17:14:59 +0100194 .irq_moderation_ms = 1, \
Loic Poulain855a70c2020-10-21 19:18:19 +0200195 .irq = (ev_ring) + 1, \
196 .priority = 1, \
197 .mode = MHI_DB_BRST_DISABLE, \
198 .data_type = MHI_ER_DATA, \
199 .hardware_event = true, \
200 .client_managed = false, \
201 .offload_channel = false, \
202 .channel = ch_num, \
203 }
204
205static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = {
Loic Poulain4da3d072021-01-04 17:14:58 +0100206 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1),
207 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1),
Loic Poulain855a70c2020-10-21 19:18:19 +0200208 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0),
209 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0),
210 MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0),
211 MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0),
212 MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0),
213 MHI_CHANNEL_CONFIG_DL(21, "IPCR", 8, 0),
Loic Poulain11134392021-04-07 10:41:00 +0200214 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
215 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
Loic Poulain4da3d072021-01-04 17:14:58 +0100216 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2),
217 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3),
Loic Poulain855a70c2020-10-21 19:18:19 +0200218};
219
Loic Poulainb91c3b32021-01-05 17:44:36 +0100220static struct mhi_event_config modem_qcom_v1_mhi_events[] = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200221 /* first ring is control+data ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100222 MHI_EVENT_CONFIG_CTRL(0, 64),
Loic Poulain4da3d072021-01-04 17:14:58 +0100223 /* DIAG dedicated event ring */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100224 MHI_EVENT_CONFIG_DATA(1, 128),
Loic Poulain855a70c2020-10-21 19:18:19 +0200225 /* Hardware channels request dedicated hardware event rings */
Loic Poulain9ea48ef2021-03-05 20:16:43 +0100226 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
227 MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101)
Loic Poulain855a70c2020-10-21 19:18:19 +0200228};
229
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530230static const struct mhi_controller_config modem_qcom_v1_mhiv_config = {
Loic Poulain855a70c2020-10-21 19:18:19 +0200231 .max_channels = 128,
Loic Poulain84026a52021-01-04 17:14:57 +0100232 .timeout_ms = 8000,
Loic Poulain855a70c2020-10-21 19:18:19 +0200233 .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels),
234 .ch_cfg = modem_qcom_v1_mhi_channels,
235 .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events),
236 .event_cfg = modem_qcom_v1_mhi_events,
237};
238
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700239static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = {
240 .name = "qcom-sdx65m",
241 .fw = "qcom/sdx65m/xbl.elf",
242 .edl = "qcom/sdx65m/edl.mbn",
243 .config = &modem_qcom_v1_mhiv_config,
244 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
245 .dma_data_width = 32
246};
247
Loic Poulain855a70c2020-10-21 19:18:19 +0200248static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = {
249 .name = "qcom-sdx55m",
250 .fw = "qcom/sdx55m/sbl1.mbn",
251 .edl = "qcom/sdx55m/edl.mbn",
252 .config = &modem_qcom_v1_mhiv_config,
253 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
254 .dma_data_width = 32
255};
256
Loic Poulain59d05b72021-03-05 20:16:45 +0100257static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = {
258 .name = "qcom-sdx24",
259 .edl = "qcom/prog_firehose_sdx24.mbn",
260 .config = &modem_qcom_v1_mhiv_config,
261 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
262 .dma_data_width = 32
263};
264
Loic Poulainac4bf602021-03-05 20:16:44 +0100265static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = {
266 MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0),
267 MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0),
268 MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
269 MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0),
270 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
271 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
272 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
273 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
274 MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
275 MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
Loic Poulain11134392021-04-07 10:41:00 +0200276 /* The EDL firmware is a flash-programmer exposing firehose protocol */
277 MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
278 MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
Loic Poulainac4bf602021-03-05 20:16:44 +0100279 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
280 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
281};
282
283static struct mhi_event_config mhi_quectel_em1xx_events[] = {
284 MHI_EVENT_CONFIG_CTRL(0, 128),
285 MHI_EVENT_CONFIG_DATA(1, 128),
286 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
287 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
288};
289
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530290static const struct mhi_controller_config modem_quectel_em1xx_config = {
Loic Poulainac4bf602021-03-05 20:16:44 +0100291 .max_channels = 128,
292 .timeout_ms = 20000,
293 .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels),
294 .ch_cfg = mhi_quectel_em1xx_channels,
295 .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events),
296 .event_cfg = mhi_quectel_em1xx_events,
297};
298
299static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = {
300 .name = "quectel-em1xx",
301 .edl = "qcom/prog_firehose_sdx24.mbn",
302 .config = &modem_quectel_em1xx_config,
303 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
304 .dma_data_width = 32
305};
306
Jarvis Jiangaac42652021-04-08 02:55:24 -0700307static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = {
308 MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0),
309 MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0),
310 MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
311 MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
312 MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
313 MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
314 MHI_CHANNEL_CONFIG_UL(32, "AT", 32, 0),
315 MHI_CHANNEL_CONFIG_DL(33, "AT", 32, 0),
316 MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
317 MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
318};
319
320static struct mhi_event_config mhi_foxconn_sdx55_events[] = {
321 MHI_EVENT_CONFIG_CTRL(0, 128),
322 MHI_EVENT_CONFIG_DATA(1, 128),
323 MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
324 MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
325};
326
Manivannan Sadhasivam5630c102021-04-08 19:15:29 +0530327static const struct mhi_controller_config modem_foxconn_sdx55_config = {
Jarvis Jiangaac42652021-04-08 02:55:24 -0700328 .max_channels = 128,
329 .timeout_ms = 20000,
330 .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels),
331 .ch_cfg = mhi_foxconn_sdx55_channels,
332 .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events),
333 .event_cfg = mhi_foxconn_sdx55_events,
334};
335
336static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = {
337 .name = "foxconn-sdx55",
338 .fw = "qcom/sdx55m/sbl1.mbn",
339 .edl = "qcom/sdx55m/edl.mbn",
340 .config = &modem_foxconn_sdx55_config,
341 .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
342 .dma_data_width = 32
343};
344
Loic Poulain855a70c2020-10-21 19:18:19 +0200345static const struct pci_device_id mhi_pci_id_table[] = {
346 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
347 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
Loic Poulain59d05b72021-03-05 20:16:45 +0100348 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
349 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
Loic Poulainac4bf602021-03-05 20:16:44 +0100350 { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */
351 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
352 { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */
353 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700354 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
355 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
Jarvis Jiangaac42652021-04-08 02:55:24 -0700356 /* T99W175 (sdx55), Both for eSIM and Non-eSIM */
357 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab),
358 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
359 /* DW5930e (sdx55), With eSIM, It's also T99W175 */
360 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0),
361 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
362 /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */
363 { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1),
364 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
Loic Poulain855a70c2020-10-21 19:18:19 +0200365 { }
366};
367MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
368
Loic Poulain8ccc3272021-01-04 17:14:53 +0100369enum mhi_pci_device_status {
370 MHI_PCI_DEV_STARTED,
Loic Poulaind3800c12021-03-05 20:16:48 +0100371 MHI_PCI_DEV_SUSPENDED,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100372};
373
374struct mhi_pci_device {
375 struct mhi_controller mhi_cntrl;
376 struct pci_saved_state *pci_state;
Loic Poulain73893372021-01-04 17:14:54 +0100377 struct work_struct recovery_work;
Loic Poulain8562d4f2021-01-04 17:14:56 +0100378 struct timer_list health_check_timer;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100379 unsigned long status;
380};
381
Loic Poulain855a70c2020-10-21 19:18:19 +0200382static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl,
383 void __iomem *addr, u32 *out)
384{
385 *out = readl(addr);
386 return 0;
387}
388
389static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
390 void __iomem *addr, u32 val)
391{
392 writel(val, addr);
393}
394
395static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
396 enum mhi_callback cb)
397{
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100398 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
399
Loic Poulain855a70c2020-10-21 19:18:19 +0200400 /* Nothing to do for now */
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100401 switch (cb) {
402 case MHI_CB_FATAL_ERROR:
403 case MHI_CB_SYS_ERROR:
404 dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb);
Loic Poulaind3800c12021-03-05 20:16:48 +0100405 pm_runtime_forbid(&pdev->dev);
406 break;
407 case MHI_CB_EE_MISSION_MODE:
408 pm_runtime_allow(&pdev->dev);
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100409 break;
410 default:
411 break;
412 }
Loic Poulain855a70c2020-10-21 19:18:19 +0200413}
414
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100415static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
416{
417 /* no-op */
418}
419
420static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
421{
422 /* no-op */
423}
424
425static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
426{
427 /* no-op */
428}
429
Loic Poulain8ccc3272021-01-04 17:14:53 +0100430static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl)
431{
432 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
433 u16 vendor = 0;
434
435 if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor))
436 return false;
437
438 if (vendor == (u16) ~0 || vendor == 0)
439 return false;
440
441 return true;
442}
443
Loic Poulain855a70c2020-10-21 19:18:19 +0200444static int mhi_pci_claim(struct mhi_controller *mhi_cntrl,
445 unsigned int bar_num, u64 dma_mask)
446{
447 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
448 int err;
449
450 err = pci_assign_resource(pdev, bar_num);
451 if (err)
452 return err;
453
454 err = pcim_enable_device(pdev);
455 if (err) {
456 dev_err(&pdev->dev, "failed to enable pci device: %d\n", err);
457 return err;
458 }
459
460 err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev));
461 if (err) {
462 dev_err(&pdev->dev, "failed to map pci region: %d\n", err);
463 return err;
464 }
465 mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num];
466
467 err = pci_set_dma_mask(pdev, dma_mask);
468 if (err) {
469 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
470 return err;
471 }
472
473 err = pci_set_consistent_dma_mask(pdev, dma_mask);
474 if (err) {
475 dev_err(&pdev->dev, "set consistent dma mask failed\n");
476 return err;
477 }
478
479 pci_set_master(pdev);
480
481 return 0;
482}
483
484static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl,
485 const struct mhi_controller_config *mhi_cntrl_config)
486{
487 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
488 int nr_vectors, i;
489 int *irq;
490
491 /*
492 * Alloc one MSI vector for BHI + one vector per event ring, ideally...
493 * No explicit pci_free_irq_vectors required, done by pcim_release.
494 */
495 mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events;
496
497 nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI);
498 if (nr_vectors < 0) {
499 dev_err(&pdev->dev, "Error allocating MSI vectors %d\n",
500 nr_vectors);
501 return nr_vectors;
502 }
503
504 if (nr_vectors < mhi_cntrl->nr_irqs) {
Loic Poulainb91c3b32021-01-05 17:44:36 +0100505 dev_warn(&pdev->dev, "using shared MSI\n");
506
507 /* Patch msi vectors, use only one (shared) */
508 for (i = 0; i < mhi_cntrl_config->num_events; i++)
509 mhi_cntrl_config->event_cfg[i].irq = 0;
510 mhi_cntrl->nr_irqs = 1;
Loic Poulain855a70c2020-10-21 19:18:19 +0200511 }
512
513 irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL);
514 if (!irq)
515 return -ENOMEM;
516
517 for (i = 0; i < mhi_cntrl->nr_irqs; i++) {
518 int vector = i >= nr_vectors ? (nr_vectors - 1) : i;
519
520 irq[i] = pci_irq_vector(pdev, vector);
521 }
522
523 mhi_cntrl->irq = irq;
524
525 return 0;
526}
527
528static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
529{
Loic Poulaind3800c12021-03-05 20:16:48 +0100530 /* The runtime_get() MHI callback means:
531 * Do whatever is requested to leave M3.
532 */
533 return pm_runtime_get(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200534}
535
536static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
537{
Loic Poulaind3800c12021-03-05 20:16:48 +0100538 /* The runtime_put() MHI callback means:
539 * Device can be moved in M3 state.
540 */
541 pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev);
542 pm_runtime_put(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200543}
544
Loic Poulain73893372021-01-04 17:14:54 +0100545static void mhi_pci_recovery_work(struct work_struct *work)
546{
547 struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device,
548 recovery_work);
549 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
550 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
551 int err;
552
553 dev_warn(&pdev->dev, "device recovery started\n");
554
Loic Poulain8562d4f2021-01-04 17:14:56 +0100555 del_timer(&mhi_pdev->health_check_timer);
Loic Poulaind3800c12021-03-05 20:16:48 +0100556 pm_runtime_forbid(&pdev->dev);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100557
Loic Poulain73893372021-01-04 17:14:54 +0100558 /* Clean up MHI state */
559 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
560 mhi_power_down(mhi_cntrl, false);
561 mhi_unprepare_after_power_down(mhi_cntrl);
562 }
563
Loic Poulain73893372021-01-04 17:14:54 +0100564 pci_set_power_state(pdev, PCI_D0);
565 pci_load_saved_state(pdev, mhi_pdev->pci_state);
566 pci_restore_state(pdev);
567
568 if (!mhi_pci_is_alive(mhi_cntrl))
569 goto err_try_reset;
570
571 err = mhi_prepare_for_power_up(mhi_cntrl);
572 if (err)
573 goto err_try_reset;
574
575 err = mhi_sync_power_up(mhi_cntrl);
576 if (err)
577 goto err_unprepare;
578
579 dev_dbg(&pdev->dev, "Recovery completed\n");
580
581 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100582 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain73893372021-01-04 17:14:54 +0100583 return;
584
585err_unprepare:
586 mhi_unprepare_after_power_down(mhi_cntrl);
587err_try_reset:
588 if (pci_reset_function(pdev))
589 dev_err(&pdev->dev, "Recovery failed\n");
590}
591
Loic Poulain8562d4f2021-01-04 17:14:56 +0100592static void health_check(struct timer_list *t)
593{
594 struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer);
595 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
596
Loic Poulaind3800c12021-03-05 20:16:48 +0100597 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
598 test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
599 return;
600
Loic Poulain8562d4f2021-01-04 17:14:56 +0100601 if (!mhi_pci_is_alive(mhi_cntrl)) {
602 dev_err(mhi_cntrl->cntrl_dev, "Device died\n");
603 queue_work(system_long_wq, &mhi_pdev->recovery_work);
604 return;
605 }
606
607 /* reschedule in two seconds */
608 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
609}
610
Loic Poulain855a70c2020-10-21 19:18:19 +0200611static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
612{
613 const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data;
614 const struct mhi_controller_config *mhi_cntrl_config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100615 struct mhi_pci_device *mhi_pdev;
Loic Poulain855a70c2020-10-21 19:18:19 +0200616 struct mhi_controller *mhi_cntrl;
617 int err;
618
619 dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name);
620
Loic Poulain8ccc3272021-01-04 17:14:53 +0100621 /* mhi_pdev.mhi_cntrl must be zero-initialized */
622 mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL);
623 if (!mhi_pdev)
Loic Poulain855a70c2020-10-21 19:18:19 +0200624 return -ENOMEM;
625
Loic Poulain73893372021-01-04 17:14:54 +0100626 INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100627 timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
Loic Poulain73893372021-01-04 17:14:54 +0100628
Loic Poulain855a70c2020-10-21 19:18:19 +0200629 mhi_cntrl_config = info->config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100630 mhi_cntrl = &mhi_pdev->mhi_cntrl;
631
Loic Poulain855a70c2020-10-21 19:18:19 +0200632 mhi_cntrl->cntrl_dev = &pdev->dev;
633 mhi_cntrl->iova_start = 0;
Loic Poulain4ea6fa22020-12-02 09:12:30 +0100634 mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
Loic Poulain855a70c2020-10-21 19:18:19 +0200635 mhi_cntrl->fw_image = info->fw;
636 mhi_cntrl->edl_image = info->edl;
637
638 mhi_cntrl->read_reg = mhi_pci_read_reg;
639 mhi_cntrl->write_reg = mhi_pci_write_reg;
640 mhi_cntrl->status_cb = mhi_pci_status_cb;
641 mhi_cntrl->runtime_get = mhi_pci_runtime_get;
642 mhi_cntrl->runtime_put = mhi_pci_runtime_put;
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100643 mhi_cntrl->wake_get = mhi_pci_wake_get_nop;
644 mhi_cntrl->wake_put = mhi_pci_wake_put_nop;
645 mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop;
Loic Poulain855a70c2020-10-21 19:18:19 +0200646
647 err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
648 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100649 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200650
651 err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
652 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100653 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200654
Loic Poulain8ccc3272021-01-04 17:14:53 +0100655 pci_set_drvdata(pdev, mhi_pdev);
656
Loic Poulaine89878a2021-03-05 20:16:47 +0100657 /* Have stored pci confspace at hand for restore in sudden PCI error.
658 * cache the state locally and discard the PCI core one.
659 */
Loic Poulain8ccc3272021-01-04 17:14:53 +0100660 pci_save_state(pdev);
661 mhi_pdev->pci_state = pci_store_saved_state(pdev);
Loic Poulaine89878a2021-03-05 20:16:47 +0100662 pci_load_saved_state(pdev, NULL);
Loic Poulain855a70c2020-10-21 19:18:19 +0200663
Loic Poulainb012ee62021-01-04 17:14:55 +0100664 pci_enable_pcie_error_reporting(pdev);
665
Loic Poulain855a70c2020-10-21 19:18:19 +0200666 err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
667 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100668 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200669
670 /* MHI bus does not power up the controller by default */
671 err = mhi_prepare_for_power_up(mhi_cntrl);
672 if (err) {
673 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
674 goto err_unregister;
675 }
676
677 err = mhi_sync_power_up(mhi_cntrl);
678 if (err) {
679 dev_err(&pdev->dev, "failed to power up MHI controller\n");
680 goto err_unprepare;
681 }
682
Loic Poulain8ccc3272021-01-04 17:14:53 +0100683 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
684
Loic Poulain8562d4f2021-01-04 17:14:56 +0100685 /* start health check */
686 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
687
Loic Poulaind3800c12021-03-05 20:16:48 +0100688 /* Only allow runtime-suspend if PME capable (for wakeup) */
689 if (pci_pme_capable(pdev, PCI_D3hot)) {
690 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
691 pm_runtime_use_autosuspend(&pdev->dev);
692 pm_runtime_mark_last_busy(&pdev->dev);
693 pm_runtime_put_noidle(&pdev->dev);
694 }
695
Loic Poulain855a70c2020-10-21 19:18:19 +0200696 return 0;
697
698err_unprepare:
699 mhi_unprepare_after_power_down(mhi_cntrl);
700err_unregister:
701 mhi_unregister_controller(mhi_cntrl);
Loic Poulain855a70c2020-10-21 19:18:19 +0200702
703 return err;
704}
705
706static void mhi_pci_remove(struct pci_dev *pdev)
707{
Loic Poulain8ccc3272021-01-04 17:14:53 +0100708 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
709 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulain855a70c2020-10-21 19:18:19 +0200710
Loic Poulain8562d4f2021-01-04 17:14:56 +0100711 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100712 cancel_work_sync(&mhi_pdev->recovery_work);
713
Loic Poulain8ccc3272021-01-04 17:14:53 +0100714 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
715 mhi_power_down(mhi_cntrl, true);
716 mhi_unprepare_after_power_down(mhi_cntrl);
717 }
718
Loic Poulaind3800c12021-03-05 20:16:48 +0100719 /* balancing probe put_noidle */
720 if (pci_pme_capable(pdev, PCI_D3hot))
721 pm_runtime_get_noresume(&pdev->dev);
722
Loic Poulain855a70c2020-10-21 19:18:19 +0200723 mhi_unregister_controller(mhi_cntrl);
Loic Poulain855a70c2020-10-21 19:18:19 +0200724}
725
Loic Poulain757072a2021-03-19 16:50:37 +0100726static void mhi_pci_shutdown(struct pci_dev *pdev)
727{
728 mhi_pci_remove(pdev);
729 pci_set_power_state(pdev, PCI_D3hot);
730}
731
Loic Poulain8ccc3272021-01-04 17:14:53 +0100732static void mhi_pci_reset_prepare(struct pci_dev *pdev)
733{
734 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
735 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
736
737 dev_info(&pdev->dev, "reset\n");
738
Loic Poulain8562d4f2021-01-04 17:14:56 +0100739 del_timer(&mhi_pdev->health_check_timer);
740
Loic Poulain8ccc3272021-01-04 17:14:53 +0100741 /* Clean up MHI state */
742 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
743 mhi_power_down(mhi_cntrl, false);
744 mhi_unprepare_after_power_down(mhi_cntrl);
745 }
746
747 /* cause internal device reset */
748 mhi_soc_reset(mhi_cntrl);
749
750 /* Be sure device reset has been executed */
751 msleep(MHI_POST_RESET_DELAY_MS);
752}
753
754static void mhi_pci_reset_done(struct pci_dev *pdev)
755{
756 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
757 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
758 int err;
759
760 /* Restore initial known working PCI state */
761 pci_load_saved_state(pdev, mhi_pdev->pci_state);
762 pci_restore_state(pdev);
763
764 /* Is device status available ? */
765 if (!mhi_pci_is_alive(mhi_cntrl)) {
766 dev_err(&pdev->dev, "reset failed\n");
767 return;
768 }
769
770 err = mhi_prepare_for_power_up(mhi_cntrl);
771 if (err) {
772 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
773 return;
774 }
775
776 err = mhi_sync_power_up(mhi_cntrl);
777 if (err) {
778 dev_err(&pdev->dev, "failed to power up MHI controller\n");
779 mhi_unprepare_after_power_down(mhi_cntrl);
780 return;
781 }
782
783 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100784 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain8ccc3272021-01-04 17:14:53 +0100785}
786
Loic Poulainb012ee62021-01-04 17:14:55 +0100787static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev,
788 pci_channel_state_t state)
789{
790 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
791 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
792
793 dev_err(&pdev->dev, "PCI error detected, state = %u\n", state);
794
795 if (state == pci_channel_io_perm_failure)
796 return PCI_ERS_RESULT_DISCONNECT;
797
798 /* Clean up MHI state */
799 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
800 mhi_power_down(mhi_cntrl, false);
801 mhi_unprepare_after_power_down(mhi_cntrl);
802 } else {
803 /* Nothing to do */
804 return PCI_ERS_RESULT_RECOVERED;
805 }
806
807 pci_disable_device(pdev);
808
809 return PCI_ERS_RESULT_NEED_RESET;
810}
811
812static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev)
813{
814 if (pci_enable_device(pdev)) {
815 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n");
816 return PCI_ERS_RESULT_DISCONNECT;
817 }
818
819 return PCI_ERS_RESULT_RECOVERED;
820}
821
822static void mhi_pci_io_resume(struct pci_dev *pdev)
823{
824 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
825
826 dev_err(&pdev->dev, "PCI slot reset done\n");
827
828 queue_work(system_long_wq, &mhi_pdev->recovery_work);
829}
830
Loic Poulain8ccc3272021-01-04 17:14:53 +0100831static const struct pci_error_handlers mhi_pci_err_handler = {
Loic Poulainb012ee62021-01-04 17:14:55 +0100832 .error_detected = mhi_pci_error_detected,
833 .slot_reset = mhi_pci_slot_reset,
834 .resume = mhi_pci_io_resume,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100835 .reset_prepare = mhi_pci_reset_prepare,
836 .reset_done = mhi_pci_reset_done,
837};
838
Loic Poulaind3800c12021-03-05 20:16:48 +0100839static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100840{
841 struct pci_dev *pdev = to_pci_dev(dev);
842 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
843 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulaind3800c12021-03-05 20:16:48 +0100844 int err;
845
846 if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
847 return 0;
Loic Poulain73893372021-01-04 17:14:54 +0100848
Loic Poulain8562d4f2021-01-04 17:14:56 +0100849 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100850 cancel_work_sync(&mhi_pdev->recovery_work);
851
Loic Poulaind3800c12021-03-05 20:16:48 +0100852 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
853 mhi_cntrl->ee != MHI_EE_AMSS)
854 goto pci_suspend; /* Nothing to do at MHI level */
Loic Poulain73893372021-01-04 17:14:54 +0100855
Loic Poulaind3800c12021-03-05 20:16:48 +0100856 /* Transition to M3 state */
857 err = mhi_pm_suspend(mhi_cntrl);
858 if (err) {
859 dev_err(&pdev->dev, "failed to suspend device: %d\n", err);
860 clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status);
861 return -EBUSY;
862 }
863
864pci_suspend:
Loic Poulain73893372021-01-04 17:14:54 +0100865 pci_disable_device(pdev);
866 pci_wake_from_d3(pdev, true);
Loic Poulain73893372021-01-04 17:14:54 +0100867
868 return 0;
869}
870
Loic Poulaind3800c12021-03-05 20:16:48 +0100871static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100872{
873 struct pci_dev *pdev = to_pci_dev(dev);
874 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
875 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
876 int err;
877
Loic Poulaind3800c12021-03-05 20:16:48 +0100878 if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
879 return 0;
880
Loic Poulain73893372021-01-04 17:14:54 +0100881 err = pci_enable_device(pdev);
882 if (err)
883 goto err_recovery;
884
Loic Poulaine89878a2021-03-05 20:16:47 +0100885 pci_set_master(pdev);
886 pci_wake_from_d3(pdev, false);
887
Loic Poulaind3800c12021-03-05 20:16:48 +0100888 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
889 mhi_cntrl->ee != MHI_EE_AMSS)
890 return 0; /* Nothing to do at MHI level */
891
Loic Poulain73893372021-01-04 17:14:54 +0100892 /* Exit M3, transition to M0 state */
893 err = mhi_pm_resume(mhi_cntrl);
894 if (err) {
895 dev_err(&pdev->dev, "failed to resume device: %d\n", err);
896 goto err_recovery;
897 }
898
Loic Poulain8562d4f2021-01-04 17:14:56 +0100899 /* Resume health check */
900 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
901
Loic Poulaind3800c12021-03-05 20:16:48 +0100902 /* It can be a remote wakeup (no mhi runtime_get), update access time */
903 pm_runtime_mark_last_busy(dev);
904
Loic Poulain73893372021-01-04 17:14:54 +0100905 return 0;
906
907err_recovery:
Loic Poulaind3800c12021-03-05 20:16:48 +0100908 /* Do not fail to not mess up our PCI device state, the device likely
909 * lost power (d3cold) and we simply need to reset it from the recovery
910 * procedure, trigger the recovery asynchronously to prevent system
911 * suspend exit delaying.
912 */
Loic Poulain73893372021-01-04 17:14:54 +0100913 queue_work(system_long_wq, &mhi_pdev->recovery_work);
Loic Poulaind3800c12021-03-05 20:16:48 +0100914 pm_runtime_mark_last_busy(dev);
Loic Poulain73893372021-01-04 17:14:54 +0100915
Loic Poulaind3800c12021-03-05 20:16:48 +0100916 return 0;
917}
918
919static int __maybe_unused mhi_pci_suspend(struct device *dev)
920{
921 pm_runtime_disable(dev);
922 return mhi_pci_runtime_suspend(dev);
923}
924
925static int __maybe_unused mhi_pci_resume(struct device *dev)
926{
927 int ret;
928
929 /* Depending the platform, device may have lost power (d3cold), we need
930 * to resume it now to check its state and recover when necessary.
931 */
932 ret = mhi_pci_runtime_resume(dev);
933 pm_runtime_enable(dev);
934
935 return ret;
Loic Poulain73893372021-01-04 17:14:54 +0100936}
937
938static const struct dev_pm_ops mhi_pci_pm_ops = {
Loic Poulaind3800c12021-03-05 20:16:48 +0100939 SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
Loic Poulain73893372021-01-04 17:14:54 +0100940 SET_SYSTEM_SLEEP_PM_OPS(mhi_pci_suspend, mhi_pci_resume)
941};
942
Loic Poulain855a70c2020-10-21 19:18:19 +0200943static struct pci_driver mhi_pci_driver = {
944 .name = "mhi-pci-generic",
945 .id_table = mhi_pci_id_table,
946 .probe = mhi_pci_probe,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100947 .remove = mhi_pci_remove,
Loic Poulain757072a2021-03-19 16:50:37 +0100948 .shutdown = mhi_pci_shutdown,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100949 .err_handler = &mhi_pci_err_handler,
Loic Poulain73893372021-01-04 17:14:54 +0100950 .driver.pm = &mhi_pci_pm_ops
Loic Poulain855a70c2020-10-21 19:18:19 +0200951};
952module_pci_driver(mhi_pci_driver);
953
954MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
955MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
956MODULE_LICENSE("GPL");