ipe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #define _PCILIB_DMA_IPE_C
  2. #define _BSD_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sched.h>
  8. #include <sys/time.h>
  9. #include <arpa/inet.h>
  10. #include "pci.h"
  11. #include "pcilib.h"
  12. #include "error.h"
  13. #include "tools.h"
  14. #include "debug.h"
  15. #include "ipe.h"
  16. #include "ipe_private.h"
  17. pcilib_dma_context_t *dma_ipe_init(pcilib_t *pcilib, const char *model, const void *arg) {
  18. // int err = 0;
  19. const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
  20. ipe_dma_t *ctx = malloc(sizeof(ipe_dma_t));
  21. if (ctx) {
  22. memset(ctx, 0, sizeof(ipe_dma_t));
  23. ctx->dmactx.pcilib = pcilib;
  24. #ifdef IPEDMA_64BIT_MODE
  25. // Always supported and we need to use it
  26. ctx->mode64 = 1;
  27. #endif /* IPEDMA_64BIT_MODE */
  28. pcilib_register_bank_t dma_bank = pcilib_find_register_bank_by_addr(pcilib, PCILIB_REGISTER_BANK_DMA);
  29. if (dma_bank == PCILIB_REGISTER_BANK_INVALID) {
  30. free(ctx);
  31. pcilib_error("DMA Register Bank could not be found");
  32. return NULL;
  33. }
  34. ctx->dma_bank = model_info->banks + dma_bank;
  35. ctx->base_addr = pcilib_resolve_register_address(pcilib, ctx->dma_bank->bar, ctx->dma_bank->read_addr);
  36. }
  37. return (pcilib_dma_context_t*)ctx;
  38. }
  39. void dma_ipe_free(pcilib_dma_context_t *vctx) {
  40. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  41. if (ctx) {
  42. dma_ipe_stop(vctx, PCILIB_DMA_ENGINE_ALL, PCILIB_DMA_FLAGS_DEFAULT);
  43. free(ctx);
  44. }
  45. }
  46. int dma_ipe_start(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags) {
  47. size_t i;
  48. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  49. #ifndef IPEDMA_TLP_SIZE
  50. const pcilib_pcie_link_info_t *link_info;
  51. #endif /* ! IPEDMA_TLP_SIZE */
  52. int preserve = 0;
  53. pcilib_kmem_flags_t kflags;
  54. pcilib_kmem_reuse_state_t reuse_desc, reuse_pages;
  55. volatile void *desc_va;
  56. volatile uint32_t *last_written_addr_ptr;
  57. pcilib_register_value_t value;
  58. int tlp_size;
  59. uint32_t address64;
  60. if (dma == PCILIB_DMA_ENGINE_INVALID) return 0;
  61. else if (dma > 1) return PCILIB_ERROR_INVALID_BANK;
  62. if (!ctx->started) ctx->started = 1;
  63. if (flags&PCILIB_DMA_FLAG_PERSISTENT) ctx->preserve = 1;
  64. if (ctx->pages) return 0;
  65. kflags = PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_EXCLUSIVE|PCILIB_KMEM_FLAG_HARDWARE|(ctx->preserve?PCILIB_KMEM_FLAG_PERSISTENT:0);
  66. pcilib_kmem_handle_t *desc = pcilib_alloc_kernel_memory(ctx->dmactx.pcilib, PCILIB_KMEM_TYPE_CONSISTENT, 1, IPEDMA_DESCRIPTOR_SIZE, IPEDMA_DESCRIPTOR_ALIGNMENT, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_RING, 0x00), kflags);
  67. pcilib_kmem_handle_t *pages = pcilib_alloc_kernel_memory(ctx->dmactx.pcilib, PCILIB_KMEM_TYPE_DMA_C2S_PAGE, IPEDMA_DMA_PAGES, 0, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x00), kflags);
  68. if (!desc||!pages) {
  69. if (pages) pcilib_free_kernel_memory(ctx->dmactx.pcilib, pages, 0);
  70. if (desc) pcilib_free_kernel_memory(ctx->dmactx.pcilib, desc, 0);
  71. return PCILIB_ERROR_MEMORY;
  72. }
  73. reuse_desc = pcilib_kmem_is_reused(ctx->dmactx.pcilib, desc);
  74. reuse_pages = pcilib_kmem_is_reused(ctx->dmactx.pcilib, pages);
  75. if (reuse_desc == reuse_pages) {
  76. if (reuse_desc & PCILIB_KMEM_REUSE_PARTIAL) pcilib_warning("Inconsistent DMA buffers are found (only part of required buffers is available), reinitializing...");
  77. else if (reuse_desc & PCILIB_KMEM_REUSE_REUSED) {
  78. if ((reuse_desc & PCILIB_KMEM_REUSE_PERSISTENT) == 0) pcilib_warning("Lost DMA buffers are found (non-persistent mode), reinitializing...");
  79. else if ((reuse_desc & PCILIB_KMEM_REUSE_HARDWARE) == 0) pcilib_warning("Lost DMA buffers are found (missing HW reference), reinitializing...");
  80. else {
  81. #ifndef IPEDMA_BUG_DMARD
  82. # ifndef IPEDMA_STREAMING_MODE
  83. RD(IPEDMA_REG_PAGE_COUNT, value);
  84. if (value != IPEDMA_DMA_PAGES) pcilib_warning("Inconsistent DMA buffers are found (Number of allocated buffers (%lu) does not match current request (%lu)), reinitializing...", value + 1, IPEDMA_DMA_PAGES);
  85. else
  86. # endif /* IPEDMA_STREAMING_MODE */
  87. #endif /* IPEDMA_BUG_DMARD */
  88. preserve = 1;
  89. }
  90. }
  91. } else pcilib_warning("Inconsistent DMA buffers (modes of ring and page buffers does not match), reinitializing....");
  92. desc_va = pcilib_kmem_get_ua(ctx->dmactx.pcilib, desc);
  93. if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  94. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  95. if (preserve) {
  96. ctx->reused = 1;
  97. ctx->preserve = 1;
  98. // Detect the current state of DMA engine
  99. #ifdef IPEDMA_BUG_DMARD
  100. FILE *f = fopen("/tmp/pcitool_lastread", "r");
  101. if (!f) pcilib_error("Can't read current status");
  102. fread(&value, 1, sizeof(pcilib_register_value_t), f);
  103. fclose(f);
  104. #else /* IPEDMA_BUG_DMARD */
  105. RD(IPEDMA_REG_LAST_READ, value);
  106. // Numbered from 1 in FPGA
  107. # ifdef IPEDMA_BUG_LAST_READ
  108. if (value == IPEDMA_DMA_PAGES)
  109. value = 0;
  110. # else /* IPEDMA_BUG_LAST_READ */
  111. value--;
  112. # endif /* IPEDMA_BUG_LAST_READ */
  113. #endif /* IPEDMA_BUG_DMARD */
  114. ctx->last_read = value;
  115. } else {
  116. ctx->reused = 0;
  117. // Disable DMA
  118. WR(IPEDMA_REG_CONTROL, 0x0);
  119. usleep(IPEDMA_RESET_DELAY);
  120. // Reset DMA engine
  121. WR(IPEDMA_REG_RESET, 0x1);
  122. usleep(IPEDMA_RESET_DELAY);
  123. WR(IPEDMA_REG_RESET, 0x0);
  124. usleep(IPEDMA_RESET_DELAY);
  125. #ifndef IPEDMA_BUG_DMARD
  126. // Verify PCIe link status
  127. RD(IPEDMA_REG_RESET, value);
  128. if ((value != 0x14031700)&&(value != 0x14021700))
  129. pcilib_warning("PCIe is not ready, code is %lx", value);
  130. #endif /* IPEDMA_BUG_DMARD */
  131. // Enable 64 bit addressing and configure TLP and PACKET sizes (40 bit mode can be used with big pre-allocated buffers later)
  132. if (ctx->mode64) address64 = 0x8000 | (0<<24);
  133. else address64 = 0;
  134. #ifdef IPEDMA_TLP_SIZE
  135. tlp_size = IPEDMA_TLP_SIZE;
  136. #else /* IPEDMA_TLP_SIZE */
  137. link_info = pcilib_get_pcie_link_info(vctx->pcilib);
  138. if (link_info) {
  139. tlp_size = 1<<link_info->payload;
  140. if (tlp_size > IPEDMA_MAX_TLP_SIZE)
  141. tlp_size = IPEDMA_MAX_TLP_SIZE;
  142. } else tlp_size = 128;
  143. #endif /* IPEDMA_TLP_SIZE */
  144. WR(IPEDMA_REG_TLP_SIZE, address64 | (tlp_size>>2));
  145. WR(IPEDMA_REG_TLP_COUNT, IPEDMA_PAGE_SIZE / (tlp_size * IPEDMA_CORES));
  146. // Setting progress register threshold
  147. WR(IPEDMA_REG_UPDATE_THRESHOLD, IPEDMA_DMA_PROGRESS_THRESHOLD);
  148. // Reseting configured DMA pages
  149. WR(IPEDMA_REG_PAGE_COUNT, 0);
  150. // Setting current read position and configuring progress register
  151. #ifdef IPEDMA_BUG_LAST_READ
  152. WR(IPEDMA_REG_LAST_READ, IPEDMA_DMA_PAGES - 1);
  153. #else /* IPEDMA_BUG_LAST_READ */
  154. WR(IPEDMA_REG_LAST_READ, IPEDMA_DMA_PAGES);
  155. #endif /* IPEDMA_BUG_LAST_READ */
  156. WR(IPEDMA_REG_UPDATE_ADDR, pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, desc, 0));
  157. // Instructing DMA engine that writting should start from the first DMA page
  158. *last_written_addr_ptr = 0;
  159. for (i = 0; i < IPEDMA_DMA_PAGES; i++) {
  160. uintptr_t bus_addr_check, bus_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, pages, i);
  161. WR(IPEDMA_REG_PAGE_ADDR, bus_addr);
  162. if (bus_addr%4096) printf("Bad address %lu: %lx\n", i, bus_addr);
  163. RD(IPEDMA_REG_PAGE_ADDR, bus_addr_check);
  164. if (bus_addr_check != bus_addr) {
  165. pcilib_error("Written (%x) and read (%x) bus addresses does not match\n", bus_addr, bus_addr_check);
  166. }
  167. usleep(IPEDMA_ADD_PAGE_DELAY);
  168. }
  169. // Enable DMA
  170. WR(IPEDMA_REG_CONTROL, 0x1);
  171. ctx->last_read = IPEDMA_DMA_PAGES - 1;
  172. #ifdef IPEDMA_BUG_DMARD
  173. FILE *f = fopen("/tmp/pcitool_lastread", "w");
  174. if (!f) pcilib_error("Can't write current status");
  175. value = ctx->last_read;
  176. fwrite(&value, 1, sizeof(pcilib_register_value_t), f);
  177. fclose(f);
  178. #endif /* IPEDMA_BUG_DMARD */
  179. }
  180. ctx->last_read_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, pages, ctx->last_read);
  181. ctx->desc = desc;
  182. ctx->pages = pages;
  183. ctx->page_size = pcilib_kmem_get_block_size(ctx->dmactx.pcilib, pages, 0);
  184. ctx->ring_size = IPEDMA_DMA_PAGES;
  185. return 0;
  186. }
  187. int dma_ipe_stop(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags) {
  188. pcilib_kmem_flags_t kflags;
  189. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  190. if (!ctx->started) return 0;
  191. if ((dma != PCILIB_DMA_ENGINE_INVALID)&&(dma > 1)) return PCILIB_ERROR_INVALID_BANK;
  192. // ignoring previous setting if flag specified
  193. if (flags&PCILIB_DMA_FLAG_PERSISTENT) {
  194. ctx->preserve = 0;
  195. }
  196. if (ctx->preserve) {
  197. kflags = PCILIB_KMEM_FLAG_REUSE;
  198. } else {
  199. kflags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT;
  200. ctx->started = 0;
  201. // Disable DMA
  202. WR(IPEDMA_REG_CONTROL, 0);
  203. usleep(IPEDMA_RESET_DELAY);
  204. // Reset DMA engine
  205. WR(IPEDMA_REG_RESET, 0x1);
  206. usleep(IPEDMA_RESET_DELAY);
  207. WR(IPEDMA_REG_RESET, 0x0);
  208. usleep(IPEDMA_RESET_DELAY);
  209. // Reseting configured DMA pages
  210. WR(IPEDMA_REG_PAGE_COUNT, 0);
  211. usleep(IPEDMA_RESET_DELAY);
  212. }
  213. // Clean buffers
  214. if (ctx->desc) {
  215. pcilib_free_kernel_memory(ctx->dmactx.pcilib, ctx->desc, kflags);
  216. ctx->desc = NULL;
  217. }
  218. if (ctx->pages) {
  219. pcilib_free_kernel_memory(ctx->dmactx.pcilib, ctx->pages, kflags);
  220. ctx->pages = NULL;
  221. }
  222. return 0;
  223. }
  224. static size_t dma_ipe_find_buffer_by_bus_addr(ipe_dma_t *ctx, uintptr_t bus_addr) {
  225. size_t i;
  226. for (i = 0; i < ctx->ring_size; i++) {
  227. uintptr_t buf_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, i);
  228. if (bus_addr == buf_addr)
  229. return i;
  230. }
  231. return (size_t)-1;
  232. }
  233. int dma_ipe_get_status(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dma_engine_status_t *status, size_t n_buffers, pcilib_dma_buffer_status_t *buffers) {
  234. size_t i;
  235. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  236. void *desc_va = (void*)pcilib_kmem_get_ua(ctx->dmactx.pcilib, ctx->desc);
  237. volatile uint32_t *last_written_addr_ptr;
  238. uint32_t last_written_addr;
  239. if (!status) return -1;
  240. if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  241. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  242. pcilib_debug(DMA, "Current DMA status - last read: %4u, last_read_addr: %4u (0x%x), last_written: %4u (0x%x)", ctx->last_read,
  243. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  244. dma_ipe_find_buffer_by_bus_addr(ctx, *last_written_addr_ptr), *last_written_addr_ptr
  245. );
  246. last_written_addr = *last_written_addr_ptr;
  247. status->started = ctx->started;
  248. status->ring_size = ctx->ring_size;
  249. status->buffer_size = ctx->page_size;
  250. status->written_buffers = 0;
  251. status->written_bytes = 0;
  252. // For simplicity, we keep last_read here, and fix in the end
  253. status->ring_tail = ctx->last_read;
  254. status->ring_head = dma_ipe_find_buffer_by_bus_addr(ctx, last_written_addr);
  255. if (status->ring_head == (size_t)-1) {
  256. if (last_written_addr) {
  257. pcilib_warning("DMA is in unknown state, last_written_addr does not correspond any of available buffers");
  258. return PCILIB_ERROR_FAILED;
  259. }
  260. status->ring_head = 0;
  261. status->ring_tail = 0;
  262. }
  263. if (n_buffers > ctx->ring_size) n_buffers = ctx->ring_size;
  264. if (buffers)
  265. memset(buffers, 0, n_buffers * sizeof(pcilib_dma_buffer_status_t));
  266. if (status->ring_head >= status->ring_tail) {
  267. for (i = status->ring_tail + 1; i <= status->ring_head; i++) {
  268. status->written_buffers++;
  269. status->written_bytes += ctx->page_size;
  270. if ((buffers)&&(i < n_buffers)) {
  271. buffers[i].used = 1;
  272. buffers[i].size = ctx->page_size;
  273. buffers[i].first = 1;
  274. buffers[i].last = 1;
  275. }
  276. }
  277. } else {
  278. for (i = 0; i <= status->ring_head; i++) {
  279. status->written_buffers++;
  280. status->written_bytes += ctx->page_size;
  281. if ((buffers)&&(i < n_buffers)) {
  282. buffers[i].used = 1;
  283. buffers[i].size = ctx->page_size;
  284. buffers[i].first = 1;
  285. buffers[i].last = 1;
  286. }
  287. }
  288. for (i = status->ring_tail + 1; i < status->ring_size; i++) {
  289. status->written_buffers++;
  290. status->written_bytes += ctx->page_size;
  291. if ((buffers)&&(i < n_buffers)) {
  292. buffers[i].used = 1;
  293. buffers[i].size = ctx->page_size;
  294. buffers[i].first = 1;
  295. buffers[i].last = 1;
  296. }
  297. }
  298. }
  299. // We actually keep last_read in the ring_tail, so need to increase
  300. if (status->ring_tail != status->ring_head) {
  301. status->ring_tail++;
  302. if (status->ring_tail == status->ring_size) status->ring_tail = 0;
  303. }
  304. return 0;
  305. }
  306. int dma_ipe_stream_read(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, uintptr_t addr, size_t size, pcilib_dma_flags_t flags, pcilib_timeout_t timeout, pcilib_dma_callback_t cb, void *cbattr) {
  307. int err, ret = PCILIB_STREAMING_REQ_PACKET;
  308. pcilib_timeout_t wait = 0;
  309. struct timeval start, cur;
  310. volatile void *desc_va;
  311. volatile uint32_t *last_written_addr_ptr;
  312. volatile uint32_t *empty_detected_ptr;
  313. pcilib_dma_flags_t packet_flags = PCILIB_DMA_FLAG_EOP;
  314. size_t nodata_sleep;
  315. #ifdef IPEDMA_BUG_DMARD
  316. pcilib_register_value_t value;
  317. #endif /* IPEDMA_BUG_DMARD */
  318. switch (sched_getscheduler(0)) {
  319. case SCHED_FIFO:
  320. case SCHED_RR:
  321. nodata_sleep = IPEDMA_NODATA_SLEEP;
  322. break;
  323. default:
  324. pcilib_info_once("Streaming DMA data using non real-time thread (may cause extra CPU load)", errno);
  325. nodata_sleep = 0;
  326. }
  327. size_t cur_read;
  328. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  329. err = dma_ipe_start(vctx, dma, PCILIB_DMA_FLAGS_DEFAULT);
  330. if (err) return err;
  331. desc_va = (void*)pcilib_kmem_get_ua(ctx->dmactx.pcilib, ctx->desc);
  332. if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  333. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  334. empty_detected_ptr = last_written_addr_ptr - 2;
  335. do {
  336. switch (ret&PCILIB_STREAMING_TIMEOUT_MASK) {
  337. case PCILIB_STREAMING_CONTINUE:
  338. // Hardware indicates that there is no more data pending and we can safely stop if there is no data in the kernel buffers already
  339. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  340. if (*empty_detected_ptr)
  341. wait = 0;
  342. else
  343. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  344. wait = IPEDMA_DMA_TIMEOUT;
  345. break;
  346. case PCILIB_STREAMING_WAIT:
  347. wait = (timeout > IPEDMA_DMA_TIMEOUT)?timeout:IPEDMA_DMA_TIMEOUT;
  348. break;
  349. // case PCILIB_STREAMING_CHECK: wait = 0; break;
  350. }
  351. pcilib_debug(DMA, "Waiting for data in %4u - last_read: %4u, last_read_addr: %4u (0x%08x), last_written: %4u (0x%08x)", ctx->last_read + 1, ctx->last_read,
  352. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  353. dma_ipe_find_buffer_by_bus_addr(ctx, *last_written_addr_ptr), *last_written_addr_ptr
  354. );
  355. gettimeofday(&start, NULL);
  356. memcpy(&cur, &start, sizeof(struct timeval));
  357. while (((*last_written_addr_ptr == 0)||(ctx->last_read_addr == (*last_written_addr_ptr)))&&((wait == PCILIB_TIMEOUT_INFINITE)||(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < wait))) {
  358. if (nodata_sleep)
  359. usleep(nodata_sleep);
  360. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  361. if ((ret != PCILIB_STREAMING_REQ_PACKET)&&(*empty_detected_ptr)) break;
  362. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  363. gettimeofday(&cur, NULL);
  364. }
  365. // Failing out if we exited on timeout
  366. if ((ctx->last_read_addr == (*last_written_addr_ptr))||(*last_written_addr_ptr == 0)) {
  367. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  368. # ifdef PCILIB_DEBUG_DMA
  369. if ((wait)&&(*last_written_addr_ptr)&&(!*empty_detected_ptr))
  370. pcilib_debug(DMA, "The empty_detected flag is not set, but no data arrived within %lu us", wait);
  371. # endif /* PCILIB_DEBUG_DMA */
  372. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  373. return (ret&PCILIB_STREAMING_FAIL)?PCILIB_ERROR_TIMEOUT:0;
  374. }
  375. // Getting next page to read
  376. cur_read = ctx->last_read + 1;
  377. if (cur_read == ctx->ring_size) cur_read = 0;
  378. pcilib_debug(DMA, "Got buffer %4u - last read: %4u, last_read_addr: %4u (0x%x), last_written: %4u (0x%x)", cur_read, ctx->last_read,
  379. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  380. dma_ipe_find_buffer_by_bus_addr(ctx, *last_written_addr_ptr), *last_written_addr_ptr
  381. );
  382. #ifdef IPEDMA_DETECT_PACKETS
  383. if ((*empty_detected_ptr)&&(pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, cur_read) == (*last_written_addr_ptr))) packet_flags = PCILIB_DMA_FLAG_EOP;
  384. else packet_flags = 0;
  385. #endif /* IPEDMA_DETECT_PACKETS */
  386. pcilib_kmem_sync_block(ctx->dmactx.pcilib, ctx->pages, PCILIB_KMEM_SYNC_FROMDEVICE, cur_read);
  387. void *buf = pcilib_kmem_get_block_ua(ctx->dmactx.pcilib, ctx->pages, cur_read);
  388. ret = cb(cbattr, packet_flags, ctx->page_size, buf);
  389. if (ret < 0) return -ret;
  390. // We don't need this because hardware does not intend to read anything from the memory
  391. // pcilib_kmem_sync_block(ctx->dmactx.pcilib, ctx->pages, PCILIB_KMEM_SYNC_TODEVICE, cur_read);
  392. // Return buffer into the DMA pool when processed
  393. #ifdef IPEDMA_STREAMING_MODE
  394. uintptr_t buf_ba = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, cur_read);
  395. WR(IPEDMA_REG_PAGE_ADDR, buf_ba);
  396. # ifdef IPEDMA_STREAMING_CHECKS
  397. pcilib_register_value_t streaming_status;
  398. RD(IPEDMA_REG_STREAMING_STATUS, streaming_status);
  399. if (streaming_status)
  400. pcilib_error("Invalid status (0x%lx) adding a DMA buffer into the queue", streaming_status);
  401. # endif /* IPEDMA_STREAMING_MODE */
  402. #endif /* IPEDMA_STREAMING_MODE */
  403. // Numbered from 1
  404. #ifdef IPEDMA_BUG_LAST_READ
  405. WR(IPEDMA_REG_LAST_READ, cur_read?cur_read:IPEDMA_DMA_PAGES);
  406. #else /* IPEDMA_BUG_LAST_READ */
  407. WR(IPEDMA_REG_LAST_READ, cur_read + 1);
  408. #endif /* IPEDMA_BUG_LAST_READ */
  409. pcilib_debug(DMA, "Buffer returned %4u - last read: %4u, last_read_addr: %4u (0x%x), last_written: %4u (0x%x)", cur_read, ctx->last_read,
  410. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  411. dma_ipe_find_buffer_by_bus_addr(ctx, *last_written_addr_ptr), *last_written_addr_ptr
  412. );
  413. ctx->last_read = cur_read;
  414. ctx->last_read_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, cur_read);
  415. #ifdef IPEDMA_BUG_DMARD
  416. FILE *f = fopen("/tmp/pcitool_lastread", "w");
  417. if (!f) pcilib_error("Can't write current status");
  418. value = cur_read;
  419. fwrite(&value, 1, sizeof(pcilib_register_value_t), f);
  420. fclose(f);
  421. #endif /* IPEDMA_BUG_DMARD */
  422. } while (ret);
  423. return 0;
  424. }