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 (!dev->has_secondary_link)
  99. return;
  100. ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  101. status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
  102. pci_read_config_dword(dev, ctrl_pos, &ctrl);
  103. id = ctrl & PCI_VC_RES_CTRL_ID;
  104. pci_read_config_dword(dev, pos, &header);
  105. /* If there is no opposite end of the link, skip to enable */
  106. if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_VC9 ||
  107. pci_is_root_bus(dev->bus))
  108. goto enable;
  109. pos2 = pci_find_ext_capability(dev->bus->self, PCI_EXT_CAP_ID_VC);
  110. if (!pos2)
  111. goto enable;
  112. pci_read_config_dword(dev->bus->self, pos2 + PCI_VC_PORT_CAP1, &cap1);
  113. evcc = cap1 & PCI_VC_CAP1_EVCC;
  114. /* VC0 is hardwired enabled, so we can start with 1 */
  115. for (i = 1; i < evcc + 1; i++) {
  116. ctrl_pos2 = pos2 + PCI_VC_RES_CTRL +
  117. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  118. status_pos2 = pos2 + PCI_VC_RES_STATUS +
  119. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  120. pci_read_config_dword(dev->bus->self, ctrl_pos2, &ctrl2);
  121. if ((ctrl2 & PCI_VC_RES_CTRL_ID) == id) {
  122. link = dev->bus->self;
  123. break;
  124. }
  125. }
  126. if (!link)
  127. goto enable;
  128. /* Disable if enabled */
  129. if (ctrl2 & PCI_VC_RES_CTRL_ENABLE) {
  130. ctrl2 &= ~PCI_VC_RES_CTRL_ENABLE;
  131. pci_write_config_dword(link, ctrl_pos2, ctrl2);
  132. }
  133. /* Enable on both ends */
  134. ctrl2 |= PCI_VC_RES_CTRL_ENABLE;
  135. pci_write_config_dword(link, ctrl_pos2, ctrl2);
  136. enable:
  137. ctrl |= PCI_VC_RES_CTRL_ENABLE;
  138. pci_write_config_dword(dev, ctrl_pos, ctrl);
  139. if (!pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_NEGO))
  140. dev_err(&dev->dev, "VC%d negotiation stuck pending\n", id);
  141. if (link && !pci_wait_for_pending(link, status_pos2,
  142. PCI_VC_RES_STATUS_NEGO))
  143. dev_err(&link->dev, "VC%d negotiation stuck pending\n", id);
  144. }
  145. /**
  146. * pci_vc_do_save_buffer - Size, save, or restore VC state
  147. * @dev: device
  148. * @pos: starting position of VC capability (VC/VC9/MFVC)
  149. * @save_state: buffer for save/restore
  150. * @name: for error message
  151. * @save: if provided a buffer, this indicates what to do with it
  152. *
  153. * Walking Virtual Channel config space to size, save, or restore it
  154. * is complicated, so we do it all from one function to reduce code and
  155. * guarantee ordering matches in the buffer. When called with NULL
  156. * @save_state, return the size of the necessary save buffer. When called
  157. * with a non-NULL @save_state, @save determines whether we save to the
  158. * buffer or restore from it.
  159. */
  160. static int pci_vc_do_save_buffer(struct pci_dev *dev, int pos,
  161. struct pci_cap_saved_state *save_state,
  162. bool save)
  163. {
  164. u32 cap1;
  165. char evcc, lpevcc, parb_size;
  166. int i, len = 0;
  167. u8 *buf = save_state ? (u8 *)save_state->cap.data : NULL;
  168. /* Sanity check buffer size for save/restore */
  169. if (buf && save_state->cap.size !=
  170. pci_vc_do_save_buffer(dev, pos, NULL, save)) {
  171. dev_err(&dev->dev,
  172. "VC save buffer size does not match @0x%x\n", pos);
  173. return -ENOMEM;
  174. }
  175. pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP1, &cap1);
  176. /* Extended VC Count (not counting VC0) */
  177. evcc = cap1 & PCI_VC_CAP1_EVCC;
  178. /* Low Priority Extended VC Count (not counting VC0) */
  179. lpevcc = (cap1 & PCI_VC_CAP1_LPEVCC) >> 4;
  180. /* Port Arbitration Table Entry Size (bits) */
  181. parb_size = 1 << ((cap1 & PCI_VC_CAP1_ARB_SIZE) >> 10);
  182. /*
  183. * Port VC Control Register contains VC Arbitration Select, which
  184. * cannot be modified when more than one LPVC is in operation. We
  185. * therefore save/restore it first, as only VC0 should be enabled
  186. * after device reset.
  187. */
  188. if (buf) {
  189. if (save)
  190. pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL,
  191. (u16 *)buf);
  192. else
  193. pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
  194. *(u16 *)buf);
  195. buf += 4;
  196. }
  197. len += 4;
  198. /*
  199. * If we have any Low Priority VCs and a VC Arbitration Table Offset
  200. * in Port VC Capability Register 2 then save/restore it next.
  201. */
  202. if (lpevcc) {
  203. u32 cap2;
  204. int vcarb_offset;
  205. pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP2, &cap2);
  206. vcarb_offset = ((cap2 & PCI_VC_CAP2_ARB_OFF) >> 24) * 16;
  207. if (vcarb_offset) {
  208. int size, vcarb_phases = 0;
  209. if (cap2 & PCI_VC_CAP2_128_PHASE)
  210. vcarb_phases = 128;
  211. else if (cap2 & PCI_VC_CAP2_64_PHASE)
  212. vcarb_phases = 64;
  213. else if (cap2 & PCI_VC_CAP2_32_PHASE)
  214. vcarb_phases = 32;
  215. /* Fixed 4 bits per phase per lpevcc (plus VC0) */
  216. size = ((lpevcc + 1) * vcarb_phases * 4) / 8;
  217. if (size && buf) {
  218. pci_vc_save_restore_dwords(dev,
  219. pos + vcarb_offset,
  220. (u32 *)buf,
  221. size / 4, save);
  222. /*
  223. * On restore, we need to signal hardware to
  224. * re-load the VC Arbitration Table.
  225. */
  226. if (!save)
  227. pci_vc_load_arb_table(dev, pos);
  228. buf += size;
  229. }
  230. len += size;
  231. }
  232. }
  233. /*
  234. * In addition to each VC Resource Control Register, we may have a
  235. * Port Arbitration Table attached to each VC. The Port Arbitration
  236. * Table Offset in each VC Resource Capability Register tells us if
  237. * it exists. The entry size is global from the Port VC Capability
  238. * Register1 above. The number of phases is determined per VC.
  239. */
  240. for (i = 0; i < evcc + 1; i++) {
  241. u32 cap;
  242. int parb_offset;
  243. pci_read_config_dword(dev, pos + PCI_VC_RES_CAP +
  244. (i * PCI_CAP_VC_PER_VC_SIZEOF), &cap);
  245. parb_offset = ((cap & PCI_VC_RES_CAP_ARB_OFF) >> 24) * 16;
  246. if (parb_offset) {
  247. int size, parb_phases = 0;
  248. if (cap & PCI_VC_RES_CAP_256_PHASE)
  249. parb_phases = 256;
  250. else if (cap & (PCI_VC_RES_CAP_128_PHASE |
  251. PCI_VC_RES_CAP_128_PHASE_TB))
  252. parb_phases = 128;
  253. else if (cap & PCI_VC_RES_CAP_64_PHASE)
  254. parb_phases = 64;
  255. else if (cap & PCI_VC_RES_CAP_32_PHASE)
  256. parb_phases = 32;
  257. size = (parb_size * parb_phases) / 8;
  258. if (size && buf) {
  259. pci_vc_save_restore_dwords(dev,
  260. pos + parb_offset,
  261. (u32 *)buf,
  262. size / 4, save);
  263. buf += size;
  264. }
  265. len += size;
  266. }
  267. /* VC Resource Control Register */
  268. if (buf) {
  269. int ctrl_pos = pos + PCI_VC_RES_CTRL +
  270. (i * PCI_CAP_VC_PER_VC_SIZEOF);
  271. if (save)
  272. pci_read_config_dword(dev, ctrl_pos,
  273. (u32 *)buf);
  274. else {
  275. u32 tmp, ctrl = *(u32 *)buf;
  276. /*
  277. * For an FLR case, the VC config may remain.
  278. * Preserve enable bit, restore the rest.
  279. */
  280. pci_read_config_dword(dev, ctrl_pos, &tmp);
  281. tmp &= PCI_VC_RES_CTRL_ENABLE;
  282. tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE;
  283. pci_write_config_dword(dev, ctrl_pos, tmp);
  284. /* Load port arbitration table if used */
  285. if (ctrl & PCI_VC_RES_CTRL_ARB_SELECT)
  286. pci_vc_load_port_arb_table(dev, pos, i);
  287. /* Re-enable if needed */
  288. if ((ctrl ^ tmp) & PCI_VC_RES_CTRL_ENABLE)
  289. pci_vc_enable(dev, pos, i);
  290. }
  291. buf += 4;
  292. }
  293. len += 4;
  294. }
  295. return buf ? 0 : len;
  296. }
  297. static struct {
  298. u16 id;
  299. const char *name;
  300. } vc_caps[] = { { PCI_EXT_CAP_ID_MFVC, "MFVC" },
  301. { PCI_EXT_CAP_ID_VC, "VC" },
  302. { PCI_EXT_CAP_ID_VC9, "VC9" } };
  303. /**
  304. * pci_save_vc_state - Save VC state to pre-allocate save buffer
  305. * @dev: device
  306. *
  307. * For each type of VC capability, VC/VC9/MFVC, find the capability and
  308. * save it to the pre-allocated save buffer.
  309. */
  310. int pci_save_vc_state(struct pci_dev *dev)
  311. {
  312. int i;
  313. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  314. int pos, ret;
  315. struct pci_cap_saved_state *save_state;
  316. pos = pci_find_ext_capability(dev, vc_caps[i].id);
  317. if (!pos)
  318. continue;
  319. save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
  320. if (!save_state) {
  321. dev_err(&dev->dev, "%s buffer not found in %s\n",
  322. vc_caps[i].name, __func__);
  323. return -ENOMEM;
  324. }
  325. ret = pci_vc_do_save_buffer(dev, pos, save_state, true);
  326. if (ret) {
  327. dev_err(&dev->dev, "%s save unsuccessful %s\n",
  328. vc_caps[i].name, __func__);
  329. return ret;
  330. }
  331. }
  332. return 0;
  333. }
  334. /**
  335. * pci_restore_vc_state - Restore VC state from save buffer
  336. * @dev: device
  337. *
  338. * For each type of VC capability, VC/VC9/MFVC, find the capability and
  339. * restore it from the previously saved buffer.
  340. */
  341. void pci_restore_vc_state(struct pci_dev *dev)
  342. {
  343. int i;
  344. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  345. int pos;
  346. struct pci_cap_saved_state *save_state;
  347. pos = pci_find_ext_capability(dev, vc_caps[i].id);
  348. save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
  349. if (!save_state || !pos)
  350. continue;
  351. pci_vc_do_save_buffer(dev, pos, save_state, false);
  352. }
  353. }
  354. /**
  355. * pci_allocate_vc_save_buffers - Allocate save buffers for VC caps
  356. * @dev: device
  357. *
  358. * For each type of VC capability, VC/VC9/MFVC, find the capability, size
  359. * it, and allocate a buffer for save/restore.
  360. */
  361. void pci_allocate_vc_save_buffers(struct pci_dev *dev)
  362. {
  363. int i;
  364. for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
  365. int len, pos = pci_find_ext_capability(dev, vc_caps[i].id);
  366. if (!pos)
  367. continue;
  368. len = pci_vc_do_save_buffer(dev, pos, NULL, false);
  369. if (pci_add_ext_cap_save_buffer(dev, vc_caps[i].id, len))
  370. dev_err(&dev->dev,
  371. "unable to preallocate %s save buffer\n",
  372. vc_caps[i].name);
  373. }
  374. }