virtio_pcm_msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * virtio-snd: Virtio sound device
  4. * Copyright (C) 2021 OpenSynergy GmbH
  5. */
  6. #include <sound/pcm_params.h>
  7. #include "virtio_card.h"
  8. /**
  9. * struct virtio_pcm_msg - VirtIO I/O message.
  10. * @substream: VirtIO PCM substream.
  11. * @xfer: Request header payload.
  12. * @status: Response header payload.
  13. * @length: Data length in bytes.
  14. * @sgs: Payload scatter-gather table.
  15. */
  16. struct virtio_pcm_msg {
  17. struct virtio_pcm_substream *substream;
  18. struct virtio_snd_pcm_xfer xfer;
  19. struct virtio_snd_pcm_status status;
  20. size_t length;
  21. struct scatterlist sgs[];
  22. };
  23. /**
  24. * enum pcm_msg_sg_index - Index values for the virtio_pcm_msg->sgs field in
  25. * an I/O message.
  26. * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure.
  27. * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure.
  28. * @PCM_MSG_SG_DATA: The first element containing a data buffer.
  29. */
  30. enum pcm_msg_sg_index {
  31. PCM_MSG_SG_XFER = 0,
  32. PCM_MSG_SG_STATUS,
  33. PCM_MSG_SG_DATA
  34. };
  35. /**
  36. * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
  37. * vmalloc'ed buffer.
  38. * @data: Pointer to vmalloc'ed buffer.
  39. * @length: Buffer size.
  40. *
  41. * Context: Any context.
  42. * Return: Number of physically contiguous parts in the @data.
  43. */
  44. static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
  45. {
  46. phys_addr_t sg_address;
  47. unsigned int sg_length;
  48. int num = 0;
  49. while (length) {
  50. struct page *pg = vmalloc_to_page(data);
  51. phys_addr_t pg_address = page_to_phys(pg);
  52. size_t pg_length;
  53. pg_length = PAGE_SIZE - offset_in_page(data);
  54. if (pg_length > length)
  55. pg_length = length;
  56. if (!num || sg_address + sg_length != pg_address) {
  57. sg_address = pg_address;
  58. sg_length = pg_length;
  59. num++;
  60. } else {
  61. sg_length += pg_length;
  62. }
  63. data += pg_length;
  64. length -= pg_length;
  65. }
  66. return num;
  67. }
  68. /**
  69. * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
  70. * @sgs: Preallocated sg-list to populate.
  71. * @nsgs: The maximum number of elements in the @sgs.
  72. * @data: Pointer to vmalloc'ed buffer.
  73. * @length: Buffer size.
  74. *
  75. * Splits the buffer into physically contiguous parts and makes an sg-list of
  76. * such parts.
  77. *
  78. * Context: Any context.
  79. */
  80. static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
  81. unsigned int length)
  82. {
  83. int idx = -1;
  84. while (length) {
  85. struct page *pg = vmalloc_to_page(data);
  86. size_t pg_length;
  87. pg_length = PAGE_SIZE - offset_in_page(data);
  88. if (pg_length > length)
  89. pg_length = length;
  90. if (idx == -1 ||
  91. sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
  92. if (idx + 1 == nsgs)
  93. break;
  94. sg_set_page(&sgs[++idx], pg, pg_length,
  95. offset_in_page(data));
  96. } else {
  97. sgs[idx].length += pg_length;
  98. }
  99. data += pg_length;
  100. length -= pg_length;
  101. }
  102. sg_mark_end(&sgs[idx]);
  103. }
  104. /**
  105. * virtsnd_pcm_msg_alloc() - Allocate I/O messages.
  106. * @vss: VirtIO PCM substream.
  107. * @periods: Current number of periods.
  108. * @period_bytes: Current period size in bytes.
  109. *
  110. * The function slices the buffer into @periods parts (each with the size of
  111. * @period_bytes), and creates @periods corresponding I/O messages.
  112. *
  113. * Context: Any context that permits to sleep.
  114. * Return: 0 on success, -ENOMEM on failure.
  115. */
  116. int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
  117. unsigned int periods, unsigned int period_bytes)
  118. {
  119. struct snd_pcm_runtime *runtime = vss->substream->runtime;
  120. unsigned int i;
  121. vss->msgs = kcalloc(periods, sizeof(*vss->msgs), GFP_KERNEL);
  122. if (!vss->msgs)
  123. return -ENOMEM;
  124. vss->nmsgs = periods;
  125. for (i = 0; i < periods; ++i) {
  126. u8 *data = runtime->dma_area + period_bytes * i;
  127. int sg_num = virtsnd_pcm_sg_num(data, period_bytes);
  128. struct virtio_pcm_msg *msg;
  129. msg = kzalloc(struct_size(msg, sgs, sg_num + 2), GFP_KERNEL);
  130. if (!msg)
  131. return -ENOMEM;
  132. msg->substream = vss;
  133. sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer,
  134. sizeof(msg->xfer));
  135. sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status,
  136. sizeof(msg->status));
  137. msg->length = period_bytes;
  138. virtsnd_pcm_sg_from(&msg->sgs[PCM_MSG_SG_DATA], sg_num, data,
  139. period_bytes);
  140. vss->msgs[i] = msg;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * virtsnd_pcm_msg_free() - Free all allocated I/O messages.
  146. * @vss: VirtIO PCM substream.
  147. *
  148. * Context: Any context.
  149. */
  150. void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss)
  151. {
  152. unsigned int i;
  153. for (i = 0; vss->msgs && i < vss->nmsgs; ++i)
  154. kfree(vss->msgs[i]);
  155. kfree(vss->msgs);
  156. vss->msgs = NULL;
  157. vss->nmsgs = 0;
  158. }
  159. /**
  160. * virtsnd_pcm_msg_send() - Send asynchronous I/O messages.
  161. * @vss: VirtIO PCM substream.
  162. *
  163. * All messages are organized in an ordered circular list. Each time the
  164. * function is called, all currently non-enqueued messages are added to the
  165. * virtqueue. For this, the function keeps track of two values:
  166. *
  167. * msg_last_enqueued = index of the last enqueued message,
  168. * msg_count = # of pending messages in the virtqueue.
  169. *
  170. * Context: Any context. Expects the tx/rx queue and the VirtIO substream
  171. * spinlocks to be held by caller.
  172. * Return: 0 on success, -errno on failure.
  173. */
  174. int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss)
  175. {
  176. struct snd_pcm_runtime *runtime = vss->substream->runtime;
  177. struct virtio_snd *snd = vss->snd;
  178. struct virtio_device *vdev = snd->vdev;
  179. struct virtqueue *vqueue = virtsnd_pcm_queue(vss)->vqueue;
  180. int i;
  181. int n;
  182. bool notify = false;
  183. i = (vss->msg_last_enqueued + 1) % runtime->periods;
  184. n = runtime->periods - vss->msg_count;
  185. for (; n; --n, i = (i + 1) % runtime->periods) {
  186. struct virtio_pcm_msg *msg = vss->msgs[i];
  187. struct scatterlist *psgs[] = {
  188. &msg->sgs[PCM_MSG_SG_XFER],
  189. &msg->sgs[PCM_MSG_SG_DATA],
  190. &msg->sgs[PCM_MSG_SG_STATUS]
  191. };
  192. int rc;
  193. msg->xfer.stream_id = cpu_to_le32(vss->sid);
  194. memset(&msg->status, 0, sizeof(msg->status));
  195. if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
  196. rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg,
  197. GFP_ATOMIC);
  198. else
  199. rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg,
  200. GFP_ATOMIC);
  201. if (rc) {
  202. dev_err(&vdev->dev,
  203. "SID %u: failed to send I/O message\n",
  204. vss->sid);
  205. return rc;
  206. }
  207. vss->msg_last_enqueued = i;
  208. vss->msg_count++;
  209. }
  210. if (!(vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)))
  211. notify = virtqueue_kick_prepare(vqueue);
  212. if (notify)
  213. virtqueue_notify(vqueue);
  214. return 0;
  215. }
  216. /**
  217. * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages.
  218. * @vss: VirtIO substream.
  219. *
  220. * Context: Any context.
  221. * Return: Number of messages.
  222. */
  223. unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
  224. {
  225. unsigned int num;
  226. unsigned long flags;
  227. spin_lock_irqsave(&vss->lock, flags);
  228. num = vss->msg_count;
  229. spin_unlock_irqrestore(&vss->lock, flags);
  230. return num;
  231. }
  232. /**
  233. * virtsnd_pcm_msg_complete() - Complete an I/O message.
  234. * @msg: I/O message.
  235. * @written_bytes: Number of bytes written to the message.
  236. *
  237. * Completion of the message means the elapsed period. If transmission is
  238. * allowed, then each completed message is immediately placed back at the end
  239. * of the queue.
  240. *
  241. * For the playback substream, @written_bytes is equal to sizeof(msg->status).
  242. *
  243. * For the capture substream, @written_bytes is equal to sizeof(msg->status)
  244. * plus the number of captured bytes.
  245. *
  246. * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
  247. */
  248. static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
  249. size_t written_bytes)
  250. {
  251. struct virtio_pcm_substream *vss = msg->substream;
  252. /*
  253. * hw_ptr always indicates the buffer position of the first I/O message
  254. * in the virtqueue. Therefore, on each completion of an I/O message,
  255. * the hw_ptr value is unconditionally advanced.
  256. */
  257. spin_lock(&vss->lock);
  258. /*
  259. * If the capture substream returned an incorrect status, then just
  260. * increase the hw_ptr by the message size.
  261. */
  262. if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
  263. written_bytes <= sizeof(msg->status))
  264. vss->hw_ptr += msg->length;
  265. else
  266. vss->hw_ptr += written_bytes - sizeof(msg->status);
  267. if (vss->hw_ptr >= vss->buffer_bytes)
  268. vss->hw_ptr -= vss->buffer_bytes;
  269. vss->xfer_xrun = false;
  270. vss->msg_count--;
  271. if (vss->xfer_enabled) {
  272. struct snd_pcm_runtime *runtime = vss->substream->runtime;
  273. runtime->delay =
  274. bytes_to_frames(runtime,
  275. le32_to_cpu(msg->status.latency_bytes));
  276. schedule_work(&vss->elapsed_period);
  277. virtsnd_pcm_msg_send(vss);
  278. } else if (!vss->msg_count) {
  279. wake_up_all(&vss->msg_empty);
  280. }
  281. spin_unlock(&vss->lock);
  282. }
  283. /**
  284. * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
  285. * @queue: Underlying tx/rx virtqueue.
  286. *
  287. * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
  288. */
  289. static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
  290. {
  291. struct virtio_pcm_msg *msg;
  292. u32 written_bytes;
  293. unsigned long flags;
  294. spin_lock_irqsave(&queue->lock, flags);
  295. do {
  296. virtqueue_disable_cb(queue->vqueue);
  297. while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
  298. virtsnd_pcm_msg_complete(msg, written_bytes);
  299. if (unlikely(virtqueue_is_broken(queue->vqueue)))
  300. break;
  301. } while (!virtqueue_enable_cb(queue->vqueue));
  302. spin_unlock_irqrestore(&queue->lock, flags);
  303. }
  304. /**
  305. * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
  306. * @vqueue: Underlying tx virtqueue.
  307. *
  308. * Context: Interrupt context.
  309. */
  310. void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
  311. {
  312. struct virtio_snd *snd = vqueue->vdev->priv;
  313. virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
  314. }
  315. /**
  316. * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
  317. * @vqueue: Underlying rx virtqueue.
  318. *
  319. * Context: Interrupt context.
  320. */
  321. void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
  322. {
  323. struct virtio_snd *snd = vqueue->vdev->priv;
  324. virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
  325. }
  326. /**
  327. * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control
  328. * message for the specified substream.
  329. * @vss: VirtIO PCM substream.
  330. * @command: Control request code (VIRTIO_SND_R_PCM_XXX).
  331. * @gfp: Kernel flags for memory allocation.
  332. *
  333. * Context: Any context. May sleep if @gfp flags permit.
  334. * Return: Allocated message on success, NULL on failure.
  335. */
  336. struct virtio_snd_msg *
  337. virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
  338. unsigned int command, gfp_t gfp)
  339. {
  340. size_t request_size = sizeof(struct virtio_snd_pcm_hdr);
  341. size_t response_size = sizeof(struct virtio_snd_hdr);
  342. struct virtio_snd_msg *msg;
  343. switch (command) {
  344. case VIRTIO_SND_R_PCM_SET_PARAMS:
  345. request_size = sizeof(struct virtio_snd_pcm_set_params);
  346. break;
  347. }
  348. msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
  349. if (msg) {
  350. struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
  351. hdr->hdr.code = cpu_to_le32(command);
  352. hdr->stream_id = cpu_to_le32(vss->sid);
  353. }
  354. return msg;
  355. }