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