pci-txe.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. * For not wake-able HW runtime pm framework
  128. * can't be used on pci device level.
  129. * Use domain runtime pm callbacks instead.
  130. */
  131. if (!pci_dev_run_wake(pdev))
  132. mei_txe_set_pm_domain(dev);
  133. pm_runtime_put_noidle(&pdev->dev);
  134. return 0;
  135. stop:
  136. mei_stop(dev);
  137. release_irq:
  138. mei_cancel_work(dev);
  139. mei_disable_interrupts(dev);
  140. free_irq(pdev->irq, dev);
  141. end:
  142. dev_err(&pdev->dev, "initialization failed.\n");
  143. return err;
  144. }
  145. /**
  146. * mei_txe_remove - Device Shutdown Routine
  147. *
  148. * @pdev: PCI device structure
  149. *
  150. * mei_txe_shutdown is called from the reboot notifier
  151. * it's a simplified version of remove so we go down
  152. * faster.
  153. */
  154. static void mei_txe_shutdown(struct pci_dev *pdev)
  155. {
  156. struct mei_device *dev;
  157. dev = pci_get_drvdata(pdev);
  158. if (!dev)
  159. return;
  160. dev_dbg(&pdev->dev, "shutdown\n");
  161. mei_stop(dev);
  162. if (!pci_dev_run_wake(pdev))
  163. mei_txe_unset_pm_domain(dev);
  164. mei_disable_interrupts(dev);
  165. free_irq(pdev->irq, dev);
  166. }
  167. /**
  168. * mei_txe_remove - Device Removal Routine
  169. *
  170. * @pdev: PCI device structure
  171. *
  172. * mei_remove is called by the PCI subsystem to alert the driver
  173. * that it should release a PCI device.
  174. */
  175. static void mei_txe_remove(struct pci_dev *pdev)
  176. {
  177. struct mei_device *dev;
  178. dev = pci_get_drvdata(pdev);
  179. if (!dev) {
  180. dev_err(&pdev->dev, "mei: dev == NULL\n");
  181. return;
  182. }
  183. pm_runtime_get_noresume(&pdev->dev);
  184. mei_stop(dev);
  185. if (!pci_dev_run_wake(pdev))
  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. /*
  263. * If everything is okay we're about to enter PCI low
  264. * power state (D3) therefor we need to disable the
  265. * interrupts towards host.
  266. * However if device is not wakeable we do not enter
  267. * D-low state and we need to keep the interrupt kicking
  268. */
  269. if (!ret && pci_dev_run_wake(pdev))
  270. mei_disable_interrupts(dev);
  271. dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
  272. mutex_unlock(&dev->device_lock);
  273. if (ret && ret != -EAGAIN)
  274. schedule_work(&dev->reset_work);
  275. return ret;
  276. }
  277. static int mei_txe_pm_runtime_resume(struct device *device)
  278. {
  279. struct pci_dev *pdev = to_pci_dev(device);
  280. struct mei_device *dev;
  281. int ret;
  282. dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
  283. dev = pci_get_drvdata(pdev);
  284. if (!dev)
  285. return -ENODEV;
  286. mutex_lock(&dev->device_lock);
  287. mei_enable_interrupts(dev);
  288. ret = mei_txe_aliveness_set_sync(dev, 1);
  289. mutex_unlock(&dev->device_lock);
  290. dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
  291. if (ret)
  292. schedule_work(&dev->reset_work);
  293. return ret;
  294. }
  295. /**
  296. * mei_txe_set_pm_domain - fill and set pm domain structure for device
  297. *
  298. * @dev: mei_device
  299. */
  300. static inline void mei_txe_set_pm_domain(struct mei_device *dev)
  301. {
  302. struct pci_dev *pdev = to_pci_dev(dev->dev);
  303. if (pdev->dev.bus && pdev->dev.bus->pm) {
  304. dev->pg_domain.ops = *pdev->dev.bus->pm;
  305. dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
  306. dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
  307. dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
  308. dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
  309. }
  310. }
  311. /**
  312. * mei_txe_unset_pm_domain - clean pm domain structure for device
  313. *
  314. * @dev: mei_device
  315. */
  316. static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
  317. {
  318. /* stop using pm callbacks if any */
  319. dev_pm_domain_set(dev->dev, NULL);
  320. }
  321. static const struct dev_pm_ops mei_txe_pm_ops = {
  322. SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
  323. mei_txe_pci_resume)
  324. SET_RUNTIME_PM_OPS(
  325. mei_txe_pm_runtime_suspend,
  326. mei_txe_pm_runtime_resume,
  327. mei_txe_pm_runtime_idle)
  328. };
  329. #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
  330. #else
  331. #define MEI_TXE_PM_OPS NULL
  332. #endif /* CONFIG_PM */
  333. /*
  334. * PCI driver structure
  335. */
  336. static struct pci_driver mei_txe_driver = {
  337. .name = KBUILD_MODNAME,
  338. .id_table = mei_txe_pci_tbl,
  339. .probe = mei_txe_probe,
  340. .remove = mei_txe_remove,
  341. .shutdown = mei_txe_shutdown,
  342. .driver.pm = MEI_TXE_PM_OPS,
  343. };
  344. module_pci_driver(mei_txe_driver);
  345. MODULE_AUTHOR("Intel Corporation");
  346. MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
  347. MODULE_LICENSE("GPL v2");