18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * PCI Virtual Channel support 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Red Hat, Inc. All rights reserved. 68c2ecf20Sopenharmony_ci * Author: Alex Williamson <alex.williamson@redhat.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/device.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/pci.h> 138c2ecf20Sopenharmony_ci#include <linux/pci_regs.h> 148c2ecf20Sopenharmony_ci#include <linux/types.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "pci.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/** 198c2ecf20Sopenharmony_ci * pci_vc_save_restore_dwords - Save or restore a series of dwords 208c2ecf20Sopenharmony_ci * @dev: device 218c2ecf20Sopenharmony_ci * @pos: starting config space position 228c2ecf20Sopenharmony_ci * @buf: buffer to save to or restore from 238c2ecf20Sopenharmony_ci * @dwords: number of dwords to save/restore 248c2ecf20Sopenharmony_ci * @save: whether to save or restore 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_cistatic void pci_vc_save_restore_dwords(struct pci_dev *dev, int pos, 278c2ecf20Sopenharmony_ci u32 *buf, int dwords, bool save) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci int i; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci for (i = 0; i < dwords; i++, buf++) { 328c2ecf20Sopenharmony_ci if (save) 338c2ecf20Sopenharmony_ci pci_read_config_dword(dev, pos + (i * 4), buf); 348c2ecf20Sopenharmony_ci else 358c2ecf20Sopenharmony_ci pci_write_config_dword(dev, pos + (i * 4), *buf); 368c2ecf20Sopenharmony_ci } 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/** 408c2ecf20Sopenharmony_ci * pci_vc_load_arb_table - load and wait for VC arbitration table 418c2ecf20Sopenharmony_ci * @dev: device 428c2ecf20Sopenharmony_ci * @pos: starting position of VC capability (VC/VC9/MFVC) 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * Set Load VC Arbitration Table bit requesting hardware to apply the VC 458c2ecf20Sopenharmony_ci * Arbitration Table (previously loaded). When the VC Arbitration Table 468c2ecf20Sopenharmony_ci * Status clears, hardware has latched the table into VC arbitration logic. 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_cistatic void pci_vc_load_arb_table(struct pci_dev *dev, int pos) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci u16 ctrl; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL, &ctrl); 538c2ecf20Sopenharmony_ci pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL, 548c2ecf20Sopenharmony_ci ctrl | PCI_VC_PORT_CTRL_LOAD_TABLE); 558c2ecf20Sopenharmony_ci if (pci_wait_for_pending(dev, pos + PCI_VC_PORT_STATUS, 568c2ecf20Sopenharmony_ci PCI_VC_PORT_STATUS_TABLE)) 578c2ecf20Sopenharmony_ci return; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci pci_err(dev, "VC arbitration table failed to load\n"); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/** 638c2ecf20Sopenharmony_ci * pci_vc_load_port_arb_table - Load and wait for VC port arbitration table 648c2ecf20Sopenharmony_ci * @dev: device 658c2ecf20Sopenharmony_ci * @pos: starting position of VC capability (VC/VC9/MFVC) 668c2ecf20Sopenharmony_ci * @res: VC resource number, ie. VCn (0-7) 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * Set Load Port Arbitration Table bit requesting hardware to apply the Port 698c2ecf20Sopenharmony_ci * Arbitration Table (previously loaded). When the Port Arbitration Table 708c2ecf20Sopenharmony_ci * Status clears, hardware has latched the table into port arbitration logic. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_cistatic void pci_vc_load_port_arb_table(struct pci_dev *dev, int pos, int res) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci int ctrl_pos, status_pos; 758c2ecf20Sopenharmony_ci u32 ctrl; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF); 788c2ecf20Sopenharmony_ci status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci pci_read_config_dword(dev, ctrl_pos, &ctrl); 818c2ecf20Sopenharmony_ci pci_write_config_dword(dev, ctrl_pos, 828c2ecf20Sopenharmony_ci ctrl | PCI_VC_RES_CTRL_LOAD_TABLE); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_TABLE)) 858c2ecf20Sopenharmony_ci return; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci pci_err(dev, "VC%d port arbitration table failed to load\n", res); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/** 918c2ecf20Sopenharmony_ci * pci_vc_enable - Enable virtual channel 928c2ecf20Sopenharmony_ci * @dev: device 938c2ecf20Sopenharmony_ci * @pos: starting position of VC capability (VC/VC9/MFVC) 948c2ecf20Sopenharmony_ci * @res: VC res number, ie. VCn (0-7) 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * A VC is enabled by setting the enable bit in matching resource control 978c2ecf20Sopenharmony_ci * registers on both sides of a link. We therefore need to find the opposite 988c2ecf20Sopenharmony_ci * end of the link. To keep this simple we enable from the downstream device. 998c2ecf20Sopenharmony_ci * RC devices do not have an upstream device, nor does it seem that VC9 do 1008c2ecf20Sopenharmony_ci * (spec is unclear). Once we find the upstream device, match the VC ID to 1018c2ecf20Sopenharmony_ci * get the correct resource, disable and enable on both ends. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_cistatic void pci_vc_enable(struct pci_dev *dev, int pos, int res) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci int ctrl_pos, status_pos, id, pos2, evcc, i, ctrl_pos2, status_pos2; 1068c2ecf20Sopenharmony_ci u32 ctrl, header, cap1, ctrl2; 1078c2ecf20Sopenharmony_ci struct pci_dev *link = NULL; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* Enable VCs from the downstream device */ 1108c2ecf20Sopenharmony_ci if (!pci_is_pcie(dev) || !pcie_downstream_port(dev)) 1118c2ecf20Sopenharmony_ci return; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci ctrl_pos = pos + PCI_VC_RES_CTRL + (res * PCI_CAP_VC_PER_VC_SIZEOF); 1148c2ecf20Sopenharmony_ci status_pos = pos + PCI_VC_RES_STATUS + (res * PCI_CAP_VC_PER_VC_SIZEOF); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci pci_read_config_dword(dev, ctrl_pos, &ctrl); 1178c2ecf20Sopenharmony_ci id = ctrl & PCI_VC_RES_CTRL_ID; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci pci_read_config_dword(dev, pos, &header); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* If there is no opposite end of the link, skip to enable */ 1228c2ecf20Sopenharmony_ci if (PCI_EXT_CAP_ID(header) == PCI_EXT_CAP_ID_VC9 || 1238c2ecf20Sopenharmony_ci pci_is_root_bus(dev->bus)) 1248c2ecf20Sopenharmony_ci goto enable; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci pos2 = pci_find_ext_capability(dev->bus->self, PCI_EXT_CAP_ID_VC); 1278c2ecf20Sopenharmony_ci if (!pos2) 1288c2ecf20Sopenharmony_ci goto enable; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci pci_read_config_dword(dev->bus->self, pos2 + PCI_VC_PORT_CAP1, &cap1); 1318c2ecf20Sopenharmony_ci evcc = cap1 & PCI_VC_CAP1_EVCC; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* VC0 is hardwired enabled, so we can start with 1 */ 1348c2ecf20Sopenharmony_ci for (i = 1; i < evcc + 1; i++) { 1358c2ecf20Sopenharmony_ci ctrl_pos2 = pos2 + PCI_VC_RES_CTRL + 1368c2ecf20Sopenharmony_ci (i * PCI_CAP_VC_PER_VC_SIZEOF); 1378c2ecf20Sopenharmony_ci status_pos2 = pos2 + PCI_VC_RES_STATUS + 1388c2ecf20Sopenharmony_ci (i * PCI_CAP_VC_PER_VC_SIZEOF); 1398c2ecf20Sopenharmony_ci pci_read_config_dword(dev->bus->self, ctrl_pos2, &ctrl2); 1408c2ecf20Sopenharmony_ci if ((ctrl2 & PCI_VC_RES_CTRL_ID) == id) { 1418c2ecf20Sopenharmony_ci link = dev->bus->self; 1428c2ecf20Sopenharmony_ci break; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (!link) 1478c2ecf20Sopenharmony_ci goto enable; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci /* Disable if enabled */ 1508c2ecf20Sopenharmony_ci if (ctrl2 & PCI_VC_RES_CTRL_ENABLE) { 1518c2ecf20Sopenharmony_ci ctrl2 &= ~PCI_VC_RES_CTRL_ENABLE; 1528c2ecf20Sopenharmony_ci pci_write_config_dword(link, ctrl_pos2, ctrl2); 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* Enable on both ends */ 1568c2ecf20Sopenharmony_ci ctrl2 |= PCI_VC_RES_CTRL_ENABLE; 1578c2ecf20Sopenharmony_ci pci_write_config_dword(link, ctrl_pos2, ctrl2); 1588c2ecf20Sopenharmony_cienable: 1598c2ecf20Sopenharmony_ci ctrl |= PCI_VC_RES_CTRL_ENABLE; 1608c2ecf20Sopenharmony_ci pci_write_config_dword(dev, ctrl_pos, ctrl); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if (!pci_wait_for_pending(dev, status_pos, PCI_VC_RES_STATUS_NEGO)) 1638c2ecf20Sopenharmony_ci pci_err(dev, "VC%d negotiation stuck pending\n", id); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (link && !pci_wait_for_pending(link, status_pos2, 1668c2ecf20Sopenharmony_ci PCI_VC_RES_STATUS_NEGO)) 1678c2ecf20Sopenharmony_ci pci_err(link, "VC%d negotiation stuck pending\n", id); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/** 1718c2ecf20Sopenharmony_ci * pci_vc_do_save_buffer - Size, save, or restore VC state 1728c2ecf20Sopenharmony_ci * @dev: device 1738c2ecf20Sopenharmony_ci * @pos: starting position of VC capability (VC/VC9/MFVC) 1748c2ecf20Sopenharmony_ci * @save_state: buffer for save/restore 1758c2ecf20Sopenharmony_ci * @save: if provided a buffer, this indicates what to do with it 1768c2ecf20Sopenharmony_ci * 1778c2ecf20Sopenharmony_ci * Walking Virtual Channel config space to size, save, or restore it 1788c2ecf20Sopenharmony_ci * is complicated, so we do it all from one function to reduce code and 1798c2ecf20Sopenharmony_ci * guarantee ordering matches in the buffer. When called with NULL 1808c2ecf20Sopenharmony_ci * @save_state, return the size of the necessary save buffer. When called 1818c2ecf20Sopenharmony_ci * with a non-NULL @save_state, @save determines whether we save to the 1828c2ecf20Sopenharmony_ci * buffer or restore from it. 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_cistatic int pci_vc_do_save_buffer(struct pci_dev *dev, int pos, 1858c2ecf20Sopenharmony_ci struct pci_cap_saved_state *save_state, 1868c2ecf20Sopenharmony_ci bool save) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci u32 cap1; 1898c2ecf20Sopenharmony_ci char evcc, lpevcc, parb_size; 1908c2ecf20Sopenharmony_ci int i, len = 0; 1918c2ecf20Sopenharmony_ci u8 *buf = save_state ? (u8 *)save_state->cap.data : NULL; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci /* Sanity check buffer size for save/restore */ 1948c2ecf20Sopenharmony_ci if (buf && save_state->cap.size != 1958c2ecf20Sopenharmony_ci pci_vc_do_save_buffer(dev, pos, NULL, save)) { 1968c2ecf20Sopenharmony_ci pci_err(dev, "VC save buffer size does not match @0x%x\n", pos); 1978c2ecf20Sopenharmony_ci return -ENOMEM; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP1, &cap1); 2018c2ecf20Sopenharmony_ci /* Extended VC Count (not counting VC0) */ 2028c2ecf20Sopenharmony_ci evcc = cap1 & PCI_VC_CAP1_EVCC; 2038c2ecf20Sopenharmony_ci /* Low Priority Extended VC Count (not counting VC0) */ 2048c2ecf20Sopenharmony_ci lpevcc = (cap1 & PCI_VC_CAP1_LPEVCC) >> 4; 2058c2ecf20Sopenharmony_ci /* Port Arbitration Table Entry Size (bits) */ 2068c2ecf20Sopenharmony_ci parb_size = 1 << ((cap1 & PCI_VC_CAP1_ARB_SIZE) >> 10); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci /* 2098c2ecf20Sopenharmony_ci * Port VC Control Register contains VC Arbitration Select, which 2108c2ecf20Sopenharmony_ci * cannot be modified when more than one LPVC is in operation. We 2118c2ecf20Sopenharmony_ci * therefore save/restore it first, as only VC0 should be enabled 2128c2ecf20Sopenharmony_ci * after device reset. 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_ci if (buf) { 2158c2ecf20Sopenharmony_ci if (save) 2168c2ecf20Sopenharmony_ci pci_read_config_word(dev, pos + PCI_VC_PORT_CTRL, 2178c2ecf20Sopenharmony_ci (u16 *)buf); 2188c2ecf20Sopenharmony_ci else 2198c2ecf20Sopenharmony_ci pci_write_config_word(dev, pos + PCI_VC_PORT_CTRL, 2208c2ecf20Sopenharmony_ci *(u16 *)buf); 2218c2ecf20Sopenharmony_ci buf += 4; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci len += 4; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci /* 2268c2ecf20Sopenharmony_ci * If we have any Low Priority VCs and a VC Arbitration Table Offset 2278c2ecf20Sopenharmony_ci * in Port VC Capability Register 2 then save/restore it next. 2288c2ecf20Sopenharmony_ci */ 2298c2ecf20Sopenharmony_ci if (lpevcc) { 2308c2ecf20Sopenharmony_ci u32 cap2; 2318c2ecf20Sopenharmony_ci int vcarb_offset; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci pci_read_config_dword(dev, pos + PCI_VC_PORT_CAP2, &cap2); 2348c2ecf20Sopenharmony_ci vcarb_offset = ((cap2 & PCI_VC_CAP2_ARB_OFF) >> 24) * 16; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (vcarb_offset) { 2378c2ecf20Sopenharmony_ci int size, vcarb_phases = 0; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (cap2 & PCI_VC_CAP2_128_PHASE) 2408c2ecf20Sopenharmony_ci vcarb_phases = 128; 2418c2ecf20Sopenharmony_ci else if (cap2 & PCI_VC_CAP2_64_PHASE) 2428c2ecf20Sopenharmony_ci vcarb_phases = 64; 2438c2ecf20Sopenharmony_ci else if (cap2 & PCI_VC_CAP2_32_PHASE) 2448c2ecf20Sopenharmony_ci vcarb_phases = 32; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci /* Fixed 4 bits per phase per lpevcc (plus VC0) */ 2478c2ecf20Sopenharmony_ci size = ((lpevcc + 1) * vcarb_phases * 4) / 8; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (size && buf) { 2508c2ecf20Sopenharmony_ci pci_vc_save_restore_dwords(dev, 2518c2ecf20Sopenharmony_ci pos + vcarb_offset, 2528c2ecf20Sopenharmony_ci (u32 *)buf, 2538c2ecf20Sopenharmony_ci size / 4, save); 2548c2ecf20Sopenharmony_ci /* 2558c2ecf20Sopenharmony_ci * On restore, we need to signal hardware to 2568c2ecf20Sopenharmony_ci * re-load the VC Arbitration Table. 2578c2ecf20Sopenharmony_ci */ 2588c2ecf20Sopenharmony_ci if (!save) 2598c2ecf20Sopenharmony_ci pci_vc_load_arb_table(dev, pos); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci buf += size; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci len += size; 2648c2ecf20Sopenharmony_ci } 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci /* 2688c2ecf20Sopenharmony_ci * In addition to each VC Resource Control Register, we may have a 2698c2ecf20Sopenharmony_ci * Port Arbitration Table attached to each VC. The Port Arbitration 2708c2ecf20Sopenharmony_ci * Table Offset in each VC Resource Capability Register tells us if 2718c2ecf20Sopenharmony_ci * it exists. The entry size is global from the Port VC Capability 2728c2ecf20Sopenharmony_ci * Register1 above. The number of phases is determined per VC. 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_ci for (i = 0; i < evcc + 1; i++) { 2758c2ecf20Sopenharmony_ci u32 cap; 2768c2ecf20Sopenharmony_ci int parb_offset; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci pci_read_config_dword(dev, pos + PCI_VC_RES_CAP + 2798c2ecf20Sopenharmony_ci (i * PCI_CAP_VC_PER_VC_SIZEOF), &cap); 2808c2ecf20Sopenharmony_ci parb_offset = ((cap & PCI_VC_RES_CAP_ARB_OFF) >> 24) * 16; 2818c2ecf20Sopenharmony_ci if (parb_offset) { 2828c2ecf20Sopenharmony_ci int size, parb_phases = 0; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci if (cap & PCI_VC_RES_CAP_256_PHASE) 2858c2ecf20Sopenharmony_ci parb_phases = 256; 2868c2ecf20Sopenharmony_ci else if (cap & (PCI_VC_RES_CAP_128_PHASE | 2878c2ecf20Sopenharmony_ci PCI_VC_RES_CAP_128_PHASE_TB)) 2888c2ecf20Sopenharmony_ci parb_phases = 128; 2898c2ecf20Sopenharmony_ci else if (cap & PCI_VC_RES_CAP_64_PHASE) 2908c2ecf20Sopenharmony_ci parb_phases = 64; 2918c2ecf20Sopenharmony_ci else if (cap & PCI_VC_RES_CAP_32_PHASE) 2928c2ecf20Sopenharmony_ci parb_phases = 32; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci size = (parb_size * parb_phases) / 8; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci if (size && buf) { 2978c2ecf20Sopenharmony_ci pci_vc_save_restore_dwords(dev, 2988c2ecf20Sopenharmony_ci pos + parb_offset, 2998c2ecf20Sopenharmony_ci (u32 *)buf, 3008c2ecf20Sopenharmony_ci size / 4, save); 3018c2ecf20Sopenharmony_ci buf += size; 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci len += size; 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci /* VC Resource Control Register */ 3078c2ecf20Sopenharmony_ci if (buf) { 3088c2ecf20Sopenharmony_ci int ctrl_pos = pos + PCI_VC_RES_CTRL + 3098c2ecf20Sopenharmony_ci (i * PCI_CAP_VC_PER_VC_SIZEOF); 3108c2ecf20Sopenharmony_ci if (save) 3118c2ecf20Sopenharmony_ci pci_read_config_dword(dev, ctrl_pos, 3128c2ecf20Sopenharmony_ci (u32 *)buf); 3138c2ecf20Sopenharmony_ci else { 3148c2ecf20Sopenharmony_ci u32 tmp, ctrl = *(u32 *)buf; 3158c2ecf20Sopenharmony_ci /* 3168c2ecf20Sopenharmony_ci * For an FLR case, the VC config may remain. 3178c2ecf20Sopenharmony_ci * Preserve enable bit, restore the rest. 3188c2ecf20Sopenharmony_ci */ 3198c2ecf20Sopenharmony_ci pci_read_config_dword(dev, ctrl_pos, &tmp); 3208c2ecf20Sopenharmony_ci tmp &= PCI_VC_RES_CTRL_ENABLE; 3218c2ecf20Sopenharmony_ci tmp |= ctrl & ~PCI_VC_RES_CTRL_ENABLE; 3228c2ecf20Sopenharmony_ci pci_write_config_dword(dev, ctrl_pos, tmp); 3238c2ecf20Sopenharmony_ci /* Load port arbitration table if used */ 3248c2ecf20Sopenharmony_ci if (ctrl & PCI_VC_RES_CTRL_ARB_SELECT) 3258c2ecf20Sopenharmony_ci pci_vc_load_port_arb_table(dev, pos, i); 3268c2ecf20Sopenharmony_ci /* Re-enable if needed */ 3278c2ecf20Sopenharmony_ci if ((ctrl ^ tmp) & PCI_VC_RES_CTRL_ENABLE) 3288c2ecf20Sopenharmony_ci pci_vc_enable(dev, pos, i); 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci buf += 4; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci len += 4; 3338c2ecf20Sopenharmony_ci } 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return buf ? 0 : len; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_cistatic struct { 3398c2ecf20Sopenharmony_ci u16 id; 3408c2ecf20Sopenharmony_ci const char *name; 3418c2ecf20Sopenharmony_ci} vc_caps[] = { { PCI_EXT_CAP_ID_MFVC, "MFVC" }, 3428c2ecf20Sopenharmony_ci { PCI_EXT_CAP_ID_VC, "VC" }, 3438c2ecf20Sopenharmony_ci { PCI_EXT_CAP_ID_VC9, "VC9" } }; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci/** 3468c2ecf20Sopenharmony_ci * pci_save_vc_state - Save VC state to pre-allocate save buffer 3478c2ecf20Sopenharmony_ci * @dev: device 3488c2ecf20Sopenharmony_ci * 3498c2ecf20Sopenharmony_ci * For each type of VC capability, VC/VC9/MFVC, find the capability and 3508c2ecf20Sopenharmony_ci * save it to the pre-allocated save buffer. 3518c2ecf20Sopenharmony_ci */ 3528c2ecf20Sopenharmony_ciint pci_save_vc_state(struct pci_dev *dev) 3538c2ecf20Sopenharmony_ci{ 3548c2ecf20Sopenharmony_ci int i; 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(vc_caps); i++) { 3578c2ecf20Sopenharmony_ci int pos, ret; 3588c2ecf20Sopenharmony_ci struct pci_cap_saved_state *save_state; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci pos = pci_find_ext_capability(dev, vc_caps[i].id); 3618c2ecf20Sopenharmony_ci if (!pos) 3628c2ecf20Sopenharmony_ci continue; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id); 3658c2ecf20Sopenharmony_ci if (!save_state) { 3668c2ecf20Sopenharmony_ci pci_err(dev, "%s buffer not found in %s\n", 3678c2ecf20Sopenharmony_ci vc_caps[i].name, __func__); 3688c2ecf20Sopenharmony_ci return -ENOMEM; 3698c2ecf20Sopenharmony_ci } 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci ret = pci_vc_do_save_buffer(dev, pos, save_state, true); 3728c2ecf20Sopenharmony_ci if (ret) { 3738c2ecf20Sopenharmony_ci pci_err(dev, "%s save unsuccessful %s\n", 3748c2ecf20Sopenharmony_ci vc_caps[i].name, __func__); 3758c2ecf20Sopenharmony_ci return ret; 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci return 0; 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci/** 3838c2ecf20Sopenharmony_ci * pci_restore_vc_state - Restore VC state from save buffer 3848c2ecf20Sopenharmony_ci * @dev: device 3858c2ecf20Sopenharmony_ci * 3868c2ecf20Sopenharmony_ci * For each type of VC capability, VC/VC9/MFVC, find the capability and 3878c2ecf20Sopenharmony_ci * restore it from the previously saved buffer. 3888c2ecf20Sopenharmony_ci */ 3898c2ecf20Sopenharmony_civoid pci_restore_vc_state(struct pci_dev *dev) 3908c2ecf20Sopenharmony_ci{ 3918c2ecf20Sopenharmony_ci int i; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(vc_caps); i++) { 3948c2ecf20Sopenharmony_ci int pos; 3958c2ecf20Sopenharmony_ci struct pci_cap_saved_state *save_state; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci pos = pci_find_ext_capability(dev, vc_caps[i].id); 3988c2ecf20Sopenharmony_ci save_state = pci_find_saved_ext_cap(dev, vc_caps[i].id); 3998c2ecf20Sopenharmony_ci if (!save_state || !pos) 4008c2ecf20Sopenharmony_ci continue; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci pci_vc_do_save_buffer(dev, pos, save_state, false); 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci} 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci/** 4078c2ecf20Sopenharmony_ci * pci_allocate_vc_save_buffers - Allocate save buffers for VC caps 4088c2ecf20Sopenharmony_ci * @dev: device 4098c2ecf20Sopenharmony_ci * 4108c2ecf20Sopenharmony_ci * For each type of VC capability, VC/VC9/MFVC, find the capability, size 4118c2ecf20Sopenharmony_ci * it, and allocate a buffer for save/restore. 4128c2ecf20Sopenharmony_ci */ 4138c2ecf20Sopenharmony_civoid pci_allocate_vc_save_buffers(struct pci_dev *dev) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci int i; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(vc_caps); i++) { 4188c2ecf20Sopenharmony_ci int len, pos = pci_find_ext_capability(dev, vc_caps[i].id); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci if (!pos) 4218c2ecf20Sopenharmony_ci continue; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci len = pci_vc_do_save_buffer(dev, pos, NULL, false); 4248c2ecf20Sopenharmony_ci if (pci_add_ext_cap_save_buffer(dev, vc_caps[i].id, len)) 4258c2ecf20Sopenharmony_ci pci_err(dev, "unable to preallocate %s save buffer\n", 4268c2ecf20Sopenharmony_ci vc_caps[i].name); 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci} 429