ats.c 8.8 KB

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