pci-txe.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. err = mei_register(dev, &pdev->dev);
  79. if (err)
  80. goto end;
  81. pci_enable_msi(pdev);
  82. /* clear spurious interrupts */
  83. mei_clear_interrupts(dev);
  84. /* request and enable interrupt */
  85. if (pci_dev_msi_enabled(pdev))
  86. err = request_threaded_irq(pdev->irq,
  87. NULL,
  88. mei_txe_irq_thread_handler,
  89. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  90. else
  91. err = request_threaded_irq(pdev->irq,
  92. mei_txe_irq_quick_handler,
  93. mei_txe_irq_thread_handler,
  94. IRQF_SHARED, KBUILD_MODNAME, dev);
  95. if (err) {
  96. dev_err(&pdev->dev, "mei: request_threaded_irq failure. irq = %d\n",
  97. pdev->irq);
  98. goto deregister;
  99. }
  100. if (mei_start(dev)) {
  101. dev_err(&pdev->dev, "init hw failure.\n");
  102. err = -ENODEV;
  103. goto deregister;
  104. }
  105. pm_runtime_set_autosuspend_delay(&pdev->dev, MEI_TXI_RPM_TIMEOUT);
  106. pm_runtime_use_autosuspend(&pdev->dev);
  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. deregister:
  125. mei_cancel_work(dev);
  126. mei_disable_interrupts(dev);
  127. free_irq(pdev->irq, dev);
  128. mei_deregister(dev);
  129. end:
  130. dev_err(&pdev->dev, "initialization failed.\n");
  131. return err;
  132. }
  133. /**
  134. * mei_txe_shutdown- Device Shutdown Routine
  135. *
  136. * @pdev: PCI device structure
  137. *
  138. * mei_txe_shutdown is called from the reboot notifier
  139. * it's a simplified version of remove so we go down
  140. * faster.
  141. */
  142. static void mei_txe_shutdown(struct pci_dev *pdev)
  143. {
  144. struct mei_device *dev = pci_get_drvdata(pdev);
  145. dev_dbg(&pdev->dev, "shutdown\n");
  146. mei_stop(dev);
  147. mei_txe_unset_pm_domain(dev);
  148. mei_disable_interrupts(dev);
  149. free_irq(pdev->irq, dev);
  150. }
  151. /**
  152. * mei_txe_remove - Device Removal Routine
  153. *
  154. * @pdev: PCI device structure
  155. *
  156. * mei_remove is called by the PCI subsystem to alert the driver
  157. * that it should release a PCI device.
  158. */
  159. static void mei_txe_remove(struct pci_dev *pdev)
  160. {
  161. struct mei_device *dev = pci_get_drvdata(pdev);
  162. pm_runtime_get_noresume(&pdev->dev);
  163. mei_stop(dev);
  164. mei_txe_unset_pm_domain(dev);
  165. mei_disable_interrupts(dev);
  166. free_irq(pdev->irq, dev);
  167. mei_deregister(dev);
  168. }
  169. #ifdef CONFIG_PM_SLEEP
  170. static int mei_txe_pci_suspend(struct device *device)
  171. {
  172. struct pci_dev *pdev = to_pci_dev(device);
  173. struct mei_device *dev = pci_get_drvdata(pdev);
  174. dev_dbg(&pdev->dev, "suspend\n");
  175. mei_stop(dev);
  176. mei_disable_interrupts(dev);
  177. free_irq(pdev->irq, dev);
  178. pci_disable_msi(pdev);
  179. return 0;
  180. }
  181. static int mei_txe_pci_resume(struct device *device)
  182. {
  183. struct pci_dev *pdev = to_pci_dev(device);
  184. struct mei_device *dev = pci_get_drvdata(pdev);
  185. int err;
  186. pci_enable_msi(pdev);
  187. mei_clear_interrupts(dev);
  188. /* request and enable interrupt */
  189. if (pci_dev_msi_enabled(pdev))
  190. err = request_threaded_irq(pdev->irq,
  191. NULL,
  192. mei_txe_irq_thread_handler,
  193. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  194. else
  195. err = request_threaded_irq(pdev->irq,
  196. mei_txe_irq_quick_handler,
  197. mei_txe_irq_thread_handler,
  198. IRQF_SHARED, KBUILD_MODNAME, dev);
  199. if (err) {
  200. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  201. pdev->irq);
  202. return err;
  203. }
  204. err = mei_restart(dev);
  205. return err;
  206. }
  207. #endif /* CONFIG_PM_SLEEP */
  208. #ifdef CONFIG_PM
  209. static int mei_txe_pm_runtime_idle(struct device *device)
  210. {
  211. struct mei_device *dev = dev_get_drvdata(device);
  212. dev_dbg(device, "rpm: txe: runtime_idle\n");
  213. if (mei_write_is_idle(dev))
  214. pm_runtime_autosuspend(device);
  215. return -EBUSY;
  216. }
  217. static int mei_txe_pm_runtime_suspend(struct device *device)
  218. {
  219. struct mei_device *dev = dev_get_drvdata(device);
  220. int ret;
  221. dev_dbg(device, "rpm: txe: runtime suspend\n");
  222. mutex_lock(&dev->device_lock);
  223. if (mei_write_is_idle(dev))
  224. ret = mei_txe_aliveness_set_sync(dev, 0);
  225. else
  226. ret = -EAGAIN;
  227. /* keep irq on we are staying in D0 */
  228. dev_dbg(device, "rpm: txe: runtime suspend ret=%d\n", ret);
  229. mutex_unlock(&dev->device_lock);
  230. if (ret && ret != -EAGAIN)
  231. schedule_work(&dev->reset_work);
  232. return ret;
  233. }
  234. static int mei_txe_pm_runtime_resume(struct device *device)
  235. {
  236. struct mei_device *dev = dev_get_drvdata(device);
  237. int ret;
  238. dev_dbg(device, "rpm: txe: runtime resume\n");
  239. mutex_lock(&dev->device_lock);
  240. mei_enable_interrupts(dev);
  241. ret = mei_txe_aliveness_set_sync(dev, 1);
  242. mutex_unlock(&dev->device_lock);
  243. dev_dbg(device, "rpm: txe: runtime resume ret = %d\n", ret);
  244. if (ret)
  245. schedule_work(&dev->reset_work);
  246. return ret;
  247. }
  248. /**
  249. * mei_txe_set_pm_domain - fill and set pm domain structure for device
  250. *
  251. * @dev: mei_device
  252. */
  253. static inline void mei_txe_set_pm_domain(struct mei_device *dev)
  254. {
  255. struct pci_dev *pdev = to_pci_dev(dev->parent);
  256. if (pdev->dev.bus && pdev->dev.bus->pm) {
  257. dev->pg_domain.ops = *pdev->dev.bus->pm;
  258. dev->pg_domain.ops.runtime_suspend = mei_txe_pm_runtime_suspend;
  259. dev->pg_domain.ops.runtime_resume = mei_txe_pm_runtime_resume;
  260. dev->pg_domain.ops.runtime_idle = mei_txe_pm_runtime_idle;
  261. dev_pm_domain_set(&pdev->dev, &dev->pg_domain);
  262. }
  263. }
  264. /**
  265. * mei_txe_unset_pm_domain - clean pm domain structure for device
  266. *
  267. * @dev: mei_device
  268. */
  269. static inline void mei_txe_unset_pm_domain(struct mei_device *dev)
  270. {
  271. /* stop using pm callbacks if any */
  272. dev_pm_domain_set(dev->parent, NULL);
  273. }
  274. static const struct dev_pm_ops mei_txe_pm_ops = {
  275. SET_SYSTEM_SLEEP_PM_OPS(mei_txe_pci_suspend,
  276. mei_txe_pci_resume)
  277. SET_RUNTIME_PM_OPS(
  278. mei_txe_pm_runtime_suspend,
  279. mei_txe_pm_runtime_resume,
  280. mei_txe_pm_runtime_idle)
  281. };
  282. #define MEI_TXE_PM_OPS (&mei_txe_pm_ops)
  283. #else
  284. #define MEI_TXE_PM_OPS NULL
  285. #endif /* CONFIG_PM */
  286. /*
  287. * PCI driver structure
  288. */
  289. static struct pci_driver mei_txe_driver = {
  290. .name = KBUILD_MODNAME,
  291. .id_table = mei_txe_pci_tbl,
  292. .probe = mei_txe_probe,
  293. .remove = mei_txe_remove,
  294. .shutdown = mei_txe_shutdown,
  295. .driver.pm = MEI_TXE_PM_OPS,
  296. };
  297. module_pci_driver(mei_txe_driver);
  298. MODULE_AUTHOR("Intel Corporation");
  299. MODULE_DESCRIPTION("Intel(R) Trusted Execution Environment Interface");
  300. MODULE_LICENSE("GPL v2");