Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CFB4CECAAA1 for ; Wed, 31 Aug 2022 03:12:13 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3CEB484A35; Wed, 31 Aug 2022 05:10:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Sh1whD3N"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5AA4E849D8; Wed, 31 Aug 2022 05:10:27 +0200 (CEST) Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1CD8484A2F for ; Wed, 31 Aug 2022 05:10:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ot1-x32d.google.com with SMTP id t8-20020a9d5908000000b0063b41908168so1382136oth.8 for ; Tue, 30 Aug 2022 20:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=asqbt6IQK7uobfV6xW0vy+HWOy/CgzwsnQv8SDjp14Q=; b=Sh1whD3NhR3K7CsuuzjyOzSxmZq/RIr4N+Q0USgpmbskLPqegEnq1gC0HJY3gHdERf uZhCNKf8ypt/HBSEWHF/1g6KSSVddHBWjRq1rbsOYiTCj80TkSB8jEL++LspTvpmLHBV h+AbgYBejIxTYGdWHQlLTa6xBUy5VbyDWyBh0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=asqbt6IQK7uobfV6xW0vy+HWOy/CgzwsnQv8SDjp14Q=; b=NByLLx4DA4DHOM4JKFm3KwAnkZUyyRcRiEdlb5Bajbvq8L3yVVZxkLojKa6ocy4lMB RsYMK5cz5d0AiCaOi1eB9mzHmHGFzLYG8cNSayLk/Rj62moIvfMQNwUaazOSnJxoG0U4 /OHk3+XZKvOrYjCcx7U/thp+z24zb3A48F/eSkdk0sT20RlMGMDPgsRyQPmO59QBPxu3 IV6T3BZOGzreMwVm+F9tb347KHzcIK2HvljMqgBAuwyC/y7+/1wx1pIZI1UI2O2zm4l5 jyFu9qVDHtztxdUDEK3M5pD/3eqRWtDPAmwkUwAbzp3mV18myONjNwpzUCtqxL4wfLjU Bkgg== X-Gm-Message-State: ACgBeo2TffZZ+3JUG4r92R6VzkApaDc8UputDazgFIaDbnYCkuF4UAUn f56+F5DvCq6ky95uONcdDBpeFmmwCxA6aw== X-Google-Smtp-Source: AA6agR7KXXF2JsutHUi7J9748o8e+R+XEnTO4yqK/cbto29JDZcojsbYLQTuuLQmv/ob5RjncrUfMA== X-Received: by 2002:a9d:12e4:0:b0:637:10e3:f145 with SMTP id g91-20020a9d12e4000000b0063710e3f145mr9204229otg.183.1661915359724; Tue, 30 Aug 2022 20:09:19 -0700 (PDT) Received: from sjg1.roam.corp.google.com (c-67-190-102-125.hsd1.co.comcast.net. [67.190.102.125]) by smtp.gmail.com with ESMTPSA id fp39-20020a05687065a700b00116d5103487sm9282605oab.31.2022.08.30.20.09.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 20:09:19 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Michal Simek , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Kishon Vijay Abraham I , Tom Rini , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH 21/21] dm: core: Add the ofnode multi-tree implementation Date: Tue, 30 Aug 2022 21:08:17 -0600 Message-Id: <20220831030817.27804-22-sjg@chromium.org> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog In-Reply-To: <20220831030817.27804-1-sjg@chromium.org> References: <20220831030817.27804-1-sjg@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Add the logic to redirect requests for the device tree through a function which can look up the tree ID. This works by using the top bits of ofnode.of_offset to encode a tree. It is assumed that there will only be a few device trees used at runtime, typically the control FDT (always tree ID 0) and possibly a separate FDT to be passed the OS. The maximum number of device trees supported at runtime is 8, with this implementation. That would use bits 30:28 of the node-offset value, meaning that the positive offset range is limited to bits 27:0, versus 30:1 with this feature disabled. That still allows a device tree of up to 256MB, which should be enough for most FITs. Larger ones can be supported by using external data with the FIT, or by enabling OF_LIVE. Update the documentation a little. Signed-off-by: Simon Glass --- doc/develop/driver-model/livetree.rst | 13 +-- drivers/core/ofnode.c | 124 +++++++++++++++++++++++++- include/dm/ofnode.h | 41 ++++++--- 3 files changed, 161 insertions(+), 17 deletions(-) diff --git a/doc/develop/driver-model/livetree.rst b/doc/develop/driver-model/livetree.rst index 4ef8c517325..76be89b9633 100644 --- a/doc/develop/driver-model/livetree.rst +++ b/doc/develop/driver-model/livetree.rst @@ -250,11 +250,14 @@ a flat tree. It would be helpful to use livetree for fixups, since adding a lot of nodes and properties would involve less memory copying and be more efficient. As a step towards this, an `oftree` type has been introduced. It is normally set to -oftree_default() but can be set to other values. Eventually this should allow -the use of FDT fixups using the ofnode interface, instead of the low-level -libfdt one. - -See dm_test_ofnode_root() for some examples. +oftree_default() but can be set to other values using oftree_from_fdt(). +So long as OF_LIVE is disabled, it is possible to do fixups using the ofnode +interface. The OF_LIVE support required addition of the flattening step at the +end. + +See dm_test_ofnode_root() for some examples. The ofnode_path_root() function +causes a flat device tree to be 'registered' such that it can be used by the +ofnode interface. Internal implementation diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index a5c6e309615..b2b4b8d54cc 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -4,6 +4,8 @@ * Written by Simon Glass */ +#define LOG_CATEGORY LOGC_DT + #include #include #include @@ -18,6 +20,126 @@ #include #include +DECLARE_GLOBAL_DATA_PTR; + +#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) +static void *oftree_list[CONFIG_OFNODE_MULTI_TREE_MAX]; +static int oftree_count; + +void oftree_reset(void) +{ + if (gd->flags & GD_FLG_RELOC) { + oftree_count = 0; + oftree_list[oftree_count++] = (void *)gd->fdt_blob; + } +} + +static int oftree_find(const void *fdt) +{ + int i; + + for (i = 0; i < oftree_count; i++) { + if (fdt == oftree_list[i]) + return i; + } + + return -1; +} + +static ofnode oftree_ensure(void *fdt, const char *path) +{ + int offset, i; + ofnode node; + + if (gd->flags & GD_FLG_RELOC) { + i = oftree_find(fdt); + if (i == -1) { + if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) { + log_warning("Too many registered device trees (max %d)\n", + CONFIG_OFNODE_MULTI_TREE_MAX); + return ofnode_null(); + } + + /* register the new tree */ + i = oftree_count++; + oftree_list[i] = fdt; + log_debug("oftree: registered tree %d: %p\n", i, fdt); + } + } else { + if (fdt != gd->fdt_blob) { + log_debug("Cannot only access control FDT before relocation\n"); + return ofnode_null(); + } + } + + offset = fdt_path_offset(fdt, path); + if (offset < 0) { + log_debug("Unable to find path '%s' in tree %p\n", path, fdt); + return ofnode_null(); + } + + node.of_offset = OFTREE_NODE(i, offset); + + return node; +} + +void *ofnode_lookup_fdt(ofnode node) +{ + if (gd->flags & GD_FLG_RELOC) { + uint i = OFTREE_TREE_ID(node.of_offset); + + if (i > oftree_count) { + log_debug("Invalid tree ID %x\n", i); + return NULL; + } + + return oftree_list[i]; + } else { + return (void *)gd->fdt_blob; + } +} + +void *ofnode_to_fdt(ofnode node) +{ +#ifdef OF_CHECKS + if (of_live_active()) + return NULL; +#endif + if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node)) + return ofnode_lookup_fdt(node); + + /* Use the control FDT by default */ + return (void *)gd->fdt_blob; +} + +/** + * ofnode_to_offset() - convert an ofnode to a flat DT offset + * + * This cannot be called if the reference contains a node pointer. + * + * @node: Reference containing offset (possibly invalid) + * Return: DT offset (can be -1) + */ +int ofnode_to_offset(ofnode node) +{ +#ifdef OF_CHECKS + if (of_live_active()) + return -1; +#endif + if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && node.of_offset >= 0) + return OFTREE_OFFSET(node.of_offset); + + return node.of_offset; +} + +#else /* !OFNODE_MULTI_TREE */ + +static ofnode oftree_ensure(void *fdt, const char *path) +{ + return offset_to_ofnode(fdt_path_offset(fdt, path)); +} +#endif /* OFNODE_MULTI_TREE */ + bool ofnode_name_eq(ofnode node, const char *name) { const char *node_name; @@ -575,7 +697,7 @@ ofnode ofnode_path_root(oftree tree, const char *path) else if (*path != '/' && tree.fdt != gd->fdt_blob) return ofnode_null(); /* Aliases only on control FDT */ else - return offset_to_ofnode(fdt_path_offset(tree.fdt, path)); + return oftree_ensure(tree.fdt, path); } const void *ofnode_read_chosen_prop(const char *propname, int *sizep) diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index a79909c12e1..4d614b0a6e3 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -27,13 +27,14 @@ struct ofnode_phandle_args { uint32_t args[OF_MAX_PHANDLE_ARGS]; }; +#if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) /** * oftree_reset() - reset the state of the oftree list * * Reset the oftree list so it can be started again. This should be called * once the control FDT is in place, but before the ofnode interface is used. */ -static inline void oftree_reset(void) {} +void oftree_reset(void); /** * ofnode_to_fdt() - convert an ofnode to a flat DT pointer @@ -43,26 +44,32 @@ static inline void oftree_reset(void) {} * @node: Reference containing offset (possibly invalid) * Return: DT offset (can be NULL) */ +__attribute_const__ void *ofnode_to_fdt(ofnode node); + +/** + * ofnode_to_offset() - convert an ofnode to a flat DT offset + * + * This cannot be called if the reference contains a node pointer. + * + * @node: Reference containing offset (possibly invalid) + * Return: DT offset (can be -1) + */ +__attribute_const__ int ofnode_to_offset(ofnode node); + +#else /* !OFNODE_MULTI_TREE */ +static inline void oftree_reset(void) {} + static inline void *ofnode_to_fdt(ofnode node) { #ifdef OF_CHECKS if (of_live_active()) return NULL; #endif - /* Use the control FDT by default */ return (void *)gd->fdt_blob; } -/** - * ofnode_to_offset() - convert an ofnode to a flat DT offset - * - * This cannot be called if the reference contains a node pointer. - * - * @node: Reference containing offset (possibly invalid) - * Return: DT offset (can be -1) - */ -static inline int ofnode_to_offset(ofnode node) +static inline __attribute_const__ int ofnode_to_offset(ofnode node) { #ifdef OF_CHECKS if (of_live_active()) @@ -70,6 +77,7 @@ static inline int ofnode_to_offset(ofnode node) #endif return node.of_offset; } +#endif /* OFNODE_MULTI_TREE */ /** * ofnode_to_np() - convert an ofnode to a live DT node pointer @@ -139,6 +147,17 @@ static inline bool ofnode_valid(ofnode node) return node.of_offset >= 0; } +/** + * ofnode_lookup_fdt() - look up the FDT for a node + * + * Given a node this returns a pointer to the device tree containing that node. + * This can only be called when the flat tree is in use + * + * @node: Node to look up, may be ofnode_null() + * @return associated device tree, or gd->fdt_blob if @node is ofnode_null() + */ +void *ofnode_lookup_fdt(ofnode node); + /** * oftree_lookup_fdt() - object the FDT pointer from an oftree * -- 2.37.2.672.g94769d06f0-goog