* [PATCH] dtc: Sort unit addresses by number
@ 2014-01-21 2:49 Anton Blanchard
2014-01-21 10:02 ` Mark Rutland
0 siblings, 1 reply; 3+ messages in thread
From: Anton Blanchard @ 2014-01-21 2:49 UTC (permalink / raw)
To: devicetree-u79uwXL29TY76Z2rM5mHXA; +Cc: jk-mnsaURCQ41sdnm+yROfE0A
The sort option in dtc treats unit addresses as strings. This causes
cpu nodes to end up out of order:
# dtc -s -I fs -O dts /proc/device-tree | grep PowerPC,POWER7
PowerPC,POWER7@30 {
PowerPC,POWER7@68 {
PowerPC,POWER7@70 {
PowerPC,POWER7@828 {
PowerPC,POWER7@860 {
PowerPC,POWER7@868 {
PowerPC,POWER7@8a0 {
PowerPC,POWER7@8b0 {
PowerPC,POWER7@8f0 {
PowerPC,POWER7@a0 {
PowerPC,POWER7@a8 {
PowerPC,POWER7@e0 {
If we use this device tree for a kexec boot we end up with a confusing
layout of logical CPUs:
node 0 cpus: 0-23 72-95
node 0 size: 32633 MB
node 1 cpus: 24-71
node 1 size: 32631 MB
The reason for this is that we allocate logical CPU ids as we walk
through the device tree.
In cmp_subnode, if both nodes have a hex unit address and the
basenames match, then compare by number.
This fixes the issue:
# dtc -s -I fs -O dts /proc/device-tree | grep PowerPC,POWER7
PowerPC,POWER7@30 {
PowerPC,POWER7@68 {
PowerPC,POWER7@70 {
PowerPC,POWER7@a0 {
PowerPC,POWER7@a8 {
PowerPC,POWER7@e0 {
PowerPC,POWER7@828 {
PowerPC,POWER7@860 {
PowerPC,POWER7@868 {
PowerPC,POWER7@8a0 {
PowerPC,POWER7@8b0 {
PowerPC,POWER7@8f0 {
And the CPU layout is as expected:
node 0 cpus: 0-47
node 0 size: 32633 MB
node 1 cpus: 48-95
node 1 size: 32631 MB
Signed-off-by: Anton Blanchard <anton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org>
--
Index: b/livetree.c
===================================================================
--- a/livetree.c
+++ b/livetree.c
@@ -656,12 +656,38 @@ static void sort_properties(struct node
free(tbl);
}
+static bool is_hex(const char *str)
+{
+ while (*str) {
+ if (!isxdigit(*str++))
+ return false;
+ }
+
+ return true;
+}
+
static int cmp_subnode(const void *ax, const void *bx)
{
- const struct node *a, *b;
+ struct node *a, *b;
+ const char *a_unit, *b_unit;
+
+ a = *((struct node * const *)ax);
+ b = *((struct node * const *)bx);
+
+ a_unit = get_unitname(a);
+ b_unit = get_unitname(b);
+
+ /* Sort hex unit addresses by number */
+ if (a_unit && b_unit && (a->basenamelen == b->basenamelen) &&
+ !strncmp(a->name, b->name, a->basenamelen) &&
+ is_hex(a_unit) && is_hex(b_unit)) {
+ unsigned long long a_num, b_num;
+
+ a_num = strtoull(a_unit, NULL, 16);
+ b_num = strtoull(b_unit, NULL, 16);
- a = *((const struct node * const *)ax);
- b = *((const struct node * const *)bx);
+ return (a_num > b_num) - (a_num < b_num);
+ }
return strcmp(a->name, b->name);
}
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] dtc: Sort unit addresses by number
2014-01-21 2:49 [PATCH] dtc: Sort unit addresses by number Anton Blanchard
@ 2014-01-21 10:02 ` Mark Rutland
[not found] ` <20140121100221.GJ28747-NuALmloUBlrZROr8t4l/smS4ubULX0JqMm0uRHvK7Nw@public.gmane.org>
0 siblings, 1 reply; 3+ messages in thread
From: Mark Rutland @ 2014-01-21 10:02 UTC (permalink / raw)
To: Anton Blanchard
Cc: devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
jk-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
On Tue, Jan 21, 2014 at 02:49:35AM +0000, Anton Blanchard wrote:
>
> The sort option in dtc treats unit addresses as strings. This causes
> cpu nodes to end up out of order:
>
> # dtc -s -I fs -O dts /proc/device-tree | grep PowerPC,POWER7
>
> PowerPC,POWER7@30 {
> PowerPC,POWER7@68 {
> PowerPC,POWER7@70 {
> PowerPC,POWER7@828 {
> PowerPC,POWER7@860 {
> PowerPC,POWER7@868 {
> PowerPC,POWER7@8a0 {
> PowerPC,POWER7@8b0 {
> PowerPC,POWER7@8f0 {
> PowerPC,POWER7@a0 {
> PowerPC,POWER7@a8 {
> PowerPC,POWER7@e0 {
>
> If we use this device tree for a kexec boot we end up with a confusing
> layout of logical CPUs:
>
> node 0 cpus: 0-23 72-95
> node 0 size: 32633 MB
>
> node 1 cpus: 24-71
> node 1 size: 32631 MB
>
> The reason for this is that we allocate logical CPU ids as we walk
> through the device tree.
>
> In cmp_subnode, if both nodes have a hex unit address and the
> basenames match, then compare by number.
>
> This fixes the issue:
>
> # dtc -s -I fs -O dts /proc/device-tree | grep PowerPC,POWER7
> PowerPC,POWER7@30 {
> PowerPC,POWER7@68 {
> PowerPC,POWER7@70 {
> PowerPC,POWER7@a0 {
> PowerPC,POWER7@a8 {
> PowerPC,POWER7@e0 {
> PowerPC,POWER7@828 {
> PowerPC,POWER7@860 {
> PowerPC,POWER7@868 {
> PowerPC,POWER7@8a0 {
> PowerPC,POWER7@8b0 {
> PowerPC,POWER7@8f0 {
>
> And the CPU layout is as expected:
>
> node 0 cpus: 0-47
> node 0 size: 32633 MB
>
> node 1 cpus: 48-95
> node 1 size: 32631 MB
>
> Signed-off-by: Anton Blanchard <anton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org>
> --
>
> Index: b/livetree.c
> ===================================================================
> --- a/livetree.c
> +++ b/livetree.c
> @@ -656,12 +656,38 @@ static void sort_properties(struct node
> free(tbl);
> }
>
> +static bool is_hex(const char *str)
> +{
> + while (*str) {
> + if (!isxdigit(*str++))
> + return false;
> + }
> +
> + return true;
> +}
> +
> static int cmp_subnode(const void *ax, const void *bx)
> {
> - const struct node *a, *b;
> + struct node *a, *b;
> + const char *a_unit, *b_unit;
> +
> + a = *((struct node * const *)ax);
> + b = *((struct node * const *)bx);
> +
> + a_unit = get_unitname(a);
> + b_unit = get_unitname(b);
> +
> + /* Sort hex unit addresses by number */
> + if (a_unit && b_unit && (a->basenamelen == b->basenamelen) &&
> + !strncmp(a->name, b->name, a->basenamelen) &&
> + is_hex(a_unit) && is_hex(b_unit)) {
> + unsigned long long a_num, b_num;
> +
> + a_num = strtoull(a_unit, NULL, 16);
> + b_num = strtoull(b_unit, NULL, 16);
>
> - a = *((const struct node * const *)ax);
> - b = *((const struct node * const *)bx);
> + return (a_num > b_num) - (a_num < b_num);
> + }
>
> return strcmp(a->name, b->name);
> }
Minor issue, but when #address-cells == 2, some unit addresses are split
in the middle by a ',' to separate the value of each cell, e.g.
"flash@2,0". For those, is_hex will return false and we'll compare
unit-addresses as strings.
I took a quick look over the dts in the Linux kernel tree (with `git
grep "@.\+," -- arch/*/boot/dts` and I think every instance there would
sort correctly as a string, but it would be nice to fix the issue
regardless of how large the unit-address is.
Perhaps we could have a helper function for reading the unit-address
that would take this into account?
Cheers,
Mark.
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] dtc: Sort unit addresses by number
[not found] ` <20140121100221.GJ28747-NuALmloUBlrZROr8t4l/smS4ubULX0JqMm0uRHvK7Nw@public.gmane.org>
@ 2014-01-24 12:21 ` Anton Blanchard
0 siblings, 0 replies; 3+ messages in thread
From: Anton Blanchard @ 2014-01-24 12:21 UTC (permalink / raw)
To: Mark Rutland
Cc: devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
jk-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
Hi Mark,
> Minor issue, but when #address-cells == 2, some unit addresses are
> split in the middle by a ',' to separate the value of each cell, e.g.
> "flash@2,0". For those, is_hex will return false and we'll compare
> unit-addresses as strings.
>
> I took a quick look over the dts in the Linux kernel tree (with `git
> grep "@.\+," -- arch/*/boot/dts` and I think every instance there
> would sort correctly as a string, but it would be nice to fix the
> issue regardless of how large the unit-address is.
>
> Perhaps we could have a helper function for reading the unit-address
> that would take this into account?
I was already getting nervous at the complexity of the sort function,
so I added the is_hex() check to ignore any complex unit addresses.
A helper function to read a unit address sounds like a simple enough
solution though.
Anton
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-01-24 12:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-21 2:49 [PATCH] dtc: Sort unit addresses by number Anton Blanchard
2014-01-21 10:02 ` Mark Rutland
[not found] ` <20140121100221.GJ28747-NuALmloUBlrZROr8t4l/smS4ubULX0JqMm0uRHvK7Nw@public.gmane.org>
2014-01-24 12:21 ` Anton Blanchard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).