public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Anastasiia Lukianenko <vicooodin@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH 11/17] xen: Port Xen grant table driver from mini-os
Date: Wed,  1 Jul 2020 19:29:53 +0300	[thread overview]
Message-ID: <20200701162959.9814-12-vicooodin@gmail.com> (raw)
In-Reply-To: <20200701162959.9814-1-vicooodin@gmail.com>

From: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>

Make required updates to run on u-boot.

Signed-off-by: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
Signed-off-by: Anastasiia Lukianenko <anastasiia_lukianenko@epam.com>
---
 board/xen/xenguest_arm64/xenguest_arm64.c |  13 ++
 drivers/xen/Makefile                      |   1 +
 drivers/xen/gnttab.c                      | 258 ++++++++++++++++++++++
 drivers/xen/hypervisor.c                  |   2 +
 include/xen/gnttab.h                      |  25 +++
 5 files changed, 299 insertions(+)
 create mode 100644 drivers/xen/gnttab.c
 create mode 100644 include/xen/gnttab.h

diff --git a/board/xen/xenguest_arm64/xenguest_arm64.c b/board/xen/xenguest_arm64/xenguest_arm64.c
index e8621f7174..b4e1650f99 100644
--- a/board/xen/xenguest_arm64/xenguest_arm64.c
+++ b/board/xen/xenguest_arm64/xenguest_arm64.c
@@ -22,6 +22,7 @@
 
 #include <linux/compiler.h>
 
+#include <xen/gnttab.h>
 #include <xen/hvm.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -64,6 +65,8 @@ static int setup_mem_map(void)
 	struct fdt_resource res;
 	const void *blob = gd->fdt_blob;
 	u64 gfn;
+	phys_addr_t gnttab_base;
+	phys_size_t gnttab_sz;
 
 	/*
 	 * Add "magic" region which is used by Xen to provide some essentials
@@ -97,6 +100,16 @@ static int setup_mem_map(void)
 				PTE_BLOCK_INNER_SHARE);
 	i++;
 
+	/* Get Xen's suggested physical page assignments for the grant table. */
+	get_gnttab_base(&gnttab_base, &gnttab_sz);
+
+	xen_mem_map[i].virt = gnttab_base;
+	xen_mem_map[i].phys = gnttab_base;
+	xen_mem_map[i].size = gnttab_sz;
+	xen_mem_map[i].attrs = (PTE_BLOCK_MEMTYPE(MT_NORMAL) |
+				PTE_BLOCK_INNER_SHARE);
+	i++;
+
 	mem = get_next_memory_node(blob, -1);
 	if (mem < 0) {
 		printf("%s: Missing /memory node\n", __func__);
diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile
index 9d0f604aaa..243b13277a 100644
--- a/drivers/xen/Makefile
+++ b/drivers/xen/Makefile
@@ -5,3 +5,4 @@
 obj-y += hypervisor.o
 obj-y += events.o
 obj-y += xenbus.o
+obj-y += gnttab.o
diff --git a/drivers/xen/gnttab.c b/drivers/xen/gnttab.c
new file mode 100644
index 0000000000..b18102e329
--- /dev/null
+++ b/drivers/xen/gnttab.c
@@ -0,0 +1,258 @@
+/*
+ ****************************************************************************
+ * (C) 2006 - Cambridge University
+ * (C) 2020 - EPAM Systems Inc.
+ ****************************************************************************
+ *
+ *		File: gnttab.c
+ *	  Author: Steven Smith (sos22 at cam.ac.uk)
+ *	 Changes: Grzegorz Milos (gm281@cam.ac.uk)
+ *
+ *		Date: July 2006
+ *
+ * Environment: Xen Minimal OS
+ * Description: Simple grant tables implementation. About as stupid as it's
+ *  possible to be and still work.
+ *
+ ****************************************************************************
+ */
+#include <common.h>
+#include <linux/compiler.h>
+#include <log.h>
+#include <malloc.h>
+
+#include <asm/armv8/mmu.h>
+#include <asm/io.h>
+#include <asm/xen/system.h>
+
+#include <linux/bug.h>
+
+#include <xen/gnttab.h>
+#include <xen/hvm.h>
+
+#include <xen/interface/memory.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define NR_RESERVED_ENTRIES 8
+
+/* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
+#define NR_GRANT_FRAMES 1
+#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(struct grant_entry_v1))
+
+static struct grant_entry_v1 *gnttab_table;
+static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
+
+static void put_free_entry(grant_ref_t ref)
+{
+	unsigned long flags;
+
+	local_irq_save(flags);
+	gnttab_list[ref] = gnttab_list[0];
+	gnttab_list[0]  = ref;
+	local_irq_restore(flags);
+}
+
+static grant_ref_t get_free_entry(void)
+{
+	unsigned int ref;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	ref = gnttab_list[0];
+	BUG_ON(ref < NR_RESERVED_ENTRIES || ref >= NR_GRANT_ENTRIES);
+	gnttab_list[0] = gnttab_list[ref];
+	local_irq_restore(flags);
+	return ref;
+}
+
+grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame, int readonly)
+{
+	grant_ref_t ref;
+
+	ref = get_free_entry();
+	gnttab_table[ref].frame = frame;
+	gnttab_table[ref].domid = domid;
+	wmb();
+	readonly *= GTF_readonly;
+	gnttab_table[ref].flags = GTF_permit_access | readonly;
+
+	return ref;
+}
+
+grant_ref_t gnttab_grant_transfer(domid_t domid, unsigned long pfn)
+{
+	grant_ref_t ref;
+
+	ref = get_free_entry();
+	gnttab_table[ref].frame = pfn;
+	gnttab_table[ref].domid = domid;
+	wmb();
+	gnttab_table[ref].flags = GTF_accept_transfer;
+
+	return ref;
+}
+
+int gnttab_end_access(grant_ref_t ref)
+{
+	u16 flags, nflags;
+
+	BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
+
+	nflags = gnttab_table[ref].flags;
+	do {
+		if ((flags = nflags) & (GTF_reading | GTF_writing)) {
+			printf("WARNING: g.e. still in use! (%x)\n", flags);
+			return 0;
+		}
+	} while ((nflags = synch_cmpxchg(&gnttab_table[ref].flags, flags, 0)) !=
+		 flags);
+
+	put_free_entry(ref);
+	return 1;
+}
+
+unsigned long gnttab_end_transfer(grant_ref_t ref)
+{
+	unsigned long frame;
+	u16 flags;
+
+	BUG_ON(ref >= NR_GRANT_ENTRIES || ref < NR_RESERVED_ENTRIES);
+
+	while (!((flags = gnttab_table[ref].flags) & GTF_transfer_committed)) {
+		if (synch_cmpxchg(&gnttab_table[ref].flags, flags, 0) == flags) {
+			printf("Release unused transfer grant.\n");
+			put_free_entry(ref);
+			return 0;
+		}
+	}
+
+	/* If a transfer is in progress then wait until it is completed. */
+	while (!(flags & GTF_transfer_completed))
+		flags = gnttab_table[ref].flags;
+
+	/* Read the frame number /after/ reading completion status. */
+	rmb();
+	frame = gnttab_table[ref].frame;
+
+	put_free_entry(ref);
+
+	return frame;
+}
+
+grant_ref_t gnttab_alloc_and_grant(void **map)
+{
+	unsigned long mfn;
+	grant_ref_t gref;
+
+	*map = (void *)memalign(PAGE_SIZE, PAGE_SIZE);
+	mfn = virt_to_mfn(*map);
+	gref = gnttab_grant_access(0, mfn, 0);
+	return gref;
+}
+
+static const char * const gnttabop_error_msgs[] = GNTTABOP_error_msgs;
+
+const char *gnttabop_error(int16_t status)
+{
+	status = -status;
+	if (status < 0 || status >= ARRAY_SIZE(gnttabop_error_msgs))
+		return "bad status";
+	else
+		return gnttabop_error_msgs[status];
+}
+
+/* Get Xen's suggested physical page assignments for the grant table. */
+void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz)
+{
+	const void *blob = gd->fdt_blob;
+	struct fdt_resource res;
+	int mem;
+
+	mem = fdt_node_offset_by_compatible(blob, -1, "xen,xen");
+	if (mem < 0) {
+		printf("No xen,xen compatible found\n");
+		BUG();
+	}
+
+	mem = fdt_get_resource(blob, mem, "reg", 0, &res);
+	if (mem == -FDT_ERR_NOTFOUND) {
+		printf("No grant table base in the device tree\n");
+		BUG();
+	}
+
+	*gnttab_base = (phys_addr_t)res.start;
+	if (gnttab_sz)
+		*gnttab_sz = (phys_size_t)(res.end - res.start + 1);
+
+	debug("FDT suggests grant table base@%llx\n",
+	      *gnttab_base);
+}
+
+void init_gnttab(void)
+{
+	struct xen_add_to_physmap xatp;
+	struct gnttab_setup_table setup;
+	xen_pfn_t frames[NR_GRANT_FRAMES];
+	int i, rc;
+
+	debug("%s\n", __func__);
+
+	for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
+		put_free_entry(i);
+
+	get_gnttab_base((phys_addr_t *)&gnttab_table, NULL);
+
+	for (i = 0; i < NR_GRANT_FRAMES; i++) {
+		xatp.domid = DOMID_SELF;
+		xatp.size = 0;
+		xatp.space = XENMAPSPACE_grant_table;
+		xatp.idx = i;
+		xatp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
+		rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
+		if (rc)
+			printf("XENMEM_add_to_physmap failed; status = %d\n",
+			       rc);
+		BUG_ON(rc != 0);
+	}
+
+	setup.dom = DOMID_SELF;
+	setup.nr_frames = NR_GRANT_FRAMES;
+	set_xen_guest_handle(setup.frame_list, frames);
+	rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
+	if (rc || setup.status) {
+		printf("GNTTABOP_setup_table failed; status = %s\n",
+		       gnttabop_error(setup.status));
+		BUG();
+	}
+}
+
+void fini_gnttab(void)
+{
+	struct xen_remove_from_physmap xrtp;
+	struct gnttab_setup_table setup;
+	int i, rc;
+
+	debug("%s\n", __func__);
+
+	for (i = 0; i < NR_GRANT_FRAMES; i++) {
+		xrtp.domid = DOMID_SELF;
+		xrtp.gpfn = PFN_DOWN((unsigned long)gnttab_table) + i;
+		rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrtp);
+		if (rc)
+			printf("XENMEM_remove_from_physmap failed; status = %d\n",
+			       rc);
+		BUG_ON(rc != 0);
+	}
+
+	setup.dom = DOMID_SELF;
+	setup.nr_frames = 0;
+
+	HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
+	if (setup.status) {
+		printf("GNTTABOP_setup_table failed; status = %s\n",
+		       gnttabop_error(setup.status));
+		BUG();
+	}
+}
+
diff --git a/drivers/xen/hypervisor.c b/drivers/xen/hypervisor.c
index d7fbacb08e..f3c2504d72 100644
--- a/drivers/xen/hypervisor.c
+++ b/drivers/xen/hypervisor.c
@@ -38,6 +38,7 @@
 
 #include <xen/hvm.h>
 #include <xen/events.h>
+#include <xen/gnttab.h>
 #include <xen/xenbus.h>
 #include <xen/interface/memory.h>
 
@@ -275,5 +276,6 @@ void xen_init(void)
 	map_shared_info(NULL);
 	init_events();
 	init_xenbus();
+	init_gnttab();
 }
 
diff --git a/include/xen/gnttab.h b/include/xen/gnttab.h
new file mode 100644
index 0000000000..7e0f6db83e
--- /dev/null
+++ b/include/xen/gnttab.h
@@ -0,0 +1,25 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0
+ *
+ * (C) 2006, Steven Smith <sos22@cam.ac.uk>
+ * (C) 2006, Grzegorz Milos <gm281@cam.ac.uk>
+ * (C) 2020, EPAM Systems Inc.
+ */
+#ifndef __GNTTAB_H__
+#define __GNTTAB_H__
+
+#include <xen/interface/grant_table.h>
+
+void init_gnttab(void);
+void fini_gnttab(void);
+
+grant_ref_t gnttab_alloc_and_grant(void **map);
+grant_ref_t gnttab_grant_access(domid_t domid, unsigned long frame,
+				int readonly);
+grant_ref_t gnttab_grant_transfer(domid_t domid, unsigned long pfn);
+int gnttab_end_access(grant_ref_t ref);
+const char *gnttabop_error(int16_t status);
+
+void get_gnttab_base(phys_addr_t *gnttab_base, phys_size_t *gnttab_sz);
+
+#endif /* !__GNTTAB_H__ */
-- 
2.17.1

  parent reply	other threads:[~2020-07-01 16:29 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-01 16:29 [PATCH 00/17] Add new board: Xen guest for ARM64 Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 01/17] armv8: Fix SMCC and ARM_PSCI_FW dependencies Anastasiia Lukianenko
2020-07-02  1:14   ` Peng Fan
2020-07-03  9:57     ` Nastya Vicodin
2020-07-01 16:29 ` [PATCH 02/17] Kconfig: Introduce CONFIG_XEN Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-03 12:42     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 03/17] board: Introduce xenguest_arm64 board Anastasiia Lukianenko
2020-07-02  1:28   ` Peng Fan
2020-07-02  7:18     ` Oleksandr Andrushchenko
2020-07-02  7:26       ` Heinrich Schuchardt
2020-07-02  7:57         ` Oleksandr Andrushchenko
2020-07-01 16:29 ` [PATCH 04/17] xen: Add essential and required interface headers Anastasiia Lukianenko
2020-07-02  1:30   ` Peng Fan
2020-07-03 12:46     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 05/17] xen: Port Xen hypervizor related code from mini-os Anastasiia Lukianenko
2020-07-01 17:46   ` Julien Grall
2020-07-03 12:21     ` Anastasiia Lukianenko
2020-07-03 13:38       ` Julien Grall
2020-07-08  8:55         ` Anastasiia Lukianenko
2020-07-16 13:16     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 06/17] xen: Port Xen event channel driver " Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-03 12:34     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 07/17] serial: serial_xen: Add Xen PV serial driver Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-03 12:59     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 08/17] linux/compat.h: Add wait_event_timeout macro Anastasiia Lukianenko
2020-07-02  4:08   ` Heinrich Schuchardt
2020-07-03 13:02     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 09/17] lib: sscanf: add sscanf implementation Anastasiia Lukianenko
2020-07-02  4:04   ` Heinrich Schuchardt
2020-07-01 16:29 ` [PATCH 10/17] xen: Port Xen bus driver from mini-os Anastasiia Lukianenko
2020-07-02  4:43   ` Heinrich Schuchardt
2020-07-01 16:29 ` Anastasiia Lukianenko [this message]
2020-07-01 16:59   ` [PATCH 11/17] xen: Port Xen grant table " Julien Grall
2020-07-03 13:09     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 12/17] xen: pvblock: Add initial support for para-virtualized block driver Anastasiia Lukianenko
2020-07-02  4:17   ` Heinrich Schuchardt
2020-07-03 13:25     ` Anastasiia Lukianenko
2020-07-02  4:29   ` Heinrich Schuchardt
2020-07-02  5:30     ` Peng Fan
2020-07-03 14:14     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 13/17] xen: pvblock: Enumerate virtual block devices Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-01 16:29 ` [PATCH 14/17] xen: pvblock: Read XenStore configuration and initialize Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-06  9:08     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 15/17] xen: pvblock: Implement front-back protocol and do IO Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-06  9:10     ` Anastasiia Lukianenko
2020-07-01 16:29 ` [PATCH 16/17] xen: pvblock: Print found devices indices Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-01 16:29 ` [PATCH 17/17] board: xen: De-initialize before jumping to Linux Anastasiia Lukianenko
2020-07-03  3:50   ` Simon Glass
2020-07-06  9:13     ` Anastasiia Lukianenko
2020-07-01 16:51 ` [PATCH 00/17] Add new board: Xen guest for ARM64 Anastasiia Lukianenko

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=20200701162959.9814-12-vicooodin@gmail.com \
    --to=vicooodin@gmail.com \
    --cc=u-boot@lists.denx.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox