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,
95 int devid, publish_pci_dev_cb publish_cb)
97 struct controller_dev_data *dev_data = pdev->pci_dev_data;
98 struct controller_dev_entry *dev_entry;
99 struct controller_list_entry *cntrl_entry;
100 struct pci_controller *dev_controller = PCI_CONTROLLER(dev);
102 int ret = 0, found = 0;
104 spin_lock_irqsave(&dev_data->lock, flags);
106 /* Look to see if we already have a domain:bus for this controller */
107 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
108 if (cntrl_entry->controller == dev_controller) {
115 cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
121 cntrl_entry->controller = dev_controller;
122 cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
124 cntrl_entry->domain = dev_data->next_domain;
125 cntrl_entry->bus = dev_data->next_bus++;
126 if (dev_data->next_bus > PCI_MAX_BUSSES) {
127 dev_data->next_domain++;
128 dev_data->next_bus = 0;
131 INIT_LIST_HEAD(&cntrl_entry->dev_list);
133 list_add_tail(&cntrl_entry->list, &dev_data->list);
136 if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
138 * While it seems unlikely, this can actually happen if
139 * a controller has P2P bridges under it.
141 xenbus_dev_fatal(pdev->xdev, -ENOSPC, "Virtual bus %04x:%02x "
142 "is full, no room to export %04x:%02x:%02x.%x",
143 cntrl_entry->domain, cntrl_entry->bus,
144 pci_domain_nr(dev->bus), dev->bus->number,
145 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
150 dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
152 if (list_empty(&cntrl_entry->dev_list)) {
153 list_del(&cntrl_entry->list);
160 dev_entry->dev = dev;
161 dev_entry->devfn = cntrl_entry->next_devfn;
163 list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
165 cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
168 spin_unlock_irqrestore(&dev_data->lock, flags);
170 /* TODO: Publish virtual domain:bus:slot.func here. */
175 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
177 struct controller_dev_data *dev_data = pdev->pci_dev_data;
178 struct controller_list_entry *cntrl_entry;
179 struct controller_dev_entry *dev_entry = NULL;
180 struct pci_dev *found_dev = NULL;
183 spin_lock_irqsave(&dev_data->lock, flags);
185 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
186 if (cntrl_entry->controller != PCI_CONTROLLER(dev))
189 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
190 if (dev_entry->dev == dev) {
191 found_dev = dev_entry->dev;
198 spin_unlock_irqrestore(&dev_data->lock, flags);
202 list_del(&dev_entry->list);
205 if (list_empty(&cntrl_entry->dev_list)) {
206 list_del(&cntrl_entry->list);
210 spin_unlock_irqrestore(&dev_data->lock, flags);
211 pcistub_put_pci_dev(found_dev);
214 int pciback_init_devices(struct pciback_device *pdev)
216 struct controller_dev_data *dev_data;
218 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
222 spin_lock_init(&dev_data->lock);
224 INIT_LIST_HEAD(&dev_data->list);
226 /* Starting domain:bus numbers */
227 dev_data->next_domain = 0;
228 dev_data->next_bus = 0;
230 pdev->pci_dev_data = dev_data;
235 static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
237 struct walk_info *info = data;
238 struct acpi_resource_address64 addr;
241 char str[32], tmp[3];
242 unsigned char *ptr, *buf;
244 status = acpi_resource_to_address64(res, &addr);
246 /* Do we care about this range? Let's check. */
247 if (!ACPI_SUCCESS(status) ||
248 !(addr.resource_type == ACPI_MEMORY_RANGE ||
249 addr.resource_type == ACPI_IO_RANGE) ||
250 !addr.address_length || addr.producer_consumer != ACPI_PRODUCER)
254 * Furthermore, we really only care to tell the guest about
255 * address ranges that require address translation of some sort.
257 if (!(addr.resource_type == ACPI_MEMORY_RANGE &&
258 addr.info.mem.translation) &&
259 !(addr.resource_type == ACPI_IO_RANGE &&
260 addr.info.io.translation))
263 /* Store the resource in xenbus for the guest */
264 len = snprintf(str, sizeof(str), "root-%d-resource-%d",
265 info->root_num, info->resource_count);
266 if (unlikely(len >= (sizeof(str) - 1)))
269 buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
273 /* Clean out resource_source */
274 res->data.address64.resource_source.index = 0xFF;
275 res->data.address64.resource_source.string_length = 0;
276 res->data.address64.resource_source.string_ptr = NULL;
278 ptr = (unsigned char *)res;
280 /* Turn the acpi_resource into an ASCII byte stream */
281 for (i = 0; i < sizeof(*res); i++) {
282 snprintf(tmp, sizeof(tmp), "%02x", ptr[i]);
283 strncat(buf, tmp, 2);
286 err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
290 info->resource_count++;
297 int pciback_publish_pci_roots(struct pciback_device *pdev,
298 publish_pci_root_cb publish_root_cb)
300 struct controller_dev_data *dev_data = pdev->pci_dev_data;
301 struct controller_list_entry *cntrl_entry;
302 int i, root_num, len, err = 0;
303 unsigned int domain, bus;
305 struct walk_info info;
307 spin_lock(&dev_data->lock);
309 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
310 /* First publish all the domain:bus info */
311 err = publish_root_cb(pdev, cntrl_entry->domain,
317 * Now figure out which root-%d this belongs to
318 * so we can associate resources with it.
320 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
321 "root_num", "%d", &root_num);
326 for (i = 0; i < root_num; i++) {
327 len = snprintf(str, sizeof(str), "root-%d", i);
328 if (unlikely(len >= (sizeof(str) - 1))) {
333 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
334 str, "%x:%x", &domain, &bus);
338 /* Is this the one we just published? */
339 if (domain == cntrl_entry->domain &&
340 bus == cntrl_entry->bus)
348 info.resource_count = 0;
351 /* Let ACPI do the heavy lifting on decoding resources */
352 acpi_walk_resources(cntrl_entry->controller->acpi_handle,
353 METHOD_NAME__CRS, write_xenbus_resource,
356 /* No resouces. OK. On to the next one */
357 if (!info.resource_count)
360 /* Store the number of resources we wrote for this root-%d */
361 len = snprintf(str, sizeof(str), "root-%d-resources", i);
362 if (unlikely(len >= (sizeof(str) - 1))) {
367 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
368 "%d", info.resource_count);
373 /* Finally, write some magic to synchronize with the guest. */
374 len = snprintf(str, sizeof(str), "root-resource-magic");
375 if (unlikely(len >= (sizeof(str) - 1))) {
380 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
381 "%lx", (sizeof(struct acpi_resource) * 2) + 1);
384 spin_unlock(&dev_data->lock);
389 void pciback_release_devices(struct pciback_device *pdev)
391 struct controller_dev_data *dev_data = pdev->pci_dev_data;
392 struct controller_list_entry *cntrl_entry, *c;
393 struct controller_dev_entry *dev_entry, *d;
395 list_for_each_entry_safe(cntrl_entry, c, &dev_data->list, list) {
396 list_for_each_entry_safe(dev_entry, d,
397 &cntrl_entry->dev_list, list) {
398 list_del(&dev_entry->list);
399 pcistub_put_pci_dev(dev_entry->dev);
402 list_del(&cntrl_entry->list);
407 pdev->pci_dev_data = NULL;
410 int pciback_get_pcifront_dev(struct pci_dev *pcidev,
411 struct pciback_device *pdev,
412 unsigned int *domain, unsigned int *bus, unsigned int *devfn)
414 struct controller_dev_data *dev_data = pdev->pci_dev_data;
415 struct controller_dev_entry *dev_entry;
416 struct controller_list_entry *cntrl_entry;
419 spin_lock_irqsave(&dev_data->lock, flags);
421 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
422 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
423 if ((dev_entry->dev->bus->number ==
424 pcidev->bus->number) &&
425 (dev_entry->dev->devfn ==
427 (pci_domain_nr(dev_entry->dev->bus) ==
428 pci_domain_nr(pcidev->bus))) {
430 *domain = cntrl_entry->domain;
431 *bus = cntrl_entry->bus;
432 *devfn = dev_entry->devfn;
438 spin_unlock_irqrestore(&dev_data->lock, flags);