sched.c 10.5 KB
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
/*====================================================================
 * 
 * Copyright 1995, Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
 * Inc.; the contents of this file may not be disclosed to third
 * parties, copied or duplicated in any form, in whole or in part,
 * without the prior written permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to
 * restrictions as set forth in subdivision (c)(1)(ii) of the Rights
 * in Technical Data and Computer Software clause at DFARS
 * 252.227-7013, and/or in similar or successor clauses in the FAR,
 * DOD or NASA FAR Supplement. Unpublished - rights reserved under the
 * Copyright Laws of the United States.
 *====================================================================*/
 
#include <ultralog.h>
#include "sched.h"


/*
 * OSScTask->flags type identifier
 */
#define OS_SC_XBUS      (OS_SC_NEEDS_RSP | OS_SC_NEEDS_RDP)
#define OS_SC_DRAM      (OS_SC_NEEDS_RSP | OS_SC_NEEDS_RDP | OS_SC_DRAM_DLIST)
#define OS_SC_DP_XBUS   (OS_SC_NEEDS_RSP)
#define OS_SC_DP_DRAM   (OS_SC_NEEDS_RSP | OS_SC_DRAM_DLIST)
#define OS_SC_SP_XBUS   (OS_SC_NEEDS_RDP)
#define OS_SC_SP_DRAM   (OS_SC_NEEDS_RDP | OS_SC_DRAM_DLIST)

/*
 * private functions
 */
static void     __scMain(void *arg);
static void     __scHandleRSP(OSSched *s);
static void     __scHandleRDP(OSSched *s);
static void     __scCheckTasks(OSSched *sc);
static void     __scExecAudio(OSSched *sc);
static void     __scExecGFX(OSSched *sc);

static void     __scAppendList(OSSched *s, OSScTask *t);
OSScTask        *__scTaskReady(OSScTask *t);
static s32      __scTaskComplete(OSSched *s,OSScTask *t);
static void	__scYield(OSSched *s);


extern u32 dbgOn;
void sendDbg(u32 dbgVal);

/***********************************************************************
 * Scheduler API
 **********************************************************************/
void osCreateScheduler(OSSched *sc, void *stack, OSPri priority,
                       u8 mode, u8 numFields)
{
    sc->curRSPTask      = 0;
    sc->curRDPTask      = 0;
    sc->clientList      = 0;
    sc->audClient       = 0;
    sc->frameCount      = 0;
    sc->audioListHead   = 0;
    sc->gfxListHead     = 0;
    sc->audioListTail   = 0;
    sc->gfxListTail     = 0;
    sc->audioMsg.type   = OS_SC_AUDIO_MSG;
    sc->retraceMsg.type = OS_SC_RETRACE_MSG;  /* sent to apps */
    sc->prenmiMsg.type  = OS_SC_PRE_NMI_MSG;
    
    osCreateMesgQueue(&sc->interruptQ, sc->intBuf, OS_SC_MAX_MESGS);
    osCreateMesgQueue(&sc->cmdQ, sc->cmdMsgBuf, OS_SC_MAX_MESGS);

    /*
     * Set up video manager, listen for Video, RSP, and RDP interrupts
     */
    osCreateViManager(OS_PRIORITY_VIMGR);    
    osViSetMode(&osViModeTable[mode]);
    osViBlack(TRUE);
    osSetEventMesg(OS_EVENT_AI, &sc->interruptQ, (OSMesg)AUDIO_MSG);
    osSetEventMesg(OS_EVENT_SP, &sc->interruptQ, (OSMesg)RSP_DONE_MSG);
    osSetEventMesg(OS_EVENT_DP, &sc->interruptQ, (OSMesg)RDP_DONE_MSG);    
    osSetEventMesg(OS_EVENT_PRENMI, &sc->interruptQ, (OSMesg)PRE_NMI_MSG);    

    osViSetEvent(&sc->interruptQ, (OSMesg)VIDEO_MSG, numFields);    

    osCreateThread(&sc->thread, 4, __scMain, (void *)sc, stack, priority);
    osStartThread(&sc->thread);
}

/*
 * Add a client to the scheduler.  Clients receive messages at retrace time
 */
void osScAddClient(OSSched *sc, OSScClient *c, OSMesgQueue *msgQ)
{
    OSIntMask mask;

    mask = osSetIntMask(OS_IM_NONE);

    c->msgQ = msgQ;
    c->next = sc->clientList;
    sc->clientList = c;

    osSetIntMask(mask);
}

void osScRemoveClient(OSSched *sc, OSScClient *c)
{
    OSScClient *client = sc->clientList; 
    OSScClient *prev   = 0;
    OSIntMask  mask;

    mask = osSetIntMask(OS_IM_NONE);
    
    while (client != 0) {
        if (client == c) {
	    if(prev)
		prev->next = c->next;
	    else
		sc->clientList = c->next;
            break;
        }
        prev   = client;
        client = client->next;
    }

    osSetIntMask(mask);
}

void osScAddAudioClient(OSSched *sc, OSScClient *c, OSMesgQueue *msgQ)
{
    OSIntMask mask;

    mask = osSetIntMask(OS_IM_NONE);

    c->msgQ = msgQ;
    c->next = sc->audClient;
    sc->audClient = c;

    osSetIntMask(mask);
}

void osScRemoveAudioClient(OSSched *sc, OSScClient *c)
{
    OSScClient *client = sc->audClient; 
    OSScClient *prev   = 0;
    OSIntMask  mask;

    mask = osSetIntMask(OS_IM_NONE);
    
    while (client != 0) {
        if (client == c) {
	    if(prev)
		prev->next = c->next;
	    else
		sc->audClient = c->next;
            break;
        }
        prev   = client;
        client = client->next;
    }

    osSetIntMask(mask);
}

OSMesgQueue *osScGetCmdQ(OSSched *sc)
{
    return &sc->cmdQ;
}

OSMesgQueue *osScGetIntQ(OSSched *sc)
{
    return &sc->interruptQ;
}

/***********************************************************************
 * Scheduler implementation
 **********************************************************************/
static void __scMain(void *arg)
{
    OSScTask    *rspTask;
    OSMesg      msg;
    OSSched *sc = (OSSched *)arg;
    OSScClient  *client;
    
    while (1) {
        
	
        osRecvMesg(&sc->interruptQ, (OSMesg *)&msg, OS_MESG_BLOCK);

	if((int)msg == RSP_DONE_MSG)
	    __scHandleRSP(sc);

	else if((int)msg == RDP_DONE_MSG)
	    __scHandleRDP(sc);

	/* Read the task command queue and schedule tasks */
	while (osRecvMesg(&sc->cmdQ, (OSMesg *)&rspTask, OS_MESG_NOBLOCK) != -1)
	    __scAppendList(sc, rspTask);

	__scCheckTasks(sc);
	if((int)msg == AUDIO_MSG)
	    for (client = sc->audClient; client != 0; client = client->next) 
		osSendMesg(client->msgQ, (OSMesg) &sc->audioMsg, OS_MESG_NOBLOCK);
	else if((int)msg == VIDEO_MSG)
	    for (client = sc->clientList; client != 0; client = client->next) 
		osSendMesg(client->msgQ, (OSMesg) &sc->retraceMsg, OS_MESG_NOBLOCK);
	else if((int)msg == PRE_NMI_MSG)
	{
	    for (client = sc->audClient; client != 0; client = client->next) 
		osSendMesg(client->msgQ, (OSMesg) &sc->prenmiMsg, OS_MESG_NOBLOCK);
	    for (client = sc->clientList;client != 0;client = client->next) 
		osSendMesg(client->msgQ, (OSMesg) &sc->prenmiMsg, OS_MESG_NOBLOCK);
        }
    }
}


/***********************************************************************
 * Check to see if you can execute a task
 ***********************************************************************/
void __scCheckTasks(OSSched *sc)
{
    if (sc->doAudio)  /* if there is an audio task pending */
    {
	if(!sc->curRSPTask) /* and the rsp is available */
	    __scExecAudio(sc);
	else
	    __scYield(sc); 
    } 
    else 
    {
	if(!sc->curRSPTask && !sc->curRDPTask)
            __scExecGFX(sc);
    }
}

/*
 * __scHandleRSP is called when an RSP task signals that it has
 * finished or yielded (at the hosts request)
 */
void __scHandleRSP(OSSched *sc)
{
    OSScTask *t;

    t = sc->curRSPTask;
    sc->curRSPTask = 0;
    
    if ((t->state & OS_SC_YIELD) && osSpTaskYielded(&t->list)) 
    {
	t->d.gd.endTicks += osGetTime() - t->d.gd.startTicks; 
	sc->curRDPTask = 0; /* mark it zero, easier for restarting test */
        t->state |= OS_SC_YIELDED;

	/* push the task back on the list */
	t->next = sc->gfxListHead;
	sc->gfxListHead = t;
	if (sc->gfxListTail == 0)
	    sc->gfxListTail = t;

    } 
    else 
    {
         t->state &= ~OS_SC_NEEDS_RSP;
        __scTaskComplete(sc, t);
    }

}

/*
 * __scHandleRDP is called when an RDP task signals that it has
 * finished
 */
void __scHandleRDP(OSSched *sc)
{
    OSScTask *t; 

    t = sc->curRDPTask;
    sc->curRDPTask = 0;
    
    if(t)
    {
	t->state &= ~OS_SC_NEEDS_RDP;
	__scTaskComplete(sc, t);
    }
    else  /* task was yielded with only gSPEndDisplayList left */
    {     /* go ahead and mark it complete */
	t = sc->gfxListHead;
	if(t)
	{
	    sc->gfxListHead = t->next;
	    if(!t->next)
		sc->gfxListTail = 0;
	    t->state = 0;
	    __scTaskComplete(sc,t);
	}
    }
}

/*
 * __scTaskComplete checks to see if the task is complete (all RCP
 * operations have been performed) and sends the done message to the
 * client if it is.
 */
s32 __scTaskComplete(OSSched *sc, OSScTask *t) 
{
    int rv;
    static int firstTime = 1;

    if ((t->state & OS_SC_RCP_MASK) == 0)  /* none of the needs bits set */
    {
	t->d.ud.endTicks += osGetTime() - t->d.ud.startTicks;
        rv = osSendMesg(t->msgQ, t->msg, OS_MESG_BLOCK);

	if (t->list.t.type == M_GFXTASK) 
	{
            if ((t->flags & OS_SC_SWAPBUFFER) && (t->flags & OS_SC_LAST_TASK))
	    {
		if(firstTime)
		{
		    osViBlack(FALSE);
		    firstTime = 0;
		}
                osViSwapBuffer(t->framebuffer);
	    }
	}
        return 1;
    }
    
    return 0;
}



/*
 * Place task on either the audio or graphics queue
 */
void __scAppendList(OSSched *sc, OSScTask *t) 
{
    long type = t->list.t.type;
    
    if (type == M_AUDTASK) 
    {
        if (sc->audioListTail)
            sc->audioListTail->next = t;
        else
            sc->audioListHead = t;
            
        sc->audioListTail = t;
        sc->doAudio++; 
    } 
    else 
    {
	
        if (sc->gfxListTail)
            sc->gfxListTail->next = t;
        else
            sc->gfxListHead = t;
        
	sc->gfxListTail = t;

    }
    
    t->next = NULL;
    t->state = t->flags & OS_SC_RCP_MASK;    
}


static void __scYield(OSSched *sc) 
{
    OSScTask *t = sc->curRSPTask;
    
    if(t)
    {
	if (t->list.t.type == M_GFXTASK) 
	{

	    if(!(t->state & OS_SC_YIELD)) /* don't yield already yielded task */
	    {
		t->state |= OS_SC_YIELD;
		osSpTaskYield();
	    }
	}
    }
}


/*
 * __scTaskReady checks to see if there is a pending video swap
 * if there is, it bails
 */
OSScTask *__scTaskReady(OSScTask *t) 
{
    void *a;
    void *b;    

    if (t) 
    {    
        if ((a=osViGetCurrentFramebuffer()) != (b=osViGetNextFramebuffer())) 
            return 0;
	else
	    return t;
    }

    return 0;
}


void __scExecAudio(OSSched *sc)
{
    OSScTask *t = sc->audioListHead; 

    sc->doAudio--;
    sc->audioListHead = t->next;
    sc->curRSPTask = t; 
    if(t == sc->audioListTail)
	sc->audioListTail = NULL;
    t->state = OS_SC_NEEDS_RSP;

    t->d.ad.startTicks = osGetTime();
    osWritebackDCacheAll();
    osSpTaskLoad(&t->list); 
    osSpTaskStartGo(&t->list); 

}

void __scExecGFX(OSSched *sc)
{	
    OSScTask *t = sc->gfxListHead;

    if(t)
    {
	if(__scTaskReady(t))
	{
	    t->state &= ~(OS_SC_YIELD | OS_SC_YIELDED);
	    t->d.gd.startTicks = osGetTime();
	    sc->curRSPTask = t;
	    sc->curRDPTask = t;
	    sc->gfxListHead = t->next;
	    if(t == sc->gfxListTail)
		sc->gfxListTail = NULL;
	    osSpTaskLoad(&t->list);
	    osSpTaskStartGo(&t->list);

	}
    }
}