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