pci-txe.c 9.5 KB

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