blob: 1e6aeedf43c4201672e1f6ea61275f44f3dcd687 [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. Miller9fd8b642007-03-08 21:55:49 -080018static void pci_register_legacy_regions(struct resource *io_res,
19 struct resource *mem_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
21 struct resource *p;
22
23 /* VGA Video RAM. */
Eric Sesterhenn91329832006-03-06 13:48:40 -080024 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 if (!p)
26 return;
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 p->name = "Video RAM area";
29 p->start = mem_res->start + 0xa0000UL;
30 p->end = p->start + 0x1ffffUL;
31 p->flags = IORESOURCE_BUSY;
32 request_resource(mem_res, p);
33
Eric Sesterhenn91329832006-03-06 13:48:40 -080034 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (!p)
36 return;
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 p->name = "System ROM";
39 p->start = mem_res->start + 0xf0000UL;
40 p->end = p->start + 0xffffUL;
41 p->flags = IORESOURCE_BUSY;
42 request_resource(mem_res, p);
43
Eric Sesterhenn91329832006-03-06 13:48:40 -080044 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!p)
46 return;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 p->name = "Video ROM";
49 p->start = mem_res->start + 0xc0000UL;
50 p->end = p->start + 0x7fffUL;
51 p->flags = IORESOURCE_BUSY;
52 request_resource(mem_res, p);
53}
54
David S. Miller9fd8b642007-03-08 21:55:49 -080055static void pci_register_iommu_region(struct pci_pbm_info *pbm)
56{
David S. Millera165b422007-03-29 01:50:16 -070057 const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
David S. Miller9fd8b642007-03-08 21:55:49 -080058
59 if (vdma) {
60 struct resource *rp = kmalloc(sizeof(*rp), GFP_KERNEL);
61
62 if (!rp) {
63 prom_printf("Cannot allocate IOMMU resource.\n");
64 prom_halt();
65 }
66 rp->name = "IOMMU";
67 rp->start = pbm->mem_space.start + (unsigned long) vdma[0];
68 rp->end = rp->start + (unsigned long) vdma[1] - 1UL;
69 rp->flags = IORESOURCE_BUSY;
70 request_resource(&pbm->mem_space, rp);
71 }
72}
73
74void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
75{
David S. Millera165b422007-03-29 01:50:16 -070076 const struct linux_prom_pci_ranges *pbm_ranges;
David S. Miller9fd8b642007-03-08 21:55:49 -080077 int i, saw_mem, saw_io;
David S. Miller3487a1f2007-03-08 22:28:17 -080078 int num_pbm_ranges;
David S. Miller9fd8b642007-03-08 21:55:49 -080079
80 saw_mem = saw_io = 0;
David S. Miller3487a1f2007-03-08 22:28:17 -080081 pbm_ranges = of_get_property(pbm->prom_node, "ranges", &i);
82 num_pbm_ranges = i / sizeof(*pbm_ranges);
83
84 for (i = 0; i < num_pbm_ranges; i++) {
David S. Millera165b422007-03-29 01:50:16 -070085 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
David S. Miller9fd8b642007-03-08 21:55:49 -080086 unsigned long a;
David S. Miller3487a1f2007-03-08 22:28:17 -080087 u32 parent_phys_hi, parent_phys_lo;
David S. Miller9fd8b642007-03-08 21:55:49 -080088 int type;
89
David S. Miller3487a1f2007-03-08 22:28:17 -080090 parent_phys_hi = pr->parent_phys_hi;
91 parent_phys_lo = pr->parent_phys_lo;
92 if (tlb_type == hypervisor)
93 parent_phys_hi &= 0x0fffffff;
94
David S. Miller9fd8b642007-03-08 21:55:49 -080095 type = (pr->child_phys_hi >> 24) & 0x3;
David S. Miller3487a1f2007-03-08 22:28:17 -080096 a = (((unsigned long)parent_phys_hi << 32UL) |
97 ((unsigned long)parent_phys_lo << 0UL));
David S. Miller9fd8b642007-03-08 21:55:49 -080098
99 switch (type) {
100 case 0:
101 /* PCI config space, 16MB */
102 pbm->config_space = a;
103 break;
104
105 case 1:
106 /* 16-bit IO space, 16MB */
107 pbm->io_space.start = a;
108 pbm->io_space.end = a + ((16UL*1024UL*1024UL) - 1UL);
109 pbm->io_space.flags = IORESOURCE_IO;
110 saw_io = 1;
111 break;
112
113 case 2:
114 /* 32-bit MEM space, 2GB */
115 pbm->mem_space.start = a;
116 pbm->mem_space.end = a + (0x80000000UL - 1UL);
117 pbm->mem_space.flags = IORESOURCE_MEM;
118 saw_mem = 1;
119 break;
120
121 case 3:
122 /* XXX 64-bit MEM handling XXX */
123
124 default:
125 break;
126 };
127 }
128
129 if (!saw_io || !saw_mem) {
130 prom_printf("%s: Fatal error, missing %s PBM range.\n",
131 pbm->name,
132 (!saw_io ? "IO" : "MEM"));
133 prom_halt();
134 }
135
136 printk("%s: PCI IO[%lx] MEM[%lx]\n",
137 pbm->name,
138 pbm->io_space.start,
139 pbm->mem_space.start);
140
141 pbm->io_space.name = pbm->mem_space.name = pbm->name;
142
143 request_resource(&ioport_resource, &pbm->io_space);
144 request_resource(&iomem_resource, &pbm->mem_space);
145
146 pci_register_legacy_regions(&pbm->io_space,
147 &pbm->mem_space);
148 pci_register_iommu_region(pbm);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Generic helper routines for PCI error reporting. */
152void pci_scan_for_target_abort(struct pci_controller_info *p,
153 struct pci_pbm_info *pbm,
154 struct pci_bus *pbus)
155{
156 struct pci_dev *pdev;
157 struct pci_bus *bus;
158
159 list_for_each_entry(pdev, &pbus->devices, bus_list) {
160 u16 status, error_bits;
161
162 pci_read_config_word(pdev, PCI_STATUS, &status);
163 error_bits =
164 (status & (PCI_STATUS_SIG_TARGET_ABORT |
165 PCI_STATUS_REC_TARGET_ABORT));
166 if (error_bits) {
167 pci_write_config_word(pdev, PCI_STATUS, error_bits);
168 printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
169 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
170 pci_name(pdev), status);
171 }
172 }
173
174 list_for_each_entry(bus, &pbus->children, node)
175 pci_scan_for_target_abort(p, pbm, bus);
176}
177
178void pci_scan_for_master_abort(struct pci_controller_info *p,
179 struct pci_pbm_info *pbm,
180 struct pci_bus *pbus)
181{
182 struct pci_dev *pdev;
183 struct pci_bus *bus;
184
185 list_for_each_entry(pdev, &pbus->devices, bus_list) {
186 u16 status, error_bits;
187
188 pci_read_config_word(pdev, PCI_STATUS, &status);
189 error_bits =
190 (status & (PCI_STATUS_REC_MASTER_ABORT));
191 if (error_bits) {
192 pci_write_config_word(pdev, PCI_STATUS, error_bits);
193 printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
194 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
195 pci_name(pdev), status);
196 }
197 }
198
199 list_for_each_entry(bus, &pbus->children, node)
200 pci_scan_for_master_abort(p, pbm, bus);
201}
202
203void pci_scan_for_parity_error(struct pci_controller_info *p,
204 struct pci_pbm_info *pbm,
205 struct pci_bus *pbus)
206{
207 struct pci_dev *pdev;
208 struct pci_bus *bus;
209
210 list_for_each_entry(pdev, &pbus->devices, bus_list) {
211 u16 status, error_bits;
212
213 pci_read_config_word(pdev, PCI_STATUS, &status);
214 error_bits =
215 (status & (PCI_STATUS_PARITY |
216 PCI_STATUS_DETECTED_PARITY));
217 if (error_bits) {
218 pci_write_config_word(pdev, PCI_STATUS, error_bits);
219 printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
220 p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
221 pci_name(pdev), status);
222 }
223 }
224
225 list_for_each_entry(bus, &pbus->children, node)
226 pci_scan_for_parity_error(p, pbm, bus);
227}