ipe.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #define _PCILIB_DMA_IPE_C
  2. #define _BSD_SOURCE
  3. #define _DEFAULT_SOURCE
  4. #define _POSIX_C_SOURCE 199309L
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sched.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <arpa/inet.h>
  13. #include "pci.h"
  14. #include "pcilib.h"
  15. #include "error.h"
  16. #include "tools.h"
  17. #include "debug.h"
  18. #include "bar.h"
  19. #include "ipe.h"
  20. #include "ipe_private.h"
  21. pcilib_dma_context_t *dma_ipe_init(pcilib_t *pcilib, const char *model, const void *arg) {
  22. int err = 0;
  23. pcilib_register_value_t version_value;
  24. // const pcilib_model_description_t *model_info = pcilib_get_model_description(pcilib);
  25. ipe_dma_t *ctx = malloc(sizeof(ipe_dma_t));
  26. if (ctx) {
  27. memset(ctx, 0, sizeof(ipe_dma_t));
  28. ctx->dmactx.pcilib = pcilib;
  29. pcilib_register_bank_t dma_bankc = pcilib_find_register_bank_by_addr(pcilib, PCILIB_REGISTER_BANK_DMACONF);
  30. pcilib_register_bank_t dma_bank0 = pcilib_find_register_bank_by_addr(pcilib, PCILIB_REGISTER_BANK_DMA0);
  31. pcilib_register_bank_t dma_bank1 = pcilib_find_register_bank_by_addr(pcilib, PCILIB_REGISTER_BANK_DMA1);
  32. if ((dma_bankc == PCILIB_REGISTER_BANK_INVALID)||(dma_bank0 == PCILIB_REGISTER_BANK_INVALID)||(dma_bank1 == PCILIB_REGISTER_BANK_INVALID)) {
  33. free(ctx);
  34. pcilib_error("DMA Register Bank could not be found");
  35. return NULL;
  36. }
  37. ctx->base_addr[0] = (void*)pcilib_resolve_bank_address_by_id(pcilib, 0, dma_bank0);
  38. ctx->base_addr[1] = (void*)pcilib_resolve_bank_address_by_id(pcilib, 0, dma_bank1);
  39. ctx->base_addr[2] = (void*)pcilib_resolve_bank_address_by_id(pcilib, 0, dma_bankc);
  40. RD(IPEDMA_REG_VERSION, version_value);
  41. ctx->version = IPEDMA_VERSION(version_value);
  42. if ((model)&&(!strcasecmp(model, "ipecamera"))) {
  43. if (IPEDMA_GENERATION(version_value) > 2) {
  44. ctx->gen = 3;
  45. } else {
  46. ctx->gen = 2;
  47. }
  48. } else {
  49. if (IPEDMA_GENERATION(version_value) > 2) {
  50. ctx->gen = 3;
  51. } else {
  52. ctx->gen = 2;
  53. }
  54. err = pcilib_add_registers(pcilib, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, ipe_dma_app_registers, NULL);
  55. }
  56. if (ctx->gen > 2) {
  57. ctx->mode64 = 1;
  58. ctx->addr64 = 1;
  59. #ifdef IPEDMA_STREAMING_MODE
  60. if (IPEDMA_STREAMING(version_value)) ctx->streaming = 1;
  61. #endif /* IPEDMA_STREAMING_MODE */
  62. ctx->reg_last_read = IPEDMA_REG3_LAST_READ;
  63. if (!err)
  64. err = pcilib_add_registers(pcilib, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, ipe_dma_v3_registers, NULL);
  65. } else {
  66. #ifdef IPEDMA_ENFORCE_64BIT_MODE
  67. // According to Lorenzo, some gen2 boards have problems with 64-bit addressing. Therefore, we only enable it for gen3 boards unless enforced
  68. ctx->mode64 = 1;
  69. #endif /* IPEDMA_ENFORCE_64BIT_MODE */
  70. ctx->addr64 = 0;
  71. ctx->streaming = 0;
  72. ctx->reg_last_read = IPEDMA_REG2_LAST_READ;
  73. if (!err)
  74. err = pcilib_add_registers(pcilib, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, ipe_dma_v2_registers, NULL);
  75. }
  76. pcilib_info("IPEDMA gen%lu version %lu (64-bit mode: %u, 64-bit addressing: %u, streaming: %u)", ctx->gen, ctx->version, ctx->mode64, ctx->addr64, ctx->streaming);
  77. if (err) {
  78. free(ctx);
  79. pcilib_error("Error (%i) registering firmware-dependent IPEDMA registers", err);
  80. return NULL;
  81. }
  82. }
  83. return (pcilib_dma_context_t*)ctx;
  84. }
  85. void dma_ipe_free(pcilib_dma_context_t *vctx) {
  86. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  87. if (ctx) {
  88. dma_ipe_stop(vctx, PCILIB_DMA_ENGINE_ALL, PCILIB_DMA_FLAGS_DEFAULT);
  89. free(ctx);
  90. }
  91. }
  92. static void dma_ipe_disable(ipe_dma_t *ctx) {
  93. // Disable DMA
  94. WR(IPEDMA_REG_CONTROL, 0x0);
  95. usleep(IPEDMA_RESET_DELAY);
  96. // Reset DMA engine
  97. WR(IPEDMA_REG_RESET, 0x1);
  98. usleep(IPEDMA_RESET_DELAY);
  99. WR(IPEDMA_REG_RESET, 0x0);
  100. usleep(IPEDMA_RESET_DELAY);
  101. // Reseting configured DMA pages
  102. if (ctx->gen < 3) {
  103. WR(IPEDMA_REG2_PAGE_COUNT, 0);
  104. }
  105. usleep(IPEDMA_RESET_DELAY);
  106. }
  107. int dma_ipe_start(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags) {
  108. int err;
  109. int mask = 32;
  110. size_t i, num_pages;
  111. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  112. pcilib_kmem_handle_t *desc = NULL;
  113. pcilib_kmem_handle_t *pages = NULL;
  114. #ifndef IPEDMA_TLP_SIZE
  115. const pcilib_pcie_link_info_t *link_info;
  116. #endif /* ! IPEDMA_TLP_SIZE */
  117. int preserve = 0;
  118. pcilib_kmem_flags_t kflags;
  119. pcilib_kmem_reuse_state_t reuse_desc, reuse_pages;
  120. volatile void *desc_va;
  121. volatile void *last_written_addr_ptr;
  122. pcilib_register_value_t value;
  123. uintptr_t dma_region = 0;
  124. int tlp_size;
  125. uint32_t address64;
  126. if (dma == PCILIB_DMA_ENGINE_INVALID) return 0;
  127. else if (dma > 1) return PCILIB_ERROR_INVALID_BANK;
  128. if (!ctx->started) ctx->started = 1;
  129. if (flags&PCILIB_DMA_FLAG_PERSISTENT) ctx->preserve = 1;
  130. if (ctx->pages) return 0;
  131. #ifdef IPEDMA_TLP_SIZE
  132. tlp_size = IPEDMA_TLP_SIZE;
  133. #else /* IPEDMA_TLP_SIZE */
  134. link_info = pcilib_get_pcie_link_info(vctx->pcilib);
  135. if (link_info) {
  136. tlp_size = 1<<link_info->payload;
  137. # ifdef IPEDMA_MAX_TLP_SIZE
  138. if (tlp_size > IPEDMA_MAX_TLP_SIZE)
  139. tlp_size = IPEDMA_MAX_TLP_SIZE;
  140. # endif /* IPEDMA_MAX_TLP_SIZE */
  141. } else tlp_size = 128;
  142. #endif /* IPEDMA_TLP_SIZE */
  143. if (!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "dma_timeout", &value))
  144. ctx->dma_timeout = value;
  145. else
  146. ctx->dma_timeout = IPEDMA_DMA_TIMEOUT;
  147. if (!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "dma_page_size", &value)) {
  148. if (value % IPEDMA_PAGE_SIZE) {
  149. pcilib_error("Invalid DMA page size (%lu) is configured", value);
  150. return PCILIB_ERROR_INVALID_ARGUMENT;
  151. }
  152. //if ((value)&&((value / (tlp_size * IPEDMA_CORES)) > ...seems no limit...)) { ... fail ... }
  153. ctx->page_size = value;
  154. } else
  155. ctx->page_size = IPEDMA_PAGE_SIZE;
  156. if ((!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "dma_pages", &value))&&(value > 0))
  157. ctx->ring_size = value;
  158. else
  159. ctx->ring_size = IPEDMA_DMA_PAGES;
  160. if (!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "dma_region_low", &value)) {
  161. dma_region = value;
  162. if (!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "dma_region_low", &value))
  163. dma_region |= ((uintptr_t)value)<<32;
  164. }
  165. if (!pcilib_read_register(ctx->dmactx.pcilib, "dmaconf", "ipedma_flags", &value))
  166. ctx->dma_flags = value;
  167. else
  168. ctx->dma_flags = 0;
  169. #ifdef IPEDMA_CONFIGURE_DMA_MASK
  170. if (ctx->addr64) mask = 64;
  171. err = pcilib_set_dma_mask(ctx->dmactx.pcilib, mask);
  172. if (err) {
  173. pcilib_error("Error (%i) configuring dma mask (%i)", err, mask);
  174. return err;
  175. }
  176. #endif /* IPEDMA_CONFIGURE_DMA_MASK */
  177. kflags = PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_EXCLUSIVE|PCILIB_KMEM_FLAG_HARDWARE|(ctx->preserve?PCILIB_KMEM_FLAG_PERSISTENT:0);
  178. 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);
  179. if (dma_region)
  180. pages = pcilib_alloc_kernel_memory(ctx->dmactx.pcilib, PCILIB_KMEM_TYPE_REGION_C2S, ctx->ring_size, ctx->page_size, dma_region, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x00), kflags);
  181. else
  182. pages = pcilib_alloc_kernel_memory(ctx->dmactx.pcilib, PCILIB_KMEM_TYPE_DMA_C2S_PAGE, ctx->ring_size, ctx->page_size, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x00), kflags);
  183. if (!desc||!pages) {
  184. if (pages) pcilib_free_kernel_memory(ctx->dmactx.pcilib, pages, KMEM_FLAG_REUSE);
  185. if (desc) pcilib_free_kernel_memory(ctx->dmactx.pcilib, desc, KMEM_FLAG_REUSE);
  186. pcilib_error("Can't allocate required kernel memory for IPEDMA engine (%lu pages of %lu bytes + %lu byte descriptor)", ctx->ring_size, ctx->page_size, (unsigned long)IPEDMA_DESCRIPTOR_SIZE);
  187. return PCILIB_ERROR_MEMORY;
  188. }
  189. reuse_desc = pcilib_kmem_is_reused(ctx->dmactx.pcilib, desc);
  190. reuse_pages = pcilib_kmem_is_reused(ctx->dmactx.pcilib, pages);
  191. if ((reuse_pages & PCILIB_KMEM_REUSE_PARTIAL)||(reuse_desc & PCILIB_KMEM_REUSE_PARTIAL)) {
  192. dma_ipe_disable(ctx);
  193. pcilib_free_kernel_memory(ctx->dmactx.pcilib, pages, KMEM_FLAG_REUSE);
  194. pcilib_free_kernel_memory(ctx->dmactx.pcilib, desc, KMEM_FLAG_REUSE);
  195. if (((flags&PCILIB_DMA_FLAG_STOP) == 0)||(dma_region)) {
  196. pcilib_error("Inconsistent DMA buffers are found (buffers are only partially re-used). This is very wrong, please stop DMA engine and correct configuration...");
  197. return PCILIB_ERROR_INVALID_STATE;
  198. }
  199. pcilib_warning("Inconsistent DMA buffers are found (buffers are only partially re-used), reinitializing...");
  200. 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|PCILIB_KMEM_FLAG_MASS);
  201. pages = pcilib_alloc_kernel_memory(ctx->dmactx.pcilib, PCILIB_KMEM_TYPE_DMA_C2S_PAGE, ctx->ring_size, ctx->page_size, 0, PCILIB_KMEM_USE(PCILIB_KMEM_USE_DMA_PAGES, 0x00), kflags|PCILIB_KMEM_FLAG_MASS);
  202. if (!desc||!pages) {
  203. if (pages) pcilib_free_kernel_memory(ctx->dmactx.pcilib, pages, KMEM_FLAG_REUSE);
  204. if (desc) pcilib_free_kernel_memory(ctx->dmactx.pcilib, desc, KMEM_FLAG_REUSE);
  205. return PCILIB_ERROR_MEMORY;
  206. }
  207. } else if (reuse_desc != reuse_pages) {
  208. pcilib_warning("Inconsistent DMA buffers (modes of ring and page buffers does not match), reinitializing....");
  209. } else if (reuse_desc & PCILIB_KMEM_REUSE_REUSED) {
  210. if ((reuse_desc & PCILIB_KMEM_REUSE_PERSISTENT) == 0) pcilib_warning("Lost DMA buffers are found (non-persistent mode), reinitializing...");
  211. else if ((reuse_desc & PCILIB_KMEM_REUSE_HARDWARE) == 0) pcilib_warning("Lost DMA buffers are found (missing HW reference), reinitializing...");
  212. else {
  213. if (ctx->streaming)
  214. preserve = 1;
  215. else {
  216. RD(IPEDMA_REG2_PAGE_COUNT, value);
  217. if (value != ctx->ring_size)
  218. pcilib_warning("Inconsistent DMA buffers are found (Number of allocated buffers (%lu) does not match current request (%lu)), reinitializing...", value + 1, IPEDMA_DMA_PAGES);
  219. else
  220. preserve = 1;
  221. }
  222. }
  223. }
  224. desc_va = pcilib_kmem_get_ua(ctx->dmactx.pcilib, desc);
  225. if (ctx->addr64) last_written_addr_ptr = desc_va + 2 * sizeof(uint32_t);
  226. else if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  227. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  228. // get page size if default size was used
  229. if (!ctx->page_size) {
  230. ctx->page_size = pcilib_kmem_get_block_size(ctx->dmactx.pcilib, pages, 0);
  231. }
  232. if (preserve) {
  233. ctx->reused = 1;
  234. ctx->preserve = 1;
  235. // Detect the current state of DMA engine
  236. RD(ctx->reg_last_read, value);
  237. // Numbered from 1 in FPGA
  238. # ifdef IPEDMA_BUG_LAST_READ
  239. if (value == ctx->ring_size)
  240. value = 0;
  241. # else /* IPEDMA_BUG_LAST_READ */
  242. value--;
  243. # endif /* IPEDMA_BUG_LAST_READ */
  244. ctx->last_read = value;
  245. } else {
  246. ctx->reused = 0;
  247. dma_ipe_disable(ctx);
  248. // Verify PCIe link status
  249. RD(IPEDMA_REG_RESET, value);
  250. if ((value != 0x14031700)&&(value != 0x14021700))
  251. pcilib_warning("PCIe is not ready, code is %lx", value);
  252. // Enable 64 bit addressing and configure TLP and PACKET sizes (40 bit mode can be used with big pre-allocated buffers later)
  253. if (ctx->mode64) address64 = 0x8000 | (0<<24);
  254. else address64 = 0;
  255. WR(IPEDMA_REG_TLP_SIZE, address64 | (tlp_size>>2));
  256. WR(IPEDMA_REG_TLP_COUNT, ctx->page_size / (tlp_size * IPEDMA_CORES));
  257. // Setting progress register threshold
  258. WR(IPEDMA_REG_UPDATE_THRESHOLD, IPEDMA_DMA_PROGRESS_THRESHOLD);
  259. // Reseting configured DMA pages
  260. if (ctx->gen < 3) {
  261. WR(IPEDMA_REG2_PAGE_COUNT, 0);
  262. }
  263. // Setting current read position and configuring progress register
  264. #ifdef IPEDMA_BUG_LAST_READ
  265. WR(ctx->reg_last_read, ctx->ring_size - 1);
  266. #else /* IPEDMA_BUG_LAST_READ */
  267. WR(ctx->reg_last_read, ctx->ring_size);
  268. #endif /* IPEDMA_BUG_LAST_READ */
  269. // Instructing DMA engine that writting should start from the first DMA page
  270. if (ctx->addr64) {
  271. WR64(IPEDMA_REG3_UPDATE_ADDR, pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, desc, 0));
  272. *(uint64_t*)last_written_addr_ptr = 0;
  273. } else {
  274. WR(IPEDMA_REG2_UPDATE_ADDR, pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, desc, 0));
  275. *(uint32_t*)last_written_addr_ptr = 0;
  276. }
  277. // In ring buffer mode, the hardware taking care to preserve an empty buffer to help distinguish between
  278. // completely empty and completely full cases. In streaming mode, it is our responsibility to track this
  279. // information. Therefore, we always keep the last buffer free
  280. num_pages = ctx->ring_size;
  281. if (ctx->streaming) num_pages--;
  282. for (i = 0; i < num_pages; i++) {
  283. uintptr_t bus_addr_check, bus_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, pages, i);
  284. if (ctx->addr64) {
  285. WR64(IPEDMA_REG3_PAGE_ADDR, bus_addr);
  286. } else {
  287. WR(IPEDMA_REG2_PAGE_ADDR, bus_addr);
  288. }
  289. if (bus_addr%4096) printf("Bad address %lu: %lx\n", i, bus_addr);
  290. if ((!ctx->addr64)&&(!ctx->streaming)) {
  291. RD(IPEDMA_REG2_PAGE_ADDR, bus_addr_check);
  292. if (bus_addr_check != bus_addr) {
  293. pcilib_error("Written (%x) and read (%x) bus addresses does not match\n", bus_addr, bus_addr_check);
  294. }
  295. }
  296. usleep(IPEDMA_ADD_PAGE_DELAY);
  297. }
  298. // Enable DMA
  299. WR(IPEDMA_REG_CONTROL, 0x1);
  300. ctx->last_read = ctx->ring_size - 1;
  301. }
  302. ctx->last_read_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, pages, ctx->last_read);
  303. ctx->desc = desc;
  304. ctx->pages = pages;
  305. return 0;
  306. }
  307. int dma_ipe_stop(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dma_flags_t flags) {
  308. pcilib_kmem_flags_t kflags;
  309. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  310. if (!ctx->started) return 0;
  311. if ((dma != PCILIB_DMA_ENGINE_INVALID)&&(dma > 1)) return PCILIB_ERROR_INVALID_BANK;
  312. // ignoring previous setting if flag specified
  313. if (flags&PCILIB_DMA_FLAG_PERSISTENT) {
  314. ctx->preserve = 0;
  315. }
  316. if (ctx->preserve) {
  317. kflags = PCILIB_KMEM_FLAG_REUSE;
  318. } else {
  319. kflags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT;
  320. ctx->started = 0;
  321. dma_ipe_disable(ctx);
  322. }
  323. // Clean buffers
  324. if (ctx->desc) {
  325. pcilib_free_kernel_memory(ctx->dmactx.pcilib, ctx->desc, kflags);
  326. ctx->desc = NULL;
  327. }
  328. if (ctx->pages) {
  329. pcilib_free_kernel_memory(ctx->dmactx.pcilib, ctx->pages, kflags);
  330. ctx->pages = NULL;
  331. }
  332. return 0;
  333. }
  334. static size_t dma_ipe_find_buffer_by_bus_addr(ipe_dma_t *ctx, uintptr_t bus_addr) {
  335. size_t i;
  336. for (i = 0; i < ctx->ring_size; i++) {
  337. uintptr_t buf_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, i);
  338. if (bus_addr == buf_addr)
  339. return i;
  340. }
  341. return (size_t)-1;
  342. }
  343. 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) {
  344. size_t i;
  345. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  346. void *desc_va = (void*)pcilib_kmem_get_ua(ctx->dmactx.pcilib, ctx->desc);
  347. volatile void *last_written_addr_ptr;
  348. uint64_t last_written_addr;
  349. if (!status) return -1;
  350. if (ctx->addr64) {
  351. last_written_addr_ptr = desc_va + 2 * sizeof(uint32_t);
  352. last_written_addr = *(uint64_t*)last_written_addr_ptr;
  353. } else {
  354. if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  355. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  356. last_written_addr = *(uint32_t*)last_written_addr_ptr;
  357. }
  358. pcilib_debug(DMA, "Current DMA status - last read: %4u, last_read_addr: %4u (0x%x), last_written: %4lu (0x%lx)", ctx->last_read,
  359. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  360. dma_ipe_find_buffer_by_bus_addr(ctx, last_written_addr), last_written_addr
  361. );
  362. status->started = ctx->started;
  363. status->ring_size = ctx->ring_size;
  364. status->buffer_size = ctx->page_size;
  365. status->written_buffers = 0;
  366. status->written_bytes = 0;
  367. // For simplicity, we keep last_read here, and fix in the end
  368. status->ring_tail = ctx->last_read;
  369. status->ring_head = dma_ipe_find_buffer_by_bus_addr(ctx, last_written_addr);
  370. if (status->ring_head == (size_t)-1) {
  371. if (last_written_addr) {
  372. pcilib_warning("DMA is in unknown state, last_written_addr does not correspond any of available buffers");
  373. return PCILIB_ERROR_FAILED;
  374. }
  375. status->ring_head = 0;
  376. status->ring_tail = 0;
  377. }
  378. if (n_buffers > ctx->ring_size) n_buffers = ctx->ring_size;
  379. if (buffers)
  380. memset(buffers, 0, n_buffers * sizeof(pcilib_dma_buffer_status_t));
  381. if (status->ring_head >= status->ring_tail) {
  382. for (i = status->ring_tail + 1; i <= status->ring_head; i++) {
  383. status->written_buffers++;
  384. status->written_bytes += ctx->page_size;
  385. if ((buffers)&&(i < n_buffers)) {
  386. buffers[i].used = 1;
  387. buffers[i].size = ctx->page_size;
  388. buffers[i].first = 1;
  389. buffers[i].last = 1;
  390. }
  391. }
  392. } else {
  393. for (i = 0; i <= status->ring_head; i++) {
  394. status->written_buffers++;
  395. status->written_bytes += ctx->page_size;
  396. if ((buffers)&&(i < n_buffers)) {
  397. buffers[i].used = 1;
  398. buffers[i].size = ctx->page_size;
  399. buffers[i].first = 1;
  400. buffers[i].last = 1;
  401. }
  402. }
  403. for (i = status->ring_tail + 1; i < status->ring_size; i++) {
  404. status->written_buffers++;
  405. status->written_bytes += ctx->page_size;
  406. if ((buffers)&&(i < n_buffers)) {
  407. buffers[i].used = 1;
  408. buffers[i].size = ctx->page_size;
  409. buffers[i].first = 1;
  410. buffers[i].last = 1;
  411. }
  412. }
  413. }
  414. // We actually keep last_read in the ring_tail, so need to increase
  415. if (status->ring_tail != status->ring_head) {
  416. status->ring_tail++;
  417. if (status->ring_tail == status->ring_size) status->ring_tail = 0;
  418. }
  419. return 0;
  420. }
  421. 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) {
  422. int err, ret = PCILIB_STREAMING_REQ_PACKET;
  423. pcilib_timeout_t wait = 0;
  424. struct timeval start, cur;
  425. volatile void *desc_va;
  426. volatile void *last_written_addr_ptr;
  427. // uint32_t empty_detected_dummy = 0;
  428. volatile uint32_t *empty_detected_ptr;
  429. pcilib_dma_flags_t packet_flags = PCILIB_DMA_FLAG_EOP;
  430. size_t nodata_sleep;
  431. struct timespec sleep_ts = {0};
  432. size_t cur_read;
  433. ipe_dma_t *ctx = (ipe_dma_t*)vctx;
  434. err = dma_ipe_start(vctx, dma, PCILIB_DMA_FLAGS_DEFAULT);
  435. if (err) return err;
  436. desc_va = (void*)pcilib_kmem_get_ua(ctx->dmactx.pcilib, ctx->desc);
  437. if (ctx->addr64) {
  438. last_written_addr_ptr = desc_va + 2 * sizeof(uint32_t);
  439. empty_detected_ptr = desc_va + sizeof(uint32_t);
  440. // empty_detected_ptr = &empty_detected_dummy;
  441. } else {
  442. if (ctx->mode64) last_written_addr_ptr = desc_va + 3 * sizeof(uint32_t);
  443. else last_written_addr_ptr = desc_va + 4 * sizeof(uint32_t);
  444. empty_detected_ptr = NULL; // Not properly supported
  445. // empty_detected_ptr = last_written_addr_ptr - 2;
  446. }
  447. switch (sched_getscheduler(0)) {
  448. case SCHED_FIFO:
  449. case SCHED_RR:
  450. if (ctx->dma_flags&IPEDMA_FLAG_NOSLEEP)
  451. nodata_sleep = 0;
  452. else
  453. nodata_sleep = IPEDMA_NODATA_SLEEP;
  454. break;
  455. default:
  456. pcilib_info_once("Streaming DMA data using non real-time thread (may cause extra CPU load)", errno);
  457. nodata_sleep = 0;
  458. }
  459. do {
  460. switch (ret&PCILIB_STREAMING_TIMEOUT_MASK) {
  461. case PCILIB_STREAMING_CONTINUE:
  462. // Hardware indicates that there is no more data pending and we can safely stop if there is no data in the kernel buffers already
  463. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  464. if ((empty_detected_ptr)&&(*empty_detected_ptr))
  465. wait = 0;
  466. else
  467. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  468. wait = ctx->dma_timeout;
  469. break;
  470. case PCILIB_STREAMING_WAIT:
  471. wait = (timeout > ctx->dma_timeout)?timeout:ctx->dma_timeout;
  472. break;
  473. // case PCILIB_STREAMING_CHECK: wait = 0; break;
  474. }
  475. 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,
  476. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  477. dma_ipe_find_buffer_by_bus_addr(ctx, DEREF(last_written_addr_ptr)), DEREF(last_written_addr_ptr)
  478. );
  479. gettimeofday(&start, NULL);
  480. memcpy(&cur, &start, sizeof(struct timeval));
  481. while (((DEREF(last_written_addr_ptr) == 0)||(ctx->last_read_addr == DEREF(last_written_addr_ptr)))&&((wait == PCILIB_TIMEOUT_INFINITE)||(((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) < wait))) {
  482. if (nodata_sleep) {
  483. sleep_ts.tv_nsec = nodata_sleep;
  484. nanosleep(&sleep_ts, NULL);
  485. }
  486. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  487. if ((ret != PCILIB_STREAMING_REQ_PACKET)&&(empty_detected_ptr)&&(*empty_detected_ptr)) break;
  488. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  489. gettimeofday(&cur, NULL);
  490. }
  491. // Failing out if we exited on timeout
  492. if ((ctx->last_read_addr == DEREF(last_written_addr_ptr))||(DEREF(last_written_addr_ptr) == 0)) {
  493. #ifdef IPEDMA_SUPPORT_EMPTY_DETECTED
  494. # ifdef PCILIB_DEBUG_DMA
  495. if ((wait)&&(empty_detected_ptr)&&(DEREF(last_written_addr_ptr))&&(!*empty_detected_ptr))
  496. pcilib_debug(DMA, "The empty_detected flag is not set, but no data arrived within %lu us", wait);
  497. # endif /* PCILIB_DEBUG_DMA */
  498. #endif /* IPEDMA_SUPPORT_EMPTY_DETECTED */
  499. return (ret&PCILIB_STREAMING_FAIL)?PCILIB_ERROR_TIMEOUT:0;
  500. }
  501. // Getting next page to read
  502. cur_read = ctx->last_read + 1;
  503. if (cur_read == ctx->ring_size) cur_read = 0;
  504. 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,
  505. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  506. dma_ipe_find_buffer_by_bus_addr(ctx, DEREF(last_written_addr_ptr)), DEREF(last_written_addr_ptr)
  507. );
  508. #ifdef IPEDMA_DETECT_PACKETS
  509. if ((empty_detected_ptr)&&(*empty_detected_ptr)&&(pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, cur_read) == DEREF(last_written_addr_ptr))) packet_flags = PCILIB_DMA_FLAG_EOP;
  510. else packet_flags = 0;
  511. #endif /* IPEDMA_DETECT_PACKETS */
  512. if ((ctx->dma_flags&IPEDMA_FLAG_NOSYNC) == 0)
  513. pcilib_kmem_sync_block(ctx->dmactx.pcilib, ctx->pages, PCILIB_KMEM_SYNC_FROMDEVICE, cur_read);
  514. void *buf = (void*)pcilib_kmem_get_block_ua(ctx->dmactx.pcilib, ctx->pages, cur_read);
  515. ret = cb(cbattr, packet_flags, ctx->page_size, buf);
  516. if (ret < 0) return -ret;
  517. // We don't need this because hardware does not intend to read anything from the memory
  518. //pcilib_kmem_sync_block(ctx->dmactx.pcilib, ctx->pages, PCILIB_KMEM_SYNC_TODEVICE, cur_read);
  519. // Return buffer into the DMA pool when processed
  520. if (ctx->streaming) {
  521. size_t last_free;
  522. // We always keep 1 buffer free to distinguish between completely full and empty cases
  523. if (cur_read) last_free = cur_read - 1;
  524. else last_free = ctx->ring_size - 1;
  525. uintptr_t buf_ba = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, last_free);
  526. if (ctx->addr64) {
  527. WR64(IPEDMA_REG3_PAGE_ADDR, buf_ba);
  528. } else {
  529. WR(IPEDMA_REG2_PAGE_ADDR, buf_ba);
  530. }
  531. # ifdef IPEDMA_STREAMING_CHECKS
  532. pcilib_register_value_t streaming_status;
  533. RD(IPEDMA_REG_STREAMING_STATUS, streaming_status);
  534. if (streaming_status)
  535. pcilib_error("Invalid status (0x%lx) adding a DMA buffer into the queue", streaming_status);
  536. # endif /* IPEDMA_STREAMING_MODE */
  537. }
  538. // Numbered from 1
  539. #ifdef IPEDMA_BUG_LAST_READ
  540. WR(ctx->reg_last_read, cur_read?cur_read:ctx->ring_size);
  541. #else /* IPEDMA_BUG_LAST_READ */
  542. WR(ctx->reg_last_read, cur_read + 1);
  543. #endif /* IPEDMA_BUG_LAST_READ */
  544. 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,
  545. dma_ipe_find_buffer_by_bus_addr(ctx, ctx->last_read_addr), ctx->last_read_addr,
  546. dma_ipe_find_buffer_by_bus_addr(ctx, DEREF(last_written_addr_ptr)), DEREF(last_written_addr_ptr)
  547. );
  548. ctx->last_read = cur_read;
  549. ctx->last_read_addr = pcilib_kmem_get_block_ba(ctx->dmactx.pcilib, ctx->pages, cur_read);
  550. } while (ret);
  551. return 0;
  552. }