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