pci-txe.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/fs.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/uuid.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/pm_domain.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/mei.h>
  32. #include "mei_dev.h"
  33. #include "hw-txe.h"
  34. static const struct pci_device_id mei_txe_pci_tbl[] = {
  35. {PCI_VDEVICE(INTEL, 0x0F18)}, /* Baytrail */
  36. {PCI_VDEVICE(INTEL, 0x2298)}, /* Cherrytrail */
  37. {0, }
  38. };
  39. MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
  40. #ifdef CONFIG_PM
  41. static inline void mei_txe_set_pm_domain(struct mei_device *dev);
  42. static inline void mei_txe_unset_pm_domain(struct mei_device *dev);
  43. #else
  44. static inline void mei_txe_set_pm_domain(struct mei_device *dev) {}
  45. static inline void mei_txe_unset_pm_domain(struct mei_device *dev) {}
  46. #endif /* CONFIG_PM */
  47. /**
  48. * mei_txe_probe - Device Initialization Routine
  49. *
  50. * @pdev: PCI device structure
  51. * @ent: entry in mei_txe_pci_tbl
  52. *
  53. * Return: 0 on success, <0 on failure.
  54. */
  55. static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  56. {
  57. struct mei_device *dev;
  58. struct mei_txe_hw *hw;
  59. const int mask = BIT(SEC_BAR) | BIT(BRIDGE_BAR);
  60. int err;
  61. /* enable pci dev */
  62. err = pcim_enable_device(pdev);
  63. if (err) {
  64. dev_err(&pdev->dev, "failed to enable pci device.\n");
  65. goto end;
  66. }
  67. /* set PCI host mastering */
  68. pci_set_master(pdev);
  69. /* pci request regions and mapping IO device memory for mei driver */
  70. err = pcim_iomap_regions(pdev, mask, KBUILD_MODNAME);
  71. if (err) {
  72. dev_err(&pdev->dev, "failed to get pci regions.\n");
  73. goto end;
  74. }
  75. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  76. if (err) {
  77. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  78. if (err) {
  79. dev_err(&pdev->dev, "No suitable DMA available.\n");
  80. goto end;
  81. }
  82. }
  83. /* allocates and initializes the mei dev structure */
  84. dev = mei_txe_dev_init(pdev);
  85. if (!dev) {
  86. err = -ENOMEM;
  87. goto end;
  88. }
  89. hw = to_txe_hw(dev);
  90. hw->mem_addr = pcim_iomap_table(pdev);
  91. pci_enable_msi(pdev);
  92. /* clear spurious interrupts */
  93. mei_clear_interrupts(dev);
  94. /* request and enable interrupt */
  95. if (pci_dev_msi_enabled(pdev))
  96. err = request_threaded_irq(pdev->irq,
  97. NULL,
  98. mei_txe_irq_thread_handler,
  99. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  100. else
  101. err = request_threaded_irq(pdev->irq,
  102. mei_txe_irq_quick_handler,
  103. mei_txe_irq_thread_handler,
  104. IRQF_SHARED, KBUILD_MODNAME, dev);
  105. if (err) {
  106. dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
  107. pdev->irq);
  108. goto end;
  109. }
  110. if (mei_start(dev)) {
  111. dev_err(&pdev->dev, "init hw failure.\n");
  112. err = -ENODEV;
  113. goto release_irq;
  114. }
  115. pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
  116. pm_runtime_use_autosuspend(&pdev->dev);
  117. err = mei_register(dev, &pdev->dev);
  118. if (err)
  119. goto stop;
  120. pci_set_drvdata(pdev, dev);
  121. /*
  122. * MEI requires to resume from runtime suspend mode
  123. * in order to perform link reset flow upon system suspend.
  124. */
  125. pdev->dev_flags |= PCI_DEV_FLAGS_NEEDS_RESUME;
  126. /*
  127. * TXE maps runtime suspend/resume to own power gating states,
  128. * hence we need to go around native PCI runtime service which
  129. * eventually brings the device into D3cold/hot state.
  130. * But the TXE device cannot wake up from D3 unlike from own
  131. * power gating. To get around PCI device native runtime pm,
  132. * TXE uses runtime pm domain handlers which take precedence.
  133. */
  134. mei_txe_set_pm_domain(dev);
  135. pm_runtime_put_noidle(&pdev->dev);
  136. return 0;
  137. stop:
  138. mei_stop(dev);
  139. release_irq:
  140. mei_cancel_work(dev);
  141. mei_disable_interrupts(dev);
  142. free_irq(pdev->irq, dev);
  143. end:
  144. dev_err(&pdev->dev, "initialization failed.\n");
  145. return err;
  146. }
  147. /**
  148. * mei_txe_remove - Device Shutdown Routine
  149. *
  150. * @pdev: PCI device structure
  151. *
  152. * mei_txe_shutdown is called from the reboot notifier
  153. * it's a simplified version of remove so we go down
  154. * faster.
  155. */
  156. static void mei_txe_shutdown(struct pci_dev *pdev)
  157. {
  158. struct mei_device *dev;
  159. dev = pci_get_drvdata(pdev);
  160. if (!dev)
  161. return;
  162. dev_dbg(&pdev->dev, "shutdown\n");
  163. mei_stop(dev);
  164. mei_txe_unset_pm_domain(dev);
  165. mei_disable_interrupts(dev);
  166. free_irq(pdev->irq, dev);
  167. }
  168. /**
  169. * mei_txe_remove - Device Removal Routine
  170. *
  171. * @pdev: PCI device structure
  172. *
  173. * mei_remove is called by the PCI subsystem to alert the driver
  174. * that it should release a PCI device.
  175. */
  176. static void mei_txe_remove(struct pci_dev *pdev)
  177. {
  178. struct mei_device *dev;
  179. dev = pci_get_drvdata(pdev);
  180. if (!dev) {
  181. dev_err(&pdev->dev, "mei: dev == NULL\n");
  182. return;
  183. }
  184. pm_runtime_get_noresume(&pdev->dev);
  185. mei_stop(dev);
  186. mei_txe_unset_pm_domain(dev);
  187. mei_disable_interrupts(dev);
  188. free_irq(pdev->irq, dev);
  189. mei_deregister(dev);
  190. }
  191. #ifdef CONFIG_PM_SLEEP
  192. static int mei_txe_pci_suspend(struct device *device)
  193. {
  194. struct pci_dev *pdev = to_pci_dev(device);
  195. struct mei_device *dev = pci_get_drvdata(pdev);
  196. if (!dev)
  197. return -ENODEV;
  198. dev_dbg(&pdev->dev, "suspend\n");
  199. mei_stop(dev);
  200. mei_disable_interrupts(dev);
  201. free_irq(pdev->irq, dev);
  202. pci_disable_msi(pdev);
  203. return 0;
  204. }
  205. static int mei_txe_pci_resume(struct device *device)
  206. {
  207. struct pci_dev *pdev = to_pci_dev(device);
  208. struct mei_device *dev;
  209. int err;
  210. dev = pci_get_drvdata(pdev);
  211. if (!dev)
  212. return -ENODEV;
  213. pci_enable_msi(pdev);
  214. mei_clear_interrupts(dev);
  215. /* request and enable interrupt */
  216. if (pci_dev_msi_enabled(pdev))
  217. err = request_threaded_irq(pdev->irq,
  218. NULL,
  219. mei_txe_irq_thread_handler,
  220. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  221. else
  222. err = request_threaded_irq(pdev->irq,
  223. mei_txe_irq_quick_handler,
  224. mei_txe_irq_thread_handler,
  225. IRQF_SHARED, KBUILD_MODNAME, dev);
  226. if (err) {
  227. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  228. pdev->irq);
  229. return err;
  230. }
  231. err = mei_restart(dev);
  232. return err;
  233. }
  234. #endif /* CONFIG_PM_SLEEP */
  235. #ifdef CONFIG_PM
  236. static int mei_txe_pm_runtime_idle(struct device *device)
  237. {
  238. struct pci_dev *pdev = to_pci_dev(device);
  239. struct mei_device *dev;
  240. dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
  241. dev = pci_get_drvdata(pdev);
  242. if (!dev)
  243. return -ENODEV;
  244. if (mei_write_is_idle(dev))
  245. pm_runtime_autosuspend(device);
  246. return -EBUSY;
  247. }
  248. static int mei_txe_pm_runtime_suspend(struct device *device)
  249. {
  250. struct pci_dev *pdev = to_pci_dev(device);
  251. struct mei_device *dev;
  252. int ret;
  253. dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
  254. dev = pci_get_drvdata(pdev);
  255. if (!dev)
  256. return -ENODEV;
  257. mutex_lock(&dev->device_lock);
  258. if (mei_write_is_idle(dev))
  259. ret = mei_txe_aliveness_set_sync(dev, 0);
  260. else
  261. ret = -EAGAIN;
  262. /* keep irq on we are staying in D0 */
  263. dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
  264. mutex_unlock(&dev->device_lock);
  265. if (ret && ret != -EAGAIN)
  266. schedule_work(&dev->reset_work);
  267. return ret;
  268. }
  269. static int mei_txe_pm_runtime_resume(struct device *device)
  270. {
  271. struct pci_dev *pdev = to_pci_dev(device);
  272. struct mei_device *dev;
  273. int ret;
  274. dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
  275. dev = pci_get_drvdata(pdev);
  276. if (!dev)
  277. return -ENODEV;
  278. mutex_lock(&dev->device_lock);
  279. mei_enable_interrupts(dev);
  280. ret = mei_txe_aliveness_set_sync(dev, 1);
  281. mutex_unlock(&dev->device_lock);
  282. dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
  283. if (ret)
  284. schedule_work(&dev->reset_work);
  285. return ret;
  286. }
  287. /**
  288. * mei_txe_set_pm_domain - fill and set pm domain structure for device
  289. *
  290. * @dev: mei_device
  291. */
  292. static inline void mei_txe_set_pm_domain(struct mei_device *dev)
  293. {
  294. struct pci_dev *pdev = to_pci_dev(dev->dev);
  295. if (pdev->dev.bus && pdev->dev.bus->pm) {
  296. dev->pg_domain.ops = *pdev->dev.bus->pm;
  297. dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
  298. dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
  299. dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
  300. dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
  301. }
  302. }
  303. /**
  304. * mei_txe_unset_pm_domain - clean pm domain structure for device
  305. *
  306. * @dev: mei_device
  307. */
  308. static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
  309. {
  310. /* stop using pm callbacks if any */
  311. dev_pm_domain_set(dev->dev, NULL);
  312. }
  313. static const struct dev_pm_ops mei_txe_pm_ops = {
  314. SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
  315. mei_txe_pci_resume)
  316. SET_RUNTIME_PM_OPS(
  317. mei_txe_pm_runtime_suspend,
  318. mei_txe_pm_runtime_resume,
  319. mei_txe_pm_runtime_idle)
  320. };
  321. #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
  322. #else
  323. #define MEI_TXE_PM_OPS NULL
  324. #endif /* CONFIG_PM */
  325. /*
  326. * PCI driver structure
  327. */
  328. static struct pci_driver mei_txe_driver = {
  329. .name = KBUILD_MODNAME,
  330. .id_table = mei_txe_pci_tbl,
  331. .probe = mei_txe_probe,
  332. .remove = mei_txe_remove,
  333. .shutdown = mei_txe_shutdown,
  334. .driver.pm = MEI_TXE_PM_OPS,
  335. };
  336. module_pci_driver(mei_txe_driver);
  337. MODULE_AUTHOR("Intel Corporation");
  338. MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
  339. MODULE_LICENSE("GPL v2");