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 {MEI_PCI_DEVICE(0x0F18, mei_txe_cfg
)}, /* 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 const struct mei_cfg
*cfg
= (struct mei_cfg
*)(ent
->driver_data
);
73 struct mei_device
*dev
;
74 struct mei_txe_hw
*hw
;
79 err
= pci_enable_device(pdev
);
81 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
84 /* set PCI host mastering */
86 /* pci request regions for mei driver */
87 err
= pci_request_regions(pdev
, KBUILD_MODNAME
);
89 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
93 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(36));
95 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
97 dev_err(&pdev
->dev
, "No suitable DMA available.\n");
102 /* allocates and initializes the mei dev structure */
103 dev
= mei_txe_dev_init(pdev
, cfg
);
106 goto release_regions
;
110 /* mapping IO device memory */
111 for (i
= SEC_BAR
; i
< NUM_OF_MEM_BARS
; i
++) {
112 hw
->mem_addr
[i
] = pci_iomap(pdev
, i
, 0);
113 if (!hw
->mem_addr
[i
]) {
114 dev_err(&pdev
->dev
, "mapping I/O device memory failure.\n");
121 pci_enable_msi(pdev
);
123 /* clear spurious interrupts */
124 mei_clear_interrupts(dev
);
126 /* request and enable interrupt */
127 if (pci_dev_msi_enabled(pdev
))
128 err
= request_threaded_irq(pdev
->irq
,
130 mei_txe_irq_thread_handler
,
131 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
133 err
= request_threaded_irq(pdev
->irq
,
134 mei_txe_irq_quick_handler
,
135 mei_txe_irq_thread_handler
,
136 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
138 dev_err(&pdev
->dev
, "mei: request_threaded_irq failure. irq = %d\n",
143 if (mei_start(dev
)) {
144 dev_err(&pdev
->dev
, "init hw failure.\n");
149 pm_runtime_set_autosuspend_delay(&pdev
->dev
, MEI_TXI_RPM_TIMEOUT
);
150 pm_runtime_use_autosuspend(&pdev
->dev
);
152 err
= mei_register(dev
);
156 pci_set_drvdata(pdev
, dev
);
159 * For not wake-able HW runtime pm framework
160 * can't be used on pci device level.
161 * Use domain runtime pm callbacks instead.
163 if (!pci_dev_run_wake(pdev
))
164 mei_txe_set_pm_domain(dev
);
166 pm_runtime_put_noidle(&pdev
->dev
);
172 mei_cancel_work(dev
);
174 /* disable interrupts */
175 mei_disable_interrupts(dev
);
177 free_irq(pdev
->irq
, dev
);
178 pci_disable_msi(pdev
);
181 mei_txe_pci_iounmap(pdev
, hw
);
185 pci_release_regions(pdev
);
187 pci_disable_device(pdev
);
189 dev_err(&pdev
->dev
, "initialization failed.\n");
194 * mei_remove - Device Removal Routine
196 * @pdev: PCI device structure
198 * mei_remove is called by the PCI subsystem to alert the driver
199 * that it should release a PCI device.
201 static void mei_txe_remove(struct pci_dev
*pdev
)
203 struct mei_device
*dev
;
204 struct mei_txe_hw
*hw
;
206 dev
= pci_get_drvdata(pdev
);
208 dev_err(&pdev
->dev
, "mei: dev =NULL\n");
212 pm_runtime_get_noresume(&pdev
->dev
);
218 if (!pci_dev_run_wake(pdev
))
219 mei_txe_unset_pm_domain(dev
);
221 /* disable interrupts */
222 mei_disable_interrupts(dev
);
223 free_irq(pdev
->irq
, dev
);
224 pci_disable_msi(pdev
);
226 pci_set_drvdata(pdev
, NULL
);
228 mei_txe_pci_iounmap(pdev
, hw
);
234 pci_release_regions(pdev
);
235 pci_disable_device(pdev
);
239 #ifdef CONFIG_PM_SLEEP
240 static int mei_txe_pci_suspend(struct device
*device
)
242 struct pci_dev
*pdev
= to_pci_dev(device
);
243 struct mei_device
*dev
= pci_get_drvdata(pdev
);
248 dev_dbg(&pdev
->dev
, "suspend\n");
252 mei_disable_interrupts(dev
);
254 free_irq(pdev
->irq
, dev
);
255 pci_disable_msi(pdev
);
260 static int mei_txe_pci_resume(struct device
*device
)
262 struct pci_dev
*pdev
= to_pci_dev(device
);
263 struct mei_device
*dev
;
266 dev
= pci_get_drvdata(pdev
);
270 pci_enable_msi(pdev
);
272 mei_clear_interrupts(dev
);
274 /* request and enable interrupt */
275 if (pci_dev_msi_enabled(pdev
))
276 err
= request_threaded_irq(pdev
->irq
,
278 mei_txe_irq_thread_handler
,
279 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
281 err
= request_threaded_irq(pdev
->irq
,
282 mei_txe_irq_quick_handler
,
283 mei_txe_irq_thread_handler
,
284 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
286 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
291 err
= mei_restart(dev
);
295 #endif /* CONFIG_PM_SLEEP */
297 #ifdef CONFIG_PM_RUNTIME
298 static int mei_txe_pm_runtime_idle(struct device
*device
)
300 struct pci_dev
*pdev
= to_pci_dev(device
);
301 struct mei_device
*dev
;
303 dev_dbg(&pdev
->dev
, "rpm: txe: runtime_idle\n");
305 dev
= pci_get_drvdata(pdev
);
308 if (mei_write_is_idle(dev
))
309 pm_schedule_suspend(device
, MEI_TXI_RPM_TIMEOUT
* 2);
313 static int mei_txe_pm_runtime_suspend(struct device
*device
)
315 struct pci_dev
*pdev
= to_pci_dev(device
);
316 struct mei_device
*dev
;
319 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend\n");
321 dev
= pci_get_drvdata(pdev
);
325 mutex_lock(&dev
->device_lock
);
327 if (mei_write_is_idle(dev
))
328 ret
= mei_txe_aliveness_set_sync(dev
, 0);
333 * If everything is okay we're about to enter PCI low
334 * power state (D3) therefor we need to disable the
335 * interrupts towards host.
336 * However if device is not wakeable we do not enter
337 * D-low state and we need to keep the interrupt kicking
339 if (!ret
&& pci_dev_run_wake(pdev
))
340 mei_disable_interrupts(dev
);
342 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend ret=%d\n", ret
);
344 mutex_unlock(&dev
->device_lock
);
348 static int mei_txe_pm_runtime_resume(struct device
*device
)
350 struct pci_dev
*pdev
= to_pci_dev(device
);
351 struct mei_device
*dev
;
354 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume\n");
356 dev
= pci_get_drvdata(pdev
);
360 mutex_lock(&dev
->device_lock
);
362 mei_enable_interrupts(dev
);
364 ret
= mei_txe_aliveness_set_sync(dev
, 1);
366 mutex_unlock(&dev
->device_lock
);
368 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume ret = %d\n", ret
);
374 * mei_txe_set_pm_domain - fill and set pm domian stucture for device
378 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
)
380 struct pci_dev
*pdev
= dev
->pdev
;
382 if (pdev
->dev
.bus
&& pdev
->dev
.bus
->pm
) {
383 dev
->pg_domain
.ops
= *pdev
->dev
.bus
->pm
;
385 dev
->pg_domain
.ops
.runtime_suspend
= mei_txe_pm_runtime_suspend
;
386 dev
->pg_domain
.ops
.runtime_resume
= mei_txe_pm_runtime_resume
;
387 dev
->pg_domain
.ops
.runtime_idle
= mei_txe_pm_runtime_idle
;
389 pdev
->dev
.pm_domain
= &dev
->pg_domain
;
394 * mei_txe_unset_pm_domain - clean pm domian stucture for device
398 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
)
400 /* stop using pm callbacks if any */
401 dev
->pdev
->dev
.pm_domain
= NULL
;
403 #endif /* CONFIG_PM_RUNTIME */
406 static const struct dev_pm_ops mei_txe_pm_ops
= {
407 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend
,
410 mei_txe_pm_runtime_suspend
,
411 mei_txe_pm_runtime_resume
,
412 mei_txe_pm_runtime_idle
)
415 #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
417 #define MEI_TXE_PM_OPS NULL
418 #endif /* CONFIG_PM */
421 * PCI driver structure
423 static struct pci_driver mei_txe_driver
= {
424 .name
= KBUILD_MODNAME
,
425 .id_table
= mei_txe_pci_tbl
,
426 .probe
= mei_txe_probe
,
427 .remove
= mei_txe_remove
,
428 .shutdown
= mei_txe_remove
,
429 .driver
.pm
= MEI_TXE_PM_OPS
,
432 module_pci_driver(mei_txe_driver
);
434 MODULE_AUTHOR("Intel Corporation");
435 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
436 MODULE_LICENSE("GPL v2");