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 */
55 static void mei_txe_pci_iounmap(struct pci_dev
*pdev
, struct mei_txe_hw
*hw
)
59 for (i
= SEC_BAR
; i
< NUM_OF_MEM_BARS
; i
++) {
60 if (hw
->mem_addr
[i
]) {
61 pci_iounmap(pdev
, hw
->mem_addr
[i
]);
62 hw
->mem_addr
[i
] = NULL
;
67 * mei_txe_probe - Device Initialization Routine
69 * @pdev: PCI device structure
70 * @ent: entry in mei_txe_pci_tbl
72 * Return: 0 on success, <0 on failure.
74 static int mei_txe_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
76 struct mei_device
*dev
;
77 struct mei_txe_hw
*hw
;
82 err
= pci_enable_device(pdev
);
84 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
87 /* set PCI host mastering */
89 /* pci request regions for mei driver */
90 err
= pci_request_regions(pdev
, KBUILD_MODNAME
);
92 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
96 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(36));
98 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
100 dev_err(&pdev
->dev
, "No suitable DMA available.\n");
101 goto release_regions
;
105 /* allocates and initializes the mei dev structure */
106 dev
= mei_txe_dev_init(pdev
);
109 goto release_regions
;
113 /* mapping IO device memory */
114 for (i
= SEC_BAR
; i
< NUM_OF_MEM_BARS
; i
++) {
115 hw
->mem_addr
[i
] = pci_iomap(pdev
, i
, 0);
116 if (!hw
->mem_addr
[i
]) {
117 dev_err(&pdev
->dev
, "mapping I/O device memory failure.\n");
124 pci_enable_msi(pdev
);
126 /* clear spurious interrupts */
127 mei_clear_interrupts(dev
);
129 /* request and enable interrupt */
130 if (pci_dev_msi_enabled(pdev
))
131 err
= request_threaded_irq(pdev
->irq
,
133 mei_txe_irq_thread_handler
,
134 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
136 err
= request_threaded_irq(pdev
->irq
,
137 mei_txe_irq_quick_handler
,
138 mei_txe_irq_thread_handler
,
139 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
141 dev_err(&pdev
->dev
, "mei: request_threaded_irq failure. irq = %d\n",
146 if (mei_start(dev
)) {
147 dev_err(&pdev
->dev
, "init hw failure.\n");
152 pm_runtime_set_autosuspend_delay(&pdev
->dev
, MEI_TXI_RPM_TIMEOUT
);
153 pm_runtime_use_autosuspend(&pdev
->dev
);
155 err
= mei_register(dev
, &pdev
->dev
);
159 pci_set_drvdata(pdev
, dev
);
162 * For not wake-able HW runtime pm framework
163 * can't be used on pci device level.
164 * Use domain runtime pm callbacks instead.
166 if (!pci_dev_run_wake(pdev
))
167 mei_txe_set_pm_domain(dev
);
169 pm_runtime_put_noidle(&pdev
->dev
);
177 mei_cancel_work(dev
);
179 /* disable interrupts */
180 mei_disable_interrupts(dev
);
182 free_irq(pdev
->irq
, dev
);
183 pci_disable_msi(pdev
);
186 mei_txe_pci_iounmap(pdev
, hw
);
190 pci_release_regions(pdev
);
192 pci_disable_device(pdev
);
194 dev_err(&pdev
->dev
, "initialization failed.\n");
199 * mei_txe_remove - Device Removal Routine
201 * @pdev: PCI device structure
203 * mei_remove is called by the PCI subsystem to alert the driver
204 * that it should release a PCI device.
206 static void mei_txe_remove(struct pci_dev
*pdev
)
208 struct mei_device
*dev
;
209 struct mei_txe_hw
*hw
;
211 dev
= pci_get_drvdata(pdev
);
213 dev_err(&pdev
->dev
, "mei: dev =NULL\n");
217 pm_runtime_get_noresume(&pdev
->dev
);
223 if (!pci_dev_run_wake(pdev
))
224 mei_txe_unset_pm_domain(dev
);
226 /* disable interrupts */
227 mei_disable_interrupts(dev
);
228 free_irq(pdev
->irq
, dev
);
229 pci_disable_msi(pdev
);
231 pci_set_drvdata(pdev
, NULL
);
233 mei_txe_pci_iounmap(pdev
, hw
);
239 pci_release_regions(pdev
);
240 pci_disable_device(pdev
);
244 #ifdef CONFIG_PM_SLEEP
245 static int mei_txe_pci_suspend(struct device
*device
)
247 struct pci_dev
*pdev
= to_pci_dev(device
);
248 struct mei_device
*dev
= pci_get_drvdata(pdev
);
253 dev_dbg(&pdev
->dev
, "suspend\n");
257 mei_disable_interrupts(dev
);
259 free_irq(pdev
->irq
, dev
);
260 pci_disable_msi(pdev
);
265 static int mei_txe_pci_resume(struct device
*device
)
267 struct pci_dev
*pdev
= to_pci_dev(device
);
268 struct mei_device
*dev
;
271 dev
= pci_get_drvdata(pdev
);
275 pci_enable_msi(pdev
);
277 mei_clear_interrupts(dev
);
279 /* request and enable interrupt */
280 if (pci_dev_msi_enabled(pdev
))
281 err
= request_threaded_irq(pdev
->irq
,
283 mei_txe_irq_thread_handler
,
284 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
286 err
= request_threaded_irq(pdev
->irq
,
287 mei_txe_irq_quick_handler
,
288 mei_txe_irq_thread_handler
,
289 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
291 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
296 err
= mei_restart(dev
);
300 #endif /* CONFIG_PM_SLEEP */
303 static int mei_txe_pm_runtime_idle(struct device
*device
)
305 struct pci_dev
*pdev
= to_pci_dev(device
);
306 struct mei_device
*dev
;
308 dev_dbg(&pdev
->dev
, "rpm: txe: runtime_idle\n");
310 dev
= pci_get_drvdata(pdev
);
313 if (mei_write_is_idle(dev
))
314 pm_runtime_autosuspend(device
);
318 static int mei_txe_pm_runtime_suspend(struct device
*device
)
320 struct pci_dev
*pdev
= to_pci_dev(device
);
321 struct mei_device
*dev
;
324 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend\n");
326 dev
= pci_get_drvdata(pdev
);
330 mutex_lock(&dev
->device_lock
);
332 if (mei_write_is_idle(dev
))
333 ret
= mei_txe_aliveness_set_sync(dev
, 0);
338 * If everything is okay we're about to enter PCI low
339 * power state (D3) therefor we need to disable the
340 * interrupts towards host.
341 * However if device is not wakeable we do not enter
342 * D-low state and we need to keep the interrupt kicking
344 if (!ret
&& pci_dev_run_wake(pdev
))
345 mei_disable_interrupts(dev
);
347 dev_dbg(&pdev
->dev
, "rpm: txe: runtime suspend ret=%d\n", ret
);
349 mutex_unlock(&dev
->device_lock
);
351 if (ret
&& ret
!= -EAGAIN
)
352 schedule_work(&dev
->reset_work
);
357 static int mei_txe_pm_runtime_resume(struct device
*device
)
359 struct pci_dev
*pdev
= to_pci_dev(device
);
360 struct mei_device
*dev
;
363 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume\n");
365 dev
= pci_get_drvdata(pdev
);
369 mutex_lock(&dev
->device_lock
);
371 mei_enable_interrupts(dev
);
373 ret
= mei_txe_aliveness_set_sync(dev
, 1);
375 mutex_unlock(&dev
->device_lock
);
377 dev_dbg(&pdev
->dev
, "rpm: txe: runtime resume ret = %d\n", ret
);
380 schedule_work(&dev
->reset_work
);
386 * mei_txe_set_pm_domain - fill and set pm domain structure for device
390 static inline void mei_txe_set_pm_domain(struct mei_device
*dev
)
392 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
394 if (pdev
->dev
.bus
&& pdev
->dev
.bus
->pm
) {
395 dev
->pg_domain
.ops
= *pdev
->dev
.bus
->pm
;
397 dev
->pg_domain
.ops
.runtime_suspend
= mei_txe_pm_runtime_suspend
;
398 dev
->pg_domain
.ops
.runtime_resume
= mei_txe_pm_runtime_resume
;
399 dev
->pg_domain
.ops
.runtime_idle
= mei_txe_pm_runtime_idle
;
401 dev_pm_domain_set(&pdev
->dev
, &dev
->pg_domain
);
406 * mei_txe_unset_pm_domain - clean pm domain structure for device
410 static inline void mei_txe_unset_pm_domain(struct mei_device
*dev
)
412 /* stop using pm callbacks if any */
413 dev_pm_domain_set(dev
->dev
, NULL
);
416 static const struct dev_pm_ops mei_txe_pm_ops
= {
417 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend
,
420 mei_txe_pm_runtime_suspend
,
421 mei_txe_pm_runtime_resume
,
422 mei_txe_pm_runtime_idle
)
425 #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
427 #define MEI_TXE_PM_OPS NULL
428 #endif /* CONFIG_PM */
431 * PCI driver structure
433 static struct pci_driver mei_txe_driver
= {
434 .name
= KBUILD_MODNAME
,
435 .id_table
= mei_txe_pci_tbl
,
436 .probe
= mei_txe_probe
,
437 .remove
= mei_txe_remove
,
438 .shutdown
= mei_txe_remove
,
439 .driver
.pm
= MEI_TXE_PM_OPS
,
442 module_pci_driver(mei_txe_driver
);
444 MODULE_AUTHOR("Intel Corporation");
445 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
446 MODULE_LICENSE("GPL v2");