void testPools(size_t size, int block) { int rv; int num_buffers; int i; CMEM_AllocParams params; printf("allocating pool buffers from CMEM memory block %d...\n", block); printf(" (will end with expected failed allocation)\n"); num_buffers = 0; params.type = CMEM_POOL; params.flags = CMEM_NONCACHED; params.alignment = 0; while (num_buffers < NUMHEAPPTRS) { ptrs[num_buffers] = CMEM_alloc2(block, size, ¶ms); if (ptrs[num_buffers] == NULL) { break; } num_buffers++; } printf("...done, %d allocated\n", num_buffers); if (non_interactive_flag == FALSE) { printf("Press ENTER to continue\n"); getchar(); } printf("freeing pool blocks...\n"); for (i = 0; i < num_buffers; i++) { rv = CMEM_free(ptrs[i], ¶ms); if (rv < 0) { printf("error freeing blocks\n"); break; } ptrs[i] = NULL; } /* make sure we can still allocate num_buffers after freeing */ printf("allocating %d pool blocks...\n", num_buffers); for (i = 0; i < num_buffers; i++) { ptrs[i] = CMEM_alloc2(block, size, ¶ms); if (ptrs[i] == NULL) { printf("error re-allocating %d heap blocks\n", num_buffers); break; } } printf("...done, freeing pool blocks...\n"); for (i = 0; i < num_buffers; i++) { rv = CMEM_free(ptrs[i], ¶ms); if (rv < 0) { printf("error freeing blocks\n"); break; } } printf("...done\n"); }
void testHeap(int size, int block) { int rv; int num_buffers; int i; CMEM_AllocParams params; printf("allocating heap buffers from CMEM memory block %d...\n", block); num_buffers = 0; params.type = CMEM_HEAP; params.flags = CMEM_NONCACHED; params.alignment = 0; while (num_buffers < NUMHEAPPTRS) { heap_ptrs[num_buffers] = CMEM_alloc2(block, size, ¶ms); if (heap_ptrs[num_buffers] == NULL) { break; } num_buffers++; } printf("...done, %d allocated\n", num_buffers); printf("Press ENTER to continue (after 'cat /proc/cmem' if desired).\n"); getchar(); printf("freeing heap blocks...\n"); for (i = 0; i < num_buffers; i++) { rv = CMEM_free(heap_ptrs[i], ¶ms); if (rv < 0) { printf("error freeing blocks\n"); break; } heap_ptrs[i] = NULL; } /* make sure we can still allocate num_buffers after freeing */ printf("allocating %d heap blocks...\n", num_buffers); for (i = 0; i < num_buffers; i++) { heap_ptrs[i] = CMEM_alloc2(block, size, ¶ms); if (heap_ptrs[i] == NULL) { printf("error re-allocating %d heap blocks\n", num_buffers); break; } } printf("...done, freeing heap blocks...\n"); for (i = 0; i < num_buffers; i++) { rv = CMEM_free(heap_ptrs[i], ¶ms); if (rv < 0) { printf("error freeing blocks\n"); break; } } printf("...done\n"); }
int writereadCMA(size_t size) { int rv; unsigned int i; CMEM_AllocParams params; char *heap_ptr; printf("allocating write-read heap buffer from CMA memory block...\n"); params.type = CMEM_HEAP; params.flags = CMEM_NONCACHED; params.alignment = 0; heap_ptr = CMEM_alloc2(CMEM_CMABLOCKID, size, ¶ms); if (heap_ptr == NULL) { printf("...failed\n"); return -1; } else { printf("...done, allocated buffer at virtp %p\n", heap_ptr); } printf("Performing write-read test on CMA buffer...\n"); for (i = 0; i < size; i++) { heap_ptr[i] = i % 256; } for (i = 0; i < size; i++) { if (heap_ptr[i] != (i % 256)) { printf("write-read error: read 0x%x, should be 0x%x\n", heap_ptr[i], i % 256); if (non_interactive_flag == FALSE) { printf("Press ENTER to continue\n"); getchar(); } break; } } if (i == size) { if (non_interactive_flag == FALSE) { printf("...succeeded, press ENTER to continue\n"); getchar(); } else { printf("...succeeded\n"); } } printf("calling CMEM_cacheInv(heap_ptr, size)...\n"); CMEM_cacheInv(heap_ptr, size); printf("...done\n"); printf("freeing heap buffer...\n"); rv = CMEM_free(heap_ptr, ¶ms); if (rv < 0) { printf("error freeing buffer\n"); } printf("...done\n"); return 0; }
void testCache(int size, int block) { unsigned int *ptr1_nocache = NULL; unsigned int *ptr1_cache = NULL; unsigned int *ptr1_dma = NULL; unsigned int *ptr2 = NULL; unsigned long physp; unsigned long physp_dma; unsigned long physp_nocache; unsigned long physp_cache; int poolid; int i, j; struct timeval start_tv, end_tv; unsigned long diff; int foo, bar; CMEM_AllocParams params; printf("Allocating first noncached buffer.\n"); /* First allocate a buffer from the pool that best fits */ ptr1_nocache = CMEM_alloc(size, NULL); if (ptr1_nocache == NULL) { fprintf(stderr, "Failed to allocate buffer of size %d\n", size); goto cleanup; } printf("Allocated buffer of size %d at address %#x.\n", size, (unsigned int) ptr1_nocache); /* Find out and print the physical address of this buffer */ physp_nocache = CMEM_getPhys(ptr1_nocache); if (physp_nocache == 0) { fprintf(stderr, "Failed to get physical address of buffer %#x\n", (unsigned int) ptr1_nocache); goto cleanup; } printf("Physical address of allocated buffer is %#x.\n", (unsigned int) physp_nocache); /* Write some data into this buffer */ for (i=0; i < size / sizeof(int) ; i++) { ptr1_nocache[i] = 0xbeefbeef; } printf("Allocating first cached buffer.\n"); /* First allocate a buffer from the pool that best fits */ params = CMEM_DEFAULTPARAMS; params.flags = CMEM_CACHED; ptr1_cache = CMEM_alloc2(block, size, ¶ms); if (ptr1_cache == NULL) { fprintf(stderr, "Failed to allocate buffer of size %d\n", size); goto cleanup; } printf("Allocated buffer of size %d at address %#x.\n", size, (unsigned int) ptr1_cache); /* Find out and print the physical address of this buffer */ physp_cache = CMEM_getPhys(ptr1_cache); if (physp_cache == 0) { fprintf(stderr, "Failed to get physical address of buffer %#x\n", (unsigned int) ptr1_cache); goto cleanup; } printf("Physical address of allocated buffer is %#x.\n", (unsigned int) physp_cache); /* Write some data into this buffer */ for (i = 0; i < size / sizeof(int); i++) { ptr1_cache[i] = 0x0dead1ce; } printf("Allocating noncached DMA source buffer.\n"); /* Allocate a noncached buffer for the DMA source */ ptr1_dma = CMEM_alloc(size, NULL); if (ptr1_dma == NULL) { fprintf(stderr, "Failed to allocate buffer of size %d\n", size); goto cleanup; } printf("Allocated buffer of size %d at address %#x.\n", size, (unsigned int) ptr1_dma); /* Find out and print the physical address of this buffer */ physp_dma = CMEM_getPhys(ptr1_dma); if (physp_dma == 0) { fprintf(stderr, "Failed to get physical address of buffer %#x\n", (unsigned int) ptr1_dma); goto cleanup; } printf("Physical address of allocated buffer is %#x.\n", (unsigned int) physp_dma); /* Initialize DMA source buffer */ for (i = 0; i < size / sizeof(int); i++) { ptr1_cache[i] = 0x0dead1ce; } /* * Measure the write performance of each buffer to check that one * is cached and the other isn't. */ printf("Measuring R-M-W performance (cached should be quicker).\n"); for (j = 0; j < 3; j++) { printf("R-M-W noncached buffer %lx\n", physp_nocache); gettimeofday(&start_tv, NULL); for (i = 0; i < (size / sizeof(int)); i += 1) { ptr1_nocache[i] += 1; } gettimeofday(&end_tv, NULL); diff = end_tv.tv_usec - start_tv.tv_usec; if (end_tv.tv_sec > start_tv.tv_sec) { diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000; } printf(" diff=%ld\n", diff); printf("R-M-W cached buffer %lx\n", physp_cache); gettimeofday(&start_tv, NULL); for (i = 0; i < (size / sizeof(int)); i += 1) { ptr1_cache[i] += 1; } gettimeofday(&end_tv, NULL); diff = end_tv.tv_usec - start_tv.tv_usec; if (end_tv.tv_sec > start_tv.tv_sec) { diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000; } printf(" diff=%ld\n", diff); } printf("Invalidate cached buffer %p\n", ptr1_cache); foo = *ptr1_cache; bar = foo; bar++; *ptr1_cache = bar; CMEM_cacheInv(ptr1_cache, size); printf("post-flush *ptr1_cache=0x%x\n", foo); printf("wrote 0x%x to *ptr1_cache\n", bar); printf("post-inv *ptr1_cache=0x%x\n", *ptr1_cache); printf("R-M-W cached buffer %lx\n", physp_cache); gettimeofday(&start_tv, NULL); for (i = 0; i < (size / sizeof(int)); i += 1) { ptr1_cache[i] += 1; } gettimeofday(&end_tv, NULL); diff = end_tv.tv_usec - start_tv.tv_usec; if (end_tv.tv_sec > start_tv.tv_sec) { diff += (end_tv.tv_sec - start_tv.tv_sec) * 1000000; } printf(" diff=%ld\n", diff); /* * Now allocate another buffer by first finding out which pool that fits * best, and then explicitly allocating from that pool. This gives more * control at the cost of an extra function call, but essentially does * the same thing as the above CMEM_alloc() call. */ printf("Allocating second buffer.\n"); poolid = CMEM_getPool(size); if (poolid == -1) { fprintf(stderr, "Failed to get a pool which fits size %d\n", size); goto cleanup; } printf("Got a pool (%d) that fits the size %d\n", poolid, size); ptr2 = CMEM_allocPool(poolid, NULL); if (ptr2 == NULL) { fprintf(stderr, "Failed to allocate buffer of size %d\n", size); goto cleanup; } printf("Allocated buffer of size %d at address %#x.\n", size, (unsigned int) ptr2); /* Find out and print the physical address of this buffer */ physp = CMEM_getPhys(ptr2); if (physp == 0) { fprintf(stderr, "Failed to get physical address of buffer %#x\n", (unsigned int) ptr2); goto cleanup; } printf("Physical address of allocated buffer is %#x.\n", (unsigned int) physp); /* Write some data into this buffer */ for (i=0; i < size / sizeof(int); i++) { ptr2[i] = 0xfeebfeeb; } printf("Inspect your memory map in /proc/%d/maps.\n", getpid()); printf("Also look at your pool info under /proc/cmem\n"); printf("Press ENTER to exit (after 'cat /proc/cmem' if desired).\n"); getchar(); cleanup: if (ptr1_nocache != NULL) { if (CMEM_free(ptr1_nocache, NULL) < 0) { fprintf(stderr, "Failed to free buffer at %#x\n", (unsigned int) ptr1_nocache); } printf("Successfully freed buffer at %#x.\n", (unsigned int) ptr1_nocache); } if (ptr1_cache != NULL) { if (CMEM_free(ptr1_cache, ¶ms) < 0) { fprintf(stderr, "Failed to free buffer at %#x\n", (unsigned int) ptr1_cache); } printf("Successfully freed buffer at %#x.\n", (unsigned int) ptr1_cache); } if (ptr1_dma != NULL) { if (CMEM_free(ptr1_dma, NULL) < 0) { fprintf(stderr, "Failed to free buffer at %#x\n", (unsigned int) ptr1_dma); } printf("Successfully freed buffer at %#x.\n", (unsigned int) ptr1_dma); } if (ptr2 != NULL) { if (CMEM_free(ptr2, NULL) < 0) { fprintf(stderr, "Failed to free buffer at %#x\n", (unsigned int) ptr2); } printf("Successfully freed buffer at %#x.\n", (unsigned int) ptr2); } }
/* ARGSUSED - this line tells the compiler not to warn about unused args. */ IRES_Status IRESMAN_MEMTCM_init(IRESMAN_Params * initArgs) { SMGRMP_Attrs attrs; IRESMAN_MemTcmParams * resmanArgs = (IRESMAN_MemTcmParams *)initArgs; /* CMEM_AllocParams params; */ GT_assert(ti_sdo_fc_ires_memtcm, initArgs != NULL); /* * Check if already initialized */ if (_initialized) { GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=IRES_EEXISTS)\n"); return (IRES_EEXISTS); } if (gtInit == 0) { GT_init(); GT_create(&CURTRACE, "ti.sdo.fc.ires.memtcm"); gtInit = 1; } GT_1trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Enter (initArgs=0x%x)\n", initArgs); /* * Information regarding the memory allocation/free functions * is stored as part of the internal state of the resource * manager */ if (NULL == _MEMTCM_lock) { /* Create a lock for protecting MEMTCM internal state object */ _MEMTCM_lock = LockMP_create(_MEMTCM_LOCKID); if (_MEMTCM_lock == NULL) { GT_1trace(CURTRACE, GT_7CLASS, "IRESMAN_MEMTCM_init> Failed to create IPC lock, " "key = 0x%x\n", _MEMTCM_LOCKID); GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=" "IRES_EFAIL)\n"); return (IRES_EFAIL); } } getInternalState(); if (NULL == _resmanInternalState) { GT_0trace(CURTRACE, GT_7CLASS, "IRESMAN_MEMTCM_init>Failed to obtain Internal state Object\n"); GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=" "IRES_EFAIL)\n"); LockMP_delete(_MEMTCM_lock); return (IRES_EFAIL); } /* * Information regarding the memory allocation/free functions */ _allocFxn = resmanArgs->baseConfig.allocFxn; _freeFxn = resmanArgs->baseConfig.freeFxn; if (0 != CMEM_init()) { GT_0trace(CURTRACE, GT_7CLASS, "IRESMAN_MEMTCM_init> Could not initialize CMEM\n"); GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=IRES_EFAIL)\n"); freeInternalState(); LockMP_delete(_MEMTCM_lock); return (IRES_EFAIL); } /* TODO: Figure out how to populate the params */ if (_resmanInternalState->numOpens == 0) { armtcmAddr = CMEM_alloc2(MEMTCM_blockId, MEMTCM_size, NULL); _resmanInternalState->armtcmAddr = (void *)CMEM_getPhys(armtcmAddr); } else { armtcmAddr = CMEM_registerAlloc( (unsigned long)_resmanInternalState->armtcmAddr); } if (NULL == armtcmAddr) { GT_0trace(CURTRACE, GT_7CLASS, "IRESMAN_MEMTCM_init> Could not allocate TCM memory from CMEM" "\n"); GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=IRES_EFAIL)\n"); freeInternalState(); LockMP_delete(_MEMTCM_lock); return (IRES_EFAIL); } if (NULL == smgr) { attrs.numScratchGroups = MEMTCM_NUM_GROUPS; attrs.numResources = (MEMTCM_NUMRES); /* One resource corresponds to a 1/2 K chunk of memory (0x200), Manage memory in chunks of 0x200 */ attrs.lock = _MEMTCM_lock; attrs.key = (void *)_MEMTCM_MEMID; /* A key specific to the resource being managed */ /* This will create a new resource manager or return a process-specific handle to an existing smgr */ smgr = SMGRMP_create(&attrs); if (NULL == smgr) { GT_0trace(CURTRACE, GT_7CLASS, "IRESMAN_MEMTCM_init> Error creating" " scratch resource manager.\n"); GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=" "IRES_EFAIL)\n"); freeInternalState(); LockMP_delete(_MEMTCM_lock); return (IRES_EFAIL); } } _resmanInternalState->numOpens++; /* * Set Initalized flag to 1 if successful */ _initialized = 1; GT_0trace(CURTRACE, GT_ENTER, "IRESMAN_MEMTCM_init> Exit (status=IRES_OK)\n"); return (IRES_OK); }