diff --git a/Documentation/PCI/controller/apple-t8140.rst b/Documentation/PCI/controller/apple-t8140.rst new file mode 100644 index 0000000000000..90d4eab8f07f4 --- /dev/null +++ b/Documentation/PCI/controller/apple-t8140.rst @@ -0,0 +1,24 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +T8140/J700 experimental radio PCIe bootstrap +========================================== + +The J700 path trains port 0, validates a bounded PIODMA bootstrap request, +and enables native ECAM for the MediaTek Wi-Fi and Bluetooth functions. +The root bridge forwards their BARs and DART translates endpoint DMA. + +This is an opt-in bring-up implementation, not general T8140 PCIe support. +Build ``CONFIG_PCIE_APPLE_PIODMA_DIAG=y`` and pass +``pcie_apple_piodma_diag.enumerate=1`` to the kernel. Without the opt-in, the +root-only diagnostic path continues to reject downstream configuration access. +The option excludes suspend and kexec. Failed or uncertain bootstrap state +requires a full external hardware reset; there is no command retry. + +The PIODMA arena remains allocated until external reset. Controller removal, +teardown, memory reuse and arbitrary downstream devices are unqualified. +Do not remove the controller or its IOMMU while this experiment is active. +These lifecycle contracts require further work before production support. + +J700's ``wifi0`` alias and PCI endpoint node allow the public bootloader to +pass the unit's own Wi-Fi address through ``local-mac-address``. The zero +placeholder in the static device tree is not a usable station address. diff --git a/Documentation/PCI/controller/index.rst b/Documentation/PCI/controller/index.rst index c2ce9ccdcfa06..8619be96d0b63 100644 --- a/Documentation/PCI/controller/index.rst +++ b/Documentation/PCI/controller/index.rst @@ -8,3 +8,4 @@ PCI Native Host Bridge and Endpoint Drivers :maxdepth: 2 rcar-pcie-firmware + apple-t8140 diff --git a/Documentation/devicetree/bindings/dma/apple,t8140-piodma-diagnostic.yaml b/Documentation/devicetree/bindings/dma/apple,t8140-piodma-diagnostic.yaml new file mode 100644 index 0000000000000..a6a8d7d3af77a --- /dev/null +++ b/Documentation/devicetree/bindings/dma/apple,t8140-piodma-diagnostic.yaml @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/dma/apple,t8140-piodma-diagnostic.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple T8140 experimental PCIe PIODMA bootstrap engine + +maintainers: + - Ace + +description: | + The J700 diagnostic driver uses this engine for a single retained request + before enabling native downstream ECAM access. It does not expose generic + DMA channels. This experimental binding describes that bounded bootstrap; + it does not establish an engine reset, removal or memory reuse contract. + +properties: + compatible: + const: apple,t8140-piodma-diagnostic + + reg: + items: + - description: Request engine + - description: Interrupt aggregator + - description: PCIe fabric interface + + reg-names: + items: + - const: engine + - const: irq-aggregator + - const: fabric + + interrupts: + maxItems: 1 + + power-domains: + maxItems: 1 + + iommus: + maxItems: 1 + + apple,pcie-host: + $ref: /schemas/types.yaml#/definitions/phandle + description: PCIe host controller whose ECAM access this engine primes. + +required: + - compatible + - reg + - reg-names + - interrupts + - power-domains + - iommus + - apple,pcie-host + +additionalProperties: false diff --git a/Documentation/devicetree/bindings/pci/apple,pcie.yaml b/Documentation/devicetree/bindings/pci/apple,pcie.yaml index 6a6608dca4d1b..48e3ae4ceafbf 100644 --- a/Documentation/devicetree/bindings/pci/apple,pcie.yaml +++ b/Documentation/devicetree/bindings/pci/apple,pcie.yaml @@ -49,6 +49,7 @@ properties: - const: apple,t8122-pcie - const: apple,t6020-pcie - const: apple,t6020-pcie + - const: apple,t8140-pcie - const: apple,t8103-pciec - items: - const: apple,t8112-pciec @@ -86,6 +87,11 @@ properties: dma-coherent: true + apple,piodma: + $ref: /schemas/types.yaml#/definitions/phandle + description: + T8140 PIODMA bootstrap engine used before native downstream ECAM access. + patternProperties: "^pci@": $ref: /schemas/pci/pci-bus.yaml# @@ -125,6 +131,17 @@ unevaluatedProperties: false allOf: - $ref: /schemas/pci/pci-host-bridge.yaml# - $ref: /schemas/interrupt-controller/msi-controller.yaml# + - if: + properties: + compatible: + contains: + const: apple,t8140-pcie + then: + properties: + apple,piodma: true + else: + properties: + apple,piodma: false - if: properties: compatible: @@ -180,21 +197,46 @@ allOf: maxItems: 3 else: properties: - reg-names: - minItems: 3 - items: - - const: config - - const: rc - - const: port0 - - const: port1 - - const: port2 - - const: port3 - - const: phy0 - - const: phy1 - - const: phy2 - - const: phy3 ranges: maxItems: 2 + allOf: + - if: + properties: + compatible: + contains: + const: apple,t8140-pcie + then: + properties: + reg: + minItems: 8 + maxItems: 8 + reg-names: + items: + - const: config + - const: rc + - const: port0 + - const: port1 + - const: port2 + - const: phy0 + - const: phy1 + - const: phy2 + interrupts: + maxItems: 3 + else: + properties: + reg-names: + minItems: 3 + items: + - const: config + - const: rc + - const: port0 + - const: port1 + - const: port2 + - const: port3 + - const: phy0 + - const: phy1 + - const: phy2 + - const: phy3 patternProperties: "^pci@": required: diff --git a/arch/arm64/boot/dts/apple/t8140-j700.dts b/arch/arm64/boot/dts/apple/t8140-j700.dts index 270932d29e966..cc9f115adb7ab 100644 --- a/arch/arm64/boot/dts/apple/t8140-j700.dts +++ b/arch/arm64/boot/dts/apple/t8140-j700.dts @@ -19,6 +19,7 @@ atcphy0 = &atcphy0; keyboard = &keyboard; serial0 = &serial0; + wifi0 = &wifi0; }; chosen { @@ -1349,3 +1350,57 @@ #include "hwmon-common.dtsi" #include "hwmon-laptop.dtsi" + +&pcie0 { + /* Experiment: guard stays closed until the retained bootstrap succeeds. */ + apple,piodma = <&pcie0_piodma_diag>; + iommu-map = <0x100 &pcie0_dart 1 2>; + iommu-map-mask = <0xffff>; + pinctrl-0 = <&pcie0_clkreq>; + pinctrl-names = "default"; + status = "okay"; +}; + +&pcie0_port0 { + reset-gpios = <&pinctrl_ap 114 GPIO_ACTIVE_LOW>; + pwren-gpios = <&smc_gpio 8 GPIO_ACTIVE_HIGH>; + max-link-speed = <2>; + status = "okay"; +}; + +&pinctrl_ap { + pcie0_clkreq: pcie0-clkreq { + pinmux = ; + }; +}; + +/* Experimental SID17 bootstrap; arena ownership lasts until full reset. */ +&pcie0_dart { + status = "okay"; + apple,dma-range = /bits/ 64 <0 0x1000000000>; +}; + +/ { + pcie0_piodma_diag: dma-controller@390030000 { + compatible = "apple,t8140-piodma-diagnostic"; + reg = <0x3 0x90030000 0x0 0x4000>, + <0x3 0x90034000 0x0 0x4000>, + <0x3 0x90038000 0x0 0x4000>; + reg-names = "engine", "irq-aggregator", "fabric"; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&ps_apcie_gp>; + iommus = <&pcie0_dart 17>; + apple,pcie-host = <&pcie0>; + }; +}; + +&pcie0_port0 { + bus-range = <1 1>; + wifi0: wifi@0,0 { + compatible = "pci14c3,7932"; + reg = <0x10000 0 0 0 0>; + /* Public m1n1 supplies this device's address at boot. */ + local-mac-address = [00 00 00 00 00 00]; + }; +}; diff --git a/arch/arm64/boot/dts/apple/t8140.dtsi b/arch/arm64/boot/dts/apple/t8140.dtsi index d9d7b3103115e..5ffe82f64ca3d 100644 --- a/arch/arm64/boot/dts/apple/t8140.dtsi +++ b/arch/arm64/boot/dts/apple/t8140.dtsi @@ -588,6 +588,62 @@ status = "disabled"; }; + pcie0: pcie@1cb0000000 { + compatible = "apple,t8140-pcie"; + device_type = "pci"; + reg = <0x1c 0xb0000000 0x0 0x10000000>, + <0x3 0x94000000 0x0 0x4000>, + <0x3 0x90028000 0x0 0x8000>, + <0x3 0x91028000 0x0 0x8000>, + <0x3 0x92028000 0x0 0x8000>, + <0x3 0x97020000 0x0 0x4000>, + <0x3 0x97024000 0x0 0x4000>, + <0x3 0x97028000 0x0 0x4000>; + reg-names = "config", "rc", "port0", "port1", "port2", + "phy0", "phy1", "phy2"; + interrupt-parent = <&aic>; + interrupts = , + , + ; + msi-controller; + msi-parent = <&pcie0>; + /* Only port 0 is described; it owns the first 32 vectors. */ + msi-ranges = <&aic AIC_IRQ 1331 IRQ_TYPE_EDGE_RISING 32>; + bus-range = <0 1>; + #address-cells = <3>; + #size-cells = <2>; + ranges = <0x43000000 0xb 0xc0000000 0xb 0xc0000000 0x0 0x20000000>, + <0x02000000 0x0 0x80000000 0xb 0x80000000 0x0 0x40000000>; + power-domains = <&ps_apcie_gp>; + status = "disabled"; + + pcie0_port0: pci@0,0 { + device_type = "pci"; + reg = <0 0 0 0 0>; + #address-cells = <3>; + #size-cells = <2>; + ranges; + interrupt-controller; + #interrupt-cells = <1>; + interrupt-map-mask = <0 0 0 7>; + interrupt-map = <0 0 0 1 &pcie0_port0 0 0 0 0>, + <0 0 0 2 &pcie0_port0 0 0 0 1>, + <0 0 0 3 &pcie0_port0 0 0 0 2>, + <0 0 0 4 &pcie0_port0 0 0 0 3>; + status = "disabled"; + }; + }; + + pcie0_dart: iommu@390000000 { + compatible = "apple,t8140-dart", "apple,t8110-dart"; + reg = <0x3 0x90000000 0x0 0x20000>; + #iommu-cells = <1>; + interrupt-parent = <&aic>; + interrupts = ; + power-domains = <&ps_apcie_gp>; + status = "disabled"; + }; + dwc3_0: usb@40a280000 { compatible = "apple,t8140-dwc3", "apple,t8103-dwc3"; reg = <0x4 0x0a280000 0x0 0xcd00>, diff --git a/drivers/gpio/gpio-macsmc.c b/drivers/gpio/gpio-macsmc.c index f44566d6598b4..471bce2e54b4f 100644 --- a/drivers/gpio/gpio-macsmc.c +++ b/drivers/gpio/gpio-macsmc.c @@ -28,6 +28,11 @@ #define MACSMC_J700_AFE_RESET_LO 0x10000 #define MACSMC_J700_AFE_RESET_HI 0x130000 +/* J700 amfm-sunrise function-reg_on: pKW8("pcIO", 0x800000, 0x80000). */ +#define MACSMC_J700_RADIO_REG_ON_GPIO 8 +#define MACSMC_J700_RADIO_REG_ON_LO 0x800000 +#define MACSMC_J700_RADIO_REG_ON_HI 0x80000 + /* * Commands 0-6 are, presumably, the intended API. * Command 0xff lets you get/set the pin configuration in detail directly, @@ -89,14 +94,16 @@ struct macsmc_gpio { int first_index; smc_key base_key; - bool j700_afe_reset; - bool j700_afe_reset_level; + bool j700_pcio; + DECLARE_BITMAP(j700_pcio_level, MAX_GPIO); }; -static bool macsmc_gpio_is_j700_afe_reset(struct macsmc_gpio *smcgp, +static bool macsmc_gpio_is_j700_pcio(struct macsmc_gpio *smcgp, unsigned int offset) { - return smcgp->j700_afe_reset && offset == MACSMC_J700_AFE_RESET_GPIO; + return smcgp->j700_pcio && + (offset == MACSMC_J700_AFE_RESET_GPIO || + offset == MACSMC_J700_RADIO_REG_ON_GPIO); } static int macsmc_gpio_nr(smc_key key) @@ -169,7 +176,7 @@ static int macsmc_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) u32 val; int ret; - if (macsmc_gpio_is_j700_afe_reset(smcgp, offset)) + if (macsmc_gpio_is_j700_pcio(smcgp, offset)) return GPIO_LINE_DIRECTION_OUT; /* First try reading the explicit pin mode register */ @@ -193,8 +200,8 @@ static int macsmc_gpio_get(struct gpio_chip *gc, unsigned int offset) int ret; /* pcIO cannot be read back; report the last level written. */ - if (macsmc_gpio_is_j700_afe_reset(smcgp, offset)) - return smcgp->j700_afe_reset_level; + if (macsmc_gpio_is_j700_pcio(smcgp, offset)) + return test_bit(offset, smcgp->j700_pcio_level); ret = macsmc_gpio_get_direction(gc, offset); if (ret < 0) @@ -218,9 +225,18 @@ static int macsmc_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) smc_key key = macsmc_gpio_key(smcgp->base_key, offset); int ret; - if (macsmc_gpio_is_j700_afe_reset(smcgp, offset)) { - u64 payload = ((u64)MACSMC_J700_AFE_RESET_HI << 32) | - MACSMC_J700_AFE_RESET_LO | (value ? 1 : 0); + if (macsmc_gpio_is_j700_pcio(smcgp, offset)) { + u32 lo, hi; + u64 payload; + + if (offset == MACSMC_J700_RADIO_REG_ON_GPIO) { + lo = MACSMC_J700_RADIO_REG_ON_LO; + hi = MACSMC_J700_RADIO_REG_ON_HI; + } else { + lo = MACSMC_J700_AFE_RESET_LO; + hi = MACSMC_J700_AFE_RESET_HI; + } + payload = ((u64)hi << 32) | lo | !!value; ret = apple_smc_write_u64(smcgp->smc, SMC_KEY(pcIO), payload); if (ret < 0) { @@ -229,7 +245,7 @@ static int macsmc_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) offset, !!value, ret); return ret; } - smcgp->j700_afe_reset_level = !!value; + assign_bit(offset, smcgp->j700_pcio_level, value); return 0; } @@ -252,8 +268,10 @@ static int macsmc_gpio_init_valid_mask(struct gpio_chip *gc, count = min(smcgp->smc->key_count, MAX_GPIO); bitmap_zero(valid_mask, ngpios); - if (smcgp->j700_afe_reset) + if (smcgp->j700_pcio) { set_bit(MACSMC_J700_AFE_RESET_GPIO, valid_mask); + set_bit(MACSMC_J700_RADIO_REG_ON_GPIO, valid_mask); + } for (i = 0; i < count; i++) { int ret, gpio_nr; @@ -297,7 +315,7 @@ static int macsmc_gpio_probe(struct platform_device *pdev) smcgp->dev = &pdev->dev; smcgp->smc = smc; smcgp->base_key = data ? data->base_key : _SMC_KEY("gP\0\0"); - smcgp->j700_afe_reset = of_machine_is_compatible("apple,j700") && + smcgp->j700_pcio = of_machine_is_compatible("apple,j700") && device_is_compatible(&pdev->dev, "apple,smc-gpio"); smcgp->first_index = macsmc_gpio_find_first_gpio_index(smcgp); diff --git a/drivers/pci/controller/Kconfig b/drivers/pci/controller/Kconfig index e3bb6ff1b6d71..ebdc821fea454 100644 --- a/drivers/pci/controller/Kconfig +++ b/drivers/pci/controller/Kconfig @@ -56,6 +56,17 @@ config PCIE_APPLE If unsure, say Y if you have an Apple Silicon system. +config PCIE_APPLE_PIODMA_DIAG + bool "J700 PIODMA diagnostic (requires full reset after use)" + depends on PCIE_APPLE=y && ARCH_APPLE && IOMMU_DMA + depends on !PM_SLEEP && !KEXEC_CORE + help + Build the experimental SID17 retained bootstrap provider. It submits + one fixed request, validates typed ECAM and permits enumeration of + the two Neo functions only with the explicit enumerate=1 parameter. + Its arena remains owned until a full reset. This is not production + support, and does not qualify memory reuse or radio operation. + config PCI_VERSATILE bool "ARM Versatile PB PCI controller" depends on ARCH_VERSATILE || COMPILE_TEST diff --git a/drivers/pci/controller/Makefile b/drivers/pci/controller/Makefile index ac8db283f0fea..5984a69a5331e 100644 --- a/drivers/pci/controller/Makefile +++ b/drivers/pci/controller/Makefile @@ -65,3 +65,5 @@ obj-$(CONFIG_ARM64) += pci-thunder-pem.o obj-$(CONFIG_ARM64) += pci-xgene.o endif endif + +obj-$(CONFIG_PCIE_APPLE_PIODMA_DIAG) += pcie-apple-piodma-diag.o diff --git a/drivers/pci/controller/pcie-apple-piodma-diag.c b/drivers/pci/controller/pcie-apple-piodma-diag.c new file mode 100644 index 0000000000000..87caa91262e37 --- /dev/null +++ b/drivers/pci/controller/pcie-apple-piodma-diag.c @@ -0,0 +1,616 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * J700 PIODMA diagnostic scaffolding, not a production DMA driver. + * + * Public Linux DMA/IOMMU APIs own the SID17 mappings. The register and + * address-view contract is attributed in the accompanying provenance ledger + * to the reviewed Aurora reference and fresh ADT observations. + * + * This built-in driver deliberately retains its coherent arena and device + * power reference until a coordinator-controlled full hardware reset. There + * is no unbind interface, suspend, kexec or retry. Native ECAM is only + * opened by the host after a successful bootstrap and typed read checks. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pcie-apple-piodma-diag.h" + +#define PIODMA_PACKET_MASK DMA_BIT_MASK(36) +#define PIODMA_POINTER_MASK DMA_BIT_MASK(42) +#define PIODMA_POINTER_PREFIX BIT_ULL(40) +#define PIODMA_SID 17 +#define PIODMA_REQUEST_COUNT 1 +#define PIODMA_SLOT_SIZE (3 * PAGE_SIZE) +#define PIODMA_ARENA_SIZE (PIODMA_REQUEST_COUNT * PIODMA_SLOT_SIZE) +#define PIODMA_DATA_OFFSET (2 * PAGE_SIZE) +#define PIODMA_COMPLETION_OFFSET PAGE_SIZE +#define PIODMA_CONFIG_BASE 0x1cb0000000ULL +#define PIODMA_CONFIG_SIZE SZ_256M +#define PIODMA_IRQ_COMPLETE (BIT(0) | BIT(6)) +#define PIODMA_IRQ_MASK GENMASK(9, 0) +#define PIODMA_FIFO_MASK GENMASK(5, 0) +#define PIODMA_IRQ_LIMIT 16 + +static bool enumerate; +module_param(enumerate, bool, 0400); +MODULE_PARM_DESC(enumerate, "Prime once, validate native ECAM, and enumerate the two Neo functions"); + +struct apple_piodma_request { + u16 offset; + u8 function; + u32 mask; + u32 expected; +}; + +static const struct apple_piodma_request requests[PIODMA_REQUEST_COUNT] = { + { PCI_VENDOR_ID, 0, U32_MAX, 0x793214c3 }, +}; + +struct apple_piodma_slot { + struct completion event; + __le32 packet[4]; + __le32 completion[4]; + u32 result; + u32 matched_status; + bool submitted; + bool observed; +}; + +struct apple_piodma_diag { + struct device *dev; + struct iommu_domain *domain; + void __iomem *engine; + void __iomem *fabric; + struct apple_piodma_slot slots[PIODMA_REQUEST_COUNT]; + atomic_t irq_count; + atomic_t attempted; + u32 active_tag; + u64 config_source; + u32 last_irq_status; + u32 last_irq_tag; + int irq; + bool irq_fault; + void *arena; + dma_addr_t dma; + u64 pointer_prefix; + u8 secondary_bus; + bool retained; +}; + +static int apple_piodma_diag_root(struct apple_piodma_diag *diag, struct pci_dev *root) +{ + int ret; + + if (!pci_is_root_bus(root->bus) || pci_domain_nr(root->bus) || + root->bus->number || root->devfn || + root->vendor != PCI_VENDOR_ID_APPLE || root->device != 0x100c || + root->class != PCI_CLASS_BRIDGE_PCI << 8) + return -ENODEV; + ret = pci_read_config_byte(root, PCI_SECONDARY_BUS, &diag->secondary_bus); + if (ret) + return pcibios_err_to_errno(ret); + if (diag->secondary_bus != 1) + return -EINVAL; + diag->config_source += (u64)diag->secondary_bus << 20; + return 0; +} + +static int apple_piodma_diag_iommu(struct apple_piodma_diag *diag) +{ + struct of_phandle_args args; + u64 range[2]; + int ret; + + ret = of_parse_phandle_with_args(diag->dev->of_node, "iommus", + "#iommu-cells", 0, &args); + if (ret) + return ret; + if (args.args_count != 1 || args.args[0] != PIODMA_SID || + !of_device_is_compatible(args.np, "apple,t8140-dart")) { + ret = -EINVAL; + goto out; + } + /* Keep the prior pointer convention with low IAS36 page tables. */ + if (of_property_present(args.np, "apple,dma-offset")) { + ret = -EINVAL; + goto out; + } + diag->pointer_prefix = PIODMA_POINTER_PREFIX; + ret = of_property_read_u64_array(args.np, "apple,dma-range", range, 2); + if (ret) + goto out; + if (diag->pointer_prefix != PIODMA_POINTER_PREFIX || range[0] || + range[1] != BIT_ULL(36)) { + ret = -EINVAL; + goto out; + } + diag->domain = iommu_get_domain_for_dev(diag->dev); + if (!diag->domain || + (diag->domain->type != IOMMU_DOMAIN_DMA && + diag->domain->type != IOMMU_DOMAIN_DMA_FQ)) { + ret = -ENODEV; + goto out; + } + ret = dma_set_mask_and_coherent(diag->dev, PIODMA_PACKET_MASK); +out: + of_node_put(args.np); + return ret; +} + +static int apple_piodma_diag_arena(struct apple_piodma_diag *diag) +{ + u64 last, pointer; + int i; + + diag->arena = dma_alloc_coherent(diag->dev, PIODMA_ARENA_SIZE, &diag->dma, GFP_KERNEL); + if (!diag->arena) + return -ENOMEM; + if (!IS_ALIGNED(diag->dma, SZ_16K) || + check_add_overflow((u64)diag->dma, PIODMA_ARENA_SIZE - 1, &last) || + last > PIODMA_PACKET_MASK || + check_add_overflow(last, diag->pointer_prefix, &pointer) || + pointer > PIODMA_POINTER_MASK || + !diag->domain->geometry.force_aperture || + diag->domain->geometry.aperture_start != 0 || + diag->domain->geometry.aperture_end != PIODMA_PACKET_MASK) + return -ERANGE; + memset(diag->arena, 0, PIODMA_ARENA_SIZE); + for (i = 0; i < PIODMA_REQUEST_COUNT; i++) { + void *slot = diag->arena + i * PIODMA_SLOT_SIZE; + u64 dma = diag->dma + i * PIODMA_SLOT_SIZE; + + memset(slot + PIODMA_DATA_OFFSET, 0xa5, PAGE_SIZE); + *(__le32 *)(slot + PIODMA_DATA_OFFSET) = cpu_to_le32(U32_MAX); + dev_info(diag->dev, + "owned SID17 slot=%d dma=%#llx descriptor=%#llx completion=%#llx data=%#llx\n", + i, dma, dma + diag->pointer_prefix, + dma + diag->pointer_prefix + PIODMA_COMPLETION_OFFSET, + dma + PIODMA_DATA_OFFSET); + } + dma_wmb(); + return 0; +} + + + +static int apple_piodma_diag_resources(struct platform_device *pdev, + struct apple_piodma_diag *diag) +{ + static const struct { + const char *name; + resource_size_t base; + } regions[] = { + { "engine", 0x390030000ULL }, + { "irq-aggregator", 0x390034000ULL }, + { "fabric", 0x390038000ULL }, + }; + struct device_node *host; + struct resource ecam, *res; + int i, ret; + + for (i = 0; i < ARRAY_SIZE(regions); i++) { + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, regions[i].name); + if (!res || res->start != regions[i].base || resource_size(res) != SZ_16K) + return -EINVAL; + } + host = of_parse_phandle(diag->dev->of_node, "apple,pcie-host", 0); + if (!host) + return -EINVAL; + ret = of_address_to_resource(host, 0, &ecam); + if (!ret && (!of_device_is_compatible(host, "apple,t8140-pcie") || + ecam.start != PIODMA_CONFIG_BASE || + resource_size(&ecam) != PIODMA_CONFIG_SIZE)) + ret = -EINVAL; + of_node_put(host); + if (ret) + return ret; + diag->config_source = ecam.start; + if (diag->config_source + (1 << 12) + sizeof(u32) - 1 > ecam.end) + return -ERANGE; + diag->engine = devm_platform_ioremap_resource_byname(pdev, "engine"); + if (IS_ERR(diag->engine)) + return PTR_ERR(diag->engine); + diag->fabric = devm_platform_ioremap_resource_byname(pdev, "fabric"); + if (IS_ERR(diag->fabric)) + return PTR_ERR(diag->fabric); + diag->irq = platform_get_irq(pdev, 0); + return diag->irq < 0 ? diag->irq : 0; +} + +static int apple_piodma_diag_idle(struct apple_piodma_diag *diag) +{ + static const struct { + u32 offset; + u32 expected; + } engine[] = { + { 0x00, 2 }, { 0x04, 0 }, { 0x08, 0 }, { 0x0c, 16 }, + { 0x44, 0 }, { 0x48, 0 }, { 0x4c, 0 }, { 0x50, 0 }, + { 0x54, 0 }, { 0xd8, 0 }, { 0xdc, 0 }, + }; + int i; + u32 value; + + for (i = 0; i < ARRAY_SIZE(engine); i++) { + value = readl(diag->engine + engine[i].offset); + if (value != engine[i].expected) { + dev_err(diag->dev, "idle mismatch engine[%#x]=%#x expected=%#x\n", + engine[i].offset, value, engine[i].expected); + return -EBUSY; + } + } + if (readl(diag->fabric) || readl(diag->fabric + 4)) + return -EBUSY; + return 0; +} + +static irqreturn_t apple_piodma_diag_irq(int irq, void *data) +{ + struct apple_piodma_diag *diag = data; + struct apple_piodma_slot *slot; + u32 fabric, status, tag, active; + int count; + + /* Acknowledge order follows the attributed prior controller contract. */ + fabric = readl(diag->fabric); + writel(fabric, diag->fabric); + status = readl(diag->engine + 4); + writel(status, diag->engine + 4); + tag = readl(diag->engine + 0xdc) & 0xff; + active = READ_ONCE(diag->active_tag); + count = atomic_inc_return(&diag->irq_count); + WRITE_ONCE(diag->last_irq_status, status); + WRITE_ONCE(diag->last_irq_tag, tag); + dev_info(diag->dev, "irq=%d fabric=%#x status=%#x tag=%#x active=%#x\n", + count, fabric, status, tag, active); + if (count >= PIODMA_IRQ_LIMIT || status & ~PIODMA_IRQ_COMPLETE || + (status && (!tag || tag > active))) { + WRITE_ONCE(diag->irq_fault, true); + disable_irq_nosync(irq); + if (active) + complete(&diag->slots[active - 1].event); + } else if (status & PIODMA_IRQ_COMPLETE && tag == active && active) { + slot = &diag->slots[active - 1]; + WRITE_ONCE(slot->matched_status, READ_ONCE(slot->matched_status) | status); + complete(&slot->event); + } + /* A late old-tag IRQ never wakes or qualifies the current request. */ + return IRQ_HANDLED; +} + +static int apple_piodma_diag_initialize(struct apple_piodma_diag *diag) +{ + static const struct { + u32 offset; + u32 mask; + u32 value; + } tunables[] = { + { 0x00, 0x2, 0 }, + { 0x10, 0x1fffffff, 0x1fbf2 }, + { 0x14, 0x1fffffff, 0x1fbf2 }, + { 0x18, 0x1fffffff, 0x1fbf3 }, + { 0x1c, 0x7ffff, 0x1fbf0 }, + { 0x20, 0x7ffff, 0x1fbf0 }, + }; + int i; + u32 value; + + /* All later failures retain the device, IRQ, domain and arena. */ + diag->retained = true; + writel(PIODMA_IRQ_MASK, diag->engine + 8); + writel(0, diag->fabric + 4); + for (i = 0; i < ARRAY_SIZE(tunables); i++) { + value = readl(diag->engine + tunables[i].offset); + value = (value & ~tunables[i].mask) | tunables[i].value; + writel(value, diag->engine + tunables[i].offset); + } + writel(readl(diag->engine) & ~BIT(0), diag->engine); + /* Exact prior bank values; their architectural meaning is unqualified. */ + for (i = 0; i < 8; i++) { + writel(0, diag->engine + 0x24 + 4 * i); + writel(i == 1 ? 1 : 0, diag->engine + 0xf0 + 4 * i); + } + /* Order the bank writes before their configuration readback. */ + wmb(); + if (readl(diag->engine) || readl(diag->engine + 8) != PIODMA_IRQ_MASK || + readl(diag->engine + 0xf4) != 1 || READ_ONCE(diag->irq_fault)) + return -EIO; + return 0; +} + +static bool apple_piodma_diag_canary(const u8 *memory, size_t size, u8 value) +{ + size_t i; + + for (i = 0; i < size; i++) + if (READ_ONCE(memory[i]) != value) + return false; + return true; +} + +static int apple_piodma_diag_check_slots(struct apple_piodma_diag *diag) +{ + int i, j; + + dma_rmb(); + for (i = 0; i < PIODMA_REQUEST_COUNT; i++) { + struct apple_piodma_slot *slot = &diag->slots[i]; + void *memory = diag->arena + i * PIODMA_SLOT_SIZE; + __le32 *packet = memory; + __le32 *completion = memory + PIODMA_COMPLETION_OFFSET; + __le32 *result = memory + PIODMA_DATA_OFFSET; + + for (j = 0; j < ARRAY_SIZE(slot->packet); j++) { + if (READ_ONCE(packet[j]) != slot->packet[j] || + (slot->observed && READ_ONCE(completion[j]) != slot->completion[j]) || + (!slot->submitted && READ_ONCE(completion[j]))) + goto changed; + } + if ((slot->observed && le32_to_cpu(READ_ONCE(*result)) != slot->result) || + (!slot->submitted && le32_to_cpu(READ_ONCE(*result)) != U32_MAX) || + !apple_piodma_diag_canary(memory + sizeof(slot->packet), + PAGE_SIZE - sizeof(slot->packet), 0) || + !apple_piodma_diag_canary(memory + PIODMA_COMPLETION_OFFSET + + sizeof(slot->completion), + PAGE_SIZE - sizeof(slot->completion), 0) || + !apple_piodma_diag_canary(memory + PIODMA_DATA_OFFSET + sizeof(u32), + PAGE_SIZE - sizeof(u32), 0xa5)) + goto changed; + } + return 0; + +changed: + dev_err(diag->dev, "slot=%d changed outside its permitted response; stop\n", i); + return -EIO; +} + +static int apple_piodma_diag_submit(struct apple_piodma_diag *diag, unsigned int index) +{ + const struct apple_piodma_request *request = &requests[index]; + struct apple_piodma_slot *slot = &diag->slots[index]; + void *memory = diag->arena + index * PIODMA_SLOT_SIZE; + __le32 *packet = memory; + __le32 *result = memory + PIODMA_DATA_OFFSET; + __le32 *completion = memory + PIODMA_COMPLETION_OFFSET; + u64 dma = diag->dma + index * PIODMA_SLOT_SIZE; + u64 source = diag->config_source + ((u64)request->function << 12) + request->offset; + u64 dst = dma + PIODMA_DATA_OFFSET; + u64 src = source & PIODMA_PACKET_MASK; + u64 descriptor = dma + diag->pointer_prefix; + u64 comp = descriptor + PIODMA_COMPLETION_OFFSET; + unsigned long waited; + u32 fifo, command, tag = index + 1; + int i, ret; + + ret = apple_piodma_diag_check_slots(diag); + if (ret || READ_ONCE(diag->irq_fault)) + return ret ?: -EIO; + /* Only these fixed, validated ECAM addresses use aperture1/low36. */ + slot->packet[0] = cpu_to_le32(0xc007 | BIT(18) | ((src & 0xff) << 24)); + slot->packet[1] = cpu_to_le32((src >> 8) | ((dst & 0xf) << 28)); + slot->packet[2] = cpu_to_le32(dst >> 4); + slot->packet[3] = cpu_to_le32(sizeof(u32)); + memcpy(packet, slot->packet, sizeof(slot->packet)); + dma_wmb(); + ret = readl_poll_timeout(diag->engine + 0x0c, fifo, + (fifo & PIODMA_FIFO_MASK) == 16, 10, 50000); + if (ret || READ_ONCE(diag->irq_fault)) + return ret ?: -EIO; + slot->submitted = true; + WRITE_ONCE(diag->active_tag, tag); + dev_info(diag->dev, + "request=%u function=%u offset=%#x source=%#llx packet=%08x:%08x:%08x:%08x tag=%u\n", + index, request->function, request->offset, source, + le32_to_cpu(packet[0]), le32_to_cpu(packet[1]), + le32_to_cpu(packet[2]), le32_to_cpu(packet[3]), tag); + writel(tag, diag->engine + 0xd8); + writel(upper_32_bits(descriptor) & 0x3ff, diag->engine + 0x50); + writel(lower_32_bits(descriptor), diag->engine + 0x4c); + writel(upper_32_bits(comp) & 0x3ff, diag->engine + 0x48); + writel(lower_32_bits(comp), diag->engine + 0x44); + writel(0x411, diag->engine + 0x54); + waited = wait_for_completion_timeout(&slot->event, msecs_to_jiffies(50)); + dma_rmb(); + slot->result = le32_to_cpu(READ_ONCE(*result)); + for (i = 0; i < ARRAY_SIZE(slot->completion); i++) + slot->completion[i] = READ_ONCE(completion[i]); + slot->observed = true; + dev_info(diag->dev, + "response=%u waited=%lu matched_status=%#x word=%#x completion=%08x:%08x:%08x:%08x\n", + index, waited, READ_ONCE(slot->matched_status), slot->result, + le32_to_cpu(slot->completion[0]), le32_to_cpu(slot->completion[1]), + le32_to_cpu(slot->completion[2]), le32_to_cpu(slot->completion[3])); + if (!waited) + return -ETIMEDOUT; + if (READ_ONCE(diag->irq_fault) || !READ_ONCE(slot->matched_status) || + slot->result == U32_MAX || + (slot->result & request->mask) != request->expected) + return -EIO; + /* Admission observations only: all published memory remains retained. */ + ret = readl_poll_timeout(diag->engine + 0x54, command, !(command & BIT(0)), + 10, 50000); + if (ret) + return ret; + ret = readl_poll_timeout(diag->engine + 0x0c, fifo, + (fifo & PIODMA_FIFO_MASK) == 16, 10, 50000); + if (ret) + return ret; + for (i = 0; i < 10; i++) { + usleep_range(1000, 2000); + ret = apple_piodma_diag_check_slots(diag); + if (ret || READ_ONCE(diag->irq_fault)) + return ret ?: -EIO; + } + dev_info(diag->dev, + "READ_VALIDATED request=%u word=%#x FIFO=%#x command=%#x; retained slots stable\n", + index, slot->result, fifo, command); + return 0; +} + +bool apple_piodma_bootstrap_enabled(void) +{ + return enumerate; +} + +static void apple_piodma_put_supplier(void *data) +{ + put_device(data); +} + +int apple_piodma_bootstrap_get(struct device *host, struct device **supplier) +{ + struct device_node *node, *host_node; + struct platform_device *pdev; + struct device_link *link; + bool correct_host; + int ret; + + if (!enumerate) + return -ENODEV; + node = of_parse_phandle(host->of_node, "apple,piodma", 0); + if (!node) + return -EINVAL; + if (!of_device_is_compatible(node, "apple,t8140-piodma-diagnostic")) { + of_node_put(node); + return -EINVAL; + } + host_node = of_parse_phandle(node, "apple,pcie-host", 0); + correct_host = host_node == host->of_node; + of_node_put(host_node); + pdev = correct_host ? of_find_device_by_node(node) : NULL; + of_node_put(node); + if (!correct_host) + return -EINVAL; + if (!pdev) + return -EPROBE_DEFER; + if (!device_is_bound(&pdev->dev) || !platform_get_drvdata(pdev)) { + put_device(&pdev->dev); + return -EPROBE_DEFER; + } + link = device_link_add(host, &pdev->dev, DL_FLAG_AUTOREMOVE_CONSUMER); + if (!link) { + put_device(&pdev->dev); + return -EINVAL; + } + ret = devm_add_action_or_reset(host, apple_piodma_put_supplier, &pdev->dev); + if (ret) + return ret; + *supplier = &pdev->dev; + return 0; +} + +int apple_piodma_bootstrap_prime(struct device *supplier, struct pci_dev *root) +{ + struct apple_piodma_diag *diag = dev_get_drvdata(supplier); + int ret; + + /* One attempt for this provider's entire lifetime, including failures. */ + if (atomic_cmpxchg(&diag->attempted, 0, 1)) + return -EALREADY; + ret = apple_piodma_diag_root(diag, root); + if (ret) + return ret; + ret = apple_piodma_diag_idle(diag); + if (ret) + return ret; + ret = apple_piodma_diag_initialize(diag); + if (!ret) + ret = apple_piodma_diag_submit(diag, 0); + if (ret) + dev_err(supplier, "bootstrap failure=%d; retained ownership; no retry\n", ret); + else + dev_info(supplier, "BOOTSTRAP_VALIDATED word=%#x; one retained slot\n", + diag->slots[0].result); + return ret; +} + +static int apple_piodma_diag_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct apple_piodma_diag *diag; + int i, ret; + + if (!enumerate || !of_machine_is_compatible("apple,j700") || + !of_machine_is_compatible("apple,t8140") || PAGE_SIZE != SZ_16K) + return -ENODEV; + diag = kzalloc_obj(*diag); + if (!diag) + return -ENOMEM; + diag->dev = dev; + for (i = 0; i < PIODMA_REQUEST_COUNT; i++) + init_completion(&diag->slots[i].event); + atomic_set(&diag->irq_count, 0); + atomic_set(&diag->attempted, 0); + ret = apple_piodma_diag_iommu(diag); + if (ret) + goto err_free; + pm_runtime_enable(dev); + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) + goto err_pm; + ret = apple_piodma_diag_arena(diag); + if (ret) + goto err_arena; + ret = apple_piodma_diag_resources(pdev, diag); + if (ret) + goto err_arena; + ret = devm_request_irq(dev, diag->irq, apple_piodma_diag_irq, 0, + dev_name(dev), diag); + if (ret) + goto err_arena; + platform_set_drvdata(pdev, diag); + dev_info(dev, "bootstrap supplier ready; no command submitted during probe\n"); + return 0; + +err_arena: + /* No path after initialization may release ownership. */ + if (WARN_ON_ONCE(diag->retained)) + return 0; + if (diag->arena) + dma_free_coherent(dev, PIODMA_ARENA_SIZE, diag->arena, diag->dma); + pm_runtime_put_sync(dev); +err_pm: + pm_runtime_disable(dev); +err_free: + kfree(diag); + return dev_err_probe(dev, ret, "PIODMA diagnostic preflight failed\n"); +} + +static void apple_piodma_diag_shutdown(struct platform_device *pdev) +{ + dev_warn(&pdev->dev, "diagnostic arena retained; require full hardware reset\n"); +} + +static const struct of_device_id apple_piodma_diag_match[] = { + { .compatible = "apple,t8140-piodma-diagnostic" }, + { } +}; + +static struct platform_driver apple_piodma_diag_driver = { + .probe = apple_piodma_diag_probe, + .shutdown = apple_piodma_diag_shutdown, + .driver = { + .name = "apple-piodma-diag", + .of_match_table = apple_piodma_diag_match, + .suppress_bind_attrs = true, + }, +}; +builtin_platform_driver(apple_piodma_diag_driver); +MODULE_DESCRIPTION("T8140 retained one-read PCIe bootstrap experiment"); +MODULE_LICENSE("GPL"); diff --git a/drivers/pci/controller/pcie-apple-piodma-diag.h b/drivers/pci/controller/pcie-apple-piodma-diag.h new file mode 100644 index 0000000000000..46a1b03aa2496 --- /dev/null +++ b/drivers/pci/controller/pcie-apple-piodma-diag.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef PCIE_APPLE_PIODMA_DIAG_H +#define PCIE_APPLE_PIODMA_DIAG_H + +#include +#include +#include + +struct device; +struct pci_dev; + +#if IS_ENABLED(CONFIG_PCIE_APPLE_PIODMA_DIAG) +bool apple_piodma_bootstrap_enabled(void); +int apple_piodma_bootstrap_get(struct device *host, struct device **supplier); +int apple_piodma_bootstrap_prime(struct device *supplier, struct pci_dev *root); +#else +static inline bool apple_piodma_bootstrap_enabled(void) +{ + return false; +} + +static inline int apple_piodma_bootstrap_get(struct device *host, struct device **supplier) +{ + return -ENODEV; +} + +static inline int apple_piodma_bootstrap_prime(struct device *supplier, struct pci_dev *root) +{ + return -ENODEV; +} +#endif +#endif diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 67f3eeac2e0d9..aeb228959a838 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -36,6 +36,7 @@ #include "../pci.h" #include "pci-host-common.h" +#include "pcie-apple-piodma-diag.h" static int link_up_timeout = 500; module_param(link_up_timeout, int, 0644); @@ -168,6 +169,11 @@ struct hw_info { u32 port_rid2sid; u32 port_msimap; u32 max_rid2sid; + u32 pwren_off_ms; + u32 pwren_on_ms; + bool sid_indexed_rid2sid; + bool retain_clocks; + bool root_bus_only; bool tunneled; }; @@ -194,6 +200,24 @@ static const struct hw_info t602x_hw = { .max_rid2sid = 512, }; +/* + * T8140 uses the newer port register layout and a SID-indexed RID table. + * Keep the PHY and application clock policy established by the bootloader; + * runtime clock gating has not been qualified on this controller. + */ +static const struct hw_info t8140_hw = { + .port_msiaddr = PORT_T602X_MSIADDR, + .port_perst = PORT_T602X_PERST, + .port_rid2sid = PORT_T602X_RID2SID, + .port_msimap = PORT_T602X_MSIMAP, + .max_rid2sid = 19, + .pwren_off_ms = 2, + .pwren_on_ms = 150, + .sid_indexed_rid2sid = true, + .retain_clocks = true, + .root_bus_only = true, +}; + static const struct hw_info t8103_pciec_hw = { .port_msiaddr = PORT_MSIADDR, .port_perst = PORT_PERST, @@ -215,6 +239,9 @@ struct apple_pcie { struct apple_tunable *debug_tunable; bool power_retained; bool bus_stopped; + bool neo_config_ready; + u8 neo_secondary_bus; + struct device *piodma_supplier; const struct hw_info *hw; unsigned long *bitmap; struct list_head ports; @@ -1114,25 +1141,40 @@ static int apple_pcie_setup_link(struct apple_pcie *pcie, for (u32 idx = 0; idx < num_aux_resets; idx++) gpiod_set_value_cansleep(aux_reset[idx], 1); - /* Power on the device if required */ - gpiod_set_value_cansleep(pwren, 1); - - ret = apple_pcie_setup_refclk(pcie, port); - if (ret < 0) + /* Power-cycle devices which cannot inherit their firmware state. */ + if (pcie->hw->pwren_off_ms) { + if (!pwren) + return -EINVAL; + rmw_clear(PORT_PERST_OFF, port->base + pcie->hw->port_perst); + ret = gpiod_set_value_cansleep(pwren, 0); + if (ret) + return ret; + msleep(pcie->hw->pwren_off_ms); + } + ret = gpiod_set_value_cansleep(pwren, 1); + if (ret) return ret; + if (!pcie->hw->retain_clocks) { + ret = apple_pcie_setup_refclk(pcie, port); + if (ret < 0) + return ret; + } + /* * The minimal Tperst-clk value is 100us (PCIe CEM r5.0, 2.9.2) * If powering up, the minimal Tpvperl is 100ms */ if (pwren) - msleep(100); + msleep(pcie->hw->pwren_on_ms ?: 100); else usleep_range(100, 200); /* Deassert PERST# */ rmw_set(PORT_PERST_OFF, port->base + pcie->hw->port_perst); - gpiod_set_value_cansleep(reset, 0); + ret = gpiod_set_value_cansleep(reset, 0); + if (ret) + return ret; for (u32 idx = 0; idx < num_aux_resets; idx++) gpiod_set_value_cansleep(aux_reset[idx], 0); @@ -1259,6 +1301,15 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, goto err_teardown; } } else { + if (pcie->hw->retain_clocks) + rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK); + if (pcie->hw->retain_clocks && + !(apple_pcie_port_readl(port, PORT_STATUS) & PORT_STATUS_READY)) { + ret = dev_err_probe(pcie->dev, -ENODEV, + "port %pOF requires bootloader initialization\n", + np); + goto err_teardown; + } /* U-Boot may already have brought up a conventional root port. */ link_stat = apple_pcie_port_readl(port, PORT_LINKSTS); if (!(link_stat & PORT_LINKSTS_UP)) { @@ -1268,14 +1319,18 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, } } - if (pcie->hw->port_refclk) - rmw_clear(PORT_REFCLK_CGDIS, port->base + pcie->hw->port_refclk); - else if (port->phy) - rmw_set(PHY_LANE_CFG_REFCLKCGEN, port->phy + PHY_LANE_CFG); - - /* The preinitialized PCIe-C APPCLK state is part of the m1n1 handoff. */ - if (!pcie->hw->tunneled) - rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK); + if (!pcie->hw->retain_clocks) { + if (pcie->hw->port_refclk) + rmw_clear(PORT_REFCLK_CGDIS, + port->base + pcie->hw->port_refclk); + else if (port->phy) + rmw_set(PHY_LANE_CFG_REFCLKCGEN, + port->phy + PHY_LANE_CFG); + + /* PCIe-C APPCLK state is part of the m1n1 handoff. */ + if (!pcie->hw->tunneled) + rmw_clear(PORT_APPCLK_CGDIS, port->base + PORT_APPCLK); + } ret = apple_pcie_port_setup_irq(port); if (ret) @@ -1323,6 +1378,11 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie, } if (pcie->hw->tunneled) port->started = true; + if (pcie->hw->root_bus_only) + dev_info(pcie->dev, + "port %pOF RUN=%#x link=%#x; downstream config blocked\n", + np, apple_pcie_port_readl(port, PORT_STATUS), + apple_pcie_port_readl(port, PORT_LINKSTS)); return 0; @@ -1438,7 +1498,7 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d { struct apple_pcie *pcie = pci_host_bridge_priv(bridge); struct resource *res; - u32 sid, rid = pci_dev_id(pdev); + u32 sid, val, readback, rid = pci_dev_id(pdev); struct apple_pcie_port *port; int idx, err; @@ -1466,11 +1526,28 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d mutex_lock(&port->pcie->lock); - idx = bitmap_find_free_region(port->sid_map, port->sid_map_sz, 0); + if (port->pcie->hw->sid_indexed_rid2sid) { + if (sid >= port->sid_map_sz) + idx = -EINVAL; + else if (test_and_set_bit(sid, port->sid_map)) + idx = -EBUSY; + else + idx = sid; + } else { + idx = bitmap_find_free_region(port->sid_map, port->sid_map_sz, 0); + if (idx < 0) + idx = -ENOSPC; + } if (idx >= 0) { - apple_pcie_rid2sid_write(port, idx, - PORT_RID2SID_VALID | - (sid << PORT_RID2SID_SID_SHIFT) | rid); + val = PORT_RID2SID_VALID | (sid << PORT_RID2SID_SID_SHIFT) | rid; + readback = apple_pcie_rid2sid_write(port, idx, val); + if (pcie->hw->sid_indexed_rid2sid && readback != val) { + dev_err(&pdev->dev, "RID-to-SID readback %#x expected %#x\n", + readback, val); + /* Reserve the failed slot; do not enable or recycle it. */ + mutex_unlock(&port->pcie->lock); + return -EIO; + } dev_dbg(&pdev->dev, "mapping RID%x to SID%x (index %d)\n", rid, sid, idx); @@ -1478,7 +1555,7 @@ static int apple_pcie_enable_device(struct pci_host_bridge *bridge, struct pci_d mutex_unlock(&port->pcie->lock); - return idx >= 0 ? 0 : -ENOSPC; + return idx >= 0 ? 0 : idx; } static void apple_pcie_disable_device(struct pci_host_bridge *bridge, struct pci_dev *pdev) @@ -1531,12 +1608,45 @@ static int apple_pcie_init(struct pci_config_window *cfg) return 0; } +/* + * The T8140 experiment starts with all downstream accesses blocked. + * Only a successful owned bootstrap and typed ECAM checks open functions0/1. + * Generic config callbacks retain their normal native widths and locking. + */ +static void __iomem *apple_pcie_map_bus(struct pci_bus *bus, + unsigned int devfn, int where) +{ + struct pci_config_window *cfg = bus->sysdata; + struct apple_pcie *pcie = apple_pcie_lookup(cfg->parent); + struct apple_pcie_port *port; + + if (!pcie->hw->root_bus_only) + return pci_ecam_map_bus(bus, devfn, where); + + if (bus->number != cfg->busr.start) { + /* Pair with ready publication before consuming the bus number. */ + if (!smp_load_acquire(&pcie->neo_config_ready) || + bus->number != pcie->neo_secondary_bus || + PCI_SLOT(devfn) || PCI_FUNC(devfn) > 1) + return NULL; + return pci_ecam_map_bus(bus, devfn, where); + } + if (PCI_FUNC(devfn)) + return NULL; + + list_for_each_entry(port, &pcie->ports, entry) + if (port->idx == PCI_SLOT(devfn)) + return pci_ecam_map_bus(bus, devfn, where); + + return NULL; +} + static const struct pci_ecam_ops apple_pcie_cfg_ecam_ops = { .init = apple_pcie_init, .enable_device = apple_pcie_enable_device, .disable_device = apple_pcie_disable_device, .pci_ops = { - .map_bus = pci_ecam_map_bus, + .map_bus = apple_pcie_map_bus, .read = pci_generic_config_read, .write = pci_generic_config_write, } @@ -1751,6 +1861,158 @@ static int apple_pcie_tunnel_add_links(struct apple_pcie *pcie) return 0; } +static int apple_pcie_neo_typed_ids(struct apple_pcie *pcie, struct pci_dev *root) +{ + static const u32 ids[] = { 0x793214c3, 0x793b14c3 }; + void __iomem *cfg; + u32 id; + u16 vendor, device; + u8 low, high, header; + int function; + + for (function = 0; function < ARRAY_SIZE(ids); function++) { + /* Bypass the closed guard only for these fixed validation reads. */ + cfg = pci_ecam_map_bus(root->subordinate, PCI_DEVFN(0, function), 0); + if (!cfg) + return -ENODEV; + id = readl(cfg + PCI_VENDOR_ID); + vendor = readw(cfg + PCI_VENDOR_ID); + device = readw(cfg + PCI_DEVICE_ID); + low = readb(cfg + PCI_VENDOR_ID); + high = readb(cfg + PCI_VENDOR_ID + 1); + header = readb(cfg + PCI_HEADER_TYPE); + dev_info(pcie->dev, + "typed ECAM function=%d id=%#x vendor=%#x device=%#x bytes=%02x:%02x header=%#x\n", + function, id, vendor, device, low, high, header); + if (id != ids[function] || vendor != (ids[function] & 0xffff) || + device != ids[function] >> 16 || low != 0xc3 || high != 0x14 || + (header & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_NORMAL || + (!function && !(header & PCI_HEADER_TYPE_MFD))) + return -EIO; + } + return 0; +} + +static int apple_pcie_neo_check_memory(struct pci_dev *root) +{ + struct pci_dev *endpoint; + struct pci_bus_region region; + struct resource *res; + u32 window = U32_MAX; + u64 base, limit; + u16 command = U16_MAX; + int function, bar, ret = 0; + + ret = pci_read_config_word(root, PCI_COMMAND, &command); + if (!ret) + ret = pci_read_config_dword(root, PCI_MEMORY_BASE, &window); + pci_info(root, "NEO_ROOT_MEMORY_READBACK status=%d command=%#x window=%#x\n", + ret, command, window); + if (ret || command == U16_MAX || !(command & PCI_COMMAND_MEMORY) || window == U32_MAX) + return -EIO; + /* Each packed register field is12 bits; PCI_MEMORY_RANGE_MASK is unsigned long. */ + base = (u64)(window & GENMASK(15, 4)) << 16; + limit = (window & GENMASK(31, 20)) | GENMASK(19, 0); + if (base > limit) + return -EIO; + for (function = 0; function < 2; function++) { + endpoint = pci_get_slot(root->subordinate, PCI_DEVFN(0, function)); + if (!endpoint) + return -ENODEV; + for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { + res = &endpoint->resource[bar]; + if (!(res->flags & IORESOURCE_MEM)) + continue; + pcibios_resource_to_bus(endpoint->bus, ®ion, res); + if (!res->parent || !resource_size(res) || + res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED | IORESOURCE_PREFETCH) || + region.start < base || region.end > limit) { + pci_err(endpoint, "BAR%d outside programmed root memory window\n", bar); + ret = -ERANGE; + break; + } + } + pci_dev_put(endpoint); + if (ret) + return ret; + } + pci_info(root, "NEO_ROOT_MEMORY_ENABLED command=%#x window=%#x bus=%#llx-%#llx\n", + command, window, base, limit); + return 0; +} + +static int apple_pcie_neo_enumerate(struct pci_host_bridge *bridge) +{ + struct apple_pcie *pcie = pci_host_bridge_priv(bridge); + struct pci_dev *root, *endpoint; + u16 command; + int function, ret; + + root = pci_get_slot(bridge->bus, PCI_DEVFN(0, 0)); + if (!root) + return -ENODEV; + if (!root->subordinate || root->subordinate->number != 1) { + ret = -EINVAL; + goto out; + } + /* No config/raw spinlock is held across this process-context request. */ + ret = apple_piodma_bootstrap_prime(pcie->piodma_supplier, root); + if (ret) + goto out; + ret = apple_pcie_neo_typed_ids(pcie, root); + if (ret) + goto out; + pcie->neo_secondary_bus = root->subordinate->number; + pci_lock_rescan_remove(); + /* Publish validated bootstrap state and bus number before config access. */ + smp_store_release(&pcie->neo_config_ready, true); + /* Root was already enabled by the closed scan. The bridge-specific API + * programs its new windows even when the generic bus rescan would skip + * pci_setup_bridge(). Publish children only after hardware validation. + */ + pci_scan_child_bus(root->subordinate); + pci_assign_unassigned_bridge_resources(root); + ret = apple_pcie_neo_check_memory(root); + if (ret) { + dev_err(pcie->dev, "root memory forwarding unavailable after assignment: %d\n", ret); + goto close_config; + } + pci_bus_add_devices(root->subordinate); + for (function = 0; function < 2; function++) { + endpoint = pci_get_slot(root->subordinate, PCI_DEVFN(0, function)); + if (!endpoint) { + ret = -ENODEV; + break; + } + if (pci_read_config_word(endpoint, PCI_COMMAND, &command)) { + ret = -EIO; + } else { + dev_info(pcie->dev, "enumerated %s id=%04x:%04x command=%#x driver=%s\n", + pci_name(endpoint), endpoint->vendor, endpoint->device, command, + endpoint->driver ? endpoint->driver->name : "unbound"); + if (command & PCI_COMMAND_MASTER) { + /* This experiment never permits endpoint bus mastering. */ + pci_clear_master(endpoint); + ret = -EIO; + } + if (endpoint->driver) + ret = -EBUSY; + } + pci_dev_put(endpoint); + if (ret) + break; + } +close_config: + if (ret) + WRITE_ONCE(pcie->neo_config_ready, false); + pci_unlock_rescan_remove(); + if (!ret) + dev_info(pcie->dev, "NEO_ENUMERATION_ONLY_READY; functions0/1 unbound, bus mastering off\n"); +out: + pci_dev_put(root); + return ret; +} + static int apple_pcie_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -1758,6 +2020,7 @@ static int apple_pcie_probe(struct platform_device *pdev) struct pci_host_bridge *bridge; struct device_node *of_port; struct apple_pcie *pcie; + struct device *piodma_supplier = NULL; int ret; hw = of_device_get_match_data(dev); @@ -1782,6 +2045,12 @@ static int apple_pcie_probe(struct platform_device *pdev) } } + if (hw->root_bus_only && apple_piodma_bootstrap_enabled()) { + ret = apple_piodma_bootstrap_get(dev, &piodma_supplier); + if (ret) + return dev_err_probe(dev, ret, "PIODMA supplier unavailable\n"); + } + bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); if (!bridge) return -ENOMEM; @@ -1789,6 +2058,9 @@ static int apple_pcie_probe(struct platform_device *pdev) pcie = pci_host_bridge_priv(bridge); pcie->dev = dev; pcie->hw = hw; + pcie->piodma_supplier = piodma_supplier; + if (hw->root_bus_only) + dev_info(dev, "root-port probe only; downstream config is blocked\n"); pcie->base = devm_platform_ioremap_resource(pdev, 1); if (IS_ERR(pcie->base)) return PTR_ERR(pcie->base); @@ -1821,6 +2093,15 @@ static int apple_pcie_probe(struct platform_device *pdev) if (ret) return ret; + if (pcie->piodma_supplier) { + /* All fallible devres setup precedes publication of DMA pointers. */ + ret = apple_pcie_neo_enumerate(bridge); + if (ret) + dev_err(dev, "enumeration experiment failed=%d; host/supplier retained, no retry\n", + ret); + return 0; + } + if (pcie->hw->tunneled) { pci_walk_bus(bridge->bus, apple_pcie_tunnel_keep_d0, NULL); return apple_pcie_tunnel_add_links(pcie); @@ -1994,6 +2275,7 @@ static const struct of_device_id apple_pcie_of_match[] = { { .compatible = "apple,t8103-pciec", .data = &t8103_pciec_hw }, { .compatible = "apple,t6000-pciec", .data = &t8103_pciec_hw }, { .compatible = "apple,t6020-pcie", .data = &t602x_hw }, + { .compatible = "apple,t8140-pcie", .data = &t8140_hw }, { .compatible = "apple,pcie", .data = &t8103_hw }, { } };