gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
device.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 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 * Copyright (c) 2004-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41/* @file
42 * Interface for devices using PCI configuration
43 */
44
45#ifndef __DEV_PCI_DEVICE_HH__
46#define __DEV_PCI_DEVICE_HH__
47
48#include <cstring>
49#include <vector>
50
51#include "dev/dma_device.hh"
52#include "dev/pci/host.hh"
53#include "dev/pci/pcireg.h"
54#include "params/PciBar.hh"
55#include "params/PciBarNone.hh"
56#include "params/PciBridge.hh"
57#include "params/PciDevice.hh"
58#include "params/PciEndpoint.hh"
59#include "params/PciIoBar.hh"
60#include "params/PciLegacyIoBar.hh"
61#include "params/PciMemBar.hh"
62#include "params/PciMemUpperBar.hh"
63#include "sim/byteswap.hh"
64
65#define PCI0_BAR_NUMBER(x) (((x) - PCI0_BASE_ADDR0) >> 0x2);
66#define PCI1_BAR_NUMBER(x) (((x) - PCI1_BASE_ADDR0) >> 0x2);
67
68namespace gem5
69{
70
71class PciBar : public SimObject
72{
73 protected:
74 // The address and size of the region this decoder recognizes.
77
78 public:
79 PciBar(const PciBarParams &p) : SimObject(p) {}
80
81 virtual bool isMem() const { return false; }
82 virtual bool isIo() const { return false; }
83
84 // Accepts a value written to config space, consumes it, and returns what
85 // value config space should actually be set to. Both should be in host
86 // endian format.
87 virtual uint32_t write(const PciHost::DeviceInterface &host,
88 uint32_t val) = 0;
89
90 AddrRange range() const { return AddrRange(_addr, _addr + _size); }
91 Addr addr() const { return _addr; }
92 Addr size() const { return _size; }
93
94 // Hack for devices that don't know their BAR sizes ahead of time :-o.
95 // Don't use unless you have to, since this may not propogate properly
96 // outside of a small window.
97 void size(Addr value) { _size = value; }
98};
99
100class PciBarNone : public PciBar
101{
102 public:
103 PciBarNone(const PciBarNoneParams &p) : PciBar(p) {}
104
105 uint32_t
106 write(const PciHost::DeviceInterface &host, uint32_t val) override
107 {
108 return 0;
109 }
110};
111
112class PciIoBar : public PciBar
113{
114 protected:
116 Bitfield<31, 2> addr;
117 Bitfield<1> reserved;
118 Bitfield<0> io;
120
121 public:
122 PciIoBar(const PciIoBarParams &p, bool legacy=false) : PciBar(p)
123 {
124 _size = p.size;
125 if (!legacy) {
126 Bar bar = _size;
127 fatal_if(!_size || !isPowerOf2(_size) || bar.io || bar.reserved,
128 "Illegal size %d for bar %s.", _size, name());
129 }
130 }
131
132 bool isIo() const override { return true; }
133
134 uint32_t
135 write(const PciHost::DeviceInterface &host, uint32_t val) override
136 {
137 // Mask away the bits fixed by hardware.
138 Bar bar = val & ~(_size - 1);
139 // Set the fixed bits to their correct values.
140 bar.reserved = 0;
141 bar.io = 1;
142
143 // Update our address.
144 _addr = host.pioAddr(bar.addr << 2);
145
146 // Return what should go into config space.
147 return bar;
148 }
149};
150
152{
153 protected:
155
156 public:
157 PciLegacyIoBar(const PciLegacyIoBarParams &p) : PciIoBar(p, true)
158 {
159 // Save the address until we get a host to translate it.
160 fixedAddr = p.addr;
161 }
162
163 uint32_t
164 write(const PciHost::DeviceInterface &host, uint32_t val) override
165 {
166 // Update the address now that we have a host to translate it.
167 _addr = host.pioAddr(fixedAddr);
168 // Ignore writes.
169 return 0;
170 }
171};
172
173class PciMemBar : public PciBar
174{
175 private:
177 Bitfield<31, 3> addr;
178 SubBitUnion(type, 2, 1)
179 Bitfield<2> wide;
180 Bitfield<1> reserved;
182 Bitfield<0> io;
184
185 bool _wide = false;
186 uint64_t _lower = 0;
187 uint64_t _upper = 0;
188
189 public:
190 PciMemBar(const PciMemBarParams &p) : PciBar(p)
191 {
192 _size = p.size;
193 Bar bar = _size;
194 fatal_if(!_size || !isPowerOf2(_size) || bar.io || bar.type,
195 "Illegal size %d for bar %s.", _size, name());
196 }
197
198 bool isMem() const override { return true; }
199
200 uint32_t
201 write(const PciHost::DeviceInterface &host, uint32_t val) override
202 {
203 // Mask away the bits fixed by hardware.
204 Bar bar = val & ~(_size - 1);
205 // Set the fixed bits to their correct values.
206 bar.type.wide = wide() ? 1 : 0;
207 bar.type.reserved = 0;
208 bar.io = 0;
209
210 // Keep track of our lower 32 bits.
211 _lower = bar.addr << 3;
212
213 // Update our address.
214 _addr = host.memAddr(upper() + lower());
215
216 // Return what should go into config space.
217 return bar;
218 }
219
220 bool wide() const { return _wide; }
221 void wide(bool val) { _wide = val; }
222
223 uint64_t upper() const { return _upper; }
224 void
225 upper(const PciHost::DeviceInterface &host, uint32_t val)
226 {
227 _upper = (uint64_t)val << 32;
228
229 // Update our address.
230 _addr = host.memAddr(upper() + lower());
231 }
232
233 uint64_t lower() const { return _lower; }
234};
235
236class PciMemUpperBar : public PciBar
237{
238 private:
239 PciMemBar *_lower = nullptr;
240
241 public:
242 PciMemUpperBar(const PciMemUpperBarParams &p) : PciBar(p)
243 {}
244
245 void
247 {
248 _lower = val;
249 // Let our lower half know we're up here.
250 _lower->wide(true);
251 }
252
253 uint32_t
254 write(const PciHost::DeviceInterface &host, uint32_t val) override
255 {
256 assert(_lower);
257
258 // Mask away bits fixed by hardware, if any.
259 Addr upper = val & ~((_lower->size() - 1) >> 32);
260
261 // Let our lower half know about the update.
262 _lower->upper(host, upper);
263
264 return upper;
265 }
266};
267
268class PciEndpoint;
269class PciBridge;
270
274class PciDevice : public DmaDevice
275{
277 friend PciBridge;
278
279 private:
282
283 bool
285 {
286 return (offs <= PCI_BIST) || (offs == PCI_CAP_PTR) ||
287 (offs == PCI_INTERRUPT_LINE) || (offs == PCI_INTERRUPT_PIN);
288 }
289
290 protected:
292
296 const int PMCAP_BASE;
301
302 const int MSICAP_BASE;
304
305 const int MSIXCAP_BASE;
315
316 const int PXCAP_BASE;
319
323
325
335 bool
336 getBAR(Addr addr, int &num, Addr &offs)
337 {
338 for (int i = 0; i < BARs.size(); i++) {
339 auto *bar = BARs[i];
340 if (!bar || !bar->range().contains(addr))
341 continue;
342 num = i;
343 offs = addr - bar->addr();
344 return true;
345 }
346 return false;
347 }
348
349 public: // Host configuration interface
356 virtual Tick writeConfig(PacketPtr pkt);
357
358
365 virtual Tick readConfig(PacketPtr pkt);
366
367 protected:
369
372
373 public:
374 Addr
375 pciToDma(Addr pci_addr) const
376 {
377 return hostInterface.dmaAddr(pci_addr);
378 }
379
380 void intrPost() { hostInterface.postInt(); }
381 void intrClear() { hostInterface.clearInt(); }
382
383 uint8_t
385 {
386 return letoh(_config.common.interruptLine);
387 }
388
394 AddrRangeList getAddrRanges() const override;
395
401 PciDevice(const PciDeviceParams &params,
402 std::initializer_list<PciBar *> BARs_init);
403
408 void serialize(CheckpointOut &cp) const override;
409
415 void unserialize(CheckpointIn &cp) override;
416
417 const PciBusAddr &busAddr() const { return _busAddr; }
418};
419
420class PciEndpoint : public PciDevice
421{
422 protected:
425 {
426 return _config.type0;
427 }
428
429 public:
435 PciEndpoint(const PciEndpointParams &params);
436
443 Tick writeConfig(PacketPtr pkt) override;
444
450 void unserialize(CheckpointIn &cp) override;
451};
452
453class PciBridge : public PciDevice
454{
455 protected:
458 {
459 return _config.type1;
460 }
461
462 public:
468 PciBridge(const PciBridgeParams &params);
469
476 Tick writeConfig(PacketPtr pkt) override;
477
483 void unserialize(CheckpointIn &cp) override;
484};
485
486} // namespace gem5
487
488#endif // __DEV_PCI_DEVICE_HH__
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition addr_range.hh:82
DmaDevice(const Params &p)
virtual std::string name() const
Definition named.hh:60
PciBarNone(const PciBarNoneParams &p)
Definition device.hh:103
uint32_t write(const PciHost::DeviceInterface &host, uint32_t val) override
Definition device.hh:106
virtual bool isIo() const
Definition device.hh:82
PciBar(const PciBarParams &p)
Definition device.hh:79
Addr _addr
Definition device.hh:75
Addr size() const
Definition device.hh:92
virtual uint32_t write(const PciHost::DeviceInterface &host, uint32_t val)=0
Addr _size
Definition device.hh:76
AddrRange range() const
Definition device.hh:90
Addr addr() const
Definition device.hh:91
void size(Addr value)
Definition device.hh:97
virtual bool isMem() const
Definition device.hh:81
Tick writeConfig(PacketPtr pkt) override
Write to the PCI config space data that is stored locally.
Definition device.cc:716
void unserialize(CheckpointIn &cp) override
Reconstruct the state of this object from a checkpoint.
Definition device.cc:840
PCIConfigType1 & config()
Definition device.hh:457
PciBridge(const PciBridgeParams &params)
Constructor for PCI Dev.
Definition device.cc:687
const int PMCAP_PC_OFFSET
Definition device.hh:298
PciHost::DeviceInterface hostInterface
Definition device.hh:368
const int MSIXCAP_MTAB_OFFSET
Definition device.hh:308
MSICAP msicap
Definition device.hh:303
PciDevice(const PciDeviceParams &params, std::initializer_list< PciBar * > BARs_init)
Constructor for PCI Dev.
Definition device.cc:67
void intrClear()
Definition device.hh:381
void unserialize(CheckpointIn &cp) override
Reconstruct the state of this object from a checkpoint.
Definition device.cc:463
friend PciEndpoint
Definition device.hh:276
std::vector< MSIXTable > msix_table
MSIX Table and PBA Structures.
Definition device.hh:321
friend PciBridge
Definition device.hh:277
void serialize(CheckpointOut &cp) const override
Serialize this object to the given output stream.
Definition device.cc:386
bool getBAR(Addr addr, int &num, Addr &offs)
Which base address register (if any) maps the given address?
Definition device.hh:336
const int MSIXCAP_BASE
Definition device.hh:305
const int PMCAP_BASE
The capability list structures and base addresses.
Definition device.hh:296
Addr pciToDma(Addr pci_addr) const
Definition device.hh:375
AddrRangeList getAddrRanges() const override
Determine the address ranges that this device responds to.
Definition device.cc:267
int MSIX_PBA_OFFSET
Definition device.hh:312
const int PMCAP_PMCS_OFFSET
Definition device.hh:299
const PciBusAddr _busAddr
Definition device.hh:291
std::vector< MSIXPbaEntry > msix_pba
Definition device.hh:322
virtual Tick readConfig(PacketPtr pkt)
Read from the PCI config space data that is stored locally.
Definition device.cc:210
PCIConfig _config
The current config space.
Definition device.hh:281
MSIXCAP msixcap
Definition device.hh:314
virtual Tick writeConfig(PacketPtr pkt)
Write to the PCI config space data that is stored locally.
Definition device.cc:281
int MSIX_TABLE_OFFSET
Definition device.hh:310
const int MSIXCAP_ID_OFFSET
Definition device.hh:306
const int MSIXCAP_MPBA_OFFSET
Definition device.hh:309
std::vector< PciBar * > BARs
Definition device.hh:324
void intrPost()
Definition device.hh:380
bool isCommonConfig(Addr offs)
Definition device.hh:284
uint8_t interruptLine() const
Definition device.hh:384
const int PMCAP_ID_OFFSET
Definition device.hh:297
const PciBusAddr & busAddr() const
Definition device.hh:417
const int MSIXCAP_MXC_OFFSET
Definition device.hh:307
const int PXCAP_BASE
Definition device.hh:316
const int MSICAP_BASE
Definition device.hh:302
PCIConfigType0 & config()
Definition device.hh:424
void unserialize(CheckpointIn &cp) override
Reconstruct the state of this object from a checkpoint.
Definition device.cc:677
PciEndpoint(const PciEndpointParams &params)
Constructor for PCI Dev.
Definition device.cc:577
Tick writeConfig(PacketPtr pkt) override
Write to the PCI config space data that is stored locally.
Definition device.cc:599
Callback interface from PCI devices to the host.
Definition host.hh:95
Addr pioAddr(Addr addr) const
Calculate the physical address of an IO location on the PCI bus.
Definition host.hh:131
Addr memAddr(Addr addr) const
Calculate the physical address of a non-prefetchable memory location in the PCI address space.
Definition host.hh:140
Bitfield< 1 > reserved
Definition device.hh:117
EndBitUnion(Bar) public
Definition device.hh:119
BitUnion32(Bar) Bitfield< 31
Bitfield< 0 > io
Definition device.hh:118
bool isIo() const override
Definition device.hh:132
uint32_t write(const PciHost::DeviceInterface &host, uint32_t val) override
Definition device.hh:135
PciLegacyIoBar(const PciLegacyIoBarParams &p)
Definition device.hh:157
uint32_t write(const PciHost::DeviceInterface &host, uint32_t val) override
Definition device.hh:164
PciMemBar(const PciMemBarParams &p)
Definition device.hh:190
bool isMem() const override
Definition device.hh:198
uint64_t lower() const
Definition device.hh:233
uint32_t write(const PciHost::DeviceInterface &host, uint32_t val) override
Definition device.hh:201
SubBitUnion(type, 2, 1) Bitfield< 2 > wide
uint64_t _upper
Definition device.hh:187
void wide(bool val)
Definition device.hh:221
uint64_t _lower
Definition device.hh:186
EndSubBitUnion(type) Bitfield< 0 > io
EndBitUnion(Bar) bool _wide
Bitfield< 1 > reserved
Definition device.hh:180
void upper(const PciHost::DeviceInterface &host, uint32_t val)
Definition device.hh:225
uint64_t upper() const
Definition device.hh:223
BitUnion32(Bar) Bitfield< 31
bool wide() const
Definition device.hh:220
PciMemBar * _lower
Definition device.hh:239
PciMemUpperBar(const PciMemUpperBarParams &p)
Definition device.hh:242
void lower(PciMemBar *val)
Definition device.hh:246
uint32_t write(const PciHost::DeviceInterface &host, uint32_t val) override
Definition device.hh:254
STL vector class.
Definition stl.hh:37
std::list< AddrRange > AddrRangeList
Convenience typedef for a collection of address ranges.
Definition addr_range.hh:64
static constexpr bool isPowerOf2(const T &n)
Definition intmath.hh:98
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:268
const Params & params() const
SimObject(const Params &p)
Definition sim_object.cc:58
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 26 > io
Bitfield< 0 > p
Bitfield< 63 > val
Definition misc.hh:804
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
T letoh(T value)
Definition byteswap.hh:173
std::ostream CheckpointOut
Definition serialize.hh:66
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
Packet * PacketPtr
#define PCI_INTERRUPT_PIN
Definition pcireg.h:190
#define PCI_BIST
Definition pcireg.h:187
#define PCI_INTERRUPT_LINE
Definition pcireg.h:189
#define PCI_CAP_PTR
Definition pcireg.h:188
Defines the MSI Capability register and its associated bitfields for the a PCI/PCIe device.
Definition pcireg.h:337
Defines the Power Management capability register and all its associated bitfields for a PCIe device.
Definition pcireg.h:302
Defines the PCI Express capability register and its associated bitfields for a PCIe device.
Definition pcireg.h:410

Generated on Mon May 26 2025 09:19:07 for gem5 by doxygen 1.13.2