blob: 7f04f116daec9ed995d537e45da08ff541bb5a75 [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
2 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
3 * Alex Williamson <alex.williamson@hp.com>
4 *
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
8 * to compact the bus.
9 *
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.
15 *
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.
20 *
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/acpi.h>
28#include <linux/list.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
31#include "pciback.h"
32
33#define PCI_MAX_BUSSES 255
34#define PCI_MAX_SLOTS 32
35
36struct controller_dev_entry {
37 struct list_head list;
38 struct pci_dev *dev;
39 unsigned int devfn;
40};
41
42struct controller_list_entry {
43 struct list_head list;
44 struct pci_controller *controller;
45 unsigned int domain;
46 unsigned int bus;
47 unsigned int next_devfn;
48 struct list_head dev_list;
49};
50
51struct controller_dev_data {
52 struct list_head list;
53 unsigned int next_domain;
54 unsigned int next_bus;
55 spinlock_t lock;
56};
57
58struct walk_info {
59 struct pciback_device *pdev;
60 int resource_count;
61 int root_num;
62};
63
64struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
65 unsigned int domain, unsigned int bus,
66 unsigned int devfn)
67{
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;
72 unsigned long flags;
73
74 spin_lock_irqsave(&dev_data->lock, flags);
75
76 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
77 if (cntrl_entry->domain != domain ||
78 cntrl_entry->bus != bus)
79 continue;
80
81 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
82 if (devfn == dev_entry->devfn) {
83 dev = dev_entry->dev;
84 goto found;
85 }
86 }
87 }
88found:
89 spin_unlock_irqrestore(&dev_data->lock, flags);
90
91 return dev;
92}
93
94int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev,
95 int devid, publish_pci_dev_cb publish_cb)
96{
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);
101 unsigned long flags;
102 int ret = 0, found = 0;
103
104 spin_lock_irqsave(&dev_data->lock, flags);
105
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) {
109 found = 1;
110 break;
111 }
112 }
113
114 if (!found) {
115 cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
116 if (!cntrl_entry) {
117 ret = -ENOMEM;
118 goto out;
119 }
120
121 cntrl_entry->controller = dev_controller;
122 cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
123
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;
129 }
130
131 INIT_LIST_HEAD(&cntrl_entry->dev_list);
132
133 list_add_tail(&cntrl_entry->list, &dev_data->list);
134 }
135
136 if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
137 /*
138 * While it seems unlikely, this can actually happen if
139 * a controller has P2P bridges under it.
140 */
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));
146 ret = -ENOSPC;
147 goto out;
148 }
149
150 dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
151 if (!dev_entry) {
152 if (list_empty(&cntrl_entry->dev_list)) {
153 list_del(&cntrl_entry->list);
154 kfree(cntrl_entry);
155 }
156 ret = -ENOMEM;
157 goto out;
158 }
159
160 dev_entry->dev = dev;
161 dev_entry->devfn = cntrl_entry->next_devfn;
162
163 list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
164
165 cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
166
167out:
168 spin_unlock_irqrestore(&dev_data->lock, flags);
169
170 /* TODO: Publish virtual domain:bus:slot.func here. */
171
172 return ret;
173}
174
175void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
176{
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;
181 unsigned long flags;
182
183 spin_lock_irqsave(&dev_data->lock, flags);
184
185 list_for_each_entry(cntrl_entry, &dev_data->list, list) {
186 if (cntrl_entry->controller != PCI_CONTROLLER(dev))
187 continue;
188
189 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
190 if (dev_entry->dev == dev) {
191 found_dev = dev_entry->dev;
192 break;
193 }
194 }
195 }
196
197 if (!found_dev) {
198 spin_unlock_irqrestore(&dev_data->lock, flags);
199 return;
200 }
201
202 list_del(&dev_entry->list);
203 kfree(dev_entry);
204
205 if (list_empty(&cntrl_entry->dev_list)) {
206 list_del(&cntrl_entry->list);
207 kfree(cntrl_entry);
208 }
209
210 spin_unlock_irqrestore(&dev_data->lock, flags);
211 pcistub_put_pci_dev(found_dev);
212}
213
214int pciback_init_devices(struct pciback_device *pdev)
215{
216 struct controller_dev_data *dev_data;
217
218 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
219 if (!dev_data)
220 return -ENOMEM;
221
222 spin_lock_init(&dev_data->lock);
223
224 INIT_LIST_HEAD(&dev_data->list);
225
226 /* Starting domain:bus numbers */
227 dev_data->next_domain = 0;
228 dev_data->next_bus = 0;
229
230 pdev->pci_dev_data = dev_data;
231
232 return 0;
233}
234
235static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
236{
237 struct walk_info *info = data;
238 struct acpi_resource_address64 addr;
239 acpi_status status;
240 int i, len, err;
241 char str[32], tmp[3];
242 unsigned char *ptr, *buf;
243
244 status = acpi_resource_to_address64(res, &addr);
245
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)
251 return AE_OK;
252
253 /*
254 * Furthermore, we really only care to tell the guest about
255 * address ranges that require address translation of some sort.
256 */
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))
261 return AE_OK;
262
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)))
267 return AE_OK;
268
269 buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
270 if (!buf)
271 return AE_OK;
272
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;
277
278 ptr = (unsigned char *)res;
279
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);
284 }
285
286 err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
287 str, "%s", buf);
288
289 if (!err)
290 info->resource_count++;
291
292 kfree(buf);
293
294 return AE_OK;
295}
296
297int pciback_publish_pci_roots(struct pciback_device *pdev,
298 publish_pci_root_cb publish_root_cb)
299{
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;
304 char str[64];
305 struct walk_info info;
306
307 spin_lock(&dev_data->lock);
308
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,
312 cntrl_entry->bus);
313 if (err)
314 goto out;
315
316 /*
317 * Now figure out which root-%d this belongs to
318 * so we can associate resources with it.
319 */
320 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
321 "root_num", "%d", &root_num);
322
323 if (err != 1)
324 goto out;
325
326 for (i = 0; i < root_num; i++) {
327 len = snprintf(str, sizeof(str), "root-%d", i);
328 if (unlikely(len >= (sizeof(str) - 1))) {
329 err = -ENOMEM;
330 goto out;
331 }
332
333 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
334 str, "%x:%x", &domain, &bus);
335 if (err != 2)
336 goto out;
337
338 /* Is this the one we just published? */
339 if (domain == cntrl_entry->domain &&
340 bus == cntrl_entry->bus)
341 break;
342 }
343
344 if (i == root_num)
345 goto out;
346
347 info.pdev = pdev;
348 info.resource_count = 0;
349 info.root_num = i;
350
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,
354 &info);
355
356 /* No resouces. OK. On to the next one */
357 if (!info.resource_count)
358 continue;
359
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))) {
363 err = -ENOMEM;
364 goto out;
365 }
366
367 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
368 "%d", info.resource_count);
369 if (err)
370 goto out;
371 }
372
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))) {
376 err = -ENOMEM;
377 goto out;
378 }
379
380 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
381 "%lx", (sizeof(struct acpi_resource) * 2) + 1);
382
383out:
384 spin_unlock(&dev_data->lock);
385
386 return err;
387}
388
389void pciback_release_devices(struct pciback_device *pdev)
390{
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;
394
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);
400 kfree(dev_entry);
401 }
402 list_del(&cntrl_entry->list);
403 kfree(cntrl_entry);
404 }
405
406 kfree(dev_data);
407 pdev->pci_dev_data = NULL;
408}
409
410int pciback_get_pcifront_dev(struct pci_dev *pcidev,
411 struct pciback_device *pdev,
412 unsigned int *domain, unsigned int *bus, unsigned int *devfn)
413{
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;
417 unsigned long flags;
418 int found = 0;
419 spin_lock_irqsave(&dev_data->lock, flags);
420
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 ==
426 pcidev->devfn) &&
427 (pci_domain_nr(dev_entry->dev->bus) ==
428 pci_domain_nr(pcidev->bus))) {
429 found = 1;
430 *domain = cntrl_entry->domain;
431 *bus = cntrl_entry->bus;
432 *devfn = dev_entry->devfn;
433 goto out;
434 }
435 }
436 }
437out:
438 spin_unlock_irqrestore(&dev_data->lock, flags);
439 return found;
440
441}
442