blob: 718addfa9fb0b865efbc20ff57083b58fa5dc2c5 [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)},
88 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT_H, mei_me_pch8_cfg)},
89 {MEI_PCI_DEVICE(MEI_DEV_ID_SPT_H_2, mei_me_pch8_cfg)},
90
Tomas Winklerdd16f6c2016-02-29 22:03:23 +020091 {MEI_PCI_DEVICE(MEI_DEV_ID_BXT_M, mei_me_pch8_cfg)},
92 {MEI_PCI_DEVICE(MEI_DEV_ID_APL_I, mei_me_pch8_cfg)},
93
Tomas Winkler2703d4b2013-02-06 14:06:39 +020094 /* required last entry */
95 {0, }
96};
97
Tomas Winklerb68301e2013-03-27 16:58:29 +020098MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
Tomas Winkler2703d4b2013-02-06 14:06:39 +020099
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100100#ifdef CONFIG_PM
Tomas Winklere13fa902014-03-18 22:52:05 +0200101static inline void mei_me_set_pm_domain(struct mei_device *dev);
102static inline void mei_me_unset_pm_domain(struct mei_device *dev);
103#else
104static inline void mei_me_set_pm_domain(struct mei_device *dev) {}
105static inline void mei_me_unset_pm_domain(struct mei_device *dev) {}
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100106#endif /* CONFIG_PM */
Tomas Winklere13fa902014-03-18 22:52:05 +0200107
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200108/**
Alexander Usyskince231392014-09-29 16:31:50 +0300109 * mei_me_quirk_probe - probe for devices that doesn't valid ME interface
Masanari Iida393b1482013-04-05 01:05:05 +0900110 *
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200111 * @pdev: PCI device structure
Tomas Winklerc9199512014-05-13 01:30:54 +0300112 * @cfg: per generation config
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200113 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300114 * Return: true if ME Interface is valid, false otherwise
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200115 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200116static bool mei_me_quirk_probe(struct pci_dev *pdev,
Tomas Winklerc9199512014-05-13 01:30:54 +0300117 const struct mei_cfg *cfg)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200118{
Tomas Winklerc9199512014-05-13 01:30:54 +0300119 if (cfg->quirk_probe && cfg->quirk_probe(pdev)) {
120 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
121 return false;
Tomas Winkler5e6533a2014-03-25 21:25:18 +0200122 }
123
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200124 return true;
125}
Tomas Winklerc9199512014-05-13 01:30:54 +0300126
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200127/**
Alexander Usyskince231392014-09-29 16:31:50 +0300128 * mei_me_probe - Device Initialization Routine
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200129 *
130 * @pdev: PCI device structure
131 * @ent: entry in kcs_pci_tbl
132 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300133 * Return: 0 on success, <0 on failure.
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200134 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200135static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200136{
Alexander Usyskin8d929d42014-05-13 01:30:53 +0300137 const struct mei_cfg *cfg = (struct mei_cfg *)(ent->driver_data);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200138 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200139 struct mei_me_hw *hw;
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300140 unsigned int irqflags;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200141 int err;
142
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200143
Tomas Winklerc9199512014-05-13 01:30:54 +0300144 if (!mei_me_quirk_probe(pdev, cfg))
145 return -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200146
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200147 /* enable pci dev */
148 err = pci_enable_device(pdev);
149 if (err) {
150 dev_err(&pdev->dev, "failed to enable pci device.\n");
151 goto end;
152 }
153 /* set PCI host mastering */
154 pci_set_master(pdev);
155 /* pci request regions for mei driver */
156 err = pci_request_regions(pdev, KBUILD_MODNAME);
157 if (err) {
158 dev_err(&pdev->dev, "failed to get pci regions.\n");
159 goto disable_device;
160 }
Tomas Winkler3ecfb162013-12-17 15:56:57 +0200161
162 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) ||
163 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
164
165 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
166 if (err)
167 err = dma_set_coherent_mask(&pdev->dev,
168 DMA_BIT_MASK(32));
169 }
170 if (err) {
171 dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
172 goto release_regions;
173 }
174
175
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200176 /* allocates and initializes the mei dev structure */
Alexander Usyskin8d929d42014-05-13 01:30:53 +0300177 dev = mei_me_dev_init(pdev, cfg);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200178 if (!dev) {
179 err = -ENOMEM;
180 goto release_regions;
181 }
Tomas Winkler52c34562013-02-06 14:06:40 +0200182 hw = to_me_hw(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200183 /* mapping IO device memory */
Tomas Winkler52c34562013-02-06 14:06:40 +0200184 hw->mem_addr = pci_iomap(pdev, 0, 0);
185 if (!hw->mem_addr) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200186 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
187 err = -ENOMEM;
188 goto free_device;
189 }
190 pci_enable_msi(pdev);
191
192 /* request and enable interrupt */
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300193 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
194
195 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200196 mei_me_irq_quick_handler,
197 mei_me_irq_thread_handler,
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300198 irqflags, KBUILD_MODNAME, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200199 if (err) {
200 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
201 pdev->irq);
202 goto disable_msi;
203 }
204
Tomas Winklerc4d589b2013-03-27 16:58:28 +0200205 if (mei_start(dev)) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200206 dev_err(&pdev->dev, "init hw failure.\n");
207 err = -ENODEV;
208 goto release_irq;
209 }
210
Tomas Winkler180ea052014-03-18 22:52:02 +0200211 pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_ME_RPM_TIMEOUT);
212 pm_runtime_use_autosuspend(&pdev->dev);
213
Alexander Usyskinf3d8e872014-06-23 15:10:35 +0300214 err = mei_register(dev, &pdev->dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200215 if (err)
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200216 goto stop;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200217
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200218 pci_set_drvdata(pdev, dev);
219
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200220 schedule_delayed_work(&dev->timer_work, HZ);
221
Tomas Winklere13fa902014-03-18 22:52:05 +0200222 /*
223 * For not wake-able HW runtime pm framework
224 * can't be used on pci device level.
225 * Use domain runtime pm callbacks instead.
226 */
227 if (!pci_dev_run_wake(pdev))
228 mei_me_set_pm_domain(dev);
229
Tomas Winkler180ea052014-03-18 22:52:02 +0200230 if (mei_pg_is_enabled(dev))
231 pm_runtime_put_noidle(&pdev->dev);
232
Alexander Usyskinc4e87b52013-10-21 22:05:42 +0300233 dev_dbg(&pdev->dev, "initialization successful.\n");
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200234
235 return 0;
236
Alexander Usyskin1f7e4892016-02-07 23:35:20 +0200237stop:
238 mei_stop(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200239release_irq:
Tomas Winklerdc844b02013-11-11 13:26:06 +0200240 mei_cancel_work(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200241 mei_disable_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200242 free_irq(pdev->irq, dev);
243disable_msi:
244 pci_disable_msi(pdev);
Tomas Winkler52c34562013-02-06 14:06:40 +0200245 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200246free_device:
247 kfree(dev);
248release_regions:
249 pci_release_regions(pdev);
250disable_device:
251 pci_disable_device(pdev);
252end:
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200253 dev_err(&pdev->dev, "initialization failed.\n");
254 return err;
255}
256
257/**
Alexander Usyskince231392014-09-29 16:31:50 +0300258 * mei_me_remove - Device Removal Routine
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200259 *
260 * @pdev: PCI device structure
261 *
262 * mei_remove is called by the PCI subsystem to alert the driver
263 * that it should release a PCI device.
264 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200265static void mei_me_remove(struct pci_dev *pdev)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200266{
267 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200268 struct mei_me_hw *hw;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200269
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200270 dev = pci_get_drvdata(pdev);
271 if (!dev)
272 return;
273
Tomas Winkler180ea052014-03-18 22:52:02 +0200274 if (mei_pg_is_enabled(dev))
275 pm_runtime_get_noresume(&pdev->dev);
276
Tomas Winkler52c34562013-02-06 14:06:40 +0200277 hw = to_me_hw(dev);
278
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200279
Paul Bolleed6f7ac2013-10-17 13:52:10 +0300280 dev_dbg(&pdev->dev, "stop\n");
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200281 mei_stop(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200282
Tomas Winklere13fa902014-03-18 22:52:05 +0200283 if (!pci_dev_run_wake(pdev))
284 mei_me_unset_pm_domain(dev);
285
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200286 /* disable interrupts */
287 mei_disable_interrupts(dev);
288
289 free_irq(pdev->irq, dev);
290 pci_disable_msi(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200291
Tomas Winkler52c34562013-02-06 14:06:40 +0200292 if (hw->mem_addr)
293 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200294
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300295 mei_deregister(dev);
296
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200297 kfree(dev);
298
299 pci_release_regions(pdev);
300 pci_disable_device(pdev);
301
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200302
303}
Alexander Usyskin16833252014-02-18 14:31:08 +0200304#ifdef CONFIG_PM_SLEEP
Tomas Winklerb68301e2013-03-27 16:58:29 +0200305static int mei_me_pci_suspend(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200306{
307 struct pci_dev *pdev = to_pci_dev(device);
308 struct mei_device *dev = pci_get_drvdata(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200309
310 if (!dev)
311 return -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200312
Paul Bolleed6f7ac2013-10-17 13:52:10 +0300313 dev_dbg(&pdev->dev, "suspend\n");
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200314
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200315 mei_stop(dev);
316
317 mei_disable_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200318
319 free_irq(pdev->irq, dev);
320 pci_disable_msi(pdev);
321
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200322 return 0;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200323}
324
Tomas Winklerb68301e2013-03-27 16:58:29 +0200325static int mei_me_pci_resume(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200326{
327 struct pci_dev *pdev = to_pci_dev(device);
328 struct mei_device *dev;
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300329 unsigned int irqflags;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200330 int err;
331
332 dev = pci_get_drvdata(pdev);
333 if (!dev)
334 return -ENODEV;
335
336 pci_enable_msi(pdev);
337
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300338 irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED;
339
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200340 /* request and enable interrupt */
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300341 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200342 mei_me_irq_quick_handler,
343 mei_me_irq_thread_handler,
Alexander Usyskin1fa55b42015-08-02 22:20:52 +0300344 irqflags, KBUILD_MODNAME, dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200345
346 if (err) {
347 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
348 pdev->irq);
349 return err;
350 }
351
Tomas Winkler33ec0822014-01-12 00:36:09 +0200352 err = mei_restart(dev);
353 if (err)
354 return err;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200355
356 /* Start timer if stopped in suspend */
357 schedule_delayed_work(&dev->timer_work, HZ);
358
Tomas Winkler33ec0822014-01-12 00:36:09 +0200359 return 0;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200360}
Tomas Winkler180ea052014-03-18 22:52:02 +0200361#endif /* CONFIG_PM_SLEEP */
Alexander Usyskin16833252014-02-18 14:31:08 +0200362
Rafael J. Wysockibbd6d052014-12-04 22:43:07 +0100363#ifdef CONFIG_PM
Tomas Winkler180ea052014-03-18 22:52:02 +0200364static int mei_me_pm_runtime_idle(struct device *device)
365{
366 struct pci_dev *pdev = to_pci_dev(device);
367 struct mei_device *dev;
368
369 dev_dbg(&pdev->dev, "rpm: me: runtime_idle\n");
370
371 dev = pci_get_drvdata(pdev);
372 if (!dev)
373 return -ENODEV;
374 if (mei_write_is_idle(dev))
Alexander Usyskind5d83f82014-07-17 10:53:36 +0300375 pm_runtime_autosuspend(device);
Tomas Winkler180ea052014-03-18 22:52:02 +0200376
377 return -EBUSY;
378}
379
380static int mei_me_pm_runtime_suspend(struct device *device)
381{
382 struct pci_dev *pdev = to_pci_dev(device);
383 struct mei_device *dev;
384 int ret;
385
386 dev_dbg(&pdev->dev, "rpm: me: runtime suspend\n");
387
388 dev = pci_get_drvdata(pdev);
389 if (!dev)
390 return -ENODEV;
391
392 mutex_lock(&dev->device_lock);
393
394 if (mei_write_is_idle(dev))
Alexander Usyskin2d1995f2015-02-10 10:39:34 +0200395 ret = mei_me_pg_enter_sync(dev);
Tomas Winkler180ea052014-03-18 22:52:02 +0200396 else
397 ret = -EAGAIN;
398
399 mutex_unlock(&dev->device_lock);
400
401 dev_dbg(&pdev->dev, "rpm: me: runtime suspend ret=%d\n", ret);
402
Alexander Usyskin77537ad2016-06-16 17:58:52 +0300403 if (ret && ret != -EAGAIN)
404 schedule_work(&dev->reset_work);
405
Tomas Winkler180ea052014-03-18 22:52:02 +0200406 return ret;
407}
408
409static int mei_me_pm_runtime_resume(struct device *device)
410{
411 struct pci_dev *pdev = to_pci_dev(device);
412 struct mei_device *dev;
413 int ret;
414
415 dev_dbg(&pdev->dev, "rpm: me: runtime resume\n");
416
417 dev = pci_get_drvdata(pdev);
418 if (!dev)
419 return -ENODEV;
420
421 mutex_lock(&dev->device_lock);
422
Alexander Usyskin2d1995f2015-02-10 10:39:34 +0200423 ret = mei_me_pg_exit_sync(dev);
Tomas Winkler180ea052014-03-18 22:52:02 +0200424
425 mutex_unlock(&dev->device_lock);
426
427 dev_dbg(&pdev->dev, "rpm: me: runtime resume ret = %d\n", ret);
428
Alexander Usyskin77537ad2016-06-16 17:58:52 +0300429 if (ret)
430 schedule_work(&dev->reset_work);
431
Tomas Winkler180ea052014-03-18 22:52:02 +0200432 return ret;
433}
Tomas Winklere13fa902014-03-18 22:52:05 +0200434
435/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200436 * mei_me_set_pm_domain - fill and set pm domain structure for device
Tomas Winklere13fa902014-03-18 22:52:05 +0200437 *
438 * @dev: mei_device
439 */
440static inline void mei_me_set_pm_domain(struct mei_device *dev)
441{
Tomas Winklerd08b8fc2014-09-29 16:31:44 +0300442 struct pci_dev *pdev = to_pci_dev(dev->dev);
Tomas Winklere13fa902014-03-18 22:52:05 +0200443
444 if (pdev->dev.bus && pdev->dev.bus->pm) {
445 dev->pg_domain.ops = *pdev->dev.bus->pm;
446
447 dev->pg_domain.ops.runtime_suspend = mei_me_pm_runtime_suspend;
448 dev->pg_domain.ops.runtime_resume = mei_me_pm_runtime_resume;
449 dev->pg_domain.ops.runtime_idle = mei_me_pm_runtime_idle;
450
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100451 dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
Tomas Winklere13fa902014-03-18 22:52:05 +0200452 }
453}
454
455/**
Geert Uytterhoeven7efceb52014-09-16 22:00:47 +0200456 * mei_me_unset_pm_domain - clean pm domain structure for device
Tomas Winklere13fa902014-03-18 22:52:05 +0200457 *
458 * @dev: mei_device
459 */
460static inline void mei_me_unset_pm_domain(struct mei_device *dev)
461{
462 /* stop using pm callbacks if any */
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100463 dev_pm_domain_set(dev->dev, NULL);
Tomas Winklere13fa902014-03-18 22:52:05 +0200464}
Tomas Winkler180ea052014-03-18 22:52:02 +0200465
Tomas Winkler180ea052014-03-18 22:52:02 +0200466static const struct dev_pm_ops mei_me_pm_ops = {
467 SET_SYSTEM_SLEEP_PM_OPS(mei_me_pci_suspend,
468 mei_me_pci_resume)
469 SET_RUNTIME_PM_OPS(
470 mei_me_pm_runtime_suspend,
471 mei_me_pm_runtime_resume,
472 mei_me_pm_runtime_idle)
473};
474
Tomas Winklerb68301e2013-03-27 16:58:29 +0200475#define MEI_ME_PM_OPS (&mei_me_pm_ops)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200476#else
Tomas Winklerb68301e2013-03-27 16:58:29 +0200477#define MEI_ME_PM_OPS NULL
Tomas Winkler180ea052014-03-18 22:52:02 +0200478#endif /* CONFIG_PM */
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200479/*
480 * PCI driver structure
481 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200482static struct pci_driver mei_me_driver = {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200483 .name = KBUILD_MODNAME,
Tomas Winklerb68301e2013-03-27 16:58:29 +0200484 .id_table = mei_me_pci_tbl,
485 .probe = mei_me_probe,
486 .remove = mei_me_remove,
487 .shutdown = mei_me_remove,
488 .driver.pm = MEI_ME_PM_OPS,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200489};
490
Tomas Winklerb68301e2013-03-27 16:58:29 +0200491module_pci_driver(mei_me_driver);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200492
493MODULE_AUTHOR("Intel Corporation");
494MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
495MODULE_LICENSE("GPL v2");