override_for_missing_acs_capabilities.patch 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. From 866f4c5de45ae13aa590de0d40819a0c38f3f682 Mon Sep 17 00:00:00 2001
  2. From: Mark Weiman <[email protected]>
  3. Date: Sun, 23 Oct 2016 12:57:37 -0400
  4. Subject: [PATCH] pci: Enable overrides for missing ACS capabilities (4.8+)
  5. This an updated version of Alex Williamson's patch from:
  6. https://lkml.org/lkml/2013/5/30/513
  7. Original commit message follows:
  8. ---
  9. PCIe ACS (Access Control Services) is the PCIe 2.0+ feature that
  10. allows us to control whether transactions are allowed to be redirected
  11. in various subnodes of a PCIe topology. For instance, if two
  12. endpoints are below a root port or downsteam switch port, the
  13. downstream port may optionally redirect transactions between the
  14. devices, bypassing upstream devices. The same can happen internally
  15. on multifunction devices. The transaction may never be visible to the
  16. upstream devices.
  17. One upstream device that we particularly care about is the IOMMU. If
  18. a redirection occurs in the topology below the IOMMU, then the IOMMU
  19. cannot provide isolation between devices. This is why the PCIe spec
  20. encourages topologies to include ACS support. Without it, we have to
  21. assume peer-to-peer DMA within a hierarchy can bypass IOMMU isolation.
  22. Unfortunately, far too many topologies do not support ACS to make this
  23. a steadfast requirement. Even the latest chipsets from Intel are only
  24. sporadically supporting ACS. We have trouble getting interconnect
  25. vendors to include the PCIe spec required PCIe capability, let alone
  26. suggested features.
  27. Therefore, we need to add some flexibility. The pcie_acs_override=
  28. boot option lets users opt-in specific devices or sets of devices to
  29. assume ACS support. The "downstream" option assumes full ACS support
  30. on root ports and downstream switch ports. The "multifunction"
  31. option assumes the subset of ACS features available on multifunction
  32. endpoints and upstream switch ports are supported. The "id:nnnn:nnnn"
  33. option enables ACS support on devices matching the provided vendor
  34. and device IDs, allowing more strategic ACS overrides. These options
  35. may be combined in any order. A maximum of 16 id specific overrides
  36. are available. It's suggested to use the most limited set of options
  37. necessary to avoid completely disabling ACS across the topology.
  38. Note to hardware vendors, we have facilities to permanently quirk
  39. specific devices which enforce isolation but not provide an ACS
  40. capability. Please contact me to have your devices added and save
  41. your customers the hassle of this boot option.
  42. Signed-off-by: Mark Weiman <[email protected]>
  43. ---
  44. Documentation/admin-guide/kernel-parameters.txt | 9 ++++
  45. drivers/pci/quirks.c | 101 ++++++++++++++++++++++++++++++++++++
  46. 2 files changed, 110 insertions(+)
  47. diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
  48. index a4f4d69..d68cfab 100644
  49. --- a/Documentation/admin-guide/kernel-parameters.txt
  50. +++ b/Documentation/admin-guide/kernel-parameters.txt
  51. @@ -2928,6 +2928,15 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
  52. nomsi [MSI] If the PCI_MSI kernel config parameter is
  53. enabled, this kernel boot option can be used to
  54. disable the use of MSI interrupts system-wide.
  55. + pci_acs_override =
  56. + [PCIE] Override missing PCIe ACS support for:
  57. + downstream
  58. + All downstream ports - full ACS capabilities
  59. + multfunction
  60. + All multifunction devices - multifunction ACS subset
  61. + id:nnnn:nnnn
  62. + Specfic device - full ACS capabilities
  63. + Specified as vid:did (vendor/device ID) in hex
  64. noioapicquirk [APIC] Disable all boot interrupt quirks.
  65. Safety option to keep boot IRQs enabled. This
  66. should never be necessary.
  67. diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
  68. index 44e0ff3..32016cb 100644
  69. --- a/drivers/pci/quirks.c
  70. +++ b/drivers/pci/quirks.c
  71. @@ -3487,6 +3487,106 @@ static int __init pci_apply_final_quirks(void)
  72. fs_initcall_sync(pci_apply_final_quirks);
  73. +static bool acs_on_downstream;
  74. +static bool acs_on_multifunction;
  75. +
  76. +#define NUM_ACS_IDS 16
  77. +struct acs_on_id {
  78. + unsigned short vendor;
  79. + unsigned short device;
  80. +};
  81. +static struct acs_on_id acs_on_ids[NUM_ACS_IDS];
  82. +static u8 max_acs_id;
  83. +
  84. +static __init int pcie_acs_override_setup(char *p)
  85. +{
  86. + if (!p)
  87. + return -EINVAL;
  88. +
  89. + while (*p) {
  90. + if (!strncmp(p, "downstream", 10))
  91. + acs_on_downstream = true;
  92. + if (!strncmp(p, "multifunction", 13))
  93. + acs_on_multifunction = true;
  94. + if (!strncmp(p, "id:", 3)) {
  95. + char opt[5];
  96. + int ret;
  97. + long val;
  98. +
  99. + if (max_acs_id >= NUM_ACS_IDS - 1) {
  100. + pr_warn("Out of PCIe ACS override slots (%d)\n",
  101. + NUM_ACS_IDS);
  102. + goto next;
  103. + }
  104. +
  105. + p += 3;
  106. + snprintf(opt, 5, "%s", p);
  107. + ret = kstrtol(opt, 16, &val);
  108. + if (ret) {
  109. + pr_warn("PCIe ACS ID parse error %d\n", ret);
  110. + goto next;
  111. + }
  112. + acs_on_ids[max_acs_id].vendor = val;
  113. +
  114. + p += strcspn(p, ":");
  115. + if (*p != ';') {
  116. + pr_warn("PCIe ACS invalid ID\n");
  117. + goto next;
  118. + }
  119. +
  120. + p++;
  121. + snprintf(opt, 5, "%s", p);
  122. + ret = kstrtol(opt, 16, &val);
  123. + if (ret) {
  124. + pr_warn("PCIe ACS ID parse error %d\n", ret);
  125. + goto next;
  126. + }
  127. + acs_on_ids[max_acs_id].device = val;
  128. + max_acs_id++;
  129. + }
  130. +next:
  131. + p += strcspn(p, ",");
  132. + if (*p == ',')
  133. + p++;
  134. + }
  135. +
  136. + if (acs_on_downstream || acs_on_multifunction || max_acs_id)
  137. + pr_warn("Warning: PCIe ACS overrides enabled; This may allow non-IOMMU protected peer-to-peer DMA\n");
  138. +
  139. + return 0;
  140. +}
  141. +early_param("pcie_acs_override", pcie_acs_override_setup);
  142. +
  143. +static int pcie_acs_overrides(struct pci_dev *dev, u16 acs_flags)
  144. +{
  145. + int i;
  146. +
  147. + /* Never override ACS for legacy devices or devices with ACS caps */
  148. + if (!pci_is_pcie(dev) ||
  149. + pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ACS))
  150. + return -ENOTTY;
  151. +
  152. + for (i = 0; i < max_acs_id; i++)
  153. + if (acs_on_ids[i].vendor == dev->vendor &&
  154. + acs_on_ids[i].device == dev->device)
  155. + return 1;
  156. +
  157. + switch (pci_pcie_type(dev)) {
  158. + case PCI_EXP_TYPE_DOWNSTREAM:
  159. + case PCI_EXP_TYPE_ROOT_PORT:
  160. + if (acs_on_downstream)
  161. + return 1;
  162. + break;
  163. + case PCI_EXP_TYPE_ENDPOINT:
  164. + case PCI_EXP_TYPE_UPSTREAM:
  165. + case PCI_EXP_TYPE_LEG_END:
  166. + case PCI_EXP_TYPE_RC_END:
  167. + if (acs_on_multifunction && dev->multifunction)
  168. + return 1;
  169. + }
  170. +
  171. + return -ENOTTY;
  172. +}
  173. /*
  174. * Followings are device-specific reset methods which can be used to
  175. * reset a single function if other methods (e.g. FLR, PM D0->D3) are
  176. @@ -4160,6 +4260,7 @@ static const struct pci_dev_acs_enabled {
  177. { 0x10df, 0x720, pci_quirk_mf_endpoint_acs }, /* Emulex Skyhawk-R */
  178. /* Cavium ThunderX */
  179. { PCI_VENDOR_ID_CAVIUM, PCI_ANY_ID, pci_quirk_cavium_acs },
  180. + { PCI_ANY_ID, PCI_ANY_ID, pcie_acs_overrides },
  181. { 0 }
  182. };
  183. --
  184. 2.10.1