Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / infiniband / qib_genbits.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 use strict;
21 use warnings;
22
23 my $offsets = {};
24 my $defaults = {};
25 my $structures = {};
26 my $structure = "";
27
28 while ( <> ) {
29   chomp;
30   if ( /^\#define (\S+)_OFFS (\S+)$/ ) {
31     $structure = $1;
32     $offsets->{$structure} = $2;
33   } elsif ( /^\#define ${structure}_DEF (\S+)$/ ) {
34     $defaults->{$structure} = $1;
35   } elsif ( /^\#define ${structure}_(\S+)_LSB (\S+)$/ ) {
36     $structures->{$structure}->{$1}->{LSB} = $2;
37   } elsif ( /^\#define ${structure}_(\S+)_MSB (\S+)$/ ) {
38     $structures->{$structure}->{$1}->{MSB} = $2;
39   } elsif ( /^\#define ${structure}_(\S+)_RMASK (\S+)$/ ) {
40     $structures->{$structure}->{$1}->{RMASK} = $2;
41   } elsif ( /^\s*$/ ) {
42     # Do nothing
43   } else {
44     print "$_\n";
45   }
46 }
47
48 my $data = [ map { { name => $_, offset => $offsets->{$_},
49                      default => $defaults->{$_} }; }
50              sort { hex ( $offsets->{$a} ) <=> hex ( $offsets->{$b} ) }
51              keys %$offsets ];
52
53 foreach my $datum ( @$data ) {
54   next unless exists $structures->{$datum->{name}};
55   $structure = $structures->{$datum->{name}};
56   my $fields = [ map { { name => $_, lsb => $structure->{$_}->{LSB},
57                          msb => $structure->{$_}->{MSB},
58                          rmask => $structure->{$_}->{RMASK} }; }
59                  sort { hex ( $structure->{$a}->{LSB} ) <=>
60                             hex ( $structure->{$b}->{LSB} ) }
61                  keys %$structure ];
62   $datum->{fields} = $fields;
63 }
64
65 print "\n/* This file has been further processed by $0 */\n\n";
66 print "FILE_LICENCE ( GPL2_ONLY );\n\n";
67
68 foreach my $datum ( @$data ) {
69   printf "#define %s_offset 0x%08xUL\n",
70       $datum->{name}, hex ( $datum->{offset} );
71   if ( exists $datum->{fields} ) {
72     my $lsb = 0;
73     my $reserved_idx = 0;
74     printf "struct %s_pb {\n", $datum->{name};
75     foreach my $field ( @{$datum->{fields}} ) {
76       my $pad_width = ( hex ( $field->{lsb} ) - $lsb );
77       die "Inconsistent LSB/RMASK in $datum->{name} before $field->{name}\n"
78           if $pad_width < 0;
79       printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
80           if $pad_width;
81       $lsb += $pad_width;
82       # Damn Perl can't cope with 64-bit hex constants
83       my $width = 0;
84       die "Missing RMASK in $datum->{name}.$field->{name}\n"
85           unless defined $field->{rmask};
86       my $rmask = $field->{rmask};
87       while ( $rmask =~ /^(0x.+)f$/i ) {
88         $width += 4;
89         $rmask = $1;
90       }
91       $rmask = hex ( $rmask );
92       while ( $rmask ) {
93         $width++;
94         $rmask >>= 1;
95       }
96       if ( defined $field->{msb} ) {
97         my $msb_width = ( hex ( $field->{msb} ) - $lsb + 1 );
98         $width ||= $msb_width;
99         die "Inconsistent LSB/MSB/RMASK in $datum->{name}.$field->{name}\n"
100             unless $width == $msb_width;
101       }
102       printf "\tpseudo_bit_t %s[%u];\n", $field->{name}, $width;
103       $lsb += $width;
104     }
105     my $pad_width = ( 64 - $lsb );
106     die "Inconsistent LSB/RMASK in $datum->{name} final field\n"
107         if $pad_width < 0;
108     printf "\tpseudo_bit_t _unused_%u[%u];\n", $reserved_idx++, $pad_width
109         if $pad_width;
110     printf "};\n";
111     printf "struct %s {\n\tPSEUDO_BIT_STRUCT ( struct %s_pb );\n};\n",
112         $datum->{name}, $datum->{name};
113   }
114   printf "/* Default value: %s */\n", $datum->{default}
115       if defined $datum->{default};
116   print "\n";
117 }