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_VDEVICE(INTEL
, 0x0F18)}, /* Baytrail */
40 {PCI_VDEVICE(INTEL
, 0x2298)}, /* Cherrytrail */
44 MODULE_DEVICE_TABLE(pci
, mei_txe_pci_tbl
);
47 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
);
48 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
);
50 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
) {}
51 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
) {}
52 #endif /* CONFIG_PM */
54 static void mei_txe_pci_iounmap(struct pci_dev
*pdev
, struct mei_txe_hw
*hw
)
58 for (i
= SEC_BAR
; i
< NUM_OF_MEM_BARS
; i
++) {
59 if (hw
->mem_addr
[i
]) {
60 pci_iounmap(pdev
, hw
->mem_addr
[i
]);
61 hw
->mem_addr
[i
] = NULL
;
66 * mei_txe_probe - Device Initialization Routine
68 * @pdev: PCI device structure
69 * @ent: entry in mei_txe_pci_tbl
71 * Return: 0 on success, <0 on failure.
73 static int mei_txe_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
75 struct mei_device
*dev
;
76 struct mei_txe_hw
*hw
;
81 err
= pci_enable_device(pdev
);
83 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
86 /* set PCI host mastering */
88 /* pci request regions for mei driver */
89 err
= pci_request_regions(pdev
, KBUILD_MODNAME
);
91 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
95 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(36));
97 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
99 dev_err(&pdev
->dev
, "No suitable DMA available.\n");
100 goto release_regions
;
104 /* allocates and initializes the mei dev structure */
105 dev
= mei_txe_dev_init(pdev
);
108 goto release_regions
;
112 /* mapping IO device memory */
113 for (i
= SEC_BAR
; i
< NUM_OF_MEM_BARS
; i
++) {
114 hw
->mem_addr
[i
] = pci_iomap(pdev
, i
, 0);
115 if (!hw
->mem_addr
[i
]) {
116 dev_err(&pdev
->dev
, "mapping I/O device memory failure.\n");
123 pci_enable_msi(pdev
);
125 /* clear spurious interrupts */
126 mei_clear_interrupts(dev
);
128 /* request and enable interrupt */
129 if (pci_dev_msi_enabled(pdev
))
130 err
= request_threaded_irq(pdev
->irq
,
132 mei_txe_irq_thread_handler
,
133 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
135 err
= request_threaded_irq(pdev
->irq
,
136 mei_txe_irq_quick_handler
,
137 mei_txe_irq_thread_handler
,
138 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
140 dev_err(&pdev
->dev
, "mei: request_threaded_irq failure. irq = %d\n",
145 if (mei_start(dev
)) {
146 dev_err(&pdev
->dev
, "init hw failure.\n");
151 pm_runtime_set_autosuspend_delay(&pdev
->dev
, MEI_TXI_RPM_TIMEOUT
);
152 pm_runtime_use_autosuspend(&pdev
->dev
);
154 err
= mei_register(dev
, &pdev
->dev
);
158 pci_set_drvdata(pdev
, dev
);
161 * For not wake-able HW runtime pm framework
162 * can't be used on pci device level.
163 * Use domain runtime pm callbacks instead.
165 if (!pci_dev_run_wake(pdev
))
166 mei_txe_set_pm_domain(dev
);
168 pm_runtime_put_noidle(&pdev
->dev
);
174 mei_cancel_work(dev
);
176 /* disable interrupts */
177 mei_disable_interrupts(dev
);
179 free_irq(pdev
->irq
, dev
);
180 pci_disable_msi(pdev
);
183 mei_txe_pci_iounmap(pdev
, hw
);
187 pci_release_regions(pdev
);
189 pci_disable_device(pdev
);
191 dev_err(&pdev
->dev
, "initialization failed.\n");
196 * mei_txe_remove - Device Removal Routine
198 * @pdev: PCI device structure
200 * mei_remove is called by the PCI subsystem to alert the driver
201 * that it should release a PCI device.
203 static void mei_txe_remove(struct pci_dev
*pdev
)
205 struct mei_device
*dev
;
206 struct mei_txe_hw
*hw
;
208 dev
= pci_get_drvdata(pdev
);
210 dev_err(&pdev
->dev
, "mei: dev =NULL\n");
214 pm_runtime_get_noresume(&pdev
->dev
);
220 if (!pci_dev_run_wake(pdev
))
221 mei_txe_unset_pm_domain(dev
);
223 /* disable interrupts */
224 mei_disable_interrupts(dev
);
225 free_irq(pdev
->irq
, dev
);
226 pci_disable_msi(pdev
);
228 pci_set_drvdata(pdev
, NULL
);
230 mei_txe_pci_iounmap(pdev
, hw
);
236 pci_release_regions(pdev
);
237 pci_disable_device(pdev
);
241 #ifdef CONFIG_PM_SLEEP
242 static int mei_txe_pci_suspend(struct device
*device
)
244 struct pci_dev
*pdev
= to_pci_dev(device
);
245 struct mei_device
*dev
= pci_get_drvdata(pdev
);
250 dev_dbg(&pdev
->dev
, "suspend\n");
254 mei_disable_interrupts(dev
);
256 free_irq(pdev
->irq
, dev
);
257 pci_disable_msi(pdev
);
262 static int mei_txe_pci_resume(struct device
*device
)
264 struct pci_dev
*pdev
= to_pci_dev(device
);
265 struct mei_device
*dev
;
268 dev
= pci_get_drvdata(pdev
);
272 pci_enable_msi(pdev
);
274 mei_clear_interrupts(dev
);
276 /* request and enable interrupt */
277 if (pci_dev_msi_enabled(pdev
))
278 err
= request_threaded_irq(pdev
->irq
,
280 mei_txe_irq_thread_handler
,
281 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
283 err
= request_threaded_irq(pdev
->irq
,
284 mei_txe_irq_quick_handler
,
285 mei_txe_irq_thread_handler
,
286 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
288 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
293 err
= mei_restart(dev
);
297 #endif /* CONFIG_PM_SLEEP */
300 static int mei_txe_pm_runtime_idle(struct device
*device
)
302 struct pci_dev
*pdev
= to_pci_dev(device
);
303 struct mei_device
*dev
;
305 dev_dbg(&pdev
->dev
, "rpm: txe: runtime_idle\n");
307 dev
= pci_get_drvdata(pdev
);
310 if (mei_write_is_idle(dev
))
311 pm_runtime_autosuspend(device
);
315 static int mei_txe_pm_runtime_suspend(struct device
*device
)
317 struct pci_dev
*pdev
= to_pci_dev(device
);
318 struct mei_device
*dev
;
321 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend\n");
323 dev
= pci_get_drvdata(pdev
);
327 mutex_lock(&dev
->device_lock
);
329 if (mei_write_is_idle(dev
))
330 ret
= mei_txe_aliveness_set_sync(dev
, 0);
335 * If everything is okay we're about to enter PCI low
336 * power state (D3) therefor we need to disable the
337 * interrupts towards host.
338 * However if device is not wakeable we do not enter
339 * D-low state and we need to keep the interrupt kicking
341 if (!ret
&& pci_dev_run_wake(pdev
))
342 mei_disable_interrupts(dev
);
344 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend ret=%d\n", ret
);
346 mutex_unlock(&dev
->device_lock
);
350 static int mei_txe_pm_runtime_resume(struct device
*device
)
352 struct pci_dev
*pdev
= to_pci_dev(device
);
353 struct mei_device
*dev
;
356 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume\n");
358 dev
= pci_get_drvdata(pdev
);
362 mutex_lock(&dev
->device_lock
);
364 mei_enable_interrupts(dev
);
366 ret
= mei_txe_aliveness_set_sync(dev
, 1);
368 mutex_unlock(&dev
->device_lock
);
370 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume ret = %d\n", ret
);
376 * mei_txe_set_pm_domain - fill and set pm domain structure for device
380 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
)
382 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
384 if (pdev
->dev
.bus
&& pdev
->dev
.bus
->pm
) {
385 dev
->pg_domain
.ops
= *pdev
->dev
.bus
->pm
;
387 dev
->pg_domain
.ops
.runtime_suspend
= mei_txe_pm_runtime_suspend
;
388 dev
->pg_domain
.ops
.runtime_resume
= mei_txe_pm_runtime_resume
;
389 dev
->pg_domain
.ops
.runtime_idle
= mei_txe_pm_runtime_idle
;
391 pdev
->dev
.pm_domain
= &dev
->pg_domain
;
396 * mei_txe_unset_pm_domain - clean pm domain structure for device
400 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
)
402 /* stop using pm callbacks if any */
403 dev
->dev
->pm_domain
= NULL
;
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");