vc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Virtual Channel support
  4. *
  5. * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <alex.williamson@redhat.com>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/device.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci_regs.h>
  14. #include <linux/types.h>
  15. #include "pci.h"
  16. /**
  17. * pci_vc_save_restore_dwords - Save or restore a series of dwords
  18. * @dev: device
  19. * @pos: starting config space position
  20. * @buf: buffer to save to or restore from
  21. * @dwords: number of dwords to save/restore
  22. * @save: whether to save or restore
  23. */
  24. static void pci_vc_save_restore_dwords(struct pci_dev *dev, int pos,
  25. u32 *buf, int dwords, bool save)
  26. {
  27. int i;
  28. for (i = 0; i < dwords; i++, buf++) {
  29. if (save)
  30. pci_read_config_dword(dev, pos + (i * 4), buf);
  31. else
  32. pci_write_config_dword(dev, pos + (i * 4), *buf);
  33. }
  34. }
  35. /**
  36. * pci_vc_load_arb_table - load and wait for VC arbitration table
  37. * @dev: device
  38. * @pos: starting position of VC capability (VC/VC9/MFVC)
  39. *
  40. * Set Load VC Arbitration Table bit requesting hardware to apply the VC
  41. * Arbitration Table (previously loaded). When the VC Arbitration Table
  42. * Status clears, hardware has latched the table into VC arbitration logic.
  43. */
  44. static void pci_vc_load_arb_table(struct pci_dev *dev, int pos)
  45. {
  46. u16 ctrl;
  47. pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL, &ctrl);
  48. pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
  49. ctrl | PCI_VC_PORT_CTRL_LOAD_TABLE);
  50. if (pci_wait_for_pending(dev, pos + PCI_VC_PORT_STATUS,
  51. PCI_VC_PORT_STATUS_TABLE))
  52. return;
  53. pci_err(dev, "VC arbitration table failed to load\n");
  54. }
  55. /**
  56. * pci_vc_load_port_arb_table - Load and wait for VC port arbitration table
  57. * @dev: device
  58. * @pos: starting position of VC capability (VC/VC9/MFVC)
  59. * @res: VC resource number, ie. VCn (0-7)
  60. *
  61. * Set Load Port Arbitration Table bit requesting hardware to apply the Port
  62. * Arbitration Table (previously loaded). When the Port Arbitration Table
  63. * Status clears, hardware has latched the table into port arbitration logic.
  64. */
  65. static void pci_vc_load_port_arb_table(struct pci_dev *dev, int pos, int res)
  66. {
  67. int ctrl_pos, status_pos;
  68. u32 ctrl;
  69. ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  70. status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  71. pci_read_config_dword(dev, ctrl_pos, &ctrl);
  72. pci_write_config_dword(dev, ctrl_pos,
  73. ctrl | PCI_VC_RES_CTRL_LOAD_TABLE);
  74. if (pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_TABLE))
  75. return;
  76. pci_err(dev, "VC%d port arbitration table failed to load\n", res);
  77. }
  78. /**
  79. * pci_vc_enable - Enable virtual channel
  80. * @dev: device
  81. * @pos: starting position of VC capability (VC/VC9/MFVC)
  82. * @res: VC res number, ie. VCn (0-7)
  83. *
  84. * A VC is enabled by setting the enable bit in matching resource control
  85. * registers on both sides of a link. We therefore need to find the opposite
  86. * end of the link. To keep this simple we enable from the downstream device.
  87. * RC devices do not have an upstream device, nor does it seem that VC9 do
  88. * (spec is unclear). Once we find the upstream device, match the VC ID to
  89. * get the correct resource, disable and enable on both ends.
  90. */
  91. static void pci_vc_enable(struct pci_dev *dev, int pos, int res)
  92. {
  93. int ctrl_pos, status_pos, id, pos2, evcc, i, ctrl_pos2, status_pos2;
  94. u32 ctrl, header, cap1, ctrl2;
  95. struct pci_dev *link = NULL;
  96. /* Enable VCs from the downstream device */
  97. if (!pci_is_pcie(dev) || !pcie_downstream_port(dev))
  98. return;
  99. ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  100. status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  101. pci_read_config_dword(dev, ctrl_pos, &ctrl);
  102. id = ctrl & PCI_VC_RES_CTRL_ID;
  103. pci_read_config_dword(dev, pos, &header);
  104. /* If there is no opposite end of the link, skip to enable */
  105. if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_VC9 ||
  106. pci_is_root_bus(dev->bus))
  107. goto enable;
  108. pos2 = pci_find_ext_capability(dev->bus->self, PCI_EXT_CAP_ID_VC);
  109. if (!pos2)
  110. goto enable;
  111. pci_read_config_dword(dev->bus->self, pos2 + PCI_VC_PORT_CAP1, &cap1);
  112. evcc = cap1 & PCI_VC_CAP1_EVCC;
  113. /* VC0 is hardwired enabled, so we can start with 1 */
  114. for (i = 1; i < evcc + 1; i++) {
  115. ctrl_pos2 = pos2 + PCI_VC_RES_CTRL +
  116. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  117. status_pos2 = pos2 + PCI_VC_RES_STATUS +
  118. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  119. pci_read_config_dword(dev->bus->self, ctrl_pos2, &ctrl2);
  120. if ((ctrl2 & PCI_VC_RES_CTRL_ID) == id) {
  121. link = dev->bus->self;
  122. break;
  123. }
  124. }
  125. if (!link)
  126. goto enable;
  127. /* Disable if enabled */
  128. if (ctrl2 & PCI_VC_RES_CTRL_ENABLE) {
  129. ctrl2 &= ~PCI_VC_RES_CTRL_ENABLE;
  130. pci_write_config_dword(link, ctrl_pos2, ctrl2);
  131. }
  132. /* Enable on both ends */
  133. ctrl2 |= PCI_VC_RES_CTRL_ENABLE;
  134. pci_write_config_dword(link, ctrl_pos2, ctrl2);
  135. enable:
  136. ctrl |= PCI_VC_RES_CTRL_ENABLE;
  137. pci_write_config_dword(dev, ctrl_pos, ctrl);
  138. if (!pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_NEGO))
  139. pci_err(dev, "VC%d negotiation stuck pending\n", id);
  140. if (link && !pci_wait_for_pending(link, status_pos2,
  141. PCI_VC_RES_STATUS_NEGO))
  142. pci_err(link, "VC%d negotiation stuck pending\n", id);
  143. }
  144. /**
  145. * pci_vc_do_save_buffer - Size, save, or restore VC state
  146. * @dev: device
  147. * @pos: starting position of VC capability (VC/VC9/MFVC)
  148. * @save_state: buffer for save/restore
  149. * @save: if provided a buffer, this indicates what to do with it
  150. *
  151. * Walking Virtual Channel config space to size, save, or restore it
  152. * is complicated, so we do it all from one function to reduce code and
  153. * guarantee ordering matches in the buffer. When called with NULL
  154. * @save_state, return the size of the necessary save buffer. When called
  155. * with a non-NULL @save_state, @save determines whether we save to the
  156. * buffer or restore from it.
  157. */
  158. static int pci_vc_do_save_buffer(struct pci_dev *dev, int pos,
  159. struct pci_cap_saved_state *save_state,
  160. bool save)
  161. {
  162. u32 cap1;
  163. char evcc, lpevcc, parb_size;
  164. int i, len = 0;
  165. u8 *buf = save_state ? (u8 *)save_state->cap.data : NULL;
  166. /* Sanity check buffer size for save/restore */
  167. if (buf && save_state->cap.size !=
  168. pci_vc_do_save_buffer(dev, pos, NULL, save)) {
  169. pci_err(dev, "VC save buffer size does not match @0x%x\n", pos);
  170. return -ENOMEM;
  171. }
  172. pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP1, &cap1);
  173. /* Extended VC Count (not counting VC0) */
  174. evcc = cap1 & PCI_VC_CAP1_EVCC;
  175. /* Low Priority Extended VC Count (not counting VC0) */
  176. lpevcc = FIELD_GET(PCI_VC_CAP1_LPEVCC, cap1);
  177. /* Port Arbitration Table Entry Size (bits) */
  178. parb_size = 1 << FIELD_GET(PCI_VC_CAP1_ARB_SIZE, cap1);
  179. /*
  180. * Port VC Control Register contains VC Arbitration Select, which
  181. * cannot be modified when more than one LPVC is in operation. We
  182. * therefore save/restore it first, as only VC0 should be enabled
  183. * after device reset.
  184. */
  185. if (buf) {
  186. if (save)
  187. pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL,
  188. (u16 *)buf);
  189. else
  190. pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
  191. *(u16 *)buf);
  192. buf += 4;
  193. }
  194. len += 4;
  195. /*
  196. * If we have any Low Priority VCs and a VC Arbitration Table Offset
  197. * in Port VC Capability Register 2 then save/restore it next.
  198. */
  199. if (lpevcc) {
  200. u32 cap2;
  201. int vcarb_offset;
  202. pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP2, &cap2);
  203. vcarb_offset = FIELD_GET(PCI_VC_CAP2_ARB_OFF, cap2) * 16;
  204. if (vcarb_offset) {
  205. int size, vcarb_phases = 0;
  206. if (cap2 & PCI_VC_CAP2_128_PHASE)
  207. vcarb_phases = 128;
  208. else if (cap2 & PCI_VC_CAP2_64_PHASE)
  209. vcarb_phases = 64;
  210. else if (cap2 & PCI_VC_CAP2_32_PHASE)
  211. vcarb_phases = 32;
  212. /* Fixed 4 bits per phase per lpevcc (plus VC0) */
  213. size = ((lpevcc + 1) * vcarb_phases * 4) / 8;
  214. if (size && buf) {
  215. pci_vc_save_restore_dwords(dev,
  216. pos + vcarb_offset,
  217. (u32 *)buf,
  218. size / 4, save);
  219. /*
  220. * On restore, we need to signal hardware to
  221. * re-load the VC Arbitration Table.
  222. */
  223. if (!save)
  224. pci_vc_load_arb_table(dev, pos);
  225. buf += size;
  226. }
  227. len += size;
  228. }
  229. }
  230. /*
  231. * In addition to each VC Resource Control Register, we may have a
  232. * Port Arbitration Table attached to each VC. The Port Arbitration
  233. * Table Offset in each VC Resource Capability Register tells us if
  234. * it exists. The entry size is global from the Port VC Capability
  235. * Register1 above. The number of phases is determined per VC.
  236. */
  237. for (i = 0; i < evcc + 1; i++) {
  238. u32 cap;
  239. int parb_offset;
  240. pci_read_config_dword(dev, pos + PCI_VC_RES_CAP +
  241. (i * PCI_CAP_VC_PER_VC_SIZEOF), &cap);
  242. parb_offset = FIELD_GET(PCI_VC_RES_CAP_ARB_OFF, cap) * 16;
  243. if (parb_offset) {
  244. int size, parb_phases = 0;
  245. if (cap & PCI_VC_RES_CAP_256_PHASE)
  246. parb_phases = 256;
  247. else if (cap & (PCI_VC_RES_CAP_128_PHASE |
  248. PCI_VC_RES_CAP_128_PHASE_TB))
  249. parb_phases = 128;
  250. else if (cap & PCI_VC_RES_CAP_64_PHASE)
  251. parb_phases = 64;
  252. else if (cap & PCI_VC_RES_CAP_32_PHASE)
  253. parb_phases = 32;
  254. size = (parb_size * parb_phases) / 8;
  255. if (size && buf) {
  256. pci_vc_save_restore_dwords(dev,
  257. pos + parb_offset,
  258. (u32 *)buf,
  259. size / 4, save);
  260. buf += size;
  261. }
  262. len += size;
  263. }
  264. /* VC Resource Control Register */
  265. if (buf) {
  266. int ctrl_pos = pos + PCI_VC_RES_CTRL +
  267. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  268. if (save)
  269. pci_read_config_dword(dev, ctrl_pos,
  270. (u32 *)buf);
  271. else {
  272. u32 tmp, ctrl = *(u32 *)buf;
  273. /*
  274. * For an FLR case, the VC config may remain.
  275. * Preserve enable bit, restore the rest.
  276. */
  277. pci_read_config_dword(dev, ctrl_pos, &tmp);
  278. tmp &= PCI_VC_RES_CTRL_ENABLE;
  279. tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE;
  280. pci_write_config_dword(dev, ctrl_pos, tmp);
  281. /* Load port arbitration table if used */
  282. if (ctrl & PCI_VC_RES_CTRL_ARB_SELECT)
  283. pci_vc_load_port_arb_table(dev, pos, i);
  284. /* Re-enable if needed */
  285. if ((ctrl ^ tmp) & PCI_VC_RES_CTRL_ENABLE)
  286. pci_vc_enable(dev, pos, i);
  287. }
  288. buf += 4;
  289. }
  290. len += 4;
  291. }
  292. return buf ? 0 : len;
  293. }
  294. static struct {
  295. u16 id;
  296. const char *name;
  297. } vc_caps[] = { { PCI_EXT_CAP_ID_MFVC, "MFVC" },
  298. { PCI_EXT_CAP_ID_VC, "VC" },
  299. { PCI_EXT_CAP_ID_VC9, "VC9" } };
  300. /**
  301. * pci_save_vc_state - Save VC state to pre-allocate save buffer
  302. * @dev: device
  303. *
  304. * For each type of VC capability, VC/VC9/MFVC, find the capability and
  305. * save it to the pre-allocated save buffer.
  306. */
  307. int pci_save_vc_state(struct pci_dev *dev)
  308. {
  309. int i;
  310. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  311. int pos, ret;
  312. struct pci_cap_saved_state *save_state;
  313. pos = pci_find_ext_capability(dev, vc_caps[i].id);
  314. if (!pos)
  315. continue;
  316. save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
  317. if (!save_state) {
  318. pci_err(dev, "%s buffer not found in %s\n",
  319. vc_caps[i].name, __func__);
  320. return -ENOMEM;
  321. }
  322. ret = pci_vc_do_save_buffer(dev, pos, save_state, true);
  323. if (ret) {
  324. pci_err(dev, "%s save unsuccessful %s\n",
  325. vc_caps[i].name, __func__);
  326. return ret;
  327. }
  328. }
  329. return 0;
  330. }
  331. /**
  332. * pci_restore_vc_state - Restore VC state from save buffer
  333. * @dev: device
  334. *
  335. * For each type of VC capability, VC/VC9/MFVC, find the capability and
  336. * restore it from the previously saved buffer.
  337. */
  338. void pci_restore_vc_state(struct pci_dev *dev)
  339. {
  340. int i;
  341. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  342. int pos;
  343. struct pci_cap_saved_state *save_state;
  344. pos = pci_find_ext_capability(dev, vc_caps[i].id);
  345. save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
  346. if (!save_state || !pos)
  347. continue;
  348. pci_vc_do_save_buffer(dev, pos, save_state, false);
  349. }
  350. }
  351. /**
  352. * pci_allocate_vc_save_buffers - Allocate save buffers for VC caps
  353. * @dev: device
  354. *
  355. * For each type of VC capability, VC/VC9/MFVC, find the capability, size
  356. * it, and allocate a buffer for save/restore.
  357. */
  358. void pci_allocate_vc_save_buffers(struct pci_dev *dev)
  359. {
  360. int i;
  361. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  362. int len, pos = pci_find_ext_capability(dev, vc_caps[i].id);
  363. if (!pos)
  364. continue;
  365. len = pci_vc_do_save_buffer(dev, pos, NULL, false);
  366. if (pci_add_ext_cap_save_buffer(dev, vc_caps[i].id, len))
  367. pci_err(dev, "unable to preallocate %s save buffer\n",
  368. vc_caps[i].name);
  369. }
  370. }