2009-10-30 13:21:18 +01:00
|
|
|
/*
|
|
|
|
* pcie_host.c
|
|
|
|
* utility functions for pci express host bridge.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
|
|
|
|
* VA Linux Systems Japan K.K.
|
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
|
|
* This program 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.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-03-07 16:48:43 +01:00
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
2009-10-30 13:21:18 +01:00
|
|
|
*/
|
|
|
|
|
2012-12-12 22:05:42 +01:00
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "hw/pci/pci.h"
|
|
|
|
#include "hw/pci/pcie_host.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2009-10-30 13:21:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI express mmcfig address
|
|
|
|
* bit 20 - 28: bus number
|
|
|
|
* bit 15 - 19: device number
|
|
|
|
* bit 12 - 14: function number
|
|
|
|
* bit 0 - 11: offset in configuration space of a given device
|
|
|
|
*/
|
|
|
|
#define PCIE_MMCFG_SIZE_MAX (1ULL << 28)
|
|
|
|
#define PCIE_MMCFG_SIZE_MIN (1ULL << 20)
|
|
|
|
#define PCIE_MMCFG_BUS_BIT 20
|
|
|
|
#define PCIE_MMCFG_BUS_MASK 0x1ff
|
|
|
|
#define PCIE_MMCFG_DEVFN_BIT 12
|
|
|
|
#define PCIE_MMCFG_DEVFN_MASK 0xff
|
|
|
|
#define PCIE_MMCFG_CONFOFFSET_MASK 0xfff
|
|
|
|
#define PCIE_MMCFG_BUS(addr) (((addr) >> PCIE_MMCFG_BUS_BIT) & \
|
|
|
|
PCIE_MMCFG_BUS_MASK)
|
|
|
|
#define PCIE_MMCFG_DEVFN(addr) (((addr) >> PCIE_MMCFG_DEVFN_BIT) & \
|
|
|
|
PCIE_MMCFG_DEVFN_MASK)
|
|
|
|
#define PCIE_MMCFG_CONFOFFSET(addr) ((addr) & PCIE_MMCFG_CONFOFFSET_MASK)
|
|
|
|
|
|
|
|
|
|
|
|
/* a helper function to get a PCIDevice for a given mmconfig address */
|
2009-11-12 12:17:23 +01:00
|
|
|
static inline PCIDevice *pcie_dev_find_by_mmcfg_addr(PCIBus *s,
|
|
|
|
uint32_t mmcfg_addr)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
|
|
|
return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
|
2011-01-27 07:56:36 +01:00
|
|
|
PCIE_MMCFG_DEVFN(mmcfg_addr));
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void pcie_mmcfg_data_write(void *opaque, hwaddr mmcfg_addr,
|
2011-08-15 16:17:25 +02:00
|
|
|
uint64_t val, unsigned len)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
2011-08-15 16:17:25 +02:00
|
|
|
PCIExpressHost *e = opaque;
|
|
|
|
PCIBus *s = e->pci.bus;
|
2009-11-12 12:17:23 +01:00
|
|
|
PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr);
|
2011-07-29 03:01:43 +02:00
|
|
|
uint32_t addr;
|
|
|
|
uint32_t limit;
|
2009-10-30 13:21:18 +01:00
|
|
|
|
2011-07-22 11:05:01 +02:00
|
|
|
if (!pci_dev) {
|
2009-10-30 13:21:18 +01:00
|
|
|
return;
|
2011-07-22 11:05:01 +02:00
|
|
|
}
|
2011-07-29 03:01:43 +02:00
|
|
|
addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr);
|
|
|
|
limit = pci_config_size(pci_dev);
|
|
|
|
if (limit <= addr) {
|
|
|
|
/* conventional pci device can be behind pcie-to-pci bridge.
|
|
|
|
256 <= addr < 4K has no effects. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pci_host_config_write_common(pci_dev, addr, limit, val, len);
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
|
|
|
|
2011-08-15 16:17:25 +02:00
|
|
|
static uint64_t pcie_mmcfg_data_read(void *opaque,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr mmcfg_addr,
|
2011-08-15 16:17:25 +02:00
|
|
|
unsigned len)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
2011-08-15 16:17:25 +02:00
|
|
|
PCIExpressHost *e = opaque;
|
|
|
|
PCIBus *s = e->pci.bus;
|
2011-07-29 03:01:43 +02:00
|
|
|
PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr);
|
|
|
|
uint32_t addr;
|
|
|
|
uint32_t limit;
|
2009-10-30 13:21:18 +01:00
|
|
|
|
|
|
|
if (!pci_dev) {
|
2009-11-12 06:58:31 +01:00
|
|
|
return ~0x0;
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
2011-07-29 03:01:43 +02:00
|
|
|
addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr);
|
|
|
|
limit = pci_config_size(pci_dev);
|
|
|
|
if (limit <= addr) {
|
|
|
|
/* conventional pci device can be behind pcie-to-pci bridge.
|
|
|
|
256 <= addr < 4K has no effects. */
|
|
|
|
return ~0x0;
|
|
|
|
}
|
|
|
|
return pci_host_config_read_common(pci_dev, addr, limit, len);
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
|
|
|
|
2011-08-15 16:17:25 +02:00
|
|
|
static const MemoryRegionOps pcie_mmcfg_ops = {
|
|
|
|
.read = pcie_mmcfg_data_read,
|
|
|
|
.write = pcie_mmcfg_data_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2009-10-30 13:21:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
|
2012-10-23 12:30:10 +02:00
|
|
|
#define PCIE_BASE_ADDR_UNMAPPED ((hwaddr)-1ULL)
|
2009-10-30 13:21:18 +01:00
|
|
|
|
2012-10-19 22:43:32 +02:00
|
|
|
int pcie_host_init(PCIExpressHost *e)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
|
|
|
e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcie_host_mmcfg_unmap(PCIExpressHost *e)
|
|
|
|
{
|
|
|
|
if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
|
2011-08-15 16:17:25 +02:00
|
|
|
memory_region_del_subregion(get_system_memory(), &e->mmio);
|
2012-10-19 22:43:32 +02:00
|
|
|
memory_region_destroy(&e->mmio);
|
2009-10-30 13:21:18 +01:00
|
|
|
e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:43:32 +02:00
|
|
|
void pcie_host_mmcfg_map(PCIExpressHost *e, hwaddr addr,
|
|
|
|
uint32_t size)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
2012-10-19 22:43:32 +02:00
|
|
|
assert(!(size & (size - 1))); /* power of 2 */
|
|
|
|
assert(size >= PCIE_MMCFG_SIZE_MIN);
|
|
|
|
assert(size <= PCIE_MMCFG_SIZE_MAX);
|
|
|
|
e->size = size;
|
2013-06-06 11:41:28 +02:00
|
|
|
memory_region_init_io(&e->mmio, NULL, &pcie_mmcfg_ops, e, "pcie-mmcfg", e->size);
|
2009-10-30 13:21:18 +01:00
|
|
|
e->base_addr = addr;
|
2011-08-15 16:17:25 +02:00
|
|
|
memory_region_add_subregion(get_system_memory(), e->base_addr, &e->mmio);
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcie_host_mmcfg_update(PCIExpressHost *e,
|
|
|
|
int enable,
|
2012-10-19 22:43:32 +02:00
|
|
|
hwaddr addr,
|
|
|
|
uint32_t size)
|
2009-10-30 13:21:18 +01:00
|
|
|
{
|
|
|
|
pcie_host_mmcfg_unmap(e);
|
|
|
|
if (enable) {
|
2012-10-19 22:43:32 +02:00
|
|
|
pcie_host_mmcfg_map(e, addr, size);
|
2009-10-30 13:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 22:43:33 +02:00
|
|
|
|
|
|
|
static const TypeInfo pcie_host_type_info = {
|
|
|
|
.name = TYPE_PCIE_HOST_BRIDGE,
|
|
|
|
.parent = TYPE_PCI_HOST_BRIDGE,
|
|
|
|
.abstract = true,
|
|
|
|
.instance_size = sizeof(PCIExpressHost),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pcie_host_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pcie_host_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(pcie_host_register_types)
|