From patchwork Wed Nov 29 12:48:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Volodymyr Babchuk X-Patchwork-Id: 10082073 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 99FE66020B for ; Wed, 29 Nov 2017 12:51:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8D2ED29769 for ; Wed, 29 Nov 2017 12:51:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 819D52976D; Wed, 29 Nov 2017 12:51:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DF2F629769 for ; Wed, 29 Nov 2017 12:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ECTQt6hFvRQB+hSb2Opmkwe2/I06NAqf/8uYwtHyivA=; b=n9C/W2+K/Jydye R3/8hikT55DNffpRlfsu1ENNhhh67qHa04oKZkiEahx3SM1LbFNNL9L4oS0ahTIYYgcxYptkdCXzI 6WJciX2di2OZuEW0OipqVGn1UIww2q11VkqyIrQvDDqeVcPO+snnGNKi+elDNYXmMzAxX6nA49VNy 9FtcOa3jc9kf7GcioAiHlHpgD54yJVzv9nvR8sp7BQ0yMkNmnk0R5XNDMjohI6EAi8teEFHSwdYR8 neyN/T44FWU+tai2Dh9M8ubQAI4AzCzlc3rXRJYgpG+gW9a/xNAgEZB3VxLqmKywtVQlwt00djRZk G4WOUnf1Uji/L/ROAxaA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eK1ph-0006pA-Uh; Wed, 29 Nov 2017 12:51:21 +0000 Received: from mail-he1eur01on0071.outbound.protection.outlook.com ([104.47.0.71] helo=EUR01-HE1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eK1oG-0004F8-Ei for linux-arm-kernel@lists.infradead.org; Wed, 29 Nov 2017 12:49:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=epam.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=pdxapNs347I6l2yvlPXL0YfekdPB+wJrndLrXiZ5fsY=; b=hDvMFYLZ6JcGhKndUWtXSvIOgn7kPAgGf1+oQCm37hbmdBiwAjdiHoLIrIUpDTm5XZsT6MwM9Kq2tTHMDfFF0I6iwjJfdSJe8M6SqYBiTDhjJogSz8BwxU8mZuAM7a+wzUxVsM1shpXYHv3TMfO+u6S/hvE7GSu4qhPtcgErQ20= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Volodymyr_Babchuk@epam.com; Received: from EPUAKYIW2556.kyiv.epam.com (85.223.209.56) by DB5PR03MB1766.eurprd03.prod.outlook.com (2a01:111:e400:c580::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.260.4; Wed, 29 Nov 2017 12:49:26 +0000 Received: by EPUAKYIW2556.kyiv.epam.com (sSMTP sendmail emulation); Wed, 29 Nov 2017 14:49:22 +0200 From: Volodymyr Babchuk To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, tee-dev@lists.linaro.org, Jens Wiklander Subject: [RESEND PATCH v2 06/14] tee: optee: add page list manipulation functions Date: Wed, 29 Nov 2017 14:48:30 +0200 Message-Id: <1511959718-5421-7-git-send-email-volodymyr_babchuk@epam.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> References: <1507923164-12796-1-git-send-email-volodymyr_babchuk@epam.com> <1511959718-5421-1-git-send-email-volodymyr_babchuk@epam.com> MIME-Version: 1.0 X-Originating-IP: [85.223.209.56] X-ClientProxiedBy: DB6PR0301CA0052.eurprd03.prod.outlook.com (2603:10a6:4:54::20) To DB5PR03MB1766.eurprd03.prod.outlook.com (2a01:111:e400:c580::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: eea32b7d-3d49-43d8-ec75-08d537279fbe X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(5600026)(4604075)(4534020)(4602075)(7168020)(4627115)(201703031133081)(201702281549075)(2017052603199); SRVR:DB5PR03MB1766; X-Microsoft-Exchange-Diagnostics: 1; DB5PR03MB1766; 3:RdQ8y+lEEiKTTEmowl/N6GJXc7Uw9pCnZzuxFRpAqGfS2N+CiZe/k9ctpI6DxpfsMxKT5J2+WebRfuVcG+iFBsap7tJTk5sU84DiPuC+SnIn6chpQOAHMV+QcgAoDIcFyLF5L+WoWUzdoaDNMQoDm2DVNU+wEA8mYchp6FrO89gXxnOXW0Vc0rdbvqqObFhxBcZoikVFloQ1PXZytD7pzrrXB+ejZlwlFzzXpgMFylL8UQ8yjzwVPgKyDfL1faaE; 25:dp0aYr2t2VIQ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bPnDR5UMKLEdZxGciWDLwhCtIWhgbnyMeRubxhQWsVoMjndxe9NEtN9KAgT+tS0s56WzK/jRw0zp+LQ9ybaXB6; 4:Bdlqw6vHR0tfmSQBCyhDxldFpv21glzpDV0m/Sf8pzW0gwi5wgw+2/FnHuX2QFVRPyx2uUzxRTtBYPavViqHKgZji4FGI3U2Pux2qhMAd4XIvsOHeIA9xHk8IcWOvmlJOIb6fabVsoYRfLGhLPZVinCRJuOM4j3aByuCl7wrPo1BEYgPO39LgZ9/iDEA3oNgYgaC6d+WsAzwsaW/oqAVRRuSlh3XB+zkLFfzTmbix95OjvBIr6iCt+K1mdlTdkO2Vced0m2Jna2+MMJFLxsbTQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(2401047)(5005006)(8121501046)(3002001)(3231022)(93006095)(93001095)(10201501046)(6041248)(20161123564025)(20161123558100)(20161123555025)(20161123560025)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148)(201708071742011); SRVR:DB5PR03MB1766; BCL:0; PCL:0; RULEID:(100000803101)(100110400095); SRVR:DB5PR03MB1766; X-Forefront-PRVS: 05066DEDBB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(366004)(346002)(376002)(199003)(189002)(51416003)(80792005)(36756003)(50466002)(106356001)(33646002)(50986999)(105586002)(76176999)(39060400002)(72206003)(478600001)(107886003)(52116002)(4326008)(66066001)(55236003)(101416001)(53936002)(122856001)(16586007)(316002)(42186006)(48376002)(47776003)(8936002)(5660300001)(6666003)(6916009)(2950100002)(189998001)(6116002)(2906002)(3846002)(68736007)(50226002)(8676002)(7736002)(305945005)(97736004)(86362001)(81156014)(81166006)(217873001); DIR:OUT; SFP:1101; SCL:1; SRVR:DB5PR03MB1766; H:EPUAKYIW2556.kyiv.epam.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: epam.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DB5PR03MB1766; 23:Xrh44IjaZHnoftpFNO9amFSW9NrQKyxYTWDKfS5Cm?= =?us-ascii?Q?uqgKOrm3lZ2VjNLQaMSrC3CZRpjmaOh2DITbIQuz/SDCCts10WA3Lw2MvUK7?= =?us-ascii?Q?EPWBbK9Qn750+ZyzOwFt08lQPQnqsR7TQjB5uArJXaTuiABWjmUULhIYd5bD?= =?us-ascii?Q?LcD0xdYJKSOjSbqy1zA0j320//D+MZ6fkpKMQ/ChtpCz7oIjDdYzMbSg0svH?= =?us-ascii?Q?u6yfGDvHgqOp4qLZuuDqi/Fr+I9raKzTyjdIkZHR56Tyn0W+pI7VLLFRnDbT?= =?us-ascii?Q?81wjqF+OtZWo70bo9GVF27hjOlzkTK7Vaf5dQZAKr78mq6dSuh5squd7SlBl?= =?us-ascii?Q?uRD0HpMiY0CIXfjMRHdsdLkM7U4O2hykiH762EDzhYlA9Ec0vzhBcVCywHjy?= =?us-ascii?Q?J99DbTVfNyR7xGr4PmfYozZWfgFzb5CT2mCUkkHO9eaaQgxzcbbNXrau8Sam?= =?us-ascii?Q?G8mqWx3CInAdEHBg5PmCdfZMJeNJXQ8XN+cfzfPrrg4s7UQgw7VNbhLD6ew6?= =?us-ascii?Q?OMvVIqfDxScwA2cFM+sBX0tIQW4ZIbSC8uUuzbUB1lSkMY0ugwT8tCxas6Pb?= =?us-ascii?Q?RM1e0Z7/I0gQoeT/IWnUboToTHoln8jhmUZTPIUW0m+QOSA7CIxJzIE2/keG?= =?us-ascii?Q?CZhhWA9ndQGIwkyUrFZcdtLSbhnntXCyLlI4uGo21TXLj/9qHdyFfrDgH8JY?= =?us-ascii?Q?XpkzcjDPFbzsmt04fe9ZsfUTpQkoiiuZ6qUh+nzvyFXRx6ytDF2XwhsRruvS?= =?us-ascii?Q?4PS4khOV9PY6F9borByImQ7HLuy7MC6qUX0OLJG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P8aC3jhF/0nsAtPoE0HLgOXIwR/aCkzZH6bzdxGnr3Q/xisBVIaIIeJP5g3fo=; 7:lpgHJ18zQ+3vFm1iImiSiJ16cLV41Y+1CJ+eKmDMQnayuy/eB7msX5Lpr9fbtuicBnhFaKA7hEUf9UwdjEd+wmb0BpUaxh2s4Y1BTnhYBqf4zijxCPlkGjR2/5Kwyk3RAO2O8jz0rMzl6YHc6U8jj6l3Cl4Mghd1wNOP1DU41jwNr1qAOv0VW7bBUxt++kIZ1kgj3P2etT59ZIdQoIBnU2Qc3c6Am2z+VIdiauFZs+gxq3fcy6yE5y85XONbH/tv SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: epam.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2017 12:49:26.1447 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eea32b7d-3d49-43d8-ec75-08d537279fbe X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b41b72d0-4e9f-4c26-8a69-f949f367c91d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5PR03MB1766 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171129_044952_928819_660EAE64 X-CRM114-Status: GOOD ( 17.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Volodymyr Babchuk , volodymyr_babchuk@epam.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Volodymyr Babchuk 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 --- 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 #include #include #include @@ -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 */