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>
9 #include <linux/device.h>
10 #include <linux/of_device.h>
13 #include <asm/oplib.h>
16 #include "pci_sun4v.h"
18 static int config_out_of_range(struct pci_pbm_info
*pbm
,
23 if (bus
< pbm
->pci_first_busno
||
24 bus
> pbm
->pci_last_busno
)
29 static void *sun4u_config_mkaddr(struct pci_pbm_info
*pbm
,
34 unsigned long rbits
= pbm
->config_space_reg_bits
;
36 if (config_out_of_range(pbm
, bus
, devfn
, reg
))
39 reg
= (reg
& ((1 << rbits
) - 1));
43 return (void *) (pbm
->config_space
| bus
| devfn
| reg
);
46 /* At least on Sabre, it is necessary to access all PCI host controller
47 * registers at their natural size, otherwise zeros are returned.
48 * Strange but true, and I see no language in the UltraSPARC-IIi
49 * programmer's manual that mentions this even indirectly.
51 static int sun4u_read_pci_cfg_host(struct pci_pbm_info
*pbm
,
52 unsigned char bus
, unsigned int devfn
,
53 int where
, int size
, u32
*value
)
59 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
61 return PCIBIOS_SUCCESSFUL
;
66 unsigned long align
= (unsigned long) addr
;
69 pci_config_read16((u16
*)align
, &tmp16
);
73 *value
= tmp16
& 0xff;
75 pci_config_read8((u8
*)addr
, &tmp8
);
82 pci_config_read16((u16
*)addr
, &tmp16
);
85 pci_config_read8((u8
*)addr
, &tmp8
);
87 pci_config_read8(((u8
*)addr
) + 1, &tmp8
);
88 *value
|= ((u32
) tmp8
) << 8;
94 sun4u_read_pci_cfg_host(pbm
, bus
, devfn
,
99 sun4u_read_pci_cfg_host(pbm
, bus
, devfn
,
100 where
+ 2, 2, &tmp32
);
101 *value
|= tmp32
<< 16;
104 return PCIBIOS_SUCCESSFUL
;
107 static int sun4u_read_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
108 int where
, int size
, u32
*value
)
110 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
111 unsigned char bus
= bus_dev
->number
;
128 if (!bus_dev
->number
&& !PCI_SLOT(devfn
))
129 return sun4u_read_pci_cfg_host(pbm
, bus
, devfn
, where
,
132 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
134 return PCIBIOS_SUCCESSFUL
;
138 pci_config_read8((u8
*)addr
, &tmp8
);
144 printk("pci_read_config_word: misaligned reg [%x]\n",
146 return PCIBIOS_SUCCESSFUL
;
148 pci_config_read16((u16
*)addr
, &tmp16
);
149 *value
= (u32
) tmp16
;
154 printk("pci_read_config_dword: misaligned reg [%x]\n",
156 return PCIBIOS_SUCCESSFUL
;
158 pci_config_read32(addr
, value
);
161 return PCIBIOS_SUCCESSFUL
;
164 static int sun4u_write_pci_cfg_host(struct pci_pbm_info
*pbm
,
165 unsigned char bus
, unsigned int devfn
,
166 int where
, int size
, u32 value
)
170 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
172 return PCIBIOS_SUCCESSFUL
;
177 unsigned long align
= (unsigned long) addr
;
181 pci_config_read16((u16
*)align
, &tmp16
);
189 pci_config_write16((u16
*)align
, tmp16
);
191 pci_config_write8((u8
*)addr
, value
);
195 pci_config_write16((u16
*)addr
, value
);
197 pci_config_write8((u8
*)addr
, value
& 0xff);
198 pci_config_write8(((u8
*)addr
) + 1, value
>> 8);
202 sun4u_write_pci_cfg_host(pbm
, bus
, devfn
,
203 where
, 2, value
& 0xffff);
204 sun4u_write_pci_cfg_host(pbm
, bus
, devfn
,
205 where
+ 2, 2, value
>> 16);
208 return PCIBIOS_SUCCESSFUL
;
211 static int sun4u_write_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
212 int where
, int size
, u32 value
)
214 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
215 unsigned char bus
= bus_dev
->number
;
218 if (!bus_dev
->number
&& !PCI_SLOT(devfn
))
219 return sun4u_write_pci_cfg_host(pbm
, bus
, devfn
, where
,
222 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
224 return PCIBIOS_SUCCESSFUL
;
228 pci_config_write8((u8
*)addr
, value
);
233 printk("pci_write_config_word: misaligned reg [%x]\n",
235 return PCIBIOS_SUCCESSFUL
;
237 pci_config_write16((u16
*)addr
, value
);
242 printk("pci_write_config_dword: misaligned reg [%x]\n",
244 return PCIBIOS_SUCCESSFUL
;
246 pci_config_write32(addr
, value
);
248 return PCIBIOS_SUCCESSFUL
;
251 struct pci_ops sun4u_pci_ops
= {
252 .read
= sun4u_read_pci_cfg
,
253 .write
= sun4u_write_pci_cfg
,
256 static int sun4v_read_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
257 int where
, int size
, u32
*value
)
259 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
260 u32 devhandle
= pbm
->devhandle
;
261 unsigned int bus
= bus_dev
->number
;
262 unsigned int device
= PCI_SLOT(devfn
);
263 unsigned int func
= PCI_FUNC(devfn
);
266 if (config_out_of_range(pbm
, bus
, devfn
, where
)) {
269 ret
= pci_sun4v_config_get(devhandle
,
270 HV_PCI_DEVICE_BUILD(bus
, device
, func
),
278 *value
= ret
& 0xffff;
281 *value
= ret
& 0xffffffff;
286 return PCIBIOS_SUCCESSFUL
;
289 static int sun4v_write_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
290 int where
, int size
, u32 value
)
292 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
293 u32 devhandle
= pbm
->devhandle
;
294 unsigned int bus
= bus_dev
->number
;
295 unsigned int device
= PCI_SLOT(devfn
);
296 unsigned int func
= PCI_FUNC(devfn
);
298 if (config_out_of_range(pbm
, bus
, devfn
, where
)) {
301 /* We don't check for hypervisor errors here, but perhaps
302 * we should and influence our return value depending upon
303 * what kind of error is thrown.
305 pci_sun4v_config_put(devhandle
,
306 HV_PCI_DEVICE_BUILD(bus
, device
, func
),
309 return PCIBIOS_SUCCESSFUL
;
312 struct pci_ops sun4v_pci_ops
= {
313 .read
= sun4v_read_pci_cfg
,
314 .write
= sun4v_write_pci_cfg
,
317 void pci_get_pbm_props(struct pci_pbm_info
*pbm
)
319 const u32
*val
= of_get_property(pbm
->op
->dev
.of_node
, "bus-range", NULL
);
321 pbm
->pci_first_busno
= val
[0];
322 pbm
->pci_last_busno
= val
[1];
324 val
= of_get_property(pbm
->op
->dev
.of_node
, "ino-bitmap", NULL
);
326 pbm
->ino_bitmap
= (((u64
)val
[1] << 32UL) |
327 ((u64
)val
[0] << 0UL));
331 static void pci_register_legacy_regions(struct resource
*io_res
,
332 struct resource
*mem_res
)
337 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
341 p
->name
= "Video RAM area";
342 p
->start
= mem_res
->start
+ 0xa0000UL
;
343 p
->end
= p
->start
+ 0x1ffffUL
;
344 p
->flags
= IORESOURCE_BUSY
;
345 request_resource(mem_res
, p
);
347 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
351 p
->name
= "System ROM";
352 p
->start
= mem_res
->start
+ 0xf0000UL
;
353 p
->end
= p
->start
+ 0xffffUL
;
354 p
->flags
= IORESOURCE_BUSY
;
355 request_resource(mem_res
, p
);
357 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
361 p
->name
= "Video ROM";
362 p
->start
= mem_res
->start
+ 0xc0000UL
;
363 p
->end
= p
->start
+ 0x7fffUL
;
364 p
->flags
= IORESOURCE_BUSY
;
365 request_resource(mem_res
, p
);
368 static void pci_register_iommu_region(struct pci_pbm_info
*pbm
)
370 const u32
*vdma
= of_get_property(pbm
->op
->dev
.of_node
, "virtual-dma",
374 struct resource
*rp
= kzalloc(sizeof(*rp
), GFP_KERNEL
);
377 pr_info("%s: Cannot allocate IOMMU resource.\n",
382 rp
->start
= pbm
->mem_space
.start
+ (unsigned long) vdma
[0];
383 rp
->end
= rp
->start
+ (unsigned long) vdma
[1] - 1UL;
384 rp
->flags
= IORESOURCE_BUSY
;
385 if (request_resource(&pbm
->mem_space
, rp
)) {
386 pr_info("%s: Unable to request IOMMU resource.\n",
393 void pci_determine_mem_io_space(struct pci_pbm_info
*pbm
)
395 const struct linux_prom_pci_ranges
*pbm_ranges
;
396 int i
, saw_mem
, saw_io
;
399 saw_mem
= saw_io
= 0;
400 pbm_ranges
= of_get_property(pbm
->op
->dev
.of_node
, "ranges", &i
);
402 prom_printf("PCI: Fatal error, missing PBM ranges property "
408 num_pbm_ranges
= i
/ sizeof(*pbm_ranges
);
410 for (i
= 0; i
< num_pbm_ranges
; i
++) {
411 const struct linux_prom_pci_ranges
*pr
= &pbm_ranges
[i
];
412 unsigned long a
, size
;
413 u32 parent_phys_hi
, parent_phys_lo
;
414 u32 size_hi
, size_lo
;
417 parent_phys_hi
= pr
->parent_phys_hi
;
418 parent_phys_lo
= pr
->parent_phys_lo
;
419 if (tlb_type
== hypervisor
)
420 parent_phys_hi
&= 0x0fffffff;
422 size_hi
= pr
->size_hi
;
423 size_lo
= pr
->size_lo
;
425 type
= (pr
->child_phys_hi
>> 24) & 0x3;
426 a
= (((unsigned long)parent_phys_hi
<< 32UL) |
427 ((unsigned long)parent_phys_lo
<< 0UL));
428 size
= (((unsigned long)size_hi
<< 32UL) |
429 ((unsigned long)size_lo
<< 0UL));
433 /* PCI config space, 16MB */
434 pbm
->config_space
= a
;
438 /* 16-bit IO space, 16MB */
439 pbm
->io_space
.start
= a
;
440 pbm
->io_space
.end
= a
+ size
- 1UL;
441 pbm
->io_space
.flags
= IORESOURCE_IO
;
446 /* 32-bit MEM space, 2GB */
447 pbm
->mem_space
.start
= a
;
448 pbm
->mem_space
.end
= a
+ size
- 1UL;
449 pbm
->mem_space
.flags
= IORESOURCE_MEM
;
454 /* XXX 64-bit MEM handling XXX */
461 if (!saw_io
|| !saw_mem
) {
462 prom_printf("%s: Fatal error, missing %s PBM range.\n",
464 (!saw_io
? "IO" : "MEM"));
468 printk("%s: PCI IO[%llx] MEM[%llx]\n",
471 pbm
->mem_space
.start
);
473 pbm
->io_space
.name
= pbm
->mem_space
.name
= pbm
->name
;
475 request_resource(&ioport_resource
, &pbm
->io_space
);
476 request_resource(&iomem_resource
, &pbm
->mem_space
);
478 pci_register_legacy_regions(&pbm
->io_space
,
480 pci_register_iommu_region(pbm
);
483 /* Generic helper routines for PCI error reporting. */
484 void pci_scan_for_target_abort(struct pci_pbm_info
*pbm
,
485 struct pci_bus
*pbus
)
487 struct pci_dev
*pdev
;
490 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
491 u16 status
, error_bits
;
493 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
495 (status
& (PCI_STATUS_SIG_TARGET_ABORT
|
496 PCI_STATUS_REC_TARGET_ABORT
));
498 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
499 printk("%s: Device %s saw Target Abort [%016x]\n",
500 pbm
->name
, pci_name(pdev
), status
);
504 list_for_each_entry(bus
, &pbus
->children
, node
)
505 pci_scan_for_target_abort(pbm
, bus
);
508 void pci_scan_for_master_abort(struct pci_pbm_info
*pbm
,
509 struct pci_bus
*pbus
)
511 struct pci_dev
*pdev
;
514 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
515 u16 status
, error_bits
;
517 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
519 (status
& (PCI_STATUS_REC_MASTER_ABORT
));
521 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
522 printk("%s: Device %s received Master Abort [%016x]\n",
523 pbm
->name
, pci_name(pdev
), status
);
527 list_for_each_entry(bus
, &pbus
->children
, node
)
528 pci_scan_for_master_abort(pbm
, bus
);
531 void pci_scan_for_parity_error(struct pci_pbm_info
*pbm
,
532 struct pci_bus
*pbus
)
534 struct pci_dev
*pdev
;
537 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
538 u16 status
, error_bits
;
540 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
542 (status
& (PCI_STATUS_PARITY
|
543 PCI_STATUS_DETECTED_PARITY
));
545 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
546 printk("%s: Device %s saw Parity Error [%016x]\n",
547 pbm
->name
, pci_name(pdev
), status
);
551 list_for_each_entry(bus
, &pbus
->children
, node
)
552 pci_scan_for_parity_error(pbm
, bus
);