Statistics
| Branch: | Revision:

root / hw / pcie_host.c @ dd4239d6

History | View | Annotate | Download (5.8 kB)

1 a9f49946 Isaku Yamahata
/*
2 a9f49946 Isaku Yamahata
 * pcie_host.c
3 a9f49946 Isaku Yamahata
 * utility functions for pci express host bridge.
4 a9f49946 Isaku Yamahata
 *
5 a9f49946 Isaku Yamahata
 * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
6 a9f49946 Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
7 a9f49946 Isaku Yamahata
 *
8 a9f49946 Isaku Yamahata
 * This program is free software; you can redistribute it and/or modify
9 a9f49946 Isaku Yamahata
 * it under the terms of the GNU General Public License as published by
10 a9f49946 Isaku Yamahata
 * the Free Software Foundation; either version 2 of the License, or
11 a9f49946 Isaku Yamahata
 * (at your option) any later version.
12 a9f49946 Isaku Yamahata

13 a9f49946 Isaku Yamahata
 * This program is distributed in the hope that it will be useful,
14 a9f49946 Isaku Yamahata
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 a9f49946 Isaku Yamahata
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 a9f49946 Isaku Yamahata
 * GNU General Public License for more details.
17 a9f49946 Isaku Yamahata

18 a9f49946 Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
19 a9f49946 Isaku Yamahata
 * with this program; if not, write to the Free Software Foundation, Inc.,
20 a9f49946 Isaku Yamahata
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 a9f49946 Isaku Yamahata
 */
22 a9f49946 Isaku Yamahata
23 a9f49946 Isaku Yamahata
#include "hw.h"
24 a9f49946 Isaku Yamahata
#include "pci.h"
25 a9f49946 Isaku Yamahata
#include "pcie_host.h"
26 a9f49946 Isaku Yamahata
27 a9f49946 Isaku Yamahata
/*
28 a9f49946 Isaku Yamahata
 * PCI express mmcfig address
29 a9f49946 Isaku Yamahata
 * bit 20 - 28: bus number
30 a9f49946 Isaku Yamahata
 * bit 15 - 19: device number
31 a9f49946 Isaku Yamahata
 * bit 12 - 14: function number
32 a9f49946 Isaku Yamahata
 * bit  0 - 11: offset in configuration space of a given device
33 a9f49946 Isaku Yamahata
 */
34 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_SIZE_MAX             (1ULL << 28)
35 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_SIZE_MIN             (1ULL << 20)
36 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_BUS_BIT              20
37 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_BUS_MASK             0x1ff
38 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_DEVFN_BIT            12
39 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_DEVFN_MASK           0xff
40 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_CONFOFFSET_MASK      0xfff
41 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_BUS(addr)            (((addr) >> PCIE_MMCFG_BUS_BIT) & \
42 a9f49946 Isaku Yamahata
                                         PCIE_MMCFG_BUS_MASK)
43 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_DEVFN(addr)          (((addr) >> PCIE_MMCFG_DEVFN_BIT) & \
44 a9f49946 Isaku Yamahata
                                         PCIE_MMCFG_DEVFN_MASK)
45 a9f49946 Isaku Yamahata
#define PCIE_MMCFG_CONFOFFSET(addr)     ((addr) & PCIE_MMCFG_CONFOFFSET_MASK)
46 a9f49946 Isaku Yamahata
47 a9f49946 Isaku Yamahata
48 a9f49946 Isaku Yamahata
/* a helper function to get a PCIDevice for a given mmconfig address */
49 a9f49946 Isaku Yamahata
static inline PCIDevice *pcie_mmcfg_addr_to_dev(PCIBus *s, uint32_t mmcfg_addr)
50 a9f49946 Isaku Yamahata
{
51 a9f49946 Isaku Yamahata
    return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
52 a9f49946 Isaku Yamahata
                           PCI_SLOT(PCIE_MMCFG_DEVFN(mmcfg_addr)),
53 a9f49946 Isaku Yamahata
                           PCI_FUNC(PCIE_MMCFG_DEVFN(mmcfg_addr)));
54 a9f49946 Isaku Yamahata
}
55 a9f49946 Isaku Yamahata
56 a9f49946 Isaku Yamahata
static void pcie_mmcfg_data_write(PCIBus *s,
57 a9f49946 Isaku Yamahata
                                  uint32_t mmcfg_addr, uint32_t val, int len)
58 a9f49946 Isaku Yamahata
{
59 a9f49946 Isaku Yamahata
    PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, mmcfg_addr);
60 a9f49946 Isaku Yamahata
61 a9f49946 Isaku Yamahata
    if (!pci_dev)
62 a9f49946 Isaku Yamahata
        return;
63 a9f49946 Isaku Yamahata
64 a9f49946 Isaku Yamahata
    pci_dev->config_write(pci_dev,
65 a9f49946 Isaku Yamahata
                          PCIE_MMCFG_CONFOFFSET(mmcfg_addr), val, len);
66 a9f49946 Isaku Yamahata
}
67 a9f49946 Isaku Yamahata
68 a9f49946 Isaku Yamahata
static uint32_t pcie_mmcfg_data_read(PCIBus *s,
69 a9f49946 Isaku Yamahata
                                     uint32_t mmcfg_addr, int len)
70 a9f49946 Isaku Yamahata
{
71 a9f49946 Isaku Yamahata
    PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, mmcfg_addr);
72 a9f49946 Isaku Yamahata
    uint32_t val;
73 a9f49946 Isaku Yamahata
74 a9f49946 Isaku Yamahata
    if (!pci_dev) {
75 a9f49946 Isaku Yamahata
        switch(len) {
76 a9f49946 Isaku Yamahata
        case 1:
77 a9f49946 Isaku Yamahata
            val = 0xff;
78 a9f49946 Isaku Yamahata
            break;
79 a9f49946 Isaku Yamahata
        case 2:
80 a9f49946 Isaku Yamahata
            val = 0xffff;
81 a9f49946 Isaku Yamahata
            break;
82 a9f49946 Isaku Yamahata
        default:
83 a9f49946 Isaku Yamahata
        case 4:
84 a9f49946 Isaku Yamahata
            val = 0xffffffff;
85 a9f49946 Isaku Yamahata
            break;
86 a9f49946 Isaku Yamahata
        }
87 a9f49946 Isaku Yamahata
    } else {
88 a9f49946 Isaku Yamahata
        val = pci_dev->config_read(pci_dev,
89 a9f49946 Isaku Yamahata
                                   PCIE_MMCFG_CONFOFFSET(mmcfg_addr), len);
90 a9f49946 Isaku Yamahata
    }
91 a9f49946 Isaku Yamahata
92 a9f49946 Isaku Yamahata
    return val;
93 a9f49946 Isaku Yamahata
}
94 a9f49946 Isaku Yamahata
95 a9f49946 Isaku Yamahata
static void pcie_mmcfg_data_writeb(void *opaque,
96 a9f49946 Isaku Yamahata
                                   target_phys_addr_t addr, uint32_t value)
97 a9f49946 Isaku Yamahata
{
98 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
99 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 1);
100 a9f49946 Isaku Yamahata
}
101 a9f49946 Isaku Yamahata
102 a9f49946 Isaku Yamahata
static void pcie_mmcfg_data_writew(void *opaque,
103 a9f49946 Isaku Yamahata
                                   target_phys_addr_t addr, uint32_t value)
104 a9f49946 Isaku Yamahata
{
105 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
106 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 2);
107 a9f49946 Isaku Yamahata
}
108 a9f49946 Isaku Yamahata
109 a9f49946 Isaku Yamahata
static void pcie_mmcfg_data_writel(void *opaque,
110 a9f49946 Isaku Yamahata
                                   target_phys_addr_t addr, uint32_t value)
111 a9f49946 Isaku Yamahata
{
112 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
113 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 4);
114 a9f49946 Isaku Yamahata
}
115 a9f49946 Isaku Yamahata
116 a9f49946 Isaku Yamahata
static uint32_t pcie_mmcfg_data_readb(void *opaque, target_phys_addr_t addr)
117 a9f49946 Isaku Yamahata
{
118 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
119 a9f49946 Isaku Yamahata
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 1);
120 a9f49946 Isaku Yamahata
}
121 a9f49946 Isaku Yamahata
122 a9f49946 Isaku Yamahata
static uint32_t pcie_mmcfg_data_readw(void *opaque, target_phys_addr_t addr)
123 a9f49946 Isaku Yamahata
{
124 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
125 a9f49946 Isaku Yamahata
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 2);
126 a9f49946 Isaku Yamahata
}
127 a9f49946 Isaku Yamahata
128 a9f49946 Isaku Yamahata
static uint32_t pcie_mmcfg_data_readl(void *opaque, target_phys_addr_t addr)
129 a9f49946 Isaku Yamahata
{
130 a9f49946 Isaku Yamahata
    PCIExpressHost *e = opaque;
131 a9f49946 Isaku Yamahata
    return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 4);
132 a9f49946 Isaku Yamahata
}
133 a9f49946 Isaku Yamahata
134 a9f49946 Isaku Yamahata
135 a9f49946 Isaku Yamahata
static CPUWriteMemoryFunc * const pcie_mmcfg_write[] =
136 a9f49946 Isaku Yamahata
{
137 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_writeb,
138 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_writew,
139 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_writel,
140 a9f49946 Isaku Yamahata
};
141 a9f49946 Isaku Yamahata
142 a9f49946 Isaku Yamahata
static CPUReadMemoryFunc * const pcie_mmcfg_read[] =
143 a9f49946 Isaku Yamahata
{
144 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_readb,
145 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_readw,
146 a9f49946 Isaku Yamahata
    pcie_mmcfg_data_readl,
147 a9f49946 Isaku Yamahata
};
148 a9f49946 Isaku Yamahata
149 a9f49946 Isaku Yamahata
/* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
150 a9f49946 Isaku Yamahata
#define PCIE_BASE_ADDR_UNMAPPED  ((target_phys_addr_t)-1ULL)
151 a9f49946 Isaku Yamahata
152 a9f49946 Isaku Yamahata
int pcie_host_init(PCIExpressHost *e)
153 a9f49946 Isaku Yamahata
{
154 a9f49946 Isaku Yamahata
    e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
155 a9f49946 Isaku Yamahata
    e->mmio_index =
156 a9f49946 Isaku Yamahata
        cpu_register_io_memory(pcie_mmcfg_read, pcie_mmcfg_write, e);
157 a9f49946 Isaku Yamahata
    if (e->mmio_index < 0) {
158 a9f49946 Isaku Yamahata
        return -1;
159 a9f49946 Isaku Yamahata
    }
160 a9f49946 Isaku Yamahata
161 a9f49946 Isaku Yamahata
    return 0;
162 a9f49946 Isaku Yamahata
}
163 a9f49946 Isaku Yamahata
164 a9f49946 Isaku Yamahata
void pcie_host_mmcfg_unmap(PCIExpressHost *e)
165 a9f49946 Isaku Yamahata
{
166 a9f49946 Isaku Yamahata
    if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
167 a9f49946 Isaku Yamahata
        cpu_register_physical_memory(e->base_addr, e->size, IO_MEM_UNASSIGNED);
168 a9f49946 Isaku Yamahata
        e->base_addr = PCIE_BASE_ADDR_UNMAPPED;
169 a9f49946 Isaku Yamahata
    }
170 a9f49946 Isaku Yamahata
}
171 a9f49946 Isaku Yamahata
172 a9f49946 Isaku Yamahata
void pcie_host_mmcfg_map(PCIExpressHost *e,
173 a9f49946 Isaku Yamahata
                         target_phys_addr_t addr, uint32_t size)
174 a9f49946 Isaku Yamahata
{
175 a9f49946 Isaku Yamahata
    assert(!(size & (size - 1)));       /* power of 2 */
176 a9f49946 Isaku Yamahata
    assert(size >= PCIE_MMCFG_SIZE_MIN);
177 a9f49946 Isaku Yamahata
    assert(size <= PCIE_MMCFG_SIZE_MAX);
178 a9f49946 Isaku Yamahata
179 a9f49946 Isaku Yamahata
    e->base_addr = addr;
180 a9f49946 Isaku Yamahata
    e->size = size;
181 a9f49946 Isaku Yamahata
    cpu_register_physical_memory(e->base_addr, e->size, e->mmio_index);
182 a9f49946 Isaku Yamahata
}
183 a9f49946 Isaku Yamahata
184 a9f49946 Isaku Yamahata
void pcie_host_mmcfg_update(PCIExpressHost *e,
185 a9f49946 Isaku Yamahata
                            int enable,
186 a9f49946 Isaku Yamahata
                            target_phys_addr_t addr, uint32_t size)
187 a9f49946 Isaku Yamahata
{
188 a9f49946 Isaku Yamahata
    pcie_host_mmcfg_unmap(e);
189 a9f49946 Isaku Yamahata
    if (enable) {
190 a9f49946 Isaku Yamahata
        pcie_host_mmcfg_map(e, addr, size);
191 a9f49946 Isaku Yamahata
    }
192 a9f49946 Isaku Yamahata
}