2c3f5625a04e3445dc2db2b0e5f8f42f83033c44
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mei / pci-txe.c
1 /*
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>
30 #include <linux/pm_runtime.h>
31
32 #include <linux/mei.h>
33
34
35 #include "mei_dev.h"
36 #include "hw-txe.h"
37
38 static const struct pci_device_id mei_txe_pci_tbl[] = {
39         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0F18)}, /* Baytrail */
40         {0, }
41 };
42 MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
43
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);
47 #else
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 */
51
52 static void mei_txe_pci_iounmap(struct pci_dev *pdev, struct mei_txe_hw *hw)
53 {
54         int i;
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;
59                 }
60         }
61 }
62 /**
63  * mei_probe - Device Initialization Routine
64  *
65  * @pdev: PCI device structure
66  * @ent: entry in mei_txe_pci_tbl
67  *
68  * returns 0 on success, <0 on failure.
69  */
70 static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
71 {
72         struct mei_device *dev;
73         struct mei_txe_hw *hw;
74         int err;
75         int i;
76
77         /* enable pci dev */
78         err = pci_enable_device(pdev);
79         if (err) {
80                 dev_err(&pdev->dev, "failed to enable pci device.\n");
81                 goto end;
82         }
83         /* set PCI host mastering  */
84         pci_set_master(pdev);
85         /* pci request regions for mei driver */
86         err = pci_request_regions(pdev, KBUILD_MODNAME);
87         if (err) {
88                 dev_err(&pdev->dev, "failed to get pci regions.\n");
89                 goto disable_device;
90         }
91
92         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
93         if (err) {
94                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
95                 if (err) {
96                         dev_err(&pdev->dev, "No suitable DMA available.\n");
97                         goto release_regions;
98                 }
99         }
100
101         /* allocates and initializes the mei dev structure */
102         dev = mei_txe_dev_init(pdev);
103         if (!dev) {
104                 err = -ENOMEM;
105                 goto release_regions;
106         }
107         hw = to_txe_hw(dev);
108
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");
114                         err = -ENOMEM;
115                         goto free_device;
116                 }
117         }
118
119
120         pci_enable_msi(pdev);
121
122         /* clear spurious interrupts */
123         mei_clear_interrupts(dev);
124
125         /* request and enable interrupt  */
126         if (pci_dev_msi_enabled(pdev))
127                 err = request_threaded_irq(pdev->irq,
128                         NULL,
129                         mei_txe_irq_thread_handler,
130                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
131         else
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);
136         if (err) {
137                 dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
138                         pdev->irq);
139                 goto free_device;
140         }
141
142         if (mei_start(dev)) {
143                 dev_err(&pdev->dev, "init hw failure.\n");
144                 err = -ENODEV;
145                 goto release_irq;
146         }
147
148         pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
149         pm_runtime_use_autosuspend(&pdev->dev);
150
151         err = mei_register(dev);
152         if (err)
153                 goto release_irq;
154
155         pci_set_drvdata(pdev, dev);
156
157         /*
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.
161         */
162         if (!pci_dev_run_wake(pdev))
163                 mei_txe_set_pm_domain(dev);
164
165         pm_runtime_put_noidle(&pdev->dev);
166
167         return 0;
168
169 release_irq:
170
171         mei_cancel_work(dev);
172
173         /* disable interrupts */
174         mei_disable_interrupts(dev);
175
176         free_irq(pdev->irq, dev);
177         pci_disable_msi(pdev);
178
179 free_device:
180         mei_txe_pci_iounmap(pdev, hw);
181
182         kfree(dev);
183 release_regions:
184         pci_release_regions(pdev);
185 disable_device:
186         pci_disable_device(pdev);
187 end:
188         dev_err(&pdev->dev, "initialization failed.\n");
189         return err;
190 }
191
192 /**
193  * mei_remove - Device Removal Routine
194  *
195  * @pdev: PCI device structure
196  *
197  * mei_remove is called by the PCI subsystem to alert the driver
198  * that it should release a PCI device.
199  */
200 static void mei_txe_remove(struct pci_dev *pdev)
201 {
202         struct mei_device *dev;
203         struct mei_txe_hw *hw;
204
205         dev = pci_get_drvdata(pdev);
206         if (!dev) {
207                 dev_err(&pdev->dev, "mei: dev =NULL\n");
208                 return;
209         }
210
211         pm_runtime_get_noresume(&pdev->dev);
212
213         hw = to_txe_hw(dev);
214
215         mei_stop(dev);
216
217         if (!pci_dev_run_wake(pdev))
218                 mei_txe_unset_pm_domain(dev);
219
220         /* disable interrupts */
221         mei_disable_interrupts(dev);
222         free_irq(pdev->irq, dev);
223         pci_disable_msi(pdev);
224
225         pci_set_drvdata(pdev, NULL);
226
227         mei_txe_pci_iounmap(pdev, hw);
228
229         mei_deregister(dev);
230
231         kfree(dev);
232
233         pci_release_regions(pdev);
234         pci_disable_device(pdev);
235 }
236
237
238 #ifdef CONFIG_PM_SLEEP
239 static int mei_txe_pci_suspend(struct device *device)
240 {
241         struct pci_dev *pdev = to_pci_dev(device);
242         struct mei_device *dev = pci_get_drvdata(pdev);
243
244         if (!dev)
245                 return -ENODEV;
246
247         dev_dbg(&pdev->dev, "suspend\n");
248
249         mei_stop(dev);
250
251         mei_disable_interrupts(dev);
252
253         free_irq(pdev->irq, dev);
254         pci_disable_msi(pdev);
255
256         return 0;
257 }
258
259 static int mei_txe_pci_resume(struct device *device)
260 {
261         struct pci_dev *pdev = to_pci_dev(device);
262         struct mei_device *dev;
263         int err;
264
265         dev = pci_get_drvdata(pdev);
266         if (!dev)
267                 return -ENODEV;
268
269         pci_enable_msi(pdev);
270
271         mei_clear_interrupts(dev);
272
273         /* request and enable interrupt */
274         if (pci_dev_msi_enabled(pdev))
275                 err = request_threaded_irq(pdev->irq,
276                         NULL,
277                         mei_txe_irq_thread_handler,
278                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
279         else
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);
284         if (err) {
285                 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
286                                 pdev->irq);
287                 return err;
288         }
289
290         err = mei_restart(dev);
291
292         return err;
293 }
294 #endif /* CONFIG_PM_SLEEP */
295
296 #ifdef CONFIG_PM_RUNTIME
297 static int mei_txe_pm_runtime_idle(struct device *device)
298 {
299         struct pci_dev *pdev = to_pci_dev(device);
300         struct mei_device *dev;
301
302         dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
303
304         dev = pci_get_drvdata(pdev);
305         if (!dev)
306                 return -ENODEV;
307         if (mei_write_is_idle(dev))
308                 pm_schedule_suspend(device, MEI_TXI_RPM_TIMEOUT * 2);
309
310         return -EBUSY;
311 }
312 static int mei_txe_pm_runtime_suspend(struct device *device)
313 {
314         struct pci_dev *pdev = to_pci_dev(device);
315         struct mei_device *dev;
316         int ret;
317
318         dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
319
320         dev = pci_get_drvdata(pdev);
321         if (!dev)
322                 return -ENODEV;
323
324         mutex_lock(&dev->device_lock);
325
326         if (mei_write_is_idle(dev))
327                 ret = mei_txe_aliveness_set_sync(dev, 0);
328         else
329                 ret = -EAGAIN;
330
331         /*
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
337          */
338          if (!ret && pci_dev_run_wake(pdev))
339                 mei_disable_interrupts(dev);
340
341         dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
342
343         mutex_unlock(&dev->device_lock);
344         return ret;
345 }
346
347 static int mei_txe_pm_runtime_resume(struct device *device)
348 {
349         struct pci_dev *pdev = to_pci_dev(device);
350         struct mei_device *dev;
351         int ret;
352
353         dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
354
355         dev = pci_get_drvdata(pdev);
356         if (!dev)
357                 return -ENODEV;
358
359         mutex_lock(&dev->device_lock);
360
361         mei_enable_interrupts(dev);
362
363         ret = mei_txe_aliveness_set_sync(dev, 1);
364
365         mutex_unlock(&dev->device_lock);
366
367         dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
368
369         return ret;
370 }
371
372 /**
373  * mei_txe_set_pm_domain - fill and set pm domian stucture for device
374  *
375  * @dev: mei_device
376  */
377 static inline void mei_txe_set_pm_domain(struct mei_device *dev)
378 {
379         struct pci_dev *pdev  = dev->pdev;
380
381         if (pdev->dev.bus && pdev->dev.bus->pm) {
382                 dev->pg_domain.ops = *pdev->dev.bus->pm;
383
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;
387
388                 pdev->dev.pm_domain = &dev->pg_domain;
389         }
390 }
391
392 /**
393  * mei_txe_unset_pm_domain - clean pm domian stucture for device
394  *
395  * @dev: mei_device
396  */
397 static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
398 {
399         /* stop using pm callbacks if any */
400         dev->pdev->dev.pm_domain = NULL;
401 }
402 #endif /* CONFIG_PM_RUNTIME */
403
404 #ifdef CONFIG_PM
405 static const struct dev_pm_ops mei_txe_pm_ops = {
406         SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
407                                 mei_txe_pci_resume)
408         SET_RUNTIME_PM_OPS(
409                 mei_txe_pm_runtime_suspend,
410                 mei_txe_pm_runtime_resume,
411                 mei_txe_pm_runtime_idle)
412 };
413
414 #define MEI_TXE_PM_OPS  (&mei_txe_pm_ops)
415 #else
416 #define MEI_TXE_PM_OPS  NULL
417 #endif /* CONFIG_PM */
418
419 /*
420  *  PCI driver structure
421  */
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,
429 };
430
431 module_pci_driver(mei_txe_driver);
432
433 MODULE_AUTHOR("Intel Corporation");
434 MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
435 MODULE_LICENSE("GPL v2");