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

Generated on Wed Sep 30 2020 14:02:10 for gem5 by doxygen 1.8.17