pci-me.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. */
  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>
  24. #include <linux/pci.h>
  25. #include <linux/poll.h>
  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>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/mei.h>
  35. #include "mei_dev.h"
  36. #include "client.h"
  37. #include "hw-me-regs.h"
  38. #include "hw-me.h"
  39. /* mei_pci_tbl - PCI Device ID Table */
  40. static const struct pci_device_id mei_me_pci_tbl[] = {
  41. {MEI_PCI_DEVICE(MEI_DEV_ID_82946GZ, mei_me_legacy_cfg)},
  42. {MEI_PCI_DEVICE(MEI_DEV_ID_82G35, mei_me_legacy_cfg)},
  43. {MEI_PCI_DEVICE(MEI_DEV_ID_82Q965, mei_me_legacy_cfg)},
  44. {MEI_PCI_DEVICE(MEI_DEV_ID_82G965, mei_me_legacy_cfg)},
  45. {MEI_PCI_DEVICE(MEI_DEV_ID_82GM965, mei_me_legacy_cfg)},
  46. {MEI_PCI_DEVICE(MEI_DEV_ID_82GME965, mei_me_legacy_cfg)},
  47. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82Q35, mei_me_legacy_cfg)},
  48. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82G33, mei_me_legacy_cfg)},
  49. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82Q33, mei_me_legacy_cfg)},
  50. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_82X38, mei_me_legacy_cfg)},
  51. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_3200, mei_me_legacy_cfg)},
  52. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_6, mei_me_legacy_cfg)},
  53. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_7, mei_me_legacy_cfg)},
  54. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_8, mei_me_legacy_cfg)},
  55. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_9, mei_me_legacy_cfg)},
  56. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9_10, mei_me_legacy_cfg)},
  57. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_1, mei_me_legacy_cfg)},
  58. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_2, mei_me_legacy_cfg)},
  59. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_3, mei_me_legacy_cfg)},
  60. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH9M_4, mei_me_legacy_cfg)},
  61. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_1, mei_me_ich_cfg)},
  62. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_2, mei_me_ich_cfg)},
  63. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_3, mei_me_ich_cfg)},
  64. {MEI_PCI_DEVICE(MEI_DEV_ID_ICH10_4, mei_me_ich_cfg)},
  65. {MEI_PCI_DEVICE(MEI_DEV_ID_IBXPK_1, mei_me_pch_cfg)},
  66. {MEI_PCI_DEVICE(MEI_DEV_ID_IBXPK_2, mei_me_pch_cfg)},
  67. {MEI_PCI_DEVICE(MEI_DEV_ID_CPT_1, mei_me_pch_cpt_pbg_cfg)},
  68. {MEI_PCI_DEVICE(MEI_DEV_ID_PBG_1, mei_me_pch_cpt_pbg_cfg)},
  69. {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_1, mei_me_pch_cfg)},
  70. {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_2, mei_me_pch_cfg)},
  71. {MEI_PCI_DEVICE(MEI_DEV_ID_PPT_3, mei_me_pch_cfg)},
  72. {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_H, mei_me_pch8_sps_cfg)},
  73. {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_W, mei_me_pch8_sps_cfg)},
  74. {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_LP, mei_me_pch8_cfg)},
  75. {MEI_PCI_DEVICE(MEI_DEV_ID_LPT_HR, mei_me_pch8_sps_cfg)},
  76. {MEI_PCI_DEVICE(MEI_DEV_ID_WPT_LP, mei_me_pch8_cfg)},
  77. {MEI_PCI_DEVICE(MEI_DEV_ID_WPT_LP_2, mei_me_pch8_cfg)},
  78. /* required last entry */
  79. {0, }
  80. };
  81. MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
  82. #ifdef CONFIG_PM
  83. static inline void mei_me_set_pm_domain(struct mei_device *dev);
  84. static inline void mei_me_unset_pm_domain(struct mei_device *dev);
  85. #else
  86. static inline void mei_me_set_pm_domain(struct mei_device *dev) {}
  87. static inline void mei_me_unset_pm_domain(struct mei_device *dev) {}
  88. #endif /* CONFIG_PM */
  89. /**
  90. * mei_me_quirk_probe - probe for devices that doesn't valid ME interface
  91. *
  92. * @pdev: PCI device structure
  93. * @cfg: per generation config
  94. *
  95. * Return: true if ME Interface is valid, false otherwise
  96. */
  97. static bool mei_me_quirk_probe(struct pci_dev *pdev,
  98. const struct mei_cfg *cfg)
  99. {
  100. if (cfg->quirk_probe && cfg->quirk_probe(pdev)) {
  101. dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
  102. return false;
  103. }
  104. return true;
  105. }
  106. /**
  107. * mei_me_probe - Device Initialization Routine
  108. *
  109. * @pdev: PCI device structure
  110. * @ent: entry in kcs_pci_tbl
  111. *
  112. * Return: 0 on success, <0 on failure.
  113. */
  114. static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  115. {
  116. const struct mei_cfg *cfg = (struct mei_cfg *)(ent->driver_data);
  117. struct mei_device *dev;
  118. struct mei_me_hw *hw;
  119. int err;
  120. if (!mei_me_quirk_probe(pdev, cfg))
  121. return -ENODEV;
  122. /* enable pci dev */
  123. err = pci_enable_device(pdev);
  124. if (err) {
  125. dev_err(&pdev->dev, "failed to enable pci device.\n");
  126. goto end;
  127. }
  128. /* set PCI host mastering */
  129. pci_set_master(pdev);
  130. /* pci request regions for mei driver */
  131. err = pci_request_regions(pdev, KBUILD_MODNAME);
  132. if (err) {
  133. dev_err(&pdev->dev, "failed to get pci regions.\n");
  134. goto disable_device;
  135. }
  136. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) ||
  137. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  138. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  139. if (err)
  140. err = dma_set_coherent_mask(&pdev->dev,
  141. DMA_BIT_MASK(32));
  142. }
  143. if (err) {
  144. dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
  145. goto release_regions;
  146. }
  147. /* allocates and initializes the mei dev structure */
  148. dev = mei_me_dev_init(pdev, cfg);
  149. if (!dev) {
  150. err = -ENOMEM;
  151. goto release_regions;
  152. }
  153. hw = to_me_hw(dev);
  154. /* mapping IO device memory */
  155. hw->mem_addr = pci_iomap(pdev, 0, 0);
  156. if (!hw->mem_addr) {
  157. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  158. err = -ENOMEM;
  159. goto free_device;
  160. }
  161. pci_enable_msi(pdev);
  162. /* request and enable interrupt */
  163. if (pci_dev_msi_enabled(pdev))
  164. err = request_threaded_irq(pdev->irq,
  165. NULL,
  166. mei_me_irq_thread_handler,
  167. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  168. else
  169. err = request_threaded_irq(pdev->irq,
  170. mei_me_irq_quick_handler,
  171. mei_me_irq_thread_handler,
  172. IRQF_SHARED, KBUILD_MODNAME, dev);
  173. if (err) {
  174. dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
  175. pdev->irq);
  176. goto disable_msi;
  177. }
  178. if (mei_start(dev)) {
  179. dev_err(&pdev->dev, "init hw failure.\n");
  180. err = -ENODEV;
  181. goto release_irq;
  182. }
  183. pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_ME_RPM_TIMEOUT);
  184. pm_runtime_use_autosuspend(&pdev->dev);
  185. err = mei_register(dev, &pdev->dev);
  186. if (err)
  187. goto release_irq;
  188. pci_set_drvdata(pdev, dev);
  189. schedule_delayed_work(&dev->timer_work, HZ);
  190. /*
  191. * For not wake-able HW runtime pm framework
  192. * can't be used on pci device level.
  193. * Use domain runtime pm callbacks instead.
  194. */
  195. if (!pci_dev_run_wake(pdev))
  196. mei_me_set_pm_domain(dev);
  197. if (mei_pg_is_enabled(dev))
  198. pm_runtime_put_noidle(&pdev->dev);
  199. dev_dbg(&pdev->dev, "initialization successful.\n");
  200. return 0;
  201. release_irq:
  202. mei_cancel_work(dev);
  203. mei_disable_interrupts(dev);
  204. free_irq(pdev->irq, dev);
  205. disable_msi:
  206. pci_disable_msi(pdev);
  207. pci_iounmap(pdev, hw->mem_addr);
  208. free_device:
  209. kfree(dev);
  210. release_regions:
  211. pci_release_regions(pdev);
  212. disable_device:
  213. pci_disable_device(pdev);
  214. end:
  215. dev_err(&pdev->dev, "initialization failed.\n");
  216. return err;
  217. }
  218. /**
  219. * mei_me_remove - Device Removal Routine
  220. *
  221. * @pdev: PCI device structure
  222. *
  223. * mei_remove is called by the PCI subsystem to alert the driver
  224. * that it should release a PCI device.
  225. */
  226. static void mei_me_remove(struct pci_dev *pdev)
  227. {
  228. struct mei_device *dev;
  229. struct mei_me_hw *hw;
  230. dev = pci_get_drvdata(pdev);
  231. if (!dev)
  232. return;
  233. if (mei_pg_is_enabled(dev))
  234. pm_runtime_get_noresume(&pdev->dev);
  235. hw = to_me_hw(dev);
  236. dev_dbg(&pdev->dev, "stop\n");
  237. mei_stop(dev);
  238. if (!pci_dev_run_wake(pdev))
  239. mei_me_unset_pm_domain(dev);
  240. /* disable interrupts */
  241. mei_disable_interrupts(dev);
  242. free_irq(pdev->irq, dev);
  243. pci_disable_msi(pdev);
  244. if (hw->mem_addr)
  245. pci_iounmap(pdev, hw->mem_addr);
  246. mei_deregister(dev);
  247. kfree(dev);
  248. pci_release_regions(pdev);
  249. pci_disable_device(pdev);
  250. }
  251. #ifdef CONFIG_PM_SLEEP
  252. static int mei_me_pci_suspend(struct device *device)
  253. {
  254. struct pci_dev *pdev = to_pci_dev(device);
  255. struct mei_device *dev = pci_get_drvdata(pdev);
  256. if (!dev)
  257. return -ENODEV;
  258. dev_dbg(&pdev->dev, "suspend\n");
  259. mei_stop(dev);
  260. mei_disable_interrupts(dev);
  261. free_irq(pdev->irq, dev);
  262. pci_disable_msi(pdev);
  263. return 0;
  264. }
  265. static int mei_me_pci_resume(struct device *device)
  266. {
  267. struct pci_dev *pdev = to_pci_dev(device);
  268. struct mei_device *dev;
  269. int err;
  270. dev = pci_get_drvdata(pdev);
  271. if (!dev)
  272. return -ENODEV;
  273. pci_enable_msi(pdev);
  274. /* request and enable interrupt */
  275. if (pci_dev_msi_enabled(pdev))
  276. err = request_threaded_irq(pdev->irq,
  277. NULL,
  278. mei_me_irq_thread_handler,
  279. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  280. else
  281. err = request_threaded_irq(pdev->irq,
  282. mei_me_irq_quick_handler,
  283. mei_me_irq_thread_handler,
  284. IRQF_SHARED, KBUILD_MODNAME, dev);
  285. if (err) {
  286. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  287. pdev->irq);
  288. return err;
  289. }
  290. err = mei_restart(dev);
  291. if (err)
  292. return err;
  293. /* Start timer if stopped in suspend */
  294. schedule_delayed_work(&dev->timer_work, HZ);
  295. return 0;
  296. }
  297. #endif /* CONFIG_PM_SLEEP */
  298. #ifdef CONFIG_PM
  299. static int mei_me_pm_runtime_idle(struct device *device)
  300. {
  301. struct pci_dev *pdev = to_pci_dev(device);
  302. struct mei_device *dev;
  303. dev_dbg(&pdev->dev, "rpm: me: runtime_idle\n");
  304. dev = pci_get_drvdata(pdev);
  305. if (!dev)
  306. return -ENODEV;
  307. if (mei_write_is_idle(dev))
  308. pm_runtime_autosuspend(device);
  309. return -EBUSY;
  310. }
  311. static int mei_me_pm_runtime_suspend(struct device *device)
  312. {
  313. struct pci_dev *pdev = to_pci_dev(device);
  314. struct mei_device *dev;
  315. int ret;
  316. dev_dbg(&pdev->dev, "rpm: me: runtime suspend\n");
  317. dev = pci_get_drvdata(pdev);
  318. if (!dev)
  319. return -ENODEV;
  320. mutex_lock(&dev->device_lock);
  321. if (mei_write_is_idle(dev))
  322. ret = mei_me_pg_enter_sync(dev);
  323. else
  324. ret = -EAGAIN;
  325. mutex_unlock(&dev->device_lock);
  326. dev_dbg(&pdev->dev, "rpm: me: runtime suspend ret=%d\n", ret);
  327. return ret;
  328. }
  329. static int mei_me_pm_runtime_resume(struct device *device)
  330. {
  331. struct pci_dev *pdev = to_pci_dev(device);
  332. struct mei_device *dev;
  333. int ret;
  334. dev_dbg(&pdev->dev, "rpm: me: runtime resume\n");
  335. dev = pci_get_drvdata(pdev);
  336. if (!dev)
  337. return -ENODEV;
  338. mutex_lock(&dev->device_lock);
  339. ret = mei_me_pg_exit_sync(dev);
  340. mutex_unlock(&dev->device_lock);
  341. dev_dbg(&pdev->dev, "rpm: me: runtime resume ret = %d\n", ret);
  342. return ret;
  343. }
  344. /**
  345. * mei_me_set_pm_domain - fill and set pm domain structure for device
  346. *
  347. * @dev: mei_device
  348. */
  349. static inline void mei_me_set_pm_domain(struct mei_device *dev)
  350. {
  351. struct pci_dev *pdev = to_pci_dev(dev->dev);
  352. if (pdev->dev.bus && pdev->dev.bus->pm) {
  353. dev->pg_domain.ops = *pdev->dev.bus->pm;
  354. dev->pg_domain.ops.runtime_suspend = mei_me_pm_runtime_suspend;
  355. dev->pg_domain.ops.runtime_resume = mei_me_pm_runtime_resume;
  356. dev->pg_domain.ops.runtime_idle = mei_me_pm_runtime_idle;
  357. pdev->dev.pm_domain = &dev->pg_domain;
  358. }
  359. }
  360. /**
  361. * mei_me_unset_pm_domain - clean pm domain structure for device
  362. *
  363. * @dev: mei_device
  364. */
  365. static inline void mei_me_unset_pm_domain(struct mei_device *dev)
  366. {
  367. /* stop using pm callbacks if any */
  368. dev->dev->pm_domain = NULL;
  369. }
  370. static const struct dev_pm_ops mei_me_pm_ops = {
  371. SET_SYSTEM_SLEEP_PM_OPS(mei_me_pci_suspend,
  372. mei_me_pci_resume)
  373. SET_RUNTIME_PM_OPS(
  374. mei_me_pm_runtime_suspend,
  375. mei_me_pm_runtime_resume,
  376. mei_me_pm_runtime_idle)
  377. };
  378. #define MEI_ME_PM_OPS (&mei_me_pm_ops)
  379. #else
  380. #define MEI_ME_PM_OPS NULL
  381. #endif /* CONFIG_PM */
  382. /*
  383. * PCI driver structure
  384. */
  385. static struct pci_driver mei_me_driver = {
  386. .name = KBUILD_MODNAME,
  387. .id_table = mei_me_pci_tbl,
  388. .probe = mei_me_probe,
  389. .remove = mei_me_remove,
  390. .shutdown = mei_me_remove,
  391. .driver.pm = MEI_ME_PM_OPS,
  392. };
  393. module_pci_driver(mei_me_driver);
  394. MODULE_AUTHOR("Intel Corporation");
  395. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  396. MODULE_LICENSE("GPL v2");