gonzui


Format: Advanced Search

tkernel_2/kernel/sysmgr/src/sysmgr.hbare sourcepermlink (0.03 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by T-Engine Forum at 2011/09/08.
   11:  *    Modified by T-Engine Forum at 2013/03/06.
   12:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   13:  *
   14:  *----------------------------------------------------------------------
   15:  */
   16: 
   17: /*
   18:  *      sysmgr.h (T-Kernel/SM)
   19:  *      T-Kernel/System Manager
   20:  */
   21: 
   22: #ifndef _SYSMGR_
   23: #define _SYSMGR_
   24: 
   25: #include <basic.h>
   26: #include <tk/tkernel.h>
   27: #include <libstr.h>
   28: #include <sys/queue.h>
   29: #include <tk/util.h>
   30: #include <sys/debug.h>
   31: 
   32: 
   33: #ifdef DEBUG
   34: #define ERCD_PRINT(exp) if ( ercd < E_OK ) { DEBUG_PRINT(exp); }
   35: #else
   36: #define ERCD_PRINT(exp)
   37: #endif
   38: 
   39: /* ------------------------------------------------------------------------ */
   40: /*
   41:  *      Device management function
   42:  */
   43: 
   44: /*
   45:  * Lock for device management exclusive control
   46:  */
   47: IMPORT FastMLock        DevMgrLock;
   48: #define LockDM()        MLock(&DevMgrLock, 0)
   49: #define UnlockDM()      MUnlock(&DevMgrLock, 0)
   50: 
   51: /*
   52:  * Lock for device registration exclusive control
   53:  */
   54: #define LockREG()       MLock(&DevMgrLock, 1)
   55: #define UnlockREG()     MUnlock(&DevMgrLock, 1)
   56: 
   57: /*
   58:  * Lock for device abort processing exclusive control
   59:  */
   60: #define LockDAbort()    MLock(&DevMgrLock, 2)
   61: #define UnlockDAbort()  MUnlock(&DevMgrLock, 2)
   62: 
   63: /*
   64:  * Device management synchronous control
   65:  *      Enter using semaphore, but free wait by 'tk_dis_wai()'
   66:  *      because it frees wait by specifying the specific task.
   67:  *      Do not execute 'tk_sig_sem().'
   68:  */
   69: IMPORT ID       DevMgrSync;
   70: #define SyncWaitDM()    {                  \
   71:         tk_wai_sem(DevMgrSync, 1, TMO_FEVR);   \
   72:         tk_ena_wai(TSK_SELF);                  \
   73: }
   74: #define SyncSignalDM(tskid)     tk_dis_wai(tskid, TTW_SEM)
   75: 
   76: /*
   77:  * Task Queue for Synchronization
   78:  */
   79: typedef struct {
   80:         QUEUE  q;
   81:         ID     tskid;                      /* wait task ID */
   82: } WaitQ;
   83: 
   84: /*
   85:  * Device registration information
   86:  */
   87: typedef struct DeviceControlBlock {
   88:         QUEUE  q;
   89:         UB     devnm[L_DEVNM+1];   /* Device name */
   90:         T_DDEV ddev;                   /* Registration information */
   91:         QUEUE  openq;                   /* Open device management queue */
   92:         QUEUE  syncq;                   /* Task Queue for Synchronization */
   93: } DevCB;
   94: 
   95: IMPORT  DevCB            *DevCBtbl;        /* Device registration info. table */
   96: IMPORT  QUEUE            UsedDevCB;        /* In-use queue */
   97: 
   98: #define DID(devcb)              ( ((devcb) - DevCBtbl + 1) << 8 )
   99: #define DEVID(devcb, unitno)    ( DID(devcb) + (unitno) )
  100: #define DEVCB(devid)            ( DevCBtbl + (((devid) >> 8) - 1) )
  101: #define UNITNO(devid)           ( (devid) & 0xff )
  102: 
  103: /*
  104:  * Device Request Packet
  105:  */
  106: typedef union devreq {
  107:         struct c_devreq {              /* DEVREQ common part */
  108:                 union devreq *next;   /* list of request packets(NULL:End) */
  109:                 void  *exinf;          /* extended information */
  110:                 ID    devid;             /* target device ID */
  111:                 INT   cmd:4;            /* request command */
  112:                 BOOL  abort:1; /* TRUE if abort request is given */
  113:                 BOOL  nolock:1;        /* TRUE if lock-space is unnecessary */
  114:                 INT   rsv:26;           /* reserved (always 0) */
  115:                 T_TSKSPC tskspc;      /* task space of the requesting task */
  116:                 /* subsequenst fields following `start' are shifted */
  117:         } c;
  118:         T_DEVREQ       s;            /* standard */
  119:         T_DEVREQ_D     l;          /* 64 bits devices (TDA_DEV_D) */
  120: } DEVREQ;
  121: 
  122: /*
  123:  * Open management information
  124:  */
  125: typedef struct OpenControlBlock {
  126:         QUEUE          q;
  127:         QUEUE          resq;           /* Queue from resource management */
  128:         ID             resid;             /* Section resource ID */
  129:         DevCB          *devcb;         /* Target device */
  130:         INT            unitno;           /* Subunit Number(0:Physical device) */
  131:         UINT           omode;           /* Open mode */
  132:         QUEUE          requestq;       /* Request management queue */
  133:         UH             syncreq;   /* Number of pending sync requests */
  134:         UH             waitone;   /* Number of individual request waits */
  135:         DEVREQ         *waireqlst;    /* List of multiple request waits */
  136:         INT            nwaireq;  /* Number of multiple request waits */
  137:         ID             abort_tskid;       /* Abort completion wait task ID */
  138:         INT            abort_cnt;        /* Number of abort completion waits */
  139: } OpnCB;
  140: 
  141: #define RESQ_OPNCB(rq)          ( (OpnCB*)((B*)(rq) - offsetof(OpnCB, resq)) )
  142: 
  143: /*
  144:  * Request management information
  145:  */
  146: typedef struct RequestControlBlock {
  147:         QUEUE          q;
  148:         OpnCB          *opncb;         /* Open device */
  149:         ID             tskid;             /* Processing task */
  150:         DEVREQ         req;           /* Request packet */
  151: } ReqCB;
  152: 
  153: /*
  154:  * Resource management information
  155:  */
  156: typedef struct ResourceControlBlock {
  157:         QUEUE          openq;          /* Open device management queue */
  158:         INT            dissus;           /* Suspend disable request count */
  159: } ResCB;
  160: 
  161: /*
  162:  * Request function types
  163:  */
  164: typedef ER  (*OPNFN)( ID devid, UINT omode, void *exinf );
  165: typedef ER  (*ABTFN)( ID tskid, DEVREQ *devreq, INT nreq, void *exinf );
  166: typedef INT (*WAIFN)( DEVREQ *devreq, INT nreq, TMO tmout, void *exinf );
  167: typedef INT (*WAIFN_U)( DEVREQ *devreq, INT nreq, TMO_U tmout, void *exinf );
  168: typedef INT (*EVTFN)( INT evttyp, void *evtinf, void *exinf );
  169: typedef ER  (*CLSFN)( ID devid, UINT option, void *exinf );
  170: typedef ER  (*EXCFN)( DEVREQ *devreq, TMO tmout, void *exinf );
  171: typedef ER  (*EXCFN_U)( DEVREQ *devreq, TMO_U tmout, void *exinf );
  172: 
  173: /*
  174:  * conversion between milliseconds and microseconds
  175:  */
  176: #define to_msec(usec)   ( ((usec) + (D)999) / 1000 )      /* round up */
  177: #define to_usec(msec)   ( (msec) * (D)1000 )
  178: 
  179: #define to_usec_tmo(ms) ( (TMO_U)(( (ms) > 0 )? to_usec(ms): (ms)) )
  180: #define to_msec_tmo(us) ( (TMO)  (( (us) > 0 )? to_msec(us): (us)) )
  181: 
  182: /*
  183:  * Device driver calls
  184:  */
  185: #define dV_     devcb->ddev
  186: #if TA_GP
  187: IMPORT INT _CallDeviceDriver( INT p1, INT p2, INT p3, INT p4, FP dv, void *gp );
  188: IMPORT INT _CallDeviceDriver_U2( INT p1, D p2, INT p3, INT p4, FP dv, void *gp );
  189: IMPORT INT _CallDeviceDriver_U3( INT p1, INT p2, D p3, INT p4, FP dv, void *gp );
  190: #define _DrvFn(p1, p2, p3, p4, drv, gp )        _CallDeviceDriver\
  191:                 ((INT)(p1), (INT)(p2), (INT)(p3), (INT)(p4), (FP)(dv), (gp))
  192: #define _DrvFnU2(p1, p2, p3, p4, drv, gp )      _CallDeviceDriver_U2\
  193:                 ((INT)(p1), (D)(p2), (INT)(p3), (INT)(p4), (FP)(dv), (gp))
  194: #define _DrvFnU3(p1, p2, p3, p4, drv, gp )      _CallDeviceDriver_U3\
  195:                 ((INT)(p1), (INT)(p2), (D)(p3), (INT)(p4), (FP)(dv), (gp))
  196: 
  197: Inline ER call_openfn( DevCB *devcb, ID devid, UINT omode )
  198: {
  199:         /* openfn == NULL is permitted for compatibility */
  200:         return (dV_.openfn == NULL) ? E_OK :
  201:                 _DrvFn(devid, omode, dV_.exinf, 0, (FP)dV_.openfn, dV_.gp);
  202: }
  203: Inline ER call_closefn( DevCB *devcb, ID devid, INT option )
  204: {
  205:         /* closefn == NULL is permitted for compatibility */
  206:         return (dV_.closefn == NULL) ? E_OK :
  207:                 _DrvFn(devid, option, dV_.exinf, 0, (FP)dV_.closefn, dV_.gp);
  208: }
  209: Inline ER call_execfn( DevCB *devcb, DEVREQ *req, TMO_U tmo )
  210: {
  211:         return ( (dV_.drvatr & TDA_TMO_U) != 0 ) ?
  212:                 _DrvFnU2(req, tmo, dV_.exinf, 0, (FP)dV_.execfn, dV_.gp) :
  213:                 _DrvFn(req, to_msec_tmo(tmo), dV_.exinf, 0, (FP)dV_.execfn, dV_.gp);
  214: }
  215: Inline ER call_waitfn( DevCB *devcb, DEVREQ *req, INT nreq, TMO_U tmo )
  216: {
  217:         return ( (dV_.drvatr & TDA_TMO_U) != 0 ) ?
  218:                 _DrvFnU3(req, nreq, tmo, dV_.exinf, (FP)dV_.waitfn, dV_.gp) :
  219:                 _DrvFn(req, nreq, to_msec_tmo(tmo), dV_.exinf, (FP)dV_.waitfn, dV_.gp);
  220: }
  221: Inline ER call_eventfn( DevCB *devcb, INT evttyp, VP evtinf )
  222: {
  223:         return _DrvFn(evttyp, evtinf, dV_.exinf, 0, (FP)dV_.eventfn, dV_.gp);
  224: }
  225: Inline ER call_abortfn( DevCB *devcb, ID tskid, DEVREQ *req, INT nreq )
  226: {
  227:         return _DrvFn(tskid, req, nreq, dV_.exinf, (FP)dV_.abortfn, dV_.gp);
  228: }
  229: #else   /* TA_GP */
  230: Inline ER call_openfn( DevCB *devcb, ID devid, UINT omode )
  231: {
  232:         /* openfn == NULL is permitted for compatibility */
  233:         return (dV_.openfn == NULL) ? E_OK :
  234:                 (*((OPNFN)dV_.openfn))(devid, omode, dV_.exinf);
  235: }
  236: Inline ER call_closefn( DevCB *devcb, ID devid, INT option )
  237: {
  238:         /* closefn == NULL is permitted for compatibility */
  239:         return (dV_.closefn == NULL) ? E_OK :
  240:                 (*((CLSFN)dV_.closefn))(devid, option, dV_.exinf);
  241: }
  242: Inline ER call_execfn( DevCB *devcb, DEVREQ *req, TMO_U tmo )
  243: {
  244:         return ( (dV_.drvatr & TDA_TMO_U) != 0 ) ?
  245:                 (*((EXCFN_U)dV_.execfn))(req, tmo, dV_.exinf) :
  246:                 (*((EXCFN)dV_.execfn))(req, to_msec_tmo(tmo), dV_.exinf);
  247: }
  248: Inline ER call_waitfn( DevCB *devcb, DEVREQ *req, INT nreq, TMO_U tmo )
  249: {
  250:         return ( (dV_.drvatr & TDA_TMO_U) != 0 ) ?
  251:                 (*((WAIFN_U)dV_.waitfn))(req, nreq, tmo, dV_.exinf) :
  252:                 (*((WAIFN)dV_.waitfn))(req, nreq, to_msec_tmo(tmo), dV_.exinf);
  253: }
  254: Inline ER call_eventfn( DevCB *devcb, INT evttyp, VP evtinf )
  255: {
  256:         return (*((EVTFN)dV_.eventfn))(evttyp, evtinf, dV_.exinf);
  257: }
  258: Inline ER call_abortfn( DevCB *devcb, ID tskid, DEVREQ *req, INT nreq )
  259: {
  260:         return (*((ABTFN)dV_.abortfn))(tskid, req, nreq, dV_.exinf);
  261: }
  262: #endif  /* TA_GP */
  263: 
  264: /* ------------------------------------------------------------------------ */
  265: 
  266: /*
  267:  *  Cache operations
  268:  */
  269: IMPORT void FlushCache( CONST void *laddr, INT len );
  270: IMPORT void FlushCacheM( CONST void *laddr, INT len, UINT mode );
  271: IMPORT ER ControlCacheM( void *laddr, INT len, UINT mode );
  272: IMPORT INT GetCacheLineSize( void );
  273: 
  274: #define IMPORT_DEFINE   1
  275: #if IMPORT_DEFINE
  276: /* smmain.c */
  277: IMPORT void* GetResBlk( ID ssid, ID tskid );
  278: IMPORT ER SystemManager( INT ac, UB *av[] );
  279: /* system.c */
  280: IMPORT INT _tk_get_cfn( CONST UB *name, INT *val, INT max );
  281: IMPORT INT _tk_get_cfs( CONST UB *name, UB *buf, INT max );
  282: IMPORT ER initialize_sysmgr( void );
  283: IMPORT ER finish_sysmgr( void );
  284: /* imalloc.c */
  285: IMPORT void* IAmalloc( size_t size, UINT attr );
  286: IMPORT void* IAcalloc( size_t nmemb, size_t size, UINT attr );
  287: IMPORT void  IAfree( void *ptr, UINT attr );
  288: IMPORT void* Imalloc( size_t size );
  289: IMPORT void* Icalloc( size_t nmemb, size_t size );
  290: IMPORT void  Ifree( void *ptr );
  291: IMPORT ER init_Imalloc( void );
  292: /* syslog.c */
  293: IMPORT  W        logtask_alive ;
  294: IMPORT ER __syslog_send( const char *string, int len );
  295: IMPORT ER initialize_syslog( void );
  296: IMPORT ER finish_syslog( void );
  297: /* device.c */
  298: IMPORT  FastMLock        DevMgrLock;
  299: IMPORT  ID               DevMgrSync;
  300: IMPORT  DevCB            *DevCBtbl;
  301: IMPORT  QUEUE            UsedDevCB;
  302: IMPORT DevCB* searchDevCB( CONST UB *devnm );
  303: IMPORT INT phydevnm( UB *pdevnm, CONST UB *ldevnm );
  304: IMPORT ER initialize_devmgr( void );
  305: IMPORT ER finish_devmgr( void );
  306: /* deviceio.c */
  307: IMPORT ER check_devdesc( ID dd, UINT mode, OpnCB **p_opncb );
  308: IMPORT ID _tk_opn_dev( CONST UB *devnm, UINT omode );
  309: IMPORT ER _tk_cls_dev( ID dd, UINT option );
  310: IMPORT ID _tk_rea_dev( ID dd, W start, void *buf, W size, TMO tmout );
  311: IMPORT ID _tk_rea_dev_du( ID dd, D start, void *buf, W size, TMO_U tmout );
  312: IMPORT ER _tk_srea_dev( ID dd, W start, void *buf, W size, W *asize );
  313: IMPORT ER _tk_srea_dev_d( ID dd, D start, void *buf, W size, W *asize );
  314: IMPORT ID _tk_wri_dev( ID dd, W start, CONST void *buf, W size, TMO tmout );
  315: IMPORT ID _tk_wri_dev_du( ID dd, D start, CONST void *buf, W size, TMO_U tmout );
  316: IMPORT ER _tk_swri_dev( ID dd, W start, CONST void *buf, W size, W *asize );
  317: IMPORT ER _tk_swri_dev_d( ID dd, D start, CONST void *buf, W size, W *asize );
  318: IMPORT ID _tk_wai_dev( ID dd, ID reqid, W *asize, ER *ioer, TMO tmout );
  319: IMPORT ID _tk_wai_dev_u( ID dd, ID reqid, W *asize, ER *ioer, TMO_U tmout );
  320: IMPORT INT _tk_sus_dev( UINT mode );
  321: IMPORT void devmgr_break( ID tskid );
  322: IMPORT void devmgr_startup( ID resid, INT info );
  323: IMPORT void devmgr_cleanup( ID resid, INT info );
  324: IMPORT ER initDevIO( void );
  325: IMPORT ER finishDevIO( void );
  326: #endif
  327: /* chkplv.c */
  328: IMPORT ER ChkCallPLevel( void );
  329: /* power.c */
  330: IMPORT void low_pow( void );
  331: IMPORT void off_pow( void );
  332: /* memmgr.c */
  333: IMPORT UW smPageCount( UW byte );
  334: IMPORT void* GetSysMemBlk( INT nblk, UINT attr );
  335: IMPORT ER RelSysMemBlk( CONST void *addr );
  336: IMPORT ER RefSysMemInfo( T_RSMB *pk_rsmb );
  337: IMPORT ER init_memmgr( void );
  338: IMPORT ER start_memmgr( void );
  339: /* segmgr.c */
  340: IMPORT ER initialize_segmgr( void );
  341: 
  342: #endif /* _SYSMGR_ */