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; }
/* * ======== MEMUTILS_cacheInv ======== */ Void MEMUTILS_cacheInv(Ptr addr, Int sizeInBytes) { if (!regInit) { addModule(); } CMEM_init(); CMEM_cacheInv(addr, sizeInBytes); CMEM_exit(); }
/* * ======== MEMUTILS_cacheInv ======== */ Void MEMUTILS_cacheInv(Ptr addr, Int sizeInBytes) { #if GT_TRACE if (!(gtInit)) { GT_init(); GT_create(&curTrace, "ti.sdo.fc.memutils"); gtInit = 1; } #endif CMEM_init(); CMEM_cacheInv(addr, sizeInBytes); CMEM_exit(); }
/* * ======== Memory_cacheInv ======== */ Void Memory_cacheInv(Ptr addr, Int sizeInBytes) { CMEM_cacheInv(addr, sizeInBytes); }
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); } }