2 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
3 * Alex Williamson <alex.williamson@hp.com>
5 * PCI "Controller" Backend - virtualize PCI bus topology based on PCI
6 * controllers. Devices under the same PCI controller are exposed on the
7 * same virtual domain:bus. Within a bus, device slots are virtualized
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/acpi.h>
28 #include <linux/list.h>
29 #include <linux/pci.h>
30 #include <linux/spinlock.h>
33 #define PCI_MAX_BUSSES 255
34 #define PCI_MAX_SLOTS 32
36 struct controller_dev_entry {
37 struct list_head list;
42 struct controller_list_entry {
43 struct list_head list;
44 struct pci_controller *controller;
47 unsigned int next_devfn;
48 struct list_head dev_list;
51 struct controller_dev_data {
52 struct list_head list;
53 unsigned int next_domain;
54 unsigned int next_bus;
59 struct pciback_device *pdev;
64 struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
65 unsigned int domain, unsigned int bus,
68 struct controller_dev_data *dev_data = pdev->pci_dev_data;
69 struct controller_dev_entry *dev_entry;
70 struct controller_list_entry *cntrl_entry;
71 struct pci_dev *dev = NULL;
74 spin_lock_irqsave(&dev_data->lock, flags);
76 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
77 if (cntrl_entry->domain != domain ||
78 cntrl_entry->bus != bus)
81 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
82 if (devfn == dev_entry->devfn) {
89 spin_unlock_irqrestore(&dev_data->lock, flags);
94 int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
96 struct controller_dev_data *dev_data = pdev->pci_dev_data;
97 struct controller_dev_entry *dev_entry;
98 struct controller_list_entry *cntrl_entry;
99 struct pci_controller *dev_controller = PCI_CONTROLLER(dev);
101 int ret = 0, found = 0;
103 spin_lock_irqsave(&dev_data->lock, flags);
105 /* Look to see if we already have a domain:bus for this controller */
106 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
107 if (cntrl_entry->controller == dev_controller) {
114 cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
120 cntrl_entry->controller = dev_controller;
121 cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
123 cntrl_entry->domain = dev_data->next_domain;
124 cntrl_entry->bus = dev_data->next_bus++;
125 if (dev_data->next_bus > PCI_MAX_BUSSES) {
126 dev_data->next_domain++;
127 dev_data->next_bus = 0;
130 INIT_LIST_HEAD(&cntrl_entry->dev_list);
132 list_add_tail(&cntrl_entry->list, &dev_data->list);
135 if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
137 * While it seems unlikely, this can actually happen if
138 * a controller has P2P bridges under it.
140 xenbus_dev_fatal(pdev->xdev, -ENOSPC, "Virtual bus %04x:%02x "
141 "is full, no room to export %04x:%02x:%02x.%x",
142 cntrl_entry->domain, cntrl_entry->bus,
143 pci_domain_nr(dev->bus), dev->bus->number,
144 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
149 dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
151 if (list_empty(&cntrl_entry->dev_list)) {
152 list_del(&cntrl_entry->list);
159 dev_entry->dev = dev;
160 dev_entry->devfn = cntrl_entry->next_devfn;
162 list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
164 cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
167 spin_unlock_irqrestore(&dev_data->lock, flags);
171 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
173 struct controller_dev_data *dev_data = pdev->pci_dev_data;
174 struct controller_list_entry *cntrl_entry;
175 struct controller_dev_entry *dev_entry = NULL;
176 struct pci_dev *found_dev = NULL;
179 spin_lock_irqsave(&dev_data->lock, flags);
181 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
182 if (cntrl_entry->controller != PCI_CONTROLLER(dev))
185 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
186 if (dev_entry->dev == dev) {
187 found_dev = dev_entry->dev;
194 spin_unlock_irqrestore(&dev_data->lock, flags);
198 list_del(&dev_entry->list);
201 if (list_empty(&cntrl_entry->dev_list)) {
202 list_del(&cntrl_entry->list);
206 spin_unlock_irqrestore(&dev_data->lock, flags);
207 pcistub_put_pci_dev(found_dev);
210 int pciback_init_devices(struct pciback_device *pdev)
212 struct controller_dev_data *dev_data;
214 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
218 spin_lock_init(&dev_data->lock);
220 INIT_LIST_HEAD(&dev_data->list);
222 /* Starting domain:bus numbers */
223 dev_data->next_domain = 0;
224 dev_data->next_bus = 0;
226 pdev->pci_dev_data = dev_data;
231 static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
233 struct walk_info *info = data;
234 struct acpi_resource_address64 addr;
237 char str[32], tmp[3];
238 unsigned char *ptr, *buf;
240 status = acpi_resource_to_address64(res, &addr);
242 /* Do we care about this range? Let's check. */
243 if (!ACPI_SUCCESS(status) ||
244 !(addr.resource_type == ACPI_MEMORY_RANGE ||
245 addr.resource_type == ACPI_IO_RANGE) ||
246 !addr.address_length || addr.producer_consumer != ACPI_PRODUCER)
250 * Furthermore, we really only care to tell the guest about
251 * address ranges that require address translation of some sort.
253 if (!(addr.resource_type == ACPI_MEMORY_RANGE &&
254 addr.info.mem.translation) &&
255 !(addr.resource_type == ACPI_IO_RANGE &&
256 addr.info.io.translation))
259 /* Store the resource in xenbus for the guest */
260 len = snprintf(str, sizeof(str), "root-%d-resource-%d",
261 info->root_num, info->resource_count);
262 if (unlikely(len >= (sizeof(str) - 1)))
265 buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
269 /* Clean out resource_source */
270 res->data.address64.resource_source.index = 0xFF;
271 res->data.address64.resource_source.string_length = 0;
272 res->data.address64.resource_source.string_ptr = NULL;
274 ptr = (unsigned char *)res;
276 /* Turn the acpi_resource into an ASCII byte stream */
277 for (i = 0; i < sizeof(*res); i++) {
278 snprintf(tmp, sizeof(tmp), "%02x", ptr[i]);
279 strncat(buf, tmp, 2);
282 err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
286 info->resource_count++;
293 int pciback_publish_pci_roots(struct pciback_device *pdev,
294 publish_pci_root_cb publish_root_cb)
296 struct controller_dev_data *dev_data = pdev->pci_dev_data;
297 struct controller_list_entry *cntrl_entry;
298 int i, root_num, len, err = 0;
299 unsigned int domain, bus;
301 struct walk_info info;
303 spin_lock(&dev_data->lock);
305 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
306 /* First publish all the domain:bus info */
307 err = publish_root_cb(pdev, cntrl_entry->domain,
313 * Now figure out which root-%d this belongs to
314 * so we can associate resources with it.
316 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
317 "root_num", "%d", &root_num);
322 for (i = 0; i < root_num; i++) {
323 len = snprintf(str, sizeof(str), "root-%d", i);
324 if (unlikely(len >= (sizeof(str) - 1))) {
329 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
330 str, "%x:%x", &domain, &bus);
334 /* Is this the one we just published? */
335 if (domain == cntrl_entry->domain &&
336 bus == cntrl_entry->bus)
344 info.resource_count = 0;
347 /* Let ACPI do the heavy lifting on decoding resources */
348 acpi_walk_resources(cntrl_entry->controller->acpi_handle,
349 METHOD_NAME__CRS, write_xenbus_resource,
352 /* No resouces. OK. On to the next one */
353 if (!info.resource_count)
356 /* Store the number of resources we wrote for this root-%d */
357 len = snprintf(str, sizeof(str), "root-%d-resources", i);
358 if (unlikely(len >= (sizeof(str) - 1))) {
363 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
364 "%d", info.resource_count);
369 /* Finally, write some magic to synchronize with the guest. */
370 len = snprintf(str, sizeof(str), "root-resource-magic");
371 if (unlikely(len >= (sizeof(str) - 1))) {
376 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
377 "%lx", (sizeof(struct acpi_resource) * 2) + 1);
380 spin_unlock(&dev_data->lock);
385 void pciback_release_devices(struct pciback_device *pdev)
387 struct controller_dev_data *dev_data = pdev->pci_dev_data;
388 struct controller_list_entry *cntrl_entry, *c;
389 struct controller_dev_entry *dev_entry, *d;
391 list_for_each_entry_safe(cntrl_entry, c, &dev_data->list, list) {
392 list_for_each_entry_safe(dev_entry, d,
393 &cntrl_entry->dev_list, list) {
394 list_del(&dev_entry->list);
395 pcistub_put_pci_dev(dev_entry->dev);
398 list_del(&cntrl_entry->list);
403 pdev->pci_dev_data = NULL;