ats.c 7.8 KB

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