2
0

xilinx_ethlite.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * QEMU model of the Xilinx Ethernet Lite MAC.
  3. *
  4. * Copyright (c) 2009 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 "qemu/osdep.h"
  25. #include "qemu/module.h"
  26. #include "cpu.h" /* FIXME should not use tswap* */
  27. #include "hw/sysbus.h"
  28. #include "hw/irq.h"
  29. #include "hw/qdev-properties.h"
  30. #include "net/net.h"
  31. #define D(x)
  32. #define R_TX_BUF0 0
  33. #define R_TX_LEN0 (0x07f4 / 4)
  34. #define R_TX_GIE0 (0x07f8 / 4)
  35. #define R_TX_CTRL0 (0x07fc / 4)
  36. #define R_TX_BUF1 (0x0800 / 4)
  37. #define R_TX_LEN1 (0x0ff4 / 4)
  38. #define R_TX_CTRL1 (0x0ffc / 4)
  39. #define R_RX_BUF0 (0x1000 / 4)
  40. #define R_RX_CTRL0 (0x17fc / 4)
  41. #define R_RX_BUF1 (0x1800 / 4)
  42. #define R_RX_CTRL1 (0x1ffc / 4)
  43. #define R_MAX (0x2000 / 4)
  44. #define GIE_GIE 0x80000000
  45. #define CTRL_I 0x8
  46. #define CTRL_P 0x2
  47. #define CTRL_S 0x1
  48. #define TYPE_XILINX_ETHLITE "xlnx.xps-ethernetlite"
  49. #define XILINX_ETHLITE(obj) \
  50. OBJECT_CHECK(struct xlx_ethlite, (obj), TYPE_XILINX_ETHLITE)
  51. struct xlx_ethlite
  52. {
  53. SysBusDevice parent_obj;
  54. MemoryRegion mmio;
  55. qemu_irq irq;
  56. NICState *nic;
  57. NICConf conf;
  58. uint32_t c_tx_pingpong;
  59. uint32_t c_rx_pingpong;
  60. unsigned int txbuf;
  61. unsigned int rxbuf;
  62. uint32_t regs[R_MAX];
  63. };
  64. static inline void eth_pulse_irq(struct xlx_ethlite *s)
  65. {
  66. /* Only the first gie reg is active. */
  67. if (s->regs[R_TX_GIE0] & GIE_GIE) {
  68. qemu_irq_pulse(s->irq);
  69. }
  70. }
  71. static uint64_t
  72. eth_read(void *opaque, hwaddr addr, unsigned int size)
  73. {
  74. struct xlx_ethlite *s = opaque;
  75. uint32_t r = 0;
  76. addr >>= 2;
  77. switch (addr)
  78. {
  79. case R_TX_GIE0:
  80. case R_TX_LEN0:
  81. case R_TX_LEN1:
  82. case R_TX_CTRL1:
  83. case R_TX_CTRL0:
  84. case R_RX_CTRL1:
  85. case R_RX_CTRL0:
  86. r = s->regs[addr];
  87. D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr * 4, r));
  88. break;
  89. default:
  90. r = tswap32(s->regs[addr]);
  91. break;
  92. }
  93. return r;
  94. }
  95. static void
  96. eth_write(void *opaque, hwaddr addr,
  97. uint64_t val64, unsigned int size)
  98. {
  99. struct xlx_ethlite *s = opaque;
  100. unsigned int base = 0;
  101. uint32_t value = val64;
  102. addr >>= 2;
  103. switch (addr)
  104. {
  105. case R_TX_CTRL0:
  106. case R_TX_CTRL1:
  107. if (addr == R_TX_CTRL1)
  108. base = 0x800 / 4;
  109. D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
  110. __func__, addr * 4, value));
  111. if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
  112. qemu_send_packet(qemu_get_queue(s->nic),
  113. (void *) &s->regs[base],
  114. s->regs[base + R_TX_LEN0]);
  115. D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
  116. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  117. eth_pulse_irq(s);
  118. } else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
  119. memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
  120. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  121. eth_pulse_irq(s);
  122. }
  123. /* We are fast and get ready pretty much immediately so
  124. we actually never flip the S nor P bits to one. */
  125. s->regs[addr] = value & ~(CTRL_P | CTRL_S);
  126. break;
  127. /* Keep these native. */
  128. case R_RX_CTRL0:
  129. case R_RX_CTRL1:
  130. if (!(value & CTRL_S)) {
  131. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  132. }
  133. /* fall through */
  134. case R_TX_LEN0:
  135. case R_TX_LEN1:
  136. case R_TX_GIE0:
  137. D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
  138. __func__, addr * 4, value));
  139. s->regs[addr] = value;
  140. break;
  141. default:
  142. s->regs[addr] = tswap32(value);
  143. break;
  144. }
  145. }
  146. static const MemoryRegionOps eth_ops = {
  147. .read = eth_read,
  148. .write = eth_write,
  149. .endianness = DEVICE_NATIVE_ENDIAN,
  150. .valid = {
  151. .min_access_size = 4,
  152. .max_access_size = 4
  153. }
  154. };
  155. static int eth_can_rx(NetClientState *nc)
  156. {
  157. struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
  158. unsigned int rxbase = s->rxbuf * (0x800 / 4);
  159. return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
  160. }
  161. static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
  162. {
  163. struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
  164. unsigned int rxbase = s->rxbuf * (0x800 / 4);
  165. /* DA filter. */
  166. if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
  167. return size;
  168. if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
  169. D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
  170. return -1;
  171. }
  172. D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
  173. if (size > (R_MAX - R_RX_BUF0 - rxbase) * 4) {
  174. D(qemu_log("ethlite packet is too big, size=%x\n", size));
  175. return -1;
  176. }
  177. memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
  178. s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
  179. if (s->regs[R_RX_CTRL0] & CTRL_I) {
  180. eth_pulse_irq(s);
  181. }
  182. /* If c_rx_pingpong was set flip buffers. */
  183. s->rxbuf ^= s->c_rx_pingpong;
  184. return size;
  185. }
  186. static void xilinx_ethlite_reset(DeviceState *dev)
  187. {
  188. struct xlx_ethlite *s = XILINX_ETHLITE(dev);
  189. s->rxbuf = 0;
  190. }
  191. static NetClientInfo net_xilinx_ethlite_info = {
  192. .type = NET_CLIENT_DRIVER_NIC,
  193. .size = sizeof(NICState),
  194. .can_receive = eth_can_rx,
  195. .receive = eth_rx,
  196. };
  197. static void xilinx_ethlite_realize(DeviceState *dev, Error **errp)
  198. {
  199. struct xlx_ethlite *s = XILINX_ETHLITE(dev);
  200. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  201. s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
  202. object_get_typename(OBJECT(dev)), dev->id, s);
  203. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  204. }
  205. static void xilinx_ethlite_init(Object *obj)
  206. {
  207. struct xlx_ethlite *s = XILINX_ETHLITE(obj);
  208. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
  209. memory_region_init_io(&s->mmio, obj, &eth_ops, s,
  210. "xlnx.xps-ethernetlite", R_MAX * 4);
  211. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  212. }
  213. static Property xilinx_ethlite_properties[] = {
  214. DEFINE_PROP_UINT32("tx-ping-pong", struct xlx_ethlite, c_tx_pingpong, 1),
  215. DEFINE_PROP_UINT32("rx-ping-pong", struct xlx_ethlite, c_rx_pingpong, 1),
  216. DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
  217. DEFINE_PROP_END_OF_LIST(),
  218. };
  219. static void xilinx_ethlite_class_init(ObjectClass *klass, void *data)
  220. {
  221. DeviceClass *dc = DEVICE_CLASS(klass);
  222. dc->realize = xilinx_ethlite_realize;
  223. dc->reset = xilinx_ethlite_reset;
  224. dc->props = xilinx_ethlite_properties;
  225. }
  226. static const TypeInfo xilinx_ethlite_info = {
  227. .name = TYPE_XILINX_ETHLITE,
  228. .parent = TYPE_SYS_BUS_DEVICE,
  229. .instance_size = sizeof(struct xlx_ethlite),
  230. .instance_init = xilinx_ethlite_init,
  231. .class_init = xilinx_ethlite_class_init,
  232. };
  233. static void xilinx_ethlite_register_types(void)
  234. {
  235. type_register_static(&xilinx_ethlite_info);
  236. }
  237. type_init(xilinx_ethlite_register_types)