pci-ish.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCI glue for ISHTP provider device (ISH) driver
  4. *
  5. * Copyright (c) 2014-2016, Intel Corporation.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/kernel.h>
  11. #include <linux/device.h>
  12. #include <linux/fs.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/pci.h>
  16. #include <linux/sched.h>
  17. #include <linux/suspend.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/workqueue.h>
  20. #define CREATE_TRACE_POINTS
  21. #include <trace/events/intel_ish.h>
  22. #include "ishtp-dev.h"
  23. #include "hw-ish.h"
  24. enum ishtp_driver_data_index {
  25. ISHTP_DRIVER_DATA_NONE,
  26. ISHTP_DRIVER_DATA_LNL_M,
  27. ISHTP_DRIVER_DATA_PTL,
  28. };
  29. #define ISH_FW_GEN_LNL_M "lnlm"
  30. #define ISH_FW_GEN_PTL "ptl"
  31. #define ISH_FIRMWARE_PATH(gen) "intel/ish/ish_" gen ".bin"
  32. #define ISH_FIRMWARE_PATH_ALL "intel/ish/ish_*.bin"
  33. static struct ishtp_driver_data ishtp_driver_data[] = {
  34. [ISHTP_DRIVER_DATA_LNL_M] = {
  35. .fw_generation = ISH_FW_GEN_LNL_M,
  36. },
  37. [ISHTP_DRIVER_DATA_PTL] = {
  38. .fw_generation = ISH_FW_GEN_PTL,
  39. },
  40. };
  41. static const struct pci_device_id ish_pci_tbl[] = {
  42. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CHV)},
  43. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Ax)},
  44. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_BXT_Bx)},
  45. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_APL_Ax)},
  46. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_Ax)},
  47. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_Ax)},
  48. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_GLK_Ax)},
  49. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CNL_H)},
  50. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ICL_MOBILE)},
  51. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_SPT_H)},
  52. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CML_LP)},
  53. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_CMP_H)},
  54. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)},
  55. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_LP)},
  56. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_TGL_H)},
  57. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_S)},
  58. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_P)},
  59. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ADL_N)},
  60. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_RPL_S)},
  61. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_MTL_P)},
  62. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_H)},
  63. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_ARL_S)},
  64. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_LNL_M), .driver_data = ISHTP_DRIVER_DATA_LNL_M},
  65. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_PTL_H), .driver_data = ISHTP_DRIVER_DATA_PTL},
  66. {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ISH_PTL_P), .driver_data = ISHTP_DRIVER_DATA_PTL},
  67. {}
  68. };
  69. MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
  70. /**
  71. * ish_event_tracer() - Callback function to dump trace messages
  72. * @dev: ishtp device
  73. * @format: printf style format
  74. *
  75. * Callback to direct log messages to Linux trace buffers
  76. */
  77. static __printf(2, 3)
  78. void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
  79. {
  80. if (trace_ishtp_dump_enabled()) {
  81. va_list args;
  82. char tmp_buf[100];
  83. va_start(args, format);
  84. vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
  85. va_end(args);
  86. trace_ishtp_dump(tmp_buf);
  87. }
  88. }
  89. /**
  90. * ish_init() - Init function
  91. * @dev: ishtp device
  92. *
  93. * This function initialize wait queues for suspend/resume and call
  94. * calls hadware initialization function. This will initiate
  95. * startup sequence
  96. *
  97. * Return: 0 for success or error code for failure
  98. */
  99. static int ish_init(struct ishtp_device *dev)
  100. {
  101. int ret;
  102. /* Set the state of ISH HW to start */
  103. ret = ish_hw_start(dev);
  104. if (ret) {
  105. dev_err(dev->devc, "ISH: hw start failed.\n");
  106. return ret;
  107. }
  108. /* Start the inter process communication to ISH processor */
  109. ret = ishtp_start(dev);
  110. if (ret) {
  111. dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
  112. return ret;
  113. }
  114. return 0;
  115. }
  116. static const struct pci_device_id ish_invalid_pci_ids[] = {
  117. /* Mehlow platform special pci ids */
  118. {PCI_VDEVICE(INTEL, 0xA309)},
  119. {PCI_VDEVICE(INTEL, 0xA30A)},
  120. {}
  121. };
  122. static inline bool ish_should_enter_d0i3(struct pci_dev *pdev)
  123. {
  124. return !pm_suspend_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV;
  125. }
  126. static inline bool ish_should_leave_d0i3(struct pci_dev *pdev)
  127. {
  128. return !pm_resume_via_firmware() || pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV;
  129. }
  130. /**
  131. * ish_probe() - PCI driver probe callback
  132. * @pdev: pci device
  133. * @ent: pci device id
  134. *
  135. * Initialize PCI function, setup interrupt and call for ISH initialization
  136. *
  137. * Return: 0 for success or error code for failure
  138. */
  139. static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  140. {
  141. int ret;
  142. struct ish_hw *hw;
  143. unsigned long irq_flag = 0;
  144. struct ishtp_device *ishtp;
  145. struct device *dev = &pdev->dev;
  146. /* Check for invalid platforms for ISH support */
  147. if (pci_dev_present(ish_invalid_pci_ids))
  148. return -ENODEV;
  149. /* enable pci dev */
  150. ret = pcim_enable_device(pdev);
  151. if (ret) {
  152. dev_err(dev, "ISH: Failed to enable PCI device\n");
  153. return ret;
  154. }
  155. /* set PCI host mastering */
  156. pci_set_master(pdev);
  157. /* pci request regions for ISH driver */
  158. ret = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
  159. if (ret) {
  160. dev_err(dev, "ISH: Failed to get PCI regions\n");
  161. return ret;
  162. }
  163. /* allocates and initializes the ISH dev structure */
  164. ishtp = ish_dev_init(pdev);
  165. if (!ishtp) {
  166. ret = -ENOMEM;
  167. return ret;
  168. }
  169. hw = to_ish_hw(ishtp);
  170. ishtp->print_log = ish_event_tracer;
  171. ishtp->driver_data = &ishtp_driver_data[ent->driver_data];
  172. /* mapping IO device memory */
  173. hw->mem_addr = pcim_iomap_table(pdev)[0];
  174. ishtp->pdev = pdev;
  175. /* request and enable interrupt */
  176. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
  177. if (ret < 0) {
  178. dev_err(dev, "ISH: Failed to allocate IRQ vectors\n");
  179. return ret;
  180. }
  181. if (!pdev->msi_enabled && !pdev->msix_enabled)
  182. irq_flag = IRQF_SHARED;
  183. ret = devm_request_irq(dev, pdev->irq, ish_irq_handler,
  184. irq_flag, KBUILD_MODNAME, ishtp);
  185. if (ret) {
  186. dev_err(dev, "ISH: request IRQ %d failed\n", pdev->irq);
  187. return ret;
  188. }
  189. dev_set_drvdata(ishtp->devc, ishtp);
  190. init_waitqueue_head(&ishtp->suspend_wait);
  191. init_waitqueue_head(&ishtp->resume_wait);
  192. /* Enable PME for EHL */
  193. if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)
  194. device_init_wakeup(dev, true);
  195. ret = ish_init(ishtp);
  196. if (ret)
  197. return ret;
  198. return 0;
  199. }
  200. /**
  201. * ish_remove() - PCI driver remove callback
  202. * @pdev: pci device
  203. *
  204. * This function does cleanup of ISH on pci remove callback
  205. */
  206. static void ish_remove(struct pci_dev *pdev)
  207. {
  208. struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
  209. ishtp_bus_remove_all_clients(ishtp_dev, false);
  210. ish_device_disable(ishtp_dev);
  211. }
  212. /**
  213. * ish_shutdown() - PCI driver shutdown callback
  214. * @pdev: pci device
  215. *
  216. * This function sets up wakeup for S5
  217. */
  218. static void ish_shutdown(struct pci_dev *pdev)
  219. {
  220. if (pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax)
  221. pci_prepare_to_sleep(pdev);
  222. }
  223. static struct device __maybe_unused *ish_resume_device;
  224. /* 50ms to get resume response */
  225. #define WAIT_FOR_RESUME_ACK_MS 50
  226. /**
  227. * ish_resume_handler() - Work function to complete resume
  228. * @work: work struct
  229. *
  230. * The resume work function to complete resume function asynchronously.
  231. * There are two resume paths, one where ISH is not powered off,
  232. * in that case a simple resume message is enough, others we need
  233. * a reset sequence.
  234. */
  235. static void __maybe_unused ish_resume_handler(struct work_struct *work)
  236. {
  237. struct pci_dev *pdev = to_pci_dev(ish_resume_device);
  238. struct ishtp_device *dev = pci_get_drvdata(pdev);
  239. uint32_t fwsts = dev->ops->get_fw_status(dev);
  240. if (ish_should_leave_d0i3(pdev) && !dev->suspend_flag
  241. && IPC_IS_ISH_ILUP(fwsts)) {
  242. if (device_may_wakeup(&pdev->dev))
  243. disable_irq_wake(pdev->irq);
  244. ish_set_host_ready(dev);
  245. ishtp_send_resume(dev);
  246. /* Waiting to get resume response */
  247. if (dev->resume_flag)
  248. wait_event_interruptible_timeout(dev->resume_wait,
  249. !dev->resume_flag,
  250. msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
  251. /*
  252. * If the flag is not cleared, something is wrong with ISH FW.
  253. * So on resume, need to go through init sequence again.
  254. */
  255. if (dev->resume_flag)
  256. ish_init(dev);
  257. } else {
  258. /*
  259. * Resume from the D3, full reboot of ISH processor will happen,
  260. * so need to go through init sequence again.
  261. */
  262. ish_init(dev);
  263. }
  264. }
  265. /**
  266. * ish_suspend() - ISH suspend callback
  267. * @device: device pointer
  268. *
  269. * ISH suspend callback
  270. *
  271. * Return: 0 to the pm core
  272. */
  273. static int __maybe_unused ish_suspend(struct device *device)
  274. {
  275. struct pci_dev *pdev = to_pci_dev(device);
  276. struct ishtp_device *dev = pci_get_drvdata(pdev);
  277. if (ish_should_enter_d0i3(pdev)) {
  278. /*
  279. * If previous suspend hasn't been asnwered then ISH is likely
  280. * dead, don't attempt nested notification
  281. */
  282. if (dev->suspend_flag)
  283. return 0;
  284. dev->resume_flag = 0;
  285. dev->suspend_flag = 1;
  286. ishtp_send_suspend(dev);
  287. /* 25 ms should be enough for live ISH to flush all IPC buf */
  288. if (dev->suspend_flag)
  289. wait_event_interruptible_timeout(dev->suspend_wait,
  290. !dev->suspend_flag,
  291. msecs_to_jiffies(25));
  292. if (dev->suspend_flag) {
  293. /*
  294. * It looks like FW halt, clear the DMA bit, and put
  295. * ISH into D3, and FW would reset on resume.
  296. */
  297. ish_disable_dma(dev);
  298. } else {
  299. /*
  300. * Save state so PCI core will keep the device at D0,
  301. * the ISH would enter D0i3
  302. */
  303. pci_save_state(pdev);
  304. if (device_may_wakeup(&pdev->dev))
  305. enable_irq_wake(pdev->irq);
  306. }
  307. } else {
  308. /*
  309. * Clear the DMA bit before putting ISH into D3,
  310. * or ISH FW would reset automatically.
  311. */
  312. ish_disable_dma(dev);
  313. }
  314. return 0;
  315. }
  316. static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
  317. /**
  318. * ish_resume() - ISH resume callback
  319. * @device: device pointer
  320. *
  321. * ISH resume callback
  322. *
  323. * Return: 0 to the pm core
  324. */
  325. static int __maybe_unused ish_resume(struct device *device)
  326. {
  327. struct pci_dev *pdev = to_pci_dev(device);
  328. struct ishtp_device *dev = pci_get_drvdata(pdev);
  329. ish_resume_device = device;
  330. dev->resume_flag = 1;
  331. schedule_work(&resume_work);
  332. return 0;
  333. }
  334. static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
  335. static struct pci_driver ish_driver = {
  336. .name = KBUILD_MODNAME,
  337. .id_table = ish_pci_tbl,
  338. .probe = ish_probe,
  339. .remove = ish_remove,
  340. .shutdown = ish_shutdown,
  341. .driver.pm = &ish_pm_ops,
  342. };
  343. module_pci_driver(ish_driver);
  344. /* Original author */
  345. MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
  346. /* Adoption to upstream Linux kernel */
  347. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  348. MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
  349. MODULE_LICENSE("GPL");
  350. MODULE_FIRMWARE(ISH_FIRMWARE_PATH(ISH_FW_GEN_LNL_M));
  351. MODULE_FIRMWARE(ISH_FIRMWARE_PATH_ALL);