virtio_rtc_driver.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * virtio_rtc driver core
  4. *
  5. * Copyright (C) 2022-2024 OpenSynergy GmbH
  6. * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/device.h>
  10. #include <linux/module.h>
  11. #include <linux/pm.h>
  12. #include <linux/virtio.h>
  13. #include <linux/virtio_config.h>
  14. #include <linux/virtio_ids.h>
  15. #include <uapi/linux/virtio_rtc.h>
  16. #include "virtio_rtc_internal.h"
  17. #define VIORTC_ALARMQ_BUF_CAP sizeof(union virtio_rtc_notif_alarmq)
  18. /* virtqueue order */
  19. enum {
  20. VIORTC_REQUESTQ,
  21. VIORTC_ALARMQ,
  22. VIORTC_MAX_NR_QUEUES,
  23. };
  24. /**
  25. * struct viortc_vq - virtqueue abstraction
  26. * @vq: virtqueue
  27. * @lock: protects access to vq
  28. */
  29. struct viortc_vq {
  30. struct virtqueue *vq;
  31. spinlock_t lock;
  32. };
  33. /**
  34. * struct viortc_dev - virtio_rtc device data
  35. * @vdev: virtio device
  36. * @viortc_class: RTC class wrapper for UTC-like clock, NULL if not available
  37. * @vqs: virtqueues
  38. * @clocks_to_unregister: Clock references, which are only used during device
  39. * removal.
  40. * For other uses, there would be a race between device
  41. * creation and setting the pointers here.
  42. * @alarmq_bufs: alarmq buffers list
  43. * @num_alarmq_bufs: # of alarmq buffers
  44. * @num_clocks: # of virtio_rtc clocks
  45. */
  46. struct viortc_dev {
  47. struct virtio_device *vdev;
  48. struct viortc_class *viortc_class;
  49. struct viortc_vq vqs[VIORTC_MAX_NR_QUEUES];
  50. struct viortc_ptp_clock **clocks_to_unregister;
  51. void **alarmq_bufs;
  52. unsigned int num_alarmq_bufs;
  53. u16 num_clocks;
  54. };
  55. /**
  56. * struct viortc_msg - Message requested by driver, responded by device.
  57. * @viortc: device data
  58. * @req: request buffer
  59. * @resp: response buffer
  60. * @responded: vqueue callback signals response reception
  61. * @refcnt: Message reference count, message and buffers will be deallocated
  62. * once 0. refcnt is decremented in the vqueue callback and in the
  63. * thread waiting on the responded completion.
  64. * If a message response wait function times out, the message will be
  65. * freed upon late reception (refcnt will reach 0 in the callback), or
  66. * device removal.
  67. * @req_size: size of request in bytes
  68. * @resp_cap: maximum size of response in bytes
  69. * @resp_actual_size: actual size of response
  70. */
  71. struct viortc_msg {
  72. struct viortc_dev *viortc;
  73. void *req;
  74. void *resp;
  75. struct completion responded;
  76. refcount_t refcnt;
  77. unsigned int req_size;
  78. unsigned int resp_cap;
  79. unsigned int resp_actual_size;
  80. };
  81. /**
  82. * viortc_class_from_dev() - Get RTC class object from virtio device.
  83. * @dev: virtio device
  84. *
  85. * Context: Any context.
  86. * Return: RTC class object if available, ERR_PTR otherwise.
  87. */
  88. struct viortc_class *viortc_class_from_dev(struct device *dev)
  89. {
  90. struct virtio_device *vdev;
  91. struct viortc_dev *viortc;
  92. vdev = container_of(dev, typeof(*vdev), dev);
  93. viortc = vdev->priv;
  94. return viortc->viortc_class ?: ERR_PTR(-ENODEV);
  95. }
  96. /**
  97. * viortc_alarms_supported() - Whether device and driver support alarms.
  98. * @vdev: virtio device
  99. *
  100. * NB: Device and driver may not support alarms for the same clocks.
  101. *
  102. * Context: Any context.
  103. * Return: True if both device and driver can support alarms.
  104. */
  105. static bool viortc_alarms_supported(struct virtio_device *vdev)
  106. {
  107. return IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) &&
  108. virtio_has_feature(vdev, VIRTIO_RTC_F_ALARM);
  109. }
  110. /**
  111. * viortc_feed_vq() - Make a device write-only buffer available.
  112. * @viortc: device data
  113. * @vq: notification virtqueue
  114. * @buf: buffer
  115. * @buf_len: buffer capacity in bytes
  116. * @data: token, identifying buffer
  117. *
  118. * Context: Caller must prevent concurrent access to vq.
  119. * Return: Zero on success, negative error code otherwise.
  120. */
  121. static int viortc_feed_vq(struct viortc_dev *viortc, struct virtqueue *vq,
  122. void *buf, unsigned int buf_len, void *data)
  123. {
  124. struct scatterlist sg;
  125. sg_init_one(&sg, buf, buf_len);
  126. return virtqueue_add_inbuf(vq, &sg, 1, data, GFP_ATOMIC);
  127. }
  128. /**
  129. * viortc_msg_init() - Allocate and initialize requestq message.
  130. * @viortc: device data
  131. * @msg_type: virtio_rtc message type
  132. * @req_size: size of request buffer to be allocated
  133. * @resp_cap: size of response buffer to be allocated
  134. *
  135. * Initializes the message refcnt to 2. The refcnt will be decremented once in
  136. * the virtqueue callback, and once in the thread waiting on the message (on
  137. * completion or timeout).
  138. *
  139. * Context: Process context.
  140. * Return: non-NULL on success.
  141. */
  142. static struct viortc_msg *viortc_msg_init(struct viortc_dev *viortc,
  143. u16 msg_type, unsigned int req_size,
  144. unsigned int resp_cap)
  145. {
  146. struct device *dev = &viortc->vdev->dev;
  147. struct virtio_rtc_req_head *req_head;
  148. struct viortc_msg *msg;
  149. msg = devm_kzalloc(dev, sizeof(*msg), GFP_KERNEL);
  150. if (!msg)
  151. return NULL;
  152. init_completion(&msg->responded);
  153. msg->req = devm_kzalloc(dev, req_size, GFP_KERNEL);
  154. if (!msg->req)
  155. goto err_free_msg;
  156. req_head = msg->req;
  157. msg->resp = devm_kzalloc(dev, resp_cap, GFP_KERNEL);
  158. if (!msg->resp)
  159. goto err_free_msg_req;
  160. msg->viortc = viortc;
  161. msg->req_size = req_size;
  162. msg->resp_cap = resp_cap;
  163. refcount_set(&msg->refcnt, 2);
  164. req_head->msg_type = virtio_cpu_to_le(msg_type, req_head->msg_type);
  165. return msg;
  166. err_free_msg_req:
  167. devm_kfree(dev, msg->req);
  168. err_free_msg:
  169. devm_kfree(dev, msg);
  170. return NULL;
  171. }
  172. /**
  173. * viortc_msg_release() - Decrement message refcnt, potentially free message.
  174. * @msg: message requested by driver
  175. *
  176. * Context: Any context.
  177. */
  178. static void viortc_msg_release(struct viortc_msg *msg)
  179. {
  180. struct device *dev;
  181. if (refcount_dec_and_test(&msg->refcnt)) {
  182. dev = &msg->viortc->vdev->dev;
  183. devm_kfree(dev, msg->req);
  184. devm_kfree(dev, msg->resp);
  185. devm_kfree(dev, msg);
  186. }
  187. }
  188. /**
  189. * viortc_do_cb() - generic virtqueue callback logic
  190. * @vq: virtqueue
  191. * @handle_buf: function to process a used buffer
  192. *
  193. * Context: virtqueue callback, typically interrupt. Takes and releases vq lock.
  194. */
  195. static void viortc_do_cb(struct virtqueue *vq,
  196. void (*handle_buf)(void *token, unsigned int len,
  197. struct virtqueue *vq,
  198. struct viortc_vq *viortc_vq,
  199. struct viortc_dev *viortc))
  200. {
  201. struct viortc_dev *viortc = vq->vdev->priv;
  202. struct viortc_vq *viortc_vq;
  203. bool cb_enabled = true;
  204. unsigned long flags;
  205. unsigned int len;
  206. void *token;
  207. viortc_vq = &viortc->vqs[vq->index];
  208. for (;;) {
  209. spin_lock_irqsave(&viortc_vq->lock, flags);
  210. if (cb_enabled) {
  211. virtqueue_disable_cb(vq);
  212. cb_enabled = false;
  213. }
  214. token = virtqueue_get_buf(vq, &len);
  215. if (!token) {
  216. if (virtqueue_enable_cb(vq)) {
  217. spin_unlock_irqrestore(&viortc_vq->lock, flags);
  218. return;
  219. }
  220. cb_enabled = true;
  221. }
  222. spin_unlock_irqrestore(&viortc_vq->lock, flags);
  223. if (token)
  224. handle_buf(token, len, vq, viortc_vq, viortc);
  225. }
  226. }
  227. /**
  228. * viortc_requestq_hdlr() - process a requestq used buffer
  229. * @token: token identifying the buffer
  230. * @len: bytes written by device
  231. * @vq: virtqueue
  232. * @viortc_vq: device specific data for virtqueue
  233. * @viortc: device data
  234. *
  235. * Signals completion for each received message.
  236. *
  237. * Context: virtqueue callback
  238. */
  239. static void viortc_requestq_hdlr(void *token, unsigned int len,
  240. struct virtqueue *vq,
  241. struct viortc_vq *viortc_vq,
  242. struct viortc_dev *viortc)
  243. {
  244. struct viortc_msg *msg = token;
  245. msg->resp_actual_size = len;
  246. complete(&msg->responded);
  247. viortc_msg_release(msg);
  248. }
  249. /**
  250. * viortc_cb_requestq() - callback for requestq
  251. * @vq: virtqueue
  252. *
  253. * Context: virtqueue callback
  254. */
  255. static void viortc_cb_requestq(struct virtqueue *vq)
  256. {
  257. viortc_do_cb(vq, viortc_requestq_hdlr);
  258. }
  259. /**
  260. * viortc_alarmq_hdlr() - process an alarmq used buffer
  261. * @token: token identifying the buffer
  262. * @len: bytes written by device
  263. * @vq: virtqueue
  264. * @viortc_vq: device specific data for virtqueue
  265. * @viortc: device data
  266. *
  267. * Processes a VIRTIO_RTC_NOTIF_ALARM notification by calling the RTC class
  268. * driver. Makes the buffer available again.
  269. *
  270. * Context: virtqueue callback
  271. */
  272. static void viortc_alarmq_hdlr(void *token, unsigned int len,
  273. struct virtqueue *vq,
  274. struct viortc_vq *viortc_vq,
  275. struct viortc_dev *viortc)
  276. {
  277. struct virtio_rtc_notif_alarm *notif = token;
  278. struct virtio_rtc_notif_head *head = token;
  279. unsigned long flags;
  280. u16 clock_id;
  281. bool notify;
  282. if (len < sizeof(*head)) {
  283. dev_err_ratelimited(&viortc->vdev->dev,
  284. "%s: ignoring notification with short header\n",
  285. __func__);
  286. goto feed_vq;
  287. }
  288. if (virtio_le_to_cpu(head->msg_type) != VIRTIO_RTC_NOTIF_ALARM) {
  289. dev_err_ratelimited(&viortc->vdev->dev,
  290. "%s: ignoring unknown notification type 0x%x\n",
  291. __func__, virtio_le_to_cpu(head->msg_type));
  292. goto feed_vq;
  293. }
  294. if (len < sizeof(*notif)) {
  295. dev_err_ratelimited(&viortc->vdev->dev,
  296. "%s: ignoring too small alarm notification\n",
  297. __func__);
  298. goto feed_vq;
  299. }
  300. clock_id = virtio_le_to_cpu(notif->clock_id);
  301. if (!viortc->viortc_class)
  302. dev_warn_ratelimited(&viortc->vdev->dev,
  303. "ignoring alarm, no RTC class device available\n");
  304. else
  305. viortc_class_alarm(viortc->viortc_class, clock_id);
  306. feed_vq:
  307. spin_lock_irqsave(&viortc_vq->lock, flags);
  308. if (viortc_feed_vq(viortc, vq, notif, VIORTC_ALARMQ_BUF_CAP, token))
  309. dev_warn(&viortc->vdev->dev,
  310. "%s: failed to re-expose input buffer\n", __func__);
  311. notify = virtqueue_kick_prepare(vq);
  312. spin_unlock_irqrestore(&viortc_vq->lock, flags);
  313. if (notify)
  314. virtqueue_notify(vq);
  315. }
  316. /**
  317. * viortc_cb_alarmq() - callback for alarmq
  318. * @vq: virtqueue
  319. *
  320. * Context: virtqueue callback
  321. */
  322. static void viortc_cb_alarmq(struct virtqueue *vq)
  323. {
  324. viortc_do_cb(vq, viortc_alarmq_hdlr);
  325. }
  326. /**
  327. * viortc_get_resp_errno() - converts virtio_rtc errnos to system errnos
  328. * @resp_head: message response header
  329. *
  330. * Return: negative system errno, or 0
  331. */
  332. static int viortc_get_resp_errno(struct virtio_rtc_resp_head *resp_head)
  333. {
  334. switch (virtio_le_to_cpu(resp_head->status)) {
  335. case VIRTIO_RTC_S_OK:
  336. return 0;
  337. case VIRTIO_RTC_S_EOPNOTSUPP:
  338. return -EOPNOTSUPP;
  339. case VIRTIO_RTC_S_EINVAL:
  340. return -EINVAL;
  341. case VIRTIO_RTC_S_ENODEV:
  342. return -ENODEV;
  343. case VIRTIO_RTC_S_EIO:
  344. default:
  345. return -EIO;
  346. }
  347. }
  348. /**
  349. * viortc_msg_xfer() - send message request, wait until message response
  350. * @vq: virtqueue
  351. * @msg: message with driver request
  352. * @timeout_jiffies: message response timeout, 0 for no timeout
  353. *
  354. * Context: Process context. Takes and releases vq.lock. May sleep.
  355. * Return: Zero on success, negative error code otherwise.
  356. */
  357. static int viortc_msg_xfer(struct viortc_vq *vq, struct viortc_msg *msg,
  358. unsigned long timeout_jiffies)
  359. {
  360. struct scatterlist out_sg[1];
  361. struct scatterlist in_sg[1];
  362. struct scatterlist *sgs[2];
  363. unsigned long flags;
  364. long timeout_ret;
  365. bool notify;
  366. int ret;
  367. sgs[0] = out_sg;
  368. sgs[1] = in_sg;
  369. sg_init_one(out_sg, msg->req, msg->req_size);
  370. sg_init_one(in_sg, msg->resp, msg->resp_cap);
  371. spin_lock_irqsave(&vq->lock, flags);
  372. ret = virtqueue_add_sgs(vq->vq, sgs, 1, 1, msg, GFP_ATOMIC);
  373. if (ret) {
  374. spin_unlock_irqrestore(&vq->lock, flags);
  375. /*
  376. * Release in place of the response callback, which will never
  377. * come.
  378. */
  379. viortc_msg_release(msg);
  380. return ret;
  381. }
  382. notify = virtqueue_kick_prepare(vq->vq);
  383. spin_unlock_irqrestore(&vq->lock, flags);
  384. if (notify)
  385. virtqueue_notify(vq->vq);
  386. if (timeout_jiffies) {
  387. timeout_ret = wait_for_completion_interruptible_timeout(
  388. &msg->responded, timeout_jiffies);
  389. if (!timeout_ret)
  390. return -ETIMEDOUT;
  391. else if (timeout_ret < 0)
  392. return (int)timeout_ret;
  393. } else {
  394. ret = wait_for_completion_interruptible(&msg->responded);
  395. if (ret)
  396. return ret;
  397. }
  398. if (msg->resp_actual_size < sizeof(struct virtio_rtc_resp_head))
  399. return -EINVAL;
  400. ret = viortc_get_resp_errno(msg->resp);
  401. if (ret)
  402. return ret;
  403. /*
  404. * There is not yet a case where returning a short message would make
  405. * sense, so consider any deviation an error.
  406. */
  407. if (msg->resp_actual_size != msg->resp_cap)
  408. return -EINVAL;
  409. return 0;
  410. }
  411. /*
  412. * common message handle macros for messages of different types
  413. */
  414. /**
  415. * VIORTC_DECLARE_MSG_HDL_ONSTACK() - declare message handle on stack
  416. * @hdl: message handle name
  417. * @msg_id: message type id
  418. * @msg_req: message request type
  419. * @msg_resp: message response type
  420. */
  421. #define VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, msg_id, msg_req, msg_resp) \
  422. struct { \
  423. struct viortc_msg *msg; \
  424. msg_req *req; \
  425. msg_resp *resp; \
  426. unsigned int req_size; \
  427. unsigned int resp_cap; \
  428. u16 msg_type; \
  429. } hdl = { \
  430. NULL, NULL, NULL, sizeof(msg_req), sizeof(msg_resp), (msg_id), \
  431. }
  432. /**
  433. * VIORTC_MSG() - extract message from message handle
  434. * @hdl: message handle
  435. *
  436. * Return: struct viortc_msg
  437. */
  438. #define VIORTC_MSG(hdl) ((hdl).msg)
  439. /**
  440. * VIORTC_MSG_INIT() - initialize message handle
  441. * @hdl: message handle
  442. * @viortc: device data (struct viortc_dev *)
  443. *
  444. * Context: Process context.
  445. * Return: 0 on success, -ENOMEM otherwise.
  446. */
  447. #define VIORTC_MSG_INIT(hdl, viortc) \
  448. ({ \
  449. typeof(hdl) *_hdl = &(hdl); \
  450. \
  451. _hdl->msg = viortc_msg_init((viortc), _hdl->msg_type, \
  452. _hdl->req_size, _hdl->resp_cap); \
  453. if (_hdl->msg) { \
  454. _hdl->req = _hdl->msg->req; \
  455. _hdl->resp = _hdl->msg->resp; \
  456. } \
  457. _hdl->msg ? 0 : -ENOMEM; \
  458. })
  459. /**
  460. * VIORTC_MSG_WRITE() - write a request message field
  461. * @hdl: message handle
  462. * @dest_member: request message field name
  463. * @src_ptr: pointer to data of compatible type
  464. *
  465. * Writes the field in little-endian format.
  466. */
  467. #define VIORTC_MSG_WRITE(hdl, dest_member, src_ptr) \
  468. do { \
  469. typeof(hdl) _hdl = (hdl); \
  470. typeof(src_ptr) _src_ptr = (src_ptr); \
  471. \
  472. /* Sanity check: must match the member's type */ \
  473. typecheck(typeof(virtio_le_to_cpu(_hdl.req->dest_member)), \
  474. *_src_ptr); \
  475. \
  476. _hdl.req->dest_member = \
  477. virtio_cpu_to_le(*_src_ptr, _hdl.req->dest_member); \
  478. } while (0)
  479. /**
  480. * VIORTC_MSG_READ() - read from a response message field
  481. * @hdl: message handle
  482. * @src_member: response message field name
  483. * @dest_ptr: pointer to data of compatible type
  484. *
  485. * Converts from little-endian format and writes to dest_ptr.
  486. */
  487. #define VIORTC_MSG_READ(hdl, src_member, dest_ptr) \
  488. do { \
  489. typeof(dest_ptr) _dest_ptr = (dest_ptr); \
  490. \
  491. /* Sanity check: must match the member's type */ \
  492. typecheck(typeof(virtio_le_to_cpu((hdl).resp->src_member)), \
  493. *_dest_ptr); \
  494. \
  495. *_dest_ptr = virtio_le_to_cpu((hdl).resp->src_member); \
  496. } while (0)
  497. /*
  498. * read requests
  499. */
  500. /** timeout for clock readings, where timeouts are considered non-fatal */
  501. #define VIORTC_MSG_READ_TIMEOUT secs_to_jiffies(60)
  502. /**
  503. * viortc_read() - VIRTIO_RTC_REQ_READ wrapper
  504. * @viortc: device data
  505. * @vio_clk_id: virtio_rtc clock id
  506. * @reading: clock reading [ns]
  507. *
  508. * Context: Process context.
  509. * Return: Zero on success, negative error code otherwise.
  510. */
  511. int viortc_read(struct viortc_dev *viortc, u16 vio_clk_id, u64 *reading)
  512. {
  513. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ,
  514. struct virtio_rtc_req_read,
  515. struct virtio_rtc_resp_read);
  516. int ret;
  517. ret = VIORTC_MSG_INIT(hdl, viortc);
  518. if (ret)
  519. return ret;
  520. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  521. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  522. VIORTC_MSG_READ_TIMEOUT);
  523. if (ret) {
  524. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  525. ret);
  526. goto out_release;
  527. }
  528. VIORTC_MSG_READ(hdl, clock_reading, reading);
  529. out_release:
  530. viortc_msg_release(VIORTC_MSG(hdl));
  531. return ret;
  532. }
  533. /**
  534. * viortc_read_cross() - VIRTIO_RTC_REQ_READ_CROSS wrapper
  535. * @viortc: device data
  536. * @vio_clk_id: virtio_rtc clock id
  537. * @hw_counter: virtio_rtc HW counter type
  538. * @reading: clock reading [ns]
  539. * @cycles: HW counter cycles during clock reading
  540. *
  541. * Context: Process context.
  542. * Return: Zero on success, negative error code otherwise.
  543. */
  544. int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
  545. u64 *reading, u64 *cycles)
  546. {
  547. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ_CROSS,
  548. struct virtio_rtc_req_read_cross,
  549. struct virtio_rtc_resp_read_cross);
  550. int ret;
  551. ret = VIORTC_MSG_INIT(hdl, viortc);
  552. if (ret)
  553. return ret;
  554. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  555. VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter);
  556. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  557. VIORTC_MSG_READ_TIMEOUT);
  558. if (ret) {
  559. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  560. ret);
  561. goto out_release;
  562. }
  563. VIORTC_MSG_READ(hdl, clock_reading, reading);
  564. VIORTC_MSG_READ(hdl, counter_cycles, cycles);
  565. out_release:
  566. viortc_msg_release(VIORTC_MSG(hdl));
  567. return ret;
  568. }
  569. /*
  570. * control requests
  571. */
  572. /**
  573. * viortc_cfg() - VIRTIO_RTC_REQ_CFG wrapper
  574. * @viortc: device data
  575. * @num_clocks: # of virtio_rtc clocks
  576. *
  577. * Context: Process context.
  578. * Return: Zero on success, negative error code otherwise.
  579. */
  580. static int viortc_cfg(struct viortc_dev *viortc, u16 *num_clocks)
  581. {
  582. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CFG,
  583. struct virtio_rtc_req_cfg,
  584. struct virtio_rtc_resp_cfg);
  585. int ret;
  586. ret = VIORTC_MSG_INIT(hdl, viortc);
  587. if (ret)
  588. return ret;
  589. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  590. 0);
  591. if (ret) {
  592. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  593. ret);
  594. goto out_release;
  595. }
  596. VIORTC_MSG_READ(hdl, num_clocks, num_clocks);
  597. out_release:
  598. viortc_msg_release(VIORTC_MSG(hdl));
  599. return ret;
  600. }
  601. /**
  602. * viortc_clock_cap() - VIRTIO_RTC_REQ_CLOCK_CAP wrapper
  603. * @viortc: device data
  604. * @vio_clk_id: virtio_rtc clock id
  605. * @type: virtio_rtc clock type
  606. * @leap_second_smearing: virtio_rtc smearing variant
  607. * @flags: struct virtio_rtc_resp_clock_cap.flags
  608. *
  609. * Context: Process context.
  610. * Return: Zero on success, negative error code otherwise.
  611. */
  612. static int viortc_clock_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 *type,
  613. u8 *leap_second_smearing, u8 *flags)
  614. {
  615. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CLOCK_CAP,
  616. struct virtio_rtc_req_clock_cap,
  617. struct virtio_rtc_resp_clock_cap);
  618. int ret;
  619. ret = VIORTC_MSG_INIT(hdl, viortc);
  620. if (ret)
  621. return ret;
  622. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  623. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  624. 0);
  625. if (ret) {
  626. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  627. ret);
  628. goto out_release;
  629. }
  630. VIORTC_MSG_READ(hdl, type, type);
  631. VIORTC_MSG_READ(hdl, leap_second_smearing, leap_second_smearing);
  632. VIORTC_MSG_READ(hdl, flags, flags);
  633. out_release:
  634. viortc_msg_release(VIORTC_MSG(hdl));
  635. return ret;
  636. }
  637. /**
  638. * viortc_cross_cap() - VIRTIO_RTC_REQ_CROSS_CAP wrapper
  639. * @viortc: device data
  640. * @vio_clk_id: virtio_rtc clock id
  641. * @hw_counter: virtio_rtc HW counter type
  642. * @supported: xtstamping is supported for the vio_clk_id/hw_counter pair
  643. *
  644. * Context: Process context.
  645. * Return: Zero on success, negative error code otherwise.
  646. */
  647. int viortc_cross_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
  648. bool *supported)
  649. {
  650. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CROSS_CAP,
  651. struct virtio_rtc_req_cross_cap,
  652. struct virtio_rtc_resp_cross_cap);
  653. u8 flags;
  654. int ret;
  655. ret = VIORTC_MSG_INIT(hdl, viortc);
  656. if (ret)
  657. return ret;
  658. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  659. VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter);
  660. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  661. 0);
  662. if (ret) {
  663. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  664. ret);
  665. goto out_release;
  666. }
  667. VIORTC_MSG_READ(hdl, flags, &flags);
  668. *supported = !!(flags & VIRTIO_RTC_FLAG_CROSS_CAP);
  669. out_release:
  670. viortc_msg_release(VIORTC_MSG(hdl));
  671. return ret;
  672. }
  673. /**
  674. * viortc_read_alarm() - VIRTIO_RTC_REQ_READ_ALARM wrapper
  675. * @viortc: device data
  676. * @vio_clk_id: virtio_rtc clock id
  677. * @alarm_time: alarm time in ns
  678. * @enabled: whether alarm is enabled
  679. *
  680. * Context: Process context.
  681. * Return: Zero on success, negative error code otherwise.
  682. */
  683. int viortc_read_alarm(struct viortc_dev *viortc, u16 vio_clk_id,
  684. u64 *alarm_time, bool *enabled)
  685. {
  686. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ_ALARM,
  687. struct virtio_rtc_req_read_alarm,
  688. struct virtio_rtc_resp_read_alarm);
  689. u8 flags;
  690. int ret;
  691. ret = VIORTC_MSG_INIT(hdl, viortc);
  692. if (ret)
  693. return ret;
  694. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  695. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  696. 0);
  697. if (ret) {
  698. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  699. ret);
  700. goto out_release;
  701. }
  702. VIORTC_MSG_READ(hdl, alarm_time, alarm_time);
  703. VIORTC_MSG_READ(hdl, flags, &flags);
  704. *enabled = !!(flags & VIRTIO_RTC_FLAG_ALARM_ENABLED);
  705. out_release:
  706. viortc_msg_release(VIORTC_MSG(hdl));
  707. return ret;
  708. }
  709. /**
  710. * viortc_set_alarm() - VIRTIO_RTC_REQ_SET_ALARM wrapper
  711. * @viortc: device data
  712. * @vio_clk_id: virtio_rtc clock id
  713. * @alarm_time: alarm time in ns
  714. * @alarm_enable: enable or disable alarm
  715. *
  716. * Context: Process context.
  717. * Return: Zero on success, negative error code otherwise.
  718. */
  719. int viortc_set_alarm(struct viortc_dev *viortc, u16 vio_clk_id, u64 alarm_time,
  720. bool alarm_enable)
  721. {
  722. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_SET_ALARM,
  723. struct virtio_rtc_req_set_alarm,
  724. struct virtio_rtc_resp_set_alarm);
  725. u8 flags = 0;
  726. int ret;
  727. ret = VIORTC_MSG_INIT(hdl, viortc);
  728. if (ret)
  729. return ret;
  730. if (alarm_enable)
  731. flags |= VIRTIO_RTC_FLAG_ALARM_ENABLED;
  732. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  733. VIORTC_MSG_WRITE(hdl, alarm_time, &alarm_time);
  734. VIORTC_MSG_WRITE(hdl, flags, &flags);
  735. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  736. 0);
  737. if (ret) {
  738. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  739. ret);
  740. goto out_release;
  741. }
  742. out_release:
  743. viortc_msg_release(VIORTC_MSG(hdl));
  744. return ret;
  745. }
  746. /**
  747. * viortc_set_alarm_enabled() - VIRTIO_RTC_REQ_SET_ALARM_ENABLED wrapper
  748. * @viortc: device data
  749. * @vio_clk_id: virtio_rtc clock id
  750. * @alarm_enable: enable or disable alarm
  751. *
  752. * Context: Process context.
  753. * Return: Zero on success, negative error code otherwise.
  754. */
  755. int viortc_set_alarm_enabled(struct viortc_dev *viortc, u16 vio_clk_id,
  756. bool alarm_enable)
  757. {
  758. VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_SET_ALARM_ENABLED,
  759. struct virtio_rtc_req_set_alarm_enabled,
  760. struct virtio_rtc_resp_set_alarm_enabled);
  761. u8 flags = 0;
  762. int ret;
  763. ret = VIORTC_MSG_INIT(hdl, viortc);
  764. if (ret)
  765. return ret;
  766. if (alarm_enable)
  767. flags |= VIRTIO_RTC_FLAG_ALARM_ENABLED;
  768. VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id);
  769. VIORTC_MSG_WRITE(hdl, flags, &flags);
  770. ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl),
  771. 0);
  772. if (ret) {
  773. dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__,
  774. ret);
  775. goto out_release;
  776. }
  777. out_release:
  778. viortc_msg_release(VIORTC_MSG(hdl));
  779. return ret;
  780. }
  781. /*
  782. * init, deinit
  783. */
  784. /**
  785. * viortc_init_rtc_class_clock() - init and register a RTC class device
  786. * @viortc: device data
  787. * @vio_clk_id: virtio_rtc clock id
  788. * @clock_type: virtio_rtc clock type
  789. * @flags: struct virtio_rtc_resp_clock_cap.flags
  790. *
  791. * The clock must be a UTC-like clock.
  792. *
  793. * Context: Process context.
  794. * Return: Positive if registered, zero if not supported by configuration,
  795. * negative error code otherwise.
  796. */
  797. static int viortc_init_rtc_class_clock(struct viortc_dev *viortc,
  798. u16 vio_clk_id, u8 clock_type, u8 flags)
  799. {
  800. struct virtio_device *vdev = viortc->vdev;
  801. struct viortc_class *viortc_class;
  802. struct device *dev = &vdev->dev;
  803. bool have_alarm;
  804. if (clock_type != VIRTIO_RTC_CLOCK_UTC_SMEARED) {
  805. dev_info(dev,
  806. "not creating RTC class device for clock %d, which may step on leap seconds\n",
  807. vio_clk_id);
  808. return 0;
  809. }
  810. if (viortc->viortc_class) {
  811. dev_warn_once(dev,
  812. "multiple UTC-like clocks are present, but creating only one RTC class device\n");
  813. return 0;
  814. }
  815. have_alarm = viortc_alarms_supported(vdev) &&
  816. !!(flags & VIRTIO_RTC_FLAG_ALARM_CAP);
  817. viortc_class = viortc_class_init(viortc, vio_clk_id, have_alarm, dev);
  818. if (IS_ERR(viortc_class))
  819. return PTR_ERR(viortc_class);
  820. viortc->viortc_class = viortc_class;
  821. if (have_alarm)
  822. devm_device_init_wakeup(dev);
  823. return viortc_class_register(viortc_class) ?: 1;
  824. }
  825. /**
  826. * viortc_init_ptp_clock() - init and register PTP clock
  827. * @viortc: device data
  828. * @vio_clk_id: virtio_rtc clock id
  829. * @clock_type: virtio_rtc clock type
  830. * @leap_second_smearing: virtio_rtc leap second smearing
  831. *
  832. * Context: Process context.
  833. * Return: Positive if registered, zero if not supported by configuration,
  834. * negative error code otherwise.
  835. */
  836. static int viortc_init_ptp_clock(struct viortc_dev *viortc, u16 vio_clk_id,
  837. u8 clock_type, u8 leap_second_smearing)
  838. {
  839. struct device *dev = &viortc->vdev->dev;
  840. char ptp_clock_name[PTP_CLOCK_NAME_LEN];
  841. struct viortc_ptp_clock *vio_ptp;
  842. snprintf(ptp_clock_name, PTP_CLOCK_NAME_LEN,
  843. "Virtio PTP type %hhu/variant %hhu", clock_type,
  844. leap_second_smearing);
  845. vio_ptp = viortc_ptp_register(viortc, dev, vio_clk_id, ptp_clock_name);
  846. if (IS_ERR(vio_ptp)) {
  847. dev_err(dev, "failed to register PTP clock '%s'\n",
  848. ptp_clock_name);
  849. return PTR_ERR(vio_ptp);
  850. }
  851. viortc->clocks_to_unregister[vio_clk_id] = vio_ptp;
  852. return !!vio_ptp;
  853. }
  854. /**
  855. * viortc_init_clock() - init local representation of virtio_rtc clock
  856. * @viortc: device data
  857. * @vio_clk_id: virtio_rtc clock id
  858. *
  859. * Initializes PHC and/or RTC class device to represent virtio_rtc clock.
  860. *
  861. * Context: Process context.
  862. * Return: Zero on success, negative error code otherwise.
  863. */
  864. static int viortc_init_clock(struct viortc_dev *viortc, u16 vio_clk_id)
  865. {
  866. u8 clock_type, leap_second_smearing, flags;
  867. bool is_exposed = false;
  868. int ret;
  869. ret = viortc_clock_cap(viortc, vio_clk_id, &clock_type,
  870. &leap_second_smearing, &flags);
  871. if (ret)
  872. return ret;
  873. if (IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) &&
  874. (clock_type == VIRTIO_RTC_CLOCK_UTC ||
  875. clock_type == VIRTIO_RTC_CLOCK_UTC_SMEARED ||
  876. clock_type == VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED)) {
  877. ret = viortc_init_rtc_class_clock(viortc, vio_clk_id,
  878. clock_type, flags);
  879. if (ret < 0)
  880. return ret;
  881. if (ret > 0)
  882. is_exposed = true;
  883. }
  884. if (IS_ENABLED(CONFIG_VIRTIO_RTC_PTP)) {
  885. ret = viortc_init_ptp_clock(viortc, vio_clk_id, clock_type,
  886. leap_second_smearing);
  887. if (ret < 0)
  888. return ret;
  889. if (ret > 0)
  890. is_exposed = true;
  891. }
  892. if (!is_exposed)
  893. dev_warn(&viortc->vdev->dev,
  894. "cannot expose clock %d (type %d, variant %d) to userspace\n",
  895. vio_clk_id, clock_type, leap_second_smearing);
  896. return 0;
  897. }
  898. /**
  899. * viortc_clocks_deinit() - unregister PHCs, stop RTC ops
  900. * @viortc: device data
  901. */
  902. static void viortc_clocks_deinit(struct viortc_dev *viortc)
  903. {
  904. struct viortc_ptp_clock *vio_ptp;
  905. unsigned int i;
  906. for (i = 0; i < viortc->num_clocks; i++) {
  907. vio_ptp = viortc->clocks_to_unregister[i];
  908. if (!vio_ptp)
  909. continue;
  910. viortc->clocks_to_unregister[i] = NULL;
  911. WARN_ON(viortc_ptp_unregister(vio_ptp, &viortc->vdev->dev));
  912. }
  913. if (viortc->viortc_class)
  914. viortc_class_stop(viortc->viortc_class);
  915. }
  916. /**
  917. * viortc_clocks_init() - init local representations of virtio_rtc clocks
  918. * @viortc: device data
  919. *
  920. * Context: Process context.
  921. * Return: Zero on success, negative error code otherwise.
  922. */
  923. static int viortc_clocks_init(struct viortc_dev *viortc)
  924. {
  925. u16 num_clocks;
  926. unsigned int i;
  927. int ret;
  928. ret = viortc_cfg(viortc, &num_clocks);
  929. if (ret)
  930. return ret;
  931. if (num_clocks < 1) {
  932. dev_err(&viortc->vdev->dev, "device reported 0 clocks\n");
  933. return -ENODEV;
  934. }
  935. viortc->num_clocks = num_clocks;
  936. viortc->clocks_to_unregister =
  937. devm_kcalloc(&viortc->vdev->dev, num_clocks,
  938. sizeof(*viortc->clocks_to_unregister), GFP_KERNEL);
  939. if (!viortc->clocks_to_unregister)
  940. return -ENOMEM;
  941. for (i = 0; i < num_clocks; i++) {
  942. ret = viortc_init_clock(viortc, i);
  943. if (ret)
  944. goto err_deinit_clocks;
  945. }
  946. return 0;
  947. err_deinit_clocks:
  948. viortc_clocks_deinit(viortc);
  949. return ret;
  950. }
  951. /**
  952. * viortc_populate_vq() - populate alarmq with device-writable buffers
  953. * @viortc: device data
  954. * @viortc_vq: device specific data for virtqueue
  955. * @buf_cap: device-writable buffer size in bytes
  956. * @lock: lock queue during accesses
  957. *
  958. * Populates the alarmq with pre-allocated buffers.
  959. *
  960. * The caller is responsible for kicking the device.
  961. *
  962. * Context: Process context.
  963. * Return: Zero on success, negative error code otherwise.
  964. */
  965. static int viortc_populate_vq(struct viortc_dev *viortc,
  966. struct viortc_vq *viortc_vq, u32 buf_cap,
  967. bool lock)
  968. {
  969. unsigned int num_elems, i;
  970. struct virtqueue *vq;
  971. unsigned long flags;
  972. void *buf;
  973. int ret;
  974. num_elems = viortc->num_alarmq_bufs;
  975. vq = viortc_vq->vq;
  976. for (i = 0; i < num_elems; i++) {
  977. buf = viortc->alarmq_bufs[i];
  978. if (lock) {
  979. spin_lock_irqsave(&viortc_vq->lock, flags);
  980. ret = viortc_feed_vq(viortc, vq, buf, buf_cap, buf);
  981. spin_unlock_irqrestore(&viortc_vq->lock, flags);
  982. } else {
  983. ret = viortc_feed_vq(viortc, vq, buf, buf_cap, buf);
  984. }
  985. if (ret)
  986. return ret;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * viortc_alloc_vq_bufs() - allocate alarmq buffers
  992. * @viortc: device data
  993. * @num_elems: # of buffers
  994. * @buf_cap: per-buffer device-writable bytes
  995. *
  996. * Context: Process context.
  997. * Return: Zero on success, negative error code otherwise.
  998. */
  999. static int viortc_alloc_vq_bufs(struct viortc_dev *viortc,
  1000. unsigned int num_elems, u32 buf_cap)
  1001. {
  1002. struct device *dev = &viortc->vdev->dev;
  1003. void **buf_list;
  1004. unsigned int i;
  1005. void *buf;
  1006. buf_list = devm_kcalloc(dev, num_elems, sizeof(*buf_list), GFP_KERNEL);
  1007. if (!buf_list)
  1008. return -ENOMEM;
  1009. viortc->alarmq_bufs = buf_list;
  1010. viortc->num_alarmq_bufs = num_elems;
  1011. for (i = 0; i < num_elems; i++) {
  1012. buf = devm_kzalloc(dev, buf_cap, GFP_KERNEL);
  1013. if (!buf)
  1014. return -ENOMEM;
  1015. buf_list[i] = buf;
  1016. }
  1017. return 0;
  1018. }
  1019. /**
  1020. * viortc_init_vqs() - init virtqueues
  1021. * @viortc: device data
  1022. *
  1023. * Inits virtqueues and associated data.
  1024. *
  1025. * Context: Process context.
  1026. * Return: Zero on success, negative error code otherwise.
  1027. */
  1028. static int viortc_init_vqs(struct viortc_dev *viortc)
  1029. {
  1030. struct virtqueue *vqs[VIORTC_MAX_NR_QUEUES];
  1031. struct virtqueue_info vqs_info[] = {
  1032. { "requestq", viortc_cb_requestq },
  1033. { "alarmq", viortc_cb_alarmq },
  1034. };
  1035. struct virtio_device *vdev = viortc->vdev;
  1036. unsigned int num_elems;
  1037. int nr_queues, ret;
  1038. bool have_alarms;
  1039. have_alarms = viortc_alarms_supported(vdev);
  1040. if (have_alarms)
  1041. nr_queues = VIORTC_ALARMQ + 1;
  1042. else
  1043. nr_queues = VIORTC_REQUESTQ + 1;
  1044. ret = virtio_find_vqs(vdev, nr_queues, vqs, vqs_info, NULL);
  1045. if (ret)
  1046. return ret;
  1047. viortc->vqs[VIORTC_REQUESTQ].vq = vqs[VIORTC_REQUESTQ];
  1048. spin_lock_init(&viortc->vqs[VIORTC_REQUESTQ].lock);
  1049. if (have_alarms) {
  1050. viortc->vqs[VIORTC_ALARMQ].vq = vqs[VIORTC_ALARMQ];
  1051. spin_lock_init(&viortc->vqs[VIORTC_ALARMQ].lock);
  1052. num_elems = virtqueue_get_vring_size(vqs[VIORTC_ALARMQ]);
  1053. if (num_elems == 0)
  1054. return -ENOSPC;
  1055. if (!viortc->alarmq_bufs) {
  1056. ret = viortc_alloc_vq_bufs(viortc, num_elems,
  1057. VIORTC_ALARMQ_BUF_CAP);
  1058. if (ret)
  1059. return ret;
  1060. } else {
  1061. viortc->num_alarmq_bufs =
  1062. min(num_elems, viortc->num_alarmq_bufs);
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. /**
  1068. * viortc_probe() - probe a virtio_rtc virtio device
  1069. * @vdev: virtio device
  1070. *
  1071. * Context: Process context.
  1072. * Return: Zero on success, negative error code otherwise.
  1073. */
  1074. static int viortc_probe(struct virtio_device *vdev)
  1075. {
  1076. struct viortc_vq *alarm_viortc_vq;
  1077. struct virtqueue *alarm_vq;
  1078. struct viortc_dev *viortc;
  1079. unsigned long flags;
  1080. bool notify;
  1081. int ret;
  1082. viortc = devm_kzalloc(&vdev->dev, sizeof(*viortc), GFP_KERNEL);
  1083. if (!viortc)
  1084. return -ENOMEM;
  1085. vdev->priv = viortc;
  1086. viortc->vdev = vdev;
  1087. ret = viortc_init_vqs(viortc);
  1088. if (ret)
  1089. return ret;
  1090. virtio_device_ready(vdev);
  1091. ret = viortc_clocks_init(viortc);
  1092. if (ret)
  1093. goto err_reset_vdev;
  1094. if (viortc_alarms_supported(vdev)) {
  1095. alarm_viortc_vq = &viortc->vqs[VIORTC_ALARMQ];
  1096. alarm_vq = alarm_viortc_vq->vq;
  1097. ret = viortc_populate_vq(viortc, alarm_viortc_vq,
  1098. VIORTC_ALARMQ_BUF_CAP, true);
  1099. if (ret)
  1100. goto err_deinit_clocks;
  1101. spin_lock_irqsave(&alarm_viortc_vq->lock, flags);
  1102. notify = virtqueue_kick_prepare(alarm_vq);
  1103. spin_unlock_irqrestore(&alarm_viortc_vq->lock, flags);
  1104. if (notify && !virtqueue_notify(alarm_vq)) {
  1105. ret = -EIO;
  1106. goto err_deinit_clocks;
  1107. }
  1108. }
  1109. return 0;
  1110. err_deinit_clocks:
  1111. viortc_clocks_deinit(viortc);
  1112. err_reset_vdev:
  1113. virtio_reset_device(vdev);
  1114. vdev->config->del_vqs(vdev);
  1115. return ret;
  1116. }
  1117. /**
  1118. * viortc_remove() - remove a virtio_rtc virtio device
  1119. * @vdev: virtio device
  1120. */
  1121. static void viortc_remove(struct virtio_device *vdev)
  1122. {
  1123. struct viortc_dev *viortc = vdev->priv;
  1124. viortc_clocks_deinit(viortc);
  1125. virtio_reset_device(vdev);
  1126. vdev->config->del_vqs(vdev);
  1127. }
  1128. static int viortc_freeze(struct virtio_device *dev)
  1129. {
  1130. /*
  1131. * Do not reset the device, so that the device may still wake up the
  1132. * system through an alarmq notification.
  1133. */
  1134. return 0;
  1135. }
  1136. static int viortc_restore(struct virtio_device *dev)
  1137. {
  1138. struct viortc_dev *viortc = dev->priv;
  1139. struct viortc_vq *alarm_viortc_vq;
  1140. struct virtqueue *alarm_vq;
  1141. bool notify = false;
  1142. int ret;
  1143. ret = viortc_init_vqs(viortc);
  1144. if (ret)
  1145. return ret;
  1146. alarm_viortc_vq = &viortc->vqs[VIORTC_ALARMQ];
  1147. alarm_vq = alarm_viortc_vq->vq;
  1148. if (viortc_alarms_supported(dev)) {
  1149. ret = viortc_populate_vq(viortc, alarm_viortc_vq,
  1150. VIORTC_ALARMQ_BUF_CAP, false);
  1151. if (ret)
  1152. return ret;
  1153. notify = virtqueue_kick_prepare(alarm_vq);
  1154. }
  1155. virtio_device_ready(dev);
  1156. if (notify && !virtqueue_notify(alarm_vq))
  1157. ret = -EIO;
  1158. return ret;
  1159. }
  1160. static unsigned int features[] = {
  1161. #if IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS)
  1162. VIRTIO_RTC_F_ALARM,
  1163. #endif
  1164. };
  1165. static struct virtio_device_id id_table[] = {
  1166. { VIRTIO_ID_CLOCK, VIRTIO_DEV_ANY_ID },
  1167. { 0 },
  1168. };
  1169. MODULE_DEVICE_TABLE(virtio, id_table);
  1170. static struct virtio_driver virtio_rtc_drv = {
  1171. .driver.name = KBUILD_MODNAME,
  1172. .feature_table = features,
  1173. .feature_table_size = ARRAY_SIZE(features),
  1174. .id_table = id_table,
  1175. .probe = viortc_probe,
  1176. .remove = viortc_remove,
  1177. .freeze = pm_sleep_ptr(viortc_freeze),
  1178. .restore = pm_sleep_ptr(viortc_restore),
  1179. };
  1180. module_virtio_driver(virtio_rtc_drv);
  1181. MODULE_DESCRIPTION("Virtio RTC driver");
  1182. MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
  1183. MODULE_LICENSE("GPL");