ats.c 10 KB

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