pcie_host.c 5.5 KB
Newer Older
I
Isaku Yamahata 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * 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
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "hw.h"
#include "pci.h"
#include "pcie_host.h"

/*
 * 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 */
static inline PCIDevice *pcie_mmcfg_addr_to_dev(PCIBus *s, uint32_t mmcfg_addr)
{
    return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
                           PCI_SLOT(PCIE_MMCFG_DEVFN(mmcfg_addr)),
                           PCI_FUNC(PCIE_MMCFG_DEVFN(mmcfg_addr)));
}

static void pcie_mmcfg_data_write(PCIBus *s,
                                  uint32_t mmcfg_addr, uint32_t val, int len)
{
    PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, mmcfg_addr);

    if (!pci_dev)
        return;

    pci_dev->config_write(pci_dev,
                          PCIE_MMCFG_CONFOFFSET(mmcfg_addr), val, len);
}

68
static uint32_t pcie_mmcfg_data_read(PCIBus *s, uint32_t addr, int len)
I
Isaku Yamahata 已提交
69
{
70
    PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, addr);
I
Isaku Yamahata 已提交
71

72
    assert(len == 1 || len == 2 || len == 4);
I
Isaku Yamahata 已提交
73
    if (!pci_dev) {
74
        return ~0x0;
I
Isaku Yamahata 已提交
75
    }
76
    return pci_dev->config_read(pci_dev, PCIE_MMCFG_CONFOFFSET(addr), len);
I
Isaku Yamahata 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}

static void pcie_mmcfg_data_writeb(void *opaque,
                                   target_phys_addr_t addr, uint32_t value)
{
    PCIExpressHost *e = opaque;
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 1);
}

static void pcie_mmcfg_data_writew(void *opaque,
                                   target_phys_addr_t addr, uint32_t value)
{
    PCIExpressHost *e = opaque;
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 2);
}

static void pcie_mmcfg_data_writel(void *opaque,
                                   target_phys_addr_t addr, uint32_t value)
{
    PCIExpressHost *e = opaque;
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 4);
}

static uint32_t pcie_mmcfg_data_readb(void *opaque, target_phys_addr_t addr)
{
    PCIExpressHost *e = opaque;
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 1);
}

static uint32_t pcie_mmcfg_data_readw(void *opaque, target_phys_addr_t addr)
{
    PCIExpressHost *e = opaque;
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 2);
}

static uint32_t pcie_mmcfg_data_readl(void *opaque, target_phys_addr_t addr)
{
    PCIExpressHost *e = opaque;
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 4);
}


static CPUWriteMemoryFunc * const pcie_mmcfg_write[] =
{
    pcie_mmcfg_data_writeb,
    pcie_mmcfg_data_writew,
    pcie_mmcfg_data_writel,
};

static CPUReadMemoryFunc * const pcie_mmcfg_read[] =
{
    pcie_mmcfg_data_readb,
    pcie_mmcfg_data_readw,
    pcie_mmcfg_data_readl,
};

/* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
#define PCIE_BASE_ADDR_UNMAPPED  ((target_phys_addr_t)-1ULL)

int pcie_host_init(PCIExpressHost *e)
{
    e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
    e->mmio_index =
        cpu_register_io_memory(pcie_mmcfg_read, pcie_mmcfg_write, e);
    if (e->mmio_index < 0) {
        return -1;
    }

    return 0;
}

void pcie_host_mmcfg_unmap(PCIExpressHost *e)
{
    if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
        cpu_register_physical_memory(e->base_addr, e->size, IO_MEM_UNASSIGNED);
        e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
    }
}

void pcie_host_mmcfg_map(PCIExpressHost *e,
                         target_phys_addr_t addr, uint32_t size)
{
    assert(!(size & (size - 1)));       /* power of 2 */
    assert(size >= PCIE_MMCFG_SIZE_MIN);
    assert(size <= PCIE_MMCFG_SIZE_MAX);

    e->base_addr = addr;
    e->size = size;
    cpu_register_physical_memory(e->base_addr, e->size, e->mmio_index);
}

void pcie_host_mmcfg_update(PCIExpressHost *e,
                            int enable,
                            target_phys_addr_t addr, uint32_t size)
{
    pcie_host_mmcfg_unmap(e);
    if (enable) {
        pcie_host_mmcfg_map(e, addr, size);
    }
}