pci-txe.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_runtime.h>
  30. #include <linux/mei.h>
  31. #include "mei_dev.h"
  32. #include "hw-txe.h"
  33. static const struct pci_device_id mei_txe_pci_tbl[] = {
  34. {PCI_VDEVICE(INTEL, 0x0F18)}, /* Baytrail */
  35. {PCI_VDEVICE(INTEL, 0x2298)}, /* Cherrytrail */
  36. {0, }
  37. };
  38. MODULE_DEVICE_TABLE(pci, mei_txe_pci_tbl);
  39. #ifdef CONFIG_PM
  40. static inline void mei_txe_set_pm_domain(struct mei_device *dev);
  41. static inline void mei_txe_unset_pm_domain(struct mei_device *dev);
  42. #else
  43. static inline void mei_txe_set_pm_domain(struct mei_device *dev) {}
  44. static inline void mei_txe_unset_pm_domain(struct mei_device *dev) {}
  45. #endif /* CONFIG_PM */
  46. static void mei_txe_pci_iounmap(struct pci_dev *pdev, struct mei_txe_hw *hw)
  47. {
  48. int i;
  49. for (i = SEC_BAR; i < NUM_OF_MEM_BARS; i++) {
  50. if (hw->mem_addr[i]) {
  51. pci_iounmap(pdev, hw->mem_addr[i]);
  52. hw->mem_addr[i] = NULL;
  53. }
  54. }
  55. }
  56. /**
  57. * mei_txe_probe - Device Initialization Routine
  58. *
  59. * @pdev: PCI device structure
  60. * @ent: entry in mei_txe_pci_tbl
  61. *
  62. * Return: 0 on success, <0 on failure.
  63. */
  64. static int mei_txe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  65. {
  66. struct mei_device *dev;
  67. struct mei_txe_hw *hw;
  68. int err;
  69. int i;
  70. /* enable pci dev */
  71. err = pci_enable_device(pdev);
  72. if (err) {
  73. dev_err(&pdev->dev, "failed to enable pci device.\n");
  74. goto end;
  75. }
  76. /* set PCI host mastering */
  77. pci_set_master(pdev);
  78. /* pci request regions for mei driver */
  79. err = pci_request_regions(pdev, KBUILD_MODNAME);
  80. if (err) {
  81. dev_err(&pdev->dev, "failed to get pci regions.\n");
  82. goto disable_device;
  83. }
  84. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  85. if (err) {
  86. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  87. if (err) {
  88. dev_err(&pdev->dev, "No suitable DMA available.\n");
  89. goto release_regions;
  90. }
  91. }
  92. /* allocates and initializes the mei dev structure */
  93. dev = mei_txe_dev_init(pdev);
  94. if (!dev) {
  95. err = -ENOMEM;
  96. goto release_regions;
  97. }
  98. hw = to_txe_hw(dev);
  99. /* mapping IO device memory */
  100. for (i = SEC_BAR; i < NUM_OF_MEM_BARS; i++) {
  101. hw->mem_addr[i] = pci_iomap(pdev, i, 0);
  102. if (!hw->mem_addr[i]) {
  103. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  104. err = -ENOMEM;
  105. goto free_device;
  106. }
  107. }
  108. pci_enable_msi(pdev);
  109. /* clear spurious interrupts */
  110. mei_clear_interrupts(dev);
  111. /* request and enable interrupt */
  112. if (pci_dev_msi_enabled(pdev))
  113. err = request_threaded_irq(pdev->irq,
  114. NULL,
  115. mei_txe_irq_thread_handler,
  116. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  117. else
  118. err = request_threaded_irq(pdev->irq,
  119. mei_txe_irq_quick_handler,
  120. mei_txe_irq_thread_handler,
  121. IRQF_SHARED, KBUILD_MODNAME, dev);
  122. if (err) {
  123. dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
  124. pdev->irq);
  125. goto free_device;
  126. }
  127. if (mei_start(dev)) {
  128. dev_err(&pdev->dev, "init hw failure.\n");
  129. err = -ENODEV;
  130. goto release_irq;
  131. }
  132. pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
  133. pm_runtime_use_autosuspend(&pdev->dev);
  134. err = mei_register(dev, &pdev->dev);
  135. if (err)
  136. goto release_irq;
  137. pci_set_drvdata(pdev, dev);
  138. /*
  139. * For not wake-able HW runtime pm framework
  140. * can't be used on pci device level.
  141. * Use domain runtime pm callbacks instead.
  142. */
  143. if (!pci_dev_run_wake(pdev))
  144. mei_txe_set_pm_domain(dev);
  145. pm_runtime_put_noidle(&pdev->dev);
  146. return 0;
  147. release_irq:
  148. mei_cancel_work(dev);
  149. /* disable interrupts */
  150. mei_disable_interrupts(dev);
  151. free_irq(pdev->irq, dev);
  152. pci_disable_msi(pdev);
  153. free_device:
  154. mei_txe_pci_iounmap(pdev, hw);
  155. kfree(dev);
  156. release_regions:
  157. pci_release_regions(pdev);
  158. disable_device:
  159. pci_disable_device(pdev);
  160. end:
  161. dev_err(&pdev->dev, "initialization failed.\n");
  162. return err;
  163. }
  164. /**
  165. * mei_txe_remove - Device Removal Routine
  166. *
  167. * @pdev: PCI device structure
  168. *
  169. * mei_remove is called by the PCI subsystem to alert the driver
  170. * that it should release a PCI device.
  171. */
  172. static void mei_txe_remove(struct pci_dev *pdev)
  173. {
  174. struct mei_device *dev;
  175. struct mei_txe_hw *hw;
  176. dev = pci_get_drvdata(pdev);
  177. if (!dev) {
  178. dev_err(&pdev->dev, "mei: dev =NULL\n");
  179. return;
  180. }
  181. pm_runtime_get_noresume(&pdev->dev);
  182. hw = to_txe_hw(dev);
  183. mei_stop(dev);
  184. if (!pci_dev_run_wake(pdev))
  185. mei_txe_unset_pm_domain(dev);
  186. /* disable interrupts */
  187. mei_disable_interrupts(dev);
  188. free_irq(pdev->irq, dev);
  189. pci_disable_msi(pdev);
  190. pci_set_drvdata(pdev, NULL);
  191. mei_txe_pci_iounmap(pdev, hw);
  192. mei_deregister(dev);
  193. kfree(dev);
  194. pci_release_regions(pdev);
  195. pci_disable_device(pdev);
  196. }
  197. #ifdef CONFIG_PM_SLEEP
  198. static int mei_txe_pci_suspend(struct device *device)
  199. {
  200. struct pci_dev *pdev = to_pci_dev(device);
  201. struct mei_device *dev = pci_get_drvdata(pdev);
  202. if (!dev)
  203. return -ENODEV;
  204. dev_dbg(&pdev->dev, "suspend\n");
  205. mei_stop(dev);
  206. mei_disable_interrupts(dev);
  207. free_irq(pdev->irq, dev);
  208. pci_disable_msi(pdev);
  209. return 0;
  210. }
  211. static int mei_txe_pci_resume(struct device *device)
  212. {
  213. struct pci_dev *pdev = to_pci_dev(device);
  214. struct mei_device *dev;
  215. int err;
  216. dev = pci_get_drvdata(pdev);
  217. if (!dev)
  218. return -ENODEV;
  219. pci_enable_msi(pdev);
  220. mei_clear_interrupts(dev);
  221. /* request and enable interrupt */
  222. if (pci_dev_msi_enabled(pdev))
  223. err = request_threaded_irq(pdev->irq,
  224. NULL,
  225. mei_txe_irq_thread_handler,
  226. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  227. else
  228. err = request_threaded_irq(pdev->irq,
  229. mei_txe_irq_quick_handler,
  230. mei_txe_irq_thread_handler,
  231. IRQF_SHARED, KBUILD_MODNAME, dev);
  232. if (err) {
  233. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  234. pdev->irq);
  235. return err;
  236. }
  237. err = mei_restart(dev);
  238. return err;
  239. }
  240. #endif /* CONFIG_PM_SLEEP */
  241. #ifdef CONFIG_PM
  242. static int mei_txe_pm_runtime_idle(struct device *device)
  243. {
  244. struct pci_dev *pdev = to_pci_dev(device);
  245. struct mei_device *dev;
  246. dev_dbg(&pdev->dev, "rpm: txe: runtime_idle\n");
  247. dev = pci_get_drvdata(pdev);
  248. if (!dev)
  249. return -ENODEV;
  250. if (mei_write_is_idle(dev))
  251. pm_runtime_autosuspend(device);
  252. return -EBUSY;
  253. }
  254. static int mei_txe_pm_runtime_suspend(struct device *device)
  255. {
  256. struct pci_dev *pdev = to_pci_dev(device);
  257. struct mei_device *dev;
  258. int ret;
  259. dev_dbg(&pdev->dev, "rpm: txe: runtime suspend\n");
  260. dev = pci_get_drvdata(pdev);
  261. if (!dev)
  262. return -ENODEV;
  263. mutex_lock(&dev->device_lock);
  264. if (mei_write_is_idle(dev))
  265. ret = mei_txe_aliveness_set_sync(dev, 0);
  266. else
  267. ret = -EAGAIN;
  268. /*
  269. * If everything is okay we're about to enter PCI low
  270. * power state (D3) therefor we need to disable the
  271. * interrupts towards host.
  272. * However if device is not wakeable we do not enter
  273. * D-low state and we need to keep the interrupt kicking
  274. */
  275. if (!ret && pci_dev_run_wake(pdev))
  276. mei_disable_interrupts(dev);
  277. dev_dbg(&pdev->dev, "rpm: txe: runtime suspend ret=%d\n", ret);
  278. mutex_unlock(&dev->device_lock);
  279. return ret;
  280. }
  281. static int mei_txe_pm_runtime_resume(struct device *device)
  282. {
  283. struct pci_dev *pdev = to_pci_dev(device);
  284. struct mei_device *dev;
  285. int ret;
  286. dev_dbg(&pdev->dev, "rpm: txe: runtime resume\n");
  287. dev = pci_get_drvdata(pdev);
  288. if (!dev)
  289. return -ENODEV;
  290. mutex_lock(&dev->device_lock);
  291. mei_enable_interrupts(dev);
  292. ret = mei_txe_aliveness_set_sync(dev, 1);
  293. mutex_unlock(&dev->device_lock);
  294. dev_dbg(&pdev->dev, "rpm: txe: runtime resume ret = %d\n", ret);
  295. return ret;
  296. }
  297. /**
  298. * mei_txe_set_pm_domain - fill and set pm domain structure for device
  299. *
  300. * @dev: mei_device
  301. */
  302. static inline void mei_txe_set_pm_domain(struct mei_device *dev)
  303. {
  304. struct pci_dev *pdev = to_pci_dev(dev->dev);
  305. if (pdev->dev.bus && pdev->dev.bus->pm) {
  306. dev->pg_domain.ops = *pdev->dev.bus->pm;
  307. dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
  308. dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
  309. dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
  310. pdev->dev.pm_domain = &dev->pg_domain;
  311. }
  312. }
  313. /**
  314. * mei_txe_unset_pm_domain - clean pm domain structure for device
  315. *
  316. * @dev: mei_device
  317. */
  318. static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
  319. {
  320. /* stop using pm callbacks if any */
  321. dev->dev->pm_domain = NULL;
  322. }
  323. static const struct dev_pm_ops mei_txe_pm_ops = {
  324. SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
  325. mei_txe_pci_resume)
  326. SET_RUNTIME_PM_OPS(
  327. mei_txe_pm_runtime_suspend,
  328. mei_txe_pm_runtime_resume,
  329. mei_txe_pm_runtime_idle)
  330. };
  331. #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
  332. #else
  333. #define MEI_TXE_PM_OPS NULL
  334. #endif /* CONFIG_PM */
  335. /*
  336. * PCI driver structure
  337. */
  338. static struct pci_driver mei_txe_driver = {
  339. .name = KBUILD_MODNAME,
  340. .id_table = mei_txe_pci_tbl,
  341. .probe = mei_txe_probe,
  342. .remove = mei_txe_remove,
  343. .shutdown = mei_txe_remove,
  344. .driver.pm = MEI_TXE_PM_OPS,
  345. };
  346. module_pci_driver(mei_txe_driver);
  347. MODULE_AUTHOR("Intel Corporation");
  348. MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
  349. MODULE_LICENSE("GPL v2");