- patches.rt/0001-sched-count-of-queued-RT-tasks.patch: Delete.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / pciback / controller.c
1 /*
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
36 struct controller_dev_entry {
37         struct list_head list;
38         struct pci_dev *dev;
39         unsigned int devfn;
40 };
41
42 struct 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
51 struct controller_dev_data {
52         struct list_head list;
53         unsigned int next_domain;
54         unsigned int next_bus;
55         spinlock_t lock;
56 };
57
58 struct walk_info {
59         struct pciback_device *pdev;
60         int resource_count;
61         int root_num;
62 };
63
64 struct 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         }
88 found:
89         spin_unlock_irqrestore(&dev_data->lock, flags);
90
91         return dev;
92 }
93
94 int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
95 {
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);
100         unsigned long flags;
101         int ret = 0, found = 0;
102
103         spin_lock_irqsave(&dev_data->lock, flags);
104
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) {
108                         found = 1;
109                         break;
110                 }
111         }
112
113         if (!found) {
114                 cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
115                 if (!cntrl_entry) {
116                         ret =  -ENOMEM;
117                         goto out;
118                 }
119
120                 cntrl_entry->controller = dev_controller;
121                 cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
122
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;
128                 }
129
130                 INIT_LIST_HEAD(&cntrl_entry->dev_list);
131
132                 list_add_tail(&cntrl_entry->list, &dev_data->list);
133         }
134
135         if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
136                 /*
137                  * While it seems unlikely, this can actually happen if
138                  * a controller has P2P bridges under it.
139                  */
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));
145                 ret = -ENOSPC;
146                 goto out;
147         }
148
149         dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
150         if (!dev_entry) {
151                 if (list_empty(&cntrl_entry->dev_list)) {
152                         list_del(&cntrl_entry->list);
153                         kfree(cntrl_entry);
154                 }
155                 ret = -ENOMEM;
156                 goto out;
157         }
158
159         dev_entry->dev = dev;
160         dev_entry->devfn = cntrl_entry->next_devfn;
161
162         list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
163
164         cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
165
166 out:
167         spin_unlock_irqrestore(&dev_data->lock, flags);
168         return ret;
169 }
170
171 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
172 {
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;
177         unsigned long flags;
178
179         spin_lock_irqsave(&dev_data->lock, flags);
180
181         list_for_each_entry(cntrl_entry, &dev_data->list, list) {
182                 if (cntrl_entry->controller != PCI_CONTROLLER(dev))
183                         continue;
184
185                 list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
186                         if (dev_entry->dev == dev) {
187                                 found_dev = dev_entry->dev;
188                                 break;
189                         }
190                 }
191         }
192
193         if (!found_dev) {
194                 spin_unlock_irqrestore(&dev_data->lock, flags);
195                 return;
196         }
197
198         list_del(&dev_entry->list);
199         kfree(dev_entry);
200
201         if (list_empty(&cntrl_entry->dev_list)) {
202                 list_del(&cntrl_entry->list);
203                 kfree(cntrl_entry);
204         }
205
206         spin_unlock_irqrestore(&dev_data->lock, flags);
207         pcistub_put_pci_dev(found_dev);
208 }
209
210 int pciback_init_devices(struct pciback_device *pdev)
211 {
212         struct controller_dev_data *dev_data;
213
214         dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
215         if (!dev_data)
216                 return -ENOMEM;
217
218         spin_lock_init(&dev_data->lock);
219
220         INIT_LIST_HEAD(&dev_data->list);
221
222         /* Starting domain:bus numbers */
223         dev_data->next_domain = 0;
224         dev_data->next_bus = 0;
225
226         pdev->pci_dev_data = dev_data;
227
228         return 0;
229 }
230
231 static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
232 {
233         struct walk_info *info = data;
234         struct acpi_resource_address64 addr;
235         acpi_status status;
236         int i, len, err;
237         char str[32], tmp[3];
238         unsigned char *ptr, *buf;
239
240         status = acpi_resource_to_address64(res, &addr);
241
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)
247                 return AE_OK;
248
249         /*
250          * Furthermore, we really only care to tell the guest about
251          * address ranges that require address translation of some sort.
252          */
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))
257                 return AE_OK;
258            
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)))
263                 return AE_OK;
264
265         buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
266         if (!buf)
267                 return AE_OK;
268
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;
273
274         ptr = (unsigned char *)res;
275
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);
280         }
281
282         err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
283                             str, "%s", buf);
284
285         if (!err)
286                 info->resource_count++;
287
288         kfree(buf);
289
290         return AE_OK;
291 }
292
293 int pciback_publish_pci_roots(struct pciback_device *pdev,
294                               publish_pci_root_cb publish_root_cb)
295 {
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;
300         char str[64];
301         struct walk_info info;
302
303         spin_lock(&dev_data->lock);
304
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,
308                                       cntrl_entry->bus);
309                 if (err)
310                         goto out;
311
312                 /*
313                  * Now figure out which root-%d this belongs to
314                  * so we can associate resources with it.
315                  */
316                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
317                                    "root_num", "%d", &root_num);
318
319                 if (err != 1)
320                         goto out;
321
322                 for (i = 0; i < root_num; i++) {
323                         len = snprintf(str, sizeof(str), "root-%d", i);
324                         if (unlikely(len >= (sizeof(str) - 1))) {
325                                 err = -ENOMEM;
326                                 goto out;
327                         }
328
329                         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
330                                            str, "%x:%x", &domain, &bus);
331                         if (err != 2)
332                                 goto out;
333
334                         /* Is this the one we just published? */
335                         if (domain == cntrl_entry->domain &&
336                             bus == cntrl_entry->bus)
337                                 break;
338                 }
339
340                 if (i == root_num)
341                         goto out;
342
343                 info.pdev = pdev;
344                 info.resource_count = 0;
345                 info.root_num = i;
346
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,
350                                     &info);
351
352                 /* No resouces.  OK.  On to the next one */
353                 if (!info.resource_count)
354                         continue;
355
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))) {
359                         err = -ENOMEM;
360                         goto out;
361                 }
362
363                 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
364                                     "%d", info.resource_count);
365                 if (err)
366                         goto out;
367         }
368
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))) {
372                 err = -ENOMEM;
373                 goto out;
374         }
375
376         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
377                             "%lx", (sizeof(struct acpi_resource) * 2) + 1);
378
379 out:
380         spin_unlock(&dev_data->lock);
381
382         return err;
383 }
384
385 void pciback_release_devices(struct pciback_device *pdev)
386 {
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;
390
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);
396                         kfree(dev_entry);
397                 }
398                 list_del(&cntrl_entry->list);
399                 kfree(cntrl_entry);
400         }
401
402         kfree(dev_data);
403         pdev->pci_dev_data = NULL;
404 }