From nobody Tue Apr 7 12:55:36 2026 Received: from mailout1.w1.samsung.com (mailout1.w1.samsung.com [210.118.77.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D11C73ACA60 for ; Fri, 13 Mar 2026 15:08:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.118.77.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773414500; cv=none; b=Q4Jq06LKqmNjfvLu7a3evra/M1mf/+GbeLXNNrh1P1gPooeIEx2a/lJt/9dt0heF8SF56wncVcL//5Bwd7YoaOhKOq0fEdl9cJB+4WbDUhCNCqP6b/hJDO4btS/Hs5wW6PZwkQmOrrspuyi8uQB2IkoWGayV6RSE3IDWvyDR3Gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773414500; c=relaxed/simple; bh=mgi1BFwZn6K+4EgippU1L+ERpH0QKvqSZ1go1yDmZVY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:MIME-Version: Content-Type:References; b=ZACNDbUgl6HLms9z53Brx4vnFiOskwLK+ZLxYo97cd4VQwHKcgjS0x3Fkd8L0OjW6zvz4ciABG05xAJJMpK0zA/BBRyIbldDjDp+lhFwvrmLunl0gC0rDbIq4AFgfrjHjv0wAT9K5/obRCjrKv7Vt/FCTm7HS7YF2mg1FLlth8U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com; spf=pass smtp.mailfrom=samsung.com; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b=G5CpfkbN; arc=none smtp.client-ip=210.118.77.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=samsung.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=samsung.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=samsung.com header.i=@samsung.com header.b="G5CpfkbN" Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout1.w1.samsung.com (KnoxPortal) with ESMTP id 20260313150812euoutp012ab8036be8feaa43ed9db5af667c87c2~cbxXIVpuH2419324193euoutp01F for ; Fri, 13 Mar 2026 15:08:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20260313150812euoutp012ab8036be8feaa43ed9db5af667c87c2~cbxXIVpuH2419324193euoutp01F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1773414492; bh=OJ+AbVie4VzqjcN+NiZ5Lgc/79RXQxJwnzBrktnQFPI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G5CpfkbNeO6ZTFAugyudepxKq6lx9RTjOsZRAQrDAbXV+lwwBub4QfkdyP5SzcCQ1 1M1TAsHWI4CsNIZlrqZAYryxKRtofzCiBvC2Rp3hv83/jntTBUD/kq8YABkXqUc43I dPHGKsdo4m1e4ZQKo2g4LdGQYoT7VBL4Hzj3fXsU= Received: from eusmtip1.samsung.com (unknown [203.254.199.221]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20260313150812eucas1p2f155af637fcaf4d7061a5c9b9a5a6233~cbxW4kA8a0324803248eucas1p2f; Fri, 13 Mar 2026 15:08:12 +0000 (GMT) Received: from AMDC4653.digital.local (unknown [106.120.51.32]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20260313150811eusmtip1d2c4765123a272f8db46c4e9353a03d6~cbxWTm3tV0285302853eusmtip17; Fri, 13 Mar 2026 15:08:11 +0000 (GMT) From: Marek Szyprowski To: Saravana Kannan , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux.dev Cc: Marek Szyprowski , Rob Herring , Krzysztof Kozlowski , Oreoluwa Babatunde , Andrew Morton , Robin Murphy Subject: [PATCH 4/7] of: reserved_mem: replace CMA quirks by generic methods Date: Fri, 13 Mar 2026 16:07:59 +0100 Message-Id: <20260313150802.1121442-5-m.szyprowski@samsung.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260313150802.1121442-1-m.szyprowski@samsung.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CMS-MailID: 20260313150812eucas1p2f155af637fcaf4d7061a5c9b9a5a6233 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-RootMTR: 20260313150812eucas1p2f155af637fcaf4d7061a5c9b9a5a6233 X-EPHeader: CA X-CMS-RootMailID: 20260313150812eucas1p2f155af637fcaf4d7061a5c9b9a5a6233 References: <20260313150802.1121442-1-m.szyprowski@samsung.com> Add optional reserved memory callbacks to perform region verification and early fixup, then move all CMA related code in of_reserved_mem.c to them. Signed-off-by: Marek Szyprowski --- drivers/of/of_reserved_mem.c | 97 +++++++++++++++++++++------------ include/linux/cma.h | 10 ---- include/linux/dma-map-ops.h | 3 - include/linux/of_reserved_mem.h | 3 + kernel/dma/contiguous.c | 70 +++++++++++++++++------- 5 files changed, 116 insertions(+), 67 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 675f1c1c6627..399cdd11a9ca 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -24,8 +24,6 @@ #include #include #include -#include -#include =20 #include "of_private.h" =20 @@ -106,6 +104,11 @@ static void __init alloc_reserved_mem_array(void) =20 static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem, unsigned long node); +static int fdt_validate_reserved_mem_node(unsigned long node, + phys_addr_t *align); +static int fdt_fixup_reserved_mem_node(unsigned long node, + phys_addr_t base, phys_addr_t size); + /* * fdt_reserved_mem_save_node() - save fdt node for second pass initializa= tion */ @@ -154,21 +157,19 @@ static int __init __reserved_mem_reserve_reg(unsigned= long node, const char *uname) { phys_addr_t base, size; - int i, len; + int i, len, err; const __be32 *prop; - bool nomap, default_cma; + bool nomap; =20 prop =3D of_flat_dt_get_addr_size_prop(node, "reg", &len); if (!prop) return -ENOENT; =20 nomap =3D of_get_flat_dt_prop(node, "no-map", NULL) !=3D NULL; - default_cma =3D of_get_flat_dt_prop(node, "linux,cma-default", NULL); =20 - if (default_cma && cma_skip_dt_default_reserved_mem()) { - pr_err("Skipping dt linux,cma-default for \"cma=3D\" kernel param.\n"); - return -EINVAL; - } + err =3D fdt_validate_reserved_mem_node(node, NULL); + if (err && err !=3D -ENODEV) + return err; =20 for (i =3D 0; i < len; i++) { u64 b, s; @@ -179,10 +180,7 @@ static int __init __reserved_mem_reserve_reg(unsigned = long node, size =3D s; =20 if (size && early_init_dt_reserve_memory(base, size, nomap) =3D=3D 0) { - /* Architecture specific contiguous memory fixup. */ - if (of_flat_dt_is_compatible(node, "shared-dma-pool") && - of_get_flat_dt_prop(node, "reusable", NULL)) - dma_contiguous_early_fixup(base, size); + fdt_fixup_reserved_mem_node(node, base, size); pr_debug("Reserved memory: reserved region for node '%s': base %pa, siz= e %lu MiB\n", uname, &base, (unsigned long)(size / SZ_1M)); } else { @@ -253,17 +251,19 @@ void __init fdt_scan_reserved_mem_reg_nodes(void) =20 fdt_for_each_subnode(child, fdt, node) { const char *uname; - bool default_cma =3D of_get_flat_dt_prop(child, "linux,cma-default", NUL= L); u64 b, s; + int ret; =20 if (!of_fdt_device_is_available(fdt, child)) continue; - if (default_cma && cma_skip_dt_default_reserved_mem()) - continue; =20 if (!of_flat_dt_get_addr_size(child, "reg", &b, &s)) continue; =20 + ret =3D fdt_validate_reserved_mem_node(node, NULL); + if (ret && ret !=3D -ENODEV) + continue; + base =3D b; size =3D s; =20 @@ -397,7 +397,7 @@ static int __init __reserved_mem_alloc_size(unsigned lo= ng node, const char *unam phys_addr_t base =3D 0, align =3D 0, size; int i, len; const __be32 *prop; - bool nomap, default_cma; + bool nomap; int ret; =20 prop =3D of_get_flat_dt_prop(node, "size", &len); @@ -421,19 +421,10 @@ static int __init __reserved_mem_alloc_size(unsigned = long node, const char *unam } =20 nomap =3D of_get_flat_dt_prop(node, "no-map", NULL) !=3D NULL; - default_cma =3D of_get_flat_dt_prop(node, "linux,cma-default", NULL); =20 - if (default_cma && cma_skip_dt_default_reserved_mem()) { - pr_err("Skipping dt linux,cma-default for \"cma=3D\" kernel param.\n"); - return -EINVAL; - } - - /* Need adjust the alignment to satisfy the CMA requirement */ - if (IS_ENABLED(CONFIG_CMA) - && of_flat_dt_is_compatible(node, "shared-dma-pool") - && of_get_flat_dt_prop(node, "reusable", NULL) - && !nomap) - align =3D max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES); + ret =3D fdt_validate_reserved_mem_node(node, &align); + if (ret && ret !=3D -ENODEV) + return ret; =20 prop =3D of_flat_dt_get_addr_size_prop(node, "alloc-ranges", &len); if (prop) { @@ -468,25 +459,61 @@ static int __init __reserved_mem_alloc_size(unsigned = long node, const char *unam uname, (unsigned long)(size / SZ_1M)); return -ENOMEM; } - /* Architecture specific contiguous memory fixup. */ - if (of_flat_dt_is_compatible(node, "shared-dma-pool") && - of_get_flat_dt_prop(node, "reusable", NULL)) - dma_contiguous_early_fixup(base, size); + + fdt_fixup_reserved_mem_node(node, base, size); + /* Save region in the reserved_mem array */ fdt_reserved_mem_save_node(node, uname, base, size); return 0; } =20 +extern const struct of_device_id __reservedmem_of_table[]; static const struct of_device_id __rmem_of_table_sentinel __used __section("__reservedmem_of_table_end"); =20 +static int __init fdt_fixup_reserved_mem_node(unsigned long node, + phys_addr_t base, phys_addr_t size) +{ + const struct of_device_id *i; + int ret =3D -ENODEV; + + for (i =3D __reservedmem_of_table; ret =3D=3D -ENODEV && + i < &__rmem_of_table_sentinel; i++) { + const struct reserved_mem_ops *ops =3D i->data; + + if (!of_flat_dt_is_compatible(node, i->compatible)) + continue; + + if (ops->node_fixup) + ret =3D ops->node_fixup(node, base, size); + } + return ret; +} + +static int __init fdt_validate_reserved_mem_node(unsigned long node, phys_= addr_t *align) +{ + const struct of_device_id *i; + int ret =3D -ENODEV; + + for (i =3D __reservedmem_of_table; ret =3D=3D -ENODEV && + i < &__rmem_of_table_sentinel; i++) { + const struct reserved_mem_ops *ops =3D i->data; + + if (!of_flat_dt_is_compatible(node, i->compatible)) + continue; + + if (ops->node_validate) + ret =3D ops->node_validate(node, align); + } + return ret; +} + /* * __reserved_mem_init_node() - call region specific reserved memory init = code */ static int __init __reserved_mem_init_node(struct reserved_mem *rmem, unsigned long node) { - extern const struct of_device_id __reservedmem_of_table[]; const struct of_device_id *i; int ret =3D -ENODEV; =20 @@ -503,7 +530,7 @@ static int __init __reserved_mem_init_node(struct reser= ved_mem *rmem, rmem->ops =3D ops; pr_info("initialized node %s, compatible id %s\n", rmem->name, compat); - break; + return ret; } } return ret; diff --git a/include/linux/cma.h b/include/linux/cma.h index d0793eaaadaa..8555d38a97b1 100644 --- a/include/linux/cma.h +++ b/include/linux/cma.h @@ -61,14 +61,4 @@ extern int cma_for_each_area(int (*it)(struct cma *cma, = void *data), void *data) extern bool cma_intersects(struct cma *cma, unsigned long start, unsigned = long end); =20 extern void cma_reserve_pages_on_error(struct cma *cma); - -#ifdef CONFIG_DMA_CMA -extern bool cma_skip_dt_default_reserved_mem(void); -#else -static inline bool cma_skip_dt_default_reserved_mem(void) -{ - return false; -} -#endif - #endif diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 60b63756df82..55ecd2934225 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -147,9 +147,6 @@ static inline void dma_free_contiguous(struct device *d= ev, struct page *page, { __free_pages(page, get_order(size)); } -static inline void dma_contiguous_early_fixup(phys_addr_t base, unsigned l= ong size) -{ -} #endif /* CONFIG_DMA_CMA*/ =20 #ifdef CONFIG_DMA_DECLARE_COHERENT diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_me= m.h index dc00502a6b69..c240dfe45c9d 100644 --- a/include/linux/of_reserved_mem.h +++ b/include/linux/of_reserved_mem.h @@ -18,6 +18,9 @@ struct reserved_mem { }; =20 struct reserved_mem_ops { + int (*node_validate)(unsigned long fdt_node, phys_addr_t *align); + int (*node_fixup)(unsigned long fdt_node, phys_addr_t base, + phys_addr_t size); int (*node_init)(unsigned long fdt_node, struct reserved_mem *rmem); int (*device_init)(struct reserved_mem *rmem, struct device *dev); diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index efeebda92537..65d216663e81 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -91,16 +91,6 @@ static int __init early_cma(char *p) } early_param("cma", early_cma); =20 -/* - * cma_skip_dt_default_reserved_mem - This is called from the - * reserved_mem framework to detect if the default cma region is being - * set by the "cma=3D" kernel parameter. - */ -bool __init cma_skip_dt_default_reserved_mem(void) -{ - return size_cmdline !=3D -1; -} - #ifdef CONFIG_DMA_NUMA_CMA =20 static struct cma *dma_contiguous_numa_area[MAX_NUMNODES]; @@ -470,25 +460,65 @@ static void rmem_cma_device_release(struct reserved_m= em *rmem, dev->cma_area =3D NULL; } =20 +static int __init __rmem_cma_verify_node(unsigned long node) +{ + if (!of_get_flat_dt_prop(node, "reusable", NULL) || + of_get_flat_dt_prop(node, "no-map", NULL)) + return -ENODEV; + + if (size_cmdline !=3D -1 && + of_get_flat_dt_prop(node, "linux,cma-default", NULL)) { + pr_err("Skipping dt linux,cma-default node in favor for \"cma=3D\" kerne= l param.\n"); + return -EBUSY; + } + return 0; +} + +static int __init rmem_cma_validate(unsigned long node, phys_addr_t *align) +{ + int ret =3D __rmem_cma_verify_node(node); + + if (ret) + return ret; + + if (align) + *align =3D max_t(phys_addr_t, *align, CMA_MIN_ALIGNMENT_BYTES); + + return 0; +} + +static int __init rmem_cma_fixup(unsigned long node, phys_addr_t base, + phys_addr_t size) +{ + int ret =3D __rmem_cma_verify_node(node); + + if (ret) + return ret; + + /* Architecture specific contiguous memory fixup. */ + dma_contiguous_early_fixup(base, size); + return 0; +} + static int __init rmem_cma_setup(unsigned long node, struct reserved_mem *= rmem) { bool default_cma =3D of_get_flat_dt_prop(node, "linux,cma-default", NULL); struct cma *cma; - int err; + int ret; =20 - if (!of_get_flat_dt_prop(node, "reusable", NULL) || - of_get_flat_dt_prop(node, "no-map", NULL)) - return -ENODEV; + ret =3D __rmem_cma_verify_node(node); + if (ret) + return ret; =20 if (!IS_ALIGNED(rmem->base | rmem->size, CMA_MIN_ALIGNMENT_BYTES)) { pr_err("Reserved memory: incorrect alignment of CMA region\n"); return -EINVAL; } =20 - err =3D cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma= ); - if (err) { + ret =3D cma_init_reserved_mem(rmem->base, rmem->size, 0, rmem->name, &cma= ); + if (ret) { pr_err("Reserved memory: unable to setup CMA region\n"); - return err; + return ret; } =20 if (default_cma) @@ -499,14 +529,16 @@ static int __init rmem_cma_setup(unsigned long node, = struct reserved_mem *rmem) pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n", &rmem->base, (unsigned long)rmem->size / SZ_1M); =20 - err =3D dma_heap_cma_register_heap(cma); - if (err) + ret =3D dma_heap_cma_register_heap(cma); + if (ret) pr_warn("Couldn't register CMA heap."); =20 return 0; } =20 static const struct reserved_mem_ops rmem_cma_ops =3D { + .node_validate =3D rmem_cma_validate, + .node_fixup =3D rmem_cma_fixup, .node_init =3D rmem_cma_setup, .device_init =3D rmem_cma_device_init, .device_release =3D rmem_cma_device_release, --=20 2.34.1