0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/device.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/pci.h>
0013 #include <linux/pci_regs.h>
0014 #include <linux/types.h>
0015
0016 #include "pci.h"
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 static void pci_vc_save_restore_dwords(struct pci_dev *dev, int pos,
0027 u32 *buf, int dwords, bool save)
0028 {
0029 int i;
0030
0031 for (i = 0; i < dwords; i++, buf++) {
0032 if (save)
0033 pci_read_config_dword(dev, pos + (i * 4), buf);
0034 else
0035 pci_write_config_dword(dev, pos + (i * 4), *buf);
0036 }
0037 }
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 static void pci_vc_load_arb_table(struct pci_dev *dev, int pos)
0049 {
0050 u16 ctrl;
0051
0052 pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL, &ctrl);
0053 pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
0054 ctrl | PCI_VC_PORT_CTRL_LOAD_TABLE);
0055 if (pci_wait_for_pending(dev, pos + PCI_VC_PORT_STATUS,
0056 PCI_VC_PORT_STATUS_TABLE))
0057 return;
0058
0059 pci_err(dev, "VC arbitration table failed to load\n");
0060 }
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 static void pci_vc_load_port_arb_table(struct pci_dev *dev, int pos, int res)
0073 {
0074 int ctrl_pos, status_pos;
0075 u32 ctrl;
0076
0077 ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
0078 status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
0079
0080 pci_read_config_dword(dev, ctrl_pos, &ctrl);
0081 pci_write_config_dword(dev, ctrl_pos,
0082 ctrl | PCI_VC_RES_CTRL_LOAD_TABLE);
0083
0084 if (pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_TABLE))
0085 return;
0086
0087 pci_err(dev, "VC%d port arbitration table failed to load\n", res);
0088 }
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 static void pci_vc_enable(struct pci_dev *dev, int pos, int res)
0104 {
0105 int ctrl_pos, status_pos, id, pos2, evcc, i, ctrl_pos2, status_pos2;
0106 u32 ctrl, header, cap1, ctrl2;
0107 struct pci_dev *link = NULL;
0108
0109
0110 if (!pci_is_pcie(dev) || !pcie_downstream_port(dev))
0111 return;
0112
0113 ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF);
0114 status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF);
0115
0116 pci_read_config_dword(dev, ctrl_pos, &ctrl);
0117 id = ctrl & PCI_VC_RES_CTRL_ID;
0118
0119 pci_read_config_dword(dev, pos, &header);
0120
0121
0122 if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_VC9 ||
0123 pci_is_root_bus(dev->bus))
0124 goto enable;
0125
0126 pos2 = pci_find_ext_capability(dev->bus->self, PCI_EXT_CAP_ID_VC);
0127 if (!pos2)
0128 goto enable;
0129
0130 pci_read_config_dword(dev->bus->self, pos2 + PCI_VC_PORT_CAP1, &cap1);
0131 evcc = cap1 & PCI_VC_CAP1_EVCC;
0132
0133
0134 for (i = 1; i < evcc + 1; i++) {
0135 ctrl_pos2 = pos2 + PCI_VC_RES_CTRL +
0136 (i * PCI_CAP_VC_PER_VC_SIZEOF);
0137 status_pos2 = pos2 + PCI_VC_RES_STATUS +
0138 (i * PCI_CAP_VC_PER_VC_SIZEOF);
0139 pci_read_config_dword(dev->bus->self, ctrl_pos2, &ctrl2);
0140 if ((ctrl2 & PCI_VC_RES_CTRL_ID) == id) {
0141 link = dev->bus->self;
0142 break;
0143 }
0144 }
0145
0146 if (!link)
0147 goto enable;
0148
0149
0150 if (ctrl2 & PCI_VC_RES_CTRL_ENABLE) {
0151 ctrl2 &= ~PCI_VC_RES_CTRL_ENABLE;
0152 pci_write_config_dword(link, ctrl_pos2, ctrl2);
0153 }
0154
0155
0156 ctrl2 |= PCI_VC_RES_CTRL_ENABLE;
0157 pci_write_config_dword(link, ctrl_pos2, ctrl2);
0158 enable:
0159 ctrl |= PCI_VC_RES_CTRL_ENABLE;
0160 pci_write_config_dword(dev, ctrl_pos, ctrl);
0161
0162 if (!pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_NEGO))
0163 pci_err(dev, "VC%d negotiation stuck pending\n", id);
0164
0165 if (link && !pci_wait_for_pending(link, status_pos2,
0166 PCI_VC_RES_STATUS_NEGO))
0167 pci_err(link, "VC%d negotiation stuck pending\n", id);
0168 }
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 static int pci_vc_do_save_buffer(struct pci_dev *dev, int pos,
0185 struct pci_cap_saved_state *save_state,
0186 bool save)
0187 {
0188 u32 cap1;
0189 char evcc, lpevcc, parb_size;
0190 int i, len = 0;
0191 u8 *buf = save_state ? (u8 *)save_state->cap.data : NULL;
0192
0193
0194 if (buf && save_state->cap.size !=
0195 pci_vc_do_save_buffer(dev, pos, NULL, save)) {
0196 pci_err(dev, "VC save buffer size does not match @0x%x\n", pos);
0197 return -ENOMEM;
0198 }
0199
0200 pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP1, &cap1);
0201
0202 evcc = cap1 & PCI_VC_CAP1_EVCC;
0203
0204 lpevcc = (cap1 & PCI_VC_CAP1_LPEVCC) >> 4;
0205
0206 parb_size = 1 << ((cap1 & PCI_VC_CAP1_ARB_SIZE) >> 10);
0207
0208
0209
0210
0211
0212
0213
0214 if (buf) {
0215 if (save)
0216 pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL,
0217 (u16 *)buf);
0218 else
0219 pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL,
0220 *(u16 *)buf);
0221 buf += 4;
0222 }
0223 len += 4;
0224
0225
0226
0227
0228
0229 if (lpevcc) {
0230 u32 cap2;
0231 int vcarb_offset;
0232
0233 pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP2, &cap2);
0234 vcarb_offset = ((cap2 & PCI_VC_CAP2_ARB_OFF) >> 24) * 16;
0235
0236 if (vcarb_offset) {
0237 int size, vcarb_phases = 0;
0238
0239 if (cap2 & PCI_VC_CAP2_128_PHASE)
0240 vcarb_phases = 128;
0241 else if (cap2 & PCI_VC_CAP2_64_PHASE)
0242 vcarb_phases = 64;
0243 else if (cap2 & PCI_VC_CAP2_32_PHASE)
0244 vcarb_phases = 32;
0245
0246
0247 size = ((lpevcc + 1) * vcarb_phases * 4) / 8;
0248
0249 if (size && buf) {
0250 pci_vc_save_restore_dwords(dev,
0251 pos + vcarb_offset,
0252 (u32 *)buf,
0253 size / 4, save);
0254
0255
0256
0257
0258 if (!save)
0259 pci_vc_load_arb_table(dev, pos);
0260
0261 buf += size;
0262 }
0263 len += size;
0264 }
0265 }
0266
0267
0268
0269
0270
0271
0272
0273
0274 for (i = 0; i < evcc + 1; i++) {
0275 u32 cap;
0276 int parb_offset;
0277
0278 pci_read_config_dword(dev, pos + PCI_VC_RES_CAP +
0279 (i * PCI_CAP_VC_PER_VC_SIZEOF), &cap);
0280 parb_offset = ((cap & PCI_VC_RES_CAP_ARB_OFF) >> 24) * 16;
0281 if (parb_offset) {
0282 int size, parb_phases = 0;
0283
0284 if (cap & PCI_VC_RES_CAP_256_PHASE)
0285 parb_phases = 256;
0286 else if (cap & (PCI_VC_RES_CAP_128_PHASE |
0287 PCI_VC_RES_CAP_128_PHASE_TB))
0288 parb_phases = 128;
0289 else if (cap & PCI_VC_RES_CAP_64_PHASE)
0290 parb_phases = 64;
0291 else if (cap & PCI_VC_RES_CAP_32_PHASE)
0292 parb_phases = 32;
0293
0294 size = (parb_size * parb_phases) / 8;
0295
0296 if (size && buf) {
0297 pci_vc_save_restore_dwords(dev,
0298 pos + parb_offset,
0299 (u32 *)buf,
0300 size / 4, save);
0301 buf += size;
0302 }
0303 len += size;
0304 }
0305
0306
0307 if (buf) {
0308 int ctrl_pos = pos + PCI_VC_RES_CTRL +
0309 (i * PCI_CAP_VC_PER_VC_SIZEOF);
0310 if (save)
0311 pci_read_config_dword(dev, ctrl_pos,
0312 (u32 *)buf);
0313 else {
0314 u32 tmp, ctrl = *(u32 *)buf;
0315
0316
0317
0318
0319 pci_read_config_dword(dev, ctrl_pos, &tmp);
0320 tmp &= PCI_VC_RES_CTRL_ENABLE;
0321 tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE;
0322 pci_write_config_dword(dev, ctrl_pos, tmp);
0323
0324 if (ctrl & PCI_VC_RES_CTRL_ARB_SELECT)
0325 pci_vc_load_port_arb_table(dev, pos, i);
0326
0327 if ((ctrl ^ tmp) & PCI_VC_RES_CTRL_ENABLE)
0328 pci_vc_enable(dev, pos, i);
0329 }
0330 buf += 4;
0331 }
0332 len += 4;
0333 }
0334
0335 return buf ? 0 : len;
0336 }
0337
0338 static struct {
0339 u16 id;
0340 const char *name;
0341 } vc_caps[] = { { PCI_EXT_CAP_ID_MFVC, "MFVC" },
0342 { PCI_EXT_CAP_ID_VC, "VC" },
0343 { PCI_EXT_CAP_ID_VC9, "VC9" } };
0344
0345
0346
0347
0348
0349
0350
0351
0352 int pci_save_vc_state(struct pci_dev *dev)
0353 {
0354 int i;
0355
0356 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
0357 int pos, ret;
0358 struct pci_cap_saved_state *save_state;
0359
0360 pos = pci_find_ext_capability(dev, vc_caps[i].id);
0361 if (!pos)
0362 continue;
0363
0364 save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
0365 if (!save_state) {
0366 pci_err(dev, "%s buffer not found in %s\n",
0367 vc_caps[i].name, __func__);
0368 return -ENOMEM;
0369 }
0370
0371 ret = pci_vc_do_save_buffer(dev, pos, save_state, true);
0372 if (ret) {
0373 pci_err(dev, "%s save unsuccessful %s\n",
0374 vc_caps[i].name, __func__);
0375 return ret;
0376 }
0377 }
0378
0379 return 0;
0380 }
0381
0382
0383
0384
0385
0386
0387
0388
0389 void pci_restore_vc_state(struct pci_dev *dev)
0390 {
0391 int i;
0392
0393 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
0394 int pos;
0395 struct pci_cap_saved_state *save_state;
0396
0397 pos = pci_find_ext_capability(dev, vc_caps[i].id);
0398 save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id);
0399 if (!save_state || !pos)
0400 continue;
0401
0402 pci_vc_do_save_buffer(dev, pos, save_state, false);
0403 }
0404 }
0405
0406
0407
0408
0409
0410
0411
0412
0413 void pci_allocate_vc_save_buffers(struct pci_dev *dev)
0414 {
0415 int i;
0416
0417 for (i = 0; i < ARRAY_SIZE(vc_caps); i++) {
0418 int len, pos = pci_find_ext_capability(dev, vc_caps[i].id);
0419
0420 if (!pos)
0421 continue;
0422
0423 len = pci_vc_do_save_buffer(dev, pos, NULL, false);
0424 if (pci_add_ext_cap_save_buffer(dev, vc_caps[i].id, len))
0425 pci_err(dev, "unable to preallocate %s save buffer\n",
0426 vc_caps[i].name);
0427 }
0428 }