Statistics
| Branch: | Revision:

root / hw / pci_host.c @ 9f6f0423

History | View | Annotate | Download (6.2 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 0b987f19 Blue Swirl
    PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " 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 9f6f0423 Michael S. Tsirkin
static void pci_host_config_write(ReadWriteHandler *handler,
83 9f6f0423 Michael S. Tsirkin
                                  pcibus_t addr, uint32_t val, int len)
84 a455783b Isaku Yamahata
{
85 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
86 a455783b Isaku Yamahata
87 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
88 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
89 a455783b Isaku Yamahata
#ifdef TARGET_WORDS_BIGENDIAN
90 9f6f0423 Michael S. Tsirkin
    val = qemu_bswap_len(val, len);
91 a455783b Isaku Yamahata
#endif
92 a455783b Isaku Yamahata
    s->config_reg = val;
93 a455783b Isaku Yamahata
}
94 a455783b Isaku Yamahata
95 9f6f0423 Michael S. Tsirkin
static uint32_t pci_host_config_read(ReadWriteHandler *handler,
96 9f6f0423 Michael S. Tsirkin
                                            pcibus_t addr, int len)
97 a455783b Isaku Yamahata
{
98 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
99 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
100 a455783b Isaku Yamahata
#ifdef TARGET_WORDS_BIGENDIAN
101 9f6f0423 Michael S. Tsirkin
    val = qemu_bswap_len(val, len);
102 a455783b Isaku Yamahata
#endif
103 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
104 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
105 a455783b Isaku Yamahata
    return val;
106 a455783b Isaku Yamahata
}
107 a455783b Isaku Yamahata
108 9f6f0423 Michael S. Tsirkin
static void pci_host_config_write_noswap(ReadWriteHandler *handler,
109 9f6f0423 Michael S. Tsirkin
                                         pcibus_t addr, uint32_t val, int len)
110 a455783b Isaku Yamahata
{
111 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_noswap_handler);
112 a455783b Isaku Yamahata
113 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
114 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
115 a455783b Isaku Yamahata
    s->config_reg = val;
116 a455783b Isaku Yamahata
}
117 a455783b Isaku Yamahata
118 9f6f0423 Michael S. Tsirkin
static uint32_t pci_host_config_read_noswap(ReadWriteHandler *handler,
119 9f6f0423 Michael S. Tsirkin
                                            pcibus_t addr, int len)
120 a455783b Isaku Yamahata
{
121 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_noswap_handler);
122 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
123 a455783b Isaku Yamahata
124 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
125 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
126 a455783b Isaku Yamahata
    return val;
127 a455783b Isaku Yamahata
}
128 a455783b Isaku Yamahata
129 9f6f0423 Michael S. Tsirkin
static void pci_host_data_write(ReadWriteHandler *handler,
130 9f6f0423 Michael S. Tsirkin
                                pcibus_t addr, uint32_t val, int len)
131 a455783b Isaku Yamahata
{
132 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
133 9f6f0423 Michael S. Tsirkin
#ifdef TARGET_WORDS_BIGENDIAN
134 9f6f0423 Michael S. Tsirkin
    val = qemu_bswap_len(val, len);
135 9f6f0423 Michael S. Tsirkin
#endif
136 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n",
137 9f6f0423 Michael S. Tsirkin
                addr, len, val);
138 9f6f0423 Michael S. Tsirkin
    if (s->config_reg & (1u << 31))
139 9f6f0423 Michael S. Tsirkin
        pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
140 a455783b Isaku Yamahata
}
141 a455783b Isaku Yamahata
142 9f6f0423 Michael S. Tsirkin
static uint32_t pci_host_data_read(ReadWriteHandler *handler,
143 9f6f0423 Michael S. Tsirkin
                                   pcibus_t addr, int len)
144 a455783b Isaku Yamahata
{
145 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
146 9f6f0423 Michael S. Tsirkin
    uint32_t val;
147 9f6f0423 Michael S. Tsirkin
    if (!(s->config_reg & (1 << 31)))
148 9f6f0423 Michael S. Tsirkin
        return 0xffffffff;
149 9f6f0423 Michael S. Tsirkin
    val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
150 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n",
151 9f6f0423 Michael S. Tsirkin
                addr, len, val);
152 9f6f0423 Michael S. Tsirkin
#ifdef TARGET_WORDS_BIGENDIAN
153 9f6f0423 Michael S. Tsirkin
    val = qemu_bswap_len(val, len);
154 9f6f0423 Michael S. Tsirkin
#endif
155 9f6f0423 Michael S. Tsirkin
    return val;
156 9f6f0423 Michael S. Tsirkin
}
157 a455783b Isaku Yamahata
158 9f6f0423 Michael S. Tsirkin
static void pci_host_init(PCIHostState *s)
159 9f6f0423 Michael S. Tsirkin
{
160 9f6f0423 Michael S. Tsirkin
    s->conf_handler.write = pci_host_config_write;
161 9f6f0423 Michael S. Tsirkin
    s->conf_handler.read = pci_host_config_read;
162 9f6f0423 Michael S. Tsirkin
    s->conf_noswap_handler.write = pci_host_config_write_noswap;
163 9f6f0423 Michael S. Tsirkin
    s->conf_noswap_handler.read = pci_host_config_read_noswap;
164 9f6f0423 Michael S. Tsirkin
    s->data_handler.write = pci_host_data_write;
165 9f6f0423 Michael S. Tsirkin
    s->data_handler.read = pci_host_data_read;
166 a455783b Isaku Yamahata
}
167 a455783b Isaku Yamahata
168 9f6f0423 Michael S. Tsirkin
int pci_host_conf_register_mmio(PCIHostState *s)
169 a455783b Isaku Yamahata
{
170 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
171 9f6f0423 Michael S. Tsirkin
    return cpu_register_io_memory_simple(&s->conf_handler);
172 9f6f0423 Michael S. Tsirkin
}
173 a455783b Isaku Yamahata
174 9f6f0423 Michael S. Tsirkin
int pci_host_conf_register_mmio_noswap(PCIHostState *s)
175 9f6f0423 Michael S. Tsirkin
{
176 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
177 9f6f0423 Michael S. Tsirkin
    return cpu_register_io_memory_simple(&s->conf_noswap_handler);
178 a455783b Isaku Yamahata
}
179 a455783b Isaku Yamahata
180 f08b32fe Isaku Yamahata
void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
181 a455783b Isaku Yamahata
{
182 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
183 9f6f0423 Michael S. Tsirkin
    register_ioport_simple(&s->conf_noswap_handler, ioport, 4, 4);
184 a455783b Isaku Yamahata
}
185 a455783b Isaku Yamahata
186 f08b32fe Isaku Yamahata
int pci_host_data_register_mmio(PCIHostState *s)
187 4f5e19e6 Isaku Yamahata
{
188 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
189 9f6f0423 Michael S. Tsirkin
    return cpu_register_io_memory_simple(&s->data_handler);
190 4f5e19e6 Isaku Yamahata
}
191 4f5e19e6 Isaku Yamahata
192 4f5e19e6 Isaku Yamahata
void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
193 4f5e19e6 Isaku Yamahata
{
194 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
195 9f6f0423 Michael S. Tsirkin
    register_ioport_simple(&s->data_handler, ioport, 4, 1);
196 9f6f0423 Michael S. Tsirkin
    register_ioport_simple(&s->data_handler, ioport, 4, 2);
197 9f6f0423 Michael S. Tsirkin
    register_ioport_simple(&s->data_handler, ioport, 4, 4);
198 4f5e19e6 Isaku Yamahata
}