device.hh revision 11260
16019Shines@cs.fsu.edu/*
27111Sgblack@eecs.umich.edu * Copyright (c) 2013 ARM Limited
37111Sgblack@eecs.umich.edu * All rights reserved
47111Sgblack@eecs.umich.edu *
57111Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
67111Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
77111Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
87111Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
97111Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
107111Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
117111Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
127111Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
137111Sgblack@eecs.umich.edu *
146019Shines@cs.fsu.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
156019Shines@cs.fsu.edu * All rights reserved.
166019Shines@cs.fsu.edu *
176019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
186019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
196019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
206019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
216019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
226019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
236019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
246019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
256019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
266019Shines@cs.fsu.edu * this software without specific prior written permission.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396019Shines@cs.fsu.edu *
406019Shines@cs.fsu.edu * Authors: Ali Saidi
416019Shines@cs.fsu.edu *          Andrew Schultz
426019Shines@cs.fsu.edu *          Nathan Binkert
436019Shines@cs.fsu.edu */
446019Shines@cs.fsu.edu
456019Shines@cs.fsu.edu/* @file
466019Shines@cs.fsu.edu * Interface for devices using PCI configuration
476019Shines@cs.fsu.edu */
487692SAli.Saidi@ARM.com
496242Sgblack@eecs.umich.edu#ifndef __DEV_PCI_DEVICE_HH__
506019Shines@cs.fsu.edu#define __DEV_PCI_DEVICE_HH__
517678Sgblack@eecs.umich.edu
527408Sgblack@eecs.umich.edu#include <cstring>
536216Snate@binkert.org#include <vector>
547720Sgblack@eecs.umich.edu
556019Shines@cs.fsu.edu#include "dev/dma_device.hh"
566019Shines@cs.fsu.edu#include "dev/pci/host.hh"
576019Shines@cs.fsu.edu#include "dev/pci/pcireg.h"
586019Shines@cs.fsu.edu#include "params/PciDevice.hh"
597751SAli.Saidi@ARM.com#include "sim/byteswap.hh"
607751SAli.Saidi@ARM.com
617751SAli.Saidi@ARM.com#define BAR_IO_MASK 0x3
627751SAli.Saidi@ARM.com#define BAR_MEM_MASK 0xF
637751SAli.Saidi@ARM.com#define BAR_IO_SPACE_BIT 0x1
647751SAli.Saidi@ARM.com#define BAR_IO_SPACE(x) ((x) & BAR_IO_SPACE_BIT)
657751SAli.Saidi@ARM.com#define BAR_NUMBER(x) (((x) - PCI0_BASE_ADDR0) >> 0x2);
667751SAli.Saidi@ARM.com
677751SAli.Saidi@ARM.com/**
688303SAli.Saidi@ARM.com * PCI device, base implementation is only config space.
697751SAli.Saidi@ARM.com */
708303SAli.Saidi@ARM.comclass PciDevice : public DmaDevice
718303SAli.Saidi@ARM.com{
728303SAli.Saidi@ARM.com  protected:
737751SAli.Saidi@ARM.com    const PciBusAddr _busAddr;
747720Sgblack@eecs.umich.edu
758303SAli.Saidi@ARM.com    /** The current config space.  */
768303SAli.Saidi@ARM.com    PCIConfig config;
778303SAli.Saidi@ARM.com
788303SAli.Saidi@ARM.com    /** The capability list structures and base addresses
798303SAli.Saidi@ARM.com     * @{
808303SAli.Saidi@ARM.com     */
818303SAli.Saidi@ARM.com    const int PMCAP_BASE;
828303SAli.Saidi@ARM.com    const int PMCAP_ID_OFFSET;
838303SAli.Saidi@ARM.com    const int PMCAP_PC_OFFSET;
848303SAli.Saidi@ARM.com    const int PMCAP_PMCS_OFFSET;
858303SAli.Saidi@ARM.com    PMCAP pmcap;
868303SAli.Saidi@ARM.com
878303SAli.Saidi@ARM.com    const int MSICAP_BASE;
888303SAli.Saidi@ARM.com    MSICAP msicap;
897751SAli.Saidi@ARM.com
907751SAli.Saidi@ARM.com    const int MSIXCAP_BASE;
917751SAli.Saidi@ARM.com    const int MSIXCAP_ID_OFFSET;
927751SAli.Saidi@ARM.com    const int MSIXCAP_MXC_OFFSET;
937720Sgblack@eecs.umich.edu    const int MSIXCAP_MTAB_OFFSET;
947751SAli.Saidi@ARM.com    const int MSIXCAP_MPBA_OFFSET;
957720Sgblack@eecs.umich.edu    int MSIX_TABLE_OFFSET;
967751SAli.Saidi@ARM.com    int MSIX_TABLE_END;
977751SAli.Saidi@ARM.com    int MSIX_PBA_OFFSET;
987751SAli.Saidi@ARM.com    int MSIX_PBA_END;
997751SAli.Saidi@ARM.com    MSIXCAP msixcap;
1007751SAli.Saidi@ARM.com
1017751SAli.Saidi@ARM.com    const int PXCAP_BASE;
1026242Sgblack@eecs.umich.edu    PXCAP pxcap;
1037751SAli.Saidi@ARM.com    /** @} */
1047751SAli.Saidi@ARM.com
1057751SAli.Saidi@ARM.com    /** MSIX Table and PBA Structures */
1067751SAli.Saidi@ARM.com    std::vector<MSIXTable> msix_table;
1076019Shines@cs.fsu.edu    std::vector<MSIXPbaEntry> msix_pba;
1087751SAli.Saidi@ARM.com
1096246Sgblack@eecs.umich.edu    /** The size of the BARs */
1107751SAli.Saidi@ARM.com    uint32_t BARSize[6];
1117751SAli.Saidi@ARM.com
1127751SAli.Saidi@ARM.com    /** The current address mapping of the BARs */
1137751SAli.Saidi@ARM.com    Addr BARAddrs[6];
1147751SAli.Saidi@ARM.com
1156329Sgblack@eecs.umich.edu    /** Whether the BARs are really hardwired legacy IO locations. */
1167751SAli.Saidi@ARM.com    bool legacyIO[6];
1176757SAli.Saidi@ARM.com
1187751SAli.Saidi@ARM.com    /**
1197751SAli.Saidi@ARM.com     * Does the given address lie within the space mapped by the given
1207751SAli.Saidi@ARM.com     * base address register?
1217751SAli.Saidi@ARM.com     */
1227751SAli.Saidi@ARM.com    bool
1237638Sgblack@eecs.umich.edu    isBAR(Addr addr, int bar) const
1247751SAli.Saidi@ARM.com    {
1257751SAli.Saidi@ARM.com        assert(bar >= 0 && bar < 6);
1267751SAli.Saidi@ARM.com        return BARAddrs[bar] <= addr && addr < BARAddrs[bar] + BARSize[bar];
1277751SAli.Saidi@ARM.com    }
1287751SAli.Saidi@ARM.com
1297638Sgblack@eecs.umich.edu    /**
1307751SAli.Saidi@ARM.com     * Which base address register (if any) maps the given address?
1317751SAli.Saidi@ARM.com     * @return The BAR number (0-5 inclusive), or -1 if none.
1327751SAli.Saidi@ARM.com     */
1337751SAli.Saidi@ARM.com    int
1347751SAli.Saidi@ARM.com    getBAR(Addr addr)
1357638Sgblack@eecs.umich.edu    {
1367751SAli.Saidi@ARM.com        for (int i = 0; i <= 5; ++i)
1377751SAli.Saidi@ARM.com            if (isBAR(addr, i))
1387751SAli.Saidi@ARM.com                return i;
1397751SAli.Saidi@ARM.com
1407751SAli.Saidi@ARM.com        return -1;
1416757SAli.Saidi@ARM.com    }
1427751SAli.Saidi@ARM.com
1437751SAli.Saidi@ARM.com    /**
1447751SAli.Saidi@ARM.com     * Which base address register (if any) maps the given address?
1457751SAli.Saidi@ARM.com     * @param addr The address to check.
1467751SAli.Saidi@ARM.com     * @retval bar The BAR number (0-5 inclusive),
1477751SAli.Saidi@ARM.com     *             only valid if return value is true.
1487640Sgblack@eecs.umich.edu     * @retval offs The offset from the base address,
1497751SAli.Saidi@ARM.com     *              only valid if return value is true.
1507751SAli.Saidi@ARM.com     * @return True iff address maps to a base address register's region.
1517751SAli.Saidi@ARM.com     */
1528206SWilliam.Wang@arm.com    bool
1538206SWilliam.Wang@arm.com    getBAR(Addr addr, int &bar, Addr &offs)
1548206SWilliam.Wang@arm.com    {
1558206SWilliam.Wang@arm.com        int b = getBAR(addr);
1568206SWilliam.Wang@arm.com        if (b < 0)
1578206SWilliam.Wang@arm.com            return false;
1587751SAli.Saidi@ARM.com
1597640Sgblack@eecs.umich.edu        offs = addr - BARAddrs[b];
1607751SAli.Saidi@ARM.com        bar = b;
1617751SAli.Saidi@ARM.com        return true;
1627751SAli.Saidi@ARM.com    }
1637751SAli.Saidi@ARM.com
1647751SAli.Saidi@ARM.com  public: // Host configuration interface
1657640Sgblack@eecs.umich.edu    /**
1667707Sgblack@eecs.umich.edu     * Write to the PCI config space data that is stored locally. This may be
1676757SAli.Saidi@ARM.com     * overridden by the device but at some point it will eventually call this
1687693SAli.Saidi@ARM.com     * for normal operations that it does not need to override.
1697693SAli.Saidi@ARM.com     * @param pkt packet containing the write the offset into config space
1707720Sgblack@eecs.umich.edu     */
1717720Sgblack@eecs.umich.edu    virtual Tick writeConfig(PacketPtr pkt);
1727720Sgblack@eecs.umich.edu
1737720Sgblack@eecs.umich.edu
1747720Sgblack@eecs.umich.edu    /**
1757720Sgblack@eecs.umich.edu     * Read from the PCI config space data that is stored locally. This may be
1767752SWilliam.Wang@arm.com     * overridden by the device but at some point it will eventually call this
1777752SWilliam.Wang@arm.com     * for normal operations that it does not need to override.
1787752SWilliam.Wang@arm.com     * @param pkt packet containing the write the offset into config space
1798300Schander.sudanthi@arm.com     */
1808300Schander.sudanthi@arm.com    virtual Tick readConfig(PacketPtr pkt);
1818300Schander.sudanthi@arm.com
1828300Schander.sudanthi@arm.com  protected:
1838300Schander.sudanthi@arm.com    PciHost::DeviceInterface hostInterface;
1848300Schander.sudanthi@arm.com
1856019Shines@cs.fsu.edu    Tick pioDelay;
1866019Shines@cs.fsu.edu    Tick configDelay;
1876019Shines@cs.fsu.edu
188  public:
189    Addr pciToDma(Addr pci_addr) const {
190        return hostInterface.dmaAddr(pci_addr);
191    }
192
193    void intrPost() { hostInterface.postInt(); }
194    void intrClear() { hostInterface.clearInt(); }
195
196    uint8_t interruptLine() const { return letoh(config.interruptLine); }
197
198    /**
199     * Determine the address ranges that this device responds to.
200     *
201     * @return a list of non-overlapping address ranges
202     */
203    AddrRangeList getAddrRanges() const override;
204
205    /**
206     * Constructor for PCI Dev. This function copies data from the
207     * config file object PCIConfigData and registers the device with
208     * a PciHost object.
209     */
210    PciDevice(const PciDeviceParams *params);
211
212    /**
213     * Serialize this object to the given output stream.
214     * @param os The stream to serialize to.
215     */
216    void serialize(CheckpointOut &cp) const override;
217
218    /**
219     * Reconstruct the state of this object from a checkpoint.
220     * @param cp The checkpoint use.
221     * @param section The section name of this object
222     */
223    void unserialize(CheckpointIn &cp) override;
224
225    const PciBusAddr &busAddr() const { return _busAddr; }
226};
227#endif // __DEV_PCI_DEVICE_HH__
228