2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Interface for devices using PCI configuration
37 #ifndef __DEV_PCIDEV_HH__
38 #define __DEV_PCIDEV_HH__
42 #include "dev/io_device.hh"
43 #include "dev/pcireg.h"
44 #include "dev/platform.hh"
45 #include "sim/byteswap.hh"
47 #define BAR_IO_MASK 0x3
48 #define BAR_MEM_MASK 0xF
49 #define BAR_IO_SPACE_BIT 0x1
50 #define BAR_IO_SPACE(x) ((x) & BAR_IO_SPACE_BIT)
51 #define BAR_NUMBER(x) (((x) - PCI0_BASE_ADDR0) >> 0x2);
55 * This class encapulates the first 64 bytes of a singles PCI
56 * devices config space that in configured by the configuration file.
58 class PciConfigData : public SimObject
62 * Constructor to initialize the devices config space to 0.
64 PciConfigData(const std::string &name)
67 std::memset(config.data, 0, sizeof(config.data));
68 std::memset(BARSize, 0, sizeof(BARSize));
71 /** The first 64 bytes */
74 /** The size of the BARs */
80 * PCI device, base implementation is only config space.
82 class PciDev : public DmaDevice
84 class PciConfigPort : public SimpleTimingPort
89 virtual Tick recvAtomic(PacketPtr pkt);
91 virtual void getDeviceAddressRanges(AddrRangeList &resp,
92 AddrRangeList &snoop);
103 PciConfigPort(PciDev *dev, int busid, int devid, int funcid,
108 struct Params : public PioDevice::Params
111 * A pointer to the object that contains the first 64 bytes of
114 PciConfigData *configData;
116 /** The bus number we are on */
119 /** The device number we have */
122 /** The function number */
123 uint32_t functionNum;
125 /** The latency for pio accesses. */
128 /** The latency for a config access. */
133 const Params *params() const { return (const Params *)_params; }
136 /** The current config space. Unlike the PciConfigData this is
137 * updated during simulation while continues to reflect what was
138 * in the config file.
142 /** The size of the BARs */
145 /** The current address mapping of the BARs */
149 * Does the given address lie within the space mapped by the given
150 * base address register?
153 isBAR(Addr addr, int bar) const
155 assert(bar >= 0 && bar < 6);
156 return BARAddrs[bar] <= addr && addr < BARAddrs[bar] + BARSize[bar];
160 * Which base address register (if any) maps the given address?
161 * @return The BAR number (0-5 inclusive), or -1 if none.
166 for (int i = 0; i <= 5; ++i)
174 * Which base address register (if any) maps the given address?
175 * @param addr The address to check.
176 * @retval bar The BAR number (0-5 inclusive),
177 * only valid if return value is true.
178 * @retval offs The offset from the base address,
179 * only valid if return value is true.
180 * @return True iff address maps to a base address register's region.
183 getBAR(Addr addr, int &bar, Addr &offs)
185 int b = getBAR(addr);
189 offs = addr - BARAddrs[b];
196 PciConfigData *configData;
199 PciConfigPort *configPort;
202 * Write to the PCI config space data that is stored locally. This may be
203 * overridden by the device but at some point it will eventually call this
204 * for normal operations that it does not need to override.
205 * @param pkt packet containing the write the offset into config space
207 virtual Tick writeConfig(PacketPtr pkt);
211 * Read from the PCI config space data that is stored locally. This may be
212 * overridden by the device but at some point it will eventually call this
213 * for normal operations that it does not need to override.
214 * @param pkt packet containing the write the offset into config space
216 virtual Tick readConfig(PacketPtr pkt);
219 Addr pciToDma(Addr pciAddr) const
220 { return plat->pciToDma(pciAddr); }
224 { plat->postPciInt(letoh(configData->config.interruptLine)); }
228 { plat->clearPciInt(letoh(configData->config.interruptLine)); }
232 { return letoh(configData->config.interruptLine); }
234 /** return the address ranges that this device responds to.
235 * @params range_list range list to populate with ranges
237 void addressRanges(AddrRangeList &range_list);
240 * Constructor for PCI Dev. This function copies data from the
241 * config file object PCIConfigData and registers the device with
242 * a PciConfigAll object.
244 PciDev(Params *params);
249 * Serialize this object to the given output stream.
250 * @param os The stream to serialize to.
252 virtual void serialize(std::ostream &os);
255 * Reconstruct the state of this object from a checkpoint.
256 * @param cp The checkpoint use.
257 * @param section The section name of this object
259 virtual void unserialize(Checkpoint *cp, const std::string §ion);
262 virtual unsigned int drain(Event *de);
264 virtual Port *getPort(const std::string &if_name, int idx = -1)
266 if (if_name == "config") {
267 if (configPort != NULL)
268 panic("pciconfig port already connected to.");
269 configPort = new PciConfigPort(this, params()->busNum,
270 params()->deviceNum, params()->functionNum,
274 return DmaDevice::getPort(if_name, idx);
278 #endif // __DEV_PCIDEV_HH__