blob: c6dc0c767850df09acc4b57f066aa156926030ff [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 Shelleyabc51c22020-11-09 21:35:35 -060034};
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
Zane Shelleyd6826e52020-11-10 17:39:00 -0600366 for my $model_ec ( __expandModelEc($node->{model_ec}) )
Zane Shelleyabc51c22020-11-09 21:35:35 -0600367 {
Zane Shelleyd6826e52020-11-10 17:39:00 -0600368 if ( defined $regs->{$model_ec}->{$r->{name}} )
Zane Shelleyabc51c22020-11-09 21:35:35 -0600369 {
Zane Shelleyd6826e52020-11-10 17:39:00 -0600370 # This register already exists so check the contents for
371 # accuracy
372 unless ( __dirtyCompare($r, $regs->{$model_ec}->{$r->{name}}) )
373 {
374 FAIL("Duplicate register: $r->{name}");
375 }
Zane Shelleyabc51c22020-11-09 21:35:35 -0600376 }
Zane Shelleyd6826e52020-11-10 17:39:00 -0600377 else
378 {
379 # Add this node's register to the master register list.
380 $regs->{$model_ec}->{$r->{name}} = $r;
381 }
Zane Shelleyabc51c22020-11-09 21:35:35 -0600382 }
383 }
384
385 # Clean up this node's register data.
386 delete $node->{register};
387}
388
389#-------------------------------------------------------------------------------
390
391sub __normalizeCaptureGroup($$)
392{
393 my ( $node, $insts_data ) = @_;
394
395 # Capture groups are optional (although recommended).
396 return unless ( defined $node->{capture_group} );
397
398 for my $c ( @{$node->{capture_group}} )
399 {
400 # There must be at least one capture_register.
401 unless ( defined $c->{capture_register} and
402 0 < scalar @{$c->{capture_register}} )
403 {
404 FAIL("<capture_group> for node $node->{name} does not contain at " .
405 "least one <capture_register>" );
406 }
407
408 my @node_insts = BitRange::expand($c->{node_inst});
409
410 for my $r ( @{$c->{capture_register}} )
411 {
412 # node_inst and reg_inst must be the same size.
413 my @reg_insts = BitRange::expand($r->{reg_inst});
414 unless ( scalar @node_insts == scalar @reg_insts )
415 {
416 FAIL("capture_group/\@node_inst and capture_register/" .
417 "\@reg_inst list sized not equal for node $node->{name}");
418 }
419
420 # Expand the capture groups so there is one per node instance.
421 for ( 0 .. (scalar @node_insts - 1) )
422 {
423 my ( $ni, $ri ) = ( $node_insts[$_], $reg_insts[$_] );
424 push @{$insts_data->{$ni}->{capture_group}},
425 { reg_name => $r->{reg_name}, reg_inst => $ri };
426 }
427 }
428 }
429
430 # Clean up this node's capture group data.
431 delete $node->{capture_group};
432}
433
434#-------------------------------------------------------------------------------
435
436sub __normalizeExpr($$$$); # Called recursively
437
438sub __normalizeExpr($$$$)
439{
440 my ( $in, $ni, $idx, $size ) = @_;
441
442 my ( $t, $e, $v1, $v2 ) = ( $in->{type}, $in->{expr},
443 $in->{value1}, $in->{value2} );
444
445 my $out = { type => $t };
446
447 if ( "and" eq $t or "or" eq $t )
448 {
449 if ( defined $v1 or defined $v2 or
450 not defined $e or not (0 < scalar @{$e}) )
451 {
452 FAIL("Invalid parameters for and/or expression");
453 }
454
455 # Iterate each sub expression.
456 push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e});
457 }
458 elsif ( "not" eq $t )
459 {
460 if ( defined $v1 or defined $v2 or
461 not defined $e or not (1 == scalar @{$e}) )
462 {
463 FAIL("Invalid parameters for not expression");
464 }
465
466 # Iterate each sub expression.
467 push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e});
468 }
469 elsif ( "lshift" eq $t or "rshift" eq $t )
470 {
471 if ( not defined $v1 or defined $v2 or
472 not defined $e or not (1 == scalar @{$e}) )
473 {
474 FAIL("Invalid parameters for lshift/rshift expression");
475 }
476
477 # Copy value1.
478 $out->{value1} = $v1;
479
480 # Iterate each sub expression.
481 push @{$out->{expr}}, __normalizeExpr($_, $ni, $idx, $size) for (@{$e});
482 }
483 elsif ( "reg" eq $t )
484 {
485 if ( not defined $v1 or defined $e )
486 {
487 FAIL("Invalid parameters for reg expression");
488 }
489
490 # Copy value1.
491 $out->{value1} = $v1;
492
493 # value2 is optional in the XML, update the value to the node or
494 # register instance.
495 if ( defined $v2 )
496 {
497 my @reg_insts = BitRange::expand($v2);
498 unless ( $size == scalar @reg_insts )
499 {
500 FAIL("reg expression value2:$v2 list not the same ".
501 "size as containing node's rule instances:$size");
502 }
503
504 $out->{value2} = $reg_insts[$idx];
505 }
506 else
507 {
508 # The register instance is the same as the node instance.
509 $out->{value2} = $ni;
510 }
511 }
512 elsif ( "int" eq $t )
513 {
514 if ( not defined $v1 or defined $v2 or defined $e )
515 {
516 FAIL("Invalid parameters for int expression");
517 }
518
519 # Copy value1.
520 $out->{value1} = $v1;
521 }
522 else
523 {
524 FAIL("Unsupported expression type: $t");
525 }
526
527 return $out;
528}
529
530#-------------------------------------------------------------------------------
531
532sub __normalizeRule($$)
533{
534 my ( $node, $insts_data ) = @_;
535
536 # There must be at least one rule entry.
537 unless ( defined $node->{rule} and 0 < scalar @{$node->{rule}} )
538 {
539 FAIL( "Node $node->{name} does not contain at least one rule" );
540 }
541
542 # There should be only one rule per attention type and node instance for
543 # this node.
544 my $rule_dups = {};
545
546 for my $r ( @{$node->{rule}} )
547 {
548 # There should be exactly one parent expression.
549 unless ( 1 == scalar @{$r->{expr}} )
550 {
551 FAIL("Multiple parent expressions for rule: $node->{name} " .
552 "$r->{attn_type}");
553 }
554 my $expr = $r->{expr}->[0];
555
556 my @node_insts = BitRange::expand($r->{node_inst});
557 my $sz_insts = scalar @node_insts;
558
559 # Expand the expression for each node instance.
560 for my $idx ( 0 .. ($sz_insts - 1) )
561 {
562 my $ni = $node_insts[$idx];
563
564 # Check for duplicates.
565 if ( defined $rule_dups->{$r->{attn_type}}->{$ni} )
566 {
567 FAIL("Duplicate rule: $node->{name} $r->{attn_type} $ni");
568 }
569 else
570 {
571 $rule_dups->{$r->{attn_type}}->{$ni} = 1;
572 }
573
574 # Add the rule for this expression.
575 push @{$insts_data->{$ni}->{rule}},
576 { attn_type => $r->{attn_type},
577 expr => __normalizeExpr($expr, $ni, $idx, $sz_insts) };
578 }
579 }
580
581 # Clean up this node's rule data.
582 delete $node->{rule};
583}
584
585#-------------------------------------------------------------------------------
586
587sub __normalizeBit($$$)
588{
589 my ( $node, $sigs, $insts_data ) = @_;
590
591 # There must be at least one bit entry.
592 unless ( defined $node->{bit} and 0 < scalar @{$node->{bit}} )
593 {
594 FAIL( "Node $node->{name} does not contain at least one bit" );
595 }
596
597 my @node_insts = sort keys %{$insts_data};
598 my $sz_insts = scalar @node_insts;
599
600 # There should be only one child node per node instance bit position.
601 my $child_dups = {};
602
603 for my $b ( sort {$a->{pos} cmp $b->{pos}} @{$node->{bit}} )
604 {
605 my @child_insts = ();
606
607 # Ensure child_node and node_inst are set properly.
608 if ( defined $b->{child_node} )
609 {
610 # Ensure each bit has a default node_inst attribute if needed.
611 $b->{node_inst} = "0" unless ( defined $b->{node_inst} );
612
613 # Get all of the instances for this child node.
614 @child_insts = BitRange::expand($b->{node_inst});
615
616 # Both inst list must be equal in size.
617 unless ( $sz_insts == scalar @child_insts )
618 {
619 FAIL("node_inst attribute list size for node:$node->{name} " .
620 "bit:$b->{pos} does not match node instances " .
621 "represented by the <rule> element");
622 }
623 }
624 elsif ( defined $b->{node_inst} )
625 {
626 FAIL("node_inst attribute exists for node:$node->{name} " .
627 "bit:$b->{pos} with no child_node attribute");
628 }
629
630 # Get the signatures for each node, instance, and bit position.
631 for my $p ( BitRange::expand($b->{pos}) )
632 {
633 for my $i ( 0 .. ($sz_insts-1) )
634 {
635 my ( $n, $ni ) = ( $node->{name}, $node_insts[$i] );
636
637 # This is to cover a bug in the figtree information where there
638 # currently is no comment for some bits.
639 $b->{content} = "" unless ( defined $b->{content} );
640
Zane Shelleyd6826e52020-11-10 17:39:00 -0600641 for my $model_ec ( __expandModelEc($node->{model_ec}) )
Zane Shelleyabc51c22020-11-09 21:35:35 -0600642 {
Zane Shelleyd6826e52020-11-10 17:39:00 -0600643 # Check if this signature already exists.
644 if ( defined $sigs->{$model_ec}->{$n}->{$ni}->{$p} and
645 $b->{content} ne $sigs->{$model_ec}->{$n}->{$ni}->{$p} )
646 {
647 FAIL("Duplicate signature for $n $ni $p");
648 }
Zane Shelleyabc51c22020-11-09 21:35:35 -0600649
Zane Shelleyd6826e52020-11-10 17:39:00 -0600650 # Get the signatures for each node, instance, and bit
651 # position.
652 $sigs->{$model_ec}->{$n}->{$ni}->{$p} = $b->{content};
653 }
Zane Shelleyabc51c22020-11-09 21:35:35 -0600654
655 # Move onto the next instance unless a child node exists.
656 next unless ( defined $b->{child_node} );
657
658 my $pi = $child_insts[$i];
659
660 my $child = { pos => $p,
661 child_node => $b->{child_node},
662 node_inst => $pi };
663
664 # Ensure this child node doesn't already exist.
665 if ( defined $child_dups->{$ni}->{$p} and
666 not __dirtyCompare($child, $child_dups->{$ni}->{$p}) )
667 {
668 FAIL("Duplicate child_node for $n $ni $p");
669 }
670
671 # Add this child node.
672 push @{$insts_data->{$ni}->{bit}}, $child;
673 }
674 }
675 }
676
677 # Clean up this node's bit data.
678 delete $node->{bit};
679}
680
681#-------------------------------------------------------------------------------
682
683sub __normalizeNode($$$)
684{
685 my ( $node, $regs, $sigs ) = @_;
686
687 # Ensure a valid register type.
688 unless ( grep { /^$node->{reg_type}$/ } keys %{$REGISTER_TYPE} )
689 {
690 FAIL( "Unsupported register type: $node->{reg_type}" );
691 }
692
693 my $insts_data = {}; # Collect data for each instance of this node.
694
695 # First, expand the <local_fir> data if it exists.
696 __normalizeLocalFir($node);
697
698 # All registers will be put in a master register list for the chip.
699 __normalizeRegister($node, $regs);
700
701 # Split the capture group information per node instance.
702 __normalizeCaptureGroup($node, $insts_data);
703
704 # Split the rule information per node instance. The sorted instance list
705 # will be used as indexes for the node_inst attribute of the <bit> elements.
706 __normalizeRule($node, $insts_data);
707
708 # Finally, collect the signature details and split the bit information per
709 # node instance.
710 __normalizeBit($node, $sigs, $insts_data);
711
712 # Now that we have all of the node data, collapse the instance data into
713 # a list.
714 for ( sort keys %{$insts_data} )
715 {
716 $insts_data->{$_}->{node_inst} = $_;
717 push @{$node->{instance}}, $insts_data->{$_};
718 }
719}
720
721#-------------------------------------------------------------------------------
722
723sub normalizeXML($)
724{
725 my ( $xml ) = @_;
726
727 my $data = {};
728
729 # Iterate each chip file.
730 for my $chip ( @{$xml->{chip}} )
731 {
732 # Iterate each model/EC.
733 for my $model_ec ( __expandModelEc($chip->{model_ec}) )
734 {
735 # Ensure there is not a duplicate definition for a model/EC.
736 if ( $data->{$model_ec}->{chip} )
737 {
738 FAIL("Duplicate data for model/EC $model_ec in:\n" .
739 " $data->{$model_ec}->{chip}->{path}\n" .
740 " $chip->{path}");
741 }
742
743 # Add this chip to the data.
744 $data->{$model_ec}->{attn_tree} = $chip->{attn_tree};
745 }
746 }
747
748 # Extract the data for each node.
749 my ( $regs, $sigs, $node_dups ) = ( {}, {}, {} );
750 for my $node ( sort { $a->{name} cmp $b->{name} } @{$xml->{node}} )
751 {
752 # A node may be defined for more than one model/EC.
753 for my $model_ec ( __expandModelEc($node->{model_ec}) )
754 {
755 # A node can only be defined once per model/EC.
756 if ( defined $node_dups->{$model_ec}->{$node->{name}} )
757 {
758 FAIL( "Duplicate node defined for $model_ec -> $node->{name} ");
759 }
760 else
761 {
762 $node_dups->{$model_ec}->{$node->{name}} = 1;
763 }
764
Zane Shelleyd6826e52020-11-10 17:39:00 -0600765 # Initialize the master list of registers and signatures of this
766 # model/EC, if necessary.
Zane Shelleyabc51c22020-11-09 21:35:35 -0600767
Zane Shelleyd6826e52020-11-10 17:39:00 -0600768 $regs->{$model_ec} = {} unless ( defined $regs->{$model_ec} );
769 $sigs->{$model_ec} = {} unless ( defined $sigs->{$model_ec} );
770 }
Zane Shelleyabc51c22020-11-09 21:35:35 -0600771
Zane Shelleyd6826e52020-11-10 17:39:00 -0600772 # The same node content will be used for each model/EC characterized by
773 # this node. There is some normalization that needs to happen because of
774 # shorthand elements, like <local_fir>, and some error checking. This
775 # only needs to be done once per node, not per model/EC.
776 __normalizeNode( $node, $regs, $sigs );
Zane Shelleyabc51c22020-11-09 21:35:35 -0600777
Zane Shelleyd6826e52020-11-10 17:39:00 -0600778 # Push the node data for each model/EC.
779 for my $model_ec ( __expandModelEc($node->{model_ec}) )
780 {
Zane Shelleyabc51c22020-11-09 21:35:35 -0600781 push @{$data->{$model_ec}->{node}}, $node;
782 }
783 }
784
785 # Sort and collapse the master register list.
786 for my $m ( keys %{$regs} )
787 {
788 for my $n ( sort keys %{$regs->{$m}} )
789 {
790 push @{$data->{$m}->{register}}, $regs->{$m}->{$n};
791 }
792 }
793
794 # Collapse the signature lists.
795 for my $m ( keys %{$sigs} )
796 {
797 for my $n ( sort keys %{$sigs->{$m}} )
798 {
799 for my $i ( sort {$a <=> $b} keys %{$sigs->{$m}->{$n}} )
800 {
801 for my $b ( sort {$a <=> $b} keys %{$sigs->{$m}->{$n}->{$i}} )
802 {
803 push @{$data->{$m}->{signature}},
804 { name => $n, inst => $i, bit => $b,
805 desc => $sigs->{$m}->{$n}->{$i}->{$b} };
806 }
807 }
808 }
809 }
810
811 return $data;
812}
813
814#-------------------------------------------------------------------------------
815# Output functions
816#-------------------------------------------------------------------------------
817
818# The $num passed into this function can be a numeric of string. All values are
819# converted to a hex string and then into the binary format. This helps avoid
820# portability issues with endianess. Requirements:
821# - Hex strings must start with '0x'.
822# - For portability, 64-bit numbers must be passed as a hex string.
823sub __bin($$$)
824{
825 my ( $fh, $bytes, $num ) = @_;
826
827 # $bytes must be a positive integer.
828 die "Invalid bytes: $bytes" unless ( 0 < $bytes );
829
830 my $str = ''; # Default invalid string
831
832 my $char = $bytes * 2; # Number of characters in the string.
833
834 # Check if $num is a hex string.
835 if ( $num =~ /^0[x|X](.*)/ )
836 {
837 $str = $1; # strip the '0x'
838 }
839 # Check if $num is string or numeric decimal integer (32-bit max).
840 elsif ( $num =~ /^[0-9]+$/ and $bytes <= 4 )
841 {
842 $str = sprintf("%0${char}x", $num); # Convert to hex string
843 }
844
845 # Check for a hex number with the valid size.
846 unless ( $str =~ /^[0-9a-fA-F]{$char}$/ )
847 {
848 die "Invalid number: $num (size: $bytes)";
849 }
850
851 # Print the binary string.
852 print $fh pack( "H$char", $str );
853}
854
855#-------------------------------------------------------------------------------
856
857sub __hash($$)
858{
859 my $bytes = shift;
860 my @str = unpack("C*", shift); # returns an array of ascii values
861
862 # Currently only supporting 1, 2, 3, and 4 byte hashes.
863 unless ( 1 <= $bytes and $bytes <= 4 )
864 {
865 FAIL("Unsupported hash size: $bytes");
866 }
867
868 # Add padding to the end of the character array so that the size is
869 # divisible by $bytes.
870 push @str, 0 until ( 0 == scalar(@str) % $bytes );
871
872 # This hash is a simple "n*s[0] + (n-1)*s[1] + ... + s[n-1]" algorithm,
873 # where s[i] is a $bytes size chunk of the input string.
874
875 my ( $sumA, $sumB ) = ( 0, 0 );
876 while ( my @chunk = splice @str, 0, $bytes )
877 {
878 # Combine the chunk array into a single value.
879 my $val = 0; for ( @chunk ) { $val <<= 8; $val |= $_; }
880
881 # Apply the simple hash.
882 $sumA += $val;
883 $sumB += $sumA;
884 }
885
886 # Mask off everything except the target number of bytes.
887 $sumB &= 0xffffffff >> ((4 - $bytes) * 8);
888
889 return $sumB;
890}
891
892#-------------------------------------------------------------------------------
893
894sub __printRegisters($$)
895{
896 my ( $fh, $data ) = @_;
897
898 my $num_regs = scalar @{$data};
899 FAIL("No registers defined") unless ( 0 < $num_regs );
900
901 # Register list metadata
902 __bin($fh, 1, $_) for ( unpack("C*", "REGS") );
903 __bin($fh, 3, $num_regs);
904
905 my $reg_ids = {}; # for hash duplicate checking
906
907 for my $r ( @{$data} )
908 {
909 # Get the hash of the register name and check for duplicates.
910 my $id = __hash(3, $r->{name});
911 if ( defined $reg_ids->{$id} )
912 {
913 FAIL("Duplicate register ID hash " . sprintf('0x%08x', $id) .
914 " for $r->{name} and $reg_ids->{$id}");
915 }
916 else
917 {
918 $reg_ids->{$id} = $r->{name};
919 }
920
921 # Get the attribute flags.
922 my $flags = 0x00;
923 $flags |= 0x80 if ( $r->{access} =~ /R/ );
924 $flags |= 0x40 if ( $r->{access} =~ /W/ );
925
926 # Get the number of address instances.
927 my $num_inst = scalar @{$r->{instance}};
928 unless ( 0 < $num_inst )
929 {
930 FAIL("No register instances defined for $r->{name}");
931 }
932
933 # Register metadata
934 __bin($fh, 3, $id );
935 __bin($fh, 1, $REGISTER_TYPE->{$r->{reg_type}}->{id});
936 __bin($fh, 1, $flags );
937 __bin($fh, 1, $num_inst);
938
939 for my $i ( @{$r->{instance}} )
940 {
941 my $s = $REGISTER_TYPE->{$r->{reg_type}}->{addr_size};
942
943 # Register Instance metadata
944 __bin($fh, 1, $i->{reg_inst});
945 __bin($fh, $s, $i->{addr} );
946 }
947 }
948}
949
950#-------------------------------------------------------------------------------
951
952sub __printExpr($$$);
953
954sub __printExpr($$$)
955{
956 my ( $fh, $size, $expr ) = @_;
957
958 my ( $t, $e, $v1, $v2 ) = ( $expr->{type}, $expr->{expr},
959 $expr->{value1}, $expr->{value2} );
960
961 if ( "reg" eq $t )
962 {
963 __bin($fh, 1, 0x01); # expression type for "reg"
964 __bin($fh, 3, __hash(3,$v1)); # register id
965 __bin($fh, 1, $v2); # register instance
966 }
967 elsif ( "int" eq $t )
968 {
969 __bin($fh, 1, 0x02); # expression type for "int"
970 __bin($fh, $size, $v1); # integer value
971 }
972 elsif ( "and" eq $t )
973 {
974 __bin($fh, 1, 0x10); # expression type for "and"
975 __bin($fh, 1, scalar @{$e}); # number of sub-expressions
976 __printExpr($fh, $size, $_) for ( @{$e} ); # add each sub-expression
977 }
978 elsif ( "or" eq $t )
979 {
980 __bin($fh, 1, 0x11); # expression type for "or"
981 __bin($fh, 1, scalar @{$e}); # number of sub-expressions
982 __printExpr($fh, $size, $_) for ( @{$e} ); # add each sub-expression
983 }
984 elsif ( "not" eq $t )
985 {
986 __bin($fh, 1, 0x12); # expression type for "not"
987 __printExpr($fh, $size, $e->[0]); # add only sub-expression
988 }
989 elsif ( "lshift" eq $t )
990 {
991 __bin($fh, 1, 0x13); # expression type for "lshift"
992 __bin($fh, 1, $v1); # shift amount
993 __printExpr($fh, $size, $e->[0]); # add only sub-expression
994 }
995 elsif ( "rshift" eq $t )
996 {
997 __bin($fh, 1, 0x14); # expression type for "rshift"
998 __bin($fh, 1, $v1); # shift amount
999 __printExpr($fh, $size, $e->[0]); # add only sub-expression
1000 }
1001}
1002
1003#-------------------------------------------------------------------------------
1004
1005sub __printNodes($$)
1006{
1007 my ( $fh, $data ) = @_;
1008
1009 my $num_nodes = scalar @{$data};
1010 FAIL("No nodes defined") unless ( 0 < $num_nodes );
1011
1012 # Isolation Node list metadata
1013 __bin($fh, 1, $_) for ( unpack("C*", "NODE") );
1014 __bin($fh, 2, $num_nodes);
1015
1016 my $node_ids = {}; # for hash duplicate checking
1017
1018 for my $n ( @{$data} )
1019 {
1020 # Get the hash of the node name and check for duplicates.
1021 my $id = __hash(2, $n->{name});
1022 if ( defined $node_ids->{$id} )
1023 {
1024 FAIL("Duplicate node ID hash " . sprintf('0x%08x', $id) .
1025 " for $n->{name} and $node_ids->{$id}");
1026 }
1027 else
1028 {
1029 $node_ids->{$id} = $n->{name};
1030 }
1031
1032 my $num_insts = scalar @{$n->{instance}};
1033 unless ( 0 < $num_insts )
1034 {
1035 FAIL("No nodes instances defined for $n->{name}");
1036 }
1037
1038 my $reg_type = $REGISTER_TYPE->{$n->{reg_type}}->{id};
1039 my $reg_size = $REGISTER_TYPE->{$n->{reg_type}}->{reg_size};
1040
1041 # Register metadata
1042 __bin($fh, 2, $id);
1043 __bin($fh, 1, $reg_type);
1044 __bin($fh, 1, $num_insts);
1045
1046 for my $i ( @{$n->{instance}} )
1047 {
1048 # Capture groups are optional.
1049 my $num_cap_regs = (defined $i->{capture_group})
1050 ? scalar @{$i->{capture_group}} : 0;
1051
1052 # At least one rule is required.
1053 my $num_rules = scalar @{$i->{rule}};
1054 unless ( 0 < $num_rules )
1055 {
1056 FAIL("No rule for $n->{name} $i->{node_inst}");
1057 }
1058
1059 # Child nodes may not exist for this node.
1060 my $num_bit = (defined $i->{bit}) ? scalar @{$i->{bit}} : 0;
1061
1062 # Register instance metadata
1063 __bin($fh, 1, $i->{node_inst});
1064 __bin($fh, 1, $num_cap_regs );
1065 __bin($fh, 1, $num_rules );
1066 __bin($fh, 1, $num_bit );
1067
1068 if ( 0 < $num_cap_regs )
1069 {
1070 for my $cg ( @{$i->{capture_group}} )
1071 {
1072 # Register capture register metadata
1073 __bin($fh, 3, __hash(3, $cg->{reg_name}));
1074 __bin($fh, 1, $cg->{reg_inst} );
1075 }
1076 }
1077
1078 for my $r ( @{$i->{rule}} )
1079 {
1080 # Register rule metadata
1081 __bin($fh, 1, $ATTN_TYPE->{$r->{attn_type}});
1082 __printExpr($fh, $reg_size, $r->{expr});
1083 }
1084
1085 if ( 0 < $num_bit )
1086 {
1087 for my $b ( @{$i->{bit}} )
1088 {
1089 # Register child node metadata
1090 __bin($fh, 1, $b->{pos} );
1091 __bin($fh, 2, __hash(2, $b->{child_node}));
1092 __bin($fh, 1, $b->{node_inst} );
1093 }
1094 }
1095 }
1096 }
1097}
1098
1099#-------------------------------------------------------------------------------
1100
1101sub __printAttnTree($$)
1102{
1103 my ( $fh, $data ) = @_;
1104
1105 my $num_root_nodes = scalar @{$data};
1106 FAIL("No root nodes defined") unless ( 0 < $num_root_nodes );
1107
1108 # Root Node list metadata
1109 __bin($fh, 1, $_) for ( unpack("C*", "ROOT") );
1110 __bin($fh, 1, $num_root_nodes);
1111
1112 for my $r ( @{$data} )
1113 {
1114 # Root Node metadata
1115 __bin($fh, 1, $ATTN_TYPE->{$r->{attn_type}});
1116 __bin($fh, 2, __hash(2, $r->{root_node}) );
1117 __bin($fh, 1, $r->{node_inst} );
1118 }
1119}
1120
1121#-------------------------------------------------------------------------------
1122
1123sub __printSignatures($$)
1124{
1125 my ( $fh, $data ) = @_;
1126
1127 my $num_sigs = scalar @{$data};
1128 FAIL("No signatures defined") unless ( 0 < $num_sigs );
1129
1130 for my $s ( @{$data} )
1131 {
1132 my $sig = __hash(2,$s->{name}) << 16 | $s->{inst} << 8 | $s->{bit};
1133 # TODO: This is temporary until we have defined the signature files.
1134 print $fh sprintf('0x%08x',$sig) .
1135 " $s->{name} $s->{inst} $s->{bit} $s->{desc}\n";
1136 }
1137}
1138
1139#-------------------------------------------------------------------------------
1140
1141sub buildBinary($$)
1142{
1143 my ( $dir, $data ) = @_;
1144
1145 while ( my ($model_ec, $chip) = each %{$data} )
1146 {
1147 unless ( defined $chip->{register} )
1148 {
1149 FAIL("Chip $model_ec does not contain registers");
1150 }
1151 unless ( defined $chip->{node} )
1152 {
1153 FAIL("Chip $model_ec does not contain nodes");
1154 }
1155 unless ( defined $chip->{attn_tree} )
1156 {
1157 FAIL("Chip $model_ec does not contain attn_tree information");
1158 }
1159
1160 my $bin_file = "$dir/chip_data_" . lc $model_ec . ".cdb";
1161 open my $bin_fh, '>', $bin_file or die "Cannot open $bin_file: $!";
1162 binmode $bin_fh; # writes a binary file
1163
1164 # Chip Data File metadata
1165 __bin($bin_fh, 1, $_) for ( unpack("C*", "CHIPDATA") );
1166 __bin($bin_fh, 4, $SUPPORTED_MODEL_EC->{$model_ec});
1167 __bin($bin_fh, 1, $FILE_VERSION->{VER_01} );
1168
1169 __printRegisters( $bin_fh, $chip->{register} );
1170 __printNodes( $bin_fh, $chip->{node} );
1171 __printAttnTree( $bin_fh, $chip->{attn_tree} );
1172
1173 close $bin_fh;
1174
1175 unless ( defined $chip->{signature} )
1176 {
1177 FAIL("Chip $model_ec does not contain signatures");
1178 }
1179
1180 my $sig_file = "$dir/chip_signatures_" . lc $model_ec . ".txt";
1181 open my $sig_fh, '>', $sig_file or die "Cannot open $sig_file: $!";
1182
1183 __printSignatures( $sig_fh, $chip->{signature} );
1184
1185 close $sig_fh;
1186 }
1187}
1188