gem5  v22.0.0.2
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 namespace gem5
46 {
47 
48 MmioVirtIO::MmioVirtIO(const MmioVirtIOParams &params)
49  : BasicPioDevice(params, params.pio_size),
50  hostFeaturesSelect(0), guestFeaturesSelect(0), pageSize(0),
51  interruptStatus(0), vio(*params.vio),
52  interrupt(params.interrupt->get())
53 {
54  fatal_if(!interrupt, "No MMIO VirtIO interrupt specified\n");
55 
56  vio.registerKickCallback([this]() { kick(); });
57 }
58 
60 {
61 }
62 
63 Tick
65 {
66  const Addr offset = pkt->getAddr() - pioAddr;
67  const unsigned size(pkt->getSize());
68 
69  DPRINTF(VIOIface, "Reading %u bytes @ 0x%x:\n", size, offset);
70 
71  // Forward device configuration writes to the device VirtIO model
72  if (offset >= OFF_CONFIG) {
74  return 0;
75  }
76 
77  panic_if(size != 4, "Unexpected read size: %u\n", size);
78 
79  const uint32_t value = read(offset);
80  DPRINTF(VIOIface, " value: 0x%x\n", value);
81  pkt->makeResponse();
82  pkt->setLE<uint32_t>(value);
83 
84  return 0;
85 }
86 
87 uint32_t
89 {
90  switch(offset) {
91  case OFF_MAGIC:
92  return MAGIC;
93 
94  case OFF_VERSION:
95  return VERSION;
96 
97  case OFF_DEVICE_ID:
98  return vio.deviceId;
99 
100  case OFF_VENDOR_ID:
101  return VENDOR_ID;
102 
103  case OFF_HOST_FEATURES:
104  // We only implement 32 bits of this register
105  if (hostFeaturesSelect == 0)
106  return vio.deviceFeatures;
107  else
108  return 0;
109 
111  return hostFeaturesSelect;
112 
113  case OFF_GUEST_FEATURES:
114  // We only implement 32 bits of this register
115  if (guestFeaturesSelect == 0)
116  return vio.getGuestFeatures();
117  else
118  return 0;
119 
121  return hostFeaturesSelect;
122 
123  case OFF_GUEST_PAGE_SIZE:
124  return pageSize;
125 
126  case OFF_QUEUE_SELECT:
127  return vio.getQueueSelect();
128 
129  case OFF_QUEUE_NUM_MAX:
130  return vio.getQueueSize();
131 
132  case OFF_QUEUE_NUM:
133  // TODO: We don't support queue resizing, so ignore this for now.
134  return vio.getQueueSize();
135 
136  case OFF_QUEUE_ALIGN:
137  // TODO: Implement this once we support other alignment sizes
138  return VirtQueue::ALIGN_SIZE;
139 
140  case OFF_QUEUE_PFN:
141  return vio.getQueueAddress();
142 
144  return interruptStatus;
145 
146  case OFF_STATUS:
147  return vio.getDeviceStatus();
148 
149  // Write-only registers
150  case OFF_QUEUE_NOTIFY:
151  case OFF_INTERRUPT_ACK:
152  warn("Guest is trying to read to write-only register 0x%\n",
153  offset);
154  return 0;
155 
156  default:
157  panic("Unhandled read offset (0x%x)\n", offset);
158  }
159 }
160 
161 Tick
163 {
164  const Addr offset = pkt->getAddr() - pioAddr;
165  const unsigned size(pkt->getSize());
166 
167  DPRINTF(VIOIface, "Writing %u bytes @ 0x%x:\n", size, offset);
168 
169  // Forward device configuration writes to the device VirtIO model
170  if (offset >= OFF_CONFIG) {
172  return 0;
173  }
174 
175  panic_if(size != 4, "Unexpected write size @ 0x%x: %u\n", offset, size);
176  DPRINTF(VIOIface, " value: 0x%x\n", pkt->getLE<uint32_t>());
177  pkt->makeResponse();
178  write(offset, pkt->getLE<uint32_t>());
179  return 0;
180 }
181 
182 void
183 MmioVirtIO::write(Addr offset, uint32_t value)
184 {
185  switch(offset) {
187  hostFeaturesSelect = value;
188  return;
189 
190  case OFF_GUEST_FEATURES:
191  if (guestFeaturesSelect == 0) {
192  vio.setGuestFeatures(value);
193  } else if (value != 0) {
194  warn("Setting unimplemented guest features register %u: %u\n",
195  guestFeaturesSelect, value);
196  }
197  return;
198 
200  guestFeaturesSelect = value;
201  return;
202 
203  case OFF_GUEST_PAGE_SIZE:
204  // TODO: We only support 4096 byte pages at the moment
206  "Unhandled VirtIO page size: %u", value);
207  pageSize = value;
208  return;
209 
210  case OFF_QUEUE_SELECT:
211  vio.setQueueSelect(value);
212  return;
213 
214  case OFF_QUEUE_NUM:
215  // TODO: We don't support queue resizing, so ignore this for now.
216  warn_once("Ignoring queue resize hint. Requested size: %u\n", value);
217  return;
218 
219  case OFF_QUEUE_ALIGN:
220  // TODO: We currently only support the hard-coded 4k alignment used
221  // in legacy VirtIO.
223  "Unhandled VirtIO alignment size: %u", value);
224  return;
225 
226  case OFF_QUEUE_PFN:
227  vio.setQueueAddress(value);
228  return;
229 
230  case OFF_QUEUE_NOTIFY:
231  vio.onNotify(value);
232  return;
233 
234  case OFF_INTERRUPT_ACK:
235  setInterrupts(interruptStatus & (~value));
236  return;
237 
238  case OFF_STATUS:
239  panic_if(value > 0xff, "Unexpected status: 0x%x\n", value);
240  vio.setDeviceStatus(value);
241  return;
242 
243  /* Read-only registers */
244  case OFF_MAGIC:
245  case OFF_VERSION:
246  case OFF_DEVICE_ID:
247  case OFF_VENDOR_ID:
248  case OFF_HOST_FEATURES:
249  case OFF_QUEUE_NUM_MAX:
251  warn("Guest is trying to write to read-only register 0x%\n",
252  offset);
253  return;
254 
255  default:
256  panic("Unhandled read offset (0x%x)\n", offset);
257  }
258 }
259 
260 void
262 {
263  DPRINTF(VIOIface, "kick(): Sending interrupt...\n");
265 }
266 
267 void
269 {
270  const uint32_t old_ints = interruptStatus;
271  interruptStatus = value;
272 
273  if (!old_ints && interruptStatus) {
274  interrupt->raise();
275  } else if (old_ints && !interruptStatus) {
276  interrupt->clear();
277  }
278 }
279 
280 } // namespace gem5
gem5::BasicPioDevice::pioAddr
Addr pioAddr
Address that the device listens to.
Definition: io_device.hh:151
gem5::MmioVirtIO::INT_USED_RING
@ INT_USED_RING
Definition: vio_mmio.hh:91
warn
#define warn(...)
Definition: logging.hh:246
gem5::MmioVirtIO::OFF_QUEUE_NUM
@ OFF_QUEUE_NUM
Definition: vio_mmio.hh:77
warn_once
#define warn_once(...)
Definition: logging.hh:250
gem5::MmioVirtIO::OFF_VERSION
@ OFF_VERSION
Definition: vio_mmio.hh:67
gem5::MmioVirtIO::OFF_QUEUE_PFN
@ OFF_QUEUE_PFN
Definition: vio_mmio.hh:79
gem5::MmioVirtIO::OFF_INTERRUPT_STATUS
@ OFF_INTERRUPT_STATUS
Definition: vio_mmio.hh:81
vio_mmio.hh
gem5::MmioVirtIO::OFF_GUEST_FEATURES
@ OFF_GUEST_FEATURES
Definition: vio_mmio.hh:72
base_gic.hh
gem5::MmioVirtIO::interrupt
ArmInterruptPin *const interrupt
Definition: vio_mmio.hh:113
gem5::MmioVirtIO::MmioVirtIO
MmioVirtIO(const MmioVirtIOParams &params)
Definition: vio_mmio.cc:48
gem5::MmioVirtIO::kick
void kick()
Definition: vio_mmio.cc:261
gem5::VirtIODeviceBase::writeConfig
virtual void writeConfig(PacketPtr pkt, Addr cfgOffset)
Write to the configuration space of a device.
Definition: base.cc:428
gem5::VirtIODeviceBase::setGuestFeatures
void setGuestFeatures(FeatureBits features)
Set feature bits accepted by the guest driver.
Definition: base.cc:398
gem5::MmioVirtIO::OFF_HOST_FEATURES_SELECT
@ OFF_HOST_FEATURES_SELECT
Definition: vio_mmio.hh:71
gem5::MmioVirtIO::interruptStatus
uint32_t interruptStatus
Definition: vio_mmio.hh:109
gem5::MmioVirtIO::write
Tick write(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: vio_mmio.cc:162
gem5::MmioVirtIO::vio
VirtIODeviceBase & vio
Definition: vio_mmio.hh:112
gem5::MmioVirtIO::read
Tick read(PacketPtr pkt) override
Pure virtual function that the device must implement.
Definition: vio_mmio.cc:64
gem5::MmioVirtIO::OFF_QUEUE_NUM_MAX
@ OFF_QUEUE_NUM_MAX
Definition: vio_mmio.hh:76
gem5::MmioVirtIO::~MmioVirtIO
virtual ~MmioVirtIO()
Definition: vio_mmio.cc:59
gem5::MmioVirtIO::OFF_CONFIG
@ OFF_CONFIG
Definition: vio_mmio.hh:84
gem5::VirtIODeviceBase::getQueueSize
uint16_t getQueueSize() const
Get the size (descriptors) of the currently active queue.
Definition: base.hh:838
gem5::MmioVirtIO::OFF_QUEUE_SELECT
@ OFF_QUEUE_SELECT
Definition: vio_mmio.hh:75
gem5::MmioVirtIO::setInterrupts
void setInterrupts(uint32_t value)
Definition: vio_mmio.cc:268
gem5::MmioVirtIO::OFF_MAGIC
@ OFF_MAGIC
Definition: vio_mmio.hh:66
gem5::VirtIODeviceBase::setQueueSelect
void setQueueSelect(QueueID idx)
Change currently active queue.
Definition: base.hh:790
gem5::VirtIODeviceBase::registerKickCallback
void registerKickCallback(const std::function< void()> &callback)
Register a callback to kick the guest through the transport interface.
Definition: base.hh:762
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::MmioVirtIO::guestFeaturesSelect
uint32_t guestFeaturesSelect
Definition: vio_mmio.hh:107
gem5::Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:291
gem5::VirtIODeviceBase::setQueueAddress
void setQueueAddress(uint32_t address)
Change the host physical address of the currently active queue.
Definition: base.cc:477
gem5::VirtIODeviceBase::deviceId
const DeviceId deviceId
Device ID (sometimes known as subsystem ID)
Definition: base.hh:873
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::ArmInterruptPin::clear
virtual void clear()=0
Clear a signalled interrupt.
gem5::VirtIODeviceBase::deviceFeatures
const FeatureBits deviceFeatures
Feature set offered by the device.
Definition: base.hh:879
gem5::MmioVirtIO::OFF_GUEST_FEATURES_SELECT
@ OFF_GUEST_FEATURES_SELECT
Definition: vio_mmio.hh:73
gem5::ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:144
gem5::VirtIODeviceBase::onNotify
void onNotify(QueueID index)
Driver is requesting service.
Definition: base.cc:386
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
packet_access.hh
gem5::MmioVirtIO::pageSize
uint32_t pageSize
Definition: vio_mmio.hh:108
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:204
gem5::MmioVirtIO::OFF_INTERRUPT_ACK
@ OFF_INTERRUPT_ACK
Definition: vio_mmio.hh:82
gem5::VirtIODeviceBase::getDeviceStatus
DeviceStatus getDeviceStatus() const
Retrieve the device status.
Definition: base.hh:855
gem5::VirtQueue::ALIGN_SIZE
static const Addr ALIGN_SIZE
Definition: base.hh:431
gem5::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:1044
gem5::VirtIODeviceBase::getQueueSelect
QueueID getQueueSelect() const
Get the currently active queue.
Definition: base.hh:800
gem5::Packet::getLE
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
Definition: packet_access.hh:78
gem5::MmioVirtIO::OFF_VENDOR_ID
@ OFF_VENDOR_ID
Definition: vio_mmio.hh:69
gem5::VirtIODeviceBase::getGuestFeatures
FeatureBits getGuestFeatures() const
Get features accepted by the guest driver.
Definition: base.hh:870
gem5::MmioVirtIO::MAGIC
static const uint32_t MAGIC
Definition: vio_mmio.hh:95
gem5::ArmInterruptPin::raise
virtual void raise()=0
Signal an interrupt.
gem5::Packet::setLE
void setLE(T v)
Set the value in the data pointer to v as little endian.
Definition: packet_access.hh:108
gem5::VirtIODeviceBase::getQueueAddress
uint32_t getQueueAddress() const
Get the host physical address of the currently active queue.
Definition: base.cc:483
gem5::Packet::getAddr
Addr getAddr() const
Definition: packet.hh:790
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:226
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::MmioVirtIO::VENDOR_ID
static const uint32_t VENDOR_ID
Definition: vio_mmio.hh:97
gem5::MmioVirtIO::OFF_QUEUE_ALIGN
@ OFF_QUEUE_ALIGN
Definition: vio_mmio.hh:78
gem5::BasicPioDevice
Definition: io_device.hh:147
gem5::MmioVirtIO::OFF_DEVICE_ID
@ OFF_DEVICE_ID
Definition: vio_mmio.hh:68
gem5::MmioVirtIO::OFF_QUEUE_NOTIFY
@ OFF_QUEUE_NOTIFY
Definition: vio_mmio.hh:80
gem5::VirtIODeviceBase::readConfig
virtual void readConfig(PacketPtr pkt, Addr cfgOffset)
Read from the configuration space of a device.
Definition: base.cc:422
gem5::Packet::getSize
unsigned getSize() const
Definition: packet.hh:800
gem5::MmioVirtIO::VERSION
static const uint32_t VERSION
Definition: vio_mmio.hh:96
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::VirtIODeviceBase::setDeviceStatus
void setDeviceStatus(DeviceStatus status)
Update device status and optionally reset device.
Definition: base.cc:412
gem5::MmioVirtIO::OFF_HOST_FEATURES
@ OFF_HOST_FEATURES
Definition: vio_mmio.hh:70
gem5::MmioVirtIO::hostFeaturesSelect
uint32_t hostFeaturesSelect
Definition: vio_mmio.hh:106
gem5::MmioVirtIO::OFF_STATUS
@ OFF_STATUS
Definition: vio_mmio.hh:83
gem5::MmioVirtIO::OFF_GUEST_PAGE_SIZE
@ OFF_GUEST_PAGE_SIZE
Definition: vio_mmio.hh:74

Generated on Thu Jul 28 2022 13:32:31 for gem5 by doxygen 1.8.17