blob: 8dcc7cc4ec71324d73603c107a46dab3cbed0b27 [file] [log] [blame]
David S. Miller9fd8b642007-03-08 21:55:49 -08001/* pci_common.c: PCI controller common support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller9fd8b642007-03-08 21:55:49 -08003 * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <linux/string.h>
7#include <linux/slab.h>
8#include <linux/init.h>
Fabio Massimo Di Nittocf69eab2006-12-20 09:22:28 -08009#include <linux/pci.h>
10#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <asm/pbm.h>
David S. Millerde8d28b2006-06-22 16:18:54 -070013#include <asm/prom.h>
David S. Miller2b1e5972006-06-29 15:07:37 -070014#include <asm/of_device.h>
David S. Millerde8d28b2006-06-22 16:18:54 -070015
16#include "pci_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David S. Millercfa06522007-05-07 21:51:41 -070018void pci_get_pbm_props(struct pci_pbm_info *pbm)
19{
20 const u32 *val = of_get_property(pbm->prom_node, "bus-range", NULL);
21
22 pbm->pci_first_busno = val[0];
23 pbm->pci_last_busno = val[1];
24
25 val = of_get_property(pbm->prom_node, "ino-bitmap", NULL);
26 if (val) {
27 pbm->ino_bitmap = (((u64)val[1] << 32UL) |
28 ((u64)val[0] << 0UL));
29 }
30}
31
David S. Miller9fd8b642007-03-08 21:55:49 -080032static void pci_register_legacy_regions(struct resource *io_res,
33 struct resource *mem_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct resource *p;
36
37 /* VGA Video RAM. */
Eric Sesterhenn91329832006-03-06 13:48:40 -080038 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!p)
40 return;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 p->name = "Video RAM area";
43 p->start = mem_res->start + 0xa0000UL;
44 p->end = p->start + 0x1ffffUL;
45 p->flags = IORESOURCE_BUSY;
46 request_resource(mem_res, p);
47
Eric Sesterhenn91329832006-03-06 13:48:40 -080048 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!p)
50 return;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 p->name = "System ROM";
53 p->start = mem_res->start + 0xf0000UL;
54 p->end = p->start + 0xffffUL;
55 p->flags = IORESOURCE_BUSY;
56 request_resource(mem_res, p);
57
Eric Sesterhenn91329832006-03-06 13:48:40 -080058 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!p)
60 return;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 p->name = "Video ROM";
63 p->start = mem_res->start + 0xc0000UL;
64 p->end = p->start + 0x7fffUL;
65 p->flags = IORESOURCE_BUSY;
66 request_resource(mem_res, p);
67}
68
David S. Miller9fd8b642007-03-08 21:55:49 -080069static void pci_register_iommu_region(struct pci_pbm_info *pbm)
70{
David S. Millera165b422007-03-29 01:50:16 -070071 const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
David S. Miller9fd8b642007-03-08 21:55:49 -080072
73 if (vdma) {
74 struct resource *rp = kmalloc(sizeof(*rp), GFP_KERNEL);
75
76 if (!rp) {
77 prom_printf("Cannot allocate IOMMU resource.\n");
78 prom_halt();
79 }
80 rp->name = "IOMMU";
81 rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
82 rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
83 rp->flags = IORESOURCE_BUSY;
84 request_resource(&pbm->mem_space, rp);
85 }
86}
87
88void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
89{
David S. Millera165b422007-03-29 01:50:16 -070090 const struct linux_prom_pci_ranges *pbm_ranges;
David S. Miller9fd8b642007-03-08 21:55:49 -080091 int i, saw_mem, saw_io;
David S. Miller3487a1f2007-03-08 22:28:17 -080092 int num_pbm_ranges;
David S. Miller9fd8b642007-03-08 21:55:49 -080093
94 saw_mem = saw_io = 0;
David S. Miller3487a1f2007-03-08 22:28:17 -080095 pbm_ranges = of_get_property(pbm->prom_node, "ranges", &i);
96 num_pbm_ranges = i / sizeof(*pbm_ranges);
97
98 for (i = 0; i < num_pbm_ranges; i++) {
David S. Millera165b422007-03-29 01:50:16 -070099 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
David S. Miller9fd8b642007-03-08 21:55:49 -0800100 unsigned long a;
David S. Miller3487a1f2007-03-08 22:28:17 -0800101 u32 parent_phys_hi, parent_phys_lo;
David S. Miller9fd8b642007-03-08 21:55:49 -0800102 int type;
103
David S. Miller3487a1f2007-03-08 22:28:17 -0800104 parent_phys_hi = pr->parent_phys_hi;
105 parent_phys_lo = pr->parent_phys_lo;
106 if (tlb_type == hypervisor)
107 parent_phys_hi &= 0x0fffffff;
108
David S. Miller9fd8b642007-03-08 21:55:49 -0800109 type = (pr->child_phys_hi >> 24) & 0x3;
David S. Miller3487a1f2007-03-08 22:28:17 -0800110 a = (((unsigned long)parent_phys_hi << 32UL) |
111 ((unsigned long)parent_phys_lo << 0UL));
David S. Miller9fd8b642007-03-08 21:55:49 -0800112
113 switch (type) {
114 case 0:
115 /* PCI config space, 16MB */
116 pbm->config_space = a;
117 break;
118
119 case 1:
120 /* 16-bit IO space, 16MB */
121 pbm->io_space.start = a;
122 pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
123 pbm->io_space.flags = IORESOURCE_IO;
124 saw_io = 1;
125 break;
126
127 case 2:
128 /* 32-bit MEM space, 2GB */
129 pbm->mem_space.start = a;
130 pbm->mem_space.end = a + (0x80000000UL - 1UL);
131 pbm->mem_space.flags = IORESOURCE_MEM;
132 saw_mem = 1;
133 break;
134
135 case 3:
136 /* XXX 64-bit MEM handling XXX */
137
138 default:
139 break;
140 };
141 }
142
143 if (!saw_io || !saw_mem) {
144 prom_printf("%s: Fatal error, missing %s PBM range.\n",
145 pbm->name,
146 (!saw_io ? "IO" : "MEM"));
147 prom_halt();
148 }
149
150 printk("%s: PCI IO[%lx] MEM[%lx]\n",
151 pbm->name,
152 pbm->io_space.start,
153 pbm->mem_space.start);
154
155 pbm->io_space.name = pbm->mem_space.name = pbm->name;
156
157 request_resource(&ioport_resource, &pbm->io_space);
158 request_resource(&iomem_resource, &pbm->mem_space);
159
160 pci_register_legacy_regions(&pbm->io_space,
161 &pbm->mem_space);
162 pci_register_iommu_region(pbm);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Generic helper routines for PCI error reporting. */
David S. Miller6c108f12007-05-07 23:49:01 -0700166void pci_scan_for_target_abort(struct pci_pbm_info *pbm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct pci_bus *pbus)
168{
169 struct pci_dev *pdev;
170 struct pci_bus *bus;
171
172 list_for_each_entry(pdev, &pbus->devices, bus_list) {
173 u16 status, error_bits;
174
175 pci_read_config_word(pdev, PCI_STATUS, &status);
176 error_bits =
177 (status & (PCI_STATUS_SIG_TARGET_ABORT |
178 PCI_STATUS_REC_TARGET_ABORT));
179 if (error_bits) {
180 pci_write_config_word(pdev, PCI_STATUS, error_bits);
David S. Miller6c108f12007-05-07 23:49:01 -0700181 printk("%s: Device %s saw Target Abort [%016x]\n",
182 pbm->name, pci_name(pdev), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 }
185
186 list_for_each_entry(bus, &pbus->children, node)
David S. Miller6c108f12007-05-07 23:49:01 -0700187 pci_scan_for_target_abort(pbm, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
David S. Miller6c108f12007-05-07 23:49:01 -0700190void pci_scan_for_master_abort(struct pci_pbm_info *pbm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct pci_bus *pbus)
192{
193 struct pci_dev *pdev;
194 struct pci_bus *bus;
195
196 list_for_each_entry(pdev, &pbus->devices, bus_list) {
197 u16 status, error_bits;
198
199 pci_read_config_word(pdev, PCI_STATUS, &status);
200 error_bits =
201 (status & (PCI_STATUS_REC_MASTER_ABORT));
202 if (error_bits) {
203 pci_write_config_word(pdev, PCI_STATUS, error_bits);
David S. Miller6c108f12007-05-07 23:49:01 -0700204 printk("%s: Device %s received Master Abort [%016x]\n",
205 pbm->name, pci_name(pdev), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 }
208
209 list_for_each_entry(bus, &pbus->children, node)
David S. Miller6c108f12007-05-07 23:49:01 -0700210 pci_scan_for_master_abort(pbm, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
David S. Miller6c108f12007-05-07 23:49:01 -0700213void pci_scan_for_parity_error(struct pci_pbm_info *pbm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct pci_bus *pbus)
215{
216 struct pci_dev *pdev;
217 struct pci_bus *bus;
218
219 list_for_each_entry(pdev, &pbus->devices, bus_list) {
220 u16 status, error_bits;
221
222 pci_read_config_word(pdev, PCI_STATUS, &status);
223 error_bits =
224 (status & (PCI_STATUS_PARITY |
225 PCI_STATUS_DETECTED_PARITY));
226 if (error_bits) {
227 pci_write_config_word(pdev, PCI_STATUS, error_bits);
David S. Miller6c108f12007-05-07 23:49:01 -0700228 printk("%s: Device %s saw Parity Error [%016x]\n",
229 pbm->name, pci_name(pdev), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 }
232
233 list_for_each_entry(bus, &pbus->children, node)
David S. Miller6c108f12007-05-07 23:49:01 -0700234 pci_scan_for_parity_error(pbm, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}