Statistics
| Branch: | Revision:

root / hw / pci_bridge.c @ 99443c21

History | View | Annotate | Download (7.3 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 783753fd Isaku Yamahata
#include "pci_bridge.h"
33 783753fd Isaku Yamahata
#include "pci_internals.h"
34 783753fd Isaku Yamahata
35 f4c817e0 Isaku Yamahata
/* PCI bridge subsystem vendor ID helper functions */
36 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SIZEOF        8
37 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SVID          4
38 f4c817e0 Isaku Yamahata
#define PCI_SSVID_SSID          6
39 f4c817e0 Isaku Yamahata
40 f4c817e0 Isaku Yamahata
int pci_bridge_ssvid_init(PCIDevice *dev, uint8_t offset,
41 f4c817e0 Isaku Yamahata
                          uint16_t svid, uint16_t ssid)
42 f4c817e0 Isaku Yamahata
{
43 f4c817e0 Isaku Yamahata
    int pos;
44 f4c817e0 Isaku Yamahata
    pos = pci_add_capability(dev, PCI_CAP_ID_SSVID, offset, PCI_SSVID_SIZEOF);
45 f4c817e0 Isaku Yamahata
    if (pos < 0) {
46 f4c817e0 Isaku Yamahata
        return pos;
47 f4c817e0 Isaku Yamahata
    }
48 f4c817e0 Isaku Yamahata
49 f4c817e0 Isaku Yamahata
    pci_set_word(dev->config + pos + PCI_SSVID_SVID, svid);
50 f4c817e0 Isaku Yamahata
    pci_set_word(dev->config + pos + PCI_SSVID_SSID, ssid);
51 f4c817e0 Isaku Yamahata
    return pos;
52 f4c817e0 Isaku Yamahata
}
53 f4c817e0 Isaku Yamahata
54 68f79994 Isaku Yamahata
/* Accessor function to get parent bridge device from pci bus. */
55 783753fd Isaku Yamahata
PCIDevice *pci_bridge_get_device(PCIBus *bus)
56 783753fd Isaku Yamahata
{
57 783753fd Isaku Yamahata
    return bus->parent_dev;
58 783753fd Isaku Yamahata
}
59 783753fd Isaku Yamahata
60 68f79994 Isaku Yamahata
/* Accessor function to get secondary bus from pci-to-pci bridge device */
61 68f79994 Isaku Yamahata
PCIBus *pci_bridge_get_sec_bus(PCIBridge *br)
62 68f79994 Isaku Yamahata
{
63 68f79994 Isaku Yamahata
    return &br->sec_bus;
64 68f79994 Isaku Yamahata
}
65 68f79994 Isaku Yamahata
66 68f79994 Isaku Yamahata
static uint32_t pci_config_get_io_base(const PCIDevice *d,
67 783753fd Isaku Yamahata
                                       uint32_t base, uint32_t base_upper16)
68 783753fd Isaku Yamahata
{
69 783753fd Isaku Yamahata
    uint32_t val;
70 783753fd Isaku Yamahata
71 783753fd Isaku Yamahata
    val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
72 783753fd Isaku Yamahata
    if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
73 783753fd Isaku Yamahata
        val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
74 783753fd Isaku Yamahata
    }
75 783753fd Isaku Yamahata
    return val;
76 783753fd Isaku Yamahata
}
77 783753fd Isaku Yamahata
78 68f79994 Isaku Yamahata
static pcibus_t pci_config_get_memory_base(const PCIDevice *d, uint32_t base)
79 783753fd Isaku Yamahata
{
80 783753fd Isaku Yamahata
    return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
81 783753fd Isaku Yamahata
        << 16;
82 783753fd Isaku Yamahata
}
83 783753fd Isaku Yamahata
84 68f79994 Isaku Yamahata
static pcibus_t pci_config_get_pref_base(const PCIDevice *d,
85 783753fd Isaku Yamahata
                                         uint32_t base, uint32_t upper)
86 783753fd Isaku Yamahata
{
87 783753fd Isaku Yamahata
    pcibus_t tmp;
88 783753fd Isaku Yamahata
    pcibus_t val;
89 783753fd Isaku Yamahata
90 783753fd Isaku Yamahata
    tmp = (pcibus_t)pci_get_word(d->config + base);
91 783753fd Isaku Yamahata
    val = (tmp & PCI_PREF_RANGE_MASK) << 16;
92 783753fd Isaku Yamahata
    if (tmp & PCI_PREF_RANGE_TYPE_64) {
93 783753fd Isaku Yamahata
        val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
94 783753fd Isaku Yamahata
    }
95 783753fd Isaku Yamahata
    return val;
96 783753fd Isaku Yamahata
}
97 783753fd Isaku Yamahata
98 68f79994 Isaku Yamahata
/* accessor function to get bridge filtering base address */
99 68f79994 Isaku Yamahata
pcibus_t pci_bridge_get_base(const PCIDevice *bridge, uint8_t type)
100 783753fd Isaku Yamahata
{
101 783753fd Isaku Yamahata
    pcibus_t base;
102 783753fd Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
103 783753fd Isaku Yamahata
        base = pci_config_get_io_base(bridge,
104 783753fd Isaku Yamahata
                                      PCI_IO_BASE, PCI_IO_BASE_UPPER16);
105 783753fd Isaku Yamahata
    } else {
106 783753fd Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
107 783753fd Isaku Yamahata
            base = pci_config_get_pref_base(
108 783753fd Isaku Yamahata
                bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
109 783753fd Isaku Yamahata
        } else {
110 783753fd Isaku Yamahata
            base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
111 783753fd Isaku Yamahata
        }
112 783753fd Isaku Yamahata
    }
113 783753fd Isaku Yamahata
114 783753fd Isaku Yamahata
    return base;
115 783753fd Isaku Yamahata
}
116 783753fd Isaku Yamahata
117 68f79994 Isaku Yamahata
/* accessor funciton to get bridge filtering limit */
118 68f79994 Isaku Yamahata
pcibus_t pci_bridge_get_limit(const PCIDevice *bridge, uint8_t type)
119 783753fd Isaku Yamahata
{
120 783753fd Isaku Yamahata
    pcibus_t limit;
121 783753fd Isaku Yamahata
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
122 783753fd Isaku Yamahata
        limit = pci_config_get_io_base(bridge,
123 783753fd Isaku Yamahata
                                      PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
124 783753fd Isaku Yamahata
        limit |= 0xfff;         /* PCI bridge spec 3.2.5.6. */
125 783753fd Isaku Yamahata
    } else {
126 783753fd Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
127 783753fd Isaku Yamahata
            limit = pci_config_get_pref_base(
128 783753fd Isaku Yamahata
                bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
129 783753fd Isaku Yamahata
        } else {
130 783753fd Isaku Yamahata
            limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
131 783753fd Isaku Yamahata
        }
132 783753fd Isaku Yamahata
        limit |= 0xfffff;       /* PCI bridge spec 3.2.5.{1, 8}. */
133 783753fd Isaku Yamahata
    }
134 783753fd Isaku Yamahata
    return limit;
135 783753fd Isaku Yamahata
}
136 783753fd Isaku Yamahata
137 68f79994 Isaku Yamahata
/* default write_config function for PCI-to-PCI bridge */
138 68f79994 Isaku Yamahata
void pci_bridge_write_config(PCIDevice *d,
139 783753fd Isaku Yamahata
                             uint32_t address, uint32_t val, int len)
140 783753fd Isaku Yamahata
{
141 783753fd Isaku Yamahata
    pci_default_write_config(d, address, val, len);
142 783753fd Isaku Yamahata
143 783753fd Isaku Yamahata
    if (/* io base/limit */
144 783753fd Isaku Yamahata
        ranges_overlap(address, len, PCI_IO_BASE, 2) ||
145 783753fd Isaku Yamahata
146 783753fd Isaku Yamahata
        /* memory base/limit, prefetchable base/limit and
147 783753fd Isaku Yamahata
           io base/limit upper 16 */
148 783753fd Isaku Yamahata
        ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
149 783753fd Isaku Yamahata
        PCIBridge *s = container_of(d, PCIBridge, dev);
150 7e98e3af Isaku Yamahata
        pci_bridge_update_mappings(&s->sec_bus);
151 783753fd Isaku Yamahata
    }
152 783753fd Isaku Yamahata
}
153 783753fd Isaku Yamahata
154 68f79994 Isaku Yamahata
/* reset bridge specific configuration registers */
155 68f79994 Isaku Yamahata
void pci_bridge_reset_reg(PCIDevice *dev)
156 68f79994 Isaku Yamahata
{
157 68f79994 Isaku Yamahata
    uint8_t *conf = dev->config;
158 68f79994 Isaku Yamahata
159 68f79994 Isaku Yamahata
    conf[PCI_PRIMARY_BUS] = 0;
160 68f79994 Isaku Yamahata
    conf[PCI_SECONDARY_BUS] = 0;
161 68f79994 Isaku Yamahata
    conf[PCI_SUBORDINATE_BUS] = 0;
162 68f79994 Isaku Yamahata
    conf[PCI_SEC_LATENCY_TIMER] = 0;
163 68f79994 Isaku Yamahata
164 68f79994 Isaku Yamahata
    conf[PCI_IO_BASE] = 0;
165 68f79994 Isaku Yamahata
    conf[PCI_IO_LIMIT] = 0;
166 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_MEMORY_BASE, 0);
167 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_MEMORY_LIMIT, 0);
168 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_PREF_MEMORY_BASE, 0);
169 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_PREF_MEMORY_LIMIT, 0);
170 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_PREF_BASE_UPPER32, 0);
171 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_PREF_LIMIT_UPPER32, 0);
172 68f79994 Isaku Yamahata
173 68f79994 Isaku Yamahata
    pci_set_word(conf + PCI_BRIDGE_CONTROL, 0);
174 68f79994 Isaku Yamahata
}
175 68f79994 Isaku Yamahata
176 68f79994 Isaku Yamahata
/* default reset function for PCI-to-PCI bridge */
177 68f79994 Isaku Yamahata
void pci_bridge_reset(DeviceState *qdev)
178 783753fd Isaku Yamahata
{
179 68f79994 Isaku Yamahata
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
180 68f79994 Isaku Yamahata
    pci_bridge_reset_reg(dev);
181 68f79994 Isaku Yamahata
}
182 783753fd Isaku Yamahata
183 68f79994 Isaku Yamahata
/* default qdev initialization function for PCI-to-PCI bridge */
184 68f79994 Isaku Yamahata
int pci_bridge_initfn(PCIDevice *dev)
185 68f79994 Isaku Yamahata
{
186 68f79994 Isaku Yamahata
    PCIBus *parent = dev->bus;
187 68f79994 Isaku Yamahata
    PCIBridge *br = DO_UPCAST(PCIBridge, dev, dev);
188 68f79994 Isaku Yamahata
    PCIBus *sec_bus = &br->sec_bus;
189 783753fd Isaku Yamahata
190 783753fd Isaku Yamahata
    pci_set_word(dev->config + PCI_STATUS,
191 783753fd Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
192 783753fd Isaku Yamahata
    pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
193 783753fd Isaku Yamahata
    dev->config[PCI_HEADER_TYPE] =
194 783753fd Isaku Yamahata
        (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
195 783753fd Isaku Yamahata
        PCI_HEADER_TYPE_BRIDGE;
196 783753fd Isaku Yamahata
    pci_set_word(dev->config + PCI_SEC_STATUS,
197 783753fd Isaku Yamahata
                 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
198 68f79994 Isaku Yamahata
199 68f79994 Isaku Yamahata
    qbus_create_inplace(&sec_bus->qbus, &pci_bus_info, &dev->qdev,
200 68f79994 Isaku Yamahata
                        br->bus_name);
201 68f79994 Isaku Yamahata
    sec_bus->parent_dev = dev;
202 68f79994 Isaku Yamahata
    sec_bus->map_irq = br->map_irq;
203 68f79994 Isaku Yamahata
204 68f79994 Isaku Yamahata
    QLIST_INIT(&sec_bus->child);
205 68f79994 Isaku Yamahata
    QLIST_INSERT_HEAD(&parent->child, sec_bus, sibling);
206 783753fd Isaku Yamahata
    return 0;
207 783753fd Isaku Yamahata
}
208 783753fd Isaku Yamahata
209 68f79994 Isaku Yamahata
/* default qdev clean up function for PCI-to-PCI bridge */
210 68f79994 Isaku Yamahata
int pci_bridge_exitfn(PCIDevice *pci_dev)
211 783753fd Isaku Yamahata
{
212 783753fd Isaku Yamahata
    PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
213 51a92333 Isaku Yamahata
    assert(QLIST_EMPTY(&s->sec_bus.child));
214 51a92333 Isaku Yamahata
    QLIST_REMOVE(&s->sec_bus, sibling);
215 68f79994 Isaku Yamahata
    /* qbus_free() is called automatically by qdev_free() */
216 783753fd Isaku Yamahata
    return 0;
217 783753fd Isaku Yamahata
}
218 783753fd Isaku Yamahata
219 68f79994 Isaku Yamahata
/*
220 68f79994 Isaku Yamahata
 * before qdev initialization(qdev_init()), this function sets bus_name and
221 68f79994 Isaku Yamahata
 * map_irq callback which are necessry for pci_bridge_initfn() to
222 68f79994 Isaku Yamahata
 * initialize bus.
223 68f79994 Isaku Yamahata
 */
224 68f79994 Isaku Yamahata
void pci_bridge_map_irq(PCIBridge *br, const char* bus_name,
225 68f79994 Isaku Yamahata
                        pci_map_irq_fn map_irq)
226 783753fd Isaku Yamahata
{
227 68f79994 Isaku Yamahata
    br->map_irq = map_irq;
228 68f79994 Isaku Yamahata
    br->bus_name = bus_name;
229 783753fd Isaku Yamahata
}