nwl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #define _PCILIB_DMA_NWL_C
  2. #define _BSD_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/time.h>
  8. #include "pci.h"
  9. #include "dma.h"
  10. #include "pcilib.h"
  11. #include "error.h"
  12. #include "tools.h"
  13. #include "nwl.h"
  14. #include "nwl_defines.h"
  15. #define NWL_XAUI_ENGINE 0
  16. #define NWL_XRAWDATA_ENGINE 1
  17. #define NWL_FIX_EOP_FOR_BIG_PACKETS // requires precise sizes in read requests
  18. /*
  19. pcilib_register_bank_description_t ipecamera_register_banks[] = {
  20. { PCILIB_REGISTER_DMABANK0, PCILIB_BAR0, 128, PCILIB_DEFAULT_PROTOCOL, DMA_NWL_OFFSET, DMA_NWL_OFFSET, PCILIB_LITTLE_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "%lx", "dma", "NorthWest Logick DMA Engine" },
  21. { 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
  22. };
  23. */
  24. typedef struct {
  25. pcilib_dma_engine_description_t desc;
  26. char *base_addr;
  27. size_t ring_size, page_size;
  28. size_t head, tail;
  29. pcilib_kmem_handle_t *ring;
  30. pcilib_kmem_handle_t *pages;
  31. int started; // indicates if DMA buffers are initialized and reading is allowed
  32. int writting; // indicates if we are in middle of writting packet
  33. } pcilib_nwl_engine_description_t;
  34. struct nwl_dma_s {
  35. pcilib_t *pcilib;
  36. pcilib_register_bank_description_t *dma_bank;
  37. char *base_addr;
  38. pcilib_dma_engine_t n_engines;
  39. pcilib_nwl_engine_description_t engines[PCILIB_MAX_DMA_ENGINES + 1];
  40. };
  41. #define nwl_read_register(var, ctx, base, reg) pcilib_datacpy(&var, base + reg, 4, 1, ctx->dma_bank->raw_endianess)
  42. #define nwl_write_register(var, ctx, base, reg) pcilib_datacpy(base + reg, &var, 4, 1, ctx->dma_bank->raw_endianess)
  43. static int nwl_read_engine_config(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info, char *base) {
  44. uint32_t val;
  45. info->base_addr = base;
  46. nwl_read_register(val, ctx, base, REG_DMA_ENG_CAP);
  47. if ((val & DMA_ENG_PRESENT_MASK) == 0) return PCILIB_ERROR_NOTAVAILABLE;
  48. info->desc.addr = (val & DMA_ENG_NUMBER) >> DMA_ENG_NUMBER_SHIFT;
  49. if ((info->desc.addr > PCILIB_MAX_DMA_ENGINES)||(info->desc.addr < 0)) return PCILIB_ERROR_INVALID_DATA;
  50. switch (val & DMA_ENG_DIRECTION_MASK) {
  51. case DMA_ENG_C2S:
  52. info->desc.direction = PCILIB_DMA_FROM_DEVICE;
  53. break;
  54. default:
  55. info->desc.direction = PCILIB_DMA_TO_DEVICE;
  56. }
  57. switch (val & DMA_ENG_TYPE_MASK) {
  58. case DMA_ENG_BLOCK:
  59. info->desc.type = PCILIB_DMA_TYPE_BLOCK;
  60. break;
  61. case DMA_ENG_PACKET:
  62. info->desc.type = PCILIB_DMA_TYPE_PACKET;
  63. break;
  64. default:
  65. info->desc.type = PCILIB_DMA_TYPE_UNKNOWN;
  66. }
  67. info->desc.addr_bits = (val & DMA_ENG_BD_MAX_BC) >> DMA_ENG_BD_MAX_BC_SHIFT;
  68. return 0;
  69. }
  70. static int nwl_stop_engine(nwl_dma_t *ctx, pcilib_dma_engine_t dma) {
  71. uint32_t val;
  72. struct timeval start, cur;
  73. pcilib_nwl_engine_description_t *info = ctx->engines + dma;
  74. char *base = ctx->engines[dma].base_addr;
  75. if (info->desc.addr == NWL_XRAWDATA_ENGINE) {
  76. // Stop Generators
  77. nwl_read_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  78. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  79. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  80. nwl_read_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  81. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  82. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  83. // Skip everything in read queue (could be we need to start and skip as well)
  84. if (info->started) pcilib_skip_dma(ctx->pcilib, dma);
  85. }
  86. // Disable IRQ
  87. nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  88. val &= ~(DMA_ENG_INT_ENABLE);
  89. nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  90. // Reseting
  91. val = DMA_ENG_DISABLE|DMA_ENG_USER_RESET; nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  92. gettimeofday(&start, NULL);
  93. do {
  94. nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  95. gettimeofday(&cur, NULL);
  96. } while ((val & (DMA_ENG_STATE_MASK|DMA_ENG_USER_RESET))&&(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < PCILIB_REGISTER_TIMEOUT));
  97. if (val & (DMA_ENG_STATE_MASK|DMA_ENG_USER_RESET)) {
  98. pcilib_error("Timeout during reset of DMA engine %i", info->desc.addr);
  99. return PCILIB_ERROR_TIMEOUT;
  100. }
  101. val = DMA_ENG_RESET; nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  102. gettimeofday(&start, NULL);
  103. do {
  104. nwl_read_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  105. gettimeofday(&cur, NULL);
  106. } while ((val & DMA_ENG_RESET)&&(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < PCILIB_REGISTER_TIMEOUT));
  107. if (val & DMA_ENG_RESET) {
  108. pcilib_error("Timeout during reset of DMA engine %i", info->desc.addr);
  109. return PCILIB_ERROR_TIMEOUT;
  110. }
  111. // Acknowledge asserted engine interrupts
  112. if (val & DMA_ENG_INT_ACTIVE_MASK) {
  113. val |= DMA_ENG_ALLINT_MASK;
  114. nwl_write_register(val, ctx, base, REG_DMA_ENG_CTRL_STATUS);
  115. }
  116. // Clean buffers
  117. if (info->ring) {
  118. pcilib_free_kernel_memory(ctx->pcilib, info->ring);
  119. info->ring = NULL;
  120. }
  121. if (info->pages) {
  122. pcilib_free_kernel_memory(ctx->pcilib, info->pages);
  123. info->pages = NULL;
  124. }
  125. info->started = 0;
  126. return 0;
  127. }
  128. pcilib_dma_context_t *dma_nwl_init(pcilib_t *pcilib) {
  129. int i;
  130. int err;
  131. uint32_t val;
  132. pcilib_dma_engine_t n_engines;
  133. pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
  134. nwl_dma_t *ctx = malloc(sizeof(nwl_dma_t));
  135. if (ctx) {
  136. memset(ctx, 0, sizeof(nwl_dma_t));
  137. ctx->pcilib = pcilib;
  138. pcilib_register_bank_t dma_bank = pcilib_find_bank_by_addr(pcilib, PCILIB_REGISTER_BANK_DMA);
  139. if (dma_bank == PCILIB_REGISTER_BANK_INVALID) {
  140. pcilib_error("DMA Register Bank could not be found");
  141. return NULL;
  142. }
  143. ctx->dma_bank = model_info->banks + dma_bank;
  144. ctx->base_addr = pcilib_resolve_register_address(pcilib, ctx->dma_bank->bar, ctx->dma_bank->read_addr);
  145. val = 0;
  146. nwl_read_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  147. nwl_read_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  148. for (i = 0, n_engines = 0; i < 2 * PCILIB_MAX_DMA_ENGINES; i++) {
  149. char *addr = ctx->base_addr + DMA_OFFSET + i * DMA_ENGINE_PER_SIZE;
  150. memset(ctx->engines + n_engines, 0, sizeof(pcilib_nwl_engine_description_t));
  151. err = nwl_read_engine_config(ctx, ctx->engines + n_engines, addr);
  152. if (!err) err = nwl_stop_engine(ctx, n_engines);
  153. if (!err) {
  154. ctx->engines[n_engines].base_addr = addr;
  155. pcilib_set_dma_engine_description(pcilib, n_engines, (pcilib_dma_engine_description_t*)(ctx->engines + n_engines));
  156. ++n_engines;
  157. }
  158. }
  159. pcilib_set_dma_engine_description(pcilib, n_engines, NULL);
  160. ctx->n_engines = n_engines;
  161. }
  162. return (pcilib_dma_context_t*)ctx;
  163. }
  164. void dma_nwl_free(pcilib_dma_context_t *vctx) {
  165. pcilib_dma_engine_t i;
  166. nwl_dma_t *ctx = (nwl_dma_t*)vctx;
  167. if (ctx) {
  168. for (i = 0; i < ctx->n_engines; i++) nwl_stop_engine(vctx, i);
  169. free(ctx);
  170. }
  171. }
  172. #define PCILIB_NWL_ALIGNMENT 64 // in bytes
  173. #define PCILIB_NWL_DMA_DESCRIPTOR_SIZE 64 // in bytes
  174. #define PCILIB_NWL_DMA_PAGES 512 // 1024
  175. #define NWL_RING_GET(data, offset) *(uint32_t*)(((char*)(data)) + (offset))
  176. #define NWL_RING_SET(data, offset, val) *(uint32_t*)(((char*)(data)) + (offset)) = (val)
  177. #define NWL_RING_UPDATE(data, offset, mask, val) *(uint32_t*)(((char*)(data)) + (offset)) = ((*(uint32_t*)(((char*)(data)) + (offset)))&(mask))|(val)
  178. int dma_nwl_sync_buffers(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info, pcilib_kmem_handle_t *kmem) {
  179. switch (info->desc.direction) {
  180. case PCILIB_DMA_FROM_DEVICE:
  181. return pcilib_sync_kernel_memory(ctx->pcilib, kmem, PCILIB_KMEM_SYNC_FROMDEVICE);
  182. case PCILIB_DMA_TO_DEVICE:
  183. return pcilib_sync_kernel_memory(ctx->pcilib, kmem, PCILIB_KMEM_SYNC_TODEVICE);
  184. }
  185. return 0;
  186. }
  187. int dma_nwl_allocate_engine_buffers(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info) {
  188. int err = 0;
  189. int i;
  190. uint32_t val;
  191. uint32_t buf_sz;
  192. uint64_t buf_pa;
  193. char *base = info->base_addr;
  194. if (info->pages) return 0;
  195. pcilib_kmem_handle_t *ring = pcilib_alloc_kernel_memory(ctx->pcilib, PCILIB_KMEM_TYPE_CONSISTENT, 1, PCILIB_NWL_DMA_PAGES * PCILIB_NWL_DMA_DESCRIPTOR_SIZE, PCILIB_NWL_ALIGNMENT, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA, info->desc.addr), 0);
  196. pcilib_kmem_handle_t *pages = pcilib_alloc_kernel_memory(ctx->pcilib, PCILIB_KMEM_TYPE_PAGE, PCILIB_NWL_DMA_PAGES, 0, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA, info->desc.addr), 0);
  197. if ((ring)&&(pages)) err = dma_nwl_sync_buffers(ctx, info, pages);
  198. else err = PCILIB_ERROR_FAILED;
  199. if (err) {
  200. if (pages) pcilib_free_kernel_memory(ctx->pcilib, pages);
  201. if (ring) pcilib_free_kernel_memory(ctx->pcilib, ring);
  202. return err;
  203. }
  204. unsigned char *data = (unsigned char*)pcilib_kmem_get_ua(ctx->pcilib, ring);
  205. uint32_t ring_pa = pcilib_kmem_get_pa(ctx->pcilib, ring);
  206. memset(data, 0, PCILIB_NWL_DMA_PAGES * PCILIB_NWL_DMA_DESCRIPTOR_SIZE);
  207. for (i = 0; i < PCILIB_NWL_DMA_PAGES; i++, data += PCILIB_NWL_DMA_DESCRIPTOR_SIZE) {
  208. buf_pa = pcilib_kmem_get_block_pa(ctx->pcilib, pages, i);
  209. buf_sz = pcilib_kmem_get_block_size(ctx->pcilib, pages, i);
  210. NWL_RING_SET(data, DMA_BD_NDESC_OFFSET, ring_pa + ((i + 1) % PCILIB_NWL_DMA_PAGES) * PCILIB_NWL_DMA_DESCRIPTOR_SIZE);
  211. NWL_RING_SET(data, DMA_BD_BUFAL_OFFSET, buf_pa&0xFFFFFFFF);
  212. NWL_RING_SET(data, DMA_BD_BUFAH_OFFSET, buf_pa>>32);
  213. NWL_RING_SET(data, DMA_BD_BUFL_CTRL_OFFSET, buf_sz);
  214. /*
  215. if (info->desc.direction == PCILIB_DMA_TO_DEVICE) {
  216. NWL_RING_SET(data, DMA_BD_BUFL_STATUS_OFFSET, buf_sz);
  217. }
  218. */
  219. }
  220. val = ring_pa;
  221. nwl_write_register(val, ctx, base, REG_DMA_ENG_NEXT_BD);
  222. nwl_write_register(val, ctx, base, REG_SW_NEXT_BD);
  223. info->ring = ring;
  224. info->pages = pages;
  225. info->page_size = buf_sz;
  226. info->ring_size = PCILIB_NWL_DMA_PAGES;
  227. info->head = 0;
  228. info->tail = 0;
  229. return 0;
  230. }
  231. static int dma_nwl_start(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info) {
  232. int err;
  233. uint32_t ring_pa;
  234. uint32_t val;
  235. if (info->started) return 0;
  236. err = dma_nwl_allocate_engine_buffers(ctx, info);
  237. if (err) return err;
  238. ring_pa = pcilib_kmem_get_pa(ctx->pcilib, info->ring);
  239. nwl_write_register(ring_pa, ctx, info->base_addr, REG_DMA_ENG_NEXT_BD);
  240. nwl_write_register(ring_pa, ctx, info->base_addr, REG_SW_NEXT_BD);
  241. __sync_synchronize();
  242. nwl_read_register(val, ctx, info->base_addr, REG_DMA_ENG_CTRL_STATUS);
  243. val |= (DMA_ENG_ENABLE);
  244. nwl_write_register(val, ctx, info->base_addr, REG_DMA_ENG_CTRL_STATUS);
  245. __sync_synchronize();
  246. if (info->desc.direction == PCILIB_DMA_FROM_DEVICE) {
  247. ring_pa += (info->ring_size - 1) * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  248. nwl_write_register(ring_pa, ctx, info->base_addr, REG_SW_NEXT_BD);
  249. // nwl_read_register(val, ctx, info->base_addr, 0x18);
  250. info->tail = 0;
  251. info->head = (info->ring_size - 1);
  252. } else {
  253. info->tail = 0;
  254. info->head = 0;
  255. }
  256. info->started = 1;
  257. return 0;
  258. }
  259. static size_t dma_nwl_clean_buffers(nwl_dma_t * ctx, pcilib_nwl_engine_description_t *info) {
  260. size_t res = 0;
  261. uint32_t status, control;
  262. unsigned char *ring = pcilib_kmem_get_ua(ctx->pcilib, info->ring);
  263. ring += info->tail * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  264. next_buffer:
  265. status = NWL_RING_GET(ring, DMA_BD_BUFL_STATUS_OFFSET)&DMA_BD_STATUS_MASK;
  266. // control = NWL_RING_GET(ring, DMA_BD_BUFL_CTRL_OFFSET)&DMA_BD_CTRL_MASK;
  267. if (status & DMA_BD_ERROR_MASK) {
  268. pcilib_error("NWL DMA Engine reported error in ring descriptor");
  269. return (size_t)-1;
  270. }
  271. if (status & DMA_BD_SHORT_MASK) {
  272. pcilib_error("NWL DMA Engine reported short error");
  273. return (size_t)-1;
  274. }
  275. if (status & DMA_BD_COMP_MASK) {
  276. info->tail++;
  277. if (info->tail == info->ring_size) {
  278. ring -= (info->tail - 1) * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  279. info->tail = 0;
  280. } else {
  281. ring += PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  282. }
  283. res++;
  284. if (info->tail != info->head) goto next_buffer;
  285. }
  286. // printf("====> Cleaned: %i\n", res);
  287. return res;
  288. }
  289. static size_t dma_nwl_get_next_buffer(nwl_dma_t * ctx, pcilib_nwl_engine_description_t *info, size_t n_buffers, size_t timeout) {
  290. struct timeval start, cur;
  291. size_t res, n = 0;
  292. size_t head;
  293. for (head = info->head; (((head + 1)%info->ring_size) != info->tail)&&(n < n_buffers); head++, n++);
  294. if (n == n_buffers) return info->head;
  295. gettimeofday(&start, NULL);
  296. res = dma_nwl_clean_buffers(ctx, info);
  297. if (res == (size_t)-1) return PCILIB_DMA_BUFFER_INVALID;
  298. else n += res;
  299. while (n < n_buffers) {
  300. if (timeout != PCILIB_TIMEOUT_INFINITE) {
  301. gettimeofday(&cur, NULL);
  302. if (((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) > timeout) break;
  303. }
  304. usleep (10);
  305. res = dma_nwl_clean_buffers(ctx, info);
  306. if (res == (size_t)-1) return PCILIB_DMA_BUFFER_INVALID;
  307. else if (res > 0) {
  308. gettimeofday(&start, NULL);
  309. n += res;
  310. }
  311. }
  312. if (n < n_buffers) return PCILIB_DMA_BUFFER_INVALID;
  313. return info->head;
  314. }
  315. static int dma_nwl_push_buffer(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info, size_t size, int eop, size_t timeout) {
  316. int flags;
  317. uint32_t val;
  318. unsigned char *ring = pcilib_kmem_get_ua(ctx->pcilib, info->ring);
  319. uint32_t ring_pa = pcilib_kmem_get_pa(ctx->pcilib, info->ring);
  320. ring += info->head * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  321. if (!info->writting) {
  322. flags |= DMA_BD_SOP_MASK;
  323. info->writting = 1;
  324. }
  325. if (eop) {
  326. flags |= DMA_BD_EOP_MASK;
  327. info->writting = 0;
  328. }
  329. NWL_RING_SET(ring, DMA_BD_BUFL_CTRL_OFFSET, size|flags);
  330. NWL_RING_SET(ring, DMA_BD_BUFL_STATUS_OFFSET, size);
  331. info->head++;
  332. if (info->head == info->ring_size) info->head = 0;
  333. val = ring_pa + info->head * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  334. nwl_write_register(val, ctx, info->base_addr, REG_SW_NEXT_BD);
  335. // nwl_read_register(val, ctx, info->base_addr, 0x18);
  336. // usleep(10000);
  337. // nwl_read_register(val, ctx, info->base_addr, REG_DMA_ENG_LAST_BD);
  338. // printf("Last BD(Write): %lx %lx\n", ring, val);
  339. return 0;
  340. }
  341. static size_t dma_nwl_wait_buffer(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info, size_t *size, int *eop, size_t timeout) {
  342. uint32_t val;
  343. struct timeval start, cur;
  344. uint32_t status_size, status, control;
  345. // usleep(10000);
  346. unsigned char *ring = pcilib_kmem_get_ua(ctx->pcilib, info->ring);
  347. // status_size = NWL_RING_GET(ring, DMA_BD_BUFL_STATUS_OFFSET);
  348. // printf("Status0: %lx\n", status_size);
  349. ring += info->tail * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  350. gettimeofday(&start, NULL);
  351. // printf("Waiting %li\n", info->tail);
  352. // nwl_read_register(val, ctx, info->base_addr, REG_DMA_ENG_LAST_BD);
  353. // printf("Last BD(Read): %lx %lx\n", ring, val);
  354. do {
  355. status_size = NWL_RING_GET(ring, DMA_BD_BUFL_STATUS_OFFSET);
  356. status = status_size & DMA_BD_STATUS_MASK;
  357. // printf("%i: %lx\n", info->tail, status_size);
  358. if (status & DMA_BD_ERROR_MASK) {
  359. pcilib_error("NWL DMA Engine reported error in ring descriptor");
  360. return (size_t)-1;
  361. }
  362. if (status & DMA_BD_COMP_MASK) {
  363. if (status & DMA_BD_EOP_MASK) *eop = 1;
  364. else *eop = 0;
  365. *size = status_size & DMA_BD_BUFL_MASK;
  366. // printf("Status: %lx\n", status_size);
  367. return info->tail;
  368. }
  369. usleep(10);
  370. gettimeofday(&cur, NULL);
  371. } while ((timeout == PCILIB_TIMEOUT_INFINITE)||(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < timeout));
  372. // printf("Final status: %lx\n", status_size);
  373. return (size_t)-1;
  374. }
  375. static int dma_nwl_return_buffer(nwl_dma_t *ctx, pcilib_nwl_engine_description_t *info) {
  376. uint32_t val;
  377. unsigned char *ring = pcilib_kmem_get_ua(ctx->pcilib, info->ring);
  378. uint32_t ring_pa = pcilib_kmem_get_pa(ctx->pcilib, info->ring);
  379. size_t bufsz = pcilib_kmem_get_block_size(ctx->pcilib, info->pages, info->tail);
  380. ring += info->tail * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  381. // printf("Returning: %i\n", info->tail);
  382. NWL_RING_SET(ring, DMA_BD_BUFL_CTRL_OFFSET, bufsz);
  383. NWL_RING_SET(ring, DMA_BD_BUFL_STATUS_OFFSET, 0);
  384. val = ring_pa + info->tail * PCILIB_NWL_DMA_DESCRIPTOR_SIZE;
  385. nwl_write_register(val, ctx, info->base_addr, REG_SW_NEXT_BD);
  386. // nwl_read_register(val, ctx, info->base_addr, 0x18);
  387. info->tail++;
  388. if (info->tail == info->ring_size) info->tail = 0;
  389. }
  390. size_t dma_nwl_write_fragment(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, size_t timeout, void *data) {
  391. int err;
  392. size_t pos;
  393. size_t bufnum;
  394. nwl_dma_t *ctx = (nwl_dma_t*)vctx;
  395. pcilib_nwl_engine_description_t *info = ctx->engines + dma;
  396. err = dma_nwl_start(ctx, info);
  397. if (err) return 0;
  398. for (pos = 0; pos < size; pos += info->page_size) {
  399. int block_size = min2(size - pos, info->page_size);
  400. bufnum = dma_nwl_get_next_buffer(ctx, info, 1, timeout);
  401. if (bufnum == PCILIB_DMA_BUFFER_INVALID) return pos;
  402. //sync
  403. void *buf = pcilib_kmem_get_block_ua(ctx->pcilib, info->pages, bufnum);
  404. memcpy(buf, data, block_size);
  405. err = dma_nwl_push_buffer(ctx, info, block_size, (flags&PCILIB_DMA_FLAG_EOP)&&((pos + block_size) == size), timeout);
  406. if (err) return pos;
  407. }
  408. return size;
  409. }
  410. size_t dma_nwl_stream_read(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, size_t timeout, pcilib_dma_callback_t cb, void *cbattr) {
  411. int err, ret;
  412. size_t res = 0;
  413. size_t bufnum;
  414. size_t bufsize;
  415. nwl_dma_t *ctx = (nwl_dma_t*)vctx;
  416. size_t buf_size;
  417. int eop;
  418. pcilib_nwl_engine_description_t *info = ctx->engines + dma;
  419. err = dma_nwl_start(ctx, info);
  420. if (err) return 0;
  421. do {
  422. bufnum = dma_nwl_wait_buffer(ctx, info, &bufsize, &eop, timeout);
  423. if (bufnum == PCILIB_DMA_BUFFER_INVALID) return 0;
  424. #ifdef NWL_FIX_EOP_FOR_BIG_PACKETS
  425. if (size > 65536) {
  426. // printf("%i %i\n", res + bufsize, size);
  427. if ((res+bufsize) < size) eop = 0;
  428. else if ((res+bufsize) == size) eop = 1;
  429. }
  430. #endif /* NWL_FIX_EOP_FOR_BIG_PACKETS */
  431. //sync
  432. void *buf = pcilib_kmem_get_block_ua(ctx->pcilib, info->pages, bufnum);
  433. ret = cb(cbattr, eop?PCILIB_DMA_FLAG_EOP:0, bufsize, buf);
  434. dma_nwl_return_buffer(ctx, info);
  435. res += bufsize;
  436. // printf("%i %i %i (%li)\n", ret, res, eop, size);
  437. } while (ret);
  438. return res;
  439. }
  440. 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) {
  441. int i;
  442. int res;
  443. int err;
  444. size_t bytes;
  445. uint32_t val;
  446. uint32_t *buf, *cmp;
  447. const char *error = NULL;
  448. size_t us = 0;
  449. struct timeval start, cur;
  450. nwl_dma_t *ctx = (nwl_dma_t*)vctx;
  451. pcilib_dma_engine_t readid = pcilib_find_dma_by_addr(ctx->pcilib, PCILIB_DMA_FROM_DEVICE, dma);
  452. pcilib_dma_engine_t writeid = pcilib_find_dma_by_addr(ctx->pcilib, PCILIB_DMA_TO_DEVICE, dma);
  453. if (size%sizeof(uint32_t)) size = 1 + size / sizeof(uint32_t);
  454. else size /= sizeof(uint32_t);
  455. // Stop Generators and drain old data
  456. nwl_read_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  457. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  458. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  459. nwl_read_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  460. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  461. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  462. /*
  463. nwl_stop_engine(ctx, readid);
  464. nwl_stop_engine(ctx, writeid);
  465. err = dma_nwl_start(ctx, ctx->engines + readid);
  466. if (err) return -1;
  467. err = dma_nwl_start(ctx, ctx->engines + writeid);
  468. if (err) return -1;
  469. */
  470. __sync_synchronize();
  471. pcilib_skip_dma(ctx->pcilib, readid);
  472. // Set size and required mode
  473. val = size * sizeof(uint32_t);
  474. nwl_write_register(val, ctx, ctx->base_addr, PKT_SIZE_ADDRESS);
  475. switch (direction) {
  476. case PCILIB_DMA_BIDIRECTIONAL:
  477. val = LOOPBACK;
  478. break;
  479. case PCILIB_DMA_TO_DEVICE:
  480. return -1;
  481. case PCILIB_DMA_FROM_DEVICE:
  482. val = PKTGENR;
  483. break;
  484. }
  485. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  486. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  487. // Allocate memory and prepare data
  488. buf = malloc(size * sizeof(uint32_t));
  489. cmp = malloc(size * sizeof(uint32_t));
  490. if ((!buf)||(!cmp)) {
  491. if (buf) free(buf);
  492. if (cmp) free(cmp);
  493. return -1;
  494. }
  495. memset(cmp, 0x13, size * sizeof(uint32_t));
  496. // Benchmark
  497. for (i = 0; i < iterations; i++) {
  498. // printf("Iteration: %i\n", i);
  499. gettimeofday(&start, NULL);
  500. if (direction&PCILIB_DMA_TO_DEVICE) {
  501. memcpy(buf, cmp, size * sizeof(uint32_t));
  502. bytes = pcilib_write_dma(ctx->pcilib, writeid, addr, size * sizeof(uint32_t), buf);
  503. if (bytes != size * sizeof(uint32_t)) {
  504. error = "Write failed";
  505. break;
  506. }
  507. }
  508. memset(buf, 0, size * sizeof(uint32_t));
  509. bytes = pcilib_read_dma(ctx->pcilib, readid, addr, size * sizeof(uint32_t), buf);
  510. gettimeofday(&cur, NULL);
  511. us += ((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec));
  512. if (bytes != size * sizeof(uint32_t)) {
  513. printf("RF: %li %li\n", bytes, size * 4);
  514. error = "Read failed";
  515. break;
  516. }
  517. if (direction == PCILIB_DMA_BIDIRECTIONAL) {
  518. res = memcmp(buf, cmp, size * sizeof(uint32_t));
  519. if (res) {
  520. error = "Written and read values does not match";
  521. break;
  522. }
  523. }
  524. }
  525. // Stop Generators and drain data if necessary
  526. nwl_read_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  527. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  528. nwl_write_register(val, ctx, ctx->base_addr, TX_CONFIG_ADDRESS);
  529. nwl_read_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  530. val = ~(LOOPBACK|PKTCHKR|PKTGENR);
  531. nwl_write_register(val, ctx, ctx->base_addr, RX_CONFIG_ADDRESS);
  532. __sync_synchronize();
  533. if (direction == PCILIB_DMA_FROM_DEVICE) {
  534. pcilib_skip_dma(ctx->pcilib, readid);
  535. }
  536. free(cmp);
  537. free(buf);
  538. return error?-1:(1. * size * sizeof(uint32_t) * iterations * 1000000) / (1024. * 1024. * us);
  539. }