usrSmObj.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:9k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* usrSmObj.c - shared memory object initialization */
  2. /* Copyright 1992-2002 Wind River Systems, Inc. */
  3. /*
  4. modification history
  5. --------------------
  6. 01e,13feb02,mas  fixed staged delay/check for slave node startup (SPR 73189)
  7. 01d,02oct01,mas  added staged delay/check for slave node startup (SPR 62128)
  8. 01c,13sep01,jws  fix smObjSpinTries initialization (SPR68418)
  9. 01b,20jan99,scb  modified to use "sm=" before SM_ANCHOR_ADRS (23035)
  10. 01a,16feb97,ms   based on old 01i version.
  11. */
  12. /*
  13. DESCRIPTION
  14. This file is used to configure and initialize the VxWorks shared memory
  15. object support.  This file is included by usrConfig.c.
  16. SEE ALSO: usrExtra.c
  17. NOMANUAL
  18. */
  19. /******************************************************************************
  20. *
  21. * usrSmObjInit - initialize shared memory objects
  22. *
  23. * This routine initializes the shared memory objects facility.  It sets up
  24. * the shared memory objects facility if called from processor 0.
  25. * Then it initializes a shared memory descriptor and calls smObjAttach()
  26. * to attach this CPU to the shared memory object facility.
  27. *
  28. * When the shared memory pool resides on the local CPU dual ported memory,
  29. * SM_OBJ_MEM_ADRS must be set to NONE in configAll.h and the shared memory
  30. * objects pool is allocated from the VxWorks system pool.
  31. *
  32. * RETURNS: OK, or ERROR if unsuccessful.
  33. *
  34. * INTERNAL
  35. * The delayed start for slave processors used below is required.  The anchor
  36. * may not yet be mapped to the bus.  So, probing of shared memory locations is
  37. * used to overcome Bus Errors which occur on many boards if the slave accesses
  38. * shared memory (SM) before the master has finished initializing it.  The code
  39. * here simply delays access to the SM region until the SM master has finished
  40. * initializing it.
  41. *
  42. * The method used is to repetitively probe key locations in the SM region
  43. * after delay periods until valid values are detected or a time-out occurs.
  44. * The initial delay period is set based on processor number.  (The master
  45. * processor does not delay.)  If the first probe of a location fails, an
  46. * exponential increase in delay period is used to reduce bus contention on
  47. * subsequent probes.
  48. *
  49. * This method is no better than receiving raw BERRs and does reduce bus
  50. * contention and the number of BERRs.
  51. *
  52. * NOMANUAL
  53. */
  54. STATUS usrSmObjInit 
  55.     (
  56.     char * bootString /* boot parameter string */
  57.     )
  58.     {
  59.     char *           smAnchor; /* anchor address */
  60.     char *           smObjFreeAdrs; /* free pool address */
  61.     int              smObjMemSize; /* pool size */
  62.     BOOT_PARAMS      params; /* boot paramters */
  63.     BOOL             allocatedPool; /* TRUE if pool is maloced */
  64.     SM_OBJ_PARAMS    smObjParams; /* smObj setup parameters */
  65.     char       bb; /* bit bucket for vxMemProbe */
  66.     int              tics; /* SM probe delay period */
  67.     UINT             temp; /* temp for smUtilMemProbe() */
  68.     UINT             maxWait   = SM_MAX_WAIT;
  69.     char *           cp;
  70.     SM_OBJ_MEM_HDR * pSmObjHdr = NULL; /* ptr to SMO header */
  71.     allocatedPool = FALSE;
  72.     /* Check for hardware test and set availability */
  73.     if (SM_TAS_TYPE != SM_TAS_HARD)
  74.         {
  75.         printf ("nError initializing shared memory objects, ");
  76. printf ("hardware test-and-set required.n");
  77.         return (ERROR);
  78.         }
  79.     if (smObjLibInit () == ERROR) /* initialize shared memory objects */
  80. {
  81.         printf("nERROR smObjLibInit : shared memory objects already initialized.n");
  82. return (ERROR);
  83. }
  84.     if (bootString == NULL)
  85.         bootString = BOOT_LINE_ADRS;
  86.     /* interpret boot command */
  87.     if (usrBootLineCrack (bootString, &params) != OK)
  88.         return (ERROR);
  89.     /* set processor number: may establish vme bus access, etc. */
  90.     if (_procNumWasSet != TRUE)
  91. {
  92.      sysProcNumSet (params.procNum);
  93. _procNumWasSet = TRUE;
  94. }
  95.     /* if we booted via the sm device use the same anchor address for smObj */
  96.     if (strncmp (params.bootDev, "sm=", 3) == 0)
  97.         {
  98.         if (bootBpAnchorExtract (params.bootDev, &smAnchor) < 0)
  99.             {
  100.     printf ("nError initializing shared memory objects, invalid ");
  101.             printf ("anchor address specified: "%s"n", params.bootDev);
  102.             return (ERROR);
  103.             }
  104.         }
  105.     else
  106.         {
  107. smAnchor = (char *) SM_ANCHOR_ADRS;         /* default anchor */
  108.         }
  109.     /* If we are shared memory master CPU, set up shared memory object (SMO) */
  110.     if (params.procNum == SM_MASTER)
  111.         {
  112.         smObjFreeAdrs = (char *) SM_OBJ_MEM_ADRS;
  113. smObjMemSize  = SM_OBJ_MEM_SIZE;
  114.         /* allocate the shared memory object pool if needed */
  115.         if (smObjFreeAdrs == (char *) NONE)
  116.             {
  117.             /* check cache configuration - must be read and write coherent */
  118.     if (!CACHE_DMA_IS_WRITE_COHERENT() || !CACHE_DMA_IS_READ_COHERENT())
  119.                 {
  120. printf ("usrSmObjInit - cache coherent buffer not available. Giving up.  n");
  121. return (ERROR);
  122.                 }
  123.             allocatedPool = TRUE;
  124.             smObjFreeAdrs = (char *) cacheDmaMalloc (SM_OBJ_MEM_SIZE);
  125.             if (smObjFreeAdrs == NULL)
  126. {
  127. printf ("usrSmObjInit - cannot allocate shared memory pool. Giving up.n");
  128.                 return (ERROR);
  129. }
  130.             }
  131.         if (!allocatedPool)
  132.             {
  133.             /* free memory pool must be behind the anchor */
  134.             smObjFreeAdrs += sizeof (SM_ANCHOR);
  135.     /* adjust pool size */
  136.     smObjMemSize = SM_OBJ_MEM_SIZE - sizeof (SM_ANCHOR);
  137.             }
  138. /* probe anchor address */
  139. if (vxMemProbe (smAnchor, VX_READ, sizeof (char), &bb) != OK)
  140.     {
  141.     printf ("usrSmObjInit - anchor address %#x unreachable. Giving up.n", (unsigned int) smAnchor);
  142.     return (ERROR);
  143.     }
  144. /* probe beginning of shared memory */
  145. if (vxMemProbe (smObjFreeAdrs, VX_WRITE, sizeof (char), &bb) != OK)
  146.     {
  147.     printf ("usrSmObjInit - shared memory address %#x unreachable. Giving up.n", (unsigned int) smObjFreeAdrs);
  148.     return (ERROR);
  149.     }
  150.         /* set up shared memory objects */
  151.         
  152.         smObjSpinTries = SM_OBJ_MAX_TRIES; /* must do before smObjSetup() */
  153.         smObjParams.allocatedPool = allocatedPool;
  154.         smObjParams.pAnchor       = (SM_ANCHOR *) smAnchor;
  155.         smObjParams.smObjFreeAdrs = (char *) smObjFreeAdrs;
  156.         smObjParams.smObjMemSize  = smObjMemSize;
  157.         smObjParams.maxCpus       = DEFAULT_CPUS_MAX;
  158.         smObjParams.maxTasks      = SM_OBJ_MAX_TASK;
  159.         smObjParams.maxSems       = SM_OBJ_MAX_SEM;
  160.         smObjParams.maxMsgQueues  = SM_OBJ_MAX_MSG_Q;
  161.         smObjParams.maxMemParts   = SM_OBJ_MAX_MEM_PART;
  162.         smObjParams.maxNames      = SM_OBJ_MAX_NAME;
  163.         if (smObjSetup (&smObjParams) != OK)
  164.             {
  165.             if (errno == S_smObjLib_SHARED_MEM_TOO_SMALL)
  166.                printf("nERROR smObjSetup : shared memory pool too small.n");
  167.             if (allocatedPool)
  168.                 free (smObjFreeAdrs); /* cleanup */
  169.             return (ERROR);
  170.             }
  171.         }
  172.     /*
  173.      * Else, we are not the master CPU, so wait until the anchor and SMO
  174.      * header regions are accessible (the master CPU has initialized them)
  175.      * before continuing.
  176.      */
  177.     else
  178.         {
  179.         /* first wait for valid anchor region */
  180.         tics = params.procNum;
  181.         for (tics <<= 1; tics < maxWait; tics <<= 1)
  182.             {
  183.             smUtilDelay (NULL, tics);
  184.             if ((smUtilMemProbe (smAnchor, VX_READ, sizeof (UINT),
  185.                                  (char *)&temp) == OK) &&
  186.                 (ntohl (temp) == SM_READY))
  187.                 {
  188.                 break;
  189.                 }
  190.             }
  191.         if (tics >= maxWait)
  192.             {
  193.             printf ("usrSmObjInit: anchor @ %p unreachable. Giving up.n",
  194.                     smAnchor);
  195.             return (ERROR);
  196.             }
  197.         /* Finally, wait for master to init SM Object facility */
  198.         tics = params.procNum;
  199.         cp = (char *)(&((SM_ANCHOR *)smAnchor)->smObjHeader);
  200.         for (tics <<= 1; tics < maxWait; tics <<= 1)
  201.             {
  202.             if ((smUtilMemProbe (cp, VX_READ, sizeof (UINT), (char *)&temp)
  203.                               == OK) &&
  204.                 (temp != (UINT)~0)   &&
  205.                 (temp != 0))
  206.                 {
  207.                 break;
  208.                 }
  209.             smUtilDelay (NULL, tics);
  210.             }
  211.         pSmObjHdr = SM_OFFSET_TO_LOCAL (ntohl (temp), (int)smAnchor,
  212.                                         SM_OBJ_MEM_HDR *);
  213.         cp = (char *)(&pSmObjHdr->initDone);
  214.         for ( ; tics < maxWait; tics <<= 1)
  215.             {
  216.             if ((smUtilMemProbe (cp, VX_READ, sizeof (UINT), (char *)&temp)
  217.                               == OK) &&
  218.                 (ntohl (temp) == TRUE))
  219.                 {
  220.                 break;
  221.                 }
  222.             smUtilDelay (NULL, tics);
  223.             }
  224.         if (tics >= maxWait)
  225.             {
  226.             printf ("usrSmObjInit: Error: time out awaiting SM Object initn");
  227.             return (ERROR);
  228.             }
  229.         }
  230.     /*
  231.      * initialize shared memory descriptor
  232.      *
  233.      *  Note that SM_OBJ_MAX_TRIES is passed to smObjInit(),
  234.      *  and is used to set the global int smObjSpinTries.
  235.      *  This has already been done above to fix SPR68418.
  236.      */
  237.     smObjInit (&smObjDesc, (SM_ANCHOR *) smAnchor, sysClkRateGet (),
  238.                SM_OBJ_MAX_TRIES, SM_INT_TYPE, SM_INT_ARG1,
  239.        SM_INT_ARG2, SM_INT_ARG3);
  240.     /* attach to shared memory object facility */
  241.     printf ("Attaching shared memory objects at %#x... ", (int) smAnchor);
  242.     if (smObjAttach (&smObjDesc) != OK)
  243. {
  244. printf ("failed: errno = %#x.n", errno);
  245.         return (ERROR);
  246. }
  247.     printf ("donen");
  248.     return (OK);
  249.     }