Skip to content

Commit 2410e33

Browse files
chenhuacaibjorn-helgaas
authored andcommitted
PCI: loongson: Don't access non-existent devices
On LS2K/LS7A, config reads to some non-existent devices don't return 0xffffffff (they are hidden devices for debug, and accessing the config space may cause machine hang). This is a hardware flaw but we can only avoid it by software now. Link: https://lore.kernel.org/r/20220714124216.1489304-5-chenhuacai@loongson.cn Signed-off-by: Huacai Chen <chenhuacai@loongson.cn> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent cd89edd commit 2410e33

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

drivers/pci/controller/pci-loongson.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define FLAG_CFG0 BIT(0)
2727
#define FLAG_CFG1 BIT(1)
2828
#define FLAG_DEV_FIX BIT(2)
29+
#define FLAG_DEV_HIDDEN BIT(3)
2930

3031
struct loongson_pci_data {
3132
u32 flags;
@@ -138,18 +139,34 @@ static void __iomem *cfg1_map(struct loongson_pci *priv, struct pci_bus *bus,
138139
return priv->cfg1_base + addroff;
139140
}
140141

142+
static bool pdev_may_exist(struct pci_bus *bus, unsigned int device,
143+
unsigned int function)
144+
{
145+
return !(pci_is_root_bus(bus) &&
146+
(device >= 9 && device <= 20) && (function > 0));
147+
}
148+
141149
static void __iomem *pci_loongson_map_bus(struct pci_bus *bus,
142150
unsigned int devfn, int where)
143151
{
152+
unsigned int device = PCI_SLOT(devfn);
153+
unsigned int function = PCI_FUNC(devfn);
144154
struct loongson_pci *priv = pci_bus_to_loongson_pci(bus);
145155

146156
/*
147157
* Do not read more than one device on the bus other than
148158
* the host bus.
149159
*/
150-
if (priv->data->flags & FLAG_DEV_FIX &&
151-
!pci_is_root_bus(bus) && PCI_SLOT(devfn) > 0)
152-
return NULL;
160+
if ((priv->data->flags & FLAG_DEV_FIX) && bus->self) {
161+
if (!pci_is_root_bus(bus) && (device > 0))
162+
return NULL;
163+
}
164+
165+
/* Don't access non-existent devices */
166+
if (priv->data->flags & FLAG_DEV_HIDDEN) {
167+
if (!pdev_may_exist(bus, device, function))
168+
return NULL;
169+
}
153170

154171
/* CFG0 can only access standard space */
155172
if (where < PCI_CFG_SPACE_SIZE && priv->cfg0_base)
@@ -197,12 +214,12 @@ static struct pci_ops loongson_pci_ops32 = {
197214
};
198215

199216
static const struct loongson_pci_data ls2k_pci_data = {
200-
.flags = FLAG_CFG1 | FLAG_DEV_FIX,
217+
.flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
201218
.ops = &loongson_pci_ops,
202219
};
203220

204221
static const struct loongson_pci_data ls7a_pci_data = {
205-
.flags = FLAG_CFG1 | FLAG_DEV_FIX,
222+
.flags = FLAG_CFG1 | FLAG_DEV_FIX | FLAG_DEV_HIDDEN,
206223
.ops = &loongson_pci_ops,
207224
};
208225

@@ -297,7 +314,7 @@ static int loongson_pci_ecam_init(struct pci_config_window *cfg)
297314
return -ENOMEM;
298315

299316
cfg->priv = priv;
300-
data->flags = FLAG_CFG1;
317+
data->flags = FLAG_CFG1 | FLAG_DEV_HIDDEN;
301318
priv->data = data;
302319
priv->cfg1_base = cfg->win - (cfg->busr.start << 16);
303320

0 commit comments

Comments
 (0)