vc.c 12 KB

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