blob: 8621a198a2ce3eae317e4007cd6c533b56cfecc4 [file] [log] [blame]
Tomas Winkler2703d4b2013-02-06 14:06:39 +02001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, 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 */
Tomas Winkler2703d4b2013-02-06 14:06:39 +020016#include <linux/module.h>
17#include <linux/moduleparam.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/fcntl.h>
Tomas Winkler2703d4b2013-02-06 14:06:39 +020024#include <linux/pci.h>
25#include <linux/poll.h>
Tomas Winkler2703d4b2013-02-06 14:06:39 +020026#include <linux/ioctl.h>
27#include <linux/cdev.h>
28#include <linux/sched.h>
29#include <linux/uuid.h>
30#include <linux/compat.h>
31#include <linux/jiffies.h>
32#include <linux/interrupt.h>
Tomas Winkler2703d4b2013-02-06 14:06:39 +020033
Tomeu Vizoso989561d2016-01-07 16:46:13 +010034#include <linux/pm_domain.h>
Tomas Winkler180ea052014-03-18 22:52:02 +020035#include <linux/pm_runtime.h>
36
Tomas Winkler2703d4b2013-02-06 14:06:39 +020037#include <linux/mei.h>
38
39#include "mei_dev.h"
Tomas Winkler2703d4b2013-02-06 14:06:39 +020040#include "client.h"
Tomas Winkler6e4cd272014-03-11 14:49:23 +020041#include "hw-me-regs.h"
42#include "hw-me.h"
Tomas Winkler2703d4b2013-02-06 14:06:39 +020043
Tomas Winkler2703d4b2013-02-06 14:06:39 +020044/* mei_pci_tbl - PCI Device ID Table */
Tomas Winklera05f8f82014-03-16 14:35:58 +020045static const struct pci_device_id mei_me_pci_tbl[] = {
Alexander Usyskin8d929d42014-05-13 01:30:53 +030046 {MEI_PCI_DEVICE(MEI_DEV_ID_82946GZ, mei_me_legacy_cfg)},
47 {MEI_PCI_DEVICE(MEI_DEV_ID_82G35, mei_me_legacy_cfg)},
48 {MEI_PCI_DEVICE(MEI_DEV_ID_82Q965, mei_me_legacy_cfg)},
49 {MEI_PCI_DEVICE(MEI_DEV_ID_82G965, mei_me_legacy_cfg)},
50 {MEI_PCI_DEVICE(MEI_DEV_ID_82GM965, mei_me_legacy_cfg)},
51 {MEI_PCI_DEVICE(MEI_DEV_ID_82GME965, mei_me_legacy_cfg)},
52 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82Q35, mei_me_legacy_cfg)},
53 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82G33, mei_me_legacy_cfg)},
54 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82Q33, mei_me_legacy_cfg)},
55 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82X38, mei_me_legacy_cfg)},
56 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_3200, mei_me_legacy_cfg)},
57
58 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_6, mei_me_legacy_cfg)},
59 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_7, mei_me_legacy_cfg)},
60 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_8, mei_me_legacy_cfg)},
61 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_9, mei_me_legacy_cfg)},
62 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_10, mei_me_legacy_cfg)},
63 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_1, mei_me_legacy_cfg)},
64 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_2, mei_me_legacy_cfg)},
65 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_3, mei_me_legacy_cfg)},
66 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_4, mei_me_legacy_cfg)},
67 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_1, mei_me_ich_cfg)},
68 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_2, mei_me_ich_cfg)},
69 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_3, mei_me_ich_cfg)},
70 {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_4, mei_me_ich_cfg)},
71
72 {MEI_PCI_DEVICE(MEI_DEV_ID_IBXPK_1, mei_me_pch_cfg)},
73 {MEI_PCI_DEVICE(MEI_DEV_ID_IBXPK_2, mei_me_pch_cfg)},
Tomas Winklerc9199512014-05-13 01:30:54 +030074 {MEI_PCI_DEVICE(MEI_DEV_ID_CPT_1, mei_me_pch_cpt_pbg_cfg)},
75 {MEI_PCI_DEVICE(MEI_DEV_ID_PBG_1, mei_me_pch_cpt_pbg_cfg)},
Alexander Usyskin8d929d42014-05-13 01:30:53 +030076 {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_1, mei_me_pch_cfg)},
77 {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_2, mei_me_pch_cfg)},
78 {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_3, mei_me_pch_cfg)},
Alexander Usyskinedca5ea2014-11-19 17:01:38 +020079 {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_H, mei_me_pch8_sps_cfg)},
80 {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_W, mei_me_pch8_sps_cfg)},
81 {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_LP, mei_me_pch8_cfg)},
82 {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_HR, mei_me_pch8_sps_cfg)},
83 {MEI_PCI_DEVICE(MEI_DEV_ID_WPT_LP, mei_me_pch8_cfg)},
84 {MEI_PCI_DEVICE(MEI_DEV_ID_WPT_LP_2, mei_me_pch8_cfg)},
Tomas Winkler2703d4b2013-02-06 14:06:39 +020085
Tomas Winkler1625c7e2015-08-02 22:20:57 +030086 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT, mei_me_pch8_cfg)},
87 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT_2, mei_me_pch8_cfg)},
Tomas Winkler8c57cac2016-07-20 10:24:02 +030088 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT_H, mei_me_pch8_sps_cfg)},
89 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT_H_2, mei_me_pch8_sps_cfg)},
Tomas Winkler9ff20072016-11-29 16:49:27 +020090 {MEI_PCI_DEVICE(MEI_DEV_ID_LBG, mei_me_pch8_cfg)},
Tomas Winkler1625c7e2015-08-02 22:20:57 +030091
Tomas Winklerdd16f6c2016-02-29 22:03:23 +020092 {MEI_PCI_DEVICE(MEI_DEV_ID_BXT_M, mei_me_pch8_cfg)},
93 {MEI_PCI_DEVICE(MEI_DEV_ID_APL_I, mei_me_pch8_cfg)},
94
Alexander Usyskinac182e82016-09-12 16:21:43 +030095 {MEI_PCI_DEVICE(MEI_DEV_ID_KBP, mei_me_pch8_cfg)},
96 {MEI_PCI_DEVICE(MEI_DEV_ID_KBP_2, mei_me_pch8_cfg)},
97
Tomas Winkler2703d4b2013-02-06 14:06:39 +020098 /* required last entry */
99 {0, }
100};
101
Tomas Winklerb68301e2013-03-27 16:58:29 +0200102MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200103
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100104#ifdef CONFIG_PM
Tomas Winklere13fa902014-03-18 22:52:05 +0200105static inline void mei_me_set_pm_domain(struct mei_device *dev);
106static inline void mei_me_unset_pm_domain(struct mei_device *dev);
107#else
108static inline void mei_me_set_pm_domain(struct mei_device *dev) {}
109static inline void mei_me_unset_pm_domain(struct mei_device *dev) {}
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100110#endif /* CONFIG_PM */
Tomas Winklere13fa902014-03-18 22:52:05 +0200111
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200112/**
Alexander Usyskince231392014-09-29 16:31:50 +0300113 * mei_me_quirk_probe - probe for devices that doesn't valid ME interface
Masanari Iida393b1482013-04-05 01:05:05 +0900114 *
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200115 * @pdev: PCI device structure
Tomas Winklerc9199512014-05-13 01:30:54 +0300116 * @cfg: per generation config
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200117 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300118 * Return: true if ME Interface is valid, false otherwise
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200119 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200120static bool mei_me_quirk_probe(struct pci_dev *pdev,
Tomas Winklerc9199512014-05-13 01:30:54 +0300121 const struct mei_cfg *cfg)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200122{
Tomas Winklerc9199512014-05-13 01:30:54 +0300123 if (cfg->quirk_probe && cfg->quirk_probe(pdev)) {
124 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
125 return false;
Tomas Winkler5e6533a2014-03-25 21:25:18 +0200126 }
127
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200128 return true;
129}
Tomas Winklerc9199512014-05-13 01:30:54 +0300130
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200131/**
Alexander Usyskince231392014-09-29 16:31:50 +0300132 * mei_me_probe - Device Initialization Routine
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200133 *
134 * @pdev: PCI device structure
135 * @ent: entry in kcs_pci_tbl
136 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300137 * Return: 0 on success, <0 on failure.
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200138 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200139static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200140{
Alexander Usyskin8d929d42014-05-13 01:30:53 +0300141 const struct mei_cfg *cfg = (struct mei_cfg *)(ent->driver_data);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200142 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200143 struct mei_me_hw *hw;
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300144 unsigned int irqflags;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200145 int err;
146
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200147
Tomas Winklerc9199512014-05-13 01:30:54 +0300148 if (!mei_me_quirk_probe(pdev, cfg))
149 return -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200150
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200151 /* enable pci dev */
Tomas Winklerf8a09602017-01-26 17:16:26 +0200152 err = pcim_enable_device(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200153 if (err) {
154 dev_err(&pdev->dev, "failed to enable pci device.\n");
155 goto end;
156 }
157 /* set PCI host mastering */
158 pci_set_master(pdev);
Tomas Winklerf8a09602017-01-26 17:16:26 +0200159 /* pci request regions and mapping IO device memory for mei driver */
160 err = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200161 if (err) {
162 dev_err(&pdev->dev, "failed to get pci regions.\n");
Tomas Winklerf8a09602017-01-26 17:16:26 +0200163 goto end;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200164 }
Tomas Winkler3ecfb162013-12-17 15:56:57 +0200165
166 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) ||
167 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
168
169 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
170 if (err)
171 err = dma_set_coherent_mask(&pdev->dev,
172 DMA_BIT_MASK(32));
173 }
174 if (err) {
175 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
Tomas Winklerf8a09602017-01-26 17:16:26 +0200176 goto end;
Tomas Winkler3ecfb162013-12-17 15:56:57 +0200177 }
178
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200179 /* allocates and initializes the mei dev structure */
Alexander Usyskin8d929d42014-05-13 01:30:53 +0300180 dev = mei_me_dev_init(pdev, cfg);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200181 if (!dev) {
182 err = -ENOMEM;
Tomas Winklerf8a09602017-01-26 17:16:26 +0200183 goto end;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200184 }
Tomas Winkler52c34562013-02-06 14:06:40 +0200185 hw = to_me_hw(dev);
Tomas Winklerf8a09602017-01-26 17:16:26 +0200186 hw->mem_addr = pcim_iomap_table(pdev)[0];
187
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200188 pci_enable_msi(pdev);
189
190 /* request and enable interrupt */
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300191 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
192
193 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200194 mei_me_irq_quick_handler,
195 mei_me_irq_thread_handler,
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300196 irqflags, KBUILD_MODNAME, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200197 if (err) {
198 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
199 pdev->irq);
Tomas Winklerf8a09602017-01-26 17:16:26 +0200200 goto end;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200201 }
202
Tomas Winklerc4d589b2013-03-27 16:58:28 +0200203 if (mei_start(dev)) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200204 dev_err(&pdev->dev, "init hw failure.\n");
205 err = -ENODEV;
206 goto release_irq;
207 }
208
Tomas Winkler180ea052014-03-18 22:52:02 +0200209 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_ME_RPM_TIMEOUT);
210 pm_runtime_use_autosuspend(&pdev->dev);
211
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300212 err = mei_register(dev, &pdev->dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200213 if (err)
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200214 goto stop;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200215
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200216 pci_set_drvdata(pdev, dev);
217
Tomas Winklere13fa902014-03-18 22:52:05 +0200218 /*
219 * For not wake-able HW runtime pm framework
220 * can't be used on pci device level.
221 * Use domain runtime pm callbacks instead.
222 */
223 if (!pci_dev_run_wake(pdev))
224 mei_me_set_pm_domain(dev);
225
Tomas Winkler180ea052014-03-18 22:52:02 +0200226 if (mei_pg_is_enabled(dev))
227 pm_runtime_put_noidle(&pdev->dev);
228
Alexander Usyskinc4e87b52013-10-21 22:05:42 +0300229 dev_dbg(&pdev->dev, "initialization successful.\n");
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200230
231 return 0;
232
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200233stop:
234 mei_stop(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200235release_irq:
Tomas Winklerdc844b02013-11-11 13:26:06 +0200236 mei_cancel_work(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200237 mei_disable_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200238 free_irq(pdev->irq, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200239end:
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200240 dev_err(&pdev->dev, "initialization failed.\n");
241 return err;
242}
243
244/**
Tomas Winkler5c4c0102017-03-20 15:04:02 +0200245 * mei_me_shutdown - Device Removal Routine
246 *
247 * @pdev: PCI device structure
248 *
249 * mei_me_shutdown is called from the reboot notifier
250 * it's a simplified version of remove so we go down
251 * faster.
252 */
253static void mei_me_shutdown(struct pci_dev *pdev)
254{
255 struct mei_device *dev;
256
257 dev = pci_get_drvdata(pdev);
258 if (!dev)
259 return;
260
261 dev_dbg(&pdev->dev, "shutdown\n");
262 mei_stop(dev);
263
264 if (!pci_dev_run_wake(pdev))
265 mei_me_unset_pm_domain(dev);
266
267 mei_disable_interrupts(dev);
268 free_irq(pdev->irq, dev);
269}
270
271/**
Alexander Usyskince231392014-09-29 16:31:50 +0300272 * mei_me_remove - Device Removal Routine
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200273 *
274 * @pdev: PCI device structure
275 *
Tomas Winkler5c4c0102017-03-20 15:04:02 +0200276 * mei_me_remove is called by the PCI subsystem to alert the driver
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200277 * that it should release a PCI device.
278 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200279static void mei_me_remove(struct pci_dev *pdev)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200280{
281 struct mei_device *dev;
282
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200283 dev = pci_get_drvdata(pdev);
284 if (!dev)
285 return;
286
Tomas Winkler180ea052014-03-18 22:52:02 +0200287 if (mei_pg_is_enabled(dev))
288 pm_runtime_get_noresume(&pdev->dev);
289
Paul Bolleed6f7ac2013-10-17 13:52:10 +0300290 dev_dbg(&pdev->dev, "stop\n");
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200291 mei_stop(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200292
Tomas Winklere13fa902014-03-18 22:52:05 +0200293 if (!pci_dev_run_wake(pdev))
294 mei_me_unset_pm_domain(dev);
295
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200296 mei_disable_interrupts(dev);
297
298 free_irq(pdev->irq, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200299
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300300 mei_deregister(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200301}
Tomas Winklerf8a09602017-01-26 17:16:26 +0200302
Alexander Usyskin16833252014-02-18 14:31:08 +0200303#ifdef CONFIG_PM_SLEEP
Tomas Winklerb68301e2013-03-27 16:58:29 +0200304static int mei_me_pci_suspend(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200305{
306 struct pci_dev *pdev = to_pci_dev(device);
307 struct mei_device *dev = pci_get_drvdata(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200308
309 if (!dev)
310 return -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200311
Paul Bolleed6f7ac2013-10-17 13:52:10 +0300312 dev_dbg(&pdev->dev, "suspend\n");
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200313
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200314 mei_stop(dev);
315
316 mei_disable_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200317
318 free_irq(pdev->irq, dev);
319 pci_disable_msi(pdev);
320
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200321 return 0;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200322}
323
Tomas Winklerb68301e2013-03-27 16:58:29 +0200324static int mei_me_pci_resume(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200325{
326 struct pci_dev *pdev = to_pci_dev(device);
327 struct mei_device *dev;
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300328 unsigned int irqflags;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200329 int err;
330
331 dev = pci_get_drvdata(pdev);
332 if (!dev)
333 return -ENODEV;
334
335 pci_enable_msi(pdev);
336
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300337 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
338
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200339 /* request and enable interrupt */
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300340 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200341 mei_me_irq_quick_handler,
342 mei_me_irq_thread_handler,
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300343 irqflags, KBUILD_MODNAME, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200344
345 if (err) {
346 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
347 pdev->irq);
348 return err;
349 }
350
Tomas Winkler33ec0822014-01-12 00:36:09 +0200351 err = mei_restart(dev);
352 if (err)
353 return err;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200354
355 /* Start timer if stopped in suspend */
356 schedule_delayed_work(&dev->timer_work, HZ);
357
Tomas Winkler33ec0822014-01-12 00:36:09 +0200358 return 0;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200359}
Tomas Winkler180ea052014-03-18 22:52:02 +0200360#endif /* CONFIG_PM_SLEEP */
Alexander Usyskin16833252014-02-18 14:31:08 +0200361
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100362#ifdef CONFIG_PM
Tomas Winkler180ea052014-03-18 22:52:02 +0200363static int mei_me_pm_runtime_idle(struct device *device)
364{
365 struct pci_dev *pdev = to_pci_dev(device);
366 struct mei_device *dev;
367
368 dev_dbg(&pdev->dev, "rpm: me: runtime_idle\n");
369
370 dev = pci_get_drvdata(pdev);
371 if (!dev)
372 return -ENODEV;
373 if (mei_write_is_idle(dev))
Alexander Usyskind5d83f82014-07-17 10:53:36 +0300374 pm_runtime_autosuspend(device);
Tomas Winkler180ea052014-03-18 22:52:02 +0200375
376 return -EBUSY;
377}
378
379static int mei_me_pm_runtime_suspend(struct device *device)
380{
381 struct pci_dev *pdev = to_pci_dev(device);
382 struct mei_device *dev;
383 int ret;
384
385 dev_dbg(&pdev->dev, "rpm: me: runtime suspend\n");
386
387 dev = pci_get_drvdata(pdev);
388 if (!dev)
389 return -ENODEV;
390
391 mutex_lock(&dev->device_lock);
392
393 if (mei_write_is_idle(dev))
Alexander Usyskin2d1995f2015-02-10 10:39:34 +0200394 ret = mei_me_pg_enter_sync(dev);
Tomas Winkler180ea052014-03-18 22:52:02 +0200395 else
396 ret = -EAGAIN;
397
398 mutex_unlock(&dev->device_lock);
399
400 dev_dbg(&pdev->dev, "rpm: me: runtime suspend ret=%d\n", ret);
401
Alexander Usyskin77537ad2016-06-16 17:58:52 +0300402 if (ret && ret != -EAGAIN)
403 schedule_work(&dev->reset_work);
404
Tomas Winkler180ea052014-03-18 22:52:02 +0200405 return ret;
406}
407
408static int mei_me_pm_runtime_resume(struct device *device)
409{
410 struct pci_dev *pdev = to_pci_dev(device);
411 struct mei_device *dev;
412 int ret;
413
414 dev_dbg(&pdev->dev, "rpm: me: runtime resume\n");
415
416 dev = pci_get_drvdata(pdev);
417 if (!dev)
418 return -ENODEV;
419
420 mutex_lock(&dev->device_lock);
421
Alexander Usyskin2d1995f2015-02-10 10:39:34 +0200422 ret = mei_me_pg_exit_sync(dev);
Tomas Winkler180ea052014-03-18 22:52:02 +0200423
424 mutex_unlock(&dev->device_lock);
425
426 dev_dbg(&pdev->dev, "rpm: me: runtime resume ret = %d\n", ret);
427
Alexander Usyskin77537ad2016-06-16 17:58:52 +0300428 if (ret)
429 schedule_work(&dev->reset_work);
430
Tomas Winkler180ea052014-03-18 22:52:02 +0200431 return ret;
432}
Tomas Winklere13fa902014-03-18 22:52:05 +0200433
434/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200435 * mei_me_set_pm_domain - fill and set pm domain structure for device
Tomas Winklere13fa902014-03-18 22:52:05 +0200436 *
437 * @dev: mei_device
438 */
439static inline void mei_me_set_pm_domain(struct mei_device *dev)
440{
Tomas Winklerd08b8fc2014-09-29 16:31:44 +0300441 struct pci_dev *pdev = to_pci_dev(dev->dev);
Tomas Winklere13fa902014-03-18 22:52:05 +0200442
443 if (pdev->dev.bus && pdev->dev.bus->pm) {
444 dev->pg_domain.ops = *pdev->dev.bus->pm;
445
446 dev->pg_domain.ops.runtime_suspend = mei_me_pm_runtime_suspend;
447 dev->pg_domain.ops.runtime_resume = mei_me_pm_runtime_resume;
448 dev->pg_domain.ops.runtime_idle = mei_me_pm_runtime_idle;
449
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100450 dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
Tomas Winklere13fa902014-03-18 22:52:05 +0200451 }
452}
453
454/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200455 * mei_me_unset_pm_domain - clean pm domain structure for device
Tomas Winklere13fa902014-03-18 22:52:05 +0200456 *
457 * @dev: mei_device
458 */
459static inline void mei_me_unset_pm_domain(struct mei_device *dev)
460{
461 /* stop using pm callbacks if any */
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100462 dev_pm_domain_set(dev->dev, NULL);
Tomas Winklere13fa902014-03-18 22:52:05 +0200463}
Tomas Winkler180ea052014-03-18 22:52:02 +0200464
Tomas Winkler180ea052014-03-18 22:52:02 +0200465static const struct dev_pm_ops mei_me_pm_ops = {
466 SET_SYSTEM_SLEEP_PM_OPS(mei_me_pci_suspend,
467 mei_me_pci_resume)
468 SET_RUNTIME_PM_OPS(
469 mei_me_pm_runtime_suspend,
470 mei_me_pm_runtime_resume,
471 mei_me_pm_runtime_idle)
472};
473
Tomas Winklerb68301e2013-03-27 16:58:29 +0200474#define MEI_ME_PM_OPS (&mei_me_pm_ops)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200475#else
Tomas Winklerb68301e2013-03-27 16:58:29 +0200476#define MEI_ME_PM_OPS NULL
Tomas Winkler180ea052014-03-18 22:52:02 +0200477#endif /* CONFIG_PM */
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200478/*
479 * PCI driver structure
480 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200481static struct pci_driver mei_me_driver = {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200482 .name = KBUILD_MODNAME,
Tomas Winklerb68301e2013-03-27 16:58:29 +0200483 .id_table = mei_me_pci_tbl,
484 .probe = mei_me_probe,
485 .remove = mei_me_remove,
Tomas Winkler5c4c0102017-03-20 15:04:02 +0200486 .shutdown = mei_me_shutdown,
Tomas Winklerb68301e2013-03-27 16:58:29 +0200487 .driver.pm = MEI_ME_PM_OPS,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200488};
489
Tomas Winklerb68301e2013-03-27 16:58:29 +0200490module_pci_driver(mei_me_driver);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200491
492MODULE_AUTHOR("Intel Corporation");
493MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
494MODULE_LICENSE("GPL v2");