From patchwork Tue Oct 1 15:56:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169157 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4F06F14DB for ; Tue, 1 Oct 2019 15:56:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 24EBE21855 for ; Tue, 1 Oct 2019 15:56:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mn3Tzvyf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389372AbfJAP4w (ORCPT ); Tue, 1 Oct 2019 11:56:52 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:54404 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727352AbfJAP4w (ORCPT ); Tue, 1 Oct 2019 11:56:52 -0400 Received: by mail-wm1-f67.google.com with SMTP id p7so3977438wmp.4 for ; Tue, 01 Oct 2019 08:56:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=M85HxRMpAzQjkmFhMcNqj/4jxGHTjtLdbpCzAjHUVKE=; b=Mn3TzvyfjdPidTViINAeEWwlZiIMumM1exvdZNYzmwrHX0p3GmyZRMM8V9flY85cFM TGgKfMYcMMCk/Z6BbYrfMeX9hn0F57JVuhVCDGIxiYNOflcKwRrHE83ox5RkR9HElHyf Q1a7jpz570MTWRKNhXqIWuThAa0xbWiyzEifXEX3YP1NWsSSFsCDDqkreCALRg0TGypv vMTof0V0m0ut/H8pAPT5XgoqSxyaQzfgpFx+zOdoUvnq7uo2loK9gclxKpKmWdYKKZWO X39hD6UmhHPEg7smpqrJE6xnC7X0lOTQm/jxED4ZyhQOn/mXwNVSaOvrf0AcMP4JYecf gBPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M85HxRMpAzQjkmFhMcNqj/4jxGHTjtLdbpCzAjHUVKE=; b=s2CiYh0gye0Bn28ytQCU4HQhNtynNFE9QvnN1km5ylCNdOSNgL6vAuWb+kGqk4v0FF 0RaQcMC3/HOXc2zJSU0gJcVEc3YoTDYgoqKeuroODjnO7AP+St1IrUfLDeh4lY4yuKQN oH5iOvkaL9chTiVcasntfbKSY42sfxqN2veQ1sxZNEOBzkkQYCHJYLNmQVgnHxwydJUH Il7+SwvlZUetqGJWlytFgrgfiY4qF5o4VkaKic3N2mkJkp4nVObaKynhoMxi3cAE8+H/ vN7/3xfVVG/s4pp1nva58R9oVCvZYN4eB0MgAYDTWjA3yOvaPM4A6zHGhWg4jQWy5EZy 6Inw== X-Gm-Message-State: APjAAAXnGfg0aYzRgz4kGHHwelNvIszy/dIbUxV/UsNRRi1geR11+H33 /hVzMS6LeWIpXCXVBA5Ckjq+Q6T2u0lTFA== X-Google-Smtp-Source: APXvYqwgHcum1wu7NMQryt4s3kdsWiHCV3vJ+/aDv8wZmVHDgpg2yBt409KP4EdnHJifTkjyoJpwpw== X-Received: by 2002:a7b:cc97:: with SMTP id p23mr4519445wma.111.1569945408656; Tue, 01 Oct 2019 08:56:48 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:47 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 1/7] firmware: qcom: scm: Add function to set IOMMU pagetable addressing Date: Tue, 1 Oct 2019 17:56:35 +0200 Message-Id: <20191001155641.37117-2-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: "Angelo G. Del Regno" Add a function to change the IOMMU pagetable addressing to AArch32 LPAE or AArch64. If doing that, then this must be done for each IOMMU context (not necessarily at the same time). --- drivers/firmware/qcom_scm-32.c | 6 ++++++ drivers/firmware/qcom_scm-64.c | 15 +++++++++++++++ drivers/firmware/qcom_scm.c | 7 +++++++ drivers/firmware/qcom_scm.h | 4 ++++ drivers/firmware/qcom_scm.h.rej | 13 +++++++++++++ include/linux/qcom_scm.h | 2 ++ include/linux/qcom_scm.h.rej | 19 +++++++++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 drivers/firmware/qcom_scm.h.rej create mode 100644 include/linux/qcom_scm.h.rej diff --git a/drivers/firmware/qcom_scm-32.c b/drivers/firmware/qcom_scm-32.c index 215061c581e1..f5437e5c8c4e 100644 --- a/drivers/firmware/qcom_scm-32.c +++ b/drivers/firmware/qcom_scm-32.c @@ -597,6 +597,12 @@ int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, u32 size, return -ENODEV; } +int __qcom_scm_iommu_set_pt_format(struct device *dev, u32 sec_id, u32 ctx_num, + u32 pt_fmt) +{ + return -ENODEV; +} + int __qcom_scm_io_readl(struct device *dev, phys_addr_t addr, unsigned int *val) { diff --git a/drivers/firmware/qcom_scm-64.c b/drivers/firmware/qcom_scm-64.c index 91d5ad7cf58b..aab5446ea9f2 100644 --- a/drivers/firmware/qcom_scm-64.c +++ b/drivers/firmware/qcom_scm-64.c @@ -459,6 +459,21 @@ int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, u32 size, return ret; } +int __qcom_scm_iommu_set_pt_format(struct device *dev, u32 sec_id, u32 ctx_num, + u32 pt_fmt) +{ + struct qcom_scm_desc desc = {0}; + struct arm_smccc_res res; + + desc.args[0] = sec_id; + desc.args[1] = ctx_num; + desc.args[2] = pt_fmt; /* 0: LPAE AArch32 - 1: AArch64 */ + desc.arginfo = QCOM_SCM_ARGS(3); + + return qcom_scm_call(dev, QCOM_SCM_SVC_SMMU_PROGRAM, + QCOM_SCM_IOMMU_PT_FORMAT, &desc, &res); +} + int __qcom_scm_set_dload_mode(struct device *dev, bool enable) { struct qcom_scm_desc desc = {0}; diff --git a/drivers/firmware/qcom_scm.c b/drivers/firmware/qcom_scm.c index 4802ab170fe5..41af7dc0b34d 100644 --- a/drivers/firmware/qcom_scm.c +++ b/drivers/firmware/qcom_scm.c @@ -345,6 +345,13 @@ int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare) } EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init); +int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt) +{ + return __qcom_scm_iommu_set_pt_format(__scm->dev, sec_id, + ctx_num, pt_fmt); +} +EXPORT_SYMBOL(qcom_scm_iommu_set_pt_format); + int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val) { return __qcom_scm_io_readl(__scm->dev, addr, val); diff --git a/drivers/firmware/qcom_scm.h b/drivers/firmware/qcom_scm.h index 99506bd873c0..f04a3903dc6c 100644 --- a/drivers/firmware/qcom_scm.h +++ b/drivers/firmware/qcom_scm.h @@ -95,6 +95,10 @@ extern int __qcom_scm_iommu_secure_ptbl_size(struct device *dev, u32 spare, size_t *size); extern int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, u32 size, u32 spare); +#define QCOM_SCM_SVC_SMMU_PROGRAM 0x15 +#define QCOM_SCM_IOMMU_PT_FORMAT 1 +extern int __qcom_scm_iommu_set_pt_format(struct device *dev, u32 sec_id, + u32 ctx_num, u32 pt_fmt); #define QCOM_MEM_PROT_ASSIGN_ID 0x16 extern int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region, size_t mem_sz, diff --git a/drivers/firmware/qcom_scm.h.rej b/drivers/firmware/qcom_scm.h.rej new file mode 100644 index 000000000000..05bb1a91dca5 --- /dev/null +++ b/drivers/firmware/qcom_scm.h.rej @@ -0,0 +1,13 @@ +--- drivers/firmware/qcom_scm.h ++++ drivers/firmware/qcom_scm.h +@@ -98,6 +98,10 @@ extern int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, + u32 size, u32 spare); + extern int __qcom_scm_iommu_set_cp_pool_size(struct device *dev, u32 spare, + u32 size); ++#define QCOM_SCM_SVC_SMMU_PROGRAM 0x15 ++#define QCOM_SCM_IOMMU_PT_FORMAT 1 ++extern int __qcom_scm_iommu_set_pt_format(struct device *dev, u32 sec_id, ++ u32 ctx_num, u32 pt_fmt); + #define QCOM_MEM_PROT_ASSIGN_ID 0x16 + extern int __qcom_scm_assign_mem(struct device *dev, + phys_addr_t mem_region, size_t mem_sz, diff --git a/include/linux/qcom_scm.h b/include/linux/qcom_scm.h index 2d5eff506e13..49937d62e462 100644 --- a/include/linux/qcom_scm.h +++ b/include/linux/qcom_scm.h @@ -58,6 +58,7 @@ extern int qcom_scm_set_remote_state(u32 state, u32 id); extern int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare); extern int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size); extern int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare); +extern int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt); extern int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val); extern int qcom_scm_io_writel(phys_addr_t addr, unsigned int val); #else @@ -97,6 +98,7 @@ qcom_scm_set_remote_state(u32 state,u32 id) { return -ENODEV; } static inline int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare) { return -ENODEV; } static inline int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size) { return -ENODEV; } static inline int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare) { return -ENODEV; } +static inline int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt) { return -ENODEV; } static inline int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val) { return -ENODEV; } static inline int qcom_scm_io_writel(phys_addr_t addr, unsigned int val) { return -ENODEV; } #endif diff --git a/include/linux/qcom_scm.h.rej b/include/linux/qcom_scm.h.rej new file mode 100644 index 000000000000..138b48e29999 --- /dev/null +++ b/include/linux/qcom_scm.h.rej @@ -0,0 +1,19 @@ +--- include/linux/qcom_scm.h ++++ include/linux/qcom_scm.h +@@ -59,6 +59,7 @@ extern int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare); + extern int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size); + extern int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare); + extern int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size); ++extern int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt); + extern int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val); + extern int qcom_scm_io_writel(phys_addr_t addr, unsigned int val); + #else +@@ -99,6 +100,8 @@ static inline int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare) { return -E + static inline int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size) { return -ENODEV; } + static inline int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare) { return -ENODEV; } + static inline int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size) { return -ENODEV; } ++static inline int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, ++ u32 pt_fmt) { return -ENODEV; } + static inline int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val) { return -ENODEV; } + static inline int qcom_scm_io_writel(phys_addr_t addr, unsigned int val) { return -ENODEV; } + #endif From patchwork Tue Oct 1 15:56:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169159 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8BCAE17EE for ; Tue, 1 Oct 2019 15:56:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5A04B2168B for ; Tue, 1 Oct 2019 15:56:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aKzXzm76" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727352AbfJAP4w (ORCPT ); Tue, 1 Oct 2019 11:56:52 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:45680 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727402AbfJAP4w (ORCPT ); Tue, 1 Oct 2019 11:56:52 -0400 Received: by mail-wr1-f67.google.com with SMTP id r5so16145383wrm.12 for ; Tue, 01 Oct 2019 08:56:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hteX5Po5jH9mp6pG+BqMJxAmFHZvrUZhbwHbRe2F5fc=; b=aKzXzm76HfjoXhv0IEyTeJT5jxxYKt/jbPKhPL0uETwbxPaz+J1kM9JhoifyzvY8WL QTWuiBd8l8mWnQP8GIQf56FGbZvPUAqyHIShSrxU2x2m9pZ6B5i/F6CKEKQvc8PXV2dp tfp4QUaNVbPXqRatm4T8PTERR6FXzgqpsjEMFElQ7qKKUuOb2Hvah6pcubK5bd8meCgz ME2JuTwuUTGwczbuTDY+SjBxumgxYR4EIRv2Dp/zUXcBCcIVcJTN1asUyI2cuxkIdCrc eTFfOI8tFhkYj/2lTlUeqbiT1H5nFyZrUmDplrtA4Jn2y0LKrX472/KGyQJGx4ezqFNh tzLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hteX5Po5jH9mp6pG+BqMJxAmFHZvrUZhbwHbRe2F5fc=; b=Z62PJYimXXaHBYu1gMvkOs1wwWfUjaS4YTAoHqFKGW3VquV5v5qTcIs5x2T/rOxKwv b1X3Wf8zNqvxjYYoag71ogIMFKyEwUZKX3us/hkf0DsitMXKzp5FdRn85EyayMVuXLe7 WIjq5icScB5GEBWHB5ZbMxEwbkbuVB3C+JJMbInqqclgXE2qnZBdDoox9St8xAb2Xxby TvcBwQ7X+F/0TG0NCRSpk4y/FyoTOx3KDmHtx43XCK5VvniAK45VvxFwgI0m0OXft/aZ h+sBJy3SSPeDlTgfGSMWG03BYGcK1d9EW9e9cRGEJ5aCXBLPTGC5TdGk//jmL+Wi3gUl OvVg== X-Gm-Message-State: APjAAAVQ80HSJhqTw1bStJBVKtteWUwZ47hL5Pr4byM1p39TY9Kf6Gnc rG20qXPJjaFm8B6V93daihy2NNO+X+m+pw== X-Google-Smtp-Source: APXvYqx5gg7wLTncQSXDPIHGvIRYQdrcUmR+eKSlpt2JGc3eEGQ2cWL9tMeEsLIxSZd5DP+t0waoLQ== X-Received: by 2002:a5d:5352:: with SMTP id t18mr19212910wrv.72.1569945410017; Tue, 01 Oct 2019 08:56:50 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:48 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 2/7] iommu/qcom: Use the asid read from device-tree if specified Date: Tue, 1 Oct 2019 17:56:36 +0200 Message-Id: <20191001155641.37117-3-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno As specified in this driver, the context banks are 0x1000 apart. Problem is that sometimes the context number (our asid) does not match this logic and we end up using the wrong one: this starts being a problem in the case that we need to send TZ commands to do anything on a specific context. For this reason, read the ASID from the DT if the property "qcom,ctx-num" is present on the IOMMU context node. Signed-off-by: AngeloGioacchino Del Regno --- .../devicetree/bindings/iommu/qcom,iommu.txt | 1 + drivers/iommu/qcom_iommu.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt index 059139abce35..ba0b77889f02 100644 --- a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt +++ b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt @@ -46,6 +46,7 @@ to non-secure vs secure interrupt line. for routing of context bank irq's to secure vs non- secure lines. (Ie. if the iommu contains secure context banks) +- qcom,ctx-num : The number associated to the context bank ** Examples: diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index c31e7bc4ccbe..504ca69adc5a 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -585,7 +585,8 @@ static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) * index into qcom_iommu->ctxs: */ if (WARN_ON(asid < 1) || - WARN_ON(asid > qcom_iommu->num_ctxs)) + WARN_ON(asid > qcom_iommu->num_ctxs) || + WARN_ON(qcom_iommu->ctxs[asid - 1] == NULL)) return -EINVAL; if (!fwspec->iommu_priv) { @@ -693,7 +694,8 @@ static int qcom_iommu_sec_ptbl_init(struct device *dev) static int get_asid(const struct device_node *np) { - u32 reg; + u32 reg, val; + int asid; /* read the "reg" property directly to get the relative address * of the context bank, and calculate the asid from that: @@ -701,7 +703,16 @@ static int get_asid(const struct device_node *np) if (of_property_read_u32_index(np, "reg", 0, ®)) return -ENODEV; - return reg / 0x1000; /* context banks are 0x1000 apart */ + /* Context banks are 0x1000 apart but, in some cases, the ASID + * number doesn't match to this logic and needs to be passed + * from the DT configuration explicitly. + */ + if (of_property_read_u32(np, "qcom,ctx-num", &val)) + asid = reg / 0x1000; + else + asid = val; + + return asid; } static int qcom_iommu_ctx_probe(struct platform_device *pdev) From patchwork Tue Oct 1 15:56:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169161 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4D7F11709 for ; Tue, 1 Oct 2019 15:56:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2B0402168B for ; Tue, 1 Oct 2019 15:56:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="V1vQ89R/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727402AbfJAP4x (ORCPT ); Tue, 1 Oct 2019 11:56:53 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:37727 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727387AbfJAP4x (ORCPT ); Tue, 1 Oct 2019 11:56:53 -0400 Received: by mail-wm1-f68.google.com with SMTP id f22so3837010wmc.2 for ; Tue, 01 Oct 2019 08:56:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xSQ3Gd/I78crTYWoKFJLF2LHykp/l0XNK/1ec4ayqbI=; b=V1vQ89R/Kqditgwc5pKk80fE3GZCa1M2VMFrKsoiHVsSFzdubkBC1dSY7cp+F+4CnG yx4S5GcUUx4RCfC/He6n3wJO/TBKg+4Fbfw+DYfxdIXNIsjTMb79xbXGBsY1CApbJ73e B7fL9Kmxgv7T9BrBuHV8WHnFMhdEanjkFF9lvqpzD+6vEXsVCEWZ6UAmnL/448APy6/s o2EurwEEHTBGAhKdcpAZaJ5NFX5zZ/pSY7CH5Q5XOJPr6k0Tgkl41hrpBRkMD3Z5AxWv bOYrdDUvbswO1RIwb4KyP7VVipZyURCrTj6MBgh/g2AmvzD6QFiy0ImJORliolT+hx8j +RVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xSQ3Gd/I78crTYWoKFJLF2LHykp/l0XNK/1ec4ayqbI=; b=lrxaqOBlnSHulUYLrv+kyBQOB0kUmXtZRtPPxcabTbd7Ssm1mkKuJzZS94KuFK4LF4 AY7usH8WMiRtBy6vd/EL65HW4k5G+aIKEwN6Xf21jYZ+nKEM102erLQiZghl3xmhoUrK JEk7kOTAISK4l1PmVrWutYwJjr0uLYxbrqtg6UNpZHqajiptJLmgMIlcmmrTmvY9hscB iq0p7YKxvoxAFPQEPo0SC//G4h9rilpiGKkljZyS3Ll4dKfuzS2Nn0WazIZ4yVWrGrCy 99/hS70aYczL8SEY+jAPyXbsUqc4ZANkf45GmB665HYW1bLHPVO5L2Js0AGmT5KC0u9J 9R1A== X-Gm-Message-State: APjAAAUT0aRckfMSBEgpJfUzoY6t4Kaf52pU3haZj6P9F/coELC4Z2qK mTzCYFAbtgUZ4F+EbsqpfLAoY6Zi34j1kg== X-Google-Smtp-Source: APXvYqwzXz9dBSJ6C4W1d7sa97hjPnTKpVydssMEIdV2ySVhM7Ui5kvvVHIhHD6bkb2OOpa+uWFoqA== X-Received: by 2002:a05:600c:24d1:: with SMTP id 17mr4341973wmu.104.1569945411253; Tue, 01 Oct 2019 08:56:51 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:50 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 3/7] iommu/qcom: Write TCR before TTBRs to fix ASID access behavior Date: Tue, 1 Oct 2019 17:56:37 +0200 Message-Id: <20191001155641.37117-4-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno As also stated in the arm-smmu driver, we must write the TCR before writing the TTBRs, since the TCR determines the access behavior of some fields. Signed-off-by: AngeloGioacchino Del Regno --- drivers/iommu/qcom_iommu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index 504ca69adc5a..c8957ec83b92 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -267,6 +267,13 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, ctx->secure_init = true; } + /* TCR */ + iommu_writel(ctx, ARM_SMMU_CB_TCR2, + (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) | + FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM)); + iommu_writel(ctx, ARM_SMMU_CB_TCR, + pgtbl_cfg.arm_lpae_s1_cfg.tcr); + /* TTBRs */ iommu_writeq(ctx, ARM_SMMU_CB_TTBR0, pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0] | @@ -275,13 +282,6 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, pgtbl_cfg.arm_lpae_s1_cfg.ttbr[1] | FIELD_PREP(TTBRn_ASID, ctx->asid)); - /* TCR */ - iommu_writel(ctx, ARM_SMMU_CB_TCR2, - (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) | - FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM)); - iommu_writel(ctx, ARM_SMMU_CB_TCR, - pgtbl_cfg.arm_lpae_s1_cfg.tcr); - /* MAIRs (stage-1 only) */ iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0, pgtbl_cfg.arm_lpae_s1_cfg.mair[0]); From patchwork Tue Oct 1 15:56:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169165 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA67A14DB for ; Tue, 1 Oct 2019 15:56:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A659A2070B for ; Tue, 1 Oct 2019 15:56:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LgHSlTJt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389755AbfJAP44 (ORCPT ); Tue, 1 Oct 2019 11:56:56 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:37729 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389730AbfJAP44 (ORCPT ); Tue, 1 Oct 2019 11:56:56 -0400 Received: by mail-wm1-f68.google.com with SMTP id f22so3837049wmc.2 for ; Tue, 01 Oct 2019 08:56:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6cgkBIqWvnVkQ9N/MHRrfLaa6SBKNUc7qvNtHetLnKc=; b=LgHSlTJtM6cK49Al1Z12BQ+7Ier1q5prnBaqn1S3RLrKU/T3qh6vHmdePSmc3lGQtf Goah4jYL7Tg1wDAUGk+I7aVapLc5so3swNqB2NEkMK5hWUt+Surd4uDMTzMKtvn9UqTp 8y0DSIh/gAb95dD/RSzAQ2eOPE/6gPTElFbYnn0h9uCSu8bo1TJrS6lBmUip524qCSWU 4pRL6aRUPspYO1HojLppoVIASv3O7fXSWuXMH/sPJ8Ag2/MJZEgpm7bbzbutR1GwVZlt t7Ca2zAM4RlfWRqxTKEHXvfHp8FWhVL1t/TMq5hrDOdGtKfbIi+370Iu8GHa7ifnF7Nu XUjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6cgkBIqWvnVkQ9N/MHRrfLaa6SBKNUc7qvNtHetLnKc=; b=gTgVXiYOkNgXGqLjwGWdxLDtSwIYzTnL5pmZ9lHdOVP1k+Sy7epKv7Ddx/0DDiyh5o B8lufXLvR1L1xuXlbXPColNWfoVy23HW93v7JuE1IxCVmc/jyW/GMiD6mhBjPiVTNxVx 8cvJ1XYipCw4Cjt3N3Wh2/ZHMcEEVYItxmGEJf/FTZdDqa73FzQ/6GR4G3Y3RxaU0RwS AXUh/9FfBJJ3CCK9H1VyiAJvgesbi/lpcyjK7PvclSzvU4w3Neaz2NZ4ltp1hpKqBAmm J0sixQI7KFVuyTb3G30bDqb5wkDSpjBriYiuNDUFIkiujdvJ3C2ILu5jv+Gi9Skkdlvw ig5w== X-Gm-Message-State: APjAAAX9N4T2PATYHZH6eRVpiILeYD+2ZJcCk6QwgsnB5XnzsPz2nW4i ztz4UfOEpDlxK2a9zYPSiH0PuC8gr5IGfw== X-Google-Smtp-Source: APXvYqw9YakoyUGtMa2OVJLxprPkitOKXvxCKUMc+ka7ce13jXusaTSuhmRvS+qox3IhiSpWQdFHMA== X-Received: by 2002:a05:600c:238a:: with SMTP id m10mr4675523wma.51.1569945412138; Tue, 01 Oct 2019 08:56:52 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:51 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 4/7] iommu/qcom: Properly reset the IOMMU context Date: Tue, 1 Oct 2019 17:56:38 +0200 Message-Id: <20191001155641.37117-5-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno To avoid context faults reset the context entirely on detach and to ensure a fresh clean start also do a complete reset before programming the context for domain initialization. Signed-off-by: AngeloGioacchino Del Regno --- drivers/iommu/qcom_iommu.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index c8957ec83b92..b4a38ef129e3 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -220,6 +220,23 @@ static irqreturn_t qcom_iommu_fault(int irq, void *dev) return IRQ_HANDLED; } +static void qcom_iommu_reset_ctx(struct qcom_iommu_ctx *ctx) +{ + iommu_writel(ctx, ARM_SMMU_CB_FAR, 0); + iommu_writel(ctx, ARM_SMMU_CB_FSR, 0); + iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1, 0); + iommu_writel(ctx, ARM_SMMU_CB_PAR, 0); + iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0, 0); + iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0); + iommu_writel(ctx, ARM_SMMU_CB_TCR2, 0); + iommu_writel(ctx, ARM_SMMU_CB_TCR, 0); + iommu_writeq(ctx, ARM_SMMU_CB_TTBR0, 0); + iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 0); + + /* Should we issue a TLBSYNC there instead? */ + mb(); +} + static int qcom_iommu_init_domain(struct iommu_domain *domain, struct qcom_iommu_dev *qcom_iommu, struct iommu_fwspec *fwspec) @@ -267,6 +284,8 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, ctx->secure_init = true; } + qcom_iommu_reset_ctx(ctx); + /* TCR */ iommu_writel(ctx, ARM_SMMU_CB_TCR2, (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) | @@ -412,8 +431,8 @@ static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *de for (i = 0; i < fwspec->num_ids; i++) { struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); - /* Disable the context bank: */ - iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0); + /* Disable and reset the context bank */ + qcom_iommu_reset_ctx(ctx); ctx->domain = NULL; } From patchwork Tue Oct 1 15:56:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169163 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3471A1709 for ; Tue, 1 Oct 2019 15:56:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 07B1321855 for ; Tue, 1 Oct 2019 15:56:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NCo4q3Yj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389744AbfJAP4z (ORCPT ); Tue, 1 Oct 2019 11:56:55 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42591 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727387AbfJAP4z (ORCPT ); Tue, 1 Oct 2019 11:56:55 -0400 Received: by mail-wr1-f67.google.com with SMTP id n14so16160972wrw.9 for ; Tue, 01 Oct 2019 08:56:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZWSfHME9EykpkSiHCNCJB/NwhqPlKPFe6uMd6py2CPE=; b=NCo4q3Yj8QLR8nmKX81BQNGieIKyAzcdACoeiFB3UHaGZvQ5T/vnnE53kLXRfCmTfR iqU3kkvQJ2ZgpQz9efLyiNvp4uJotj/mbXFezLUomfURJiwZuD4rx7+oQkjGtg10KtBz QlQxR9G9XmG/7hP3/N9s/EvBrOuZQlh9e4+XttXkqVeFnA13VuYjARQSTlqI1HhBWjhu tBoOJ4rA8m8gBlvBf2eLKPB5hk8Lz3S97i5/DlfGxI34wUOpu+3dT69imUX/s0sozO1I tYpMG7Qy0TquWn7eABxwBxymR7tdxqh/IA5J0DEX87oNEo65uUCBsncCJm1LO4gLxC7A tZ+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZWSfHME9EykpkSiHCNCJB/NwhqPlKPFe6uMd6py2CPE=; b=JeiksNV8YAdZ7gZedNR472jOEo8cRunVhoTOQ6uAkH/hAoTn7j8PBcjfNl4jS0RGMX y16rxqUd/E4NKQxBaSc1Fd1IVnOQMR5rZ/b+lu1li0ox6/XwC4UTHTxvD+siAlpk4qei u7D1FHmILqYcPwCfPz7vxO449T7XHqD06lS+RoHoTFPtaxLMfg39BUQTJHusyN298OL5 tZyWzPxj1/BLfQ9Lj9EOY7CfJ7Htn5gxV/Eocf/qP8OSxcLXm1LbeSuw7y/gloU2mMN1 ZWVZnUetKesBBzSccqtF3vs+A9C9U4l35/pMXbr3HVHXCAQZBIjjwCxueGWTn7MXgtVs lD6w== X-Gm-Message-State: APjAAAXVetLRRitUKwnaxn81N4I3ez++OJxe35yPUwN5J25cjkq138n6 gT18aWKcBr1mK5VMzov9zTwbk2YxApSeMw== X-Google-Smtp-Source: APXvYqyd3Xi3KLE97kpVPM+DoKJEFygDlOB1uTxQHVZ92EpOpcHON9Ot92PWS4jnNfAQP3SychwDPg== X-Received: by 2002:adf:efcb:: with SMTP id i11mr18856063wrp.69.1569945413036; Tue, 01 Oct 2019 08:56:53 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:52 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 5/7] iommu/qcom: Add support for AArch64 IOMMU pagetables Date: Tue, 1 Oct 2019 17:56:39 +0200 Message-Id: <20191001155641.37117-6-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno Some IOMMUs associated with some TZ firmwares may support switching to the AArch64 pagetable format by sending a "set pagetable format" scm command indicating the IOMMU secure ID and the context number to switch. Add a DT property "qcom,use-aarch64-pagetables" for this driver to send this command to the secure world and to switch the pagetable format to benefit of the ARM64 IOMMU pagetables, where possible. Note that, even though the command should be valid to switch each context, the property is made global because: 1. It doesn't make too much sense to switch only one or two context(s) to AA64 instead of just the entire thing 2. Some IOMMUs will go crazy and produce spectacular results when trying to mix up the pagetables on a per-context basis. Signed-off-by: AngeloGioacchino Del Regno --- .../devicetree/bindings/iommu/qcom,iommu.txt | 2 + drivers/iommu/qcom_iommu.c | 55 +++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt index ba0b77889f02..72ae0595efff 100644 --- a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt +++ b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt @@ -47,6 +47,8 @@ to non-secure vs secure interrupt line. secure lines. (Ie. if the iommu contains secure context banks) - qcom,ctx-num : The number associated to the context bank +- qcom,use-aarch64-pagetables : Switch to AArch64 pagetable format on all + contexts declared in this IOMMU ** Examples: diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index b4a38ef129e3..dac77dbec0f0 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -48,6 +48,7 @@ struct qcom_iommu_dev { void __iomem *local_base; u32 sec_id; u8 num_ctxs; + bool use_aarch64_pt; struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */ }; @@ -153,11 +154,17 @@ static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size, reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA; for (i = 0; i < fwspec->num_ids; i++) { + struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec); struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); size_t s = size; - iova = (iova >> 12) << 12; - iova |= ctx->asid; + if (qcom_iommu->use_aarch64_pt) { + iova >>= 12; + iova |= (u64)ctx->asid << 48; + } else { + iova = (iova >> 12) << 12; + iova |= ctx->asid; + } do { iommu_writel(ctx, reg, iova); iova += granule; @@ -244,6 +251,8 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); struct io_pgtable_ops *pgtbl_ops; struct io_pgtable_cfg pgtbl_cfg; + enum io_pgtable_fmt pgtbl_fmt; + unsigned long ias, oas; int i, ret = 0; u32 reg; @@ -251,16 +260,25 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, if (qcom_domain->iommu) goto out_unlock; + if (qcom_iommu->use_aarch64_pt) { + pgtbl_fmt = ARM_64_LPAE_S1; + ias = oas = 48; + } else { + pgtbl_fmt = ARM_32_LPAE_S1; + ias = 32; + oas = 40; + } + pgtbl_cfg = (struct io_pgtable_cfg) { .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap, - .ias = 32, - .oas = 40, + .ias = ias, + .oas = oas, .tlb = &qcom_flush_ops, .iommu_dev = qcom_iommu->dev, }; qcom_domain->iommu = qcom_iommu; - pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec); + pgtbl_ops = alloc_io_pgtable_ops(pgtbl_fmt, &pgtbl_cfg, fwspec); if (!pgtbl_ops) { dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n"); ret = -ENOMEM; @@ -274,6 +292,7 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, for (i = 0; i < fwspec->num_ids; i++) { struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); + u32 tcr[2]; if (!ctx->secure_init) { ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid); @@ -286,12 +305,25 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, qcom_iommu_reset_ctx(ctx); + tcr[0] = pgtbl_cfg.arm_lpae_s1_cfg.tcr; + tcr[1] = pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32; + tcr[1] |= FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM); + + if (qcom_iommu->use_aarch64_pt) { + /* This shall not fail, or spectacular things happen */ + if (qcom_scm_iommu_set_pt_format(qcom_iommu->sec_id, + ctx->asid, 1)) { + dev_warn(qcom_iommu->dev, + "Cannot set AArch64 pt format\n"); + goto out_clear_iommu; + } + + tcr[1] |= TCR2_AS; + } + /* TCR */ - iommu_writel(ctx, ARM_SMMU_CB_TCR2, - (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) | - FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM)); - iommu_writel(ctx, ARM_SMMU_CB_TCR, - pgtbl_cfg.arm_lpae_s1_cfg.tcr); + iommu_writel(ctx, ARM_SMMU_CB_TCR2, tcr[1]); + iommu_writel(ctx, ARM_SMMU_CB_TCR, tcr[0]); /* TTBRs */ iommu_writeq(ctx, ARM_SMMU_CB_TTBR0, @@ -869,6 +901,9 @@ static int qcom_iommu_device_probe(struct platform_device *pdev) return -ENODEV; } + if (of_property_read_bool(dev->of_node, "qcom,use-aarch64-pagetables")) + qcom_iommu->use_aarch64_pt = true; + if (qcom_iommu_has_secure_context(qcom_iommu)) { ret = qcom_iommu_sec_ptbl_init(dev); if (ret) { From patchwork Tue Oct 1 15:56:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169167 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 799491709 for ; Tue, 1 Oct 2019 15:56:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5784D21872 for ; Tue, 1 Oct 2019 15:56:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BMLOwJbW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389730AbfJAP45 (ORCPT ); Tue, 1 Oct 2019 11:56:57 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:41964 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389739AbfJAP44 (ORCPT ); Tue, 1 Oct 2019 11:56:56 -0400 Received: by mail-wr1-f67.google.com with SMTP id h7so16199008wrw.8 for ; Tue, 01 Oct 2019 08:56:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AzvndIhQVi7PkccKpszP9q3kYn4x6KQ1z0v2TlWXPc4=; b=BMLOwJbWkVn0zbUlNrg3GdPdEor9qWoQ6lh4jslJvSqyg63M9hNBBRzpsnprlOU/Wi OHpgXn9kmFIgNXyBU4wfKxovsDa5VWwU/BItTTle3W8EPdGSkBLZ6fE5Ab0XVGq2z5zK 8TcovKGHr1z+OXUDyTNts7LIr1thdT5onv9QJ0EFIsbyEsUfU0TzLrZCP+oY43Pz0Xxg y/SjAkQYnEw+DTdTS70GSjqExFUVxcwiXqE5BoQ68g4y2MQaFOvv82RQXN3upp0aHZdJ Nnp4gOHpNQkn23PYBJrqn3cknq14wp61RIi4ORoq2W9RBiMHkMZGgmrP2cM/lQxKOx7q dKcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AzvndIhQVi7PkccKpszP9q3kYn4x6KQ1z0v2TlWXPc4=; b=MINt1Z2zYcztuJRDljdoxGZzmJek0n2gcqNzhO3qonZO12lKGceSAuPykMGeFuWp55 ggV4wMucHRJNN/iVv2SgGagLFmTdIsCD0bTDy6SVasKM68WniFyQf0BRW3xh/Zg9Drfq iivWDUoo88zcRa31QyGek79Nu0OxdtQZXGMosFOEQjf6fqPSm2JTdLfimDkajAMh191U ZK3xE6KDTVw89/8GoqMVg/m/ivibOQmAtwtLqR1+0PUTyk0+3ES538aaCNJ4Z5BmhYnD MrABzViAfJebbaQpFims/V4wsenAYl5LyDo+RZus7WTktJFic4H0ar8xF/AnMzw3KmvJ g9ag== X-Gm-Message-State: APjAAAUs7Jfp+rJwWJbcjHO4MJhLEeVMflzKUExMKU0E84dWgmZWsxlk JOeN5UHuTo3mu+FXpFEvUf5GJfvtbN0owg== X-Google-Smtp-Source: APXvYqySbQPELpv1ZumQxF1Vk5aOIdlPrRFDCczcUDGUWaI3DVVZc+ik+LxF2F0v9dK/O8T+CAKY2w== X-Received: by 2002:a5d:5231:: with SMTP id i17mr18612203wra.142.1569945414006; Tue, 01 Oct 2019 08:56:54 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:53 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 6/7] iommu/qcom: Index contexts by asid number to allow asid 0 Date: Tue, 1 Oct 2019 17:56:40 +0200 Message-Id: <20191001155641.37117-7-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno This driver was indexing the contexts by asid-1, which is probably done under the assumption that the first ASID is always 1. Unfortunately this is not entirely true: at least in the MSM8956 and MSM8976 GPU IOMMU, the gpu_user context's ASID number is zero. To allow using an asid number of zero, stop indexing the contexts by asid-1 and rather index them by asid. Signed-off-by: AngeloGioacchino Del Regno --- drivers/iommu/qcom_iommu.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index dac77dbec0f0..555cbc55b073 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -49,7 +49,7 @@ struct qcom_iommu_dev { u32 sec_id; u8 num_ctxs; bool use_aarch64_pt; - struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */ + struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid */ }; struct qcom_iommu_ctx { @@ -87,7 +87,7 @@ static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec); if (!qcom_iommu) return NULL; - return qcom_iommu->ctxs[asid - 1]; + return qcom_iommu->ctxs[asid]; } static inline void @@ -632,12 +632,10 @@ static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) qcom_iommu = platform_get_drvdata(iommu_pdev); /* make sure the asid specified in dt is valid, so we don't have - * to sanity check this elsewhere, since 'asid - 1' is used to - * index into qcom_iommu->ctxs: + * to sanity check this elsewhere: */ - if (WARN_ON(asid < 1) || - WARN_ON(asid > qcom_iommu->num_ctxs) || - WARN_ON(qcom_iommu->ctxs[asid - 1] == NULL)) + if (WARN_ON(asid > qcom_iommu->num_ctxs) || + WARN_ON(qcom_iommu->ctxs[asid] == NULL)) return -EINVAL; if (!fwspec->iommu_priv) { @@ -815,7 +813,7 @@ static int qcom_iommu_ctx_probe(struct platform_device *pdev) dev_dbg(dev, "found asid %u\n", ctx->asid); - qcom_iommu->ctxs[ctx->asid - 1] = ctx; + qcom_iommu->ctxs[ctx->asid] = ctx; return 0; } @@ -827,7 +825,7 @@ static int qcom_iommu_ctx_remove(struct platform_device *pdev) platform_set_drvdata(pdev, NULL); - qcom_iommu->ctxs[ctx->asid - 1] = NULL; + qcom_iommu->ctxs[ctx->asid] = NULL; return 0; } @@ -872,8 +870,8 @@ static int qcom_iommu_device_probe(struct platform_device *pdev) for_each_child_of_node(dev->of_node, child) max_asid = max(max_asid, get_asid(child)); - qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, max_asid), - GFP_KERNEL); + qcom_iommu = devm_kzalloc(dev, struct_size(qcom_iommu, ctxs, + max_asid + 1), GFP_KERNEL); if (!qcom_iommu) return -ENOMEM; qcom_iommu->num_ctxs = max_asid; From patchwork Tue Oct 1 15:56:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AngeloGioacchino Del Regno X-Patchwork-Id: 11169169 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 236A717EE for ; Tue, 1 Oct 2019 15:56:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 01B462168B for ; Tue, 1 Oct 2019 15:56:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G8LV0yqz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727387AbfJAP46 (ORCPT ); Tue, 1 Oct 2019 11:56:58 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40168 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389749AbfJAP46 (ORCPT ); Tue, 1 Oct 2019 11:56:58 -0400 Received: by mail-wm1-f66.google.com with SMTP id b24so3814221wmj.5 for ; Tue, 01 Oct 2019 08:56:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hNjIRfETItHrHAwc5fX92U90M7+/WUq/4UckUIkwrKY=; b=G8LV0yqz0/7wpieIGxNYSdqop3flOifTawe8gjYNoU94tIFrlXcnUj3In0WrJ/wdp5 VQ9cyacalBh9d2zHkevulhWmqoNzqVjfNfHuLvNty86B3RuLm7e3Mr6CUADfUDt9/4rh ElTOHvJYSAMcl2KiGc1CLnz1bo9A/FAyQkqhyYjX2VnU86puYiMAwPNtAeGBJLDs4W+V /Sg/PP3EdmdOFSpdtQDiXNrgGwij23q0uivTY+S2zov015zKUuhEjU2clxOWGo7kcW/+ nidSwZWD3u4KX7A2YkeACrF7z0Qfjq4/vXNF8B/QWHpAcxvDs81Olc+X0A6qy8Ku0esF KYwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hNjIRfETItHrHAwc5fX92U90M7+/WUq/4UckUIkwrKY=; b=HDOsV1NQmHq6dCgBfr/UUJT+hXfXDVr/F5pAedp+0rbQTyDOZisF9IhHgXv9WlI1qL t9aCQXUgRT7vwqutxjYnZrByNfUpEoMpqLRHRUBNSwYcK4YoZJUtGuXBfEaiR9ySX4fd lS6OIp8XaAKoQc4nMptSsS8YKxdUSajhsdgqWGmrwRU1En/NblMIR0yxVcB2xBGwPyrI 7IovvYKo5OtwK3/C3ov11VH0SMdj2TwoG/FvPoLkkM+Emyv+M6m0Ft0ErAwewRzoIkJl A+iJwJaQfKMnzSG8Wyol/Hg889C8y/BHq2o7KyUfVnI8pDlwYxatNdn2VMYRyczJmjNW DSwA== X-Gm-Message-State: APjAAAUeYIlnqb4BuCaTJOzoVEhjVtN+Ei9rQA4vbuaBb5aS+l1T2tM6 S0o1+vMdxBJZPfmUjTtwvo7K51Zw03monw== X-Google-Smtp-Source: APXvYqx8PnIeAQsmG0Lb0jl1J7pgyjxNk2QYamMWU7XansZtVzuzg1wCO8eZYi/ndnKurupThHzGyw== X-Received: by 2002:a1c:7c0b:: with SMTP id x11mr4332465wmc.92.1569945415237; Tue, 01 Oct 2019 08:56:55 -0700 (PDT) Received: from IcarusMOD.eternityproject.eu ([93.51.16.173]) by smtp.gmail.com with ESMTPSA id h63sm6209136wmf.15.2019.10.01.08.56.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Oct 2019 08:56:54 -0700 (PDT) From: kholk11@gmail.com To: linux-arm-msm@vger.kernel.org Cc: kholk11@gmail.com, iommu@lists.linux-foundation.org, marijns95@gmail.com, agross@kernel.org, robdclark@gmail.com, joro@8bytes.org Subject: [PATCH v3 7/7] iommu/qcom: Add support for QCIOMMUv2 and QCIOMMU-500 secured contexts Date: Tue, 1 Oct 2019 17:56:41 +0200 Message-Id: <20191001155641.37117-8-kholk11@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191001155641.37117-1-kholk11@gmail.com> References: <20191001155641.37117-1-kholk11@gmail.com> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: AngeloGioacchino Del Regno This IOMMU is yet another Qualcomm variant of known IOMMUs, found in Family-B SoCs, such as MSM8956, MSM8976, MSM8953, MSM8917 and others, and that firmware perfectly adheres to this driver logic. This time, though, the catch is that the secure contexts are also secured, meaning that these are programmed by the bootloader or TZ and their "interesting" registers are locked out, so the hypervisor disallows touching them from the non-secure world: in this case the OS is supposed to blindly trust the secure configuration of these contexts and just use them "as they are". For this reason, it is necessary to distinguish between the v1 and 500/v2 secure contexts in this driver in order to adhere to this specification. To do this, add a new DT compatible, named "qcom,msm-iommu-v2-sec" that will trigger the new behavior. For the sake of completeness, also add a "qcom,msm-iommu-v2-ns" so that the human eye gets pleased with it when reading the contexts in the final SoC DT. Of course, the latter is just cosmetic. Signed-off-by: AngeloGioacchino Del Regno --- .../devicetree/bindings/iommu/qcom,iommu.txt | 2 ++ drivers/iommu/qcom_iommu.c | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt index 72ae0595efff..861c0cd9c512 100644 --- a/Documentation/devicetree/bindings/iommu/qcom,iommu.txt +++ b/Documentation/devicetree/bindings/iommu/qcom,iommu.txt @@ -36,6 +36,8 @@ to non-secure vs secure interrupt line. - compatible : Should be one of: - "qcom,msm-iommu-v1-ns" : non-secure context bank - "qcom,msm-iommu-v1-sec" : secure context bank + - "qcom,msm-iommu-v2-ns" : non-secure QSMMUv2/QSMMU500 context bank + - "qcom,msm-iommu-v2-sec" : secure QSMMUv2/QSMMU500 context bank - reg : Base address and size of context bank within the iommu - interrupts : The context fault irq. diff --git a/drivers/iommu/qcom_iommu.c b/drivers/iommu/qcom_iommu.c index 555cbc55b073..eaecb009849c 100644 --- a/drivers/iommu/qcom_iommu.c +++ b/drivers/iommu/qcom_iommu.c @@ -56,6 +56,7 @@ struct qcom_iommu_ctx { struct device *dev; void __iomem *base; bool secure_init; + bool secured_ctx; u8 asid; /* asid and ctx bank # are 1:1 */ struct iommu_domain *domain; }; @@ -303,6 +304,12 @@ static int qcom_iommu_init_domain(struct iommu_domain *domain, ctx->secure_init = true; } + /* Secured QSMMU-500/QSMMU-v2 contexts cannot be programmed */ + if (ctx->secured_ctx) { + ctx->domain = domain; + break; + } + qcom_iommu_reset_ctx(ctx); tcr[0] = pgtbl_cfg.arm_lpae_s1_cfg.tcr; @@ -788,10 +795,15 @@ static int qcom_iommu_ctx_probe(struct platform_device *pdev) if (irq < 0) return -ENODEV; + if (of_device_is_compatible(dev->of_node, "qcom,msm-iommu-v2-sec")) + ctx->secured_ctx = true; + /* clear IRQs before registering fault handler, just in case the * boot-loader left us a surprise: */ - iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR)); + if (!ctx->secured_ctx) + iommu_writel(ctx, ARM_SMMU_CB_FSR, + iommu_readl(ctx, ARM_SMMU_CB_FSR)); ret = devm_request_irq(dev, irq, qcom_iommu_fault, @@ -833,6 +845,8 @@ static int qcom_iommu_ctx_remove(struct platform_device *pdev) static const struct of_device_id ctx_of_match[] = { { .compatible = "qcom,msm-iommu-v1-ns" }, { .compatible = "qcom,msm-iommu-v1-sec" }, + { .compatible = "qcom,msm-iommu-v2-ns" }, + { .compatible = "qcom,msm-iommu-v2-sec" }, { /* sentinel */ } }; @@ -850,7 +864,8 @@ static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu) struct device_node *child; for_each_child_of_node(qcom_iommu->dev->of_node, child) - if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) + if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec") || + of_device_is_compatible(child, "qcom,msm-iommu-v2-sec")) return true; return false;