aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjorn Helgaas <bhelgaas@google.com>2013-12-18 16:04:35 -0500
committerBjorn Helgaas <bhelgaas@google.com>2013-12-18 16:04:35 -0500
commit608235a30706814dfff5b23938dc5f14fdb7e7e3 (patch)
treea190e6110cb0bc22bac8b5bb5e66a4258b5a2150
parenta737f76baee3d44cb25285986812e9cd97fd6002 (diff)
parent274127a1fdbad3c0d64e813521f4a0ef96cfc70e (diff)
Merge branch 'pci/vc' into next
* pci/vc: PCI: Rename PCI_VC_PORT_REG1/2 to PCI_VC_PORT_CAP1/2 PCI: Add Virtual Channel to save/restore support PCI: Add support for save/restore of extended capabilities PCI: Add pci_wait_for_pending() (refactor pci_wait_for_pending_transaction())
-rw-r--r--drivers/pci/Makefile2
-rw-r--r--drivers/pci/pci.c102
-rw-r--r--drivers/pci/vc.c434
-rw-r--r--drivers/vfio/pci/vfio_pci_config.c12
-rw-r--r--include/linux/pci.h15
-rw-r--r--include/uapi/linux/pci_regs.h29
6 files changed, 549 insertions, 45 deletions
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 6ebf5bf8e7a7..17d2b07ee67c 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -4,7 +4,7 @@
4 4
5obj-y += access.o bus.o probe.o host-bridge.o remove.o pci.o \ 5obj-y += access.o bus.o probe.o host-bridge.o remove.o pci.o \
6 pci-driver.o search.o pci-sysfs.o rom.o setup-res.o \ 6 pci-driver.o search.o pci-sysfs.o rom.o setup-res.o \
7 irq.o vpd.o setup-bus.o 7 irq.o vpd.o setup-bus.o vc.o
8obj-$(CONFIG_PROC_FS) += proc.o 8obj-$(CONFIG_PROC_FS) += proc.o
9obj-$(CONFIG_SYSFS) += slot.o 9obj-$(CONFIG_SYSFS) += slot.o
10 10
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 33120d156668..508e560b7d2a 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -431,6 +431,32 @@ pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
431} 431}
432 432
433/** 433/**
434 * pci_wait_for_pending - wait for @mask bit(s) to clear in status word @pos
435 * @dev: the PCI device to operate on
436 * @pos: config space offset of status word
437 * @mask: mask of bit(s) to care about in status word
438 *
439 * Return 1 when mask bit(s) in status word clear, 0 otherwise.
440 */
441int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask)
442{
443 int i;
444
445 /* Wait for Transaction Pending bit clean */
446 for (i = 0; i < 4; i++) {
447 u16 status;
448 if (i)
449 msleep((1 << (i - 1)) * 100);
450
451 pci_read_config_word(dev, pos, &status);
452 if (!(status & mask))
453 return 1;
454 }
455
456 return 0;
457}
458
459/**
434 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up) 460 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
435 * @dev: PCI device to have its BARs restored 461 * @dev: PCI device to have its BARs restored
436 * 462 *
@@ -835,18 +861,28 @@ EXPORT_SYMBOL(pci_choose_state);
835#define PCI_EXP_SAVE_REGS 7 861#define PCI_EXP_SAVE_REGS 7
836 862
837 863
838static struct pci_cap_saved_state *pci_find_saved_cap( 864static struct pci_cap_saved_state *_pci_find_saved_cap(struct pci_dev *pci_dev,
839 struct pci_dev *pci_dev, char cap) 865 u16 cap, bool extended)
840{ 866{
841 struct pci_cap_saved_state *tmp; 867 struct pci_cap_saved_state *tmp;
842 868
843 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) { 869 hlist_for_each_entry(tmp, &pci_dev->saved_cap_space, next) {
844 if (tmp->cap.cap_nr == cap) 870 if (tmp->cap.cap_extended == extended && tmp->cap.cap_nr == cap)
845 return tmp; 871 return tmp;
846 } 872 }
847 return NULL; 873 return NULL;
848} 874}
849 875
876struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap)
877{
878 return _pci_find_saved_cap(dev, cap, false);
879}
880
881struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev, u16 cap)
882{
883 return _pci_find_saved_cap(dev, cap, true);
884}
885
850static int pci_save_pcie_state(struct pci_dev *dev) 886static int pci_save_pcie_state(struct pci_dev *dev)
851{ 887{
852 int i = 0; 888 int i = 0;
@@ -948,6 +984,8 @@ pci_save_state(struct pci_dev *dev)
948 return i; 984 return i;
949 if ((i = pci_save_pcix_state(dev)) != 0) 985 if ((i = pci_save_pcix_state(dev)) != 0)
950 return i; 986 return i;
987 if ((i = pci_save_vc_state(dev)) != 0)
988 return i;
951 return 0; 989 return 0;
952} 990}
953 991
@@ -1010,6 +1048,7 @@ void pci_restore_state(struct pci_dev *dev)
1010 /* PCI Express register must be restored first */ 1048 /* PCI Express register must be restored first */
1011 pci_restore_pcie_state(dev); 1049 pci_restore_pcie_state(dev);
1012 pci_restore_ats_state(dev); 1050 pci_restore_ats_state(dev);
1051 pci_restore_vc_state(dev);
1013 1052
1014 pci_restore_config_space(dev); 1053 pci_restore_config_space(dev);
1015 1054
@@ -1087,7 +1126,7 @@ int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state)
1087 while (cap->size) { 1126 while (cap->size) {
1088 struct pci_cap_saved_state *tmp; 1127 struct pci_cap_saved_state *tmp;
1089 1128
1090 tmp = pci_find_saved_cap(dev, cap->cap_nr); 1129 tmp = _pci_find_saved_cap(dev, cap->cap_nr, cap->cap_extended);
1091 if (!tmp || tmp->cap.size != cap->size) 1130 if (!tmp || tmp->cap.size != cap->size)
1092 return -EINVAL; 1131 return -EINVAL;
1093 1132
@@ -2021,18 +2060,24 @@ static void pci_add_saved_cap(struct pci_dev *pci_dev,
2021} 2060}
2022 2061
2023/** 2062/**
2024 * pci_add_cap_save_buffer - allocate buffer for saving given capability registers 2063 * _pci_add_cap_save_buffer - allocate buffer for saving given
2064 * capability registers
2025 * @dev: the PCI device 2065 * @dev: the PCI device
2026 * @cap: the capability to allocate the buffer for 2066 * @cap: the capability to allocate the buffer for
2067 * @extended: Standard or Extended capability ID
2027 * @size: requested size of the buffer 2068 * @size: requested size of the buffer
2028 */ 2069 */
2029static int pci_add_cap_save_buffer( 2070static int _pci_add_cap_save_buffer(struct pci_dev *dev, u16 cap,
2030 struct pci_dev *dev, char cap, unsigned int size) 2071 bool extended, unsigned int size)
2031{ 2072{
2032 int pos; 2073 int pos;
2033 struct pci_cap_saved_state *save_state; 2074 struct pci_cap_saved_state *save_state;
2034 2075
2035 pos = pci_find_capability(dev, cap); 2076 if (extended)
2077 pos = pci_find_ext_capability(dev, cap);
2078 else
2079 pos = pci_find_capability(dev, cap);
2080
2036 if (pos <= 0) 2081 if (pos <= 0)
2037 return 0; 2082 return 0;
2038 2083
@@ -2041,12 +2086,23 @@ static int pci_add_cap_save_buffer(
2041 return -ENOMEM; 2086 return -ENOMEM;
2042 2087
2043 save_state->cap.cap_nr = cap; 2088 save_state->cap.cap_nr = cap;
2089 save_state->cap.cap_extended = extended;
2044 save_state->cap.size = size; 2090 save_state->cap.size = size;
2045 pci_add_saved_cap(dev, save_state); 2091 pci_add_saved_cap(dev, save_state);
2046 2092
2047 return 0; 2093 return 0;
2048} 2094}
2049 2095
2096int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size)
2097{
2098 return _pci_add_cap_save_buffer(dev, cap, false, size);
2099}
2100
2101int pci_add_ext_cap_save_buffer(struct pci_dev *dev, u16 cap, unsigned int size)
2102{
2103 return _pci_add_cap_save_buffer(dev, cap, true, size);
2104}
2105
2050/** 2106/**
2051 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities 2107 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
2052 * @dev: the PCI device 2108 * @dev: the PCI device
@@ -2065,6 +2121,8 @@ void pci_allocate_cap_save_buffers(struct pci_dev *dev)
2065 if (error) 2121 if (error)
2066 dev_err(&dev->dev, 2122 dev_err(&dev->dev,
2067 "unable to preallocate PCI-X save buffer\n"); 2123 "unable to preallocate PCI-X save buffer\n");
2124
2125 pci_allocate_vc_save_buffers(dev);
2068} 2126}
2069 2127
2070void pci_free_cap_save_buffers(struct pci_dev *dev) 2128void pci_free_cap_save_buffers(struct pci_dev *dev)
@@ -3204,20 +3262,10 @@ EXPORT_SYMBOL(pci_set_dma_seg_boundary);
3204 */ 3262 */
3205int pci_wait_for_pending_transaction(struct pci_dev *dev) 3263int pci_wait_for_pending_transaction(struct pci_dev *dev)
3206{ 3264{
3207 int i; 3265 if (!pci_is_pcie(dev))
3208 u16 status; 3266 return 1;
3209
3210 /* Wait for Transaction Pending bit clean */
3211 for (i = 0; i < 4; i++) {
3212 if (i)
3213 msleep((1 << (i - 1)) * 100);
3214
3215 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
3216 if (!(status & PCI_EXP_DEVSTA_TRPND))
3217 return 1;
3218 }
3219 3267
3220 return 0; 3268 return pci_wait_for_pending(dev, PCI_EXP_DEVSTA, PCI_EXP_DEVSTA_TRPND);
3221} 3269}
3222EXPORT_SYMBOL(pci_wait_for_pending_transaction); 3270EXPORT_SYMBOL(pci_wait_for_pending_transaction);
3223 3271
@@ -3244,10 +3292,8 @@ static int pcie_flr(struct pci_dev *dev, int probe)
3244 3292
3245static int pci_af_flr(struct pci_dev *dev, int probe) 3293static int pci_af_flr(struct pci_dev *dev, int probe)
3246{ 3294{
3247 int i;
3248 int pos; 3295 int pos;
3249 u8 cap; 3296 u8 cap;
3250 u8 status;
3251 3297
3252 pos = pci_find_capability(dev, PCI_CAP_ID_AF); 3298 pos = pci_find_capability(dev, PCI_CAP_ID_AF);
3253 if (!pos) 3299 if (!pos)
@@ -3261,14 +3307,8 @@ static int pci_af_flr(struct pci_dev *dev, int probe)
3261 return 0; 3307 return 0;
3262 3308
3263 /* Wait for Transaction Pending bit clean */ 3309 /* Wait for Transaction Pending bit clean */
3264 for (i = 0; i < 4; i++) { 3310 if (pci_wait_for_pending(dev, PCI_AF_STATUS, PCI_AF_STATUS_TP))
3265 if (i) 3311 goto clear;
3266 msleep((1 << (i - 1)) * 100);
3267
3268 pci_read_config_byte(dev, pos + PCI_AF_STATUS, &status);
3269 if (!(status & PCI_AF_STATUS_TP))
3270 goto clear;
3271 }
3272 3312
3273 dev_err(&dev->dev, "transaction is not cleared; " 3313 dev_err(&dev->dev, "transaction is not cleared; "
3274 "proceeding with reset anyway\n"); 3314 "proceeding with reset anyway\n");
diff --git a/drivers/pci/vc.c b/drivers/pci/vc.c
new file mode 100644
index 000000000000..7e1304d2e389
--- /dev/null
+++ b/drivers/pci/vc.c
@@ -0,0 +1,434 @@
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
12#include <linux/device.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/pci.h>
16#include <linux/pci_regs.h>
17#include <linux/types.h>
18
19/**
20 * pci_vc_save_restore_dwords - Save or restore a series of dwords
21 * @dev: device
22 * @pos: starting config space position
23 * @buf: buffer to save to or restore from
24 * @dwords: number of dwords to save/restore
25 * @save: whether to save or restore
26 */
27static void pci_vc_save_restore_dwords(struct pci_dev *dev, int pos,
28 u32 *buf, int dwords, bool save)
29{
30 int i;
31
32 for (i = 0; i < dwords; i++, buf++) {
33 if (save)
34 pci_read_config_dword(dev, pos + (i * 4), buf);
35 else
36 pci_write_config_dword(dev, pos + (i * 4), *buf);
37 }
38}
39
40/**
41 * pci_vc_load_arb_table - load and wait for VC arbitration table
42 * @dev: device
43 * @pos: starting position of VC capability (VC/VC9/MFVC)
44 *
45 * Set Load VC Arbitration Table bit requesting hardware to apply the VC
46 * Arbitration Table (previously loaded). When the VC Arbitration Table
47 * Status clears, hardware has latched the table into VC arbitration logic.
48 */
49static void pci_vc_load_arb_table(struct pci_dev *dev, int pos)
50{
51 u16 ctrl;
52
53 pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL, &ctrl);
54 pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
55 ctrl | PCI_VC_PORT_CTRL_LOAD_TABLE);
56 if (pci_wait_for_pending(dev, pos + PCI_VC_PORT_STATUS,
57 PCI_VC_PORT_STATUS_TABLE))
58 return;
59
60 dev_err(&dev->dev, "VC arbitration table failed to load\n");
61}
62
63/**
64 * pci_vc_load_port_arb_table - Load and wait for VC port arbitration table
65 * @dev: device
66 * @pos: starting position of VC capability (VC/VC9/MFVC)
67 * @res: VC resource number, ie. VCn (0-7)
68 *
69 * Set Load Port Arbitration Table bit requesting hardware to apply the Port
70 * Arbitration Table (previously loaded). When the Port Arbitration Table
71 * Status clears, hardware has latched the table into port arbitration logic.
72 */
73static void pci_vc_load_port_arb_table(struct pci_dev *dev, int pos, int res)
74{
75 int ctrl_pos, status_pos;
76 u32 ctrl;
77
78 ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
79 status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
80
81 pci_read_config_dword(dev, ctrl_pos, &ctrl);
82 pci_write_config_dword(dev, ctrl_pos,
83 ctrl | PCI_VC_RES_CTRL_LOAD_TABLE);
84
85 if (pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_TABLE))
86 return;
87
88 dev_err(&dev->dev, "VC%d port arbitration table failed to load\n", res);
89}
90
91/**
92 * pci_vc_enable - Enable virtual channel
93 * @dev: device
94 * @pos: starting position of VC capability (VC/VC9/MFVC)
95 * @res: VC res number, ie. VCn (0-7)
96 *
97 * A VC is enabled by setting the enable bit in matching resource control
98 * registers on both sides of a link. We therefore need to find the opposite
99 * end of the link. To keep this simple we enable from the downstream device.
100 * RC devices do not have an upstream device, nor does it seem that VC9 do
101 * (spec is unclear). Once we find the upstream device, match the VC ID to
102 * get the correct resource, disable and enable on both ends.
103 */
104static void pci_vc_enable(struct pci_dev *dev, int pos, int res)
105{
106 int ctrl_pos, status_pos, id, pos2, evcc, i, ctrl_pos2, status_pos2;
107 u32 ctrl, header, cap1, ctrl2;
108 struct pci_dev *link = NULL;
109
110 /* Enable VCs from the downstream device */
111 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
112 pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)
113 return;
114
115 ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
116 status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
117
118 pci_read_config_dword(dev, ctrl_pos, &ctrl);
119 id = ctrl & PCI_VC_RES_CTRL_ID;
120
121 pci_read_config_dword(dev, pos, &header);
122
123 /* If there is no opposite end of the link, skip to enable */
124 if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_VC9 ||
125 pci_is_root_bus(dev->bus))
126 goto enable;
127
128 pos2 = pci_find_ext_capability(dev->bus->self, PCI_EXT_CAP_ID_VC);
129 if (!pos2)
130 goto enable;
131
132 pci_read_config_dword(dev->bus->self, pos2 + PCI_VC_PORT_CAP1, &cap1);
133 evcc = cap1 & PCI_VC_CAP1_EVCC;
134
135 /* VC0 is hardwired enabled, so we can start with 1 */
136 for (i = 1; i < evcc + 1; i++) {
137 ctrl_pos2 = pos2 + PCI_VC_RES_CTRL +
138 (i * PCI_CAP_VC_PER_VC_SIZEOF);
139 status_pos2 = pos2 + PCI_VC_RES_STATUS +
140 (i * PCI_CAP_VC_PER_VC_SIZEOF);
141 pci_read_config_dword(dev->bus->self, ctrl_pos2, &ctrl2);
142 if ((ctrl2 & PCI_VC_RES_CTRL_ID) == id) {
143 link = dev->bus->self;
144 break;
145 }
146 }
147
148 if (!link)
149 goto enable;
150
151 /* Disable if enabled */
152 if (ctrl2 & PCI_VC_RES_CTRL_ENABLE) {
153 ctrl2 &= ~PCI_VC_RES_CTRL_ENABLE;
154 pci_write_config_dword(link, ctrl_pos2, ctrl2);
155 }
156
157 /* Enable on both ends */
158 ctrl2 |= PCI_VC_RES_CTRL_ENABLE;
159 pci_write_config_dword(link, ctrl_pos2, ctrl2);
160enable:
161 ctrl |= PCI_VC_RES_CTRL_ENABLE;
162 pci_write_config_dword(dev, ctrl_pos, ctrl);
163
164 if (!pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_NEGO))
165 dev_err(&dev->dev, "VC%d negotiation stuck pending\n", id);
166
167 if (link && !pci_wait_for_pending(link, status_pos2,
168 PCI_VC_RES_STATUS_NEGO))
169 dev_err(&link->dev, "VC%d negotiation stuck pending\n", id);
170}
171
172/**
173 * pci_vc_do_save_buffer - Size, save, or restore VC state
174 * @dev: device
175 * @pos: starting position of VC capability (VC/VC9/MFVC)
176 * @save_state: buffer for save/restore
177 * @name: for error message
178 * @save: if provided a buffer, this indicates what to do with it
179 *
180 * Walking Virtual Channel config space to size, save, or restore it
181 * is complicated, so we do it all from one function to reduce code and
182 * guarantee ordering matches in the buffer. When called with NULL
183 * @save_state, return the size of the necessary save buffer. When called
184 * with a non-NULL @save_state, @save determines whether we save to the
185 * buffer or restore from it.
186 */
187static int pci_vc_do_save_buffer(struct pci_dev *dev, int pos,
188 struct pci_cap_saved_state *save_state,
189 bool save)
190{
191 u32 cap1;
192 char evcc, lpevcc, parb_size;
193 int i, len = 0;
194 u8 *buf = save_state ? (u8 *)save_state->cap.data : NULL;
195
196 /* Sanity check buffer size for save/restore */
197 if (buf && save_state->cap.size !=
198 pci_vc_do_save_buffer(dev, pos, NULL, save)) {
199 dev_err(&dev->dev,
200 "VC save buffer size does not match @0x%x\n", pos);
201 return -ENOMEM;
202 }
203
204 pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP1, &cap1);
205 /* Extended VC Count (not counting VC0) */
206 evcc = cap1 & PCI_VC_CAP1_EVCC;
207 /* Low Priority Extended VC Count (not counting VC0) */
208 lpevcc = (cap1 & PCI_VC_CAP1_LPEVCC) >> 4;
209 /* Port Arbitration Table Entry Size (bits) */
210 parb_size = 1 << ((cap1 & PCI_VC_CAP1_ARB_SIZE) >> 10);
211
212 /*
213 * Port VC Control Register contains VC Arbitration Select, which
214 * cannot be modified when more than one LPVC is in operation. We
215 * therefore save/restore it first, as only VC0 should be enabled
216 * after device reset.
217 */
218 if (buf) {
219 if (save)
220 pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL,
221 (u16 *)buf);
222 else
223 pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
224 *(u16 *)buf);
225 buf += 2;
226 }
227 len += 2;
228
229 /*
230 * If we have any Low Priority VCs and a VC Arbitration Table Offset
231 * in Port VC Capability Register 2 then save/restore it next.
232 */
233 if (lpevcc) {
234 u32 cap2;
235 int vcarb_offset;
236
237 pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP2, &cap2);
238 vcarb_offset = ((cap2 & PCI_VC_CAP2_ARB_OFF) >> 24) * 16;
239
240 if (vcarb_offset) {
241 int size, vcarb_phases = 0;
242
243 if (cap2 & PCI_VC_CAP2_128_PHASE)
244 vcarb_phases = 128;
245 else if (cap2 & PCI_VC_CAP2_64_PHASE)
246 vcarb_phases = 64;
247 else if (cap2 & PCI_VC_CAP2_32_PHASE)
248 vcarb_phases = 32;
249
250 /* Fixed 4 bits per phase per lpevcc (plus VC0) */
251 size = ((lpevcc + 1) * vcarb_phases * 4) / 8;
252
253 if (size && buf) {
254 pci_vc_save_restore_dwords(dev,
255 pos + vcarb_offset,
256 (u32 *)buf,
257 size / 4, save);
258 /*
259 * On restore, we need to signal hardware to
260 * re-load the VC Arbitration Table.
261 */
262 if (!save)
263 pci_vc_load_arb_table(dev, pos);
264
265 buf += size;
266 }
267 len += size;
268 }
269 }
270
271 /*
272 * In addition to each VC Resource Control Register, we may have a
273 * Port Arbitration Table attached to each VC. The Port Arbitration
274 * Table Offset in each VC Resource Capability Register tells us if
275 * it exists. The entry size is global from the Port VC Capability
276 * Register1 above. The number of phases is determined per VC.
277 */
278 for (i = 0; i < evcc + 1; i++) {
279 u32 cap;
280 int parb_offset;
281
282 pci_read_config_dword(dev, pos + PCI_VC_RES_CAP +
283 (i * PCI_CAP_VC_PER_VC_SIZEOF), &cap);
284 parb_offset = ((cap & PCI_VC_RES_CAP_ARB_OFF) >> 24) * 16;
285 if (parb_offset) {
286 int size, parb_phases = 0;
287
288 if (cap & PCI_VC_RES_CAP_256_PHASE)
289 parb_phases = 256;
290 else if (cap & (PCI_VC_RES_CAP_128_PHASE |
291 PCI_VC_RES_CAP_128_PHASE_TB))
292 parb_phases = 128;
293 else if (cap & PCI_VC_RES_CAP_64_PHASE)
294 parb_phases = 64;
295 else if (cap & PCI_VC_RES_CAP_32_PHASE)
296 parb_phases = 32;
297
298 size = (parb_size * parb_phases) / 8;
299
300 if (size && buf) {
301 pci_vc_save_restore_dwords(dev,
302 pos + parb_offset,
303 (u32 *)buf,
304 size / 4, save);
305 buf += size;
306 }
307 len += size;
308 }
309
310 /* VC Resource Control Register */
311 if (buf) {
312 int ctrl_pos = pos + PCI_VC_RES_CTRL +
313 (i * PCI_CAP_VC_PER_VC_SIZEOF);
314 if (save)
315 pci_read_config_dword(dev, ctrl_pos,
316 (u32 *)buf);
317 else {
318 u32 tmp, ctrl = *(u32 *)buf;
319 /*
320 * For an FLR case, the VC config may remain.
321 * Preserve enable bit, restore the rest.
322 */
323 pci_read_config_dword(dev, ctrl_pos, &tmp);
324 tmp &= PCI_VC_RES_CTRL_ENABLE;
325 tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE;
326 pci_write_config_dword(dev, ctrl_pos, tmp);
327 /* Load port arbitration table if used */
328 if (ctrl & PCI_VC_RES_CTRL_ARB_SELECT)
329 pci_vc_load_port_arb_table(dev, pos, i);
330 /* Re-enable if needed */
331 if ((ctrl ^ tmp) & PCI_VC_RES_CTRL_ENABLE)
332 pci_vc_enable(dev, pos, i);
333 }
334 buf += 4;
335 }
336 len += 4;
337 }
338
339 return buf ? 0 : len;
340}
341
342static struct {
343 u16 id;
344 const char *name;
345} vc_caps[] = { { PCI_EXT_CAP_ID_MFVC, "MFVC" },
346 { PCI_EXT_CAP_ID_VC, "VC" },
347 { PCI_EXT_CAP_ID_VC9, "VC9" } };
348
349/**
350 * pci_save_vc_state - Save VC state to pre-allocate save buffer
351 * @dev: device
352 *
353 * For each type of VC capability, VC/VC9/MFVC, find the capability and
354 * save it to the pre-allocated save buffer.
355 */
356int pci_save_vc_state(struct pci_dev *dev)
357{
358 int i;
359
360 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
361 int pos, ret;
362 struct pci_cap_saved_state *save_state;
363
364 pos = pci_find_ext_capability(dev, vc_caps[i].id);
365 if (!pos)
366 continue;
367
368 save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
369 if (!save_state) {
370 dev_err(&dev->dev, "%s buffer not found in %s\n",
371 vc_caps[i].name, __func__);
372 return -ENOMEM;
373 }
374
375 ret = pci_vc_do_save_buffer(dev, pos, save_state, true);
376 if (ret) {
377 dev_err(&dev->dev, "%s save unsuccessful %s\n",
378 vc_caps[i].name, __func__);
379 return ret;
380 }
381 }
382
383 return 0;
384}
385
386/**
387 * pci_restore_vc_state - Restore VC state from save buffer
388 * @dev: device
389 *
390 * For each type of VC capability, VC/VC9/MFVC, find the capability and
391 * restore it from the previously saved buffer.
392 */
393void pci_restore_vc_state(struct pci_dev *dev)
394{
395 int i;
396
397 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
398 int pos;
399 struct pci_cap_saved_state *save_state;
400
401 pos = pci_find_ext_capability(dev, vc_caps[i].id);
402 save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
403 if (!save_state || !pos)
404 continue;
405
406 pci_vc_do_save_buffer(dev, pos, save_state, false);
407 }
408}
409
410/**
411 * pci_allocate_vc_save_buffers - Allocate save buffers for VC caps
412 * @dev: device
413 *
414 * For each type of VC capability, VC/VC9/MFVC, find the capability, size
415 * it, and allocate a buffer for save/restore.
416 */
417
418void pci_allocate_vc_save_buffers(struct pci_dev *dev)
419{
420 int i;
421
422 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
423 int len, pos = pci_find_ext_capability(dev, vc_caps[i].id);
424
425 if (!pos)
426 continue;
427
428 len = pci_vc_do_save_buffer(dev, pos, NULL, false);
429 if (pci_add_ext_cap_save_buffer(dev, vc_caps[i].id, len))
430 dev_err(&dev->dev,
431 "unable to preallocate %s save buffer\n",
432 vc_caps[i].name);
433 }
434}
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index ffd0632c3cbc..83cd1574c810 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -975,20 +975,20 @@ static int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
975 int ret, evcc, phases, vc_arb; 975 int ret, evcc, phases, vc_arb;
976 int len = PCI_CAP_VC_BASE_SIZEOF; 976 int len = PCI_CAP_VC_BASE_SIZEOF;
977 977
978 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_REG1, &tmp); 978 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP1, &tmp);
979 if (ret) 979 if (ret)
980 return pcibios_err_to_errno(ret); 980 return pcibios_err_to_errno(ret);
981 981
982 evcc = tmp & PCI_VC_REG1_EVCC; /* extended vc count */ 982 evcc = tmp & PCI_VC_CAP1_EVCC; /* extended vc count */
983 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_REG2, &tmp); 983 ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP2, &tmp);
984 if (ret) 984 if (ret)
985 return pcibios_err_to_errno(ret); 985 return pcibios_err_to_errno(ret);
986 986
987 if (tmp & PCI_VC_REG2_128_PHASE) 987 if (tmp & PCI_VC_CAP2_128_PHASE)
988 phases = 128; 988 phases = 128;
989 else if (tmp & PCI_VC_REG2_64_PHASE) 989 else if (tmp & PCI_VC_CAP2_64_PHASE)
990 phases = 64; 990 phases = 64;
991 else if (tmp & PCI_VC_REG2_32_PHASE) 991 else if (tmp & PCI_VC_CAP2_32_PHASE)
992 phases = 32; 992 phases = 32;
993 else 993 else
994 phases = 0; 994 phases = 0;
diff --git a/include/linux/pci.h b/include/linux/pci.h
index eb8078aeadc8..f16fb1f01317 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -224,7 +224,8 @@ enum pci_bus_speed {
224}; 224};
225 225
226struct pci_cap_saved_data { 226struct pci_cap_saved_data {
227 char cap_nr; 227 u16 cap_nr;
228 bool cap_extended;
228 unsigned int size; 229 unsigned int size;
229 u32 data[0]; 230 u32 data[0];
230}; 231};
@@ -938,6 +939,7 @@ bool pci_check_and_unmask_intx(struct pci_dev *dev);
938void pci_msi_off(struct pci_dev *dev); 939void pci_msi_off(struct pci_dev *dev);
939int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size); 940int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size);
940int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask); 941int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask);
942int pci_wait_for_pending(struct pci_dev *dev, int pos, u16 mask);
941int pci_wait_for_pending_transaction(struct pci_dev *dev); 943int pci_wait_for_pending_transaction(struct pci_dev *dev);
942int pcix_get_max_mmrbc(struct pci_dev *dev); 944int pcix_get_max_mmrbc(struct pci_dev *dev);
943int pcix_get_mmrbc(struct pci_dev *dev); 945int pcix_get_mmrbc(struct pci_dev *dev);
@@ -976,6 +978,12 @@ struct pci_saved_state *pci_store_saved_state(struct pci_dev *dev);
976int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state); 978int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state);
977int pci_load_and_free_saved_state(struct pci_dev *dev, 979int pci_load_and_free_saved_state(struct pci_dev *dev,
978 struct pci_saved_state **state); 980 struct pci_saved_state **state);
981struct pci_cap_saved_state *pci_find_saved_cap(struct pci_dev *dev, char cap);
982struct pci_cap_saved_state *pci_find_saved_ext_cap(struct pci_dev *dev,
983 u16 cap);
984int pci_add_cap_save_buffer(struct pci_dev *dev, char cap, unsigned int size);
985int pci_add_ext_cap_save_buffer(struct pci_dev *dev,
986 u16 cap, unsigned int size);
979int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state); 987int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state);
980int pci_set_power_state(struct pci_dev *dev, pci_power_t state); 988int pci_set_power_state(struct pci_dev *dev, pci_power_t state);
981pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state); 989pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state);
@@ -997,6 +1005,11 @@ static inline int pci_enable_wake(struct pci_dev *dev, pci_power_t state,
997 return __pci_enable_wake(dev, state, false, enable); 1005 return __pci_enable_wake(dev, state, false, enable);
998} 1006}
999 1007
1008/* PCI Virtual Channel */
1009int pci_save_vc_state(struct pci_dev *dev);
1010void pci_restore_vc_state(struct pci_dev *dev);
1011void pci_allocate_vc_save_buffers(struct pci_dev *dev);
1012
1000#define PCI_EXP_IDO_REQUEST (1<<0) 1013#define PCI_EXP_IDO_REQUEST (1<<0)
1001#define PCI_EXP_IDO_COMPLETION (1<<1) 1014#define PCI_EXP_IDO_COMPLETION (1<<1)
1002void pci_enable_ido(struct pci_dev *dev, unsigned long type); 1015void pci_enable_ido(struct pci_dev *dev, unsigned long type);
diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
index 6d03ba42ab23..ab6b4e7f6657 100644
--- a/include/uapi/linux/pci_regs.h
+++ b/include/uapi/linux/pci_regs.h
@@ -685,17 +685,34 @@
685#define PCI_ERR_ROOT_ERR_SRC 52 /* Error Source Identification */ 685#define PCI_ERR_ROOT_ERR_SRC 52 /* Error Source Identification */
686 686
687/* Virtual Channel */ 687/* Virtual Channel */
688#define PCI_VC_PORT_REG1 4 688#define PCI_VC_PORT_CAP1 4
689#define PCI_VC_REG1_EVCC 0x7 /* extended VC count */ 689#define PCI_VC_CAP1_EVCC 0x00000007 /* extended VC count */
690#define PCI_VC_PORT_REG2 8 690#define PCI_VC_CAP1_LPEVCC 0x00000070 /* low prio extended VC count */
691#define PCI_VC_REG2_32_PHASE 0x2 691#define PCI_VC_CAP1_ARB_SIZE 0x00000c00
692#define PCI_VC_REG2_64_PHASE 0x4 692#define PCI_VC_PORT_CAP2 8
693#define PCI_VC_REG2_128_PHASE 0x8 693#define PCI_VC_CAP2_32_PHASE 0x00000002
694#define PCI_VC_CAP2_64_PHASE 0x00000004
695#define PCI_VC_CAP2_128_PHASE 0x00000008
696#define PCI_VC_CAP2_ARB_OFF 0xff000000
694#define PCI_VC_PORT_CTRL 12 697#define PCI_VC_PORT_CTRL 12
698#define PCI_VC_PORT_CTRL_LOAD_TABLE 0x00000001
695#define PCI_VC_PORT_STATUS 14 699#define PCI_VC_PORT_STATUS 14
700#define PCI_VC_PORT_STATUS_TABLE 0x00000001
696#define PCI_VC_RES_CAP 16 701#define PCI_VC_RES_CAP 16
702#define PCI_VC_RES_CAP_32_PHASE 0x00000002
703#define PCI_VC_RES_CAP_64_PHASE 0x00000004
704#define PCI_VC_RES_CAP_128_PHASE 0x00000008
705#define PCI_VC_RES_CAP_128_PHASE_TB 0x00000010
706#define PCI_VC_RES_CAP_256_PHASE 0x00000020
707#define PCI_VC_RES_CAP_ARB_OFF 0xff000000
697#define PCI_VC_RES_CTRL 20 708#define PCI_VC_RES_CTRL 20
709#define PCI_VC_RES_CTRL_LOAD_TABLE 0x00010000
710#define PCI_VC_RES_CTRL_ARB_SELECT 0x000e0000
711#define PCI_VC_RES_CTRL_ID 0x07000000
712#define PCI_VC_RES_CTRL_ENABLE 0x80000000
698#define PCI_VC_RES_STATUS 26 713#define PCI_VC_RES_STATUS 26
714#define PCI_VC_RES_STATUS_TABLE 0x00000001
715#define PCI_VC_RES_STATUS_NEGO 0x00000002
699#define PCI_CAP_VC_BASE_SIZEOF 0x10 716#define PCI_CAP_VC_BASE_SIZEOF 0x10
700#define PCI_CAP_VC_PER_VC_SIZEOF 0x0C 717#define PCI_CAP_VC_PER_VC_SIZEOF 0x0C
701 718