ats.c 8.6 KB

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