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