/*
 * Basic test of FIQ intercpu interrupts
 */

#include "sc/arm.h"
#include <types.h>
#include <sc/sc.h>
#include <ioslibc.h>
#include <sc/dv_boot.h>
#include "dv_cpu_local.h"

extern u32 fiqHandler;

#define FIQ_STACK_SIZE 64
u32 cpu0FIQStack[FIQ_STACK_SIZE>>2] CACHE_ALIGNED = {0xff};
u32 cpu1FIQStack[FIQ_STACK_SIZE>>2] CACHE_ALIGNED = {0xff};

#define WAITCOUNT 10000

volatile u32 cpu0flag CACHE_ALIGNED = 0xff;
volatile u32 cpu0fiq = 0xff;

volatile u32 cpu1flag CACHE_ALIGNED = 0xff;
volatile u32 cpu1fiq = 0xff;

int 
cpu0main(void)
{
    volatile int i;
    u32 val;

    __skDisableInt();
    /* Clear WERR if set and make sure IRQs are masked */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_WERR|SYS_SMP_SET_IMASK);

    cpu0flag = 0;
    cpu1flag = 0;
    cpu0fiq = 0;
    cpu1fiq = 0;
    DVSetFIQStack((u32) cpu0FIQStack + sizeof(cpu0FIQStack));
    (void) DVSetExcHandler(ARM_EXC_FIQ, (u32) &fiqHandler);

    DVStartCpu1();

    printf("cpu0: wait for cpu1 to come up\n");

    for (i = 0; cpu1flag == 0 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-1);
        }
    }

    printf("cpu0: cpu1 is awake!\n");
    val = IO_READ(SYS_SMP_REG);
    printf("cpu0: SYS_SMP_REG 0x%x\n", val);
    if (val & SYS_SMP_SMP_IN) {
        /* Clear own SMP Interrupt */
        IO_WRITE(SYS_SMP_REG, SYS_SMP_SMP_IN);
        val = IO_READ(SYS_SMP_REG);
        if (val & SYS_SMP_SMP_IN) {
            DVExit(-2);
        }
    }

    /* send FIQ interrupt to cpu1 */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SMP_OUT);
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_OUT) == 0) {
        DVExit(-3);
    }
    
    cpu1flag = 2;
    printf("cpu0: sent FIQ to cpu1 ...\n");

    for (i = 0; cpu1flag == 2 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-4);
        }
    }

    cpu0flag = 1;
    for (i = 0; cpu0flag != 2 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-5);
        }
    }
    
    /* interrupt bit should now be set */
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_IN) == 0) {
        DVExit(-6);
    }

    /* now unmask interrupts and wait for it to be delivered */
    __skEnableInt();

    cpu0flag = 3;
    for (i = 0; cpu0fiq == 0 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-7);
        }
    }
    printf("cpu0 actually got the FIQ\n");

    return 0;
}

int 
cpu1main(void)
{
    volatile int i;
    u32 val;

    DVSetFIQStack((u32) cpu1FIQStack + sizeof(cpu1FIQStack));

    //printf("cpu1 is ready\n");
    cpu1flag = 1;

    for (i = 0; cpu1flag == 1 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-21);
        }
    }

    /* cpu0 should have posted an FIQ */
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_IN) == 0) {
        DVExit(-22);
    }
    cpu1flag = 3;
    //printf("cpu1 sees the SMP_IN bit\n");

    /* now unmask FIQ and see if it gets delivered */
    __skEnableInt();

    for (i = 0; cpu1fiq == 0 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-23);
        }
    }
    //printf("cpu1 got the FIQ interrupt\n");

    /* now cpu1 pokes cpu0 */
    for (i = 0; cpu0flag != 1 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-24);
        }
    }

    /* check that bit is clear to start */
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_OUT) != 0) {
        DVExit(-25);
    }

    /* send FIQ interrupt to cpu0 */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SMP_OUT);
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_OUT) == 0) {
        DVExit(-26);
    }

    /* wait for cpu0 to see it */
    cpu0flag = 2;
    for (i = 0; cpu0flag != 3 ; i++) {
        if (i > WAITCOUNT) {
            DVExit(-27);
        }
    }

    /* wait for cpu0 to exit */
    while (1) ;

    return 0;
}

/*
 * FIQ interrupt handler c code
 */
void
fiq_chandler(void)
{
    u32 val;

    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_IN) == 0) {
        DVExit(-31);
    }

    /* Clear the interrupt bit */
    IO_WRITE(SYS_SMP_REG, SYS_SMP_SMP_IN);
    val = IO_READ(SYS_SMP_REG);
    if ((val & SYS_SMP_SMP_IN) != 0) {
        DVExit(-32);
    }

    if ((val & SYS_SMP_CPU_ID) != 0)
        cpu1fiq = 1;
    else
        cpu0fiq = 1;

    return;
}

void
irq_chandler(void)
{
    return;
}
