pci.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2007 - 2009 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in
  15. * the file called "COPYING".
  16. *
  17. */
  18. /*
  19. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  20. * copy operations.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dca.h>
  27. #include <linux/slab.h>
  28. #include <linux/acpi.h>
  29. #include "dma.h"
  30. #include "dma_v2.h"
  31. #include "registers.h"
  32. #include "hw.h"
  33. MODULE_VERSION(IOAT_DMA_VERSION);
  34. MODULE_LICENSE("Dual BSD/GPL");
  35. MODULE_AUTHOR("Intel Corporation");
  36. static struct pci_device_id ioat_pci_tbl[] = {
  37. /* I/OAT v3 platforms */
  38. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG0) },
  39. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG1) },
  40. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG2) },
  41. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG3) },
  42. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG4) },
  43. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG5) },
  44. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG6) },
  45. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG7) },
  46. /* I/OAT v3.2 platforms */
  47. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF0) },
  48. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF1) },
  49. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF2) },
  50. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF3) },
  51. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF4) },
  52. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF5) },
  53. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF6) },
  54. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF7) },
  55. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF8) },
  56. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF9) },
  57. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB0) },
  58. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB1) },
  59. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB2) },
  60. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB3) },
  61. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB4) },
  62. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB5) },
  63. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB6) },
  64. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB7) },
  65. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB8) },
  66. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB9) },
  67. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB0) },
  68. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB1) },
  69. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB2) },
  70. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB3) },
  71. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB4) },
  72. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB5) },
  73. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB6) },
  74. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB7) },
  75. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB8) },
  76. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB9) },
  77. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW0) },
  78. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW1) },
  79. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW2) },
  80. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW3) },
  81. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW4) },
  82. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW5) },
  83. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW6) },
  84. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW7) },
  85. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW8) },
  86. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW9) },
  87. /* I/OAT v3.3 platforms */
  88. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD0) },
  89. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD1) },
  90. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD2) },
  91. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD3) },
  92. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE0) },
  93. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE1) },
  94. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE2) },
  95. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE3) },
  96. { 0, }
  97. };
  98. MODULE_DEVICE_TABLE(pci, ioat_pci_tbl);
  99. static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id);
  100. static void ioat_remove(struct pci_dev *pdev);
  101. static int ioat_dca_enabled = 1;
  102. module_param(ioat_dca_enabled, int, 0644);
  103. MODULE_PARM_DESC(ioat_dca_enabled, "control support of dca service (default: 1)");
  104. struct kmem_cache *ioat2_cache;
  105. struct kmem_cache *ioat3_sed_cache;
  106. #define DRV_NAME "ioatdma"
  107. static struct pci_driver ioat_pci_driver = {
  108. .name = DRV_NAME,
  109. .id_table = ioat_pci_tbl,
  110. .probe = ioat_pci_probe,
  111. .remove = ioat_remove,
  112. };
  113. static struct ioatdma_device *
  114. alloc_ioatdma(struct pci_dev *pdev, void __iomem *iobase)
  115. {
  116. struct device *dev = &pdev->dev;
  117. struct ioatdma_device *d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
  118. if (!d)
  119. return NULL;
  120. d->pdev = pdev;
  121. d->reg_base = iobase;
  122. return d;
  123. }
  124. /*
  125. * The dmaengine core assumes that async DMA devices will only be removed
  126. * when they not used anymore, or it assumes dma_async_device_unregister()
  127. * will only be called by dma driver exit routines. But this assumption is
  128. * not true for the IOAT driver, which calls dma_async_device_unregister()
  129. * from ioat_remove(). So current IOAT driver doesn't support device
  130. * hot-removal because it may cause system crash to hot-remove inuse IOAT
  131. * devices.
  132. *
  133. * This is a hack to disable IOAT devices under ejectable PCI host bridge
  134. * so it won't break PCI host bridge hot-removal.
  135. */
  136. static bool ioat_pci_has_ejectable_acpi_ancestor(struct pci_dev *pdev)
  137. {
  138. #ifdef CONFIG_ACPI
  139. struct pci_bus *bus = pdev->bus;
  140. struct acpi_device *adev;
  141. while (bus->parent)
  142. bus = bus->parent;
  143. for (adev = ACPI_COMPANION(bus->bridge); adev; adev = adev->parent)
  144. if (adev->flags.ejectable)
  145. return true;
  146. #endif
  147. return false;
  148. }
  149. static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  150. {
  151. void __iomem * const *iomap;
  152. struct device *dev = &pdev->dev;
  153. struct ioatdma_device *device;
  154. int err;
  155. if (ioat_pci_has_ejectable_acpi_ancestor(pdev)) {
  156. dev_dbg(&pdev->dev, "ignore ejectable IOAT device.\n");
  157. return -ENODEV;
  158. }
  159. err = pcim_enable_device(pdev);
  160. if (err)
  161. return err;
  162. err = pcim_iomap_regions(pdev, 1 << IOAT_MMIO_BAR, DRV_NAME);
  163. if (err)
  164. return err;
  165. iomap = pcim_iomap_table(pdev);
  166. if (!iomap)
  167. return -ENOMEM;
  168. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  169. if (err)
  170. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  171. if (err)
  172. return err;
  173. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  174. if (err)
  175. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  176. if (err)
  177. return err;
  178. device = alloc_ioatdma(pdev, iomap[IOAT_MMIO_BAR]);
  179. if (!device)
  180. return -ENOMEM;
  181. pci_set_master(pdev);
  182. pci_set_drvdata(pdev, device);
  183. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  184. if (device->version == IOAT_VER_2_0)
  185. err = ioat2_dma_probe(device, ioat_dca_enabled);
  186. else if (device->version >= IOAT_VER_3_0)
  187. err = ioat3_dma_probe(device, ioat_dca_enabled);
  188. else
  189. return -ENODEV;
  190. if (err) {
  191. dev_err(dev, "Intel(R) I/OAT DMA Engine init failed\n");
  192. return -ENODEV;
  193. }
  194. return 0;
  195. }
  196. static void ioat_remove(struct pci_dev *pdev)
  197. {
  198. struct ioatdma_device *device = pci_get_drvdata(pdev);
  199. if (!device)
  200. return;
  201. dev_err(&pdev->dev, "Removing dma and dca services\n");
  202. if (device->dca) {
  203. unregister_dca_provider(device->dca, &pdev->dev);
  204. free_dca_provider(device->dca);
  205. device->dca = NULL;
  206. }
  207. ioat_dma_remove(device);
  208. }
  209. static int __init ioat_init_module(void)
  210. {
  211. int err = -ENOMEM;
  212. pr_info("%s: Intel(R) QuickData Technology Driver %s\n",
  213. DRV_NAME, IOAT_DMA_VERSION);
  214. ioat2_cache = kmem_cache_create("ioat2", sizeof(struct ioat_ring_ent),
  215. 0, SLAB_HWCACHE_ALIGN, NULL);
  216. if (!ioat2_cache)
  217. return -ENOMEM;
  218. ioat3_sed_cache = KMEM_CACHE(ioat_sed_ent, 0);
  219. if (!ioat3_sed_cache)
  220. goto err_ioat2_cache;
  221. err = pci_register_driver(&ioat_pci_driver);
  222. if (err)
  223. goto err_ioat3_cache;
  224. return 0;
  225. err_ioat3_cache:
  226. kmem_cache_destroy(ioat3_sed_cache);
  227. err_ioat2_cache:
  228. kmem_cache_destroy(ioat2_cache);
  229. return err;
  230. }
  231. module_init(ioat_init_module);
  232. static void __exit ioat_exit_module(void)
  233. {
  234. pci_unregister_driver(&ioat_pci_driver);
  235. kmem_cache_destroy(ioat2_cache);
  236. }
  237. module_exit(ioat_exit_module);