From patchwork Thu Apr 6 04:01:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver O'Halloran X-Patchwork-Id: 747596 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vz8DT6qx0z9s8S for ; Thu, 6 Apr 2017 14:02:17 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JI4eCwFN"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3vz8DT5fqyzDqKd for ; Thu, 6 Apr 2017 14:02:17 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JI4eCwFN"; dkim-atps=neutral X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from mail-pg0-x244.google.com (mail-pg0-x244.google.com [IPv6:2607:f8b0:400e:c05::244]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3vz8DN2nr6zDqKJ for ; Thu, 6 Apr 2017 14:02:12 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JI4eCwFN"; dkim-atps=neutral Received: by mail-pg0-x244.google.com with SMTP id g2so5444319pge.2 for ; Wed, 05 Apr 2017 21:02:12 -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; bh=mROh3fGXwXmXRc8Fzi7I7/fzqMVZ0/82dfFaSphPA4A=; b=JI4eCwFN8XgJ8+54oIxEvRFVRaoxmHtGk569ZmNEGrEZA5+mcph8HW531R4O/YJi6z EK0zTuUUga0aCtilPX7baoJzoc+NwQhgOVDJwB6uEKyvpm8d72k1Iap+a6F7Y1giusUl dJZE4pzr03w48LTt7Q3olzy6DcMuSPhiYMK4gMyafLitwcGE5XQUOUKVF0/pz8cdr26M wz0FM212r8kLRwb0ECsSmPQe0EQppP8QLIlQHvzlNhMJo55VToatbkuuoESCCGZg4+XQ O2DU47oIfDkpdjMcGmQRAkywmOL8stf6+O6oaNzSMJQJkF62mh+rIJ+NCEo28liyC7qF HaFQ== 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; bh=mROh3fGXwXmXRc8Fzi7I7/fzqMVZ0/82dfFaSphPA4A=; b=NdVg3PTjb8QnfYgNWBLXAHnNm9LTPgqRvo4+Sp+rvU82NnwBpM3iigvB3OJzoxMZci JyXgsgOutzwpLOFaZT3MIx19y6a/Qyk5/aZ6vyKTsMQ8tXGBex4QGGxwzHBV8kfCscVO t1ulimr5tXxexHAU+UtqyxLtjRTlLJiNOOk8xgAiSqUpd30CSnZ6y2VRjc7N5U4c1oHq PZuSOwUdFm1u/PugbvfrvB3zATcNVK3/dQCZCTcsesNyDB0TV4Gv9PZYoQM/THvdeNJv H4qgEHEpE3kTzLKT+e3uKnYE8WdrvGjHtGVi9cE0D2T7Bvo6l7ciCggeR/SDKbD2/I1d E6aw== X-Gm-Message-State: AFeK/H227aEHa+2/RtRIIGjXN71gNj36GyHTBlskudGN6IzQd6Bj4KBjFjpZMpRTx0v2iw== X-Received: by 10.99.67.3 with SMTP id q3mr34570555pga.221.1491451330518; Wed, 05 Apr 2017 21:02:10 -0700 (PDT) Received: from flat-canetoad.ozlabs.ibm.com ([122.99.82.10]) by smtp.gmail.com with ESMTPSA id u129sm479994pfu.48.2017.04.05.21.02.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Apr 2017 21:02:10 -0700 (PDT) From: Oliver O'Halloran To: skiboot@lists.ozlabs.org Date: Thu, 6 Apr 2017 14:01:57 +1000 Message-Id: <20170406040158.30765-1-oohall@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [Skiboot] [PATCH 1/2] device: add dt_find_by_name_addr X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Adds two helper functions that allow device nodes to be found via their name and unit address. One will take an integer address and format it to a hex string while the other looks up the unit based a user supplied string. This is handy in a few places inside the HDAT parser. Signed-off-by: Oliver O'Halloran Reviewed-by: Vasant Hegde --- core/device.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/test/run-device.c | 1 + include/device.h | 5 +++++ 3 files changed, 56 insertions(+) diff --git a/core/device.c b/core/device.c index 76c2f8481b9f..f3ee63fbc423 100644 --- a/core/device.c +++ b/core/device.c @@ -21,6 +21,7 @@ #include #include #include +#include /* Used to give unique handles. */ u32 last_phandle = 0; @@ -155,6 +156,55 @@ struct dt_node *dt_new(struct dt_node *parent, const char *name) return new; } +/* + * low level variant, we export this because there are "weird" address + * formats, such as LPC/ISA bus addresses which have a letter to identify + * which bus space the address is inside of. + */ +struct dt_node *__dt_find_by_name_addr(struct dt_node *parent, const char *name, + const char *addr) +{ + struct dt_node *node; + + if (list_empty(&parent->children)) + return NULL; + + dt_for_each_child(parent, node) { + const char *unit = get_unitname(node); + int len; + + if (!unit) + continue; + + /* match the name */ + len = (int) (unit - node->name) - 1; + if (strncmp(node->name, name, len)) + continue; + + /* match the unit */ + if (strcmp(unit, addr) == 0) + return node; + } + + dt_for_each_child(parent, node) { + struct dt_node *ret = __dt_find_by_name_addr(node, name, addr); + + if (ret) + return ret; + } + + return NULL; +} + +struct dt_node *dt_find_by_name_addr(struct dt_node *parent, const char *name, + uint64_t addr) +{ + char addr_str[16 + 1]; /* max size of a 64bit int */ + snprintf(addr_str, sizeof(addr_str), "%" PRIx64, addr); + + return __dt_find_by_name_addr(parent, name, addr_str); +} + struct dt_node *dt_new_addr(struct dt_node *parent, const char *name, uint64_t addr) { diff --git a/core/test/run-device.c b/core/test/run-device.c index e91e5a5fc5c6..5939afc70e54 100644 --- a/core/test/run-device.c +++ b/core/test/run-device.c @@ -150,6 +150,7 @@ int main(void) assert(!list_top(&addr1->properties, struct dt_property, list)); check_path(addr1, "/addrs/addr@1337"); assert(dt_find_by_name(root, "addr@1337") == addr1); + assert(dt_find_by_name_addr(root, "addr", 0x1337) == addr1); assert(dt_find_by_path(root, "/addrs/addr@1337") == addr1); addr2 = dt_new_2addr(addrs, "2addr", 0xdead, 0xbeef); diff --git a/include/device.h b/include/device.h index 1ad403f18450..5155daadc7ee 100644 --- a/include/device.h +++ b/include/device.h @@ -247,4 +247,9 @@ u64 dt_translate_address(const struct dt_node *node, unsigned int index, */ int dt_cmp_subnodes(const struct dt_node *a, const struct dt_node *b); +struct dt_node *__dt_find_by_name_addr(struct dt_node *parent, const char *name, + const char *addr); +struct dt_node *dt_find_by_name_addr(struct dt_node *parent, const char *name, + uint64_t addr); + #endif /* __DEVICE_H */