pci_32.c 7.69 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Common pmac/prep/chrp pci routines. -- Cort
 */

#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/capability.h>
#include <linux/sched.h>
#include <linux/errno.h>
14
#include <linux/memblock.h>
15
#include <linux/syscalls.h>
16
#include <linux/irq.h>
17
#include <linux/list.h>
18
#include <linux/of.h>
19
#include <linux/slab.h>
20
#include <linux/export.h>
21 22 23 24 25 26

#include <asm/processor.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/sections.h>
#include <asm/pci-bridge.h>
27
#include <asm/ppc-pci.h>
28
#include <asm/byteorder.h>
29
#include <linux/uaccess.h>
30 31 32 33 34 35 36
#include <asm/machdep.h>

#undef DEBUG

unsigned long isa_io_base     = 0;
unsigned long pci_dram_offset = 0;
int pcibios_assign_bus_offset = 1;
Al Viro's avatar
Al Viro committed
37 38
EXPORT_SYMBOL(isa_io_base);
EXPORT_SYMBOL(pci_dram_offset);
39 40 41 42 43 44 45 46 47

void pcibios_make_OF_bus_map(void);

static void fixup_cpc710_pci64(struct pci_dev* dev);
static u8* pci_to_OF_bus_map;

/* By default, we don't re-assign bus numbers. We do this only on
 * some pmacs
 */
48
static int pci_assign_all_buses;
49 50 51

static int pci_bus_count;

52 53 54 55 56 57
/* This will remain NULL for now, until isa-bridge.c is made common
 * to both 32-bit and 64-bit.
 */
struct pci_dev *isa_bridge_pcidev;
EXPORT_SYMBOL_GPL(isa_bridge_pcidev);

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static void
fixup_cpc710_pci64(struct pci_dev* dev)
{
	/* Hide the PCI64 BARs from the kernel as their content doesn't
	 * fit well in the resource management
	 */
	dev->resource[0].start = dev->resource[0].end = 0;
	dev->resource[0].flags = 0;
	dev->resource[1].start = dev->resource[1].end = 0;
	dev->resource[1].flags = 0;
}
DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM,	PCI_DEVICE_ID_IBM_CPC710_PCI64,	fixup_cpc710_pci64);

/*
 * Functions below are used on OpenFirmware machines.
 */
static void
make_one_node_map(struct device_node* node, u8 pci_bus)
{
77
	const int *bus_range;
78 79 80 81
	int len;

	if (pci_bus >= pci_bus_count)
		return;
82
	bus_range = of_get_property(node, "bus-range", &len);
83
	if (bus_range == NULL || len < 2 * sizeof(int)) {
84 85
		printk(KERN_WARNING "Can't get bus-range for %pOF, "
		       "assuming it starts at 0\n", node);
86 87 88 89
		pci_to_OF_bus_map[pci_bus] = 0;
	} else
		pci_to_OF_bus_map[pci_bus] = bus_range[0];

90
	for_each_child_of_node(node, node) {
91
		struct pci_dev* dev;
92
		const unsigned int *class_code, *reg;
93
	
94
		class_code = of_get_property(node, "class-code", NULL);
95 96 97
		if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
			(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
			continue;
98
		reg = of_get_property(node, "reg", NULL);
99 100
		if (!reg)
			continue;
101 102
		dev = pci_get_domain_bus_and_slot(0, pci_bus,
						  ((reg[0] >> 8) & 0xff));
103 104
		if (!dev || !dev->subordinate) {
			pci_dev_put(dev);
105
			continue;
106
		}
107
		make_one_node_map(node, dev->subordinate->number);
108
		pci_dev_put(dev);
109 110 111 112 113 114 115
	}
}
	
void
pcibios_make_OF_bus_map(void)
{
	int i;
116
	struct pci_controller *hose, *tmp;
117
	struct property *map_prop;
118
	struct device_node *dn;
119

120
	pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
121 122 123 124 125 126 127 128 129 130 131 132
	if (!pci_to_OF_bus_map) {
		printk(KERN_ERR "Can't allocate OF bus map !\n");
		return;
	}

	/* We fill the bus map with invalid values, that helps
	 * debugging.
	 */
	for (i=0; i<pci_bus_count; i++)
		pci_to_OF_bus_map[i] = 0xff;

	/* For each hose, we begin searching bridges */
133
	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
134 135
		struct device_node* node = hose->dn;

136 137 138 139
		if (!node)
			continue;
		make_one_node_map(node, hose->first_busno);
	}
140 141
	dn = of_find_node_by_path("/");
	map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
142 143 144 145
	if (map_prop) {
		BUG_ON(pci_bus_count > map_prop->length);
		memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
	}
146
	of_node_put(dn);
147 148 149 150 151 152 153 154 155 156 157 158 159 160
#ifdef DEBUG
	printk("PCI->OF bus map:\n");
	for (i=0; i<pci_bus_count; i++) {
		if (pci_to_OF_bus_map[i] == 0xff)
			continue;
		printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
	}
#endif
}


/*
 * Returns the PCI device matching a given OF node
 */
161
int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
162
{
163 164 165 166 167 168
	struct pci_dev *dev = NULL;
	const __be32 *reg;
	int size;

	/* Check if it might have a chance to be a PCI device */
	if (!pci_find_hose_for_OF_device(node))
169
		return -ENODEV;
170 171 172

	reg = of_get_property(node, "reg", &size);
	if (!reg || size < 5 * sizeof(u32))
173
		return -ENODEV;
174 175 176

	*bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff;
	*devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	/* Ok, here we need some tweak. If we have already renumbered
	 * all busses, we can't rely on the OF bus number any more.
	 * the pci_to_OF_bus_map is not enough as several PCI busses
	 * may match the same OF bus number.
	 */
	if (!pci_to_OF_bus_map)
		return 0;

	for_each_pci_dev(dev)
		if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
				dev->devfn == *devfn) {
			*bus = dev->bus->number;
			pci_dev_put(dev);
			return 0;
		}

	return -ENODEV;
}
EXPORT_SYMBOL(pci_device_from_OF_node);

/* We create the "pci-OF-bus-map" property now so it appears in the
 * /proc device tree
 */
void __init
pci_create_OF_bus_map(void)
{
	struct property* of_prop;
205 206
	struct device_node *dn;

207 208
	of_prop = memblock_alloc(sizeof(struct property) + 256,
				 SMP_CACHE_BYTES);
209 210 211
	if (!of_prop)
		panic("%s: Failed to allocate %zu bytes\n", __func__,
		      sizeof(struct property) + 256);
212 213
	dn = of_find_node_by_path("/");
	if (dn) {
214 215 216
		memset(of_prop, -1, sizeof(struct property) + 256);
		of_prop->name = "pci-OF-bus-map";
		of_prop->length = 256;
217
		of_prop->value = &of_prop[1];
218
		of_add_property(dn, of_prop);
219
		of_node_put(dn);
220 221 222
	}
}

223
void pcibios_setup_phb_io_space(struct pci_controller *hose)
224 225 226 227 228
{
	unsigned long io_offset;
	struct resource *res = &hose->io_resource;

	/* Fixup IO space offset */
229 230 231
	io_offset = pcibios_io_space_offset(hose);
	res->start += io_offset;
	res->end += io_offset;
232 233
}

234
static int __init pcibios_init(void)
235
{
236 237
	struct pci_controller *hose, *tmp;
	int next_busno = 0;
238 239 240

	printk(KERN_INFO "PCI: Probing PCI hardware\n");

241
	if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
242 243
		pci_assign_all_buses = 1;

244
	/* Scan all of the recorded PCI controllers.  */
245
	list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
246 247 248
		if (pci_assign_all_buses)
			hose->first_busno = next_busno;
		hose->last_busno = 0xff;
249
		pcibios_scan_phb(hose);
250
		pci_bus_add_devices(hose->bus);
251 252 253 254 255 256 257 258 259
		if (pci_assign_all_buses || next_busno <= hose->last_busno)
			next_busno = hose->last_busno + pcibios_assign_bus_offset;
	}
	pci_bus_count = next_busno;

	/* OpenFirmware based machines need a map of OF bus
	 * numbers vs. kernel bus numbers since we may have to
	 * remap them.
	 */
260
	if (pci_assign_all_buses)
261 262
		pcibios_make_OF_bus_map();

263 264
	/* Call common code to handle resource allocation */
	pcibios_resource_survey();
265 266 267 268 269 270 271 272 273 274

	/* Call machine dependent post-init code */
	if (ppc_md.pcibios_after_init)
		ppc_md.pcibios_after_init();

	return 0;
}

subsys_initcall(pcibios_init);

275
static struct pci_controller*
276 277
pci_bus_to_hose(int bus)
{
278
	struct pci_controller *hose, *tmp;
279

280
	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
281 282 283 284 285 286 287 288 289 290 291
		if (bus >= hose->first_busno && bus <= hose->last_busno)
			return hose;
	return NULL;
}

/* Provide information on locations of various I/O regions in physical
 * memory.  Do this on a per-card basis so that we choose the right
 * root bridge.
 * Note that the returned IO or memory base is a physical address
 */

292 293
SYSCALL_DEFINE3(pciconfig_iobase, long, which,
		unsigned long, bus, unsigned long, devfn)
294 295 296 297 298 299 300 301 302 303 304 305
{
	struct pci_controller* hose;
	long result = -EOPNOTSUPP;

	hose = pci_bus_to_hose(bus);
	if (!hose)
		return -ENODEV;

	switch (which) {
	case IOBASE_BRIDGE_NUMBER:
		return (long)hose->first_busno;
	case IOBASE_MEMORY:
306
		return (long)hose->mem_offset[0];
307 308 309 310 311 312 313 314 315 316
	case IOBASE_IO:
		return (long)hose->io_base_phys;
	case IOBASE_ISA_IO:
		return (long)isa_io_base;
	case IOBASE_ISA_MEM:
		return (long)isa_mem_base;
	}

	return result;
}