From patchwork Tue Jul 5 12:58:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 1652505 Return-Path: X-Original-To: incoming-dt@patchwork.ozlabs.org Delivered-To: patchwork-incoming-dt@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=Py0vxhDT; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=devicetree-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4LckJ43Dwyz9s2R for ; Tue, 5 Jul 2022 23:37:40 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230054AbiGENhh (ORCPT ); Tue, 5 Jul 2022 09:37:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231307AbiGENhO (ORCPT ); Tue, 5 Jul 2022 09:37:14 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0AE474782; Tue, 5 Jul 2022 05:58:43 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id fd6so15169358edb.5; Tue, 05 Jul 2022 05:58:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/KFZ7cMq9JU+ArgnFFOioWDlil9QesIzP5/MdIscAXo=; b=Py0vxhDTdUr4+MxP647OOnJwClrnNnu90Pd79Y02NW8ZHIZiTv7rHNn/Z+ThujmZW9 g0WYwIiwfE+htVnBa/fyVdL587u9cXDLC0vIUNRJSth5U5nuPNnqvsPV8owIWnI4UypF vvcaZnMdZh+S17Gti6kcnvj4wynbzZ9gpjflsHdGCqR08vruWJp3Mzc5J69KrxShmamh 07pVeQOtmLgrdKFUbvlXSsHSolLtmw9r84deR3Du0qinOJvQTpqFR/AY3lWsvPSQ7mR5 4CEQ3Gq3Gqv01hiNcC18jkVqqIripnnkK2Y9NMEG1WjISnoLS07MYL49fI7WhWKUrlnk eqXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/KFZ7cMq9JU+ArgnFFOioWDlil9QesIzP5/MdIscAXo=; b=kBjlJWk81ND5HepYgUnLcH0wrcZV0u6ByJzuRzoCfQ2uZIRXaJQfBN5JgnDPcRDkzy D3OYQpEf4EivNYXWyezjsel6QHyOapvIoT6mR1W+418HuQ+x1gZA4QWBzQxcp5Pl44Pw 1zQQY1We/rQRJmljFR2zjgy6iOlLRW8g/pvDZAtBkjdPCtfEKyptpfdcWgbWdIcf9nuP p/uzw65zUr7I7gTmUIngqT4v6njVHbJy6g65gdi9Eo7WHa5343aI4mbbe2Zrn4/CT89d +nMsbfPtF0aKyfE7VT3XCqskoDVbGAPu8ZaHClQSE8eiIzkc8djtP0ofEP4A1ntlrEq/ vM7Q== X-Gm-Message-State: AJIora+tCPCtlqTVV93ftyN5rvlvncqUKt+rPniitoLXlQA+Aze4oYlD 3NBsU0pt+VS7Vp6gTGPj4YE= X-Google-Smtp-Source: AGRyM1tL7dLUK0tNEdwSBHVbXevA1qu+6i9xsWr/SUwd7OcqQyEtiWItJWgq2Lpy3YPrD0xM44NgDQ== X-Received: by 2002:a05:6402:149:b0:431:7dde:9b59 with SMTP id s9-20020a056402014900b004317dde9b59mr46118082edu.339.1657025921510; Tue, 05 Jul 2022 05:58:41 -0700 (PDT) Received: from localhost (p200300e41f12c800f22f74fffe1f3a53.dip0.t-ipconnect.de. [2003:e4:1f12:c800:f22f:74ff:fe1f:3a53]) by smtp.gmail.com with ESMTPSA id b9-20020aa7dc09000000b00437938c731fsm17191040edu.97.2022.07.05.05.58.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jul 2022 05:58:40 -0700 (PDT) From: Thierry Reding To: Rob Herring , Joerg Roedel Cc: Will Deacon , Robin Murphy , Nicolin Chen , Krishna Reddy , Dmitry Osipenko , Alyssa Rosenzweig , Janne Grunau , Sameer Pujar , devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, linux-tegra@vger.kernel.org, asahi@lists.linux.dev Subject: [PATCH v6 1/5] dt-bindings: reserved-memory: Document iommu-addresses Date: Tue, 5 Jul 2022 14:58:30 +0200 Message-Id: <20220705125834.431711-2-thierry.reding@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220705125834.431711-1-thierry.reding@gmail.com> References: <20220705125834.431711-1-thierry.reding@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Thierry Reding This adds the "iommu-addresses" property to reserved-memory nodes, which allow describing the interaction of memory regions with IOMMUs. Two use- cases are supported: 1. Static mappings can be described by pairing the "iommu-addresses" property with a "reg" property. This is mostly useful for adopting firmware-allocated buffers via identity mappings. One common use- case where this is required is if early firmware or bootloaders have set up a bootsplash framebuffer that a display controller is actively scanning out from during the operating system boot process. 2. If an "iommu-addresses" property exists without a "reg" property, the reserved-memory node describes an IOVA reservation. Such memory regions are excluded from the IOVA space available to operating system drivers and can be used for regions that must not be used to map arbitrary buffers. Each mapping or reservation is tied to a specific device via a phandle to the device's device tree node. This allows a reserved-memory region to be reused across multiple devices. Signed-off-by: Thierry Reding --- Changes in v6: - add device phandle to iommu-addresses property in examples - remove now unused dt-bindings/reserved-memory.h header .../reserved-memory/reserved-memory.txt | 1 - .../reserved-memory/reserved-memory.yaml | 62 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) delete mode 100644 Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt diff --git a/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt b/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt deleted file mode 100644 index 1810701a8509..000000000000 --- a/Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +++ /dev/null @@ -1 +0,0 @@ -This file has been moved to reserved-memory.yaml. diff --git a/Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml b/Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml index 7a0744052ff6..8b885ee82ff4 100644 --- a/Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml +++ b/Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml @@ -52,6 +52,30 @@ properties: Address and Length pairs. Specifies regions of memory that are acceptable to allocate from. + iommu-addresses: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: > + A list of phandle and specifier pairs that describe static IO virtual + address space mappings and carveouts associated with a given reserved + memory region. The phandle in the first cell refers to the device for + which the mapping or carveout is to be created. + + The specifier consists of an address/size pair and denotes the IO + virtual address range of the region for the given device. The exact + format depends on the values of the "#address-cells" and "#size-cells" + properties of the device referenced via the phandle. + + When used in combination with a "reg" property, an IOVA mapping is to + be established for this memory region. One example where this can be + useful is to create an identity mapping for physical memory that the + firmware has configured some hardware to access (such as a bootsplash + framebuffer). + + If no "reg" property is specified, the "iommu-addresses" property + defines carveout regions in the IOVA space for the given device. This + can be useful if a certain memory region should not be mapped through + the IOMMU. + no-map: type: boolean description: > @@ -97,4 +121,42 @@ oneOf: additionalProperties: true +examples: + - | + / { + #address-cells = <2>; + #size-cells = <2>; + + reserved-memory { + ranges; + + adsp_resv: reservation-adsp { + /* + * Restrict IOVA mappings for ADSP buffers to the 512 MiB region + * from 0x40000000 - 0x5fffffff. Anything outside is reserved by + * the ADSP for I/O memory and private memory allocations. + */ + iommu-addresses = <&adsp 0x0 0x00000000 0x00 0x40000000>, + <&adsp 0x0 0x60000000 0xff 0xa0000000>; + }; + + fb: framebuffer@90000000 { + reg = <0x0 0x90000000 0x0 0x00800000>; + iommu-addresses = <&dc0 0x0 0x90000000 0x0 0x00800000>; + }; + }; + + bus@0 { + adsp: adsp@2990000 { + reg = <0x0 0x2990000 0x0 0x2000>; + memory-region = <&adsp_resv>; + }; + + dc0: display@15200000 { + reg = <0x0 0x15200000 0x0 0x10000>; + memory-region = <&fb>; + }; + }; + }; + ... From patchwork Tue Jul 5 12:58:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 1652507 Return-Path: X-Original-To: incoming-dt@patchwork.ozlabs.org Delivered-To: patchwork-incoming-dt@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=JadRO2da; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=devicetree-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4LckJ66hytz9s2R for ; Tue, 5 Jul 2022 23:37:42 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230450AbiGENhh (ORCPT ); Tue, 5 Jul 2022 09:37:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232420AbiGENhQ (ORCPT ); Tue, 5 Jul 2022 09:37:16 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED5577479B; Tue, 5 Jul 2022 05:58:45 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id n8so15228374eda.0; Tue, 05 Jul 2022 05:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xCS/UTkATleI46kY+d+Ry97Ig9k9wIcwQdZNrwaHgpM=; b=JadRO2davE9HZRu1a2w8V+rwDgA+FRulCfg6dk/lwD24+Mc9YLHMLUaPrkZhsolQ5A +RQlApZFGH3nZf7J3Nwtxv2LpaunU60IFQpCmTioXYb27sJBaRjRbF9n2s70LoIi/zGX ubI/5bGIfWdhakq6tYOLYiWmR1QZ0Tj/qb0y43WYld84g4VQoL5aQoJ0KFvWT8Ri2o7c i2uF8Mm3rqj0r17EfAlBjt3fQbTmFXjd7XyHqYmV0F+o12ZLGZicSpUC03KfS6bL0Xzp XMKt+44Zz2B7iB7W/lF83FgfPImlcgcMXxbKgTP0X3/1J1Vx5tzaPcOvoLxSED9HggFG RsbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xCS/UTkATleI46kY+d+Ry97Ig9k9wIcwQdZNrwaHgpM=; b=yYnZesS+FBZOaxnX0LPIBK/6zBaLd2bjfs2PYI0iwdUS718SiT/YVv1X6c6z64N1IS 95VgCXLXet7/R7cboQ+78PHDeZ29Sp8XLWxe09RWCVAD6J0pnF1ju8as5Jci9hMHWM8V c5Ok0YP1kjJcPcsBjaDE6ix2m1gX+W7lPm22R91jrWxlqsZHYHk4U59rw2G0VE/quwN0 O6T0xlSh4os2S/l3qSXEnzRRojrzQK5hs4eFQqMc3DSeRy3PdDj1Ob5oqvfe7FmKbRpb RD0Zz17W6YAFQau8KIV4HKsLzLXYwxk2eFBHbwgtC3Dbs0vngwjgxmzezztVjXp/clFa 8xAA== X-Gm-Message-State: AJIora/aIctCQSkTv4beU4QDJpZGzqUVkZNHLq3D+roGoctPPFFn2lgx jTXvWBMLRTigccStheoCEtA= X-Google-Smtp-Source: AGRyM1s2xhTwQ+8WrilHKLkENRobG1A+q28m5fpH/FlUM+xi8Fke/LHaw3oMWgCrOd0SRuyLEREiyQ== X-Received: by 2002:a05:6402:4387:b0:435:94c6:716d with SMTP id o7-20020a056402438700b0043594c6716dmr46827461edc.298.1657025923139; Tue, 05 Jul 2022 05:58:43 -0700 (PDT) Received: from localhost (p200300e41f12c800f22f74fffe1f3a53.dip0.t-ipconnect.de. [2003:e4:1f12:c800:f22f:74ff:fe1f:3a53]) by smtp.gmail.com with ESMTPSA id kv12-20020a17090778cc00b0072ac3f06615sm3282062ejc.133.2022.07.05.05.58.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jul 2022 05:58:42 -0700 (PDT) From: Thierry Reding To: Rob Herring , Joerg Roedel Cc: Will Deacon , Robin Murphy , Nicolin Chen , Krishna Reddy , Dmitry Osipenko , Alyssa Rosenzweig , Janne Grunau , Sameer Pujar , devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, linux-tegra@vger.kernel.org, asahi@lists.linux.dev, Frank Rowand , Rob Herring Subject: [PATCH v6 2/5] iommu: Implement of_iommu_get_resv_regions() Date: Tue, 5 Jul 2022 14:58:31 +0200 Message-Id: <20220705125834.431711-3-thierry.reding@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220705125834.431711-1-thierry.reding@gmail.com> References: <20220705125834.431711-1-thierry.reding@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Thierry Reding This is an implementation that IOMMU drivers can use to obtain reserved memory regions from a device tree node. It uses the reserved-memory DT bindings to find the regions associated with a given device. If these regions are marked accordingly, identity mappings will be created for them in the IOMMU domain that the devices will be attached to. Cc: Frank Rowand Cc: devicetree@vger.kernel.org Reviewed-by: Rob Herring Signed-off-by: Thierry Reding --- Changes in v6: - remove reference to now unused dt-bindings/reserved-memory.h include Changes in v5: - update for new "iommu-addresses" device tree bindings Changes in v4: - fix build failure on !CONFIG_OF_ADDRESS Changes in v3: - change "active" property to identity mapping flag that is part of the memory region specifier (as defined by #memory-region-cells) to allow per-reference flags to be used Changes in v2: - use "active" property to determine whether direct mappings are needed drivers/iommu/of_iommu.c | 88 ++++++++++++++++++++++++++++++++++++++++ include/linux/of_iommu.h | 8 ++++ 2 files changed, 96 insertions(+) diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c index 41f4eb005219..c62da41516eb 100644 --- a/drivers/iommu/of_iommu.c +++ b/drivers/iommu/of_iommu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -172,3 +173,90 @@ const struct iommu_ops *of_iommu_configure(struct device *dev, return ops; } + +/** + * of_iommu_get_resv_regions - reserved region driver helper for device tree + * @dev: device for which to get reserved regions + * @list: reserved region list + * + * IOMMU drivers can use this to implement their .get_resv_regions() callback + * for memory regions attached to a device tree node. See the reserved-memory + * device tree bindings on how to use these: + * + * Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt + */ +void of_iommu_get_resv_regions(struct device *dev, struct list_head *list) +{ +#if IS_ENABLED(CONFIG_OF_ADDRESS) + struct of_phandle_iterator it; + int err; + + of_for_each_phandle(&it, err, dev->of_node, "memory-region", NULL, 0) { + struct iommu_resv_region *region; + struct resource res; + const __be32 *maps; + int size; + + memset(&res, 0, sizeof(res)); + + /* + * The "reg" property is optional and can be omitted by reserved-memory regions + * that represent reservations in the IOVA space, which are regions that should + * not be mapped. + */ + if (of_find_property(it.node, "reg", NULL)) { + err = of_address_to_resource(it.node, 0, &res); + if (err < 0) { + dev_err(dev, "failed to parse memory region %pOF: %d\n", + it.node, err); + continue; + } + } + + maps = of_get_property(it.node, "iommu-addresses", &size); + if (maps) { + const __be32 *end = maps + size / sizeof(__be32); + struct device_node *np; + unsigned int index = 0; + u32 phandle; + int na, ns; + + while (maps < end) { + phys_addr_t start, end; + size_t length; + + phandle = be32_to_cpup(maps++); + np = of_find_node_by_phandle(phandle); + na = of_n_addr_cells(np); + ns = of_n_size_cells(np); + + start = of_translate_dma_address(np, maps); + length = of_read_number(maps + na, ns); + end = start + length - 1; + + if (np == dev->of_node) { + int prot = IOMMU_READ | IOMMU_WRITE; + enum iommu_resv_type type; + + /* + * IOMMU regions without an associated physical region + * cannot be mapped and are simply reservations. + */ + if (res.end > res.start) + type = IOMMU_RESV_DIRECT_RELAXABLE; + else + type = IOMMU_RESV_RESERVED; + + region = iommu_alloc_resv_region(start, length, prot, type); + if (region) + list_add_tail(®ion->list, list); + } + + maps += na + ns; + index++; + } + } + } +#endif +} +EXPORT_SYMBOL(of_iommu_get_resv_regions); diff --git a/include/linux/of_iommu.h b/include/linux/of_iommu.h index 55c1eb300a86..9a5e6b410dd2 100644 --- a/include/linux/of_iommu.h +++ b/include/linux/of_iommu.h @@ -12,6 +12,9 @@ extern const struct iommu_ops *of_iommu_configure(struct device *dev, struct device_node *master_np, const u32 *id); +extern void of_iommu_get_resv_regions(struct device *dev, + struct list_head *list); + #else static inline const struct iommu_ops *of_iommu_configure(struct device *dev, @@ -21,6 +24,11 @@ static inline const struct iommu_ops *of_iommu_configure(struct device *dev, return NULL; } +static inline void of_iommu_get_resv_regions(struct device *dev, + struct list_head *list) +{ +} + #endif /* CONFIG_OF_IOMMU */ #endif /* __OF_IOMMU_H */