ipe.c 18 KB

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