root / hw / pci.c @ 010ec629
History | View | Annotate | Download (58.9 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 | 376253ec | aliguori | #include "monitor.h" |
27 | 87ecb68b | pbrook | #include "net.h" |
28 | 880345c4 | aliguori | #include "sysemu.h" |
29 | c2039bd0 | Anthony Liguori | #include "loader.h" |
30 | 163c8a59 | Luiz Capitulino | #include "qemu-objects.h" |
31 | bf1b0071 | Blue Swirl | #include "range.h" |
32 | 69b91039 | bellard | |
33 | 69b91039 | bellard | //#define DEBUG_PCI
|
34 | d8d2e079 | Isaku Yamahata | #ifdef DEBUG_PCI
|
35 | 2e49d64a | Isaku Yamahata | # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
36 | d8d2e079 | Isaku Yamahata | #else
|
37 | d8d2e079 | Isaku Yamahata | # define PCI_DPRINTF(format, ...) do { } while (0) |
38 | d8d2e079 | Isaku Yamahata | #endif
|
39 | 69b91039 | bellard | |
40 | 30468f78 | bellard | struct PCIBus {
|
41 | 02e2da45 | Paul Brook | BusState qbus; |
42 | 30468f78 | bellard | int devfn_min;
|
43 | 502a5395 | pbrook | pci_set_irq_fn set_irq; |
44 | d2b59317 | pbrook | pci_map_irq_fn map_irq; |
45 | ee995ffb | Gerd Hoffmann | pci_hotplug_fn hotplug; |
46 | 87c30546 | Isaku Yamahata | DeviceState *hotplug_qdev; |
47 | 5d4e84c8 | Juan Quintela | void *irq_opaque;
|
48 | 30468f78 | bellard | PCIDevice *devices[256];
|
49 | 80b3ada7 | pbrook | PCIDevice *parent_dev; |
50 | 2e01c8cf | Blue Swirl | target_phys_addr_t mem_base; |
51 | e822a52a | Isaku Yamahata | |
52 | e822a52a | Isaku Yamahata | QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
|
53 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
|
54 | e822a52a | Isaku Yamahata | |
55 | d2b59317 | pbrook | /* The bus IRQ state is the logical OR of the connected devices.
|
56 | d2b59317 | pbrook | Keep a count of the number of devices with raised IRQs. */
|
57 | 52fc1d83 | balrog | int nirq;
|
58 | 10c4c98a | Gerd Hoffmann | int *irq_count;
|
59 | 10c4c98a | Gerd Hoffmann | }; |
60 | 10c4c98a | Gerd Hoffmann | |
61 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
62 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev); |
63 | 10c4c98a | Gerd Hoffmann | |
64 | 10c4c98a | Gerd Hoffmann | static struct BusInfo pci_bus_info = { |
65 | 10c4c98a | Gerd Hoffmann | .name = "PCI",
|
66 | 10c4c98a | Gerd Hoffmann | .size = sizeof(PCIBus),
|
67 | 10c4c98a | Gerd Hoffmann | .print_dev = pcibus_dev_print, |
68 | 4f43c1ff | Alex Williamson | .get_dev_path = pcibus_get_dev_path, |
69 | ee6847d1 | Gerd Hoffmann | .props = (Property[]) { |
70 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
71 | 8c52c8f3 | Gerd Hoffmann | DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
|
72 | 88169ddf | Gerd Hoffmann | DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), |
73 | 49823868 | Isaku Yamahata | DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
|
74 | 49823868 | Isaku Yamahata | QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
|
75 | 54586bd1 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST() |
76 | ee6847d1 | Gerd Hoffmann | } |
77 | 30468f78 | bellard | }; |
78 | 69b91039 | bellard | |
79 | 1941d19c | bellard | static void pci_update_mappings(PCIDevice *d); |
80 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level); |
81 | 8c52c8f3 | Gerd Hoffmann | static int pci_add_option_rom(PCIDevice *pdev); |
82 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev); |
83 | 1941d19c | bellard | |
84 | d350d97d | aliguori | static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
85 | d350d97d | aliguori | static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
86 | e822a52a | Isaku Yamahata | |
87 | e822a52a | Isaku Yamahata | struct PCIHostBus {
|
88 | e822a52a | Isaku Yamahata | int domain;
|
89 | e822a52a | Isaku Yamahata | struct PCIBus *bus;
|
90 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIHostBus) next; |
91 | e822a52a | Isaku Yamahata | }; |
92 | e822a52a | Isaku Yamahata | static QLIST_HEAD(, PCIHostBus) host_buses;
|
93 | 30468f78 | bellard | |
94 | 2d1e9f96 | Juan Quintela | static const VMStateDescription vmstate_pcibus = { |
95 | 2d1e9f96 | Juan Quintela | .name = "PCIBUS",
|
96 | 2d1e9f96 | Juan Quintela | .version_id = 1,
|
97 | 2d1e9f96 | Juan Quintela | .minimum_version_id = 1,
|
98 | 2d1e9f96 | Juan Quintela | .minimum_version_id_old = 1,
|
99 | 2d1e9f96 | Juan Quintela | .fields = (VMStateField []) { |
100 | 2d1e9f96 | Juan Quintela | VMSTATE_INT32_EQUAL(nirq, PCIBus), |
101 | c7bde572 | Juan Quintela | VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
102 | 2d1e9f96 | Juan Quintela | VMSTATE_END_OF_LIST() |
103 | 52fc1d83 | balrog | } |
104 | 2d1e9f96 | Juan Quintela | }; |
105 | 52fc1d83 | balrog | |
106 | b3b11697 | Isaku Yamahata | static int pci_bar(PCIDevice *d, int reg) |
107 | 5330de09 | Michael S. Tsirkin | { |
108 | b3b11697 | Isaku Yamahata | uint8_t type; |
109 | b3b11697 | Isaku Yamahata | |
110 | b3b11697 | Isaku Yamahata | if (reg != PCI_ROM_SLOT)
|
111 | b3b11697 | Isaku Yamahata | return PCI_BASE_ADDRESS_0 + reg * 4; |
112 | b3b11697 | Isaku Yamahata | |
113 | b3b11697 | Isaku Yamahata | type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
114 | b3b11697 | Isaku Yamahata | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
115 | 5330de09 | Michael S. Tsirkin | } |
116 | 5330de09 | Michael S. Tsirkin | |
117 | d036bb21 | Michael S. Tsirkin | static inline int pci_irq_state(PCIDevice *d, int irq_num) |
118 | d036bb21 | Michael S. Tsirkin | { |
119 | d036bb21 | Michael S. Tsirkin | return (d->irq_state >> irq_num) & 0x1; |
120 | d036bb21 | Michael S. Tsirkin | } |
121 | d036bb21 | Michael S. Tsirkin | |
122 | d036bb21 | Michael S. Tsirkin | static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) |
123 | d036bb21 | Michael S. Tsirkin | { |
124 | d036bb21 | Michael S. Tsirkin | d->irq_state &= ~(0x1 << irq_num);
|
125 | d036bb21 | Michael S. Tsirkin | d->irq_state |= level << irq_num; |
126 | d036bb21 | Michael S. Tsirkin | } |
127 | d036bb21 | Michael S. Tsirkin | |
128 | d036bb21 | Michael S. Tsirkin | static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) |
129 | d036bb21 | Michael S. Tsirkin | { |
130 | d036bb21 | Michael S. Tsirkin | PCIBus *bus; |
131 | d036bb21 | Michael S. Tsirkin | for (;;) {
|
132 | d036bb21 | Michael S. Tsirkin | bus = pci_dev->bus; |
133 | d036bb21 | Michael S. Tsirkin | irq_num = bus->map_irq(pci_dev, irq_num); |
134 | d036bb21 | Michael S. Tsirkin | if (bus->set_irq)
|
135 | d036bb21 | Michael S. Tsirkin | break;
|
136 | d036bb21 | Michael S. Tsirkin | pci_dev = bus->parent_dev; |
137 | d036bb21 | Michael S. Tsirkin | } |
138 | d036bb21 | Michael S. Tsirkin | bus->irq_count[irq_num] += change; |
139 | d036bb21 | Michael S. Tsirkin | bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
140 | d036bb21 | Michael S. Tsirkin | } |
141 | d036bb21 | Michael S. Tsirkin | |
142 | f9bf77dd | Michael S. Tsirkin | /* Update interrupt status bit in config space on interrupt
|
143 | f9bf77dd | Michael S. Tsirkin | * state change. */
|
144 | f9bf77dd | Michael S. Tsirkin | static void pci_update_irq_status(PCIDevice *dev) |
145 | f9bf77dd | Michael S. Tsirkin | { |
146 | f9bf77dd | Michael S. Tsirkin | if (dev->irq_state) {
|
147 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; |
148 | f9bf77dd | Michael S. Tsirkin | } else {
|
149 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
150 | f9bf77dd | Michael S. Tsirkin | } |
151 | f9bf77dd | Michael S. Tsirkin | } |
152 | f9bf77dd | Michael S. Tsirkin | |
153 | 5330de09 | Michael S. Tsirkin | static void pci_device_reset(PCIDevice *dev) |
154 | 5330de09 | Michael S. Tsirkin | { |
155 | c0b1905b | Michael S. Tsirkin | int r;
|
156 | c0b1905b | Michael S. Tsirkin | |
157 | d036bb21 | Michael S. Tsirkin | dev->irq_state = 0;
|
158 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(dev); |
159 | 71ebd6dc | Isaku Yamahata | /* Clear all writeable bits */
|
160 | 71ebd6dc | Isaku Yamahata | pci_set_word(dev->config + PCI_COMMAND, |
161 | 71ebd6dc | Isaku Yamahata | pci_get_word(dev->config + PCI_COMMAND) & |
162 | 71ebd6dc | Isaku Yamahata | ~pci_get_word(dev->wmask + PCI_COMMAND)); |
163 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
164 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
165 | c0b1905b | Michael S. Tsirkin | for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
166 | 71ebd6dc | Isaku Yamahata | PCIIORegion *region = &dev->io_regions[r]; |
167 | 71ebd6dc | Isaku Yamahata | if (!region->size) {
|
168 | c0b1905b | Michael S. Tsirkin | continue;
|
169 | c0b1905b | Michael S. Tsirkin | } |
170 | 71ebd6dc | Isaku Yamahata | |
171 | 71ebd6dc | Isaku Yamahata | if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
172 | 71ebd6dc | Isaku Yamahata | region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
173 | 71ebd6dc | Isaku Yamahata | pci_set_quad(dev->config + pci_bar(dev, r), region->type); |
174 | 71ebd6dc | Isaku Yamahata | } else {
|
175 | 71ebd6dc | Isaku Yamahata | pci_set_long(dev->config + pci_bar(dev, r), region->type); |
176 | 71ebd6dc | Isaku Yamahata | } |
177 | c0b1905b | Michael S. Tsirkin | } |
178 | c0b1905b | Michael S. Tsirkin | pci_update_mappings(dev); |
179 | 5330de09 | Michael S. Tsirkin | } |
180 | 5330de09 | Michael S. Tsirkin | |
181 | 6eaa6847 | Gleb Natapov | static void pci_bus_reset(void *opaque) |
182 | 6eaa6847 | Gleb Natapov | { |
183 | a60380a5 | Juan Quintela | PCIBus *bus = opaque; |
184 | 6eaa6847 | Gleb Natapov | int i;
|
185 | 6eaa6847 | Gleb Natapov | |
186 | 6eaa6847 | Gleb Natapov | for (i = 0; i < bus->nirq; i++) { |
187 | 6eaa6847 | Gleb Natapov | bus->irq_count[i] = 0;
|
188 | 6eaa6847 | Gleb Natapov | } |
189 | 5330de09 | Michael S. Tsirkin | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
190 | 5330de09 | Michael S. Tsirkin | if (bus->devices[i]) {
|
191 | 5330de09 | Michael S. Tsirkin | pci_device_reset(bus->devices[i]); |
192 | 5330de09 | Michael S. Tsirkin | } |
193 | 6eaa6847 | Gleb Natapov | } |
194 | 6eaa6847 | Gleb Natapov | } |
195 | 6eaa6847 | Gleb Natapov | |
196 | e822a52a | Isaku Yamahata | static void pci_host_bus_register(int domain, PCIBus *bus) |
197 | e822a52a | Isaku Yamahata | { |
198 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
199 | e822a52a | Isaku Yamahata | host = qemu_mallocz(sizeof(*host));
|
200 | e822a52a | Isaku Yamahata | host->domain = domain; |
201 | e822a52a | Isaku Yamahata | host->bus = bus; |
202 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&host_buses, host, next); |
203 | e822a52a | Isaku Yamahata | } |
204 | e822a52a | Isaku Yamahata | |
205 | c469e1dd | Isaku Yamahata | PCIBus *pci_find_root_bus(int domain)
|
206 | e822a52a | Isaku Yamahata | { |
207 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
208 | e822a52a | Isaku Yamahata | |
209 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
210 | e822a52a | Isaku Yamahata | if (host->domain == domain) {
|
211 | e822a52a | Isaku Yamahata | return host->bus;
|
212 | e822a52a | Isaku Yamahata | } |
213 | e822a52a | Isaku Yamahata | } |
214 | e822a52a | Isaku Yamahata | |
215 | e822a52a | Isaku Yamahata | return NULL; |
216 | e822a52a | Isaku Yamahata | } |
217 | e822a52a | Isaku Yamahata | |
218 | e075e788 | Isaku Yamahata | int pci_find_domain(const PCIBus *bus) |
219 | e075e788 | Isaku Yamahata | { |
220 | e075e788 | Isaku Yamahata | PCIDevice *d; |
221 | e075e788 | Isaku Yamahata | struct PCIHostBus *host;
|
222 | e075e788 | Isaku Yamahata | |
223 | e075e788 | Isaku Yamahata | /* obtain root bus */
|
224 | e075e788 | Isaku Yamahata | while ((d = bus->parent_dev) != NULL) { |
225 | e075e788 | Isaku Yamahata | bus = d->bus; |
226 | e075e788 | Isaku Yamahata | } |
227 | e075e788 | Isaku Yamahata | |
228 | e075e788 | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
229 | e075e788 | Isaku Yamahata | if (host->bus == bus) {
|
230 | e075e788 | Isaku Yamahata | return host->domain;
|
231 | e075e788 | Isaku Yamahata | } |
232 | e075e788 | Isaku Yamahata | } |
233 | e075e788 | Isaku Yamahata | |
234 | e075e788 | Isaku Yamahata | abort(); /* should not be reached */
|
235 | e075e788 | Isaku Yamahata | return -1; |
236 | e075e788 | Isaku Yamahata | } |
237 | e075e788 | Isaku Yamahata | |
238 | 21eea4b3 | Gerd Hoffmann | void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
239 | 21eea4b3 | Gerd Hoffmann | const char *name, int devfn_min) |
240 | 30468f78 | bellard | { |
241 | 21eea4b3 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name); |
242 | 6fa84913 | Isaku Yamahata | assert(PCI_FUNC(devfn_min) == 0);
|
243 | 502a5395 | pbrook | bus->devfn_min = devfn_min; |
244 | e822a52a | Isaku Yamahata | |
245 | e822a52a | Isaku Yamahata | /* host bridge */
|
246 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
247 | e822a52a | Isaku Yamahata | pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
248 | e822a52a | Isaku Yamahata | |
249 | 0be71e32 | Alex Williamson | vmstate_register(NULL, -1, &vmstate_pcibus, bus); |
250 | a08d4367 | Jan Kiszka | qemu_register_reset(pci_bus_reset, bus); |
251 | 21eea4b3 | Gerd Hoffmann | } |
252 | 21eea4b3 | Gerd Hoffmann | |
253 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min) |
254 | 21eea4b3 | Gerd Hoffmann | { |
255 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
256 | 21eea4b3 | Gerd Hoffmann | |
257 | 21eea4b3 | Gerd Hoffmann | bus = qemu_mallocz(sizeof(*bus));
|
258 | 21eea4b3 | Gerd Hoffmann | bus->qbus.qdev_allocated = 1;
|
259 | 21eea4b3 | Gerd Hoffmann | pci_bus_new_inplace(bus, parent, name, devfn_min); |
260 | 21eea4b3 | Gerd Hoffmann | return bus;
|
261 | 21eea4b3 | Gerd Hoffmann | } |
262 | 21eea4b3 | Gerd Hoffmann | |
263 | 21eea4b3 | Gerd Hoffmann | void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
264 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int nirq) |
265 | 21eea4b3 | Gerd Hoffmann | { |
266 | 21eea4b3 | Gerd Hoffmann | bus->set_irq = set_irq; |
267 | 21eea4b3 | Gerd Hoffmann | bus->map_irq = map_irq; |
268 | 21eea4b3 | Gerd Hoffmann | bus->irq_opaque = irq_opaque; |
269 | 21eea4b3 | Gerd Hoffmann | bus->nirq = nirq; |
270 | 21eea4b3 | Gerd Hoffmann | bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0])); |
271 | 21eea4b3 | Gerd Hoffmann | } |
272 | 21eea4b3 | Gerd Hoffmann | |
273 | 87c30546 | Isaku Yamahata | void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
|
274 | ee995ffb | Gerd Hoffmann | { |
275 | ee995ffb | Gerd Hoffmann | bus->qbus.allow_hotplug = 1;
|
276 | ee995ffb | Gerd Hoffmann | bus->hotplug = hotplug; |
277 | 87c30546 | Isaku Yamahata | bus->hotplug_qdev = qdev; |
278 | ee995ffb | Gerd Hoffmann | } |
279 | ee995ffb | Gerd Hoffmann | |
280 | 2e01c8cf | Blue Swirl | void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
|
281 | 2e01c8cf | Blue Swirl | { |
282 | 2e01c8cf | Blue Swirl | bus->mem_base = base; |
283 | 2e01c8cf | Blue Swirl | } |
284 | 2e01c8cf | Blue Swirl | |
285 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
286 | 21eea4b3 | Gerd Hoffmann | pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
287 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int devfn_min, int nirq) |
288 | 21eea4b3 | Gerd Hoffmann | { |
289 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
290 | 21eea4b3 | Gerd Hoffmann | |
291 | 21eea4b3 | Gerd Hoffmann | bus = pci_bus_new(parent, name, devfn_min); |
292 | 21eea4b3 | Gerd Hoffmann | pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
293 | 30468f78 | bellard | return bus;
|
294 | 30468f78 | bellard | } |
295 | 69b91039 | bellard | |
296 | e822a52a | Isaku Yamahata | static void pci_register_secondary_bus(PCIBus *parent, |
297 | e822a52a | Isaku Yamahata | PCIBus *bus, |
298 | 03587182 | Gerd Hoffmann | PCIDevice *dev, |
299 | 03587182 | Gerd Hoffmann | pci_map_irq_fn map_irq, |
300 | 03587182 | Gerd Hoffmann | const char *name) |
301 | 80b3ada7 | pbrook | { |
302 | 03587182 | Gerd Hoffmann | qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name); |
303 | 80b3ada7 | pbrook | bus->map_irq = map_irq; |
304 | 80b3ada7 | pbrook | bus->parent_dev = dev; |
305 | e822a52a | Isaku Yamahata | |
306 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
307 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&parent->child, bus, sibling); |
308 | e822a52a | Isaku Yamahata | } |
309 | e822a52a | Isaku Yamahata | |
310 | e822a52a | Isaku Yamahata | static void pci_unregister_secondary_bus(PCIBus *bus) |
311 | e822a52a | Isaku Yamahata | { |
312 | e822a52a | Isaku Yamahata | assert(QLIST_EMPTY(&bus->child)); |
313 | e822a52a | Isaku Yamahata | QLIST_REMOVE(bus, sibling); |
314 | 80b3ada7 | pbrook | } |
315 | 80b3ada7 | pbrook | |
316 | 502a5395 | pbrook | int pci_bus_num(PCIBus *s)
|
317 | 502a5395 | pbrook | { |
318 | e94ff650 | Isaku Yamahata | if (!s->parent_dev)
|
319 | e94ff650 | Isaku Yamahata | return 0; /* pci host bridge */ |
320 | e94ff650 | Isaku Yamahata | return s->parent_dev->config[PCI_SECONDARY_BUS];
|
321 | 502a5395 | pbrook | } |
322 | 502a5395 | pbrook | |
323 | 73534f2f | Juan Quintela | static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
324 | 30ca2aab | bellard | { |
325 | 73534f2f | Juan Quintela | PCIDevice *s = container_of(pv, PCIDevice, config); |
326 | a9f49946 | Isaku Yamahata | uint8_t *config; |
327 | 52fc1d83 | balrog | int i;
|
328 | 52fc1d83 | balrog | |
329 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(s)); |
330 | a9f49946 | Isaku Yamahata | config = qemu_malloc(size); |
331 | a9f49946 | Isaku Yamahata | |
332 | a9f49946 | Isaku Yamahata | qemu_get_buffer(f, config, size); |
333 | a9f49946 | Isaku Yamahata | for (i = 0; i < size; ++i) { |
334 | a9f49946 | Isaku Yamahata | if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
|
335 | a9f49946 | Isaku Yamahata | qemu_free(config); |
336 | bd4b65ee | Michael S. Tsirkin | return -EINVAL;
|
337 | a9f49946 | Isaku Yamahata | } |
338 | a9f49946 | Isaku Yamahata | } |
339 | a9f49946 | Isaku Yamahata | memcpy(s->config, config, size); |
340 | bd4b65ee | Michael S. Tsirkin | |
341 | 1941d19c | bellard | pci_update_mappings(s); |
342 | 52fc1d83 | balrog | |
343 | a9f49946 | Isaku Yamahata | qemu_free(config); |
344 | 30ca2aab | bellard | return 0; |
345 | 30ca2aab | bellard | } |
346 | 30ca2aab | bellard | |
347 | 73534f2f | Juan Quintela | /* just put buffer */
|
348 | 84e2e3eb | Juan Quintela | static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
349 | 73534f2f | Juan Quintela | { |
350 | dbe73d7f | Juan Quintela | const uint8_t **v = pv;
|
351 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
352 | dbe73d7f | Juan Quintela | qemu_put_buffer(f, *v, size); |
353 | 73534f2f | Juan Quintela | } |
354 | 73534f2f | Juan Quintela | |
355 | 73534f2f | Juan Quintela | static VMStateInfo vmstate_info_pci_config = {
|
356 | 73534f2f | Juan Quintela | .name = "pci config",
|
357 | 73534f2f | Juan Quintela | .get = get_pci_config_device, |
358 | 73534f2f | Juan Quintela | .put = put_pci_config_device, |
359 | 73534f2f | Juan Quintela | }; |
360 | 73534f2f | Juan Quintela | |
361 | d036bb21 | Michael S. Tsirkin | static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
362 | d036bb21 | Michael S. Tsirkin | { |
363 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
364 | d036bb21 | Michael S. Tsirkin | uint32_t irq_state[PCI_NUM_PINS]; |
365 | d036bb21 | Michael S. Tsirkin | int i;
|
366 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
367 | d036bb21 | Michael S. Tsirkin | irq_state[i] = qemu_get_be32(f); |
368 | d036bb21 | Michael S. Tsirkin | if (irq_state[i] != 0x1 && irq_state[i] != 0) { |
369 | d036bb21 | Michael S. Tsirkin | fprintf(stderr, "irq state %d: must be 0 or 1.\n",
|
370 | d036bb21 | Michael S. Tsirkin | irq_state[i]); |
371 | d036bb21 | Michael S. Tsirkin | return -EINVAL;
|
372 | d036bb21 | Michael S. Tsirkin | } |
373 | d036bb21 | Michael S. Tsirkin | } |
374 | d036bb21 | Michael S. Tsirkin | |
375 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
376 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(s, i, irq_state[i]); |
377 | d036bb21 | Michael S. Tsirkin | } |
378 | d036bb21 | Michael S. Tsirkin | |
379 | d036bb21 | Michael S. Tsirkin | return 0; |
380 | d036bb21 | Michael S. Tsirkin | } |
381 | d036bb21 | Michael S. Tsirkin | |
382 | d036bb21 | Michael S. Tsirkin | static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
383 | d036bb21 | Michael S. Tsirkin | { |
384 | d036bb21 | Michael S. Tsirkin | int i;
|
385 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
386 | d036bb21 | Michael S. Tsirkin | |
387 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
388 | d036bb21 | Michael S. Tsirkin | qemu_put_be32(f, pci_irq_state(s, i)); |
389 | d036bb21 | Michael S. Tsirkin | } |
390 | d036bb21 | Michael S. Tsirkin | } |
391 | d036bb21 | Michael S. Tsirkin | |
392 | d036bb21 | Michael S. Tsirkin | static VMStateInfo vmstate_info_pci_irq_state = {
|
393 | d036bb21 | Michael S. Tsirkin | .name = "pci irq state",
|
394 | d036bb21 | Michael S. Tsirkin | .get = get_pci_irq_state, |
395 | d036bb21 | Michael S. Tsirkin | .put = put_pci_irq_state, |
396 | d036bb21 | Michael S. Tsirkin | }; |
397 | d036bb21 | Michael S. Tsirkin | |
398 | 73534f2f | Juan Quintela | const VMStateDescription vmstate_pci_device = {
|
399 | 73534f2f | Juan Quintela | .name = "PCIDevice",
|
400 | 73534f2f | Juan Quintela | .version_id = 2,
|
401 | 73534f2f | Juan Quintela | .minimum_version_id = 1,
|
402 | 73534f2f | Juan Quintela | .minimum_version_id_old = 1,
|
403 | 73534f2f | Juan Quintela | .fields = (VMStateField []) { |
404 | 73534f2f | Juan Quintela | VMSTATE_INT32_LE(version_id, PCIDevice), |
405 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
406 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
407 | a9f49946 | Isaku Yamahata | PCI_CONFIG_SPACE_SIZE), |
408 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
409 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
410 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
411 | a9f49946 | Isaku Yamahata | VMSTATE_END_OF_LIST() |
412 | a9f49946 | Isaku Yamahata | } |
413 | a9f49946 | Isaku Yamahata | }; |
414 | a9f49946 | Isaku Yamahata | |
415 | a9f49946 | Isaku Yamahata | const VMStateDescription vmstate_pcie_device = {
|
416 | a9f49946 | Isaku Yamahata | .name = "PCIDevice",
|
417 | a9f49946 | Isaku Yamahata | .version_id = 2,
|
418 | a9f49946 | Isaku Yamahata | .minimum_version_id = 1,
|
419 | a9f49946 | Isaku Yamahata | .minimum_version_id_old = 1,
|
420 | a9f49946 | Isaku Yamahata | .fields = (VMStateField []) { |
421 | a9f49946 | Isaku Yamahata | VMSTATE_INT32_LE(version_id, PCIDevice), |
422 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
423 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
424 | a9f49946 | Isaku Yamahata | PCIE_CONFIG_SPACE_SIZE), |
425 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
426 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
427 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
428 | 73534f2f | Juan Quintela | VMSTATE_END_OF_LIST() |
429 | 73534f2f | Juan Quintela | } |
430 | 73534f2f | Juan Quintela | }; |
431 | 73534f2f | Juan Quintela | |
432 | a9f49946 | Isaku Yamahata | static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
433 | a9f49946 | Isaku Yamahata | { |
434 | a9f49946 | Isaku Yamahata | return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
435 | a9f49946 | Isaku Yamahata | } |
436 | a9f49946 | Isaku Yamahata | |
437 | 73534f2f | Juan Quintela | void pci_device_save(PCIDevice *s, QEMUFile *f)
|
438 | 73534f2f | Juan Quintela | { |
439 | f9bf77dd | Michael S. Tsirkin | /* Clear interrupt status bit: it is implicit
|
440 | f9bf77dd | Michael S. Tsirkin | * in irq_state which we are saving.
|
441 | f9bf77dd | Michael S. Tsirkin | * This makes us compatible with old devices
|
442 | f9bf77dd | Michael S. Tsirkin | * which never set or clear this bit. */
|
443 | f9bf77dd | Michael S. Tsirkin | s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
444 | a9f49946 | Isaku Yamahata | vmstate_save_state(f, pci_get_vmstate(s), s); |
445 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
446 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
447 | 73534f2f | Juan Quintela | } |
448 | 73534f2f | Juan Quintela | |
449 | 73534f2f | Juan Quintela | int pci_device_load(PCIDevice *s, QEMUFile *f)
|
450 | 73534f2f | Juan Quintela | { |
451 | f9bf77dd | Michael S. Tsirkin | int ret;
|
452 | f9bf77dd | Michael S. Tsirkin | ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id); |
453 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
454 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
455 | f9bf77dd | Michael S. Tsirkin | return ret;
|
456 | 73534f2f | Juan Quintela | } |
457 | 73534f2f | Juan Quintela | |
458 | 5e434f4e | Isaku Yamahata | static void pci_set_default_subsystem_id(PCIDevice *pci_dev) |
459 | d350d97d | aliguori | { |
460 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
461 | 5e434f4e | Isaku Yamahata | pci_default_sub_vendor_id); |
462 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
463 | 5e434f4e | Isaku Yamahata | pci_default_sub_device_id); |
464 | d350d97d | aliguori | } |
465 | d350d97d | aliguori | |
466 | 880345c4 | aliguori | /*
|
467 | 880345c4 | aliguori | * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
|
468 | 880345c4 | aliguori | */
|
469 | 880345c4 | aliguori | static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp) |
470 | 880345c4 | aliguori | { |
471 | 880345c4 | aliguori | const char *p; |
472 | 880345c4 | aliguori | char *e;
|
473 | 880345c4 | aliguori | unsigned long val; |
474 | 880345c4 | aliguori | unsigned long dom = 0, bus = 0; |
475 | 880345c4 | aliguori | unsigned slot = 0; |
476 | 880345c4 | aliguori | |
477 | 880345c4 | aliguori | p = addr; |
478 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
479 | 880345c4 | aliguori | if (e == p)
|
480 | 880345c4 | aliguori | return -1; |
481 | 880345c4 | aliguori | if (*e == ':') { |
482 | 880345c4 | aliguori | bus = val; |
483 | 880345c4 | aliguori | p = e + 1;
|
484 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
485 | 880345c4 | aliguori | if (e == p)
|
486 | 880345c4 | aliguori | return -1; |
487 | 880345c4 | aliguori | if (*e == ':') { |
488 | 880345c4 | aliguori | dom = bus; |
489 | 880345c4 | aliguori | bus = val; |
490 | 880345c4 | aliguori | p = e + 1;
|
491 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
492 | 880345c4 | aliguori | if (e == p)
|
493 | 880345c4 | aliguori | return -1; |
494 | 880345c4 | aliguori | } |
495 | 880345c4 | aliguori | } |
496 | 880345c4 | aliguori | |
497 | 880345c4 | aliguori | if (dom > 0xffff || bus > 0xff || val > 0x1f) |
498 | 880345c4 | aliguori | return -1; |
499 | 880345c4 | aliguori | |
500 | 880345c4 | aliguori | slot = val; |
501 | 880345c4 | aliguori | |
502 | 880345c4 | aliguori | if (*e)
|
503 | 880345c4 | aliguori | return -1; |
504 | 880345c4 | aliguori | |
505 | 880345c4 | aliguori | /* Note: QEMU doesn't implement domains other than 0 */
|
506 | c469e1dd | Isaku Yamahata | if (!pci_find_bus(pci_find_root_bus(dom), bus))
|
507 | 880345c4 | aliguori | return -1; |
508 | 880345c4 | aliguori | |
509 | 880345c4 | aliguori | *domp = dom; |
510 | 880345c4 | aliguori | *busp = bus; |
511 | 880345c4 | aliguori | *slotp = slot; |
512 | 880345c4 | aliguori | return 0; |
513 | 880345c4 | aliguori | } |
514 | 880345c4 | aliguori | |
515 | e9283f8b | Jan Kiszka | int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp, |
516 | e9283f8b | Jan Kiszka | unsigned *slotp)
|
517 | 880345c4 | aliguori | { |
518 | e9283f8b | Jan Kiszka | /* strip legacy tag */
|
519 | e9283f8b | Jan Kiszka | if (!strncmp(addr, "pci_addr=", 9)) { |
520 | e9283f8b | Jan Kiszka | addr += 9;
|
521 | e9283f8b | Jan Kiszka | } |
522 | e9283f8b | Jan Kiszka | if (pci_parse_devaddr(addr, domp, busp, slotp)) {
|
523 | e9283f8b | Jan Kiszka | monitor_printf(mon, "Invalid pci address\n");
|
524 | 880345c4 | aliguori | return -1; |
525 | e9283f8b | Jan Kiszka | } |
526 | e9283f8b | Jan Kiszka | return 0; |
527 | 880345c4 | aliguori | } |
528 | 880345c4 | aliguori | |
529 | 49bd1458 | Markus Armbruster | PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr) |
530 | 5607c388 | Markus Armbruster | { |
531 | 5607c388 | Markus Armbruster | int dom, bus;
|
532 | 5607c388 | Markus Armbruster | unsigned slot;
|
533 | 5607c388 | Markus Armbruster | |
534 | 5607c388 | Markus Armbruster | if (!devaddr) {
|
535 | 5607c388 | Markus Armbruster | *devfnp = -1;
|
536 | c469e1dd | Isaku Yamahata | return pci_find_bus(pci_find_root_bus(0), 0); |
537 | 5607c388 | Markus Armbruster | } |
538 | 5607c388 | Markus Armbruster | |
539 | 5607c388 | Markus Armbruster | if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) { |
540 | 5607c388 | Markus Armbruster | return NULL; |
541 | 5607c388 | Markus Armbruster | } |
542 | 5607c388 | Markus Armbruster | |
543 | 5607c388 | Markus Armbruster | *devfnp = slot << 3;
|
544 | e075e788 | Isaku Yamahata | return pci_find_bus(pci_find_root_bus(dom), bus);
|
545 | 5607c388 | Markus Armbruster | } |
546 | 5607c388 | Markus Armbruster | |
547 | bd4b65ee | Michael S. Tsirkin | static void pci_init_cmask(PCIDevice *dev) |
548 | bd4b65ee | Michael S. Tsirkin | { |
549 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
550 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
551 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
552 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_REVISION_ID] = 0xff;
|
553 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CLASS_PROG] = 0xff;
|
554 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
555 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
556 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
557 | bd4b65ee | Michael S. Tsirkin | } |
558 | bd4b65ee | Michael S. Tsirkin | |
559 | b7ee1603 | Michael S. Tsirkin | static void pci_init_wmask(PCIDevice *dev) |
560 | b7ee1603 | Michael S. Tsirkin | { |
561 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(dev);
|
562 | a9f49946 | Isaku Yamahata | |
563 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
564 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
565 | 67a51b48 | Isaku Yamahata | pci_set_word(dev->wmask + PCI_COMMAND, |
566 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
567 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_INTX_DISABLE); |
568 | 3e21ffc9 | Isaku Yamahata | |
569 | 3e21ffc9 | Isaku Yamahata | memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
|
570 | 3e21ffc9 | Isaku Yamahata | config_size - PCI_CONFIG_HEADER_SIZE); |
571 | b7ee1603 | Michael S. Tsirkin | } |
572 | b7ee1603 | Michael S. Tsirkin | |
573 | fb231628 | Isaku Yamahata | static void pci_init_wmask_bridge(PCIDevice *d) |
574 | fb231628 | Isaku Yamahata | { |
575 | fb231628 | Isaku Yamahata | /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
576 | fb231628 | Isaku Yamahata | PCI_SEC_LETENCY_TIMER */
|
577 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
578 | fb231628 | Isaku Yamahata | |
579 | fb231628 | Isaku Yamahata | /* base and limit */
|
580 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
581 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
582 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_BASE, |
583 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
584 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
585 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
586 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
587 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
588 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
589 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
590 | fb231628 | Isaku Yamahata | |
591 | fb231628 | Isaku Yamahata | /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
592 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
593 | fb231628 | Isaku Yamahata | |
594 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
|
595 | fb231628 | Isaku Yamahata | } |
596 | fb231628 | Isaku Yamahata | |
597 | 6eab3de1 | Isaku Yamahata | static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) |
598 | 6eab3de1 | Isaku Yamahata | { |
599 | 6eab3de1 | Isaku Yamahata | uint8_t slot = PCI_SLOT(dev->devfn); |
600 | 6eab3de1 | Isaku Yamahata | uint8_t func; |
601 | 6eab3de1 | Isaku Yamahata | |
602 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
603 | 6eab3de1 | Isaku Yamahata | dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; |
604 | 6eab3de1 | Isaku Yamahata | } |
605 | 6eab3de1 | Isaku Yamahata | |
606 | 6eab3de1 | Isaku Yamahata | /*
|
607 | 6eab3de1 | Isaku Yamahata | * multifuction bit is interpreted in two ways as follows.
|
608 | 6eab3de1 | Isaku Yamahata | * - all functions must set the bit to 1.
|
609 | 6eab3de1 | Isaku Yamahata | * Example: Intel X53
|
610 | 6eab3de1 | Isaku Yamahata | * - function 0 must set the bit, but the rest function (> 0)
|
611 | 6eab3de1 | Isaku Yamahata | * is allowed to leave the bit to 0.
|
612 | 6eab3de1 | Isaku Yamahata | * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
|
613 | 6eab3de1 | Isaku Yamahata | *
|
614 | 6eab3de1 | Isaku Yamahata | * So OS (at least Linux) checks the bit of only function 0,
|
615 | 6eab3de1 | Isaku Yamahata | * and doesn't see the bit of function > 0.
|
616 | 6eab3de1 | Isaku Yamahata | *
|
617 | 6eab3de1 | Isaku Yamahata | * The below check allows both interpretation.
|
618 | 6eab3de1 | Isaku Yamahata | */
|
619 | 6eab3de1 | Isaku Yamahata | if (PCI_FUNC(dev->devfn)) {
|
620 | 6eab3de1 | Isaku Yamahata | PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
|
621 | 6eab3de1 | Isaku Yamahata | if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
|
622 | 6eab3de1 | Isaku Yamahata | /* function 0 should set multifunction bit */
|
623 | 6eab3de1 | Isaku Yamahata | error_report("PCI: single function device can't be populated "
|
624 | 6eab3de1 | Isaku Yamahata | "in function %x.%x", slot, PCI_FUNC(dev->devfn));
|
625 | 6eab3de1 | Isaku Yamahata | return -1; |
626 | 6eab3de1 | Isaku Yamahata | } |
627 | 6eab3de1 | Isaku Yamahata | return 0; |
628 | 6eab3de1 | Isaku Yamahata | } |
629 | 6eab3de1 | Isaku Yamahata | |
630 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
631 | 6eab3de1 | Isaku Yamahata | return 0; |
632 | 6eab3de1 | Isaku Yamahata | } |
633 | 6eab3de1 | Isaku Yamahata | /* function 0 indicates single function, so function > 0 must be NULL */
|
634 | 6eab3de1 | Isaku Yamahata | for (func = 1; func < PCI_FUNC_MAX; ++func) { |
635 | 6eab3de1 | Isaku Yamahata | if (bus->devices[PCI_DEVFN(slot, func)]) {
|
636 | 6eab3de1 | Isaku Yamahata | error_report("PCI: %x.0 indicates single function, "
|
637 | 6eab3de1 | Isaku Yamahata | "but %x.%x is already populated.",
|
638 | 6eab3de1 | Isaku Yamahata | slot, slot, func); |
639 | 6eab3de1 | Isaku Yamahata | return -1; |
640 | 6eab3de1 | Isaku Yamahata | } |
641 | 6eab3de1 | Isaku Yamahata | } |
642 | 6eab3de1 | Isaku Yamahata | return 0; |
643 | 6eab3de1 | Isaku Yamahata | } |
644 | 6eab3de1 | Isaku Yamahata | |
645 | a9f49946 | Isaku Yamahata | static void pci_config_alloc(PCIDevice *pci_dev) |
646 | a9f49946 | Isaku Yamahata | { |
647 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pci_dev);
|
648 | a9f49946 | Isaku Yamahata | |
649 | a9f49946 | Isaku Yamahata | pci_dev->config = qemu_mallocz(config_size); |
650 | a9f49946 | Isaku Yamahata | pci_dev->cmask = qemu_mallocz(config_size); |
651 | a9f49946 | Isaku Yamahata | pci_dev->wmask = qemu_mallocz(config_size); |
652 | a9f49946 | Isaku Yamahata | pci_dev->used = qemu_mallocz(config_size); |
653 | a9f49946 | Isaku Yamahata | } |
654 | a9f49946 | Isaku Yamahata | |
655 | a9f49946 | Isaku Yamahata | static void pci_config_free(PCIDevice *pci_dev) |
656 | a9f49946 | Isaku Yamahata | { |
657 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->config); |
658 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->cmask); |
659 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->wmask); |
660 | a9f49946 | Isaku Yamahata | qemu_free(pci_dev->used); |
661 | a9f49946 | Isaku Yamahata | } |
662 | a9f49946 | Isaku Yamahata | |
663 | 69b91039 | bellard | /* -1 for devfn means auto assign */
|
664 | 6b1b92d3 | Paul Brook | static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
665 | 6b1b92d3 | Paul Brook | const char *name, int devfn, |
666 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
667 | fb231628 | Isaku Yamahata | PCIConfigWriteFunc *config_write, |
668 | e327e323 | Isaku Yamahata | bool is_bridge)
|
669 | 69b91039 | bellard | { |
670 | 69b91039 | bellard | if (devfn < 0) { |
671 | b47b0706 | Isaku Yamahata | for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
|
672 | 6fa84913 | Isaku Yamahata | devfn += PCI_FUNC_MAX) { |
673 | 30468f78 | bellard | if (!bus->devices[devfn])
|
674 | 69b91039 | bellard | goto found;
|
675 | 69b91039 | bellard | } |
676 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: no slot/function available for %s, all in use", name);
|
677 | 09e3acc6 | Gerd Hoffmann | return NULL; |
678 | 69b91039 | bellard | found: ;
|
679 | 07b7d053 | Markus Armbruster | } else if (bus->devices[devfn]) { |
680 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: slot %d function %d not available for %s, in use by %s",
|
681 | 3709c1b7 | Daniel P. Berrange | PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name); |
682 | 09e3acc6 | Gerd Hoffmann | return NULL; |
683 | 69b91039 | bellard | } |
684 | 30468f78 | bellard | pci_dev->bus = bus; |
685 | 69b91039 | bellard | pci_dev->devfn = devfn; |
686 | 69b91039 | bellard | pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
687 | d036bb21 | Michael S. Tsirkin | pci_dev->irq_state = 0;
|
688 | a9f49946 | Isaku Yamahata | pci_config_alloc(pci_dev); |
689 | fb231628 | Isaku Yamahata | |
690 | e327e323 | Isaku Yamahata | if (!is_bridge) {
|
691 | fb231628 | Isaku Yamahata | pci_set_default_subsystem_id(pci_dev); |
692 | fb231628 | Isaku Yamahata | } |
693 | bd4b65ee | Michael S. Tsirkin | pci_init_cmask(pci_dev); |
694 | b7ee1603 | Michael S. Tsirkin | pci_init_wmask(pci_dev); |
695 | e327e323 | Isaku Yamahata | if (is_bridge) {
|
696 | fb231628 | Isaku Yamahata | pci_init_wmask_bridge(pci_dev); |
697 | fb231628 | Isaku Yamahata | } |
698 | 6eab3de1 | Isaku Yamahata | if (pci_init_multifunction(bus, pci_dev)) {
|
699 | 6eab3de1 | Isaku Yamahata | pci_config_free(pci_dev); |
700 | 6eab3de1 | Isaku Yamahata | return NULL; |
701 | 6eab3de1 | Isaku Yamahata | } |
702 | 0ac32c83 | bellard | |
703 | 0ac32c83 | bellard | if (!config_read)
|
704 | 0ac32c83 | bellard | config_read = pci_default_read_config; |
705 | 0ac32c83 | bellard | if (!config_write)
|
706 | 0ac32c83 | bellard | config_write = pci_default_write_config; |
707 | 69b91039 | bellard | pci_dev->config_read = config_read; |
708 | 69b91039 | bellard | pci_dev->config_write = config_write; |
709 | 30468f78 | bellard | bus->devices[devfn] = pci_dev; |
710 | e369cad7 | Isaku Yamahata | pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
711 | f16c4abf | Juan Quintela | pci_dev->version_id = 2; /* Current pci device vmstate version */ |
712 | 69b91039 | bellard | return pci_dev;
|
713 | 69b91039 | bellard | } |
714 | 69b91039 | bellard | |
715 | 925fe64a | Alex Williamson | static void do_pci_unregister_device(PCIDevice *pci_dev) |
716 | 925fe64a | Alex Williamson | { |
717 | 925fe64a | Alex Williamson | qemu_free_irqs(pci_dev->irq); |
718 | 925fe64a | Alex Williamson | pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
719 | 925fe64a | Alex Williamson | pci_config_free(pci_dev); |
720 | 925fe64a | Alex Williamson | } |
721 | 925fe64a | Alex Williamson | |
722 | 6b1b92d3 | Paul Brook | PCIDevice *pci_register_device(PCIBus *bus, const char *name, |
723 | 6b1b92d3 | Paul Brook | int instance_size, int devfn, |
724 | 6b1b92d3 | Paul Brook | PCIConfigReadFunc *config_read, |
725 | 6b1b92d3 | Paul Brook | PCIConfigWriteFunc *config_write) |
726 | 6b1b92d3 | Paul Brook | { |
727 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev; |
728 | 6b1b92d3 | Paul Brook | |
729 | 6b1b92d3 | Paul Brook | pci_dev = qemu_mallocz(instance_size); |
730 | 6b1b92d3 | Paul Brook | pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, |
731 | fb231628 | Isaku Yamahata | config_read, config_write, |
732 | fb231628 | Isaku Yamahata | PCI_HEADER_TYPE_NORMAL); |
733 | 09e3acc6 | Gerd Hoffmann | if (pci_dev == NULL) { |
734 | 09e3acc6 | Gerd Hoffmann | hw_error("PCI: can't register device\n");
|
735 | 09e3acc6 | Gerd Hoffmann | } |
736 | 6b1b92d3 | Paul Brook | return pci_dev;
|
737 | 6b1b92d3 | Paul Brook | } |
738 | 2e01c8cf | Blue Swirl | |
739 | 2e01c8cf | Blue Swirl | static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
|
740 | 2e01c8cf | Blue Swirl | target_phys_addr_t addr) |
741 | 5851e08c | aliguori | { |
742 | 2e01c8cf | Blue Swirl | return addr + bus->mem_base;
|
743 | 5851e08c | aliguori | } |
744 | 5851e08c | aliguori | |
745 | 5851e08c | aliguori | static void pci_unregister_io_regions(PCIDevice *pci_dev) |
746 | 5851e08c | aliguori | { |
747 | 5851e08c | aliguori | PCIIORegion *r; |
748 | 5851e08c | aliguori | int i;
|
749 | 5851e08c | aliguori | |
750 | 5851e08c | aliguori | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
751 | 5851e08c | aliguori | r = &pci_dev->io_regions[i]; |
752 | 182f9c8a | Isaku Yamahata | if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
753 | 5851e08c | aliguori | continue;
|
754 | 0392a017 | Isaku Yamahata | if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
|
755 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
756 | 5851e08c | aliguori | } else {
|
757 | 2e01c8cf | Blue Swirl | cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus, |
758 | 2e01c8cf | Blue Swirl | r->addr), |
759 | 2e01c8cf | Blue Swirl | r->filtered_size, |
760 | 2e01c8cf | Blue Swirl | IO_MEM_UNASSIGNED); |
761 | 5851e08c | aliguori | } |
762 | 5851e08c | aliguori | } |
763 | 5851e08c | aliguori | } |
764 | 5851e08c | aliguori | |
765 | a36a344d | Gerd Hoffmann | static int pci_unregister_device(DeviceState *dev) |
766 | 5851e08c | aliguori | { |
767 | a36a344d | Gerd Hoffmann | PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev); |
768 | e3936fa5 | Gerd Hoffmann | PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info); |
769 | 5851e08c | aliguori | int ret = 0; |
770 | 5851e08c | aliguori | |
771 | e3936fa5 | Gerd Hoffmann | if (info->exit)
|
772 | e3936fa5 | Gerd Hoffmann | ret = info->exit(pci_dev); |
773 | 5851e08c | aliguori | if (ret)
|
774 | 5851e08c | aliguori | return ret;
|
775 | 5851e08c | aliguori | |
776 | 5851e08c | aliguori | pci_unregister_io_regions(pci_dev); |
777 | 230741dc | Alex Williamson | pci_del_option_rom(pci_dev); |
778 | 925fe64a | Alex Williamson | do_pci_unregister_device(pci_dev); |
779 | 5851e08c | aliguori | return 0; |
780 | 5851e08c | aliguori | } |
781 | 5851e08c | aliguori | |
782 | 28c2c264 | Avi Kivity | void pci_register_bar(PCIDevice *pci_dev, int region_num, |
783 | 6e355d90 | Isaku Yamahata | pcibus_t size, int type,
|
784 | 69b91039 | bellard | PCIMapIORegionFunc *map_func) |
785 | 69b91039 | bellard | { |
786 | 69b91039 | bellard | PCIIORegion *r; |
787 | d7ce493a | pbrook | uint32_t addr; |
788 | 6e355d90 | Isaku Yamahata | pcibus_t wmask; |
789 | 69b91039 | bellard | |
790 | 8a8696a3 | bellard | if ((unsigned int)region_num >= PCI_NUM_REGIONS) |
791 | 69b91039 | bellard | return;
|
792 | a4c20c6a | aliguori | |
793 | a4c20c6a | aliguori | if (size & (size-1)) { |
794 | a4c20c6a | aliguori | fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
795 | 89e8b13c | Isaku Yamahata | "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
796 | a4c20c6a | aliguori | exit(1);
|
797 | a4c20c6a | aliguori | } |
798 | a4c20c6a | aliguori | |
799 | 69b91039 | bellard | r = &pci_dev->io_regions[region_num]; |
800 | 182f9c8a | Isaku Yamahata | r->addr = PCI_BAR_UNMAPPED; |
801 | 69b91039 | bellard | r->size = size; |
802 | a0c7a97e | Isaku Yamahata | r->filtered_size = size; |
803 | 69b91039 | bellard | r->type = type; |
804 | 69b91039 | bellard | r->map_func = map_func; |
805 | b7ee1603 | Michael S. Tsirkin | |
806 | b7ee1603 | Michael S. Tsirkin | wmask = ~(size - 1);
|
807 | b3b11697 | Isaku Yamahata | addr = pci_bar(pci_dev, region_num); |
808 | d7ce493a | pbrook | if (region_num == PCI_ROM_SLOT) {
|
809 | b7ee1603 | Michael S. Tsirkin | /* ROM enable bit is writeable */
|
810 | 5330de09 | Michael S. Tsirkin | wmask |= PCI_ROM_ADDRESS_ENABLE; |
811 | d7ce493a | pbrook | } |
812 | b0ff8eb2 | Isaku Yamahata | pci_set_long(pci_dev->config + addr, type); |
813 | 14421258 | Isaku Yamahata | if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
814 | 14421258 | Isaku Yamahata | r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
815 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->wmask + addr, wmask); |
816 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
817 | 14421258 | Isaku Yamahata | } else {
|
818 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
819 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
820 | 14421258 | Isaku Yamahata | } |
821 | 69b91039 | bellard | } |
822 | 69b91039 | bellard | |
823 | a0c7a97e | Isaku Yamahata | static uint32_t pci_config_get_io_base(PCIDevice *d,
|
824 | a0c7a97e | Isaku Yamahata | uint32_t base, uint32_t base_upper16) |
825 | a0c7a97e | Isaku Yamahata | { |
826 | a0c7a97e | Isaku Yamahata | uint32_t val; |
827 | a0c7a97e | Isaku Yamahata | |
828 | a0c7a97e | Isaku Yamahata | val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
|
829 | a0c7a97e | Isaku Yamahata | if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
|
830 | 10c9c329 | Isaku Yamahata | val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
|
831 | a0c7a97e | Isaku Yamahata | } |
832 | a0c7a97e | Isaku Yamahata | return val;
|
833 | a0c7a97e | Isaku Yamahata | } |
834 | a0c7a97e | Isaku Yamahata | |
835 | d46636b8 | Isaku Yamahata | static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
|
836 | a0c7a97e | Isaku Yamahata | { |
837 | d46636b8 | Isaku Yamahata | return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
|
838 | a0c7a97e | Isaku Yamahata | << 16;
|
839 | a0c7a97e | Isaku Yamahata | } |
840 | a0c7a97e | Isaku Yamahata | |
841 | d46636b8 | Isaku Yamahata | static pcibus_t pci_config_get_pref_base(PCIDevice *d,
|
842 | a0c7a97e | Isaku Yamahata | uint32_t base, uint32_t upper) |
843 | a0c7a97e | Isaku Yamahata | { |
844 | d46636b8 | Isaku Yamahata | pcibus_t tmp; |
845 | d46636b8 | Isaku Yamahata | pcibus_t val; |
846 | d46636b8 | Isaku Yamahata | |
847 | d46636b8 | Isaku Yamahata | tmp = (pcibus_t)pci_get_word(d->config + base); |
848 | d46636b8 | Isaku Yamahata | val = (tmp & PCI_PREF_RANGE_MASK) << 16;
|
849 | d46636b8 | Isaku Yamahata | if (tmp & PCI_PREF_RANGE_TYPE_64) {
|
850 | d46636b8 | Isaku Yamahata | val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
|
851 | d46636b8 | Isaku Yamahata | } |
852 | a0c7a97e | Isaku Yamahata | return val;
|
853 | a0c7a97e | Isaku Yamahata | } |
854 | a0c7a97e | Isaku Yamahata | |
855 | a0c7a97e | Isaku Yamahata | static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
|
856 | a0c7a97e | Isaku Yamahata | { |
857 | a0c7a97e | Isaku Yamahata | pcibus_t base; |
858 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
859 | a0c7a97e | Isaku Yamahata | base = pci_config_get_io_base(bridge, |
860 | a0c7a97e | Isaku Yamahata | PCI_IO_BASE, PCI_IO_BASE_UPPER16); |
861 | a0c7a97e | Isaku Yamahata | } else {
|
862 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
863 | a0c7a97e | Isaku Yamahata | base = pci_config_get_pref_base( |
864 | a0c7a97e | Isaku Yamahata | bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32); |
865 | a0c7a97e | Isaku Yamahata | } else {
|
866 | a0c7a97e | Isaku Yamahata | base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE); |
867 | a0c7a97e | Isaku Yamahata | } |
868 | a0c7a97e | Isaku Yamahata | } |
869 | a0c7a97e | Isaku Yamahata | |
870 | a0c7a97e | Isaku Yamahata | return base;
|
871 | a0c7a97e | Isaku Yamahata | } |
872 | a0c7a97e | Isaku Yamahata | |
873 | a0c7a97e | Isaku Yamahata | static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
|
874 | a0c7a97e | Isaku Yamahata | { |
875 | a0c7a97e | Isaku Yamahata | pcibus_t limit; |
876 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
877 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_io_base(bridge, |
878 | a0c7a97e | Isaku Yamahata | PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16); |
879 | a0c7a97e | Isaku Yamahata | limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */ |
880 | a0c7a97e | Isaku Yamahata | } else {
|
881 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
|
882 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_pref_base( |
883 | a0c7a97e | Isaku Yamahata | bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32); |
884 | a0c7a97e | Isaku Yamahata | } else {
|
885 | a0c7a97e | Isaku Yamahata | limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT); |
886 | a0c7a97e | Isaku Yamahata | } |
887 | a0c7a97e | Isaku Yamahata | limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */ |
888 | a0c7a97e | Isaku Yamahata | } |
889 | a0c7a97e | Isaku Yamahata | return limit;
|
890 | a0c7a97e | Isaku Yamahata | } |
891 | a0c7a97e | Isaku Yamahata | |
892 | a0c7a97e | Isaku Yamahata | static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size, |
893 | a0c7a97e | Isaku Yamahata | uint8_t type) |
894 | a0c7a97e | Isaku Yamahata | { |
895 | a0c7a97e | Isaku Yamahata | pcibus_t base = *addr; |
896 | a0c7a97e | Isaku Yamahata | pcibus_t limit = *addr + *size - 1;
|
897 | a0c7a97e | Isaku Yamahata | PCIDevice *br; |
898 | a0c7a97e | Isaku Yamahata | |
899 | a0c7a97e | Isaku Yamahata | for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
|
900 | a0c7a97e | Isaku Yamahata | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
901 | a0c7a97e | Isaku Yamahata | |
902 | a0c7a97e | Isaku Yamahata | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
903 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_IO)) {
|
904 | a0c7a97e | Isaku Yamahata | goto no_map;
|
905 | a0c7a97e | Isaku Yamahata | } |
906 | a0c7a97e | Isaku Yamahata | } else {
|
907 | a0c7a97e | Isaku Yamahata | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
908 | a0c7a97e | Isaku Yamahata | goto no_map;
|
909 | a0c7a97e | Isaku Yamahata | } |
910 | a0c7a97e | Isaku Yamahata | } |
911 | a0c7a97e | Isaku Yamahata | |
912 | a0c7a97e | Isaku Yamahata | base = MAX(base, pci_bridge_get_base(br, type)); |
913 | a0c7a97e | Isaku Yamahata | limit = MIN(limit, pci_bridge_get_limit(br, type)); |
914 | a0c7a97e | Isaku Yamahata | } |
915 | a0c7a97e | Isaku Yamahata | |
916 | a0c7a97e | Isaku Yamahata | if (base > limit) {
|
917 | 88a95564 | Michael S. Tsirkin | goto no_map;
|
918 | a0c7a97e | Isaku Yamahata | } |
919 | 88a95564 | Michael S. Tsirkin | *addr = base; |
920 | 88a95564 | Michael S. Tsirkin | *size = limit - base + 1;
|
921 | 88a95564 | Michael S. Tsirkin | return;
|
922 | 88a95564 | Michael S. Tsirkin | no_map:
|
923 | 88a95564 | Michael S. Tsirkin | *addr = PCI_BAR_UNMAPPED; |
924 | 88a95564 | Michael S. Tsirkin | *size = 0;
|
925 | a0c7a97e | Isaku Yamahata | } |
926 | a0c7a97e | Isaku Yamahata | |
927 | 876a350d | Michael S. Tsirkin | static pcibus_t pci_bar_address(PCIDevice *d,
|
928 | 876a350d | Michael S. Tsirkin | int reg, uint8_t type, pcibus_t size)
|
929 | 876a350d | Michael S. Tsirkin | { |
930 | 876a350d | Michael S. Tsirkin | pcibus_t new_addr, last_addr; |
931 | 876a350d | Michael S. Tsirkin | int bar = pci_bar(d, reg);
|
932 | 876a350d | Michael S. Tsirkin | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
933 | 876a350d | Michael S. Tsirkin | |
934 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
935 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_IO)) {
|
936 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
937 | 876a350d | Michael S. Tsirkin | } |
938 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar) & ~(size - 1);
|
939 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
940 | 876a350d | Michael S. Tsirkin | /* NOTE: we have only 64K ioports on PC */
|
941 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) { |
942 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
943 | 876a350d | Michael S. Tsirkin | } |
944 | 876a350d | Michael S. Tsirkin | return new_addr;
|
945 | 876a350d | Michael S. Tsirkin | } |
946 | 876a350d | Michael S. Tsirkin | |
947 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
948 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
949 | 876a350d | Michael S. Tsirkin | } |
950 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
951 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_quad(d->config + bar); |
952 | 876a350d | Michael S. Tsirkin | } else {
|
953 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar); |
954 | 876a350d | Michael S. Tsirkin | } |
955 | 876a350d | Michael S. Tsirkin | /* the ROM slot has a specific enable bit */
|
956 | 876a350d | Michael S. Tsirkin | if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
|
957 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
958 | 876a350d | Michael S. Tsirkin | } |
959 | 876a350d | Michael S. Tsirkin | new_addr &= ~(size - 1);
|
960 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
961 | 876a350d | Michael S. Tsirkin | /* NOTE: we do not support wrapping */
|
962 | 876a350d | Michael S. Tsirkin | /* XXX: as we cannot support really dynamic
|
963 | 876a350d | Michael S. Tsirkin | mappings, we handle specific values as invalid
|
964 | 876a350d | Michael S. Tsirkin | mappings. */
|
965 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || |
966 | 876a350d | Michael S. Tsirkin | last_addr == PCI_BAR_UNMAPPED) { |
967 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
968 | 876a350d | Michael S. Tsirkin | } |
969 | 876a350d | Michael S. Tsirkin | |
970 | 876a350d | Michael S. Tsirkin | /* Now pcibus_t is 64bit.
|
971 | 876a350d | Michael S. Tsirkin | * Check if 32 bit BAR wraps around explicitly.
|
972 | 876a350d | Michael S. Tsirkin | * Without this, PC ide doesn't work well.
|
973 | 876a350d | Michael S. Tsirkin | * TODO: remove this work around.
|
974 | 876a350d | Michael S. Tsirkin | */
|
975 | 876a350d | Michael S. Tsirkin | if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
|
976 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
977 | 876a350d | Michael S. Tsirkin | } |
978 | 876a350d | Michael S. Tsirkin | |
979 | 876a350d | Michael S. Tsirkin | /*
|
980 | 876a350d | Michael S. Tsirkin | * OS is allowed to set BAR beyond its addressable
|
981 | 876a350d | Michael S. Tsirkin | * bits. For example, 32 bit OS can set 64bit bar
|
982 | 876a350d | Michael S. Tsirkin | * to >4G. Check it. TODO: we might need to support
|
983 | 876a350d | Michael S. Tsirkin | * it in the future for e.g. PAE.
|
984 | 876a350d | Michael S. Tsirkin | */
|
985 | 876a350d | Michael S. Tsirkin | if (last_addr >= TARGET_PHYS_ADDR_MAX) {
|
986 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
987 | 876a350d | Michael S. Tsirkin | } |
988 | 876a350d | Michael S. Tsirkin | |
989 | 876a350d | Michael S. Tsirkin | return new_addr;
|
990 | 876a350d | Michael S. Tsirkin | } |
991 | 876a350d | Michael S. Tsirkin | |
992 | 0ac32c83 | bellard | static void pci_update_mappings(PCIDevice *d) |
993 | 0ac32c83 | bellard | { |
994 | 0ac32c83 | bellard | PCIIORegion *r; |
995 | 876a350d | Michael S. Tsirkin | int i;
|
996 | c71b5b4a | Blue Swirl | pcibus_t new_addr, filtered_size; |
997 | 3b46e624 | ths | |
998 | 8a8696a3 | bellard | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
999 | 0ac32c83 | bellard | r = &d->io_regions[i]; |
1000 | a9688570 | Isaku Yamahata | |
1001 | a9688570 | Isaku Yamahata | /* this region isn't registered */
|
1002 | ec503442 | Isaku Yamahata | if (!r->size)
|
1003 | a9688570 | Isaku Yamahata | continue;
|
1004 | a9688570 | Isaku Yamahata | |
1005 | 876a350d | Michael S. Tsirkin | new_addr = pci_bar_address(d, i, r->type, r->size); |
1006 | a9688570 | Isaku Yamahata | |
1007 | a0c7a97e | Isaku Yamahata | /* bridge filtering */
|
1008 | a0c7a97e | Isaku Yamahata | filtered_size = r->size; |
1009 | a0c7a97e | Isaku Yamahata | if (new_addr != PCI_BAR_UNMAPPED) {
|
1010 | a0c7a97e | Isaku Yamahata | pci_bridge_filter(d, &new_addr, &filtered_size, r->type); |
1011 | a0c7a97e | Isaku Yamahata | } |
1012 | a0c7a97e | Isaku Yamahata | |
1013 | a9688570 | Isaku Yamahata | /* This bar isn't changed */
|
1014 | a0c7a97e | Isaku Yamahata | if (new_addr == r->addr && filtered_size == r->filtered_size)
|
1015 | a9688570 | Isaku Yamahata | continue;
|
1016 | a9688570 | Isaku Yamahata | |
1017 | a9688570 | Isaku Yamahata | /* now do the real mapping */
|
1018 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
1019 | a9688570 | Isaku Yamahata | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1020 | a9688570 | Isaku Yamahata | int class;
|
1021 | a9688570 | Isaku Yamahata | /* NOTE: specific hack for IDE in PC case:
|
1022 | a9688570 | Isaku Yamahata | only one byte must be mapped. */
|
1023 | a9688570 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1024 | a9688570 | Isaku Yamahata | if (class == 0x0101 && r->size == 4) { |
1025 | a9688570 | Isaku Yamahata | isa_unassign_ioport(r->addr + 2, 1); |
1026 | a9688570 | Isaku Yamahata | } else {
|
1027 | a0c7a97e | Isaku Yamahata | isa_unassign_ioport(r->addr, r->filtered_size); |
1028 | 0ac32c83 | bellard | } |
1029 | a9688570 | Isaku Yamahata | } else {
|
1030 | c71b5b4a | Blue Swirl | cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr), |
1031 | a0c7a97e | Isaku Yamahata | r->filtered_size, |
1032 | a9688570 | Isaku Yamahata | IO_MEM_UNASSIGNED); |
1033 | a0c7a97e | Isaku Yamahata | qemu_unregister_coalesced_mmio(r->addr, r->filtered_size); |
1034 | 0ac32c83 | bellard | } |
1035 | 0ac32c83 | bellard | } |
1036 | a9688570 | Isaku Yamahata | r->addr = new_addr; |
1037 | a0c7a97e | Isaku Yamahata | r->filtered_size = filtered_size; |
1038 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
1039 | a0c7a97e | Isaku Yamahata | /*
|
1040 | a0c7a97e | Isaku Yamahata | * TODO: currently almost all the map funcions assumes
|
1041 | a0c7a97e | Isaku Yamahata | * filtered_size == size and addr & ~(size - 1) == addr.
|
1042 | a0c7a97e | Isaku Yamahata | * However with bridge filtering, they aren't always true.
|
1043 | a0c7a97e | Isaku Yamahata | * Teach them such cases, such that filtered_size < size and
|
1044 | a0c7a97e | Isaku Yamahata | * addr & (size - 1) != 0.
|
1045 | a0c7a97e | Isaku Yamahata | */
|
1046 | cf616802 | Blue Swirl | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1047 | cf616802 | Blue Swirl | r->map_func(d, i, r->addr, r->filtered_size, r->type); |
1048 | cf616802 | Blue Swirl | } else {
|
1049 | cf616802 | Blue Swirl | r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr), |
1050 | cf616802 | Blue Swirl | r->filtered_size, r->type); |
1051 | cf616802 | Blue Swirl | } |
1052 | a9688570 | Isaku Yamahata | } |
1053 | 0ac32c83 | bellard | } |
1054 | 0ac32c83 | bellard | } |
1055 | 0ac32c83 | bellard | |
1056 | a7b15a5c | Michael S. Tsirkin | static inline int pci_irq_disabled(PCIDevice *d) |
1057 | a7b15a5c | Michael S. Tsirkin | { |
1058 | a7b15a5c | Michael S. Tsirkin | return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
|
1059 | a7b15a5c | Michael S. Tsirkin | } |
1060 | a7b15a5c | Michael S. Tsirkin | |
1061 | a7b15a5c | Michael S. Tsirkin | /* Called after interrupt disabled field update in config space,
|
1062 | a7b15a5c | Michael S. Tsirkin | * assert/deassert interrupts if necessary.
|
1063 | a7b15a5c | Michael S. Tsirkin | * Gets original interrupt disable bit value (before update). */
|
1064 | a7b15a5c | Michael S. Tsirkin | static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) |
1065 | a7b15a5c | Michael S. Tsirkin | { |
1066 | a7b15a5c | Michael S. Tsirkin | int i, disabled = pci_irq_disabled(d);
|
1067 | a7b15a5c | Michael S. Tsirkin | if (disabled == was_irq_disabled)
|
1068 | a7b15a5c | Michael S. Tsirkin | return;
|
1069 | a7b15a5c | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
1070 | a7b15a5c | Michael S. Tsirkin | int state = pci_irq_state(d, i);
|
1071 | a7b15a5c | Michael S. Tsirkin | pci_change_irq_level(d, i, disabled ? -state : state); |
1072 | a7b15a5c | Michael S. Tsirkin | } |
1073 | a7b15a5c | Michael S. Tsirkin | } |
1074 | a7b15a5c | Michael S. Tsirkin | |
1075 | 5fafdf24 | ths | uint32_t pci_default_read_config(PCIDevice *d, |
1076 | 0ac32c83 | bellard | uint32_t address, int len)
|
1077 | 69b91039 | bellard | { |
1078 | 5029fe12 | Isaku Yamahata | uint32_t val = 0;
|
1079 | 5029fe12 | Isaku Yamahata | assert(len == 1 || len == 2 || len == 4); |
1080 | a9f49946 | Isaku Yamahata | len = MIN(len, pci_config_size(d) - address); |
1081 | 5029fe12 | Isaku Yamahata | memcpy(&val, d->config + address, len); |
1082 | 5029fe12 | Isaku Yamahata | return le32_to_cpu(val);
|
1083 | 0ac32c83 | bellard | } |
1084 | 0ac32c83 | bellard | |
1085 | b7ee1603 | Michael S. Tsirkin | void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
1086 | 0ac32c83 | bellard | { |
1087 | a7b15a5c | Michael S. Tsirkin | int i, was_irq_disabled = pci_irq_disabled(d);
|
1088 | a9f49946 | Isaku Yamahata | uint32_t config_size = pci_config_size(d); |
1089 | 0ac32c83 | bellard | |
1090 | 91011d4f | Stefan Weil | for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) { |
1091 | 91011d4f | Stefan Weil | uint8_t wmask = d->wmask[addr + i]; |
1092 | 91011d4f | Stefan Weil | d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
1093 | 0ac32c83 | bellard | } |
1094 | 260c0cd3 | Isaku Yamahata | if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
1095 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
1096 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
1097 | 260c0cd3 | Isaku Yamahata | range_covers_byte(addr, l, PCI_COMMAND)) |
1098 | 0ac32c83 | bellard | pci_update_mappings(d); |
1099 | a7b15a5c | Michael S. Tsirkin | |
1100 | a7b15a5c | Michael S. Tsirkin | if (range_covers_byte(addr, l, PCI_COMMAND))
|
1101 | a7b15a5c | Michael S. Tsirkin | pci_update_irq_disabled(d, was_irq_disabled); |
1102 | 69b91039 | bellard | } |
1103 | 69b91039 | bellard | |
1104 | 502a5395 | pbrook | /***********************************************************/
|
1105 | 502a5395 | pbrook | /* generic PCI irq support */
|
1106 | 30468f78 | bellard | |
1107 | 502a5395 | pbrook | /* 0 <= irq_num <= 3. level must be 0 or 1 */
|
1108 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level) |
1109 | 69b91039 | bellard | { |
1110 | a60380a5 | Juan Quintela | PCIDevice *pci_dev = opaque; |
1111 | 80b3ada7 | pbrook | int change;
|
1112 | 3b46e624 | ths | |
1113 | d036bb21 | Michael S. Tsirkin | change = level - pci_irq_state(pci_dev, irq_num); |
1114 | 80b3ada7 | pbrook | if (!change)
|
1115 | 80b3ada7 | pbrook | return;
|
1116 | d2b59317 | pbrook | |
1117 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(pci_dev, irq_num, level); |
1118 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(pci_dev); |
1119 | a7b15a5c | Michael S. Tsirkin | if (pci_irq_disabled(pci_dev))
|
1120 | a7b15a5c | Michael S. Tsirkin | return;
|
1121 | d036bb21 | Michael S. Tsirkin | pci_change_irq_level(pci_dev, irq_num, change); |
1122 | 69b91039 | bellard | } |
1123 | 69b91039 | bellard | |
1124 | 502a5395 | pbrook | /***********************************************************/
|
1125 | 502a5395 | pbrook | /* monitor info on PCI */
|
1126 | 0ac32c83 | bellard | |
1127 | 6650ee6d | pbrook | typedef struct { |
1128 | 6650ee6d | pbrook | uint16_t class; |
1129 | 6650ee6d | pbrook | const char *desc; |
1130 | 6650ee6d | pbrook | } pci_class_desc; |
1131 | 6650ee6d | pbrook | |
1132 | 09bc878a | blueswir1 | static const pci_class_desc pci_class_descriptions[] = |
1133 | 6650ee6d | pbrook | { |
1134 | 4ca9c76f | pbrook | { 0x0100, "SCSI controller"}, |
1135 | 6650ee6d | pbrook | { 0x0101, "IDE controller"}, |
1136 | dcb5b19a | ths | { 0x0102, "Floppy controller"}, |
1137 | dcb5b19a | ths | { 0x0103, "IPI controller"}, |
1138 | dcb5b19a | ths | { 0x0104, "RAID controller"}, |
1139 | dcb5b19a | ths | { 0x0106, "SATA controller"}, |
1140 | dcb5b19a | ths | { 0x0107, "SAS controller"}, |
1141 | dcb5b19a | ths | { 0x0180, "Storage controller"}, |
1142 | 6650ee6d | pbrook | { 0x0200, "Ethernet controller"}, |
1143 | dcb5b19a | ths | { 0x0201, "Token Ring controller"}, |
1144 | dcb5b19a | ths | { 0x0202, "FDDI controller"}, |
1145 | dcb5b19a | ths | { 0x0203, "ATM controller"}, |
1146 | dcb5b19a | ths | { 0x0280, "Network controller"}, |
1147 | 6650ee6d | pbrook | { 0x0300, "VGA controller"}, |
1148 | dcb5b19a | ths | { 0x0301, "XGA controller"}, |
1149 | dcb5b19a | ths | { 0x0302, "3D controller"}, |
1150 | dcb5b19a | ths | { 0x0380, "Display controller"}, |
1151 | dcb5b19a | ths | { 0x0400, "Video controller"}, |
1152 | dcb5b19a | ths | { 0x0401, "Audio controller"}, |
1153 | dcb5b19a | ths | { 0x0402, "Phone"}, |
1154 | dcb5b19a | ths | { 0x0480, "Multimedia controller"}, |
1155 | dcb5b19a | ths | { 0x0500, "RAM controller"}, |
1156 | dcb5b19a | ths | { 0x0501, "Flash controller"}, |
1157 | dcb5b19a | ths | { 0x0580, "Memory controller"}, |
1158 | 6650ee6d | pbrook | { 0x0600, "Host bridge"}, |
1159 | 6650ee6d | pbrook | { 0x0601, "ISA bridge"}, |
1160 | dcb5b19a | ths | { 0x0602, "EISA bridge"}, |
1161 | dcb5b19a | ths | { 0x0603, "MC bridge"}, |
1162 | 6650ee6d | pbrook | { 0x0604, "PCI bridge"}, |
1163 | dcb5b19a | ths | { 0x0605, "PCMCIA bridge"}, |
1164 | dcb5b19a | ths | { 0x0606, "NUBUS bridge"}, |
1165 | dcb5b19a | ths | { 0x0607, "CARDBUS bridge"}, |
1166 | dcb5b19a | ths | { 0x0608, "RACEWAY bridge"}, |
1167 | dcb5b19a | ths | { 0x0680, "Bridge"}, |
1168 | 6650ee6d | pbrook | { 0x0c03, "USB controller"}, |
1169 | 6650ee6d | pbrook | { 0, NULL} |
1170 | 6650ee6d | pbrook | }; |
1171 | 6650ee6d | pbrook | |
1172 | 163c8a59 | Luiz Capitulino | static void pci_for_each_device_under_bus(PCIBus *bus, |
1173 | 163c8a59 | Luiz Capitulino | void (*fn)(PCIBus *b, PCIDevice *d))
|
1174 | 30468f78 | bellard | { |
1175 | 163c8a59 | Luiz Capitulino | PCIDevice *d; |
1176 | 163c8a59 | Luiz Capitulino | int devfn;
|
1177 | 30468f78 | bellard | |
1178 | 163c8a59 | Luiz Capitulino | for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1179 | 163c8a59 | Luiz Capitulino | d = bus->devices[devfn]; |
1180 | 163c8a59 | Luiz Capitulino | if (d) {
|
1181 | 163c8a59 | Luiz Capitulino | fn(bus, d); |
1182 | 163c8a59 | Luiz Capitulino | } |
1183 | 163c8a59 | Luiz Capitulino | } |
1184 | 163c8a59 | Luiz Capitulino | } |
1185 | 163c8a59 | Luiz Capitulino | |
1186 | 163c8a59 | Luiz Capitulino | void pci_for_each_device(PCIBus *bus, int bus_num, |
1187 | 163c8a59 | Luiz Capitulino | void (*fn)(PCIBus *b, PCIDevice *d))
|
1188 | 163c8a59 | Luiz Capitulino | { |
1189 | 163c8a59 | Luiz Capitulino | bus = pci_find_bus(bus, bus_num); |
1190 | 163c8a59 | Luiz Capitulino | |
1191 | 163c8a59 | Luiz Capitulino | if (bus) {
|
1192 | 163c8a59 | Luiz Capitulino | pci_for_each_device_under_bus(bus, fn); |
1193 | 163c8a59 | Luiz Capitulino | } |
1194 | 163c8a59 | Luiz Capitulino | } |
1195 | 163c8a59 | Luiz Capitulino | |
1196 | 163c8a59 | Luiz Capitulino | static void pci_device_print(Monitor *mon, QDict *device) |
1197 | 163c8a59 | Luiz Capitulino | { |
1198 | 163c8a59 | Luiz Capitulino | QDict *qdict; |
1199 | 163c8a59 | Luiz Capitulino | QListEntry *entry; |
1200 | 163c8a59 | Luiz Capitulino | uint64_t addr, size; |
1201 | 163c8a59 | Luiz Capitulino | |
1202 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " Bus %2" PRId64 ", ", qdict_get_int(device, "bus")); |
1203 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n", |
1204 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "slot"),
|
1205 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "function"));
|
1206 | 376253ec | aliguori | monitor_printf(mon, " ");
|
1207 | 163c8a59 | Luiz Capitulino | |
1208 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "class_info");
|
1209 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(qdict, "desc")) { |
1210 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "%s", qdict_get_str(qdict, "desc")); |
1211 | 6650ee6d | pbrook | } else {
|
1212 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class")); |
1213 | 72cc6cfe | bellard | } |
1214 | 30468f78 | bellard | |
1215 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "id");
|
1216 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n", |
1217 | 163c8a59 | Luiz Capitulino | qdict_get_int(qdict, "device"),
|
1218 | 163c8a59 | Luiz Capitulino | qdict_get_int(qdict, "vendor"));
|
1219 | 163c8a59 | Luiz Capitulino | |
1220 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(device, "irq")) { |
1221 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " IRQ %" PRId64 ".\n", |
1222 | 163c8a59 | Luiz Capitulino | qdict_get_int(device, "irq"));
|
1223 | 30468f78 | bellard | } |
1224 | b4dccd8d | Isaku Yamahata | |
1225 | 163c8a59 | Luiz Capitulino | if (qdict_haskey(device, "pci_bridge")) { |
1226 | 163c8a59 | Luiz Capitulino | QDict *info; |
1227 | 163c8a59 | Luiz Capitulino | |
1228 | 163c8a59 | Luiz Capitulino | qdict = qdict_get_qdict(device, "pci_bridge");
|
1229 | 163c8a59 | Luiz Capitulino | |
1230 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "bus");
|
1231 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " BUS %" PRId64 ".\n", |
1232 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "number"));
|
1233 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " secondary bus %" PRId64 ".\n", |
1234 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "secondary"));
|
1235 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " subordinate bus %" PRId64 ".\n", |
1236 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "subordinate"));
|
1237 | b4dccd8d | Isaku Yamahata | |
1238 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "io_range");
|
1239 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n", |
1240 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1241 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1242 | b4dccd8d | Isaku Yamahata | |
1243 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "memory_range");
|
1244 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, |
1245 | b4dccd8d | Isaku Yamahata | " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1246 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1247 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1248 | b4dccd8d | Isaku Yamahata | |
1249 | 163c8a59 | Luiz Capitulino | info = qdict_get_qdict(qdict, "prefetchable_range");
|
1250 | b4dccd8d | Isaku Yamahata | monitor_printf(mon, " prefetchable memory range "
|
1251 | 163c8a59 | Luiz Capitulino | "[0x%08"PRIx64", 0x%08"PRIx64"]\n", |
1252 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "base"),
|
1253 | 163c8a59 | Luiz Capitulino | qdict_get_int(info, "limit"));
|
1254 | 80b3ada7 | pbrook | } |
1255 | 14421258 | Isaku Yamahata | |
1256 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
|
1257 | 163c8a59 | Luiz Capitulino | qdict = qobject_to_qdict(qlist_entry_obj(entry)); |
1258 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " BAR%d: ", (int) qdict_get_int(qdict, "bar")); |
1259 | 163c8a59 | Luiz Capitulino | |
1260 | 163c8a59 | Luiz Capitulino | addr = qdict_get_int(qdict, "address");
|
1261 | 163c8a59 | Luiz Capitulino | size = qdict_get_int(qdict, "size");
|
1262 | 163c8a59 | Luiz Capitulino | |
1263 | 163c8a59 | Luiz Capitulino | if (!strcmp(qdict_get_str(qdict, "type"), "io")) { |
1264 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
|
1265 | 163c8a59 | Luiz Capitulino | " [0x%04"FMT_PCIBUS"].\n", |
1266 | 163c8a59 | Luiz Capitulino | addr, addr + size - 1);
|
1267 | 163c8a59 | Luiz Capitulino | } else {
|
1268 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
|
1269 | 89e8b13c | Isaku Yamahata | " [0x%08"FMT_PCIBUS"].\n", |
1270 | 163c8a59 | Luiz Capitulino | qdict_get_bool(qdict, "mem_type_64") ? 64 : 32, |
1271 | 163c8a59 | Luiz Capitulino | qdict_get_bool(qdict, "prefetch") ?
|
1272 | 163c8a59 | Luiz Capitulino | " prefetchable" : "", addr, addr + size - 1); |
1273 | 502a5395 | pbrook | } |
1274 | 77d4bc34 | bellard | } |
1275 | 163c8a59 | Luiz Capitulino | |
1276 | 163c8a59 | Luiz Capitulino | monitor_printf(mon, " id \"%s\"\n", qdict_get_str(device, "qdev_id")); |
1277 | 163c8a59 | Luiz Capitulino | |
1278 | d5e4acf7 | Luiz Capitulino | if (qdict_haskey(device, "pci_bridge")) { |
1279 | d5e4acf7 | Luiz Capitulino | qdict = qdict_get_qdict(device, "pci_bridge");
|
1280 | d5e4acf7 | Luiz Capitulino | if (qdict_haskey(qdict, "devices")) { |
1281 | d5e4acf7 | Luiz Capitulino | QListEntry *dev; |
1282 | d5e4acf7 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1283 | d5e4acf7 | Luiz Capitulino | pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1284 | d5e4acf7 | Luiz Capitulino | } |
1285 | d5e4acf7 | Luiz Capitulino | } |
1286 | d5e4acf7 | Luiz Capitulino | } |
1287 | 163c8a59 | Luiz Capitulino | } |
1288 | 163c8a59 | Luiz Capitulino | |
1289 | 163c8a59 | Luiz Capitulino | void do_pci_info_print(Monitor *mon, const QObject *data) |
1290 | 163c8a59 | Luiz Capitulino | { |
1291 | 163c8a59 | Luiz Capitulino | QListEntry *bus, *dev; |
1292 | 163c8a59 | Luiz Capitulino | |
1293 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) { |
1294 | 163c8a59 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus)); |
1295 | 163c8a59 | Luiz Capitulino | QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
|
1296 | 163c8a59 | Luiz Capitulino | pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev))); |
1297 | 163c8a59 | Luiz Capitulino | } |
1298 | 80b3ada7 | pbrook | } |
1299 | 384d8876 | bellard | } |
1300 | 384d8876 | bellard | |
1301 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_dev_class(const PCIDevice *dev) |
1302 | 163c8a59 | Luiz Capitulino | { |
1303 | 163c8a59 | Luiz Capitulino | int class;
|
1304 | 163c8a59 | Luiz Capitulino | const pci_class_desc *desc;
|
1305 | 163c8a59 | Luiz Capitulino | |
1306 | 163c8a59 | Luiz Capitulino | class = pci_get_word(dev->config + PCI_CLASS_DEVICE); |
1307 | 163c8a59 | Luiz Capitulino | desc = pci_class_descriptions; |
1308 | 163c8a59 | Luiz Capitulino | while (desc->desc && class != desc->class)
|
1309 | 163c8a59 | Luiz Capitulino | desc++; |
1310 | 163c8a59 | Luiz Capitulino | |
1311 | 163c8a59 | Luiz Capitulino | if (desc->desc) {
|
1312 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'desc': %s, 'class': %d }", |
1313 | 163c8a59 | Luiz Capitulino | desc->desc, class); |
1314 | 163c8a59 | Luiz Capitulino | } else {
|
1315 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'class': %d }", class); |
1316 | 163c8a59 | Luiz Capitulino | } |
1317 | 163c8a59 | Luiz Capitulino | } |
1318 | 163c8a59 | Luiz Capitulino | |
1319 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_dev_id(const PCIDevice *dev) |
1320 | 163c8a59 | Luiz Capitulino | { |
1321 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }", |
1322 | 163c8a59 | Luiz Capitulino | pci_get_word(dev->config + PCI_VENDOR_ID), |
1323 | 163c8a59 | Luiz Capitulino | pci_get_word(dev->config + PCI_DEVICE_ID)); |
1324 | 163c8a59 | Luiz Capitulino | } |
1325 | 163c8a59 | Luiz Capitulino | |
1326 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_regions_list(const PCIDevice *dev) |
1327 | 163c8a59 | Luiz Capitulino | { |
1328 | 163c8a59 | Luiz Capitulino | int i;
|
1329 | 163c8a59 | Luiz Capitulino | QList *regions_list; |
1330 | 163c8a59 | Luiz Capitulino | |
1331 | 163c8a59 | Luiz Capitulino | regions_list = qlist_new(); |
1332 | 163c8a59 | Luiz Capitulino | |
1333 | 163c8a59 | Luiz Capitulino | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1334 | 163c8a59 | Luiz Capitulino | QObject *obj; |
1335 | 163c8a59 | Luiz Capitulino | const PCIIORegion *r = &dev->io_regions[i];
|
1336 | 163c8a59 | Luiz Capitulino | |
1337 | 163c8a59 | Luiz Capitulino | if (!r->size) {
|
1338 | 163c8a59 | Luiz Capitulino | continue;
|
1339 | 163c8a59 | Luiz Capitulino | } |
1340 | 163c8a59 | Luiz Capitulino | |
1341 | 163c8a59 | Luiz Capitulino | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1342 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
|
1343 | 163c8a59 | Luiz Capitulino | "'address': %" PRId64 ", " |
1344 | 163c8a59 | Luiz Capitulino | "'size': %" PRId64 " }", |
1345 | 163c8a59 | Luiz Capitulino | i, r->addr, r->size); |
1346 | 163c8a59 | Luiz Capitulino | } else {
|
1347 | 163c8a59 | Luiz Capitulino | int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
|
1348 | 163c8a59 | Luiz Capitulino | |
1349 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
|
1350 | 163c8a59 | Luiz Capitulino | "'mem_type_64': %i, 'prefetch': %i, "
|
1351 | 163c8a59 | Luiz Capitulino | "'address': %" PRId64 ", " |
1352 | 163c8a59 | Luiz Capitulino | "'size': %" PRId64 " }", |
1353 | 163c8a59 | Luiz Capitulino | i, mem_type_64, |
1354 | 163c8a59 | Luiz Capitulino | r->type & PCI_BASE_ADDRESS_MEM_PREFETCH, |
1355 | 163c8a59 | Luiz Capitulino | r->addr, r->size); |
1356 | 163c8a59 | Luiz Capitulino | } |
1357 | 163c8a59 | Luiz Capitulino | |
1358 | 163c8a59 | Luiz Capitulino | qlist_append_obj(regions_list, obj); |
1359 | 163c8a59 | Luiz Capitulino | } |
1360 | 163c8a59 | Luiz Capitulino | |
1361 | 163c8a59 | Luiz Capitulino | return QOBJECT(regions_list);
|
1362 | 163c8a59 | Luiz Capitulino | } |
1363 | 163c8a59 | Luiz Capitulino | |
1364 | d5e4acf7 | Luiz Capitulino | static QObject *pci_get_devices_list(PCIBus *bus, int bus_num); |
1365 | d5e4acf7 | Luiz Capitulino | |
1366 | d5e4acf7 | Luiz Capitulino | static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num) |
1367 | 163c8a59 | Luiz Capitulino | { |
1368 | b5937f29 | Isaku Yamahata | uint8_t type; |
1369 | 163c8a59 | Luiz Capitulino | QObject *obj; |
1370 | 163c8a59 | Luiz Capitulino | |
1371 | 163c8a59 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d," "'class_info': %p, 'id': %p, 'regions': %p," |
1372 | 163c8a59 | Luiz Capitulino | " 'qdev_id': %s }",
|
1373 | 163c8a59 | Luiz Capitulino | bus_num, |
1374 | 163c8a59 | Luiz Capitulino | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
1375 | 163c8a59 | Luiz Capitulino | pci_get_dev_class(dev), pci_get_dev_id(dev), |
1376 | 163c8a59 | Luiz Capitulino | pci_get_regions_list(dev), |
1377 | 163c8a59 | Luiz Capitulino | dev->qdev.id ? dev->qdev.id : "");
|
1378 | 163c8a59 | Luiz Capitulino | |
1379 | 163c8a59 | Luiz Capitulino | if (dev->config[PCI_INTERRUPT_PIN] != 0) { |
1380 | 163c8a59 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(obj); |
1381 | 163c8a59 | Luiz Capitulino | qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
|
1382 | 163c8a59 | Luiz Capitulino | } |
1383 | 163c8a59 | Luiz Capitulino | |
1384 | b5937f29 | Isaku Yamahata | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
1385 | b5937f29 | Isaku Yamahata | if (type == PCI_HEADER_TYPE_BRIDGE) {
|
1386 | 163c8a59 | Luiz Capitulino | QDict *qdict; |
1387 | 163c8a59 | Luiz Capitulino | QObject *pci_bridge; |
1388 | 163c8a59 | Luiz Capitulino | |
1389 | 163c8a59 | Luiz Capitulino | pci_bridge = qobject_from_jsonf("{ 'bus': "
|
1390 | 163c8a59 | Luiz Capitulino | "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
|
1391 | 163c8a59 | Luiz Capitulino | "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1392 | 163c8a59 | Luiz Capitulino | "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, " |
1393 | 163c8a59 | Luiz Capitulino | "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }", |
1394 | c021f8e6 | Blue Swirl | dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS], |
1395 | 163c8a59 | Luiz Capitulino | dev->config[PCI_SUBORDINATE_BUS], |
1396 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1397 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO), |
1398 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1399 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY), |
1400 | 163c8a59 | Luiz Capitulino | pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1401 | 163c8a59 | Luiz Capitulino | PCI_BASE_ADDRESS_MEM_PREFETCH), |
1402 | 163c8a59 | Luiz Capitulino | pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY | |
1403 | 163c8a59 | Luiz Capitulino | PCI_BASE_ADDRESS_MEM_PREFETCH)); |
1404 | 163c8a59 | Luiz Capitulino | |
1405 | c021f8e6 | Blue Swirl | if (dev->config[PCI_SECONDARY_BUS] != 0) { |
1406 | c021f8e6 | Blue Swirl | PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]); |
1407 | d5e4acf7 | Luiz Capitulino | |
1408 | c021f8e6 | Blue Swirl | if (child_bus) {
|
1409 | c021f8e6 | Blue Swirl | qdict = qobject_to_qdict(pci_bridge); |
1410 | c021f8e6 | Blue Swirl | qdict_put_obj(qdict, "devices",
|
1411 | c021f8e6 | Blue Swirl | pci_get_devices_list(child_bus, |
1412 | c021f8e6 | Blue Swirl | dev->config[PCI_SECONDARY_BUS])); |
1413 | c021f8e6 | Blue Swirl | } |
1414 | c021f8e6 | Blue Swirl | } |
1415 | 163c8a59 | Luiz Capitulino | qdict = qobject_to_qdict(obj); |
1416 | 163c8a59 | Luiz Capitulino | qdict_put_obj(qdict, "pci_bridge", pci_bridge);
|
1417 | 163c8a59 | Luiz Capitulino | } |
1418 | 163c8a59 | Luiz Capitulino | |
1419 | 163c8a59 | Luiz Capitulino | return obj;
|
1420 | 163c8a59 | Luiz Capitulino | } |
1421 | 163c8a59 | Luiz Capitulino | |
1422 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_devices_list(PCIBus *bus, int bus_num) |
1423 | 384d8876 | bellard | { |
1424 | 502a5395 | pbrook | int devfn;
|
1425 | 163c8a59 | Luiz Capitulino | PCIDevice *dev; |
1426 | 163c8a59 | Luiz Capitulino | QList *dev_list; |
1427 | 3b46e624 | ths | |
1428 | 163c8a59 | Luiz Capitulino | dev_list = qlist_new(); |
1429 | 163c8a59 | Luiz Capitulino | |
1430 | 163c8a59 | Luiz Capitulino | for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1431 | 163c8a59 | Luiz Capitulino | dev = bus->devices[devfn]; |
1432 | 163c8a59 | Luiz Capitulino | if (dev) {
|
1433 | d5e4acf7 | Luiz Capitulino | qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num)); |
1434 | 163c8a59 | Luiz Capitulino | } |
1435 | 1074df4f | Isaku Yamahata | } |
1436 | 163c8a59 | Luiz Capitulino | |
1437 | 163c8a59 | Luiz Capitulino | return QOBJECT(dev_list);
|
1438 | 1074df4f | Isaku Yamahata | } |
1439 | 1074df4f | Isaku Yamahata | |
1440 | 163c8a59 | Luiz Capitulino | static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num) |
1441 | 1074df4f | Isaku Yamahata | { |
1442 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1443 | 502a5395 | pbrook | if (bus) {
|
1444 | 163c8a59 | Luiz Capitulino | return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }", |
1445 | 163c8a59 | Luiz Capitulino | bus_num, pci_get_devices_list(bus, bus_num)); |
1446 | f2aa58c6 | bellard | } |
1447 | 163c8a59 | Luiz Capitulino | |
1448 | 163c8a59 | Luiz Capitulino | return NULL; |
1449 | f2aa58c6 | bellard | } |
1450 | f2aa58c6 | bellard | |
1451 | 163c8a59 | Luiz Capitulino | void do_pci_info(Monitor *mon, QObject **ret_data)
|
1452 | f2aa58c6 | bellard | { |
1453 | 163c8a59 | Luiz Capitulino | QList *bus_list; |
1454 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
1455 | 163c8a59 | Luiz Capitulino | |
1456 | 163c8a59 | Luiz Capitulino | bus_list = qlist_new(); |
1457 | 163c8a59 | Luiz Capitulino | |
1458 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
1459 | 163c8a59 | Luiz Capitulino | QObject *obj = pci_get_bus_dict(host->bus, 0);
|
1460 | 163c8a59 | Luiz Capitulino | if (obj) {
|
1461 | 163c8a59 | Luiz Capitulino | qlist_append_obj(bus_list, obj); |
1462 | 163c8a59 | Luiz Capitulino | } |
1463 | e822a52a | Isaku Yamahata | } |
1464 | 163c8a59 | Luiz Capitulino | |
1465 | 163c8a59 | Luiz Capitulino | *ret_data = QOBJECT(bus_list); |
1466 | 77d4bc34 | bellard | } |
1467 | a41b2ff2 | pbrook | |
1468 | cb457d76 | aliguori | static const char * const pci_nic_models[] = { |
1469 | cb457d76 | aliguori | "ne2k_pci",
|
1470 | cb457d76 | aliguori | "i82551",
|
1471 | cb457d76 | aliguori | "i82557b",
|
1472 | cb457d76 | aliguori | "i82559er",
|
1473 | cb457d76 | aliguori | "rtl8139",
|
1474 | cb457d76 | aliguori | "e1000",
|
1475 | cb457d76 | aliguori | "pcnet",
|
1476 | cb457d76 | aliguori | "virtio",
|
1477 | cb457d76 | aliguori | NULL
|
1478 | cb457d76 | aliguori | }; |
1479 | cb457d76 | aliguori | |
1480 | 9d07d757 | Paul Brook | static const char * const pci_nic_names[] = { |
1481 | 9d07d757 | Paul Brook | "ne2k_pci",
|
1482 | 9d07d757 | Paul Brook | "i82551",
|
1483 | 9d07d757 | Paul Brook | "i82557b",
|
1484 | 9d07d757 | Paul Brook | "i82559er",
|
1485 | 9d07d757 | Paul Brook | "rtl8139",
|
1486 | 9d07d757 | Paul Brook | "e1000",
|
1487 | 9d07d757 | Paul Brook | "pcnet",
|
1488 | 53c25cea | Paul Brook | "virtio-net-pci",
|
1489 | cb457d76 | aliguori | NULL
|
1490 | cb457d76 | aliguori | }; |
1491 | cb457d76 | aliguori | |
1492 | a41b2ff2 | pbrook | /* Initialize a PCI NIC. */
|
1493 | 33e66b86 | Markus Armbruster | /* FIXME callers should check for failure, but don't */
|
1494 | 5607c388 | Markus Armbruster | PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model, |
1495 | 5607c388 | Markus Armbruster | const char *default_devaddr) |
1496 | a41b2ff2 | pbrook | { |
1497 | 5607c388 | Markus Armbruster | const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1498 | 07caea31 | Markus Armbruster | PCIBus *bus; |
1499 | 07caea31 | Markus Armbruster | int devfn;
|
1500 | 5607c388 | Markus Armbruster | PCIDevice *pci_dev; |
1501 | 9d07d757 | Paul Brook | DeviceState *dev; |
1502 | cb457d76 | aliguori | int i;
|
1503 | cb457d76 | aliguori | |
1504 | 07caea31 | Markus Armbruster | i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1505 | 07caea31 | Markus Armbruster | if (i < 0) |
1506 | 07caea31 | Markus Armbruster | return NULL; |
1507 | 07caea31 | Markus Armbruster | |
1508 | 07caea31 | Markus Armbruster | bus = pci_get_bus_devfn(&devfn, devaddr); |
1509 | 07caea31 | Markus Armbruster | if (!bus) {
|
1510 | 1ecda02b | Markus Armbruster | error_report("Invalid PCI device address %s for device %s",
|
1511 | 1ecda02b | Markus Armbruster | devaddr, pci_nic_names[i]); |
1512 | 07caea31 | Markus Armbruster | return NULL; |
1513 | 07caea31 | Markus Armbruster | } |
1514 | 07caea31 | Markus Armbruster | |
1515 | 499cf102 | Markus Armbruster | pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1516 | 9ee05825 | Markus Armbruster | dev = &pci_dev->qdev; |
1517 | 1cc33683 | Gerd Hoffmann | qdev_set_nic_properties(dev, nd); |
1518 | 07caea31 | Markus Armbruster | if (qdev_init(dev) < 0) |
1519 | 07caea31 | Markus Armbruster | return NULL; |
1520 | 9ee05825 | Markus Armbruster | return pci_dev;
|
1521 | a41b2ff2 | pbrook | } |
1522 | a41b2ff2 | pbrook | |
1523 | 07caea31 | Markus Armbruster | PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model, |
1524 | 07caea31 | Markus Armbruster | const char *default_devaddr) |
1525 | 07caea31 | Markus Armbruster | { |
1526 | 07caea31 | Markus Armbruster | PCIDevice *res; |
1527 | 07caea31 | Markus Armbruster | |
1528 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1529 | 07caea31 | Markus Armbruster | exit(0);
|
1530 | 07caea31 | Markus Armbruster | |
1531 | 07caea31 | Markus Armbruster | res = pci_nic_init(nd, default_model, default_devaddr); |
1532 | 07caea31 | Markus Armbruster | if (!res)
|
1533 | 07caea31 | Markus Armbruster | exit(1);
|
1534 | 07caea31 | Markus Armbruster | return res;
|
1535 | 07caea31 | Markus Armbruster | } |
1536 | 07caea31 | Markus Armbruster | |
1537 | 80b3ada7 | pbrook | typedef struct { |
1538 | 80b3ada7 | pbrook | PCIDevice dev; |
1539 | 03587182 | Gerd Hoffmann | PCIBus bus; |
1540 | 03587182 | Gerd Hoffmann | uint32_t vid; |
1541 | 03587182 | Gerd Hoffmann | uint32_t did; |
1542 | 80b3ada7 | pbrook | } PCIBridge; |
1543 | 80b3ada7 | pbrook | |
1544 | a0c7a97e | Isaku Yamahata | |
1545 | a0c7a97e | Isaku Yamahata | static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d) |
1546 | a0c7a97e | Isaku Yamahata | { |
1547 | a0c7a97e | Isaku Yamahata | pci_update_mappings(d); |
1548 | a0c7a97e | Isaku Yamahata | } |
1549 | a0c7a97e | Isaku Yamahata | |
1550 | a0c7a97e | Isaku Yamahata | static void pci_bridge_update_mappings(PCIBus *b) |
1551 | a0c7a97e | Isaku Yamahata | { |
1552 | a0c7a97e | Isaku Yamahata | PCIBus *child; |
1553 | a0c7a97e | Isaku Yamahata | |
1554 | a0c7a97e | Isaku Yamahata | pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn); |
1555 | a0c7a97e | Isaku Yamahata | |
1556 | a0c7a97e | Isaku Yamahata | QLIST_FOREACH(child, &b->child, sibling) { |
1557 | a0c7a97e | Isaku Yamahata | pci_bridge_update_mappings(child); |
1558 | a0c7a97e | Isaku Yamahata | } |
1559 | a0c7a97e | Isaku Yamahata | } |
1560 | a0c7a97e | Isaku Yamahata | |
1561 | 9596ebb7 | pbrook | static void pci_bridge_write_config(PCIDevice *d, |
1562 | 80b3ada7 | pbrook | uint32_t address, uint32_t val, int len)
|
1563 | 80b3ada7 | pbrook | { |
1564 | 80b3ada7 | pbrook | pci_default_write_config(d, address, val, len); |
1565 | a0c7a97e | Isaku Yamahata | |
1566 | a0c7a97e | Isaku Yamahata | if (/* io base/limit */ |
1567 | a0c7a97e | Isaku Yamahata | ranges_overlap(address, len, PCI_IO_BASE, 2) ||
|
1568 | a0c7a97e | Isaku Yamahata | |
1569 | a0c7a97e | Isaku Yamahata | /* memory base/limit, prefetchable base/limit and
|
1570 | a0c7a97e | Isaku Yamahata | io base/limit upper 16 */
|
1571 | a0c7a97e | Isaku Yamahata | ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
|
1572 | eb0557db | Michael S. Tsirkin | PCIBridge *s = container_of(d, PCIBridge, dev); |
1573 | eb0557db | Michael S. Tsirkin | PCIBus *secondary_bus = &s->bus; |
1574 | eb0557db | Michael S. Tsirkin | pci_bridge_update_mappings(secondary_bus); |
1575 | a0c7a97e | Isaku Yamahata | } |
1576 | 80b3ada7 | pbrook | } |
1577 | 80b3ada7 | pbrook | |
1578 | e822a52a | Isaku Yamahata | PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
|
1579 | 3ae80618 | aliguori | { |
1580 | 470e6363 | Isaku Yamahata | PCIBus *sec; |
1581 | 3ae80618 | aliguori | |
1582 | 470e6363 | Isaku Yamahata | if (!bus) {
|
1583 | e822a52a | Isaku Yamahata | return NULL; |
1584 | 470e6363 | Isaku Yamahata | } |
1585 | 3ae80618 | aliguori | |
1586 | e822a52a | Isaku Yamahata | if (pci_bus_num(bus) == bus_num) {
|
1587 | e822a52a | Isaku Yamahata | return bus;
|
1588 | e822a52a | Isaku Yamahata | } |
1589 | e822a52a | Isaku Yamahata | |
1590 | e822a52a | Isaku Yamahata | /* try child bus */
|
1591 | 470e6363 | Isaku Yamahata | if (!bus->parent_dev /* host pci bridge */ || |
1592 | 470e6363 | Isaku Yamahata | (bus->parent_dev->config[PCI_SECONDARY_BUS] < bus_num && |
1593 | 470e6363 | Isaku Yamahata | bus_num <= bus->parent_dev->config[PCI_SUBORDINATE_BUS])) { |
1594 | 470e6363 | Isaku Yamahata | for (; bus; bus = sec) {
|
1595 | 470e6363 | Isaku Yamahata | QLIST_FOREACH(sec, &bus->child, sibling) { |
1596 | 470e6363 | Isaku Yamahata | assert(sec->parent_dev); |
1597 | 470e6363 | Isaku Yamahata | if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
|
1598 | 470e6363 | Isaku Yamahata | return sec;
|
1599 | 470e6363 | Isaku Yamahata | } |
1600 | 470e6363 | Isaku Yamahata | if (sec->parent_dev->config[PCI_SECONDARY_BUS] < bus_num &&
|
1601 | 470e6363 | Isaku Yamahata | bus_num <= sec->parent_dev->config[PCI_SUBORDINATE_BUS]) { |
1602 | 470e6363 | Isaku Yamahata | break;
|
1603 | 470e6363 | Isaku Yamahata | } |
1604 | c021f8e6 | Blue Swirl | } |
1605 | e822a52a | Isaku Yamahata | } |
1606 | e822a52a | Isaku Yamahata | } |
1607 | e822a52a | Isaku Yamahata | |
1608 | e822a52a | Isaku Yamahata | return NULL; |
1609 | 3ae80618 | aliguori | } |
1610 | 3ae80618 | aliguori | |
1611 | e822a52a | Isaku Yamahata | PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function) |
1612 | 3ae80618 | aliguori | { |
1613 | e822a52a | Isaku Yamahata | bus = pci_find_bus(bus, bus_num); |
1614 | 3ae80618 | aliguori | |
1615 | 3ae80618 | aliguori | if (!bus)
|
1616 | 3ae80618 | aliguori | return NULL; |
1617 | 3ae80618 | aliguori | |
1618 | 3ae80618 | aliguori | return bus->devices[PCI_DEVFN(slot, function)];
|
1619 | 3ae80618 | aliguori | } |
1620 | 3ae80618 | aliguori | |
1621 | 03587182 | Gerd Hoffmann | static int pci_bridge_initfn(PCIDevice *dev) |
1622 | 80b3ada7 | pbrook | { |
1623 | 03587182 | Gerd Hoffmann | PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev); |
1624 | 480b9f24 | blueswir1 | |
1625 | 03587182 | Gerd Hoffmann | pci_config_set_vendor_id(s->dev.config, s->vid); |
1626 | 03587182 | Gerd Hoffmann | pci_config_set_device_id(s->dev.config, s->did); |
1627 | 480b9f24 | blueswir1 | |
1628 | 74c01823 | Isaku Yamahata | pci_set_word(dev->config + PCI_STATUS, |
1629 | 74c01823 | Isaku Yamahata | PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1630 | 74c01823 | Isaku Yamahata | pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI); |
1631 | e327e323 | Isaku Yamahata | dev->config[PCI_HEADER_TYPE] = |
1632 | e327e323 | Isaku Yamahata | (dev->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) | |
1633 | e327e323 | Isaku Yamahata | PCI_HEADER_TYPE_BRIDGE; |
1634 | 74c01823 | Isaku Yamahata | pci_set_word(dev->config + PCI_SEC_STATUS, |
1635 | 74c01823 | Isaku Yamahata | PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK); |
1636 | 03587182 | Gerd Hoffmann | return 0; |
1637 | 03587182 | Gerd Hoffmann | } |
1638 | 80b3ada7 | pbrook | |
1639 | e822a52a | Isaku Yamahata | static int pci_bridge_exitfn(PCIDevice *pci_dev) |
1640 | e822a52a | Isaku Yamahata | { |
1641 | e822a52a | Isaku Yamahata | PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev); |
1642 | e822a52a | Isaku Yamahata | PCIBus *bus = &s->bus; |
1643 | e822a52a | Isaku Yamahata | pci_unregister_secondary_bus(bus); |
1644 | e822a52a | Isaku Yamahata | return 0; |
1645 | e822a52a | Isaku Yamahata | } |
1646 | e822a52a | Isaku Yamahata | |
1647 | 7c7b829e | Isaku Yamahata | PCIBus *pci_bridge_init(PCIBus *bus, int devfn, bool multifunction, |
1648 | 7c7b829e | Isaku Yamahata | uint16_t vid, uint16_t did, |
1649 | 03587182 | Gerd Hoffmann | pci_map_irq_fn map_irq, const char *name) |
1650 | 03587182 | Gerd Hoffmann | { |
1651 | 03587182 | Gerd Hoffmann | PCIDevice *dev; |
1652 | 03587182 | Gerd Hoffmann | PCIBridge *s; |
1653 | 03587182 | Gerd Hoffmann | |
1654 | 7c7b829e | Isaku Yamahata | dev = pci_create_multifunction(bus, devfn, multifunction, "pci-bridge");
|
1655 | 03587182 | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
|
1656 | 03587182 | Gerd Hoffmann | qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
|
1657 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1658 | 03587182 | Gerd Hoffmann | |
1659 | 03587182 | Gerd Hoffmann | s = DO_UPCAST(PCIBridge, dev, dev); |
1660 | e822a52a | Isaku Yamahata | pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name); |
1661 | 03587182 | Gerd Hoffmann | return &s->bus;
|
1662 | 80b3ada7 | pbrook | } |
1663 | 6b1b92d3 | Paul Brook | |
1664 | d6318738 | Michael S. Tsirkin | PCIDevice *pci_bridge_get_device(PCIBus *bus) |
1665 | d6318738 | Michael S. Tsirkin | { |
1666 | d6318738 | Michael S. Tsirkin | return bus->parent_dev;
|
1667 | d6318738 | Michael S. Tsirkin | } |
1668 | d6318738 | Michael S. Tsirkin | |
1669 | 81a322d4 | Gerd Hoffmann | static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) |
1670 | 6b1b92d3 | Paul Brook | { |
1671 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev = (PCIDevice *)qdev; |
1672 | 02e2da45 | Paul Brook | PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev); |
1673 | 6b1b92d3 | Paul Brook | PCIBus *bus; |
1674 | ee995ffb | Gerd Hoffmann | int devfn, rc;
|
1675 | 6b1b92d3 | Paul Brook | |
1676 | a9f49946 | Isaku Yamahata | /* initialize cap_present for pci_is_express() and pci_config_size() */
|
1677 | a9f49946 | Isaku Yamahata | if (info->is_express) {
|
1678 | a9f49946 | Isaku Yamahata | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1679 | a9f49946 | Isaku Yamahata | } |
1680 | a9f49946 | Isaku Yamahata | |
1681 | 02e2da45 | Paul Brook | bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev)); |
1682 | ee6847d1 | Gerd Hoffmann | devfn = pci_dev->devfn; |
1683 | 16eaedf2 | Gerd Hoffmann | pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn, |
1684 | fb231628 | Isaku Yamahata | info->config_read, info->config_write, |
1685 | e327e323 | Isaku Yamahata | info->is_bridge); |
1686 | 09e3acc6 | Gerd Hoffmann | if (pci_dev == NULL) |
1687 | 09e3acc6 | Gerd Hoffmann | return -1; |
1688 | ee995ffb | Gerd Hoffmann | rc = info->init(pci_dev); |
1689 | 925fe64a | Alex Williamson | if (rc != 0) { |
1690 | 925fe64a | Alex Williamson | do_pci_unregister_device(pci_dev); |
1691 | ee995ffb | Gerd Hoffmann | return rc;
|
1692 | 925fe64a | Alex Williamson | } |
1693 | 8c52c8f3 | Gerd Hoffmann | |
1694 | 8c52c8f3 | Gerd Hoffmann | /* rom loading */
|
1695 | 8c52c8f3 | Gerd Hoffmann | if (pci_dev->romfile == NULL && info->romfile != NULL) |
1696 | 8c52c8f3 | Gerd Hoffmann | pci_dev->romfile = qemu_strdup(info->romfile); |
1697 | 8c52c8f3 | Gerd Hoffmann | pci_add_option_rom(pci_dev); |
1698 | 8c52c8f3 | Gerd Hoffmann | |
1699 | a213ff63 | Isaku Yamahata | if (qdev->hotplugged) {
|
1700 | a213ff63 | Isaku Yamahata | rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
|
1701 | a213ff63 | Isaku Yamahata | if (rc != 0) { |
1702 | a213ff63 | Isaku Yamahata | int r = pci_unregister_device(&pci_dev->qdev);
|
1703 | a213ff63 | Isaku Yamahata | assert(!r); |
1704 | a213ff63 | Isaku Yamahata | return rc;
|
1705 | a213ff63 | Isaku Yamahata | } |
1706 | a213ff63 | Isaku Yamahata | } |
1707 | ee995ffb | Gerd Hoffmann | return 0; |
1708 | ee995ffb | Gerd Hoffmann | } |
1709 | ee995ffb | Gerd Hoffmann | |
1710 | ee995ffb | Gerd Hoffmann | static int pci_unplug_device(DeviceState *qdev) |
1711 | ee995ffb | Gerd Hoffmann | { |
1712 | ee995ffb | Gerd Hoffmann | PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev); |
1713 | ee995ffb | Gerd Hoffmann | |
1714 | a213ff63 | Isaku Yamahata | return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0); |
1715 | 6b1b92d3 | Paul Brook | } |
1716 | 6b1b92d3 | Paul Brook | |
1717 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register(PCIDeviceInfo *info)
|
1718 | 6b1b92d3 | Paul Brook | { |
1719 | 02e2da45 | Paul Brook | info->qdev.init = pci_qdev_init; |
1720 | ee995ffb | Gerd Hoffmann | info->qdev.unplug = pci_unplug_device; |
1721 | a36a344d | Gerd Hoffmann | info->qdev.exit = pci_unregister_device; |
1722 | 10c4c98a | Gerd Hoffmann | info->qdev.bus_info = &pci_bus_info; |
1723 | 074f2fff | Gerd Hoffmann | qdev_register(&info->qdev); |
1724 | 6b1b92d3 | Paul Brook | } |
1725 | 6b1b92d3 | Paul Brook | |
1726 | 0aab0d3a | Gerd Hoffmann | void pci_qdev_register_many(PCIDeviceInfo *info)
|
1727 | 0aab0d3a | Gerd Hoffmann | { |
1728 | 0aab0d3a | Gerd Hoffmann | while (info->qdev.name) {
|
1729 | 0aab0d3a | Gerd Hoffmann | pci_qdev_register(info); |
1730 | 0aab0d3a | Gerd Hoffmann | info++; |
1731 | 0aab0d3a | Gerd Hoffmann | } |
1732 | 0aab0d3a | Gerd Hoffmann | } |
1733 | 0aab0d3a | Gerd Hoffmann | |
1734 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction, |
1735 | 49823868 | Isaku Yamahata | const char *name) |
1736 | 6b1b92d3 | Paul Brook | { |
1737 | 6b1b92d3 | Paul Brook | DeviceState *dev; |
1738 | 6b1b92d3 | Paul Brook | |
1739 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
1740 | a6307b08 | Gerd Hoffmann | qdev_prop_set_uint32(dev, "addr", devfn);
|
1741 | 49823868 | Isaku Yamahata | qdev_prop_set_bit(dev, "multifunction", multifunction);
|
1742 | 71077c1c | Gerd Hoffmann | return DO_UPCAST(PCIDevice, qdev, dev);
|
1743 | 71077c1c | Gerd Hoffmann | } |
1744 | 6b1b92d3 | Paul Brook | |
1745 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
|
1746 | 49823868 | Isaku Yamahata | bool multifunction,
|
1747 | 49823868 | Isaku Yamahata | const char *name) |
1748 | 71077c1c | Gerd Hoffmann | { |
1749 | 49823868 | Isaku Yamahata | PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name); |
1750 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1751 | 71077c1c | Gerd Hoffmann | return dev;
|
1752 | 6b1b92d3 | Paul Brook | } |
1753 | 6f4cbd39 | Michael S. Tsirkin | |
1754 | 49823868 | Isaku Yamahata | PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1755 | 49823868 | Isaku Yamahata | { |
1756 | 49823868 | Isaku Yamahata | return pci_create_multifunction(bus, devfn, false, name); |
1757 | 49823868 | Isaku Yamahata | } |
1758 | 49823868 | Isaku Yamahata | |
1759 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1760 | 49823868 | Isaku Yamahata | { |
1761 | 49823868 | Isaku Yamahata | return pci_create_simple_multifunction(bus, devfn, false, name); |
1762 | 49823868 | Isaku Yamahata | } |
1763 | 49823868 | Isaku Yamahata | |
1764 | 6f4cbd39 | Michael S. Tsirkin | static int pci_find_space(PCIDevice *pdev, uint8_t size) |
1765 | 6f4cbd39 | Michael S. Tsirkin | { |
1766 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pdev);
|
1767 | 6f4cbd39 | Michael S. Tsirkin | int offset = PCI_CONFIG_HEADER_SIZE;
|
1768 | 6f4cbd39 | Michael S. Tsirkin | int i;
|
1769 | a9f49946 | Isaku Yamahata | for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
|
1770 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->used[i])
|
1771 | 6f4cbd39 | Michael S. Tsirkin | offset = i + 1;
|
1772 | 6f4cbd39 | Michael S. Tsirkin | else if (i - offset + 1 == size) |
1773 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1774 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1775 | 6f4cbd39 | Michael S. Tsirkin | } |
1776 | 6f4cbd39 | Michael S. Tsirkin | |
1777 | 6f4cbd39 | Michael S. Tsirkin | static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1778 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *prev_p) |
1779 | 6f4cbd39 | Michael S. Tsirkin | { |
1780 | 6f4cbd39 | Michael S. Tsirkin | uint8_t next, prev; |
1781 | 6f4cbd39 | Michael S. Tsirkin | |
1782 | 6f4cbd39 | Michael S. Tsirkin | if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1783 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1784 | 6f4cbd39 | Michael S. Tsirkin | |
1785 | 6f4cbd39 | Michael S. Tsirkin | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1786 | 6f4cbd39 | Michael S. Tsirkin | prev = next + PCI_CAP_LIST_NEXT) |
1787 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1788 | 6f4cbd39 | Michael S. Tsirkin | break;
|
1789 | 6f4cbd39 | Michael S. Tsirkin | |
1790 | 6f4cbd39 | Michael S. Tsirkin | if (prev_p)
|
1791 | 6f4cbd39 | Michael S. Tsirkin | *prev_p = prev; |
1792 | 6f4cbd39 | Michael S. Tsirkin | return next;
|
1793 | 6f4cbd39 | Michael S. Tsirkin | } |
1794 | 6f4cbd39 | Michael S. Tsirkin | |
1795 | c2039bd0 | Anthony Liguori | static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type) |
1796 | c2039bd0 | Anthony Liguori | { |
1797 | c2039bd0 | Anthony Liguori | cpu_register_physical_memory(addr, size, pdev->rom_offset); |
1798 | c2039bd0 | Anthony Liguori | } |
1799 | c2039bd0 | Anthony Liguori | |
1800 | c2039bd0 | Anthony Liguori | /* Add an option rom for the device */
|
1801 | 8c52c8f3 | Gerd Hoffmann | static int pci_add_option_rom(PCIDevice *pdev) |
1802 | c2039bd0 | Anthony Liguori | { |
1803 | c2039bd0 | Anthony Liguori | int size;
|
1804 | c2039bd0 | Anthony Liguori | char *path;
|
1805 | c2039bd0 | Anthony Liguori | void *ptr;
|
1806 | 1724f049 | Alex Williamson | char name[32]; |
1807 | c2039bd0 | Anthony Liguori | |
1808 | 8c52c8f3 | Gerd Hoffmann | if (!pdev->romfile)
|
1809 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1810 | 8c52c8f3 | Gerd Hoffmann | if (strlen(pdev->romfile) == 0) |
1811 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1812 | 8c52c8f3 | Gerd Hoffmann | |
1813 | 88169ddf | Gerd Hoffmann | if (!pdev->rom_bar) {
|
1814 | 88169ddf | Gerd Hoffmann | /*
|
1815 | 88169ddf | Gerd Hoffmann | * Load rom via fw_cfg instead of creating a rom bar,
|
1816 | 88169ddf | Gerd Hoffmann | * for 0.11 compatibility.
|
1817 | 88169ddf | Gerd Hoffmann | */
|
1818 | 88169ddf | Gerd Hoffmann | int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
|
1819 | 88169ddf | Gerd Hoffmann | if (class == 0x0300) { |
1820 | 88169ddf | Gerd Hoffmann | rom_add_vga(pdev->romfile); |
1821 | 88169ddf | Gerd Hoffmann | } else {
|
1822 | 88169ddf | Gerd Hoffmann | rom_add_option(pdev->romfile); |
1823 | 88169ddf | Gerd Hoffmann | } |
1824 | 88169ddf | Gerd Hoffmann | return 0; |
1825 | 88169ddf | Gerd Hoffmann | } |
1826 | 88169ddf | Gerd Hoffmann | |
1827 | 8c52c8f3 | Gerd Hoffmann | path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); |
1828 | c2039bd0 | Anthony Liguori | if (path == NULL) { |
1829 | 8c52c8f3 | Gerd Hoffmann | path = qemu_strdup(pdev->romfile); |
1830 | c2039bd0 | Anthony Liguori | } |
1831 | c2039bd0 | Anthony Liguori | |
1832 | c2039bd0 | Anthony Liguori | size = get_image_size(path); |
1833 | 8c52c8f3 | Gerd Hoffmann | if (size < 0) { |
1834 | 1ecda02b | Markus Armbruster | error_report("%s: failed to find romfile \"%s\"",
|
1835 | 1ecda02b | Markus Armbruster | __FUNCTION__, pdev->romfile); |
1836 | 8c52c8f3 | Gerd Hoffmann | return -1; |
1837 | 8c52c8f3 | Gerd Hoffmann | } |
1838 | c2039bd0 | Anthony Liguori | if (size & (size - 1)) { |
1839 | c2039bd0 | Anthony Liguori | size = 1 << qemu_fls(size);
|
1840 | c2039bd0 | Anthony Liguori | } |
1841 | c2039bd0 | Anthony Liguori | |
1842 | 1724f049 | Alex Williamson | if (pdev->qdev.info->vmsd)
|
1843 | 1724f049 | Alex Williamson | snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name); |
1844 | 1724f049 | Alex Williamson | else
|
1845 | 1724f049 | Alex Williamson | snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name); |
1846 | 1724f049 | Alex Williamson | pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size); |
1847 | c2039bd0 | Anthony Liguori | |
1848 | c2039bd0 | Anthony Liguori | ptr = qemu_get_ram_ptr(pdev->rom_offset); |
1849 | c2039bd0 | Anthony Liguori | load_image(path, ptr); |
1850 | c2039bd0 | Anthony Liguori | qemu_free(path); |
1851 | c2039bd0 | Anthony Liguori | |
1852 | c2039bd0 | Anthony Liguori | pci_register_bar(pdev, PCI_ROM_SLOT, size, |
1853 | c2039bd0 | Anthony Liguori | 0, pci_map_option_rom);
|
1854 | c2039bd0 | Anthony Liguori | |
1855 | c2039bd0 | Anthony Liguori | return 0; |
1856 | c2039bd0 | Anthony Liguori | } |
1857 | c2039bd0 | Anthony Liguori | |
1858 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev) |
1859 | 230741dc | Alex Williamson | { |
1860 | 230741dc | Alex Williamson | if (!pdev->rom_offset)
|
1861 | 230741dc | Alex Williamson | return;
|
1862 | 230741dc | Alex Williamson | |
1863 | 230741dc | Alex Williamson | qemu_ram_free(pdev->rom_offset); |
1864 | 230741dc | Alex Williamson | pdev->rom_offset = 0;
|
1865 | 230741dc | Alex Williamson | } |
1866 | 230741dc | Alex Williamson | |
1867 | 6f4cbd39 | Michael S. Tsirkin | /* Reserve space and add capability to the linked list in pci config space */
|
1868 | 1db5a3aa | Michael S. Tsirkin | int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
|
1869 | 1db5a3aa | Michael S. Tsirkin | uint8_t offset, uint8_t size) |
1870 | 6f4cbd39 | Michael S. Tsirkin | { |
1871 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *config = pdev->config + offset; |
1872 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_ID] = cap_id; |
1873 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
1874 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_CAPABILITY_LIST] = offset; |
1875 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
1876 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xFF, size);
|
1877 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability read-only by default */
|
1878 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0, size);
|
1879 | bd4b65ee | Michael S. Tsirkin | /* Check capability by default */
|
1880 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0xFF, size);
|
1881 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1882 | 6f4cbd39 | Michael S. Tsirkin | } |
1883 | 6f4cbd39 | Michael S. Tsirkin | |
1884 | 1db5a3aa | Michael S. Tsirkin | /* Find and reserve space and add capability to the linked list
|
1885 | 1db5a3aa | Michael S. Tsirkin | * in pci config space */
|
1886 | 1db5a3aa | Michael S. Tsirkin | int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1887 | 1db5a3aa | Michael S. Tsirkin | { |
1888 | 1db5a3aa | Michael S. Tsirkin | uint8_t offset = pci_find_space(pdev, size); |
1889 | 1db5a3aa | Michael S. Tsirkin | if (!offset) {
|
1890 | 1db5a3aa | Michael S. Tsirkin | return -ENOSPC;
|
1891 | 1db5a3aa | Michael S. Tsirkin | } |
1892 | 1db5a3aa | Michael S. Tsirkin | return pci_add_capability_at_offset(pdev, cap_id, offset, size);
|
1893 | 1db5a3aa | Michael S. Tsirkin | } |
1894 | 1db5a3aa | Michael S. Tsirkin | |
1895 | 6f4cbd39 | Michael S. Tsirkin | /* Unlink capability from the pci config space. */
|
1896 | 6f4cbd39 | Michael S. Tsirkin | void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
1897 | 6f4cbd39 | Michael S. Tsirkin | { |
1898 | 6f4cbd39 | Michael S. Tsirkin | uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
1899 | 6f4cbd39 | Michael S. Tsirkin | if (!offset)
|
1900 | 6f4cbd39 | Michael S. Tsirkin | return;
|
1901 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
1902 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability writeable again */
|
1903 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0xff, size);
|
1904 | bd4b65ee | Michael S. Tsirkin | /* Clear cmask as device-specific registers can't be checked */
|
1905 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0, size);
|
1906 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0, size);
|
1907 | 6f4cbd39 | Michael S. Tsirkin | |
1908 | 6f4cbd39 | Michael S. Tsirkin | if (!pdev->config[PCI_CAPABILITY_LIST])
|
1909 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
1910 | 6f4cbd39 | Michael S. Tsirkin | } |
1911 | 6f4cbd39 | Michael S. Tsirkin | |
1912 | 6f4cbd39 | Michael S. Tsirkin | /* Reserve space for capability at a known offset (to call after load). */
|
1913 | 6f4cbd39 | Michael S. Tsirkin | void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
|
1914 | 6f4cbd39 | Michael S. Tsirkin | { |
1915 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->used + offset, 0xff, size);
|
1916 | 6f4cbd39 | Michael S. Tsirkin | } |
1917 | 6f4cbd39 | Michael S. Tsirkin | |
1918 | 6f4cbd39 | Michael S. Tsirkin | uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
1919 | 6f4cbd39 | Michael S. Tsirkin | { |
1920 | 6f4cbd39 | Michael S. Tsirkin | return pci_find_capability_list(pdev, cap_id, NULL); |
1921 | 6f4cbd39 | Michael S. Tsirkin | } |
1922 | 10c4c98a | Gerd Hoffmann | |
1923 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
1924 | 10c4c98a | Gerd Hoffmann | { |
1925 | 10c4c98a | Gerd Hoffmann | PCIDevice *d = (PCIDevice *)dev; |
1926 | 10c4c98a | Gerd Hoffmann | const pci_class_desc *desc;
|
1927 | 10c4c98a | Gerd Hoffmann | char ctxt[64]; |
1928 | 10c4c98a | Gerd Hoffmann | PCIIORegion *r; |
1929 | 10c4c98a | Gerd Hoffmann | int i, class;
|
1930 | 10c4c98a | Gerd Hoffmann | |
1931 | b0ff8eb2 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
1932 | 10c4c98a | Gerd Hoffmann | desc = pci_class_descriptions; |
1933 | 10c4c98a | Gerd Hoffmann | while (desc->desc && class != desc->class)
|
1934 | 10c4c98a | Gerd Hoffmann | desc++; |
1935 | 10c4c98a | Gerd Hoffmann | if (desc->desc) {
|
1936 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
1937 | 10c4c98a | Gerd Hoffmann | } else {
|
1938 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
1939 | 10c4c98a | Gerd Hoffmann | } |
1940 | 10c4c98a | Gerd Hoffmann | |
1941 | 10c4c98a | Gerd Hoffmann | monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
1942 | 10c4c98a | Gerd Hoffmann | "pci id %04x:%04x (sub %04x:%04x)\n",
|
1943 | 10c4c98a | Gerd Hoffmann | indent, "", ctxt,
|
1944 | e822a52a | Isaku Yamahata | d->config[PCI_SECONDARY_BUS], |
1945 | e822a52a | Isaku Yamahata | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
1946 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_VENDOR_ID), |
1947 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_DEVICE_ID), |
1948 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
1949 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
1950 | 10c4c98a | Gerd Hoffmann | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1951 | 10c4c98a | Gerd Hoffmann | r = &d->io_regions[i]; |
1952 | 10c4c98a | Gerd Hoffmann | if (!r->size)
|
1953 | 10c4c98a | Gerd Hoffmann | continue;
|
1954 | 89e8b13c | Isaku Yamahata | monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
1955 | 89e8b13c | Isaku Yamahata | " [0x%"FMT_PCIBUS"]\n", |
1956 | 89e8b13c | Isaku Yamahata | indent, "",
|
1957 | 0392a017 | Isaku Yamahata | i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
1958 | 10c4c98a | Gerd Hoffmann | r->addr, r->addr + r->size - 1);
|
1959 | 10c4c98a | Gerd Hoffmann | } |
1960 | 10c4c98a | Gerd Hoffmann | } |
1961 | 03587182 | Gerd Hoffmann | |
1962 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev) |
1963 | 4f43c1ff | Alex Williamson | { |
1964 | 4f43c1ff | Alex Williamson | PCIDevice *d = (PCIDevice *)dev; |
1965 | 4f43c1ff | Alex Williamson | char path[16]; |
1966 | 4f43c1ff | Alex Williamson | |
1967 | 4f43c1ff | Alex Williamson | snprintf(path, sizeof(path), "%04x:%02x:%02x.%x", |
1968 | 4f43c1ff | Alex Williamson | pci_find_domain(d->bus), d->config[PCI_SECONDARY_BUS], |
1969 | 4f43c1ff | Alex Williamson | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn)); |
1970 | 4f43c1ff | Alex Williamson | |
1971 | 4f43c1ff | Alex Williamson | return strdup(path);
|
1972 | 4f43c1ff | Alex Williamson | } |
1973 | 4f43c1ff | Alex Williamson | |
1974 | 03587182 | Gerd Hoffmann | static PCIDeviceInfo bridge_info = {
|
1975 | 03587182 | Gerd Hoffmann | .qdev.name = "pci-bridge",
|
1976 | 03587182 | Gerd Hoffmann | .qdev.size = sizeof(PCIBridge),
|
1977 | 03587182 | Gerd Hoffmann | .init = pci_bridge_initfn, |
1978 | e822a52a | Isaku Yamahata | .exit = pci_bridge_exitfn, |
1979 | 03587182 | Gerd Hoffmann | .config_write = pci_bridge_write_config, |
1980 | e327e323 | Isaku Yamahata | .is_bridge = 1,
|
1981 | 03587182 | Gerd Hoffmann | .qdev.props = (Property[]) { |
1982 | 03587182 | Gerd Hoffmann | DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0), |
1983 | 03587182 | Gerd Hoffmann | DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0), |
1984 | 03587182 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
1985 | 03587182 | Gerd Hoffmann | } |
1986 | 03587182 | Gerd Hoffmann | }; |
1987 | 03587182 | Gerd Hoffmann | |
1988 | 03587182 | Gerd Hoffmann | static void pci_register_devices(void) |
1989 | 03587182 | Gerd Hoffmann | { |
1990 | 03587182 | Gerd Hoffmann | pci_qdev_register(&bridge_info); |
1991 | 03587182 | Gerd Hoffmann | } |
1992 | 03587182 | Gerd Hoffmann | |
1993 | 03587182 | Gerd Hoffmann | device_init(pci_register_devices) |