root / hw / pci.c @ dd4239d6
History | View | Annotate | Download (43.3 kB)
1 | 69b91039 | bellard | /*
|
---|---|---|---|
2 | 69b91039 | bellard | * QEMU PCI bus manager
|
3 | 69b91039 | bellard | *
|
4 | 69b91039 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 69b91039 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 69b91039 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 69b91039 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 69b91039 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 69b91039 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 69b91039 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 69b91039 | bellard | *
|
13 | 69b91039 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 69b91039 | bellard | * all copies or substantial portions of the Software.
|
15 | 69b91039 | bellard | *
|
16 | 69b91039 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 69b91039 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 69b91039 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 69b91039 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 69b91039 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 69b91039 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 69b91039 | bellard | * THE SOFTWARE.
|
23 | 69b91039 | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "pci.h" |
26 | a9f49946 | Isaku Yamahata | #include "pci_host.h" |
27 | 376253ec | aliguori | #include "monitor.h" |
28 | 87ecb68b | pbrook | #include "net.h" |
29 | 880345c4 | aliguori | #include "sysemu.h" |
30 | 69b91039 | bellard | |
31 | 69b91039 | bellard | //#define DEBUG_PCI
|
32 | d8d2e079 | Isaku Yamahata | #ifdef DEBUG_PCI
|
33 | 2e49d64a | Isaku Yamahata | # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
34 | d8d2e079 | Isaku Yamahata | #else
|
35 | d8d2e079 | Isaku Yamahata | # define PCI_DPRINTF(format, ...) do { } while (0) |
36 | d8d2e079 | Isaku Yamahata | #endif
|
37 | 69b91039 | bellard | |
38 | 30468f78 | bellard | struct PCIBus {
|
39 | 02e2da45 | Paul Brook | BusState qbus; |
40 | 30468f78 | bellard | int devfn_min;
|
41 | 502a5395 | pbrook | pci_set_irq_fn set_irq; |
42 | d2b59317 | pbrook | pci_map_irq_fn map_irq; |
43 | ee995ffb | Gerd Hoffmann | pci_hotplug_fn hotplug; |
44 | 30468f78 | bellard | uint32_t config_reg; /* XXX: suppress */
|
45 | 5d4e84c8 | Juan Quintela | void *irq_opaque;
|
46 | 30468f78 | bellard | PCIDevice *devices[256];
|
47 | 80b3ada7 | pbrook | PCIDevice *parent_dev; |
48 | e822a52a | Isaku Yamahata | |
49 | e822a52a | Isaku Yamahata | QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
|
50 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
|
51 | e822a52a | Isaku Yamahata | |
52 | d2b59317 | pbrook | /* The bus IRQ state is the logical OR of the connected devices.
|
53 | d2b59317 | pbrook | Keep a count of the number of devices with raised IRQs. */
|
54 | 52fc1d83 | balrog | int nirq;
|
55 | 10c4c98a | Gerd Hoffmann | int *irq_count;
|
56 | 10c4c98a | Gerd Hoffmann | }; |
57 | 10c4c98a | Gerd Hoffmann | |
58 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
59 | 10c4c98a | Gerd Hoffmann | |
60 | 10c4c98a | Gerd Hoffmann | static struct BusInfo pci_bus_info = { |
61 | 10c4c98a | Gerd Hoffmann | .name = "PCI",
|
62 | 10c4c98a | Gerd Hoffmann | .size = sizeof(PCIBus),
|
63 | 10c4c98a | Gerd Hoffmann | .print_dev = pcibus_dev_print, |
64 | ee6847d1 | Gerd Hoffmann | .props = (Property[]) { |
65 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
66 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST() |
67 | ee6847d1 | Gerd Hoffmann | } |
68 | 30468f78 | bellard | }; |
69 | 69b91039 | bellard | |
70 | 1941d19c | bellard | static void pci_update_mappings(PCIDevice *d); |
71 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level); |
72 | 1941d19c | bellard | |
73 | c227f099 | Anthony Liguori | target_phys_addr_t pci_mem_base; |
74 | d350d97d | aliguori | static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
75 | d350d97d | aliguori | static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
76 | e822a52a | Isaku Yamahata | |
77 | e822a52a | Isaku Yamahata | struct PCIHostBus {
|
78 | e822a52a | Isaku Yamahata | int domain;
|
79 | e822a52a | Isaku Yamahata | struct PCIBus *bus;
|
80 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIHostBus) next; |
81 | e822a52a | Isaku Yamahata | }; |
82 | e822a52a | Isaku Yamahata | static QLIST_HEAD(, PCIHostBus) host_buses;
|
83 | 30468f78 | bellard | |
84 | 2d1e9f96 | Juan Quintela | static const VMStateDescription vmstate_pcibus = { |
85 | 2d1e9f96 | Juan Quintela | .name = "PCIBUS",
|
86 | 2d1e9f96 | Juan Quintela | .version_id = 1,
|
87 | 2d1e9f96 | Juan Quintela | .minimum_version_id = 1,
|
88 | 2d1e9f96 | Juan Quintela | .minimum_version_id_old = 1,
|
89 | 2d1e9f96 | Juan Quintela | .fields = (VMStateField []) { |
90 | 2d1e9f96 | Juan Quintela | VMSTATE_INT32_EQUAL(nirq, PCIBus), |
91 | c7bde572 | Juan Quintela | VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
92 | 2d1e9f96 | Juan Quintela | VMSTATE_END_OF_LIST() |
93 | 52fc1d83 | balrog | } |
94 | 2d1e9f96 | Juan Quintela | }; |
95 | 52fc1d83 | balrog | |
96 | b3b11697 | Isaku Yamahata | static int pci_bar(PCIDevice *d, int reg) |
97 | 5330de09 | Michael S. Tsirkin | { |
98 | b3b11697 | Isaku Yamahata | uint8_t type; |
99 | b3b11697 | Isaku Yamahata | |
100 | b3b11697 | Isaku Yamahata | if (reg != PCI_ROM_SLOT)
|
101 | b3b11697 | Isaku Yamahata | return PCI_BASE_ADDRESS_0 + reg * 4; |
102 | b3b11697 | Isaku Yamahata | |
103 | b3b11697 | Isaku Yamahata | type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
104 | b3b11697 | Isaku Yamahata | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
105 | 5330de09 | Michael S. Tsirkin | } |
106 | 5330de09 | Michael S. Tsirkin | |
107 | 5330de09 | Michael S. Tsirkin | static void pci_device_reset(PCIDevice *dev) |
108 | 5330de09 | Michael S. Tsirkin | { |
109 | c0b1905b | Michael S. Tsirkin | int r;
|
110 | c0b1905b | Michael S. Tsirkin | |
111 | 5330de09 | Michael S. Tsirkin | memset(dev->irq_state, 0, sizeof dev->irq_state); |
112 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
113 | c0b1905b | Michael S. Tsirkin | PCI_COMMAND_MASTER); |
114 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
115 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
116 | c0b1905b | Michael S. Tsirkin | for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
117 | c0b1905b | Michael S. Tsirkin | if (!dev->io_regions[r].size) {
|
118 | c0b1905b | Michael S. Tsirkin | continue;
|
119 | c0b1905b | Michael S. Tsirkin | } |
120 | b3b11697 | Isaku Yamahata | pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type); |
121 | c0b1905b | Michael S. Tsirkin | } |
122 | c0b1905b | Michael S. Tsirkin | pci_update_mappings(dev); |
123 | 5330de09 | Michael S. Tsirkin | } |
124 | 5330de09 | Michael S. Tsirkin | |
125 | 6eaa6847 | Gleb Natapov | static void pci_bus_reset(void *opaque) |
126 | 6eaa6847 | Gleb Natapov | { |
127 | a60380a5 | Juan Quintela | PCIBus *bus = opaque; |
128 | 6eaa6847 | Gleb Natapov | int i;
|
129 | 6eaa6847 | Gleb Natapov | |
130 | 6eaa6847 | Gleb Natapov | for (i = 0; i < bus->nirq; i++) { |
131 | 6eaa6847 | Gleb Natapov | bus->irq_count[i] = 0;
|
132 | 6eaa6847 | Gleb Natapov | } |
133 | 5330de09 | Michael S. Tsirkin | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
134 | 5330de09 | Michael S. Tsirkin | if (bus->devices[i]) {
|
135 | 5330de09 | Michael S. Tsirkin | pci_device_reset(bus->devices[i]); |
136 | 5330de09 | Michael S. Tsirkin | } |
137 | 6eaa6847 | Gleb Natapov | } |
138 | 6eaa6847 | Gleb Natapov | } |
139 | 6eaa6847 | Gleb Natapov | |
140 | e822a52a | Isaku Yamahata | static void pci_host_bus_register(int domain, PCIBus *bus) |
141 | e822a52a | Isaku Yamahata | { |
142 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
143 | e822a52a | Isaku Yamahata | host = qemu_mallocz(sizeof(*host));
|
144 | e822a52a | Isaku Yamahata | host->domain = domain; |
145 | e822a52a | Isaku Yamahata | host->bus = bus; |
146 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&host_buses, host, next); |
147 | e822a52a | Isaku Yamahata | } |
148 | e822a52a | Isaku Yamahata | |
149 | e822a52a | Isaku Yamahata | PCIBus *pci_find_host_bus(int domain)
|
150 | e822a52a | Isaku Yamahata | { |
151 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
152 | e822a52a | Isaku Yamahata | |
153 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
154 | e822a52a | Isaku Yamahata | if (host->domain == domain) {
|
155 | e822a52a | Isaku Yamahata | return host->bus;
|
156 | e822a52a | Isaku Yamahata | } |
157 | e822a52a | Isaku Yamahata | } |
158 | e822a52a | Isaku Yamahata | |
159 | e822a52a | Isaku Yamahata | return NULL; |
160 | e822a52a | Isaku Yamahata | } |
161 | e822a52a | Isaku Yamahata | |
162 | 21eea4b3 | Gerd Hoffmann | void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
163 | 21eea4b3 | Gerd Hoffmann | const char *name, int devfn_min) |
164 | 30468f78 | bellard | { |
165 | 52fc1d83 | balrog | static int nbus = 0; |
166 | 52fc1d83 | balrog | |
167 | 21eea4b3 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
168 | 502a5395 | pbrook | bus->devfn_min = devfn_min; |
169 | e822a52a | Isaku Yamahata | |
170 | e822a52a | Isaku Yamahata | /* host bridge */
|
171 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
172 | e822a52a | Isaku Yamahata | pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
173 | e822a52a | Isaku Yamahata | |
174 | 2d1e9f96 | Juan Quintela | vmstate_register(nbus++, &vmstate_pcibus, bus); |
175 | a08d4367 | Jan Kiszka | qemu_register_reset(pci_bus_reset, bus); |
176 | 21eea4b3 | Gerd Hoffmann | } |
177 | 21eea4b3 | Gerd Hoffmann | |
178 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
179 | 21eea4b3 | Gerd Hoffmann | { |
180 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
181 | 21eea4b3 | Gerd Hoffmann | |
182 | 21eea4b3 | Gerd Hoffmann | bus = qemu_mallocz(sizeof(*bus));
|
183 | 21eea4b3 | Gerd Hoffmann | bus->qbus.qdev_allocated = 1;
|
184 | 21eea4b3 | Gerd Hoffmann | pci_bus_new_inplace(bus, parent, name, devfn_min); |
185 | 21eea4b3 | Gerd Hoffmann | return bus;
|
186 | 21eea4b3 | Gerd Hoffmann | } |
187 | 21eea4b3 | Gerd Hoffmann | |
188 | 21eea4b3 | Gerd Hoffmann | void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
189 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int nirq) |
190 | 21eea4b3 | Gerd Hoffmann | { |
191 | 21eea4b3 | Gerd Hoffmann | bus->set_irq = set_irq; |
192 | 21eea4b3 | Gerd Hoffmann | bus->map_irq = map_irq; |
193 | 21eea4b3 | Gerd Hoffmann | bus->irq_opaque = irq_opaque; |
194 | 21eea4b3 | Gerd Hoffmann | bus->nirq = nirq; |
195 | 21eea4b3 | Gerd Hoffmann | bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
196 | 21eea4b3 | Gerd Hoffmann | } |
197 | 21eea4b3 | Gerd Hoffmann | |
198 | ee995ffb | Gerd Hoffmann | void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
|
199 | ee995ffb | Gerd Hoffmann | { |
200 | ee995ffb | Gerd Hoffmann | bus->qbus.allow_hotplug = 1;
|
201 | ee995ffb | Gerd Hoffmann | bus->hotplug = hotplug; |
202 | ee995ffb | Gerd Hoffmann | } |
203 | ee995ffb | Gerd Hoffmann | |
204 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
205 | 21eea4b3 | Gerd Hoffmann | pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
206 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int devfn_min, int nirq) |
207 | 21eea4b3 | Gerd Hoffmann | { |
208 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
209 | 21eea4b3 | Gerd Hoffmann | |
210 | 21eea4b3 | Gerd Hoffmann | bus = pci_bus_new(parent, name, devfn_min); |
211 | 21eea4b3 | Gerd Hoffmann | pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
212 | 30468f78 | bellard | return bus;
|
213 | 30468f78 | bellard | } |
214 | 69b91039 | bellard | |
215 | e822a52a | Isaku Yamahata | static void pci_register_secondary_bus(PCIBus *parent, |
216 | e822a52a | Isaku Yamahata | PCIBus *bus, |
217 | 03587182 | Gerd Hoffmann | PCIDevice *dev, |
218 | 03587182 | Gerd Hoffmann | pci_map_irq_fn map_irq, |
219 | 03587182 | Gerd Hoffmann | const char *name) |
220 | 80b3ada7 | pbrook | { |
221 | 03587182 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name); |
222 | 80b3ada7 | pbrook | bus->map_irq = map_irq; |
223 | 80b3ada7 | pbrook | bus->parent_dev = dev; |
224 | e822a52a | Isaku Yamahata | |
225 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
226 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&parent->child, bus, sibling); |
227 | e822a52a | Isaku Yamahata | } |
228 | e822a52a | Isaku Yamahata | |
229 | e822a52a | Isaku Yamahata | static void pci_unregister_secondary_bus(PCIBus *bus) |
230 | e822a52a | Isaku Yamahata | { |
231 | e822a52a | Isaku Yamahata | assert(QLIST_EMPTY(&bus->child)); |
232 | e822a52a | Isaku Yamahata | QLIST_REMOVE(bus, sibling); |
233 | 80b3ada7 | pbrook | } |
234 | 80b3ada7 | pbrook | |
235 | 502a5395 | pbrook | int pci_bus_num(PCIBus *s)
|
236 | 502a5395 | pbrook | { |
237 | e94ff650 | Isaku Yamahata | if (!s->parent_dev)
|
238 | e94ff650 | Isaku Yamahata | return 0; /* pci host bridge */ |
239 | e94ff650 | Isaku Yamahata | return s->parent_dev->config[PCI_SECONDARY_BUS];
|
240 | 502a5395 | pbrook | } |
241 | 502a5395 | pbrook | |
242 | e822a52a | Isaku Yamahata | static uint8_t pci_sub_bus(PCIBus *s)
|
243 | e822a52a | Isaku Yamahata | { |
244 | e822a52a | Isaku Yamahata | if (!s->parent_dev)
|
245 | e822a52a | Isaku Yamahata | return 255; /* pci host bridge */ |
246 | e822a52a | Isaku Yamahata | return s->parent_dev->config[PCI_SUBORDINATE_BUS];
|
247 | e822a52a | Isaku Yamahata | } |
248 | e822a52a | Isaku Yamahata | |
249 | 73534f2f | Juan Quintela | static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
250 | 30ca2aab | bellard | { |
251 | 73534f2f | Juan Quintela | PCIDevice *s = container_of(pv, PCIDevice, config); |
252 | a9f49946 | Isaku Yamahata | uint8_t *config; |
253 | 52fc1d83 | balrog | int i;
|
254 | 52fc1d83 | balrog | |
255 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(s)); |
256 | a9f49946 | Isaku Yamahata | config = qemu_malloc(size); |
257 | a9f49946 | Isaku Yamahata | |
258 | a9f49946 | Isaku Yamahata | qemu_get_buffer(f, config, size); |
259 | a9f49946 | Isaku Yamahata | for (i = 0; i < size; ++i) { |
260 | a9f49946 | Isaku Yamahata | if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
|
261 | a9f49946 | Isaku Yamahata | qemu_free(config); |
262 | bd4b65ee | Michael S. Tsirkin | return -EINVAL;
|
263 | a9f49946 | Isaku Yamahata | } |
264 | a9f49946 | Isaku Yamahata | } |
265 | a9f49946 | Isaku Yamahata | memcpy(s->config, config, size); |
266 | bd4b65ee | Michael S. Tsirkin | |
267 | 1941d19c | bellard | pci_update_mappings(s); |
268 | 52fc1d83 | balrog | |
269 | a9f49946 | Isaku Yamahata | qemu_free(config); |
270 | 30ca2aab | bellard | return 0; |
271 | 30ca2aab | bellard | } |
272 | 30ca2aab | bellard | |
273 | 73534f2f | Juan Quintela | /* just put buffer */
|
274 | 84e2e3eb | Juan Quintela | static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
275 | 73534f2f | Juan Quintela | { |
276 | dbe73d7f | Juan Quintela | const uint8_t **v = pv;
|
277 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
278 | dbe73d7f | Juan Quintela | qemu_put_buffer(f, *v, size); |
279 | 73534f2f | Juan Quintela | } |
280 | 73534f2f | Juan Quintela | |
281 | 73534f2f | Juan Quintela | static VMStateInfo vmstate_info_pci_config = {
|
282 | 73534f2f | Juan Quintela | .name = "pci config",
|
283 | 73534f2f | Juan Quintela | .get = get_pci_config_device, |
284 | 73534f2f | Juan Quintela | .put = put_pci_config_device, |
285 | 73534f2f | Juan Quintela | }; |
286 | 73534f2f | Juan Quintela | |
287 | 73534f2f | Juan Quintela | const VMStateDescription vmstate_pci_device = {
|
288 | 73534f2f | Juan Quintela | .name = "PCIDevice",
|
289 | 73534f2f | Juan Quintela | .version_id = 2,
|
290 | 73534f2f | Juan Quintela | .minimum_version_id = 1,
|
291 | 73534f2f | Juan Quintela | .minimum_version_id_old = 1,
|
292 | 73534f2f | Juan Quintela | .fields = (VMStateField []) { |
293 | 73534f2f | Juan Quintela | VMSTATE_INT32_LE(version_id, PCIDevice), |
294 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
295 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
296 | a9f49946 | Isaku Yamahata | PCI_CONFIG_SPACE_SIZE), |
297 | a9f49946 | Isaku Yamahata | VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
298 | a9f49946 | Isaku Yamahata | VMSTATE_END_OF_LIST() |
299 | a9f49946 | Isaku Yamahata | } |
300 | a9f49946 | Isaku Yamahata | }; |
301 | a9f49946 | Isaku Yamahata | |
302 | a9f49946 | Isaku Yamahata | const VMStateDescription vmstate_pcie_device = {
|
303 | a9f49946 | Isaku Yamahata | .name = "PCIDevice",
|
304 | a9f49946 | Isaku Yamahata | .version_id = 2,
|
305 | a9f49946 | Isaku Yamahata | .minimum_version_id = 1,
|
306 | a9f49946 | Isaku Yamahata | .minimum_version_id_old = 1,
|
307 | a9f49946 | Isaku Yamahata | .fields = (VMStateField []) { |
308 | a9f49946 | Isaku Yamahata | VMSTATE_INT32_LE(version_id, PCIDevice), |
309 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
310 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
311 | a9f49946 | Isaku Yamahata | PCIE_CONFIG_SPACE_SIZE), |
312 | e369cad7 | Isaku Yamahata | VMSTATE_INT32_ARRAY_V(irq_state, PCIDevice, PCI_NUM_PINS, 2),
|
313 | 73534f2f | Juan Quintela | VMSTATE_END_OF_LIST() |
314 | 73534f2f | Juan Quintela | } |
315 | 73534f2f | Juan Quintela | }; |
316 | 73534f2f | Juan Quintela | |
317 | a9f49946 | Isaku Yamahata | static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
318 | a9f49946 | Isaku Yamahata | { |
319 | a9f49946 | Isaku Yamahata | return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
320 | a9f49946 | Isaku Yamahata | } |
321 | a9f49946 | Isaku Yamahata | |
322 | 73534f2f | Juan Quintela | void pci_device_save(PCIDevice *s, QEMUFile *f)
|
323 | 73534f2f | Juan Quintela | { |
324 | a9f49946 | Isaku Yamahata | vmstate_save_state(f, pci_get_vmstate(s), s); |
325 | 73534f2f | Juan Quintela | } |
326 | 73534f2f | Juan Quintela | |
327 | 73534f2f | Juan Quintela | int pci_device_load(PCIDevice *s, QEMUFile *f)
|
328 | 73534f2f | Juan Quintela | { |
329 | a9f49946 | Isaku Yamahata | return vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
|
330 | 73534f2f | Juan Quintela | } |
331 | 73534f2f | Juan Quintela | |
332 | d350d97d | aliguori | static int pci_set_default_subsystem_id(PCIDevice *pci_dev) |
333 | d350d97d | aliguori | { |
334 | d350d97d | aliguori | uint16_t *id; |
335 | d350d97d | aliguori | |
336 | d350d97d | aliguori | id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
|
337 | d350d97d | aliguori | id[0] = cpu_to_le16(pci_default_sub_vendor_id);
|
338 | d350d97d | aliguori | id[1] = cpu_to_le16(pci_default_sub_device_id);
|
339 | d350d97d | aliguori | return 0; |
340 | d350d97d | aliguori | } |
341 | d350d97d | aliguori | |
342 | 880345c4 | aliguori | /*
|
343 | 880345c4 | aliguori | * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
|
344 | 880345c4 | aliguori | */
|
345 | 880345c4 | aliguori | static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp) |
346 | 880345c4 | aliguori | { |
347 | 880345c4 | aliguori | const char *p; |
348 | 880345c4 | aliguori | char *e;
|
349 | 880345c4 | aliguori | unsigned long val; |
350 | 880345c4 | aliguori | unsigned long dom = 0, bus = 0; |
351 | 880345c4 | aliguori | unsigned slot = 0; |
352 | 880345c4 | aliguori | |
353 | 880345c4 | aliguori | p = addr; |
354 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
355 | 880345c4 | aliguori | if (e == p)
|
356 | 880345c4 | aliguori | return -1; |
357 | 880345c4 | aliguori | if (*e == ':') { |
358 | 880345c4 | aliguori | bus = val; |
359 | 880345c4 | aliguori | p = e + 1;
|
360 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
361 | 880345c4 | aliguori | if (e == p)
|
362 | 880345c4 | aliguori | return -1; |
363 | 880345c4 | aliguori | if (*e == ':') { |
364 | 880345c4 | aliguori | dom = bus; |
365 | 880345c4 | aliguori | bus = val; |
366 | 880345c4 | aliguori | p = e + 1;
|
367 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
368 | 880345c4 | aliguori | if (e == p)
|
369 | 880345c4 | aliguori | return -1; |
370 | 880345c4 | aliguori | } |
371 | 880345c4 | aliguori | } |
372 | 880345c4 | aliguori | |
373 | 880345c4 | aliguori | if (dom > 0xffff || bus > 0xff || val > 0x1f) |
374 | 880345c4 | aliguori | return -1; |
375 | 880345c4 | aliguori | |
376 | 880345c4 | aliguori | slot = val; |
377 | 880345c4 | aliguori | |
378 | 880345c4 | aliguori | if (*e)
|
379 | 880345c4 | aliguori | return -1; |
380 | 880345c4 | aliguori | |
381 | 880345c4 | aliguori | /* Note: QEMU doesn't implement domains other than 0 */
|
382 | e822a52a | Isaku Yamahata | if (!pci_find_bus(pci_find_host_bus(dom), bus))
|
383 | 880345c4 | aliguori | return -1; |
384 | 880345c4 | aliguori | |
385 | 880345c4 | aliguori | *domp = dom; |
386 | 880345c4 | aliguori | *busp = bus; |
387 | 880345c4 | aliguori | *slotp = slot; |
388 | 880345c4 | aliguori | return 0; |
389 | 880345c4 | aliguori | } |
390 | 880345c4 | aliguori | |
391 | e9283f8b | Jan Kiszka | int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
392 | e9283f8b | Jan Kiszka | unsigned *slotp)
|
393 | 880345c4 | aliguori | { |
394 | e9283f8b | Jan Kiszka | /* strip legacy tag */
|
395 | e9283f8b | Jan Kiszka | if (!strncmp(addr, "pci_addr=", 9)) { |
396 | e9283f8b | Jan Kiszka | addr += 9;
|
397 | e9283f8b | Jan Kiszka | } |
398 | e9283f8b | Jan Kiszka | if (pci_parse_devaddr(addr, domp, busp, slotp)) {
|
399 | e9283f8b | Jan Kiszka | monitor_printf(mon, "Invalid pci address\n");
|
400 | 880345c4 | aliguori | return -1; |
401 | e9283f8b | Jan Kiszka | } |
402 | e9283f8b | Jan Kiszka | return 0; |
403 | 880345c4 | aliguori | } |
404 | 880345c4 | aliguori | |
405 | 49bd1458 | Markus Armbruster | PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
406 | 5607c388 | Markus Armbruster | { |
407 | 5607c388 | Markus Armbruster | int dom, bus;
|
408 | 5607c388 | Markus Armbruster | unsigned slot;
|
409 | 5607c388 | Markus Armbruster | |
410 | 5607c388 | Markus Armbruster | if (!devaddr) {
|
411 | 5607c388 | Markus Armbruster | *devfnp = -1;
|
412 | e822a52a | Isaku Yamahata | return pci_find_bus(pci_find_host_bus(0), 0); |
413 | 5607c388 | Markus Armbruster | } |
414 | 5607c388 | Markus Armbruster | |
415 | 5607c388 | Markus Armbruster | if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) { |
416 | 5607c388 | Markus Armbruster | return NULL; |
417 | 5607c388 | Markus Armbruster | } |
418 | 5607c388 | Markus Armbruster | |
419 | 5607c388 | Markus Armbruster | *devfnp = slot << 3;
|
420 | e822a52a | Isaku Yamahata | return pci_find_bus(pci_find_host_bus(0), bus); |
421 | 5607c388 | Markus Armbruster | } |
422 | 5607c388 | Markus Armbruster | |
423 | bd4b65ee | Michael S. Tsirkin | static void pci_init_cmask(PCIDevice *dev) |
424 | bd4b65ee | Michael S. Tsirkin | { |
425 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
426 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
427 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
428 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_REVISION_ID] = 0xff;
|
429 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CLASS_PROG] = 0xff;
|
430 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
431 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
432 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
433 | bd4b65ee | Michael S. Tsirkin | } |
434 | bd4b65ee | Michael S. Tsirkin | |
435 | b7ee1603 | Michael S. Tsirkin | static void pci_init_wmask(PCIDevice *dev) |
436 | b7ee1603 | Michael S. Tsirkin | { |
437 | b7ee1603 | Michael S. Tsirkin | int i;
|
438 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(dev);
|
439 | a9f49946 | Isaku Yamahata | |
440 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
441 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
442 | 67a51b48 | Isaku Yamahata | pci_set_word(dev->wmask + PCI_COMMAND, |
443 | 67a51b48 | Isaku Yamahata | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
444 | a9f49946 | Isaku Yamahata | for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
445 | b7ee1603 | Michael S. Tsirkin | dev->wmask[i] = 0xff;
|
446 | b7ee1603 | Michael S. Tsirkin | } |
447 | b7ee1603 | Michael S. Tsirkin | |
448 | fb231628 | Isaku Yamahata | static void pci_init_wmask_bridge(PCIDevice *d) |
449 | fb231628 | Isaku Yamahata | { |
450 | fb231628 | Isaku Yamahata | /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
451 | fb231628 | Isaku Yamahata | PCI_SEC_LETENCY_TIMER */
|
452 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
453 | fb231628 | Isaku Yamahata | |
454 | fb231628 | Isaku Yamahata | /* base and limit */
|
455 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
456 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
457 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_BASE, |
458 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
459 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
460 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
461 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
462 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
463 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
464 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
465 | fb231628 | Isaku Yamahata | |
466 | fb231628 | Isaku Yamahata | /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
467 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
468 | fb231628 | Isaku Yamahata | |
469 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
|
470 | fb231628 | Isaku Yamahata | } |
471 | fb231628 | Isaku Yamahata | |
472 | a9f49946 | Isaku Yamahata | static void pci_config_alloc(PCIDevice *pci_dev) |
473 | a9f49946 | Isaku Yamahata | { |
474 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pci_dev);
|
475 | a9f49946 | Isaku Yamahata | |
476 | a9f49946 | Isaku Yamahata | pci_dev->config = qemu_mallocz(config_size); |
477 | a9f49946 | Isaku Yamahata | pci_dev->cmask = qemu_mallocz(config_size); |
478 | a9f49946 | Isaku Yamahata | pci_dev->wmask = qemu_mallocz(config_size); |
479 | a9f49946 | Isaku Yamahata | pci_dev->used = qemu_mallocz(config_size); |
480 | a9f49946 | Isaku Yamahata | } |
481 | a9f49946 | Isaku Yamahata | |
482 | a9f49946 | Isaku Yamahata | static void pci_config_free(PCIDevice *pci_dev) |
483 | a9f49946 | Isaku Yamahata | { |
484 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->config); |
485 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->cmask); |
486 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->wmask); |
487 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->used); |
488 | a9f49946 | Isaku Yamahata | } |
489 | a9f49946 | Isaku Yamahata | |
490 | 69b91039 | bellard | /* -1 for devfn means auto assign */
|
491 | 6b1b92d3 | Paul Brook | static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
492 | 6b1b92d3 | Paul Brook | const char *name, int devfn, |
493 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
494 | fb231628 | Isaku Yamahata | PCIConfigWriteFunc *config_write, |
495 | fb231628 | Isaku Yamahata | uint8_t header_type) |
496 | 69b91039 | bellard | { |
497 | 69b91039 | bellard | if (devfn < 0) { |
498 | 30468f78 | bellard | for(devfn = bus->devfn_min ; devfn < 256; devfn += 8) { |
499 | 30468f78 | bellard | if (!bus->devices[devfn])
|
500 | 69b91039 | bellard | goto found;
|
501 | 69b91039 | bellard | } |
502 | c364c974 | Blue Swirl | hw_error("PCI: no devfn available for %s, all in use\n", name);
|
503 | 69b91039 | bellard | found: ;
|
504 | 07b7d053 | Markus Armbruster | } else if (bus->devices[devfn]) { |
505 | c364c974 | Blue Swirl | hw_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
|
506 | c364c974 | Blue Swirl | name, bus->devices[devfn]->name); |
507 | 69b91039 | bellard | } |
508 | 30468f78 | bellard | pci_dev->bus = bus; |
509 | 69b91039 | bellard | pci_dev->devfn = devfn; |
510 | 69b91039 | bellard | pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
511 | d2b59317 | pbrook | memset(pci_dev->irq_state, 0, sizeof(pci_dev->irq_state)); |
512 | a9f49946 | Isaku Yamahata | pci_config_alloc(pci_dev); |
513 | fb231628 | Isaku Yamahata | |
514 | fb231628 | Isaku Yamahata | header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
515 | fb231628 | Isaku Yamahata | if (header_type == PCI_HEADER_TYPE_NORMAL) {
|
516 | fb231628 | Isaku Yamahata | pci_set_default_subsystem_id(pci_dev); |
517 | fb231628 | Isaku Yamahata | } |
518 | bd4b65ee | Michael S. Tsirkin | pci_init_cmask(pci_dev); |
519 | b7ee1603 | Michael S. Tsirkin | pci_init_wmask(pci_dev); |
520 | fb231628 | Isaku Yamahata | if (header_type == PCI_HEADER_TYPE_BRIDGE) {
|
521 | fb231628 | Isaku Yamahata | pci_init_wmask_bridge(pci_dev); |
522 | fb231628 | Isaku Yamahata | } |
523 | 0ac32c83 | bellard | |
524 | 0ac32c83 | bellard | if (!config_read)
|
525 | 0ac32c83 | bellard | config_read = pci_default_read_config; |
526 | 0ac32c83 | bellard | if (!config_write)
|
527 | 0ac32c83 | bellard | config_write = pci_default_write_config; |
528 | 69b91039 | bellard | pci_dev->config_read = config_read; |
529 | 69b91039 | bellard | pci_dev->config_write = config_write; |
530 | 30468f78 | bellard | bus->devices[devfn] = pci_dev; |
531 | e369cad7 | Isaku Yamahata | pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
532 | f16c4abf | Juan Quintela | pci_dev->version_id = 2; /* Current pci device vmstate version */ |
533 | 69b91039 | bellard | return pci_dev;
|
534 | 69b91039 | bellard | } |
535 | 69b91039 | bellard | |
536 | 6b1b92d3 | Paul Brook | PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
537 | 6b1b92d3 | Paul Brook | int instance_size, int devfn, |
538 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
539 | 6b1b92d3 | Paul Brook | PCIConfigWriteFunc *config_write) |
540 | 6b1b92d3 | Paul Brook | { |
541 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev; |
542 | 6b1b92d3 | Paul Brook | |
543 | 6b1b92d3 | Paul Brook | pci_dev = qemu_mallocz(instance_size); |
544 | 6b1b92d3 | Paul Brook | pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
545 | fb231628 | Isaku Yamahata | config_read, config_write, |
546 | fb231628 | Isaku Yamahata | PCI_HEADER_TYPE_NORMAL); |
547 | 6b1b92d3 | Paul Brook | return pci_dev;
|
548 | 6b1b92d3 | Paul Brook | } |
549 | c227f099 | Anthony Liguori | static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
|
550 | 5851e08c | aliguori | { |
551 | 5851e08c | aliguori | return addr + pci_mem_base;
|
552 | 5851e08c | aliguori | } |
553 | 5851e08c | aliguori | |
554 | 5851e08c | aliguori | static void pci_unregister_io_regions(PCIDevice *pci_dev) |
555 | 5851e08c | aliguori | { |
556 | 5851e08c | aliguori | PCIIORegion *r; |
557 | 5851e08c | aliguori | int i;
|
558 | 5851e08c | aliguori | |
559 | 5851e08c | aliguori | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
560 | 5851e08c | aliguori | r = &pci_dev->io_regions[i]; |
561 | 182f9c8a | Isaku Yamahata | if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
562 | 5851e08c | aliguori | continue;
|
563 | 0392a017 | Isaku Yamahata | if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
564 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
565 | 5851e08c | aliguori | } else {
|
566 | 5851e08c | aliguori | cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
567 | a0c7a97e | Isaku Yamahata | r->filtered_size, |
568 | 5851e08c | aliguori | IO_MEM_UNASSIGNED); |
569 | 5851e08c | aliguori | } |
570 | 5851e08c | aliguori | } |
571 | 5851e08c | aliguori | } |
572 | 5851e08c | aliguori | |
573 | a36a344d | Gerd Hoffmann | static int pci_unregister_device(DeviceState *dev) |
574 | 5851e08c | aliguori | { |
575 | a36a344d | Gerd Hoffmann | PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
576 | e3936fa5 | Gerd Hoffmann | PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
577 | 5851e08c | aliguori | int ret = 0; |
578 | 5851e08c | aliguori | |
579 | e3936fa5 | Gerd Hoffmann | if (info->exit)
|
580 | e3936fa5 | Gerd Hoffmann | ret = info->exit(pci_dev); |
581 | 5851e08c | aliguori | if (ret)
|
582 | 5851e08c | aliguori | return ret;
|
583 | 5851e08c | aliguori | |
584 | 5851e08c | aliguori | pci_unregister_io_regions(pci_dev); |
585 | 5851e08c | aliguori | |
586 | 5851e08c | aliguori | qemu_free_irqs(pci_dev->irq); |
587 | 5851e08c | aliguori | pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
588 | a9f49946 | Isaku Yamahata | pci_config_free(pci_dev); |
589 | 5851e08c | aliguori | return 0; |
590 | 5851e08c | aliguori | } |
591 | 5851e08c | aliguori | |
592 | 28c2c264 | Avi Kivity | void pci_register_bar(PCIDevice *pci_dev, int region_num, |
593 | 6e355d90 | Isaku Yamahata | pcibus_t size, int type,
|
594 | 69b91039 | bellard | PCIMapIORegionFunc *map_func) |
595 | 69b91039 | bellard | { |
596 | 69b91039 | bellard | PCIIORegion *r; |
597 | d7ce493a | pbrook | uint32_t addr; |
598 | 6e355d90 | Isaku Yamahata | pcibus_t wmask; |
599 | 69b91039 | bellard | |
600 | 8a8696a3 | bellard | if ((unsigned int)region_num >= PCI_NUM_REGIONS) |
601 | 69b91039 | bellard | return;
|
602 | a4c20c6a | aliguori | |
603 | a4c20c6a | aliguori | if (size & (size-1)) { |
604 | a4c20c6a | aliguori | fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
605 | 89e8b13c | Isaku Yamahata | "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
606 | a4c20c6a | aliguori | exit(1);
|
607 | a4c20c6a | aliguori | } |
608 | a4c20c6a | aliguori | |
609 | 69b91039 | bellard | r = &pci_dev->io_regions[region_num]; |
610 | 182f9c8a | Isaku Yamahata | r->addr = PCI_BAR_UNMAPPED; |
611 | 69b91039 | bellard | r->size = size; |
612 | a0c7a97e | Isaku Yamahata | r->filtered_size = size; |
613 | 69b91039 | bellard | r->type = type; |
614 | 69b91039 | bellard | r->map_func = map_func; |
615 | b7ee1603 | Michael S. Tsirkin | |
616 | b7ee1603 | Michael S. Tsirkin | wmask = ~(size - 1);
|
617 | b3b11697 | Isaku Yamahata | addr = pci_bar(pci_dev, region_num); |
618 | d7ce493a | pbrook | if (region_num == PCI_ROM_SLOT) {
|
619 | b7ee1603 | Michael S. Tsirkin | /* ROM enable bit is writeable */
|
620 | 5330de09 | Michael S. Tsirkin | wmask |= PCI_ROM_ADDRESS_ENABLE; |
621 | d7ce493a | pbrook | } |
622 | b0ff8eb2 | Isaku Yamahata | pci_set_long(pci_dev->config + addr, type); |
623 | 14421258 | Isaku Yamahata | if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
624 | 14421258 | Isaku Yamahata | r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
625 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->wmask + addr, wmask); |
626 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
627 | 14421258 | Isaku Yamahata | } else {
|
628 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
629 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
630 | 14421258 | Isaku Yamahata | } |
631 | 69b91039 | bellard | } |
632 | 69b91039 | bellard | |
633 | a0c7a97e | Isaku Yamahata | static uint32_t pci_config_get_io_base(PCIDevice *d,
|
634 | a0c7a97e | Isaku Yamahata | uint32_t base, uint32_t base_upper16) |
635 | a0c7a97e | Isaku Yamahata | { |
636 | a0c7a97e | Isaku Yamahata | uint32_t val; |
637 | a0c7a97e | Isaku Yamahata | |
638 | a0c7a97e | Isaku Yamahata | val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
|
639 | a0c7a97e | Isaku Yamahata | if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
|
640 | a0c7a97e | Isaku Yamahata | val |= (uint32_t)pci_get_word(d->config + PCI_IO_BASE_UPPER16) << 16;
|
641 | a0c7a97e | Isaku Yamahata | } |
642 | a0c7a97e | Isaku Yamahata | return val;
|
643 | a0c7a97e | Isaku Yamahata | } |
644 | a0c7a97e | Isaku Yamahata | |
645 | a0c7a97e | Isaku Yamahata | static uint64_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
|
646 | a0c7a97e | Isaku Yamahata | { |
647 | a0c7a97e | Isaku Yamahata | return ((uint64_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
|
648 | a0c7a97e | Isaku Yamahata | << 16;
|
649 | a0c7a97e | Isaku Yamahata | } |
650 | a0c7a97e | Isaku Yamahata | |
651 | a0c7a97e | Isaku Yamahata | static uint64_t pci_config_get_pref_base(PCIDevice *d,
|
652 | a0c7a97e | Isaku Yamahata | uint32_t base, uint32_t upper) |
653 | a0c7a97e | Isaku Yamahata | { |
654 | a0c7a97e | Isaku Yamahata | uint64_t val; |
655 | a0c7a97e | Isaku Yamahata | val = ((uint64_t)pci_get_word(d->config + base) & |
656 | a0c7a97e | Isaku Yamahata | PCI_PREF_RANGE_MASK) << 16;
|
657 | a0c7a97e | Isaku Yamahata | val |= (uint64_t)pci_get_long(d->config + upper) << 32;
|
658 | a0c7a97e | Isaku Yamahata | return val;
|
659 | a0c7a97e | Isaku Yamahata | } |
660 | a0c7a97e | Isaku Yamahata | |
661 | a0c7a97e | Isaku Yamahata | static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
|
662 | a0c7a97e | Isaku Yamahata | { |
663 | a0c7a97e | Isaku Yamahata | pcibus_t base; |
664 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
665 | a0c7a97e | Isaku Yamahata | base = pci_config_get_io_base(bridge, |
666 | a0c7a97e | Isaku Yamahata | PCI_IO_BASE, PCI_IO_BASE_UPPER16); |
667 | a0c7a97e | Isaku Yamahata | } else {
|
668 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
669 | a0c7a97e | Isaku Yamahata | base = pci_config_get_pref_base( |
670 | a0c7a97e | Isaku Yamahata | bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32); |
671 | a0c7a97e | Isaku Yamahata | } else {
|
672 | a0c7a97e | Isaku Yamahata | base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE); |
673 | a0c7a97e | Isaku Yamahata | } |
674 | a0c7a97e | Isaku Yamahata | } |
675 | a0c7a97e | Isaku Yamahata | |
676 | a0c7a97e | Isaku Yamahata | return base;
|
677 | a0c7a97e | Isaku Yamahata | } |
678 | a0c7a97e | Isaku Yamahata | |
679 | a0c7a97e | Isaku Yamahata | static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
|
680 | a0c7a97e | Isaku Yamahata | { |
681 | a0c7a97e | Isaku Yamahata | pcibus_t limit; |
682 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
683 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_io_base(bridge, |
684 | a0c7a97e | Isaku Yamahata | PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16); |
685 | a0c7a97e | Isaku Yamahata | limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */ |
686 | a0c7a97e | Isaku Yamahata | } else {
|
687 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
688 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_pref_base( |
689 | a0c7a97e | Isaku Yamahata | bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32); |
690 | a0c7a97e | Isaku Yamahata | } else {
|
691 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT); |
692 | a0c7a97e | Isaku Yamahata | } |
693 | a0c7a97e | Isaku Yamahata | limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */ |
694 | a0c7a97e | Isaku Yamahata | } |
695 | a0c7a97e | Isaku Yamahata | return limit;
|
696 | a0c7a97e | Isaku Yamahata | } |
697 | a0c7a97e | Isaku Yamahata | |
698 | a0c7a97e | Isaku Yamahata | static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size, |
699 | a0c7a97e | Isaku Yamahata | uint8_t type) |
700 | a0c7a97e | Isaku Yamahata | { |
701 | a0c7a97e | Isaku Yamahata | pcibus_t base = *addr; |
702 | a0c7a97e | Isaku Yamahata | pcibus_t limit = *addr + *size - 1;
|
703 | a0c7a97e | Isaku Yamahata | PCIDevice *br; |
704 | a0c7a97e | Isaku Yamahata | |
705 | a0c7a97e | Isaku Yamahata | for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
|
706 | a0c7a97e | Isaku Yamahata | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
707 | a0c7a97e | Isaku Yamahata | |
708 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
709 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_IO)) {
|
710 | a0c7a97e | Isaku Yamahata | goto no_map;
|
711 | a0c7a97e | Isaku Yamahata | } |
712 | a0c7a97e | Isaku Yamahata | } else {
|
713 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
714 | a0c7a97e | Isaku Yamahata | goto no_map;
|
715 | a0c7a97e | Isaku Yamahata | } |
716 | a0c7a97e | Isaku Yamahata | } |
717 | a0c7a97e | Isaku Yamahata | |
718 | a0c7a97e | Isaku Yamahata | base = MAX(base, pci_bridge_get_base(br, type)); |
719 | a0c7a97e | Isaku Yamahata | limit = MIN(limit, pci_bridge_get_limit(br, type)); |
720 | a0c7a97e | Isaku Yamahata | } |
721 | a0c7a97e | Isaku Yamahata | |
722 | a0c7a97e | Isaku Yamahata | if (base > limit) {
|
723 | a0c7a97e | Isaku Yamahata | no_map:
|
724 | a0c7a97e | Isaku Yamahata | *addr = PCI_BAR_UNMAPPED; |
725 | a0c7a97e | Isaku Yamahata | *size = 0;
|
726 | a0c7a97e | Isaku Yamahata | } else {
|
727 | a0c7a97e | Isaku Yamahata | *addr = base; |
728 | a0c7a97e | Isaku Yamahata | *size = limit - base + 1;
|
729 | a0c7a97e | Isaku Yamahata | } |
730 | a0c7a97e | Isaku Yamahata | } |
731 | a0c7a97e | Isaku Yamahata | |
732 | 0ac32c83 | bellard | static void pci_update_mappings(PCIDevice *d) |
733 | 0ac32c83 | bellard | { |
734 | 0ac32c83 | bellard | PCIIORegion *r; |
735 | 0ac32c83 | bellard | int cmd, i;
|
736 | 6e355d90 | Isaku Yamahata | pcibus_t last_addr, new_addr; |
737 | a0c7a97e | Isaku Yamahata | pcibus_t filtered_size; |
738 | 3b46e624 | ths | |
739 | b0ff8eb2 | Isaku Yamahata | cmd = pci_get_word(d->config + PCI_COMMAND); |
740 | 8a8696a3 | bellard | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
741 | 0ac32c83 | bellard | r = &d->io_regions[i]; |
742 | a9688570 | Isaku Yamahata | |
743 | a9688570 | Isaku Yamahata | /* this region isn't registered */
|
744 | a9688570 | Isaku Yamahata | if (r->size == 0) |
745 | a9688570 | Isaku Yamahata | continue;
|
746 | a9688570 | Isaku Yamahata | |
747 | a9688570 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
748 | a9688570 | Isaku Yamahata | if (cmd & PCI_COMMAND_IO) {
|
749 | a9688570 | Isaku Yamahata | new_addr = pci_get_long(d->config + pci_bar(d, i)); |
750 | a9688570 | Isaku Yamahata | new_addr = new_addr & ~(r->size - 1);
|
751 | a9688570 | Isaku Yamahata | last_addr = new_addr + r->size - 1;
|
752 | a9688570 | Isaku Yamahata | /* NOTE: we have only 64K ioports on PC */
|
753 | a9688570 | Isaku Yamahata | if (last_addr <= new_addr || new_addr == 0 || |
754 | a9688570 | Isaku Yamahata | last_addr >= 0x10000) {
|
755 | 182f9c8a | Isaku Yamahata | new_addr = PCI_BAR_UNMAPPED; |
756 | 0ac32c83 | bellard | } |
757 | 0ac32c83 | bellard | } else {
|
758 | a9688570 | Isaku Yamahata | new_addr = PCI_BAR_UNMAPPED; |
759 | a9688570 | Isaku Yamahata | } |
760 | a9688570 | Isaku Yamahata | } else {
|
761 | a9688570 | Isaku Yamahata | if (cmd & PCI_COMMAND_MEMORY) {
|
762 | a9688570 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
763 | a9688570 | Isaku Yamahata | new_addr = pci_get_quad(d->config + pci_bar(d, i)); |
764 | 0ac32c83 | bellard | } else {
|
765 | a9688570 | Isaku Yamahata | new_addr = pci_get_long(d->config + pci_bar(d, i)); |
766 | a9688570 | Isaku Yamahata | } |
767 | a9688570 | Isaku Yamahata | /* the ROM slot has a specific enable bit */
|
768 | a9688570 | Isaku Yamahata | if (i == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE))
|
769 | a9688570 | Isaku Yamahata | goto no_mem_map;
|
770 | a9688570 | Isaku Yamahata | new_addr = new_addr & ~(r->size - 1);
|
771 | a9688570 | Isaku Yamahata | last_addr = new_addr + r->size - 1;
|
772 | a9688570 | Isaku Yamahata | /* NOTE: we do not support wrapping */
|
773 | a9688570 | Isaku Yamahata | /* XXX: as we cannot support really dynamic
|
774 | a9688570 | Isaku Yamahata | mappings, we handle specific values as invalid
|
775 | a9688570 | Isaku Yamahata | mappings. */
|
776 | a9688570 | Isaku Yamahata | if (last_addr <= new_addr || new_addr == 0 || |
777 | a9688570 | Isaku Yamahata | last_addr == PCI_BAR_UNMAPPED || |
778 | a9688570 | Isaku Yamahata | |
779 | a9688570 | Isaku Yamahata | /* Now pcibus_t is 64bit.
|
780 | a9688570 | Isaku Yamahata | * Check if 32 bit BAR wrap around explicitly.
|
781 | a9688570 | Isaku Yamahata | * Without this, PC ide doesn't work well.
|
782 | a9688570 | Isaku Yamahata | * TODO: remove this work around.
|
783 | a9688570 | Isaku Yamahata | */
|
784 | a9688570 | Isaku Yamahata | (!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) && |
785 | a9688570 | Isaku Yamahata | last_addr >= UINT32_MAX) || |
786 | a9688570 | Isaku Yamahata | |
787 | a9688570 | Isaku Yamahata | /*
|
788 | a9688570 | Isaku Yamahata | * OS is allowed to set BAR beyond its addressable
|
789 | a9688570 | Isaku Yamahata | * bits. For example, 32 bit OS can set 64bit bar
|
790 | a9688570 | Isaku Yamahata | * to >4G. Check it.
|
791 | a9688570 | Isaku Yamahata | */
|
792 | a9688570 | Isaku Yamahata | last_addr >= TARGET_PHYS_ADDR_MAX) { |
793 | 182f9c8a | Isaku Yamahata | new_addr = PCI_BAR_UNMAPPED; |
794 | 0ac32c83 | bellard | } |
795 | a9688570 | Isaku Yamahata | } else {
|
796 | a9688570 | Isaku Yamahata | no_mem_map:
|
797 | a9688570 | Isaku Yamahata | new_addr = PCI_BAR_UNMAPPED; |
798 | 0ac32c83 | bellard | } |
799 | a9688570 | Isaku Yamahata | } |
800 | a9688570 | Isaku Yamahata | |
801 | a0c7a97e | Isaku Yamahata | /* bridge filtering */
|
802 | a0c7a97e | Isaku Yamahata | filtered_size = r->size; |
803 | a0c7a97e | Isaku Yamahata | if (new_addr != PCI_BAR_UNMAPPED) {
|
804 | a0c7a97e | Isaku Yamahata | pci_bridge_filter(d, &new_addr, &filtered_size, r->type); |
805 | a0c7a97e | Isaku Yamahata | } |
806 | a0c7a97e | Isaku Yamahata | |
807 | a9688570 | Isaku Yamahata | /* This bar isn't changed */
|
808 | a0c7a97e | Isaku Yamahata | if (new_addr == r->addr && filtered_size == r->filtered_size)
|
809 | a9688570 | Isaku Yamahata | continue;
|
810 | a9688570 | Isaku Yamahata | |
811 | a9688570 | Isaku Yamahata | /* now do the real mapping */
|
812 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
813 | a9688570 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
814 | a9688570 | Isaku Yamahata | int class;
|
815 | a9688570 | Isaku Yamahata | /* NOTE: specific hack for IDE in PC case:
|
816 | a9688570 | Isaku Yamahata | only one byte must be mapped. */
|
817 | a9688570 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
818 | a9688570 | Isaku Yamahata | if (class == 0x0101 && r->size == 4) { |
819 | a9688570 | Isaku Yamahata | isa_unassign_ioport(r->addr + 2, 1); |
820 | a9688570 | Isaku Yamahata | } else {
|
821 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
822 | 0ac32c83 | bellard | } |
823 | a9688570 | Isaku Yamahata | } else {
|
824 | a9688570 | Isaku Yamahata | cpu_register_physical_memory(pci_to_cpu_addr(r->addr), |
825 | a0c7a97e | Isaku Yamahata | r->filtered_size, |
826 | a9688570 | Isaku Yamahata | IO_MEM_UNASSIGNED); |
827 | a0c7a97e | Isaku Yamahata | qemu_unregister_coalesced_mmio(r->addr, r->filtered_size); |
828 | 0ac32c83 | bellard | } |
829 | 0ac32c83 | bellard | } |
830 | a9688570 | Isaku Yamahata | r->addr = new_addr; |
831 | a0c7a97e | Isaku Yamahata | r->filtered_size = filtered_size; |
832 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
833 | a0c7a97e | Isaku Yamahata | /*
|
834 | a0c7a97e | Isaku Yamahata | * TODO: currently almost all the map funcions assumes
|
835 | a0c7a97e | Isaku Yamahata | * filtered_size == size and addr & ~(size - 1) == addr.
|
836 | a0c7a97e | Isaku Yamahata | * However with bridge filtering, they aren't always true.
|
837 | a0c7a97e | Isaku Yamahata | * Teach them such cases, such that filtered_size < size and
|
838 | a0c7a97e | Isaku Yamahata | * addr & (size - 1) != 0.
|
839 | a0c7a97e | Isaku Yamahata | */
|
840 | a0c7a97e | Isaku Yamahata | r->map_func(d, i, r->addr, r->filtered_size, r->type); |
841 | a9688570 | Isaku Yamahata | } |
842 | 0ac32c83 | bellard | } |
843 | 0ac32c83 | bellard | } |
844 | 0ac32c83 | bellard | |
845 | 5fafdf24 | ths | uint32_t pci_default_read_config(PCIDevice *d, |
846 | 0ac32c83 | bellard | uint32_t address, int len)
|
847 | 69b91039 | bellard | { |
848 | 5029fe12 | Isaku Yamahata | uint32_t val = 0;
|
849 | 5029fe12 | Isaku Yamahata | assert(len == 1 || len == 2 || len == 4); |
850 | a9f49946 | Isaku Yamahata | len = MIN(len, pci_config_size(d) - address); |
851 | 5029fe12 | Isaku Yamahata | memcpy(&val, d->config + address, len); |
852 | 5029fe12 | Isaku Yamahata | return le32_to_cpu(val);
|
853 | 0ac32c83 | bellard | } |
854 | 0ac32c83 | bellard | |
855 | b7ee1603 | Michael S. Tsirkin | void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
856 | 0ac32c83 | bellard | { |
857 | b7ee1603 | Michael S. Tsirkin | int i;
|
858 | a9f49946 | Isaku Yamahata | uint32_t config_size = pci_config_size(d); |
859 | 0ac32c83 | bellard | |
860 | 91011d4f | Stefan Weil | for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) { |
861 | 91011d4f | Stefan Weil | uint8_t wmask = d->wmask[addr + i]; |
862 | 91011d4f | Stefan Weil | d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
863 | 0ac32c83 | bellard | } |
864 | 260c0cd3 | Isaku Yamahata | if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
865 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
866 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
867 | 260c0cd3 | Isaku Yamahata | range_covers_byte(addr, l, PCI_COMMAND)) |
868 | 0ac32c83 | bellard | pci_update_mappings(d); |
869 | 69b91039 | bellard | } |
870 | 69b91039 | bellard | |
871 | 502a5395 | pbrook | /***********************************************************/
|
872 | 502a5395 | pbrook | /* generic PCI irq support */
|
873 | 30468f78 | bellard | |
874 | 502a5395 | pbrook | /* 0 <= irq_num <= 3. level must be 0 or 1 */
|
875 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level) |
876 | 69b91039 | bellard | { |
877 | a60380a5 | Juan Quintela | PCIDevice *pci_dev = opaque; |
878 | 80b3ada7 | pbrook | PCIBus *bus; |
879 | 80b3ada7 | pbrook | int change;
|
880 | 3b46e624 | ths | |
881 | 80b3ada7 | pbrook | change = level - pci_dev->irq_state[irq_num]; |
882 | 80b3ada7 | pbrook | if (!change)
|
883 | 80b3ada7 | pbrook | return;
|
884 | d2b59317 | pbrook | |
885 | d2b59317 | pbrook | pci_dev->irq_state[irq_num] = level; |
886 | 5e966ce6 | pbrook | for (;;) {
|
887 | 5e966ce6 | pbrook | bus = pci_dev->bus; |
888 | 80b3ada7 | pbrook | irq_num = bus->map_irq(pci_dev, irq_num); |
889 | 5e966ce6 | pbrook | if (bus->set_irq)
|
890 | 5e966ce6 | pbrook | break;
|
891 | 80b3ada7 | pbrook | pci_dev = bus->parent_dev; |
892 | 80b3ada7 | pbrook | } |
893 | 80b3ada7 | pbrook | bus->irq_count[irq_num] += change; |
894 | d2b59317 | pbrook | bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
895 | 69b91039 | bellard | } |
896 | 69b91039 | bellard | |
897 | 502a5395 | pbrook | /***********************************************************/
|
898 | 502a5395 | pbrook | /* monitor info on PCI */
|
899 | 0ac32c83 | bellard | |
900 | 6650ee6d | pbrook | typedef struct { |
901 | 6650ee6d | pbrook | uint16_t class; |
902 | 6650ee6d | pbrook | const char *desc; |
903 | 6650ee6d | pbrook | } pci_class_desc; |
904 | 6650ee6d | pbrook | |
905 | 09bc878a | blueswir1 | static const pci_class_desc pci_class_descriptions[] = |
906 | 6650ee6d | pbrook | { |
907 | 4ca9c76f | pbrook | { 0x0100, "SCSI controller"}, |
908 | 6650ee6d | pbrook | { 0x0101, "IDE controller"}, |
909 | dcb5b19a | ths | { 0x0102, "Floppy controller"}, |
910 | dcb5b19a | ths | { 0x0103, "IPI controller"}, |
911 | dcb5b19a | ths | { 0x0104, "RAID controller"}, |
912 | dcb5b19a | ths | { 0x0106, "SATA controller"}, |
913 | dcb5b19a | ths | { 0x0107, "SAS controller"}, |
914 | dcb5b19a | ths | { 0x0180, "Storage controller"}, |
915 | 6650ee6d | pbrook | { 0x0200, "Ethernet controller"}, |
916 | dcb5b19a | ths | { 0x0201, "Token Ring controller"}, |
917 | dcb5b19a | ths | { 0x0202, "FDDI controller"}, |
918 | dcb5b19a | ths | { 0x0203, "ATM controller"}, |
919 | dcb5b19a | ths | { 0x0280, "Network controller"}, |
920 | 6650ee6d | pbrook | { 0x0300, "VGA controller"}, |
921 | dcb5b19a | ths | { 0x0301, "XGA controller"}, |
922 | dcb5b19a | ths | { 0x0302, "3D controller"}, |
923 | dcb5b19a | ths | { 0x0380, "Display controller"}, |
924 | dcb5b19a | ths | { 0x0400, "Video controller"}, |
925 | dcb5b19a | ths | { 0x0401, "Audio controller"}, |
926 | dcb5b19a | ths | { 0x0402, "Phone"}, |
927 | dcb5b19a | ths | { 0x0480, "Multimedia controller"}, |
928 | dcb5b19a | ths | { 0x0500, "RAM controller"}, |
929 | dcb5b19a | ths | { 0x0501, "Flash controller"}, |
930 | dcb5b19a | ths | { 0x0580, "Memory controller"}, |
931 | 6650ee6d | pbrook | { 0x0600, "Host bridge"}, |
932 | 6650ee6d | pbrook | { 0x0601, "ISA bridge"}, |
933 | dcb5b19a | ths | { 0x0602, "EISA bridge"}, |
934 | dcb5b19a | ths | { 0x0603, "MC bridge"}, |
935 | 6650ee6d | pbrook | { 0x0604, "PCI bridge"}, |
936 | dcb5b19a | ths | { 0x0605, "PCMCIA bridge"}, |
937 | dcb5b19a | ths | { 0x0606, "NUBUS bridge"}, |
938 | dcb5b19a | ths | { 0x0607, "CARDBUS bridge"}, |
939 | dcb5b19a | ths | { 0x0608, "RACEWAY bridge"}, |
940 | dcb5b19a | ths | { 0x0680, "Bridge"}, |
941 | 6650ee6d | pbrook | { 0x0c03, "USB controller"}, |
942 | 6650ee6d | pbrook | { 0, NULL} |
943 | 6650ee6d | pbrook | }; |
944 | 6650ee6d | pbrook | |
945 | e822a52a | Isaku Yamahata | static void pci_info_device(PCIBus *bus, PCIDevice *d) |
946 | 30468f78 | bellard | { |
947 | 376253ec | aliguori | Monitor *mon = cur_mon; |
948 | 502a5395 | pbrook | int i, class;
|
949 | 502a5395 | pbrook | PCIIORegion *r; |
950 | 09bc878a | blueswir1 | const pci_class_desc *desc;
|
951 | 30468f78 | bellard | |
952 | 376253ec | aliguori | monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
|
953 | e94ff650 | Isaku Yamahata | pci_bus_num(d->bus), |
954 | e94ff650 | Isaku Yamahata | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
955 | b0ff8eb2 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
956 | 376253ec | aliguori | monitor_printf(mon, " ");
|
957 | 6650ee6d | pbrook | desc = pci_class_descriptions; |
958 | 6650ee6d | pbrook | while (desc->desc && class != desc->class)
|
959 | 6650ee6d | pbrook | desc++; |
960 | 6650ee6d | pbrook | if (desc->desc) {
|
961 | 376253ec | aliguori | monitor_printf(mon, "%s", desc->desc);
|
962 | 6650ee6d | pbrook | } else {
|
963 | 376253ec | aliguori | monitor_printf(mon, "Class %04x", class);
|
964 | 72cc6cfe | bellard | } |
965 | 376253ec | aliguori | monitor_printf(mon, ": PCI device %04x:%04x\n",
|
966 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_VENDOR_ID), |
967 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_DEVICE_ID)); |
968 | 30468f78 | bellard | |
969 | 502a5395 | pbrook | if (d->config[PCI_INTERRUPT_PIN] != 0) { |
970 | 376253ec | aliguori | monitor_printf(mon, " IRQ %d.\n",
|
971 | 376253ec | aliguori | d->config[PCI_INTERRUPT_LINE]); |
972 | 30468f78 | bellard | } |
973 | 80b3ada7 | pbrook | if (class == 0x0604) { |
974 | b4dccd8d | Isaku Yamahata | uint64_t base; |
975 | b4dccd8d | Isaku Yamahata | uint64_t limit; |
976 | b4dccd8d | Isaku Yamahata | |
977 | 376253ec | aliguori | monitor_printf(mon, " BUS %d.\n", d->config[0x19]); |
978 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " secondary bus %d.\n",
|
979 | b4dccd8d | Isaku Yamahata | d->config[PCI_SECONDARY_BUS]); |
980 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " subordinate bus %d.\n",
|
981 | b4dccd8d | Isaku Yamahata | d->config[PCI_SUBORDINATE_BUS]); |
982 | b4dccd8d | Isaku Yamahata | |
983 | b4dccd8d | Isaku Yamahata | base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO); |
984 | b4dccd8d | Isaku Yamahata | limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO); |
985 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n", |
986 | b4dccd8d | Isaku Yamahata | base, limit); |
987 | b4dccd8d | Isaku Yamahata | |
988 | b4dccd8d | Isaku Yamahata | base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY); |
989 | b4dccd8d | Isaku Yamahata | limit= pci_config_get_memory_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY); |
990 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, |
991 | b4dccd8d | Isaku Yamahata | " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n", |
992 | b4dccd8d | Isaku Yamahata | base, limit); |
993 | b4dccd8d | Isaku Yamahata | |
994 | b4dccd8d | Isaku Yamahata | base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY | |
995 | b4dccd8d | Isaku Yamahata | PCI_BASE_ADDRESS_MEM_PREFETCH); |
996 | b4dccd8d | Isaku Yamahata | limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY | |
997 | b4dccd8d | Isaku Yamahata | PCI_BASE_ADDRESS_MEM_PREFETCH); |
998 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " prefetchable memory range "
|
999 | b4dccd8d | Isaku Yamahata | "[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit); |
1000 | 80b3ada7 | pbrook | } |
1001 | 502a5395 | pbrook | for(i = 0;i < PCI_NUM_REGIONS; i++) { |
1002 | 502a5395 | pbrook | r = &d->io_regions[i]; |
1003 | 502a5395 | pbrook | if (r->size != 0) { |
1004 | 376253ec | aliguori | monitor_printf(mon, " BAR%d: ", i);
|
1005 | 0392a017 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1006 | 89e8b13c | Isaku Yamahata | monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
1007 | 89e8b13c | Isaku Yamahata | " [0x%04"FMT_PCIBUS"].\n", |
1008 | 376253ec | aliguori | r->addr, r->addr + r->size - 1);
|
1009 | 502a5395 | pbrook | } else {
|
1010 | 14421258 | Isaku Yamahata | const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ? |
1011 | 14421258 | Isaku Yamahata | "64 bit" : "32 bit"; |
1012 | 14421258 | Isaku Yamahata | const char *prefetch = |
1013 | 14421258 | Isaku Yamahata | r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ? |
1014 | 14421258 | Isaku Yamahata | " prefetchable" : ""; |
1015 | 14421258 | Isaku Yamahata | |
1016 | 14421258 | Isaku Yamahata | monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
|
1017 | 89e8b13c | Isaku Yamahata | " [0x%08"FMT_PCIBUS"].\n", |
1018 | 14421258 | Isaku Yamahata | type, prefetch, |
1019 | 376253ec | aliguori | r->addr, r->addr + r->size - 1);
|
1020 | 502a5395 | pbrook | } |
1021 | 502a5395 | pbrook | } |
1022 | 77d4bc34 | bellard | } |
1023 | 8ad12514 | Gerd Hoffmann | monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : ""); |
1024 | 80b3ada7 | pbrook | if (class == 0x0604 && d->config[0x19] != 0) { |
1025 | e822a52a | Isaku Yamahata | pci_for_each_device(bus, d->config[0x19], pci_info_device);
|
1026 | 80b3ada7 | pbrook | } |
1027 | 384d8876 | bellard | } |
1028 | 384d8876 | bellard | |
1029 | 1074df4f | Isaku Yamahata | static void pci_for_each_device_under_bus(PCIBus *bus, |
1030 | 1074df4f | Isaku Yamahata | void (*fn)(PCIBus *b, PCIDevice *d))
|
1031 | 384d8876 | bellard | { |
1032 | 384d8876 | bellard | PCIDevice *d; |
1033 | 502a5395 | pbrook | int devfn;
|
1034 | 3b46e624 | ths | |
1035 | 1074df4f | Isaku Yamahata | for(devfn = 0; devfn < 256; devfn++) { |
1036 | 1074df4f | Isaku Yamahata | d = bus->devices[devfn]; |
1037 | 1074df4f | Isaku Yamahata | if (d)
|
1038 | 1074df4f | Isaku Yamahata | fn(bus, d); |
1039 | 1074df4f | Isaku Yamahata | } |
1040 | 1074df4f | Isaku Yamahata | } |
1041 | 1074df4f | Isaku Yamahata | |
1042 | 1074df4f | Isaku Yamahata | void pci_for_each_device(PCIBus *bus, int bus_num, |
1043 | 1074df4f | Isaku Yamahata | void (*fn)(PCIBus *b, PCIDevice *d))
|
1044 | 1074df4f | Isaku Yamahata | { |
1045 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1046 | 1074df4f | Isaku Yamahata | |
1047 | 502a5395 | pbrook | if (bus) {
|
1048 | 1074df4f | Isaku Yamahata | pci_for_each_device_under_bus(bus, fn); |
1049 | f2aa58c6 | bellard | } |
1050 | f2aa58c6 | bellard | } |
1051 | f2aa58c6 | bellard | |
1052 | 376253ec | aliguori | void pci_info(Monitor *mon)
|
1053 | f2aa58c6 | bellard | { |
1054 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
1055 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
1056 | e822a52a | Isaku Yamahata | pci_for_each_device(host->bus, 0, pci_info_device);
|
1057 | e822a52a | Isaku Yamahata | } |
1058 | 77d4bc34 | bellard | } |
1059 | a41b2ff2 | pbrook | |
1060 | cb457d76 | aliguori | static const char * const pci_nic_models[] = { |
1061 | cb457d76 | aliguori | "ne2k_pci",
|
1062 | cb457d76 | aliguori | "i82551",
|
1063 | cb457d76 | aliguori | "i82557b",
|
1064 | cb457d76 | aliguori | "i82559er",
|
1065 | cb457d76 | aliguori | "rtl8139",
|
1066 | cb457d76 | aliguori | "e1000",
|
1067 | cb457d76 | aliguori | "pcnet",
|
1068 | cb457d76 | aliguori | "virtio",
|
1069 | cb457d76 | aliguori | NULL
|
1070 | cb457d76 | aliguori | }; |
1071 | cb457d76 | aliguori | |
1072 | 9d07d757 | Paul Brook | static const char * const pci_nic_names[] = { |
1073 | 9d07d757 | Paul Brook | "ne2k_pci",
|
1074 | 9d07d757 | Paul Brook | "i82551",
|
1075 | 9d07d757 | Paul Brook | "i82557b",
|
1076 | 9d07d757 | Paul Brook | "i82559er",
|
1077 | 9d07d757 | Paul Brook | "rtl8139",
|
1078 | 9d07d757 | Paul Brook | "e1000",
|
1079 | 9d07d757 | Paul Brook | "pcnet",
|
1080 | 53c25cea | Paul Brook | "virtio-net-pci",
|
1081 | cb457d76 | aliguori | NULL
|
1082 | cb457d76 | aliguori | }; |
1083 | cb457d76 | aliguori | |
1084 | a41b2ff2 | pbrook | /* Initialize a PCI NIC. */
|
1085 | 33e66b86 | Markus Armbruster | /* FIXME callers should check for failure, but don't */
|
1086 | 5607c388 | Markus Armbruster | PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
1087 | 5607c388 | Markus Armbruster | const char *default_devaddr) |
1088 | a41b2ff2 | pbrook | { |
1089 | 5607c388 | Markus Armbruster | const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1090 | 07caea31 | Markus Armbruster | PCIBus *bus; |
1091 | 07caea31 | Markus Armbruster | int devfn;
|
1092 | 5607c388 | Markus Armbruster | PCIDevice *pci_dev; |
1093 | 9d07d757 | Paul Brook | DeviceState *dev; |
1094 | cb457d76 | aliguori | int i;
|
1095 | cb457d76 | aliguori | |
1096 | 07caea31 | Markus Armbruster | i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1097 | 07caea31 | Markus Armbruster | if (i < 0) |
1098 | 07caea31 | Markus Armbruster | return NULL; |
1099 | 07caea31 | Markus Armbruster | |
1100 | 07caea31 | Markus Armbruster | bus = pci_get_bus_devfn(&devfn, devaddr); |
1101 | 07caea31 | Markus Armbruster | if (!bus) {
|
1102 | 07caea31 | Markus Armbruster | qemu_error("Invalid PCI device address %s for device %s\n",
|
1103 | 07caea31 | Markus Armbruster | devaddr, pci_nic_names[i]); |
1104 | 07caea31 | Markus Armbruster | return NULL; |
1105 | 07caea31 | Markus Armbruster | } |
1106 | 07caea31 | Markus Armbruster | |
1107 | 499cf102 | Markus Armbruster | pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1108 | 9ee05825 | Markus Armbruster | dev = &pci_dev->qdev; |
1109 | dea7b3b9 | Mark McLoughlin | if (nd->name)
|
1110 | dea7b3b9 | Mark McLoughlin | dev->id = qemu_strdup(nd->name); |
1111 | 1cc33683 | Gerd Hoffmann | qdev_set_nic_properties(dev, nd); |
1112 | 07caea31 | Markus Armbruster | if (qdev_init(dev) < 0) |
1113 | 07caea31 | Markus Armbruster | return NULL; |
1114 | 9ee05825 | Markus Armbruster | return pci_dev;
|
1115 | a41b2ff2 | pbrook | } |
1116 | a41b2ff2 | pbrook | |
1117 | 07caea31 | Markus Armbruster | PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
1118 | 07caea31 | Markus Armbruster | const char *default_devaddr) |
1119 | 07caea31 | Markus Armbruster | { |
1120 | 07caea31 | Markus Armbruster | PCIDevice *res; |
1121 | 07caea31 | Markus Armbruster | |
1122 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1123 | 07caea31 | Markus Armbruster | exit(0);
|
1124 | 07caea31 | Markus Armbruster | |
1125 | 07caea31 | Markus Armbruster | res = pci_nic_init(nd, default_model, default_devaddr); |
1126 | 07caea31 | Markus Armbruster | if (!res)
|
1127 | 07caea31 | Markus Armbruster | exit(1);
|
1128 | 07caea31 | Markus Armbruster | return res;
|
1129 | 07caea31 | Markus Armbruster | } |
1130 | 07caea31 | Markus Armbruster | |
1131 | 80b3ada7 | pbrook | typedef struct { |
1132 | 80b3ada7 | pbrook | PCIDevice dev; |
1133 | 03587182 | Gerd Hoffmann | PCIBus bus; |
1134 | 03587182 | Gerd Hoffmann | uint32_t vid; |
1135 | 03587182 | Gerd Hoffmann | uint32_t did; |
1136 | 80b3ada7 | pbrook | } PCIBridge; |
1137 | 80b3ada7 | pbrook | |
1138 | a0c7a97e | Isaku Yamahata | |
1139 | a0c7a97e | Isaku Yamahata | static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d) |
1140 | a0c7a97e | Isaku Yamahata | { |
1141 | a0c7a97e | Isaku Yamahata | pci_update_mappings(d); |
1142 | a0c7a97e | Isaku Yamahata | } |
1143 | a0c7a97e | Isaku Yamahata | |
1144 | a0c7a97e | Isaku Yamahata | static void pci_bridge_update_mappings(PCIBus *b) |
1145 | a0c7a97e | Isaku Yamahata | { |
1146 | a0c7a97e | Isaku Yamahata | PCIBus *child; |
1147 | a0c7a97e | Isaku Yamahata | |
1148 | a0c7a97e | Isaku Yamahata | pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn); |
1149 | a0c7a97e | Isaku Yamahata | |
1150 | a0c7a97e | Isaku Yamahata | QLIST_FOREACH(child, &b->child, sibling) { |
1151 | a0c7a97e | Isaku Yamahata | pci_bridge_update_mappings(child); |
1152 | a0c7a97e | Isaku Yamahata | } |
1153 | a0c7a97e | Isaku Yamahata | } |
1154 | a0c7a97e | Isaku Yamahata | |
1155 | 9596ebb7 | pbrook | static void pci_bridge_write_config(PCIDevice *d, |
1156 | 80b3ada7 | pbrook | uint32_t address, uint32_t val, int len)
|
1157 | 80b3ada7 | pbrook | { |
1158 | 80b3ada7 | pbrook | pci_default_write_config(d, address, val, len); |
1159 | a0c7a97e | Isaku Yamahata | |
1160 | a0c7a97e | Isaku Yamahata | if (/* io base/limit */ |
1161 | a0c7a97e | Isaku Yamahata | ranges_overlap(address, len, PCI_IO_BASE, 2) ||
|
1162 | a0c7a97e | Isaku Yamahata | |
1163 | a0c7a97e | Isaku Yamahata | /* memory base/limit, prefetchable base/limit and
|
1164 | a0c7a97e | Isaku Yamahata | io base/limit upper 16 */
|
1165 | a0c7a97e | Isaku Yamahata | ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
|
1166 | a0c7a97e | Isaku Yamahata | pci_bridge_update_mappings(d->bus); |
1167 | a0c7a97e | Isaku Yamahata | } |
1168 | 80b3ada7 | pbrook | } |
1169 | 80b3ada7 | pbrook | |
1170 | e822a52a | Isaku Yamahata | PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
1171 | 3ae80618 | aliguori | { |
1172 | e822a52a | Isaku Yamahata | PCIBus *sec; |
1173 | 3ae80618 | aliguori | |
1174 | e822a52a | Isaku Yamahata | if (!bus)
|
1175 | e822a52a | Isaku Yamahata | return NULL; |
1176 | 3ae80618 | aliguori | |
1177 | e822a52a | Isaku Yamahata | if (pci_bus_num(bus) == bus_num) {
|
1178 | e822a52a | Isaku Yamahata | return bus;
|
1179 | e822a52a | Isaku Yamahata | } |
1180 | e822a52a | Isaku Yamahata | |
1181 | e822a52a | Isaku Yamahata | /* try child bus */
|
1182 | e822a52a | Isaku Yamahata | QLIST_FOREACH(sec, &bus->child, sibling) { |
1183 | e822a52a | Isaku Yamahata | if (pci_bus_num(sec) <= bus_num && bus_num <= pci_sub_bus(sec)) {
|
1184 | e822a52a | Isaku Yamahata | return pci_find_bus(sec, bus_num);
|
1185 | e822a52a | Isaku Yamahata | } |
1186 | e822a52a | Isaku Yamahata | } |
1187 | e822a52a | Isaku Yamahata | |
1188 | e822a52a | Isaku Yamahata | return NULL; |
1189 | 3ae80618 | aliguori | } |
1190 | 3ae80618 | aliguori | |
1191 | e822a52a | Isaku Yamahata | PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
1192 | 3ae80618 | aliguori | { |
1193 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1194 | 3ae80618 | aliguori | |
1195 | 3ae80618 | aliguori | if (!bus)
|
1196 | 3ae80618 | aliguori | return NULL; |
1197 | 3ae80618 | aliguori | |
1198 | 3ae80618 | aliguori | return bus->devices[PCI_DEVFN(slot, function)];
|
1199 | 3ae80618 | aliguori | } |
1200 | 3ae80618 | aliguori | |
1201 | 03587182 | Gerd Hoffmann | static int pci_bridge_initfn(PCIDevice *dev) |
1202 | 80b3ada7 | pbrook | { |
1203 | 03587182 | Gerd Hoffmann | PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev); |
1204 | 480b9f24 | blueswir1 | |
1205 | 03587182 | Gerd Hoffmann | pci_config_set_vendor_id(s->dev.config, s->vid); |
1206 | 03587182 | Gerd Hoffmann | pci_config_set_device_id(s->dev.config, s->did); |
1207 | 480b9f24 | blueswir1 | |
1208 | 74c01823 | Isaku Yamahata | /* TODO: intial value
|
1209 | 74c01823 | Isaku Yamahata | * command register:
|
1210 | 74c01823 | Isaku Yamahata | * According to PCI bridge spec, after reset
|
1211 | 74c01823 | Isaku Yamahata | * bus master bit is off
|
1212 | 74c01823 | Isaku Yamahata | * memory space enable bit is off
|
1213 | 74c01823 | Isaku Yamahata | * According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
|
1214 | 74c01823 | Isaku Yamahata | * the reset value should be zero unless the boot pin is tied high
|
1215 | 74c01823 | Isaku Yamahata | * (which is tru) and thus it should be PCI_COMMAND_MEMORY.
|
1216 | 74c01823 | Isaku Yamahata | *
|
1217 | 74c01823 | Isaku Yamahata | * For now, don't touch the value.
|
1218 | 74c01823 | Isaku Yamahata | * Later command register will be set to zero and apb_pci.c will
|
1219 | 74c01823 | Isaku Yamahata | * override the value.
|
1220 | 74c01823 | Isaku Yamahata | * Same for latency timer, and multi function bit of header type.
|
1221 | 74c01823 | Isaku Yamahata | */
|
1222 | 74c01823 | Isaku Yamahata | pci_set_word(dev->config + PCI_COMMAND, |
1223 | 74c01823 | Isaku Yamahata | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
1224 | 74c01823 | Isaku Yamahata | |
1225 | 74c01823 | Isaku Yamahata | pci_set_word(dev->config + PCI_STATUS, |
1226 | 74c01823 | Isaku Yamahata | PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1227 | 74c01823 | Isaku Yamahata | pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); |
1228 | 74c01823 | Isaku Yamahata | dev->config[PCI_LATENCY_TIMER] = 0x10;
|
1229 | 74c01823 | Isaku Yamahata | dev->config[PCI_HEADER_TYPE] = |
1230 | 74c01823 | Isaku Yamahata | PCI_HEADER_TYPE_MULTI_FUNCTION | PCI_HEADER_TYPE_BRIDGE; |
1231 | 74c01823 | Isaku Yamahata | pci_set_word(dev->config + PCI_SEC_STATUS, |
1232 | 74c01823 | Isaku Yamahata | PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1233 | 03587182 | Gerd Hoffmann | return 0; |
1234 | 03587182 | Gerd Hoffmann | } |
1235 | 80b3ada7 | pbrook | |
1236 | e822a52a | Isaku Yamahata | static int pci_bridge_exitfn(PCIDevice *pci_dev) |
1237 | e822a52a | Isaku Yamahata | { |
1238 | e822a52a | Isaku Yamahata | PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev); |
1239 | e822a52a | Isaku Yamahata | PCIBus *bus = &s->bus; |
1240 | e822a52a | Isaku Yamahata | pci_unregister_secondary_bus(bus); |
1241 | e822a52a | Isaku Yamahata | return 0; |
1242 | e822a52a | Isaku Yamahata | } |
1243 | e822a52a | Isaku Yamahata | |
1244 | 03587182 | Gerd Hoffmann | PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
|
1245 | 03587182 | Gerd Hoffmann | pci_map_irq_fn map_irq, const char *name) |
1246 | 03587182 | Gerd Hoffmann | { |
1247 | 03587182 | Gerd Hoffmann | PCIDevice *dev; |
1248 | 03587182 | Gerd Hoffmann | PCIBridge *s; |
1249 | 03587182 | Gerd Hoffmann | |
1250 | 499cf102 | Markus Armbruster | dev = pci_create(bus, devfn, "pci-bridge");
|
1251 | 03587182 | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
|
1252 | 03587182 | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
|
1253 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1254 | 03587182 | Gerd Hoffmann | |
1255 | 03587182 | Gerd Hoffmann | s = DO_UPCAST(PCIBridge, dev, dev); |
1256 | e822a52a | Isaku Yamahata | pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name); |
1257 | 03587182 | Gerd Hoffmann | return &s->bus;
|
1258 | 80b3ada7 | pbrook | } |
1259 | 6b1b92d3 | Paul Brook | |
1260 | 81a322d4 | Gerd Hoffmann | static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1261 | 6b1b92d3 | Paul Brook | { |
1262 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev = (PCIDevice *)qdev; |
1263 | 02e2da45 | Paul Brook | PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1264 | 6b1b92d3 | Paul Brook | PCIBus *bus; |
1265 | ee995ffb | Gerd Hoffmann | int devfn, rc;
|
1266 | 6b1b92d3 | Paul Brook | |
1267 | a9f49946 | Isaku Yamahata | /* initialize cap_present for pci_is_express() and pci_config_size() */
|
1268 | a9f49946 | Isaku Yamahata | if (info->is_express) {
|
1269 | a9f49946 | Isaku Yamahata | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1270 | a9f49946 | Isaku Yamahata | } |
1271 | a9f49946 | Isaku Yamahata | |
1272 | 02e2da45 | Paul Brook | bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1273 | ee6847d1 | Gerd Hoffmann | devfn = pci_dev->devfn; |
1274 | 16eaedf2 | Gerd Hoffmann | pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1275 | fb231628 | Isaku Yamahata | info->config_read, info->config_write, |
1276 | fb231628 | Isaku Yamahata | info->header_type); |
1277 | ee995ffb | Gerd Hoffmann | rc = info->init(pci_dev); |
1278 | ee995ffb | Gerd Hoffmann | if (rc != 0) |
1279 | ee995ffb | Gerd Hoffmann | return rc;
|
1280 | ee995ffb | Gerd Hoffmann | if (qdev->hotplugged)
|
1281 | ee995ffb | Gerd Hoffmann | bus->hotplug(pci_dev, 1);
|
1282 | ee995ffb | Gerd Hoffmann | return 0; |
1283 | ee995ffb | Gerd Hoffmann | } |
1284 | ee995ffb | Gerd Hoffmann | |
1285 | ee995ffb | Gerd Hoffmann | static int pci_unplug_device(DeviceState *qdev) |
1286 | ee995ffb | Gerd Hoffmann | { |
1287 | ee995ffb | Gerd Hoffmann | PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1288 | ee995ffb | Gerd Hoffmann | |
1289 | ee995ffb | Gerd Hoffmann | dev->bus->hotplug(dev, 0);
|
1290 | ee995ffb | Gerd Hoffmann | return 0; |
1291 | 6b1b92d3 | Paul Brook | } |
1292 | 6b1b92d3 | Paul Brook | |
1293 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register(PCIDeviceInfo *info)
|
1294 | 6b1b92d3 | Paul Brook | { |
1295 | 02e2da45 | Paul Brook | info->qdev.init = pci_qdev_init; |
1296 | ee995ffb | Gerd Hoffmann | info->qdev.unplug = pci_unplug_device; |
1297 | a36a344d | Gerd Hoffmann | info->qdev.exit = pci_unregister_device; |
1298 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &pci_bus_info; |
1299 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
1300 | 6b1b92d3 | Paul Brook | } |
1301 | 6b1b92d3 | Paul Brook | |
1302 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register_many(PCIDeviceInfo *info)
|
1303 | 0aab0d3a | Gerd Hoffmann | { |
1304 | 0aab0d3a | Gerd Hoffmann | while (info->qdev.name) {
|
1305 | 0aab0d3a | Gerd Hoffmann | pci_qdev_register(info); |
1306 | 0aab0d3a | Gerd Hoffmann | info++; |
1307 | 0aab0d3a | Gerd Hoffmann | } |
1308 | 0aab0d3a | Gerd Hoffmann | } |
1309 | 0aab0d3a | Gerd Hoffmann | |
1310 | 499cf102 | Markus Armbruster | PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1311 | 6b1b92d3 | Paul Brook | { |
1312 | 6b1b92d3 | Paul Brook | DeviceState *dev; |
1313 | 6b1b92d3 | Paul Brook | |
1314 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
1315 | a6307b08 | Gerd Hoffmann | qdev_prop_set_uint32(dev, "addr", devfn);
|
1316 | 71077c1c | Gerd Hoffmann | return DO_UPCAST(PCIDevice, qdev, dev);
|
1317 | 71077c1c | Gerd Hoffmann | } |
1318 | 6b1b92d3 | Paul Brook | |
1319 | 71077c1c | Gerd Hoffmann | PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1320 | 71077c1c | Gerd Hoffmann | { |
1321 | 499cf102 | Markus Armbruster | PCIDevice *dev = pci_create(bus, devfn, name); |
1322 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1323 | 71077c1c | Gerd Hoffmann | return dev;
|
1324 | 6b1b92d3 | Paul Brook | } |
1325 | 6f4cbd39 | Michael S. Tsirkin | |
1326 | 6f4cbd39 | Michael S. Tsirkin | static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1327 | 6f4cbd39 | Michael S. Tsirkin | { |
1328 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pdev);
|
1329 | 6f4cbd39 | Michael S. Tsirkin | int offset = PCI_CONFIG_HEADER_SIZE;
|
1330 | 6f4cbd39 | Michael S. Tsirkin | int i;
|
1331 | a9f49946 | Isaku Yamahata | for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1332 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->used[i])
|
1333 | 6f4cbd39 | Michael S. Tsirkin | offset = i + 1;
|
1334 | 6f4cbd39 | Michael S. Tsirkin | else if (i - offset + 1 == size) |
1335 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1336 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1337 | 6f4cbd39 | Michael S. Tsirkin | } |
1338 | 6f4cbd39 | Michael S. Tsirkin | |
1339 | 6f4cbd39 | Michael S. Tsirkin | static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1340 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *prev_p) |
1341 | 6f4cbd39 | Michael S. Tsirkin | { |
1342 | 6f4cbd39 | Michael S. Tsirkin | uint8_t next, prev; |
1343 | 6f4cbd39 | Michael S. Tsirkin | |
1344 | 6f4cbd39 | Michael S. Tsirkin | if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1345 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1346 | 6f4cbd39 | Michael S. Tsirkin | |
1347 | 6f4cbd39 | Michael S. Tsirkin | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1348 | 6f4cbd39 | Michael S. Tsirkin | prev = next + PCI_CAP_LIST_NEXT) |
1349 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1350 | 6f4cbd39 | Michael S. Tsirkin | break;
|
1351 | 6f4cbd39 | Michael S. Tsirkin | |
1352 | 6f4cbd39 | Michael S. Tsirkin | if (prev_p)
|
1353 | 6f4cbd39 | Michael S. Tsirkin | *prev_p = prev; |
1354 | 6f4cbd39 | Michael S. Tsirkin | return next;
|
1355 | 6f4cbd39 | Michael S. Tsirkin | } |
1356 | 6f4cbd39 | Michael S. Tsirkin | |
1357 | 6f4cbd39 | Michael S. Tsirkin | /* Reserve space and add capability to the linked list in pci config space */
|
1358 | 6f4cbd39 | Michael S. Tsirkin | int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1359 | 6f4cbd39 | Michael S. Tsirkin | { |
1360 | 6f4cbd39 | Michael S. Tsirkin | uint8_t offset = pci_find_space(pdev, size); |
1361 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *config = pdev->config + offset; |
1362 | 6f4cbd39 | Michael S. Tsirkin | if (!offset)
|
1363 | 6f4cbd39 | Michael S. Tsirkin | return -ENOSPC;
|
1364 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_ID] = cap_id; |
1365 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1366 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_CAPABILITY_LIST] = offset; |
1367 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1368 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xFF, size);
|
1369 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability read-only by default */
|
1370 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0, size);
|
1371 | bd4b65ee | Michael S. Tsirkin | /* Check capability by default */
|
1372 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0xFF, size);
|
1373 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1374 | 6f4cbd39 | Michael S. Tsirkin | } |
1375 | 6f4cbd39 | Michael S. Tsirkin | |
1376 | 6f4cbd39 | Michael S. Tsirkin | /* Unlink capability from the pci config space. */
|
1377 | 6f4cbd39 | Michael S. Tsirkin | void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1378 | 6f4cbd39 | Michael S. Tsirkin | { |
1379 | 6f4cbd39 | Michael S. Tsirkin | uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1380 | 6f4cbd39 | Michael S. Tsirkin | if (!offset)
|
1381 | 6f4cbd39 | Michael S. Tsirkin | return;
|
1382 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1383 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability writeable again */
|
1384 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0xff, size);
|
1385 | bd4b65ee | Michael S. Tsirkin | /* Clear cmask as device-specific registers can't be checked */
|
1386 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0, size);
|
1387 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0, size);
|
1388 | 6f4cbd39 | Michael S. Tsirkin | |
1389 | 6f4cbd39 | Michael S. Tsirkin | if (!pdev->config[PCI_CAPABILITY_LIST])
|
1390 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1391 | 6f4cbd39 | Michael S. Tsirkin | } |
1392 | 6f4cbd39 | Michael S. Tsirkin | |
1393 | 6f4cbd39 | Michael S. Tsirkin | /* Reserve space for capability at a known offset (to call after load). */
|
1394 | 6f4cbd39 | Michael S. Tsirkin | void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1395 | 6f4cbd39 | Michael S. Tsirkin | { |
1396 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xff, size);
|
1397 | 6f4cbd39 | Michael S. Tsirkin | } |
1398 | 6f4cbd39 | Michael S. Tsirkin | |
1399 | 6f4cbd39 | Michael S. Tsirkin | uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1400 | 6f4cbd39 | Michael S. Tsirkin | { |
1401 | 6f4cbd39 | Michael S. Tsirkin | return pci_find_capability_list(pdev, cap_id, NULL); |
1402 | 6f4cbd39 | Michael S. Tsirkin | } |
1403 | 10c4c98a | Gerd Hoffmann | |
1404 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1405 | 10c4c98a | Gerd Hoffmann | { |
1406 | 10c4c98a | Gerd Hoffmann | PCIDevice *d = (PCIDevice *)dev; |
1407 | 10c4c98a | Gerd Hoffmann | const pci_class_desc *desc;
|
1408 | 10c4c98a | Gerd Hoffmann | char ctxt[64]; |
1409 | 10c4c98a | Gerd Hoffmann | PCIIORegion *r; |
1410 | 10c4c98a | Gerd Hoffmann | int i, class;
|
1411 | 10c4c98a | Gerd Hoffmann | |
1412 | b0ff8eb2 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1413 | 10c4c98a | Gerd Hoffmann | desc = pci_class_descriptions; |
1414 | 10c4c98a | Gerd Hoffmann | while (desc->desc && class != desc->class)
|
1415 | 10c4c98a | Gerd Hoffmann | desc++; |
1416 | 10c4c98a | Gerd Hoffmann | if (desc->desc) {
|
1417 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1418 | 10c4c98a | Gerd Hoffmann | } else {
|
1419 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1420 | 10c4c98a | Gerd Hoffmann | } |
1421 | 10c4c98a | Gerd Hoffmann | |
1422 | 10c4c98a | Gerd Hoffmann | monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1423 | 10c4c98a | Gerd Hoffmann | "pci id %04x:%04x (sub %04x:%04x)\n",
|
1424 | 10c4c98a | Gerd Hoffmann | indent, "", ctxt,
|
1425 | e822a52a | Isaku Yamahata | d->config[PCI_SECONDARY_BUS], |
1426 | e822a52a | Isaku Yamahata | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1427 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_VENDOR_ID), |
1428 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_DEVICE_ID), |
1429 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1430 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1431 | 10c4c98a | Gerd Hoffmann | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1432 | 10c4c98a | Gerd Hoffmann | r = &d->io_regions[i]; |
1433 | 10c4c98a | Gerd Hoffmann | if (!r->size)
|
1434 | 10c4c98a | Gerd Hoffmann | continue;
|
1435 | 89e8b13c | Isaku Yamahata | monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1436 | 89e8b13c | Isaku Yamahata | " [0x%"FMT_PCIBUS"]\n", |
1437 | 89e8b13c | Isaku Yamahata | indent, "",
|
1438 | 0392a017 | Isaku Yamahata | i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1439 | 10c4c98a | Gerd Hoffmann | r->addr, r->addr + r->size - 1);
|
1440 | 10c4c98a | Gerd Hoffmann | } |
1441 | 10c4c98a | Gerd Hoffmann | } |
1442 | 03587182 | Gerd Hoffmann | |
1443 | 03587182 | Gerd Hoffmann | static PCIDeviceInfo bridge_info = {
|
1444 | 03587182 | Gerd Hoffmann | .qdev.name = "pci-bridge",
|
1445 | 03587182 | Gerd Hoffmann | .qdev.size = sizeof(PCIBridge),
|
1446 | 03587182 | Gerd Hoffmann | .init = pci_bridge_initfn, |
1447 | e822a52a | Isaku Yamahata | .exit = pci_bridge_exitfn, |
1448 | 03587182 | Gerd Hoffmann | .config_write = pci_bridge_write_config, |
1449 | 03587182 | Gerd Hoffmann | .qdev.props = (Property[]) { |
1450 | 03587182 | Gerd Hoffmann | DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0), |
1451 | 03587182 | Gerd Hoffmann | DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0), |
1452 | 03587182 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
1453 | 03587182 | Gerd Hoffmann | } |
1454 | 03587182 | Gerd Hoffmann | }; |
1455 | 03587182 | Gerd Hoffmann | |
1456 | 03587182 | Gerd Hoffmann | static void pci_register_devices(void) |
1457 | 03587182 | Gerd Hoffmann | { |
1458 | 03587182 | Gerd Hoffmann | pci_qdev_register(&bridge_info); |
1459 | 03587182 | Gerd Hoffmann | } |
1460 | 03587182 | Gerd Hoffmann | |
1461 | 03587182 | Gerd Hoffmann | device_init(pci_register_devices) |