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_PRI_CAP);
  145. if (!pos)
  146. return -EINVAL;
  147. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  148. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  149. if ((control & PCI_PRI_ENABLE) || !(status & PCI_PRI_STATUS_STOPPED))
  150. return -EBUSY;
  151. pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ_OFF, &max_requests);
  152. reqs = min(max_requests, reqs);
  153. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ_OFF, reqs);
  154. control |= PCI_PRI_ENABLE;
  155. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, control);
  156. return 0;
  157. }
  158. EXPORT_SYMBOL_GPL(pci_enable_pri);
  159. /**
  160. * pci_disable_pri - Disable PRI capability
  161. * @pdev: PCI device structure
  162. *
  163. * Only clears the enabled-bit, regardless of its former value
  164. */
  165. void pci_disable_pri(struct pci_dev *pdev)
  166. {
  167. u16 control;
  168. int pos;
  169. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  170. if (!pos)
  171. return;
  172. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  173. control &= ~PCI_PRI_ENABLE;
  174. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, control);
  175. }
  176. EXPORT_SYMBOL_GPL(pci_disable_pri);
  177. /**
  178. * pci_pri_enabled - Checks if PRI capability is enabled
  179. * @pdev: PCI device structure
  180. *
  181. * Returns true if PRI is enabled on the device, false otherwise
  182. */
  183. bool pci_pri_enabled(struct pci_dev *pdev)
  184. {
  185. u16 control;
  186. int pos;
  187. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  188. if (!pos)
  189. return false;
  190. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  191. return (control & PCI_PRI_ENABLE) ? true : false;
  192. }
  193. EXPORT_SYMBOL_GPL(pci_pri_enabled);
  194. /**
  195. * pci_reset_pri - Resets device's PRI state
  196. * @pdev: PCI device structure
  197. *
  198. * The PRI capability must be disabled before this function is called.
  199. * Returns 0 on success, negative value on error.
  200. */
  201. int pci_reset_pri(struct pci_dev *pdev)
  202. {
  203. u16 control;
  204. int pos;
  205. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  206. if (!pos)
  207. return -EINVAL;
  208. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  209. if (control & PCI_PRI_ENABLE)
  210. return -EBUSY;
  211. control |= PCI_PRI_RESET;
  212. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, control);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(pci_reset_pri);
  216. /**
  217. * pci_pri_stopped - Checks whether the PRI capability is stopped
  218. * @pdev: PCI device structure
  219. *
  220. * Returns true if the PRI capability on the device is disabled and the
  221. * device has no outstanding PRI requests, false otherwise. The device
  222. * indicates this via the STOPPED bit in the status register of the
  223. * capability.
  224. * The device internal state can be cleared by resetting the PRI state
  225. * with pci_reset_pri(). This can force the capability into the STOPPED
  226. * state.
  227. */
  228. bool pci_pri_stopped(struct pci_dev *pdev)
  229. {
  230. u16 control, status;
  231. int pos;
  232. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  233. if (!pos)
  234. return true;
  235. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  236. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  237. if (control & PCI_PRI_ENABLE)
  238. return false;
  239. return (status & PCI_PRI_STATUS_STOPPED) ? true : false;
  240. }
  241. EXPORT_SYMBOL_GPL(pci_pri_stopped);
  242. /**
  243. * pci_pri_status - Request PRI status of a device
  244. * @pdev: PCI device structure
  245. *
  246. * Returns negative value on failure, status on success. The status can
  247. * be checked against status-bits. Supported bits are currently:
  248. * PCI_PRI_STATUS_RF: Response failure
  249. * PCI_PRI_STATUS_UPRGI: Unexpected Page Request Group Index
  250. * PCI_PRI_STATUS_STOPPED: PRI has stopped
  251. */
  252. int pci_pri_status(struct pci_dev *pdev)
  253. {
  254. u16 status, control;
  255. int pos;
  256. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  257. if (!pos)
  258. return -EINVAL;
  259. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  260. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  261. /* Stopped bit is undefined when enable == 1, so clear it */
  262. if (control & PCI_PRI_ENABLE)
  263. status &= ~PCI_PRI_STATUS_STOPPED;
  264. return status;
  265. }
  266. EXPORT_SYMBOL_GPL(pci_pri_status);
  267. #endif /* CONFIG_PCI_PRI */
  268. #ifdef CONFIG_PCI_PASID
  269. /**
  270. * pci_enable_pasid - Enable the PASID capability
  271. * @pdev: PCI device structure
  272. * @features: Features to enable
  273. *
  274. * Returns 0 on success, negative value on error. This function checks
  275. * whether the features are actually supported by the device and returns
  276. * an error if not.
  277. */
  278. int pci_enable_pasid(struct pci_dev *pdev, int features)
  279. {
  280. u16 control, supported;
  281. int pos;
  282. pos = pci_find_ext_capability(pdev, PCI_PASID_CAP);
  283. if (!pos)
  284. return -EINVAL;
  285. pci_read_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, &control);
  286. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &supported);
  287. if (!(supported & PCI_PASID_ENABLE))
  288. return -EINVAL;
  289. supported &= PCI_PASID_EXEC | PCI_PASID_PRIV;
  290. /* User wants to enable anything unsupported? */
  291. if ((supported & features) != features)
  292. return -EINVAL;
  293. control = PCI_PASID_ENABLE | features;
  294. pci_write_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, control);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(pci_enable_pasid);
  298. /**
  299. * pci_disable_pasid - Disable the PASID capability
  300. * @pdev: PCI device structure
  301. *
  302. */
  303. void pci_disable_pasid(struct pci_dev *pdev)
  304. {
  305. u16 control = 0;
  306. int pos;
  307. pos = pci_find_ext_capability(pdev, PCI_PASID_CAP);
  308. if (!pos)
  309. return;
  310. pci_write_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, control);
  311. }
  312. EXPORT_SYMBOL_GPL(pci_disable_pasid);
  313. /**
  314. * pci_pasid_features - Check which PASID features are supported
  315. * @pdev: PCI device structure
  316. *
  317. * Returns a negative value when no PASI capability is present.
  318. * Otherwise is returns a bitmask with supported features. Current
  319. * features reported are:
  320. * PCI_PASID_ENABLE - PASID capability can be enabled
  321. * PCI_PASID_EXEC - Execute permission supported
  322. * PCI_PASID_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_PASID_CAP);
  329. if (!pos)
  330. return -EINVAL;
  331. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &supported);
  332. supported &= PCI_PASID_ENABLE | PCI_PASID_EXEC | PCI_PASID_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_PASID_CAP);
  350. if (!pos)
  351. return -EINVAL;
  352. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &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 */