Config: Cause a fatal() when a parameter without a default value isn't set(FS #315).
[gem5.git] / src / dev / pcidev.hh
index fc477390834f7788743ecdd7f86f23c3e2f48a09..a584a957d2e255c32d2ebcb378023300eef0bdc5 100644 (file)
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Authors: Ali Saidi
+ *          Andrew Schultz
+ *          Nathan Binkert
  */
 
 /* @file
 #ifndef __DEV_PCIDEV_HH__
 #define __DEV_PCIDEV_HH__
 
+#include <cstring>
+
 #include "dev/io_device.hh"
 #include "dev/pcireg.h"
 #include "dev/platform.hh"
+#include "params/PciDevice.hh"
+#include "sim/byteswap.hh"
 
 #define BAR_IO_MASK 0x3
 #define BAR_MEM_MASK 0xF
 #define BAR_IO_SPACE(x) ((x) & BAR_IO_SPACE_BIT)
 #define BAR_NUMBER(x) (((x) - PCI0_BASE_ADDR0) >> 0x2);
 
-class PciConfigAll;
 
 
 /**
- * This class encapulates the first 64 bytes of a singles PCI
- * devices config space that in configured by the configuration file.
- */
-class PciConfigData : public SimObject
-{
-  public:
-    /**
-     * Constructor to initialize the devices config space to 0.
-     */
-    PciConfigData(const std::string &name)
-        : SimObject(name)
-    {
-        memset(config.data, 0, sizeof(config.data));
-        memset(BARAddrs, 0, sizeof(BARAddrs));
-        memset(BARSize, 0, sizeof(BARSize));
-    }
-
-    /** The first 64 bytes */
-    PCIConfig config;
-
-    /** The size of the BARs */
-    uint32_t BARSize[6];
-
-    /** The addresses of the BARs */
-    Addr BARAddrs[6];
-};
-
-/**
- * PCI device, base implemnation is only config space.
- * Each device is connected to a PCIConfigSpace device
- * which returns -1 for everything but the pcidevs that
- * register with it. This object registers with the PCIConfig space
- * object.
+ * PCI device, base implementation is only config space.
  */
 class PciDev : public DmaDevice
 {
-  public:
-    struct Params : public ::PioDevice::Params
+    class PciConfigPort : public SimpleTimingPort
     {
-        /**
-         * A pointer to the configspace all object that calls us when
-         * a read comes to this particular device/function.
-         */
-        PciConfigAll *configSpace;
+      protected:
+        PciDev *device;
 
-        /**
-         * A pointer to the object that contains the first 64 bytes of
-         * config space
-         */
-        PciConfigData *configData;
+        virtual Tick recvAtomic(PacketPtr pkt);
 
-        /** The bus number we are on */
-        uint32_t busNum;
+        virtual void getDeviceAddressRanges(AddrRangeList &resp,
+                                            bool &snoop);
 
-        /** The device number we have */
-        uint32_t deviceNum;
+        Platform *platform;
 
-        /** The function number */
-        uint32_t functionNum;
+        int busId;
+        int deviceId;
+        int functionId;
 
-        /** The latency for pio accesses. */
-        Tick pio_delay;
+        Addr configAddr;
+
+      public:
+        PciConfigPort(PciDev *dev, int busid, int devid, int funcid,
+                      Platform *p);
     };
 
   public:
-    const Params *params() const { return (const Params *)_params; }
+    typedef PciDeviceParams Params;
+    const Params *
+    params() const
+    {
+        return dynamic_cast<const Params *>(_params);
+    }
 
   protected:
-    /** The current config space. Unlike the PciConfigData this is
-     * updated during simulation while continues to reflect what was
-     * in the config file.
-     */
+    /** The current config space.  */
     PCIConfig config;
 
     /** The size of the BARs */
@@ -127,6 +99,10 @@ class PciDev : public DmaDevice
     /** The current address mapping of the BARs */
     Addr BARAddrs[6];
 
+    /**
+     * Does the given address lie within the space mapped by the given
+     * base address register?
+     */
     bool
     isBAR(Addr addr, int bar) const
     {
@@ -134,6 +110,10 @@ class PciDev : public DmaDevice
         return BARAddrs[bar] <= addr && addr < BARAddrs[bar] + BARSize[bar];
     }
 
+    /**
+     * Which base address register (if any) maps the given address?
+     * @return The BAR number (0-5 inclusive), or -1 if none.
+     */
     int
     getBAR(Addr addr)
     {
@@ -144,22 +124,49 @@ class PciDev : public DmaDevice
         return -1;
     }
 
+    /**
+     * Which base address register (if any) maps the given address?
+     * @param addr The address to check.
+     * @retval bar The BAR number (0-5 inclusive),
+     *             only valid if return value is true.
+     * @retval offs The offset from the base address,
+     *              only valid if return value is true.
+     * @return True iff address maps to a base address register's region.
+     */
     bool
-    getBAR(Addr paddr, Addr &daddr, int &bar)
+    getBAR(Addr addr, int &bar, Addr &offs)
     {
-        int b = getBAR(paddr);
+        int b = getBAR(addr);
         if (b < 0)
             return false;
 
-        daddr = paddr - BARAddrs[b];
+        offs = addr - BARAddrs[b];
         bar = b;
         return true;
     }
 
   protected:
     Platform *plat;
-    PciConfigData *configData;
     Tick pioDelay;
+    Tick configDelay;
+    PciConfigPort *configPort;
+
+    /**
+     * Write to the PCI config space data that is stored locally. This may be
+     * overridden by the device but at some point it will eventually call this
+     * for normal operations that it does not need to override.
+     * @param pkt packet containing the write the offset into config space
+     */
+    virtual Tick writeConfig(PacketPtr pkt);
+
+
+    /**
+     * Read from the PCI config space data that is stored locally. This may be
+     * overridden by the device but at some point it will eventually call this
+     * for normal operations that it does not need to override.
+     * @param pkt packet containing the write the offset into config space
+     */
+    virtual Tick readConfig(PacketPtr pkt);
 
   public:
     Addr pciToDma(Addr pciAddr) const
@@ -167,15 +174,15 @@ class PciDev : public DmaDevice
 
     void
     intrPost()
-    { plat->postPciInt(configData->config.interruptLine); }
+    { plat->postPciInt(letoh(config.interruptLine)); }
 
     void
     intrClear()
-    { plat->clearPciInt(configData->config.interruptLine); }
+    { plat->clearPciInt(letoh(config.interruptLine)); }
 
     uint8_t
     interruptLine()
-    { return configData->config.interruptLine; }
+    { return letoh(config.interruptLine); }
 
     /** return the address ranges that this device responds to.
      * @params range_list range list to populate with ranges
@@ -187,32 +194,9 @@ class PciDev : public DmaDevice
      * config file object PCIConfigData and registers the device with
      * a PciConfigAll object.
      */
-    PciDev(Params *params);
-
-    /**
-     * Write to the PCI config space data that is stored locally. This may be
-     * overridden by the device but at some point it will eventually call this
-     * for normal operations that it does not need to override.
-     * @param offset the offset into config space
-     * @param size the size of the write
-     * @param data the data to write
-     */
-    virtual void writeConfig(int offset, const uint8_t data);
-    virtual void writeConfig(int offset, const uint16_t data);
-    virtual void writeConfig(int offset, const uint32_t data);
+    PciDev(const Params *params);
 
-
-    /**
-     * Read from the PCI config space data that is stored locally. This may be
-     * overridden by the device but at some point it will eventually call this
-     * for normal operations that it does not need to override.
-     * @param offset the offset into config space
-     * @param size the size of the read
-     * @param data pointer to the location where the read value should be stored
-     */
-    virtual void readConfig(int offset, uint8_t *data);
-    virtual void readConfig(int offset, uint16_t *data);
-    virtual void readConfig(int offset, uint32_t *data);
+    virtual void init();
 
     /**
      * Serialize this object to the given output stream.
@@ -226,5 +210,22 @@ class PciDev : public DmaDevice
      * @param section The section name of this object
      */
     virtual void unserialize(Checkpoint *cp, const std::string &section);
+
+
+    virtual unsigned int drain(Event *de);
+
+    virtual Port *getPort(const std::string &if_name, int idx = -1)
+    {
+        if (if_name == "config") {
+            if (configPort != NULL)
+                panic("pciconfig port already connected to.");
+            configPort = new PciConfigPort(this, params()->pci_bus,
+                    params()->pci_dev, params()->pci_func,
+                    params()->platform);
+            return configPort;
+        }
+        return DmaDevice::getPort(if_name, idx);
+    }
+
 };
 #endif // __DEV_PCIDEV_HH__