1 /* pci_common.c: PCI controller common support.
3 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
6 #include <linux/string.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
10 #include <linux/device.h>
13 #include <asm/of_device.h>
14 #include <asm/oplib.h>
17 #include "pci_sun4v.h"
19 static int config_out_of_range(struct pci_pbm_info *pbm,
24 if (bus < pbm->pci_first_busno ||
25 bus > pbm->pci_last_busno)
30 static void *sun4u_config_mkaddr(struct pci_pbm_info *pbm,
35 unsigned long rbits = pbm->config_space_reg_bits;
37 if (config_out_of_range(pbm, bus, devfn, reg))
40 reg = (reg & ((1 << rbits) - 1));
44 return (void *) (pbm->config_space | bus | devfn | reg);
47 static int sun4u_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
48 int where, int size, u32 *value)
50 struct pci_pbm_info *pbm = bus_dev->sysdata;
51 unsigned char bus = bus_dev->number;
56 if (bus_dev == pbm->pci_bus && devfn == 0x00)
57 return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
72 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
74 return PCIBIOS_SUCCESSFUL;
78 pci_config_read8((u8 *)addr, &tmp8);
84 printk("pci_read_config_word: misaligned reg [%x]\n",
86 return PCIBIOS_SUCCESSFUL;
88 pci_config_read16((u16 *)addr, &tmp16);
94 printk("pci_read_config_dword: misaligned reg [%x]\n",
96 return PCIBIOS_SUCCESSFUL;
98 pci_config_read32(addr, value);
101 return PCIBIOS_SUCCESSFUL;
104 static int sun4u_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
105 int where, int size, u32 value)
107 struct pci_pbm_info *pbm = bus_dev->sysdata;
108 unsigned char bus = bus_dev->number;
111 if (bus_dev == pbm->pci_bus && devfn == 0x00)
112 return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
114 addr = sun4u_config_mkaddr(pbm, bus, devfn, where);
116 return PCIBIOS_SUCCESSFUL;
120 pci_config_write8((u8 *)addr, value);
125 printk("pci_write_config_word: misaligned reg [%x]\n",
127 return PCIBIOS_SUCCESSFUL;
129 pci_config_write16((u16 *)addr, value);
134 printk("pci_write_config_dword: misaligned reg [%x]\n",
136 return PCIBIOS_SUCCESSFUL;
138 pci_config_write32(addr, value);
140 return PCIBIOS_SUCCESSFUL;
143 struct pci_ops sun4u_pci_ops = {
144 .read = sun4u_read_pci_cfg,
145 .write = sun4u_write_pci_cfg,
148 static int sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
149 int where, int size, u32 *value)
151 struct pci_pbm_info *pbm = bus_dev->sysdata;
152 u32 devhandle = pbm->devhandle;
153 unsigned int bus = bus_dev->number;
154 unsigned int device = PCI_SLOT(devfn);
155 unsigned int func = PCI_FUNC(devfn);
158 if (bus_dev == pbm->pci_bus && devfn == 0x00)
159 return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
161 if (config_out_of_range(pbm, bus, devfn, where)) {
164 ret = pci_sun4v_config_get(devhandle,
165 HV_PCI_DEVICE_BUILD(bus, device, func),
173 *value = ret & 0xffff;
176 *value = ret & 0xffffffff;
181 return PCIBIOS_SUCCESSFUL;
184 static int sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
185 int where, int size, u32 value)
187 struct pci_pbm_info *pbm = bus_dev->sysdata;
188 u32 devhandle = pbm->devhandle;
189 unsigned int bus = bus_dev->number;
190 unsigned int device = PCI_SLOT(devfn);
191 unsigned int func = PCI_FUNC(devfn);
194 if (bus_dev == pbm->pci_bus && devfn == 0x00)
195 return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
197 if (config_out_of_range(pbm, bus, devfn, where)) {
200 ret = pci_sun4v_config_put(devhandle,
201 HV_PCI_DEVICE_BUILD(bus, device, func),
204 return PCIBIOS_SUCCESSFUL;
207 struct pci_ops sun4v_pci_ops = {
208 .read = sun4v_read_pci_cfg,
209 .write = sun4v_write_pci_cfg,
212 void pci_get_pbm_props(struct pci_pbm_info *pbm)
214 const u32 *val = of_get_property(pbm->prom_node, "bus-range", NULL);
216 pbm->pci_first_busno = val[0];
217 pbm->pci_last_busno = val[1];
219 val = of_get_property(pbm->prom_node, "ino-bitmap", NULL);
221 pbm->ino_bitmap = (((u64)val[1] << 32UL) |
222 ((u64)val[0] << 0UL));
226 static void pci_register_legacy_regions(struct resource *io_res,
227 struct resource *mem_res)
232 p = kzalloc(sizeof(*p), GFP_KERNEL);
236 p->name = "Video RAM area";
237 p->start = mem_res->start + 0xa0000UL;
238 p->end = p->start + 0x1ffffUL;
239 p->flags = IORESOURCE_BUSY;
240 request_resource(mem_res, p);
242 p = kzalloc(sizeof(*p), GFP_KERNEL);
246 p->name = "System ROM";
247 p->start = mem_res->start + 0xf0000UL;
248 p->end = p->start + 0xffffUL;
249 p->flags = IORESOURCE_BUSY;
250 request_resource(mem_res, p);
252 p = kzalloc(sizeof(*p), GFP_KERNEL);
256 p->name = "Video ROM";
257 p->start = mem_res->start + 0xc0000UL;
258 p->end = p->start + 0x7fffUL;
259 p->flags = IORESOURCE_BUSY;
260 request_resource(mem_res, p);
263 static void pci_register_iommu_region(struct pci_pbm_info *pbm)
265 const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
268 struct resource *rp = kmalloc(sizeof(*rp), GFP_KERNEL);
271 prom_printf("Cannot allocate IOMMU resource.\n");
275 rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
276 rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
277 rp->flags = IORESOURCE_BUSY;
278 request_resource(&pbm->mem_space, rp);
282 void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
284 const struct linux_prom_pci_ranges *pbm_ranges;
285 int i, saw_mem, saw_io;
288 saw_mem = saw_io = 0;
289 pbm_ranges = of_get_property(pbm->prom_node, "ranges", &i);
290 num_pbm_ranges = i / sizeof(*pbm_ranges);
292 for (i = 0; i < num_pbm_ranges; i++) {
293 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
295 u32 parent_phys_hi, parent_phys_lo;
298 parent_phys_hi = pr->parent_phys_hi;
299 parent_phys_lo = pr->parent_phys_lo;
300 if (tlb_type == hypervisor)
301 parent_phys_hi &= 0x0fffffff;
303 type = (pr->child_phys_hi >> 24) & 0x3;
304 a = (((unsigned long)parent_phys_hi << 32UL) |
305 ((unsigned long)parent_phys_lo << 0UL));
309 /* PCI config space, 16MB */
310 pbm->config_space = a;
314 /* 16-bit IO space, 16MB */
315 pbm->io_space.start = a;
316 pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
317 pbm->io_space.flags = IORESOURCE_IO;
322 /* 32-bit MEM space, 2GB */
323 pbm->mem_space.start = a;
324 pbm->mem_space.end = a + (0x80000000UL - 1UL);
325 pbm->mem_space.flags = IORESOURCE_MEM;
330 /* XXX 64-bit MEM handling XXX */
337 if (!saw_io || !saw_mem) {
338 prom_printf("%s: Fatal error, missing %s PBM range.\n",
340 (!saw_io ? "IO" : "MEM"));
344 printk("%s: PCI IO[%lx] MEM[%lx]\n",
347 pbm->mem_space.start);
349 pbm->io_space.name = pbm->mem_space.name = pbm->name;
351 request_resource(&ioport_resource, &pbm->io_space);
352 request_resource(&iomem_resource, &pbm->mem_space);
354 pci_register_legacy_regions(&pbm->io_space,
356 pci_register_iommu_region(pbm);
359 /* Generic helper routines for PCI error reporting. */
360 void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
361 struct pci_bus *pbus)
363 struct pci_dev *pdev;
366 list_for_each_entry(pdev, &pbus->devices, bus_list) {
367 u16 status, error_bits;
369 pci_read_config_word(pdev, PCI_STATUS, &status);
371 (status & (PCI_STATUS_SIG_TARGET_ABORT |
372 PCI_STATUS_REC_TARGET_ABORT));
374 pci_write_config_word(pdev, PCI_STATUS, error_bits);
375 printk("%s: Device %s saw Target Abort [%016x]\n",
376 pbm->name, pci_name(pdev), status);
380 list_for_each_entry(bus, &pbus->children, node)
381 pci_scan_for_target_abort(pbm, bus);
384 void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
385 struct pci_bus *pbus)
387 struct pci_dev *pdev;
390 list_for_each_entry(pdev, &pbus->devices, bus_list) {
391 u16 status, error_bits;
393 pci_read_config_word(pdev, PCI_STATUS, &status);
395 (status & (PCI_STATUS_REC_MASTER_ABORT));
397 pci_write_config_word(pdev, PCI_STATUS, error_bits);
398 printk("%s: Device %s received Master Abort [%016x]\n",
399 pbm->name, pci_name(pdev), status);
403 list_for_each_entry(bus, &pbus->children, node)
404 pci_scan_for_master_abort(pbm, bus);
407 void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
408 struct pci_bus *pbus)
410 struct pci_dev *pdev;
413 list_for_each_entry(pdev, &pbus->devices, bus_list) {
414 u16 status, error_bits;
416 pci_read_config_word(pdev, PCI_STATUS, &status);
418 (status & (PCI_STATUS_PARITY |
419 PCI_STATUS_DETECTED_PARITY));
421 pci_write_config_word(pdev, PCI_STATUS, error_bits);
422 printk("%s: Device %s saw Parity Error [%016x]\n",
423 pbm->name, pci_name(pdev), status);
427 list_for_each_entry(bus, &pbus->children, node)
428 pci_scan_for_parity_error(pbm, bus);