Statistics
| Branch: | Revision:

root / hw / pci / pci_bridge.c @ 062db740

History | View | Annotate | Download (14.8 kB)

1 783753fd Isaku Yamahata
/*
2 783753fd Isaku Yamahata
 * QEMU PCI bus manager
3 783753fd Isaku Yamahata
 *
4 783753fd Isaku Yamahata
 * Copyright (c) 2004 Fabrice Bellard
5 783753fd Isaku Yamahata
 *
6 783753fd Isaku Yamahata
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 783753fd Isaku Yamahata
 * of this software and associated documentation files (the "Software"), to dea
8 783753fd Isaku Yamahata

9 783753fd Isaku Yamahata
 * in the Software without restriction, including without limitation the rights
10 783753fd Isaku Yamahata
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 783753fd Isaku Yamahata
 * copies of the Software, and to permit persons to whom the Software is
12 783753fd Isaku Yamahata
 * furnished to do so, subject to the following conditions:
13 783753fd Isaku Yamahata
 *
14 783753fd Isaku Yamahata
 * The above copyright notice and this permission notice shall be included in
15 783753fd Isaku Yamahata
 * all copies or substantial portions of the Software.
16 783753fd Isaku Yamahata
 *
17 783753fd Isaku Yamahata
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 783753fd Isaku Yamahata
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 783753fd Isaku Yamahata
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 783753fd Isaku Yamahata
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 783753fd Isaku Yamahata
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
22 783753fd Isaku Yamahata

23 783753fd Isaku Yamahata
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 783753fd Isaku Yamahata
 * THE SOFTWARE.
25 783753fd Isaku Yamahata
 */
26 783753fd Isaku Yamahata
/*
27 783753fd Isaku Yamahata
 * split out from pci.c
28 783753fd Isaku Yamahata
 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
29 783753fd Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
30 783753fd Isaku Yamahata
 */
31 783753fd Isaku Yamahata
32 c759b24f Michael S. Tsirkin
#include "hw/pci/pci_bridge.h"
33 06aac7bd Michael S. Tsirkin
#include "hw/pci/pci_bus.h"
34 1de7afc9 Paolo Bonzini
#include "qemu/range.h"
35 783753fd Isaku Yamahata
36 f4c817e0 Isaku Yamahata
/* PCI bridge subsystem vendor ID helper functions */
37 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SIZEOF        8
38 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SVID          4
39 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SSID          6
40 f4c817e0 Isaku Yamahata
41 f4c817e0 Isaku Yamahata
int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
42 f4c817e0 Isaku Yamahata
                          uint16_t svid, uint16_t ssid)
43 f4c817e0 Isaku Yamahata
{
44 f4c817e0 Isaku Yamahata
    int pos;
45 f4c817e0 Isaku Yamahata
    pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, PCI_SSVID_SIZEOF);
46 f4c817e0 Isaku Yamahata
    if (pos < 0) {
47 f4c817e0 Isaku Yamahata
        return pos;
48 f4c817e0 Isaku Yamahata
    }
49 f4c817e0 Isaku Yamahata
50 f4c817e0 Isaku Yamahata
    pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid);
51 f4c817e0 Isaku Yamahata
    pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid);
52 f4c817e0 Isaku Yamahata
    return pos;
53 f4c817e0 Isaku Yamahata
}
54 f4c817e0 Isaku Yamahata
55 68f79994 Isaku Yamahata
/* Accessor function to get parent bridge device from pci bus. */
56 783753fd Isaku Yamahata
PCIDevice *pci_bridge_get_device(PCIBus *bus)
57 783753fd Isaku Yamahata
{
58 783753fd Isaku Yamahata
    return bus->parent_dev;
59 783753fd Isaku Yamahata
}
60 783753fd Isaku Yamahata
61 68f79994 Isaku Yamahata
/* Accessor function to get secondary bus from pci-to-pci bridge device */
62 68f79994 Isaku Yamahata
PCIBus *pci_bridge_get_sec_bus(PCIBridge *br)
63 68f79994 Isaku Yamahata
{
64 68f79994 Isaku Yamahata
    return &br->sec_bus;
65 68f79994 Isaku Yamahata
}
66 68f79994 Isaku Yamahata
67 68f79994 Isaku Yamahata
static uint32_t pci_config_get_io_base(const PCIDevice *d,
68 783753fd Isaku Yamahata
                                       uint32_t base, uint32_t base_upper16)
69 783753fd Isaku Yamahata
{
70 783753fd Isaku Yamahata
    uint32_t val;
71 783753fd Isaku Yamahata
72 783753fd Isaku Yamahata
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
73 783753fd Isaku Yamahata
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
74 783753fd Isaku Yamahata
        val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
75 783753fd Isaku Yamahata
    }
76 783753fd Isaku Yamahata
    return val;
77 783753fd Isaku Yamahata
}
78 783753fd Isaku Yamahata
79 68f79994 Isaku Yamahata
static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base)
80 783753fd Isaku Yamahata
{
81 783753fd Isaku Yamahata
    return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
82 783753fd Isaku Yamahata
        << 16;
83 783753fd Isaku Yamahata
}
84 783753fd Isaku Yamahata
85 68f79994 Isaku Yamahata
static pcibus_t pci_config_get_pref_base(const PCIDevice *d,
86 783753fd Isaku Yamahata
                                         uint32_t base, uint32_t upper)
87 783753fd Isaku Yamahata
{
88 783753fd Isaku Yamahata
    pcibus_t tmp;
89 783753fd Isaku Yamahata
    pcibus_t val;
90 783753fd Isaku Yamahata
91 783753fd Isaku Yamahata
    tmp = (pcibus_t)pci_get_word(d->config + base);
92 783753fd Isaku Yamahata
    val = (tmp & PCI_PREF_RANGE_MASK) << 16;
93 783753fd Isaku Yamahata
    if (tmp & PCI_PREF_RANGE_TYPE_64) {
94 783753fd Isaku Yamahata
        val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
95 783753fd Isaku Yamahata
    }
96 783753fd Isaku Yamahata
    return val;
97 783753fd Isaku Yamahata
}
98 783753fd Isaku Yamahata
99 68f79994 Isaku Yamahata
/* accessor function to get bridge filtering base address */
100 68f79994 Isaku Yamahata
pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type)
101 783753fd Isaku Yamahata
{
102 783753fd Isaku Yamahata
    pcibus_t base;
103 783753fd Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
104 783753fd Isaku Yamahata
        base = pci_config_get_io_base(bridge,
105 783753fd Isaku Yamahata
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
106 783753fd Isaku Yamahata
    } else {
107 783753fd Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
108 783753fd Isaku Yamahata
            base = pci_config_get_pref_base(
109 783753fd Isaku Yamahata
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
110 783753fd Isaku Yamahata
        } else {
111 783753fd Isaku Yamahata
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
112 783753fd Isaku Yamahata
        }
113 783753fd Isaku Yamahata
    }
114 783753fd Isaku Yamahata
115 783753fd Isaku Yamahata
    return base;
116 783753fd Isaku Yamahata
}
117 783753fd Isaku Yamahata
118 68f79994 Isaku Yamahata
/* accessor funciton to get bridge filtering limit */
119 68f79994 Isaku Yamahata
pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type)
120 783753fd Isaku Yamahata
{
121 783753fd Isaku Yamahata
    pcibus_t limit;
122 783753fd Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
123 783753fd Isaku Yamahata
        limit = pci_config_get_io_base(bridge,
124 783753fd Isaku Yamahata
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
125 783753fd Isaku Yamahata
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
126 783753fd Isaku Yamahata
    } else {
127 783753fd Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
128 783753fd Isaku Yamahata
            limit = pci_config_get_pref_base(
129 783753fd Isaku Yamahata
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
130 783753fd Isaku Yamahata
        } else {
131 783753fd Isaku Yamahata
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
132 783753fd Isaku Yamahata
        }
133 783753fd Isaku Yamahata
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
134 783753fd Isaku Yamahata
    }
135 783753fd Isaku Yamahata
    return limit;
136 783753fd Isaku Yamahata
}
137 783753fd Isaku Yamahata
138 7df32ca0 Michael S. Tsirkin
static void pci_bridge_init_alias(PCIBridge *bridge, MemoryRegion *alias,
139 7df32ca0 Michael S. Tsirkin
                                  uint8_t type, const char *name,
140 7df32ca0 Michael S. Tsirkin
                                  MemoryRegion *space,
141 7df32ca0 Michael S. Tsirkin
                                  MemoryRegion *parent_space,
142 7df32ca0 Michael S. Tsirkin
                                  bool enabled)
143 7df32ca0 Michael S. Tsirkin
{
144 7df32ca0 Michael S. Tsirkin
    pcibus_t base = pci_bridge_get_base(&bridge->dev, type);
145 7df32ca0 Michael S. Tsirkin
    pcibus_t limit = pci_bridge_get_limit(&bridge->dev, type);
146 7df32ca0 Michael S. Tsirkin
    /* TODO: this doesn't handle base = 0 limit = 2^64 - 1 correctly.
147 7df32ca0 Michael S. Tsirkin
     * Apparently no way to do this with existing memory APIs. */
148 7df32ca0 Michael S. Tsirkin
    pcibus_t size = enabled && limit >= base ? limit + 1 - base : 0;
149 7df32ca0 Michael S. Tsirkin
150 7df32ca0 Michael S. Tsirkin
    memory_region_init_alias(alias, name, space, base, size);
151 7df32ca0 Michael S. Tsirkin
    memory_region_add_subregion_overlap(parent_space, base, alias, 1);
152 7df32ca0 Michael S. Tsirkin
}
153 7df32ca0 Michael S. Tsirkin
154 ba7d8515 Alex Williamson
static void pci_bridge_init_vga_aliases(PCIBridge *br, PCIBus *parent,
155 ba7d8515 Alex Williamson
                                        MemoryRegion *alias_vga)
156 ba7d8515 Alex Williamson
{
157 ba7d8515 Alex Williamson
    uint16_t brctl = pci_get_word(br->dev.config + PCI_BRIDGE_CONTROL);
158 ba7d8515 Alex Williamson
159 ba7d8515 Alex Williamson
    memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_LO],
160 ba7d8515 Alex Williamson
                             "pci_bridge_vga_io_lo", &br->address_space_io,
161 ba7d8515 Alex Williamson
                             QEMU_PCI_VGA_IO_LO_BASE, QEMU_PCI_VGA_IO_LO_SIZE);
162 ba7d8515 Alex Williamson
    memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_IO_HI],
163 ba7d8515 Alex Williamson
                             "pci_bridge_vga_io_hi", &br->address_space_io,
164 ba7d8515 Alex Williamson
                             QEMU_PCI_VGA_IO_HI_BASE, QEMU_PCI_VGA_IO_HI_SIZE);
165 ba7d8515 Alex Williamson
    memory_region_init_alias(&alias_vga[QEMU_PCI_VGA_MEM],
166 ba7d8515 Alex Williamson
                             "pci_bridge_vga_mem", &br->address_space_mem,
167 ba7d8515 Alex Williamson
                             QEMU_PCI_VGA_MEM_BASE, QEMU_PCI_VGA_MEM_SIZE);
168 ba7d8515 Alex Williamson
169 ba7d8515 Alex Williamson
    if (brctl & PCI_BRIDGE_CTL_VGA) {
170 ba7d8515 Alex Williamson
        pci_register_vga(&br->dev, &alias_vga[QEMU_PCI_VGA_MEM],
171 ba7d8515 Alex Williamson
                         &alias_vga[QEMU_PCI_VGA_IO_LO],
172 ba7d8515 Alex Williamson
                         &alias_vga[QEMU_PCI_VGA_IO_HI]);
173 ba7d8515 Alex Williamson
    }
174 ba7d8515 Alex Williamson
}
175 ba7d8515 Alex Williamson
176 b308c82c Avi Kivity
static PCIBridgeWindows *pci_bridge_region_init(PCIBridge *br)
177 7df32ca0 Michael S. Tsirkin
{
178 7df32ca0 Michael S. Tsirkin
    PCIBus *parent = br->dev.bus;
179 b308c82c Avi Kivity
    PCIBridgeWindows *w = g_new(PCIBridgeWindows, 1);
180 7df32ca0 Michael S. Tsirkin
    uint16_t cmd = pci_get_word(br->dev.config + PCI_COMMAND);
181 7df32ca0 Michael S. Tsirkin
182 b308c82c Avi Kivity
    pci_bridge_init_alias(br, &w->alias_pref_mem,
183 7df32ca0 Michael S. Tsirkin
                          PCI_BASE_ADDRESS_MEM_PREFETCH,
184 7df32ca0 Michael S. Tsirkin
                          "pci_bridge_pref_mem",
185 336411ca Michael S. Tsirkin
                          &br->address_space_mem,
186 7df32ca0 Michael S. Tsirkin
                          parent->address_space_mem,
187 7df32ca0 Michael S. Tsirkin
                          cmd & PCI_COMMAND_MEMORY);
188 b308c82c Avi Kivity
    pci_bridge_init_alias(br, &w->alias_mem,
189 7df32ca0 Michael S. Tsirkin
                          PCI_BASE_ADDRESS_SPACE_MEMORY,
190 7df32ca0 Michael S. Tsirkin
                          "pci_bridge_mem",
191 336411ca Michael S. Tsirkin
                          &br->address_space_mem,
192 7df32ca0 Michael S. Tsirkin
                          parent->address_space_mem,
193 7df32ca0 Michael S. Tsirkin
                          cmd & PCI_COMMAND_MEMORY);
194 b308c82c Avi Kivity
    pci_bridge_init_alias(br, &w->alias_io,
195 7df32ca0 Michael S. Tsirkin
                          PCI_BASE_ADDRESS_SPACE_IO,
196 7df32ca0 Michael S. Tsirkin
                          "pci_bridge_io",
197 336411ca Michael S. Tsirkin
                          &br->address_space_io,
198 7df32ca0 Michael S. Tsirkin
                          parent->address_space_io,
199 7df32ca0 Michael S. Tsirkin
                          cmd & PCI_COMMAND_IO);
200 ba7d8515 Alex Williamson
201 ba7d8515 Alex Williamson
    pci_bridge_init_vga_aliases(br, parent, w->alias_vga);
202 b308c82c Avi Kivity
203 b308c82c Avi Kivity
    return w;
204 7df32ca0 Michael S. Tsirkin
}
205 7df32ca0 Michael S. Tsirkin
206 b308c82c Avi Kivity
static void pci_bridge_region_del(PCIBridge *br, PCIBridgeWindows *w)
207 7df32ca0 Michael S. Tsirkin
{
208 7df32ca0 Michael S. Tsirkin
    PCIBus *parent = br->dev.bus;
209 b308c82c Avi Kivity
210 b308c82c Avi Kivity
    memory_region_del_subregion(parent->address_space_io, &w->alias_io);
211 b308c82c Avi Kivity
    memory_region_del_subregion(parent->address_space_mem, &w->alias_mem);
212 b308c82c Avi Kivity
    memory_region_del_subregion(parent->address_space_mem, &w->alias_pref_mem);
213 ba7d8515 Alex Williamson
    pci_unregister_vga(&br->dev);
214 b308c82c Avi Kivity
}
215 b308c82c Avi Kivity
216 b308c82c Avi Kivity
static void pci_bridge_region_cleanup(PCIBridge *br, PCIBridgeWindows *w)
217 b308c82c Avi Kivity
{
218 b308c82c Avi Kivity
    memory_region_destroy(&w->alias_io);
219 b308c82c Avi Kivity
    memory_region_destroy(&w->alias_mem);
220 b308c82c Avi Kivity
    memory_region_destroy(&w->alias_pref_mem);
221 ba7d8515 Alex Williamson
    memory_region_destroy(&w->alias_vga[QEMU_PCI_VGA_IO_LO]);
222 ba7d8515 Alex Williamson
    memory_region_destroy(&w->alias_vga[QEMU_PCI_VGA_IO_HI]);
223 ba7d8515 Alex Williamson
    memory_region_destroy(&w->alias_vga[QEMU_PCI_VGA_MEM]);
224 b308c82c Avi Kivity
    g_free(w);
225 7df32ca0 Michael S. Tsirkin
}
226 7df32ca0 Michael S. Tsirkin
227 7df32ca0 Michael S. Tsirkin
static void pci_bridge_update_mappings(PCIBridge *br)
228 7df32ca0 Michael S. Tsirkin
{
229 b308c82c Avi Kivity
    PCIBridgeWindows *w = br->windows;
230 b308c82c Avi Kivity
231 7df32ca0 Michael S. Tsirkin
    /* Make updates atomic to: handle the case of one VCPU updating the bridge
232 7df32ca0 Michael S. Tsirkin
     * while another accesses an unaffected region. */
233 7df32ca0 Michael S. Tsirkin
    memory_region_transaction_begin();
234 b308c82c Avi Kivity
    pci_bridge_region_del(br, br->windows);
235 b308c82c Avi Kivity
    br->windows = pci_bridge_region_init(br);
236 7df32ca0 Michael S. Tsirkin
    memory_region_transaction_commit();
237 b308c82c Avi Kivity
    pci_bridge_region_cleanup(br, w);
238 7df32ca0 Michael S. Tsirkin
}
239 7df32ca0 Michael S. Tsirkin
240 68f79994 Isaku Yamahata
/* default write_config function for PCI-to-PCI bridge */
241 68f79994 Isaku Yamahata
void pci_bridge_write_config(PCIDevice *d,
242 783753fd Isaku Yamahata
                             uint32_t address, uint32_t val, int len)
243 783753fd Isaku Yamahata
{
244 a5fce077 Isaku Yamahata
    PCIBridge *s = container_of(d, PCIBridge, dev);
245 a5fce077 Isaku Yamahata
    uint16_t oldctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
246 a5fce077 Isaku Yamahata
    uint16_t newctl;
247 a5fce077 Isaku Yamahata
248 783753fd Isaku Yamahata
    pci_default_write_config(d, address, val, len);
249 783753fd Isaku Yamahata
250 7df32ca0 Michael S. Tsirkin
    if (ranges_overlap(address, len, PCI_COMMAND, 2) ||
251 7df32ca0 Michael S. Tsirkin
252 7df32ca0 Michael S. Tsirkin
        /* io base/limit */
253 783753fd Isaku Yamahata
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
254 783753fd Isaku Yamahata
255 783753fd Isaku Yamahata
        /* memory base/limit, prefetchable base/limit and
256 783753fd Isaku Yamahata
           io base/limit upper 16 */
257 ba7d8515 Alex Williamson
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20) ||
258 ba7d8515 Alex Williamson
259 ba7d8515 Alex Williamson
        /* vga enable */
260 ba7d8515 Alex Williamson
        ranges_overlap(address, len, PCI_BRIDGE_CONTROL, 2)) {
261 7df32ca0 Michael S. Tsirkin
        pci_bridge_update_mappings(s);
262 783753fd Isaku Yamahata
    }
263 a5fce077 Isaku Yamahata
264 a5fce077 Isaku Yamahata
    newctl = pci_get_word(d->config + PCI_BRIDGE_CONTROL);
265 a5fce077 Isaku Yamahata
    if (~oldctl & newctl & PCI_BRIDGE_CTL_BUS_RESET) {
266 a5fce077 Isaku Yamahata
        /* Trigger hot reset on 0->1 transition. */
267 a5fce077 Isaku Yamahata
        pci_bus_reset(&s->sec_bus);
268 a5fce077 Isaku Yamahata
    }
269 783753fd Isaku Yamahata
}
270 783753fd Isaku Yamahata
271 0208def1 Isaku Yamahata
void pci_bridge_disable_base_limit(PCIDevice *dev)
272 0208def1 Isaku Yamahata
{
273 0208def1 Isaku Yamahata
    uint8_t *conf = dev->config;
274 0208def1 Isaku Yamahata
275 0208def1 Isaku Yamahata
    pci_byte_test_and_set_mask(conf + PCI_IO_BASE,
276 0208def1 Isaku Yamahata
                               PCI_IO_RANGE_MASK & 0xff);
277 0208def1 Isaku Yamahata
    pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
278 0208def1 Isaku Yamahata
                                 PCI_IO_RANGE_MASK & 0xff);
279 0208def1 Isaku Yamahata
    pci_word_test_and_set_mask(conf + PCI_MEMORY_BASE,
280 0208def1 Isaku Yamahata
                               PCI_MEMORY_RANGE_MASK & 0xffff);
281 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
282 0208def1 Isaku Yamahata
                                 PCI_MEMORY_RANGE_MASK & 0xffff);
283 0208def1 Isaku Yamahata
    pci_word_test_and_set_mask(conf + PCI_PREF_MEMORY_BASE,
284 0208def1 Isaku Yamahata
                               PCI_PREF_RANGE_MASK & 0xffff);
285 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
286 0208def1 Isaku Yamahata
                                 PCI_PREF_RANGE_MASK & 0xffff);
287 cd7898f7 Michael S. Tsirkin
    pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0);
288 cd7898f7 Michael S. Tsirkin
    pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0);
289 0208def1 Isaku Yamahata
}
290 0208def1 Isaku Yamahata
291 68f79994 Isaku Yamahata
/* reset bridge specific configuration registers */
292 cbd2d434 Jan Kiszka
void pci_bridge_reset(DeviceState *qdev)
293 68f79994 Isaku Yamahata
{
294 cbd2d434 Jan Kiszka
    PCIDevice *dev = PCI_DEVICE(qdev);
295 68f79994 Isaku Yamahata
    uint8_t *conf = dev->config;
296 68f79994 Isaku Yamahata
297 68f79994 Isaku Yamahata
    conf[PCI_PRIMARY_BUS] = 0;
298 68f79994 Isaku Yamahata
    conf[PCI_SECONDARY_BUS] = 0;
299 68f79994 Isaku Yamahata
    conf[PCI_SUBORDINATE_BUS] = 0;
300 68f79994 Isaku Yamahata
    conf[PCI_SEC_LATENCY_TIMER] = 0;
301 68f79994 Isaku Yamahata
302 0208def1 Isaku Yamahata
    /*
303 0208def1 Isaku Yamahata
     * the default values for base/limit registers aren't specified
304 0208def1 Isaku Yamahata
     * in the PCI-to-PCI-bridge spec. So we don't thouch them here.
305 0208def1 Isaku Yamahata
     * Each implementation can override it.
306 0208def1 Isaku Yamahata
     * typical implementation does
307 0208def1 Isaku Yamahata
     * zero base/limit registers or
308 0208def1 Isaku Yamahata
     * disable forwarding: pci_bridge_disable_base_limit()
309 0208def1 Isaku Yamahata
     * If disable forwarding is wanted, call pci_bridge_disable_base_limit()
310 0208def1 Isaku Yamahata
     * after this function.
311 0208def1 Isaku Yamahata
     */
312 0208def1 Isaku Yamahata
    pci_byte_test_and_clear_mask(conf + PCI_IO_BASE,
313 0208def1 Isaku Yamahata
                                 PCI_IO_RANGE_MASK & 0xff);
314 0208def1 Isaku Yamahata
    pci_byte_test_and_clear_mask(conf + PCI_IO_LIMIT,
315 0208def1 Isaku Yamahata
                                 PCI_IO_RANGE_MASK & 0xff);
316 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_MEMORY_BASE,
317 0208def1 Isaku Yamahata
                                 PCI_MEMORY_RANGE_MASK & 0xffff);
318 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_MEMORY_LIMIT,
319 0208def1 Isaku Yamahata
                                 PCI_MEMORY_RANGE_MASK & 0xffff);
320 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_BASE,
321 0208def1 Isaku Yamahata
                                 PCI_PREF_RANGE_MASK & 0xffff);
322 0208def1 Isaku Yamahata
    pci_word_test_and_clear_mask(conf + PCI_PREF_MEMORY_LIMIT,
323 0208def1 Isaku Yamahata
                                 PCI_PREF_RANGE_MASK & 0xffff);
324 cd7898f7 Michael S. Tsirkin
    pci_set_long(conf + PCI_PREF_BASE_UPPER32, 0);
325 cd7898f7 Michael S. Tsirkin
    pci_set_long(conf + PCI_PREF_LIMIT_UPPER32, 0);
326 68f79994 Isaku Yamahata
327 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_BRIDGE_CONTROL, 0);
328 68f79994 Isaku Yamahata
}
329 68f79994 Isaku Yamahata
330 68f79994 Isaku Yamahata
/* default qdev initialization function for PCI-to-PCI bridge */
331 60a0e443 Alex Williamson
int pci_bridge_initfn(PCIDevice *dev, const char *typename)
332 68f79994 Isaku Yamahata
{
333 68f79994 Isaku Yamahata
    PCIBus *parent = dev->bus;
334 68f79994 Isaku Yamahata
    PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
335 68f79994 Isaku Yamahata
    PCIBus *sec_bus = &br->sec_bus;
336 783753fd Isaku Yamahata
337 95be1196 Michael S. Tsirkin
    pci_word_test_and_set_mask(dev->config + PCI_STATUS,
338 95be1196 Michael S. Tsirkin
                               PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
339 ba7d8515 Alex Williamson
340 ba7d8515 Alex Williamson
    /*
341 ba7d8515 Alex Williamson
     * TODO: We implement VGA Enable in the Bridge Control Register
342 ba7d8515 Alex Williamson
     * therefore per the PCI to PCI bridge spec we must also implement
343 ba7d8515 Alex Williamson
     * VGA Palette Snooping.  When done, set this bit writable:
344 ba7d8515 Alex Williamson
     *
345 ba7d8515 Alex Williamson
     * pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND,
346 ba7d8515 Alex Williamson
     *                            PCI_COMMAND_VGA_PALETTE);
347 ba7d8515 Alex Williamson
     */
348 ba7d8515 Alex Williamson
349 783753fd Isaku Yamahata
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
350 783753fd Isaku Yamahata
    dev->config[PCI_HEADER_TYPE] =
351 783753fd Isaku Yamahata
        (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
352 783753fd Isaku Yamahata
        PCI_HEADER_TYPE_BRIDGE;
353 783753fd Isaku Yamahata
    pci_set_word(dev->config + PCI_SEC_STATUS,
354 783753fd Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
355 68f79994 Isaku Yamahata
356 8a3d80fa Michael S. Tsirkin
    /*
357 8a3d80fa Michael S. Tsirkin
     * If we don't specify the name, the bus will be addressed as <id>.0, where
358 8a3d80fa Michael S. Tsirkin
     * id is the device id.
359 8a3d80fa Michael S. Tsirkin
     * Since PCI Bridge devices have a single bus each, we don't need the index:
360 8a3d80fa Michael S. Tsirkin
     * let users address the bus using the device name.
361 8a3d80fa Michael S. Tsirkin
     */
362 8a3d80fa Michael S. Tsirkin
    if (!br->bus_name && dev->qdev.id && *dev->qdev.id) {
363 8a3d80fa Michael S. Tsirkin
            br->bus_name = dev->qdev.id;
364 8a3d80fa Michael S. Tsirkin
    }
365 8a3d80fa Michael S. Tsirkin
366 60a0e443 Alex Williamson
    qbus_create_inplace(&sec_bus->qbus, typename, &dev->qdev, br->bus_name);
367 68f79994 Isaku Yamahata
    sec_bus->parent_dev = dev;
368 659fefee Alex Williamson
    sec_bus->map_irq = br->map_irq ? br->map_irq : pci_swizzle_map_irq_fn;
369 336411ca Michael S. Tsirkin
    sec_bus->address_space_mem = &br->address_space_mem;
370 52ce6f05 Blue Swirl
    memory_region_init(&br->address_space_mem, "pci_bridge_pci", INT64_MAX);
371 336411ca Michael S. Tsirkin
    sec_bus->address_space_io = &br->address_space_io;
372 336411ca Michael S. Tsirkin
    memory_region_init(&br->address_space_io, "pci_bridge_io", 65536);
373 b308c82c Avi Kivity
    br->windows = pci_bridge_region_init(br);
374 68f79994 Isaku Yamahata
    QLIST_INIT(&sec_bus->child);
375 68f79994 Isaku Yamahata
    QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
376 783753fd Isaku Yamahata
    return 0;
377 783753fd Isaku Yamahata
}
378 783753fd Isaku Yamahata
379 68f79994 Isaku Yamahata
/* default qdev clean up function for PCI-to-PCI bridge */
380 f90c2bcd Alex Williamson
void pci_bridge_exitfn(PCIDevice *pci_dev)
381 783753fd Isaku Yamahata
{
382 783753fd Isaku Yamahata
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
383 51a92333 Isaku Yamahata
    assert(QLIST_EMPTY(&s->sec_bus.child));
384 51a92333 Isaku Yamahata
    QLIST_REMOVE(&s->sec_bus, sibling);
385 b308c82c Avi Kivity
    pci_bridge_region_del(s, s->windows);
386 b308c82c Avi Kivity
    pci_bridge_region_cleanup(s, s->windows);
387 336411ca Michael S. Tsirkin
    memory_region_destroy(&s->address_space_mem);
388 336411ca Michael S. Tsirkin
    memory_region_destroy(&s->address_space_io);
389 68f79994 Isaku Yamahata
    /* qbus_free() is called automatically by qdev_free() */
390 783753fd Isaku Yamahata
}
391 783753fd Isaku Yamahata
392 68f79994 Isaku Yamahata
/*
393 68f79994 Isaku Yamahata
 * before qdev initialization(qdev_init()), this function sets bus_name and
394 68f79994 Isaku Yamahata
 * map_irq callback which are necessry for pci_bridge_initfn() to
395 68f79994 Isaku Yamahata
 * initialize bus.
396 68f79994 Isaku Yamahata
 */
397 68f79994 Isaku Yamahata
void pci_bridge_map_irq(PCIBridge *br, const char* bus_name,
398 68f79994 Isaku Yamahata
                        pci_map_irq_fn map_irq)
399 783753fd Isaku Yamahata
{
400 68f79994 Isaku Yamahata
    br->map_irq = map_irq;
401 68f79994 Isaku Yamahata
    br->bus_name = bus_name;
402 783753fd Isaku Yamahata
}