ats.c 10 KB

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