[PATCH 4/9] Introduce a simple iomap structure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a simple structure and search function to the common code that
can be used for looking up a set of addresses by type. The user
must supply the iomaps[] table, e.g.

struct iomap iomaps[] = {
{
    .type = "virtio_mmio",
    .nr = 4,
    .addrs = { 0x2000000, 0x2000200, 0x2000400, 0x2000600, },
},
{
    .type = NULL,
},
};

Also add a script scripts/gen-devtree-iomaps.pl that can generate
the iomaps table from an fdt, e.g.

fdtdump dtb | scripts/gen-devtree-iomaps.pl - virtio_mmio

Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx>
---
 README                        |  1 +
 lib/iomaps.c                  | 12 +++++++
 lib/iomaps.h                  | 12 +++++++
 scripts/gen-devtree-iomaps.pl | 81 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 106 insertions(+)
 create mode 100644 lib/iomaps.c
 create mode 100644 lib/iomaps.h
 create mode 100755 scripts/gen-devtree-iomaps.pl

diff --git a/README b/README
index 49468b94bc7fc..1e58fd2e3ca00 100644
--- a/README
+++ b/README
@@ -20,6 +20,7 @@ Directory structure:
 .:		Makefile and config files for the tests
 ./config:	config files for the tests
 ./docs:		documentation files
+./scripts:	misc helper scripts
 ./lib:		general services for the tests
 ./lib/<ARCH>:	architecture dependent services for the tests
 ./<ARCH>:	the sources of the tests and the created objects/images
diff --git a/lib/iomaps.c b/lib/iomaps.c
new file mode 100644
index 0000000000000..6169bcf2e43d2
--- /dev/null
+++ b/lib/iomaps.c
@@ -0,0 +1,12 @@
+#include "libcflat.h"
+#include "iomaps.h"
+
+extern struct iomap iomaps[];
+
+struct iomap *iomaps_find(const char *type)
+{
+    struct iomap *m = &iomaps[0];
+    for (; m->type && strcmp(m->type, type); ++m)
+	;
+    return m->type ? m : NULL;
+}
diff --git a/lib/iomaps.h b/lib/iomaps.h
new file mode 100644
index 0000000000000..4581eee0e5337
--- /dev/null
+++ b/lib/iomaps.h
@@ -0,0 +1,12 @@
+#ifndef _IOMAPS_H_
+#define _IOMAPS_H_
+#include "libcflat.h"
+
+struct iomap {
+    const char *type;
+    u32 nr;
+    u32 addrs[64];
+};
+
+struct iomap *iomaps_find(const char *type);
+#endif
diff --git a/scripts/gen-devtree-iomaps.pl b/scripts/gen-devtree-iomaps.pl
new file mode 100755
index 0000000000000..91fa83d521839
--- /dev/null
+++ b/scripts/gen-devtree-iomaps.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/perl -w
+use strict;
+
+my $fdt = shift @ARGV;
+my @types = @ARGV;
+my $max_nr = 64;
+
+if (!defined $fdt || $#types < 0) {
+    print STDERR "Usage: gen-devtree-iomaps ".
+		 "<fdt-file|-> <addr-type> [addr-types...]\n";
+    exit 1;
+}
+
+my @dtb;
+if ($fdt eq '-') {
+    @dtb = <STDIN>;
+} else {
+    open F, "<$fdt" or die "can't read $fdt: $!";
+    @dtb = <F>;
+    close F;
+}
+
+my $g = (join '@|', @types) . '@';
+@dtb = grep { /$g/ } @dtb;
+foreach (@dtb) {
+    chomp($_);
+    s/^\s*//, s/\s*{$//;
+}
+
+my %iomaps;
+foreach (@dtb) {
+    my ($type, $addr) = split /@/, $_;
+    if (!exists $iomaps{$type}) {
+	$iomaps{$type} = [$addr];
+    } else {
+	push $iomaps{$type}, $addr;
+    }
+}
+
+print <<EOF;
+/*
+ * Generated file. See gen-devtree-iomaps.pl
+ */
+#include "iomaps.h"
+EOF
+print "\nstruct iomap iomaps[] = {\n";
+foreach my $type (keys %iomaps) {
+    @{ $iomaps{$type} } = sort @{ $iomaps{$type} };
+    my $nr = $#{ $iomaps{$type} } + 1;
+    if ($nr > $max_nr) {
+	print STDERR "$type has $nr addrs, but iomaps can only ".
+		     "support up to $max_nr.\n";
+	$nr = $max_nr;
+	splice @{ $iomaps{$type} }, $nr;
+    }
+    print "{\n";
+    print "    .type = \"$type\",\n";
+    print "    .nr = $nr,\n";
+    print "    .addrs = {";
+    if ($nr < 5) {
+	my $line = join ', 0x', @{ $iomaps{$type} };
+	print " 0x$line, },\n";
+	print "},\n";
+	next;
+    }
+    print "\n";
+    for (my $i = 0; $i < $nr; $i += 5) {
+	print "\t";
+	my $j = $i;
+	while ($j < $i + 4 && $j < $nr - 1) {
+	    print "0x$iomaps{$type}[$j], ";
+	    ++$j;
+	}
+	print "0x$iomaps{$type}[$j],\n";
+    }
+    print "    },\n";
+    print "},\n";
+}
+print "{\n    .type = NULL,\n},\n";
+print "};\n";
+exit 0;
-- 
1.8.1.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux