162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2018 Marvell 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Author: Thomas Petazzoni <thomas.petazzoni@bootlin.com> 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * This file helps PCI controller drivers implement a fake root port 862306a36Sopenharmony_ci * PCI bridge when the HW doesn't provide such a root port PCI 962306a36Sopenharmony_ci * bridge. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * It emulates a PCI bridge by providing a fake PCI configuration 1262306a36Sopenharmony_ci * space (and optionally a PCIe capability configuration space) in 1362306a36Sopenharmony_ci * memory. By default the read/write operations simply read and update 1462306a36Sopenharmony_ci * this fake configuration space in memory. However, PCI controller 1562306a36Sopenharmony_ci * drivers can provide through the 'struct pci_sw_bridge_ops' 1662306a36Sopenharmony_ci * structure a set of operations to override or complement this 1762306a36Sopenharmony_ci * default behavior. 1862306a36Sopenharmony_ci */ 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#include <linux/pci.h> 2162306a36Sopenharmony_ci#include "pci-bridge-emul.h" 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#define PCI_BRIDGE_CONF_END PCI_STD_HEADER_SIZEOF 2462306a36Sopenharmony_ci#define PCI_CAP_SSID_SIZEOF (PCI_SSVID_DEVICE_ID + 2) 2562306a36Sopenharmony_ci#define PCI_CAP_PCIE_SIZEOF (PCI_EXP_SLTSTA2 + 2) 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci/** 2862306a36Sopenharmony_ci * struct pci_bridge_reg_behavior - register bits behaviors 2962306a36Sopenharmony_ci * @ro: Read-Only bits 3062306a36Sopenharmony_ci * @rw: Read-Write bits 3162306a36Sopenharmony_ci * @w1c: Write-1-to-Clear bits 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * Reads and Writes will be filtered by specified behavior. All other bits not 3462306a36Sopenharmony_ci * declared are assumed 'Reserved' and will return 0 on reads, per PCIe 5.0: 3562306a36Sopenharmony_ci * "Reserved register fields must be read only and must return 0 (all 0's for 3662306a36Sopenharmony_ci * multi-bit fields) when read". 3762306a36Sopenharmony_ci */ 3862306a36Sopenharmony_cistruct pci_bridge_reg_behavior { 3962306a36Sopenharmony_ci /* Read-only bits */ 4062306a36Sopenharmony_ci u32 ro; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci /* Read-write bits */ 4362306a36Sopenharmony_ci u32 rw; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci /* Write-1-to-clear bits */ 4662306a36Sopenharmony_ci u32 w1c; 4762306a36Sopenharmony_ci}; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic const 5062306a36Sopenharmony_cistruct pci_bridge_reg_behavior pci_regs_behavior[PCI_STD_HEADER_SIZEOF / 4] = { 5162306a36Sopenharmony_ci [PCI_VENDOR_ID / 4] = { .ro = ~0 }, 5262306a36Sopenharmony_ci [PCI_COMMAND / 4] = { 5362306a36Sopenharmony_ci .rw = (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | 5462306a36Sopenharmony_ci PCI_COMMAND_MASTER | PCI_COMMAND_PARITY | 5562306a36Sopenharmony_ci PCI_COMMAND_SERR), 5662306a36Sopenharmony_ci .ro = ((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE | 5762306a36Sopenharmony_ci PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT | 5862306a36Sopenharmony_ci PCI_COMMAND_FAST_BACK) | 5962306a36Sopenharmony_ci (PCI_STATUS_CAP_LIST | PCI_STATUS_66MHZ | 6062306a36Sopenharmony_ci PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MASK) << 16), 6162306a36Sopenharmony_ci .w1c = PCI_STATUS_ERROR_BITS << 16, 6262306a36Sopenharmony_ci }, 6362306a36Sopenharmony_ci [PCI_CLASS_REVISION / 4] = { .ro = ~0 }, 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci /* 6662306a36Sopenharmony_ci * Cache Line Size register: implement as read-only, we do not 6762306a36Sopenharmony_ci * pretend implementing "Memory Write and Invalidate" 6862306a36Sopenharmony_ci * transactions" 6962306a36Sopenharmony_ci * 7062306a36Sopenharmony_ci * Latency Timer Register: implemented as read-only, as "A 7162306a36Sopenharmony_ci * bridge that is not capable of a burst transfer of more than 7262306a36Sopenharmony_ci * two data phases on its primary interface is permitted to 7362306a36Sopenharmony_ci * hardwire the Latency Timer to a value of 16 or less" 7462306a36Sopenharmony_ci * 7562306a36Sopenharmony_ci * Header Type: always read-only 7662306a36Sopenharmony_ci * 7762306a36Sopenharmony_ci * BIST register: implemented as read-only, as "A bridge that 7862306a36Sopenharmony_ci * does not support BIST must implement this register as a 7962306a36Sopenharmony_ci * read-only register that returns 0 when read" 8062306a36Sopenharmony_ci */ 8162306a36Sopenharmony_ci [PCI_CACHE_LINE_SIZE / 4] = { .ro = ~0 }, 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci /* 8462306a36Sopenharmony_ci * Base Address registers not used must be implemented as 8562306a36Sopenharmony_ci * read-only registers that return 0 when read. 8662306a36Sopenharmony_ci */ 8762306a36Sopenharmony_ci [PCI_BASE_ADDRESS_0 / 4] = { .ro = ~0 }, 8862306a36Sopenharmony_ci [PCI_BASE_ADDRESS_1 / 4] = { .ro = ~0 }, 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci [PCI_PRIMARY_BUS / 4] = { 9162306a36Sopenharmony_ci /* Primary, secondary and subordinate bus are RW */ 9262306a36Sopenharmony_ci .rw = GENMASK(24, 0), 9362306a36Sopenharmony_ci /* Secondary latency is read-only */ 9462306a36Sopenharmony_ci .ro = GENMASK(31, 24), 9562306a36Sopenharmony_ci }, 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci [PCI_IO_BASE / 4] = { 9862306a36Sopenharmony_ci /* The high four bits of I/O base/limit are RW */ 9962306a36Sopenharmony_ci .rw = (GENMASK(15, 12) | GENMASK(7, 4)), 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci /* The low four bits of I/O base/limit are RO */ 10262306a36Sopenharmony_ci .ro = (((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 10362306a36Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16) | 10462306a36Sopenharmony_ci GENMASK(11, 8) | GENMASK(3, 0)), 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci .w1c = PCI_STATUS_ERROR_BITS << 16, 10762306a36Sopenharmony_ci }, 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci [PCI_MEMORY_BASE / 4] = { 11062306a36Sopenharmony_ci /* The high 12-bits of mem base/limit are RW */ 11162306a36Sopenharmony_ci .rw = GENMASK(31, 20) | GENMASK(15, 4), 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci /* The low four bits of mem base/limit are RO */ 11462306a36Sopenharmony_ci .ro = GENMASK(19, 16) | GENMASK(3, 0), 11562306a36Sopenharmony_ci }, 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci [PCI_PREF_MEMORY_BASE / 4] = { 11862306a36Sopenharmony_ci /* The high 12-bits of pref mem base/limit are RW */ 11962306a36Sopenharmony_ci .rw = GENMASK(31, 20) | GENMASK(15, 4), 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci /* The low four bits of pref mem base/limit are RO */ 12262306a36Sopenharmony_ci .ro = GENMASK(19, 16) | GENMASK(3, 0), 12362306a36Sopenharmony_ci }, 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci [PCI_PREF_BASE_UPPER32 / 4] = { 12662306a36Sopenharmony_ci .rw = ~0, 12762306a36Sopenharmony_ci }, 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci [PCI_PREF_LIMIT_UPPER32 / 4] = { 13062306a36Sopenharmony_ci .rw = ~0, 13162306a36Sopenharmony_ci }, 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci [PCI_IO_BASE_UPPER16 / 4] = { 13462306a36Sopenharmony_ci .rw = ~0, 13562306a36Sopenharmony_ci }, 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci [PCI_CAPABILITY_LIST / 4] = { 13862306a36Sopenharmony_ci .ro = GENMASK(7, 0), 13962306a36Sopenharmony_ci }, 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci /* 14262306a36Sopenharmony_ci * If expansion ROM is unsupported then ROM Base Address register must 14362306a36Sopenharmony_ci * be implemented as read-only register that return 0 when read, same 14462306a36Sopenharmony_ci * as for unused Base Address registers. 14562306a36Sopenharmony_ci */ 14662306a36Sopenharmony_ci [PCI_ROM_ADDRESS1 / 4] = { 14762306a36Sopenharmony_ci .ro = ~0, 14862306a36Sopenharmony_ci }, 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci /* 15162306a36Sopenharmony_ci * Interrupt line (bits 7:0) are RW, interrupt pin (bits 15:8) 15262306a36Sopenharmony_ci * are RO, and bridge control (31:16) are a mix of RW, RO, 15362306a36Sopenharmony_ci * reserved and W1C bits 15462306a36Sopenharmony_ci */ 15562306a36Sopenharmony_ci [PCI_INTERRUPT_LINE / 4] = { 15662306a36Sopenharmony_ci /* Interrupt line is RW */ 15762306a36Sopenharmony_ci .rw = (GENMASK(7, 0) | 15862306a36Sopenharmony_ci ((PCI_BRIDGE_CTL_PARITY | 15962306a36Sopenharmony_ci PCI_BRIDGE_CTL_SERR | 16062306a36Sopenharmony_ci PCI_BRIDGE_CTL_ISA | 16162306a36Sopenharmony_ci PCI_BRIDGE_CTL_VGA | 16262306a36Sopenharmony_ci PCI_BRIDGE_CTL_MASTER_ABORT | 16362306a36Sopenharmony_ci PCI_BRIDGE_CTL_BUS_RESET | 16462306a36Sopenharmony_ci BIT(8) | BIT(9) | BIT(11)) << 16)), 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci /* Interrupt pin is RO */ 16762306a36Sopenharmony_ci .ro = (GENMASK(15, 8) | ((PCI_BRIDGE_CTL_FAST_BACK) << 16)), 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci .w1c = BIT(10) << 16, 17062306a36Sopenharmony_ci }, 17162306a36Sopenharmony_ci}; 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_cistatic const 17462306a36Sopenharmony_cistruct pci_bridge_reg_behavior pcie_cap_regs_behavior[PCI_CAP_PCIE_SIZEOF / 4] = { 17562306a36Sopenharmony_ci [PCI_CAP_LIST_ID / 4] = { 17662306a36Sopenharmony_ci /* 17762306a36Sopenharmony_ci * Capability ID, Next Capability Pointer and 17862306a36Sopenharmony_ci * bits [14:0] of Capabilities register are all read-only. 17962306a36Sopenharmony_ci * Bit 15 of Capabilities register is reserved. 18062306a36Sopenharmony_ci */ 18162306a36Sopenharmony_ci .ro = GENMASK(30, 0), 18262306a36Sopenharmony_ci }, 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci [PCI_EXP_DEVCAP / 4] = { 18562306a36Sopenharmony_ci /* 18662306a36Sopenharmony_ci * Bits [31:29] and [17:16] are reserved. 18762306a36Sopenharmony_ci * Bits [27:18] are reserved for non-upstream ports. 18862306a36Sopenharmony_ci * Bits 28 and [14:6] are reserved for non-endpoint devices. 18962306a36Sopenharmony_ci * Other bits are read-only. 19062306a36Sopenharmony_ci */ 19162306a36Sopenharmony_ci .ro = BIT(15) | GENMASK(5, 0), 19262306a36Sopenharmony_ci }, 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci [PCI_EXP_DEVCTL / 4] = { 19562306a36Sopenharmony_ci /* 19662306a36Sopenharmony_ci * Device control register is RW, except bit 15 which is 19762306a36Sopenharmony_ci * reserved for non-endpoints or non-PCIe-to-PCI/X bridges. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ci .rw = GENMASK(14, 0), 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci /* 20262306a36Sopenharmony_ci * Device status register has bits 6 and [3:0] W1C, [5:4] RO, 20362306a36Sopenharmony_ci * the rest is reserved. Also bit 6 is reserved for non-upstream 20462306a36Sopenharmony_ci * ports. 20562306a36Sopenharmony_ci */ 20662306a36Sopenharmony_ci .w1c = GENMASK(3, 0) << 16, 20762306a36Sopenharmony_ci .ro = GENMASK(5, 4) << 16, 20862306a36Sopenharmony_ci }, 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci [PCI_EXP_LNKCAP / 4] = { 21162306a36Sopenharmony_ci /* 21262306a36Sopenharmony_ci * All bits are RO, except bit 23 which is reserved and 21362306a36Sopenharmony_ci * bit 18 which is reserved for non-upstream ports. 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci .ro = lower_32_bits(~(BIT(23) | PCI_EXP_LNKCAP_CLKPM)), 21662306a36Sopenharmony_ci }, 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci [PCI_EXP_LNKCTL / 4] = { 21962306a36Sopenharmony_ci /* 22062306a36Sopenharmony_ci * Link control has bits [15:14], [11:3] and [1:0] RW, the 22162306a36Sopenharmony_ci * rest is reserved. Bit 8 is reserved for non-upstream ports. 22262306a36Sopenharmony_ci * 22362306a36Sopenharmony_ci * Link status has bits [13:0] RO, and bits [15:14] 22462306a36Sopenharmony_ci * W1C. 22562306a36Sopenharmony_ci */ 22662306a36Sopenharmony_ci .rw = GENMASK(15, 14) | GENMASK(11, 9) | GENMASK(7, 3) | GENMASK(1, 0), 22762306a36Sopenharmony_ci .ro = GENMASK(13, 0) << 16, 22862306a36Sopenharmony_ci .w1c = GENMASK(15, 14) << 16, 22962306a36Sopenharmony_ci }, 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci [PCI_EXP_SLTCAP / 4] = { 23262306a36Sopenharmony_ci .ro = ~0, 23362306a36Sopenharmony_ci }, 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci [PCI_EXP_SLTCTL / 4] = { 23662306a36Sopenharmony_ci /* 23762306a36Sopenharmony_ci * Slot control has bits [14:0] RW, the rest is 23862306a36Sopenharmony_ci * reserved. 23962306a36Sopenharmony_ci * 24062306a36Sopenharmony_ci * Slot status has bits 8 and [4:0] W1C, bits [7:5] RO, the 24162306a36Sopenharmony_ci * rest is reserved. 24262306a36Sopenharmony_ci */ 24362306a36Sopenharmony_ci .rw = GENMASK(14, 0), 24462306a36Sopenharmony_ci .w1c = (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD | 24562306a36Sopenharmony_ci PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_PDC | 24662306a36Sopenharmony_ci PCI_EXP_SLTSTA_CC | PCI_EXP_SLTSTA_DLLSC) << 16, 24762306a36Sopenharmony_ci .ro = (PCI_EXP_SLTSTA_MRLSS | PCI_EXP_SLTSTA_PDS | 24862306a36Sopenharmony_ci PCI_EXP_SLTSTA_EIS) << 16, 24962306a36Sopenharmony_ci }, 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci [PCI_EXP_RTCTL / 4] = { 25262306a36Sopenharmony_ci /* 25362306a36Sopenharmony_ci * Root control has bits [4:0] RW, the rest is 25462306a36Sopenharmony_ci * reserved. 25562306a36Sopenharmony_ci * 25662306a36Sopenharmony_ci * Root capabilities has bit 0 RO, the rest is reserved. 25762306a36Sopenharmony_ci */ 25862306a36Sopenharmony_ci .rw = (PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE | 25962306a36Sopenharmony_ci PCI_EXP_RTCTL_SEFEE | PCI_EXP_RTCTL_PMEIE | 26062306a36Sopenharmony_ci PCI_EXP_RTCTL_CRSSVE), 26162306a36Sopenharmony_ci .ro = PCI_EXP_RTCAP_CRSVIS << 16, 26262306a36Sopenharmony_ci }, 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci [PCI_EXP_RTSTA / 4] = { 26562306a36Sopenharmony_ci /* 26662306a36Sopenharmony_ci * Root status has bits 17 and [15:0] RO, bit 16 W1C, the rest 26762306a36Sopenharmony_ci * is reserved. 26862306a36Sopenharmony_ci */ 26962306a36Sopenharmony_ci .ro = GENMASK(15, 0) | PCI_EXP_RTSTA_PENDING, 27062306a36Sopenharmony_ci .w1c = PCI_EXP_RTSTA_PME, 27162306a36Sopenharmony_ci }, 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci [PCI_EXP_DEVCAP2 / 4] = { 27462306a36Sopenharmony_ci /* 27562306a36Sopenharmony_ci * Device capabilities 2 register has reserved bits [30:27]. 27662306a36Sopenharmony_ci * Also bits [26:24] are reserved for non-upstream ports. 27762306a36Sopenharmony_ci */ 27862306a36Sopenharmony_ci .ro = BIT(31) | GENMASK(23, 0), 27962306a36Sopenharmony_ci }, 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci [PCI_EXP_DEVCTL2 / 4] = { 28262306a36Sopenharmony_ci /* 28362306a36Sopenharmony_ci * Device control 2 register is RW. Bit 11 is reserved for 28462306a36Sopenharmony_ci * non-upstream ports. 28562306a36Sopenharmony_ci * 28662306a36Sopenharmony_ci * Device status 2 register is reserved. 28762306a36Sopenharmony_ci */ 28862306a36Sopenharmony_ci .rw = GENMASK(15, 12) | GENMASK(10, 0), 28962306a36Sopenharmony_ci }, 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci [PCI_EXP_LNKCAP2 / 4] = { 29262306a36Sopenharmony_ci /* Link capabilities 2 register has reserved bits [30:25] and 0. */ 29362306a36Sopenharmony_ci .ro = BIT(31) | GENMASK(24, 1), 29462306a36Sopenharmony_ci }, 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci [PCI_EXP_LNKCTL2 / 4] = { 29762306a36Sopenharmony_ci /* 29862306a36Sopenharmony_ci * Link control 2 register is RW. 29962306a36Sopenharmony_ci * 30062306a36Sopenharmony_ci * Link status 2 register has bits 5, 15 W1C; 30162306a36Sopenharmony_ci * bits 10, 11 reserved and others are RO. 30262306a36Sopenharmony_ci */ 30362306a36Sopenharmony_ci .rw = GENMASK(15, 0), 30462306a36Sopenharmony_ci .w1c = (BIT(15) | BIT(5)) << 16, 30562306a36Sopenharmony_ci .ro = (GENMASK(14, 12) | GENMASK(9, 6) | GENMASK(4, 0)) << 16, 30662306a36Sopenharmony_ci }, 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci [PCI_EXP_SLTCAP2 / 4] = { 30962306a36Sopenharmony_ci /* Slot capabilities 2 register is reserved. */ 31062306a36Sopenharmony_ci }, 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci [PCI_EXP_SLTCTL2 / 4] = { 31362306a36Sopenharmony_ci /* Both Slot control 2 and Slot status 2 registers are reserved. */ 31462306a36Sopenharmony_ci }, 31562306a36Sopenharmony_ci}; 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_cistatic pci_bridge_emul_read_status_t 31862306a36Sopenharmony_cipci_bridge_emul_read_ssid(struct pci_bridge_emul *bridge, int reg, u32 *value) 31962306a36Sopenharmony_ci{ 32062306a36Sopenharmony_ci switch (reg) { 32162306a36Sopenharmony_ci case PCI_CAP_LIST_ID: 32262306a36Sopenharmony_ci *value = PCI_CAP_ID_SSVID | 32362306a36Sopenharmony_ci ((bridge->pcie_start > bridge->ssid_start) ? (bridge->pcie_start << 8) : 0); 32462306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci case PCI_SSVID_VENDOR_ID: 32762306a36Sopenharmony_ci *value = bridge->subsystem_vendor_id | 32862306a36Sopenharmony_ci (bridge->subsystem_id << 16); 32962306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci default: 33262306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 33362306a36Sopenharmony_ci } 33462306a36Sopenharmony_ci} 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci/* 33762306a36Sopenharmony_ci * Initialize a pci_bridge_emul structure to represent a fake PCI 33862306a36Sopenharmony_ci * bridge configuration space. The caller needs to have initialized 33962306a36Sopenharmony_ci * the PCI configuration space with whatever values make sense 34062306a36Sopenharmony_ci * (typically at least vendor, device, revision), the ->ops pointer, 34162306a36Sopenharmony_ci * and optionally ->data and ->has_pcie. 34262306a36Sopenharmony_ci */ 34362306a36Sopenharmony_ciint pci_bridge_emul_init(struct pci_bridge_emul *bridge, 34462306a36Sopenharmony_ci unsigned int flags) 34562306a36Sopenharmony_ci{ 34662306a36Sopenharmony_ci BUILD_BUG_ON(sizeof(bridge->conf) != PCI_BRIDGE_CONF_END); 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci /* 34962306a36Sopenharmony_ci * class_revision: Class is high 24 bits and revision is low 8 bit 35062306a36Sopenharmony_ci * of this member, while class for PCI Bridge Normal Decode has the 35162306a36Sopenharmony_ci * 24-bit value: PCI_CLASS_BRIDGE_PCI_NORMAL 35262306a36Sopenharmony_ci */ 35362306a36Sopenharmony_ci bridge->conf.class_revision |= 35462306a36Sopenharmony_ci cpu_to_le32(PCI_CLASS_BRIDGE_PCI_NORMAL << 8); 35562306a36Sopenharmony_ci bridge->conf.header_type = PCI_HEADER_TYPE_BRIDGE; 35662306a36Sopenharmony_ci bridge->conf.cache_line_size = 0x10; 35762306a36Sopenharmony_ci bridge->conf.status = cpu_to_le16(PCI_STATUS_CAP_LIST); 35862306a36Sopenharmony_ci bridge->pci_regs_behavior = kmemdup(pci_regs_behavior, 35962306a36Sopenharmony_ci sizeof(pci_regs_behavior), 36062306a36Sopenharmony_ci GFP_KERNEL); 36162306a36Sopenharmony_ci if (!bridge->pci_regs_behavior) 36262306a36Sopenharmony_ci return -ENOMEM; 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci /* If ssid_start and pcie_start were not specified then choose the lowest possible value. */ 36562306a36Sopenharmony_ci if (!bridge->ssid_start && !bridge->pcie_start) { 36662306a36Sopenharmony_ci if (bridge->subsystem_vendor_id) 36762306a36Sopenharmony_ci bridge->ssid_start = PCI_BRIDGE_CONF_END; 36862306a36Sopenharmony_ci if (bridge->has_pcie) 36962306a36Sopenharmony_ci bridge->pcie_start = bridge->ssid_start + PCI_CAP_SSID_SIZEOF; 37062306a36Sopenharmony_ci } else if (!bridge->ssid_start && bridge->subsystem_vendor_id) { 37162306a36Sopenharmony_ci if (bridge->pcie_start - PCI_BRIDGE_CONF_END >= PCI_CAP_SSID_SIZEOF) 37262306a36Sopenharmony_ci bridge->ssid_start = PCI_BRIDGE_CONF_END; 37362306a36Sopenharmony_ci else 37462306a36Sopenharmony_ci bridge->ssid_start = bridge->pcie_start + PCI_CAP_PCIE_SIZEOF; 37562306a36Sopenharmony_ci } else if (!bridge->pcie_start && bridge->has_pcie) { 37662306a36Sopenharmony_ci if (bridge->ssid_start - PCI_BRIDGE_CONF_END >= PCI_CAP_PCIE_SIZEOF) 37762306a36Sopenharmony_ci bridge->pcie_start = PCI_BRIDGE_CONF_END; 37862306a36Sopenharmony_ci else 37962306a36Sopenharmony_ci bridge->pcie_start = bridge->ssid_start + PCI_CAP_SSID_SIZEOF; 38062306a36Sopenharmony_ci } 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci bridge->conf.capabilities_pointer = min(bridge->ssid_start, bridge->pcie_start); 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci if (bridge->conf.capabilities_pointer) 38562306a36Sopenharmony_ci bridge->conf.status |= cpu_to_le16(PCI_STATUS_CAP_LIST); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci if (bridge->has_pcie) { 38862306a36Sopenharmony_ci bridge->pcie_conf.cap_id = PCI_CAP_ID_EXP; 38962306a36Sopenharmony_ci bridge->pcie_conf.next = (bridge->ssid_start > bridge->pcie_start) ? 39062306a36Sopenharmony_ci bridge->ssid_start : 0; 39162306a36Sopenharmony_ci bridge->pcie_conf.cap |= cpu_to_le16(PCI_EXP_TYPE_ROOT_PORT << 4); 39262306a36Sopenharmony_ci bridge->pcie_cap_regs_behavior = 39362306a36Sopenharmony_ci kmemdup(pcie_cap_regs_behavior, 39462306a36Sopenharmony_ci sizeof(pcie_cap_regs_behavior), 39562306a36Sopenharmony_ci GFP_KERNEL); 39662306a36Sopenharmony_ci if (!bridge->pcie_cap_regs_behavior) { 39762306a36Sopenharmony_ci kfree(bridge->pci_regs_behavior); 39862306a36Sopenharmony_ci return -ENOMEM; 39962306a36Sopenharmony_ci } 40062306a36Sopenharmony_ci /* These bits are applicable only for PCI and reserved on PCIe */ 40162306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_CACHE_LINE_SIZE / 4].ro &= 40262306a36Sopenharmony_ci ~GENMASK(15, 8); 40362306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_COMMAND / 4].ro &= 40462306a36Sopenharmony_ci ~((PCI_COMMAND_SPECIAL | PCI_COMMAND_INVALIDATE | 40562306a36Sopenharmony_ci PCI_COMMAND_VGA_PALETTE | PCI_COMMAND_WAIT | 40662306a36Sopenharmony_ci PCI_COMMAND_FAST_BACK) | 40762306a36Sopenharmony_ci (PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 40862306a36Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16); 40962306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_PRIMARY_BUS / 4].ro &= 41062306a36Sopenharmony_ci ~GENMASK(31, 24); 41162306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE / 4].ro &= 41262306a36Sopenharmony_ci ~((PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK | 41362306a36Sopenharmony_ci PCI_STATUS_DEVSEL_MASK) << 16); 41462306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].rw &= 41562306a36Sopenharmony_ci ~((PCI_BRIDGE_CTL_MASTER_ABORT | 41662306a36Sopenharmony_ci BIT(8) | BIT(9) | BIT(11)) << 16); 41762306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].ro &= 41862306a36Sopenharmony_ci ~((PCI_BRIDGE_CTL_FAST_BACK) << 16); 41962306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_INTERRUPT_LINE / 4].w1c &= 42062306a36Sopenharmony_ci ~(BIT(10) << 16); 42162306a36Sopenharmony_ci } 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci if (flags & PCI_BRIDGE_EMUL_NO_PREFMEM_FORWARD) { 42462306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].ro = ~0; 42562306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_PREF_MEMORY_BASE / 4].rw = 0; 42662306a36Sopenharmony_ci } 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci if (flags & PCI_BRIDGE_EMUL_NO_IO_FORWARD) { 42962306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_COMMAND / 4].ro |= PCI_COMMAND_IO; 43062306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_COMMAND / 4].rw &= ~PCI_COMMAND_IO; 43162306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE / 4].ro |= GENMASK(15, 0); 43262306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE / 4].rw &= ~GENMASK(15, 0); 43362306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE_UPPER16 / 4].ro = ~0; 43462306a36Sopenharmony_ci bridge->pci_regs_behavior[PCI_IO_BASE_UPPER16 / 4].rw = 0; 43562306a36Sopenharmony_ci } 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci return 0; 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_init); 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci/* 44262306a36Sopenharmony_ci * Cleanup a pci_bridge_emul structure that was previously initialized 44362306a36Sopenharmony_ci * using pci_bridge_emul_init(). 44462306a36Sopenharmony_ci */ 44562306a36Sopenharmony_civoid pci_bridge_emul_cleanup(struct pci_bridge_emul *bridge) 44662306a36Sopenharmony_ci{ 44762306a36Sopenharmony_ci if (bridge->has_pcie) 44862306a36Sopenharmony_ci kfree(bridge->pcie_cap_regs_behavior); 44962306a36Sopenharmony_ci kfree(bridge->pci_regs_behavior); 45062306a36Sopenharmony_ci} 45162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_cleanup); 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ci/* 45462306a36Sopenharmony_ci * Should be called by the PCI controller driver when reading the PCI 45562306a36Sopenharmony_ci * configuration space of the fake bridge. It will call back the 45662306a36Sopenharmony_ci * ->ops->read_base or ->ops->read_pcie operations. 45762306a36Sopenharmony_ci */ 45862306a36Sopenharmony_ciint pci_bridge_emul_conf_read(struct pci_bridge_emul *bridge, int where, 45962306a36Sopenharmony_ci int size, u32 *value) 46062306a36Sopenharmony_ci{ 46162306a36Sopenharmony_ci int ret; 46262306a36Sopenharmony_ci int reg = where & ~3; 46362306a36Sopenharmony_ci pci_bridge_emul_read_status_t (*read_op)(struct pci_bridge_emul *bridge, 46462306a36Sopenharmony_ci int reg, u32 *value); 46562306a36Sopenharmony_ci __le32 *cfgspace; 46662306a36Sopenharmony_ci const struct pci_bridge_reg_behavior *behavior; 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci if (reg < PCI_BRIDGE_CONF_END) { 46962306a36Sopenharmony_ci /* Emulated PCI space */ 47062306a36Sopenharmony_ci read_op = bridge->ops->read_base; 47162306a36Sopenharmony_ci cfgspace = (__le32 *) &bridge->conf; 47262306a36Sopenharmony_ci behavior = bridge->pci_regs_behavior; 47362306a36Sopenharmony_ci } else if (reg >= bridge->ssid_start && reg < bridge->ssid_start + PCI_CAP_SSID_SIZEOF && 47462306a36Sopenharmony_ci bridge->subsystem_vendor_id) { 47562306a36Sopenharmony_ci /* Emulated PCI Bridge Subsystem Vendor ID capability */ 47662306a36Sopenharmony_ci reg -= bridge->ssid_start; 47762306a36Sopenharmony_ci read_op = pci_bridge_emul_read_ssid; 47862306a36Sopenharmony_ci cfgspace = NULL; 47962306a36Sopenharmony_ci behavior = NULL; 48062306a36Sopenharmony_ci } else if (reg >= bridge->pcie_start && reg < bridge->pcie_start + PCI_CAP_PCIE_SIZEOF && 48162306a36Sopenharmony_ci bridge->has_pcie) { 48262306a36Sopenharmony_ci /* Our emulated PCIe capability */ 48362306a36Sopenharmony_ci reg -= bridge->pcie_start; 48462306a36Sopenharmony_ci read_op = bridge->ops->read_pcie; 48562306a36Sopenharmony_ci cfgspace = (__le32 *) &bridge->pcie_conf; 48662306a36Sopenharmony_ci behavior = bridge->pcie_cap_regs_behavior; 48762306a36Sopenharmony_ci } else if (reg >= PCI_CFG_SPACE_SIZE && bridge->has_pcie) { 48862306a36Sopenharmony_ci /* PCIe extended capability space */ 48962306a36Sopenharmony_ci reg -= PCI_CFG_SPACE_SIZE; 49062306a36Sopenharmony_ci read_op = bridge->ops->read_ext; 49162306a36Sopenharmony_ci cfgspace = NULL; 49262306a36Sopenharmony_ci behavior = NULL; 49362306a36Sopenharmony_ci } else { 49462306a36Sopenharmony_ci /* Not implemented */ 49562306a36Sopenharmony_ci *value = 0; 49662306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 49762306a36Sopenharmony_ci } 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci if (read_op) 50062306a36Sopenharmony_ci ret = read_op(bridge, reg, value); 50162306a36Sopenharmony_ci else 50262306a36Sopenharmony_ci ret = PCI_BRIDGE_EMUL_NOT_HANDLED; 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci if (ret == PCI_BRIDGE_EMUL_NOT_HANDLED) { 50562306a36Sopenharmony_ci if (cfgspace) 50662306a36Sopenharmony_ci *value = le32_to_cpu(cfgspace[reg / 4]); 50762306a36Sopenharmony_ci else 50862306a36Sopenharmony_ci *value = 0; 50962306a36Sopenharmony_ci } 51062306a36Sopenharmony_ci 51162306a36Sopenharmony_ci /* 51262306a36Sopenharmony_ci * Make sure we never return any reserved bit with a value 51362306a36Sopenharmony_ci * different from 0. 51462306a36Sopenharmony_ci */ 51562306a36Sopenharmony_ci if (behavior) 51662306a36Sopenharmony_ci *value &= behavior[reg / 4].ro | behavior[reg / 4].rw | 51762306a36Sopenharmony_ci behavior[reg / 4].w1c; 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ci if (size == 1) 52062306a36Sopenharmony_ci *value = (*value >> (8 * (where & 3))) & 0xff; 52162306a36Sopenharmony_ci else if (size == 2) 52262306a36Sopenharmony_ci *value = (*value >> (8 * (where & 3))) & 0xffff; 52362306a36Sopenharmony_ci else if (size != 4) 52462306a36Sopenharmony_ci return PCIBIOS_BAD_REGISTER_NUMBER; 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 52762306a36Sopenharmony_ci} 52862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_conf_read); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci/* 53162306a36Sopenharmony_ci * Should be called by the PCI controller driver when writing the PCI 53262306a36Sopenharmony_ci * configuration space of the fake bridge. It will call back the 53362306a36Sopenharmony_ci * ->ops->write_base or ->ops->write_pcie operations. 53462306a36Sopenharmony_ci */ 53562306a36Sopenharmony_ciint pci_bridge_emul_conf_write(struct pci_bridge_emul *bridge, int where, 53662306a36Sopenharmony_ci int size, u32 value) 53762306a36Sopenharmony_ci{ 53862306a36Sopenharmony_ci int reg = where & ~3; 53962306a36Sopenharmony_ci int mask, ret, old, new, shift; 54062306a36Sopenharmony_ci void (*write_op)(struct pci_bridge_emul *bridge, int reg, 54162306a36Sopenharmony_ci u32 old, u32 new, u32 mask); 54262306a36Sopenharmony_ci __le32 *cfgspace; 54362306a36Sopenharmony_ci const struct pci_bridge_reg_behavior *behavior; 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci ret = pci_bridge_emul_conf_read(bridge, reg, 4, &old); 54662306a36Sopenharmony_ci if (ret != PCIBIOS_SUCCESSFUL) 54762306a36Sopenharmony_ci return ret; 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci if (reg < PCI_BRIDGE_CONF_END) { 55062306a36Sopenharmony_ci /* Emulated PCI space */ 55162306a36Sopenharmony_ci write_op = bridge->ops->write_base; 55262306a36Sopenharmony_ci cfgspace = (__le32 *) &bridge->conf; 55362306a36Sopenharmony_ci behavior = bridge->pci_regs_behavior; 55462306a36Sopenharmony_ci } else if (reg >= bridge->pcie_start && reg < bridge->pcie_start + PCI_CAP_PCIE_SIZEOF && 55562306a36Sopenharmony_ci bridge->has_pcie) { 55662306a36Sopenharmony_ci /* Our emulated PCIe capability */ 55762306a36Sopenharmony_ci reg -= bridge->pcie_start; 55862306a36Sopenharmony_ci write_op = bridge->ops->write_pcie; 55962306a36Sopenharmony_ci cfgspace = (__le32 *) &bridge->pcie_conf; 56062306a36Sopenharmony_ci behavior = bridge->pcie_cap_regs_behavior; 56162306a36Sopenharmony_ci } else if (reg >= PCI_CFG_SPACE_SIZE && bridge->has_pcie) { 56262306a36Sopenharmony_ci /* PCIe extended capability space */ 56362306a36Sopenharmony_ci reg -= PCI_CFG_SPACE_SIZE; 56462306a36Sopenharmony_ci write_op = bridge->ops->write_ext; 56562306a36Sopenharmony_ci cfgspace = NULL; 56662306a36Sopenharmony_ci behavior = NULL; 56762306a36Sopenharmony_ci } else { 56862306a36Sopenharmony_ci /* Not implemented */ 56962306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 57062306a36Sopenharmony_ci } 57162306a36Sopenharmony_ci 57262306a36Sopenharmony_ci shift = (where & 0x3) * 8; 57362306a36Sopenharmony_ci 57462306a36Sopenharmony_ci if (size == 4) 57562306a36Sopenharmony_ci mask = 0xffffffff; 57662306a36Sopenharmony_ci else if (size == 2) 57762306a36Sopenharmony_ci mask = 0xffff << shift; 57862306a36Sopenharmony_ci else if (size == 1) 57962306a36Sopenharmony_ci mask = 0xff << shift; 58062306a36Sopenharmony_ci else 58162306a36Sopenharmony_ci return PCIBIOS_BAD_REGISTER_NUMBER; 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci if (behavior) { 58462306a36Sopenharmony_ci /* Keep all bits, except the RW bits */ 58562306a36Sopenharmony_ci new = old & (~mask | ~behavior[reg / 4].rw); 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci /* Update the value of the RW bits */ 58862306a36Sopenharmony_ci new |= (value << shift) & (behavior[reg / 4].rw & mask); 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci /* Clear the W1C bits */ 59162306a36Sopenharmony_ci new &= ~((value << shift) & (behavior[reg / 4].w1c & mask)); 59262306a36Sopenharmony_ci } else { 59362306a36Sopenharmony_ci new = old & ~mask; 59462306a36Sopenharmony_ci new |= (value << shift) & mask; 59562306a36Sopenharmony_ci } 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci if (cfgspace) { 59862306a36Sopenharmony_ci /* Save the new value with the cleared W1C bits into the cfgspace */ 59962306a36Sopenharmony_ci cfgspace[reg / 4] = cpu_to_le32(new); 60062306a36Sopenharmony_ci } 60162306a36Sopenharmony_ci 60262306a36Sopenharmony_ci if (behavior) { 60362306a36Sopenharmony_ci /* 60462306a36Sopenharmony_ci * Clear the W1C bits not specified by the write mask, so that the 60562306a36Sopenharmony_ci * write_op() does not clear them. 60662306a36Sopenharmony_ci */ 60762306a36Sopenharmony_ci new &= ~(behavior[reg / 4].w1c & ~mask); 60862306a36Sopenharmony_ci 60962306a36Sopenharmony_ci /* 61062306a36Sopenharmony_ci * Set the W1C bits specified by the write mask, so that write_op() 61162306a36Sopenharmony_ci * knows about that they are to be cleared. 61262306a36Sopenharmony_ci */ 61362306a36Sopenharmony_ci new |= (value << shift) & (behavior[reg / 4].w1c & mask); 61462306a36Sopenharmony_ci } 61562306a36Sopenharmony_ci 61662306a36Sopenharmony_ci if (write_op) 61762306a36Sopenharmony_ci write_op(bridge, reg, old, new, mask); 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 62062306a36Sopenharmony_ci} 62162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_bridge_emul_conf_write); 622