3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2013-2014, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/uuid.h>
27 #include <linux/jiffies.h>
28 #include <linux/interrupt.h>
29 #include <linux/workqueue.h>
30 #include <linux/pm_domain.h>
31 #include <linux/pm_runtime.h>
33 #include <linux/mei.h>
39 static const struct pci_device_id mei_txe_pci_tbl[] = {
40 {PCI_VDEVICE(INTEL, 0x0F18)}, /* Baytrail */
41 {PCI_VDEVICE(INTEL, 0x2298)}, /* Cherrytrail */
45 MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
48 static inline void mei_txe_set_pm_domain(struct mei_device *dev);
49 static inline void mei_txe_unset_pm_domain(struct mei_device *dev);
51 static inline void mei_txe_set_pm_domain(struct mei_device *dev) {}
52 static inline void mei_txe_unset_pm_domain(struct mei_device *dev) {}
53 #endif /* CONFIG_PM */
56 * mei_txe_probe - Device Initialization Routine
58 * @pdev: PCI device structure
59 * @ent: entry in mei_txe_pci_tbl
61 * Return: 0 on success, <0 on failure.
63 static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
65 struct mei_device *dev;
66 struct mei_txe_hw *hw;
67 const int mask = BIT(SEC_BAR) | BIT(BRIDGE_BAR);
71 err = pcim_enable_device(pdev);
73 dev_err(&pdev->dev, "failed to enable pci device.\n");
76 /* set PCI host mastering */
78 /* pci request regions and mapping IO device memory for mei driver */
79 err = pcim_iomap_regions(pdev, mask, KBUILD_MODNAME);
81 dev_err(&pdev->dev, "failed to get pci regions.\n");
85 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
87 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
89 dev_err(&pdev->dev, "No suitable DMA available.\n");
94 /* allocates and initializes the mei dev structure */
95 dev = mei_txe_dev_init(pdev);
101 hw->mem_addr = pcim_iomap_table(pdev);
103 pci_enable_msi(pdev);
105 /* clear spurious interrupts */
106 mei_clear_interrupts(dev);
108 /* request and enable interrupt */
109 if (pci_dev_msi_enabled(pdev))
110 err = request_threaded_irq(pdev->irq,
112 mei_txe_irq_thread_handler,
113 IRQF_ONESHOT, KBUILD_MODNAME, dev);
115 err = request_threaded_irq(pdev->irq,
116 mei_txe_irq_quick_handler,
117 mei_txe_irq_thread_handler,
118 IRQF_SHARED, KBUILD_MODNAME, dev);
120 dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
125 if (mei_start(dev)) {
126 dev_err(&pdev->dev, "init hw failure.\n");
131 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
132 pm_runtime_use_autosuspend(&pdev->dev);
134 err = mei_register(dev, &pdev->dev);
138 pci_set_drvdata(pdev, dev);
141 * For not wake-able HW runtime pm framework
142 * can't be used on pci device level.
143 * Use domain runtime pm callbacks instead.
145 if (!pci_dev_run_wake(pdev))
146 mei_txe_set_pm_domain(dev);
148 pm_runtime_put_noidle(&pdev->dev);
155 mei_cancel_work(dev);
156 mei_disable_interrupts(dev);
157 free_irq(pdev->irq, dev);
159 dev_err(&pdev->dev, "initialization failed.\n");
164 * mei_txe_remove - Device Shutdown Routine
166 * @pdev: PCI device structure
168 * mei_txe_shutdown is called from the reboot notifier
169 * it's a simplified version of remove so we go down
172 static void mei_txe_shutdown(struct pci_dev *pdev)
174 struct mei_device *dev;
176 dev = pci_get_drvdata(pdev);
180 dev_dbg(&pdev->dev, "shutdown\n");
183 if (!pci_dev_run_wake(pdev))
184 mei_txe_unset_pm_domain(dev);
186 mei_disable_interrupts(dev);
187 free_irq(pdev->irq, dev);
191 * mei_txe_remove - Device Removal Routine
193 * @pdev: PCI device structure
195 * mei_remove is called by the PCI subsystem to alert the driver
196 * that it should release a PCI device.
198 static void mei_txe_remove(struct pci_dev *pdev)
200 struct mei_device *dev;
202 dev = pci_get_drvdata(pdev);
204 dev_err(&pdev->dev, "mei: dev == NULL\n");
208 pm_runtime_get_noresume(&pdev->dev);
212 if (!pci_dev_run_wake(pdev))
213 mei_txe_unset_pm_domain(dev);
215 mei_disable_interrupts(dev);
216 free_irq(pdev->irq, dev);
222 #ifdef CONFIG_PM_SLEEP
223 static int mei_txe_pci_suspend(struct device *device)
225 struct pci_dev *pdev = to_pci_dev(device);
226 struct mei_device *dev = pci_get_drvdata(pdev);
231 dev_dbg(&pdev->dev, "suspend\n");
235 mei_disable_interrupts(dev);
237 free_irq(pdev->irq, dev);
238 pci_disable_msi(pdev);
243 static int mei_txe_pci_resume(struct device *device)
245 struct pci_dev *pdev = to_pci_dev(device);
246 struct mei_device *dev;
249 dev = pci_get_drvdata(pdev);
253 pci_enable_msi(pdev);
255 mei_clear_interrupts(dev);
257 /* request and enable interrupt */
258 if (pci_dev_msi_enabled(pdev))
259 err = request_threaded_irq(pdev->irq,
261 mei_txe_irq_thread_handler,
262 IRQF_ONESHOT, KBUILD_MODNAME, dev);
264 err = request_threaded_irq(pdev->irq,
265 mei_txe_irq_quick_handler,
266 mei_txe_irq_thread_handler,
267 IRQF_SHARED, KBUILD_MODNAME, dev);
269 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
274 err = mei_restart(dev);
278 #endif /* CONFIG_PM_SLEEP */
281 static int mei_txe_pm_runtime_idle(struct device *device)
283 struct pci_dev *pdev = to_pci_dev(device);
284 struct mei_device *dev;
286 dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
288 dev = pci_get_drvdata(pdev);
291 if (mei_write_is_idle(dev))
292 pm_runtime_autosuspend(device);
296 static int mei_txe_pm_runtime_suspend(struct device *device)
298 struct pci_dev *pdev = to_pci_dev(device);
299 struct mei_device *dev;
302 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
304 dev = pci_get_drvdata(pdev);
308 mutex_lock(&dev->device_lock);
310 if (mei_write_is_idle(dev))
311 ret = mei_txe_aliveness_set_sync(dev, 0);
316 * If everything is okay we're about to enter PCI low
317 * power state (D3) therefor we need to disable the
318 * interrupts towards host.
319 * However if device is not wakeable we do not enter
320 * D-low state and we need to keep the interrupt kicking
322 if (!ret && pci_dev_run_wake(pdev))
323 mei_disable_interrupts(dev);
325 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
327 mutex_unlock(&dev->device_lock);
329 if (ret && ret != -EAGAIN)
330 schedule_work(&dev->reset_work);
335 static int mei_txe_pm_runtime_resume(struct device *device)
337 struct pci_dev *pdev = to_pci_dev(device);
338 struct mei_device *dev;
341 dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
343 dev = pci_get_drvdata(pdev);
347 mutex_lock(&dev->device_lock);
349 mei_enable_interrupts(dev);
351 ret = mei_txe_aliveness_set_sync(dev, 1);
353 mutex_unlock(&dev->device_lock);
355 dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
358 schedule_work(&dev->reset_work);
364 * mei_txe_set_pm_domain - fill and set pm domain structure for device
368 static inline void mei_txe_set_pm_domain(struct mei_device *dev)
370 struct pci_dev *pdev = to_pci_dev(dev->dev);
372 if (pdev->dev.bus && pdev->dev.bus->pm) {
373 dev->pg_domain.ops = *pdev->dev.bus->pm;
375 dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
376 dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
377 dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
379 dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
384 * mei_txe_unset_pm_domain - clean pm domain structure for device
388 static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
390 /* stop using pm callbacks if any */
391 dev_pm_domain_set(dev->dev, NULL);
394 static const struct dev_pm_ops mei_txe_pm_ops = {
395 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
398 mei_txe_pm_runtime_suspend,
399 mei_txe_pm_runtime_resume,
400 mei_txe_pm_runtime_idle)
403 #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
405 #define MEI_TXE_PM_OPS NULL
406 #endif /* CONFIG_PM */
409 * PCI driver structure
411 static struct pci_driver mei_txe_driver = {
412 .name = KBUILD_MODNAME,
413 .id_table = mei_txe_pci_tbl,
414 .probe = mei_txe_probe,
415 .remove = mei_txe_remove,
416 .shutdown = mei_txe_shutdown,
417 .driver.pm = MEI_TXE_PM_OPS,
420 module_pci_driver(mei_txe_driver);
422 MODULE_AUTHOR("Intel Corporation");
423 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
424 MODULE_LICENSE("GPL v2");