Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 1 | /* |
| 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 Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 16 | #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 Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 24 | #include <linux/pci.h> |
| 25 | #include <linux/poll.h> |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 26 | #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 Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 33 | |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 34 | #include <linux/pm_domain.h> |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
| 36 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 37 | #include <linux/mei.h> |
| 38 | |
| 39 | #include "mei_dev.h" |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 40 | #include "client.h" |
Tomas Winkler | 6e4cd27 | 2014-03-11 14:49:23 +0200 | [diff] [blame] | 41 | #include "hw-me-regs.h" |
| 42 | #include "hw-me.h" |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 43 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 44 | /* mei_pci_tbl - PCI Device ID Table */ |
Tomas Winkler | a05f8f8 | 2014-03-16 14:35:58 +0200 | [diff] [blame] | 45 | static const struct pci_device_id mei_me_pci_tbl[] = { |
Alexander Usyskin | 8d929d4 | 2014-05-13 01:30:53 +0300 | [diff] [blame] | 46 | {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 Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 74 | {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 Usyskin | 8d929d4 | 2014-05-13 01:30:53 +0300 | [diff] [blame] | 76 | {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 Usyskin | edca5ea | 2014-11-19 17:01:38 +0200 | [diff] [blame] | 79 | {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 Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 85 | |
Tomas Winkler | 1625c7e | 2015-08-02 22:20:57 +0300 | [diff] [blame] | 86 | {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 Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 91 | /* required last entry */ |
| 92 | {0, } |
| 93 | }; |
| 94 | |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 95 | MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 96 | |
Rafael J. Wysocki | bbd6d05 | 2014-12-04 22:43:07 +0100 | [diff] [blame] | 97 | #ifdef CONFIG_PM |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 98 | static inline void mei_me_set_pm_domain(struct mei_device *dev); |
| 99 | static inline void mei_me_unset_pm_domain(struct mei_device *dev); |
| 100 | #else |
| 101 | static inline void mei_me_set_pm_domain(struct mei_device *dev) {} |
| 102 | static inline void mei_me_unset_pm_domain(struct mei_device *dev) {} |
Rafael J. Wysocki | bbd6d05 | 2014-12-04 22:43:07 +0100 | [diff] [blame] | 103 | #endif /* CONFIG_PM */ |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 104 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 105 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 106 | * mei_me_quirk_probe - probe for devices that doesn't valid ME interface |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 107 | * |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 108 | * @pdev: PCI device structure |
Tomas Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 109 | * @cfg: per generation config |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 110 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 111 | * Return: true if ME Interface is valid, false otherwise |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 112 | */ |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 113 | static bool mei_me_quirk_probe(struct pci_dev *pdev, |
Tomas Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 114 | const struct mei_cfg *cfg) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 115 | { |
Tomas Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 116 | if (cfg->quirk_probe && cfg->quirk_probe(pdev)) { |
| 117 | dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); |
| 118 | return false; |
Tomas Winkler | 5e6533a | 2014-03-25 21:25:18 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 121 | return true; |
| 122 | } |
Tomas Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 123 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 124 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 125 | * mei_me_probe - Device Initialization Routine |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 126 | * |
| 127 | * @pdev: PCI device structure |
| 128 | * @ent: entry in kcs_pci_tbl |
| 129 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 130 | * Return: 0 on success, <0 on failure. |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 131 | */ |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 132 | static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 133 | { |
Alexander Usyskin | 8d929d4 | 2014-05-13 01:30:53 +0300 | [diff] [blame] | 134 | const struct mei_cfg *cfg = (struct mei_cfg *)(ent->driver_data); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 135 | struct mei_device *dev; |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 136 | struct mei_me_hw *hw; |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 137 | unsigned int irqflags; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 138 | int err; |
| 139 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 140 | |
Tomas Winkler | c919951 | 2014-05-13 01:30:54 +0300 | [diff] [blame] | 141 | if (!mei_me_quirk_probe(pdev, cfg)) |
| 142 | return -ENODEV; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 143 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 144 | /* enable pci dev */ |
| 145 | err = pci_enable_device(pdev); |
| 146 | if (err) { |
| 147 | dev_err(&pdev->dev, "failed to enable pci device.\n"); |
| 148 | goto end; |
| 149 | } |
| 150 | /* set PCI host mastering */ |
| 151 | pci_set_master(pdev); |
| 152 | /* pci request regions for mei driver */ |
| 153 | err = pci_request_regions(pdev, KBUILD_MODNAME); |
| 154 | if (err) { |
| 155 | dev_err(&pdev->dev, "failed to get pci regions.\n"); |
| 156 | goto disable_device; |
| 157 | } |
Tomas Winkler | 3ecfb16 | 2013-12-17 15:56:57 +0200 | [diff] [blame] | 158 | |
| 159 | if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) || |
| 160 | dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) { |
| 161 | |
| 162 | err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 163 | if (err) |
| 164 | err = dma_set_coherent_mask(&pdev->dev, |
| 165 | DMA_BIT_MASK(32)); |
| 166 | } |
| 167 | if (err) { |
| 168 | dev_err(&pdev->dev, "No usable DMA configuration, aborting\n"); |
| 169 | goto release_regions; |
| 170 | } |
| 171 | |
| 172 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 173 | /* allocates and initializes the mei dev structure */ |
Alexander Usyskin | 8d929d4 | 2014-05-13 01:30:53 +0300 | [diff] [blame] | 174 | dev = mei_me_dev_init(pdev, cfg); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 175 | if (!dev) { |
| 176 | err = -ENOMEM; |
| 177 | goto release_regions; |
| 178 | } |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 179 | hw = to_me_hw(dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 180 | /* mapping IO device memory */ |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 181 | hw->mem_addr = pci_iomap(pdev, 0, 0); |
| 182 | if (!hw->mem_addr) { |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 183 | dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); |
| 184 | err = -ENOMEM; |
| 185 | goto free_device; |
| 186 | } |
| 187 | pci_enable_msi(pdev); |
| 188 | |
| 189 | /* request and enable interrupt */ |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 190 | irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED; |
| 191 | |
| 192 | err = request_threaded_irq(pdev->irq, |
Tomas Winkler | 06ecd64 | 2013-02-06 14:06:42 +0200 | [diff] [blame] | 193 | mei_me_irq_quick_handler, |
| 194 | mei_me_irq_thread_handler, |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 195 | irqflags, KBUILD_MODNAME, dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 196 | if (err) { |
| 197 | dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", |
| 198 | pdev->irq); |
| 199 | goto disable_msi; |
| 200 | } |
| 201 | |
Tomas Winkler | c4d589b | 2013-03-27 16:58:28 +0200 | [diff] [blame] | 202 | if (mei_start(dev)) { |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 203 | dev_err(&pdev->dev, "init hw failure.\n"); |
| 204 | err = -ENODEV; |
| 205 | goto release_irq; |
| 206 | } |
| 207 | |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 208 | pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_ME_RPM_TIMEOUT); |
| 209 | pm_runtime_use_autosuspend(&pdev->dev); |
| 210 | |
Alexander Usyskin | f3d8e87 | 2014-06-23 15:10:35 +0300 | [diff] [blame] | 211 | err = mei_register(dev, &pdev->dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 212 | if (err) |
| 213 | goto release_irq; |
| 214 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 215 | pci_set_drvdata(pdev, dev); |
| 216 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 217 | schedule_delayed_work(&dev->timer_work, HZ); |
| 218 | |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 219 | /* |
| 220 | * For not wake-able HW runtime pm framework |
| 221 | * can't be used on pci device level. |
| 222 | * Use domain runtime pm callbacks instead. |
| 223 | */ |
| 224 | if (!pci_dev_run_wake(pdev)) |
| 225 | mei_me_set_pm_domain(dev); |
| 226 | |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 227 | if (mei_pg_is_enabled(dev)) |
| 228 | pm_runtime_put_noidle(&pdev->dev); |
| 229 | |
Alexander Usyskin | c4e87b5 | 2013-10-21 22:05:42 +0300 | [diff] [blame] | 230 | dev_dbg(&pdev->dev, "initialization successful.\n"); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 231 | |
| 232 | return 0; |
| 233 | |
| 234 | release_irq: |
Tomas Winkler | dc844b0 | 2013-11-11 13:26:06 +0200 | [diff] [blame] | 235 | mei_cancel_work(dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 236 | mei_disable_interrupts(dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 237 | free_irq(pdev->irq, dev); |
| 238 | disable_msi: |
| 239 | pci_disable_msi(pdev); |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 240 | pci_iounmap(pdev, hw->mem_addr); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 241 | free_device: |
| 242 | kfree(dev); |
| 243 | release_regions: |
| 244 | pci_release_regions(pdev); |
| 245 | disable_device: |
| 246 | pci_disable_device(pdev); |
| 247 | end: |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 248 | dev_err(&pdev->dev, "initialization failed.\n"); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 253 | * mei_me_remove - Device Removal Routine |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 254 | * |
| 255 | * @pdev: PCI device structure |
| 256 | * |
| 257 | * mei_remove is called by the PCI subsystem to alert the driver |
| 258 | * that it should release a PCI device. |
| 259 | */ |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 260 | static void mei_me_remove(struct pci_dev *pdev) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 261 | { |
| 262 | struct mei_device *dev; |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 263 | struct mei_me_hw *hw; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 264 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 265 | dev = pci_get_drvdata(pdev); |
| 266 | if (!dev) |
| 267 | return; |
| 268 | |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 269 | if (mei_pg_is_enabled(dev)) |
| 270 | pm_runtime_get_noresume(&pdev->dev); |
| 271 | |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 272 | hw = to_me_hw(dev); |
| 273 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 274 | |
Paul Bolle | ed6f7ac | 2013-10-17 13:52:10 +0300 | [diff] [blame] | 275 | dev_dbg(&pdev->dev, "stop\n"); |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 276 | mei_stop(dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 277 | |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 278 | if (!pci_dev_run_wake(pdev)) |
| 279 | mei_me_unset_pm_domain(dev); |
| 280 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 281 | /* disable interrupts */ |
| 282 | mei_disable_interrupts(dev); |
| 283 | |
| 284 | free_irq(pdev->irq, dev); |
| 285 | pci_disable_msi(pdev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 286 | |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 287 | if (hw->mem_addr) |
| 288 | pci_iounmap(pdev, hw->mem_addr); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 289 | |
Tomas Winkler | 30e53bb | 2013-04-05 22:10:34 +0300 | [diff] [blame] | 290 | mei_deregister(dev); |
| 291 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 292 | kfree(dev); |
| 293 | |
| 294 | pci_release_regions(pdev); |
| 295 | pci_disable_device(pdev); |
| 296 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 297 | |
| 298 | } |
Alexander Usyskin | 1683325 | 2014-02-18 14:31:08 +0200 | [diff] [blame] | 299 | #ifdef CONFIG_PM_SLEEP |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 300 | static int mei_me_pci_suspend(struct device *device) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 301 | { |
| 302 | struct pci_dev *pdev = to_pci_dev(device); |
| 303 | struct mei_device *dev = pci_get_drvdata(pdev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 304 | |
| 305 | if (!dev) |
| 306 | return -ENODEV; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 307 | |
Paul Bolle | ed6f7ac | 2013-10-17 13:52:10 +0300 | [diff] [blame] | 308 | dev_dbg(&pdev->dev, "suspend\n"); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 309 | |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 310 | mei_stop(dev); |
| 311 | |
| 312 | mei_disable_interrupts(dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 313 | |
| 314 | free_irq(pdev->irq, dev); |
| 315 | pci_disable_msi(pdev); |
| 316 | |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 317 | return 0; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 320 | static int mei_me_pci_resume(struct device *device) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 321 | { |
| 322 | struct pci_dev *pdev = to_pci_dev(device); |
| 323 | struct mei_device *dev; |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 324 | unsigned int irqflags; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 325 | int err; |
| 326 | |
| 327 | dev = pci_get_drvdata(pdev); |
| 328 | if (!dev) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | pci_enable_msi(pdev); |
| 332 | |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 333 | irqflags = pci_dev_msi_enabled(pdev) ? IRQF_ONESHOT : IRQF_SHARED; |
| 334 | |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 335 | /* request and enable interrupt */ |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 336 | err = request_threaded_irq(pdev->irq, |
Tomas Winkler | 06ecd64 | 2013-02-06 14:06:42 +0200 | [diff] [blame] | 337 | mei_me_irq_quick_handler, |
| 338 | mei_me_irq_thread_handler, |
Alexander Usyskin | 1fa55b4 | 2015-08-02 22:20:52 +0300 | [diff] [blame] | 339 | irqflags, KBUILD_MODNAME, dev); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 340 | |
| 341 | if (err) { |
| 342 | dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", |
| 343 | pdev->irq); |
| 344 | return err; |
| 345 | } |
| 346 | |
Tomas Winkler | 33ec082 | 2014-01-12 00:36:09 +0200 | [diff] [blame] | 347 | err = mei_restart(dev); |
| 348 | if (err) |
| 349 | return err; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 350 | |
| 351 | /* Start timer if stopped in suspend */ |
| 352 | schedule_delayed_work(&dev->timer_work, HZ); |
| 353 | |
Tomas Winkler | 33ec082 | 2014-01-12 00:36:09 +0200 | [diff] [blame] | 354 | return 0; |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 355 | } |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 356 | #endif /* CONFIG_PM_SLEEP */ |
Alexander Usyskin | 1683325 | 2014-02-18 14:31:08 +0200 | [diff] [blame] | 357 | |
Rafael J. Wysocki | bbd6d05 | 2014-12-04 22:43:07 +0100 | [diff] [blame] | 358 | #ifdef CONFIG_PM |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 359 | static int mei_me_pm_runtime_idle(struct device *device) |
| 360 | { |
| 361 | struct pci_dev *pdev = to_pci_dev(device); |
| 362 | struct mei_device *dev; |
| 363 | |
| 364 | dev_dbg(&pdev->dev, "rpm: me: runtime_idle\n"); |
| 365 | |
| 366 | dev = pci_get_drvdata(pdev); |
| 367 | if (!dev) |
| 368 | return -ENODEV; |
| 369 | if (mei_write_is_idle(dev)) |
Alexander Usyskin | d5d83f8 | 2014-07-17 10:53:36 +0300 | [diff] [blame] | 370 | pm_runtime_autosuspend(device); |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 371 | |
| 372 | return -EBUSY; |
| 373 | } |
| 374 | |
| 375 | static int mei_me_pm_runtime_suspend(struct device *device) |
| 376 | { |
| 377 | struct pci_dev *pdev = to_pci_dev(device); |
| 378 | struct mei_device *dev; |
| 379 | int ret; |
| 380 | |
| 381 | dev_dbg(&pdev->dev, "rpm: me: runtime suspend\n"); |
| 382 | |
| 383 | dev = pci_get_drvdata(pdev); |
| 384 | if (!dev) |
| 385 | return -ENODEV; |
| 386 | |
| 387 | mutex_lock(&dev->device_lock); |
| 388 | |
| 389 | if (mei_write_is_idle(dev)) |
Alexander Usyskin | 2d1995f | 2015-02-10 10:39:34 +0200 | [diff] [blame] | 390 | ret = mei_me_pg_enter_sync(dev); |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 391 | else |
| 392 | ret = -EAGAIN; |
| 393 | |
| 394 | mutex_unlock(&dev->device_lock); |
| 395 | |
| 396 | dev_dbg(&pdev->dev, "rpm: me: runtime suspend ret=%d\n", ret); |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static int mei_me_pm_runtime_resume(struct device *device) |
| 402 | { |
| 403 | struct pci_dev *pdev = to_pci_dev(device); |
| 404 | struct mei_device *dev; |
| 405 | int ret; |
| 406 | |
| 407 | dev_dbg(&pdev->dev, "rpm: me: runtime resume\n"); |
| 408 | |
| 409 | dev = pci_get_drvdata(pdev); |
| 410 | if (!dev) |
| 411 | return -ENODEV; |
| 412 | |
| 413 | mutex_lock(&dev->device_lock); |
| 414 | |
Alexander Usyskin | 2d1995f | 2015-02-10 10:39:34 +0200 | [diff] [blame] | 415 | ret = mei_me_pg_exit_sync(dev); |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 416 | |
| 417 | mutex_unlock(&dev->device_lock); |
| 418 | |
| 419 | dev_dbg(&pdev->dev, "rpm: me: runtime resume ret = %d\n", ret); |
| 420 | |
| 421 | return ret; |
| 422 | } |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 423 | |
| 424 | /** |
Geert Uytterhoeven | 7efceb5 | 2014-09-16 22:00:47 +0200 | [diff] [blame] | 425 | * mei_me_set_pm_domain - fill and set pm domain structure for device |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 426 | * |
| 427 | * @dev: mei_device |
| 428 | */ |
| 429 | static inline void mei_me_set_pm_domain(struct mei_device *dev) |
| 430 | { |
Tomas Winkler | d08b8fc | 2014-09-29 16:31:44 +0300 | [diff] [blame] | 431 | struct pci_dev *pdev = to_pci_dev(dev->dev); |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 432 | |
| 433 | if (pdev->dev.bus && pdev->dev.bus->pm) { |
| 434 | dev->pg_domain.ops = *pdev->dev.bus->pm; |
| 435 | |
| 436 | dev->pg_domain.ops.runtime_suspend = mei_me_pm_runtime_suspend; |
| 437 | dev->pg_domain.ops.runtime_resume = mei_me_pm_runtime_resume; |
| 438 | dev->pg_domain.ops.runtime_idle = mei_me_pm_runtime_idle; |
| 439 | |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 440 | dev_pm_domain_set(&pdev->dev, &dev->pg_domain); |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
Geert Uytterhoeven | 7efceb5 | 2014-09-16 22:00:47 +0200 | [diff] [blame] | 445 | * mei_me_unset_pm_domain - clean pm domain structure for device |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 446 | * |
| 447 | * @dev: mei_device |
| 448 | */ |
| 449 | static inline void mei_me_unset_pm_domain(struct mei_device *dev) |
| 450 | { |
| 451 | /* stop using pm callbacks if any */ |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 452 | dev_pm_domain_set(dev->dev, NULL); |
Tomas Winkler | e13fa90 | 2014-03-18 22:52:05 +0200 | [diff] [blame] | 453 | } |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 454 | |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 455 | static const struct dev_pm_ops mei_me_pm_ops = { |
| 456 | SET_SYSTEM_SLEEP_PM_OPS(mei_me_pci_suspend, |
| 457 | mei_me_pci_resume) |
| 458 | SET_RUNTIME_PM_OPS( |
| 459 | mei_me_pm_runtime_suspend, |
| 460 | mei_me_pm_runtime_resume, |
| 461 | mei_me_pm_runtime_idle) |
| 462 | }; |
| 463 | |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 464 | #define MEI_ME_PM_OPS (&mei_me_pm_ops) |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 465 | #else |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 466 | #define MEI_ME_PM_OPS NULL |
Tomas Winkler | 180ea05 | 2014-03-18 22:52:02 +0200 | [diff] [blame] | 467 | #endif /* CONFIG_PM */ |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 468 | /* |
| 469 | * PCI driver structure |
| 470 | */ |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 471 | static struct pci_driver mei_me_driver = { |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 472 | .name = KBUILD_MODNAME, |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 473 | .id_table = mei_me_pci_tbl, |
| 474 | .probe = mei_me_probe, |
| 475 | .remove = mei_me_remove, |
| 476 | .shutdown = mei_me_remove, |
| 477 | .driver.pm = MEI_ME_PM_OPS, |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 478 | }; |
| 479 | |
Tomas Winkler | b68301e | 2013-03-27 16:58:29 +0200 | [diff] [blame] | 480 | module_pci_driver(mei_me_driver); |
Tomas Winkler | 2703d4b | 2013-02-06 14:06:39 +0200 | [diff] [blame] | 481 | |
| 482 | MODULE_AUTHOR("Intel Corporation"); |
| 483 | MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); |
| 484 | MODULE_LICENSE("GPL v2"); |