xilinx_axidma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * QEMU model of Xilinx AXI-DMA block.
  3. *
  4. * Copyright (c) 2011 Edgar E. Iglesias.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "sysbus.h"
  25. #include "qemu-char.h"
  26. #include "qemu-timer.h"
  27. #include "ptimer.h"
  28. #include "qemu-log.h"
  29. #include "qdev-addr.h"
  30. #include "stream.h"
  31. #define D(x)
  32. #define R_DMACR (0x00 / 4)
  33. #define R_DMASR (0x04 / 4)
  34. #define R_CURDESC (0x08 / 4)
  35. #define R_TAILDESC (0x10 / 4)
  36. #define R_MAX (0x30 / 4)
  37. enum {
  38. DMACR_RUNSTOP = 1,
  39. DMACR_TAILPTR_MODE = 2,
  40. DMACR_RESET = 4
  41. };
  42. enum {
  43. DMASR_HALTED = 1,
  44. DMASR_IDLE = 2,
  45. DMASR_IOC_IRQ = 1 << 12,
  46. DMASR_DLY_IRQ = 1 << 13,
  47. DMASR_IRQ_MASK = 7 << 12
  48. };
  49. struct SDesc {
  50. uint64_t nxtdesc;
  51. uint64_t buffer_address;
  52. uint64_t reserved;
  53. uint32_t control;
  54. uint32_t status;
  55. uint32_t app[6];
  56. };
  57. enum {
  58. SDESC_CTRL_EOF = (1 << 26),
  59. SDESC_CTRL_SOF = (1 << 27),
  60. SDESC_CTRL_LEN_MASK = (1 << 23) - 1
  61. };
  62. enum {
  63. SDESC_STATUS_EOF = (1 << 26),
  64. SDESC_STATUS_SOF_BIT = 27,
  65. SDESC_STATUS_SOF = (1 << SDESC_STATUS_SOF_BIT),
  66. SDESC_STATUS_COMPLETE = (1 << 31)
  67. };
  68. struct Stream {
  69. QEMUBH *bh;
  70. ptimer_state *ptimer;
  71. qemu_irq irq;
  72. int nr;
  73. struct SDesc desc;
  74. int pos;
  75. unsigned int complete_cnt;
  76. uint32_t regs[R_MAX];
  77. };
  78. struct XilinxAXIDMA {
  79. SysBusDevice busdev;
  80. MemoryRegion iomem;
  81. uint32_t freqhz;
  82. StreamSlave *tx_dev;
  83. struct Stream streams[2];
  84. };
  85. /*
  86. * Helper calls to extract info from desriptors and other trivial
  87. * state from regs.
  88. */
  89. static inline int stream_desc_sof(struct SDesc *d)
  90. {
  91. return d->control & SDESC_CTRL_SOF;
  92. }
  93. static inline int stream_desc_eof(struct SDesc *d)
  94. {
  95. return d->control & SDESC_CTRL_EOF;
  96. }
  97. static inline int stream_resetting(struct Stream *s)
  98. {
  99. return !!(s->regs[R_DMACR] & DMACR_RESET);
  100. }
  101. static inline int stream_running(struct Stream *s)
  102. {
  103. return s->regs[R_DMACR] & DMACR_RUNSTOP;
  104. }
  105. static inline int stream_halted(struct Stream *s)
  106. {
  107. return s->regs[R_DMASR] & DMASR_HALTED;
  108. }
  109. static inline int stream_idle(struct Stream *s)
  110. {
  111. return !!(s->regs[R_DMASR] & DMASR_IDLE);
  112. }
  113. static void stream_reset(struct Stream *s)
  114. {
  115. s->regs[R_DMASR] = DMASR_HALTED; /* starts up halted. */
  116. s->regs[R_DMACR] = 1 << 16; /* Starts with one in compl threshold. */
  117. }
  118. /* Map an offset addr into a channel index. */
  119. static inline int streamid_from_addr(target_phys_addr_t addr)
  120. {
  121. int sid;
  122. sid = addr / (0x30);
  123. sid &= 1;
  124. return sid;
  125. }
  126. #ifdef DEBUG_ENET
  127. static void stream_desc_show(struct SDesc *d)
  128. {
  129. qemu_log("buffer_addr = " PRIx64 "\n", d->buffer_address);
  130. qemu_log("nxtdesc = " PRIx64 "\n", d->nxtdesc);
  131. qemu_log("control = %x\n", d->control);
  132. qemu_log("status = %x\n", d->status);
  133. }
  134. #endif
  135. static void stream_desc_load(struct Stream *s, target_phys_addr_t addr)
  136. {
  137. struct SDesc *d = &s->desc;
  138. int i;
  139. cpu_physical_memory_read(addr, (void *) d, sizeof *d);
  140. /* Convert from LE into host endianness. */
  141. d->buffer_address = le64_to_cpu(d->buffer_address);
  142. d->nxtdesc = le64_to_cpu(d->nxtdesc);
  143. d->control = le32_to_cpu(d->control);
  144. d->status = le32_to_cpu(d->status);
  145. for (i = 0; i < ARRAY_SIZE(d->app); i++) {
  146. d->app[i] = le32_to_cpu(d->app[i]);
  147. }
  148. }
  149. static void stream_desc_store(struct Stream *s, target_phys_addr_t addr)
  150. {
  151. struct SDesc *d = &s->desc;
  152. int i;
  153. /* Convert from host endianness into LE. */
  154. d->buffer_address = cpu_to_le64(d->buffer_address);
  155. d->nxtdesc = cpu_to_le64(d->nxtdesc);
  156. d->control = cpu_to_le32(d->control);
  157. d->status = cpu_to_le32(d->status);
  158. for (i = 0; i < ARRAY_SIZE(d->app); i++) {
  159. d->app[i] = cpu_to_le32(d->app[i]);
  160. }
  161. cpu_physical_memory_write(addr, (void *) d, sizeof *d);
  162. }
  163. static void stream_update_irq(struct Stream *s)
  164. {
  165. unsigned int pending, mask, irq;
  166. pending = s->regs[R_DMASR] & DMASR_IRQ_MASK;
  167. mask = s->regs[R_DMACR] & DMASR_IRQ_MASK;
  168. irq = pending & mask;
  169. qemu_set_irq(s->irq, !!irq);
  170. }
  171. static void stream_reload_complete_cnt(struct Stream *s)
  172. {
  173. unsigned int comp_th;
  174. comp_th = (s->regs[R_DMACR] >> 16) & 0xff;
  175. s->complete_cnt = comp_th;
  176. }
  177. static void timer_hit(void *opaque)
  178. {
  179. struct Stream *s = opaque;
  180. stream_reload_complete_cnt(s);
  181. s->regs[R_DMASR] |= DMASR_DLY_IRQ;
  182. stream_update_irq(s);
  183. }
  184. static void stream_complete(struct Stream *s)
  185. {
  186. unsigned int comp_delay;
  187. /* Start the delayed timer. */
  188. comp_delay = s->regs[R_DMACR] >> 24;
  189. if (comp_delay) {
  190. ptimer_stop(s->ptimer);
  191. ptimer_set_count(s->ptimer, comp_delay);
  192. ptimer_run(s->ptimer, 1);
  193. }
  194. s->complete_cnt--;
  195. if (s->complete_cnt == 0) {
  196. /* Raise the IOC irq. */
  197. s->regs[R_DMASR] |= DMASR_IOC_IRQ;
  198. stream_reload_complete_cnt(s);
  199. }
  200. }
  201. static void stream_process_mem2s(struct Stream *s,
  202. StreamSlave *tx_dev)
  203. {
  204. uint32_t prev_d;
  205. unsigned char txbuf[16 * 1024];
  206. unsigned int txlen;
  207. uint32_t app[6];
  208. if (!stream_running(s) || stream_idle(s)) {
  209. return;
  210. }
  211. while (1) {
  212. stream_desc_load(s, s->regs[R_CURDESC]);
  213. if (s->desc.status & SDESC_STATUS_COMPLETE) {
  214. s->regs[R_DMASR] |= DMASR_IDLE;
  215. break;
  216. }
  217. if (stream_desc_sof(&s->desc)) {
  218. s->pos = 0;
  219. memcpy(app, s->desc.app, sizeof app);
  220. }
  221. txlen = s->desc.control & SDESC_CTRL_LEN_MASK;
  222. if ((txlen + s->pos) > sizeof txbuf) {
  223. hw_error("%s: too small internal txbuf! %d\n", __func__,
  224. txlen + s->pos);
  225. }
  226. cpu_physical_memory_read(s->desc.buffer_address,
  227. txbuf + s->pos, txlen);
  228. s->pos += txlen;
  229. if (stream_desc_eof(&s->desc)) {
  230. stream_push(tx_dev, txbuf, s->pos, app);
  231. s->pos = 0;
  232. stream_complete(s);
  233. }
  234. /* Update the descriptor. */
  235. s->desc.status = txlen | SDESC_STATUS_COMPLETE;
  236. stream_desc_store(s, s->regs[R_CURDESC]);
  237. /* Advance. */
  238. prev_d = s->regs[R_CURDESC];
  239. s->regs[R_CURDESC] = s->desc.nxtdesc;
  240. if (prev_d == s->regs[R_TAILDESC]) {
  241. s->regs[R_DMASR] |= DMASR_IDLE;
  242. break;
  243. }
  244. }
  245. }
  246. static void stream_process_s2mem(struct Stream *s,
  247. unsigned char *buf, size_t len, uint32_t *app)
  248. {
  249. uint32_t prev_d;
  250. unsigned int rxlen;
  251. int pos = 0;
  252. int sof = 1;
  253. if (!stream_running(s) || stream_idle(s)) {
  254. return;
  255. }
  256. while (len) {
  257. stream_desc_load(s, s->regs[R_CURDESC]);
  258. if (s->desc.status & SDESC_STATUS_COMPLETE) {
  259. s->regs[R_DMASR] |= DMASR_IDLE;
  260. break;
  261. }
  262. rxlen = s->desc.control & SDESC_CTRL_LEN_MASK;
  263. if (rxlen > len) {
  264. /* It fits. */
  265. rxlen = len;
  266. }
  267. cpu_physical_memory_write(s->desc.buffer_address, buf + pos, rxlen);
  268. len -= rxlen;
  269. pos += rxlen;
  270. /* Update the descriptor. */
  271. if (!len) {
  272. int i;
  273. stream_complete(s);
  274. for (i = 0; i < 5; i++) {
  275. s->desc.app[i] = app[i];
  276. }
  277. s->desc.status |= SDESC_STATUS_EOF;
  278. }
  279. s->desc.status |= sof << SDESC_STATUS_SOF_BIT;
  280. s->desc.status |= SDESC_STATUS_COMPLETE;
  281. stream_desc_store(s, s->regs[R_CURDESC]);
  282. sof = 0;
  283. /* Advance. */
  284. prev_d = s->regs[R_CURDESC];
  285. s->regs[R_CURDESC] = s->desc.nxtdesc;
  286. if (prev_d == s->regs[R_TAILDESC]) {
  287. s->regs[R_DMASR] |= DMASR_IDLE;
  288. break;
  289. }
  290. }
  291. }
  292. static void
  293. axidma_push(StreamSlave *obj, unsigned char *buf, size_t len, uint32_t *app)
  294. {
  295. struct XilinxAXIDMA *d = FROM_SYSBUS(typeof(*d), SYS_BUS_DEVICE(obj));
  296. struct Stream *s = &d->streams[1];
  297. if (!app) {
  298. hw_error("No stream app data!\n");
  299. }
  300. stream_process_s2mem(s, buf, len, app);
  301. stream_update_irq(s);
  302. }
  303. static uint64_t axidma_read(void *opaque, target_phys_addr_t addr,
  304. unsigned size)
  305. {
  306. struct XilinxAXIDMA *d = opaque;
  307. struct Stream *s;
  308. uint32_t r = 0;
  309. int sid;
  310. sid = streamid_from_addr(addr);
  311. s = &d->streams[sid];
  312. addr = addr % 0x30;
  313. addr >>= 2;
  314. switch (addr) {
  315. case R_DMACR:
  316. /* Simulate one cycles reset delay. */
  317. s->regs[addr] &= ~DMACR_RESET;
  318. r = s->regs[addr];
  319. break;
  320. case R_DMASR:
  321. s->regs[addr] &= 0xffff;
  322. s->regs[addr] |= (s->complete_cnt & 0xff) << 16;
  323. s->regs[addr] |= (ptimer_get_count(s->ptimer) & 0xff) << 24;
  324. r = s->regs[addr];
  325. break;
  326. default:
  327. r = s->regs[addr];
  328. D(qemu_log("%s ch=%d addr=" TARGET_FMT_plx " v=%x\n",
  329. __func__, sid, addr * 4, r));
  330. break;
  331. }
  332. return r;
  333. }
  334. static void axidma_write(void *opaque, target_phys_addr_t addr,
  335. uint64_t value, unsigned size)
  336. {
  337. struct XilinxAXIDMA *d = opaque;
  338. struct Stream *s;
  339. int sid;
  340. sid = streamid_from_addr(addr);
  341. s = &d->streams[sid];
  342. addr = addr % 0x30;
  343. addr >>= 2;
  344. switch (addr) {
  345. case R_DMACR:
  346. /* Tailptr mode is always on. */
  347. value |= DMACR_TAILPTR_MODE;
  348. /* Remember our previous reset state. */
  349. value |= (s->regs[addr] & DMACR_RESET);
  350. s->regs[addr] = value;
  351. if (value & DMACR_RESET) {
  352. stream_reset(s);
  353. }
  354. if ((value & 1) && !stream_resetting(s)) {
  355. /* Start processing. */
  356. s->regs[R_DMASR] &= ~(DMASR_HALTED | DMASR_IDLE);
  357. }
  358. stream_reload_complete_cnt(s);
  359. break;
  360. case R_DMASR:
  361. /* Mask away write to clear irq lines. */
  362. value &= ~(value & DMASR_IRQ_MASK);
  363. s->regs[addr] = value;
  364. break;
  365. case R_TAILDESC:
  366. s->regs[addr] = value;
  367. s->regs[R_DMASR] &= ~DMASR_IDLE; /* Not idle. */
  368. if (!sid) {
  369. stream_process_mem2s(s, d->tx_dev);
  370. }
  371. break;
  372. default:
  373. D(qemu_log("%s: ch=%d addr=" TARGET_FMT_plx " v=%x\n",
  374. __func__, sid, addr * 4, value));
  375. s->regs[addr] = value;
  376. break;
  377. }
  378. stream_update_irq(s);
  379. }
  380. static const MemoryRegionOps axidma_ops = {
  381. .read = axidma_read,
  382. .write = axidma_write,
  383. .endianness = DEVICE_NATIVE_ENDIAN,
  384. };
  385. static int xilinx_axidma_init(SysBusDevice *dev)
  386. {
  387. struct XilinxAXIDMA *s = FROM_SYSBUS(typeof(*s), dev);
  388. int i;
  389. sysbus_init_irq(dev, &s->streams[0].irq);
  390. sysbus_init_irq(dev, &s->streams[1].irq);
  391. memory_region_init_io(&s->iomem, &axidma_ops, s,
  392. "xlnx.axi-dma", R_MAX * 4 * 2);
  393. sysbus_init_mmio(dev, &s->iomem);
  394. for (i = 0; i < 2; i++) {
  395. stream_reset(&s->streams[i]);
  396. s->streams[i].nr = i;
  397. s->streams[i].bh = qemu_bh_new(timer_hit, &s->streams[i]);
  398. s->streams[i].ptimer = ptimer_init(s->streams[i].bh);
  399. ptimer_set_freq(s->streams[i].ptimer, s->freqhz);
  400. }
  401. return 0;
  402. }
  403. static void xilinx_axidma_initfn(Object *obj)
  404. {
  405. struct XilinxAXIDMA *s = FROM_SYSBUS(typeof(*s), SYS_BUS_DEVICE(obj));
  406. object_property_add_link(obj, "axistream-connected", TYPE_STREAM_SLAVE,
  407. (Object **) &s->tx_dev, NULL);
  408. }
  409. static Property axidma_properties[] = {
  410. DEFINE_PROP_UINT32("freqhz", struct XilinxAXIDMA, freqhz, 50000000),
  411. DEFINE_PROP_END_OF_LIST(),
  412. };
  413. static void axidma_class_init(ObjectClass *klass, void *data)
  414. {
  415. DeviceClass *dc = DEVICE_CLASS(klass);
  416. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  417. StreamSlaveClass *ssc = STREAM_SLAVE_CLASS(klass);
  418. k->init = xilinx_axidma_init;
  419. dc->props = axidma_properties;
  420. ssc->push = axidma_push;
  421. }
  422. static TypeInfo axidma_info = {
  423. .name = "xlnx.axi-dma",
  424. .parent = TYPE_SYS_BUS_DEVICE,
  425. .instance_size = sizeof(struct XilinxAXIDMA),
  426. .class_init = axidma_class_init,
  427. .instance_init = xilinx_axidma_initfn,
  428. .interfaces = (InterfaceInfo[]) {
  429. { TYPE_STREAM_SLAVE },
  430. { }
  431. }
  432. };
  433. static void xilinx_axidma_register_types(void)
  434. {
  435. type_register_static(&axidma_info);
  436. }
  437. type_init(xilinx_axidma_register_types)