blob: 2ade72dea081f5895056591587bd0e9e3ed06729 [file] [log] [blame]
Peter Maydelle8d40462016-01-26 18:17:11 +00001#include "qemu/osdep.h"
Markus Armbruster64552b62019-08-12 07:23:42 +02002#include "hw/irq.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +02003#include "hw/qdev-properties.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +02004#include "net/net.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +02005#include "qemu/module.h"
Hervé Poussineau83818f72011-09-04 22:29:27 +02006#include "trace.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +01007#include "hw/sysbus.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +02008#include "migration/vmstate.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -04009#include "qom/object.h"
thsf0fc6f82007-10-17 13:39:42 +000010
thsf0fc6f82007-10-17 13:39:42 +000011/* MIPSnet register offsets */
12
Filip Bozuta83aecba2019-12-06 14:58:05 +010013#define MIPSNET_DEV_ID 0x00
14#define MIPSNET_BUSY 0x08
15#define MIPSNET_RX_DATA_COUNT 0x0c
16#define MIPSNET_TX_DATA_COUNT 0x10
17#define MIPSNET_INT_CTL 0x14
18# define MIPSNET_INTCTL_TXDONE 0x00000001
19# define MIPSNET_INTCTL_RXDONE 0x00000002
20# define MIPSNET_INTCTL_TESTBIT 0x80000000
21#define MIPSNET_INTERRUPT_INFO 0x18
22#define MIPSNET_RX_DATA_BUFFER 0x1c
23#define MIPSNET_TX_DATA_BUFFER 0x20
thsf0fc6f82007-10-17 13:39:42 +000024
Filip Bozuta83aecba2019-12-06 14:58:05 +010025#define MAX_ETH_FRAME_SIZE 1514
thsf0fc6f82007-10-17 13:39:42 +000026
Andreas Färbera4dbb8b2013-07-27 15:59:07 +020027#define TYPE_MIPS_NET "mipsnet"
Eduardo Habkost80633962020-09-16 14:25:19 -040028OBJECT_DECLARE_SIMPLE_TYPE(MIPSnetState, MIPS_NET)
Andreas Färbera4dbb8b2013-07-27 15:59:07 +020029
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040030struct MIPSnetState {
Andreas Färbera4dbb8b2013-07-27 15:59:07 +020031 SysBusDevice parent_obj;
Hervé Poussineaud118d642011-09-04 22:29:26 +020032
thsf0fc6f82007-10-17 13:39:42 +000033 uint32_t busy;
34 uint32_t rx_count;
35 uint32_t rx_read;
36 uint32_t tx_count;
37 uint32_t tx_written;
38 uint32_t intctl;
39 uint8_t rx_buffer[MAX_ETH_FRAME_SIZE];
40 uint8_t tx_buffer[MAX_ETH_FRAME_SIZE];
Hervé Poussineaud118d642011-09-04 22:29:26 +020041 MemoryRegion io;
thsf0fc6f82007-10-17 13:39:42 +000042 qemu_irq irq;
Mark McLoughlin1f30d102009-11-25 18:49:21 +000043 NICState *nic;
44 NICConf conf;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040045};
thsf0fc6f82007-10-17 13:39:42 +000046
47static void mipsnet_reset(MIPSnetState *s)
48{
49 s->busy = 1;
50 s->rx_count = 0;
51 s->rx_read = 0;
52 s->tx_count = 0;
53 s->tx_written = 0;
54 s->intctl = 0;
55 memset(s->rx_buffer, 0, MAX_ETH_FRAME_SIZE);
56 memset(s->tx_buffer, 0, MAX_ETH_FRAME_SIZE);
57}
58
59static void mipsnet_update_irq(MIPSnetState *s)
60{
61 int isr = !!s->intctl;
Hervé Poussineau83818f72011-09-04 22:29:27 +020062 trace_mipsnet_irq(isr, s->intctl);
thsf0fc6f82007-10-17 13:39:42 +000063 qemu_set_irq(s->irq, isr);
64}
65
66static int mipsnet_buffer_full(MIPSnetState *s)
67{
Filip Bozuta83aecba2019-12-06 14:58:05 +010068 if (s->rx_count >= MAX_ETH_FRAME_SIZE) {
thsf0fc6f82007-10-17 13:39:42 +000069 return 1;
Filip Bozuta83aecba2019-12-06 14:58:05 +010070 }
thsf0fc6f82007-10-17 13:39:42 +000071 return 0;
72}
73
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010074static int mipsnet_can_receive(NetClientState *nc)
thsf0fc6f82007-10-17 13:39:42 +000075{
Jason Wangcc1f0f42013-01-30 19:12:23 +080076 MIPSnetState *s = qemu_get_nic_opaque(nc);
thsf0fc6f82007-10-17 13:39:42 +000077
Filip Bozuta83aecba2019-12-06 14:58:05 +010078 if (s->busy) {
thsf0fc6f82007-10-17 13:39:42 +000079 return 0;
Filip Bozuta83aecba2019-12-06 14:58:05 +010080 }
thsf0fc6f82007-10-17 13:39:42 +000081 return !mipsnet_buffer_full(s);
82}
83
Filip Bozuta83aecba2019-12-06 14:58:05 +010084static ssize_t mipsnet_receive(NetClientState *nc,
85 const uint8_t *buf, size_t size)
thsf0fc6f82007-10-17 13:39:42 +000086{
Jason Wangcc1f0f42013-01-30 19:12:23 +080087 MIPSnetState *s = qemu_get_nic_opaque(nc);
thsf0fc6f82007-10-17 13:39:42 +000088
Hervé Poussineau83818f72011-09-04 22:29:27 +020089 trace_mipsnet_receive(size);
Filip Bozuta83aecba2019-12-06 14:58:05 +010090 if (!mipsnet_can_receive(nc)) {
Fam Zheng1dd58ae2015-07-15 18:19:10 +080091 return 0;
Filip Bozuta83aecba2019-12-06 14:58:05 +010092 }
thsf0fc6f82007-10-17 13:39:42 +000093
Prasad J Pandit3af91872016-04-07 15:56:02 +053094 if (size >= sizeof(s->rx_buffer)) {
95 return 0;
96 }
thsf0fc6f82007-10-17 13:39:42 +000097 s->busy = 1;
98
99 /* Just accept everything. */
100
101 /* Write packet data. */
102 memcpy(s->rx_buffer, buf, size);
103
104 s->rx_count = size;
105 s->rx_read = 0;
106
107 /* Now we can signal we have received something. */
108 s->intctl |= MIPSNET_INTCTL_RXDONE;
109 mipsnet_update_irq(s);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100110
111 return size;
thsf0fc6f82007-10-17 13:39:42 +0000112}
113
Avi Kivitya8170e52012-10-23 12:30:10 +0200114static uint64_t mipsnet_ioport_read(void *opaque, hwaddr addr,
Hervé Poussineaud118d642011-09-04 22:29:26 +0200115 unsigned int size)
thsf0fc6f82007-10-17 13:39:42 +0000116{
117 MIPSnetState *s = opaque;
118 int ret = 0;
thsf0fc6f82007-10-17 13:39:42 +0000119
120 addr &= 0x3f;
121 switch (addr) {
122 case MIPSNET_DEV_ID:
Filip Bozuta83aecba2019-12-06 14:58:05 +0100123 ret = be32_to_cpu(0x4d495053); /* MIPS */
thsf0fc6f82007-10-17 13:39:42 +0000124 break;
125 case MIPSNET_DEV_ID + 4:
Filip Bozuta83aecba2019-12-06 14:58:05 +0100126 ret = be32_to_cpu(0x4e455430); /* NET0 */
thsf0fc6f82007-10-17 13:39:42 +0000127 break;
128 case MIPSNET_BUSY:
Paolo Bonzini7d374352018-12-13 23:37:37 +0100129 ret = s->busy;
thsf0fc6f82007-10-17 13:39:42 +0000130 break;
131 case MIPSNET_RX_DATA_COUNT:
Paolo Bonzini7d374352018-12-13 23:37:37 +0100132 ret = s->rx_count;
thsf0fc6f82007-10-17 13:39:42 +0000133 break;
134 case MIPSNET_TX_DATA_COUNT:
Paolo Bonzini7d374352018-12-13 23:37:37 +0100135 ret = s->tx_count;
thsf0fc6f82007-10-17 13:39:42 +0000136 break;
137 case MIPSNET_INT_CTL:
Paolo Bonzini7d374352018-12-13 23:37:37 +0100138 ret = s->intctl;
thsf0fc6f82007-10-17 13:39:42 +0000139 s->intctl &= ~MIPSNET_INTCTL_TESTBIT;
140 break;
141 case MIPSNET_INTERRUPT_INFO:
142 /* XXX: This seems to be a per-VPE interrupt number. */
Paolo Bonzini7d374352018-12-13 23:37:37 +0100143 ret = 0;
thsf0fc6f82007-10-17 13:39:42 +0000144 break;
145 case MIPSNET_RX_DATA_BUFFER:
146 if (s->rx_count) {
147 s->rx_count--;
148 ret = s->rx_buffer[s->rx_read++];
Fam Zheng1dd58ae2015-07-15 18:19:10 +0800149 if (mipsnet_can_receive(s->nic->ncs)) {
150 qemu_flush_queued_packets(qemu_get_queue(s->nic));
151 }
thsf0fc6f82007-10-17 13:39:42 +0000152 }
153 break;
154 /* Reads as zero. */
155 case MIPSNET_TX_DATA_BUFFER:
156 default:
157 break;
158 }
Hervé Poussineau83818f72011-09-04 22:29:27 +0200159 trace_mipsnet_read(addr, ret);
thsf0fc6f82007-10-17 13:39:42 +0000160 return ret;
161}
162
Avi Kivitya8170e52012-10-23 12:30:10 +0200163static void mipsnet_ioport_write(void *opaque, hwaddr addr,
Hervé Poussineaud118d642011-09-04 22:29:26 +0200164 uint64_t val, unsigned int size)
thsf0fc6f82007-10-17 13:39:42 +0000165{
166 MIPSnetState *s = opaque;
167
168 addr &= 0x3f;
Hervé Poussineau83818f72011-09-04 22:29:27 +0200169 trace_mipsnet_write(addr, val);
thsf0fc6f82007-10-17 13:39:42 +0000170 switch (addr) {
171 case MIPSNET_TX_DATA_COUNT:
Paolo Bonzini7d374352018-12-13 23:37:37 +0100172 s->tx_count = (val <= MAX_ETH_FRAME_SIZE) ? val : 0;
thsf0fc6f82007-10-17 13:39:42 +0000173 s->tx_written = 0;
174 break;
175 case MIPSNET_INT_CTL:
176 if (val & MIPSNET_INTCTL_TXDONE) {
177 s->intctl &= ~MIPSNET_INTCTL_TXDONE;
178 } else if (val & MIPSNET_INTCTL_RXDONE) {
179 s->intctl &= ~MIPSNET_INTCTL_RXDONE;
180 } else if (val & MIPSNET_INTCTL_TESTBIT) {
181 mipsnet_reset(s);
182 s->intctl |= MIPSNET_INTCTL_TESTBIT;
183 } else if (!val) {
184 /* ACK testbit interrupt, flag was cleared on read. */
185 }
186 s->busy = !!s->intctl;
187 mipsnet_update_irq(s);
Fam Zheng1dd58ae2015-07-15 18:19:10 +0800188 if (mipsnet_can_receive(s->nic->ncs)) {
189 qemu_flush_queued_packets(qemu_get_queue(s->nic));
190 }
thsf0fc6f82007-10-17 13:39:42 +0000191 break;
192 case MIPSNET_TX_DATA_BUFFER:
193 s->tx_buffer[s->tx_written++] = val;
Prasad J Panditd88d3a02016-06-08 16:07:04 +0530194 if ((s->tx_written >= MAX_ETH_FRAME_SIZE)
195 || (s->tx_written == s->tx_count)) {
thsf0fc6f82007-10-17 13:39:42 +0000196 /* Send buffer. */
Prasad J Panditd88d3a02016-06-08 16:07:04 +0530197 trace_mipsnet_send(s->tx_written);
198 qemu_send_packet(qemu_get_queue(s->nic),
199 s->tx_buffer, s->tx_written);
thsf0fc6f82007-10-17 13:39:42 +0000200 s->tx_count = s->tx_written = 0;
201 s->intctl |= MIPSNET_INTCTL_TXDONE;
202 s->busy = 1;
203 mipsnet_update_irq(s);
204 }
205 break;
206 /* Read-only registers */
207 case MIPSNET_DEV_ID:
208 case MIPSNET_BUSY:
209 case MIPSNET_RX_DATA_COUNT:
210 case MIPSNET_INTERRUPT_INFO:
211 case MIPSNET_RX_DATA_BUFFER:
212 default:
213 break;
214 }
215}
216
Juan Quintelac7298ab2010-12-01 23:02:56 +0100217static const VMStateDescription vmstate_mipsnet = {
218 .name = "mipsnet",
219 .version_id = 0,
220 .minimum_version_id = 0,
Juan Quintela35d08452014-04-16 16:01:33 +0200221 .fields = (VMStateField[]) {
Juan Quintelac7298ab2010-12-01 23:02:56 +0100222 VMSTATE_UINT32(busy, MIPSnetState),
223 VMSTATE_UINT32(rx_count, MIPSnetState),
224 VMSTATE_UINT32(rx_read, MIPSnetState),
225 VMSTATE_UINT32(tx_count, MIPSnetState),
226 VMSTATE_UINT32(tx_written, MIPSnetState),
227 VMSTATE_UINT32(intctl, MIPSnetState),
228 VMSTATE_BUFFER(rx_buffer, MIPSnetState),
229 VMSTATE_BUFFER(tx_buffer, MIPSnetState),
230 VMSTATE_END_OF_LIST()
231 }
232};
thsf0fc6f82007-10-17 13:39:42 +0000233
Mark McLoughlin1f30d102009-11-25 18:49:21 +0000234static NetClientInfo net_mipsnet_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600235 .type = NET_CLIENT_DRIVER_NIC,
Mark McLoughlin1f30d102009-11-25 18:49:21 +0000236 .size = sizeof(NICState),
Mark McLoughlin1f30d102009-11-25 18:49:21 +0000237 .receive = mipsnet_receive,
Mark McLoughlin1f30d102009-11-25 18:49:21 +0000238};
239
Stefan Weila348f102012-02-05 10:19:07 +0000240static const MemoryRegionOps mipsnet_ioport_ops = {
Hervé Poussineaud118d642011-09-04 22:29:26 +0200241 .read = mipsnet_ioport_read,
242 .write = mipsnet_ioport_write,
243 .impl.min_access_size = 1,
244 .impl.max_access_size = 4,
245};
246
Cédric Le Goater04cb1572018-10-01 08:37:58 +0200247static void mipsnet_realize(DeviceState *dev, Error **errp)
thsf0fc6f82007-10-17 13:39:42 +0000248{
Cédric Le Goater04cb1572018-10-01 08:37:58 +0200249 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Andreas Färbera4dbb8b2013-07-27 15:59:07 +0200250 MIPSnetState *s = MIPS_NET(dev);
thsf0fc6f82007-10-17 13:39:42 +0000251
Paolo Bonzinieedfac62013-06-06 21:25:08 -0400252 memory_region_init_io(&s->io, OBJECT(dev), &mipsnet_ioport_ops, s,
253 "mipsnet-io", 36);
Andreas Färbera4dbb8b2013-07-27 15:59:07 +0200254 sysbus_init_mmio(sbd, &s->io);
255 sysbus_init_irq(sbd, &s->irq);
aliguori0ae18ce2009-01-13 19:39:36 +0000256
Hervé Poussineaud118d642011-09-04 22:29:26 +0200257 s->nic = qemu_new_nic(&net_mipsnet_info, &s->conf,
Andreas Färbera4dbb8b2013-07-27 15:59:07 +0200258 object_get_typename(OBJECT(dev)), dev->id, s);
Jason Wangb356f762013-01-30 19:12:22 +0800259 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
thsf0fc6f82007-10-17 13:39:42 +0000260}
Hervé Poussineaud118d642011-09-04 22:29:26 +0200261
262static void mipsnet_sysbus_reset(DeviceState *dev)
263{
Andreas Färbera4dbb8b2013-07-27 15:59:07 +0200264 MIPSnetState *s = MIPS_NET(dev);
Hervé Poussineaud118d642011-09-04 22:29:26 +0200265 mipsnet_reset(s);
266}
267
Anthony Liguori999e12b2012-01-24 13:12:29 -0600268static Property mipsnet_properties[] = {
269 DEFINE_NIC_PROPERTIES(MIPSnetState, conf),
270 DEFINE_PROP_END_OF_LIST(),
271};
272
273static void mipsnet_class_init(ObjectClass *klass, void *data)
274{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600275 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600276
Cédric Le Goater04cb1572018-10-01 08:37:58 +0200277 dc->realize = mipsnet_realize;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300278 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600279 dc->desc = "MIPS Simulator network device";
280 dc->reset = mipsnet_sysbus_reset;
281 dc->vmsd = &vmstate_mipsnet;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400282 device_class_set_props(dc, mipsnet_properties);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600283}
284
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100285static const TypeInfo mipsnet_info = {
Andreas Färbera4dbb8b2013-07-27 15:59:07 +0200286 .name = TYPE_MIPS_NET,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600287 .parent = TYPE_SYS_BUS_DEVICE,
288 .instance_size = sizeof(MIPSnetState),
289 .class_init = mipsnet_class_init,
Hervé Poussineaud118d642011-09-04 22:29:26 +0200290};
291
Andreas Färber83f7d432012-02-09 15:20:55 +0100292static void mipsnet_register_types(void)
Hervé Poussineaud118d642011-09-04 22:29:26 +0200293{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600294 type_register_static(&mipsnet_info);
Hervé Poussineaud118d642011-09-04 22:29:26 +0200295}
296
Andreas Färber83f7d432012-02-09 15:20:55 +0100297type_init(mipsnet_register_types)