ats.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if (WARN_ON(pdev->pri_enabled))
  130. return -EBUSY;
  131. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  132. if (!pos)
  133. return -EINVAL;
  134. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  135. if (!(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. pdev->pri_reqs_alloc = reqs;
  140. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
  141. control = PCI_PRI_CTRL_ENABLE;
  142. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  143. pdev->pri_enabled = 1;
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(pci_enable_pri);
  147. /**
  148. * pci_disable_pri - Disable PRI capability
  149. * @pdev: PCI device structure
  150. *
  151. * Only clears the enabled-bit, regardless of its former value
  152. */
  153. void pci_disable_pri(struct pci_dev *pdev)
  154. {
  155. u16 control;
  156. int pos;
  157. if (WARN_ON(!pdev->pri_enabled))
  158. return;
  159. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  160. if (!pos)
  161. return;
  162. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  163. control &= ~PCI_PRI_CTRL_ENABLE;
  164. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  165. pdev->pri_enabled = 0;
  166. }
  167. EXPORT_SYMBOL_GPL(pci_disable_pri);
  168. /**
  169. * pci_restore_pri_state - Restore PRI
  170. * @pdev: PCI device structure
  171. */
  172. void pci_restore_pri_state(struct pci_dev *pdev)
  173. {
  174. u16 control = PCI_PRI_CTRL_ENABLE;
  175. u32 reqs = pdev->pri_reqs_alloc;
  176. int pos;
  177. if (!pdev->pri_enabled)
  178. return;
  179. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  180. if (!pos)
  181. return;
  182. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ, reqs);
  183. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  184. }
  185. EXPORT_SYMBOL_GPL(pci_restore_pri_state);
  186. /**
  187. * pci_reset_pri - Resets device's PRI state
  188. * @pdev: PCI device structure
  189. *
  190. * The PRI capability must be disabled before this function is called.
  191. * Returns 0 on success, negative value on error.
  192. */
  193. int pci_reset_pri(struct pci_dev *pdev)
  194. {
  195. u16 control;
  196. int pos;
  197. if (WARN_ON(pdev->pri_enabled))
  198. return -EBUSY;
  199. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  200. if (!pos)
  201. return -EINVAL;
  202. control = PCI_PRI_CTRL_RESET;
  203. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(pci_reset_pri);
  207. #endif /* CONFIG_PCI_PRI */
  208. #ifdef CONFIG_PCI_PASID
  209. /**
  210. * pci_enable_pasid - Enable the PASID capability
  211. * @pdev: PCI device structure
  212. * @features: Features to enable
  213. *
  214. * Returns 0 on success, negative value on error. This function checks
  215. * whether the features are actually supported by the device and returns
  216. * an error if not.
  217. */
  218. int pci_enable_pasid(struct pci_dev *pdev, int features)
  219. {
  220. u16 control, supported;
  221. int pos;
  222. if (WARN_ON(pdev->pasid_enabled))
  223. return -EBUSY;
  224. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  225. if (!pos)
  226. return -EINVAL;
  227. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  228. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  229. /* User wants to enable anything unsupported? */
  230. if ((supported & features) != features)
  231. return -EINVAL;
  232. control = PCI_PASID_CTRL_ENABLE | features;
  233. pdev->pasid_features = features;
  234. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  235. pdev->pasid_enabled = 1;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(pci_enable_pasid);
  239. /**
  240. * pci_disable_pasid - Disable the PASID capability
  241. * @pdev: PCI device structure
  242. */
  243. void pci_disable_pasid(struct pci_dev *pdev)
  244. {
  245. u16 control = 0;
  246. int pos;
  247. if (WARN_ON(!pdev->pasid_enabled))
  248. return;
  249. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  250. if (!pos)
  251. return;
  252. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  253. pdev->pasid_enabled = 0;
  254. }
  255. EXPORT_SYMBOL_GPL(pci_disable_pasid);
  256. /**
  257. * pci_restore_pasid_state - Restore PASID capabilities
  258. * @pdev: PCI device structure
  259. */
  260. void pci_restore_pasid_state(struct pci_dev *pdev)
  261. {
  262. u16 control;
  263. int pos;
  264. if (!pdev->pasid_enabled)
  265. return;
  266. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  267. if (!pos)
  268. return;
  269. control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
  270. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  271. }
  272. EXPORT_SYMBOL_GPL(pci_restore_pasid_state);
  273. /**
  274. * pci_pasid_features - Check which PASID features are supported
  275. * @pdev: PCI device structure
  276. *
  277. * Returns a negative value when no PASI capability is present.
  278. * Otherwise is returns a bitmask with supported features. Current
  279. * features reported are:
  280. * PCI_PASID_CAP_EXEC - Execute permission supported
  281. * PCI_PASID_CAP_PRIV - Privileged mode supported
  282. */
  283. int pci_pasid_features(struct pci_dev *pdev)
  284. {
  285. u16 supported;
  286. int pos;
  287. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  288. if (!pos)
  289. return -EINVAL;
  290. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  291. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  292. return supported;
  293. }
  294. EXPORT_SYMBOL_GPL(pci_pasid_features);
  295. #define PASID_NUMBER_SHIFT 8
  296. #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
  297. /**
  298. * pci_max_pasid - Get maximum number of PASIDs supported by device
  299. * @pdev: PCI device structure
  300. *
  301. * Returns negative value when PASID capability is not present.
  302. * Otherwise it returns the numer of supported PASIDs.
  303. */
  304. int pci_max_pasids(struct pci_dev *pdev)
  305. {
  306. u16 supported;
  307. int pos;
  308. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  309. if (!pos)
  310. return -EINVAL;
  311. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  312. supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
  313. return (1 << supported);
  314. }
  315. EXPORT_SYMBOL_GPL(pci_max_pasids);
  316. #endif /* CONFIG_PCI_PASID */