summaryrefslogtreecommitdiff
path: root/arch/tile/include/uapi/arch/sim.h
blob: c4183dcd2ea73f13e15509fad3063cda0121d221 (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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/*
 * Copyright 2010 Tilera Corporation. All Rights Reserved.
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation, version 2.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 *   NON INFRINGEMENT.  See the GNU General Public License for
 *   more details.
 */

/**
 * @file
 *
 * Provides an API for controlling the simulator at runtime.
 */

/**
 * @addtogroup arch_sim
 * @{
 *
 * An API for controlling the simulator at runtime.
 *
 * The simulator's behavior can be modified while it is running.
 * For example, human-readable trace output can be enabled and disabled
 * around code of interest.
 *
 * There are two ways to modify simulator behavior:
 * programmatically, by calling various sim_* functions, and
 * interactively, by entering commands like "sim set functional true"
 * at the tile-monitor prompt.  Typing "sim help" at that prompt provides
 * a list of interactive commands.
 *
 * All interactive commands can also be executed programmatically by
 * passing a string to the sim_command function.
 */

#ifndef __ARCH_SIM_H__
#define __ARCH_SIM_H__

#include <arch/sim_def.h>
#include <arch/abi.h>

#ifndef __ASSEMBLER__

#include <arch/spr_def.h>


/**
 * Return true if the current program is running under a simulator,
 * rather than on real hardware.  If running on hardware, other "sim_xxx()"
 * calls have no useful effect.
 */
static inline int
sim_is_simulator(void)
{
  return __insn_mfspr(SPR_SIM_CONTROL) != 0;
}


/**
 * Checkpoint the simulator state to a checkpoint file.
 *
 * The checkpoint file name is either the default or the name specified
 * on the command line with "--checkpoint-file".
 */
static __inline void
sim_checkpoint(void)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_CHECKPOINT);
}


/**
 * Report whether or not various kinds of simulator tracing are enabled.
 *
 * @return The bitwise OR of these values:
 *
 * SIM_TRACE_CYCLES (--trace-cycles),
 * SIM_TRACE_ROUTER (--trace-router),
 * SIM_TRACE_REGISTER_WRITES (--trace-register-writes),
 * SIM_TRACE_DISASM (--trace-disasm),
 * SIM_TRACE_STALL_INFO (--trace-stall-info)
 * SIM_TRACE_MEMORY_CONTROLLER (--trace-memory-controller)
 * SIM_TRACE_L2_CACHE (--trace-l2)
 * SIM_TRACE_LINES (--trace-lines)
 */
static __inline unsigned int
sim_get_tracing(void)
{
  return __insn_mfspr(SPR_SIM_CONTROL) & SIM_TRACE_FLAG_MASK;
}


/**
 * Turn on or off different kinds of simulator tracing.
 *
 * @param mask Either one of these special values:
 *
 * SIM_TRACE_NONE (turns off tracing),
 * SIM_TRACE_ALL (turns on all possible tracing).
 *
 * or the bitwise OR of these values:
 *
 * SIM_TRACE_CYCLES (--trace-cycles),
 * SIM_TRACE_ROUTER (--trace-router),
 * SIM_TRACE_REGISTER_WRITES (--trace-register-writes),
 * SIM_TRACE_DISASM (--trace-disasm),
 * SIM_TRACE_STALL_INFO (--trace-stall-info)
 * SIM_TRACE_MEMORY_CONTROLLER (--trace-memory-controller)
 * SIM_TRACE_L2_CACHE (--trace-l2)
 * SIM_TRACE_LINES (--trace-lines)
 */
static __inline void
sim_set_tracing(unsigned int mask)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_TRACE_SPR_ARG(mask));
}


/**
 * Request dumping of different kinds of simulator state.
 *
 * @param mask Either this special value:
 *
 * SIM_DUMP_ALL (dump all known state)
 *
 * or the bitwise OR of these values:
 *
 * SIM_DUMP_REGS (the register file),
 * SIM_DUMP_SPRS (the SPRs),
 * SIM_DUMP_ITLB (the iTLB),
 * SIM_DUMP_DTLB (the dTLB),
 * SIM_DUMP_L1I (the L1 I-cache),
 * SIM_DUMP_L1D (the L1 D-cache),
 * SIM_DUMP_L2 (the L2 cache),
 * SIM_DUMP_SNREGS (the switch register file),
 * SIM_DUMP_SNITLB (the switch iTLB),
 * SIM_DUMP_SNL1I (the switch L1 I-cache),
 * SIM_DUMP_BACKTRACE (the current backtrace)
 */
static __inline void
sim_dump(unsigned int mask)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_DUMP_SPR_ARG(mask));
}


/**
 * Print a string to the simulator stdout.
 *
 * @param str The string to be written.
 */
static __inline void
sim_print(const char* str)
{
  for ( ; *str != '\0'; str++)
  {
    __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
                 (*str << _SIM_CONTROL_OPERATOR_BITS));
  }
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
               (SIM_PUTC_FLUSH_BINARY << _SIM_CONTROL_OPERATOR_BITS));
}


/**
 * Print a string to the simulator stdout.
 *
 * @param str The string to be written (a newline is automatically added).
 */
static __inline void
sim_print_string(const char* str)
{
  for ( ; *str != '\0'; str++)
  {
    __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
                 (*str << _SIM_CONTROL_OPERATOR_BITS));
  }
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
               (SIM_PUTC_FLUSH_STRING << _SIM_CONTROL_OPERATOR_BITS));
}


/**
 * Execute a simulator command string.
 *
 * Type 'sim help' at the tile-monitor prompt to learn what commands
 * are available.  Note the use of the tile-monitor "sim" command to
 * pass commands to the simulator.
 *
 * The argument to sim_command() does not include the leading "sim"
 * prefix used at the tile-monitor prompt; for example, you might call
 * sim_command("trace disasm").
 */
static __inline void
sim_command(const char* str)
{
  int c;
  do
  {
    c = *str++;
    __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_COMMAND |
                 (c << _SIM_CONTROL_OPERATOR_BITS));
  }
  while (c);
}



#ifndef __DOXYGEN__

/**
 * The underlying implementation of "_sim_syscall()".
 *
 * We use extra "and" instructions to ensure that all the values
 * we are passing to the simulator are actually valid in the registers
 * (i.e. returned from memory) prior to the SIM_CONTROL spr.
 */
static __inline long _sim_syscall0(int val)
{
  long result;
  __asm__ __volatile__ ("mtspr SIM_CONTROL, r0"
                        : "=R00" (result) : "R00" (val));
  return result;
}

static __inline long _sim_syscall1(int val, long arg1)
{
  long result;
  __asm__ __volatile__ ("{ and zero, r1, r1; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (result) : "R00" (val), "R01" (arg1));
  return result;
}

static __inline long _sim_syscall2(int val, long arg1, long arg2)
{
  long result;
  __asm__ __volatile__ ("{ and zero, r1, r2; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (result)
                        : "R00" (val), "R01" (arg1), "R02" (arg2));
  return result;
}

/* Note that _sim_syscall3() and higher are technically at risk of
   receiving an interrupt right before the mtspr bundle, in which case
   the register values for arguments 3 and up may still be in flight
   to the core from a stack frame reload. */

static __inline long _sim_syscall3(int val, long arg1, long arg2, long arg3)
{
  long result;
  __asm__ __volatile__ ("{ and zero, r3, r3 };"
                        "{ and zero, r1, r2; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (result)
                        : "R00" (val), "R01" (arg1), "R02" (arg2),
                          "R03" (arg3));
  return result;
}

static __inline long _sim_syscall4(int val, long arg1, long arg2, long arg3,
                                  long arg4)
{
  long result;
  __asm__ __volatile__ ("{ and zero, r3, r4 };"
                        "{ and zero, r1, r2; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (result)
                        : "R00" (val), "R01" (arg1), "R02" (arg2),
                          "R03" (arg3), "R04" (arg4));
  return result;
}

static __inline long _sim_syscall5(int val, long arg1, long arg2, long arg3,
                                  long arg4, long arg5)
{
  long result;
  __asm__ __volatile__ ("{ and zero, r3, r4; and zero, r5, r5 };"
                        "{ and zero, r1, r2; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (result)
                        : "R00" (val), "R01" (arg1), "R02" (arg2),
                          "R03" (arg3), "R04" (arg4), "R05" (arg5));
  return result;
}

/**
 * Make a special syscall to the simulator itself, if running under
 * simulation. This is used as the implementation of other functions
 * and should not be used outside this file.
 *
 * @param syscall_num The simulator syscall number.
 * @param nr The number of additional arguments provided.
 *
 * @return Varies by syscall.
 */
#define _sim_syscall(syscall_num, nr, args...) \
  _sim_syscall##nr( \
    ((syscall_num) << _SIM_CONTROL_OPERATOR_BITS) | SIM_CONTROL_SYSCALL, \
    ##args)


/* Values for the "access_mask" parameters below. */
#define SIM_WATCHPOINT_READ    1
#define SIM_WATCHPOINT_WRITE   2
#define SIM_WATCHPOINT_EXECUTE 4


static __inline int
sim_add_watchpoint(unsigned int process_id,
                   unsigned long address,
                   unsigned long size,
                   unsigned int access_mask,
                   unsigned long user_data)
{
  return _sim_syscall(SIM_SYSCALL_ADD_WATCHPOINT, 5, process_id,
                     address, size, access_mask, user_data);
}


static __inline int
sim_remove_watchpoint(unsigned int process_id,
                      unsigned long address,
                      unsigned long size,
                      unsigned int access_mask,
                      unsigned long user_data)
{
  return _sim_syscall(SIM_SYSCALL_REMOVE_WATCHPOINT, 5, process_id,
                     address, size, access_mask, user_data);
}


/**
 * Return value from sim_query_watchpoint.
 */
struct SimQueryWatchpointStatus
{
  /**
   * 0 if a watchpoint fired, 1 if no watchpoint fired, or -1 for
   * error (meaning a bad process_id).
   */
  int syscall_status;

  /**
   * The address of the watchpoint that fired (this is the address
   * passed to sim_add_watchpoint, not an address within that range
   * that actually triggered the watchpoint).
   */
  unsigned long address;

  /** The arbitrary user_data installed by sim_add_watchpoint. */
  unsigned long user_data;
};


static __inline struct SimQueryWatchpointStatus
sim_query_watchpoint(unsigned int process_id)
{
  struct SimQueryWatchpointStatus status;
  long val = SIM_CONTROL_SYSCALL |
    (SIM_SYSCALL_QUERY_WATCHPOINT << _SIM_CONTROL_OPERATOR_BITS);
  __asm__ __volatile__ ("{ and zero, r1, r1; mtspr SIM_CONTROL, r0 }"
                        : "=R00" (status.syscall_status),
                          "=R01" (status.address),
                          "=R02" (status.user_data)
                        : "R00" (val), "R01" (process_id));
  return status;
}


/* On the simulator, confirm lines have been evicted everywhere. */
static __inline void
sim_validate_lines_evicted(unsigned long long pa, unsigned long length)
{
#ifdef __LP64__
  _sim_syscall(SIM_SYSCALL_VALIDATE_LINES_EVICTED, 2, pa, length);
#else
  _sim_syscall(SIM_SYSCALL_VALIDATE_LINES_EVICTED, 4,
               0 /* dummy */, (long)(pa), (long)(pa >> 32), length);
#endif
}


/* Return the current CPU speed in cycles per second. */
static __inline long
sim_query_cpu_speed(void)
{
  return _sim_syscall(SIM_SYSCALL_QUERY_CPU_SPEED, 0);
}

#endif /* !__DOXYGEN__ */




/**
 * Modify the shaping parameters of a shim.
 *
 * @param shim The shim to modify. One of:
 *   SIM_CONTROL_SHAPING_GBE_0
 *   SIM_CONTROL_SHAPING_GBE_1
 *   SIM_CONTROL_SHAPING_GBE_2
 *   SIM_CONTROL_SHAPING_GBE_3
 *   SIM_CONTROL_SHAPING_XGBE_0
 *   SIM_CONTROL_SHAPING_XGBE_1
 *
 * @param type The type of shaping. This should be the same type of
 * shaping that is already in place on the shim. One of:
 *   SIM_CONTROL_SHAPING_MULTIPLIER
 *   SIM_CONTROL_SHAPING_PPS
 *   SIM_CONTROL_SHAPING_BPS
 *
 * @param units The magnitude of the rate. One of:
 *   SIM_CONTROL_SHAPING_UNITS_SINGLE
 *   SIM_CONTROL_SHAPING_UNITS_KILO
 *   SIM_CONTROL_SHAPING_UNITS_MEGA
 *   SIM_CONTROL_SHAPING_UNITS_GIGA
 *
 * @param rate The rate to which to change it. This must fit in
 * SIM_CONTROL_SHAPING_RATE_BITS bits or a warning is issued and
 * the shaping is not changed.
 *
 * @return 0 if no problems were detected in the arguments to sim_set_shaping
 * or 1 if problems were detected (for example, rate does not fit in 17 bits).
 */
static __inline int
sim_set_shaping(unsigned shim,
                unsigned type,
                unsigned units,
                unsigned rate)
{
  if ((rate & ~((1 << SIM_CONTROL_SHAPING_RATE_BITS) - 1)) != 0)
    return 1;

  __insn_mtspr(SPR_SIM_CONTROL, SIM_SHAPING_SPR_ARG(shim, type, units, rate));
  return 0;
}

#ifdef __tilegx__

/** Enable a set of mPIPE links.  Pass a -1 link_mask to enable all links. */
static __inline void
sim_enable_mpipe_links(unsigned mpipe, unsigned long link_mask)
{
  __insn_mtspr(SPR_SIM_CONTROL,
               (SIM_CONTROL_ENABLE_MPIPE_LINK_MAGIC_BYTE |
                (mpipe << 8) | (1 << 16) | ((uint_reg_t)link_mask << 32)));
}

/** Disable a set of mPIPE links.  Pass a -1 link_mask to disable all links. */
static __inline void
sim_disable_mpipe_links(unsigned mpipe, unsigned long link_mask)
{
  __insn_mtspr(SPR_SIM_CONTROL,
               (SIM_CONTROL_ENABLE_MPIPE_LINK_MAGIC_BYTE |
                (mpipe << 8) | (0 << 16) | ((uint_reg_t)link_mask << 32)));
}

#endif /* __tilegx__ */


/*
 * An API for changing "functional" mode.
 */

#ifndef __DOXYGEN__

#define sim_enable_functional() \
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_ENABLE_FUNCTIONAL)

#define sim_disable_functional() \
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_DISABLE_FUNCTIONAL)

#endif /* __DOXYGEN__ */


/*
 * Profiler support.
 */

/**
 * Turn profiling on for the current task.
 *
 * Note that this has no effect if run in an environment without
 * profiling support (thus, the proper flags to the simulator must
 * be supplied).
 */
static __inline void
sim_profiler_enable(void)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PROFILER_ENABLE);
}


/** Turn profiling off for the current task. */
static __inline void
sim_profiler_disable(void)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PROFILER_DISABLE);
}


/**
 * Turn profiling on or off for the current task.
 *
 * @param enabled If true, turns on profiling. If false, turns it off.
 *
 * Note that this has no effect if run in an environment without
 * profiling support (thus, the proper flags to the simulator must
 * be supplied).
 */
static __inline void
sim_profiler_set_enabled(int enabled)
{
  int val =
    enabled ? SIM_CONTROL_PROFILER_ENABLE : SIM_CONTROL_PROFILER_DISABLE;
  __insn_mtspr(SPR_SIM_CONTROL, val);
}


/**
 * Return true if and only if profiling is currently enabled
 * for the current task.
 *
 * This returns false even if sim_profiler_enable() was called
 * if the current execution environment does not support profiling.
 */
static __inline int
sim_profiler_is_enabled(void)
{
  return ((__insn_mfspr(SPR_SIM_CONTROL) & SIM_PROFILER_ENABLED_MASK) != 0);
}


/**
 * Reset profiling counters to zero for the current task.
 *
 * Resetting can be done while profiling is enabled.  It does not affect
 * the chip-wide profiling counters.
 */
static __inline void
sim_profiler_clear(void)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PROFILER_CLEAR);
}


/**
 * Enable specified chip-level profiling counters.
 *
 * Does not affect the per-task profiling counters.
 *
 * @param mask Either this special value:
 *
 * SIM_CHIP_ALL (enables all chip-level components).
 *
 * or the bitwise OR of these values:
 *
 * SIM_CHIP_MEMCTL (enable all memory controllers)
 * SIM_CHIP_XAUI (enable all XAUI controllers)
 * SIM_CHIP_MPIPE (enable all MPIPE controllers)
 */
static __inline void
sim_profiler_chip_enable(unsigned int mask)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_PROFILER_CHIP_ENABLE_SPR_ARG(mask));
}


/**
 * Disable specified chip-level profiling counters.
 *
 * Does not affect the per-task profiling counters.
 *
 * @param mask Either this special value:
 *
 * SIM_CHIP_ALL (disables all chip-level components).
 *
 * or the bitwise OR of these values:
 *
 * SIM_CHIP_MEMCTL (disable all memory controllers)
 * SIM_CHIP_XAUI (disable all XAUI controllers)
 * SIM_CHIP_MPIPE (disable all MPIPE controllers)
 */
static __inline void
sim_profiler_chip_disable(unsigned int mask)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_PROFILER_CHIP_DISABLE_SPR_ARG(mask));
}


/**
 * Reset specified chip-level profiling counters to zero.
 *
 * Does not affect the per-task profiling counters.
 *
 * @param mask Either this special value:
 *
 * SIM_CHIP_ALL (clears all chip-level components).
 *
 * or the bitwise OR of these values:
 *
 * SIM_CHIP_MEMCTL (clear all memory controllers)
 * SIM_CHIP_XAUI (clear all XAUI controllers)
 * SIM_CHIP_MPIPE (clear all MPIPE controllers)
 */
static __inline void
sim_profiler_chip_clear(unsigned int mask)
{
  __insn_mtspr(SPR_SIM_CONTROL, SIM_PROFILER_CHIP_CLEAR_SPR_ARG(mask));
}


/*
 * Event support.
 */

#ifndef __DOXYGEN__

static __inline void
sim_event_begin(unsigned int x)
{
#if defined(__tile__) && !defined(__NO_EVENT_SPR__)
  __insn_mtspr(SPR_EVENT_BEGIN, x);
#endif
}

static __inline void
sim_event_end(unsigned int x)
{
#if defined(__tile__) && !defined(__NO_EVENT_SPR__)
  __insn_mtspr(SPR_EVENT_END, x);
#endif
}

#endif /* !__DOXYGEN__ */

#endif /* !__ASSEMBLER__ */

#endif /* !__ARCH_SIM_H__ */

/** @} */