pci-ish.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * PCI glue for ISHTP provider device (ISH) driver
  3. *
  4. * Copyright (c) 2014-2016, 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. #include <linux/module.h>
  16. #include <linux/moduleparam.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/sched.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/workqueue.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/intel_ish.h>
  28. #include "ishtp-dev.h"
  29. #include "hw-ish.h"
  30. static const struct pci_device_id ish_pci_tbl[] = {
  31. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CHV_DEVICE_ID)},
  32. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Ax_DEVICE_ID)},
  33. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, BXT_Bx_DEVICE_ID)},
  34. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, APL_Ax_DEVICE_ID)},
  35. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_Ax_DEVICE_ID)},
  36. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_Ax_DEVICE_ID)},
  37. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, GLK_Ax_DEVICE_ID)},
  38. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, CNL_H_DEVICE_ID)},
  39. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, ICL_MOBILE_DEVICE_ID)},
  40. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, SPT_H_DEVICE_ID)},
  41. {0, }
  42. };
  43. MODULE_DEVICE_TABLE(pci, ish_pci_tbl);
  44. /**
  45. * ish_event_tracer() - Callback function to dump trace messages
  46. * @dev: ishtp device
  47. * @format: printf style format
  48. *
  49. * Callback to direct log messages to Linux trace buffers
  50. */
  51. static __printf(2, 3)
  52. void ish_event_tracer(struct ishtp_device *dev, const char *format, ...)
  53. {
  54. if (trace_ishtp_dump_enabled()) {
  55. va_list args;
  56. char tmp_buf[100];
  57. va_start(args, format);
  58. vsnprintf(tmp_buf, sizeof(tmp_buf), format, args);
  59. va_end(args);
  60. trace_ishtp_dump(tmp_buf);
  61. }
  62. }
  63. /**
  64. * ish_init() - Init function
  65. * @dev: ishtp device
  66. *
  67. * This function initialize wait queues for suspend/resume and call
  68. * calls hadware initialization function. This will initiate
  69. * startup sequence
  70. *
  71. * Return: 0 for success or error code for failure
  72. */
  73. static int ish_init(struct ishtp_device *dev)
  74. {
  75. int ret;
  76. /* Set the state of ISH HW to start */
  77. ret = ish_hw_start(dev);
  78. if (ret) {
  79. dev_err(dev->devc, "ISH: hw start failed.\n");
  80. return ret;
  81. }
  82. /* Start the inter process communication to ISH processor */
  83. ret = ishtp_start(dev);
  84. if (ret) {
  85. dev_err(dev->devc, "ISHTP: Protocol init failed.\n");
  86. return ret;
  87. }
  88. return 0;
  89. }
  90. static const struct pci_device_id ish_invalid_pci_ids[] = {
  91. /* Mehlow platform special pci ids */
  92. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA309)},
  93. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xA30A)},
  94. {}
  95. };
  96. /**
  97. * ish_probe() - PCI driver probe callback
  98. * @pdev: pci device
  99. * @ent: pci device id
  100. *
  101. * Initialize PCI function, setup interrupt and call for ISH initialization
  102. *
  103. * Return: 0 for success or error code for failure
  104. */
  105. static int ish_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  106. {
  107. struct ishtp_device *dev;
  108. struct ish_hw *hw;
  109. int ret;
  110. /* Check for invalid platforms for ISH support */
  111. if (pci_dev_present(ish_invalid_pci_ids))
  112. return -ENODEV;
  113. /* enable pci dev */
  114. ret = pci_enable_device(pdev);
  115. if (ret) {
  116. dev_err(&pdev->dev, "ISH: Failed to enable PCI device\n");
  117. return ret;
  118. }
  119. /* set PCI host mastering */
  120. pci_set_master(pdev);
  121. /* pci request regions for ISH driver */
  122. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  123. if (ret) {
  124. dev_err(&pdev->dev, "ISH: Failed to get PCI regions\n");
  125. goto disable_device;
  126. }
  127. /* allocates and initializes the ISH dev structure */
  128. dev = ish_dev_init(pdev);
  129. if (!dev) {
  130. ret = -ENOMEM;
  131. goto release_regions;
  132. }
  133. hw = to_ish_hw(dev);
  134. dev->print_log = ish_event_tracer;
  135. /* mapping IO device memory */
  136. hw->mem_addr = pci_iomap(pdev, 0, 0);
  137. if (!hw->mem_addr) {
  138. dev_err(&pdev->dev, "ISH: mapping I/O range failure\n");
  139. ret = -ENOMEM;
  140. goto free_device;
  141. }
  142. dev->pdev = pdev;
  143. pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
  144. /* request and enable interrupt */
  145. ret = request_irq(pdev->irq, ish_irq_handler, IRQF_SHARED,
  146. KBUILD_MODNAME, dev);
  147. if (ret) {
  148. dev_err(&pdev->dev, "ISH: request IRQ failure (%d)\n",
  149. pdev->irq);
  150. goto free_device;
  151. }
  152. dev_set_drvdata(dev->devc, dev);
  153. init_waitqueue_head(&dev->suspend_wait);
  154. init_waitqueue_head(&dev->resume_wait);
  155. ret = ish_init(dev);
  156. if (ret)
  157. goto free_irq;
  158. return 0;
  159. free_irq:
  160. free_irq(pdev->irq, dev);
  161. free_device:
  162. pci_iounmap(pdev, hw->mem_addr);
  163. release_regions:
  164. pci_release_regions(pdev);
  165. disable_device:
  166. pci_clear_master(pdev);
  167. pci_disable_device(pdev);
  168. dev_err(&pdev->dev, "ISH: PCI driver initialization failed.\n");
  169. return ret;
  170. }
  171. /**
  172. * ish_remove() - PCI driver remove callback
  173. * @pdev: pci device
  174. *
  175. * This function does cleanup of ISH on pci remove callback
  176. */
  177. static void ish_remove(struct pci_dev *pdev)
  178. {
  179. struct ishtp_device *ishtp_dev = pci_get_drvdata(pdev);
  180. struct ish_hw *hw = to_ish_hw(ishtp_dev);
  181. ishtp_bus_remove_all_clients(ishtp_dev, false);
  182. ish_device_disable(ishtp_dev);
  183. free_irq(pdev->irq, ishtp_dev);
  184. pci_iounmap(pdev, hw->mem_addr);
  185. pci_release_regions(pdev);
  186. pci_clear_master(pdev);
  187. pci_disable_device(pdev);
  188. }
  189. static struct device __maybe_unused *ish_resume_device;
  190. /* 50ms to get resume response */
  191. #define WAIT_FOR_RESUME_ACK_MS 50
  192. /**
  193. * ish_resume_handler() - Work function to complete resume
  194. * @work: work struct
  195. *
  196. * The resume work function to complete resume function asynchronously.
  197. * There are two resume paths, one where ISH is not powered off,
  198. * in that case a simple resume message is enough, others we need
  199. * a reset sequence.
  200. */
  201. static void __maybe_unused ish_resume_handler(struct work_struct *work)
  202. {
  203. struct pci_dev *pdev = to_pci_dev(ish_resume_device);
  204. struct ishtp_device *dev = pci_get_drvdata(pdev);
  205. uint32_t fwsts;
  206. int ret;
  207. /* Get ISH FW status */
  208. fwsts = IPC_GET_ISH_FWSTS(dev->ops->get_fw_status(dev));
  209. /*
  210. * If currently, in ISH FW, sensor app is loaded or beyond that,
  211. * it means ISH isn't powered off, in this case, send a resume message.
  212. */
  213. if (fwsts >= FWSTS_SENSOR_APP_LOADED) {
  214. ishtp_send_resume(dev);
  215. /* Waiting to get resume response */
  216. if (dev->resume_flag)
  217. ret = wait_event_interruptible_timeout(dev->resume_wait,
  218. !dev->resume_flag,
  219. msecs_to_jiffies(WAIT_FOR_RESUME_ACK_MS));
  220. }
  221. /*
  222. * If in ISH FW, sensor app isn't loaded yet, or no resume response.
  223. * That means this platform is not S0ix compatible, or something is
  224. * wrong with ISH FW. So on resume, full reboot of ISH processor will
  225. * happen, so need to go through init sequence again.
  226. */
  227. if (dev->resume_flag)
  228. ish_init(dev);
  229. }
  230. /**
  231. * ish_suspend() - ISH suspend callback
  232. * @device: device pointer
  233. *
  234. * ISH suspend callback
  235. *
  236. * Return: 0 to the pm core
  237. */
  238. static int __maybe_unused ish_suspend(struct device *device)
  239. {
  240. struct pci_dev *pdev = to_pci_dev(device);
  241. struct ishtp_device *dev = pci_get_drvdata(pdev);
  242. enable_irq_wake(pdev->irq);
  243. /*
  244. * If previous suspend hasn't been asnwered then ISH is likely dead,
  245. * don't attempt nested notification
  246. */
  247. if (dev->suspend_flag)
  248. return 0;
  249. dev->resume_flag = 0;
  250. dev->suspend_flag = 1;
  251. ishtp_send_suspend(dev);
  252. /* 25 ms should be enough for live ISH to flush all IPC buf */
  253. if (dev->suspend_flag)
  254. wait_event_interruptible_timeout(dev->suspend_wait,
  255. !dev->suspend_flag,
  256. msecs_to_jiffies(25));
  257. return 0;
  258. }
  259. static __maybe_unused DECLARE_WORK(resume_work, ish_resume_handler);
  260. /**
  261. * ish_resume() - ISH resume callback
  262. * @device: device pointer
  263. *
  264. * ISH resume callback
  265. *
  266. * Return: 0 to the pm core
  267. */
  268. static int __maybe_unused ish_resume(struct device *device)
  269. {
  270. struct pci_dev *pdev = to_pci_dev(device);
  271. struct ishtp_device *dev = pci_get_drvdata(pdev);
  272. ish_resume_device = device;
  273. dev->resume_flag = 1;
  274. disable_irq_wake(pdev->irq);
  275. schedule_work(&resume_work);
  276. return 0;
  277. }
  278. static SIMPLE_DEV_PM_OPS(ish_pm_ops, ish_suspend, ish_resume);
  279. static struct pci_driver ish_driver = {
  280. .name = KBUILD_MODNAME,
  281. .id_table = ish_pci_tbl,
  282. .probe = ish_probe,
  283. .remove = ish_remove,
  284. .driver.pm = &ish_pm_ops,
  285. };
  286. module_pci_driver(ish_driver);
  287. /* Original author */
  288. MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
  289. /* Adoption to upstream Linux kernel */
  290. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  291. MODULE_DESCRIPTION("Intel(R) Integrated Sensor Hub PCI Device Driver");
  292. MODULE_LICENSE("GPL");