pci-txe.c 9.6 KB

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