ats.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /**
  106. * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
  107. * @dev: the PCI device
  108. *
  109. * Returns the queue depth on success, or negative on failure.
  110. *
  111. * The ATS spec uses 0 in the Invalidate Queue Depth field to
  112. * indicate that the function can accept 32 Invalidate Request.
  113. * But here we use the `real' values (i.e. 1~32) for the Queue
  114. * Depth; and 0 indicates the function shares the Queue with
  115. * other functions (doesn't exclusively own a Queue).
  116. */
  117. int pci_ats_queue_depth(struct pci_dev *dev)
  118. {
  119. int pos;
  120. u16 cap;
  121. if (dev->is_virtfn)
  122. return 0;
  123. if (dev->ats)
  124. return dev->ats->qdep;
  125. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
  126. if (!pos)
  127. return -ENODEV;
  128. pci_read_config_word(dev, pos + PCI_ATS_CAP, &cap);
  129. return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) :
  130. PCI_ATS_MAX_QDEP;
  131. }
  132. EXPORT_SYMBOL_GPL(pci_ats_queue_depth);
  133. #ifdef CONFIG_PCI_PRI
  134. /**
  135. * pci_enable_pri - Enable PRI capability
  136. * @ pdev: PCI device structure
  137. *
  138. * Returns 0 on success, negative value on error
  139. */
  140. int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
  141. {
  142. u16 control, status;
  143. u32 max_requests;
  144. int pos;
  145. pos = pci_find_ext_capability(pdev, PCI_PRI_CAP);
  146. if (!pos)
  147. return -EINVAL;
  148. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  149. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  150. if ((control & PCI_PRI_ENABLE) || !(status & PCI_PRI_STATUS_STOPPED))
  151. return -EBUSY;
  152. pci_read_config_dword(pdev, pos + PCI_PRI_MAX_REQ_OFF, &max_requests);
  153. reqs = min(max_requests, reqs);
  154. pci_write_config_dword(pdev, pos + PCI_PRI_ALLOC_REQ_OFF, reqs);
  155. control |= PCI_PRI_ENABLE;
  156. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, 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_PRI_CAP);
  171. if (!pos)
  172. return;
  173. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  174. control &= ~PCI_PRI_ENABLE;
  175. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, 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_PRI_CAP);
  189. if (!pos)
  190. return false;
  191. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  192. return (control & PCI_PRI_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_PRI_CAP);
  207. if (!pos)
  208. return -EINVAL;
  209. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  210. if (control & PCI_PRI_ENABLE)
  211. return -EBUSY;
  212. control |= PCI_PRI_RESET;
  213. pci_write_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, 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_PRI_CAP);
  234. if (!pos)
  235. return true;
  236. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  237. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  238. if (control & PCI_PRI_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_PRI_CAP);
  258. if (!pos)
  259. return -EINVAL;
  260. pci_read_config_word(pdev, pos + PCI_PRI_CONTROL_OFF, &control);
  261. pci_read_config_word(pdev, pos + PCI_PRI_STATUS_OFF, &status);
  262. /* Stopped bit is undefined when enable == 1, so clear it */
  263. if (control & PCI_PRI_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_PASID_CAP);
  284. if (!pos)
  285. return -EINVAL;
  286. pci_read_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, &control);
  287. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &supported);
  288. if (!(supported & PCI_PASID_ENABLE))
  289. return -EINVAL;
  290. supported &= PCI_PASID_EXEC | PCI_PASID_PRIV;
  291. /* User wants to enable anything unsupported? */
  292. if ((supported & features) != features)
  293. return -EINVAL;
  294. control = PCI_PASID_ENABLE | features;
  295. pci_write_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, 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_PASID_CAP);
  309. if (!pos)
  310. return;
  311. pci_write_config_word(pdev, pos + PCI_PASID_CONTROL_OFF, 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_ENABLE - PASID capability can be enabled
  322. * PCI_PASID_EXEC - Execute permission supported
  323. * PCI_PASID_PRIV - Priviledged mode supported
  324. */
  325. int pci_pasid_features(struct pci_dev *pdev)
  326. {
  327. u16 supported;
  328. int pos;
  329. pos = pci_find_ext_capability(pdev, PCI_PASID_CAP);
  330. if (!pos)
  331. return -EINVAL;
  332. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &supported);
  333. supported &= PCI_PASID_ENABLE | PCI_PASID_EXEC | PCI_PASID_PRIV;
  334. return supported;
  335. }
  336. EXPORT_SYMBOL_GPL(pci_pasid_features);
  337. #define PASID_NUMBER_SHIFT 8
  338. #define PASID_NUMBER_MASK (0x1f << PASID_NUMBER_SHIFT)
  339. /**
  340. * pci_max_pasid - Get maximum number of PASIDs supported by device
  341. * @pdev: PCI device structure
  342. *
  343. * Returns negative value when PASID capability is not present.
  344. * Otherwise it returns the numer of supported PASIDs.
  345. */
  346. int pci_max_pasids(struct pci_dev *pdev)
  347. {
  348. u16 supported;
  349. int pos;
  350. pos = pci_find_ext_capability(pdev, PCI_PASID_CAP);
  351. if (!pos)
  352. return -EINVAL;
  353. pci_read_config_word(pdev, pos + PCI_PASID_CAP_OFF, &supported);
  354. supported = (supported & PASID_NUMBER_MASK) >> PASID_NUMBER_SHIFT;
  355. return (1 << supported);
  356. }
  357. EXPORT_SYMBOL_GPL(pci_max_pasids);
  358. #endif /* CONFIG_PCI_PASID */