qaic_timesync.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved. */
  3. #include <linux/io.h>
  4. #include <linux/kernel.h>
  5. #include <linux/math64.h>
  6. #include <linux/mhi.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/time64.h>
  10. #include <linux/timer.h>
  11. #include "qaic.h"
  12. #include "qaic_timesync.h"
  13. #define QTIMER_REG_OFFSET 0xa28
  14. #define QAIC_TIMESYNC_SIGNATURE 0x55aa
  15. #define QAIC_CONV_QTIMER_TO_US(qtimer) (mul_u64_u32_div(qtimer, 10, 192))
  16. static unsigned int timesync_delay_ms = 1000; /* 1 sec default */
  17. module_param(timesync_delay_ms, uint, 0600);
  18. MODULE_PARM_DESC(timesync_delay_ms, "Delay in ms between two consecutive timesync operations");
  19. enum qts_msg_type {
  20. QAIC_TS_CMD_TO_HOST,
  21. QAIC_TS_SYNC_REQ,
  22. QAIC_TS_ACK_TO_HOST,
  23. QAIC_TS_MSG_TYPE_MAX
  24. };
  25. /**
  26. * struct qts_hdr - Timesync message header structure.
  27. * @signature: Unique signature to identify the timesync message.
  28. * @reserved_1: Reserved for future use.
  29. * @reserved_2: Reserved for future use.
  30. * @msg_type: sub-type of the timesync message.
  31. * @reserved_3: Reserved for future use.
  32. */
  33. struct qts_hdr {
  34. __le16 signature;
  35. __le16 reserved_1;
  36. u8 reserved_2;
  37. u8 msg_type;
  38. __le16 reserved_3;
  39. } __packed;
  40. /**
  41. * struct qts_timeval - Structure to carry time information.
  42. * @tv_sec: Seconds part of the time.
  43. * @tv_usec: uS (microseconds) part of the time.
  44. */
  45. struct qts_timeval {
  46. __le64 tv_sec;
  47. __le64 tv_usec;
  48. } __packed;
  49. /**
  50. * struct qts_host_time_sync_msg_data - Structure to denote the timesync message.
  51. * @header: Header of the timesync message.
  52. * @data: Time information.
  53. */
  54. struct qts_host_time_sync_msg_data {
  55. struct qts_hdr header;
  56. struct qts_timeval data;
  57. } __packed;
  58. /**
  59. * struct mqts_dev - MHI QAIC Timesync Control device.
  60. * @qdev: Pointer to the root device struct driven by QAIC driver.
  61. * @mhi_dev: Pointer to associated MHI device.
  62. * @timer: Timer handle used for timesync.
  63. * @qtimer_addr: Device QTimer register pointer.
  64. * @buff_in_use: atomic variable to track if the sync_msg buffer is in use.
  65. * @dev: Device pointer to qdev->pdev->dev stored for easy access.
  66. * @sync_msg: Buffer used to send timesync message over MHI.
  67. */
  68. struct mqts_dev {
  69. struct qaic_device *qdev;
  70. struct mhi_device *mhi_dev;
  71. struct timer_list timer;
  72. void __iomem *qtimer_addr;
  73. atomic_t buff_in_use;
  74. struct device *dev;
  75. struct qts_host_time_sync_msg_data *sync_msg;
  76. };
  77. struct qts_resp_msg {
  78. struct qts_hdr hdr;
  79. } __packed;
  80. struct qts_resp {
  81. struct qts_resp_msg data;
  82. struct work_struct work;
  83. struct qaic_device *qdev;
  84. };
  85. #ifdef readq
  86. static u64 read_qtimer(const volatile void __iomem *addr)
  87. {
  88. return readq(addr);
  89. }
  90. #else
  91. static u64 read_qtimer(const volatile void __iomem *addr)
  92. {
  93. u64 low, high;
  94. low = readl(addr);
  95. high = readl(addr + sizeof(u32));
  96. return low | (high << 32);
  97. }
  98. #endif
  99. static void qaic_timesync_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  100. {
  101. struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev);
  102. dev_dbg(mqtsdev->dev, "%s status: %d xfer_len: %zu\n", __func__,
  103. mhi_result->transaction_status, mhi_result->bytes_xferd);
  104. atomic_set(&mqtsdev->buff_in_use, 0);
  105. }
  106. static void qaic_timesync_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  107. {
  108. struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev);
  109. dev_err(mqtsdev->dev, "%s no data expected on dl channel\n", __func__);
  110. }
  111. static void qaic_timesync_timer(struct timer_list *t)
  112. {
  113. struct mqts_dev *mqtsdev = from_timer(mqtsdev, t, timer);
  114. struct qts_host_time_sync_msg_data *sync_msg;
  115. u64 device_qtimer_us;
  116. u64 device_qtimer;
  117. u64 host_time_us;
  118. u64 offset_us;
  119. u64 host_sec;
  120. int ret;
  121. if (atomic_read(&mqtsdev->buff_in_use)) {
  122. dev_dbg(mqtsdev->dev, "%s buffer not free, schedule next cycle\n", __func__);
  123. goto mod_timer;
  124. }
  125. atomic_set(&mqtsdev->buff_in_use, 1);
  126. sync_msg = mqtsdev->sync_msg;
  127. sync_msg->header.signature = cpu_to_le16(QAIC_TIMESYNC_SIGNATURE);
  128. sync_msg->header.msg_type = QAIC_TS_SYNC_REQ;
  129. /* Read host UTC time and convert to uS*/
  130. host_time_us = div_u64(ktime_get_real_ns(), NSEC_PER_USEC);
  131. device_qtimer = read_qtimer(mqtsdev->qtimer_addr);
  132. device_qtimer_us = QAIC_CONV_QTIMER_TO_US(device_qtimer);
  133. /* Offset between host UTC and device time */
  134. offset_us = host_time_us - device_qtimer_us;
  135. host_sec = div_u64(offset_us, USEC_PER_SEC);
  136. sync_msg->data.tv_usec = cpu_to_le64(offset_us - host_sec * USEC_PER_SEC);
  137. sync_msg->data.tv_sec = cpu_to_le64(host_sec);
  138. ret = mhi_queue_buf(mqtsdev->mhi_dev, DMA_TO_DEVICE, sync_msg, sizeof(*sync_msg), MHI_EOT);
  139. if (ret && (ret != -EAGAIN)) {
  140. dev_err(mqtsdev->dev, "%s unable to queue to mhi:%d\n", __func__, ret);
  141. return;
  142. } else if (ret == -EAGAIN) {
  143. atomic_set(&mqtsdev->buff_in_use, 0);
  144. }
  145. mod_timer:
  146. ret = mod_timer(t, jiffies + msecs_to_jiffies(timesync_delay_ms));
  147. if (ret)
  148. dev_err(mqtsdev->dev, "%s mod_timer error:%d\n", __func__, ret);
  149. }
  150. static int qaic_timesync_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
  151. {
  152. struct qaic_device *qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev));
  153. struct mqts_dev *mqtsdev;
  154. struct timer_list *timer;
  155. int ret;
  156. mqtsdev = kzalloc(sizeof(*mqtsdev), GFP_KERNEL);
  157. if (!mqtsdev) {
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. timer = &mqtsdev->timer;
  162. mqtsdev->mhi_dev = mhi_dev;
  163. mqtsdev->qdev = qdev;
  164. mqtsdev->dev = &qdev->pdev->dev;
  165. mqtsdev->sync_msg = kzalloc(sizeof(*mqtsdev->sync_msg), GFP_KERNEL);
  166. if (!mqtsdev->sync_msg) {
  167. ret = -ENOMEM;
  168. goto free_mqts_dev;
  169. }
  170. atomic_set(&mqtsdev->buff_in_use, 0);
  171. ret = mhi_prepare_for_transfer(mhi_dev);
  172. if (ret)
  173. goto free_sync_msg;
  174. /* Qtimer register pointer */
  175. mqtsdev->qtimer_addr = qdev->bar_0 + QTIMER_REG_OFFSET;
  176. timer_setup(timer, qaic_timesync_timer, 0);
  177. timer->expires = jiffies + msecs_to_jiffies(timesync_delay_ms);
  178. add_timer(timer);
  179. dev_set_drvdata(&mhi_dev->dev, mqtsdev);
  180. return 0;
  181. free_sync_msg:
  182. kfree(mqtsdev->sync_msg);
  183. free_mqts_dev:
  184. kfree(mqtsdev);
  185. out:
  186. return ret;
  187. };
  188. static void qaic_timesync_remove(struct mhi_device *mhi_dev)
  189. {
  190. struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev);
  191. del_timer_sync(&mqtsdev->timer);
  192. mhi_unprepare_from_transfer(mqtsdev->mhi_dev);
  193. kfree(mqtsdev->sync_msg);
  194. kfree(mqtsdev);
  195. }
  196. static const struct mhi_device_id qaic_timesync_match_table[] = {
  197. { .chan = "QAIC_TIMESYNC_PERIODIC"},
  198. {},
  199. };
  200. MODULE_DEVICE_TABLE(mhi, qaic_timesync_match_table);
  201. static struct mhi_driver qaic_timesync_driver = {
  202. .id_table = qaic_timesync_match_table,
  203. .remove = qaic_timesync_remove,
  204. .probe = qaic_timesync_probe,
  205. .ul_xfer_cb = qaic_timesync_ul_xfer_cb,
  206. .dl_xfer_cb = qaic_timesync_dl_xfer_cb,
  207. .driver = {
  208. .name = "qaic_timesync_periodic",
  209. },
  210. };
  211. static void qaic_boot_timesync_worker(struct work_struct *work)
  212. {
  213. struct qts_resp *resp = container_of(work, struct qts_resp, work);
  214. struct qts_host_time_sync_msg_data *req;
  215. struct qts_resp_msg data = resp->data;
  216. struct qaic_device *qdev = resp->qdev;
  217. struct mhi_device *mhi_dev;
  218. struct timespec64 ts;
  219. int ret;
  220. mhi_dev = qdev->qts_ch;
  221. /* Queue the response message beforehand to avoid race conditions */
  222. ret = mhi_queue_buf(mhi_dev, DMA_FROM_DEVICE, &resp->data, sizeof(resp->data), MHI_EOT);
  223. if (ret) {
  224. kfree(resp);
  225. dev_warn(&mhi_dev->dev, "Failed to re-queue response buffer %d\n", ret);
  226. return;
  227. }
  228. switch (data.hdr.msg_type) {
  229. case QAIC_TS_CMD_TO_HOST:
  230. req = kzalloc(sizeof(*req), GFP_KERNEL);
  231. if (!req)
  232. break;
  233. req->header = data.hdr;
  234. req->header.msg_type = QAIC_TS_SYNC_REQ;
  235. ktime_get_real_ts64(&ts);
  236. req->data.tv_sec = cpu_to_le64(ts.tv_sec);
  237. req->data.tv_usec = cpu_to_le64(div_u64(ts.tv_nsec, NSEC_PER_USEC));
  238. ret = mhi_queue_buf(mhi_dev, DMA_TO_DEVICE, req, sizeof(*req), MHI_EOT);
  239. if (ret) {
  240. kfree(req);
  241. dev_dbg(&mhi_dev->dev, "Failed to send request message. Error %d\n", ret);
  242. }
  243. break;
  244. case QAIC_TS_ACK_TO_HOST:
  245. dev_dbg(&mhi_dev->dev, "ACK received from device\n");
  246. break;
  247. default:
  248. dev_err(&mhi_dev->dev, "Invalid message type %u.\n", data.hdr.msg_type);
  249. }
  250. }
  251. static int qaic_boot_timesync_queue_resp(struct mhi_device *mhi_dev, struct qaic_device *qdev)
  252. {
  253. struct qts_resp *resp;
  254. int ret;
  255. resp = kzalloc(sizeof(*resp), GFP_KERNEL);
  256. if (!resp)
  257. return -ENOMEM;
  258. resp->qdev = qdev;
  259. INIT_WORK(&resp->work, qaic_boot_timesync_worker);
  260. ret = mhi_queue_buf(mhi_dev, DMA_FROM_DEVICE, &resp->data, sizeof(resp->data), MHI_EOT);
  261. if (ret) {
  262. kfree(resp);
  263. dev_warn(&mhi_dev->dev, "Failed to queue response buffer %d\n", ret);
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static void qaic_boot_timesync_remove(struct mhi_device *mhi_dev)
  269. {
  270. struct qaic_device *qdev;
  271. qdev = dev_get_drvdata(&mhi_dev->dev);
  272. mhi_unprepare_from_transfer(qdev->qts_ch);
  273. qdev->qts_ch = NULL;
  274. }
  275. static int qaic_boot_timesync_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id)
  276. {
  277. struct qaic_device *qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev));
  278. int ret;
  279. ret = mhi_prepare_for_transfer(mhi_dev);
  280. if (ret)
  281. return ret;
  282. qdev->qts_ch = mhi_dev;
  283. dev_set_drvdata(&mhi_dev->dev, qdev);
  284. ret = qaic_boot_timesync_queue_resp(mhi_dev, qdev);
  285. if (ret) {
  286. dev_set_drvdata(&mhi_dev->dev, NULL);
  287. qdev->qts_ch = NULL;
  288. mhi_unprepare_from_transfer(mhi_dev);
  289. }
  290. return ret;
  291. }
  292. static void qaic_boot_timesync_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  293. {
  294. kfree(mhi_result->buf_addr);
  295. }
  296. static void qaic_boot_timesync_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
  297. {
  298. struct qts_resp *resp = container_of(mhi_result->buf_addr, struct qts_resp, data);
  299. if (mhi_result->transaction_status || mhi_result->bytes_xferd != sizeof(resp->data)) {
  300. kfree(resp);
  301. return;
  302. }
  303. queue_work(resp->qdev->qts_wq, &resp->work);
  304. }
  305. static const struct mhi_device_id qaic_boot_timesync_match_table[] = {
  306. { .chan = "QAIC_TIMESYNC"},
  307. {},
  308. };
  309. static struct mhi_driver qaic_boot_timesync_driver = {
  310. .id_table = qaic_boot_timesync_match_table,
  311. .remove = qaic_boot_timesync_remove,
  312. .probe = qaic_boot_timesync_probe,
  313. .ul_xfer_cb = qaic_boot_timesync_ul_xfer_cb,
  314. .dl_xfer_cb = qaic_boot_timesync_dl_xfer_cb,
  315. .driver = {
  316. .name = "qaic_timesync",
  317. },
  318. };
  319. int qaic_timesync_init(void)
  320. {
  321. int ret;
  322. ret = mhi_driver_register(&qaic_timesync_driver);
  323. if (ret)
  324. return ret;
  325. ret = mhi_driver_register(&qaic_boot_timesync_driver);
  326. return ret;
  327. }
  328. void qaic_timesync_deinit(void)
  329. {
  330. mhi_driver_unregister(&qaic_boot_timesync_driver);
  331. mhi_driver_unregister(&qaic_timesync_driver);
  332. }