nwl_loopback.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #define _BSD_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/time.h>
  7. #include "pci.h"
  8. #include "pcilib.h"
  9. #include "error.h"
  10. #include "tools.h"
  11. #include "nwl_private.h"
  12. #include "nwl_defines.h"
  13. #define NWL_BUG_EXTRA_DATA
  14. int dma_nwl_start_loopback(nwl_dma_t *ctx, pcilib_dma_direction_t direction, size_t packet_size) {
  15. uint32_t val;
  16. ctx->loopback_started = 1;
  17. dma_nwl_stop_loopback(ctx);
  18. val = packet_size;
  19. nwl_write_register(val, ctx, ctx->base_addr, PKT_SIZE_ADDRESS);
  20. if (ctx->type == PCILIB_DMA_MODIFICATION_DEFAULT) {
  21. switch (direction) {
  22. case PCILIB_DMA_BIDIRECTIONAL:
  23. val = LOOPBACK;
  24. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  25. break;
  26. case PCILIB_DMA_TO_DEVICE:
  27. return -1;
  28. case PCILIB_DMA_FROM_DEVICE:
  29. val = PKTGENR;
  30. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  31. break;
  32. }
  33. }
  34. ctx->loopback_started = 1;
  35. return 0;
  36. }
  37. int dma_nwl_stop_loopback(nwl_dma_t *ctx) {
  38. uint32_t val = 0;
  39. if (!ctx->loopback_started) return 0;
  40. /* Stop in any case, otherwise we can have problems in benchmark due to
  41. engine initialized in previous run, and benchmark is only actual usage.
  42. Otherwise, we should detect current loopback status during initialization */
  43. if (ctx->type == PCILIB_DMA_MODIFICATION_DEFAULT) {
  44. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  45. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  46. }
  47. ctx->loopback_started = 0;
  48. return 0;
  49. }
  50. double dma_nwl_benchmark(pcilib_dma_context_t *vctx, pcilib_dma_engine_addr_t dma, uintptr_t addr, size_t size, size_t iterations, pcilib_dma_direction_t direction) {
  51. int iter, i;
  52. int err;
  53. size_t bytes, rbytes;
  54. uint32_t *buf, *cmp;
  55. const char *error = NULL;
  56. size_t packet_size, blocks;
  57. size_t us = 0;
  58. struct timeval start, cur;
  59. nwl_dma_t *ctx = (nwl_dma_t*)vctx;
  60. pcilib_dma_engine_t readid = pcilib_find_dma_by_addr(ctx->pcilib, PCILIB_DMA_FROM_DEVICE, dma);
  61. pcilib_dma_engine_t writeid = pcilib_find_dma_by_addr(ctx->pcilib, PCILIB_DMA_TO_DEVICE, dma);
  62. if (size%sizeof(uint32_t)) size = 1 + size / sizeof(uint32_t);
  63. else size /= sizeof(uint32_t);
  64. // Not supported
  65. if (ctx->type == PCILIB_DMA_MODIFICATION_DEFAULT) {
  66. if (direction == PCILIB_DMA_TO_DEVICE) return -1.;
  67. }
  68. // else if ((direction == PCILIB_DMA_FROM_DEVICE)&&(ctx->type != PCILIB_DMA_MODIFICATION_DEFAULT)) return -1.;
  69. // Stop Generators and drain old data
  70. if (ctx->type == PCILIB_DMA_MODIFICATION_DEFAULT) dma_nwl_stop_loopback(ctx);
  71. // dma_nwl_stop_engine(ctx, readid); // DS: replace with something better
  72. __sync_synchronize();
  73. err = pcilib_skip_dma(ctx->pcilib, readid);
  74. if (err) {
  75. pcilib_error("Can't start benchmark, devices continuously writes unexpected data using DMA engine");
  76. return -1;
  77. }
  78. #ifdef NWL_GENERATE_DMA_IRQ
  79. dma_nwl_enable_engine_irq(ctx, readid);
  80. dma_nwl_enable_engine_irq(ctx, writeid);
  81. #endif /* NWL_GENERATE_DMA_IRQ */
  82. if (size * sizeof(uint32_t) > NWL_MAX_PACKET_SIZE) {
  83. packet_size = NWL_MAX_PACKET_SIZE;
  84. blocks = (size * sizeof(uint32_t)) / packet_size + (((size*sizeof(uint32_t))%packet_size)?1:0);
  85. } else {
  86. packet_size = size * sizeof(uint32_t);
  87. blocks = 1;
  88. }
  89. dma_nwl_start_loopback(ctx, direction, packet_size);
  90. // Allocate memory and prepare data
  91. buf = malloc(blocks * packet_size * sizeof(uint32_t));
  92. cmp = malloc(blocks * packet_size * sizeof(uint32_t));
  93. if ((!buf)||(!cmp)) {
  94. if (buf) free(buf);
  95. if (cmp) free(cmp);
  96. return -1;
  97. }
  98. if (ctx->type == PCILIB_NWL_MODIFICATION_IPECAMERA) {
  99. pcilib_write_register(ctx->pcilib, NULL, "control", 0x1e5);
  100. usleep(100000);
  101. pcilib_write_register(ctx->pcilib, NULL, "control", 0x1e1);
  102. // This way causes more problems with garbage
  103. //pcilib_write_register(ctx->pcilib, NULL, "control", 0x3e1);
  104. }
  105. // Benchmark
  106. for (iter = 0; iter < iterations; iter++) {
  107. memset(cmp, 0x13 + iter, size * sizeof(uint32_t));
  108. if (ctx->type == PCILIB_NWL_MODIFICATION_IPECAMERA) {
  109. pcilib_write_register(ctx->pcilib, NULL, "control", 0x1e1);
  110. }
  111. if ((direction&PCILIB_DMA_TO_DEVICE)||(ctx->type != PCILIB_DMA_MODIFICATION_DEFAULT)) {
  112. memcpy(buf, cmp, size * sizeof(uint32_t));
  113. if (direction&PCILIB_DMA_TO_DEVICE) {
  114. gettimeofday(&start, NULL);
  115. }
  116. err = pcilib_write_dma(ctx->pcilib, writeid, addr, size * sizeof(uint32_t), buf, &bytes);
  117. if ((err)||(bytes != size * sizeof(uint32_t))) {
  118. error = "Write failed";
  119. break;
  120. }
  121. if (direction&PCILIB_DMA_TO_DEVICE) {
  122. // wait written
  123. if (direction == PCILIB_DMA_TO_DEVICE) {
  124. dma_nwl_wait_completion(ctx, writeid, PCILIB_DMA_TIMEOUT);
  125. }
  126. gettimeofday(&cur, NULL);
  127. us += ((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec));
  128. }
  129. }
  130. if (ctx->type == PCILIB_NWL_MODIFICATION_IPECAMERA) {
  131. pcilib_write_register(ctx->pcilib, NULL, "control", 0x3e1);
  132. }
  133. memset(buf, 0, size * sizeof(uint32_t));
  134. if (direction&PCILIB_DMA_FROM_DEVICE) {
  135. gettimeofday(&start, NULL);
  136. }
  137. for (i = 0, bytes = 0; i < blocks; i++) {
  138. #ifdef NWL_BUG_EXTRA_DATA
  139. retry:
  140. #endif
  141. err = pcilib_read_dma(ctx->pcilib, readid, addr, packet_size * sizeof(uint32_t), buf + (bytes>>2), &rbytes);
  142. if ((err)||(rbytes%sizeof(uint32_t))) {
  143. break;
  144. }
  145. #ifdef NWL_BUG_EXTRA_DATA
  146. else if (rbytes == 8) {
  147. goto retry;
  148. }
  149. #endif
  150. bytes += rbytes;
  151. }
  152. if (direction&PCILIB_DMA_FROM_DEVICE) {
  153. gettimeofday(&cur, NULL);
  154. us += ((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec));
  155. }
  156. #ifdef NWL_BUG_EXTRA_DATA
  157. if ((err)||((bytes != size * sizeof(uint32_t))&&((bytes - 8) != size * sizeof(uint32_t)))) {
  158. #else
  159. if ((err)||(bytes != size * sizeof(uint32_t))) {
  160. #endif
  161. printf("Expected: %zu bytes, but %zu read, error: %i\n", size * sizeof(uint32_t), bytes, err);
  162. error = "Read failed";
  163. break;
  164. }
  165. #ifndef NWL_BUG_EXTRA_DATA
  166. if (direction == PCILIB_DMA_BIDIRECTIONAL) {
  167. if (memcmp(buf, cmp, size * sizeof(uint32_t))) {
  168. for (i = 0; i < size; i++)
  169. if (buf[i] != cmp[i]) break;
  170. bytes = i;
  171. printf("Expected: *0x%lx, Written at dword %lu:", 0x13 + iter, bytes);
  172. for (; (i < size)&&(i < (bytes + 16)); i++) {
  173. if (((i - bytes)%8)==0) printf("\n");
  174. printf("% 10lx", buf[i]);
  175. }
  176. printf("\n");
  177. error = "Written and read values does not match";
  178. break;
  179. }
  180. }
  181. #endif
  182. }
  183. if (ctx->type == PCILIB_NWL_MODIFICATION_IPECAMERA) {
  184. pcilib_write_register(ctx->pcilib, NULL, "control", 0x1e1);
  185. }
  186. if (error) {
  187. pcilib_warning("%s at iteration %i, error: %i, bytes: %zu", error, iter, err, bytes);
  188. }
  189. #ifdef NWL_GENERATE_DMA_IRQ
  190. dma_nwl_disable_engine_irq(ctx, writeid);
  191. dma_nwl_disable_engine_irq(ctx, readid);
  192. #endif /* NWL_GENERATE_DMA_IRQ */
  193. dma_nwl_stop_loopback(ctx);
  194. __sync_synchronize();
  195. if (direction == PCILIB_DMA_FROM_DEVICE) {
  196. pcilib_skip_dma(ctx->pcilib, readid);
  197. }
  198. free(cmp);
  199. free(buf);
  200. return /*error?-1:*/(1. * size * sizeof(uint32_t) * iterations * 1000000) / (1024. * 1024. * us);
  201. }