gsc-me.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(c) 2019-2022, Intel Corporation. All rights reserved.
  4. *
  5. * Intel Management Engine Interface (Intel MEI) Linux driver
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mei_aux.h>
  9. #include <linux/device.h>
  10. #include <linux/irqreturn.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/ktime.h>
  13. #include <linux/delay.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/kthread.h>
  16. #include "mei_dev.h"
  17. #include "hw-me.h"
  18. #include "hw-me-regs.h"
  19. #include "mei-trace.h"
  20. #define MEI_GSC_RPM_TIMEOUT 500
  21. static int mei_gsc_read_hfs(const struct mei_device *dev, int where, u32 *val)
  22. {
  23. struct mei_me_hw *hw = to_me_hw(dev);
  24. *val = ioread32(hw->mem_addr + where + 0xC00);
  25. return 0;
  26. }
  27. static void mei_gsc_set_ext_op_mem(const struct mei_me_hw *hw, struct resource *mem)
  28. {
  29. u32 low = lower_32_bits(mem->start);
  30. u32 hi = upper_32_bits(mem->start);
  31. u32 limit = (resource_size(mem) / SZ_4K) | GSC_EXT_OP_MEM_VALID;
  32. iowrite32(low, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_LO_REG);
  33. iowrite32(hi, hw->mem_addr + H_GSC_EXT_OP_MEM_BASE_ADDR_HI_REG);
  34. iowrite32(limit, hw->mem_addr + H_GSC_EXT_OP_MEM_LIMIT_REG);
  35. }
  36. static int mei_gsc_probe(struct auxiliary_device *aux_dev,
  37. const struct auxiliary_device_id *aux_dev_id)
  38. {
  39. struct mei_aux_device *adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
  40. struct mei_device *dev;
  41. struct mei_me_hw *hw;
  42. struct device *device;
  43. const struct mei_cfg *cfg;
  44. int ret;
  45. cfg = mei_me_get_cfg(aux_dev_id->driver_data);
  46. if (!cfg)
  47. return -ENODEV;
  48. device = &aux_dev->dev;
  49. dev = mei_me_dev_init(device, cfg, adev->slow_firmware);
  50. if (!dev) {
  51. ret = -ENOMEM;
  52. goto err;
  53. }
  54. hw = to_me_hw(dev);
  55. hw->mem_addr = devm_ioremap_resource(device, &adev->bar);
  56. if (IS_ERR(hw->mem_addr)) {
  57. ret = PTR_ERR(hw->mem_addr);
  58. goto err;
  59. }
  60. hw->irq = adev->irq;
  61. hw->read_fws = mei_gsc_read_hfs;
  62. dev_set_drvdata(device, dev);
  63. if (adev->ext_op_mem.start) {
  64. mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
  65. dev->pxp_mode = MEI_DEV_PXP_INIT;
  66. }
  67. /* use polling */
  68. if (mei_me_hw_use_polling(hw)) {
  69. mei_disable_interrupts(dev);
  70. mei_clear_interrupts(dev);
  71. init_waitqueue_head(&hw->wait_active);
  72. hw->is_active = true; /* start in active mode for initialization */
  73. hw->polling_thread = kthread_run(mei_me_polling_thread, dev,
  74. "kmegscirqd/%s", dev_name(device));
  75. if (IS_ERR(hw->polling_thread)) {
  76. ret = PTR_ERR(hw->polling_thread);
  77. dev_err(device, "unable to create kernel thread: %d\n", ret);
  78. goto err;
  79. }
  80. } else {
  81. ret = devm_request_threaded_irq(device, hw->irq,
  82. mei_me_irq_quick_handler,
  83. mei_me_irq_thread_handler,
  84. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  85. if (ret) {
  86. dev_err(device, "irq register failed %d\n", ret);
  87. goto err;
  88. }
  89. }
  90. pm_runtime_get_noresume(device);
  91. pm_runtime_set_active(device);
  92. pm_runtime_enable(device);
  93. /* Continue to char device setup in spite of firmware handshake failure.
  94. * In order to provide access to the firmware status registers to the user
  95. * space via sysfs.
  96. */
  97. if (mei_start(dev))
  98. dev_warn(device, "init hw failure.\n");
  99. pm_runtime_set_autosuspend_delay(device, MEI_GSC_RPM_TIMEOUT);
  100. pm_runtime_use_autosuspend(device);
  101. ret = mei_register(dev, device);
  102. if (ret)
  103. goto register_err;
  104. pm_runtime_put_noidle(device);
  105. return 0;
  106. register_err:
  107. mei_stop(dev);
  108. if (!mei_me_hw_use_polling(hw))
  109. devm_free_irq(device, hw->irq, dev);
  110. err:
  111. dev_err(device, "probe failed: %d\n", ret);
  112. dev_set_drvdata(device, NULL);
  113. return ret;
  114. }
  115. static void mei_gsc_remove(struct auxiliary_device *aux_dev)
  116. {
  117. struct mei_device *dev;
  118. struct mei_me_hw *hw;
  119. dev = dev_get_drvdata(&aux_dev->dev);
  120. if (!dev)
  121. return;
  122. hw = to_me_hw(dev);
  123. mei_stop(dev);
  124. hw = to_me_hw(dev);
  125. if (mei_me_hw_use_polling(hw))
  126. kthread_stop(hw->polling_thread);
  127. mei_deregister(dev);
  128. pm_runtime_disable(&aux_dev->dev);
  129. mei_disable_interrupts(dev);
  130. if (!mei_me_hw_use_polling(hw))
  131. devm_free_irq(&aux_dev->dev, hw->irq, dev);
  132. }
  133. static int __maybe_unused mei_gsc_pm_suspend(struct device *device)
  134. {
  135. struct mei_device *dev = dev_get_drvdata(device);
  136. if (!dev)
  137. return -ENODEV;
  138. mei_stop(dev);
  139. mei_disable_interrupts(dev);
  140. return 0;
  141. }
  142. static int __maybe_unused mei_gsc_pm_resume(struct device *device)
  143. {
  144. struct mei_device *dev = dev_get_drvdata(device);
  145. struct auxiliary_device *aux_dev;
  146. struct mei_aux_device *adev;
  147. int err;
  148. struct mei_me_hw *hw;
  149. if (!dev)
  150. return -ENODEV;
  151. hw = to_me_hw(dev);
  152. aux_dev = to_auxiliary_dev(device);
  153. adev = auxiliary_dev_to_mei_aux_dev(aux_dev);
  154. if (adev->ext_op_mem.start) {
  155. mei_gsc_set_ext_op_mem(hw, &adev->ext_op_mem);
  156. dev->pxp_mode = MEI_DEV_PXP_INIT;
  157. }
  158. err = mei_restart(dev);
  159. if (err)
  160. return err;
  161. /* Start timer if stopped in suspend */
  162. schedule_delayed_work(&dev->timer_work, HZ);
  163. return 0;
  164. }
  165. static int __maybe_unused mei_gsc_pm_runtime_idle(struct device *device)
  166. {
  167. struct mei_device *dev = dev_get_drvdata(device);
  168. if (!dev)
  169. return -ENODEV;
  170. if (mei_write_is_idle(dev))
  171. pm_runtime_autosuspend(device);
  172. return -EBUSY;
  173. }
  174. static int __maybe_unused mei_gsc_pm_runtime_suspend(struct device *device)
  175. {
  176. struct mei_device *dev = dev_get_drvdata(device);
  177. struct mei_me_hw *hw;
  178. int ret;
  179. if (!dev)
  180. return -ENODEV;
  181. mutex_lock(&dev->device_lock);
  182. if (mei_write_is_idle(dev)) {
  183. hw = to_me_hw(dev);
  184. hw->pg_state = MEI_PG_ON;
  185. if (mei_me_hw_use_polling(hw))
  186. hw->is_active = false;
  187. ret = 0;
  188. } else {
  189. ret = -EAGAIN;
  190. }
  191. mutex_unlock(&dev->device_lock);
  192. return ret;
  193. }
  194. static int __maybe_unused mei_gsc_pm_runtime_resume(struct device *device)
  195. {
  196. struct mei_device *dev = dev_get_drvdata(device);
  197. struct mei_me_hw *hw;
  198. irqreturn_t irq_ret;
  199. if (!dev)
  200. return -ENODEV;
  201. mutex_lock(&dev->device_lock);
  202. hw = to_me_hw(dev);
  203. hw->pg_state = MEI_PG_OFF;
  204. if (mei_me_hw_use_polling(hw)) {
  205. hw->is_active = true;
  206. wake_up(&hw->wait_active);
  207. }
  208. mutex_unlock(&dev->device_lock);
  209. irq_ret = mei_me_irq_thread_handler(1, dev);
  210. if (irq_ret != IRQ_HANDLED)
  211. dev_err(dev->dev, "thread handler fail %d\n", irq_ret);
  212. return 0;
  213. }
  214. static const struct dev_pm_ops mei_gsc_pm_ops = {
  215. SET_SYSTEM_SLEEP_PM_OPS(mei_gsc_pm_suspend,
  216. mei_gsc_pm_resume)
  217. SET_RUNTIME_PM_OPS(mei_gsc_pm_runtime_suspend,
  218. mei_gsc_pm_runtime_resume,
  219. mei_gsc_pm_runtime_idle)
  220. };
  221. static const struct auxiliary_device_id mei_gsc_id_table[] = {
  222. {
  223. .name = "i915.mei-gsc",
  224. .driver_data = MEI_ME_GSC_CFG,
  225. },
  226. {
  227. .name = "i915.mei-gscfi",
  228. .driver_data = MEI_ME_GSCFI_CFG,
  229. },
  230. {
  231. /* sentinel */
  232. }
  233. };
  234. MODULE_DEVICE_TABLE(auxiliary, mei_gsc_id_table);
  235. static struct auxiliary_driver mei_gsc_driver = {
  236. .probe = mei_gsc_probe,
  237. .remove = mei_gsc_remove,
  238. .driver = {
  239. /* auxiliary_driver_register() sets .name to be the modname */
  240. .pm = &mei_gsc_pm_ops,
  241. },
  242. .id_table = mei_gsc_id_table
  243. };
  244. module_auxiliary_driver(mei_gsc_driver);
  245. MODULE_AUTHOR("Intel Corporation");
  246. MODULE_ALIAS("auxiliary:i915.mei-gsc");
  247. MODULE_ALIAS("auxiliary:i915.mei-gscfi");
  248. MODULE_LICENSE("GPL");