virtio_pcm_msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 = kzalloc_objs(*vss->msgs, periods);
  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_flex(*msg, sgs, sg_num + 2);
  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. virtsnd_pcm_sg_from(&msg->sgs[PCM_MSG_SG_DATA], sg_num, data,
  138. period_bytes);
  139. vss->msgs[i] = msg;
  140. }
  141. return 0;
  142. }
  143. /**
  144. * virtsnd_pcm_msg_free() - Free all allocated I/O messages.
  145. * @vss: VirtIO PCM substream.
  146. *
  147. * Context: Any context.
  148. */
  149. void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss)
  150. {
  151. unsigned int i;
  152. for (i = 0; vss->msgs && i < vss->nmsgs; ++i)
  153. kfree(vss->msgs[i]);
  154. kfree(vss->msgs);
  155. vss->msgs = NULL;
  156. vss->nmsgs = 0;
  157. }
  158. /**
  159. * virtsnd_pcm_msg_send() - Send asynchronous I/O messages.
  160. * @vss: VirtIO PCM substream.
  161. * @offset: starting position that has been updated
  162. * @bytes: number of bytes that has been updated
  163. *
  164. * All messages are organized in an ordered circular list. Each time the
  165. * function is called, all currently non-enqueued messages are added to the
  166. * virtqueue. For this, the function uses offset and bytes to calculate the
  167. * messages that need to be added.
  168. *
  169. * Context: Any context. Expects the tx/rx queue and the VirtIO substream
  170. * spinlocks to be held by caller.
  171. * Return: 0 on success, -errno on failure.
  172. */
  173. int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss, unsigned long offset,
  174. unsigned long bytes)
  175. {
  176. struct virtio_snd *snd = vss->snd;
  177. struct virtio_device *vdev = snd->vdev;
  178. struct virtqueue *vqueue = virtsnd_pcm_queue(vss)->vqueue;
  179. unsigned long period_bytes = snd_pcm_lib_period_bytes(vss->substream);
  180. unsigned long start, end, i;
  181. unsigned int msg_count = vss->msg_count;
  182. bool notify = false;
  183. int rc;
  184. start = offset / period_bytes;
  185. end = (offset + bytes - 1) / period_bytes;
  186. for (i = start; i <= end; i++) {
  187. struct virtio_pcm_msg *msg = vss->msgs[i];
  188. struct scatterlist *psgs[] = {
  189. &msg->sgs[PCM_MSG_SG_XFER],
  190. &msg->sgs[PCM_MSG_SG_DATA],
  191. &msg->sgs[PCM_MSG_SG_STATUS]
  192. };
  193. unsigned long n;
  194. n = period_bytes - (offset % period_bytes);
  195. if (n > bytes)
  196. n = bytes;
  197. msg->length += n;
  198. if (msg->length == period_bytes) {
  199. msg->xfer.stream_id = cpu_to_le32(vss->sid);
  200. memset(&msg->status, 0, sizeof(msg->status));
  201. if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
  202. rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg,
  203. GFP_ATOMIC);
  204. else
  205. rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg,
  206. GFP_ATOMIC);
  207. if (rc) {
  208. dev_err(&vdev->dev,
  209. "SID %u: failed to send I/O message\n",
  210. vss->sid);
  211. return rc;
  212. }
  213. vss->msg_count++;
  214. }
  215. offset = 0;
  216. bytes -= n;
  217. }
  218. if (msg_count == vss->msg_count)
  219. return 0;
  220. if (!(vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)))
  221. notify = virtqueue_kick_prepare(vqueue);
  222. if (notify)
  223. virtqueue_notify(vqueue);
  224. return 0;
  225. }
  226. /**
  227. * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages.
  228. * @vss: VirtIO substream.
  229. *
  230. * Context: Any context.
  231. * Return: Number of messages.
  232. */
  233. unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
  234. {
  235. guard(spinlock_irqsave)(&vss->lock);
  236. return vss->msg_count;
  237. }
  238. /**
  239. * virtsnd_pcm_msg_complete() - Complete an I/O message.
  240. * @msg: I/O message.
  241. * @written_bytes: Number of bytes written to the message.
  242. *
  243. * Completion of the message means the elapsed period. If transmission is
  244. * allowed, then each completed message is immediately placed back at the end
  245. * of the queue.
  246. *
  247. * For the playback substream, @written_bytes is equal to sizeof(msg->status).
  248. *
  249. * For the capture substream, @written_bytes is equal to sizeof(msg->status)
  250. * plus the number of captured bytes.
  251. *
  252. * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
  253. */
  254. static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
  255. size_t written_bytes)
  256. {
  257. struct virtio_pcm_substream *vss = msg->substream;
  258. /*
  259. * hw_ptr always indicates the buffer position of the first I/O message
  260. * in the virtqueue. Therefore, on each completion of an I/O message,
  261. * the hw_ptr value is unconditionally advanced.
  262. */
  263. guard(spinlock)(&vss->lock);
  264. /*
  265. * If the capture substream returned an incorrect status, then just
  266. * increase the hw_ptr by the message size.
  267. */
  268. if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
  269. written_bytes <= sizeof(msg->status))
  270. vss->hw_ptr += msg->length;
  271. else
  272. vss->hw_ptr += written_bytes - sizeof(msg->status);
  273. if (vss->hw_ptr >= vss->buffer_bytes)
  274. vss->hw_ptr -= vss->buffer_bytes;
  275. msg->length = 0;
  276. vss->xfer_xrun = false;
  277. vss->msg_count--;
  278. if (vss->xfer_enabled) {
  279. struct snd_pcm_runtime *runtime = vss->substream->runtime;
  280. runtime->delay =
  281. bytes_to_frames(runtime,
  282. le32_to_cpu(msg->status.latency_bytes));
  283. schedule_work(&vss->elapsed_period);
  284. } else if (!vss->msg_count) {
  285. wake_up_all(&vss->msg_empty);
  286. }
  287. }
  288. /**
  289. * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
  290. * @queue: Underlying tx/rx virtqueue.
  291. *
  292. * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
  293. */
  294. static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
  295. {
  296. struct virtio_pcm_msg *msg;
  297. u32 written_bytes;
  298. guard(spinlock_irqsave)(&queue->lock);
  299. do {
  300. virtqueue_disable_cb(queue->vqueue);
  301. while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
  302. virtsnd_pcm_msg_complete(msg, written_bytes);
  303. } while (!virtqueue_enable_cb(queue->vqueue));
  304. }
  305. /**
  306. * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
  307. * @vqueue: Underlying tx virtqueue.
  308. *
  309. * Context: Interrupt context.
  310. */
  311. void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
  312. {
  313. struct virtio_snd *snd = vqueue->vdev->priv;
  314. virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
  315. }
  316. /**
  317. * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
  318. * @vqueue: Underlying rx virtqueue.
  319. *
  320. * Context: Interrupt context.
  321. */
  322. void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
  323. {
  324. struct virtio_snd *snd = vqueue->vdev->priv;
  325. virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
  326. }
  327. /**
  328. * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control
  329. * message for the specified substream.
  330. * @vss: VirtIO PCM substream.
  331. * @command: Control request code (VIRTIO_SND_R_PCM_XXX).
  332. * @gfp: Kernel flags for memory allocation.
  333. *
  334. * Context: Any context. May sleep if @gfp flags permit.
  335. * Return: Allocated message on success, NULL on failure.
  336. */
  337. struct virtio_snd_msg *
  338. virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
  339. unsigned int command, gfp_t gfp)
  340. {
  341. size_t request_size = sizeof(struct virtio_snd_pcm_hdr);
  342. size_t response_size = sizeof(struct virtio_snd_hdr);
  343. struct virtio_snd_msg *msg;
  344. switch (command) {
  345. case VIRTIO_SND_R_PCM_SET_PARAMS:
  346. request_size = sizeof(struct virtio_snd_pcm_set_params);
  347. break;
  348. }
  349. msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
  350. if (msg) {
  351. struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
  352. hdr->hdr.code = cpu_to_le32(command);
  353. hdr->stream_id = cpu_to_le32(vss->sid);
  354. }
  355. return msg;
  356. }