|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Primary to Sideband (P2SB) bridge access support |
| 4 | + * |
| 5 | + * Copyright (c) 2017, 2021-2022 Intel Corporation. |
| 6 | + * |
| 7 | + * Authors: Andy Shevchenko <andriy.shevchenko@linux.intel.com> |
| 8 | + * Jonathan Yong <jonathan.yong@intel.com> |
| 9 | + */ |
| 10 | + |
| 11 | +#include <linux/bits.h> |
| 12 | +#include <linux/export.h> |
| 13 | +#include <linux/pci.h> |
| 14 | +#include <linux/platform_data/x86/p2sb.h> |
| 15 | + |
| 16 | +#include <asm/cpu_device_id.h> |
| 17 | +#include <asm/intel-family.h> |
| 18 | + |
| 19 | +#define P2SBC 0xe0 |
| 20 | +#define P2SBC_HIDE BIT(8) |
| 21 | + |
| 22 | +static const struct x86_cpu_id p2sb_cpu_ids[] = { |
| 23 | + X86_MATCH_INTEL_FAM6_MODEL(ATOM_GOLDMONT, PCI_DEVFN(13, 0)), |
| 24 | + {} |
| 25 | +}; |
| 26 | + |
| 27 | +static int p2sb_get_devfn(unsigned int *devfn) |
| 28 | +{ |
| 29 | + const struct x86_cpu_id *id; |
| 30 | + |
| 31 | + id = x86_match_cpu(p2sb_cpu_ids); |
| 32 | + if (!id) |
| 33 | + return -ENODEV; |
| 34 | + |
| 35 | + *devfn = (unsigned int)id->driver_data; |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem) |
| 40 | +{ |
| 41 | + /* Copy resource from the first BAR of the device in question */ |
| 42 | + *mem = pdev->resource[0]; |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +static int p2sb_scan_and_read(struct pci_bus *bus, unsigned int devfn, struct resource *mem) |
| 47 | +{ |
| 48 | + struct pci_dev *pdev; |
| 49 | + int ret; |
| 50 | + |
| 51 | + pdev = pci_scan_single_device(bus, devfn); |
| 52 | + if (!pdev) |
| 53 | + return -ENODEV; |
| 54 | + |
| 55 | + ret = p2sb_read_bar0(pdev, mem); |
| 56 | + |
| 57 | + pci_stop_and_remove_bus_device(pdev); |
| 58 | + return ret; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR |
| 63 | + * @bus: PCI bus to communicate with |
| 64 | + * @devfn: PCI slot and function to communicate with |
| 65 | + * @mem: memory resource to be filled in |
| 66 | + * |
| 67 | + * The BIOS prevents the P2SB device from being enumerated by the PCI |
| 68 | + * subsystem, so we need to unhide and hide it back to lookup the BAR. |
| 69 | + * |
| 70 | + * if @bus is NULL, the bus 0 in domain 0 will be used. |
| 71 | + * If @devfn is 0, it will be replaced by devfn of the P2SB device. |
| 72 | + * |
| 73 | + * Caller must provide a valid pointer to @mem. |
| 74 | + * |
| 75 | + * Locking is handled by pci_rescan_remove_lock mutex. |
| 76 | + * |
| 77 | + * Return: |
| 78 | + * 0 on success or appropriate errno value on error. |
| 79 | + */ |
| 80 | +int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem) |
| 81 | +{ |
| 82 | + struct pci_dev *pdev_p2sb; |
| 83 | + unsigned int devfn_p2sb; |
| 84 | + u32 value = P2SBC_HIDE; |
| 85 | + int ret; |
| 86 | + |
| 87 | + /* Get devfn for P2SB device itself */ |
| 88 | + ret = p2sb_get_devfn(&devfn_p2sb); |
| 89 | + if (ret) |
| 90 | + return ret; |
| 91 | + |
| 92 | + /* if @bus is NULL, use bus 0 in domain 0 */ |
| 93 | + bus = bus ?: pci_find_bus(0, 0); |
| 94 | + |
| 95 | + /* |
| 96 | + * Prevent concurrent PCI bus scan from seeing the P2SB device and |
| 97 | + * removing via sysfs while it is temporarily exposed. |
| 98 | + */ |
| 99 | + pci_lock_rescan_remove(); |
| 100 | + |
| 101 | + /* Unhide the P2SB device, if needed */ |
| 102 | + pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value); |
| 103 | + if (value & P2SBC_HIDE) |
| 104 | + pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0); |
| 105 | + |
| 106 | + pdev_p2sb = pci_scan_single_device(bus, devfn_p2sb); |
| 107 | + if (devfn) |
| 108 | + ret = p2sb_scan_and_read(bus, devfn, mem); |
| 109 | + else |
| 110 | + ret = p2sb_read_bar0(pdev_p2sb, mem); |
| 111 | + pci_stop_and_remove_bus_device(pdev_p2sb); |
| 112 | + |
| 113 | + /* Hide the P2SB device, if it was hidden */ |
| 114 | + if (value & P2SBC_HIDE) |
| 115 | + pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE); |
| 116 | + |
| 117 | + pci_unlock_rescan_remove(); |
| 118 | + |
| 119 | + if (ret) |
| 120 | + return ret; |
| 121 | + |
| 122 | + if (mem->flags == 0) |
| 123 | + return -ENODEV; |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | +EXPORT_SYMBOL_GPL(p2sb_bar); |
0 commit comments