gem5  v22.1.0.0
pci.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2017 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/virtio/pci.hh"
39 
40 #include "base/bitfield.hh"
41 #include "base/compiler.hh"
42 #include "debug/VIOIface.hh"
43 #include "mem/packet_access.hh"
44 #include "params/PciVirtIO.hh"
45 
46 namespace gem5
47 {
48 
50  : PciDevice(params), queueNotify(0), interruptDeliveryPending(false),
51  vio(*params.vio)
52 {
53  // Override the subsystem ID with the device ID from VirtIO
54  config.subsystemID = htole(vio.deviceId);
55 
56  // The kernel driver expects the BAR size to be an exact power of
57  // two. Nothing else is supported. Therefore, we need to force
58  // that alignment here. We do not touch vio.configSize as this is
59  // used to check accesses later on.
61 
62  vio.registerKickCallback([this]() { kick(); });
63 }
64 
66 {
67 }
68 
69 Tick
71 {
72  [[maybe_unused]] const unsigned size(pkt->getSize());
73  int bar;
74  Addr offset;
75  if (!getBAR(pkt->getAddr(), bar, offset))
76  panic("Invalid PCI memory access to unmapped memory.\n");
77  assert(bar == 0);
78 
79  DPRINTF(VIOIface, "Reading offset 0x%x [len: %i]\n", offset, size);
80 
81  // Forward device configuration writes to the device VirtIO model
82  if (offset >= OFF_VIO_DEVICE) {
84  return 0;
85  }
86 
87  pkt->makeResponse();
88 
89  switch(offset) {
91  DPRINTF(VIOIface, " DEVICE_FEATURES request\n");
92  assert(size == sizeof(uint32_t));
93  pkt->setLE<uint32_t>(vio.deviceFeatures);
94  break;
95 
96  case OFF_GUEST_FEATURES:
97  DPRINTF(VIOIface, " GUEST_FEATURES request\n");
98  assert(size == sizeof(uint32_t));
99  pkt->setLE<uint32_t>(vio.getGuestFeatures());
100  break;
101 
102  case OFF_QUEUE_ADDRESS:
103  DPRINTF(VIOIface, " QUEUE_ADDRESS request\n");
104  assert(size == sizeof(uint32_t));
105  pkt->setLE<uint32_t>(vio.getQueueAddress());
106  break;
107 
108  case OFF_QUEUE_SIZE:
109  DPRINTF(VIOIface, " QUEUE_SIZE request\n");
110  assert(size == sizeof(uint16_t));
111  pkt->setLE<uint16_t>(vio.getQueueSize());
112  break;
113 
114  case OFF_QUEUE_SELECT:
115  DPRINTF(VIOIface, " QUEUE_SELECT\n");
116  assert(size == sizeof(uint16_t));
117  pkt->setLE<uint16_t>(vio.getQueueSelect());
118  break;
119 
120  case OFF_QUEUE_NOTIFY:
121  DPRINTF(VIOIface, " QUEUE_NOTIFY request\n");
122  assert(size == sizeof(uint16_t));
123  pkt->setLE<uint16_t>(queueNotify);
124  break;
125 
126  case OFF_DEVICE_STATUS:
127  DPRINTF(VIOIface, " DEVICE_STATUS request\n");
128  assert(size == sizeof(uint8_t));
129  pkt->setLE<uint8_t>(vio.getDeviceStatus());
130  break;
131 
132  case OFF_ISR_STATUS: {
133  DPRINTF(VIOIface, " ISR_STATUS\n");
134  assert(size == sizeof(uint8_t));
135  const uint8_t isr_status(interruptDeliveryPending ? 1 : 0);
137  interruptDeliveryPending = false;
138  intrClear();
139  }
140  pkt->setLE<uint8_t>(isr_status);
141  } break;
142 
143  default:
144  panic("Unhandled read offset (0x%x)\n", offset);
145  }
146 
147  return 0;
148 }
149 
150 Tick
152 {
153  [[maybe_unused]] const unsigned size(pkt->getSize());
154  int bar;
155  Addr offset;
156  if (!getBAR(pkt->getAddr(), bar, offset))
157  panic("Invalid PCI memory access to unmapped memory.\n");
158  assert(bar == 0);
159 
160  DPRINTF(VIOIface, "Writing offset 0x%x [len: %i]\n", offset, size);
161 
162  // Forward device configuration writes to the device VirtIO model
163  if (offset >= OFF_VIO_DEVICE) {
165  return 0;
166  }
167 
168  pkt->makeResponse();
169 
170  switch(offset) {
171  case OFF_DEVICE_FEATURES:
172  warn("Guest tried to write device features.");
173  break;
174 
175  case OFF_GUEST_FEATURES:
176  DPRINTF(VIOIface, " WRITE GUEST_FEATURES request\n");
177  assert(size == sizeof(uint32_t));
178  vio.setGuestFeatures(pkt->getLE<uint32_t>());
179  break;
180 
181  case OFF_QUEUE_ADDRESS:
182  DPRINTF(VIOIface, " WRITE QUEUE_ADDRESS\n");
183  assert(size == sizeof(uint32_t));
184  vio.setQueueAddress(pkt->getLE<uint32_t>());
185  break;
186 
187  case OFF_QUEUE_SIZE:
188  panic("Guest tried to write queue size.");
189  break;
190 
191  case OFF_QUEUE_SELECT:
192  DPRINTF(VIOIface, " WRITE QUEUE_SELECT\n");
193  assert(size == sizeof(uint16_t));
194  vio.setQueueSelect(pkt->getLE<uint16_t>());
195  break;
196 
197  case OFF_QUEUE_NOTIFY:
198  DPRINTF(VIOIface, " WRITE QUEUE_NOTIFY\n");
199  assert(size == sizeof(uint16_t));
200  queueNotify = pkt->getLE<uint16_t>();
202  break;
203 
204  case OFF_DEVICE_STATUS: {
205  assert(size == sizeof(uint8_t));
206  uint8_t status(pkt->getLE<uint8_t>());
207  DPRINTF(VIOIface, "VirtIO set status: 0x%x\n", status);
209  } break;
210 
211  case OFF_ISR_STATUS:
212  warn("Guest tried to write ISR status.");
213  break;
214 
215  default:
216  panic("Unhandled read offset (0x%x)\n", offset);
217  }
218 
219  return 0;
220 }
221 
222 void
224 {
225  DPRINTF(VIOIface, "kick(): Sending interrupt...\n");
227  intrPost();
228 }
229 
230 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
DmaDeviceParams Params
Definition: dma_device.hh:209
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
Addr getAddr() const
Definition: packet.hh:805
void setLE(T v)
Set the value in the data pointer to v as little endian.
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:1059
unsigned getSize() const
Definition: packet.hh:815
T getLE() const
Get the data in the packet byte swapped from little endian to host endian.
PCI device, base implementation is only config space.
Definition: device.hh:270
PCIConfig config
The current config space.
Definition: device.hh:275
void intrClear()
Definition: device.hh:365
bool getBAR(Addr addr, int &num, Addr &offs)
Which base address register (if any) maps the given address?
Definition: device.hh:320
std::array< PciBar *, 6 > BARs
Definition: device.hh:308
void intrPost()
Definition: device.hh:364
virtual ~PciVirtIO()
Definition: pci.cc:65
static const Addr OFF_GUEST_FEATURES
Definition: pci.hh:67
Tick read(PacketPtr pkt)
Pure virtual function that the device must implement.
Definition: pci.cc:70
Tick write(PacketPtr pkt)
Pure virtual function that the device must implement.
Definition: pci.cc:151
static const Addr OFF_DEVICE_FEATURES
Offsets into VirtIO header (BAR0 relative).
Definition: pci.hh:66
static const Addr OFF_QUEUE_SELECT
Definition: pci.hh:70
static const Addr OFF_QUEUE_NOTIFY
Definition: pci.hh:71
VirtIODeviceBase & vio
Definition: pci.hh:85
static const Addr OFF_QUEUE_ADDRESS
Definition: pci.hh:68
PciVirtIO(const Params &params)
Definition: pci.cc:49
static const Addr OFF_VIO_DEVICE
Definition: pci.hh:74
static const Addr BAR0_SIZE_BASE
Definition: pci.hh:78
bool interruptDeliveryPending
Definition: pci.hh:83
static const Addr OFF_QUEUE_SIZE
Definition: pci.hh:69
static const Addr OFF_DEVICE_STATUS
Definition: pci.hh:72
static const Addr OFF_ISR_STATUS
Definition: pci.hh:73
void kick()
Definition: pci.cc:223
VirtIODeviceBase::QueueID queueNotify
Definition: pci.hh:81
void setDeviceStatus(DeviceStatus status)
Update device status and optionally reset device.
Definition: base.cc:412
const size_t configSize
Size of the device's configuration space.
Definition: base.hh:876
virtual void writeConfig(PacketPtr pkt, Addr cfgOffset)
Write to the configuration space of a device.
Definition: base.cc:428
FeatureBits getGuestFeatures() const
Get features accepted by the guest driver.
Definition: base.hh:870
QueueID getQueueSelect() const
Get the currently active queue.
Definition: base.hh:800
DeviceStatus getDeviceStatus() const
Retrieve the device status.
Definition: base.hh:855
uint32_t getQueueAddress() const
Get the host physical address of the currently active queue.
Definition: base.cc:483
const FeatureBits deviceFeatures
Feature set offered by the device.
Definition: base.hh:879
void onNotify(QueueID index)
Driver is requesting service.
Definition: base.cc:386
void setQueueSelect(QueueID idx)
Change currently active queue.
Definition: base.hh:790
uint16_t getQueueSize() const
Get the size (descriptors) of the currently active queue.
Definition: base.hh:838
void registerKickCallback(const std::function< void()> &callback)
Register a callback to kick the guest through the transport interface.
Definition: base.hh:762
virtual void readConfig(PacketPtr pkt, Addr cfgOffset)
Read from the configuration space of a device.
Definition: base.cc:422
void setQueueAddress(uint32_t address)
Change the host physical address of the currently active queue.
Definition: base.cc:477
const DeviceId deviceId
Device ID (sometimes known as subsystem ID)
Definition: base.hh:873
void setGuestFeatures(FeatureBits features)
Set feature bits accepted by the guest driver.
Definition: base.cc:398
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:369
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
#define warn(...)
Definition: logging.hh:246
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 5, 0 > status
Definition: misc_types.hh:429
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
uint64_t Tick
Tick count type.
Definition: types.hh:58
T htole(T value)
Definition: byteswap.hh:172

Generated on Wed Dec 21 2022 10:22:35 for gem5 by doxygen 1.9.1