pci-ish.c 7.6 KB

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