device.hh revision 2665
110448Snilay@cs.wisc.edu/*
210448Snilay@cs.wisc.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
310448Snilay@cs.wisc.edu * All rights reserved.
410448Snilay@cs.wisc.edu *
510448Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
610448Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions are
710448Snilay@cs.wisc.edu * met: redistributions of source code must retain the above copyright
810448Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer;
910448Snilay@cs.wisc.edu * redistributions in binary form must reproduce the above copyright
1010448Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer in the
1110448Snilay@cs.wisc.edu * documentation and/or other materials provided with the distribution;
1210448Snilay@cs.wisc.edu * neither the name of the copyright holders nor the names of its
1310448Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
1410448Snilay@cs.wisc.edu * this software without specific prior written permission.
1510448Snilay@cs.wisc.edu *
1610448Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710448Snilay@cs.wisc.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810448Snilay@cs.wisc.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910448Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010448Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110448Snilay@cs.wisc.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210447Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310447Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410447Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510447Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610447Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710447Snilay@cs.wisc.edu *
2810447Snilay@cs.wisc.edu * Authors: Ali Saidi
2910447Snilay@cs.wisc.edu *          Andrew Schultz
3010447Snilay@cs.wisc.edu *          Nathan Binkert
3110447Snilay@cs.wisc.edu */
3210447Snilay@cs.wisc.edu
3310447Snilay@cs.wisc.edu/* @file
3410447Snilay@cs.wisc.edu * Interface for devices using PCI configuration
3510447Snilay@cs.wisc.edu */
3610447Snilay@cs.wisc.edu
3710447Snilay@cs.wisc.edu#ifndef __DEV_PCIDEV_HH__
3810447Snilay@cs.wisc.edu#define __DEV_PCIDEV_HH__
3910447Snilay@cs.wisc.edu
4010447Snilay@cs.wisc.edu#include "dev/io_device.hh"
4110447Snilay@cs.wisc.edu#include "dev/pcireg.h"
4210447Snilay@cs.wisc.edu#include "dev/platform.hh"
4310447Snilay@cs.wisc.edu
4410447Snilay@cs.wisc.edu#define BAR_IO_MASK 0x3
4510447Snilay@cs.wisc.edu#define BAR_MEM_MASK 0xF
4610447Snilay@cs.wisc.edu#define BAR_IO_SPACE_BIT 0x1
4710447Snilay@cs.wisc.edu#define BAR_IO_SPACE(x) ((x) & BAR_IO_SPACE_BIT)
4810447Snilay@cs.wisc.edu#define BAR_NUMBER(x) (((x) - PCI0_BASE_ADDR0) >> 0x2);
4910447Snilay@cs.wisc.edu
5010447Snilay@cs.wisc.educlass PciConfigAll;
5110447Snilay@cs.wisc.edu
5210447Snilay@cs.wisc.edu
5310447Snilay@cs.wisc.edu/**
5410447Snilay@cs.wisc.edu * This class encapulates the first 64 bytes of a singles PCI
5510447Snilay@cs.wisc.edu * devices config space that in configured by the configuration file.
5610447Snilay@cs.wisc.edu */
5710447Snilay@cs.wisc.educlass PciConfigData : public SimObject
5810447Snilay@cs.wisc.edu{
59  public:
60    /**
61     * Constructor to initialize the devices config space to 0.
62     */
63    PciConfigData(const std::string &name)
64        : SimObject(name)
65    {
66        memset(config.data, 0, sizeof(config.data));
67        memset(BARAddrs, 0, sizeof(BARAddrs));
68        memset(BARSize, 0, sizeof(BARSize));
69    }
70
71    /** The first 64 bytes */
72    PCIConfig config;
73
74    /** The size of the BARs */
75    uint32_t BARSize[6];
76
77    /** The addresses of the BARs */
78    Addr BARAddrs[6];
79};
80
81/**
82 * PCI device, base implemnation is only config space.
83 * Each device is connected to a PCIConfigSpace device
84 * which returns -1 for everything but the pcidevs that
85 * register with it. This object registers with the PCIConfig space
86 * object.
87 */
88class PciDev : public DmaDevice
89{
90  public:
91    struct Params : public ::PioDevice::Params
92    {
93        /**
94         * A pointer to the configspace all object that calls us when
95         * a read comes to this particular device/function.
96         */
97        PciConfigAll *configSpace;
98
99        /**
100         * A pointer to the object that contains the first 64 bytes of
101         * config space
102         */
103        PciConfigData *configData;
104
105        /** The bus number we are on */
106        uint32_t busNum;
107
108        /** The device number we have */
109        uint32_t deviceNum;
110
111        /** The function number */
112        uint32_t functionNum;
113
114        /** The latency for pio accesses. */
115        Tick pio_delay;
116    };
117
118  public:
119    const Params *params() const { return (const Params *)_params; }
120
121  protected:
122    /** The current config space. Unlike the PciConfigData this is
123     * updated during simulation while continues to reflect what was
124     * in the config file.
125     */
126    PCIConfig config;
127
128    /** The size of the BARs */
129    uint32_t BARSize[6];
130
131    /** The current address mapping of the BARs */
132    Addr BARAddrs[6];
133
134    bool
135    isBAR(Addr addr, int bar) const
136    {
137        assert(bar >= 0 && bar < 6);
138        return BARAddrs[bar] <= addr && addr < BARAddrs[bar] + BARSize[bar];
139    }
140
141    int
142    getBAR(Addr addr)
143    {
144        for (int i = 0; i <= 5; ++i)
145            if (isBAR(addr, i))
146                return i;
147
148        return -1;
149    }
150
151    bool
152    getBAR(Addr paddr, Addr &daddr, int &bar)
153    {
154        int b = getBAR(paddr);
155        if (b < 0)
156            return false;
157
158        daddr = paddr - BARAddrs[b];
159        bar = b;
160        return true;
161    }
162
163  protected:
164    Platform *plat;
165    PciConfigData *configData;
166    Tick pioDelay;
167
168  public:
169    Addr pciToDma(Addr pciAddr) const
170    { return plat->pciToDma(pciAddr); }
171
172    void
173    intrPost()
174    { plat->postPciInt(configData->config.interruptLine); }
175
176    void
177    intrClear()
178    { plat->clearPciInt(configData->config.interruptLine); }
179
180    uint8_t
181    interruptLine()
182    { return configData->config.interruptLine; }
183
184    /** return the address ranges that this device responds to.
185     * @params range_list range list to populate with ranges
186     */
187    void addressRanges(AddrRangeList &range_list);
188
189    /**
190     * Constructor for PCI Dev. This function copies data from the
191     * config file object PCIConfigData and registers the device with
192     * a PciConfigAll object.
193     */
194    PciDev(Params *params);
195
196    /**
197     * Write to the PCI config space data that is stored locally. This may be
198     * overridden by the device but at some point it will eventually call this
199     * for normal operations that it does not need to override.
200     * @param offset the offset into config space
201     * @param size the size of the write
202     * @param data the data to write
203     */
204    virtual void writeConfig(int offset, const uint8_t data);
205    virtual void writeConfig(int offset, const uint16_t data);
206    virtual void writeConfig(int offset, const uint32_t data);
207
208
209    /**
210     * Read from the PCI config space data that is stored locally. This may be
211     * overridden by the device but at some point it will eventually call this
212     * for normal operations that it does not need to override.
213     * @param offset the offset into config space
214     * @param size the size of the read
215     * @param data pointer to the location where the read value should be stored
216     */
217    virtual void readConfig(int offset, uint8_t *data);
218    virtual void readConfig(int offset, uint16_t *data);
219    virtual void readConfig(int offset, uint32_t *data);
220
221    /**
222     * Serialize this object to the given output stream.
223     * @param os The stream to serialize to.
224     */
225    virtual void serialize(std::ostream &os);
226
227    /**
228     * Reconstruct the state of this object from a checkpoint.
229     * @param cp The checkpoint use.
230     * @param section The section name of this object
231     */
232    virtual void unserialize(Checkpoint *cp, const std::string &section);
233};
234#endif // __DEV_PCIDEV_HH__
235