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 /* At least on Sabre, it is necessary to access all PCI host controller
48 * registers at their natural size, otherwise zeros are returned.
49 * Strange but true, and I see no language in the UltraSPARC-IIi
50 * programmer's manual that mentions this even indirectly.
52 static int sun4u_read_pci_cfg_host(struct pci_pbm_info
*pbm
,
53 unsigned char bus
, unsigned int devfn
,
54 int where
, int size
, u32
*value
)
60 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
62 return PCIBIOS_SUCCESSFUL
;
67 unsigned long align
= (unsigned long) addr
;
70 pci_config_read16((u16
*)align
, &tmp16
);
74 *value
= tmp16
& 0xff;
76 pci_config_read8((u8
*)addr
, &tmp8
);
83 pci_config_read16((u16
*)addr
, &tmp16
);
86 pci_config_read8((u8
*)addr
, &tmp8
);
88 pci_config_read8(((u8
*)addr
) + 1, &tmp8
);
89 *value
|= ((u32
) tmp8
) << 8;
95 sun4u_read_pci_cfg_host(pbm
, bus
, devfn
,
100 sun4u_read_pci_cfg_host(pbm
, bus
, devfn
,
101 where
+ 2, 2, &tmp32
);
102 *value
|= tmp32
<< 16;
105 return PCIBIOS_SUCCESSFUL
;
108 static int sun4u_read_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
109 int where
, int size
, u32
*value
)
111 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
112 unsigned char bus
= bus_dev
->number
;
129 if (!bus_dev
->number
&& !PCI_SLOT(devfn
))
130 return sun4u_read_pci_cfg_host(pbm
, bus
, devfn
, where
,
133 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
135 return PCIBIOS_SUCCESSFUL
;
139 pci_config_read8((u8
*)addr
, &tmp8
);
145 printk("pci_read_config_word: misaligned reg [%x]\n",
147 return PCIBIOS_SUCCESSFUL
;
149 pci_config_read16((u16
*)addr
, &tmp16
);
150 *value
= (u32
) tmp16
;
155 printk("pci_read_config_dword: misaligned reg [%x]\n",
157 return PCIBIOS_SUCCESSFUL
;
159 pci_config_read32(addr
, value
);
162 return PCIBIOS_SUCCESSFUL
;
165 static int sun4u_write_pci_cfg_host(struct pci_pbm_info
*pbm
,
166 unsigned char bus
, unsigned int devfn
,
167 int where
, int size
, u32 value
)
171 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
173 return PCIBIOS_SUCCESSFUL
;
178 unsigned long align
= (unsigned long) addr
;
182 pci_config_read16((u16
*)align
, &tmp16
);
190 pci_config_write16((u16
*)align
, tmp16
);
192 pci_config_write8((u8
*)addr
, value
);
196 pci_config_write16((u16
*)addr
, value
);
198 pci_config_write8((u8
*)addr
, value
& 0xff);
199 pci_config_write8(((u8
*)addr
) + 1, value
>> 8);
203 sun4u_write_pci_cfg_host(pbm
, bus
, devfn
,
204 where
, 2, value
& 0xffff);
205 sun4u_write_pci_cfg_host(pbm
, bus
, devfn
,
206 where
+ 2, 2, value
>> 16);
209 return PCIBIOS_SUCCESSFUL
;
212 static int sun4u_write_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
213 int where
, int size
, u32 value
)
215 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
216 unsigned char bus
= bus_dev
->number
;
219 if (!bus_dev
->number
&& !PCI_SLOT(devfn
))
220 return sun4u_write_pci_cfg_host(pbm
, bus
, devfn
, where
,
223 addr
= sun4u_config_mkaddr(pbm
, bus
, devfn
, where
);
225 return PCIBIOS_SUCCESSFUL
;
229 pci_config_write8((u8
*)addr
, value
);
234 printk("pci_write_config_word: misaligned reg [%x]\n",
236 return PCIBIOS_SUCCESSFUL
;
238 pci_config_write16((u16
*)addr
, value
);
243 printk("pci_write_config_dword: misaligned reg [%x]\n",
245 return PCIBIOS_SUCCESSFUL
;
247 pci_config_write32(addr
, value
);
249 return PCIBIOS_SUCCESSFUL
;
252 struct pci_ops sun4u_pci_ops
= {
253 .read
= sun4u_read_pci_cfg
,
254 .write
= sun4u_write_pci_cfg
,
257 static int sun4v_read_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
258 int where
, int size
, u32
*value
)
260 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
261 u32 devhandle
= pbm
->devhandle
;
262 unsigned int bus
= bus_dev
->number
;
263 unsigned int device
= PCI_SLOT(devfn
);
264 unsigned int func
= PCI_FUNC(devfn
);
267 if (!bus
&& devfn
== 0x00)
268 return pci_host_bridge_read_pci_cfg(bus_dev
, devfn
, where
,
270 if (config_out_of_range(pbm
, bus
, devfn
, where
)) {
273 ret
= pci_sun4v_config_get(devhandle
,
274 HV_PCI_DEVICE_BUILD(bus
, device
, func
),
282 *value
= ret
& 0xffff;
285 *value
= ret
& 0xffffffff;
290 return PCIBIOS_SUCCESSFUL
;
293 static int sun4v_write_pci_cfg(struct pci_bus
*bus_dev
, unsigned int devfn
,
294 int where
, int size
, u32 value
)
296 struct pci_pbm_info
*pbm
= bus_dev
->sysdata
;
297 u32 devhandle
= pbm
->devhandle
;
298 unsigned int bus
= bus_dev
->number
;
299 unsigned int device
= PCI_SLOT(devfn
);
300 unsigned int func
= PCI_FUNC(devfn
);
303 if (!bus
&& devfn
== 0x00)
304 return pci_host_bridge_write_pci_cfg(bus_dev
, devfn
, where
,
306 if (config_out_of_range(pbm
, bus
, devfn
, where
)) {
309 ret
= pci_sun4v_config_put(devhandle
,
310 HV_PCI_DEVICE_BUILD(bus
, device
, func
),
313 return PCIBIOS_SUCCESSFUL
;
316 struct pci_ops sun4v_pci_ops
= {
317 .read
= sun4v_read_pci_cfg
,
318 .write
= sun4v_write_pci_cfg
,
321 void pci_get_pbm_props(struct pci_pbm_info
*pbm
)
323 const u32
*val
= of_get_property(pbm
->prom_node
, "bus-range", NULL
);
325 pbm
->pci_first_busno
= val
[0];
326 pbm
->pci_last_busno
= val
[1];
328 val
= of_get_property(pbm
->prom_node
, "ino-bitmap", NULL
);
330 pbm
->ino_bitmap
= (((u64
)val
[1] << 32UL) |
331 ((u64
)val
[0] << 0UL));
335 static void pci_register_legacy_regions(struct resource
*io_res
,
336 struct resource
*mem_res
)
341 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
345 p
->name
= "Video RAM area";
346 p
->start
= mem_res
->start
+ 0xa0000UL
;
347 p
->end
= p
->start
+ 0x1ffffUL
;
348 p
->flags
= IORESOURCE_BUSY
;
349 request_resource(mem_res
, p
);
351 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
355 p
->name
= "System ROM";
356 p
->start
= mem_res
->start
+ 0xf0000UL
;
357 p
->end
= p
->start
+ 0xffffUL
;
358 p
->flags
= IORESOURCE_BUSY
;
359 request_resource(mem_res
, p
);
361 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
365 p
->name
= "Video ROM";
366 p
->start
= mem_res
->start
+ 0xc0000UL
;
367 p
->end
= p
->start
+ 0x7fffUL
;
368 p
->flags
= IORESOURCE_BUSY
;
369 request_resource(mem_res
, p
);
372 static void pci_register_iommu_region(struct pci_pbm_info
*pbm
)
374 const u32
*vdma
= of_get_property(pbm
->prom_node
, "virtual-dma", NULL
);
377 struct resource
*rp
= kmalloc(sizeof(*rp
), GFP_KERNEL
);
380 prom_printf("Cannot allocate IOMMU resource.\n");
384 rp
->start
= pbm
->mem_space
.start
+ (unsigned long) vdma
[0];
385 rp
->end
= rp
->start
+ (unsigned long) vdma
[1] - 1UL;
386 rp
->flags
= IORESOURCE_BUSY
;
387 request_resource(&pbm
->mem_space
, rp
);
391 void pci_determine_mem_io_space(struct pci_pbm_info
*pbm
)
393 const struct linux_prom_pci_ranges
*pbm_ranges
;
394 int i
, saw_mem
, saw_io
;
397 saw_mem
= saw_io
= 0;
398 pbm_ranges
= of_get_property(pbm
->prom_node
, "ranges", &i
);
400 prom_printf("PCI: Fatal error, missing PBM ranges property "
406 num_pbm_ranges
= i
/ sizeof(*pbm_ranges
);
408 for (i
= 0; i
< num_pbm_ranges
; i
++) {
409 const struct linux_prom_pci_ranges
*pr
= &pbm_ranges
[i
];
410 unsigned long a
, size
;
411 u32 parent_phys_hi
, parent_phys_lo
;
412 u32 size_hi
, size_lo
;
415 parent_phys_hi
= pr
->parent_phys_hi
;
416 parent_phys_lo
= pr
->parent_phys_lo
;
417 if (tlb_type
== hypervisor
)
418 parent_phys_hi
&= 0x0fffffff;
420 size_hi
= pr
->size_hi
;
421 size_lo
= pr
->size_lo
;
423 type
= (pr
->child_phys_hi
>> 24) & 0x3;
424 a
= (((unsigned long)parent_phys_hi
<< 32UL) |
425 ((unsigned long)parent_phys_lo
<< 0UL));
426 size
= (((unsigned long)size_hi
<< 32UL) |
427 ((unsigned long)size_lo
<< 0UL));
431 /* PCI config space, 16MB */
432 pbm
->config_space
= a
;
436 /* 16-bit IO space, 16MB */
437 pbm
->io_space
.start
= a
;
438 pbm
->io_space
.end
= a
+ size
- 1UL;
439 pbm
->io_space
.flags
= IORESOURCE_IO
;
444 /* 32-bit MEM space, 2GB */
445 pbm
->mem_space
.start
= a
;
446 pbm
->mem_space
.end
= a
+ size
- 1UL;
447 pbm
->mem_space
.flags
= IORESOURCE_MEM
;
452 /* XXX 64-bit MEM handling XXX */
459 if (!saw_io
|| !saw_mem
) {
460 prom_printf("%s: Fatal error, missing %s PBM range.\n",
462 (!saw_io
? "IO" : "MEM"));
466 printk("%s: PCI IO[%lx] MEM[%lx]\n",
469 pbm
->mem_space
.start
);
471 pbm
->io_space
.name
= pbm
->mem_space
.name
= pbm
->name
;
473 request_resource(&ioport_resource
, &pbm
->io_space
);
474 request_resource(&iomem_resource
, &pbm
->mem_space
);
476 pci_register_legacy_regions(&pbm
->io_space
,
478 pci_register_iommu_region(pbm
);
481 /* Generic helper routines for PCI error reporting. */
482 void pci_scan_for_target_abort(struct pci_pbm_info
*pbm
,
483 struct pci_bus
*pbus
)
485 struct pci_dev
*pdev
;
488 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
489 u16 status
, error_bits
;
491 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
493 (status
& (PCI_STATUS_SIG_TARGET_ABORT
|
494 PCI_STATUS_REC_TARGET_ABORT
));
496 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
497 printk("%s: Device %s saw Target Abort [%016x]\n",
498 pbm
->name
, pci_name(pdev
), status
);
502 list_for_each_entry(bus
, &pbus
->children
, node
)
503 pci_scan_for_target_abort(pbm
, bus
);
506 void pci_scan_for_master_abort(struct pci_pbm_info
*pbm
,
507 struct pci_bus
*pbus
)
509 struct pci_dev
*pdev
;
512 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
513 u16 status
, error_bits
;
515 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
517 (status
& (PCI_STATUS_REC_MASTER_ABORT
));
519 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
520 printk("%s: Device %s received Master Abort [%016x]\n",
521 pbm
->name
, pci_name(pdev
), status
);
525 list_for_each_entry(bus
, &pbus
->children
, node
)
526 pci_scan_for_master_abort(pbm
, bus
);
529 void pci_scan_for_parity_error(struct pci_pbm_info
*pbm
,
530 struct pci_bus
*pbus
)
532 struct pci_dev
*pdev
;
535 list_for_each_entry(pdev
, &pbus
->devices
, bus_list
) {
536 u16 status
, error_bits
;
538 pci_read_config_word(pdev
, PCI_STATUS
, &status
);
540 (status
& (PCI_STATUS_PARITY
|
541 PCI_STATUS_DETECTED_PARITY
));
543 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
);
544 printk("%s: Device %s saw Parity Error [%016x]\n",
545 pbm
->name
, pci_name(pdev
), status
);
549 list_for_each_entry(bus
, &pbus
->children
, node
)
550 pci_scan_for_parity_error(pbm
, bus
);