controller.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include <linux/acpi.h>
  27. #include <linux/list.h>
  28. #include <linux/pci.h>
  29. #include <linux/spinlock.h>
  30. #include "pciback.h"
  31. #define PCI_MAX_BUSSES 255
  32. #define PCI_MAX_SLOTS 32
  33. struct controller_dev_entry {
  34. struct list_head list;
  35. struct pci_dev *dev;
  36. unsigned int devfn;
  37. };
  38. struct controller_list_entry {
  39. struct list_head list;
  40. struct pci_controller *controller;
  41. unsigned int domain;
  42. unsigned int bus;
  43. unsigned int next_devfn;
  44. struct list_head dev_list;
  45. };
  46. struct controller_dev_data {
  47. struct list_head list;
  48. unsigned int next_domain;
  49. unsigned int next_bus;
  50. spinlock_t lock;
  51. };
  52. struct walk_info {
  53. struct pciback_device *pdev;
  54. int resource_count;
  55. int root_num;
  56. };
  57. struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
  58. unsigned int domain, unsigned int bus,
  59. unsigned int devfn)
  60. {
  61. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  62. struct controller_dev_entry *dev_entry;
  63. struct controller_list_entry *cntrl_entry;
  64. struct pci_dev *dev = NULL;
  65. unsigned long flags;
  66. spin_lock_irqsave(&dev_data->lock, flags);
  67. list_for_each_entry(cntrl_entry, &dev_data->list, list) {
  68. if (cntrl_entry->domain != domain ||
  69. cntrl_entry->bus != bus)
  70. continue;
  71. list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
  72. if (devfn == dev_entry->devfn) {
  73. dev = dev_entry->dev;
  74. goto found;
  75. }
  76. }
  77. }
  78. found:
  79. spin_unlock_irqrestore(&dev_data->lock, flags);
  80. return dev;
  81. }
  82. int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev,
  83. int devid, publish_pci_dev_cb publish_cb)
  84. {
  85. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  86. struct controller_dev_entry *dev_entry;
  87. struct controller_list_entry *cntrl_entry;
  88. struct pci_controller *dev_controller = PCI_CONTROLLER(dev);
  89. unsigned long flags;
  90. int ret = 0, found = 0;
  91. spin_lock_irqsave(&dev_data->lock, flags);
  92. /* Look to see if we already have a domain:bus for this controller */
  93. list_for_each_entry(cntrl_entry, &dev_data->list, list) {
  94. if (cntrl_entry->controller == dev_controller) {
  95. found = 1;
  96. break;
  97. }
  98. }
  99. if (!found) {
  100. cntrl_entry = kmalloc(sizeof(*cntrl_entry), GFP_ATOMIC);
  101. if (!cntrl_entry) {
  102. ret = -ENOMEM;
  103. goto out;
  104. }
  105. cntrl_entry->controller = dev_controller;
  106. cntrl_entry->next_devfn = PCI_DEVFN(0, 0);
  107. cntrl_entry->domain = dev_data->next_domain;
  108. cntrl_entry->bus = dev_data->next_bus++;
  109. if (dev_data->next_bus > PCI_MAX_BUSSES) {
  110. dev_data->next_domain++;
  111. dev_data->next_bus = 0;
  112. }
  113. INIT_LIST_HEAD(&cntrl_entry->dev_list);
  114. list_add_tail(&cntrl_entry->list, &dev_data->list);
  115. }
  116. if (PCI_SLOT(cntrl_entry->next_devfn) > PCI_MAX_SLOTS) {
  117. /*
  118. * While it seems unlikely, this can actually happen if
  119. * a controller has P2P bridges under it.
  120. */
  121. xenbus_dev_fatal(pdev->xdev, -ENOSPC, "Virtual bus %04x:%02x "
  122. "is full, no room to export %04x:%02x:%02x.%x",
  123. cntrl_entry->domain, cntrl_entry->bus,
  124. pci_domain_nr(dev->bus), dev->bus->number,
  125. PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  126. ret = -ENOSPC;
  127. goto out;
  128. }
  129. dev_entry = kmalloc(sizeof(*dev_entry), GFP_ATOMIC);
  130. if (!dev_entry) {
  131. if (list_empty(&cntrl_entry->dev_list)) {
  132. list_del(&cntrl_entry->list);
  133. kfree(cntrl_entry);
  134. }
  135. ret = -ENOMEM;
  136. goto out;
  137. }
  138. dev_entry->dev = dev;
  139. dev_entry->devfn = cntrl_entry->next_devfn;
  140. list_add_tail(&dev_entry->list, &cntrl_entry->dev_list);
  141. cntrl_entry->next_devfn += PCI_DEVFN(1, 0);
  142. out:
  143. spin_unlock_irqrestore(&dev_data->lock, flags);
  144. /* TODO: Publish virtual domain:bus:slot.func here. */
  145. return ret;
  146. }
  147. void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
  148. {
  149. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  150. struct controller_list_entry *cntrl_entry;
  151. struct controller_dev_entry *dev_entry = NULL;
  152. struct pci_dev *found_dev = NULL;
  153. unsigned long flags;
  154. spin_lock_irqsave(&dev_data->lock, flags);
  155. list_for_each_entry(cntrl_entry, &dev_data->list, list) {
  156. if (cntrl_entry->controller != PCI_CONTROLLER(dev))
  157. continue;
  158. list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
  159. if (dev_entry->dev == dev) {
  160. found_dev = dev_entry->dev;
  161. break;
  162. }
  163. }
  164. }
  165. if (!found_dev) {
  166. spin_unlock_irqrestore(&dev_data->lock, flags);
  167. return;
  168. }
  169. list_del(&dev_entry->list);
  170. kfree(dev_entry);
  171. if (list_empty(&cntrl_entry->dev_list)) {
  172. list_del(&cntrl_entry->list);
  173. kfree(cntrl_entry);
  174. }
  175. spin_unlock_irqrestore(&dev_data->lock, flags);
  176. pcistub_put_pci_dev(found_dev);
  177. }
  178. int pciback_init_devices(struct pciback_device *pdev)
  179. {
  180. struct controller_dev_data *dev_data;
  181. dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
  182. if (!dev_data)
  183. return -ENOMEM;
  184. spin_lock_init(&dev_data->lock);
  185. INIT_LIST_HEAD(&dev_data->list);
  186. /* Starting domain:bus numbers */
  187. dev_data->next_domain = 0;
  188. dev_data->next_bus = 0;
  189. pdev->pci_dev_data = dev_data;
  190. return 0;
  191. }
  192. static acpi_status write_xenbus_resource(struct acpi_resource *res, void *data)
  193. {
  194. struct walk_info *info = data;
  195. struct acpi_resource_address64 addr;
  196. acpi_status status;
  197. int i, len, err;
  198. char str[32], tmp[3];
  199. unsigned char *ptr, *buf;
  200. status = acpi_resource_to_address64(res, &addr);
  201. /* Do we care about this range? Let's check. */
  202. if (!ACPI_SUCCESS(status) ||
  203. !(addr.resource_type == ACPI_MEMORY_RANGE ||
  204. addr.resource_type == ACPI_IO_RANGE) ||
  205. !addr.address_length || addr.producer_consumer != ACPI_PRODUCER)
  206. return AE_OK;
  207. /*
  208. * Furthermore, we really only care to tell the guest about
  209. * address ranges that require address translation of some sort.
  210. */
  211. if (!(addr.resource_type == ACPI_MEMORY_RANGE &&
  212. addr.info.mem.translation) &&
  213. !(addr.resource_type == ACPI_IO_RANGE &&
  214. addr.info.io.translation))
  215. return AE_OK;
  216. /* Store the resource in xenbus for the guest */
  217. len = snprintf(str, sizeof(str), "root-%d-resource-%d",
  218. info->root_num, info->resource_count);
  219. if (unlikely(len >= (sizeof(str) - 1)))
  220. return AE_OK;
  221. buf = kzalloc((sizeof(*res) * 2) + 1, GFP_KERNEL);
  222. if (!buf)
  223. return AE_OK;
  224. /* Clean out resource_source */
  225. res->data.address64.resource_source.index = 0xFF;
  226. res->data.address64.resource_source.string_length = 0;
  227. res->data.address64.resource_source.string_ptr = NULL;
  228. ptr = (unsigned char *)res;
  229. /* Turn the acpi_resource into an ASCII byte stream */
  230. for (i = 0; i < sizeof(*res); i++) {
  231. snprintf(tmp, sizeof(tmp), "%02x", ptr[i]);
  232. strncat(buf, tmp, 2);
  233. }
  234. err = xenbus_printf(XBT_NIL, info->pdev->xdev->nodename,
  235. str, "%s", buf);
  236. if (!err)
  237. info->resource_count++;
  238. kfree(buf);
  239. return AE_OK;
  240. }
  241. int pciback_publish_pci_roots(struct pciback_device *pdev,
  242. publish_pci_root_cb publish_root_cb)
  243. {
  244. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  245. struct controller_list_entry *cntrl_entry;
  246. int i, root_num, len, err = 0;
  247. unsigned int domain, bus;
  248. char str[64];
  249. struct walk_info info;
  250. spin_lock(&dev_data->lock);
  251. list_for_each_entry(cntrl_entry, &dev_data->list, list) {
  252. /* First publish all the domain:bus info */
  253. err = publish_root_cb(pdev, cntrl_entry->domain,
  254. cntrl_entry->bus);
  255. if (err)
  256. goto out;
  257. /*
  258. * Now figure out which root-%d this belongs to
  259. * so we can associate resources with it.
  260. */
  261. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  262. "root_num", "%d", &root_num);
  263. if (err != 1)
  264. goto out;
  265. for (i = 0; i < root_num; i++) {
  266. len = snprintf(str, sizeof(str), "root-%d", i);
  267. if (unlikely(len >= (sizeof(str) - 1))) {
  268. err = -ENOMEM;
  269. goto out;
  270. }
  271. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  272. str, "%x:%x", &domain, &bus);
  273. if (err != 2)
  274. goto out;
  275. /* Is this the one we just published? */
  276. if (domain == cntrl_entry->domain &&
  277. bus == cntrl_entry->bus)
  278. break;
  279. }
  280. if (i == root_num)
  281. goto out;
  282. info.pdev = pdev;
  283. info.resource_count = 0;
  284. info.root_num = i;
  285. /* Let ACPI do the heavy lifting on decoding resources */
  286. acpi_walk_resources(cntrl_entry->controller->acpi_handle,
  287. METHOD_NAME__CRS, write_xenbus_resource,
  288. &info);
  289. /* No resouces. OK. On to the next one */
  290. if (!info.resource_count)
  291. continue;
  292. /* Store the number of resources we wrote for this root-%d */
  293. len = snprintf(str, sizeof(str), "root-%d-resources", i);
  294. if (unlikely(len >= (sizeof(str) - 1))) {
  295. err = -ENOMEM;
  296. goto out;
  297. }
  298. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  299. "%d", info.resource_count);
  300. if (err)
  301. goto out;
  302. }
  303. /* Finally, write some magic to synchronize with the guest. */
  304. len = snprintf(str, sizeof(str), "root-resource-magic");
  305. if (unlikely(len >= (sizeof(str) - 1))) {
  306. err = -ENOMEM;
  307. goto out;
  308. }
  309. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  310. "%lx", (sizeof(struct acpi_resource) * 2) + 1);
  311. out:
  312. spin_unlock(&dev_data->lock);
  313. return err;
  314. }
  315. void pciback_release_devices(struct pciback_device *pdev)
  316. {
  317. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  318. struct controller_list_entry *cntrl_entry, *c;
  319. struct controller_dev_entry *dev_entry, *d;
  320. list_for_each_entry_safe(cntrl_entry, c, &dev_data->list, list) {
  321. list_for_each_entry_safe(dev_entry, d,
  322. &cntrl_entry->dev_list, list) {
  323. list_del(&dev_entry->list);
  324. pcistub_put_pci_dev(dev_entry->dev);
  325. kfree(dev_entry);
  326. }
  327. list_del(&cntrl_entry->list);
  328. kfree(cntrl_entry);
  329. }
  330. kfree(dev_data);
  331. pdev->pci_dev_data = NULL;
  332. }
  333. int pciback_get_pcifront_dev(struct pci_dev *pcidev,
  334. struct pciback_device *pdev,
  335. unsigned int *domain, unsigned int *bus, unsigned int *devfn)
  336. {
  337. struct controller_dev_data *dev_data = pdev->pci_dev_data;
  338. struct controller_dev_entry *dev_entry;
  339. struct controller_list_entry *cntrl_entry;
  340. unsigned long flags;
  341. int found = 0;
  342. spin_lock_irqsave(&dev_data->lock, flags);
  343. list_for_each_entry(cntrl_entry, &dev_data->list, list) {
  344. list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
  345. if ((dev_entry->dev->bus->number ==
  346. pcidev->bus->number) &&
  347. (dev_entry->dev->devfn ==
  348. pcidev->devfn) &&
  349. (pci_domain_nr(dev_entry->dev->bus) ==
  350. pci_domain_nr(pcidev->bus))) {
  351. found = 1;
  352. *domain = cntrl_entry->domain;
  353. *bus = cntrl_entry->bus;
  354. *devfn = dev_entry->devfn;
  355. goto out;
  356. }
  357. }
  358. }
  359. out:
  360. spin_unlock_irqrestore(&dev_data->lock, flags);
  361. return found;
  362. }