ats.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Express I/O Virtualization (IOV) support
  4. * Address Translation Service 1.0
  5. * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
  6. * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
  7. *
  8. * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
  9. * Copyright (C) 2011 Advanced Micro Devices,
  10. */
  11. #include <linux/export.h>
  12. #include <linux/pci-ats.h>
  13. #include <linux/pci.h>
  14. #include <linux/slab.h>
  15. #include "pci.h"
  16. void pci_ats_init(struct pci_dev *dev)
  17. {
  18. int pos;
  19. if (pci_ats_disabled())
  20. return;
  21. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
  22. if (!pos)
  23. return;
  24. dev->ats_cap = pos;
  25. }
  26. /**
  27. * pci_enable_ats - enable the ATS capability
  28. * @dev: the PCI device
  29. * @ps: the IOMMU page shift
  30. *
  31. * Returns 0 on success, or negative on failure.
  32. */
  33. int pci_enable_ats(struct pci_dev *dev, int ps)
  34. {
  35. u16 ctrl;
  36. struct pci_dev *pdev;
  37. if (!dev->ats_cap)
  38. return -EINVAL;
  39. if (WARN_ON(dev->ats_enabled))
  40. return -EBUSY;
  41. if (ps < PCI_ATS_MIN_STU)
  42. return -EINVAL;
  43. /*
  44. * Note that enabling ATS on a VF fails unless it's already enabled
  45. * with the same STU on the PF.
  46. */
  47. ctrl = PCI_ATS_CTRL_ENABLE;
  48. if (dev->is_virtfn) {
  49. pdev = pci_physfn(dev);
  50. if (pdev->ats_stu != ps)
  51. return -EINVAL;
  52. atomic_inc(&pdev->ats_ref_cnt); /* count enabled VFs */
  53. } else {
  54. dev->ats_stu = ps;
  55. ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
  56. }
  57. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  58. dev->ats_enabled = 1;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(pci_enable_ats);
  62. /**
  63. * pci_disable_ats - disable the ATS capability
  64. * @dev: the PCI device
  65. */
  66. void pci_disable_ats(struct pci_dev *dev)
  67. {
  68. struct pci_dev *pdev;
  69. u16 ctrl;
  70. if (WARN_ON(!dev->ats_enabled))
  71. return;
  72. if (atomic_read(&dev->ats_ref_cnt))
  73. return; /* VFs still enabled */
  74. if (dev->is_virtfn) {
  75. pdev = pci_physfn(dev);
  76. atomic_dec(&pdev->ats_ref_cnt);
  77. }
  78. pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
  79. ctrl &= ~PCI_ATS_CTRL_ENABLE;
  80. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  81. dev->ats_enabled = 0;
  82. }
  83. EXPORT_SYMBOL_GPL(pci_disable_ats);
  84. void pci_restore_ats_state(struct pci_dev *dev)
  85. {
  86. u16 ctrl;
  87. if (!dev->ats_enabled)
  88. return;
  89. ctrl = PCI_ATS_CTRL_ENABLE;
  90. if (!dev->is_virtfn)
  91. ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
  92. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  93. }
  94. EXPORT_SYMBOL_GPL(pci_restore_ats_state);
  95. /**
  96. * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
  97. * @dev: the PCI device
  98. *
  99. * Returns the queue depth on success, or negative on failure.
  100. *
  101. * The ATS spec uses 0 in the Invalidate Queue Depth field to
  102. * indicate that the function can accept 32 Invalidate Request.
  103. * But here we use the `real' values (i.e. 1~32) for the Queue
  104. * Depth; and 0 indicates the function shares the Queue with
  105. * other functions (doesn't exclusively own a Queue).
  106. */
  107. int pci_ats_queue_depth(struct pci_dev *dev)
  108. {
  109. u16 cap;
  110. if (!dev->ats_cap)
  111. return -EINVAL;
  112. if (dev->is_virtfn)
  113. return 0;
  114. pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
  115. return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
  116. }
  117. EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
  118. #ifdef CONFIG_PCI_PRI
  119. /**
  120. * pci_enable_pri - Enable PRI capability
  121. * @ pdev: PCI device structure
  122. *
  123. * Returns 0 on success, negative value on error
  124. */
  125. int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
  126. {
  127. u16 control, status;
  128. u32 max_requests;
  129. int pos;
  130. if (WARN_ON(pdev->pri_enabled))
  131. return -EBUSY;
  132. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  133. if (!pos)
  134. return -EINVAL;
  135. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  136. if (!(status & PCI_PRI_STATUS_STOPPED))
  137. return -EBUSY;
  138. pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ, &max_requests);
  139. reqs = min(max_requests, reqs);
  140. pdev->pri_reqs_alloc = reqs;
  141. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
  142. control = PCI_PRI_CTRL_ENABLE;
  143. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  144. pdev->pri_enabled = 1;
  145. return 0;
  146. }
  147. EXPORT_SYMBOL_GPL(pci_enable_pri);
  148. /**
  149. * pci_disable_pri - Disable PRI capability
  150. * @pdev: PCI device structure
  151. *
  152. * Only clears the enabled-bit, regardless of its former value
  153. */
  154. void pci_disable_pri(struct pci_dev *pdev)
  155. {
  156. u16 control;
  157. int pos;
  158. if (WARN_ON(!pdev->pri_enabled))
  159. return;
  160. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  161. if (!pos)
  162. return;
  163. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  164. control &= ~PCI_PRI_CTRL_ENABLE;
  165. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  166. pdev->pri_enabled = 0;
  167. }
  168. EXPORT_SYMBOL_GPL(pci_disable_pri);
  169. /**
  170. * pci_restore_pri_state - Restore PRI
  171. * @pdev: PCI device structure
  172. */
  173. void pci_restore_pri_state(struct pci_dev *pdev)
  174. {
  175. u16 control = PCI_PRI_CTRL_ENABLE;
  176. u32 reqs = pdev->pri_reqs_alloc;
  177. int pos;
  178. if (!pdev->pri_enabled)
  179. return;
  180. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  181. if (!pos)
  182. return;
  183. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
  184. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  185. }
  186. EXPORT_SYMBOL_GPL(pci_restore_pri_state);
  187. /**
  188. * pci_reset_pri - Resets device's PRI state
  189. * @pdev: PCI device structure
  190. *
  191. * The PRI capability must be disabled before this function is called.
  192. * Returns 0 on success, negative value on error.
  193. */
  194. int pci_reset_pri(struct pci_dev *pdev)
  195. {
  196. u16 control;
  197. int pos;
  198. if (WARN_ON(pdev->pri_enabled))
  199. return -EBUSY;
  200. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  201. if (!pos)
  202. return -EINVAL;
  203. control = PCI_PRI_CTRL_RESET;
  204. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(pci_reset_pri);
  208. #endif /* CONFIG_PCI_PRI */
  209. #ifdef CONFIG_PCI_PASID
  210. /**
  211. * pci_enable_pasid - Enable the PASID capability
  212. * @pdev: PCI device structure
  213. * @features: Features to enable
  214. *
  215. * Returns 0 on success, negative value on error. This function checks
  216. * whether the features are actually supported by the device and returns
  217. * an error if not.
  218. */
  219. int pci_enable_pasid(struct pci_dev *pdev, int features)
  220. {
  221. u16 control, supported;
  222. int pos;
  223. if (WARN_ON(pdev->pasid_enabled))
  224. return -EBUSY;
  225. if (!pdev->eetlp_prefix_path)
  226. return -EINVAL;
  227. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  228. if (!pos)
  229. return -EINVAL;
  230. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  231. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  232. /* User wants to enable anything unsupported? */
  233. if ((supported & features) != features)
  234. return -EINVAL;
  235. control = PCI_PASID_CTRL_ENABLE | features;
  236. pdev->pasid_features = features;
  237. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  238. pdev->pasid_enabled = 1;
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(pci_enable_pasid);
  242. /**
  243. * pci_disable_pasid - Disable the PASID capability
  244. * @pdev: PCI device structure
  245. */
  246. void pci_disable_pasid(struct pci_dev *pdev)
  247. {
  248. u16 control = 0;
  249. int pos;
  250. if (WARN_ON(!pdev->pasid_enabled))
  251. return;
  252. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  253. if (!pos)
  254. return;
  255. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  256. pdev->pasid_enabled = 0;
  257. }
  258. EXPORT_SYMBOL_GPL(pci_disable_pasid);
  259. /**
  260. * pci_restore_pasid_state - Restore PASID capabilities
  261. * @pdev: PCI device structure
  262. */
  263. void pci_restore_pasid_state(struct pci_dev *pdev)
  264. {
  265. u16 control;
  266. int pos;
  267. if (!pdev->pasid_enabled)
  268. return;
  269. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  270. if (!pos)
  271. return;
  272. control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
  273. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  274. }
  275. EXPORT_SYMBOL_GPL(pci_restore_pasid_state);
  276. /**
  277. * pci_pasid_features - Check which PASID features are supported
  278. * @pdev: PCI device structure
  279. *
  280. * Returns a negative value when no PASI capability is present.
  281. * Otherwise is returns a bitmask with supported features. Current
  282. * features reported are:
  283. * PCI_PASID_CAP_EXEC - Execute permission supported
  284. * PCI_PASID_CAP_PRIV - Privileged mode supported
  285. */
  286. int pci_pasid_features(struct pci_dev *pdev)
  287. {
  288. u16 supported;
  289. int pos;
  290. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  291. if (!pos)
  292. return -EINVAL;
  293. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  294. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  295. return supported;
  296. }
  297. EXPORT_SYMBOL_GPL(pci_pasid_features);
  298. #define PASID_NUMBER_SHIFT 8
  299. #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
  300. /**
  301. * pci_max_pasid - Get maximum number of PASIDs supported by device
  302. * @pdev: PCI device structure
  303. *
  304. * Returns negative value when PASID capability is not present.
  305. * Otherwise it returns the numer of supported PASIDs.
  306. */
  307. int pci_max_pasids(struct pci_dev *pdev)
  308. {
  309. u16 supported;
  310. int pos;
  311. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  312. if (!pos)
  313. return -EINVAL;
  314. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  315. supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
  316. return (1 << supported);
  317. }
  318. EXPORT_SYMBOL_GPL(pci_max_pasids);
  319. #endif /* CONFIG_PCI_PASID */