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_runtime.h>
32 #include <linux/mei.h>
38 static const struct pci_device_id mei_txe_pci_tbl[] = {
39 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0F18)}, /* Baytrail */
42 MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
44 #ifdef CONFIG_PM_RUNTIME
45 static inline void mei_txe_set_pm_domain(struct mei_device *dev);
46 static inline void mei_txe_unset_pm_domain(struct mei_device *dev);
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) {}
50 #endif /* CONFIG_PM_RUNTIME */
52 static void mei_txe_pci_iounmap(struct pci_dev *pdev, struct mei_txe_hw *hw)
55 for (i = SEC_BAR; i < NUM_OF_MEM_BARS; i++) {
56 if (hw->mem_addr[i]) {
57 pci_iounmap(pdev, hw->mem_addr[i]);
58 hw->mem_addr[i] = NULL;
63 * mei_probe - Device Initialization Routine
65 * @pdev: PCI device structure
66 * @ent: entry in mei_txe_pci_tbl
68 * returns 0 on success, <0 on failure.
70 static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
72 struct mei_device *dev;
73 struct mei_txe_hw *hw;
78 err = pci_enable_device(pdev);
80 dev_err(&pdev->dev, "failed to enable pci device.\n");
83 /* set PCI host mastering */
85 /* pci request regions for mei driver */
86 err = pci_request_regions(pdev, KBUILD_MODNAME);
88 dev_err(&pdev->dev, "failed to get pci regions.\n");
92 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
94 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
96 dev_err(&pdev->dev, "No suitable DMA available.\n");
101 /* allocates and initializes the mei dev structure */
102 dev = mei_txe_dev_init(pdev);
105 goto release_regions;
109 /* mapping IO device memory */
110 for (i = SEC_BAR; i < NUM_OF_MEM_BARS; i++) {
111 hw->mem_addr[i] = pci_iomap(pdev, i, 0);
112 if (!hw->mem_addr[i]) {
113 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
120 pci_enable_msi(pdev);
122 /* clear spurious interrupts */
123 mei_clear_interrupts(dev);
125 /* request and enable interrupt */
126 if (pci_dev_msi_enabled(pdev))
127 err = request_threaded_irq(pdev->irq,
129 mei_txe_irq_thread_handler,
130 IRQF_ONESHOT, KBUILD_MODNAME, dev);
132 err = request_threaded_irq(pdev->irq,
133 mei_txe_irq_quick_handler,
134 mei_txe_irq_thread_handler,
135 IRQF_SHARED, KBUILD_MODNAME, dev);
137 dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
142 if (mei_start(dev)) {
143 dev_err(&pdev->dev, "init hw failure.\n");
148 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
149 pm_runtime_use_autosuspend(&pdev->dev);
151 err = mei_register(dev);
155 pci_set_drvdata(pdev, dev);
158 * For not wake-able HW runtime pm framework
159 * can't be used on pci device level.
160 * Use domain runtime pm callbacks instead.
162 if (!pci_dev_run_wake(pdev))
163 mei_txe_set_pm_domain(dev);
165 pm_runtime_put_noidle(&pdev->dev);
171 mei_cancel_work(dev);
173 /* disable interrupts */
174 mei_disable_interrupts(dev);
176 free_irq(pdev->irq, dev);
177 pci_disable_msi(pdev);
180 mei_txe_pci_iounmap(pdev, hw);
184 pci_release_regions(pdev);
186 pci_disable_device(pdev);
188 dev_err(&pdev->dev, "initialization failed.\n");
193 * mei_remove - Device Removal Routine
195 * @pdev: PCI device structure
197 * mei_remove is called by the PCI subsystem to alert the driver
198 * that it should release a PCI device.
200 static void mei_txe_remove(struct pci_dev *pdev)
202 struct mei_device *dev;
203 struct mei_txe_hw *hw;
205 dev = pci_get_drvdata(pdev);
207 dev_err(&pdev->dev, "mei: dev =NULL\n");
211 pm_runtime_get_noresume(&pdev->dev);
217 if (!pci_dev_run_wake(pdev))
218 mei_txe_unset_pm_domain(dev);
220 /* disable interrupts */
221 mei_disable_interrupts(dev);
222 free_irq(pdev->irq, dev);
223 pci_disable_msi(pdev);
225 pci_set_drvdata(pdev, NULL);
227 mei_txe_pci_iounmap(pdev, hw);
233 pci_release_regions(pdev);
234 pci_disable_device(pdev);
238 #ifdef CONFIG_PM_SLEEP
239 static int mei_txe_pci_suspend(struct device *device)
241 struct pci_dev *pdev = to_pci_dev(device);
242 struct mei_device *dev = pci_get_drvdata(pdev);
247 dev_dbg(&pdev->dev, "suspend\n");
251 mei_disable_interrupts(dev);
253 free_irq(pdev->irq, dev);
254 pci_disable_msi(pdev);
259 static int mei_txe_pci_resume(struct device *device)
261 struct pci_dev *pdev = to_pci_dev(device);
262 struct mei_device *dev;
265 dev = pci_get_drvdata(pdev);
269 pci_enable_msi(pdev);
271 mei_clear_interrupts(dev);
273 /* request and enable interrupt */
274 if (pci_dev_msi_enabled(pdev))
275 err = request_threaded_irq(pdev->irq,
277 mei_txe_irq_thread_handler,
278 IRQF_ONESHOT, KBUILD_MODNAME, dev);
280 err = request_threaded_irq(pdev->irq,
281 mei_txe_irq_quick_handler,
282 mei_txe_irq_thread_handler,
283 IRQF_SHARED, KBUILD_MODNAME, dev);
285 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
290 err = mei_restart(dev);
294 #endif /* CONFIG_PM_SLEEP */
296 #ifdef CONFIG_PM_RUNTIME
297 static int mei_txe_pm_runtime_idle(struct device *device)
299 struct pci_dev *pdev = to_pci_dev(device);
300 struct mei_device *dev;
302 dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
304 dev = pci_get_drvdata(pdev);
307 if (mei_write_is_idle(dev))
308 pm_schedule_suspend(device, MEI_TXI_RPM_TIMEOUT * 2);
312 static int mei_txe_pm_runtime_suspend(struct device *device)
314 struct pci_dev *pdev = to_pci_dev(device);
315 struct mei_device *dev;
318 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
320 dev = pci_get_drvdata(pdev);
324 mutex_lock(&dev->device_lock);
326 if (mei_write_is_idle(dev))
327 ret = mei_txe_aliveness_set_sync(dev, 0);
332 * If everything is okay we're about to enter PCI low
333 * power state (D3) therefor we need to disable the
334 * interrupts towards host.
335 * However if device is not wakeable we do not enter
336 * D-low state and we need to keep the interrupt kicking
338 if (!ret && pci_dev_run_wake(pdev))
339 mei_disable_interrupts(dev);
341 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
343 mutex_unlock(&dev->device_lock);
347 static int mei_txe_pm_runtime_resume(struct device *device)
349 struct pci_dev *pdev = to_pci_dev(device);
350 struct mei_device *dev;
353 dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
355 dev = pci_get_drvdata(pdev);
359 mutex_lock(&dev->device_lock);
361 mei_enable_interrupts(dev);
363 ret = mei_txe_aliveness_set_sync(dev, 1);
365 mutex_unlock(&dev->device_lock);
367 dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
373 * mei_txe_set_pm_domain - fill and set pm domian stucture for device
377 static inline void mei_txe_set_pm_domain(struct mei_device *dev)
379 struct pci_dev *pdev = dev->pdev;
381 if (pdev->dev.bus && pdev->dev.bus->pm) {
382 dev->pg_domain.ops = *pdev->dev.bus->pm;
384 dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
385 dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
386 dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
388 pdev->dev.pm_domain = &dev->pg_domain;
393 * mei_txe_unset_pm_domain - clean pm domian stucture for device
397 static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
399 /* stop using pm callbacks if any */
400 dev->pdev->dev.pm_domain = NULL;
402 #endif /* CONFIG_PM_RUNTIME */
405 static const struct dev_pm_ops mei_txe_pm_ops = {
406 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
409 mei_txe_pm_runtime_suspend,
410 mei_txe_pm_runtime_resume,
411 mei_txe_pm_runtime_idle)
414 #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
416 #define MEI_TXE_PM_OPS NULL
417 #endif /* CONFIG_PM */
420 * PCI driver structure
422 static struct pci_driver mei_txe_driver = {
423 .name = KBUILD_MODNAME,
424 .id_table = mei_txe_pci_tbl,
425 .probe = mei_txe_probe,
426 .remove = mei_txe_remove,
427 .shutdown = mei_txe_remove,
428 .driver.pm = MEI_TXE_PM_OPS,
431 module_pci_driver(mei_txe_driver);
433 MODULE_AUTHOR("Intel Corporation");
434 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
435 MODULE_LICENSE("GPL v2");