ats.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Express I/O Virtualization (IOV) support
  4. * Address Translation Service 1.0
  5. * Page Request Interface added by Joerg Roedel <joerg.roedel@amd.com>
  6. * PASID support added by Joerg Roedel <joerg.roedel@amd.com>
  7. *
  8. * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
  9. * Copyright (C) 2011 Advanced Micro Devices,
  10. */
  11. #include <linux/bitfield.h>
  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. void pci_ats_init(struct pci_dev *dev)
  18. {
  19. int pos;
  20. if (pci_ats_disabled())
  21. return;
  22. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ATS);
  23. if (!pos)
  24. return;
  25. dev->ats_cap = pos;
  26. }
  27. /**
  28. * pci_ats_supported - check if the device can use ATS
  29. * @dev: the PCI device
  30. *
  31. * Returns true if the device supports ATS and is allowed to use it, false
  32. * otherwise.
  33. */
  34. bool pci_ats_supported(struct pci_dev *dev)
  35. {
  36. if (!dev->ats_cap)
  37. return false;
  38. return (dev->untrusted == 0);
  39. }
  40. EXPORT_SYMBOL_GPL(pci_ats_supported);
  41. /**
  42. * pci_prepare_ats - Setup the PS for ATS
  43. * @dev: the PCI device
  44. * @ps: the IOMMU page shift
  45. *
  46. * This must be done by the IOMMU driver on the PF before any VFs are created to
  47. * ensure that the VF can have ATS enabled.
  48. *
  49. * Returns 0 on success, or negative on failure.
  50. */
  51. int pci_prepare_ats(struct pci_dev *dev, int ps)
  52. {
  53. u16 ctrl;
  54. if (!pci_ats_supported(dev))
  55. return -EINVAL;
  56. if (WARN_ON(dev->ats_enabled))
  57. return -EBUSY;
  58. if (ps < PCI_ATS_MIN_STU)
  59. return -EINVAL;
  60. if (dev->is_virtfn)
  61. return 0;
  62. dev->ats_stu = ps;
  63. ctrl = PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
  64. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(pci_prepare_ats);
  68. /**
  69. * pci_enable_ats - enable the ATS capability
  70. * @dev: the PCI device
  71. * @ps: the IOMMU page shift
  72. *
  73. * Returns 0 on success, or negative on failure.
  74. */
  75. int pci_enable_ats(struct pci_dev *dev, int ps)
  76. {
  77. u16 ctrl;
  78. struct pci_dev *pdev;
  79. if (!pci_ats_supported(dev))
  80. return -EINVAL;
  81. if (WARN_ON(dev->ats_enabled))
  82. return -EBUSY;
  83. if (ps < PCI_ATS_MIN_STU)
  84. return -EINVAL;
  85. /*
  86. * Note that enabling ATS on a VF fails unless it's already enabled
  87. * with the same STU on the PF.
  88. */
  89. ctrl = PCI_ATS_CTRL_ENABLE;
  90. if (dev->is_virtfn) {
  91. pdev = pci_physfn(dev);
  92. if (pdev->ats_stu != ps)
  93. return -EINVAL;
  94. } else {
  95. dev->ats_stu = ps;
  96. ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
  97. }
  98. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  99. dev->ats_enabled = 1;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(pci_enable_ats);
  103. /**
  104. * pci_disable_ats - disable the ATS capability
  105. * @dev: the PCI device
  106. */
  107. void pci_disable_ats(struct pci_dev *dev)
  108. {
  109. u16 ctrl;
  110. if (WARN_ON(!dev->ats_enabled))
  111. return;
  112. pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, &ctrl);
  113. ctrl &= ~PCI_ATS_CTRL_ENABLE;
  114. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  115. dev->ats_enabled = 0;
  116. }
  117. EXPORT_SYMBOL_GPL(pci_disable_ats);
  118. void pci_restore_ats_state(struct pci_dev *dev)
  119. {
  120. u16 ctrl;
  121. if (!dev->ats_enabled)
  122. return;
  123. ctrl = PCI_ATS_CTRL_ENABLE;
  124. if (!dev->is_virtfn)
  125. ctrl |= PCI_ATS_CTRL_STU(dev->ats_stu - PCI_ATS_MIN_STU);
  126. pci_write_config_word(dev, dev->ats_cap + PCI_ATS_CTRL, ctrl);
  127. }
  128. /**
  129. * pci_ats_queue_depth - query the ATS Invalidate Queue Depth
  130. * @dev: the PCI device
  131. *
  132. * Returns the queue depth on success, or negative on failure.
  133. *
  134. * The ATS spec uses 0 in the Invalidate Queue Depth field to
  135. * indicate that the function can accept 32 Invalidate Request.
  136. * But here we use the `real' values (i.e. 1~32) for the Queue
  137. * Depth; and 0 indicates the function shares the Queue with
  138. * other functions (doesn't exclusively own a Queue).
  139. */
  140. int pci_ats_queue_depth(struct pci_dev *dev)
  141. {
  142. u16 cap;
  143. if (!dev->ats_cap)
  144. return -EINVAL;
  145. if (dev->is_virtfn)
  146. return 0;
  147. pci_read_config_word(dev, dev->ats_cap + PCI_ATS_CAP, &cap);
  148. return PCI_ATS_CAP_QDEP(cap) ? PCI_ATS_CAP_QDEP(cap) : PCI_ATS_MAX_QDEP;
  149. }
  150. /**
  151. * pci_ats_page_aligned - Return Page Aligned Request bit status.
  152. * @pdev: the PCI device
  153. *
  154. * Returns 1, if the Untranslated Addresses generated by the device
  155. * are always aligned or 0 otherwise.
  156. *
  157. * Per PCIe spec r4.0, sec 10.5.1.2, if the Page Aligned Request bit
  158. * is set, it indicates the Untranslated Addresses generated by the
  159. * device are always aligned to a 4096 byte boundary.
  160. */
  161. int pci_ats_page_aligned(struct pci_dev *pdev)
  162. {
  163. u16 cap;
  164. if (!pdev->ats_cap)
  165. return 0;
  166. pci_read_config_word(pdev, pdev->ats_cap + PCI_ATS_CAP, &cap);
  167. if (cap & PCI_ATS_CAP_PAGE_ALIGNED)
  168. return 1;
  169. return 0;
  170. }
  171. #ifdef CONFIG_PCI_PRI
  172. void pci_pri_init(struct pci_dev *pdev)
  173. {
  174. u16 status;
  175. pdev->pri_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
  176. if (!pdev->pri_cap)
  177. return;
  178. pci_read_config_word(pdev, pdev->pri_cap + PCI_PRI_STATUS, &status);
  179. if (status & PCI_PRI_STATUS_PASID)
  180. pdev->pasid_required = 1;
  181. }
  182. /**
  183. * pci_enable_pri - Enable PRI capability
  184. * @pdev: PCI device structure
  185. * @reqs: outstanding requests
  186. *
  187. * Returns 0 on success, negative value on error
  188. */
  189. int pci_enable_pri(struct pci_dev *pdev, u32 reqs)
  190. {
  191. u16 control, status;
  192. u32 max_requests;
  193. int pri = pdev->pri_cap;
  194. /*
  195. * VFs must not implement the PRI Capability. If their PF
  196. * implements PRI, it is shared by the VFs, so if the PF PRI is
  197. * enabled, it is also enabled for the VF.
  198. */
  199. if (pdev->is_virtfn) {
  200. if (pci_physfn(pdev)->pri_enabled)
  201. return 0;
  202. return -EINVAL;
  203. }
  204. if (WARN_ON(pdev->pri_enabled))
  205. return -EBUSY;
  206. if (!pri)
  207. return -EINVAL;
  208. pci_read_config_word(pdev, pri + PCI_PRI_STATUS, &status);
  209. if (!(status & PCI_PRI_STATUS_STOPPED))
  210. return -EBUSY;
  211. pci_read_config_dword(pdev, pri + PCI_PRI_MAX_REQ, &max_requests);
  212. reqs = min(max_requests, reqs);
  213. pdev->pri_reqs_alloc = reqs;
  214. pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
  215. control = PCI_PRI_CTRL_ENABLE;
  216. pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
  217. pdev->pri_enabled = 1;
  218. return 0;
  219. }
  220. /**
  221. * pci_disable_pri - Disable PRI capability
  222. * @pdev: PCI device structure
  223. *
  224. * Only clears the enabled-bit, regardless of its former value
  225. */
  226. void pci_disable_pri(struct pci_dev *pdev)
  227. {
  228. u16 control;
  229. int pri = pdev->pri_cap;
  230. /* VFs share the PF PRI */
  231. if (pdev->is_virtfn)
  232. return;
  233. if (WARN_ON(!pdev->pri_enabled))
  234. return;
  235. if (!pri)
  236. return;
  237. pci_read_config_word(pdev, pri + PCI_PRI_CTRL, &control);
  238. control &= ~PCI_PRI_CTRL_ENABLE;
  239. pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
  240. pdev->pri_enabled = 0;
  241. }
  242. EXPORT_SYMBOL_GPL(pci_disable_pri);
  243. /**
  244. * pci_restore_pri_state - Restore PRI
  245. * @pdev: PCI device structure
  246. */
  247. void pci_restore_pri_state(struct pci_dev *pdev)
  248. {
  249. u16 control = PCI_PRI_CTRL_ENABLE;
  250. u32 reqs = pdev->pri_reqs_alloc;
  251. int pri = pdev->pri_cap;
  252. if (pdev->is_virtfn)
  253. return;
  254. if (!pdev->pri_enabled)
  255. return;
  256. if (!pri)
  257. return;
  258. pci_write_config_dword(pdev, pri + PCI_PRI_ALLOC_REQ, reqs);
  259. pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
  260. }
  261. /**
  262. * pci_reset_pri - Resets device's PRI state
  263. * @pdev: PCI device structure
  264. *
  265. * The PRI capability must be disabled before this function is called.
  266. * Returns 0 on success, negative value on error.
  267. */
  268. int pci_reset_pri(struct pci_dev *pdev)
  269. {
  270. u16 control;
  271. int pri = pdev->pri_cap;
  272. if (pdev->is_virtfn)
  273. return 0;
  274. if (WARN_ON(pdev->pri_enabled))
  275. return -EBUSY;
  276. if (!pri)
  277. return -EINVAL;
  278. control = PCI_PRI_CTRL_RESET;
  279. pci_write_config_word(pdev, pri + PCI_PRI_CTRL, control);
  280. return 0;
  281. }
  282. /**
  283. * pci_prg_resp_pasid_required - Return PRG Response PASID Required bit
  284. * status.
  285. * @pdev: PCI device structure
  286. *
  287. * Returns 1 if PASID is required in PRG Response Message, 0 otherwise.
  288. */
  289. int pci_prg_resp_pasid_required(struct pci_dev *pdev)
  290. {
  291. if (pdev->is_virtfn)
  292. pdev = pci_physfn(pdev);
  293. return pdev->pasid_required;
  294. }
  295. /**
  296. * pci_pri_supported - Check if PRI is supported.
  297. * @pdev: PCI device structure
  298. *
  299. * Returns true if PRI capability is present, false otherwise.
  300. */
  301. bool pci_pri_supported(struct pci_dev *pdev)
  302. {
  303. /* VFs share the PF PRI */
  304. if (pci_physfn(pdev)->pri_cap)
  305. return true;
  306. return false;
  307. }
  308. EXPORT_SYMBOL_GPL(pci_pri_supported);
  309. #endif /* CONFIG_PCI_PRI */
  310. #ifdef CONFIG_PCI_PASID
  311. void pci_pasid_init(struct pci_dev *pdev)
  312. {
  313. pdev->pasid_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID);
  314. }
  315. /**
  316. * pci_enable_pasid - Enable the PASID capability
  317. * @pdev: PCI device structure
  318. * @features: Features to enable
  319. *
  320. * Returns 0 on success, negative value on error. This function checks
  321. * whether the features are actually supported by the device and returns
  322. * an error if not.
  323. */
  324. int pci_enable_pasid(struct pci_dev *pdev, int features)
  325. {
  326. u16 control, supported;
  327. int pasid = pdev->pasid_cap;
  328. /*
  329. * VFs must not implement the PASID Capability, but if a PF
  330. * supports PASID, its VFs share the PF PASID configuration.
  331. */
  332. if (pdev->is_virtfn) {
  333. if (pci_physfn(pdev)->pasid_enabled)
  334. return 0;
  335. return -EINVAL;
  336. }
  337. if (WARN_ON(pdev->pasid_enabled))
  338. return -EBUSY;
  339. if (!pdev->eetlp_prefix_path && !pdev->pasid_no_tlp)
  340. return -EINVAL;
  341. if (!pasid)
  342. return -EINVAL;
  343. if (!pci_acs_path_enabled(pdev, NULL, PCI_ACS_RR | PCI_ACS_UF))
  344. return -EINVAL;
  345. pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
  346. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  347. /* User wants to enable anything unsupported? */
  348. if ((supported & features) != features)
  349. return -EINVAL;
  350. control = PCI_PASID_CTRL_ENABLE | features;
  351. pdev->pasid_features = features;
  352. pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
  353. pdev->pasid_enabled = 1;
  354. return 0;
  355. }
  356. EXPORT_SYMBOL_GPL(pci_enable_pasid);
  357. /**
  358. * pci_disable_pasid - Disable the PASID capability
  359. * @pdev: PCI device structure
  360. */
  361. void pci_disable_pasid(struct pci_dev *pdev)
  362. {
  363. u16 control = 0;
  364. int pasid = pdev->pasid_cap;
  365. /* VFs share the PF PASID configuration */
  366. if (pdev->is_virtfn)
  367. return;
  368. if (WARN_ON(!pdev->pasid_enabled))
  369. return;
  370. if (!pasid)
  371. return;
  372. pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
  373. pdev->pasid_enabled = 0;
  374. }
  375. EXPORT_SYMBOL_GPL(pci_disable_pasid);
  376. /**
  377. * pci_restore_pasid_state - Restore PASID capabilities
  378. * @pdev: PCI device structure
  379. */
  380. void pci_restore_pasid_state(struct pci_dev *pdev)
  381. {
  382. u16 control;
  383. int pasid = pdev->pasid_cap;
  384. if (pdev->is_virtfn)
  385. return;
  386. if (!pdev->pasid_enabled)
  387. return;
  388. if (!pasid)
  389. return;
  390. control = PCI_PASID_CTRL_ENABLE | pdev->pasid_features;
  391. pci_write_config_word(pdev, pasid + PCI_PASID_CTRL, control);
  392. }
  393. /**
  394. * pci_pasid_features - Check which PASID features are supported
  395. * @pdev: PCI device structure
  396. *
  397. * Return a negative value when no PASID capability is present.
  398. * Otherwise return a bitmask with supported features. Current
  399. * features reported are:
  400. * PCI_PASID_CAP_EXEC - Execute permission supported
  401. * PCI_PASID_CAP_PRIV - Privileged mode supported
  402. */
  403. int pci_pasid_features(struct pci_dev *pdev)
  404. {
  405. u16 supported;
  406. int pasid;
  407. if (pdev->is_virtfn)
  408. pdev = pci_physfn(pdev);
  409. pasid = pdev->pasid_cap;
  410. if (!pasid)
  411. return -EINVAL;
  412. pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
  413. supported &= PCI_PASID_CAP_EXEC | PCI_PASID_CAP_PRIV;
  414. return supported;
  415. }
  416. EXPORT_SYMBOL_GPL(pci_pasid_features);
  417. /**
  418. * pci_max_pasids - Get maximum number of PASIDs supported by device
  419. * @pdev: PCI device structure
  420. *
  421. * Returns negative value when PASID capability is not present.
  422. * Otherwise it returns the number of supported PASIDs.
  423. */
  424. int pci_max_pasids(struct pci_dev *pdev)
  425. {
  426. u16 supported;
  427. int pasid;
  428. if (pdev->is_virtfn)
  429. pdev = pci_physfn(pdev);
  430. pasid = pdev->pasid_cap;
  431. if (!pasid)
  432. return -EINVAL;
  433. pci_read_config_word(pdev, pasid + PCI_PASID_CAP, &supported);
  434. return (1 << FIELD_GET(PCI_PASID_CAP_WIDTH, supported));
  435. }
  436. EXPORT_SYMBOL_GPL(pci_max_pasids);
  437. /**
  438. * pci_pasid_status - Check the PASID status
  439. * @pdev: PCI device structure
  440. *
  441. * Returns a negative value when no PASID capability is present.
  442. * Otherwise the value of the control register is returned.
  443. * Status reported are:
  444. *
  445. * PCI_PASID_CTRL_ENABLE - PASID enabled
  446. * PCI_PASID_CTRL_EXEC - Execute permission enabled
  447. * PCI_PASID_CTRL_PRIV - Privileged mode enabled
  448. */
  449. int pci_pasid_status(struct pci_dev *pdev)
  450. {
  451. int pasid;
  452. u16 ctrl;
  453. if (pdev->is_virtfn)
  454. pdev = pci_physfn(pdev);
  455. pasid = pdev->pasid_cap;
  456. if (!pasid)
  457. return -EINVAL;
  458. pci_read_config_word(pdev, pasid + PCI_PASID_CTRL, &ctrl);
  459. ctrl &= PCI_PASID_CTRL_ENABLE | PCI_PASID_CTRL_EXEC |
  460. PCI_PASID_CTRL_PRIV;
  461. return ctrl;
  462. }
  463. EXPORT_SYMBOL_GPL(pci_pasid_status);
  464. #endif /* CONFIG_PCI_PASID */