18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Marvell 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Author: Thomas Petazzoni <thomas.petazzoni@bootlin.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file helps PCI controller drivers implement a fake root port 88c2ecf20Sopenharmony_ci * PCI bridge when the HW doesn't provide such a root port PCI 98c2ecf20Sopenharmony_ci * bridge. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * It emulates a PCI bridge by providing a fake PCI configuration 128c2ecf20Sopenharmony_ci * space (and optionally a PCIe capability configuration space) in 138c2ecf20Sopenharmony_ci * memory. By default the read/write operations simply read and update 148c2ecf20Sopenharmony_ci * this fake configuration space in memory. However, PCI controller 158c2ecf20Sopenharmony_ci * drivers can provide through the 'struct pci_sw_bridge_ops' 168c2ecf20Sopenharmony_ci * structure a set of operations to override or complement this 178c2ecf20Sopenharmony_ci * default behavior. 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <linux/pci.h> 218c2ecf20Sopenharmony_ci#include "pci-bridge-emul.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF 248c2ecf20Sopenharmony_ci#define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2) 258c2ecf20Sopenharmony_ci#define PCI_CAP_PCIE_START PCI_BRIDGE_CONF_END 268c2ecf20Sopenharmony_ci#define PCI_CAP_PCIE_END (PCI_CAP_PCIE_START + PCI_CAP_PCIE_SIZEOF) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/** 298c2ecf20Sopenharmony_ci * struct pci_bridge_reg_behavior - register bits behaviors 308c2ecf20Sopenharmony_ci * @ro: Read-Only bits 318c2ecf20Sopenharmony_ci * @rw: Read-Write bits 328c2ecf20Sopenharmony_ci * @w1c: Write-1-to-Clear bits 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * Reads and Writes will be filtered by specified behavior. All other bits not 358c2ecf20Sopenharmony_ci * declared are assumed 'Reserved' and will return 0 on reads, per PCIe 5.0: 368c2ecf20Sopenharmony_ci * "Reserved register fields must be read only and must return 0 (all 0's for 378c2ecf20Sopenharmony_ci * multi-bit fields) when read". 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_cistruct pci_bridge_reg_behavior { 408c2ecf20Sopenharmony_ci /* Read-only bits */ 418c2ecf20Sopenharmony_ci u32 ro; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci /* Read-write bits */ 448c2ecf20Sopenharmony_ci u32 rw; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci /* Write-1-to-clear bits */ 478c2ecf20Sopenharmony_ci u32 w1c; 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic const 518c2ecf20Sopenharmony_cistruct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = { 528c2ecf20Sopenharmony_ci [PCI_VENDOR_ID / 4] = { .ro = ~0 }, 538c2ecf20Sopenharmony_ci [PCI_COMMAND / 4] = { 548c2ecf20Sopenharmony_ci .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | 558c2ecf20Sopenharmony_ci PCI_COMMAND_MASTER | PCI_COMMAND_PARITY | 568c2ecf20Sopenharmony_ci PCI_COMMAND_SERR), 578c2ecf20Sopenharmony_ci .ro = ((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE | 588c2ecf20Sopenharmony_ci PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT | 598c2ecf20Sopenharmony_ci PCI_COMMAND_FAST_BACK) | 608c2ecf20Sopenharmony_ci (PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ | 618c2ecf20Sopenharmony_ci PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MASK) << 16), 628c2ecf20Sopenharmony_ci .w1c = PCI_STATUS_ERROR_BITS << 16, 638c2ecf20Sopenharmony_ci }, 648c2ecf20Sopenharmony_ci [PCI_CLASS_REVISION / 4] = { .ro = ~0 }, 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci /* 678c2ecf20Sopenharmony_ci * Cache Line Size register: implement as read-only, we do not 688c2ecf20Sopenharmony_ci * pretend implementing "Memory Write and Invalidate" 698c2ecf20Sopenharmony_ci * transactions" 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * Latency Timer Register: implemented as read-only, as "A 728c2ecf20Sopenharmony_ci * bridge that is not capable of a burst transfer of more than 738c2ecf20Sopenharmony_ci * two data phases on its primary interface is permitted to 748c2ecf20Sopenharmony_ci * hardwire the Latency Timer to a value of 16 or less" 758c2ecf20Sopenharmony_ci * 768c2ecf20Sopenharmony_ci * Header Type: always read-only 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * BIST register: implemented as read-only, as "A bridge that 798c2ecf20Sopenharmony_ci * does not support BIST must implement this register as a 808c2ecf20Sopenharmony_ci * read-only register that returns 0 when read" 818c2ecf20Sopenharmony_ci */ 828c2ecf20Sopenharmony_ci [PCI_CACHE_LINE_SIZE / 4] = { .ro = ~0 }, 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* 858c2ecf20Sopenharmony_ci * Base Address registers not used must be implemented as 868c2ecf20Sopenharmony_ci * read-only registers that return 0 when read. 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_ci [PCI_BASE_ADDRESS_0 / 4] = { .ro = ~0 }, 898c2ecf20Sopenharmony_ci [PCI_BASE_ADDRESS_1 / 4] = { .ro = ~0 }, 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci [PCI_PRIMARY_BUS / 4] = { 928c2ecf20Sopenharmony_ci /* Primary, secondary and subordinate bus are RW */ 938c2ecf20Sopenharmony_ci .rw = GENMASK(24, 0), 948c2ecf20Sopenharmony_ci /* Secondary latency is read-only */ 958c2ecf20Sopenharmony_ci .ro = GENMASK(31, 24), 968c2ecf20Sopenharmony_ci }, 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci [PCI_IO_BASE / 4] = { 998c2ecf20Sopenharmony_ci /* The high four bits of I/O base/limit are RW */ 1008c2ecf20Sopenharmony_ci .rw = (GENMASK(15, 12) | GENMASK(7, 4)), 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* The low four bits of I/O base/limit are RO */ 1038c2ecf20Sopenharmony_ci .ro = (((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 1048c2ecf20Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16) | 1058c2ecf20Sopenharmony_ci GENMASK(11, 8) | GENMASK(3, 0)), 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci .w1c = PCI_STATUS_ERROR_BITS << 16, 1088c2ecf20Sopenharmony_ci }, 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci [PCI_MEMORY_BASE / 4] = { 1118c2ecf20Sopenharmony_ci /* The high 12-bits of mem base/limit are RW */ 1128c2ecf20Sopenharmony_ci .rw = GENMASK(31, 20) | GENMASK(15, 4), 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* The low four bits of mem base/limit are RO */ 1158c2ecf20Sopenharmony_ci .ro = GENMASK(19, 16) | GENMASK(3, 0), 1168c2ecf20Sopenharmony_ci }, 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci [PCI_PREF_MEMORY_BASE / 4] = { 1198c2ecf20Sopenharmony_ci /* The high 12-bits of pref mem base/limit are RW */ 1208c2ecf20Sopenharmony_ci .rw = GENMASK(31, 20) | GENMASK(15, 4), 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* The low four bits of pref mem base/limit are RO */ 1238c2ecf20Sopenharmony_ci .ro = GENMASK(19, 16) | GENMASK(3, 0), 1248c2ecf20Sopenharmony_ci }, 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci [PCI_PREF_BASE_UPPER32 / 4] = { 1278c2ecf20Sopenharmony_ci .rw = ~0, 1288c2ecf20Sopenharmony_ci }, 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci [PCI_PREF_LIMIT_UPPER32 / 4] = { 1318c2ecf20Sopenharmony_ci .rw = ~0, 1328c2ecf20Sopenharmony_ci }, 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci [PCI_IO_BASE_UPPER16 / 4] = { 1358c2ecf20Sopenharmony_ci .rw = ~0, 1368c2ecf20Sopenharmony_ci }, 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci [PCI_CAPABILITY_LIST / 4] = { 1398c2ecf20Sopenharmony_ci .ro = GENMASK(7, 0), 1408c2ecf20Sopenharmony_ci }, 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci /* 1438c2ecf20Sopenharmony_ci * If expansion ROM is unsupported then ROM Base Address register must 1448c2ecf20Sopenharmony_ci * be implemented as read-only register that return 0 when read, same 1458c2ecf20Sopenharmony_ci * as for unused Base Address registers. 1468c2ecf20Sopenharmony_ci */ 1478c2ecf20Sopenharmony_ci [PCI_ROM_ADDRESS1 / 4] = { 1488c2ecf20Sopenharmony_ci .ro = ~0, 1498c2ecf20Sopenharmony_ci }, 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* 1528c2ecf20Sopenharmony_ci * Interrupt line (bits 7:0) are RW, interrupt pin (bits 15:8) 1538c2ecf20Sopenharmony_ci * are RO, and bridge control (31:16) are a mix of RW, RO, 1548c2ecf20Sopenharmony_ci * reserved and W1C bits 1558c2ecf20Sopenharmony_ci */ 1568c2ecf20Sopenharmony_ci [PCI_INTERRUPT_LINE / 4] = { 1578c2ecf20Sopenharmony_ci /* Interrupt line is RW */ 1588c2ecf20Sopenharmony_ci .rw = (GENMASK(7, 0) | 1598c2ecf20Sopenharmony_ci ((PCI_BRIDGE_CTL_PARITY | 1608c2ecf20Sopenharmony_ci PCI_BRIDGE_CTL_SERR | 1618c2ecf20Sopenharmony_ci PCI_BRIDGE_CTL_ISA | 1628c2ecf20Sopenharmony_ci PCI_BRIDGE_CTL_VGA | 1638c2ecf20Sopenharmony_ci PCI_BRIDGE_CTL_MASTER_ABORT | 1648c2ecf20Sopenharmony_ci PCI_BRIDGE_CTL_BUS_RESET | 1658c2ecf20Sopenharmony_ci BIT(8) | BIT(9) | BIT(11)) << 16)), 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* Interrupt pin is RO */ 1688c2ecf20Sopenharmony_ci .ro = (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK) << 16)), 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci .w1c = BIT(10) << 16, 1718c2ecf20Sopenharmony_ci }, 1728c2ecf20Sopenharmony_ci}; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic const 1758c2ecf20Sopenharmony_cistruct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = { 1768c2ecf20Sopenharmony_ci [PCI_CAP_LIST_ID / 4] = { 1778c2ecf20Sopenharmony_ci /* 1788c2ecf20Sopenharmony_ci * Capability ID, Next Capability Pointer and 1798c2ecf20Sopenharmony_ci * bits [14:0] of Capabilities register are all read-only. 1808c2ecf20Sopenharmony_ci * Bit 15 of Capabilities register is reserved. 1818c2ecf20Sopenharmony_ci */ 1828c2ecf20Sopenharmony_ci .ro = GENMASK(30, 0), 1838c2ecf20Sopenharmony_ci }, 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci [PCI_EXP_DEVCAP / 4] = { 1868c2ecf20Sopenharmony_ci /* 1878c2ecf20Sopenharmony_ci * Bits [31:29] and [17:16] are reserved. 1888c2ecf20Sopenharmony_ci * Bits [27:18] are reserved for non-upstream ports. 1898c2ecf20Sopenharmony_ci * Bits 28 and [14:6] are reserved for non-endpoint devices. 1908c2ecf20Sopenharmony_ci * Other bits are read-only. 1918c2ecf20Sopenharmony_ci */ 1928c2ecf20Sopenharmony_ci .ro = BIT(15) | GENMASK(5, 0), 1938c2ecf20Sopenharmony_ci }, 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci [PCI_EXP_DEVCTL / 4] = { 1968c2ecf20Sopenharmony_ci /* 1978c2ecf20Sopenharmony_ci * Device control register is RW, except bit 15 which is 1988c2ecf20Sopenharmony_ci * reserved for non-endpoints or non-PCIe-to-PCI/X bridges. 1998c2ecf20Sopenharmony_ci */ 2008c2ecf20Sopenharmony_ci .rw = GENMASK(14, 0), 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* 2038c2ecf20Sopenharmony_ci * Device status register has bits 6 and [3:0] W1C, [5:4] RO, 2048c2ecf20Sopenharmony_ci * the rest is reserved. Also bit 6 is reserved for non-upstream 2058c2ecf20Sopenharmony_ci * ports. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ci .w1c = GENMASK(3, 0) << 16, 2088c2ecf20Sopenharmony_ci .ro = GENMASK(5, 4) << 16, 2098c2ecf20Sopenharmony_ci }, 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci [PCI_EXP_LNKCAP / 4] = { 2128c2ecf20Sopenharmony_ci /* 2138c2ecf20Sopenharmony_ci * All bits are RO, except bit 23 which is reserved and 2148c2ecf20Sopenharmony_ci * bit 18 which is reserved for non-upstream ports. 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_ci .ro = lower_32_bits(~(BIT(23) | PCI_EXP_LNKCAP_CLKPM)), 2178c2ecf20Sopenharmony_ci }, 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci [PCI_EXP_LNKCTL / 4] = { 2208c2ecf20Sopenharmony_ci /* 2218c2ecf20Sopenharmony_ci * Link control has bits [15:14], [11:3] and [1:0] RW, the 2228c2ecf20Sopenharmony_ci * rest is reserved. Bit 8 is reserved for non-upstream ports. 2238c2ecf20Sopenharmony_ci * 2248c2ecf20Sopenharmony_ci * Link status has bits [13:0] RO, and bits [15:14] 2258c2ecf20Sopenharmony_ci * W1C. 2268c2ecf20Sopenharmony_ci */ 2278c2ecf20Sopenharmony_ci .rw = GENMASK(15, 14) | GENMASK(11, 9) | GENMASK(7, 3) | GENMASK(1, 0), 2288c2ecf20Sopenharmony_ci .ro = GENMASK(13, 0) << 16, 2298c2ecf20Sopenharmony_ci .w1c = GENMASK(15, 14) << 16, 2308c2ecf20Sopenharmony_ci }, 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci [PCI_EXP_SLTCAP / 4] = { 2338c2ecf20Sopenharmony_ci .ro = ~0, 2348c2ecf20Sopenharmony_ci }, 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci [PCI_EXP_SLTCTL / 4] = { 2378c2ecf20Sopenharmony_ci /* 2388c2ecf20Sopenharmony_ci * Slot control has bits [14:0] RW, the rest is 2398c2ecf20Sopenharmony_ci * reserved. 2408c2ecf20Sopenharmony_ci * 2418c2ecf20Sopenharmony_ci * Slot status has bits 8 and [4:0] W1C, bits [7:5] RO, the 2428c2ecf20Sopenharmony_ci * rest is reserved. 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_ci .rw = GENMASK(14, 0), 2458c2ecf20Sopenharmony_ci .w1c = (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD | 2468c2ecf20Sopenharmony_ci PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC | 2478c2ecf20Sopenharmony_ci PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC) << 16, 2488c2ecf20Sopenharmony_ci .ro = (PCI_EXP_SLTSTA_MRLSS | PCI_EXP_SLTSTA_PDS | 2498c2ecf20Sopenharmony_ci PCI_EXP_SLTSTA_EIS) << 16, 2508c2ecf20Sopenharmony_ci }, 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci [PCI_EXP_RTCTL / 4] = { 2538c2ecf20Sopenharmony_ci /* 2548c2ecf20Sopenharmony_ci * Root control has bits [4:0] RW, the rest is 2558c2ecf20Sopenharmony_ci * reserved. 2568c2ecf20Sopenharmony_ci * 2578c2ecf20Sopenharmony_ci * Root capabilities has bit 0 RO, the rest is reserved. 2588c2ecf20Sopenharmony_ci */ 2598c2ecf20Sopenharmony_ci .rw = (PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE | 2608c2ecf20Sopenharmony_ci PCI_EXP_RTCTL_SEFEE | PCI_EXP_RTCTL_PMEIE | 2618c2ecf20Sopenharmony_ci PCI_EXP_RTCTL_CRSSVE), 2628c2ecf20Sopenharmony_ci .ro = PCI_EXP_RTCAP_CRSVIS << 16, 2638c2ecf20Sopenharmony_ci }, 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci [PCI_EXP_RTSTA / 4] = { 2668c2ecf20Sopenharmony_ci /* 2678c2ecf20Sopenharmony_ci * Root status has bits 17 and [15:0] RO, bit 16 W1C, the rest 2688c2ecf20Sopenharmony_ci * is reserved. 2698c2ecf20Sopenharmony_ci */ 2708c2ecf20Sopenharmony_ci .ro = GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING, 2718c2ecf20Sopenharmony_ci .w1c = PCI_EXP_RTSTA_PME, 2728c2ecf20Sopenharmony_ci }, 2738c2ecf20Sopenharmony_ci}; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci/* 2768c2ecf20Sopenharmony_ci * Initialize a pci_bridge_emul structure to represent a fake PCI 2778c2ecf20Sopenharmony_ci * bridge configuration space. The caller needs to have initialized 2788c2ecf20Sopenharmony_ci * the PCI configuration space with whatever values make sense 2798c2ecf20Sopenharmony_ci * (typically at least vendor, device, revision), the ->ops pointer, 2808c2ecf20Sopenharmony_ci * and optionally ->data and ->has_pcie. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_ciint pci_bridge_emul_init(struct pci_bridge_emul *bridge, 2838c2ecf20Sopenharmony_ci unsigned int flags) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END); 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci bridge->conf.class_revision |= cpu_to_le32(PCI_CLASS_BRIDGE_PCI << 16); 2888c2ecf20Sopenharmony_ci bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE; 2898c2ecf20Sopenharmony_ci bridge->conf.cache_line_size = 0x10; 2908c2ecf20Sopenharmony_ci bridge->conf.status = cpu_to_le16(PCI_STATUS_CAP_LIST); 2918c2ecf20Sopenharmony_ci bridge->pci_regs_behavior = kmemdup(pci_regs_behavior, 2928c2ecf20Sopenharmony_ci sizeof(pci_regs_behavior), 2938c2ecf20Sopenharmony_ci GFP_KERNEL); 2948c2ecf20Sopenharmony_ci if (!bridge->pci_regs_behavior) 2958c2ecf20Sopenharmony_ci return -ENOMEM; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci if (bridge->has_pcie) { 2988c2ecf20Sopenharmony_ci bridge->conf.capabilities_pointer = PCI_CAP_PCIE_START; 2998c2ecf20Sopenharmony_ci bridge->conf.status |= cpu_to_le16(PCI_STATUS_CAP_LIST); 3008c2ecf20Sopenharmony_ci bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP; 3018c2ecf20Sopenharmony_ci bridge->pcie_conf.cap |= cpu_to_le16(PCI_EXP_TYPE_ROOT_PORT << 4); 3028c2ecf20Sopenharmony_ci bridge->pcie_cap_regs_behavior = 3038c2ecf20Sopenharmony_ci kmemdup(pcie_cap_regs_behavior, 3048c2ecf20Sopenharmony_ci sizeof(pcie_cap_regs_behavior), 3058c2ecf20Sopenharmony_ci GFP_KERNEL); 3068c2ecf20Sopenharmony_ci if (!bridge->pcie_cap_regs_behavior) { 3078c2ecf20Sopenharmony_ci kfree(bridge->pci_regs_behavior); 3088c2ecf20Sopenharmony_ci return -ENOMEM; 3098c2ecf20Sopenharmony_ci } 3108c2ecf20Sopenharmony_ci /* These bits are applicable only for PCI and reserved on PCIe */ 3118c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_CACHE_LINE_SIZE / 4].ro &= 3128c2ecf20Sopenharmony_ci ~GENMASK(15, 8); 3138c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_COMMAND / 4].ro &= 3148c2ecf20Sopenharmony_ci ~((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE | 3158c2ecf20Sopenharmony_ci PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT | 3168c2ecf20Sopenharmony_ci PCI_COMMAND_FAST_BACK) | 3178c2ecf20Sopenharmony_ci (PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 3188c2ecf20Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16); 3198c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_PRIMARY_BUS / 4].ro &= 3208c2ecf20Sopenharmony_ci ~GENMASK(31, 24); 3218c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE / 4].ro &= 3228c2ecf20Sopenharmony_ci ~((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 3238c2ecf20Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16); 3248c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].rw &= 3258c2ecf20Sopenharmony_ci ~((PCI_BRIDGE_CTL_MASTER_ABORT | 3268c2ecf20Sopenharmony_ci BIT(8) | BIT(9) | BIT(11)) << 16); 3278c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].ro &= 3288c2ecf20Sopenharmony_ci ~((PCI_BRIDGE_CTL_FAST_BACK) << 16); 3298c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].w1c &= 3308c2ecf20Sopenharmony_ci ~(BIT(10) << 16); 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci if (flags & PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR) { 3348c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].ro = ~0; 3358c2ecf20Sopenharmony_ci bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].rw = 0; 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci return 0; 3398c2ecf20Sopenharmony_ci} 3408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_init); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci/* 3438c2ecf20Sopenharmony_ci * Cleanup a pci_bridge_emul structure that was previously initialized 3448c2ecf20Sopenharmony_ci * using pci_bridge_emul_init(). 3458c2ecf20Sopenharmony_ci */ 3468c2ecf20Sopenharmony_civoid pci_bridge_emul_cleanup(struct pci_bridge_emul *bridge) 3478c2ecf20Sopenharmony_ci{ 3488c2ecf20Sopenharmony_ci if (bridge->has_pcie) 3498c2ecf20Sopenharmony_ci kfree(bridge->pcie_cap_regs_behavior); 3508c2ecf20Sopenharmony_ci kfree(bridge->pci_regs_behavior); 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_cleanup); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci/* 3558c2ecf20Sopenharmony_ci * Should be called by the PCI controller driver when reading the PCI 3568c2ecf20Sopenharmony_ci * configuration space of the fake bridge. It will call back the 3578c2ecf20Sopenharmony_ci * ->ops->read_base or ->ops->read_pcie operations. 3588c2ecf20Sopenharmony_ci */ 3598c2ecf20Sopenharmony_ciint pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where, 3608c2ecf20Sopenharmony_ci int size, u32 *value) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci int ret; 3638c2ecf20Sopenharmony_ci int reg = where & ~3; 3648c2ecf20Sopenharmony_ci pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge, 3658c2ecf20Sopenharmony_ci int reg, u32 *value); 3668c2ecf20Sopenharmony_ci __le32 *cfgspace; 3678c2ecf20Sopenharmony_ci const struct pci_bridge_reg_behavior *behavior; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END) { 3708c2ecf20Sopenharmony_ci *value = 0; 3718c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 3728c2ecf20Sopenharmony_ci } 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END) { 3758c2ecf20Sopenharmony_ci *value = 0; 3768c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) { 3808c2ecf20Sopenharmony_ci reg -= PCI_CAP_PCIE_START; 3818c2ecf20Sopenharmony_ci read_op = bridge->ops->read_pcie; 3828c2ecf20Sopenharmony_ci cfgspace = (__le32 *) &bridge->pcie_conf; 3838c2ecf20Sopenharmony_ci behavior = bridge->pcie_cap_regs_behavior; 3848c2ecf20Sopenharmony_ci } else { 3858c2ecf20Sopenharmony_ci read_op = bridge->ops->read_base; 3868c2ecf20Sopenharmony_ci cfgspace = (__le32 *) &bridge->conf; 3878c2ecf20Sopenharmony_ci behavior = bridge->pci_regs_behavior; 3888c2ecf20Sopenharmony_ci } 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci if (read_op) 3918c2ecf20Sopenharmony_ci ret = read_op(bridge, reg, value); 3928c2ecf20Sopenharmony_ci else 3938c2ecf20Sopenharmony_ci ret = PCI_BRIDGE_EMUL_NOT_HANDLED; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED) 3968c2ecf20Sopenharmony_ci *value = le32_to_cpu(cfgspace[reg / 4]); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* 3998c2ecf20Sopenharmony_ci * Make sure we never return any reserved bit with a value 4008c2ecf20Sopenharmony_ci * different from 0. 4018c2ecf20Sopenharmony_ci */ 4028c2ecf20Sopenharmony_ci *value &= behavior[reg / 4].ro | behavior[reg / 4].rw | 4038c2ecf20Sopenharmony_ci behavior[reg / 4].w1c; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci if (size == 1) 4068c2ecf20Sopenharmony_ci *value = (*value >> (8 * (where & 3))) & 0xff; 4078c2ecf20Sopenharmony_ci else if (size == 2) 4088c2ecf20Sopenharmony_ci *value = (*value >> (8 * (where & 3))) & 0xffff; 4098c2ecf20Sopenharmony_ci else if (size != 4) 4108c2ecf20Sopenharmony_ci return PCIBIOS_BAD_REGISTER_NUMBER; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_conf_read); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci/* 4178c2ecf20Sopenharmony_ci * Should be called by the PCI controller driver when writing the PCI 4188c2ecf20Sopenharmony_ci * configuration space of the fake bridge. It will call back the 4198c2ecf20Sopenharmony_ci * ->ops->write_base or ->ops->write_pcie operations. 4208c2ecf20Sopenharmony_ci */ 4218c2ecf20Sopenharmony_ciint pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where, 4228c2ecf20Sopenharmony_ci int size, u32 value) 4238c2ecf20Sopenharmony_ci{ 4248c2ecf20Sopenharmony_ci int reg = where & ~3; 4258c2ecf20Sopenharmony_ci int mask, ret, old, new, shift; 4268c2ecf20Sopenharmony_ci void (*write_op)(struct pci_bridge_emul *bridge, int reg, 4278c2ecf20Sopenharmony_ci u32 old, u32 new, u32 mask); 4288c2ecf20Sopenharmony_ci __le32 *cfgspace; 4298c2ecf20Sopenharmony_ci const struct pci_bridge_reg_behavior *behavior; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci if (bridge->has_pcie && reg >= PCI_CAP_PCIE_END) 4328c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (!bridge->has_pcie && reg >= PCI_BRIDGE_CONF_END) 4358c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci shift = (where & 0x3) * 8; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci if (size == 4) 4408c2ecf20Sopenharmony_ci mask = 0xffffffff; 4418c2ecf20Sopenharmony_ci else if (size == 2) 4428c2ecf20Sopenharmony_ci mask = 0xffff << shift; 4438c2ecf20Sopenharmony_ci else if (size == 1) 4448c2ecf20Sopenharmony_ci mask = 0xff << shift; 4458c2ecf20Sopenharmony_ci else 4468c2ecf20Sopenharmony_ci return PCIBIOS_BAD_REGISTER_NUMBER; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci ret = pci_bridge_emul_conf_read(bridge, reg, 4, &old); 4498c2ecf20Sopenharmony_ci if (ret != PCIBIOS_SUCCESSFUL) 4508c2ecf20Sopenharmony_ci return ret; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci if (bridge->has_pcie && reg >= PCI_CAP_PCIE_START) { 4538c2ecf20Sopenharmony_ci reg -= PCI_CAP_PCIE_START; 4548c2ecf20Sopenharmony_ci write_op = bridge->ops->write_pcie; 4558c2ecf20Sopenharmony_ci cfgspace = (__le32 *) &bridge->pcie_conf; 4568c2ecf20Sopenharmony_ci behavior = bridge->pcie_cap_regs_behavior; 4578c2ecf20Sopenharmony_ci } else { 4588c2ecf20Sopenharmony_ci write_op = bridge->ops->write_base; 4598c2ecf20Sopenharmony_ci cfgspace = (__le32 *) &bridge->conf; 4608c2ecf20Sopenharmony_ci behavior = bridge->pci_regs_behavior; 4618c2ecf20Sopenharmony_ci } 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci /* Keep all bits, except the RW bits */ 4648c2ecf20Sopenharmony_ci new = old & (~mask | ~behavior[reg / 4].rw); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci /* Update the value of the RW bits */ 4678c2ecf20Sopenharmony_ci new |= (value << shift) & (behavior[reg / 4].rw & mask); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci /* Clear the W1C bits */ 4708c2ecf20Sopenharmony_ci new &= ~((value << shift) & (behavior[reg / 4].w1c & mask)); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci /* Save the new value with the cleared W1C bits into the cfgspace */ 4738c2ecf20Sopenharmony_ci cfgspace[reg / 4] = cpu_to_le32(new); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci /* 4768c2ecf20Sopenharmony_ci * Clear the W1C bits not specified by the write mask, so that the 4778c2ecf20Sopenharmony_ci * write_op() does not clear them. 4788c2ecf20Sopenharmony_ci */ 4798c2ecf20Sopenharmony_ci new &= ~(behavior[reg / 4].w1c & ~mask); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci /* 4828c2ecf20Sopenharmony_ci * Set the W1C bits specified by the write mask, so that write_op() 4838c2ecf20Sopenharmony_ci * knows about that they are to be cleared. 4848c2ecf20Sopenharmony_ci */ 4858c2ecf20Sopenharmony_ci new |= (value << shift) & (behavior[reg / 4].w1c & mask); 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci if (write_op) 4888c2ecf20Sopenharmony_ci write_op(bridge, reg, old, new, mask); 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_conf_write); 493