ats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_pri_enabled - Checks if PRI capability is enabled
  194. * @pdev: PCI device structure
  195. *
  196. * Returns true if PRI is enabled on the device, false otherwise
  197. */
  198. bool pci_pri_enabled(struct pci_dev *pdev)
  199. {
  200. u16 control;
  201. int pos;
  202. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  203. if (!pos)
  204. return false;
  205. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  206. return (control & PCI_PRI_CTRL_ENABLE) ? true : false;
  207. }
  208. EXPORT_SYMBOL_GPL(pci_pri_enabled);
  209. /**
  210. * pci_reset_pri - Resets device's PRI state
  211. * @pdev: PCI device structure
  212. *
  213. * The PRI capability must be disabled before this function is called.
  214. * Returns 0 on success, negative value on error.
  215. */
  216. int pci_reset_pri(struct pci_dev *pdev)
  217. {
  218. u16 control;
  219. int pos;
  220. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  221. if (!pos)
  222. return -EINVAL;
  223. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  224. if (control & PCI_PRI_CTRL_ENABLE)
  225. return -EBUSY;
  226. control |= PCI_PRI_CTRL_RESET;
  227. pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control);
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(pci_reset_pri);
  231. /**
  232. * pci_pri_stopped - Checks whether the PRI capability is stopped
  233. * @pdev: PCI device structure
  234. *
  235. * Returns true if the PRI capability on the device is disabled and the
  236. * device has no outstanding PRI requests, false otherwise. The device
  237. * indicates this via the STOPPED bit in the status register of the
  238. * capability.
  239. * The device internal state can be cleared by resetting the PRI state
  240. * with pci_reset_pri(). This can force the capability into the STOPPED
  241. * state.
  242. */
  243. bool pci_pri_stopped(struct pci_dev *pdev)
  244. {
  245. u16 control, status;
  246. int pos;
  247. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  248. if (!pos)
  249. return true;
  250. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  251. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  252. if (control & PCI_PRI_CTRL_ENABLE)
  253. return false;
  254. return (status & PCI_PRI_STATUS_STOPPED) ? true : false;
  255. }
  256. EXPORT_SYMBOL_GPL(pci_pri_stopped);
  257. /**
  258. * pci_pri_status - Request PRI status of a device
  259. * @pdev: PCI device structure
  260. *
  261. * Returns negative value on failure, status on success. The status can
  262. * be checked against status-bits. Supported bits are currently:
  263. * PCI_PRI_STATUS_RF: Response failure
  264. * PCI_PRI_STATUS_UPRGI: Unexpected Page Request Group Index
  265. * PCI_PRI_STATUS_STOPPED: PRI has stopped
  266. */
  267. int pci_pri_status(struct pci_dev *pdev)
  268. {
  269. u16 status, control;
  270. int pos;
  271. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  272. if (!pos)
  273. return -EINVAL;
  274. pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control);
  275. pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status);
  276. /* Stopped bit is undefined when enable == 1, so clear it */
  277. if (control & PCI_PRI_CTRL_ENABLE)
  278. status &= ~PCI_PRI_STATUS_STOPPED;
  279. return status;
  280. }
  281. EXPORT_SYMBOL_GPL(pci_pri_status);
  282. #endif /* CONFIG_PCI_PRI */
  283. #ifdef CONFIG_PCI_PASID
  284. /**
  285. * pci_enable_pasid - Enable the PASID capability
  286. * @pdev: PCI device structure
  287. * @features: Features to enable
  288. *
  289. * Returns 0 on success, negative value on error. This function checks
  290. * whether the features are actually supported by the device and returns
  291. * an error if not.
  292. */
  293. int pci_enable_pasid(struct pci_dev *pdev, int features)
  294. {
  295. u16 control, supported;
  296. int pos;
  297. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  298. if (!pos)
  299. return -EINVAL;
  300. pci_read_config_word(pdev, pos + PCI_PASID_CTRL, &control);
  301. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  302. if (control & PCI_PASID_CTRL_ENABLE)
  303. return -EINVAL;
  304. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  305. /* User wants to enable anything unsupported? */
  306. if ((supported & features) != features)
  307. return -EINVAL;
  308. control = PCI_PASID_CTRL_ENABLE | features;
  309. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  310. return 0;
  311. }
  312. EXPORT_SYMBOL_GPL(pci_enable_pasid);
  313. /**
  314. * pci_disable_pasid - Disable the PASID capability
  315. * @pdev: PCI device structure
  316. *
  317. */
  318. void pci_disable_pasid(struct pci_dev *pdev)
  319. {
  320. u16 control = 0;
  321. int pos;
  322. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  323. if (!pos)
  324. return;
  325. pci_write_config_word(pdev, pos + PCI_PASID_CTRL, control);
  326. }
  327. EXPORT_SYMBOL_GPL(pci_disable_pasid);
  328. /**
  329. * pci_pasid_features - Check which PASID features are supported
  330. * @pdev: PCI device structure
  331. *
  332. * Returns a negative value when no PASI capability is present.
  333. * Otherwise is returns a bitmask with supported features. Current
  334. * features reported are:
  335. * PCI_PASID_CAP_EXEC - Execute permission supported
  336. * PCI_PASID_CAP_PRIV - Priviledged mode supported
  337. */
  338. int pci_pasid_features(struct pci_dev *pdev)
  339. {
  340. u16 supported;
  341. int pos;
  342. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  343. if (!pos)
  344. return -EINVAL;
  345. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  346. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  347. return supported;
  348. }
  349. EXPORT_SYMBOL_GPL(pci_pasid_features);
  350. #define PASID_NUMBER_SHIFT 8
  351. #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
  352. /**
  353. * pci_max_pasid - Get maximum number of PASIDs supported by device
  354. * @pdev: PCI device structure
  355. *
  356. * Returns negative value when PASID capability is not present.
  357. * Otherwise it returns the numer of supported PASIDs.
  358. */
  359. int pci_max_pasids(struct pci_dev *pdev)
  360. {
  361. u16 supported;
  362. int pos;
  363. pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  364. if (!pos)
  365. return -EINVAL;
  366. pci_read_config_word(pdev, pos + PCI_PASID_CAP, &supported);
  367. supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
  368. return (1 << supported);
  369. }
  370. EXPORT_SYMBOL_GPL(pci_max_pasids);
  371. #endif /* CONFIG_PCI_PASID */