1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/pci.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/workqueue.h>
17 #include <linux/pm_domain.h>
18 #include <linux/pm_runtime.h>
20 #include <linux/mei.h>
26 static const struct pci_device_id mei_txe_pci_tbl
[] = {
27 {PCI_VDEVICE(INTEL
, 0x0F18)}, /* Baytrail */
28 {PCI_VDEVICE(INTEL
, 0x2298)}, /* Cherrytrail */
32 MODULE_DEVICE_TABLE(pci
, mei_txe_pci_tbl
);
35 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
);
36 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
);
38 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
) {}
39 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
) {}
40 #endif /* CONFIG_PM */
43 * mei_txe_probe - Device Initialization Routine
45 * @pdev: PCI device structure
46 * @ent: entry in mei_txe_pci_tbl
48 * Return: 0 on success, <0 on failure.
50 static int mei_txe_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
52 struct mei_device
*dev
;
53 struct mei_txe_hw
*hw
;
54 const int mask
= BIT(SEC_BAR
) | BIT(BRIDGE_BAR
);
58 err
= pcim_enable_device(pdev
);
60 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
63 /* set PCI host mastering */
65 /* pci request regions and mapping IO device memory for mei driver */
66 err
= pcim_iomap_regions(pdev
, mask
, KBUILD_MODNAME
);
68 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
72 err
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(36));
74 err
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32));
76 dev_err(&pdev
->dev
, "No suitable DMA available.\n");
81 /* allocates and initializes the mei dev structure */
82 dev
= mei_txe_dev_init(pdev
);
88 hw
->mem_addr
= pcim_iomap_table(pdev
);
92 /* clear spurious interrupts */
93 mei_clear_interrupts(dev
);
95 /* request and enable interrupt */
96 if (pci_dev_msi_enabled(pdev
))
97 err
= request_threaded_irq(pdev
->irq
,
99 mei_txe_irq_thread_handler
,
100 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
102 err
= request_threaded_irq(pdev
->irq
,
103 mei_txe_irq_quick_handler
,
104 mei_txe_irq_thread_handler
,
105 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
107 dev_err(&pdev
->dev
, "mei: request_threaded_irq failure. irq = %d\n",
112 if (mei_start(dev
)) {
113 dev_err(&pdev
->dev
, "init hw failure.\n");
118 pm_runtime_set_autosuspend_delay(&pdev
->dev
, MEI_TXI_RPM_TIMEOUT
);
119 pm_runtime_use_autosuspend(&pdev
->dev
);
121 err
= mei_register(dev
, &pdev
->dev
);
125 pci_set_drvdata(pdev
, dev
);
128 * MEI requires to resume from runtime suspend mode
129 * in order to perform link reset flow upon system suspend.
131 dev_pm_set_driver_flags(&pdev
->dev
, DPM_FLAG_NO_DIRECT_COMPLETE
);
134 * TXE maps runtime suspend/resume to own power gating states,
135 * hence we need to go around native PCI runtime service which
136 * eventually brings the device into D3cold/hot state.
137 * But the TXE device cannot wake up from D3 unlike from own
138 * power gating. To get around PCI device native runtime pm,
139 * TXE uses runtime pm domain handlers which take precedence.
141 mei_txe_set_pm_domain(dev
);
143 pm_runtime_put_noidle(&pdev
->dev
);
150 mei_cancel_work(dev
);
151 mei_disable_interrupts(dev
);
152 free_irq(pdev
->irq
, dev
);
154 dev_err(&pdev
->dev
, "initialization failed.\n");
159 * mei_txe_shutdown- Device Shutdown Routine
161 * @pdev: PCI device structure
163 * mei_txe_shutdown is called from the reboot notifier
164 * it's a simplified version of remove so we go down
167 static void mei_txe_shutdown(struct pci_dev
*pdev
)
169 struct mei_device
*dev
= pci_get_drvdata(pdev
);
171 dev_dbg(&pdev
->dev
, "shutdown\n");
174 mei_txe_unset_pm_domain(dev
);
176 mei_disable_interrupts(dev
);
177 free_irq(pdev
->irq
, dev
);
181 * mei_txe_remove - Device Removal Routine
183 * @pdev: PCI device structure
185 * mei_remove is called by the PCI subsystem to alert the driver
186 * that it should release a PCI device.
188 static void mei_txe_remove(struct pci_dev
*pdev
)
190 struct mei_device
*dev
= pci_get_drvdata(pdev
);
192 pm_runtime_get_noresume(&pdev
->dev
);
196 mei_txe_unset_pm_domain(dev
);
198 mei_disable_interrupts(dev
);
199 free_irq(pdev
->irq
, dev
);
205 #ifdef CONFIG_PM_SLEEP
206 static int mei_txe_pci_suspend(struct device
*device
)
208 struct pci_dev
*pdev
= to_pci_dev(device
);
209 struct mei_device
*dev
= pci_get_drvdata(pdev
);
211 dev_dbg(&pdev
->dev
, "suspend\n");
215 mei_disable_interrupts(dev
);
217 free_irq(pdev
->irq
, dev
);
218 pci_disable_msi(pdev
);
223 static int mei_txe_pci_resume(struct device
*device
)
225 struct pci_dev
*pdev
= to_pci_dev(device
);
226 struct mei_device
*dev
= pci_get_drvdata(pdev
);
229 pci_enable_msi(pdev
);
231 mei_clear_interrupts(dev
);
233 /* request and enable interrupt */
234 if (pci_dev_msi_enabled(pdev
))
235 err
= request_threaded_irq(pdev
->irq
,
237 mei_txe_irq_thread_handler
,
238 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
240 err
= request_threaded_irq(pdev
->irq
,
241 mei_txe_irq_quick_handler
,
242 mei_txe_irq_thread_handler
,
243 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
245 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
250 err
= mei_restart(dev
);
254 #endif /* CONFIG_PM_SLEEP */
257 static int mei_txe_pm_runtime_idle(struct device
*device
)
259 struct mei_device
*dev
= dev_get_drvdata(device
);
261 dev_dbg(device
, "rpm: txe: runtime_idle\n");
263 if (mei_write_is_idle(dev
))
264 pm_runtime_autosuspend(device
);
268 static int mei_txe_pm_runtime_suspend(struct device
*device
)
270 struct mei_device
*dev
= dev_get_drvdata(device
);
273 dev_dbg(device
, "rpm: txe: runtime suspend\n");
275 mutex_lock(&dev
->device_lock
);
277 if (mei_write_is_idle(dev
))
278 ret
= mei_txe_aliveness_set_sync(dev
, 0);
282 /* keep irq on we are staying in D0 */
284 dev_dbg(device
, "rpm: txe: runtime suspend ret=%d\n", ret
);
286 mutex_unlock(&dev
->device_lock
);
288 if (ret
&& ret
!= -EAGAIN
)
289 schedule_work(&dev
->reset_work
);
294 static int mei_txe_pm_runtime_resume(struct device
*device
)
296 struct mei_device
*dev
= dev_get_drvdata(device
);
299 dev_dbg(device
, "rpm: txe: runtime resume\n");
301 mutex_lock(&dev
->device_lock
);
303 mei_enable_interrupts(dev
);
305 ret
= mei_txe_aliveness_set_sync(dev
, 1);
307 mutex_unlock(&dev
->device_lock
);
309 dev_dbg(device
, "rpm: txe: runtime resume ret = %d\n", ret
);
312 schedule_work(&dev
->reset_work
);
318 * mei_txe_set_pm_domain - fill and set pm domain structure for device
322 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
)
324 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
326 if (pdev
->dev
.bus
&& pdev
->dev
.bus
->pm
) {
327 dev
->pg_domain
.ops
= *pdev
->dev
.bus
->pm
;
329 dev
->pg_domain
.ops
.runtime_suspend
= mei_txe_pm_runtime_suspend
;
330 dev
->pg_domain
.ops
.runtime_resume
= mei_txe_pm_runtime_resume
;
331 dev
->pg_domain
.ops
.runtime_idle
= mei_txe_pm_runtime_idle
;
333 dev_pm_domain_set(&pdev
->dev
, &dev
->pg_domain
);
338 * mei_txe_unset_pm_domain - clean pm domain structure for device
342 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
)
344 /* stop using pm callbacks if any */
345 dev_pm_domain_set(dev
->dev
, NULL
);
348 static const struct dev_pm_ops mei_txe_pm_ops
= {
349 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend
,
352 mei_txe_pm_runtime_suspend
,
353 mei_txe_pm_runtime_resume
,
354 mei_txe_pm_runtime_idle
)
357 #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
359 #define MEI_TXE_PM_OPS NULL
360 #endif /* CONFIG_PM */
363 * PCI driver structure
365 static struct pci_driver mei_txe_driver
= {
366 .name
= KBUILD_MODNAME
,
367 .id_table
= mei_txe_pci_tbl
,
368 .probe
= mei_txe_probe
,
369 .remove
= mei_txe_remove
,
370 .shutdown
= mei_txe_shutdown
,
371 .driver
.pm
= MEI_TXE_PM_OPS
,
374 module_pci_driver(mei_txe_driver
);
376 MODULE_AUTHOR("Intel Corporation");
377 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
378 MODULE_LICENSE("GPL v2");