/*******************************************************************************
 * 
 *
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 *
 *
 *
 *
 ******************************************************************************/


#ifndef __STRESS_H__
#define __STRESS_H__

#include "sys.h"
#include "rvldv.h"
#include "addr_map.h"
#include "iop.h"

//#define TERMINATE_ON_FAIL
#define   MODULE_CONCLUDE      (30)   /* Any value will do */
#define   PRIORITY_MOD         (-2)

#define STR_DEBUG_CRIT             1
#define STR_DEBUG_ERROR            2
#define STR_DEBUG_WARN             4
#define STR_DEBUG_INFO             8
#define STR_DEBUG_NOTIFY           16
#define STR_DEBUG_DEBUG            32
#define STR_DEBUG_TRACE            64
#define STR_DEBUG_COMP             128
#define STR_DEBUG_TIMER            256
#define STR_DEBUG_TEMP             0x200

#define STR_DEBUG_MIN              (0x03)  // CRIT | ERROR
#define STR_DEBUG_TRC              (0x43)  // CRIT | ERROR | TRACE
#define STR_DEBUG_STD              (0x0F)  // CRIT | ERROR | WARN  | INFO
#define STR_DEBUG_DBG              (0x3F)  // CRIT | ERROR | WARN  | INFO | NOTIFY | DEBUG
#define STR_DEBUG_ALL              (0xFF)  // All output (including comparisons)

#define STR_DEBUG_LVL              (STR_DEBUG_MIN)

#define strPrint(l, s, args...)                                \
    do {                                                       \
        if (l & STR_DEBUG_LVL) {                               \
            /* The ## remove the ',' when there's no args */   \
            printf(s, ## args);                                \
        }                                                      \
    } while (0)

enum __module {AES = 0x10, SHA = 0x20, FLA = 0x40, 
               TCP = 0x80, SDI = 0x100, UDP=0x200, NEW_TCP=0x400, 
               ES = 0x800,
               CPU = 0x80000000};

#define NETWORK    (TCP | UDP | NEW_TCP)

enum Stress_Msg {STR_TIMER = 5,   STR_DONE = 10,   STR_FAIL = 15,
                 AES_OP_DONE=20, AES_TEST_CONTINUE=25, AES_TEST_QUIT=30, 
                 SHA_OP_DONE=35, SHA_TEST_CONTINUE=40, SHA_TEST_QUIT=45,
                 FLA_OP_DONE=50, FLA_TEST_CONTINUE=55, FLA_TEST_QUIT=60,
                 CPU_TEST_CONTINUE = 65, CPU_TEST_QUIT = 70};

enum memArea {SRON, NAPA, DDR, RAND};
enum _stressStatus {GOOD=40, STOP=41, FAIL=42};

// XXX - need a memory region allocation module.
// For now statically allocate areas for DMA buffers, etc.

#define busGetTime()    (IO_READ(SYS_TIMER_REG))
#define ALIGNED(x)      __attribute__ ((aligned(x)))
#define RANDOM(x)       (busGetTime() % x)

/* Busy memory client use:
PI   0x00020000-0x0002FFFF    0x02020000-0x0202FFFF
DSP  0x00030000-0x0003FFFF    0x02030000-0x0203FFFF
IO   0x00040000-0x0004FFFF    0x02040000-0x0204FFFF
VI   0x00050000-0x0005FFFF    0x02050000-0x0205FFFF
CP   0x00060000-0x0006FFFF    0x02060000-0x0206FFFF
TC   0x00070000-0x0007FFFF    0x02070000-0x0207FFFF
PE   0x00080000-0x0008FFFF    0x02080000-0x0208FFFF
                        (upper range only used if large mem enabled)
*/

//MEMMAP_STRESS_BASE

#define STR_CONFIG          0x10000000
#define MEM_CLIENT_SIZE     0x00010000         // 1/16 MB (64KB); largest block size
#define MEMMAP_STRESS_BASE  (0x00020000)

#define MEM_AES_SIZE    (MEM_CLIENT_SIZE * 5) // DMA_SRC, DMA_DST, ENC_SRC, ENC_DST, DEC_DST
#define MEM_SHA_SIZE    (MEM_CLIENT_SIZE * 2) // MULT_SRC, RAND_MUL_SRC
#define MEM_FLA_SIZE    (MEM_CLIENT_SIZE * 2)
#define MEM_CPU_SIZE    (MEM_CLIENT_SIZE)
#define MEM_SDI_SIZE    (MEM_CLIENT_SIZE / 8) // 4 KB

#define MEM_AES_ADDR    (MEMMAP_STRESS_BASE+0x200)
#define MEM_SHA_ADDR    (MEM_AES_ADDR + MEM_AES_SIZE)
#define MEM_FLA_ADDR    (MEM_SHA_ADDR + MEM_SHA_SIZE)
#define MEM_CPU_ADDR    (MEM_FLA_ADDR + MEM_FLA_SIZE)
#define MEM_SDI_ADDR    (MEM_CPU_ADDR + MEM_CPU_SIZE)

#define MEM_DDR_ADDR    (0x131e0000)
#define MEM_DDR_RANGE   0x17400000

#define MEM_AES_CNT     (MEMMAP_STRESS_BASE + 0x20)
#define MEM_SHA_CNT     (MEMMAP_STRESS_BASE + 0x40)
#define MEM_FLA_CNT     (MEMMAP_STRESS_BASE + 0x60)

#define ERRSRC_MEM	1
#define ERRSRC_EXI	2
#define ERRSRC_DI 	3
#define ERRSRC_DSPDSP 	4
#define ERRSRC_DSPCPU 	5
#define ERRSRC_SI	6
#define ERRSRC_D3	7

extern u32 maxTime;
extern int D3Active;
extern int D3Test;

struct module_s {
  enum __module test;
  void (*init)(void);
  void (*stats)(void);
  void (*end)(void);
};
typedef struct module_s Module;

extern Module modAes;
extern Module modSha;
extern Module modFla;
extern Module modTcp;
extern Module modSdi;
extern Module modUdp;
extern Module modNewTcp;
extern Module modEs;

void doFail(u32 eSrc);

enum addr_mode_e
{
    COMPAT_MODE,
    DDR_ONLY_MODE,
};

typedef struct stress_config
{
    int disable_gfx;
    int enable_aip;
    int ahb_live;
} stress_config_s;


extern stress_config_s g_stress_config;

#endif // __STRESS_H__
