Zane Shelley | ba5dc16 | 2020-11-09 21:47:55 -0600 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 2 | |
| 3 | use warnings; |
| 4 | use strict; |
| 5 | |
| 6 | use Data::Dumper; |
| 7 | use Getopt::Long qw(:config no_ignore_case); |
| 8 | use File::Path qw(make_path); |
| 9 | use XML::Simple qw(:strict); |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 10 | use JSON; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 11 | |
| 12 | # Pull in from the lib directory |
| 13 | use FindBin qw($RealBin); |
| 14 | use FindBin qw($RealScript); |
| 15 | use lib "$RealBin/lib"; |
| 16 | |
| 17 | use BitRange; |
| 18 | |
| 19 | #------------------------------------------------------------------------------- |
| 20 | # Global Variables |
| 21 | #------------------------------------------------------------------------------- |
| 22 | |
| 23 | # Supported file versions and their values. |
| 24 | my $FILE_VERSION = |
| 25 | { |
| 26 | VER_01 => 0x01, |
| 27 | }; |
| 28 | |
| 29 | # This is a map of all currently supported models/ECs and their IDs. |
| 30 | my $SUPPORTED_MODEL_EC = |
| 31 | { |
Zane Shelley | 13d0408 | 2021-06-04 08:40:48 -0500 | [diff] [blame] | 32 | EXPLORER_11 => { id => 0x60D20011, type => "ocmb", desc => "Explorer 1.1" }, |
| 33 | EXPLORER_20 => { id => 0x60D20020, type => "ocmb", desc => "Explorer 2.0" }, |
| 34 | P10_10 => { id => 0x20DA0010, type => "proc", desc => "P10 1.0" }, |
| 35 | P10_20 => { id => 0x20DA0020, type => "proc", desc => "P10 2.0" }, |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | # All models/ECs that may exist in the XML, but no longer needs to be built. |
| 39 | # This is useful for build optimization and also help prevent build breaks when |
| 40 | # the XML still exists, but not needed anymore. |
| 41 | my $DEPRECATED_MODEL_EC = []; |
| 42 | |
| 43 | # Supported register types and their values. |
| 44 | my $REGISTER_TYPE = |
| 45 | { |
| 46 | SCOM => { id => 0x01, addr_size => 4, reg_size => 8 }, |
| 47 | IDSCOM => { id => 0x02, addr_size => 8, reg_size => 8 }, |
| 48 | }; |
| 49 | |
| 50 | # Supported attention types and their values. |
| 51 | my $ATTN_TYPE = |
| 52 | { |
Zane Shelley | 3d30890 | 2021-06-04 16:35:33 -0500 | [diff] [blame] | 53 | CS => [ 1, 'checkstop' ], # System checkstop hardware attention |
| 54 | UCS => [ 2, 'unit checkstop' ], # Unit checkstop hardware attention |
| 55 | RE => [ 3, 'recoverable' ], # Recoverable hardware attention |
| 56 | SPA => [ 4, 'special attention' ], # event requiring action by the SP FW |
| 57 | HA => [ 5, 'host attention' ], # event requiring action by the host FW |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #------------------------------------------------------------------------------- |
| 61 | # Help function |
| 62 | #------------------------------------------------------------------------------- |
| 63 | |
| 64 | sub help() |
| 65 | { |
| 66 | print <<EOF; |
| 67 | Usage: $RealScript -h |
| 68 | $RealScript -i <input_dir> -o <output_dir> |
| 69 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 70 | Generates specified data files from the input Chip Data XML. |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 71 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 72 | General options: |
| 73 | |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 74 | -h, --help Prints this menu. |
| 75 | -i, --input Directory containing the Chip Data XML files. |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 76 | -o, --output Directory that will contain the data files. |
| 77 | |
| 78 | Data file options (must specify at least one): |
| 79 | |
| 80 | --cdb Generates Chip Data Binary files. |
| 81 | --json Generates PEL Parser Data JSON files. |
| 82 | |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 83 | EOF |
| 84 | |
| 85 | exit; |
| 86 | } |
| 87 | |
| 88 | #------------------------------------------------------------------------------- |
| 89 | # Input |
| 90 | #------------------------------------------------------------------------------- |
| 91 | |
| 92 | help() unless @ARGV; # print help if no arguments |
| 93 | |
| 94 | # Get options |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 95 | my ( $help, $src_dir, $dest_dir, $gen_cdb, $gen_json ); |
| 96 | help() unless GetOptions( |
| 97 | 'h|help' => \$help, |
| 98 | 'i|input=s' => \$src_dir, |
| 99 | 'o|output=s' => \$dest_dir, |
| 100 | 'cdb' => \$gen_cdb, |
| 101 | 'json' => \$gen_json, |
| 102 | ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 103 | |
| 104 | help() if @ARGV; # print usage if there are extra arguments |
| 105 | |
| 106 | # -h,--help |
| 107 | help() if ( $help ); |
| 108 | |
| 109 | # -i,--input |
| 110 | die "ERROR> Option -i required." unless ( defined $src_dir ); |
| 111 | die "ERROR> '$src_dir' is not a directory" unless ( -d $src_dir ); |
| 112 | |
| 113 | # -o,--output |
| 114 | die "ERROR> Option -o required." unless ( defined $dest_dir ); |
| 115 | make_path( $dest_dir, {error => \my $err} ); |
| 116 | if ( @{$err} ) |
| 117 | { |
| 118 | my ( $file, $message ) = %{shift @{$err}}; |
| 119 | die "ERROR> $message: $file\n"; |
| 120 | } |
| 121 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 122 | # --cdb, --json |
| 123 | unless ( $gen_cdb or $gen_json ) |
| 124 | { |
| 125 | die "ERROR> Must specify at least one data file option."; |
| 126 | } |
| 127 | |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 128 | #------------------------------------------------------------------------------- |
| 129 | # Prototypes |
| 130 | #------------------------------------------------------------------------------- |
| 131 | |
| 132 | sub importXML($); |
| 133 | sub normalizeXML($); |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 134 | sub buildDataFiles($$); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 135 | |
| 136 | #------------------------------------------------------------------------------- |
| 137 | # Main |
| 138 | #------------------------------------------------------------------------------- |
| 139 | |
| 140 | # Validate and import the XML. |
| 141 | my $chip_data_xml = importXML( $src_dir ); |
| 142 | |
| 143 | # There are some fields in the XML that are shorthand and need to be expanded |
| 144 | # before building the binary files. |
| 145 | my $normalized_data = normalizeXML( $chip_data_xml ); |
| 146 | |
| 147 | # The XML should now be in a format to start building the binary files. |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 148 | buildDataFiles( $dest_dir, $normalized_data ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 149 | |
| 150 | #------------------------------------------------------------------------------- |
| 151 | # Helper functions |
| 152 | #------------------------------------------------------------------------------- |
| 153 | |
| 154 | sub FAIL($) { die( "ERROR> " . shift @_ ); } |
| 155 | |
| 156 | #------------------------------------------------------------------------------- |
| 157 | # Import functions |
| 158 | #------------------------------------------------------------------------------- |
| 159 | |
| 160 | # For each supported XML file in the given directory: |
| 161 | # - Ensures the XML is well-formed. |
| 162 | # - Ensures the XML validates against the schema. |
| 163 | # - Imports the XML into Perl data structures. |
| 164 | sub importXML($) |
| 165 | { |
| 166 | my ( $dir ) = @_; |
| 167 | |
| 168 | my $data = {}; |
| 169 | |
| 170 | # Get a list of all the XML files. |
| 171 | opendir DIR, $dir or die "Couldn't open dir '$dir': $!"; |
| 172 | my @files = grep { /^.+\.xml$/ } readdir DIR; |
| 173 | closedir DIR; |
| 174 | |
| 175 | # Iterate each supported file type. |
| 176 | for my $type ( "chip", "node" ) |
| 177 | { |
| 178 | for my $file ( grep { /^$type\_.+\.xml$/ } @files ) |
| 179 | { |
| 180 | my $path = "$dir/$file"; |
| 181 | |
| 182 | # Ensure the XML is well-formed and validates against the schema. |
Zane Shelley | f21a282 | 2021-07-26 14:23:00 -0500 | [diff] [blame] | 183 | my $schema = "$RealBin/schema/$type.xsd"; |
| 184 | my $out = `xmllint --noout --schema $schema $path 2>&1`; |
Zane Shelley | fc4aa5e | 2021-01-14 13:45:39 -0600 | [diff] [blame] | 185 | die "$out\nRAS XML validation failed on $file" if ( 0 != $? ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 186 | |
| 187 | # Import the XML. |
| 188 | my $xml = XMLin( $path, KeyAttr => {}, ForceArray => 1 ); |
| 189 | |
| 190 | # Add the file path to the XML for error output. |
| 191 | $xml->{path} = $path; |
| 192 | |
| 193 | # Push each file's data to a list for each file type. |
| 194 | push @{$data->{$type}}, $xml; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return $data; |
| 199 | } |
| 200 | |
| 201 | #------------------------------------------------------------------------------- |
| 202 | # Normalize functions |
| 203 | #------------------------------------------------------------------------------- |
| 204 | |
| 205 | # Takes a string of models/ECs separated by ',' and returns a list of supported |
| 206 | # models/ECs. See $SUPPORTED_MODEL_EC and $DEPRECATED_MODEL_EC. |
| 207 | sub __expandModelEc($) |
| 208 | { |
| 209 | my ( $str ) = @_; |
| 210 | |
| 211 | my @list = split(/,/, $str); |
| 212 | |
| 213 | # Remove any deprecated models/ECs. |
| 214 | for my $d ( @{$DEPRECATED_MODEL_EC} ) |
| 215 | { |
| 216 | @list = grep { $d ne $_ } @list; |
| 217 | } |
| 218 | |
| 219 | # Validate the remaining models/ECs. |
| 220 | for my $m ( @list ) |
| 221 | { |
| 222 | unless ( defined $SUPPORTED_MODEL_EC->{$m} ) |
| 223 | { |
| 224 | FAIL("Unsupported model/EC: $m"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return @list; |
| 229 | } |
| 230 | |
| 231 | #------------------------------------------------------------------------------- |
| 232 | |
| 233 | sub __getInstRange($) |
| 234 | { |
| 235 | my ( $insts ) = @_; |
| 236 | |
| 237 | my $list = []; |
| 238 | for ( @{$insts} ) { push @{$list}, $_->{reg_inst}; } |
| 239 | |
Zane Shelley | 70ee34a | 2022-09-16 08:52:37 -0500 | [diff] [blame] | 240 | @{$list} = sort {$a <=> $b} @{$list}; # Sort the list just in case. |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 241 | |
| 242 | return BitRange::compress($list); |
| 243 | } |
| 244 | |
| 245 | sub __getReg($$$$) |
| 246 | { |
| 247 | my ( $inst_in, $reg_type, $name, $addr_mod ) = @_; |
| 248 | |
| 249 | my $inst_out = []; |
| 250 | for ( @{$inst_in} ) |
| 251 | { |
| 252 | my $addr = ""; |
| 253 | if ( "SCOM" eq $reg_type ) |
| 254 | { |
| 255 | $addr = sprintf( "0x%08x", hex($_->{addr}) + $addr_mod ); |
| 256 | } |
| 257 | elsif ( "IDSCOM" eq $reg_type ) |
| 258 | { |
| 259 | # TODO: Need a portable way of handling 64-bit numbers. |
| 260 | FAIL("IDSCOM address currently not supported"); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | FAIL("Unsupported register type for node: $name"); |
| 265 | } |
| 266 | |
| 267 | push @{$inst_out}, { reg_inst => $_->{reg_inst}, addr => $addr }; |
| 268 | } |
| 269 | |
| 270 | return { name => $name, instance => $inst_out }; |
| 271 | } |
| 272 | |
| 273 | sub __getExpr($$) |
| 274 | { |
| 275 | my ( $name, $config ) = @_; |
| 276 | |
| 277 | # Get the register expression. |
| 278 | my $expr = { type => 'reg', value1 => $name }; |
| 279 | |
| 280 | if ( '0' eq $config ) |
| 281 | { |
| 282 | # Take the NOT of the register expression. |
| 283 | $expr = { type => 'not', expr => [ $expr ] }; |
| 284 | } |
| 285 | |
| 286 | return $expr; |
| 287 | } |
| 288 | |
| 289 | sub __getAct($$$$) |
| 290 | { |
| 291 | my ( $fir, $range, $type, $config ) = @_; |
| 292 | |
| 293 | FAIL("Invalid action config: $config") unless ( $config =~ /^[01]{2,3}$/ ); |
| 294 | |
| 295 | my @c = split( //, $config ); |
| 296 | |
| 297 | my $e = []; |
| 298 | push( @{$e}, __getExpr("${fir}", '1' ) ); |
| 299 | push( @{$e}, __getExpr("${fir}_MASK", '0' ) ); |
| 300 | push( @{$e}, __getExpr("${fir}_ACT0", shift @c) ); |
| 301 | push( @{$e}, __getExpr("${fir}_ACT1", shift @c) ); |
| 302 | push( @{$e}, __getExpr("${fir}_ACT2", shift @c) ) if ( 0 < scalar @c ); |
| 303 | |
| 304 | return { node_inst => $range, attn_type => $type, |
| 305 | expr => [ { type => 'and', expr => $e } ] }; |
| 306 | } |
| 307 | |
| 308 | #------------------------------------------------------------------------------- |
| 309 | |
| 310 | sub __normalizeLocalFir($) |
| 311 | { |
| 312 | my ( $node ) = @_; |
| 313 | |
| 314 | return unless ( defined $node->{local_fir} ); |
| 315 | |
| 316 | # Note that the isolator will implicitly add all register referenced by the |
| 317 | # rules to the capture group. To reduce redundancy and overall file size, we |
| 318 | # won't add these registers to the capture group. |
| 319 | |
| 320 | $node->{register} = [] unless ( defined $node->{register} ); |
| 321 | $node->{rule} = [] unless ( defined $node->{rule} ); |
| 322 | |
| 323 | for my $l ( @{$node->{local_fir}} ) |
| 324 | { |
| 325 | my $n = $l->{name}; |
| 326 | my $i = $l->{instance}; |
| 327 | my $t = $node->{reg_type}; |
| 328 | |
| 329 | my $inst_range = __getInstRange($i); |
| 330 | |
| 331 | my $r = []; |
| 332 | push @{$r}, __getReg($i, $t, "${n}", 0); |
| 333 | push @{$r}, __getReg($i, $t, "${n}_MASK", 3); |
| 334 | push @{$r}, __getReg($i, $t, "${n}_ACT0", 6); |
| 335 | push @{$r}, __getReg($i, $t, "${n}_ACT1", 7); |
| 336 | push @{$r}, __getReg($i, $t, "${n}_WOF", 8) if ($l->{config} =~ /W/); |
| 337 | push @{$r}, __getReg($i, $t, "${n}_ACT2", 9) if ($l->{config} =~ /2/); |
| 338 | |
| 339 | push @{$node->{register}}, @{$r}; |
| 340 | |
| 341 | for ( @{$l->{action}} ) |
| 342 | { |
| 343 | push @{$node->{rule}}, |
| 344 | __getAct( $n, $inst_range, $_->{attn_type}, $_->{config} ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | delete $node->{local_fir}; |
| 349 | } |
| 350 | |
| 351 | #------------------------------------------------------------------------------- |
| 352 | |
| 353 | # This is not very efficient, especially for large data structures. It is |
| 354 | # recommended to use Data::Compare, but that is not available on the pool |
| 355 | # machines. |
| 356 | sub __dirtyCompare($$) |
| 357 | { |
| 358 | local $Data::Dumper::Terse = 1; |
| 359 | local $Data::Dumper::Indent = 0; |
| 360 | local $Data::Dumper::Sortkeys = 1; |
| 361 | my ( $a, $b ) = ( Dumper(shift), Dumper(shift) ); |
| 362 | return $a eq $b; |
| 363 | } |
| 364 | |
| 365 | #------------------------------------------------------------------------------- |
| 366 | |
| 367 | sub __normalizeRegister($$) |
| 368 | { |
| 369 | my ( $node, $regs ) = @_; |
| 370 | |
Zane Shelley | 467bce8 | 2021-07-15 20:16:54 -0500 | [diff] [blame] | 371 | return unless ( defined $node->{register} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 372 | |
| 373 | # All of the registers will be put in the master register list for the chip. |
| 374 | for my $r ( @{$node->{register}} ) |
| 375 | { |
| 376 | # Set the default access if needed. |
| 377 | $r->{access} = 'RW' unless ( defined $r->{access} ); |
| 378 | |
| 379 | # Each register will keep track of its type. |
| 380 | $r->{reg_type} = $node->{reg_type}; |
| 381 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 382 | for my $model_ec ( __expandModelEc($node->{model_ec}) ) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 383 | { |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 384 | if ( defined $regs->{$model_ec}->{$r->{name}} ) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 385 | { |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 386 | # This register already exists so check the contents for |
| 387 | # accuracy |
| 388 | unless ( __dirtyCompare($r, $regs->{$model_ec}->{$r->{name}}) ) |
| 389 | { |
| 390 | FAIL("Duplicate register: $r->{name}"); |
| 391 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 392 | } |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 393 | else |
| 394 | { |
| 395 | # Add this node's register to the master register list. |
| 396 | $regs->{$model_ec}->{$r->{name}} = $r; |
| 397 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | # Clean up this node's register data. |
| 402 | delete $node->{register}; |
| 403 | } |
| 404 | |
| 405 | #------------------------------------------------------------------------------- |
| 406 | |
| 407 | sub __normalizeCaptureGroup($$) |
| 408 | { |
| 409 | my ( $node, $insts_data ) = @_; |
| 410 | |
| 411 | # Capture groups are optional (although recommended). |
| 412 | return unless ( defined $node->{capture_group} ); |
| 413 | |
| 414 | for my $c ( @{$node->{capture_group}} ) |
| 415 | { |
| 416 | # There must be at least one capture_register. |
| 417 | unless ( defined $c->{capture_register} and |
| 418 | 0 < scalar @{$c->{capture_register}} ) |
| 419 | { |
| 420 | FAIL("<capture_group> for node $node->{name} does not contain at " . |
| 421 | "least one <capture_register>" ); |
| 422 | } |
| 423 | |
| 424 | my @node_insts = BitRange::expand($c->{node_inst}); |
| 425 | |
| 426 | for my $r ( @{$c->{capture_register}} ) |
| 427 | { |
| 428 | # node_inst and reg_inst must be the same size. |
| 429 | my @reg_insts = BitRange::expand($r->{reg_inst}); |
| 430 | unless ( scalar @node_insts == scalar @reg_insts ) |
| 431 | { |
| 432 | FAIL("capture_group/\@node_inst and capture_register/" . |
| 433 | "\@reg_inst list sized not equal for node $node->{name}"); |
| 434 | } |
| 435 | |
| 436 | # Expand the capture groups so there is one per node instance. |
| 437 | for ( 0 .. (scalar @node_insts - 1) ) |
| 438 | { |
| 439 | my ( $ni, $ri ) = ( $node_insts[$_], $reg_insts[$_] ); |
| 440 | push @{$insts_data->{$ni}->{capture_group}}, |
| 441 | { reg_name => $r->{reg_name}, reg_inst => $ri }; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | # Clean up this node's capture group data. |
| 447 | delete $node->{capture_group}; |
| 448 | } |
| 449 | |
| 450 | #------------------------------------------------------------------------------- |
| 451 | |
| 452 | sub __normalizeExpr($$$$); # Called recursively |
| 453 | |
| 454 | sub __normalizeExpr($$$$) |
| 455 | { |
| 456 | my ( $in, $ni, $idx, $size ) = @_; |
| 457 | |
| 458 | my ( $t, $e, $v1, $v2 ) = ( $in->{type}, $in->{expr}, |
| 459 | $in->{value1}, $in->{value2} ); |
| 460 | |
| 461 | my $out = { type => $t }; |
| 462 | |
| 463 | if ( "and" eq $t or "or" eq $t ) |
| 464 | { |
| 465 | if ( defined $v1 or defined $v2 or |
| 466 | not defined $e or not (0 < scalar @{$e}) ) |
| 467 | { |
| 468 | FAIL("Invalid parameters for and/or expression"); |
| 469 | } |
| 470 | |
| 471 | # Iterate each sub expression. |
| 472 | push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e}); |
| 473 | } |
| 474 | elsif ( "not" eq $t ) |
| 475 | { |
| 476 | if ( defined $v1 or defined $v2 or |
| 477 | not defined $e or not (1 == scalar @{$e}) ) |
| 478 | { |
| 479 | FAIL("Invalid parameters for not expression"); |
| 480 | } |
| 481 | |
| 482 | # Iterate each sub expression. |
| 483 | push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e}); |
| 484 | } |
| 485 | elsif ( "lshift" eq $t or "rshift" eq $t ) |
| 486 | { |
| 487 | if ( not defined $v1 or defined $v2 or |
| 488 | not defined $e or not (1 == scalar @{$e}) ) |
| 489 | { |
| 490 | FAIL("Invalid parameters for lshift/rshift expression"); |
| 491 | } |
| 492 | |
| 493 | # Copy value1. |
| 494 | $out->{value1} = $v1; |
| 495 | |
| 496 | # Iterate each sub expression. |
| 497 | push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e}); |
| 498 | } |
| 499 | elsif ( "reg" eq $t ) |
| 500 | { |
| 501 | if ( not defined $v1 or defined $e ) |
| 502 | { |
| 503 | FAIL("Invalid parameters for reg expression"); |
| 504 | } |
| 505 | |
| 506 | # Copy value1. |
| 507 | $out->{value1} = $v1; |
| 508 | |
| 509 | # value2 is optional in the XML, update the value to the node or |
| 510 | # register instance. |
| 511 | if ( defined $v2 ) |
| 512 | { |
| 513 | my @reg_insts = BitRange::expand($v2); |
| 514 | unless ( $size == scalar @reg_insts ) |
| 515 | { |
| 516 | FAIL("reg expression value2:$v2 list not the same ". |
| 517 | "size as containing node's rule instances:$size"); |
| 518 | } |
| 519 | |
| 520 | $out->{value2} = $reg_insts[$idx]; |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | # The register instance is the same as the node instance. |
| 525 | $out->{value2} = $ni; |
| 526 | } |
| 527 | } |
| 528 | elsif ( "int" eq $t ) |
| 529 | { |
| 530 | if ( not defined $v1 or defined $v2 or defined $e ) |
| 531 | { |
| 532 | FAIL("Invalid parameters for int expression"); |
| 533 | } |
| 534 | |
| 535 | # Copy value1. |
| 536 | $out->{value1} = $v1; |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | FAIL("Unsupported expression type: $t"); |
| 541 | } |
| 542 | |
| 543 | return $out; |
| 544 | } |
| 545 | |
| 546 | #------------------------------------------------------------------------------- |
| 547 | |
| 548 | sub __normalizeRule($$) |
| 549 | { |
| 550 | my ( $node, $insts_data ) = @_; |
| 551 | |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 552 | # There should be only one rule per attention type and node instance for |
| 553 | # this node. |
| 554 | my $rule_dups = {}; |
| 555 | |
| 556 | for my $r ( @{$node->{rule}} ) |
| 557 | { |
| 558 | # There should be exactly one parent expression. |
| 559 | unless ( 1 == scalar @{$r->{expr}} ) |
| 560 | { |
| 561 | FAIL("Multiple parent expressions for rule: $node->{name} " . |
| 562 | "$r->{attn_type}"); |
| 563 | } |
| 564 | my $expr = $r->{expr}->[0]; |
| 565 | |
| 566 | my @node_insts = BitRange::expand($r->{node_inst}); |
| 567 | my $sz_insts = scalar @node_insts; |
| 568 | |
| 569 | # Expand the expression for each node instance. |
| 570 | for my $idx ( 0 .. ($sz_insts - 1) ) |
| 571 | { |
| 572 | my $ni = $node_insts[$idx]; |
| 573 | |
| 574 | # Check for duplicates. |
| 575 | if ( defined $rule_dups->{$r->{attn_type}}->{$ni} ) |
| 576 | { |
| 577 | FAIL("Duplicate rule: $node->{name} $r->{attn_type} $ni"); |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | $rule_dups->{$r->{attn_type}}->{$ni} = 1; |
| 582 | } |
| 583 | |
| 584 | # Add the rule for this expression. |
| 585 | push @{$insts_data->{$ni}->{rule}}, |
| 586 | { attn_type => $r->{attn_type}, |
| 587 | expr => __normalizeExpr($expr, $ni, $idx, $sz_insts) }; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | # Clean up this node's rule data. |
| 592 | delete $node->{rule}; |
| 593 | } |
| 594 | |
| 595 | #------------------------------------------------------------------------------- |
| 596 | |
| 597 | sub __normalizeBit($$$) |
| 598 | { |
| 599 | my ( $node, $sigs, $insts_data ) = @_; |
| 600 | |
Zane Shelley | 70ee34a | 2022-09-16 08:52:37 -0500 | [diff] [blame] | 601 | my @node_insts = sort {$a <=> $b} keys %{$insts_data}; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 602 | my $sz_insts = scalar @node_insts; |
| 603 | |
| 604 | # There should be only one child node per node instance bit position. |
| 605 | my $child_dups = {}; |
| 606 | |
| 607 | for my $b ( sort {$a->{pos} cmp $b->{pos}} @{$node->{bit}} ) |
| 608 | { |
| 609 | my @child_insts = (); |
| 610 | |
| 611 | # Ensure child_node and node_inst are set properly. |
| 612 | if ( defined $b->{child_node} ) |
| 613 | { |
| 614 | # Ensure each bit has a default node_inst attribute if needed. |
| 615 | $b->{node_inst} = "0" unless ( defined $b->{node_inst} ); |
| 616 | |
| 617 | # Get all of the instances for this child node. |
| 618 | @child_insts = BitRange::expand($b->{node_inst}); |
| 619 | |
| 620 | # Both inst list must be equal in size. |
| 621 | unless ( $sz_insts == scalar @child_insts ) |
| 622 | { |
| 623 | FAIL("node_inst attribute list size for node:$node->{name} " . |
| 624 | "bit:$b->{pos} does not match node instances " . |
| 625 | "represented by the <rule> element"); |
| 626 | } |
| 627 | } |
| 628 | elsif ( defined $b->{node_inst} ) |
| 629 | { |
| 630 | FAIL("node_inst attribute exists for node:$node->{name} " . |
| 631 | "bit:$b->{pos} with no child_node attribute"); |
| 632 | } |
| 633 | |
| 634 | # Get the signatures for each node, instance, and bit position. |
| 635 | for my $p ( BitRange::expand($b->{pos}) ) |
| 636 | { |
| 637 | for my $i ( 0 .. ($sz_insts-1) ) |
| 638 | { |
| 639 | my ( $n, $ni ) = ( $node->{name}, $node_insts[$i] ); |
| 640 | |
| 641 | # This is to cover a bug in the figtree information where there |
| 642 | # currently is no comment for some bits. |
| 643 | $b->{content} = "" unless ( defined $b->{content} ); |
| 644 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 645 | for my $model_ec ( __expandModelEc($node->{model_ec}) ) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 646 | { |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 647 | # Check if this signature already exists. |
| 648 | if ( defined $sigs->{$model_ec}->{$n}->{$ni}->{$p} and |
| 649 | $b->{content} ne $sigs->{$model_ec}->{$n}->{$ni}->{$p} ) |
| 650 | { |
| 651 | FAIL("Duplicate signature for $n $ni $p"); |
| 652 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 653 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 654 | # Get the signatures for each node, instance, and bit |
| 655 | # position. |
| 656 | $sigs->{$model_ec}->{$n}->{$ni}->{$p} = $b->{content}; |
| 657 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 658 | |
| 659 | # Move onto the next instance unless a child node exists. |
| 660 | next unless ( defined $b->{child_node} ); |
| 661 | |
| 662 | my $pi = $child_insts[$i]; |
| 663 | |
| 664 | my $child = { pos => $p, |
| 665 | child_node => $b->{child_node}, |
| 666 | node_inst => $pi }; |
| 667 | |
| 668 | # Ensure this child node doesn't already exist. |
| 669 | if ( defined $child_dups->{$ni}->{$p} and |
| 670 | not __dirtyCompare($child, $child_dups->{$ni}->{$p}) ) |
| 671 | { |
| 672 | FAIL("Duplicate child_node for $n $ni $p"); |
| 673 | } |
| 674 | |
| 675 | # Add this child node. |
| 676 | push @{$insts_data->{$ni}->{bit}}, $child; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | # Clean up this node's bit data. |
| 682 | delete $node->{bit}; |
| 683 | } |
| 684 | |
| 685 | #------------------------------------------------------------------------------- |
| 686 | |
| 687 | sub __normalizeNode($$$) |
| 688 | { |
| 689 | my ( $node, $regs, $sigs ) = @_; |
| 690 | |
| 691 | # Ensure a valid register type. |
| 692 | unless ( grep { /^$node->{reg_type}$/ } keys %{$REGISTER_TYPE} ) |
| 693 | { |
| 694 | FAIL( "Unsupported register type: $node->{reg_type}" ); |
| 695 | } |
| 696 | |
| 697 | my $insts_data = {}; # Collect data for each instance of this node. |
| 698 | |
| 699 | # First, expand the <local_fir> data if it exists. |
| 700 | __normalizeLocalFir($node); |
| 701 | |
| 702 | # All registers will be put in a master register list for the chip. |
| 703 | __normalizeRegister($node, $regs); |
| 704 | |
| 705 | # Split the capture group information per node instance. |
| 706 | __normalizeCaptureGroup($node, $insts_data); |
| 707 | |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 708 | my $is_rule = (defined $node->{rule} and 0 < scalar @{$node->{rule}}) ? 1 : 0; |
| 709 | my $is_bit = (defined $node->{bit} and 0 < scalar @{$node->{bit}}) ? 1 : 0; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 710 | |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 711 | # If a rule is defined, a bit must be defined as well. It is possible for |
| 712 | # neither to be defined (FFDC-only node). |
| 713 | if ( $is_rule and $is_bit ) |
| 714 | { |
| 715 | # Split the rule information per node instance. The sorted instance list |
| 716 | # will be used as indexes for the node_inst attribute of the <bit> |
| 717 | # elements. |
| 718 | __normalizeRule($node, $insts_data); |
| 719 | |
| 720 | # Finally, collect the signature details and split the bit information |
| 721 | # per node instance. |
| 722 | __normalizeBit($node, $sigs, $insts_data); |
| 723 | } |
| 724 | elsif ( $is_rule or $is_bit ) |
| 725 | { |
| 726 | # One is defined and the other is not. This is an error. |
| 727 | FAIL("Node $node->{name} has a bit or rule defined and the other is not."); |
| 728 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 729 | |
| 730 | # Now that we have all of the node data, collapse the instance data into |
Zane Shelley | 70ee34a | 2022-09-16 08:52:37 -0500 | [diff] [blame] | 731 | # a list. Note that sort order doesn't matter. Only used for consistency. |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 732 | for ( sort keys %{$insts_data} ) |
| 733 | { |
| 734 | $insts_data->{$_}->{node_inst} = $_; |
| 735 | push @{$node->{instance}}, $insts_data->{$_}; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | #------------------------------------------------------------------------------- |
| 740 | |
| 741 | sub normalizeXML($) |
| 742 | { |
| 743 | my ( $xml ) = @_; |
| 744 | |
| 745 | my $data = {}; |
| 746 | |
| 747 | # Iterate each chip file. |
| 748 | for my $chip ( @{$xml->{chip}} ) |
| 749 | { |
| 750 | # Iterate each model/EC. |
| 751 | for my $model_ec ( __expandModelEc($chip->{model_ec}) ) |
| 752 | { |
| 753 | # Ensure there is not a duplicate definition for a model/EC. |
| 754 | if ( $data->{$model_ec}->{chip} ) |
| 755 | { |
| 756 | FAIL("Duplicate data for model/EC $model_ec in:\n" . |
| 757 | " $data->{$model_ec}->{chip}->{path}\n" . |
| 758 | " $chip->{path}"); |
| 759 | } |
| 760 | |
| 761 | # Add this chip to the data. |
| 762 | $data->{$model_ec}->{attn_tree} = $chip->{attn_tree}; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | # Extract the data for each node. |
| 767 | my ( $regs, $sigs, $node_dups ) = ( {}, {}, {} ); |
| 768 | for my $node ( sort { $a->{name} cmp $b->{name} } @{$xml->{node}} ) |
| 769 | { |
| 770 | # A node may be defined for more than one model/EC. |
| 771 | for my $model_ec ( __expandModelEc($node->{model_ec}) ) |
| 772 | { |
| 773 | # A node can only be defined once per model/EC. |
| 774 | if ( defined $node_dups->{$model_ec}->{$node->{name}} ) |
| 775 | { |
| 776 | FAIL( "Duplicate node defined for $model_ec -> $node->{name} "); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | $node_dups->{$model_ec}->{$node->{name}} = 1; |
| 781 | } |
| 782 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 783 | # Initialize the master list of registers and signatures of this |
| 784 | # model/EC, if necessary. |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 785 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 786 | $regs->{$model_ec} = {} unless ( defined $regs->{$model_ec} ); |
| 787 | $sigs->{$model_ec} = {} unless ( defined $sigs->{$model_ec} ); |
| 788 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 789 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 790 | # The same node content will be used for each model/EC characterized by |
| 791 | # this node. There is some normalization that needs to happen because of |
| 792 | # shorthand elements, like <local_fir>, and some error checking. This |
| 793 | # only needs to be done once per node, not per model/EC. |
| 794 | __normalizeNode( $node, $regs, $sigs ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 795 | |
Zane Shelley | d6826e5 | 2020-11-10 17:39:00 -0600 | [diff] [blame] | 796 | # Push the node data for each model/EC. |
| 797 | for my $model_ec ( __expandModelEc($node->{model_ec}) ) |
| 798 | { |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 799 | push @{$data->{$model_ec}->{node}}, $node; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | # Sort and collapse the master register list. |
| 804 | for my $m ( keys %{$regs} ) |
| 805 | { |
| 806 | for my $n ( sort keys %{$regs->{$m}} ) |
| 807 | { |
| 808 | push @{$data->{$m}->{register}}, $regs->{$m}->{$n}; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | # Collapse the signature lists. |
| 813 | for my $m ( keys %{$sigs} ) |
| 814 | { |
| 815 | for my $n ( sort keys %{$sigs->{$m}} ) |
| 816 | { |
| 817 | for my $i ( sort {$a <=> $b} keys %{$sigs->{$m}->{$n}} ) |
| 818 | { |
| 819 | for my $b ( sort {$a <=> $b} keys %{$sigs->{$m}->{$n}->{$i}} ) |
| 820 | { |
| 821 | push @{$data->{$m}->{signature}}, |
| 822 | { name => $n, inst => $i, bit => $b, |
| 823 | desc => $sigs->{$m}->{$n}->{$i}->{$b} }; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return $data; |
| 830 | } |
| 831 | |
| 832 | #------------------------------------------------------------------------------- |
| 833 | # Output functions |
| 834 | #------------------------------------------------------------------------------- |
| 835 | |
| 836 | # The $num passed into this function can be a numeric of string. All values are |
| 837 | # converted to a hex string and then into the binary format. This helps avoid |
| 838 | # portability issues with endianess. Requirements: |
| 839 | # - Hex strings must start with '0x'. |
| 840 | # - For portability, 64-bit numbers must be passed as a hex string. |
| 841 | sub __bin($$$) |
| 842 | { |
| 843 | my ( $fh, $bytes, $num ) = @_; |
| 844 | |
| 845 | # $bytes must be a positive integer. |
| 846 | die "Invalid bytes: $bytes" unless ( 0 < $bytes ); |
| 847 | |
| 848 | my $str = ''; # Default invalid string |
| 849 | |
| 850 | my $char = $bytes * 2; # Number of characters in the string. |
| 851 | |
| 852 | # Check if $num is a hex string. |
| 853 | if ( $num =~ /^0[x|X](.*)/ ) |
| 854 | { |
| 855 | $str = $1; # strip the '0x' |
| 856 | } |
| 857 | # Check if $num is string or numeric decimal integer (32-bit max). |
| 858 | elsif ( $num =~ /^[0-9]+$/ and $bytes <= 4 ) |
| 859 | { |
| 860 | $str = sprintf("%0${char}x", $num); # Convert to hex string |
| 861 | } |
| 862 | |
| 863 | # Check for a hex number with the valid size. |
| 864 | unless ( $str =~ /^[0-9a-fA-F]{$char}$/ ) |
| 865 | { |
| 866 | die "Invalid number: $num (size: $bytes)"; |
| 867 | } |
| 868 | |
| 869 | # Print the binary string. |
| 870 | print $fh pack( "H$char", $str ); |
| 871 | } |
| 872 | |
| 873 | #------------------------------------------------------------------------------- |
| 874 | |
| 875 | sub __hash($$) |
| 876 | { |
| 877 | my $bytes = shift; |
| 878 | my @str = unpack("C*", shift); # returns an array of ascii values |
| 879 | |
| 880 | # Currently only supporting 1, 2, 3, and 4 byte hashes. |
| 881 | unless ( 1 <= $bytes and $bytes <= 4 ) |
| 882 | { |
| 883 | FAIL("Unsupported hash size: $bytes"); |
| 884 | } |
| 885 | |
| 886 | # Add padding to the end of the character array so that the size is |
| 887 | # divisible by $bytes. |
| 888 | push @str, 0 until ( 0 == scalar(@str) % $bytes ); |
| 889 | |
| 890 | # This hash is a simple "n*s[0] + (n-1)*s[1] + ... + s[n-1]" algorithm, |
| 891 | # where s[i] is a $bytes size chunk of the input string. |
| 892 | |
| 893 | my ( $sumA, $sumB ) = ( 0, 0 ); |
| 894 | while ( my @chunk = splice @str, 0, $bytes ) |
| 895 | { |
| 896 | # Combine the chunk array into a single value. |
| 897 | my $val = 0; for ( @chunk ) { $val <<= 8; $val |= $_; } |
| 898 | |
| 899 | # Apply the simple hash. |
| 900 | $sumA += $val; |
| 901 | $sumB += $sumA; |
| 902 | } |
| 903 | |
| 904 | # Mask off everything except the target number of bytes. |
| 905 | $sumB &= 0xffffffff >> ((4 - $bytes) * 8); |
| 906 | |
| 907 | return $sumB; |
| 908 | } |
| 909 | |
| 910 | #------------------------------------------------------------------------------- |
| 911 | |
| 912 | sub __printRegisters($$) |
| 913 | { |
| 914 | my ( $fh, $data ) = @_; |
| 915 | |
| 916 | my $num_regs = scalar @{$data}; |
| 917 | FAIL("No registers defined") unless ( 0 < $num_regs ); |
| 918 | |
| 919 | # Register list metadata |
| 920 | __bin($fh, 1, $_) for ( unpack("C*", "REGS") ); |
| 921 | __bin($fh, 3, $num_regs); |
| 922 | |
| 923 | my $reg_ids = {}; # for hash duplicate checking |
| 924 | |
| 925 | for my $r ( @{$data} ) |
| 926 | { |
| 927 | # Get the hash of the register name and check for duplicates. |
| 928 | my $id = __hash(3, $r->{name}); |
| 929 | if ( defined $reg_ids->{$id} ) |
| 930 | { |
| 931 | FAIL("Duplicate register ID hash " . sprintf('0x%08x', $id) . |
| 932 | " for $r->{name} and $reg_ids->{$id}"); |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | $reg_ids->{$id} = $r->{name}; |
| 937 | } |
| 938 | |
| 939 | # Get the attribute flags. |
| 940 | my $flags = 0x00; |
| 941 | $flags |= 0x80 if ( $r->{access} =~ /R/ ); |
| 942 | $flags |= 0x40 if ( $r->{access} =~ /W/ ); |
| 943 | |
| 944 | # Get the number of address instances. |
| 945 | my $num_inst = scalar @{$r->{instance}}; |
| 946 | unless ( 0 < $num_inst ) |
| 947 | { |
| 948 | FAIL("No register instances defined for $r->{name}"); |
| 949 | } |
| 950 | |
| 951 | # Register metadata |
| 952 | __bin($fh, 3, $id ); |
| 953 | __bin($fh, 1, $REGISTER_TYPE->{$r->{reg_type}}->{id}); |
| 954 | __bin($fh, 1, $flags ); |
| 955 | __bin($fh, 1, $num_inst); |
| 956 | |
| 957 | for my $i ( @{$r->{instance}} ) |
| 958 | { |
| 959 | my $s = $REGISTER_TYPE->{$r->{reg_type}}->{addr_size}; |
| 960 | |
| 961 | # Register Instance metadata |
| 962 | __bin($fh, 1, $i->{reg_inst}); |
| 963 | __bin($fh, $s, $i->{addr} ); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | #------------------------------------------------------------------------------- |
| 969 | |
| 970 | sub __printExpr($$$); |
| 971 | |
| 972 | sub __printExpr($$$) |
| 973 | { |
| 974 | my ( $fh, $size, $expr ) = @_; |
| 975 | |
| 976 | my ( $t, $e, $v1, $v2 ) = ( $expr->{type}, $expr->{expr}, |
| 977 | $expr->{value1}, $expr->{value2} ); |
| 978 | |
| 979 | if ( "reg" eq $t ) |
| 980 | { |
| 981 | __bin($fh, 1, 0x01); # expression type for "reg" |
| 982 | __bin($fh, 3, __hash(3,$v1)); # register id |
| 983 | __bin($fh, 1, $v2); # register instance |
| 984 | } |
| 985 | elsif ( "int" eq $t ) |
| 986 | { |
| 987 | __bin($fh, 1, 0x02); # expression type for "int" |
| 988 | __bin($fh, $size, $v1); # integer value |
| 989 | } |
| 990 | elsif ( "and" eq $t ) |
| 991 | { |
| 992 | __bin($fh, 1, 0x10); # expression type for "and" |
| 993 | __bin($fh, 1, scalar @{$e}); # number of sub-expressions |
| 994 | __printExpr($fh, $size, $_) for ( @{$e} ); # add each sub-expression |
| 995 | } |
| 996 | elsif ( "or" eq $t ) |
| 997 | { |
| 998 | __bin($fh, 1, 0x11); # expression type for "or" |
| 999 | __bin($fh, 1, scalar @{$e}); # number of sub-expressions |
| 1000 | __printExpr($fh, $size, $_) for ( @{$e} ); # add each sub-expression |
| 1001 | } |
| 1002 | elsif ( "not" eq $t ) |
| 1003 | { |
| 1004 | __bin($fh, 1, 0x12); # expression type for "not" |
| 1005 | __printExpr($fh, $size, $e->[0]); # add only sub-expression |
| 1006 | } |
| 1007 | elsif ( "lshift" eq $t ) |
| 1008 | { |
| 1009 | __bin($fh, 1, 0x13); # expression type for "lshift" |
| 1010 | __bin($fh, 1, $v1); # shift amount |
| 1011 | __printExpr($fh, $size, $e->[0]); # add only sub-expression |
| 1012 | } |
| 1013 | elsif ( "rshift" eq $t ) |
| 1014 | { |
| 1015 | __bin($fh, 1, 0x14); # expression type for "rshift" |
| 1016 | __bin($fh, 1, $v1); # shift amount |
| 1017 | __printExpr($fh, $size, $e->[0]); # add only sub-expression |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | #------------------------------------------------------------------------------- |
| 1022 | |
| 1023 | sub __printNodes($$) |
| 1024 | { |
| 1025 | my ( $fh, $data ) = @_; |
| 1026 | |
| 1027 | my $num_nodes = scalar @{$data}; |
| 1028 | FAIL("No nodes defined") unless ( 0 < $num_nodes ); |
| 1029 | |
| 1030 | # Isolation Node list metadata |
| 1031 | __bin($fh, 1, $_) for ( unpack("C*", "NODE") ); |
| 1032 | __bin($fh, 2, $num_nodes); |
| 1033 | |
| 1034 | my $node_ids = {}; # for hash duplicate checking |
| 1035 | |
| 1036 | for my $n ( @{$data} ) |
| 1037 | { |
| 1038 | # Get the hash of the node name and check for duplicates. |
| 1039 | my $id = __hash(2, $n->{name}); |
| 1040 | if ( defined $node_ids->{$id} ) |
| 1041 | { |
| 1042 | FAIL("Duplicate node ID hash " . sprintf('0x%08x', $id) . |
| 1043 | " for $n->{name} and $node_ids->{$id}"); |
| 1044 | } |
| 1045 | else |
| 1046 | { |
| 1047 | $node_ids->{$id} = $n->{name}; |
| 1048 | } |
| 1049 | |
| 1050 | my $num_insts = scalar @{$n->{instance}}; |
| 1051 | unless ( 0 < $num_insts ) |
| 1052 | { |
| 1053 | FAIL("No nodes instances defined for $n->{name}"); |
| 1054 | } |
| 1055 | |
| 1056 | my $reg_type = $REGISTER_TYPE->{$n->{reg_type}}->{id}; |
| 1057 | my $reg_size = $REGISTER_TYPE->{$n->{reg_type}}->{reg_size}; |
| 1058 | |
| 1059 | # Register metadata |
| 1060 | __bin($fh, 2, $id); |
| 1061 | __bin($fh, 1, $reg_type); |
| 1062 | __bin($fh, 1, $num_insts); |
| 1063 | |
| 1064 | for my $i ( @{$n->{instance}} ) |
| 1065 | { |
| 1066 | # Capture groups are optional. |
| 1067 | my $num_cap_regs = (defined $i->{capture_group}) |
| 1068 | ? scalar @{$i->{capture_group}} : 0; |
| 1069 | |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 1070 | # Rules may not exist for this node. |
| 1071 | my $num_rules = (defined $i->{rule}) ? scalar @{$i->{rule}} : 0; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1072 | |
| 1073 | # Child nodes may not exist for this node. |
| 1074 | my $num_bit = (defined $i->{bit}) ? scalar @{$i->{bit}} : 0; |
| 1075 | |
| 1076 | # Register instance metadata |
| 1077 | __bin($fh, 1, $i->{node_inst}); |
| 1078 | __bin($fh, 1, $num_cap_regs ); |
| 1079 | __bin($fh, 1, $num_rules ); |
| 1080 | __bin($fh, 1, $num_bit ); |
| 1081 | |
| 1082 | if ( 0 < $num_cap_regs ) |
| 1083 | { |
| 1084 | for my $cg ( @{$i->{capture_group}} ) |
| 1085 | { |
| 1086 | # Register capture register metadata |
| 1087 | __bin($fh, 3, __hash(3, $cg->{reg_name})); |
| 1088 | __bin($fh, 1, $cg->{reg_inst} ); |
| 1089 | } |
| 1090 | } |
| 1091 | |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 1092 | if ( 0 < $num_rules ) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1093 | { |
Zane Shelley | bcb4395 | 2021-07-08 22:13:57 -0500 | [diff] [blame] | 1094 | for my $r ( @{$i->{rule}} ) |
| 1095 | { |
| 1096 | # Register rule metadata |
| 1097 | __bin($fh, 1, $ATTN_TYPE->{$r->{attn_type}}->[0]); |
| 1098 | __printExpr($fh, $reg_size, $r->{expr}); |
| 1099 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | if ( 0 < $num_bit ) |
| 1103 | { |
| 1104 | for my $b ( @{$i->{bit}} ) |
| 1105 | { |
| 1106 | # Register child node metadata |
| 1107 | __bin($fh, 1, $b->{pos} ); |
| 1108 | __bin($fh, 2, __hash(2, $b->{child_node})); |
| 1109 | __bin($fh, 1, $b->{node_inst} ); |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | #------------------------------------------------------------------------------- |
| 1117 | |
| 1118 | sub __printAttnTree($$) |
| 1119 | { |
| 1120 | my ( $fh, $data ) = @_; |
| 1121 | |
| 1122 | my $num_root_nodes = scalar @{$data}; |
| 1123 | FAIL("No root nodes defined") unless ( 0 < $num_root_nodes ); |
| 1124 | |
| 1125 | # Root Node list metadata |
| 1126 | __bin($fh, 1, $_) for ( unpack("C*", "ROOT") ); |
| 1127 | __bin($fh, 1, $num_root_nodes); |
| 1128 | |
| 1129 | for my $r ( @{$data} ) |
| 1130 | { |
| 1131 | # Root Node metadata |
Zane Shelley | 3d30890 | 2021-06-04 16:35:33 -0500 | [diff] [blame] | 1132 | __bin($fh, 1, $ATTN_TYPE->{$r->{attn_type}}->[0]); |
| 1133 | __bin($fh, 2, __hash(2, $r->{root_node}) ); |
| 1134 | __bin($fh, 1, $r->{node_inst} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | #------------------------------------------------------------------------------- |
| 1139 | |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1140 | sub __printParserData($$$$) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1141 | { |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1142 | my ( $fh, $model_ec, $sig_list, $reg_list) = @_; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1143 | |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1144 | # IMPORTANT: All hash keys with hex values must be lowercase. |
| 1145 | |
Zane Shelley | 3d30890 | 2021-06-04 16:35:33 -0500 | [diff] [blame] | 1146 | my $attns = {}; |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1147 | my $regs = {}; |
| 1148 | my $sigs = {}; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1149 | |
Zane Shelley | 13d0408 | 2021-06-04 08:40:48 -0500 | [diff] [blame] | 1150 | # Get the chip info. |
| 1151 | my $info = $SUPPORTED_MODEL_EC->{$model_ec}; |
| 1152 | $info->{'id'} = sprintf('%08x', $info->{'id'}); |
| 1153 | |
Zane Shelley | 3d30890 | 2021-06-04 16:35:33 -0500 | [diff] [blame] | 1154 | # Get the list of attention types. |
| 1155 | while ( my ($k, $v) = each %{$ATTN_TYPE} ) |
| 1156 | { |
| 1157 | $attns->{$v->[0]} = $v->[1]; |
| 1158 | } |
| 1159 | |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1160 | # Get the signature data. |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1161 | for my $s ( @{$sig_list} ) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1162 | { |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1163 | # Format is: |
| 1164 | # { id : [ name, { bit : desc, ... } ], ... } |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1165 | |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1166 | # The ID is a 2-byte hash of the node name (lowercase). |
| 1167 | my $id = sprintf('%04x', __hash(2, $s->{name})); |
| 1168 | |
| 1169 | if ( exists($sigs->{$id}) ) |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1170 | { |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1171 | # Check for hash collisions. |
| 1172 | if ($sigs->{$id}->[0] ne $s->{name} ) |
| 1173 | { |
| 1174 | FAIL("Node hash collision for $id: $sigs->{$id}->[0] " . |
| 1175 | "and $s->{name}"); |
| 1176 | } |
| 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | # Initialize this node. |
| 1181 | $sigs->{$id} = [ $s->{name}, {} ]; |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1182 | } |
| 1183 | |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1184 | # Check for signature collisions. |
| 1185 | if ( exists($sigs->{$id}->[1]->{$s->{bit}}) ) |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1186 | { |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1187 | # Check for signature collisions. |
| 1188 | if ( $sigs->{$id}->[1]->{$s->{bit}} ne $s->{desc} ) |
| 1189 | { |
| 1190 | FAIL("Multiple signatures for $s->{name} bit $s->{bit}:\n" . |
| 1191 | " $sigs->{$id}->[1]->{$s->{bit}}\n" . |
| 1192 | " $s->{desc}"); |
| 1193 | } |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1194 | } |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1195 | else |
| 1196 | { |
| 1197 | # Set the signature for this bit. |
| 1198 | $sigs->{$id}->[1]->{$s->{bit}} = $s->{desc}; |
| 1199 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1200 | } |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1201 | |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1202 | # Get the register data. |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1203 | for my $r ( @{$reg_list} ) |
| 1204 | { |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1205 | # Format is: |
| 1206 | # { id : [ name, { inst : addr, ... } ], ... } |
| 1207 | |
| 1208 | # The ID is a 3-byte hash of the register name (lowercase). |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1209 | my $id = sprintf('%06x', __hash(3, $r->{name})); |
| 1210 | |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1211 | if ( exists($regs->{$id}) ) |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1212 | { |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1213 | # Check for hash collisions. |
| 1214 | if ( $regs->{$id}->[0] ne $r->{name} ) |
| 1215 | { |
| 1216 | FAIL("Register hash collision for $id: " . |
| 1217 | "$regs->{$id}->[0] and $r->{name}"); |
| 1218 | } |
| 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | # Initialize this register. |
| 1223 | $regs->{$id} = [ $r->{name}, {} ]; |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
Zane Shelley | 74678c1 | 2021-06-04 15:46:16 -0500 | [diff] [blame] | 1226 | # Add the address for each instance of the register (shouldn't have to |
| 1227 | # worry about duplicates). |
| 1228 | for ( @{$r->{instance}} ) |
| 1229 | { |
| 1230 | $regs->{$id}->[1]->{$_->{reg_inst}} = $_->{addr}; |
| 1231 | } |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | my $data = |
| 1235 | { |
Zane Shelley | 13d0408 | 2021-06-04 08:40:48 -0500 | [diff] [blame] | 1236 | 'model_ec' => $info, |
Zane Shelley | 3d30890 | 2021-06-04 16:35:33 -0500 | [diff] [blame] | 1237 | 'attn_types' => $attns, |
Zane Shelley | bc3c040 | 2021-06-04 16:11:21 -0500 | [diff] [blame] | 1238 | 'registers' => $regs, |
| 1239 | 'signatures' => $sigs, |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1240 | }; |
| 1241 | |
| 1242 | print $fh to_json( $data, {utf8 => 1, pretty => 1, canonical => 1} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | #------------------------------------------------------------------------------- |
| 1246 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1247 | sub buildDataFiles($$) |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1248 | { |
| 1249 | my ( $dir, $data ) = @_; |
| 1250 | |
| 1251 | while ( my ($model_ec, $chip) = each %{$data} ) |
| 1252 | { |
| 1253 | unless ( defined $chip->{register} ) |
| 1254 | { |
| 1255 | FAIL("Chip $model_ec does not contain registers"); |
| 1256 | } |
| 1257 | unless ( defined $chip->{node} ) |
| 1258 | { |
| 1259 | FAIL("Chip $model_ec does not contain nodes"); |
| 1260 | } |
| 1261 | unless ( defined $chip->{attn_tree} ) |
| 1262 | { |
| 1263 | FAIL("Chip $model_ec does not contain attn_tree information"); |
| 1264 | } |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1265 | unless ( defined $chip->{signature} ) |
| 1266 | { |
| 1267 | FAIL("Chip $model_ec does not contain signatures"); |
| 1268 | } |
| 1269 | |
| 1270 | # Chip Data Binary files ############################################### |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1271 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1272 | if ( $gen_cdb ) |
| 1273 | { |
| 1274 | my $bin_file = "$dir/chip_data_" . lc $model_ec . ".cdb"; |
| 1275 | open my $bin_fh, '>', $bin_file or die "Cannot open $bin_file: $!"; |
| 1276 | binmode $bin_fh; # writes a binary file |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1277 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1278 | # Chip Data File metadata |
| 1279 | __bin($bin_fh, 1, $_) for ( unpack("C*", "CHIPDATA") ); |
Zane Shelley | 13d0408 | 2021-06-04 08:40:48 -0500 | [diff] [blame] | 1280 | __bin($bin_fh, 4, $SUPPORTED_MODEL_EC->{$model_ec}->{id}); |
| 1281 | __bin($bin_fh, 1, $FILE_VERSION->{VER_01} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1282 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1283 | __printRegisters( $bin_fh, $chip->{register} ); |
| 1284 | __printNodes( $bin_fh, $chip->{node} ); |
| 1285 | __printAttnTree( $bin_fh, $chip->{attn_tree} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1286 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1287 | close $bin_fh; |
| 1288 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1289 | |
Zane Shelley | 0a90501 | 2021-04-26 17:07:24 -0500 | [diff] [blame] | 1290 | # eBMC PEL parsing JSON ################################################ |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1291 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1292 | if ( $gen_json ) |
| 1293 | { |
| 1294 | my $file = "$dir/pel_parser_data_" . lc $model_ec . ".json"; |
| 1295 | open my $fh, '>', $file or die "Cannot open $file: $!"; |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1296 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1297 | __printParserData( $fh, $model_ec, $chip->{signature}, |
| 1298 | $chip->{register} ); |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1299 | |
Zane Shelley | 738276a | 2021-05-24 12:55:34 -0500 | [diff] [blame] | 1300 | close $fh; |
| 1301 | } |
Zane Shelley | abc51c2 | 2020-11-09 21:35:35 -0600 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |