All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleksandr Andrushchenko <andr2000@gmail.com>
To: alsa-devel@alsa-project.org, xen-devel@lists.xen.org,
	linux-kernel@vger.kernel.org
Cc: andr2000@gmail.com,
	Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>,
	tiwai@suse.com, perex@perex.cz
Subject: [PATCH RESEND1 06/12] ALSA: vsnd: Implement handling of shared buffers
Date: Mon,  7 Aug 2017 15:22:51 +0300	[thread overview]
Message-ID: <1502108577-8099-7-git-send-email-andr2000__15986.4180525348$1502108651$gmane$org@gmail.com> (raw)
In-Reply-To: <1502108577-8099-1-git-send-email-andr2000@gmail.com>

From: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>

Implement shared buffer handling according to the
para-virtualized sound device protocol at xen/interface/io/sndif.h:
- manage buffer memory
- handle granted references
- handle page directories

Signed-off-by: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
---
 sound/drivers/xen-front.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 178 insertions(+)

diff --git a/sound/drivers/xen-front.c b/sound/drivers/xen-front.c
index a92459b2737e..04ebc15757f4 100644
--- a/sound/drivers/xen-front.c
+++ b/sound/drivers/xen-front.c
@@ -58,6 +58,14 @@ struct xdrv_evtchnl_info {
 	uint16_t resp_id;
 };
 
+struct sh_buf_info {
+	int num_grefs;
+	grant_ref_t *grefs;
+	uint8_t *vdirectory;
+	uint8_t *vbuffer;
+	size_t vbuffer_sz;
+};
+
 struct cfg_stream {
 	int unique_id;
 	char *xenstore_path;
@@ -825,6 +833,176 @@ static void xdrv_remove_internal(struct xdrv_info *drv_info)
 	xdrv_evtchnl_free_all(drv_info);
 }
 
+static inline grant_ref_t sh_buf_get_dir_start(struct sh_buf_info *buf)
+{
+	if (!buf->grefs)
+		return GRANT_INVALID_REF;
+	return buf->grefs[0];
+}
+
+static inline void sh_buf_clear(struct sh_buf_info *buf)
+{
+	memset(buf, 0, sizeof(*buf));
+}
+
+static void sh_buf_free(struct sh_buf_info *buf)
+{
+	int i;
+
+	if (buf->grefs) {
+		for (i = 0; i < buf->num_grefs; i++)
+			if (buf->grefs[i] != GRANT_INVALID_REF)
+				gnttab_end_foreign_access(buf->grefs[i],
+						0, 0UL);
+		kfree(buf->grefs);
+	}
+	kfree(buf->vdirectory);
+	free_pages_exact(buf->vbuffer, buf->vbuffer_sz);
+	sh_buf_clear(buf);
+}
+
+/*
+ * number of grant references a page can hold with respect to the
+ * xendispl_page_directory header
+ */
+#define XENSND_NUM_GREFS_PER_PAGE ((XEN_PAGE_SIZE - \
+	offsetof(struct xensnd_page_directory, gref)) / \
+	sizeof(grant_ref_t))
+
+static void sh_buf_fill_page_dir(struct sh_buf_info *buf, int num_pages_dir)
+{
+	struct xensnd_page_directory *page_dir;
+	unsigned char *ptr;
+	int i, cur_gref, grefs_left, to_copy;
+
+	ptr = buf->vdirectory;
+	grefs_left = buf->num_grefs - num_pages_dir;
+	/*
+	 * skip grant references at the beginning, they are for pages granted
+	 * for the page directory itself
+	 */
+	cur_gref = num_pages_dir;
+	for (i = 0; i < num_pages_dir; i++) {
+		page_dir = (struct xensnd_page_directory *)ptr;
+		if (grefs_left <= XENSND_NUM_GREFS_PER_PAGE) {
+			to_copy = grefs_left;
+			page_dir->gref_dir_next_page = GRANT_INVALID_REF;
+		} else {
+			to_copy = XENSND_NUM_GREFS_PER_PAGE;
+			page_dir->gref_dir_next_page = buf->grefs[i + 1];
+		}
+		memcpy(&page_dir->gref, &buf->grefs[cur_gref],
+			to_copy * sizeof(grant_ref_t));
+		ptr += XEN_PAGE_SIZE;
+		grefs_left -= to_copy;
+		cur_gref += to_copy;
+	}
+}
+
+static int sh_buf_grant_refs(struct xenbus_device *xb_dev,
+	struct sh_buf_info *buf,
+	int num_pages_dir, int num_pages_vbuffer, int num_grefs)
+{
+	grant_ref_t priv_gref_head;
+	int ret, i, j, cur_ref;
+	int otherend_id;
+
+	ret = gnttab_alloc_grant_references(num_grefs, &priv_gref_head);
+	if (ret)
+		return ret;
+
+	buf->num_grefs = num_grefs;
+	otherend_id = xb_dev->otherend_id;
+	j = 0;
+
+	for (i = 0; i < num_pages_dir; i++) {
+		cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
+		if (cur_ref < 0) {
+			ret = cur_ref;
+			goto fail;
+		}
+
+		gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
+			xen_page_to_gfn(virt_to_page(buf->vdirectory +
+				XEN_PAGE_SIZE * i)), 0);
+		buf->grefs[j++] = cur_ref;
+	}
+
+	for (i = 0; i < num_pages_vbuffer; i++) {
+		cur_ref = gnttab_claim_grant_reference(&priv_gref_head);
+		if (cur_ref < 0) {
+			ret = cur_ref;
+			goto fail;
+		}
+
+		gnttab_grant_foreign_access_ref(cur_ref, otherend_id,
+			xen_page_to_gfn(virt_to_page(buf->vbuffer +
+				XEN_PAGE_SIZE * i)), 0);
+		buf->grefs[j++] = cur_ref;
+	}
+
+	gnttab_free_grant_references(priv_gref_head);
+	sh_buf_fill_page_dir(buf, num_pages_dir);
+	return 0;
+
+fail:
+	gnttab_free_grant_references(priv_gref_head);
+	return ret;
+}
+
+static int sh_buf_alloc_int_buffers(struct sh_buf_info *buf,
+		int num_pages_dir, int num_pages_vbuffer, int num_grefs)
+{
+	buf->grefs = kcalloc(num_grefs, sizeof(*buf->grefs), GFP_KERNEL);
+	if (!buf->grefs)
+		return -ENOMEM;
+
+	buf->vdirectory = kcalloc(num_pages_dir, XEN_PAGE_SIZE, GFP_KERNEL);
+	if (!buf->vdirectory)
+		goto fail;
+
+	buf->vbuffer_sz = num_pages_vbuffer * XEN_PAGE_SIZE;
+	buf->vbuffer = alloc_pages_exact(buf->vbuffer_sz, GFP_KERNEL);
+	if (!buf->vbuffer)
+		goto fail;
+	return 0;
+
+fail:
+	kfree(buf->grefs);
+	buf->grefs = NULL;
+	kfree(buf->vdirectory);
+	buf->vdirectory = NULL;
+	return -ENOMEM;
+}
+
+static int sh_buf_alloc(struct xenbus_device *xb_dev,
+	struct sh_buf_info *buf, unsigned int buffer_size)
+{
+	int num_pages_vbuffer, num_pages_dir, num_grefs;
+	int ret;
+
+	sh_buf_clear(buf);
+
+	num_pages_vbuffer = DIV_ROUND_UP(buffer_size, XEN_PAGE_SIZE);
+	/* number of pages the page directory consumes itself */
+	num_pages_dir = DIV_ROUND_UP(num_pages_vbuffer,
+		XENSND_NUM_GREFS_PER_PAGE);
+	num_grefs = num_pages_vbuffer + num_pages_dir;
+
+	ret = sh_buf_alloc_int_buffers(buf, num_pages_dir,
+		num_pages_vbuffer, num_grefs);
+	if (ret < 0)
+		return ret;
+
+	ret = sh_buf_grant_refs(xb_dev, buf,
+		num_pages_dir, num_pages_vbuffer, num_grefs);
+	if (ret < 0)
+		return ret;
+
+	sh_buf_fill_page_dir(buf, num_pages_dir);
+	return 0;
+}
+
 static int xdrv_be_on_initwait(struct xdrv_info *drv_info)
 {
 	int stream_idx;
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-08-07 12:22 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-07 12:22 [PATCH RESEND1 00/12] ALSA: vsnd: Add Xen para-virtualized frontend driver Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 01/12] ALSA: vsnd: Introduce Xen para-virtualized sound " Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 02/12] ALSA: vsnd: Implement driver's probe/remove Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 03/12] ALSA: vsnd: Implement Xen bus state handling Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 04/12] ALSA: vsnd: Read sound driver configuration from Xen store Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 05/12] ALSA: vsnd: Implement Xen event channel handling Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 06/12] ALSA: vsnd: Implement handling of shared buffers Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko [this message]
2017-08-07 12:22 ` [PATCH RESEND1 07/12] ALSA: vsnd: Introduce ALSA virtual sound driver Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 08/12] ALSA: vsnd: Initialize virtul sound card Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 09/12] ALSA: vsnd: Add timer for period interrupt emulation Oleksandr Andrushchenko
2017-08-07 12:22   ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 10/12] ALSA: vsnd: Implement ALSA PCM operations Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 11/12] ALSA: vsnd: Implement communication with backend Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-07 12:22 ` [PATCH RESEND1 12/12] ALSA: vsnd: Introduce Kconfig option to enable Xen PV sound Oleksandr Andrushchenko
2017-08-07 12:22 ` Oleksandr Andrushchenko
2017-08-10  3:14 ` [PATCH RESEND1 00/12] ALSA: vsnd: Add Xen para-virtualized frontend driver Takashi Sakamoto
2017-08-10  3:14 ` Takashi Sakamoto
2017-08-10  3:14   ` Takashi Sakamoto
2017-08-10  8:10   ` Oleksandr Andrushchenko
2017-08-10  8:10   ` Oleksandr Andrushchenko
2017-08-10  8:10     ` Oleksandr Andrushchenko
2017-08-17 10:05     ` [Xen-devel] " Oleksandr Grytsov
2017-08-17 10:05       ` Oleksandr Grytsov
2017-08-18  5:43       ` Takashi Sakamoto
2017-08-18  5:43         ` Takashi Sakamoto
2017-08-18  5:56         ` Oleksandr Andrushchenko
2017-08-18  7:17           ` Takashi Sakamoto
2017-08-18  7:23             ` Oleksandr Andrushchenko
2017-08-18  7:23               ` Oleksandr Andrushchenko
2017-08-22  2:43               ` Takashi Sakamoto
2017-08-23 14:51                 ` Oleksandr Grytsov
2017-08-23 14:51                 ` Oleksandr Grytsov
2017-08-23 14:51                   ` Oleksandr Grytsov
2017-08-24  4:38                   ` Takashi Sakamoto
2017-08-24  7:04                     ` Oleksandr Andrushchenko
2017-09-04  7:21                       ` Oleksandr Andrushchenko
2017-09-04  7:21                       ` Oleksandr Andrushchenko
2017-09-05  7:24                       ` [alsa-devel] " Clemens Ladisch
2017-09-12  7:52                         ` Oleksandr Grytsov
2017-09-12  7:52                         ` Oleksandr Grytsov
2017-09-19  8:57                           ` Oleksandr Andrushchenko
2017-09-19  8:57                           ` Oleksandr Andrushchenko
2017-09-26 11:35                             ` Oleksandr Andrushchenko
2017-10-04  6:50                               ` Oleksandr Andrushchenko
2017-10-04  6:50                               ` Oleksandr Andrushchenko
2017-10-13  6:15                                 ` Oleksandr Andrushchenko
2017-10-13  6:15                                 ` Oleksandr Andrushchenko
2017-10-30  6:33                                   ` [RFC] " Oleksandr Andrushchenko
2017-10-30  6:33                                   ` Oleksandr Andrushchenko
2017-11-02  9:44                                     ` Takashi Sakamoto
2017-11-02  9:55                                       ` Oleksandr Andrushchenko
2017-11-02  9:55                                       ` Oleksandr Andrushchenko
2017-11-02  9:44                                     ` Takashi Sakamoto
2017-09-26 11:35                             ` [alsa-devel] [PATCH RESEND1 00/12] " Oleksandr Andrushchenko
2017-09-05  7:24                       ` Clemens Ladisch
2017-08-24  7:04                     ` Oleksandr Andrushchenko
2017-08-24  4:38                   ` Takashi Sakamoto
2017-08-22  2:43               ` Takashi Sakamoto
2017-08-18  7:23             ` Oleksandr Andrushchenko
2017-08-18  7:17           ` Takashi Sakamoto
2017-08-18  5:56         ` Oleksandr Andrushchenko
2017-08-18  5:43       ` Takashi Sakamoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='1502108577-8099-7-git-send-email-andr2000__15986.4180525348$1502108651$gmane$org@gmail.com' \
    --to=andr2000@gmail.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleksandr_andrushchenko@epam.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.