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];
294 unsigned long a, size;
295 u32 parent_phys_hi, parent_phys_lo;
296 u32 size_hi, size_lo;
299 parent_phys_hi = pr->parent_phys_hi;
300 parent_phys_lo = pr->parent_phys_lo;
301 if (tlb_type == hypervisor)
302 parent_phys_hi &= 0x0fffffff;
304 size_hi = pr->size_hi;
305 size_lo = pr->size_lo;
307 type = (pr->child_phys_hi >> 24) & 0x3;
308 a = (((unsigned long)parent_phys_hi << 32UL) |
309 ((unsigned long)parent_phys_lo << 0UL));
310 size = (((unsigned long)size_hi << 32UL) |
311 ((unsigned long)size_lo << 0UL));
315 /* PCI config space, 16MB */
316 pbm->config_space = a;
320 /* 16-bit IO space, 16MB */
321 pbm->io_space.start = a;
322 pbm->io_space.end = a + size - 1UL;
323 pbm->io_space.flags = IORESOURCE_IO;
328 /* 32-bit MEM space, 2GB */
329 pbm->mem_space.start = a;
330 pbm->mem_space.end = a + size - 1UL;
331 pbm->mem_space.flags = IORESOURCE_MEM;
336 /* XXX 64-bit MEM handling XXX */
343 if (!saw_io || !saw_mem) {
344 prom_printf("%s: Fatal error, missing %s PBM range.\n",
346 (!saw_io ? "IO" : "MEM"));
350 printk("%s: PCI IO[%lx] MEM[%lx]\n",
353 pbm->mem_space.start);
355 pbm->io_space.name = pbm->mem_space.name = pbm->name;
357 request_resource(&ioport_resource, &pbm->io_space);
358 request_resource(&iomem_resource, &pbm->mem_space);
360 pci_register_legacy_regions(&pbm->io_space,
362 pci_register_iommu_region(pbm);
365 /* Generic helper routines for PCI error reporting. */
366 void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
367 struct pci_bus *pbus)
369 struct pci_dev *pdev;
372 list_for_each_entry(pdev, &pbus->devices, bus_list) {
373 u16 status, error_bits;
375 pci_read_config_word(pdev, PCI_STATUS, &status);
377 (status & (PCI_STATUS_SIG_TARGET_ABORT |
378 PCI_STATUS_REC_TARGET_ABORT));
380 pci_write_config_word(pdev, PCI_STATUS, error_bits);
381 printk("%s: Device %s saw Target Abort [%016x]\n",
382 pbm->name, pci_name(pdev), status);
386 list_for_each_entry(bus, &pbus->children, node)
387 pci_scan_for_target_abort(pbm, bus);
390 void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
391 struct pci_bus *pbus)
393 struct pci_dev *pdev;
396 list_for_each_entry(pdev, &pbus->devices, bus_list) {
397 u16 status, error_bits;
399 pci_read_config_word(pdev, PCI_STATUS, &status);
401 (status & (PCI_STATUS_REC_MASTER_ABORT));
403 pci_write_config_word(pdev, PCI_STATUS, error_bits);
404 printk("%s: Device %s received Master Abort [%016x]\n",
405 pbm->name, pci_name(pdev), status);
409 list_for_each_entry(bus, &pbus->children, node)
410 pci_scan_for_master_abort(pbm, bus);
413 void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
414 struct pci_bus *pbus)
416 struct pci_dev *pdev;
419 list_for_each_entry(pdev, &pbus->devices, bus_list) {
420 u16 status, error_bits;
422 pci_read_config_word(pdev, PCI_STATUS, &status);
424 (status & (PCI_STATUS_PARITY |
425 PCI_STATUS_DETECTED_PARITY));
427 pci_write_config_word(pdev, PCI_STATUS, error_bits);
428 printk("%s: Device %s saw Parity Error [%016x]\n",
429 pbm->name, pci_name(pdev), status);
433 list_for_each_entry(bus, &pbus->children, node)
434 pci_scan_for_parity_error(pbm, bus);