gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vio_mmio.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Huawei International
3  * Copyright (c) 2016-2018 ARM Limited
4  * All rights reserved
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met: redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer;
19  * redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution;
22  * neither the name of the copyright holders nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include "dev/riscv/vio_mmio.hh"
40 
41 #include "debug/VirtIOMMIO.hh"
42 #include "dev/riscv/hifive.hh"
43 #include "mem/packet_access.hh"
44 #include "params/MmioVirtIO.hh"
45 
46 MmioVirtIO::MmioVirtIO(const MmioVirtIOParams &params)
47  : PlicIntDevice(params),
48  hostFeaturesSelect(0), guestFeaturesSelect(0), pageSize(0),
49  interruptStatus(0), vio(*params.vio)
50 {
51  vio.registerKickCallback([this]() { kick(); });
52 }
53 
55 {
56 }
57 
58 Tick
60 {
61  const Addr offset = pkt->getAddr() - pioAddr;
62  const unsigned size(pkt->getSize());
63 
64  DPRINTF(VirtIOMMIO, "Reading %u bytes @ 0x%x:\n", size, offset);
65 
66  // Forward device configuration writes to the device VirtIO model
67  if (offset >= OFF_CONFIG) {
69  return 0;
70  }
71  panic_if(size != 4, "Unexpected read size: %u\n", size);
72 
73  const uint32_t value = read(offset);
74  DPRINTF(VirtIOMMIO, " value: 0x%x\n", value);
75  pkt->makeResponse();
76  pkt->setLE<uint32_t>(value);
77 
78  return 0;
79 }
80 
81 uint32_t
83 {
84  switch(offset) {
85  case OFF_MAGIC:
86  return MAGIC;
87 
88  case OFF_VERSION:
89  return VERSION;
90 
91  case OFF_DEVICE_ID:
92  return vio.deviceId;
93 
94  case OFF_VENDOR_ID:
95  return VENDOR_ID;
96 
97  case OFF_HOST_FEATURES:
98  // We only implement 32 bits of this register
99  if (hostFeaturesSelect == 0)
100  return vio.deviceFeatures;
101  else
102  return 0;
103 
105  return hostFeaturesSelect;
106 
107  case OFF_GUEST_FEATURES:
108  // We only implement 32 bits of this register
109  if (guestFeaturesSelect == 0)
110  return vio.getGuestFeatures();
111  else
112  return 0;
113 
115  return hostFeaturesSelect;
116 
117  case OFF_GUEST_PAGE_SIZE:
118  return pageSize;
119 
120  case OFF_QUEUE_SELECT:
121  return vio.getQueueSelect();
122 
123  case OFF_QUEUE_NUM_MAX:
124  return vio.getQueueSize();
125 
126  case OFF_QUEUE_NUM:
127  // TODO: We don't support queue resizing, so ignore this for now.
128  return vio.getQueueSize();
129 
130  case OFF_QUEUE_ALIGN:
131  // TODO: Implement this once we support other alignment sizes
132  return VirtQueue::ALIGN_SIZE;
133 
134  case OFF_QUEUE_PFN:
135  return vio.getQueueAddress();
136 
138  return interruptStatus;
139 
140  case OFF_STATUS:
141  return vio.getDeviceStatus();
142 
143  // Write-only registers
144  case OFF_QUEUE_NOTIFY:
145  case OFF_INTERRUPT_ACK:
146  warn("Guest is trying to read to write-only register 0x%\n",
147  offset);
148  return 0;
149 
150  default:
151  panic("Unhandled read offset (0x%x)\n", offset);
152  }
153 }
154 
155 Tick
157 {
158  const Addr offset = pkt->getAddr() - pioAddr;
159  const unsigned size(pkt->getSize());
160 
161  DPRINTF(VirtIOMMIO, "Writing %u bytes @ 0x%x:\n", size, offset);
162 
163  // Forward device configuration writes to the device VirtIO model
164  if (offset >= OFF_CONFIG) {
166  return 0;
167  }
168 
169  panic_if(size != 4, "Unexpected write size @ 0x%x: %u\n", offset, size);
170  DPRINTF(VirtIOMMIO, " value: 0x%x\n", pkt->getLE<uint32_t>());
171  pkt->makeResponse();
172  write(offset, pkt->getLE<uint32_t>());
173  return 0;
174 }
175 
176 void
177 MmioVirtIO::write(Addr offset, uint32_t value)
178 {
179  switch(offset) {
181  hostFeaturesSelect = value;
182  return;
183 
184  case OFF_GUEST_FEATURES:
185  if (guestFeaturesSelect == 0) {
186  vio.setGuestFeatures(value);
187  } else if (value != 0) {
188  warn("Setting unimplemented guest features register %u: %u\n",
189  guestFeaturesSelect, value);
190  }
191  return;
192 
194  guestFeaturesSelect = value;
195  return;
196 
197  case OFF_GUEST_PAGE_SIZE:
198  // TODO: We only support 4096 byte pages at the moment
200  "Unhandled VirtIO page size: %u", value);
201  pageSize = value;
202  return;
203 
204  case OFF_QUEUE_SELECT:
205  vio.setQueueSelect(value);
206  return;
207 
208  case OFF_QUEUE_NUM:
209  // TODO: We don't support queue resizing, so ignore this for now.
210  warn_once("Ignoring queue resize hint. Requested size: %u\n", value);
211  return;
212 
213  case OFF_QUEUE_ALIGN:
214  // TODO: We currently only support the hard-coded 4k alignment used
215  // in legacy VirtIO.
217  "Unhandled VirtIO alignment size: %u", value);
218  return;
219 
220  case OFF_QUEUE_PFN:
221  vio.setQueueAddress(value);
222  return;
223 
224  case OFF_QUEUE_NOTIFY:
225  vio.onNotify(value);
226  return;
227 
228  case OFF_INTERRUPT_ACK:
229  setInterrupts(interruptStatus & (~value));
230  return;
231 
232  case OFF_STATUS:
233  panic_if(value > 0xff, "Unexpected status: 0x%x\n", value);
234  vio.setDeviceStatus(value);
235  return;
236 
237  /* Read-only registers */
238  case OFF_MAGIC:
239  case OFF_VERSION:
240  case OFF_DEVICE_ID:
241  case OFF_VENDOR_ID:
242  case OFF_HOST_FEATURES:
243  case OFF_QUEUE_NUM_MAX:
245  warn("Guest is trying to write to read-only register 0x%\n",
246  offset);
247  return;
248 
249  default:
250  panic("Unhandled read offset (0x%x)\n", offset);
251  }
252 }
253 
254 void
256 {
257  DPRINTF(VirtIOMMIO, "kick(): Sending interrupt...\n");
259 }
260 
261 void
262 MmioVirtIO::setInterrupts(uint32_t value)
263 {
264  const uint32_t old_ints = interruptStatus;
265  interruptStatus = value;
266 
267  if (!old_ints && interruptStatus) {
268  platform->postPciInt(_interruptID);
269  } else if (old_ints && !interruptStatus) {
270  platform->clearPciInt(_interruptID);
271  }
272 }
273 
VirtIODeviceBase::onNotify
void onNotify(QueueID index)
Driver is requesting service.
Definition: base.cc:383
MmioVirtIO::VENDOR_ID
static const uint32_t VENDOR_ID
Definition: vio_mmio.hh:92
MmioVirtIO::OFF_GUEST_PAGE_SIZE
@ OFF_GUEST_PAGE_SIZE
Definition: vio_mmio.hh:70
MmioVirtIO::guestFeaturesSelect
uint32_t guestFeaturesSelect
Definition: vio_mmio.hh:102
MmioVirtIO::OFF_HOST_FEATURES
@ OFF_HOST_FEATURES
Definition: vio_mmio.hh:66
BasicPioDevice::pioAddr
Addr pioAddr
Address that the device listens to.
Definition: io_device.hh:148
warn
#define warn(...)
Definition: logging.hh:239
MmioVirtIO::read
Tick read(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: vio_mmio.cc:61
Packet::getAddr
Addr getAddr() const
Definition: packet.hh:755
MmioVirtIO::OFF_QUEUE_NUM_MAX
@ OFF_QUEUE_NUM_MAX
Definition: vio_mmio.hh:72
VirtIODeviceBase::readConfig
virtual void readConfig(PacketPtr pkt, Addr cfgOffset)
Read from the configuration space of a device.
Definition: base.cc:419
warn_once
#define warn_once(...)
Definition: logging.hh:243
MmioVirtIO::OFF_QUEUE_PFN
@ OFF_QUEUE_PFN
Definition: vio_mmio.hh:75
MmioVirtIO::OFF_STATUS
@ OFF_STATUS
Definition: vio_mmio.hh:79
MmioVirtIO::OFF_QUEUE_NUM
@ OFF_QUEUE_NUM
Definition: vio_mmio.hh:73
MmioVirtIO::kick
void kick()
Definition: vio_mmio.cc:258
VirtIODeviceBase::getDeviceStatus
DeviceStatus getDeviceStatus() const
Retrieve the device status.
Definition: base.hh:846
MmioVirtIO::OFF_INTERRUPT_ACK
@ OFF_INTERRUPT_ACK
Definition: vio_mmio.hh:78
MmioVirtIO::setInterrupts
void setInterrupts(uint32_t value)
Definition: vio_mmio.cc:265
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:59
VirtIODeviceBase::getQueueAddress
uint32_t getQueueAddress() const
Get the host physical address of the currently active queue.
Definition: base.cc:480
VirtIODeviceBase::getGuestFeatures
FeatureBits getGuestFeatures() const
Get features accepted by the guest driver.
Definition: base.hh:861
Packet::getSize
unsigned getSize() const
Definition: packet.hh:765
MmioVirtIO::OFF_INTERRUPT_STATUS
@ OFF_INTERRUPT_STATUS
Definition: vio_mmio.hh:77
VirtIODeviceBase::deviceFeatures
const FeatureBits deviceFeatures
Feature set offered by the device.
Definition: base.hh:870
VirtIODeviceBase::setQueueAddress
void setQueueAddress(uint32_t address)
Change the host physical address of the currently active queue.
Definition: base.cc:474
VirtIODeviceBase::getQueueSelect
QueueID getQueueSelect() const
Get the currently active queue.
Definition: base.hh:791
hifive.hh
MmioVirtIO::MmioVirtIO
MmioVirtIO(const MmioVirtIOParams &params)
Definition: vio_mmio.cc:45
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:237
VirtQueue::ALIGN_SIZE
static const Addr ALIGN_SIZE
Definition: base.hh:423
MmioVirtIO::OFF_QUEUE_ALIGN
@ OFF_QUEUE_ALIGN
Definition: vio_mmio.hh:74
MmioVirtIO::OFF_VERSION
@ OFF_VERSION
Definition: vio_mmio.hh:63
MmioVirtIO::OFF_GUEST_FEATURES_SELECT
@ OFF_GUEST_FEATURES_SELECT
Definition: vio_mmio.hh:69
MmioVirtIO::VERSION
static const uint32_t VERSION
Definition: vio_mmio.hh:91
VirtIODeviceBase::getQueueSize
uint16_t getQueueSize() const
Get the size (descriptors) of the currently active queue.
Definition: base.hh:829
MmioVirtIO::OFF_CONFIG
@ OFF_CONFIG
Definition: vio_mmio.hh:80
MmioVirtIO::OFF_MAGIC
@ OFF_MAGIC
Definition: vio_mmio.hh:62
MmioVirtIO::write
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: vio_mmio.cc:159
MmioVirtIO::MAGIC
static const uint32_t MAGIC
Definition: vio_mmio.hh:90
MmioVirtIO::interruptStatus
uint32_t interruptStatus
Definition: vio_mmio.hh:104
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
Packet::makeResponse
void makeResponse()
Take a request packet and modify it in place to be suitable for returning as a response to that reque...
Definition: packet.hh:1005
MmioVirtIO::OFF_QUEUE_NOTIFY
@ OFF_QUEUE_NOTIFY
Definition: vio_mmio.hh:76
packet_access.hh
VirtIODeviceBase::setGuestFeatures
void setGuestFeatures(FeatureBits features)
Set feature bits accepted by the guest driver.
Definition: base.cc:395
MmioVirtIO::OFF_HOST_FEATURES_SELECT
@ OFF_HOST_FEATURES_SELECT
Definition: vio_mmio.hh:67
VirtIODeviceBase::setQueueSelect
void setQueueSelect(QueueID idx)
Change currently active queue.
Definition: base.hh:781
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
VirtIODeviceBase::deviceId
const DeviceId deviceId
Device ID (sometimes known as subsystem ID)
Definition: base.hh:864
Packet::getLE
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
Definition: packet_access.hh:75
PlicIntDevice
Definition: plic_device.hh:48
VirtIODeviceBase::setDeviceStatus
void setDeviceStatus(DeviceStatus status)
Update device status and optionally reset device.
Definition: base.cc:409
MmioVirtIO::OFF_VENDOR_ID
@ OFF_VENDOR_ID
Definition: vio_mmio.hh:65
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:258
MmioVirtIO::INT_USED_RING
@ INT_USED_RING
Definition: vio_mmio.hh:86
vio_mmio.hh
Packet::setLE
void setLE(T v)
Set the value in the data pointer to v as little endian.
Definition: packet_access.hh:105
MmioVirtIO::OFF_DEVICE_ID
@ OFF_DEVICE_ID
Definition: vio_mmio.hh:64
MmioVirtIO::~MmioVirtIO
virtual ~MmioVirtIO()
Definition: vio_mmio.cc:56
MmioVirtIO::hostFeaturesSelect
uint32_t hostFeaturesSelect
Definition: vio_mmio.hh:101
MmioVirtIO::OFF_QUEUE_SELECT
@ OFF_QUEUE_SELECT
Definition: vio_mmio.hh:71
VirtIODeviceBase::registerKickCallback
void registerKickCallback(const std::function< void()> &callback)
Register a callback to kick the guest through the transport interface.
Definition: base.hh:753
MmioVirtIO::vio
VirtIODeviceBase & vio
Definition: vio_mmio.hh:107
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
MmioVirtIO::pageSize
uint32_t pageSize
Definition: vio_mmio.hh:103
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153
VirtIODeviceBase::writeConfig
virtual void writeConfig(PacketPtr pkt, Addr cfgOffset)
Write to the configuration space of a device.
Definition: base.cc:425
MmioVirtIO::OFF_GUEST_FEATURES
@ OFF_GUEST_FEATURES
Definition: vio_mmio.hh:68

Generated on Tue Mar 23 2021 19:41:26 for gem5 by doxygen 1.8.17