pci-txe.c 8.7 KB

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