summaryrefslogtreecommitdiffstats
path: root/src/build/vpo/VBU_Cacheline.pm
blob: 9adf3612edc91e3a05791bd43f7eaf6974e45351 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/vpo/VBU_Cacheline.pm $
#
# OpenPOWER HostBoot Project
#
# COPYRIGHT International Business Machines Corp. 2011,2014
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG
#
# Name:     src/build/vpo/VBU_Cacheline.pm
#
# Purpose:  routines for reading and writing a 64-bit value into L3 in an
#           AWAN session.  Accepts an address into L3, and 64-bit data hex word
#           (write).
#           VBU can only read/write to memory in 128-byte cachelines, so if
#           we want to write a memory location we must read in the entire
#           cacheline, modify the correct byte(s) and then write it back.
#           Called from shell script do_p8vbu_script_hbi*
#           Written in perl because that is what is being used for the debug
#           framework
#
# Author: Mark Wenning
#

package VBU_Cacheline;
require Exporter;

our @ISA        =   qw( Exporter );
our @EXPORT     =   qw( CLread CLwrite RunClocks P8_Ins_Start P8_Ins_Start_T0 P8_Ins_Stop P8_Ins_Query SetFlags);


#------------------------------------------------------------------------------
# Specify perl modules to use
#------------------------------------------------------------------------------
use strict;
use warnings;
use POSIX;
use Fcntl;

## 64-bit input
use bigint;
no  warnings    'portable';

#------------------------------------------------------------------------------
#   Forward declarations
#------------------------------------------------------------------------------
sub CLread;
sub CLwrite;
sub RunClocks;
sub P8_Ins_Start;
sub P8_Ins_Start_T0;
sub P8_Ins_Stop;
sub P8_Ins_Query;
sub P8_Flush_L2;
sub SetFlags;


############################################
##  constants
############################################
my  $curDir      =   $ENV{'PWD'};
my  $CLfile     =   "$curDir/istepmodereg.dma";
my  $CORE       =   "-cft";

my  $SIM_CLOCKS =   $ENV{'HB_SIMCLOCKS'};
if ( ! defined ( $SIM_CLOCKS ) || $SIM_CLOCKS == 0 ) {
	 $SIM_CLOCKS =   "4000000";
}
print "clocks=$SIM_CLOCKS\n";

#############################################
##  Internal Globals
#############################################
my  $CLdebug    =   0;
my  $CLtest     =   0;

##  flushed Flag, if 0, it means the L2 cache has not been flushed.
##  It must be flushed once before doing L3 reads
my  $L2_Flushed =   0;

my $vbuToolsDir = $ENV{'HB_VBUTOOLS'};
if ( ! defined ( $vbuToolsDir ) || $vbuToolsDir eq "" ) {
	$vbuToolsDir = "/gsa/ausgsa/projects/h/hostboot/vbutools/dev";
}

my $vbuProcsRoot = $ENV{'VBU_PROCS_ROOT'};
if ( ! defined ( $vbuProcsRoot ) || $vbuProcsRoot eq "" ) {
	my $vbuProcsRoot = $vbuToolsDir;
}

my  $DUMPCMD    =   "$vbuToolsDir/p8_dump_l3";
my  $GETMEMCMD    =   "$vbuToolsDir/proc_pba_getmem_coherent_wrap.x86";
my  $PUTMEMCMD    =   "$vbuToolsDir/proc_pba_putmem_coherent_wrap.x86";
my  $LOADCMD    =   "$vbuToolsDir/p8_load_l3";
my  $FLUSHCMD   =   "$vbuProcsRoot/proc_l2_flush_wrap.x86 $CORE -quiet";
my  $FLUSHQUERY =   "$vbuToolsDir/p8_check_l3";
my  $RUNCLKSCMD =   "simclock";

# Start/Stop/Query tool commands
# Note:
# -----
#    Query command output strings:
#       Single thread (-t0,1,..)   "Running" or "Quiesced"
#       All threads   (-tall)      A formatted table
#
# TODO:
# Needs to resolve the error when using "start -tall".  After running a while,
# start -tall may lock up with this error:
#   "proc_thread_control: Thread Start failed: RAS Status Run bit is not on"
# Temporary workaround is using only one thread as shown below.
# Lance Karm is still investigating.
my  $QUERYCMD   =   "sh -c \"$vbuProcsRoot/proc_thread_control_wrap.x86 -query  $CORE -tall -quiet | sed -n -e '/State/s/ *//gp'\"";
my  $STARTCMD   =   "sh -c \"$vbuProcsRoot/proc_thread_control_wrap.x86 -start  $CORE -tall -quiet -warncheck \| sed -e '/ Using deprecated ReturnCode function to assign integer/d'\"";
my  $STARTCMD_T0   =   "sh -c \"$vbuProcsRoot/proc_thread_control_wrap.x86 -start  $CORE -t0 -quiet -warncheck \| sed -e '/ Using deprecated ReturnCode function to assign integer/d'\"";
my  $STOPCMD    =   "sh -c \"$vbuProcsRoot/proc_thread_control_wrap.x86 -stop   $CORE -tall -quiet \| sed -e '/ Using deprecated ReturnCode function to assign integer/d'\"";
my  $RESETCMD   =   "$vbuProcsRoot/proc_thread_control_wrap.x86 -sreset $CORE -tall -quiet";

##
#==============================================================================
# SUBROUTINES
#==============================================================================


##
##  Read the cacheline at addr from L3 and dump it to a binary file.
##  Assumes that the input address is a binary addr on a 128 byte boundary
##
sub readcacheline( $ )
{
    my ( $addr ) =   @_;
    my $cmd;
    ##  my  $hexaddr    =   sprintf( "0x%x", $addr );
    my  $hexaddr    =   sprintf( "%x", $addr );

    if ( $CLdebug )   { print  STDERR __LINE__,  "--  Read cacheline at $hexaddr...\n"; }

    $cmd    =   "$GETMEMCMD $hexaddr 1 -f $CLfile -binmode -quiet $CORE";
    if ( $CLdebug )   {   print STDERR __LINE__,  "-- run $cmd ...\n";   }
    ( system( "sh -c \"$cmd  \| sed -e '/ Using deprecated ReturnCode function to assign integer/d'\"" ) == 0)
        or die "$cmd failed $? : $! \n";

}


##
##  derived from Perl Cookbook, 8.13
##  pack/unpack format is unsigned big endian 32-bit hi, lo
##  however, the input data from getopts still assumes that perl is compiled
##  for 64-bit #s
##
sub modifycacheline( $$ )
{
    my  ( $offset, $data )  =   @_;

    my $typedef     =   'N N';                        #  2 32-bit network order
    my $sizeof      =   length( pack($typedef,() ) );
    my $filesize    =   -s $CLfile;
    my $buffer;

    open( FH, "+< $CLfile") or die "can't open $CLfile : $!";
    binmode FH;                 ## not really necessary, but....
    seek( FH, $offset, SEEK_SET) or die "seek $CLfile failed: $!";
    read( FH, $buffer, $sizeof) == $sizeof or die "read failed: $!";

    ( my $hi, my $lo ) = unpack($typedef, $buffer);

    $hi = ($data >> 32) ;
    $lo = ($data & 0x00000000ffffffff);

    $buffer = pack($typedef, $hi, $lo );

    # back up one record
    seek( FH, -$sizeof, SEEK_CUR) or die "seek $CLfile failed: $!";

    print FH $buffer;

    close( FH ) or die "close $CLfile failed: $!";
}

##
##  Write modified file back to L3 cacheline.
##  This assumes that addr has already been converted to binary addr on a
##  128 byte boundary
##
sub writecacheline( $ )
{
    my  ( $addr )  =   @_;
    my  $cmd;
    ##  my  $hexaddr    =   sprintf( "0x%x", $addr );
    my  $hexaddr    =   sprintf( "%x", $addr );

    $cmd    =   "$LOADCMD -o $hexaddr -f $CLfile -b $CORE";
    if ( $CLdebug )   {   print STDERR __LINE__,  "-- run $cmd ...\n";   }
    ( system( $cmd ) == 0 )
        or die "$cmd failed, $? : $! \n";

}


##
##  Query the state of the simulator, "RUNNING" or "STOPPED".
##
sub P8_Ins_Query()
{
    my  $cmd    =   "$QUERYCMD";
    my  $retstr =   "";

    if ( $CLdebug ) {   print STDERR __LINE__,  "--   run $cmd ...\n";   }

    ## execute it with backticks so we can get the output.
    $retstr = `$cmd`;
    if ( $? != 0 )  { die "$cmd failed $? : $! \n"; }

    chomp($retstr);

    if ( $CLdebug ) {   print STDERR __LINE__,  "--   P8_Ins_Query: $retstr\n"; }

    # Return STOPPED only if all threads are quiesced (including POR state)
    $retstr =~ s/\(POR\)//g;
    if  ( "$retstr" eq "State|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|" )
    {
        return "STOPPED";
    }
    elsif  ( "$retstr" eq "State|Running|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|Quiesced|" )
    {
	return "RUNNING_T0";
    }

    # Return RUNNING if one or more threads are Running and the rest are Quiesced
    $retstr =~ s/Quiesced/Running/g;

    if ( "$retstr" eq "State|Running|Running|Running|Running|Running|Running|Running|Running|" )
    {
        return "RUNNING";
    }
    else
    {
        die "invalid string \"$retstr\" from P8_Ins_Query\n";
    }
}


##
##  Start the simulation.
##
sub P8_Ins_Start()
{
    my  $cmd    =   "$STARTCMD";

    if ( !$CLdebug )
    {   $cmd    .=  " -quiet";  }
    else
    {   print STDERR __LINE__,  "--   run $cmd ...\n";   }

    ( system( $cmd ) == 0 )
        or die "$cmd failed $? : $! \n";

    ##  reset the flushFlag, need to flush again before a read.
    $L2_Flushed   =   0;

}

##
##  Start master thread 0
##
sub P8_Ins_Start_T0()
{
    my  $cmd    =   "$STARTCMD_T0";

    if ( !$CLdebug )
    {   $cmd    .=  " -quiet";  }
    else
    {   print STDERR __LINE__,  "--   run $cmd ...\n";   }

    ( system( $cmd ) == 0 )
        or die "$cmd failed $? : $! \n";

    ##  reset the flushFlag, need to flush again before a read.
    $L2_Flushed   =   0;

}

##
##  Stop the simulation.
##
sub P8_Ins_Stop()
{
    my  $cmd    =   "$STOPCMD";

    if ( P8_Ins_Query() ne "STOPPED" )
    {
        if ( ! $CLdebug )
        {   $cmd    .=  " -quiet";  }
        else
        {   print STDERR __LINE__,  "--   run $cmd ...\n";  }

       ( system( $cmd ) == 0 )
            or die "$cmd failed $? : $! \n";

    }
    else
    {
        if ($CLdebug)   {   print STDERR __LINE__,    "-- P8_Ins_Stop: already STOPPED\n"; }
    }

}

##
##  Check if cache is flushed.
##  $TODO
##   p8_check_L3 will scan the L3 directory for unfilled cachelines and
##   return a string:
##      $ p8_check_l3 100 4 -c3 -f $labhome/foo -x -o
##      p8_check_l3 - address (0x100) not found in L3 directory.
##
##      $ p8_check_l3 100 4 -c3 -f $labhome/foo -x -o
##      p8_check_l3 - all addresses found in L3 directory.
##
sub P8_Check_Flushed
{
    my  ( $addr, $lines )   =   @_;
    my  $tmpfile =   "./tmpflush";
    my  $cmd    =   "$FLUSHQUERY";
    my  $rc     =   0;

        ## execute it with backticks so we can get the output.
    my $retstr = `$cmd $CORE -f $tmpfile -x -o`;
    if ( $? != 0 )  { die "$cmd failed $? : $! \n"; }

    chomp( $retstr );

    if ( $retstr    =~  /^.*.all addresses.*/ )
    {
        $rc =   1;
    }

    return  $rc;
}

##
##  Flush L2 Cache
##  This only needs to be done once after the clock is stopped,
##      thus the toggle flag
##
sub P8_Flush_L2()
{
    my  $cmd    =   "$FLUSHCMD";

    ## if ( !$L2_Flushed )
    ##{
        if ( $CLdebug )   {   print STDERR __LINE__,  "-- run $cmd ...\n";   }
        ( system( $cmd ) == 0 )
            or die "$cmd failed $? : $! \n";

   ##     ##  mark the CPU as flushed.
   ##     $L2_Flushed =   1;
   ## }
   ##
   ##if ($CLdebug)   {   print STDERR __LINE__,    "-- P8_FLush_L2 : $L2_Flushed\n"; }
}


##
##  tell the simulator to run for so many clock cycles.
##  If simulator is stopped, then start it up first.
##
sub RunClocks()
{
    my  $cmd    =   0;

    if ( $CLdebug ) {   printf STDERR __LINE__, "-- RunClocks()\n"; }

    $cmd    =   "$RUNCLKSCMD $SIM_CLOCKS   -quiet";
    if ( $CLdebug )   {   print STDERR __LINE__,  "-- run $cmd ...\n";   }
    ( system( $cmd ) == 0 )
        or die "$cmd failed, $? : $! \n";

}


##
##  Read a 64-bit value from L3 at hex addr.
##  Input is expected to be a hex ascii string
##
sub CLread( $ )
{
    my ( $addr ) =   @_;
    my $cmd;
    my  $CLbase     =   ( hex($addr) & 0xffffff80);
    my  $CLoffset   =   ( hex($addr) & (~0xffffff80) );
    my  $result     =   0;     ## 64-bit hex

    if ( $CLdebug ) {   printf STDERR __LINE__, "-- CLread( %s ) : CLbase=0x%x, CLoffset=0x%x\n", $addr, $CLbase, $CLoffset }

    system( "rm -f $CLfile" );
    readcacheline( $CLbase );

    ##  extract quadword from cacheline file
    my $typedef     =   'N N';                        #  QuadWord
    my $sizeof      =   length( pack($typedef,() ) );
    my $filesize    =   -s $CLfile;
    my $buffer;
    open( FH, "+< $CLfile") or die "can't open $CLfile : $!";
    binmode FH;                 ## not really necessary, but....
    seek( FH, $CLoffset, SEEK_SET) or die "seek $CLfile failed: $!";
    read( FH, $buffer, $sizeof) == $sizeof or die "read failed: $!";
    close( FH ) or die "close $CLfile failed: $!";

    ## unpack and reassemble as big-endian
    ( my $hi, my $lo )  =   unpack($typedef, $buffer);
    $result =   ( ( ( $hi << 32 ) & 0xffffffff00000000 ) | $lo );

    if ( $CLdebug )
    {
        printf STDERR __LINE__, "-- CLread( %s ) = 0x%lx  ", $addr, $result;
        dumpcacheline(" " );
    }

    return ( $result );
}

##
##  Write command byte to cacheline
##      Inputs are expected to be hex ascii strings
##
sub CLwrite( $$ )
{
    my  ( $addr, $data )    =   @_;
    my  $CLbase     =   ( hex($addr) & 0xffffff80 );
    my  $CLoffset   =   ( hex($addr) & (~0xffffff80) );
    my  $CLdata     =   hex($data);
    my  $result     =   0;

    if ( $CLdebug ) {   printf STDERR __LINE__, "-- CLwrite( %s, %s ) : CLbase=0x%x, CLoffset=0x%x, CLdata=0x%lx\n",
                                $addr, $data, $CLbase, $CLoffset, $CLdata;   }

    ##  clear the cacheline file
    system( "rm -f $CLfile" );

    ## issue the command to dump the cacheline to a file
    readcacheline( $CLbase );

    ## dumpcacheline( "after read", $CLfile );

   ##   modify the cacheline file
    modifycacheline( $CLoffset, $data );

    ## dumpcacheline( "after modify", $CLfile );

    ##  write the cacheline back to L3
    writecacheline( $CLbase );

    if ( $CLdebug )
    {
        ##  check, clear the cacheline file and read again
        system( "rm -f $CLfile" );
        readcacheline( $CLbase );
        dumpcacheline( "Readback", $CLfile );
    }

}


sub dumpcacheline()
{
    my  ( $comment )   =   @_;

    if ( $CLdebug )
    {
        print STDERR __LINE__, "--    $comment, dump cache file :\n";
        system( "xxd $CLfile" );
    }

}

sub SetFlags( $$ )
{
    my  ( $debug, $test )   =   @_;

    $CLdebug    =   $debug;
    $CLtest     =   $test;

    if ( $CLdebug )
    {
        print STDERR __LINE__, "-- CLdebug=$CLdebug, CLtest=$CLtest\n";
    }
}


##  required at the end of perl modules
1;

__END__
OpenPOWER on IntegriCloud