Statistics
| Branch: | Revision:

root / hw / pci_host.c @ 42a4260f

History | View | Annotate | Download (6.6 kB)

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

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

17 4f5e19e6 Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
18 4f5e19e6 Isaku Yamahata
 * with this program; if not, write to the Free Software Foundation, Inc.,
19 4f5e19e6 Isaku Yamahata
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 4f5e19e6 Isaku Yamahata
 */
21 4f5e19e6 Isaku Yamahata
22 4f5e19e6 Isaku Yamahata
#include "pci.h"
23 4f5e19e6 Isaku Yamahata
#include "pci_host.h"
24 4f5e19e6 Isaku Yamahata
25 4f5e19e6 Isaku Yamahata
/* debug PCI */
26 4f5e19e6 Isaku Yamahata
//#define DEBUG_PCI
27 4f5e19e6 Isaku Yamahata
28 4f5e19e6 Isaku Yamahata
#ifdef DEBUG_PCI
29 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...) \
30 4f5e19e6 Isaku Yamahata
do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
31 4f5e19e6 Isaku Yamahata
#else
32 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...)
33 4f5e19e6 Isaku Yamahata
#endif
34 4f5e19e6 Isaku Yamahata
35 766347cc Isaku Yamahata
/*
36 766347cc Isaku Yamahata
 * PCI address
37 766347cc Isaku Yamahata
 * bit 16 - 24: bus number
38 766347cc Isaku Yamahata
 * bit  8 - 15: devfun number
39 766347cc Isaku Yamahata
 * bit  0 -  7: offset in configuration space of a given pci device
40 766347cc Isaku Yamahata
 */
41 766347cc Isaku Yamahata
42 766347cc Isaku Yamahata
/* the helper functio to get a PCIDeice* for a given pci address */
43 8d6514f8 Isaku Yamahata
static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
44 766347cc Isaku Yamahata
{
45 42331e9f Isaku Yamahata
    uint8_t bus_num = addr >> 16;
46 42331e9f Isaku Yamahata
    uint8_t devfn = addr >> 8;
47 42331e9f Isaku Yamahata
48 766347cc Isaku Yamahata
    return pci_find_device(bus, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
49 766347cc Isaku Yamahata
}
50 766347cc Isaku Yamahata
51 ce195fb5 Isaku Yamahata
void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
52 766347cc Isaku Yamahata
{
53 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
54 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
55 766347cc Isaku Yamahata
56 766347cc Isaku Yamahata
    if (!pci_dev)
57 766347cc Isaku Yamahata
        return;
58 766347cc Isaku Yamahata
59 766347cc Isaku Yamahata
    PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRI32x" len=%d\n",
60 766347cc Isaku Yamahata
                __func__, pci_dev->name, config_addr, val, len);
61 766347cc Isaku Yamahata
    pci_dev->config_write(pci_dev, config_addr, val, len);
62 766347cc Isaku Yamahata
}
63 766347cc Isaku Yamahata
64 ce195fb5 Isaku Yamahata
uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
65 766347cc Isaku Yamahata
{
66 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
67 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
68 766347cc Isaku Yamahata
    uint32_t val;
69 766347cc Isaku Yamahata
70 4677d8ed Michael S. Tsirkin
    assert(len == 1 || len == 2 || len == 4);
71 766347cc Isaku Yamahata
    if (!pci_dev) {
72 4677d8ed Michael S. Tsirkin
        return ~0x0;
73 766347cc Isaku Yamahata
    }
74 766347cc Isaku Yamahata
75 4677d8ed Michael S. Tsirkin
    val = pci_dev->config_read(pci_dev, config_addr, len);
76 4677d8ed Michael S. Tsirkin
    PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
77 4677d8ed Michael S. Tsirkin
                __func__, pci_dev->name, config_addr, val, len);
78 4677d8ed Michael S. Tsirkin
79 766347cc Isaku Yamahata
    return val;
80 766347cc Isaku Yamahata
}
81 766347cc Isaku Yamahata
82 a455783b Isaku Yamahata
static void pci_host_config_writel(void *opaque, target_phys_addr_t addr,
83 a455783b Isaku Yamahata
                                   uint32_t val)
84 a455783b Isaku Yamahata
{
85 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
86 a455783b Isaku Yamahata
87 a455783b Isaku Yamahata
#ifdef TARGET_WORDS_BIGENDIAN
88 a455783b Isaku Yamahata
    val = bswap32(val);
89 a455783b Isaku Yamahata
#endif
90 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " val %"PRIx32"\n",
91 a455783b Isaku Yamahata
                __func__, addr, val);
92 a455783b Isaku Yamahata
    s->config_reg = val;
93 a455783b Isaku Yamahata
}
94 a455783b Isaku Yamahata
95 a455783b Isaku Yamahata
static uint32_t pci_host_config_readl(void *opaque, target_phys_addr_t addr)
96 a455783b Isaku Yamahata
{
97 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
98 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
99 a455783b Isaku Yamahata
100 a455783b Isaku Yamahata
#ifdef TARGET_WORDS_BIGENDIAN
101 a455783b Isaku Yamahata
    val = bswap32(val);
102 a455783b Isaku Yamahata
#endif
103 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " val %"PRIx32"\n",
104 a455783b Isaku Yamahata
                __func__, addr, val);
105 a455783b Isaku Yamahata
    return val;
106 a455783b Isaku Yamahata
}
107 a455783b Isaku Yamahata
108 a455783b Isaku Yamahata
static CPUWriteMemoryFunc * const pci_host_config_write[] = {
109 a455783b Isaku Yamahata
    &pci_host_config_writel,
110 a455783b Isaku Yamahata
    &pci_host_config_writel,
111 a455783b Isaku Yamahata
    &pci_host_config_writel,
112 a455783b Isaku Yamahata
};
113 a455783b Isaku Yamahata
114 a455783b Isaku Yamahata
static CPUReadMemoryFunc * const pci_host_config_read[] = {
115 a455783b Isaku Yamahata
    &pci_host_config_readl,
116 a455783b Isaku Yamahata
    &pci_host_config_readl,
117 a455783b Isaku Yamahata
    &pci_host_config_readl,
118 a455783b Isaku Yamahata
};
119 a455783b Isaku Yamahata
120 f08b32fe Isaku Yamahata
int pci_host_conf_register_mmio(PCIHostState *s)
121 a455783b Isaku Yamahata
{
122 a455783b Isaku Yamahata
    return cpu_register_io_memory(pci_host_config_read,
123 a455783b Isaku Yamahata
                                  pci_host_config_write, s);
124 a455783b Isaku Yamahata
}
125 a455783b Isaku Yamahata
126 a455783b Isaku Yamahata
static void pci_host_config_writel_noswap(void *opaque,
127 a455783b Isaku Yamahata
                                          target_phys_addr_t addr,
128 a455783b Isaku Yamahata
                                          uint32_t val)
129 a455783b Isaku Yamahata
{
130 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
131 a455783b Isaku Yamahata
132 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " val %"PRIx32"\n",
133 a455783b Isaku Yamahata
                __func__, addr, val);
134 a455783b Isaku Yamahata
    s->config_reg = val;
135 a455783b Isaku Yamahata
}
136 a455783b Isaku Yamahata
137 a455783b Isaku Yamahata
static uint32_t pci_host_config_readl_noswap(void *opaque,
138 a455783b Isaku Yamahata
                                             target_phys_addr_t addr)
139 a455783b Isaku Yamahata
{
140 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
141 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
142 a455783b Isaku Yamahata
143 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " val %"PRIx32"\n",
144 a455783b Isaku Yamahata
                __func__, addr, val);
145 a455783b Isaku Yamahata
    return val;
146 a455783b Isaku Yamahata
}
147 a455783b Isaku Yamahata
148 a455783b Isaku Yamahata
static CPUWriteMemoryFunc * const pci_host_config_write_noswap[] = {
149 a455783b Isaku Yamahata
    &pci_host_config_writel_noswap,
150 a455783b Isaku Yamahata
    &pci_host_config_writel_noswap,
151 a455783b Isaku Yamahata
    &pci_host_config_writel_noswap,
152 a455783b Isaku Yamahata
};
153 a455783b Isaku Yamahata
154 a455783b Isaku Yamahata
static CPUReadMemoryFunc * const pci_host_config_read_noswap[] = {
155 a455783b Isaku Yamahata
    &pci_host_config_readl_noswap,
156 a455783b Isaku Yamahata
    &pci_host_config_readl_noswap,
157 a455783b Isaku Yamahata
    &pci_host_config_readl_noswap,
158 a455783b Isaku Yamahata
};
159 a455783b Isaku Yamahata
160 f08b32fe Isaku Yamahata
int pci_host_conf_register_mmio_noswap(PCIHostState *s)
161 a455783b Isaku Yamahata
{
162 a455783b Isaku Yamahata
    return cpu_register_io_memory(pci_host_config_read_noswap,
163 a455783b Isaku Yamahata
                                  pci_host_config_write_noswap, s);
164 a455783b Isaku Yamahata
}
165 a455783b Isaku Yamahata
166 a455783b Isaku Yamahata
static void pci_host_config_writel_ioport(void *opaque,
167 a455783b Isaku Yamahata
                                          uint32_t addr, uint32_t val)
168 a455783b Isaku Yamahata
{
169 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
170 a455783b Isaku Yamahata
171 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr %"PRIx32 " val %"PRIx32"\n", __func__, addr, val);
172 a455783b Isaku Yamahata
    s->config_reg = val;
173 a455783b Isaku Yamahata
}
174 a455783b Isaku Yamahata
175 a455783b Isaku Yamahata
static uint32_t pci_host_config_readl_ioport(void *opaque, uint32_t addr)
176 a455783b Isaku Yamahata
{
177 a455783b Isaku Yamahata
    PCIHostState *s = opaque;
178 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
179 a455783b Isaku Yamahata
180 a455783b Isaku Yamahata
    PCI_DPRINTF("%s addr %"PRIx32" val %"PRIx32"\n", __func__, addr, val);
181 a455783b Isaku Yamahata
    return val;
182 a455783b Isaku Yamahata
}
183 a455783b Isaku Yamahata
184 f08b32fe Isaku Yamahata
void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
185 a455783b Isaku Yamahata
{
186 a455783b Isaku Yamahata
    register_ioport_write(ioport, 4, 4, pci_host_config_writel_ioport, s);
187 a455783b Isaku Yamahata
    register_ioport_read(ioport, 4, 4, pci_host_config_readl_ioport, s);
188 a455783b Isaku Yamahata
}
189 a455783b Isaku Yamahata
190 4f5e19e6 Isaku Yamahata
#define PCI_ADDR_T      target_phys_addr_t
191 4f5e19e6 Isaku Yamahata
#define PCI_HOST_SUFFIX _mmio
192 4f5e19e6 Isaku Yamahata
193 4f5e19e6 Isaku Yamahata
#include "pci_host_template.h"
194 4f5e19e6 Isaku Yamahata
195 4f5e19e6 Isaku Yamahata
static CPUWriteMemoryFunc * const pci_host_data_write_mmio[] = {
196 4f5e19e6 Isaku Yamahata
    pci_host_data_writeb_mmio,
197 4f5e19e6 Isaku Yamahata
    pci_host_data_writew_mmio,
198 4f5e19e6 Isaku Yamahata
    pci_host_data_writel_mmio,
199 4f5e19e6 Isaku Yamahata
};
200 4f5e19e6 Isaku Yamahata
201 4f5e19e6 Isaku Yamahata
static CPUReadMemoryFunc * const pci_host_data_read_mmio[] = {
202 4f5e19e6 Isaku Yamahata
    pci_host_data_readb_mmio,
203 4f5e19e6 Isaku Yamahata
    pci_host_data_readw_mmio,
204 4f5e19e6 Isaku Yamahata
    pci_host_data_readl_mmio,
205 4f5e19e6 Isaku Yamahata
};
206 4f5e19e6 Isaku Yamahata
207 f08b32fe Isaku Yamahata
int pci_host_data_register_mmio(PCIHostState *s)
208 4f5e19e6 Isaku Yamahata
{
209 4f5e19e6 Isaku Yamahata
    return cpu_register_io_memory(pci_host_data_read_mmio,
210 4f5e19e6 Isaku Yamahata
                                  pci_host_data_write_mmio,
211 4f5e19e6 Isaku Yamahata
                                  s);
212 4f5e19e6 Isaku Yamahata
}
213 4f5e19e6 Isaku Yamahata
214 4f5e19e6 Isaku Yamahata
#undef PCI_ADDR_T
215 4f5e19e6 Isaku Yamahata
#undef PCI_HOST_SUFFIX
216 4f5e19e6 Isaku Yamahata
217 4f5e19e6 Isaku Yamahata
#define PCI_ADDR_T      uint32_t
218 4f5e19e6 Isaku Yamahata
#define PCI_HOST_SUFFIX _ioport
219 4f5e19e6 Isaku Yamahata
220 4f5e19e6 Isaku Yamahata
#include "pci_host_template.h"
221 4f5e19e6 Isaku Yamahata
222 4f5e19e6 Isaku Yamahata
void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
223 4f5e19e6 Isaku Yamahata
{
224 4f5e19e6 Isaku Yamahata
    register_ioport_write(ioport, 4, 1, pci_host_data_writeb_ioport, s);
225 4f5e19e6 Isaku Yamahata
    register_ioport_write(ioport, 4, 2, pci_host_data_writew_ioport, s);
226 4f5e19e6 Isaku Yamahata
    register_ioport_write(ioport, 4, 4, pci_host_data_writel_ioport, s);
227 4f5e19e6 Isaku Yamahata
    register_ioport_read(ioport, 4, 1, pci_host_data_readb_ioport, s);
228 4f5e19e6 Isaku Yamahata
    register_ioport_read(ioport, 4, 2, pci_host_data_readw_ioport, s);
229 4f5e19e6 Isaku Yamahata
    register_ioport_read(ioport, 4, 4, pci_host_data_readl_ioport, s);
230 4f5e19e6 Isaku Yamahata
}