blob: 30cc30683c07186e7ff33d6395f6e8a64d2cf3c0 [file] [log] [blame]
Tomas Winkler795536a2014-01-16 00:58:35 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2013-2014, Intel Corporation.
5 *
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.
9 *
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
13 * more details.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/fs.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>
Tomeu Vizoso989561d2016-01-07 16:46:13 +010030#include <linux/pm_domain.h>
Alexander Usyskincfe5ab82014-03-18 22:52:03 +020031#include <linux/pm_runtime.h>
Tomas Winkler795536a2014-01-16 00:58:35 +020032
33#include <linux/mei.h>
34
35
36#include "mei_dev.h"
37#include "hw-txe.h"
38
Tomas Winklera05f8f82014-03-16 14:35:58 +020039static const struct pci_device_id mei_txe_pci_tbl[] = {
Tomas Winkler4ad96db2014-09-29 16:31:45 +030040 {PCI_VDEVICE(INTEL, 0x0F18)}, /* Baytrail */
Tomas Winklere88281e2014-11-18 00:13:21 +020041 {PCI_VDEVICE(INTEL, 0x2298)}, /* Cherrytrail */
Tomas Winkler4ad96db2014-09-29 16:31:45 +030042
Tomas Winkler795536a2014-01-16 00:58:35 +020043 {0, }
44};
45MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
46
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +010047#ifdef CONFIG_PM
Alexander Usyskind2d56fa2014-03-18 22:52:06 +020048static inline void mei_txe_set_pm_domain(struct mei_device *dev);
49static inline void mei_txe_unset_pm_domain(struct mei_device *dev);
50#else
51static inline void mei_txe_set_pm_domain(struct mei_device *dev) {}
52static inline void mei_txe_unset_pm_domain(struct mei_device *dev) {}
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +010053#endif /* CONFIG_PM */
Tomas Winkler795536a2014-01-16 00:58:35 +020054
55static void mei_txe_pci_iounmap(struct pci_dev *pdev, struct mei_txe_hw *hw)
56{
57 int i;
Tomas Winkler92db1552014-09-29 16:31:37 +030058
Tomas Winkler795536a2014-01-16 00:58:35 +020059 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;
63 }
64 }
65}
66/**
Alexander Usyskin3908be62015-02-10 10:39:35 +020067 * mei_txe_probe - Device Initialization Routine
Tomas Winkler795536a2014-01-16 00:58:35 +020068 *
69 * @pdev: PCI device structure
70 * @ent: entry in mei_txe_pci_tbl
71 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030072 * Return: 0 on success, <0 on failure.
Tomas Winkler795536a2014-01-16 00:58:35 +020073 */
74static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
75{
76 struct mei_device *dev;
77 struct mei_txe_hw *hw;
78 int err;
79 int i;
80
81 /* enable pci dev */
82 err = pci_enable_device(pdev);
83 if (err) {
84 dev_err(&pdev->dev, "failed to enable pci device.\n");
85 goto end;
86 }
87 /* set PCI host mastering */
88 pci_set_master(pdev);
89 /* pci request regions for mei driver */
90 err = pci_request_regions(pdev, KBUILD_MODNAME);
91 if (err) {
92 dev_err(&pdev->dev, "failed to get pci regions.\n");
93 goto disable_device;
94 }
95
96 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
97 if (err) {
98 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
99 if (err) {
100 dev_err(&pdev->dev, "No suitable DMA available.\n");
101 goto release_regions;
102 }
103 }
104
105 /* allocates and initializes the mei dev structure */
Tomas Winkler4ad96db2014-09-29 16:31:45 +0300106 dev = mei_txe_dev_init(pdev);
Tomas Winkler795536a2014-01-16 00:58:35 +0200107 if (!dev) {
108 err = -ENOMEM;
109 goto release_regions;
110 }
111 hw = to_txe_hw(dev);
112
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");
118 err = -ENOMEM;
119 goto free_device;
120 }
121 }
122
123
124 pci_enable_msi(pdev);
125
126 /* clear spurious interrupts */
127 mei_clear_interrupts(dev);
128
129 /* request and enable interrupt */
130 if (pci_dev_msi_enabled(pdev))
131 err = request_threaded_irq(pdev->irq,
132 NULL,
133 mei_txe_irq_thread_handler,
134 IRQF_ONESHOT, KBUILD_MODNAME, dev);
135 else
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);
140 if (err) {
141 dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
142 pdev->irq);
143 goto free_device;
144 }
145
146 if (mei_start(dev)) {
147 dev_err(&pdev->dev, "init hw failure.\n");
148 err = -ENODEV;
149 goto release_irq;
150 }
151
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200152 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
153 pm_runtime_use_autosuspend(&pdev->dev);
154
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300155 err = mei_register(dev, &pdev->dev);
Tomas Winkler795536a2014-01-16 00:58:35 +0200156 if (err)
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200157 goto stop;
Tomas Winkler795536a2014-01-16 00:58:35 +0200158
159 pci_set_drvdata(pdev, dev);
160
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200161 /*
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.
165 */
166 if (!pci_dev_run_wake(pdev))
167 mei_txe_set_pm_domain(dev);
168
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200169 pm_runtime_put_noidle(&pdev->dev);
170
Tomas Winkler795536a2014-01-16 00:58:35 +0200171 return 0;
172
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200173stop:
174 mei_stop(dev);
Tomas Winkler795536a2014-01-16 00:58:35 +0200175release_irq:
176
177 mei_cancel_work(dev);
178
179 /* disable interrupts */
180 mei_disable_interrupts(dev);
181
182 free_irq(pdev->irq, dev);
183 pci_disable_msi(pdev);
184
185free_device:
186 mei_txe_pci_iounmap(pdev, hw);
187
188 kfree(dev);
189release_regions:
190 pci_release_regions(pdev);
191disable_device:
192 pci_disable_device(pdev);
193end:
194 dev_err(&pdev->dev, "initialization failed.\n");
195 return err;
196}
197
198/**
Alexander Usyskin3908be62015-02-10 10:39:35 +0200199 * mei_txe_remove - Device Removal Routine
Tomas Winkler795536a2014-01-16 00:58:35 +0200200 *
201 * @pdev: PCI device structure
202 *
203 * mei_remove is called by the PCI subsystem to alert the driver
204 * that it should release a PCI device.
205 */
206static void mei_txe_remove(struct pci_dev *pdev)
207{
208 struct mei_device *dev;
209 struct mei_txe_hw *hw;
210
211 dev = pci_get_drvdata(pdev);
212 if (!dev) {
213 dev_err(&pdev->dev, "mei: dev =NULL\n");
214 return;
215 }
216
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200217 pm_runtime_get_noresume(&pdev->dev);
218
Tomas Winkler795536a2014-01-16 00:58:35 +0200219 hw = to_txe_hw(dev);
220
221 mei_stop(dev);
222
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200223 if (!pci_dev_run_wake(pdev))
224 mei_txe_unset_pm_domain(dev);
225
Tomas Winkler795536a2014-01-16 00:58:35 +0200226 /* disable interrupts */
227 mei_disable_interrupts(dev);
228 free_irq(pdev->irq, dev);
229 pci_disable_msi(pdev);
230
231 pci_set_drvdata(pdev, NULL);
232
233 mei_txe_pci_iounmap(pdev, hw);
234
235 mei_deregister(dev);
236
237 kfree(dev);
238
239 pci_release_regions(pdev);
240 pci_disable_device(pdev);
241}
242
243
Tomas Winklere0270ad2014-02-18 14:31:09 +0200244#ifdef CONFIG_PM_SLEEP
Tomas Winkler795536a2014-01-16 00:58:35 +0200245static int mei_txe_pci_suspend(struct device *device)
246{
247 struct pci_dev *pdev = to_pci_dev(device);
248 struct mei_device *dev = pci_get_drvdata(pdev);
249
250 if (!dev)
251 return -ENODEV;
252
253 dev_dbg(&pdev->dev, "suspend\n");
254
255 mei_stop(dev);
256
257 mei_disable_interrupts(dev);
258
259 free_irq(pdev->irq, dev);
260 pci_disable_msi(pdev);
261
262 return 0;
263}
264
265static int mei_txe_pci_resume(struct device *device)
266{
267 struct pci_dev *pdev = to_pci_dev(device);
268 struct mei_device *dev;
269 int err;
270
271 dev = pci_get_drvdata(pdev);
272 if (!dev)
273 return -ENODEV;
274
275 pci_enable_msi(pdev);
276
277 mei_clear_interrupts(dev);
278
279 /* request and enable interrupt */
280 if (pci_dev_msi_enabled(pdev))
281 err = request_threaded_irq(pdev->irq,
282 NULL,
283 mei_txe_irq_thread_handler,
284 IRQF_ONESHOT, KBUILD_MODNAME, dev);
285 else
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);
290 if (err) {
291 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
292 pdev->irq);
293 return err;
294 }
295
296 err = mei_restart(dev);
297
298 return err;
299}
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200300#endif /* CONFIG_PM_SLEEP */
Tomas Winkler795536a2014-01-16 00:58:35 +0200301
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100302#ifdef CONFIG_PM
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200303static int mei_txe_pm_runtime_idle(struct device *device)
304{
305 struct pci_dev *pdev = to_pci_dev(device);
306 struct mei_device *dev;
307
308 dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
309
310 dev = pci_get_drvdata(pdev);
311 if (!dev)
312 return -ENODEV;
313 if (mei_write_is_idle(dev))
Alexander Usyskind5d83f82014-07-17 10:53:36 +0300314 pm_runtime_autosuspend(device);
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200315
316 return -EBUSY;
317}
318static int mei_txe_pm_runtime_suspend(struct device *device)
319{
320 struct pci_dev *pdev = to_pci_dev(device);
321 struct mei_device *dev;
322 int ret;
323
324 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
325
326 dev = pci_get_drvdata(pdev);
327 if (!dev)
328 return -ENODEV;
329
330 mutex_lock(&dev->device_lock);
331
332 if (mei_write_is_idle(dev))
333 ret = mei_txe_aliveness_set_sync(dev, 0);
334 else
335 ret = -EAGAIN;
336
337 /*
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
343 */
Tomas Winkler900f4452015-05-04 09:43:59 +0300344 if (!ret && pci_dev_run_wake(pdev))
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200345 mei_disable_interrupts(dev);
346
347 dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
348
349 mutex_unlock(&dev->device_lock);
350 return ret;
351}
352
353static int mei_txe_pm_runtime_resume(struct device *device)
354{
355 struct pci_dev *pdev = to_pci_dev(device);
356 struct mei_device *dev;
357 int ret;
358
359 dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
360
361 dev = pci_get_drvdata(pdev);
362 if (!dev)
363 return -ENODEV;
364
365 mutex_lock(&dev->device_lock);
366
367 mei_enable_interrupts(dev);
368
369 ret = mei_txe_aliveness_set_sync(dev, 1);
370
371 mutex_unlock(&dev->device_lock);
372
373 dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
374
375 return ret;
376}
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200377
378/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200379 * mei_txe_set_pm_domain - fill and set pm domain structure for device
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200380 *
381 * @dev: mei_device
382 */
383static inline void mei_txe_set_pm_domain(struct mei_device *dev)
384{
Tomas Winklerd08b8fc2014-09-29 16:31:44 +0300385 struct pci_dev *pdev = to_pci_dev(dev->dev);
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200386
387 if (pdev->dev.bus && pdev->dev.bus->pm) {
388 dev->pg_domain.ops = *pdev->dev.bus->pm;
389
390 dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
391 dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
392 dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
393
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100394 dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200395 }
396}
397
398/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200399 * mei_txe_unset_pm_domain - clean pm domain structure for device
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200400 *
401 * @dev: mei_device
402 */
403static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
404{
405 /* stop using pm callbacks if any */
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100406 dev_pm_domain_set(dev->dev, NULL);
Alexander Usyskind2d56fa2014-03-18 22:52:06 +0200407}
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200408
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200409static const struct dev_pm_ops mei_txe_pm_ops = {
410 SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
411 mei_txe_pci_resume)
412 SET_RUNTIME_PM_OPS(
413 mei_txe_pm_runtime_suspend,
414 mei_txe_pm_runtime_resume,
415 mei_txe_pm_runtime_idle)
416};
Tomas Winkler795536a2014-01-16 00:58:35 +0200417
418#define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
419#else
420#define MEI_TXE_PM_OPS NULL
Alexander Usyskincfe5ab82014-03-18 22:52:03 +0200421#endif /* CONFIG_PM */
422
Tomas Winkler795536a2014-01-16 00:58:35 +0200423/*
424 * PCI driver structure
425 */
426static struct pci_driver mei_txe_driver = {
427 .name = KBUILD_MODNAME,
428 .id_table = mei_txe_pci_tbl,
429 .probe = mei_txe_probe,
430 .remove = mei_txe_remove,
431 .shutdown = mei_txe_remove,
432 .driver.pm = MEI_TXE_PM_OPS,
433};
434
435module_pci_driver(mei_txe_driver);
436
437MODULE_AUTHOR("Intel Corporation");
438MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
439MODULE_LICENSE("GPL v2");