root / hw / pci / pci.c @ 9bc47305
History | View | Annotate | Download (66.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 | c759b24f | Michael S. Tsirkin | #include "hw/hw.h" |
25 | c759b24f | Michael S. Tsirkin | #include "hw/pci/pci.h" |
26 | c759b24f | Michael S. Tsirkin | #include "hw/pci/pci_bridge.h" |
27 | 06aac7bd | Michael S. Tsirkin | #include "hw/pci/pci_bus.h" |
28 | 568f0690 | David Gibson | #include "hw/pci/pci_host.h" |
29 | 83c9089e | Paolo Bonzini | #include "monitor/monitor.h" |
30 | 1422e32d | Paolo Bonzini | #include "net/net.h" |
31 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
32 | c759b24f | Michael S. Tsirkin | #include "hw/loader.h" |
33 | 1de7afc9 | Paolo Bonzini | #include "qemu/range.h" |
34 | 79627472 | Luiz Capitulino | #include "qmp-commands.h" |
35 | c759b24f | Michael S. Tsirkin | #include "hw/pci/msi.h" |
36 | c759b24f | Michael S. Tsirkin | #include "hw/pci/msix.h" |
37 | 022c62cb | Paolo Bonzini | #include "exec/address-spaces.h" |
38 | 69b91039 | bellard | |
39 | 69b91039 | bellard | //#define DEBUG_PCI
|
40 | d8d2e079 | Isaku Yamahata | #ifdef DEBUG_PCI
|
41 | 2e49d64a | Isaku Yamahata | # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
42 | d8d2e079 | Isaku Yamahata | #else
|
43 | d8d2e079 | Isaku Yamahata | # define PCI_DPRINTF(format, ...) do { } while (0) |
44 | d8d2e079 | Isaku Yamahata | #endif
|
45 | 69b91039 | bellard | |
46 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
47 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev); |
48 | 5e0259e7 | Gleb Natapov | static char *pcibus_get_fw_dev_path(DeviceState *dev); |
49 | 9bb33586 | Isaku Yamahata | static int pcibus_reset(BusState *qbus); |
50 | 10c4c98a | Gerd Hoffmann | |
51 | 3cb75a7c | Paolo Bonzini | static Property pci_props[] = {
|
52 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), |
53 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
|
54 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), |
55 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
|
56 | 3cb75a7c | Paolo Bonzini | QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
|
57 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
|
58 | 3cb75a7c | Paolo Bonzini | QEMU_PCI_CAP_SERR_BITNR, true),
|
59 | 3cb75a7c | Paolo Bonzini | DEFINE_PROP_END_OF_LIST() |
60 | 3cb75a7c | Paolo Bonzini | }; |
61 | 3cb75a7c | Paolo Bonzini | |
62 | 0d936928 | Anthony Liguori | static void pci_bus_class_init(ObjectClass *klass, void *data) |
63 | 0d936928 | Anthony Liguori | { |
64 | 0d936928 | Anthony Liguori | BusClass *k = BUS_CLASS(klass); |
65 | 0d936928 | Anthony Liguori | |
66 | 0d936928 | Anthony Liguori | k->print_dev = pcibus_dev_print; |
67 | 0d936928 | Anthony Liguori | k->get_dev_path = pcibus_get_dev_path; |
68 | 0d936928 | Anthony Liguori | k->get_fw_dev_path = pcibus_get_fw_dev_path; |
69 | 0d936928 | Anthony Liguori | k->reset = pcibus_reset; |
70 | 0d936928 | Anthony Liguori | } |
71 | 0d936928 | Anthony Liguori | |
72 | 0d936928 | Anthony Liguori | static const TypeInfo pci_bus_info = { |
73 | 0d936928 | Anthony Liguori | .name = TYPE_PCI_BUS, |
74 | 0d936928 | Anthony Liguori | .parent = TYPE_BUS, |
75 | 0d936928 | Anthony Liguori | .instance_size = sizeof(PCIBus),
|
76 | 0d936928 | Anthony Liguori | .class_init = pci_bus_class_init, |
77 | 30468f78 | bellard | }; |
78 | 69b91039 | bellard | |
79 | 3a861c46 | Alex Williamson | static const TypeInfo pcie_bus_info = { |
80 | 3a861c46 | Alex Williamson | .name = TYPE_PCIE_BUS, |
81 | 3a861c46 | Alex Williamson | .parent = TYPE_PCI_BUS, |
82 | 3a861c46 | Alex Williamson | }; |
83 | 3a861c46 | Alex Williamson | |
84 | d662210a | Michael S. Tsirkin | static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num); |
85 | 1941d19c | bellard | static void pci_update_mappings(PCIDevice *d); |
86 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level); |
87 | ab85ceb1 | Stefan Weil | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom); |
88 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev); |
89 | 1941d19c | bellard | |
90 | d350d97d | aliguori | static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
|
91 | d350d97d | aliguori | static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
|
92 | e822a52a | Isaku Yamahata | |
93 | e822a52a | Isaku Yamahata | struct PCIHostBus {
|
94 | e822a52a | Isaku Yamahata | int domain;
|
95 | e822a52a | Isaku Yamahata | struct PCIBus *bus;
|
96 | e822a52a | Isaku Yamahata | QLIST_ENTRY(PCIHostBus) next; |
97 | e822a52a | Isaku Yamahata | }; |
98 | e822a52a | Isaku Yamahata | static QLIST_HEAD(, PCIHostBus) host_buses;
|
99 | 30468f78 | bellard | |
100 | 2d1e9f96 | Juan Quintela | static const VMStateDescription vmstate_pcibus = { |
101 | 2d1e9f96 | Juan Quintela | .name = "PCIBUS",
|
102 | 2d1e9f96 | Juan Quintela | .version_id = 1,
|
103 | 2d1e9f96 | Juan Quintela | .minimum_version_id = 1,
|
104 | 2d1e9f96 | Juan Quintela | .minimum_version_id_old = 1,
|
105 | 2d1e9f96 | Juan Quintela | .fields = (VMStateField []) { |
106 | 2d1e9f96 | Juan Quintela | VMSTATE_INT32_EQUAL(nirq, PCIBus), |
107 | c7bde572 | Juan Quintela | VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
|
108 | 2d1e9f96 | Juan Quintela | VMSTATE_END_OF_LIST() |
109 | 52fc1d83 | balrog | } |
110 | 2d1e9f96 | Juan Quintela | }; |
111 | b3b11697 | Isaku Yamahata | static int pci_bar(PCIDevice *d, int reg) |
112 | 5330de09 | Michael S. Tsirkin | { |
113 | b3b11697 | Isaku Yamahata | uint8_t type; |
114 | b3b11697 | Isaku Yamahata | |
115 | b3b11697 | Isaku Yamahata | if (reg != PCI_ROM_SLOT)
|
116 | b3b11697 | Isaku Yamahata | return PCI_BASE_ADDRESS_0 + reg * 4; |
117 | b3b11697 | Isaku Yamahata | |
118 | b3b11697 | Isaku Yamahata | type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
119 | b3b11697 | Isaku Yamahata | return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
|
120 | 5330de09 | Michael S. Tsirkin | } |
121 | 5330de09 | Michael S. Tsirkin | |
122 | d036bb21 | Michael S. Tsirkin | static inline int pci_irq_state(PCIDevice *d, int irq_num) |
123 | d036bb21 | Michael S. Tsirkin | { |
124 | d036bb21 | Michael S. Tsirkin | return (d->irq_state >> irq_num) & 0x1; |
125 | d036bb21 | Michael S. Tsirkin | } |
126 | d036bb21 | Michael S. Tsirkin | |
127 | d036bb21 | Michael S. Tsirkin | static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) |
128 | d036bb21 | Michael S. Tsirkin | { |
129 | d036bb21 | Michael S. Tsirkin | d->irq_state &= ~(0x1 << irq_num);
|
130 | d036bb21 | Michael S. Tsirkin | d->irq_state |= level << irq_num; |
131 | d036bb21 | Michael S. Tsirkin | } |
132 | d036bb21 | Michael S. Tsirkin | |
133 | d036bb21 | Michael S. Tsirkin | static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) |
134 | d036bb21 | Michael S. Tsirkin | { |
135 | d036bb21 | Michael S. Tsirkin | PCIBus *bus; |
136 | d036bb21 | Michael S. Tsirkin | for (;;) {
|
137 | d036bb21 | Michael S. Tsirkin | bus = pci_dev->bus; |
138 | d036bb21 | Michael S. Tsirkin | irq_num = bus->map_irq(pci_dev, irq_num); |
139 | d036bb21 | Michael S. Tsirkin | if (bus->set_irq)
|
140 | d036bb21 | Michael S. Tsirkin | break;
|
141 | d036bb21 | Michael S. Tsirkin | pci_dev = bus->parent_dev; |
142 | d036bb21 | Michael S. Tsirkin | } |
143 | d036bb21 | Michael S. Tsirkin | bus->irq_count[irq_num] += change; |
144 | d036bb21 | Michael S. Tsirkin | bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
|
145 | d036bb21 | Michael S. Tsirkin | } |
146 | d036bb21 | Michael S. Tsirkin | |
147 | 9ddf8437 | Isaku Yamahata | int pci_bus_get_irq_level(PCIBus *bus, int irq_num) |
148 | 9ddf8437 | Isaku Yamahata | { |
149 | 9ddf8437 | Isaku Yamahata | assert(irq_num >= 0);
|
150 | 9ddf8437 | Isaku Yamahata | assert(irq_num < bus->nirq); |
151 | 9ddf8437 | Isaku Yamahata | return !!bus->irq_count[irq_num];
|
152 | 9ddf8437 | Isaku Yamahata | } |
153 | 9ddf8437 | Isaku Yamahata | |
154 | f9bf77dd | Michael S. Tsirkin | /* Update interrupt status bit in config space on interrupt
|
155 | f9bf77dd | Michael S. Tsirkin | * state change. */
|
156 | f9bf77dd | Michael S. Tsirkin | static void pci_update_irq_status(PCIDevice *dev) |
157 | f9bf77dd | Michael S. Tsirkin | { |
158 | f9bf77dd | Michael S. Tsirkin | if (dev->irq_state) {
|
159 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; |
160 | f9bf77dd | Michael S. Tsirkin | } else {
|
161 | f9bf77dd | Michael S. Tsirkin | dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
162 | f9bf77dd | Michael S. Tsirkin | } |
163 | f9bf77dd | Michael S. Tsirkin | } |
164 | f9bf77dd | Michael S. Tsirkin | |
165 | 4c92325b | Isaku Yamahata | void pci_device_deassert_intx(PCIDevice *dev)
|
166 | 4c92325b | Isaku Yamahata | { |
167 | 4c92325b | Isaku Yamahata | int i;
|
168 | 4c92325b | Isaku Yamahata | for (i = 0; i < PCI_NUM_PINS; ++i) { |
169 | 4c92325b | Isaku Yamahata | qemu_set_irq(dev->irq[i], 0);
|
170 | 4c92325b | Isaku Yamahata | } |
171 | 4c92325b | Isaku Yamahata | } |
172 | 4c92325b | Isaku Yamahata | |
173 | 0ead87c8 | Isaku Yamahata | /*
|
174 | 0ead87c8 | Isaku Yamahata | * This function is called on #RST and FLR.
|
175 | 0ead87c8 | Isaku Yamahata | * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
|
176 | 0ead87c8 | Isaku Yamahata | */
|
177 | 0ead87c8 | Isaku Yamahata | void pci_device_reset(PCIDevice *dev)
|
178 | 5330de09 | Michael S. Tsirkin | { |
179 | c0b1905b | Michael S. Tsirkin | int r;
|
180 | 6fc4925b | Anthony Liguori | |
181 | 6fc4925b | Anthony Liguori | qdev_reset_all(&dev->qdev); |
182 | c0b1905b | Michael S. Tsirkin | |
183 | d036bb21 | Michael S. Tsirkin | dev->irq_state = 0;
|
184 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(dev); |
185 | 4c92325b | Isaku Yamahata | pci_device_deassert_intx(dev); |
186 | ebabb67a | Stefan Weil | /* Clear all writable bits */
|
187 | 99443c21 | Isaku Yamahata | pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, |
188 | f9aebe2e | Michael S. Tsirkin | pci_get_word(dev->wmask + PCI_COMMAND) | |
189 | f9aebe2e | Michael S. Tsirkin | pci_get_word(dev->w1cmask + PCI_COMMAND)); |
190 | 89d437df | Isaku Yamahata | pci_word_test_and_clear_mask(dev->config + PCI_STATUS, |
191 | 89d437df | Isaku Yamahata | pci_get_word(dev->wmask + PCI_STATUS) | |
192 | 89d437df | Isaku Yamahata | pci_get_word(dev->w1cmask + PCI_STATUS)); |
193 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
|
194 | c0b1905b | Michael S. Tsirkin | dev->config[PCI_INTERRUPT_LINE] = 0x0;
|
195 | c0b1905b | Michael S. Tsirkin | for (r = 0; r < PCI_NUM_REGIONS; ++r) { |
196 | 71ebd6dc | Isaku Yamahata | PCIIORegion *region = &dev->io_regions[r]; |
197 | 71ebd6dc | Isaku Yamahata | if (!region->size) {
|
198 | c0b1905b | Michael S. Tsirkin | continue;
|
199 | c0b1905b | Michael S. Tsirkin | } |
200 | 71ebd6dc | Isaku Yamahata | |
201 | 71ebd6dc | Isaku Yamahata | if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
202 | 71ebd6dc | Isaku Yamahata | region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
203 | 71ebd6dc | Isaku Yamahata | pci_set_quad(dev->config + pci_bar(dev, r), region->type); |
204 | 71ebd6dc | Isaku Yamahata | } else {
|
205 | 71ebd6dc | Isaku Yamahata | pci_set_long(dev->config + pci_bar(dev, r), region->type); |
206 | 71ebd6dc | Isaku Yamahata | } |
207 | c0b1905b | Michael S. Tsirkin | } |
208 | c0b1905b | Michael S. Tsirkin | pci_update_mappings(dev); |
209 | cbd2d434 | Jan Kiszka | |
210 | cbd2d434 | Jan Kiszka | msi_reset(dev); |
211 | cbd2d434 | Jan Kiszka | msix_reset(dev); |
212 | 5330de09 | Michael S. Tsirkin | } |
213 | 5330de09 | Michael S. Tsirkin | |
214 | 9bb33586 | Isaku Yamahata | /*
|
215 | 9bb33586 | Isaku Yamahata | * Trigger pci bus reset under a given bus.
|
216 | 9bb33586 | Isaku Yamahata | * To be called on RST# assert.
|
217 | 9bb33586 | Isaku Yamahata | */
|
218 | 9bb33586 | Isaku Yamahata | void pci_bus_reset(PCIBus *bus)
|
219 | 6eaa6847 | Gleb Natapov | { |
220 | 6eaa6847 | Gleb Natapov | int i;
|
221 | 6eaa6847 | Gleb Natapov | |
222 | 6eaa6847 | Gleb Natapov | for (i = 0; i < bus->nirq; i++) { |
223 | 6eaa6847 | Gleb Natapov | bus->irq_count[i] = 0;
|
224 | 6eaa6847 | Gleb Natapov | } |
225 | 5330de09 | Michael S. Tsirkin | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
226 | 5330de09 | Michael S. Tsirkin | if (bus->devices[i]) {
|
227 | 5330de09 | Michael S. Tsirkin | pci_device_reset(bus->devices[i]); |
228 | 5330de09 | Michael S. Tsirkin | } |
229 | 6eaa6847 | Gleb Natapov | } |
230 | 6eaa6847 | Gleb Natapov | } |
231 | 6eaa6847 | Gleb Natapov | |
232 | 9bb33586 | Isaku Yamahata | static int pcibus_reset(BusState *qbus) |
233 | 9bb33586 | Isaku Yamahata | { |
234 | 9bb33586 | Isaku Yamahata | pci_bus_reset(DO_UPCAST(PCIBus, qbus, qbus)); |
235 | 9bb33586 | Isaku Yamahata | |
236 | 9bb33586 | Isaku Yamahata | /* topology traverse is done by pci_bus_reset().
|
237 | 9bb33586 | Isaku Yamahata | Tell qbus/qdev walker not to traverse the tree */
|
238 | 9bb33586 | Isaku Yamahata | return 1; |
239 | 9bb33586 | Isaku Yamahata | } |
240 | 9bb33586 | Isaku Yamahata | |
241 | e822a52a | Isaku Yamahata | static void pci_host_bus_register(int domain, PCIBus *bus) |
242 | e822a52a | Isaku Yamahata | { |
243 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
244 | 7267c094 | Anthony Liguori | host = g_malloc0(sizeof(*host));
|
245 | e822a52a | Isaku Yamahata | host->domain = domain; |
246 | e822a52a | Isaku Yamahata | host->bus = bus; |
247 | e822a52a | Isaku Yamahata | QLIST_INSERT_HEAD(&host_buses, host, next); |
248 | e822a52a | Isaku Yamahata | } |
249 | e822a52a | Isaku Yamahata | |
250 | 1ef7a2a2 | David Gibson | PCIBus *pci_find_primary_bus(void)
|
251 | e822a52a | Isaku Yamahata | { |
252 | 9bc47305 | David Gibson | PCIBus *primary_bus = NULL;
|
253 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
254 | e822a52a | Isaku Yamahata | |
255 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
256 | 9bc47305 | David Gibson | if (primary_bus) {
|
257 | 9bc47305 | David Gibson | /* We have multiple root buses, refuse to select a primary */
|
258 | 9bc47305 | David Gibson | return NULL; |
259 | e822a52a | Isaku Yamahata | } |
260 | 9bc47305 | David Gibson | primary_bus = host->bus; |
261 | e822a52a | Isaku Yamahata | } |
262 | e822a52a | Isaku Yamahata | |
263 | 9bc47305 | David Gibson | return primary_bus;
|
264 | e822a52a | Isaku Yamahata | } |
265 | e822a52a | Isaku Yamahata | |
266 | c473d18d | David Gibson | PCIBus *pci_device_root_bus(const PCIDevice *d)
|
267 | e075e788 | Isaku Yamahata | { |
268 | c473d18d | David Gibson | PCIBus *bus = d->bus; |
269 | e075e788 | Isaku Yamahata | |
270 | e075e788 | Isaku Yamahata | while ((d = bus->parent_dev) != NULL) { |
271 | e075e788 | Isaku Yamahata | bus = d->bus; |
272 | e075e788 | Isaku Yamahata | } |
273 | e075e788 | Isaku Yamahata | |
274 | c473d18d | David Gibson | return bus;
|
275 | c473d18d | David Gibson | } |
276 | c473d18d | David Gibson | |
277 | 568f0690 | David Gibson | const char *pci_root_bus_path(PCIDevice *dev) |
278 | c473d18d | David Gibson | { |
279 | 568f0690 | David Gibson | PCIBus *rootbus = pci_device_root_bus(dev); |
280 | 568f0690 | David Gibson | PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent); |
281 | 568f0690 | David Gibson | PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge); |
282 | c473d18d | David Gibson | |
283 | 568f0690 | David Gibson | assert(!rootbus->parent_dev); |
284 | 568f0690 | David Gibson | assert(host_bridge->bus == rootbus); |
285 | 568f0690 | David Gibson | |
286 | 568f0690 | David Gibson | if (hc->root_bus_path) {
|
287 | 568f0690 | David Gibson | return (*hc->root_bus_path)(host_bridge, rootbus);
|
288 | e075e788 | Isaku Yamahata | } |
289 | e075e788 | Isaku Yamahata | |
290 | 568f0690 | David Gibson | return rootbus->qbus.name;
|
291 | e075e788 | Isaku Yamahata | } |
292 | e075e788 | Isaku Yamahata | |
293 | 4fec6404 | Paolo Bonzini | static void pci_bus_init(PCIBus *bus, DeviceState *parent, |
294 | 1e39101c | Avi Kivity | const char *name, |
295 | aee97b84 | Avi Kivity | MemoryRegion *address_space_mem, |
296 | aee97b84 | Avi Kivity | MemoryRegion *address_space_io, |
297 | 1e39101c | Avi Kivity | uint8_t devfn_min) |
298 | 30468f78 | bellard | { |
299 | 6fa84913 | Isaku Yamahata | assert(PCI_FUNC(devfn_min) == 0);
|
300 | 502a5395 | pbrook | bus->devfn_min = devfn_min; |
301 | 5968eca3 | Avi Kivity | bus->address_space_mem = address_space_mem; |
302 | 5968eca3 | Avi Kivity | bus->address_space_io = address_space_io; |
303 | e822a52a | Isaku Yamahata | |
304 | e822a52a | Isaku Yamahata | /* host bridge */
|
305 | e822a52a | Isaku Yamahata | QLIST_INIT(&bus->child); |
306 | e822a52a | Isaku Yamahata | pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */ |
307 | e822a52a | Isaku Yamahata | |
308 | 0be71e32 | Alex Williamson | vmstate_register(NULL, -1, &vmstate_pcibus, bus); |
309 | 21eea4b3 | Gerd Hoffmann | } |
310 | 21eea4b3 | Gerd Hoffmann | |
311 | 8c0bf9e2 | Alex Williamson | bool pci_bus_is_express(PCIBus *bus)
|
312 | 8c0bf9e2 | Alex Williamson | { |
313 | 8c0bf9e2 | Alex Williamson | return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
|
314 | 8c0bf9e2 | Alex Williamson | } |
315 | 8c0bf9e2 | Alex Williamson | |
316 | 0889464a | Alex Williamson | bool pci_bus_is_root(PCIBus *bus)
|
317 | 0889464a | Alex Williamson | { |
318 | 0889464a | Alex Williamson | return !bus->parent_dev;
|
319 | 0889464a | Alex Williamson | } |
320 | 0889464a | Alex Williamson | |
321 | 4fec6404 | Paolo Bonzini | void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
|
322 | 4fec6404 | Paolo Bonzini | const char *name, |
323 | 4fec6404 | Paolo Bonzini | MemoryRegion *address_space_mem, |
324 | 4fec6404 | Paolo Bonzini | MemoryRegion *address_space_io, |
325 | 60a0e443 | Alex Williamson | uint8_t devfn_min, const char *typename) |
326 | 4fec6404 | Paolo Bonzini | { |
327 | 60a0e443 | Alex Williamson | qbus_create_inplace(bus, typename, parent, name); |
328 | 4fec6404 | Paolo Bonzini | pci_bus_init(bus, parent, name, address_space_mem, |
329 | 4fec6404 | Paolo Bonzini | address_space_io, devfn_min); |
330 | 4fec6404 | Paolo Bonzini | } |
331 | 4fec6404 | Paolo Bonzini | |
332 | 1e39101c | Avi Kivity | PCIBus *pci_bus_new(DeviceState *parent, const char *name, |
333 | aee97b84 | Avi Kivity | MemoryRegion *address_space_mem, |
334 | aee97b84 | Avi Kivity | MemoryRegion *address_space_io, |
335 | 60a0e443 | Alex Williamson | uint8_t devfn_min, const char *typename) |
336 | 21eea4b3 | Gerd Hoffmann | { |
337 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
338 | 21eea4b3 | Gerd Hoffmann | |
339 | 60a0e443 | Alex Williamson | bus = PCI_BUS(qbus_create(typename, parent, name)); |
340 | 4fec6404 | Paolo Bonzini | pci_bus_init(bus, parent, name, address_space_mem, |
341 | 4fec6404 | Paolo Bonzini | address_space_io, devfn_min); |
342 | 21eea4b3 | Gerd Hoffmann | return bus;
|
343 | 21eea4b3 | Gerd Hoffmann | } |
344 | 21eea4b3 | Gerd Hoffmann | |
345 | 21eea4b3 | Gerd Hoffmann | void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
|
346 | 21eea4b3 | Gerd Hoffmann | void *irq_opaque, int nirq) |
347 | 21eea4b3 | Gerd Hoffmann | { |
348 | 21eea4b3 | Gerd Hoffmann | bus->set_irq = set_irq; |
349 | 21eea4b3 | Gerd Hoffmann | bus->map_irq = map_irq; |
350 | 21eea4b3 | Gerd Hoffmann | bus->irq_opaque = irq_opaque; |
351 | 21eea4b3 | Gerd Hoffmann | bus->nirq = nirq; |
352 | 7267c094 | Anthony Liguori | bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0])); |
353 | 21eea4b3 | Gerd Hoffmann | } |
354 | 21eea4b3 | Gerd Hoffmann | |
355 | 87c30546 | Isaku Yamahata | void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
|
356 | ee995ffb | Gerd Hoffmann | { |
357 | ee995ffb | Gerd Hoffmann | bus->qbus.allow_hotplug = 1;
|
358 | ee995ffb | Gerd Hoffmann | bus->hotplug = hotplug; |
359 | 87c30546 | Isaku Yamahata | bus->hotplug_qdev = qdev; |
360 | ee995ffb | Gerd Hoffmann | } |
361 | ee995ffb | Gerd Hoffmann | |
362 | 21eea4b3 | Gerd Hoffmann | PCIBus *pci_register_bus(DeviceState *parent, const char *name, |
363 | 21eea4b3 | Gerd Hoffmann | pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, |
364 | 1e39101c | Avi Kivity | void *irq_opaque,
|
365 | aee97b84 | Avi Kivity | MemoryRegion *address_space_mem, |
366 | aee97b84 | Avi Kivity | MemoryRegion *address_space_io, |
367 | 60a0e443 | Alex Williamson | uint8_t devfn_min, int nirq, const char *typename) |
368 | 21eea4b3 | Gerd Hoffmann | { |
369 | 21eea4b3 | Gerd Hoffmann | PCIBus *bus; |
370 | 21eea4b3 | Gerd Hoffmann | |
371 | aee97b84 | Avi Kivity | bus = pci_bus_new(parent, name, address_space_mem, |
372 | 60a0e443 | Alex Williamson | address_space_io, devfn_min, typename); |
373 | 21eea4b3 | Gerd Hoffmann | pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq); |
374 | 30468f78 | bellard | return bus;
|
375 | 30468f78 | bellard | } |
376 | 69b91039 | bellard | |
377 | 502a5395 | pbrook | int pci_bus_num(PCIBus *s)
|
378 | 502a5395 | pbrook | { |
379 | 0889464a | Alex Williamson | if (pci_bus_is_root(s))
|
380 | e94ff650 | Isaku Yamahata | return 0; /* pci host bridge */ |
381 | e94ff650 | Isaku Yamahata | return s->parent_dev->config[PCI_SECONDARY_BUS];
|
382 | 502a5395 | pbrook | } |
383 | 502a5395 | pbrook | |
384 | 73534f2f | Juan Quintela | static int get_pci_config_device(QEMUFile *f, void *pv, size_t size) |
385 | 30ca2aab | bellard | { |
386 | 73534f2f | Juan Quintela | PCIDevice *s = container_of(pv, PCIDevice, config); |
387 | a9f49946 | Isaku Yamahata | uint8_t *config; |
388 | 52fc1d83 | balrog | int i;
|
389 | 52fc1d83 | balrog | |
390 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(s)); |
391 | 7267c094 | Anthony Liguori | config = g_malloc(size); |
392 | a9f49946 | Isaku Yamahata | |
393 | a9f49946 | Isaku Yamahata | qemu_get_buffer(f, config, size); |
394 | a9f49946 | Isaku Yamahata | for (i = 0; i < size; ++i) { |
395 | f9aebe2e | Michael S. Tsirkin | if ((config[i] ^ s->config[i]) &
|
396 | f9aebe2e | Michael S. Tsirkin | s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) { |
397 | 7267c094 | Anthony Liguori | g_free(config); |
398 | bd4b65ee | Michael S. Tsirkin | return -EINVAL;
|
399 | a9f49946 | Isaku Yamahata | } |
400 | a9f49946 | Isaku Yamahata | } |
401 | a9f49946 | Isaku Yamahata | memcpy(s->config, config, size); |
402 | bd4b65ee | Michael S. Tsirkin | |
403 | 1941d19c | bellard | pci_update_mappings(s); |
404 | 52fc1d83 | balrog | |
405 | 4ea375bf | Gerd Hoffmann | memory_region_set_enabled(&s->bus_master_enable_region, |
406 | 4ea375bf | Gerd Hoffmann | pci_get_word(s->config + PCI_COMMAND) |
407 | 4ea375bf | Gerd Hoffmann | & PCI_COMMAND_MASTER); |
408 | 4ea375bf | Gerd Hoffmann | |
409 | 7267c094 | Anthony Liguori | g_free(config); |
410 | 30ca2aab | bellard | return 0; |
411 | 30ca2aab | bellard | } |
412 | 30ca2aab | bellard | |
413 | 73534f2f | Juan Quintela | /* just put buffer */
|
414 | 84e2e3eb | Juan Quintela | static void put_pci_config_device(QEMUFile *f, void *pv, size_t size) |
415 | 73534f2f | Juan Quintela | { |
416 | dbe73d7f | Juan Quintela | const uint8_t **v = pv;
|
417 | a9f49946 | Isaku Yamahata | assert(size == pci_config_size(container_of(pv, PCIDevice, config))); |
418 | dbe73d7f | Juan Quintela | qemu_put_buffer(f, *v, size); |
419 | 73534f2f | Juan Quintela | } |
420 | 73534f2f | Juan Quintela | |
421 | 73534f2f | Juan Quintela | static VMStateInfo vmstate_info_pci_config = {
|
422 | 73534f2f | Juan Quintela | .name = "pci config",
|
423 | 73534f2f | Juan Quintela | .get = get_pci_config_device, |
424 | 73534f2f | Juan Quintela | .put = put_pci_config_device, |
425 | 73534f2f | Juan Quintela | }; |
426 | 73534f2f | Juan Quintela | |
427 | d036bb21 | Michael S. Tsirkin | static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
428 | d036bb21 | Michael S. Tsirkin | { |
429 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
430 | d036bb21 | Michael S. Tsirkin | uint32_t irq_state[PCI_NUM_PINS]; |
431 | d036bb21 | Michael S. Tsirkin | int i;
|
432 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
433 | d036bb21 | Michael S. Tsirkin | irq_state[i] = qemu_get_be32(f); |
434 | d036bb21 | Michael S. Tsirkin | if (irq_state[i] != 0x1 && irq_state[i] != 0) { |
435 | d036bb21 | Michael S. Tsirkin | fprintf(stderr, "irq state %d: must be 0 or 1.\n",
|
436 | d036bb21 | Michael S. Tsirkin | irq_state[i]); |
437 | d036bb21 | Michael S. Tsirkin | return -EINVAL;
|
438 | d036bb21 | Michael S. Tsirkin | } |
439 | d036bb21 | Michael S. Tsirkin | } |
440 | d036bb21 | Michael S. Tsirkin | |
441 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
442 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(s, i, irq_state[i]); |
443 | d036bb21 | Michael S. Tsirkin | } |
444 | d036bb21 | Michael S. Tsirkin | |
445 | d036bb21 | Michael S. Tsirkin | return 0; |
446 | d036bb21 | Michael S. Tsirkin | } |
447 | d036bb21 | Michael S. Tsirkin | |
448 | d036bb21 | Michael S. Tsirkin | static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size) |
449 | d036bb21 | Michael S. Tsirkin | { |
450 | d036bb21 | Michael S. Tsirkin | int i;
|
451 | c3f8f611 | Michael S. Tsirkin | PCIDevice *s = container_of(pv, PCIDevice, irq_state); |
452 | d036bb21 | Michael S. Tsirkin | |
453 | d036bb21 | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
454 | d036bb21 | Michael S. Tsirkin | qemu_put_be32(f, pci_irq_state(s, i)); |
455 | d036bb21 | Michael S. Tsirkin | } |
456 | d036bb21 | Michael S. Tsirkin | } |
457 | d036bb21 | Michael S. Tsirkin | |
458 | d036bb21 | Michael S. Tsirkin | static VMStateInfo vmstate_info_pci_irq_state = {
|
459 | d036bb21 | Michael S. Tsirkin | .name = "pci irq state",
|
460 | d036bb21 | Michael S. Tsirkin | .get = get_pci_irq_state, |
461 | d036bb21 | Michael S. Tsirkin | .put = put_pci_irq_state, |
462 | d036bb21 | Michael S. Tsirkin | }; |
463 | d036bb21 | Michael S. Tsirkin | |
464 | 73534f2f | Juan Quintela | const VMStateDescription vmstate_pci_device = {
|
465 | 73534f2f | Juan Quintela | .name = "PCIDevice",
|
466 | 73534f2f | Juan Quintela | .version_id = 2,
|
467 | 73534f2f | Juan Quintela | .minimum_version_id = 1,
|
468 | 73534f2f | Juan Quintela | .minimum_version_id_old = 1,
|
469 | 73534f2f | Juan Quintela | .fields = (VMStateField []) { |
470 | 73534f2f | Juan Quintela | VMSTATE_INT32_LE(version_id, PCIDevice), |
471 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
472 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
473 | a9f49946 | Isaku Yamahata | PCI_CONFIG_SPACE_SIZE), |
474 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
475 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
476 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
477 | a9f49946 | Isaku Yamahata | VMSTATE_END_OF_LIST() |
478 | a9f49946 | Isaku Yamahata | } |
479 | a9f49946 | Isaku Yamahata | }; |
480 | a9f49946 | Isaku Yamahata | |
481 | a9f49946 | Isaku Yamahata | const VMStateDescription vmstate_pcie_device = {
|
482 | 1de53459 | Jason Baron | .name = "PCIEDevice",
|
483 | a9f49946 | Isaku Yamahata | .version_id = 2,
|
484 | a9f49946 | Isaku Yamahata | .minimum_version_id = 1,
|
485 | a9f49946 | Isaku Yamahata | .minimum_version_id_old = 1,
|
486 | a9f49946 | Isaku Yamahata | .fields = (VMStateField []) { |
487 | a9f49946 | Isaku Yamahata | VMSTATE_INT32_LE(version_id, PCIDevice), |
488 | a9f49946 | Isaku Yamahata | VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
|
489 | a9f49946 | Isaku Yamahata | vmstate_info_pci_config, |
490 | a9f49946 | Isaku Yamahata | PCIE_CONFIG_SPACE_SIZE), |
491 | d036bb21 | Michael S. Tsirkin | VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
|
492 | d036bb21 | Michael S. Tsirkin | vmstate_info_pci_irq_state, |
493 | d036bb21 | Michael S. Tsirkin | PCI_NUM_PINS * sizeof(int32_t)),
|
494 | 73534f2f | Juan Quintela | VMSTATE_END_OF_LIST() |
495 | 73534f2f | Juan Quintela | } |
496 | 73534f2f | Juan Quintela | }; |
497 | 73534f2f | Juan Quintela | |
498 | a9f49946 | Isaku Yamahata | static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s) |
499 | a9f49946 | Isaku Yamahata | { |
500 | a9f49946 | Isaku Yamahata | return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
|
501 | a9f49946 | Isaku Yamahata | } |
502 | a9f49946 | Isaku Yamahata | |
503 | 73534f2f | Juan Quintela | void pci_device_save(PCIDevice *s, QEMUFile *f)
|
504 | 73534f2f | Juan Quintela | { |
505 | f9bf77dd | Michael S. Tsirkin | /* Clear interrupt status bit: it is implicit
|
506 | f9bf77dd | Michael S. Tsirkin | * in irq_state which we are saving.
|
507 | f9bf77dd | Michael S. Tsirkin | * This makes us compatible with old devices
|
508 | f9bf77dd | Michael S. Tsirkin | * which never set or clear this bit. */
|
509 | f9bf77dd | Michael S. Tsirkin | s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; |
510 | a9f49946 | Isaku Yamahata | vmstate_save_state(f, pci_get_vmstate(s), s); |
511 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
512 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
513 | 73534f2f | Juan Quintela | } |
514 | 73534f2f | Juan Quintela | |
515 | 73534f2f | Juan Quintela | int pci_device_load(PCIDevice *s, QEMUFile *f)
|
516 | 73534f2f | Juan Quintela | { |
517 | f9bf77dd | Michael S. Tsirkin | int ret;
|
518 | f9bf77dd | Michael S. Tsirkin | ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id); |
519 | f9bf77dd | Michael S. Tsirkin | /* Restore the interrupt status bit. */
|
520 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(s); |
521 | f9bf77dd | Michael S. Tsirkin | return ret;
|
522 | 73534f2f | Juan Quintela | } |
523 | 73534f2f | Juan Quintela | |
524 | 5e434f4e | Isaku Yamahata | static void pci_set_default_subsystem_id(PCIDevice *pci_dev) |
525 | d350d97d | aliguori | { |
526 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
527 | 5e434f4e | Isaku Yamahata | pci_default_sub_vendor_id); |
528 | 5e434f4e | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
529 | 5e434f4e | Isaku Yamahata | pci_default_sub_device_id); |
530 | d350d97d | aliguori | } |
531 | d350d97d | aliguori | |
532 | 880345c4 | aliguori | /*
|
533 | 43c945f1 | Isaku Yamahata | * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
|
534 | 43c945f1 | Isaku Yamahata | * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
|
535 | 880345c4 | aliguori | */
|
536 | 6ac363b5 | David Gibson | int pci_parse_devaddr(const char *addr, int *domp, int *busp, |
537 | 43c945f1 | Isaku Yamahata | unsigned int *slotp, unsigned int *funcp) |
538 | 880345c4 | aliguori | { |
539 | 880345c4 | aliguori | const char *p; |
540 | 880345c4 | aliguori | char *e;
|
541 | 880345c4 | aliguori | unsigned long val; |
542 | 880345c4 | aliguori | unsigned long dom = 0, bus = 0; |
543 | 43c945f1 | Isaku Yamahata | unsigned int slot = 0; |
544 | 43c945f1 | Isaku Yamahata | unsigned int func = 0; |
545 | 880345c4 | aliguori | |
546 | 880345c4 | aliguori | p = addr; |
547 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
548 | 880345c4 | aliguori | if (e == p)
|
549 | 880345c4 | aliguori | return -1; |
550 | 880345c4 | aliguori | if (*e == ':') { |
551 | 880345c4 | aliguori | bus = val; |
552 | 880345c4 | aliguori | p = e + 1;
|
553 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
554 | 880345c4 | aliguori | if (e == p)
|
555 | 880345c4 | aliguori | return -1; |
556 | 880345c4 | aliguori | if (*e == ':') { |
557 | 880345c4 | aliguori | dom = bus; |
558 | 880345c4 | aliguori | bus = val; |
559 | 880345c4 | aliguori | p = e + 1;
|
560 | 880345c4 | aliguori | val = strtoul(p, &e, 16);
|
561 | 880345c4 | aliguori | if (e == p)
|
562 | 880345c4 | aliguori | return -1; |
563 | 880345c4 | aliguori | } |
564 | 880345c4 | aliguori | } |
565 | 880345c4 | aliguori | |
566 | 880345c4 | aliguori | slot = val; |
567 | 880345c4 | aliguori | |
568 | 43c945f1 | Isaku Yamahata | if (funcp != NULL) { |
569 | 43c945f1 | Isaku Yamahata | if (*e != '.') |
570 | 43c945f1 | Isaku Yamahata | return -1; |
571 | 43c945f1 | Isaku Yamahata | |
572 | 43c945f1 | Isaku Yamahata | p = e + 1;
|
573 | 43c945f1 | Isaku Yamahata | val = strtoul(p, &e, 16);
|
574 | 43c945f1 | Isaku Yamahata | if (e == p)
|
575 | 43c945f1 | Isaku Yamahata | return -1; |
576 | 43c945f1 | Isaku Yamahata | |
577 | 43c945f1 | Isaku Yamahata | func = val; |
578 | 43c945f1 | Isaku Yamahata | } |
579 | 43c945f1 | Isaku Yamahata | |
580 | 43c945f1 | Isaku Yamahata | /* if funcp == NULL func is 0 */
|
581 | 43c945f1 | Isaku Yamahata | if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) |
582 | 43c945f1 | Isaku Yamahata | return -1; |
583 | 43c945f1 | Isaku Yamahata | |
584 | 880345c4 | aliguori | if (*e)
|
585 | 880345c4 | aliguori | return -1; |
586 | 880345c4 | aliguori | |
587 | 880345c4 | aliguori | *domp = dom; |
588 | 880345c4 | aliguori | *busp = bus; |
589 | 880345c4 | aliguori | *slotp = slot; |
590 | 43c945f1 | Isaku Yamahata | if (funcp != NULL) |
591 | 43c945f1 | Isaku Yamahata | *funcp = func; |
592 | 880345c4 | aliguori | return 0; |
593 | 880345c4 | aliguori | } |
594 | 880345c4 | aliguori | |
595 | 85c6e4fa | David Gibson | PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root, const char *devaddr) |
596 | 5607c388 | Markus Armbruster | { |
597 | 5607c388 | Markus Armbruster | int dom, bus;
|
598 | 5607c388 | Markus Armbruster | unsigned slot;
|
599 | 5607c388 | Markus Armbruster | |
600 | 85c6e4fa | David Gibson | assert(!root->parent_dev); |
601 | 85c6e4fa | David Gibson | |
602 | 1ef7a2a2 | David Gibson | if (!root) {
|
603 | 1ef7a2a2 | David Gibson | fprintf(stderr, "No primary PCI bus\n");
|
604 | 1ef7a2a2 | David Gibson | return NULL; |
605 | 1ef7a2a2 | David Gibson | } |
606 | 1ef7a2a2 | David Gibson | |
607 | 5607c388 | Markus Armbruster | if (!devaddr) {
|
608 | 5607c388 | Markus Armbruster | *devfnp = -1;
|
609 | 1ef7a2a2 | David Gibson | return pci_find_bus_nr(root, 0); |
610 | 5607c388 | Markus Armbruster | } |
611 | 5607c388 | Markus Armbruster | |
612 | 43c945f1 | Isaku Yamahata | if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) { |
613 | 5607c388 | Markus Armbruster | return NULL; |
614 | 5607c388 | Markus Armbruster | } |
615 | 5607c388 | Markus Armbruster | |
616 | 1ef7a2a2 | David Gibson | if (dom != 0) { |
617 | 1ef7a2a2 | David Gibson | fprintf(stderr, "No support for non-zero PCI domains\n");
|
618 | 1ef7a2a2 | David Gibson | return NULL; |
619 | 1ef7a2a2 | David Gibson | } |
620 | 1ef7a2a2 | David Gibson | |
621 | 6ff534b6 | Isaku Yamahata | *devfnp = PCI_DEVFN(slot, 0);
|
622 | 1ef7a2a2 | David Gibson | return pci_find_bus_nr(root, bus);
|
623 | 5607c388 | Markus Armbruster | } |
624 | 5607c388 | Markus Armbruster | |
625 | bd4b65ee | Michael S. Tsirkin | static void pci_init_cmask(PCIDevice *dev) |
626 | bd4b65ee | Michael S. Tsirkin | { |
627 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
|
628 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
|
629 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; |
630 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_REVISION_ID] = 0xff;
|
631 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CLASS_PROG] = 0xff;
|
632 | bd4b65ee | Michael S. Tsirkin | pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
|
633 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_HEADER_TYPE] = 0xff;
|
634 | bd4b65ee | Michael S. Tsirkin | dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
|
635 | bd4b65ee | Michael S. Tsirkin | } |
636 | bd4b65ee | Michael S. Tsirkin | |
637 | b7ee1603 | Michael S. Tsirkin | static void pci_init_wmask(PCIDevice *dev) |
638 | b7ee1603 | Michael S. Tsirkin | { |
639 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(dev);
|
640 | a9f49946 | Isaku Yamahata | |
641 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
|
642 | b7ee1603 | Michael S. Tsirkin | dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
|
643 | 67a51b48 | Isaku Yamahata | pci_set_word(dev->wmask + PCI_COMMAND, |
644 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | |
645 | a7b15a5c | Michael S. Tsirkin | PCI_COMMAND_INTX_DISABLE); |
646 | b1aeb926 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_SERR) {
|
647 | b1aeb926 | Isaku Yamahata | pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR); |
648 | b1aeb926 | Isaku Yamahata | } |
649 | 3e21ffc9 | Isaku Yamahata | |
650 | 3e21ffc9 | Isaku Yamahata | memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
|
651 | 3e21ffc9 | Isaku Yamahata | config_size - PCI_CONFIG_HEADER_SIZE); |
652 | b7ee1603 | Michael S. Tsirkin | } |
653 | b7ee1603 | Michael S. Tsirkin | |
654 | 89d437df | Isaku Yamahata | static void pci_init_w1cmask(PCIDevice *dev) |
655 | 89d437df | Isaku Yamahata | { |
656 | 89d437df | Isaku Yamahata | /*
|
657 | f6bdfcc9 | Michael S. Tsirkin | * Note: It's okay to set w1cmask even for readonly bits as
|
658 | 89d437df | Isaku Yamahata | * long as their value is hardwired to 0.
|
659 | 89d437df | Isaku Yamahata | */
|
660 | 89d437df | Isaku Yamahata | pci_set_word(dev->w1cmask + PCI_STATUS, |
661 | 89d437df | Isaku Yamahata | PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT | |
662 | 89d437df | Isaku Yamahata | PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT | |
663 | 89d437df | Isaku Yamahata | PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY); |
664 | 89d437df | Isaku Yamahata | } |
665 | 89d437df | Isaku Yamahata | |
666 | d5f27e88 | Michael S. Tsirkin | static void pci_init_mask_bridge(PCIDevice *d) |
667 | fb231628 | Isaku Yamahata | { |
668 | fb231628 | Isaku Yamahata | /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
|
669 | fb231628 | Isaku Yamahata | PCI_SEC_LETENCY_TIMER */
|
670 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); |
671 | fb231628 | Isaku Yamahata | |
672 | fb231628 | Isaku Yamahata | /* base and limit */
|
673 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
|
674 | fb231628 | Isaku Yamahata | d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
|
675 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_BASE, |
676 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
677 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_MEMORY_LIMIT, |
678 | fb231628 | Isaku Yamahata | PCI_MEMORY_RANGE_MASK & 0xffff);
|
679 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, |
680 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
681 | fb231628 | Isaku Yamahata | pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, |
682 | fb231628 | Isaku Yamahata | PCI_PREF_RANGE_MASK & 0xffff);
|
683 | fb231628 | Isaku Yamahata | |
684 | fb231628 | Isaku Yamahata | /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
|
685 | fb231628 | Isaku Yamahata | memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); |
686 | fb231628 | Isaku Yamahata | |
687 | d5f27e88 | Michael S. Tsirkin | /* Supported memory and i/o types */
|
688 | 68917102 | Michael S. Tsirkin | d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16; |
689 | 68917102 | Michael S. Tsirkin | d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16; |
690 | d5f27e88 | Michael S. Tsirkin | pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE, |
691 | d5f27e88 | Michael S. Tsirkin | PCI_PREF_RANGE_TYPE_64); |
692 | d5f27e88 | Michael S. Tsirkin | pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT, |
693 | d5f27e88 | Michael S. Tsirkin | PCI_PREF_RANGE_TYPE_64); |
694 | d5f27e88 | Michael S. Tsirkin | |
695 | 45eb768c | Michael S. Tsirkin | /*
|
696 | 45eb768c | Michael S. Tsirkin | * TODO: Bridges default to 10-bit VGA decoding but we currently only
|
697 | 45eb768c | Michael S. Tsirkin | * implement 16-bit decoding (no alias support).
|
698 | 45eb768c | Michael S. Tsirkin | */
|
699 | f6bdfcc9 | Michael S. Tsirkin | pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, |
700 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_PARITY | |
701 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_SERR | |
702 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_ISA | |
703 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_VGA | |
704 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_VGA_16BIT | |
705 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_MASTER_ABORT | |
706 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_BUS_RESET | |
707 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_FAST_BACK | |
708 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD | |
709 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_SEC_DISCARD | |
710 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD_SERR); |
711 | f6bdfcc9 | Michael S. Tsirkin | /* Below does not do anything as we never set this bit, put here for
|
712 | f6bdfcc9 | Michael S. Tsirkin | * completeness. */
|
713 | f6bdfcc9 | Michael S. Tsirkin | pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL, |
714 | f6bdfcc9 | Michael S. Tsirkin | PCI_BRIDGE_CTL_DISCARD_STATUS); |
715 | d5f27e88 | Michael S. Tsirkin | d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK; |
716 | 15ab7a75 | Michael S. Tsirkin | d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK; |
717 | d5f27e88 | Michael S. Tsirkin | pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE, |
718 | d5f27e88 | Michael S. Tsirkin | PCI_PREF_RANGE_TYPE_MASK); |
719 | 15ab7a75 | Michael S. Tsirkin | pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT, |
720 | 15ab7a75 | Michael S. Tsirkin | PCI_PREF_RANGE_TYPE_MASK); |
721 | fb231628 | Isaku Yamahata | } |
722 | fb231628 | Isaku Yamahata | |
723 | 6eab3de1 | Isaku Yamahata | static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) |
724 | 6eab3de1 | Isaku Yamahata | { |
725 | 6eab3de1 | Isaku Yamahata | uint8_t slot = PCI_SLOT(dev->devfn); |
726 | 6eab3de1 | Isaku Yamahata | uint8_t func; |
727 | 6eab3de1 | Isaku Yamahata | |
728 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
729 | 6eab3de1 | Isaku Yamahata | dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; |
730 | 6eab3de1 | Isaku Yamahata | } |
731 | 6eab3de1 | Isaku Yamahata | |
732 | 6eab3de1 | Isaku Yamahata | /*
|
733 | b0cd712c | Stefan Weil | * multifunction bit is interpreted in two ways as follows.
|
734 | 6eab3de1 | Isaku Yamahata | * - all functions must set the bit to 1.
|
735 | 6eab3de1 | Isaku Yamahata | * Example: Intel X53
|
736 | 6eab3de1 | Isaku Yamahata | * - function 0 must set the bit, but the rest function (> 0)
|
737 | 6eab3de1 | Isaku Yamahata | * is allowed to leave the bit to 0.
|
738 | 6eab3de1 | Isaku Yamahata | * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
|
739 | 6eab3de1 | Isaku Yamahata | *
|
740 | 6eab3de1 | Isaku Yamahata | * So OS (at least Linux) checks the bit of only function 0,
|
741 | 6eab3de1 | Isaku Yamahata | * and doesn't see the bit of function > 0.
|
742 | 6eab3de1 | Isaku Yamahata | *
|
743 | 6eab3de1 | Isaku Yamahata | * The below check allows both interpretation.
|
744 | 6eab3de1 | Isaku Yamahata | */
|
745 | 6eab3de1 | Isaku Yamahata | if (PCI_FUNC(dev->devfn)) {
|
746 | 6eab3de1 | Isaku Yamahata | PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
|
747 | 6eab3de1 | Isaku Yamahata | if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
|
748 | 6eab3de1 | Isaku Yamahata | /* function 0 should set multifunction bit */
|
749 | 6eab3de1 | Isaku Yamahata | error_report("PCI: single function device can't be populated "
|
750 | 6eab3de1 | Isaku Yamahata | "in function %x.%x", slot, PCI_FUNC(dev->devfn));
|
751 | 6eab3de1 | Isaku Yamahata | return -1; |
752 | 6eab3de1 | Isaku Yamahata | } |
753 | 6eab3de1 | Isaku Yamahata | return 0; |
754 | 6eab3de1 | Isaku Yamahata | } |
755 | 6eab3de1 | Isaku Yamahata | |
756 | 6eab3de1 | Isaku Yamahata | if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
|
757 | 6eab3de1 | Isaku Yamahata | return 0; |
758 | 6eab3de1 | Isaku Yamahata | } |
759 | 6eab3de1 | Isaku Yamahata | /* function 0 indicates single function, so function > 0 must be NULL */
|
760 | 6eab3de1 | Isaku Yamahata | for (func = 1; func < PCI_FUNC_MAX; ++func) { |
761 | 6eab3de1 | Isaku Yamahata | if (bus->devices[PCI_DEVFN(slot, func)]) {
|
762 | 6eab3de1 | Isaku Yamahata | error_report("PCI: %x.0 indicates single function, "
|
763 | 6eab3de1 | Isaku Yamahata | "but %x.%x is already populated.",
|
764 | 6eab3de1 | Isaku Yamahata | slot, slot, func); |
765 | 6eab3de1 | Isaku Yamahata | return -1; |
766 | 6eab3de1 | Isaku Yamahata | } |
767 | 6eab3de1 | Isaku Yamahata | } |
768 | 6eab3de1 | Isaku Yamahata | return 0; |
769 | 6eab3de1 | Isaku Yamahata | } |
770 | 6eab3de1 | Isaku Yamahata | |
771 | a9f49946 | Isaku Yamahata | static void pci_config_alloc(PCIDevice *pci_dev) |
772 | a9f49946 | Isaku Yamahata | { |
773 | a9f49946 | Isaku Yamahata | int config_size = pci_config_size(pci_dev);
|
774 | a9f49946 | Isaku Yamahata | |
775 | 7267c094 | Anthony Liguori | pci_dev->config = g_malloc0(config_size); |
776 | 7267c094 | Anthony Liguori | pci_dev->cmask = g_malloc0(config_size); |
777 | 7267c094 | Anthony Liguori | pci_dev->wmask = g_malloc0(config_size); |
778 | 7267c094 | Anthony Liguori | pci_dev->w1cmask = g_malloc0(config_size); |
779 | 7267c094 | Anthony Liguori | pci_dev->used = g_malloc0(config_size); |
780 | a9f49946 | Isaku Yamahata | } |
781 | a9f49946 | Isaku Yamahata | |
782 | a9f49946 | Isaku Yamahata | static void pci_config_free(PCIDevice *pci_dev) |
783 | a9f49946 | Isaku Yamahata | { |
784 | 7267c094 | Anthony Liguori | g_free(pci_dev->config); |
785 | 7267c094 | Anthony Liguori | g_free(pci_dev->cmask); |
786 | 7267c094 | Anthony Liguori | g_free(pci_dev->wmask); |
787 | 7267c094 | Anthony Liguori | g_free(pci_dev->w1cmask); |
788 | 7267c094 | Anthony Liguori | g_free(pci_dev->used); |
789 | a9f49946 | Isaku Yamahata | } |
790 | a9f49946 | Isaku Yamahata | |
791 | 69b91039 | bellard | /* -1 for devfn means auto assign */
|
792 | 6b1b92d3 | Paul Brook | static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
|
793 | 40021f08 | Anthony Liguori | const char *name, int devfn) |
794 | 69b91039 | bellard | { |
795 | 40021f08 | Anthony Liguori | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); |
796 | 40021f08 | Anthony Liguori | PCIConfigReadFunc *config_read = pc->config_read; |
797 | 40021f08 | Anthony Liguori | PCIConfigWriteFunc *config_write = pc->config_write; |
798 | e00387d5 | Avi Kivity | AddressSpace *dma_as; |
799 | 113f89df | Isaku Yamahata | |
800 | 69b91039 | bellard | if (devfn < 0) { |
801 | b47b0706 | Isaku Yamahata | for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
|
802 | 6fa84913 | Isaku Yamahata | devfn += PCI_FUNC_MAX) { |
803 | 30468f78 | bellard | if (!bus->devices[devfn])
|
804 | 69b91039 | bellard | goto found;
|
805 | 69b91039 | bellard | } |
806 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: no slot/function available for %s, all in use", name);
|
807 | 09e3acc6 | Gerd Hoffmann | return NULL; |
808 | 69b91039 | bellard | found: ;
|
809 | 07b7d053 | Markus Armbruster | } else if (bus->devices[devfn]) { |
810 | 3709c1b7 | Daniel P. Berrange | error_report("PCI: slot %d function %d not available for %s, in use by %s",
|
811 | 3709c1b7 | Daniel P. Berrange | PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name); |
812 | 09e3acc6 | Gerd Hoffmann | return NULL; |
813 | 69b91039 | bellard | } |
814 | e00387d5 | Avi Kivity | |
815 | 30468f78 | bellard | pci_dev->bus = bus; |
816 | e00387d5 | Avi Kivity | if (bus->iommu_fn) {
|
817 | e00387d5 | Avi Kivity | dma_as = bus->iommu_fn(bus, bus->iommu_opaque, devfn); |
818 | 817dcc53 | Avi Kivity | } else {
|
819 | 817dcc53 | Avi Kivity | /* FIXME: inherit memory region from bus creator */
|
820 | e00387d5 | Avi Kivity | dma_as = &address_space_memory; |
821 | 5fa45de5 | David Gibson | } |
822 | 24addbc7 | Paolo Bonzini | |
823 | e00387d5 | Avi Kivity | memory_region_init_alias(&pci_dev->bus_master_enable_region, "bus master",
|
824 | e00387d5 | Avi Kivity | dma_as->root, 0, memory_region_size(dma_as->root));
|
825 | e00387d5 | Avi Kivity | memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
|
826 | 7dca8043 | Alexey Kardashevskiy | address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region, |
827 | 7dca8043 | Alexey Kardashevskiy | name); |
828 | e00387d5 | Avi Kivity | |
829 | 69b91039 | bellard | pci_dev->devfn = devfn; |
830 | 69b91039 | bellard | pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
|
831 | d036bb21 | Michael S. Tsirkin | pci_dev->irq_state = 0;
|
832 | a9f49946 | Isaku Yamahata | pci_config_alloc(pci_dev); |
833 | fb231628 | Isaku Yamahata | |
834 | 40021f08 | Anthony Liguori | pci_config_set_vendor_id(pci_dev->config, pc->vendor_id); |
835 | 40021f08 | Anthony Liguori | pci_config_set_device_id(pci_dev->config, pc->device_id); |
836 | 40021f08 | Anthony Liguori | pci_config_set_revision(pci_dev->config, pc->revision); |
837 | 40021f08 | Anthony Liguori | pci_config_set_class(pci_dev->config, pc->class_id); |
838 | 113f89df | Isaku Yamahata | |
839 | 40021f08 | Anthony Liguori | if (!pc->is_bridge) {
|
840 | 40021f08 | Anthony Liguori | if (pc->subsystem_vendor_id || pc->subsystem_id) {
|
841 | 113f89df | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, |
842 | 40021f08 | Anthony Liguori | pc->subsystem_vendor_id); |
843 | 113f89df | Isaku Yamahata | pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, |
844 | 40021f08 | Anthony Liguori | pc->subsystem_id); |
845 | 113f89df | Isaku Yamahata | } else {
|
846 | 113f89df | Isaku Yamahata | pci_set_default_subsystem_id(pci_dev); |
847 | 113f89df | Isaku Yamahata | } |
848 | 113f89df | Isaku Yamahata | } else {
|
849 | 113f89df | Isaku Yamahata | /* subsystem_vendor_id/subsystem_id are only for header type 0 */
|
850 | 40021f08 | Anthony Liguori | assert(!pc->subsystem_vendor_id); |
851 | 40021f08 | Anthony Liguori | assert(!pc->subsystem_id); |
852 | fb231628 | Isaku Yamahata | } |
853 | bd4b65ee | Michael S. Tsirkin | pci_init_cmask(pci_dev); |
854 | b7ee1603 | Michael S. Tsirkin | pci_init_wmask(pci_dev); |
855 | 89d437df | Isaku Yamahata | pci_init_w1cmask(pci_dev); |
856 | 40021f08 | Anthony Liguori | if (pc->is_bridge) {
|
857 | d5f27e88 | Michael S. Tsirkin | pci_init_mask_bridge(pci_dev); |
858 | fb231628 | Isaku Yamahata | } |
859 | 6eab3de1 | Isaku Yamahata | if (pci_init_multifunction(bus, pci_dev)) {
|
860 | 6eab3de1 | Isaku Yamahata | pci_config_free(pci_dev); |
861 | 6eab3de1 | Isaku Yamahata | return NULL; |
862 | 6eab3de1 | Isaku Yamahata | } |
863 | 0ac32c83 | bellard | |
864 | 0ac32c83 | bellard | if (!config_read)
|
865 | 0ac32c83 | bellard | config_read = pci_default_read_config; |
866 | 0ac32c83 | bellard | if (!config_write)
|
867 | 0ac32c83 | bellard | config_write = pci_default_write_config; |
868 | 69b91039 | bellard | pci_dev->config_read = config_read; |
869 | 69b91039 | bellard | pci_dev->config_write = config_write; |
870 | 30468f78 | bellard | bus->devices[devfn] = pci_dev; |
871 | e369cad7 | Isaku Yamahata | pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS); |
872 | f16c4abf | Juan Quintela | pci_dev->version_id = 2; /* Current pci device vmstate version */ |
873 | 69b91039 | bellard | return pci_dev;
|
874 | 69b91039 | bellard | } |
875 | 69b91039 | bellard | |
876 | 925fe64a | Alex Williamson | static void do_pci_unregister_device(PCIDevice *pci_dev) |
877 | 925fe64a | Alex Williamson | { |
878 | 925fe64a | Alex Williamson | qemu_free_irqs(pci_dev->irq); |
879 | 925fe64a | Alex Williamson | pci_dev->bus->devices[pci_dev->devfn] = NULL;
|
880 | 925fe64a | Alex Williamson | pci_config_free(pci_dev); |
881 | 817dcc53 | Avi Kivity | |
882 | e00387d5 | Avi Kivity | address_space_destroy(&pci_dev->bus_master_as); |
883 | e00387d5 | Avi Kivity | memory_region_destroy(&pci_dev->bus_master_enable_region); |
884 | 925fe64a | Alex Williamson | } |
885 | 925fe64a | Alex Williamson | |
886 | 5851e08c | aliguori | static void pci_unregister_io_regions(PCIDevice *pci_dev) |
887 | 5851e08c | aliguori | { |
888 | 5851e08c | aliguori | PCIIORegion *r; |
889 | 5851e08c | aliguori | int i;
|
890 | 5851e08c | aliguori | |
891 | 5851e08c | aliguori | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
892 | 5851e08c | aliguori | r = &pci_dev->io_regions[i]; |
893 | 182f9c8a | Isaku Yamahata | if (!r->size || r->addr == PCI_BAR_UNMAPPED)
|
894 | 5851e08c | aliguori | continue;
|
895 | 03952339 | Avi Kivity | memory_region_del_subregion(r->address_space, r->memory); |
896 | 5851e08c | aliguori | } |
897 | e01fd687 | Alex Williamson | |
898 | e01fd687 | Alex Williamson | pci_unregister_vga(pci_dev); |
899 | 5851e08c | aliguori | } |
900 | 5851e08c | aliguori | |
901 | a36a344d | Gerd Hoffmann | static int pci_unregister_device(DeviceState *dev) |
902 | 5851e08c | aliguori | { |
903 | 40021f08 | Anthony Liguori | PCIDevice *pci_dev = PCI_DEVICE(dev); |
904 | 40021f08 | Anthony Liguori | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); |
905 | 5851e08c | aliguori | |
906 | 5851e08c | aliguori | pci_unregister_io_regions(pci_dev); |
907 | 230741dc | Alex Williamson | pci_del_option_rom(pci_dev); |
908 | 7cf1b0fd | Alex Williamson | |
909 | f90c2bcd | Alex Williamson | if (pc->exit) {
|
910 | f90c2bcd | Alex Williamson | pc->exit(pci_dev); |
911 | f90c2bcd | Alex Williamson | } |
912 | 5851e08c | aliguori | |
913 | 925fe64a | Alex Williamson | do_pci_unregister_device(pci_dev); |
914 | 5851e08c | aliguori | return 0; |
915 | 5851e08c | aliguori | } |
916 | 5851e08c | aliguori | |
917 | e824b2cc | Avi Kivity | void pci_register_bar(PCIDevice *pci_dev, int region_num, |
918 | e824b2cc | Avi Kivity | uint8_t type, MemoryRegion *memory) |
919 | 69b91039 | bellard | { |
920 | 69b91039 | bellard | PCIIORegion *r; |
921 | d7ce493a | pbrook | uint32_t addr; |
922 | 5a9ff381 | Isaku Yamahata | uint64_t wmask; |
923 | cfc0be25 | Avi Kivity | pcibus_t size = memory_region_size(memory); |
924 | a4c20c6a | aliguori | |
925 | 2bbb9c2f | Isaku Yamahata | assert(region_num >= 0);
|
926 | 2bbb9c2f | Isaku Yamahata | assert(region_num < PCI_NUM_REGIONS); |
927 | a4c20c6a | aliguori | if (size & (size-1)) { |
928 | a4c20c6a | aliguori | fprintf(stderr, "ERROR: PCI region size must be pow2 "
|
929 | 89e8b13c | Isaku Yamahata | "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size); |
930 | a4c20c6a | aliguori | exit(1);
|
931 | a4c20c6a | aliguori | } |
932 | a4c20c6a | aliguori | |
933 | 69b91039 | bellard | r = &pci_dev->io_regions[region_num]; |
934 | 182f9c8a | Isaku Yamahata | r->addr = PCI_BAR_UNMAPPED; |
935 | 69b91039 | bellard | r->size = size; |
936 | 69b91039 | bellard | r->type = type; |
937 | 79ff8cb0 | Avi Kivity | r->memory = NULL;
|
938 | b7ee1603 | Michael S. Tsirkin | |
939 | b7ee1603 | Michael S. Tsirkin | wmask = ~(size - 1);
|
940 | b3b11697 | Isaku Yamahata | addr = pci_bar(pci_dev, region_num); |
941 | d7ce493a | pbrook | if (region_num == PCI_ROM_SLOT) {
|
942 | ebabb67a | Stefan Weil | /* ROM enable bit is writable */
|
943 | 5330de09 | Michael S. Tsirkin | wmask |= PCI_ROM_ADDRESS_ENABLE; |
944 | d7ce493a | pbrook | } |
945 | b0ff8eb2 | Isaku Yamahata | pci_set_long(pci_dev->config + addr, type); |
946 | 14421258 | Isaku Yamahata | if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
|
947 | 14421258 | Isaku Yamahata | r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { |
948 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->wmask + addr, wmask); |
949 | 14421258 | Isaku Yamahata | pci_set_quad(pci_dev->cmask + addr, ~0ULL);
|
950 | 14421258 | Isaku Yamahata | } else {
|
951 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
|
952 | 14421258 | Isaku Yamahata | pci_set_long(pci_dev->cmask + addr, 0xffffffff);
|
953 | 14421258 | Isaku Yamahata | } |
954 | 79ff8cb0 | Avi Kivity | pci_dev->io_regions[region_num].memory = memory; |
955 | 5968eca3 | Avi Kivity | pci_dev->io_regions[region_num].address_space |
956 | cfc0be25 | Avi Kivity | = type & PCI_BASE_ADDRESS_SPACE_IO |
957 | 5968eca3 | Avi Kivity | ? pci_dev->bus->address_space_io |
958 | 5968eca3 | Avi Kivity | : pci_dev->bus->address_space_mem; |
959 | 79ff8cb0 | Avi Kivity | } |
960 | 79ff8cb0 | Avi Kivity | |
961 | e01fd687 | Alex Williamson | static void pci_update_vga(PCIDevice *pci_dev) |
962 | e01fd687 | Alex Williamson | { |
963 | e01fd687 | Alex Williamson | uint16_t cmd; |
964 | e01fd687 | Alex Williamson | |
965 | e01fd687 | Alex Williamson | if (!pci_dev->has_vga) {
|
966 | e01fd687 | Alex Williamson | return;
|
967 | e01fd687 | Alex Williamson | } |
968 | e01fd687 | Alex Williamson | |
969 | e01fd687 | Alex Williamson | cmd = pci_get_word(pci_dev->config + PCI_COMMAND); |
970 | e01fd687 | Alex Williamson | |
971 | e01fd687 | Alex Williamson | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM], |
972 | e01fd687 | Alex Williamson | cmd & PCI_COMMAND_MEMORY); |
973 | e01fd687 | Alex Williamson | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO], |
974 | e01fd687 | Alex Williamson | cmd & PCI_COMMAND_IO); |
975 | e01fd687 | Alex Williamson | memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI], |
976 | e01fd687 | Alex Williamson | cmd & PCI_COMMAND_IO); |
977 | e01fd687 | Alex Williamson | } |
978 | e01fd687 | Alex Williamson | |
979 | e01fd687 | Alex Williamson | void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
|
980 | e01fd687 | Alex Williamson | MemoryRegion *io_lo, MemoryRegion *io_hi) |
981 | e01fd687 | Alex Williamson | { |
982 | e01fd687 | Alex Williamson | assert(!pci_dev->has_vga); |
983 | e01fd687 | Alex Williamson | |
984 | e01fd687 | Alex Williamson | assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE); |
985 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem; |
986 | e01fd687 | Alex Williamson | memory_region_add_subregion_overlap(pci_dev->bus->address_space_mem, |
987 | e01fd687 | Alex Williamson | QEMU_PCI_VGA_MEM_BASE, mem, 1);
|
988 | e01fd687 | Alex Williamson | |
989 | e01fd687 | Alex Williamson | assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE); |
990 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo; |
991 | e01fd687 | Alex Williamson | memory_region_add_subregion_overlap(pci_dev->bus->address_space_io, |
992 | e01fd687 | Alex Williamson | QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
|
993 | e01fd687 | Alex Williamson | |
994 | e01fd687 | Alex Williamson | assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE); |
995 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi; |
996 | e01fd687 | Alex Williamson | memory_region_add_subregion_overlap(pci_dev->bus->address_space_io, |
997 | e01fd687 | Alex Williamson | QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
|
998 | e01fd687 | Alex Williamson | pci_dev->has_vga = true;
|
999 | e01fd687 | Alex Williamson | |
1000 | e01fd687 | Alex Williamson | pci_update_vga(pci_dev); |
1001 | e01fd687 | Alex Williamson | } |
1002 | e01fd687 | Alex Williamson | |
1003 | e01fd687 | Alex Williamson | void pci_unregister_vga(PCIDevice *pci_dev)
|
1004 | e01fd687 | Alex Williamson | { |
1005 | e01fd687 | Alex Williamson | if (!pci_dev->has_vga) {
|
1006 | e01fd687 | Alex Williamson | return;
|
1007 | e01fd687 | Alex Williamson | } |
1008 | e01fd687 | Alex Williamson | |
1009 | e01fd687 | Alex Williamson | memory_region_del_subregion(pci_dev->bus->address_space_mem, |
1010 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_MEM]); |
1011 | e01fd687 | Alex Williamson | memory_region_del_subregion(pci_dev->bus->address_space_io, |
1012 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]); |
1013 | e01fd687 | Alex Williamson | memory_region_del_subregion(pci_dev->bus->address_space_io, |
1014 | e01fd687 | Alex Williamson | pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]); |
1015 | e01fd687 | Alex Williamson | pci_dev->has_vga = false;
|
1016 | e01fd687 | Alex Williamson | } |
1017 | e01fd687 | Alex Williamson | |
1018 | 16a96f28 | Avi Kivity | pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
|
1019 | 16a96f28 | Avi Kivity | { |
1020 | 16a96f28 | Avi Kivity | return pci_dev->io_regions[region_num].addr;
|
1021 | 16a96f28 | Avi Kivity | } |
1022 | 16a96f28 | Avi Kivity | |
1023 | 876a350d | Michael S. Tsirkin | static pcibus_t pci_bar_address(PCIDevice *d,
|
1024 | 876a350d | Michael S. Tsirkin | int reg, uint8_t type, pcibus_t size)
|
1025 | 876a350d | Michael S. Tsirkin | { |
1026 | 876a350d | Michael S. Tsirkin | pcibus_t new_addr, last_addr; |
1027 | 876a350d | Michael S. Tsirkin | int bar = pci_bar(d, reg);
|
1028 | 876a350d | Michael S. Tsirkin | uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); |
1029 | 876a350d | Michael S. Tsirkin | |
1030 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1031 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_IO)) {
|
1032 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1033 | 876a350d | Michael S. Tsirkin | } |
1034 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar) & ~(size - 1);
|
1035 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
1036 | 876a350d | Michael S. Tsirkin | /* NOTE: we have only 64K ioports on PC */
|
1037 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) { |
1038 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1039 | 876a350d | Michael S. Tsirkin | } |
1040 | 876a350d | Michael S. Tsirkin | return new_addr;
|
1041 | 876a350d | Michael S. Tsirkin | } |
1042 | 876a350d | Michael S. Tsirkin | |
1043 | 876a350d | Michael S. Tsirkin | if (!(cmd & PCI_COMMAND_MEMORY)) {
|
1044 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1045 | 876a350d | Michael S. Tsirkin | } |
1046 | 876a350d | Michael S. Tsirkin | if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
|
1047 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_quad(d->config + bar); |
1048 | 876a350d | Michael S. Tsirkin | } else {
|
1049 | 876a350d | Michael S. Tsirkin | new_addr = pci_get_long(d->config + bar); |
1050 | 876a350d | Michael S. Tsirkin | } |
1051 | 876a350d | Michael S. Tsirkin | /* the ROM slot has a specific enable bit */
|
1052 | 876a350d | Michael S. Tsirkin | if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
|
1053 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1054 | 876a350d | Michael S. Tsirkin | } |
1055 | 876a350d | Michael S. Tsirkin | new_addr &= ~(size - 1);
|
1056 | 876a350d | Michael S. Tsirkin | last_addr = new_addr + size - 1;
|
1057 | 876a350d | Michael S. Tsirkin | /* NOTE: we do not support wrapping */
|
1058 | 876a350d | Michael S. Tsirkin | /* XXX: as we cannot support really dynamic
|
1059 | 876a350d | Michael S. Tsirkin | mappings, we handle specific values as invalid
|
1060 | 876a350d | Michael S. Tsirkin | mappings. */
|
1061 | 876a350d | Michael S. Tsirkin | if (last_addr <= new_addr || new_addr == 0 || |
1062 | 876a350d | Michael S. Tsirkin | last_addr == PCI_BAR_UNMAPPED) { |
1063 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1064 | 876a350d | Michael S. Tsirkin | } |
1065 | 876a350d | Michael S. Tsirkin | |
1066 | 876a350d | Michael S. Tsirkin | /* Now pcibus_t is 64bit.
|
1067 | 876a350d | Michael S. Tsirkin | * Check if 32 bit BAR wraps around explicitly.
|
1068 | 876a350d | Michael S. Tsirkin | * Without this, PC ide doesn't work well.
|
1069 | 876a350d | Michael S. Tsirkin | * TODO: remove this work around.
|
1070 | 876a350d | Michael S. Tsirkin | */
|
1071 | 876a350d | Michael S. Tsirkin | if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
|
1072 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1073 | 876a350d | Michael S. Tsirkin | } |
1074 | 876a350d | Michael S. Tsirkin | |
1075 | 876a350d | Michael S. Tsirkin | /*
|
1076 | 876a350d | Michael S. Tsirkin | * OS is allowed to set BAR beyond its addressable
|
1077 | 876a350d | Michael S. Tsirkin | * bits. For example, 32 bit OS can set 64bit bar
|
1078 | 876a350d | Michael S. Tsirkin | * to >4G. Check it. TODO: we might need to support
|
1079 | 876a350d | Michael S. Tsirkin | * it in the future for e.g. PAE.
|
1080 | 876a350d | Michael S. Tsirkin | */
|
1081 | a8170e5e | Avi Kivity | if (last_addr >= HWADDR_MAX) {
|
1082 | 876a350d | Michael S. Tsirkin | return PCI_BAR_UNMAPPED;
|
1083 | 876a350d | Michael S. Tsirkin | } |
1084 | 876a350d | Michael S. Tsirkin | |
1085 | 876a350d | Michael S. Tsirkin | return new_addr;
|
1086 | 876a350d | Michael S. Tsirkin | } |
1087 | 876a350d | Michael S. Tsirkin | |
1088 | 0ac32c83 | bellard | static void pci_update_mappings(PCIDevice *d) |
1089 | 0ac32c83 | bellard | { |
1090 | 0ac32c83 | bellard | PCIIORegion *r; |
1091 | 876a350d | Michael S. Tsirkin | int i;
|
1092 | 7df32ca0 | Michael S. Tsirkin | pcibus_t new_addr; |
1093 | 3b46e624 | ths | |
1094 | 8a8696a3 | bellard | for(i = 0; i < PCI_NUM_REGIONS; i++) { |
1095 | 0ac32c83 | bellard | r = &d->io_regions[i]; |
1096 | a9688570 | Isaku Yamahata | |
1097 | a9688570 | Isaku Yamahata | /* this region isn't registered */
|
1098 | ec503442 | Isaku Yamahata | if (!r->size)
|
1099 | a9688570 | Isaku Yamahata | continue;
|
1100 | a9688570 | Isaku Yamahata | |
1101 | 876a350d | Michael S. Tsirkin | new_addr = pci_bar_address(d, i, r->type, r->size); |
1102 | a9688570 | Isaku Yamahata | |
1103 | a9688570 | Isaku Yamahata | /* This bar isn't changed */
|
1104 | 7df32ca0 | Michael S. Tsirkin | if (new_addr == r->addr)
|
1105 | a9688570 | Isaku Yamahata | continue;
|
1106 | a9688570 | Isaku Yamahata | |
1107 | a9688570 | Isaku Yamahata | /* now do the real mapping */
|
1108 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
1109 | 03952339 | Avi Kivity | memory_region_del_subregion(r->address_space, r->memory); |
1110 | 0ac32c83 | bellard | } |
1111 | a9688570 | Isaku Yamahata | r->addr = new_addr; |
1112 | a9688570 | Isaku Yamahata | if (r->addr != PCI_BAR_UNMAPPED) {
|
1113 | 8b881e77 | Avi Kivity | memory_region_add_subregion_overlap(r->address_space, |
1114 | 8b881e77 | Avi Kivity | r->addr, r->memory, 1);
|
1115 | a9688570 | Isaku Yamahata | } |
1116 | 0ac32c83 | bellard | } |
1117 | e01fd687 | Alex Williamson | |
1118 | e01fd687 | Alex Williamson | pci_update_vga(d); |
1119 | 0ac32c83 | bellard | } |
1120 | 0ac32c83 | bellard | |
1121 | a7b15a5c | Michael S. Tsirkin | static inline int pci_irq_disabled(PCIDevice *d) |
1122 | a7b15a5c | Michael S. Tsirkin | { |
1123 | a7b15a5c | Michael S. Tsirkin | return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
|
1124 | a7b15a5c | Michael S. Tsirkin | } |
1125 | a7b15a5c | Michael S. Tsirkin | |
1126 | a7b15a5c | Michael S. Tsirkin | /* Called after interrupt disabled field update in config space,
|
1127 | a7b15a5c | Michael S. Tsirkin | * assert/deassert interrupts if necessary.
|
1128 | a7b15a5c | Michael S. Tsirkin | * Gets original interrupt disable bit value (before update). */
|
1129 | a7b15a5c | Michael S. Tsirkin | static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) |
1130 | a7b15a5c | Michael S. Tsirkin | { |
1131 | a7b15a5c | Michael S. Tsirkin | int i, disabled = pci_irq_disabled(d);
|
1132 | a7b15a5c | Michael S. Tsirkin | if (disabled == was_irq_disabled)
|
1133 | a7b15a5c | Michael S. Tsirkin | return;
|
1134 | a7b15a5c | Michael S. Tsirkin | for (i = 0; i < PCI_NUM_PINS; ++i) { |
1135 | a7b15a5c | Michael S. Tsirkin | int state = pci_irq_state(d, i);
|
1136 | a7b15a5c | Michael S. Tsirkin | pci_change_irq_level(d, i, disabled ? -state : state); |
1137 | a7b15a5c | Michael S. Tsirkin | } |
1138 | a7b15a5c | Michael S. Tsirkin | } |
1139 | a7b15a5c | Michael S. Tsirkin | |
1140 | 5fafdf24 | ths | uint32_t pci_default_read_config(PCIDevice *d, |
1141 | 0ac32c83 | bellard | uint32_t address, int len)
|
1142 | 69b91039 | bellard | { |
1143 | 5029fe12 | Isaku Yamahata | uint32_t val = 0;
|
1144 | 42e4126b | Jan Kiszka | |
1145 | 5029fe12 | Isaku Yamahata | memcpy(&val, d->config + address, len); |
1146 | 5029fe12 | Isaku Yamahata | return le32_to_cpu(val);
|
1147 | 0ac32c83 | bellard | } |
1148 | 0ac32c83 | bellard | |
1149 | b7ee1603 | Michael S. Tsirkin | void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) |
1150 | 0ac32c83 | bellard | { |
1151 | a7b15a5c | Michael S. Tsirkin | int i, was_irq_disabled = pci_irq_disabled(d);
|
1152 | 0ac32c83 | bellard | |
1153 | 42e4126b | Jan Kiszka | for (i = 0; i < l; val >>= 8, ++i) { |
1154 | 91011d4f | Stefan Weil | uint8_t wmask = d->wmask[addr + i]; |
1155 | 92ba5f51 | Isaku Yamahata | uint8_t w1cmask = d->w1cmask[addr + i]; |
1156 | 92ba5f51 | Isaku Yamahata | assert(!(wmask & w1cmask)); |
1157 | 91011d4f | Stefan Weil | d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); |
1158 | 92ba5f51 | Isaku Yamahata | d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
|
1159 | 0ac32c83 | bellard | } |
1160 | 260c0cd3 | Isaku Yamahata | if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || |
1161 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
|
1162 | edb00035 | Isaku Yamahata | ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
|
1163 | 260c0cd3 | Isaku Yamahata | range_covers_byte(addr, l, PCI_COMMAND)) |
1164 | 0ac32c83 | bellard | pci_update_mappings(d); |
1165 | a7b15a5c | Michael S. Tsirkin | |
1166 | 1c380f94 | Avi Kivity | if (range_covers_byte(addr, l, PCI_COMMAND)) {
|
1167 | a7b15a5c | Michael S. Tsirkin | pci_update_irq_disabled(d, was_irq_disabled); |
1168 | 1c380f94 | Avi Kivity | memory_region_set_enabled(&d->bus_master_enable_region, |
1169 | 1c380f94 | Avi Kivity | pci_get_word(d->config + PCI_COMMAND) |
1170 | 1c380f94 | Avi Kivity | & PCI_COMMAND_MASTER); |
1171 | 1c380f94 | Avi Kivity | } |
1172 | 95d65800 | Jan Kiszka | |
1173 | 95d65800 | Jan Kiszka | msi_write_config(d, addr, val, l); |
1174 | 95d65800 | Jan Kiszka | msix_write_config(d, addr, val, l); |
1175 | 69b91039 | bellard | } |
1176 | 69b91039 | bellard | |
1177 | 502a5395 | pbrook | /***********************************************************/
|
1178 | 502a5395 | pbrook | /* generic PCI irq support */
|
1179 | 30468f78 | bellard | |
1180 | 502a5395 | pbrook | /* 0 <= irq_num <= 3. level must be 0 or 1 */
|
1181 | d537cf6c | pbrook | static void pci_set_irq(void *opaque, int irq_num, int level) |
1182 | 69b91039 | bellard | { |
1183 | a60380a5 | Juan Quintela | PCIDevice *pci_dev = opaque; |
1184 | 80b3ada7 | pbrook | int change;
|
1185 | 3b46e624 | ths | |
1186 | d036bb21 | Michael S. Tsirkin | change = level - pci_irq_state(pci_dev, irq_num); |
1187 | 80b3ada7 | pbrook | if (!change)
|
1188 | 80b3ada7 | pbrook | return;
|
1189 | d2b59317 | pbrook | |
1190 | d036bb21 | Michael S. Tsirkin | pci_set_irq_state(pci_dev, irq_num, level); |
1191 | f9bf77dd | Michael S. Tsirkin | pci_update_irq_status(pci_dev); |
1192 | a7b15a5c | Michael S. Tsirkin | if (pci_irq_disabled(pci_dev))
|
1193 | a7b15a5c | Michael S. Tsirkin | return;
|
1194 | d036bb21 | Michael S. Tsirkin | pci_change_irq_level(pci_dev, irq_num, change); |
1195 | 69b91039 | bellard | } |
1196 | 69b91039 | bellard | |
1197 | 3afa9bb4 | Michael S. Tsirkin | /* Special hooks used by device assignment */
|
1198 | 3afa9bb4 | Michael S. Tsirkin | void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
|
1199 | 3afa9bb4 | Michael S. Tsirkin | { |
1200 | 0889464a | Alex Williamson | assert(pci_bus_is_root(bus)); |
1201 | 3afa9bb4 | Michael S. Tsirkin | bus->route_intx_to_irq = route_intx_to_irq; |
1202 | 3afa9bb4 | Michael S. Tsirkin | } |
1203 | 3afa9bb4 | Michael S. Tsirkin | |
1204 | 3afa9bb4 | Michael S. Tsirkin | PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
|
1205 | 3afa9bb4 | Michael S. Tsirkin | { |
1206 | 3afa9bb4 | Michael S. Tsirkin | PCIBus *bus; |
1207 | 3afa9bb4 | Michael S. Tsirkin | |
1208 | 3afa9bb4 | Michael S. Tsirkin | do {
|
1209 | 3afa9bb4 | Michael S. Tsirkin | bus = dev->bus; |
1210 | 3afa9bb4 | Michael S. Tsirkin | pin = bus->map_irq(dev, pin); |
1211 | 3afa9bb4 | Michael S. Tsirkin | dev = bus->parent_dev; |
1212 | 3afa9bb4 | Michael S. Tsirkin | } while (dev);
|
1213 | 05c0621e | Alex Williamson | |
1214 | 05c0621e | Alex Williamson | if (!bus->route_intx_to_irq) {
|
1215 | 312fd5f2 | Markus Armbruster | error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
|
1216 | 05c0621e | Alex Williamson | object_get_typename(OBJECT(bus->qbus.parent))); |
1217 | 05c0621e | Alex Williamson | return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 }; |
1218 | 05c0621e | Alex Williamson | } |
1219 | 05c0621e | Alex Williamson | |
1220 | 3afa9bb4 | Michael S. Tsirkin | return bus->route_intx_to_irq(bus->irq_opaque, pin);
|
1221 | 0ae16251 | Jan Kiszka | } |
1222 | 0ae16251 | Jan Kiszka | |
1223 | d6e65d54 | Alex Williamson | bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
|
1224 | d6e65d54 | Alex Williamson | { |
1225 | d6e65d54 | Alex Williamson | return old->mode != new->mode || old->irq != new->irq;
|
1226 | d6e65d54 | Alex Williamson | } |
1227 | d6e65d54 | Alex Williamson | |
1228 | 0ae16251 | Jan Kiszka | void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
|
1229 | 0ae16251 | Jan Kiszka | { |
1230 | 0ae16251 | Jan Kiszka | PCIDevice *dev; |
1231 | 0ae16251 | Jan Kiszka | PCIBus *sec; |
1232 | 0ae16251 | Jan Kiszka | int i;
|
1233 | 0ae16251 | Jan Kiszka | |
1234 | 0ae16251 | Jan Kiszka | for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { |
1235 | 0ae16251 | Jan Kiszka | dev = bus->devices[i]; |
1236 | 0ae16251 | Jan Kiszka | if (dev && dev->intx_routing_notifier) {
|
1237 | 0ae16251 | Jan Kiszka | dev->intx_routing_notifier(dev); |
1238 | 0ae16251 | Jan Kiszka | } |
1239 | e5368f0d | Alex Williamson | } |
1240 | e5368f0d | Alex Williamson | |
1241 | e5368f0d | Alex Williamson | QLIST_FOREACH(sec, &bus->child, sibling) { |
1242 | e5368f0d | Alex Williamson | pci_bus_fire_intx_routing_notifier(sec); |
1243 | 0ae16251 | Jan Kiszka | } |
1244 | 0ae16251 | Jan Kiszka | } |
1245 | 0ae16251 | Jan Kiszka | |
1246 | 0ae16251 | Jan Kiszka | void pci_device_set_intx_routing_notifier(PCIDevice *dev,
|
1247 | 0ae16251 | Jan Kiszka | PCIINTxRoutingNotifier notifier) |
1248 | 0ae16251 | Jan Kiszka | { |
1249 | 0ae16251 | Jan Kiszka | dev->intx_routing_notifier = notifier; |
1250 | 69b91039 | bellard | } |
1251 | 69b91039 | bellard | |
1252 | 91e56159 | Isaku Yamahata | /*
|
1253 | 91e56159 | Isaku Yamahata | * PCI-to-PCI bridge specification
|
1254 | 91e56159 | Isaku Yamahata | * 9.1: Interrupt routing. Table 9-1
|
1255 | 91e56159 | Isaku Yamahata | *
|
1256 | 91e56159 | Isaku Yamahata | * the PCI Express Base Specification, Revision 2.1
|
1257 | 91e56159 | Isaku Yamahata | * 2.2.8.1: INTx interrutp signaling - Rules
|
1258 | 91e56159 | Isaku Yamahata | * the Implementation Note
|
1259 | 91e56159 | Isaku Yamahata | * Table 2-20
|
1260 | 91e56159 | Isaku Yamahata | */
|
1261 | 91e56159 | Isaku Yamahata | /*
|
1262 | 91e56159 | Isaku Yamahata | * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
|
1263 | 91e56159 | Isaku Yamahata | * 0-origin unlike PCI interrupt pin register.
|
1264 | 91e56159 | Isaku Yamahata | */
|
1265 | 91e56159 | Isaku Yamahata | int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin) |
1266 | 91e56159 | Isaku Yamahata | { |
1267 | 91e56159 | Isaku Yamahata | return (pin + PCI_SLOT(pci_dev->devfn)) % PCI_NUM_PINS;
|
1268 | 91e56159 | Isaku Yamahata | } |
1269 | 91e56159 | Isaku Yamahata | |
1270 | 502a5395 | pbrook | /***********************************************************/
|
1271 | 502a5395 | pbrook | /* monitor info on PCI */
|
1272 | 0ac32c83 | bellard | |
1273 | 6650ee6d | pbrook | typedef struct { |
1274 | 6650ee6d | pbrook | uint16_t class; |
1275 | 6650ee6d | pbrook | const char *desc; |
1276 | 5e0259e7 | Gleb Natapov | const char *fw_name; |
1277 | 5e0259e7 | Gleb Natapov | uint16_t fw_ign_bits; |
1278 | 6650ee6d | pbrook | } pci_class_desc; |
1279 | 6650ee6d | pbrook | |
1280 | 09bc878a | blueswir1 | static const pci_class_desc pci_class_descriptions[] = |
1281 | 6650ee6d | pbrook | { |
1282 | 5e0259e7 | Gleb Natapov | { 0x0001, "VGA controller", "display"}, |
1283 | 5e0259e7 | Gleb Natapov | { 0x0100, "SCSI controller", "scsi"}, |
1284 | 5e0259e7 | Gleb Natapov | { 0x0101, "IDE controller", "ide"}, |
1285 | 5e0259e7 | Gleb Natapov | { 0x0102, "Floppy controller", "fdc"}, |
1286 | 5e0259e7 | Gleb Natapov | { 0x0103, "IPI controller", "ipi"}, |
1287 | 5e0259e7 | Gleb Natapov | { 0x0104, "RAID controller", "raid"}, |
1288 | dcb5b19a | ths | { 0x0106, "SATA controller"}, |
1289 | dcb5b19a | ths | { 0x0107, "SAS controller"}, |
1290 | dcb5b19a | ths | { 0x0180, "Storage controller"}, |
1291 | 5e0259e7 | Gleb Natapov | { 0x0200, "Ethernet controller", "ethernet"}, |
1292 | 5e0259e7 | Gleb Natapov | { 0x0201, "Token Ring controller", "token-ring"}, |
1293 | 5e0259e7 | Gleb Natapov | { 0x0202, "FDDI controller", "fddi"}, |
1294 | 5e0259e7 | Gleb Natapov | { 0x0203, "ATM controller", "atm"}, |
1295 | dcb5b19a | ths | { 0x0280, "Network controller"}, |
1296 | 5e0259e7 | Gleb Natapov | { 0x0300, "VGA controller", "display", 0x00ff}, |
1297 | dcb5b19a | ths | { 0x0301, "XGA controller"}, |
1298 | dcb5b19a | ths | { 0x0302, "3D controller"}, |
1299 | dcb5b19a | ths | { 0x0380, "Display controller"}, |
1300 | 5e0259e7 | Gleb Natapov | { 0x0400, "Video controller", "video"}, |
1301 | 5e0259e7 | Gleb Natapov | { 0x0401, "Audio controller", "sound"}, |
1302 | dcb5b19a | ths | { 0x0402, "Phone"}, |
1303 | 602ef4d9 | Jan Kiszka | { 0x0403, "Audio controller", "sound"}, |
1304 | dcb5b19a | ths | { 0x0480, "Multimedia controller"}, |
1305 | 5e0259e7 | Gleb Natapov | { 0x0500, "RAM controller", "memory"}, |
1306 | 5e0259e7 | Gleb Natapov | { 0x0501, "Flash controller", "flash"}, |
1307 | dcb5b19a | ths | { 0x0580, "Memory controller"}, |
1308 | 5e0259e7 | Gleb Natapov | { 0x0600, "Host bridge", "host"}, |
1309 | 5e0259e7 | Gleb Natapov | { 0x0601, "ISA bridge", "isa"}, |
1310 | 5e0259e7 | Gleb Natapov | { 0x0602, "EISA bridge", "eisa"}, |
1311 | 5e0259e7 | Gleb Natapov | { 0x0603, "MC bridge", "mca"}, |
1312 | 5e0259e7 | Gleb Natapov | { 0x0604, "PCI bridge", "pci"}, |
1313 | 5e0259e7 | Gleb Natapov | { 0x0605, "PCMCIA bridge", "pcmcia"}, |
1314 | 5e0259e7 | Gleb Natapov | { 0x0606, "NUBUS bridge", "nubus"}, |
1315 | 5e0259e7 | Gleb Natapov | { 0x0607, "CARDBUS bridge", "cardbus"}, |
1316 | dcb5b19a | ths | { 0x0608, "RACEWAY bridge"}, |
1317 | dcb5b19a | ths | { 0x0680, "Bridge"}, |
1318 | 5e0259e7 | Gleb Natapov | { 0x0700, "Serial port", "serial"}, |
1319 | 5e0259e7 | Gleb Natapov | { 0x0701, "Parallel port", "parallel"}, |
1320 | 5e0259e7 | Gleb Natapov | { 0x0800, "Interrupt controller", "interrupt-controller"}, |
1321 | 5e0259e7 | Gleb Natapov | { 0x0801, "DMA controller", "dma-controller"}, |
1322 | 5e0259e7 | Gleb Natapov | { 0x0802, "Timer", "timer"}, |
1323 | 5e0259e7 | Gleb Natapov | { 0x0803, "RTC", "rtc"}, |
1324 | 5e0259e7 | Gleb Natapov | { 0x0900, "Keyboard", "keyboard"}, |
1325 | 5e0259e7 | Gleb Natapov | { 0x0901, "Pen", "pen"}, |
1326 | 5e0259e7 | Gleb Natapov | { 0x0902, "Mouse", "mouse"}, |
1327 | 5e0259e7 | Gleb Natapov | { 0x0A00, "Dock station", "dock", 0x00ff}, |
1328 | 5e0259e7 | Gleb Natapov | { 0x0B00, "i386 cpu", "cpu", 0x00ff}, |
1329 | 5e0259e7 | Gleb Natapov | { 0x0c00, "Fireware contorller", "fireware"}, |
1330 | 5e0259e7 | Gleb Natapov | { 0x0c01, "Access bus controller", "access-bus"}, |
1331 | 5e0259e7 | Gleb Natapov | { 0x0c02, "SSA controller", "ssa"}, |
1332 | 5e0259e7 | Gleb Natapov | { 0x0c03, "USB controller", "usb"}, |
1333 | 5e0259e7 | Gleb Natapov | { 0x0c04, "Fibre channel controller", "fibre-channel"}, |
1334 | f7748569 | Jan Kiszka | { 0x0c05, "SMBus"}, |
1335 | 6650ee6d | pbrook | { 0, NULL} |
1336 | 6650ee6d | pbrook | }; |
1337 | 6650ee6d | pbrook | |
1338 | 163c8a59 | Luiz Capitulino | static void pci_for_each_device_under_bus(PCIBus *bus, |
1339 | 7aa8cbb9 | Anthony PERARD | void (*fn)(PCIBus *b, PCIDevice *d,
|
1340 | 7aa8cbb9 | Anthony PERARD | void *opaque),
|
1341 | 7aa8cbb9 | Anthony PERARD | void *opaque)
|
1342 | 30468f78 | bellard | { |
1343 | 163c8a59 | Luiz Capitulino | PCIDevice *d; |
1344 | 163c8a59 | Luiz Capitulino | int devfn;
|
1345 | 30468f78 | bellard | |
1346 | 163c8a59 | Luiz Capitulino | for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1347 | 163c8a59 | Luiz Capitulino | d = bus->devices[devfn]; |
1348 | 163c8a59 | Luiz Capitulino | if (d) {
|
1349 | 7aa8cbb9 | Anthony PERARD | fn(bus, d, opaque); |
1350 | 163c8a59 | Luiz Capitulino | } |
1351 | 163c8a59 | Luiz Capitulino | } |
1352 | 163c8a59 | Luiz Capitulino | } |
1353 | 163c8a59 | Luiz Capitulino | |
1354 | 163c8a59 | Luiz Capitulino | void pci_for_each_device(PCIBus *bus, int bus_num, |
1355 | 7aa8cbb9 | Anthony PERARD | void (*fn)(PCIBus *b, PCIDevice *d, void *opaque), |
1356 | 7aa8cbb9 | Anthony PERARD | void *opaque)
|
1357 | 163c8a59 | Luiz Capitulino | { |
1358 | d662210a | Michael S. Tsirkin | bus = pci_find_bus_nr(bus, bus_num); |
1359 | 163c8a59 | Luiz Capitulino | |
1360 | 163c8a59 | Luiz Capitulino | if (bus) {
|
1361 | 7aa8cbb9 | Anthony PERARD | pci_for_each_device_under_bus(bus, fn, opaque); |
1362 | 163c8a59 | Luiz Capitulino | } |
1363 | 163c8a59 | Luiz Capitulino | } |
1364 | 163c8a59 | Luiz Capitulino | |
1365 | 79627472 | Luiz Capitulino | static const pci_class_desc *get_class_desc(int class) |
1366 | 163c8a59 | Luiz Capitulino | { |
1367 | 79627472 | Luiz Capitulino | const pci_class_desc *desc;
|
1368 | 163c8a59 | Luiz Capitulino | |
1369 | 79627472 | Luiz Capitulino | desc = pci_class_descriptions; |
1370 | 79627472 | Luiz Capitulino | while (desc->desc && class != desc->class) {
|
1371 | 79627472 | Luiz Capitulino | desc++; |
1372 | 30468f78 | bellard | } |
1373 | b4dccd8d | Isaku Yamahata | |
1374 | 79627472 | Luiz Capitulino | return desc;
|
1375 | 79627472 | Luiz Capitulino | } |
1376 | 14421258 | Isaku Yamahata | |
1377 | 79627472 | Luiz Capitulino | static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num); |
1378 | 163c8a59 | Luiz Capitulino | |
1379 | 79627472 | Luiz Capitulino | static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev) |
1380 | 79627472 | Luiz Capitulino | { |
1381 | 79627472 | Luiz Capitulino | PciMemoryRegionList *head = NULL, *cur_item = NULL; |
1382 | 79627472 | Luiz Capitulino | int i;
|
1383 | 163c8a59 | Luiz Capitulino | |
1384 | 79627472 | Luiz Capitulino | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
1385 | 79627472 | Luiz Capitulino | const PCIIORegion *r = &dev->io_regions[i];
|
1386 | 79627472 | Luiz Capitulino | PciMemoryRegionList *region; |
1387 | 79627472 | Luiz Capitulino | |
1388 | 79627472 | Luiz Capitulino | if (!r->size) {
|
1389 | 79627472 | Luiz Capitulino | continue;
|
1390 | 502a5395 | pbrook | } |
1391 | 163c8a59 | Luiz Capitulino | |
1392 | 79627472 | Luiz Capitulino | region = g_malloc0(sizeof(*region));
|
1393 | 79627472 | Luiz Capitulino | region->value = g_malloc0(sizeof(*region->value));
|
1394 | 163c8a59 | Luiz Capitulino | |
1395 | 79627472 | Luiz Capitulino | if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
|
1396 | 79627472 | Luiz Capitulino | region->value->type = g_strdup("io");
|
1397 | 79627472 | Luiz Capitulino | } else {
|
1398 | 79627472 | Luiz Capitulino | region->value->type = g_strdup("memory");
|
1399 | 79627472 | Luiz Capitulino | region->value->has_prefetch = true;
|
1400 | 79627472 | Luiz Capitulino | region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH); |
1401 | 79627472 | Luiz Capitulino | region->value->has_mem_type_64 = true;
|
1402 | 79627472 | Luiz Capitulino | region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64); |
1403 | d5e4acf7 | Luiz Capitulino | } |
1404 | 163c8a59 | Luiz Capitulino | |
1405 | 79627472 | Luiz Capitulino | region->value->bar = i; |
1406 | 79627472 | Luiz Capitulino | region->value->address = r->addr; |
1407 | 79627472 | Luiz Capitulino | region->value->size = r->size; |
1408 | 163c8a59 | Luiz Capitulino | |
1409 | 79627472 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
1410 | 79627472 | Luiz Capitulino | if (!cur_item) {
|
1411 | 79627472 | Luiz Capitulino | head = cur_item = region; |
1412 | 79627472 | Luiz Capitulino | } else {
|
1413 | 79627472 | Luiz Capitulino | cur_item->next = region; |
1414 | 79627472 | Luiz Capitulino | cur_item = region; |
1415 | 163c8a59 | Luiz Capitulino | } |
1416 | 80b3ada7 | pbrook | } |
1417 | 384d8876 | bellard | |
1418 | 79627472 | Luiz Capitulino | return head;
|
1419 | 163c8a59 | Luiz Capitulino | } |
1420 | 163c8a59 | Luiz Capitulino | |
1421 | 79627472 | Luiz Capitulino | static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
|
1422 | 79627472 | Luiz Capitulino | int bus_num)
|
1423 | 163c8a59 | Luiz Capitulino | { |
1424 | 79627472 | Luiz Capitulino | PciBridgeInfo *info; |
1425 | 163c8a59 | Luiz Capitulino | |
1426 | 79627472 | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
1427 | 163c8a59 | Luiz Capitulino | |
1428 | 79627472 | Luiz Capitulino | info->bus.number = dev->config[PCI_PRIMARY_BUS]; |
1429 | 79627472 | Luiz Capitulino | info->bus.secondary = dev->config[PCI_SECONDARY_BUS]; |
1430 | 79627472 | Luiz Capitulino | info->bus.subordinate = dev->config[PCI_SUBORDINATE_BUS]; |
1431 | 163c8a59 | Luiz Capitulino | |
1432 | 79627472 | Luiz Capitulino | info->bus.io_range = g_malloc0(sizeof(*info->bus.io_range));
|
1433 | 79627472 | Luiz Capitulino | info->bus.io_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO); |
1434 | 79627472 | Luiz Capitulino | info->bus.io_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO); |
1435 | 163c8a59 | Luiz Capitulino | |
1436 | 79627472 | Luiz Capitulino | info->bus.memory_range = g_malloc0(sizeof(*info->bus.memory_range));
|
1437 | 79627472 | Luiz Capitulino | info->bus.memory_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
1438 | 79627472 | Luiz Capitulino | info->bus.memory_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY); |
1439 | 163c8a59 | Luiz Capitulino | |
1440 | 79627472 | Luiz Capitulino | info->bus.prefetchable_range = g_malloc0(sizeof(*info->bus.prefetchable_range));
|
1441 | 79627472 | Luiz Capitulino | info->bus.prefetchable_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
1442 | 79627472 | Luiz Capitulino | info->bus.prefetchable_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); |
1443 | 163c8a59 | Luiz Capitulino | |
1444 | 79627472 | Luiz Capitulino | if (dev->config[PCI_SECONDARY_BUS] != 0) { |
1445 | d662210a | Michael S. Tsirkin | PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]); |
1446 | 79627472 | Luiz Capitulino | if (child_bus) {
|
1447 | 79627472 | Luiz Capitulino | info->has_devices = true;
|
1448 | 79627472 | Luiz Capitulino | info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]); |
1449 | 79627472 | Luiz Capitulino | } |
1450 | 163c8a59 | Luiz Capitulino | } |
1451 | 163c8a59 | Luiz Capitulino | |
1452 | 79627472 | Luiz Capitulino | return info;
|
1453 | 163c8a59 | Luiz Capitulino | } |
1454 | 163c8a59 | Luiz Capitulino | |
1455 | 79627472 | Luiz Capitulino | static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
|
1456 | 79627472 | Luiz Capitulino | int bus_num)
|
1457 | 163c8a59 | Luiz Capitulino | { |
1458 | 79627472 | Luiz Capitulino | const pci_class_desc *desc;
|
1459 | 79627472 | Luiz Capitulino | PciDeviceInfo *info; |
1460 | b5937f29 | Isaku Yamahata | uint8_t type; |
1461 | 79627472 | Luiz Capitulino | int class;
|
1462 | 163c8a59 | Luiz Capitulino | |
1463 | 79627472 | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
1464 | 79627472 | Luiz Capitulino | info->bus = bus_num; |
1465 | 79627472 | Luiz Capitulino | info->slot = PCI_SLOT(dev->devfn); |
1466 | 79627472 | Luiz Capitulino | info->function = PCI_FUNC(dev->devfn); |
1467 | 79627472 | Luiz Capitulino | |
1468 | 79627472 | Luiz Capitulino | class = pci_get_word(dev->config + PCI_CLASS_DEVICE); |
1469 | 79627472 | Luiz Capitulino | info->class_info.class = class; |
1470 | 79627472 | Luiz Capitulino | desc = get_class_desc(class); |
1471 | 79627472 | Luiz Capitulino | if (desc->desc) {
|
1472 | 79627472 | Luiz Capitulino | info->class_info.has_desc = true;
|
1473 | 79627472 | Luiz Capitulino | info->class_info.desc = g_strdup(desc->desc); |
1474 | 79627472 | Luiz Capitulino | } |
1475 | 79627472 | Luiz Capitulino | |
1476 | 79627472 | Luiz Capitulino | info->id.vendor = pci_get_word(dev->config + PCI_VENDOR_ID); |
1477 | 79627472 | Luiz Capitulino | info->id.device = pci_get_word(dev->config + PCI_DEVICE_ID); |
1478 | 79627472 | Luiz Capitulino | info->regions = qmp_query_pci_regions(dev); |
1479 | 79627472 | Luiz Capitulino | info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
|
1480 | 163c8a59 | Luiz Capitulino | |
1481 | 163c8a59 | Luiz Capitulino | if (dev->config[PCI_INTERRUPT_PIN] != 0) { |
1482 | 79627472 | Luiz Capitulino | info->has_irq = true;
|
1483 | 79627472 | Luiz Capitulino | info->irq = dev->config[PCI_INTERRUPT_LINE]; |
1484 | 163c8a59 | Luiz Capitulino | } |
1485 | 163c8a59 | Luiz Capitulino | |
1486 | b5937f29 | Isaku Yamahata | type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; |
1487 | b5937f29 | Isaku Yamahata | if (type == PCI_HEADER_TYPE_BRIDGE) {
|
1488 | 79627472 | Luiz Capitulino | info->has_pci_bridge = true;
|
1489 | 79627472 | Luiz Capitulino | info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num); |
1490 | 163c8a59 | Luiz Capitulino | } |
1491 | 163c8a59 | Luiz Capitulino | |
1492 | 79627472 | Luiz Capitulino | return info;
|
1493 | 163c8a59 | Luiz Capitulino | } |
1494 | 163c8a59 | Luiz Capitulino | |
1495 | 79627472 | Luiz Capitulino | static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num) |
1496 | 384d8876 | bellard | { |
1497 | 79627472 | Luiz Capitulino | PciDeviceInfoList *info, *head = NULL, *cur_item = NULL; |
1498 | 163c8a59 | Luiz Capitulino | PCIDevice *dev; |
1499 | 79627472 | Luiz Capitulino | int devfn;
|
1500 | 163c8a59 | Luiz Capitulino | |
1501 | 163c8a59 | Luiz Capitulino | for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { |
1502 | 163c8a59 | Luiz Capitulino | dev = bus->devices[devfn]; |
1503 | 163c8a59 | Luiz Capitulino | if (dev) {
|
1504 | 79627472 | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
1505 | 79627472 | Luiz Capitulino | info->value = qmp_query_pci_device(dev, bus, bus_num); |
1506 | 79627472 | Luiz Capitulino | |
1507 | 79627472 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
1508 | 79627472 | Luiz Capitulino | if (!cur_item) {
|
1509 | 79627472 | Luiz Capitulino | head = cur_item = info; |
1510 | 79627472 | Luiz Capitulino | } else {
|
1511 | 79627472 | Luiz Capitulino | cur_item->next = info; |
1512 | 79627472 | Luiz Capitulino | cur_item = info; |
1513 | 79627472 | Luiz Capitulino | } |
1514 | 163c8a59 | Luiz Capitulino | } |
1515 | 1074df4f | Isaku Yamahata | } |
1516 | 163c8a59 | Luiz Capitulino | |
1517 | 79627472 | Luiz Capitulino | return head;
|
1518 | 1074df4f | Isaku Yamahata | } |
1519 | 1074df4f | Isaku Yamahata | |
1520 | 79627472 | Luiz Capitulino | static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num) |
1521 | 1074df4f | Isaku Yamahata | { |
1522 | 79627472 | Luiz Capitulino | PciInfo *info = NULL;
|
1523 | 79627472 | Luiz Capitulino | |
1524 | d662210a | Michael S. Tsirkin | bus = pci_find_bus_nr(bus, bus_num); |
1525 | 502a5395 | pbrook | if (bus) {
|
1526 | 79627472 | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
1527 | 79627472 | Luiz Capitulino | info->bus = bus_num; |
1528 | 79627472 | Luiz Capitulino | info->devices = qmp_query_pci_devices(bus, bus_num); |
1529 | f2aa58c6 | bellard | } |
1530 | 163c8a59 | Luiz Capitulino | |
1531 | 79627472 | Luiz Capitulino | return info;
|
1532 | f2aa58c6 | bellard | } |
1533 | f2aa58c6 | bellard | |
1534 | 79627472 | Luiz Capitulino | PciInfoList *qmp_query_pci(Error **errp) |
1535 | f2aa58c6 | bellard | { |
1536 | 79627472 | Luiz Capitulino | PciInfoList *info, *head = NULL, *cur_item = NULL; |
1537 | e822a52a | Isaku Yamahata | struct PCIHostBus *host;
|
1538 | 163c8a59 | Luiz Capitulino | |
1539 | e822a52a | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
1540 | 79627472 | Luiz Capitulino | info = g_malloc0(sizeof(*info));
|
1541 | 79627472 | Luiz Capitulino | info->value = qmp_query_pci_bus(host->bus, 0);
|
1542 | 79627472 | Luiz Capitulino | |
1543 | 79627472 | Luiz Capitulino | /* XXX: waiting for the qapi to support GSList */
|
1544 | 79627472 | Luiz Capitulino | if (!cur_item) {
|
1545 | 79627472 | Luiz Capitulino | head = cur_item = info; |
1546 | 79627472 | Luiz Capitulino | } else {
|
1547 | 79627472 | Luiz Capitulino | cur_item->next = info; |
1548 | 79627472 | Luiz Capitulino | cur_item = info; |
1549 | 163c8a59 | Luiz Capitulino | } |
1550 | e822a52a | Isaku Yamahata | } |
1551 | 163c8a59 | Luiz Capitulino | |
1552 | 79627472 | Luiz Capitulino | return head;
|
1553 | 77d4bc34 | bellard | } |
1554 | a41b2ff2 | pbrook | |
1555 | cb457d76 | aliguori | static const char * const pci_nic_models[] = { |
1556 | cb457d76 | aliguori | "ne2k_pci",
|
1557 | cb457d76 | aliguori | "i82551",
|
1558 | cb457d76 | aliguori | "i82557b",
|
1559 | cb457d76 | aliguori | "i82559er",
|
1560 | cb457d76 | aliguori | "rtl8139",
|
1561 | cb457d76 | aliguori | "e1000",
|
1562 | cb457d76 | aliguori | "pcnet",
|
1563 | cb457d76 | aliguori | "virtio",
|
1564 | cb457d76 | aliguori | NULL
|
1565 | cb457d76 | aliguori | }; |
1566 | cb457d76 | aliguori | |
1567 | 9d07d757 | Paul Brook | static const char * const pci_nic_names[] = { |
1568 | 9d07d757 | Paul Brook | "ne2k_pci",
|
1569 | 9d07d757 | Paul Brook | "i82551",
|
1570 | 9d07d757 | Paul Brook | "i82557b",
|
1571 | 9d07d757 | Paul Brook | "i82559er",
|
1572 | 9d07d757 | Paul Brook | "rtl8139",
|
1573 | 9d07d757 | Paul Brook | "e1000",
|
1574 | 9d07d757 | Paul Brook | "pcnet",
|
1575 | 53c25cea | Paul Brook | "virtio-net-pci",
|
1576 | cb457d76 | aliguori | NULL
|
1577 | cb457d76 | aliguori | }; |
1578 | cb457d76 | aliguori | |
1579 | a41b2ff2 | pbrook | /* Initialize a PCI NIC. */
|
1580 | 33e66b86 | Markus Armbruster | /* FIXME callers should check for failure, but don't */
|
1581 | 29b358f9 | David Gibson | PCIDevice *pci_nic_init(NICInfo *nd, PCIBus *rootbus, |
1582 | 29b358f9 | David Gibson | const char *default_model, |
1583 | 5607c388 | Markus Armbruster | const char *default_devaddr) |
1584 | a41b2ff2 | pbrook | { |
1585 | 5607c388 | Markus Armbruster | const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; |
1586 | 07caea31 | Markus Armbruster | PCIBus *bus; |
1587 | 07caea31 | Markus Armbruster | int devfn;
|
1588 | 5607c388 | Markus Armbruster | PCIDevice *pci_dev; |
1589 | 9d07d757 | Paul Brook | DeviceState *dev; |
1590 | cb457d76 | aliguori | int i;
|
1591 | cb457d76 | aliguori | |
1592 | 07caea31 | Markus Armbruster | i = qemu_find_nic_model(nd, pci_nic_models, default_model); |
1593 | 07caea31 | Markus Armbruster | if (i < 0) |
1594 | 07caea31 | Markus Armbruster | return NULL; |
1595 | 07caea31 | Markus Armbruster | |
1596 | 29b358f9 | David Gibson | bus = pci_get_bus_devfn(&devfn, rootbus, devaddr); |
1597 | 07caea31 | Markus Armbruster | if (!bus) {
|
1598 | 1ecda02b | Markus Armbruster | error_report("Invalid PCI device address %s for device %s",
|
1599 | 1ecda02b | Markus Armbruster | devaddr, pci_nic_names[i]); |
1600 | 07caea31 | Markus Armbruster | return NULL; |
1601 | 07caea31 | Markus Armbruster | } |
1602 | 07caea31 | Markus Armbruster | |
1603 | 499cf102 | Markus Armbruster | pci_dev = pci_create(bus, devfn, pci_nic_names[i]); |
1604 | 9ee05825 | Markus Armbruster | dev = &pci_dev->qdev; |
1605 | 1cc33683 | Gerd Hoffmann | qdev_set_nic_properties(dev, nd); |
1606 | 07caea31 | Markus Armbruster | if (qdev_init(dev) < 0) |
1607 | 07caea31 | Markus Armbruster | return NULL; |
1608 | 9ee05825 | Markus Armbruster | return pci_dev;
|
1609 | a41b2ff2 | pbrook | } |
1610 | a41b2ff2 | pbrook | |
1611 | 29b358f9 | David Gibson | PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, |
1612 | 29b358f9 | David Gibson | const char *default_model, |
1613 | 07caea31 | Markus Armbruster | const char *default_devaddr) |
1614 | 07caea31 | Markus Armbruster | { |
1615 | 07caea31 | Markus Armbruster | PCIDevice *res; |
1616 | 07caea31 | Markus Armbruster | |
1617 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, pci_nic_models))
|
1618 | 07caea31 | Markus Armbruster | exit(0);
|
1619 | 07caea31 | Markus Armbruster | |
1620 | 29b358f9 | David Gibson | res = pci_nic_init(nd, rootbus, default_model, default_devaddr); |
1621 | 07caea31 | Markus Armbruster | if (!res)
|
1622 | 07caea31 | Markus Armbruster | exit(1);
|
1623 | 07caea31 | Markus Armbruster | return res;
|
1624 | 07caea31 | Markus Armbruster | } |
1625 | 07caea31 | Markus Armbruster | |
1626 | 129d42fb | Aurelien Jarno | PCIDevice *pci_vga_init(PCIBus *bus) |
1627 | 129d42fb | Aurelien Jarno | { |
1628 | 129d42fb | Aurelien Jarno | switch (vga_interface_type) {
|
1629 | 129d42fb | Aurelien Jarno | case VGA_CIRRUS:
|
1630 | 129d42fb | Aurelien Jarno | return pci_create_simple(bus, -1, "cirrus-vga"); |
1631 | 129d42fb | Aurelien Jarno | case VGA_QXL:
|
1632 | 129d42fb | Aurelien Jarno | return pci_create_simple(bus, -1, "qxl-vga"); |
1633 | 129d42fb | Aurelien Jarno | case VGA_STD:
|
1634 | 129d42fb | Aurelien Jarno | return pci_create_simple(bus, -1, "VGA"); |
1635 | 129d42fb | Aurelien Jarno | case VGA_VMWARE:
|
1636 | 129d42fb | Aurelien Jarno | return pci_create_simple(bus, -1, "vmware-svga"); |
1637 | 129d42fb | Aurelien Jarno | case VGA_NONE:
|
1638 | 129d42fb | Aurelien Jarno | default: /* Other non-PCI types. Checking for unsupported types is already |
1639 | 129d42fb | Aurelien Jarno | done in vl.c. */
|
1640 | 129d42fb | Aurelien Jarno | return NULL; |
1641 | 129d42fb | Aurelien Jarno | } |
1642 | 129d42fb | Aurelien Jarno | } |
1643 | 129d42fb | Aurelien Jarno | |
1644 | 929176c3 | Michael S. Tsirkin | /* Whether a given bus number is in range of the secondary
|
1645 | 929176c3 | Michael S. Tsirkin | * bus of the given bridge device. */
|
1646 | 929176c3 | Michael S. Tsirkin | static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num) |
1647 | 929176c3 | Michael S. Tsirkin | { |
1648 | 929176c3 | Michael S. Tsirkin | return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
|
1649 | 929176c3 | Michael S. Tsirkin | PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
|
1650 | 929176c3 | Michael S. Tsirkin | dev->config[PCI_SECONDARY_BUS] < bus_num && |
1651 | 929176c3 | Michael S. Tsirkin | bus_num <= dev->config[PCI_SUBORDINATE_BUS]; |
1652 | 929176c3 | Michael S. Tsirkin | } |
1653 | 929176c3 | Michael S. Tsirkin | |
1654 | d662210a | Michael S. Tsirkin | static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num) |
1655 | 3ae80618 | aliguori | { |
1656 | 470e6363 | Isaku Yamahata | PCIBus *sec; |
1657 | 3ae80618 | aliguori | |
1658 | 470e6363 | Isaku Yamahata | if (!bus) {
|
1659 | e822a52a | Isaku Yamahata | return NULL; |
1660 | 470e6363 | Isaku Yamahata | } |
1661 | 3ae80618 | aliguori | |
1662 | e822a52a | Isaku Yamahata | if (pci_bus_num(bus) == bus_num) {
|
1663 | e822a52a | Isaku Yamahata | return bus;
|
1664 | e822a52a | Isaku Yamahata | } |
1665 | e822a52a | Isaku Yamahata | |
1666 | 929176c3 | Michael S. Tsirkin | /* Consider all bus numbers in range for the host pci bridge. */
|
1667 | 0889464a | Alex Williamson | if (!pci_bus_is_root(bus) &&
|
1668 | 929176c3 | Michael S. Tsirkin | !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) { |
1669 | 929176c3 | Michael S. Tsirkin | return NULL; |
1670 | 929176c3 | Michael S. Tsirkin | } |
1671 | 929176c3 | Michael S. Tsirkin | |
1672 | e822a52a | Isaku Yamahata | /* try child bus */
|
1673 | 929176c3 | Michael S. Tsirkin | for (; bus; bus = sec) {
|
1674 | 929176c3 | Michael S. Tsirkin | QLIST_FOREACH(sec, &bus->child, sibling) { |
1675 | 0889464a | Alex Williamson | assert(!pci_bus_is_root(sec)); |
1676 | 929176c3 | Michael S. Tsirkin | if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
|
1677 | 929176c3 | Michael S. Tsirkin | return sec;
|
1678 | 929176c3 | Michael S. Tsirkin | } |
1679 | 929176c3 | Michael S. Tsirkin | if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
|
1680 | 929176c3 | Michael S. Tsirkin | break;
|
1681 | c021f8e6 | Blue Swirl | } |
1682 | e822a52a | Isaku Yamahata | } |
1683 | e822a52a | Isaku Yamahata | } |
1684 | e822a52a | Isaku Yamahata | |
1685 | e822a52a | Isaku Yamahata | return NULL; |
1686 | 3ae80618 | aliguori | } |
1687 | 3ae80618 | aliguori | |
1688 | 5256d8bf | Isaku Yamahata | PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
|
1689 | 3ae80618 | aliguori | { |
1690 | d662210a | Michael S. Tsirkin | bus = pci_find_bus_nr(bus, bus_num); |
1691 | 3ae80618 | aliguori | |
1692 | 3ae80618 | aliguori | if (!bus)
|
1693 | 3ae80618 | aliguori | return NULL; |
1694 | 3ae80618 | aliguori | |
1695 | 5256d8bf | Isaku Yamahata | return bus->devices[devfn];
|
1696 | 3ae80618 | aliguori | } |
1697 | 3ae80618 | aliguori | |
1698 | d307af79 | Anthony Liguori | static int pci_qdev_init(DeviceState *qdev) |
1699 | 6b1b92d3 | Paul Brook | { |
1700 | 6b1b92d3 | Paul Brook | PCIDevice *pci_dev = (PCIDevice *)qdev; |
1701 | 40021f08 | Anthony Liguori | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); |
1702 | 6b1b92d3 | Paul Brook | PCIBus *bus; |
1703 | 113f89df | Isaku Yamahata | int rc;
|
1704 | ab85ceb1 | Stefan Weil | bool is_default_rom;
|
1705 | 6b1b92d3 | Paul Brook | |
1706 | a9f49946 | Isaku Yamahata | /* initialize cap_present for pci_is_express() and pci_config_size() */
|
1707 | 40021f08 | Anthony Liguori | if (pc->is_express) {
|
1708 | a9f49946 | Isaku Yamahata | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1709 | a9f49946 | Isaku Yamahata | } |
1710 | a9f49946 | Isaku Yamahata | |
1711 | fef7fbc9 | Andreas Färber | bus = PCI_BUS(qdev_get_parent_bus(qdev)); |
1712 | 6e008585 | Anthony Liguori | pci_dev = do_pci_register_device(pci_dev, bus, |
1713 | 6e008585 | Anthony Liguori | object_get_typename(OBJECT(qdev)), |
1714 | 6e008585 | Anthony Liguori | pci_dev->devfn); |
1715 | 09e3acc6 | Gerd Hoffmann | if (pci_dev == NULL) |
1716 | 09e3acc6 | Gerd Hoffmann | return -1; |
1717 | 40021f08 | Anthony Liguori | if (qdev->hotplugged && pc->no_hotplug) {
|
1718 | f79f2bfc | Anthony Liguori | qerror_report(QERR_DEVICE_NO_HOTPLUG, object_get_typename(OBJECT(pci_dev))); |
1719 | 180c22e1 | Gerd Hoffmann | do_pci_unregister_device(pci_dev); |
1720 | 180c22e1 | Gerd Hoffmann | return -1; |
1721 | 180c22e1 | Gerd Hoffmann | } |
1722 | 40021f08 | Anthony Liguori | if (pc->init) {
|
1723 | 40021f08 | Anthony Liguori | rc = pc->init(pci_dev); |
1724 | c2afc922 | Isaku Yamahata | if (rc != 0) { |
1725 | c2afc922 | Isaku Yamahata | do_pci_unregister_device(pci_dev); |
1726 | c2afc922 | Isaku Yamahata | return rc;
|
1727 | c2afc922 | Isaku Yamahata | } |
1728 | 925fe64a | Alex Williamson | } |
1729 | 8c52c8f3 | Gerd Hoffmann | |
1730 | 8c52c8f3 | Gerd Hoffmann | /* rom loading */
|
1731 | ab85ceb1 | Stefan Weil | is_default_rom = false;
|
1732 | 40021f08 | Anthony Liguori | if (pci_dev->romfile == NULL && pc->romfile != NULL) { |
1733 | 40021f08 | Anthony Liguori | pci_dev->romfile = g_strdup(pc->romfile); |
1734 | ab85ceb1 | Stefan Weil | is_default_rom = true;
|
1735 | ab85ceb1 | Stefan Weil | } |
1736 | ab85ceb1 | Stefan Weil | pci_add_option_rom(pci_dev, is_default_rom); |
1737 | 8c52c8f3 | Gerd Hoffmann | |
1738 | 5beb8ad5 | Isaku Yamahata | if (bus->hotplug) {
|
1739 | e927d487 | Michael S. Tsirkin | /* Let buses differentiate between hotplug and when device is
|
1740 | e927d487 | Michael S. Tsirkin | * enabled during qemu machine creation. */
|
1741 | e927d487 | Michael S. Tsirkin | rc = bus->hotplug(bus->hotplug_qdev, pci_dev, |
1742 | e927d487 | Michael S. Tsirkin | qdev->hotplugged ? PCI_HOTPLUG_ENABLED: |
1743 | e927d487 | Michael S. Tsirkin | PCI_COLDPLUG_ENABLED); |
1744 | a213ff63 | Isaku Yamahata | if (rc != 0) { |
1745 | a213ff63 | Isaku Yamahata | int r = pci_unregister_device(&pci_dev->qdev);
|
1746 | a213ff63 | Isaku Yamahata | assert(!r); |
1747 | a213ff63 | Isaku Yamahata | return rc;
|
1748 | a213ff63 | Isaku Yamahata | } |
1749 | a213ff63 | Isaku Yamahata | } |
1750 | ee995ffb | Gerd Hoffmann | return 0; |
1751 | ee995ffb | Gerd Hoffmann | } |
1752 | ee995ffb | Gerd Hoffmann | |
1753 | ee995ffb | Gerd Hoffmann | static int pci_unplug_device(DeviceState *qdev) |
1754 | ee995ffb | Gerd Hoffmann | { |
1755 | 40021f08 | Anthony Liguori | PCIDevice *dev = PCI_DEVICE(qdev); |
1756 | 40021f08 | Anthony Liguori | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); |
1757 | ee995ffb | Gerd Hoffmann | |
1758 | 40021f08 | Anthony Liguori | if (pc->no_hotplug) {
|
1759 | f79f2bfc | Anthony Liguori | qerror_report(QERR_DEVICE_NO_HOTPLUG, object_get_typename(OBJECT(dev))); |
1760 | 180c22e1 | Gerd Hoffmann | return -1; |
1761 | 180c22e1 | Gerd Hoffmann | } |
1762 | e927d487 | Michael S. Tsirkin | return dev->bus->hotplug(dev->bus->hotplug_qdev, dev,
|
1763 | e927d487 | Michael S. Tsirkin | PCI_HOTPLUG_DISABLED); |
1764 | 6b1b92d3 | Paul Brook | } |
1765 | 6b1b92d3 | Paul Brook | |
1766 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction, |
1767 | 49823868 | Isaku Yamahata | const char *name) |
1768 | 6b1b92d3 | Paul Brook | { |
1769 | 6b1b92d3 | Paul Brook | DeviceState *dev; |
1770 | 6b1b92d3 | Paul Brook | |
1771 | 02e2da45 | Paul Brook | dev = qdev_create(&bus->qbus, name); |
1772 | 09f1bbcd | Michael Roth | qdev_prop_set_int32(dev, "addr", devfn);
|
1773 | 49823868 | Isaku Yamahata | qdev_prop_set_bit(dev, "multifunction", multifunction);
|
1774 | 40021f08 | Anthony Liguori | return PCI_DEVICE(dev);
|
1775 | 71077c1c | Gerd Hoffmann | } |
1776 | 6b1b92d3 | Paul Brook | |
1777 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
|
1778 | 49823868 | Isaku Yamahata | bool multifunction,
|
1779 | 49823868 | Isaku Yamahata | const char *name) |
1780 | 71077c1c | Gerd Hoffmann | { |
1781 | 49823868 | Isaku Yamahata | PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name); |
1782 | e23a1b33 | Markus Armbruster | qdev_init_nofail(&dev->qdev); |
1783 | 71077c1c | Gerd Hoffmann | return dev;
|
1784 | 6b1b92d3 | Paul Brook | } |
1785 | 6f4cbd39 | Michael S. Tsirkin | |
1786 | 49823868 | Isaku Yamahata | PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name) |
1787 | 49823868 | Isaku Yamahata | { |
1788 | 49823868 | Isaku Yamahata | return pci_create_multifunction(bus, devfn, false, name); |
1789 | 49823868 | Isaku Yamahata | } |
1790 | 49823868 | Isaku Yamahata | |
1791 | 49823868 | Isaku Yamahata | PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) |
1792 | 49823868 | Isaku Yamahata | { |
1793 | 49823868 | Isaku Yamahata | return pci_create_simple_multifunction(bus, devfn, false, name); |
1794 | 49823868 | Isaku Yamahata | } |
1795 | 49823868 | Isaku Yamahata | |
1796 | b56d701f | Isaku Yamahata | static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
|
1797 | 6f4cbd39 | Michael S. Tsirkin | { |
1798 | 6f4cbd39 | Michael S. Tsirkin | int offset = PCI_CONFIG_HEADER_SIZE;
|
1799 | 6f4cbd39 | Michael S. Tsirkin | int i;
|
1800 | b56d701f | Isaku Yamahata | for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
|
1801 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->used[i])
|
1802 | 6f4cbd39 | Michael S. Tsirkin | offset = i + 1;
|
1803 | 6f4cbd39 | Michael S. Tsirkin | else if (i - offset + 1 == size) |
1804 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
1805 | b56d701f | Isaku Yamahata | } |
1806 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1807 | 6f4cbd39 | Michael S. Tsirkin | } |
1808 | 6f4cbd39 | Michael S. Tsirkin | |
1809 | 6f4cbd39 | Michael S. Tsirkin | static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
|
1810 | 6f4cbd39 | Michael S. Tsirkin | uint8_t *prev_p) |
1811 | 6f4cbd39 | Michael S. Tsirkin | { |
1812 | 6f4cbd39 | Michael S. Tsirkin | uint8_t next, prev; |
1813 | 6f4cbd39 | Michael S. Tsirkin | |
1814 | 6f4cbd39 | Michael S. Tsirkin | if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
|
1815 | 6f4cbd39 | Michael S. Tsirkin | return 0; |
1816 | 6f4cbd39 | Michael S. Tsirkin | |
1817 | 6f4cbd39 | Michael S. Tsirkin | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1818 | 6f4cbd39 | Michael S. Tsirkin | prev = next + PCI_CAP_LIST_NEXT) |
1819 | 6f4cbd39 | Michael S. Tsirkin | if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
|
1820 | 6f4cbd39 | Michael S. Tsirkin | break;
|
1821 | 6f4cbd39 | Michael S. Tsirkin | |
1822 | 6f4cbd39 | Michael S. Tsirkin | if (prev_p)
|
1823 | 6f4cbd39 | Michael S. Tsirkin | *prev_p = prev; |
1824 | 6f4cbd39 | Michael S. Tsirkin | return next;
|
1825 | 6f4cbd39 | Michael S. Tsirkin | } |
1826 | 6f4cbd39 | Michael S. Tsirkin | |
1827 | c9abe111 | Jan Kiszka | static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
|
1828 | c9abe111 | Jan Kiszka | { |
1829 | c9abe111 | Jan Kiszka | uint8_t next, prev, found = 0;
|
1830 | c9abe111 | Jan Kiszka | |
1831 | c9abe111 | Jan Kiszka | if (!(pdev->used[offset])) {
|
1832 | c9abe111 | Jan Kiszka | return 0; |
1833 | c9abe111 | Jan Kiszka | } |
1834 | c9abe111 | Jan Kiszka | |
1835 | c9abe111 | Jan Kiszka | assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST); |
1836 | c9abe111 | Jan Kiszka | |
1837 | c9abe111 | Jan Kiszka | for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
|
1838 | c9abe111 | Jan Kiszka | prev = next + PCI_CAP_LIST_NEXT) { |
1839 | c9abe111 | Jan Kiszka | if (next <= offset && next > found) {
|
1840 | c9abe111 | Jan Kiszka | found = next; |
1841 | c9abe111 | Jan Kiszka | } |
1842 | c9abe111 | Jan Kiszka | } |
1843 | c9abe111 | Jan Kiszka | return found;
|
1844 | c9abe111 | Jan Kiszka | } |
1845 | c9abe111 | Jan Kiszka | |
1846 | ab85ceb1 | Stefan Weil | /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
|
1847 | ab85ceb1 | Stefan Weil | This is needed for an option rom which is used for more than one device. */
|
1848 | ab85ceb1 | Stefan Weil | static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size) |
1849 | ab85ceb1 | Stefan Weil | { |
1850 | ab85ceb1 | Stefan Weil | uint16_t vendor_id; |
1851 | ab85ceb1 | Stefan Weil | uint16_t device_id; |
1852 | ab85ceb1 | Stefan Weil | uint16_t rom_vendor_id; |
1853 | ab85ceb1 | Stefan Weil | uint16_t rom_device_id; |
1854 | ab85ceb1 | Stefan Weil | uint16_t rom_magic; |
1855 | ab85ceb1 | Stefan Weil | uint16_t pcir_offset; |
1856 | ab85ceb1 | Stefan Weil | uint8_t checksum; |
1857 | ab85ceb1 | Stefan Weil | |
1858 | ab85ceb1 | Stefan Weil | /* Words in rom data are little endian (like in PCI configuration),
|
1859 | ab85ceb1 | Stefan Weil | so they can be read / written with pci_get_word / pci_set_word. */
|
1860 | ab85ceb1 | Stefan Weil | |
1861 | ab85ceb1 | Stefan Weil | /* Only a valid rom will be patched. */
|
1862 | ab85ceb1 | Stefan Weil | rom_magic = pci_get_word(ptr); |
1863 | ab85ceb1 | Stefan Weil | if (rom_magic != 0xaa55) { |
1864 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
|
1865 | ab85ceb1 | Stefan Weil | return;
|
1866 | ab85ceb1 | Stefan Weil | } |
1867 | ab85ceb1 | Stefan Weil | pcir_offset = pci_get_word(ptr + 0x18);
|
1868 | ab85ceb1 | Stefan Weil | if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) { |
1869 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
|
1870 | ab85ceb1 | Stefan Weil | return;
|
1871 | ab85ceb1 | Stefan Weil | } |
1872 | ab85ceb1 | Stefan Weil | |
1873 | ab85ceb1 | Stefan Weil | vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID); |
1874 | ab85ceb1 | Stefan Weil | device_id = pci_get_word(pdev->config + PCI_DEVICE_ID); |
1875 | ab85ceb1 | Stefan Weil | rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
|
1876 | ab85ceb1 | Stefan Weil | rom_device_id = pci_get_word(ptr + pcir_offset + 6);
|
1877 | ab85ceb1 | Stefan Weil | |
1878 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
|
1879 | ab85ceb1 | Stefan Weil | vendor_id, device_id, rom_vendor_id, rom_device_id); |
1880 | ab85ceb1 | Stefan Weil | |
1881 | ab85ceb1 | Stefan Weil | checksum = ptr[6];
|
1882 | ab85ceb1 | Stefan Weil | |
1883 | ab85ceb1 | Stefan Weil | if (vendor_id != rom_vendor_id) {
|
1884 | ab85ceb1 | Stefan Weil | /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
|
1885 | ab85ceb1 | Stefan Weil | checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
|
1886 | ab85ceb1 | Stefan Weil | checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
|
1887 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); |
1888 | ab85ceb1 | Stefan Weil | ptr[6] = checksum;
|
1889 | ab85ceb1 | Stefan Weil | pci_set_word(ptr + pcir_offset + 4, vendor_id);
|
1890 | ab85ceb1 | Stefan Weil | } |
1891 | ab85ceb1 | Stefan Weil | |
1892 | ab85ceb1 | Stefan Weil | if (device_id != rom_device_id) {
|
1893 | ab85ceb1 | Stefan Weil | /* Patch device id and checksum (at offset 6 for etherboot roms). */
|
1894 | ab85ceb1 | Stefan Weil | checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
|
1895 | ab85ceb1 | Stefan Weil | checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
|
1896 | ab85ceb1 | Stefan Weil | PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); |
1897 | ab85ceb1 | Stefan Weil | ptr[6] = checksum;
|
1898 | ab85ceb1 | Stefan Weil | pci_set_word(ptr + pcir_offset + 6, device_id);
|
1899 | ab85ceb1 | Stefan Weil | } |
1900 | ab85ceb1 | Stefan Weil | } |
1901 | ab85ceb1 | Stefan Weil | |
1902 | c2039bd0 | Anthony Liguori | /* Add an option rom for the device */
|
1903 | ab85ceb1 | Stefan Weil | static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom) |
1904 | c2039bd0 | Anthony Liguori | { |
1905 | c2039bd0 | Anthony Liguori | int size;
|
1906 | c2039bd0 | Anthony Liguori | char *path;
|
1907 | c2039bd0 | Anthony Liguori | void *ptr;
|
1908 | 1724f049 | Alex Williamson | char name[32]; |
1909 | 4be9f0d1 | Anthony Liguori | const VMStateDescription *vmsd;
|
1910 | c2039bd0 | Anthony Liguori | |
1911 | 8c52c8f3 | Gerd Hoffmann | if (!pdev->romfile)
|
1912 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1913 | 8c52c8f3 | Gerd Hoffmann | if (strlen(pdev->romfile) == 0) |
1914 | 8c52c8f3 | Gerd Hoffmann | return 0; |
1915 | 8c52c8f3 | Gerd Hoffmann | |
1916 | 88169ddf | Gerd Hoffmann | if (!pdev->rom_bar) {
|
1917 | 88169ddf | Gerd Hoffmann | /*
|
1918 | 88169ddf | Gerd Hoffmann | * Load rom via fw_cfg instead of creating a rom bar,
|
1919 | 88169ddf | Gerd Hoffmann | * for 0.11 compatibility.
|
1920 | 88169ddf | Gerd Hoffmann | */
|
1921 | 88169ddf | Gerd Hoffmann | int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
|
1922 | 88169ddf | Gerd Hoffmann | if (class == 0x0300) { |
1923 | 88169ddf | Gerd Hoffmann | rom_add_vga(pdev->romfile); |
1924 | 88169ddf | Gerd Hoffmann | } else {
|
1925 | 2e55e842 | Gleb Natapov | rom_add_option(pdev->romfile, -1);
|
1926 | 88169ddf | Gerd Hoffmann | } |
1927 | 88169ddf | Gerd Hoffmann | return 0; |
1928 | 88169ddf | Gerd Hoffmann | } |
1929 | 88169ddf | Gerd Hoffmann | |
1930 | 8c52c8f3 | Gerd Hoffmann | path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); |
1931 | c2039bd0 | Anthony Liguori | if (path == NULL) { |
1932 | 7267c094 | Anthony Liguori | path = g_strdup(pdev->romfile); |
1933 | c2039bd0 | Anthony Liguori | } |
1934 | c2039bd0 | Anthony Liguori | |
1935 | c2039bd0 | Anthony Liguori | size = get_image_size(path); |
1936 | 8c52c8f3 | Gerd Hoffmann | if (size < 0) { |
1937 | 1ecda02b | Markus Armbruster | error_report("%s: failed to find romfile \"%s\"",
|
1938 | 8c7f3dd0 | Stefan Hajnoczi | __func__, pdev->romfile); |
1939 | 8c7f3dd0 | Stefan Hajnoczi | g_free(path); |
1940 | 8c7f3dd0 | Stefan Hajnoczi | return -1; |
1941 | 8c7f3dd0 | Stefan Hajnoczi | } else if (size == 0) { |
1942 | 8c7f3dd0 | Stefan Hajnoczi | error_report("%s: ignoring empty romfile \"%s\"",
|
1943 | 8c7f3dd0 | Stefan Hajnoczi | __func__, pdev->romfile); |
1944 | 7267c094 | Anthony Liguori | g_free(path); |
1945 | 8c52c8f3 | Gerd Hoffmann | return -1; |
1946 | 8c52c8f3 | Gerd Hoffmann | } |
1947 | c2039bd0 | Anthony Liguori | if (size & (size - 1)) { |
1948 | c2039bd0 | Anthony Liguori | size = 1 << qemu_fls(size);
|
1949 | c2039bd0 | Anthony Liguori | } |
1950 | c2039bd0 | Anthony Liguori | |
1951 | 4be9f0d1 | Anthony Liguori | vmsd = qdev_get_vmsd(DEVICE(pdev)); |
1952 | 4be9f0d1 | Anthony Liguori | |
1953 | 4be9f0d1 | Anthony Liguori | if (vmsd) {
|
1954 | 4be9f0d1 | Anthony Liguori | snprintf(name, sizeof(name), "%s.rom", vmsd->name); |
1955 | 4be9f0d1 | Anthony Liguori | } else {
|
1956 | f79f2bfc | Anthony Liguori | snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev))); |
1957 | 4be9f0d1 | Anthony Liguori | } |
1958 | 14caaf7f | Avi Kivity | pdev->has_rom = true;
|
1959 | c5705a77 | Avi Kivity | memory_region_init_ram(&pdev->rom, name, size); |
1960 | c5705a77 | Avi Kivity | vmstate_register_ram(&pdev->rom, &pdev->qdev); |
1961 | 14caaf7f | Avi Kivity | ptr = memory_region_get_ram_ptr(&pdev->rom); |
1962 | c2039bd0 | Anthony Liguori | load_image(path, ptr); |
1963 | 7267c094 | Anthony Liguori | g_free(path); |
1964 | c2039bd0 | Anthony Liguori | |
1965 | ab85ceb1 | Stefan Weil | if (is_default_rom) {
|
1966 | ab85ceb1 | Stefan Weil | /* Only the default rom images will be patched (if needed). */
|
1967 | ab85ceb1 | Stefan Weil | pci_patch_ids(pdev, ptr, size); |
1968 | ab85ceb1 | Stefan Weil | } |
1969 | ab85ceb1 | Stefan Weil | |
1970 | e824b2cc | Avi Kivity | pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
|
1971 | c2039bd0 | Anthony Liguori | |
1972 | c2039bd0 | Anthony Liguori | return 0; |
1973 | c2039bd0 | Anthony Liguori | } |
1974 | c2039bd0 | Anthony Liguori | |
1975 | 230741dc | Alex Williamson | static void pci_del_option_rom(PCIDevice *pdev) |
1976 | 230741dc | Alex Williamson | { |
1977 | 14caaf7f | Avi Kivity | if (!pdev->has_rom)
|
1978 | 230741dc | Alex Williamson | return;
|
1979 | 230741dc | Alex Williamson | |
1980 | c5705a77 | Avi Kivity | vmstate_unregister_ram(&pdev->rom, &pdev->qdev); |
1981 | 14caaf7f | Avi Kivity | memory_region_destroy(&pdev->rom); |
1982 | 14caaf7f | Avi Kivity | pdev->has_rom = false;
|
1983 | 230741dc | Alex Williamson | } |
1984 | 230741dc | Alex Williamson | |
1985 | ca77089d | Isaku Yamahata | /*
|
1986 | ca77089d | Isaku Yamahata | * if !offset
|
1987 | ca77089d | Isaku Yamahata | * Reserve space and add capability to the linked list in pci config space
|
1988 | ca77089d | Isaku Yamahata | *
|
1989 | ca77089d | Isaku Yamahata | * if offset = 0,
|
1990 | ca77089d | Isaku Yamahata | * Find and reserve space and add capability to the linked list
|
1991 | ca77089d | Isaku Yamahata | * in pci config space */
|
1992 | ca77089d | Isaku Yamahata | int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
|
1993 | ca77089d | Isaku Yamahata | uint8_t offset, uint8_t size) |
1994 | 6f4cbd39 | Michael S. Tsirkin | { |
1995 | ca77089d | Isaku Yamahata | uint8_t *config; |
1996 | c9abe111 | Jan Kiszka | int i, overlapping_cap;
|
1997 | c9abe111 | Jan Kiszka | |
1998 | ca77089d | Isaku Yamahata | if (!offset) {
|
1999 | ca77089d | Isaku Yamahata | offset = pci_find_space(pdev, size); |
2000 | ca77089d | Isaku Yamahata | if (!offset) {
|
2001 | ca77089d | Isaku Yamahata | return -ENOSPC;
|
2002 | ca77089d | Isaku Yamahata | } |
2003 | c9abe111 | Jan Kiszka | } else {
|
2004 | c9abe111 | Jan Kiszka | /* Verify that capabilities don't overlap. Note: device assignment
|
2005 | c9abe111 | Jan Kiszka | * depends on this check to verify that the device is not broken.
|
2006 | c9abe111 | Jan Kiszka | * Should never trigger for emulated devices, but it's helpful
|
2007 | c9abe111 | Jan Kiszka | * for debugging these. */
|
2008 | c9abe111 | Jan Kiszka | for (i = offset; i < offset + size; i++) {
|
2009 | c9abe111 | Jan Kiszka | overlapping_cap = pci_find_capability_at_offset(pdev, i); |
2010 | c9abe111 | Jan Kiszka | if (overlapping_cap) {
|
2011 | 568f0690 | David Gibson | fprintf(stderr, "ERROR: %s:%02x:%02x.%x "
|
2012 | c9abe111 | Jan Kiszka | "Attempt to add PCI capability %x at offset "
|
2013 | c9abe111 | Jan Kiszka | "%x overlaps existing capability %x at offset %x\n",
|
2014 | 568f0690 | David Gibson | pci_root_bus_path(pdev), pci_bus_num(pdev->bus), |
2015 | c9abe111 | Jan Kiszka | PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), |
2016 | c9abe111 | Jan Kiszka | cap_id, offset, overlapping_cap, i); |
2017 | c9abe111 | Jan Kiszka | return -EINVAL;
|
2018 | c9abe111 | Jan Kiszka | } |
2019 | c9abe111 | Jan Kiszka | } |
2020 | ca77089d | Isaku Yamahata | } |
2021 | ca77089d | Isaku Yamahata | |
2022 | ca77089d | Isaku Yamahata | config = pdev->config + offset; |
2023 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_ID] = cap_id; |
2024 | 6f4cbd39 | Michael S. Tsirkin | config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; |
2025 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_CAPABILITY_LIST] = offset; |
2026 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; |
2027 | e26631b7 | Michael S. Tsirkin | memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4)); |
2028 | 6f4cbd39 | Michael S. Tsirkin | /* Make capability read-only by default */
|
2029 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0, size);
|
2030 | bd4b65ee | Michael S. Tsirkin | /* Check capability by default */
|
2031 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0xFF, size);
|
2032 | 6f4cbd39 | Michael S. Tsirkin | return offset;
|
2033 | 6f4cbd39 | Michael S. Tsirkin | } |
2034 | 6f4cbd39 | Michael S. Tsirkin | |
2035 | 6f4cbd39 | Michael S. Tsirkin | /* Unlink capability from the pci config space. */
|
2036 | 6f4cbd39 | Michael S. Tsirkin | void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
|
2037 | 6f4cbd39 | Michael S. Tsirkin | { |
2038 | 6f4cbd39 | Michael S. Tsirkin | uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); |
2039 | 6f4cbd39 | Michael S. Tsirkin | if (!offset)
|
2040 | 6f4cbd39 | Michael S. Tsirkin | return;
|
2041 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; |
2042 | ebabb67a | Stefan Weil | /* Make capability writable again */
|
2043 | 6f4cbd39 | Michael S. Tsirkin | memset(pdev->wmask + offset, 0xff, size);
|
2044 | 1a4f5971 | Isaku Yamahata | memset(pdev->w1cmask + offset, 0, size);
|
2045 | bd4b65ee | Michael S. Tsirkin | /* Clear cmask as device-specific registers can't be checked */
|
2046 | bd4b65ee | Michael S. Tsirkin | memset(pdev->cmask + offset, 0, size);
|
2047 | e26631b7 | Michael S. Tsirkin | memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4)); |
2048 | 6f4cbd39 | Michael S. Tsirkin | |
2049 | 6f4cbd39 | Michael S. Tsirkin | if (!pdev->config[PCI_CAPABILITY_LIST])
|
2050 | 6f4cbd39 | Michael S. Tsirkin | pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; |
2051 | 6f4cbd39 | Michael S. Tsirkin | } |
2052 | 6f4cbd39 | Michael S. Tsirkin | |
2053 | 6f4cbd39 | Michael S. Tsirkin | uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) |
2054 | 6f4cbd39 | Michael S. Tsirkin | { |
2055 | 6f4cbd39 | Michael S. Tsirkin | return pci_find_capability_list(pdev, cap_id, NULL); |
2056 | 6f4cbd39 | Michael S. Tsirkin | } |
2057 | 10c4c98a | Gerd Hoffmann | |
2058 | 10c4c98a | Gerd Hoffmann | static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
2059 | 10c4c98a | Gerd Hoffmann | { |
2060 | 10c4c98a | Gerd Hoffmann | PCIDevice *d = (PCIDevice *)dev; |
2061 | 10c4c98a | Gerd Hoffmann | const pci_class_desc *desc;
|
2062 | 10c4c98a | Gerd Hoffmann | char ctxt[64]; |
2063 | 10c4c98a | Gerd Hoffmann | PCIIORegion *r; |
2064 | 10c4c98a | Gerd Hoffmann | int i, class;
|
2065 | 10c4c98a | Gerd Hoffmann | |
2066 | b0ff8eb2 | Isaku Yamahata | class = pci_get_word(d->config + PCI_CLASS_DEVICE); |
2067 | 10c4c98a | Gerd Hoffmann | desc = pci_class_descriptions; |
2068 | 10c4c98a | Gerd Hoffmann | while (desc->desc && class != desc->class)
|
2069 | 10c4c98a | Gerd Hoffmann | desc++; |
2070 | 10c4c98a | Gerd Hoffmann | if (desc->desc) {
|
2071 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); |
2072 | 10c4c98a | Gerd Hoffmann | } else {
|
2073 | 10c4c98a | Gerd Hoffmann | snprintf(ctxt, sizeof(ctxt), "Class %04x", class); |
2074 | 10c4c98a | Gerd Hoffmann | } |
2075 | 10c4c98a | Gerd Hoffmann | |
2076 | 10c4c98a | Gerd Hoffmann | monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
|
2077 | 10c4c98a | Gerd Hoffmann | "pci id %04x:%04x (sub %04x:%04x)\n",
|
2078 | 7f5feab4 | Alex Williamson | indent, "", ctxt, pci_bus_num(d->bus),
|
2079 | e822a52a | Isaku Yamahata | PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), |
2080 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_VENDOR_ID), |
2081 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_DEVICE_ID), |
2082 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), |
2083 | b0ff8eb2 | Isaku Yamahata | pci_get_word(d->config + PCI_SUBSYSTEM_ID)); |
2084 | 10c4c98a | Gerd Hoffmann | for (i = 0; i < PCI_NUM_REGIONS; i++) { |
2085 | 10c4c98a | Gerd Hoffmann | r = &d->io_regions[i]; |
2086 | 10c4c98a | Gerd Hoffmann | if (!r->size)
|
2087 | 10c4c98a | Gerd Hoffmann | continue;
|
2088 | 89e8b13c | Isaku Yamahata | monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
|
2089 | 89e8b13c | Isaku Yamahata | " [0x%"FMT_PCIBUS"]\n", |
2090 | 89e8b13c | Isaku Yamahata | indent, "",
|
2091 | 0392a017 | Isaku Yamahata | i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", |
2092 | 10c4c98a | Gerd Hoffmann | r->addr, r->addr + r->size - 1);
|
2093 | 10c4c98a | Gerd Hoffmann | } |
2094 | 10c4c98a | Gerd Hoffmann | } |
2095 | 03587182 | Gerd Hoffmann | |
2096 | 5e0259e7 | Gleb Natapov | static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len) |
2097 | 5e0259e7 | Gleb Natapov | { |
2098 | 5e0259e7 | Gleb Natapov | PCIDevice *d = (PCIDevice *)dev; |
2099 | 5e0259e7 | Gleb Natapov | const char *name = NULL; |
2100 | 5e0259e7 | Gleb Natapov | const pci_class_desc *desc = pci_class_descriptions;
|
2101 | 5e0259e7 | Gleb Natapov | int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
|
2102 | 5e0259e7 | Gleb Natapov | |
2103 | 5e0259e7 | Gleb Natapov | while (desc->desc &&
|
2104 | 5e0259e7 | Gleb Natapov | (class & ~desc->fw_ign_bits) != |
2105 | 5e0259e7 | Gleb Natapov | (desc->class & ~desc->fw_ign_bits)) { |
2106 | 5e0259e7 | Gleb Natapov | desc++; |
2107 | 5e0259e7 | Gleb Natapov | } |
2108 | 5e0259e7 | Gleb Natapov | |
2109 | 5e0259e7 | Gleb Natapov | if (desc->desc) {
|
2110 | 5e0259e7 | Gleb Natapov | name = desc->fw_name; |
2111 | 5e0259e7 | Gleb Natapov | } |
2112 | 5e0259e7 | Gleb Natapov | |
2113 | 5e0259e7 | Gleb Natapov | if (name) {
|
2114 | 5e0259e7 | Gleb Natapov | pstrcpy(buf, len, name); |
2115 | 5e0259e7 | Gleb Natapov | } else {
|
2116 | 5e0259e7 | Gleb Natapov | snprintf(buf, len, "pci%04x,%04x",
|
2117 | 5e0259e7 | Gleb Natapov | pci_get_word(d->config + PCI_VENDOR_ID), |
2118 | 5e0259e7 | Gleb Natapov | pci_get_word(d->config + PCI_DEVICE_ID)); |
2119 | 5e0259e7 | Gleb Natapov | } |
2120 | 5e0259e7 | Gleb Natapov | |
2121 | 5e0259e7 | Gleb Natapov | return buf;
|
2122 | 5e0259e7 | Gleb Natapov | } |
2123 | 5e0259e7 | Gleb Natapov | |
2124 | 5e0259e7 | Gleb Natapov | static char *pcibus_get_fw_dev_path(DeviceState *dev) |
2125 | 5e0259e7 | Gleb Natapov | { |
2126 | 5e0259e7 | Gleb Natapov | PCIDevice *d = (PCIDevice *)dev; |
2127 | 5e0259e7 | Gleb Natapov | char path[50], name[33]; |
2128 | 5e0259e7 | Gleb Natapov | int off;
|
2129 | 5e0259e7 | Gleb Natapov | |
2130 | 5e0259e7 | Gleb Natapov | off = snprintf(path, sizeof(path), "%s@%x", |
2131 | 5e0259e7 | Gleb Natapov | pci_dev_fw_name(dev, name, sizeof name),
|
2132 | 5e0259e7 | Gleb Natapov | PCI_SLOT(d->devfn)); |
2133 | 5e0259e7 | Gleb Natapov | if (PCI_FUNC(d->devfn))
|
2134 | 5e0259e7 | Gleb Natapov | snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn)); |
2135 | a5cf8262 | Jim Meyering | return g_strdup(path);
|
2136 | 5e0259e7 | Gleb Natapov | } |
2137 | 5e0259e7 | Gleb Natapov | |
2138 | 4f43c1ff | Alex Williamson | static char *pcibus_get_dev_path(DeviceState *dev) |
2139 | 4f43c1ff | Alex Williamson | { |
2140 | a6a7005d | Michael S. Tsirkin | PCIDevice *d = container_of(dev, PCIDevice, qdev); |
2141 | a6a7005d | Michael S. Tsirkin | PCIDevice *t; |
2142 | a6a7005d | Michael S. Tsirkin | int slot_depth;
|
2143 | a6a7005d | Michael S. Tsirkin | /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
|
2144 | a6a7005d | Michael S. Tsirkin | * 00 is added here to make this format compatible with
|
2145 | a6a7005d | Michael S. Tsirkin | * domain:Bus:Slot.Func for systems without nested PCI bridges.
|
2146 | a6a7005d | Michael S. Tsirkin | * Slot.Function list specifies the slot and function numbers for all
|
2147 | a6a7005d | Michael S. Tsirkin | * devices on the path from root to the specific device. */
|
2148 | 568f0690 | David Gibson | const char *root_bus_path; |
2149 | 568f0690 | David Gibson | int root_bus_len;
|
2150 | 2991181a | Michael S. Tsirkin | char slot[] = ":SS.F"; |
2151 | 2991181a | Michael S. Tsirkin | int slot_len = sizeof slot - 1 /* For '\0' */; |
2152 | a6a7005d | Michael S. Tsirkin | int path_len;
|
2153 | a6a7005d | Michael S. Tsirkin | char *path, *p;
|
2154 | 2991181a | Michael S. Tsirkin | int s;
|
2155 | a6a7005d | Michael S. Tsirkin | |
2156 | 568f0690 | David Gibson | root_bus_path = pci_root_bus_path(d); |
2157 | 568f0690 | David Gibson | root_bus_len = strlen(root_bus_path); |
2158 | 568f0690 | David Gibson | |
2159 | a6a7005d | Michael S. Tsirkin | /* Calculate # of slots on path between device and root. */;
|
2160 | a6a7005d | Michael S. Tsirkin | slot_depth = 0;
|
2161 | a6a7005d | Michael S. Tsirkin | for (t = d; t; t = t->bus->parent_dev) {
|
2162 | a6a7005d | Michael S. Tsirkin | ++slot_depth; |
2163 | a6a7005d | Michael S. Tsirkin | } |
2164 | a6a7005d | Michael S. Tsirkin | |
2165 | 568f0690 | David Gibson | path_len = root_bus_len + slot_len * slot_depth; |
2166 | a6a7005d | Michael S. Tsirkin | |
2167 | a6a7005d | Michael S. Tsirkin | /* Allocate memory, fill in the terminating null byte. */
|
2168 | 7267c094 | Anthony Liguori | path = g_malloc(path_len + 1 /* For '\0' */); |
2169 | a6a7005d | Michael S. Tsirkin | path[path_len] = '\0';
|
2170 | a6a7005d | Michael S. Tsirkin | |
2171 | 568f0690 | David Gibson | memcpy(path, root_bus_path, root_bus_len); |
2172 | a6a7005d | Michael S. Tsirkin | |
2173 | a6a7005d | Michael S. Tsirkin | /* Fill in slot numbers. We walk up from device to root, so need to print
|
2174 | a6a7005d | Michael S. Tsirkin | * them in the reverse order, last to first. */
|
2175 | a6a7005d | Michael S. Tsirkin | p = path + path_len; |
2176 | a6a7005d | Michael S. Tsirkin | for (t = d; t; t = t->bus->parent_dev) {
|
2177 | a6a7005d | Michael S. Tsirkin | p -= slot_len; |
2178 | 2991181a | Michael S. Tsirkin | s = snprintf(slot, sizeof slot, ":%02x.%x", |
2179 | 4c900518 | Isaku Yamahata | PCI_SLOT(t->devfn), PCI_FUNC(t->devfn)); |
2180 | 2991181a | Michael S. Tsirkin | assert(s == slot_len); |
2181 | 2991181a | Michael S. Tsirkin | memcpy(p, slot, slot_len); |
2182 | a6a7005d | Michael S. Tsirkin | } |
2183 | a6a7005d | Michael S. Tsirkin | |
2184 | a6a7005d | Michael S. Tsirkin | return path;
|
2185 | 4f43c1ff | Alex Williamson | } |
2186 | 4f43c1ff | Alex Williamson | |
2187 | f3006dd1 | Isaku Yamahata | static int pci_qdev_find_recursive(PCIBus *bus, |
2188 | f3006dd1 | Isaku Yamahata | const char *id, PCIDevice **pdev) |
2189 | f3006dd1 | Isaku Yamahata | { |
2190 | f3006dd1 | Isaku Yamahata | DeviceState *qdev = qdev_find_recursive(&bus->qbus, id); |
2191 | f3006dd1 | Isaku Yamahata | if (!qdev) {
|
2192 | f3006dd1 | Isaku Yamahata | return -ENODEV;
|
2193 | f3006dd1 | Isaku Yamahata | } |
2194 | f3006dd1 | Isaku Yamahata | |
2195 | f3006dd1 | Isaku Yamahata | /* roughly check if given qdev is pci device */
|
2196 | 4be9f0d1 | Anthony Liguori | if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
|
2197 | 40021f08 | Anthony Liguori | *pdev = PCI_DEVICE(qdev); |
2198 | f3006dd1 | Isaku Yamahata | return 0; |
2199 | f3006dd1 | Isaku Yamahata | } |
2200 | f3006dd1 | Isaku Yamahata | return -EINVAL;
|
2201 | f3006dd1 | Isaku Yamahata | } |
2202 | f3006dd1 | Isaku Yamahata | |
2203 | f3006dd1 | Isaku Yamahata | int pci_qdev_find_device(const char *id, PCIDevice **pdev) |
2204 | f3006dd1 | Isaku Yamahata | { |
2205 | f3006dd1 | Isaku Yamahata | struct PCIHostBus *host;
|
2206 | f3006dd1 | Isaku Yamahata | int rc = -ENODEV;
|
2207 | f3006dd1 | Isaku Yamahata | |
2208 | f3006dd1 | Isaku Yamahata | QLIST_FOREACH(host, &host_buses, next) { |
2209 | f3006dd1 | Isaku Yamahata | int tmp = pci_qdev_find_recursive(host->bus, id, pdev);
|
2210 | f3006dd1 | Isaku Yamahata | if (!tmp) {
|
2211 | f3006dd1 | Isaku Yamahata | rc = 0;
|
2212 | f3006dd1 | Isaku Yamahata | break;
|
2213 | f3006dd1 | Isaku Yamahata | } |
2214 | f3006dd1 | Isaku Yamahata | if (tmp != -ENODEV) {
|
2215 | f3006dd1 | Isaku Yamahata | rc = tmp; |
2216 | f3006dd1 | Isaku Yamahata | } |
2217 | f3006dd1 | Isaku Yamahata | } |
2218 | f3006dd1 | Isaku Yamahata | |
2219 | f3006dd1 | Isaku Yamahata | return rc;
|
2220 | f3006dd1 | Isaku Yamahata | } |
2221 | f5e6fed8 | Avi Kivity | |
2222 | f5e6fed8 | Avi Kivity | MemoryRegion *pci_address_space(PCIDevice *dev) |
2223 | f5e6fed8 | Avi Kivity | { |
2224 | f5e6fed8 | Avi Kivity | return dev->bus->address_space_mem;
|
2225 | f5e6fed8 | Avi Kivity | } |
2226 | e11d6439 | Richard Henderson | |
2227 | e11d6439 | Richard Henderson | MemoryRegion *pci_address_space_io(PCIDevice *dev) |
2228 | e11d6439 | Richard Henderson | { |
2229 | e11d6439 | Richard Henderson | return dev->bus->address_space_io;
|
2230 | e11d6439 | Richard Henderson | } |
2231 | 40021f08 | Anthony Liguori | |
2232 | 39bffca2 | Anthony Liguori | static void pci_device_class_init(ObjectClass *klass, void *data) |
2233 | 39bffca2 | Anthony Liguori | { |
2234 | 39bffca2 | Anthony Liguori | DeviceClass *k = DEVICE_CLASS(klass); |
2235 | 39bffca2 | Anthony Liguori | k->init = pci_qdev_init; |
2236 | 39bffca2 | Anthony Liguori | k->unplug = pci_unplug_device; |
2237 | 39bffca2 | Anthony Liguori | k->exit = pci_unregister_device; |
2238 | 0d936928 | Anthony Liguori | k->bus_type = TYPE_PCI_BUS; |
2239 | bce54474 | Paolo Bonzini | k->props = pci_props; |
2240 | 39bffca2 | Anthony Liguori | } |
2241 | 39bffca2 | Anthony Liguori | |
2242 | e00387d5 | Avi Kivity | void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque) |
2243 | 5fa45de5 | David Gibson | { |
2244 | e00387d5 | Avi Kivity | bus->iommu_fn = fn; |
2245 | e00387d5 | Avi Kivity | bus->iommu_opaque = opaque; |
2246 | 5fa45de5 | David Gibson | } |
2247 | 5fa45de5 | David Gibson | |
2248 | 8c43a6f0 | Andreas Färber | static const TypeInfo pci_device_type_info = { |
2249 | 40021f08 | Anthony Liguori | .name = TYPE_PCI_DEVICE, |
2250 | 40021f08 | Anthony Liguori | .parent = TYPE_DEVICE, |
2251 | 40021f08 | Anthony Liguori | .instance_size = sizeof(PCIDevice),
|
2252 | 40021f08 | Anthony Liguori | .abstract = true,
|
2253 | 40021f08 | Anthony Liguori | .class_size = sizeof(PCIDeviceClass),
|
2254 | 39bffca2 | Anthony Liguori | .class_init = pci_device_class_init, |
2255 | 40021f08 | Anthony Liguori | }; |
2256 | 40021f08 | Anthony Liguori | |
2257 | 83f7d43a | Andreas Färber | static void pci_register_types(void) |
2258 | 40021f08 | Anthony Liguori | { |
2259 | 0d936928 | Anthony Liguori | type_register_static(&pci_bus_info); |
2260 | 3a861c46 | Alex Williamson | type_register_static(&pcie_bus_info); |
2261 | 40021f08 | Anthony Liguori | type_register_static(&pci_device_type_info); |
2262 | 40021f08 | Anthony Liguori | } |
2263 | 40021f08 | Anthony Liguori | |
2264 | 83f7d43a | Andreas Färber | type_init(pci_register_types) |