2 * Copyright (c) 2015 ARM Limited
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.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Sandberg
40 #include "dev/pci/host.hh"
44 #include "debug/PciHost.hh"
45 #include "dev/pci/device.hh"
46 #include "dev/platform.hh"
47 #include "params/GenericPciHost.hh"
48 #include "params/PciHost.hh"
50 PciHost::PciHost(const PciHostParams
*p
)
59 PciHost::DeviceInterface
60 PciHost::registerDevice(PciDevice
*device
, PciBusAddr bus_addr
, PciIntPin pin
)
62 auto map_entry
= devices
.emplace(bus_addr
, device
);
64 DPRINTF(PciHost
, "%02x:%02x.%i: Registering device\n",
65 bus_addr
.bus
, bus_addr
.dev
, bus_addr
.func
);
67 fatal_if(!map_entry
.second
,
68 "%02x:%02x.%i: PCI bus ID collision\n",
69 bus_addr
.bus
, bus_addr
.dev
, bus_addr
.func
);
71 return DeviceInterface(*this, bus_addr
, pin
);
75 PciHost::getDevice(const PciBusAddr
&addr
)
77 auto device
= devices
.find(addr
);
78 return device
!= devices
.end() ? device
->second
: nullptr;
82 PciHost::getDevice(const PciBusAddr
&addr
) const
84 auto device
= devices
.find(addr
);
85 return device
!= devices
.end() ? device
->second
: nullptr;
88 PciHost::DeviceInterface::DeviceInterface(
90 PciBusAddr
&bus_addr
, PciIntPin interrupt_pin
)
92 busAddr(bus_addr
), interruptPin(interrupt_pin
)
97 PciHost::DeviceInterface::name() const
99 return csprintf("%s.interface[%02x:%02x.%i]",
100 host
.name(), busAddr
.bus
, busAddr
.dev
, busAddr
.func
);
104 PciHost::DeviceInterface::postInt()
106 DPRINTF(PciHost
, "postInt\n");
108 host
.postInt(busAddr
, interruptPin
);
112 PciHost::DeviceInterface::clearInt()
114 DPRINTF(PciHost
, "clearInt\n");
116 host
.clearInt(busAddr
, interruptPin
);
120 GenericPciHost::GenericPciHost(const GenericPciHostParams
*p
)
122 platform(*p
->platform
),
123 confBase(p
->conf_base
), confSize(p
->conf_size
),
124 confDeviceBits(p
->conf_device_bits
),
125 pciPioBase(p
->pci_pio_base
), pciMemBase(p
->pci_mem_base
),
126 pciDmaBase(p
->pci_dma_base
)
130 GenericPciHost::~GenericPciHost()
136 GenericPciHost::read(PacketPtr pkt
)
138 const auto dev_addr(decodeAddress(pkt
->getAddr() - confBase
));
139 const Addr
size(pkt
->getSize());
141 DPRINTF(PciHost
, "%02x:%02x.%i: read: offset=0x%x, size=0x%x\n",
142 dev_addr
.first
.bus
, dev_addr
.first
.dev
, dev_addr
.first
.func
,
146 PciDevice
*const pci_dev(getDevice(dev_addr
.first
));
148 // @todo Remove this after testing
149 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
150 return pci_dev
->readConfig(pkt
);
152 uint8_t *pkt_data(pkt
->getPtr
<uint8_t>());
153 std::fill(pkt_data
, pkt_data
+ size
, 0xFF);
154 pkt
->makeAtomicResponse();
160 GenericPciHost::write(PacketPtr pkt
)
162 const auto dev_addr(decodeAddress(pkt
->getAddr() - confBase
));
164 DPRINTF(PciHost
, "%02x:%02x.%i: write: offset=0x%x, size=0x%x\n",
165 dev_addr
.first
.bus
, dev_addr
.first
.dev
, dev_addr
.first
.func
,
169 PciDevice
*const pci_dev(getDevice(dev_addr
.first
));
171 "%02x:%02x.%i: Write to config space on non-existent PCI device\n",
172 dev_addr
.first
.bus
, dev_addr
.first
.dev
, dev_addr
.first
.func
);
174 // @todo Remove this after testing
175 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
177 return pci_dev
->writeConfig(pkt
);
181 GenericPciHost::getAddrRanges() const
183 return AddrRangeList({ RangeSize(confBase
, confSize
) });
186 std::pair
<PciBusAddr
, Addr
>
187 GenericPciHost::decodeAddress(Addr addr
)
189 const Addr
offset(addr
& mask(confDeviceBits
));
190 const Addr
bus_addr(addr
>> confDeviceBits
);
192 return std::make_pair(
193 PciBusAddr(bits(bus_addr
, 15, 8),
194 bits(bus_addr
, 7, 3),
195 bits(bus_addr
, 2, 0)),
201 GenericPciHost::postInt(const PciBusAddr
&addr
, PciIntPin pin
)
203 platform
.postPciInt(mapPciInterrupt(addr
, pin
));
207 GenericPciHost::clearInt(const PciBusAddr
&addr
, PciIntPin pin
)
209 platform
.clearPciInt(mapPciInterrupt(addr
, pin
));
214 GenericPciHost::mapPciInterrupt(const PciBusAddr
&addr
, PciIntPin pin
) const
216 const PciDevice
*dev(getDevice(addr
));
219 return dev
->interruptLine();
224 GenericPciHostParams::create()
226 return new GenericPciHost(this);