2012-07-27 19:40:25 -04:00
|
|
|
/*******************************************************************************
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
pci.cpp
|
|
|
|
Functions for handling PCI devices.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
*******************************************************************************/
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2013-10-19 17:50:50 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <endian.h>
|
|
|
|
|
2013-05-12 18:23:24 -04:00
|
|
|
#include <sortix/kernel/cpu.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/kernel.h>
|
2014-02-08 08:47:10 -05:00
|
|
|
#include <sortix/kernel/kthread.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
#include <sortix/kernel/pci.h>
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
namespace Sortix {
|
|
|
|
namespace PCI {
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
static kthread_mutex_t pci_lock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
const uint16_t CONFIG_ADDRESS = 0xCF8;
|
|
|
|
const uint16_t CONFIG_DATA = 0xCFC;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint32_t MakeDevAddr(uint8_t bus, uint8_t slot, uint8_t func)
|
|
|
|
{
|
2012-09-22 08:57:20 -04:00
|
|
|
//assert(bus < 1UL<<8UL); // bus is 8 bit anyways.
|
|
|
|
assert(slot < 1UL<<5UL);
|
|
|
|
assert(func < 1UL<<3UL);
|
2012-07-27 19:40:25 -04:00
|
|
|
return func << 8U | slot << 11U | bus << 16U | 1 << 31U;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
void SplitDevAddr(uint32_t devaddr, uint8_t* vals /* bus, slot, func */)
|
|
|
|
{
|
|
|
|
vals[0] = devaddr >> 16U & ((1UL<<8UL)-1);
|
|
|
|
vals[1] = devaddr >> 11U & ((1UL<<3UL)-1);
|
|
|
|
vals[2] = devaddr >> 8U & ((1UL<<5UL)-1);
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint32_t ReadRaw32(uint32_t devaddr, uint8_t off)
|
|
|
|
{
|
|
|
|
CPU::OutPortL(CONFIG_ADDRESS, devaddr + off);
|
|
|
|
return CPU::InPortL(CONFIG_DATA);
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
void WriteRaw32(uint32_t devaddr, uint8_t off, uint32_t val)
|
|
|
|
{
|
|
|
|
CPU::OutPortL(CONFIG_ADDRESS, devaddr + off);
|
|
|
|
CPU::OutPortL(CONFIG_DATA, val);
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint32_t Read32(uint32_t devaddr, uint8_t off)
|
|
|
|
{
|
2013-10-19 17:50:50 -04:00
|
|
|
return le32toh(ReadRaw32(devaddr, off));
|
2012-07-27 19:40:25 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
void Write32(uint32_t devaddr, uint8_t off, uint32_t val)
|
|
|
|
{
|
2013-10-19 17:50:50 -04:00
|
|
|
WriteRaw32(devaddr, off, htole32(val));
|
2012-07-27 19:40:25 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint16_t Read16(uint32_t devaddr, uint8_t off)
|
|
|
|
{
|
2012-09-22 08:57:20 -04:00
|
|
|
assert((off & 0x1) == 0);
|
2012-07-27 19:40:25 -04:00
|
|
|
uint8_t alignedoff = off & ~0x3;
|
|
|
|
union { uint16_t val16[2]; uint32_t val32; };
|
|
|
|
val32 = ReadRaw32(devaddr, alignedoff);
|
|
|
|
uint16_t ret = off & 0x2 ? val16[0] : val16[1];
|
2013-10-19 17:50:50 -04:00
|
|
|
return le16toh(ret);
|
2012-07-27 19:40:25 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint8_t Read8(uint32_t devaddr, uint8_t off)
|
|
|
|
{
|
|
|
|
uint8_t alignedoff = off & ~0x1;
|
|
|
|
union { uint8_t val8[2]; uint32_t val16; };
|
2013-10-19 17:50:50 -04:00
|
|
|
val16 = htole16(Read16(devaddr, alignedoff));
|
2012-07-27 19:40:25 -04:00
|
|
|
uint8_t ret = off & 0x1 ? val8[0] : val8[1];
|
|
|
|
return ret;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
uint32_t CheckDevice(uint8_t bus, uint8_t slot, uint8_t func)
|
|
|
|
{
|
|
|
|
return Read32(MakeDevAddr(bus, slot, func), 0x0);
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
pciid_t GetDeviceId(uint32_t devaddr)
|
|
|
|
{
|
|
|
|
pciid_t ret;
|
|
|
|
ret.deviceid = Read16(devaddr, 0x00);
|
|
|
|
ret.vendorid = Read16(devaddr, 0x02);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
pcitype_t GetDeviceType(uint32_t devaddr)
|
|
|
|
{
|
|
|
|
pcitype_t ret;
|
|
|
|
ret.classid = Read8(devaddr, 0x08);
|
|
|
|
ret.subclassid = Read8(devaddr, 0x09);
|
|
|
|
ret.progif = Read8(devaddr, 0x0A);
|
|
|
|
ret.revid = Read8(devaddr, 0x0B);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
static bool MatchesSearchCriteria(uint32_t devaddr, pcifind_t pcifind)
|
|
|
|
{
|
|
|
|
pciid_t id = GetDeviceId(devaddr);
|
|
|
|
if ( id.vendorid == 0xFFFF && id.deviceid == 0xFFFF )
|
|
|
|
return false;
|
|
|
|
pcitype_t type = GetDeviceType(devaddr);
|
|
|
|
if ( pcifind.vendorid != 0xFFFF && id.vendorid != pcifind.vendorid )
|
|
|
|
return false;
|
|
|
|
if ( pcifind.deviceid != 0xFFFF && id.deviceid != pcifind.deviceid )
|
|
|
|
return false;
|
|
|
|
if ( pcifind.classid != 0xFF && type.classid != pcifind.classid )
|
|
|
|
return false;
|
|
|
|
if ( pcifind.subclassid != 0xFF && type.subclassid != pcifind.subclassid )
|
|
|
|
return false;
|
|
|
|
if ( pcifind.progif != 0xFF && type.progif != pcifind.progif )
|
|
|
|
return false;
|
|
|
|
if ( pcifind.revid != 0xFF && type.revid != pcifind.revid )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
static uint32_t SearchForDeviceOnBus(uint8_t bus, pcifind_t pcifind)
|
|
|
|
{
|
|
|
|
for ( unsigned slot = 0; slot < 32; slot++ )
|
|
|
|
{
|
|
|
|
unsigned numfuncs = 1;
|
|
|
|
for ( unsigned func = 0; func < numfuncs; func++ )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2012-07-27 19:40:25 -04:00
|
|
|
uint32_t devaddr = MakeDevAddr(bus, slot, func);
|
|
|
|
if ( MatchesSearchCriteria(devaddr, pcifind) )
|
|
|
|
return devaddr;
|
|
|
|
uint8_t header = Read8(devaddr, 0x0D); // Secondary Bus Number.
|
|
|
|
if ( header & 0x80 ) // Multi function device.
|
|
|
|
numfuncs = 8;
|
|
|
|
if ( (header & 0x7F) == 0x01 ) // PCI to PCI bus.
|
|
|
|
{
|
|
|
|
uint8_t subbusid = Read8(devaddr, 0x1A);
|
|
|
|
uint32_t recret = SearchForDeviceOnBus(subbusid, pcifind);
|
|
|
|
if ( recret )
|
|
|
|
return recret;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-27 19:40:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SearchForDevice(pcifind_t pcifind)
|
|
|
|
{
|
|
|
|
// Search on bus 0 and recurse on other detected busses.
|
|
|
|
return SearchForDeviceOnBus(0, pcifind);
|
|
|
|
}
|
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
pcibar_t GetBAR(uint32_t devaddr, uint8_t bar)
|
2012-07-27 19:40:25 -04:00
|
|
|
{
|
2014-02-08 08:47:10 -05:00
|
|
|
ScopedLock lock(&pci_lock);
|
|
|
|
|
|
|
|
uint32_t low = PCI::Read32(devaddr, 0x10 + 4 * (bar+0));
|
|
|
|
|
|
|
|
pcibar_t result;
|
|
|
|
result.addr_raw = low;
|
|
|
|
result.size_raw = 0;
|
|
|
|
if ( result.is_64bit() )
|
|
|
|
{
|
|
|
|
uint32_t high = PCI::Read32(devaddr, 0x10 + 4 * (bar+1));
|
|
|
|
result.addr_raw |= (uint64_t) high << 32;
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), 0xFFFFFFFF);
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+1), 0xFFFFFFFF);
|
|
|
|
uint32_t size_low = PCI::Read32(devaddr, 0x10 + 4 * (bar+0));
|
|
|
|
uint32_t size_high = PCI::Read32(devaddr, 0x10 + 4 * (bar+1));
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), low);
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+1), high);
|
|
|
|
result.size_raw = (uint64_t) size_high << 32 | (uint64_t) size_low << 0;
|
|
|
|
result.size_raw = ~(result.size_raw & 0xFFFFFFFFFFFFFFF0) + 1;
|
|
|
|
}
|
|
|
|
else if ( result.is_32bit() )
|
|
|
|
{
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), 0xFFFFFFFF);
|
|
|
|
uint32_t size_low = PCI::Read32(devaddr, 0x10 + 4 * (bar+0));
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), low);
|
|
|
|
result.size_raw = (uint64_t) size_low << 0;
|
|
|
|
result.size_raw = ~(result.size_raw & 0xFFFFFFF0) + 1;
|
2014-03-25 13:50:41 -04:00
|
|
|
result.size_raw &= 0xFFFFFFFF;
|
2014-02-08 08:47:10 -05:00
|
|
|
}
|
|
|
|
else if ( result.is_iospace() )
|
2012-07-27 19:40:25 -04:00
|
|
|
{
|
2014-02-08 08:47:10 -05:00
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), 0xFFFFFFFF);
|
|
|
|
uint32_t size_low = PCI::Read32(devaddr, 0x10 + 4 * (bar+0));
|
|
|
|
PCI::Write32(devaddr, 0x10 + 4 * (bar+0), low);
|
|
|
|
result.size_raw = (uint64_t) size_low << 0;
|
|
|
|
result.size_raw = ~(result.size_raw & 0xFFFFFFFC) + 1;
|
2014-03-25 13:50:41 -04:00
|
|
|
result.size_raw &= 0xFFFFFFFF;
|
2012-07-27 19:40:25 -04:00
|
|
|
}
|
2014-02-08 08:47:10 -05:00
|
|
|
|
|
|
|
return result;
|
2012-07-27 19:40:25 -04:00
|
|
|
}
|
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
pcibar_t GetExpansionROM(uint32_t devaddr)
|
2013-05-27 14:25:36 -04:00
|
|
|
{
|
2014-02-08 08:47:10 -05:00
|
|
|
const uint32_t ROM_ADDRESS_MASK = ~UINT32_C(0x7FF);
|
|
|
|
|
|
|
|
ScopedLock lock(&pci_lock);
|
|
|
|
|
|
|
|
uint32_t low = PCI::Read32(devaddr, 0x30);
|
|
|
|
PCI::Write32(devaddr, 0x30, ROM_ADDRESS_MASK | low);
|
|
|
|
uint32_t size_low = PCI::Read32(devaddr, 0x30);
|
|
|
|
PCI::Write32(devaddr, 0x30, low);
|
|
|
|
|
|
|
|
pcibar_t result;
|
|
|
|
result.addr_raw = (low & ROM_ADDRESS_MASK) | PCIBAR_TYPE_32BIT;
|
|
|
|
result.size_raw = ~(size_low & ROM_ADDRESS_MASK) + 1;
|
|
|
|
return result;
|
2013-05-27 14:25:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
void EnableExpansionROM(uint32_t devaddr)
|
2013-05-27 14:25:36 -04:00
|
|
|
{
|
2014-02-08 08:47:10 -05:00
|
|
|
ScopedLock lock(&pci_lock);
|
|
|
|
|
|
|
|
PCI::Write32(devaddr, 0x30, PCI::Read32(devaddr, 0x30) | 0x1);
|
2013-05-27 14:25:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
void DisableExpansionROM(uint32_t devaddr)
|
2013-05-27 14:25:36 -04:00
|
|
|
{
|
2014-02-08 08:47:10 -05:00
|
|
|
ScopedLock lock(&pci_lock);
|
|
|
|
|
|
|
|
PCI::Write32(devaddr, 0x30, PCI::Read32(devaddr, 0x30) & ~UINT32_C(0x1));
|
2013-05-27 14:25:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-08 08:47:10 -05:00
|
|
|
bool IsExpansionROMEnabled(uint32_t devaddr)
|
|
|
|
{
|
|
|
|
ScopedLock lock(&pci_lock);
|
|
|
|
|
|
|
|
return PCI::Read32(devaddr, 0x30) & 0x1;
|
|
|
|
}
|
2013-05-27 14:25:36 -04:00
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
void Init()
|
|
|
|
{
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2012-07-27 19:40:25 -04:00
|
|
|
} // namespace PCI
|
|
|
|
} // namespace Sortix
|