All of lore.kernel.org
 help / color / mirror / Atom feed
From: Volodymyr Babchuk <volodymyr_babchuk@epam.com>
To: linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, tee-dev@lists.linaro.org,
	Jens Wiklander <jens.wiklander@linaro.org>
Cc: Volodymyr Babchuk <vlad.babchuk@gmail.com>, volodymyr_babchuk@epam.com
Subject: [RESEND PATCH v2 06/14] tee: optee: add page list manipulation functions
Date: Wed, 29 Nov 2017 14:48:30 +0200	[thread overview]
Message-ID: <1511959718-5421-7-git-send-email-volodymyr_babchuk@epam.com> (raw)
In-Reply-To: <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com>

From: Volodymyr Babchuk <vlad.babchuk@gmail.com>

These functions will be used to pass information about shared
buffers to OP-TEE. ABI between Linux and OP-TEE is defined
in optee_msg.h and optee_smc.h.

optee_msg.h defines OPTEE_MSG_ATTR_NONCONTIG attribute
for shared memory references and describes how such references
should be passed. Note that it uses 64-bit page addresses even
on 32 bit systems. This is done to support LPAE and to unify
interface.

Signed-off-by: Volodymyr Babchuk <vlad.babchuk@gmail.com>
---
 drivers/tee/optee/call.c          | 91 +++++++++++++++++++++++++++++++++++++++
 drivers/tee/optee/optee_private.h |  5 +++
 2 files changed, 96 insertions(+)

diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c
index f7b7b40..4db52fe 100644
--- a/drivers/tee/optee/call.c
+++ b/drivers/tee/optee/call.c
@@ -11,6 +11,7 @@
  * GNU General Public License for more details.
  *
  */
+#include <asm/pgtable.h>
 #include <linux/arm-smccc.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -442,3 +443,93 @@ void optee_disable_shm_cache(struct optee *optee)
 	}
 	optee_cq_wait_final(&optee->call_queue, &w);
 }
+
+#define PAGELIST_ENTRIES_PER_PAGE				\
+	((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
+
+/**
+ * optee_fill_pages_list() - write list of user pages to given shared
+ * buffer.
+ *
+ * @dst: page-aligned buffer where list of pages will be stored
+ * @pages: array of pages that represents shared buffer
+ * @num_pages: number of entries in @pages
+ * @page_offset: offset of user buffer from page start
+ *
+ * @dst should be big enough to hold list of user page addresses and
+ *	links to the next pages of buffer
+ */
+void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
+			   size_t page_offset)
+{
+	int n = 0;
+	phys_addr_t optee_page;
+	/*
+	 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
+	 * for details.
+	 */
+	struct {
+		uint64_t pages_list[PAGELIST_ENTRIES_PER_PAGE];
+		uint64_t next_page_data;
+	} *pages_data;
+
+	/*
+	 * Currently OP-TEE uses 4k page size and it does not looks
+	 * like this will change in the future.  On other hand, there are
+	 * no know ARM architectures with page size < 4k.
+	 * Thus the next built assert looks redundant. But the following
+	 * code heavily relies on this assumption, so it is better be
+	 * safe than sorry.
+	 */
+	BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
+
+	pages_data = (void *)dst;
+	/*
+	 * If linux page is bigger than 4k, and user buffer offset is
+	 * larger than 4k/8k/12k/etc this will skip first 4k pages,
+	 * because they bear no value data for OP-TEE.
+	 */
+	optee_page = page_to_phys(*pages) +
+		round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
+
+	while (true) {
+		pages_data->pages_list[n++] = optee_page;
+
+		if (n == PAGELIST_ENTRIES_PER_PAGE) {
+			pages_data->next_page_data =
+				virt_to_phys(pages_data + 1);
+			pages_data++;
+			n = 0;
+		}
+
+		optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
+		if (!(optee_page & ~PAGE_MASK)) {
+			if (!--num_pages)
+				break;
+			pages++;
+			optee_page = page_to_phys(*pages);
+		}
+	}
+}
+
+/*
+ * The final entry in each pagelist page is a pointer to the next
+ * pagelist page.
+ */
+static size_t get_pages_list_size(size_t num_entries)
+{
+	int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
+
+	return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
+}
+
+u64 *optee_allocate_pages_list(size_t num_entries)
+{
+	return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
+}
+
+void optee_free_pages_list(void *list, size_t num_entries)
+{
+	free_pages_exact(list, get_pages_list_size(num_entries));
+}
+
diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h
index c374cd5..b63213d 100644
--- a/drivers/tee/optee/optee_private.h
+++ b/drivers/tee/optee/optee_private.h
@@ -165,6 +165,11 @@ int optee_from_msg_param(struct tee_param *params, size_t num_params,
 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
 		       const struct tee_param *params);
 
+u64 *optee_allocate_pages_list(size_t num_entries);
+void optee_free_pages_list(void *array, size_t num_entries);
+void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
+			   size_t page_offset);
+
 /*
  * Small helpers
  */
-- 
2.7.4

WARNING: multiple messages have this Message-ID (diff)
From: volodymyr_babchuk@epam.com (Volodymyr Babchuk)
To: linux-arm-kernel@lists.infradead.org
Subject: [RESEND PATCH v2 06/14] tee: optee: add page list manipulation functions
Date: Wed, 29 Nov 2017 14:48:30 +0200	[thread overview]
Message-ID: <1511959718-5421-7-git-send-email-volodymyr_babchuk@epam.com> (raw)
In-Reply-To: <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com>

From: Volodymyr Babchuk <vlad.babchuk@gmail.com>

These functions will be used to pass information about shared
buffers to OP-TEE. ABI between Linux and OP-TEE is defined
in optee_msg.h and optee_smc.h.

optee_msg.h defines OPTEE_MSG_ATTR_NONCONTIG attribute
for shared memory references and describes how such references
should be passed. Note that it uses 64-bit page addresses even
on 32 bit systems. This is done to support LPAE and to unify
interface.

Signed-off-by: Volodymyr Babchuk <vlad.babchuk@gmail.com>
---
 drivers/tee/optee/call.c          | 91 +++++++++++++++++++++++++++++++++++++++
 drivers/tee/optee/optee_private.h |  5 +++
 2 files changed, 96 insertions(+)

diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c
index f7b7b40..4db52fe 100644
--- a/drivers/tee/optee/call.c
+++ b/drivers/tee/optee/call.c
@@ -11,6 +11,7 @@
  * GNU General Public License for more details.
  *
  */
+#include <asm/pgtable.h>
 #include <linux/arm-smccc.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -442,3 +443,93 @@ void optee_disable_shm_cache(struct optee *optee)
 	}
 	optee_cq_wait_final(&optee->call_queue, &w);
 }
+
+#define PAGELIST_ENTRIES_PER_PAGE				\
+	((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
+
+/**
+ * optee_fill_pages_list() - write list of user pages to given shared
+ * buffer.
+ *
+ * @dst: page-aligned buffer where list of pages will be stored
+ * @pages: array of pages that represents shared buffer
+ * @num_pages: number of entries in @pages
+ * @page_offset: offset of user buffer from page start
+ *
+ * @dst should be big enough to hold list of user page addresses and
+ *	links to the next pages of buffer
+ */
+void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
+			   size_t page_offset)
+{
+	int n = 0;
+	phys_addr_t optee_page;
+	/*
+	 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
+	 * for details.
+	 */
+	struct {
+		uint64_t pages_list[PAGELIST_ENTRIES_PER_PAGE];
+		uint64_t next_page_data;
+	} *pages_data;
+
+	/*
+	 * Currently OP-TEE uses 4k page size and it does not looks
+	 * like this will change in the future.  On other hand, there are
+	 * no know ARM architectures with page size < 4k.
+	 * Thus the next built assert looks redundant. But the following
+	 * code heavily relies on this assumption, so it is better be
+	 * safe than sorry.
+	 */
+	BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
+
+	pages_data = (void *)dst;
+	/*
+	 * If linux page is bigger than 4k, and user buffer offset is
+	 * larger than 4k/8k/12k/etc this will skip first 4k pages,
+	 * because they bear no value data for OP-TEE.
+	 */
+	optee_page = page_to_phys(*pages) +
+		round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
+
+	while (true) {
+		pages_data->pages_list[n++] = optee_page;
+
+		if (n == PAGELIST_ENTRIES_PER_PAGE) {
+			pages_data->next_page_data =
+				virt_to_phys(pages_data + 1);
+			pages_data++;
+			n = 0;
+		}
+
+		optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
+		if (!(optee_page & ~PAGE_MASK)) {
+			if (!--num_pages)
+				break;
+			pages++;
+			optee_page = page_to_phys(*pages);
+		}
+	}
+}
+
+/*
+ * The final entry in each pagelist page is a pointer to the next
+ * pagelist page.
+ */
+static size_t get_pages_list_size(size_t num_entries)
+{
+	int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
+
+	return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
+}
+
+u64 *optee_allocate_pages_list(size_t num_entries)
+{
+	return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
+}
+
+void optee_free_pages_list(void *list, size_t num_entries)
+{
+	free_pages_exact(list, get_pages_list_size(num_entries));
+}
+
diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h
index c374cd5..b63213d 100644
--- a/drivers/tee/optee/optee_private.h
+++ b/drivers/tee/optee/optee_private.h
@@ -165,6 +165,11 @@ int optee_from_msg_param(struct tee_param *params, size_t num_params,
 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
 		       const struct tee_param *params);
 
+u64 *optee_allocate_pages_list(size_t num_entries);
+void optee_free_pages_list(void *array, size_t num_entries);
+void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
+			   size_t page_offset);
+
 /*
  * Small helpers
  */
-- 
2.7.4

  parent reply	other threads:[~2017-11-29 12:51 UTC|newest]

Thread overview: 128+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-28 18:03 [PATCH v1 00/14] tee: optee: add dynamic shared memory support Volodymyr Babchuk
2017-09-28 18:03 ` Volodymyr Babchuk
2017-09-28 18:03 ` [PATCH v1 01/14] tee: flexible shared memory pool creation Volodymyr Babchuk
2017-09-28 18:03   ` Volodymyr Babchuk
2017-09-28 18:03 ` [PATCH v1 02/14] tee: add register user memory Volodymyr Babchuk
2017-09-28 18:03   ` Volodymyr Babchuk
2017-09-29 10:53   ` Mark Rutland
2017-09-29 10:53     ` Mark Rutland
2017-09-29 15:19     ` Volodymyr Babchuk
2017-09-29 15:19       ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 03/14] tee: shm: add accessors for buffer size and page offset Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 04/14] tee: shm: add page accessor functions Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 22:14   ` Yury Norov
2017-09-28 22:14     ` Yury Norov
2017-09-29 10:17     ` Volodymyr Babchuk
2017-09-29 10:17       ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 05/14] tee: optee: Update protocol definitions Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 06/14] tee: optee: add page list manipulation functions Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-29  0:23   ` Yury Norov
2017-09-29  0:23     ` Yury Norov
2017-09-29 10:34     ` Volodymyr Babchuk
2017-09-29 10:34       ` Volodymyr Babchuk
2017-09-29 16:23       ` Yury Norov
2017-09-29 16:23         ` Yury Norov
2017-09-29 13:00   ` Mark Rutland
2017-09-29 13:00     ` Mark Rutland
2017-09-28 18:04 ` [PATCH v1 07/14] tee: optee: add shared buffer registration functions Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-29 13:06   ` Mark Rutland
2017-09-29 13:06     ` Mark Rutland
2017-09-29 15:37     ` Volodymyr Babchuk
2017-09-29 15:37       ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 08/14] tee: optee: add registered shared parameters handling Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 09/14] tee: optee: add registered buffers handling into RPC calls Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 10/14] tee: optee: store OP-TEE capabilities in private data Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 11/14] tee: optee: add optee-specific shared pool implementation Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 12/14] tee: optee: enable dynamic SHM support Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-10-03 16:06   ` [Tee-dev] " Stuart Yoder
2017-10-03 16:06     ` Stuart Yoder
2017-10-04 11:49     ` Jens Wiklander
2017-10-04 11:49       ` Jens Wiklander
2017-09-28 18:04 ` [PATCH v1 13/14] tee: use reference counting for tee_context Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-28 18:04 ` [PATCH v1 14/14] tee: shm: inline tee_shm getter functions Volodymyr Babchuk
2017-09-28 18:04   ` Volodymyr Babchuk
2017-09-29  0:50   ` Yury Norov
2017-09-29  0:50     ` Yury Norov
2017-09-29 10:31 ` [PATCH v1 00/14] tee: optee: add dynamic shared memory support Mark Rutland
2017-09-29 10:31   ` Mark Rutland
2017-09-29 10:51   ` Volodymyr Babchuk
2017-09-29 10:51     ` Volodymyr Babchuk
2017-10-03 16:05 ` [Tee-dev] " Stuart Yoder
2017-10-03 16:05   ` Stuart Yoder
2017-10-04 17:23   ` Volodymyr Babchuk
2017-10-04 17:23     ` Volodymyr Babchuk
2017-10-13 19:32 ` Volodymyr Babchuk
2017-10-13 19:32   ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 01/14] tee: flexible shared memory pool creation Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 02/14] tee: add register user memory Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 03/14] tee: shm: add accessors for buffer size and page offset Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 04/14] tee: shm: add page accessor functions Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 05/14] tee: optee: Update protocol definitions Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 06/14] tee: optee: add page list manipulation functions Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 07/14] tee: optee: add shared buffer registration functions Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 08/14] tee: optee: add registered shared parameters handling Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 09/14] tee: optee: add registered buffers handling into RPC calls Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 10/14] tee: optee: store OP-TEE capabilities in private data Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 11/14] tee: optee: add optee-specific shared pool implementation Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 12/14] tee: optee: enable dynamic SHM support Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 13/14] tee: use reference counting for tee_context Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:32   ` [PATCH v1 14/14] tee: shm: inline tee_shm_get_id() Volodymyr Babchuk
2017-10-13 19:32     ` Volodymyr Babchuk
2017-10-13 19:40   ` [PATCH v1 00/14] tee: optee: add dynamic shared memory support Volodymyr Babchuk
2017-10-13 19:40     ` Volodymyr Babchuk
2017-11-29 12:48   ` [RESEND PATCH v2 " Volodymyr Babchuk
2017-11-29 12:48     ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 01/14] tee: flexible shared memory pool creation Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 02/14] tee: add register user memory Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 03/14] tee: shm: add accessors for buffer size and page offset Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 04/14] tee: shm: add page accessor functions Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 05/14] tee: optee: Update protocol definitions Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` Volodymyr Babchuk [this message]
2017-11-29 12:48       ` [RESEND PATCH v2 06/14] tee: optee: add page list manipulation functions Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 07/14] tee: optee: add shared buffer registration functions Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 08/14] tee: optee: add registered shared parameters handling Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 09/14] tee: optee: add registered buffers handling into RPC calls Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 10/14] tee: optee: store OP-TEE capabilities in private data Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 11/14] tee: optee: add optee-specific shared pool implementation Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 12/14] tee: optee: enable dynamic SHM support Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 13/14] tee: use reference counting for tee_context Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-11-29 12:48     ` [RESEND PATCH v2 14/14] tee: shm: inline tee_shm_get_id() Volodymyr Babchuk
2017-11-29 12:48       ` Volodymyr Babchuk
2017-12-06 14:32     ` [RESEND PATCH v2 00/14] tee: optee: add dynamic shared memory support Jens Wiklander
2017-12-06 14:32       ` Jens Wiklander

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=1511959718-5421-7-git-send-email-volodymyr_babchuk@epam.com \
    --to=volodymyr_babchuk@epam.com \
    --cc=jens.wiklander@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tee-dev@lists.linaro.org \
    --cc=vlad.babchuk@gmail.com \
    /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.