virtio_pcm_msg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. 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. unsigned int num;
  236. unsigned long flags;
  237. spin_lock_irqsave(&vss->lock, flags);
  238. num = vss->msg_count;
  239. spin_unlock_irqrestore(&vss->lock, flags);
  240. return num;
  241. }
  242. /**
  243. * virtsnd_pcm_msg_complete() - Complete an I/O message.
  244. * @msg: I/O message.
  245. * @written_bytes: Number of bytes written to the message.
  246. *
  247. * Completion of the message means the elapsed period. If transmission is
  248. * allowed, then each completed message is immediately placed back at the end
  249. * of the queue.
  250. *
  251. * For the playback substream, @written_bytes is equal to sizeof(msg->status).
  252. *
  253. * For the capture substream, @written_bytes is equal to sizeof(msg->status)
  254. * plus the number of captured bytes.
  255. *
  256. * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
  257. */
  258. static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
  259. size_t written_bytes)
  260. {
  261. struct virtio_pcm_substream *vss = msg->substream;
  262. /*
  263. * hw_ptr always indicates the buffer position of the first I/O message
  264. * in the virtqueue. Therefore, on each completion of an I/O message,
  265. * the hw_ptr value is unconditionally advanced.
  266. */
  267. spin_lock(&vss->lock);
  268. /*
  269. * If the capture substream returned an incorrect status, then just
  270. * increase the hw_ptr by the message size.
  271. */
  272. if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
  273. written_bytes <= sizeof(msg->status))
  274. vss->hw_ptr += msg->length;
  275. else
  276. vss->hw_ptr += written_bytes - sizeof(msg->status);
  277. if (vss->hw_ptr >= vss->buffer_bytes)
  278. vss->hw_ptr -= vss->buffer_bytes;
  279. msg->length = 0;
  280. vss->xfer_xrun = false;
  281. vss->msg_count--;
  282. if (vss->xfer_enabled) {
  283. struct snd_pcm_runtime *runtime = vss->substream->runtime;
  284. runtime->delay =
  285. bytes_to_frames(runtime,
  286. le32_to_cpu(msg->status.latency_bytes));
  287. schedule_work(&vss->elapsed_period);
  288. } else if (!vss->msg_count) {
  289. wake_up_all(&vss->msg_empty);
  290. }
  291. spin_unlock(&vss->lock);
  292. }
  293. /**
  294. * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
  295. * @queue: Underlying tx/rx virtqueue.
  296. *
  297. * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
  298. */
  299. static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
  300. {
  301. struct virtio_pcm_msg *msg;
  302. u32 written_bytes;
  303. unsigned long flags;
  304. spin_lock_irqsave(&queue->lock, flags);
  305. do {
  306. virtqueue_disable_cb(queue->vqueue);
  307. while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
  308. virtsnd_pcm_msg_complete(msg, written_bytes);
  309. } while (!virtqueue_enable_cb(queue->vqueue));
  310. spin_unlock_irqrestore(&queue->lock, flags);
  311. }
  312. /**
  313. * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
  314. * @vqueue: Underlying tx virtqueue.
  315. *
  316. * Context: Interrupt context.
  317. */
  318. void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
  319. {
  320. struct virtio_snd *snd = vqueue->vdev->priv;
  321. virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
  322. }
  323. /**
  324. * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
  325. * @vqueue: Underlying rx virtqueue.
  326. *
  327. * Context: Interrupt context.
  328. */
  329. void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
  330. {
  331. struct virtio_snd *snd = vqueue->vdev->priv;
  332. virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
  333. }
  334. /**
  335. * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control
  336. * message for the specified substream.
  337. * @vss: VirtIO PCM substream.
  338. * @command: Control request code (VIRTIO_SND_R_PCM_XXX).
  339. * @gfp: Kernel flags for memory allocation.
  340. *
  341. * Context: Any context. May sleep if @gfp flags permit.
  342. * Return: Allocated message on success, NULL on failure.
  343. */
  344. struct virtio_snd_msg *
  345. virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
  346. unsigned int command, gfp_t gfp)
  347. {
  348. size_t request_size = sizeof(struct virtio_snd_pcm_hdr);
  349. size_t response_size = sizeof(struct virtio_snd_hdr);
  350. struct virtio_snd_msg *msg;
  351. switch (command) {
  352. case VIRTIO_SND_R_PCM_SET_PARAMS:
  353. request_size = sizeof(struct virtio_snd_pcm_set_params);
  354. break;
  355. }
  356. msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
  357. if (msg) {
  358. struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
  359. hdr->hdr.code = cpu_to_le32(command);
  360. hdr->stream_id = cpu_to_le32(vss->sid);
  361. }
  362. return msg;
  363. }