blob: aa8f8d5413712a88825bbfe5adc26e006f2e6107 [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
Loic Poulainb91c3b32021-01-05 17:44:36 +0100230static 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
290static struct mhi_controller_config modem_quectel_em1xx_config = {
291 .max_channels = 128,
292 .timeout_ms = 20000,
293 .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels),
294 .ch_cfg = mhi_quectel_em1xx_channels,
295 .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events),
296 .event_cfg = mhi_quectel_em1xx_events,
297};
298
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
Loic Poulain855a70c2020-10-21 19:18:19 +0200307static const struct pci_device_id mhi_pci_id_table[] = {
308 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
309 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
Loic Poulain59d05b72021-03-05 20:16:45 +0100310 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
311 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
Loic Poulainac4bf602021-03-05 20:16:44 +0100312 { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */
313 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
314 { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */
315 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
Bhaumik Bhatt49d38eb2021-04-02 14:33:19 -0700316 { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
317 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
Loic Poulain855a70c2020-10-21 19:18:19 +0200318 { }
319};
320MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
321
Loic Poulain8ccc3272021-01-04 17:14:53 +0100322enum mhi_pci_device_status {
323 MHI_PCI_DEV_STARTED,
Loic Poulaind3800c12021-03-05 20:16:48 +0100324 MHI_PCI_DEV_SUSPENDED,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100325};
326
327struct mhi_pci_device {
328 struct mhi_controller mhi_cntrl;
329 struct pci_saved_state *pci_state;
Loic Poulain73893372021-01-04 17:14:54 +0100330 struct work_struct recovery_work;
Loic Poulain8562d4f2021-01-04 17:14:56 +0100331 struct timer_list health_check_timer;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100332 unsigned long status;
333};
334
Loic Poulain855a70c2020-10-21 19:18:19 +0200335static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl,
336 void __iomem *addr, u32 *out)
337{
338 *out = readl(addr);
339 return 0;
340}
341
342static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
343 void __iomem *addr, u32 val)
344{
345 writel(val, addr);
346}
347
348static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
349 enum mhi_callback cb)
350{
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100351 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
352
Loic Poulain855a70c2020-10-21 19:18:19 +0200353 /* Nothing to do for now */
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100354 switch (cb) {
355 case MHI_CB_FATAL_ERROR:
356 case MHI_CB_SYS_ERROR:
357 dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb);
Loic Poulaind3800c12021-03-05 20:16:48 +0100358 pm_runtime_forbid(&pdev->dev);
359 break;
360 case MHI_CB_EE_MISSION_MODE:
361 pm_runtime_allow(&pdev->dev);
Loic Poulain1e2f29b2021-02-03 17:39:42 +0100362 break;
363 default:
364 break;
365 }
Loic Poulain855a70c2020-10-21 19:18:19 +0200366}
367
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100368static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
369{
370 /* no-op */
371}
372
373static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
374{
375 /* no-op */
376}
377
378static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
379{
380 /* no-op */
381}
382
Loic Poulain8ccc3272021-01-04 17:14:53 +0100383static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl)
384{
385 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
386 u16 vendor = 0;
387
388 if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor))
389 return false;
390
391 if (vendor == (u16) ~0 || vendor == 0)
392 return false;
393
394 return true;
395}
396
Loic Poulain855a70c2020-10-21 19:18:19 +0200397static int mhi_pci_claim(struct mhi_controller *mhi_cntrl,
398 unsigned int bar_num, u64 dma_mask)
399{
400 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
401 int err;
402
403 err = pci_assign_resource(pdev, bar_num);
404 if (err)
405 return err;
406
407 err = pcim_enable_device(pdev);
408 if (err) {
409 dev_err(&pdev->dev, "failed to enable pci device: %d\n", err);
410 return err;
411 }
412
413 err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev));
414 if (err) {
415 dev_err(&pdev->dev, "failed to map pci region: %d\n", err);
416 return err;
417 }
418 mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num];
419
420 err = pci_set_dma_mask(pdev, dma_mask);
421 if (err) {
422 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
423 return err;
424 }
425
426 err = pci_set_consistent_dma_mask(pdev, dma_mask);
427 if (err) {
428 dev_err(&pdev->dev, "set consistent dma mask failed\n");
429 return err;
430 }
431
432 pci_set_master(pdev);
433
434 return 0;
435}
436
437static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl,
438 const struct mhi_controller_config *mhi_cntrl_config)
439{
440 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
441 int nr_vectors, i;
442 int *irq;
443
444 /*
445 * Alloc one MSI vector for BHI + one vector per event ring, ideally...
446 * No explicit pci_free_irq_vectors required, done by pcim_release.
447 */
448 mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events;
449
450 nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI);
451 if (nr_vectors < 0) {
452 dev_err(&pdev->dev, "Error allocating MSI vectors %d\n",
453 nr_vectors);
454 return nr_vectors;
455 }
456
457 if (nr_vectors < mhi_cntrl->nr_irqs) {
Loic Poulainb91c3b32021-01-05 17:44:36 +0100458 dev_warn(&pdev->dev, "using shared MSI\n");
459
460 /* Patch msi vectors, use only one (shared) */
461 for (i = 0; i < mhi_cntrl_config->num_events; i++)
462 mhi_cntrl_config->event_cfg[i].irq = 0;
463 mhi_cntrl->nr_irqs = 1;
Loic Poulain855a70c2020-10-21 19:18:19 +0200464 }
465
466 irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL);
467 if (!irq)
468 return -ENOMEM;
469
470 for (i = 0; i < mhi_cntrl->nr_irqs; i++) {
471 int vector = i >= nr_vectors ? (nr_vectors - 1) : i;
472
473 irq[i] = pci_irq_vector(pdev, vector);
474 }
475
476 mhi_cntrl->irq = irq;
477
478 return 0;
479}
480
481static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
482{
Loic Poulaind3800c12021-03-05 20:16:48 +0100483 /* The runtime_get() MHI callback means:
484 * Do whatever is requested to leave M3.
485 */
486 return pm_runtime_get(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200487}
488
489static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
490{
Loic Poulaind3800c12021-03-05 20:16:48 +0100491 /* The runtime_put() MHI callback means:
492 * Device can be moved in M3 state.
493 */
494 pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev);
495 pm_runtime_put(mhi_cntrl->cntrl_dev);
Loic Poulain855a70c2020-10-21 19:18:19 +0200496}
497
Loic Poulain73893372021-01-04 17:14:54 +0100498static void mhi_pci_recovery_work(struct work_struct *work)
499{
500 struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device,
501 recovery_work);
502 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
503 struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
504 int err;
505
506 dev_warn(&pdev->dev, "device recovery started\n");
507
Loic Poulain8562d4f2021-01-04 17:14:56 +0100508 del_timer(&mhi_pdev->health_check_timer);
Loic Poulaind3800c12021-03-05 20:16:48 +0100509 pm_runtime_forbid(&pdev->dev);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100510
Loic Poulain73893372021-01-04 17:14:54 +0100511 /* Clean up MHI state */
512 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
513 mhi_power_down(mhi_cntrl, false);
514 mhi_unprepare_after_power_down(mhi_cntrl);
515 }
516
Loic Poulain73893372021-01-04 17:14:54 +0100517 pci_set_power_state(pdev, PCI_D0);
518 pci_load_saved_state(pdev, mhi_pdev->pci_state);
519 pci_restore_state(pdev);
520
521 if (!mhi_pci_is_alive(mhi_cntrl))
522 goto err_try_reset;
523
524 err = mhi_prepare_for_power_up(mhi_cntrl);
525 if (err)
526 goto err_try_reset;
527
528 err = mhi_sync_power_up(mhi_cntrl);
529 if (err)
530 goto err_unprepare;
531
532 dev_dbg(&pdev->dev, "Recovery completed\n");
533
534 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100535 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain73893372021-01-04 17:14:54 +0100536 return;
537
538err_unprepare:
539 mhi_unprepare_after_power_down(mhi_cntrl);
540err_try_reset:
541 if (pci_reset_function(pdev))
542 dev_err(&pdev->dev, "Recovery failed\n");
543}
544
Loic Poulain8562d4f2021-01-04 17:14:56 +0100545static void health_check(struct timer_list *t)
546{
547 struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer);
548 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
549
Loic Poulaind3800c12021-03-05 20:16:48 +0100550 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
551 test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
552 return;
553
Loic Poulain8562d4f2021-01-04 17:14:56 +0100554 if (!mhi_pci_is_alive(mhi_cntrl)) {
555 dev_err(mhi_cntrl->cntrl_dev, "Device died\n");
556 queue_work(system_long_wq, &mhi_pdev->recovery_work);
557 return;
558 }
559
560 /* reschedule in two seconds */
561 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
562}
563
Loic Poulain855a70c2020-10-21 19:18:19 +0200564static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
565{
566 const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data;
567 const struct mhi_controller_config *mhi_cntrl_config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100568 struct mhi_pci_device *mhi_pdev;
Loic Poulain855a70c2020-10-21 19:18:19 +0200569 struct mhi_controller *mhi_cntrl;
570 int err;
571
572 dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name);
573
Loic Poulain8ccc3272021-01-04 17:14:53 +0100574 /* mhi_pdev.mhi_cntrl must be zero-initialized */
575 mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL);
576 if (!mhi_pdev)
Loic Poulain855a70c2020-10-21 19:18:19 +0200577 return -ENOMEM;
578
Loic Poulain73893372021-01-04 17:14:54 +0100579 INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100580 timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
Loic Poulain73893372021-01-04 17:14:54 +0100581
Loic Poulain855a70c2020-10-21 19:18:19 +0200582 mhi_cntrl_config = info->config;
Loic Poulain8ccc3272021-01-04 17:14:53 +0100583 mhi_cntrl = &mhi_pdev->mhi_cntrl;
584
Loic Poulain855a70c2020-10-21 19:18:19 +0200585 mhi_cntrl->cntrl_dev = &pdev->dev;
586 mhi_cntrl->iova_start = 0;
Loic Poulain4ea6fa22020-12-02 09:12:30 +0100587 mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
Loic Poulain855a70c2020-10-21 19:18:19 +0200588 mhi_cntrl->fw_image = info->fw;
589 mhi_cntrl->edl_image = info->edl;
590
591 mhi_cntrl->read_reg = mhi_pci_read_reg;
592 mhi_cntrl->write_reg = mhi_pci_write_reg;
593 mhi_cntrl->status_cb = mhi_pci_status_cb;
594 mhi_cntrl->runtime_get = mhi_pci_runtime_get;
595 mhi_cntrl->runtime_put = mhi_pci_runtime_put;
Loic Poulaine3e5e6502021-03-05 20:16:46 +0100596 mhi_cntrl->wake_get = mhi_pci_wake_get_nop;
597 mhi_cntrl->wake_put = mhi_pci_wake_put_nop;
598 mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop;
Loic Poulain855a70c2020-10-21 19:18:19 +0200599
600 err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
601 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100602 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200603
604 err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
605 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100606 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200607
Loic Poulain8ccc3272021-01-04 17:14:53 +0100608 pci_set_drvdata(pdev, mhi_pdev);
609
Loic Poulaine89878a2021-03-05 20:16:47 +0100610 /* Have stored pci confspace at hand for restore in sudden PCI error.
611 * cache the state locally and discard the PCI core one.
612 */
Loic Poulain8ccc3272021-01-04 17:14:53 +0100613 pci_save_state(pdev);
614 mhi_pdev->pci_state = pci_store_saved_state(pdev);
Loic Poulaine89878a2021-03-05 20:16:47 +0100615 pci_load_saved_state(pdev, NULL);
Loic Poulain855a70c2020-10-21 19:18:19 +0200616
Loic Poulainb012ee62021-01-04 17:14:55 +0100617 pci_enable_pcie_error_reporting(pdev);
618
Loic Poulain855a70c2020-10-21 19:18:19 +0200619 err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
620 if (err)
Loic Poulain8ccc3272021-01-04 17:14:53 +0100621 return err;
Loic Poulain855a70c2020-10-21 19:18:19 +0200622
623 /* MHI bus does not power up the controller by default */
624 err = mhi_prepare_for_power_up(mhi_cntrl);
625 if (err) {
626 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
627 goto err_unregister;
628 }
629
630 err = mhi_sync_power_up(mhi_cntrl);
631 if (err) {
632 dev_err(&pdev->dev, "failed to power up MHI controller\n");
633 goto err_unprepare;
634 }
635
Loic Poulain8ccc3272021-01-04 17:14:53 +0100636 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
637
Loic Poulain8562d4f2021-01-04 17:14:56 +0100638 /* start health check */
639 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
640
Loic Poulaind3800c12021-03-05 20:16:48 +0100641 /* Only allow runtime-suspend if PME capable (for wakeup) */
642 if (pci_pme_capable(pdev, PCI_D3hot)) {
643 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
644 pm_runtime_use_autosuspend(&pdev->dev);
645 pm_runtime_mark_last_busy(&pdev->dev);
646 pm_runtime_put_noidle(&pdev->dev);
647 }
648
Loic Poulain855a70c2020-10-21 19:18:19 +0200649 return 0;
650
651err_unprepare:
652 mhi_unprepare_after_power_down(mhi_cntrl);
653err_unregister:
654 mhi_unregister_controller(mhi_cntrl);
Loic Poulain855a70c2020-10-21 19:18:19 +0200655
656 return err;
657}
658
659static void mhi_pci_remove(struct pci_dev *pdev)
660{
Loic Poulain8ccc3272021-01-04 17:14:53 +0100661 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
662 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulain855a70c2020-10-21 19:18:19 +0200663
Loic Poulain8562d4f2021-01-04 17:14:56 +0100664 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100665 cancel_work_sync(&mhi_pdev->recovery_work);
666
Loic Poulain8ccc3272021-01-04 17:14:53 +0100667 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
668 mhi_power_down(mhi_cntrl, true);
669 mhi_unprepare_after_power_down(mhi_cntrl);
670 }
671
Loic Poulaind3800c12021-03-05 20:16:48 +0100672 /* balancing probe put_noidle */
673 if (pci_pme_capable(pdev, PCI_D3hot))
674 pm_runtime_get_noresume(&pdev->dev);
675
Loic Poulain855a70c2020-10-21 19:18:19 +0200676 mhi_unregister_controller(mhi_cntrl);
Loic Poulain855a70c2020-10-21 19:18:19 +0200677}
678
Loic Poulain757072a2021-03-19 16:50:37 +0100679static void mhi_pci_shutdown(struct pci_dev *pdev)
680{
681 mhi_pci_remove(pdev);
682 pci_set_power_state(pdev, PCI_D3hot);
683}
684
Loic Poulain8ccc3272021-01-04 17:14:53 +0100685static void mhi_pci_reset_prepare(struct pci_dev *pdev)
686{
687 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
688 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
689
690 dev_info(&pdev->dev, "reset\n");
691
Loic Poulain8562d4f2021-01-04 17:14:56 +0100692 del_timer(&mhi_pdev->health_check_timer);
693
Loic Poulain8ccc3272021-01-04 17:14:53 +0100694 /* Clean up MHI state */
695 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
696 mhi_power_down(mhi_cntrl, false);
697 mhi_unprepare_after_power_down(mhi_cntrl);
698 }
699
700 /* cause internal device reset */
701 mhi_soc_reset(mhi_cntrl);
702
703 /* Be sure device reset has been executed */
704 msleep(MHI_POST_RESET_DELAY_MS);
705}
706
707static void mhi_pci_reset_done(struct pci_dev *pdev)
708{
709 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
710 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
711 int err;
712
713 /* Restore initial known working PCI state */
714 pci_load_saved_state(pdev, mhi_pdev->pci_state);
715 pci_restore_state(pdev);
716
717 /* Is device status available ? */
718 if (!mhi_pci_is_alive(mhi_cntrl)) {
719 dev_err(&pdev->dev, "reset failed\n");
720 return;
721 }
722
723 err = mhi_prepare_for_power_up(mhi_cntrl);
724 if (err) {
725 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
726 return;
727 }
728
729 err = mhi_sync_power_up(mhi_cntrl);
730 if (err) {
731 dev_err(&pdev->dev, "failed to power up MHI controller\n");
732 mhi_unprepare_after_power_down(mhi_cntrl);
733 return;
734 }
735
736 set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
Loic Poulain8562d4f2021-01-04 17:14:56 +0100737 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
Loic Poulain8ccc3272021-01-04 17:14:53 +0100738}
739
Loic Poulainb012ee62021-01-04 17:14:55 +0100740static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev,
741 pci_channel_state_t state)
742{
743 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
744 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
745
746 dev_err(&pdev->dev, "PCI error detected, state = %u\n", state);
747
748 if (state == pci_channel_io_perm_failure)
749 return PCI_ERS_RESULT_DISCONNECT;
750
751 /* Clean up MHI state */
752 if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
753 mhi_power_down(mhi_cntrl, false);
754 mhi_unprepare_after_power_down(mhi_cntrl);
755 } else {
756 /* Nothing to do */
757 return PCI_ERS_RESULT_RECOVERED;
758 }
759
760 pci_disable_device(pdev);
761
762 return PCI_ERS_RESULT_NEED_RESET;
763}
764
765static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev)
766{
767 if (pci_enable_device(pdev)) {
768 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n");
769 return PCI_ERS_RESULT_DISCONNECT;
770 }
771
772 return PCI_ERS_RESULT_RECOVERED;
773}
774
775static void mhi_pci_io_resume(struct pci_dev *pdev)
776{
777 struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
778
779 dev_err(&pdev->dev, "PCI slot reset done\n");
780
781 queue_work(system_long_wq, &mhi_pdev->recovery_work);
782}
783
Loic Poulain8ccc3272021-01-04 17:14:53 +0100784static const struct pci_error_handlers mhi_pci_err_handler = {
Loic Poulainb012ee62021-01-04 17:14:55 +0100785 .error_detected = mhi_pci_error_detected,
786 .slot_reset = mhi_pci_slot_reset,
787 .resume = mhi_pci_io_resume,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100788 .reset_prepare = mhi_pci_reset_prepare,
789 .reset_done = mhi_pci_reset_done,
790};
791
Loic Poulaind3800c12021-03-05 20:16:48 +0100792static int __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100793{
794 struct pci_dev *pdev = to_pci_dev(dev);
795 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
796 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
Loic Poulaind3800c12021-03-05 20:16:48 +0100797 int err;
798
799 if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
800 return 0;
Loic Poulain73893372021-01-04 17:14:54 +0100801
Loic Poulain8562d4f2021-01-04 17:14:56 +0100802 del_timer(&mhi_pdev->health_check_timer);
Loic Poulain73893372021-01-04 17:14:54 +0100803 cancel_work_sync(&mhi_pdev->recovery_work);
804
Loic Poulaind3800c12021-03-05 20:16:48 +0100805 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
806 mhi_cntrl->ee != MHI_EE_AMSS)
807 goto pci_suspend; /* Nothing to do at MHI level */
Loic Poulain73893372021-01-04 17:14:54 +0100808
Loic Poulaind3800c12021-03-05 20:16:48 +0100809 /* Transition to M3 state */
810 err = mhi_pm_suspend(mhi_cntrl);
811 if (err) {
812 dev_err(&pdev->dev, "failed to suspend device: %d\n", err);
813 clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status);
814 return -EBUSY;
815 }
816
817pci_suspend:
Loic Poulain73893372021-01-04 17:14:54 +0100818 pci_disable_device(pdev);
819 pci_wake_from_d3(pdev, true);
Loic Poulain73893372021-01-04 17:14:54 +0100820
821 return 0;
822}
823
Loic Poulaind3800c12021-03-05 20:16:48 +0100824static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
Loic Poulain73893372021-01-04 17:14:54 +0100825{
826 struct pci_dev *pdev = to_pci_dev(dev);
827 struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
828 struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
829 int err;
830
Loic Poulaind3800c12021-03-05 20:16:48 +0100831 if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
832 return 0;
833
Loic Poulain73893372021-01-04 17:14:54 +0100834 err = pci_enable_device(pdev);
835 if (err)
836 goto err_recovery;
837
Loic Poulaine89878a2021-03-05 20:16:47 +0100838 pci_set_master(pdev);
839 pci_wake_from_d3(pdev, false);
840
Loic Poulaind3800c12021-03-05 20:16:48 +0100841 if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
842 mhi_cntrl->ee != MHI_EE_AMSS)
843 return 0; /* Nothing to do at MHI level */
844
Loic Poulain73893372021-01-04 17:14:54 +0100845 /* Exit M3, transition to M0 state */
846 err = mhi_pm_resume(mhi_cntrl);
847 if (err) {
848 dev_err(&pdev->dev, "failed to resume device: %d\n", err);
849 goto err_recovery;
850 }
851
Loic Poulain8562d4f2021-01-04 17:14:56 +0100852 /* Resume health check */
853 mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
854
Loic Poulaind3800c12021-03-05 20:16:48 +0100855 /* It can be a remote wakeup (no mhi runtime_get), update access time */
856 pm_runtime_mark_last_busy(dev);
857
Loic Poulain73893372021-01-04 17:14:54 +0100858 return 0;
859
860err_recovery:
Loic Poulaind3800c12021-03-05 20:16:48 +0100861 /* Do not fail to not mess up our PCI device state, the device likely
862 * lost power (d3cold) and we simply need to reset it from the recovery
863 * procedure, trigger the recovery asynchronously to prevent system
864 * suspend exit delaying.
865 */
Loic Poulain73893372021-01-04 17:14:54 +0100866 queue_work(system_long_wq, &mhi_pdev->recovery_work);
Loic Poulaind3800c12021-03-05 20:16:48 +0100867 pm_runtime_mark_last_busy(dev);
Loic Poulain73893372021-01-04 17:14:54 +0100868
Loic Poulaind3800c12021-03-05 20:16:48 +0100869 return 0;
870}
871
872static int __maybe_unused mhi_pci_suspend(struct device *dev)
873{
874 pm_runtime_disable(dev);
875 return mhi_pci_runtime_suspend(dev);
876}
877
878static int __maybe_unused mhi_pci_resume(struct device *dev)
879{
880 int ret;
881
882 /* Depending the platform, device may have lost power (d3cold), we need
883 * to resume it now to check its state and recover when necessary.
884 */
885 ret = mhi_pci_runtime_resume(dev);
886 pm_runtime_enable(dev);
887
888 return ret;
Loic Poulain73893372021-01-04 17:14:54 +0100889}
890
891static const struct dev_pm_ops mhi_pci_pm_ops = {
Loic Poulaind3800c12021-03-05 20:16:48 +0100892 SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
Loic Poulain73893372021-01-04 17:14:54 +0100893 SET_SYSTEM_SLEEP_PM_OPS(mhi_pci_suspend, mhi_pci_resume)
894};
895
Loic Poulain855a70c2020-10-21 19:18:19 +0200896static struct pci_driver mhi_pci_driver = {
897 .name = "mhi-pci-generic",
898 .id_table = mhi_pci_id_table,
899 .probe = mhi_pci_probe,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100900 .remove = mhi_pci_remove,
Loic Poulain757072a2021-03-19 16:50:37 +0100901 .shutdown = mhi_pci_shutdown,
Loic Poulain8ccc3272021-01-04 17:14:53 +0100902 .err_handler = &mhi_pci_err_handler,
Loic Poulain73893372021-01-04 17:14:54 +0100903 .driver.pm = &mhi_pci_pm_ops
Loic Poulain855a70c2020-10-21 19:18:19 +0200904};
905module_pci_driver(mhi_pci_driver);
906
907MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
908MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
909MODULE_LICENSE("GPL");