[SPARC64] PCI: Use common routine to fetch PBM properties.
[sfrench/cifs-2.6.git] / arch / sparc64 / kernel / pci_common.c
1 /* pci_common.c: PCI controller common support.
2  *
3  * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
4  */
5
6 #include <linux/string.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/pci.h>
10 #include <linux/device.h>
11
12 #include <asm/pbm.h>
13 #include <asm/prom.h>
14 #include <asm/of_device.h>
15
16 #include "pci_impl.h"
17
18 void 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
32 static void pci_register_legacy_regions(struct resource *io_res,
33                                         struct resource *mem_res)
34 {
35         struct resource *p;
36
37         /* VGA Video RAM. */
38         p = kzalloc(sizeof(*p), GFP_KERNEL);
39         if (!p)
40                 return;
41
42         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
48         p = kzalloc(sizeof(*p), GFP_KERNEL);
49         if (!p)
50                 return;
51
52         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
58         p = kzalloc(sizeof(*p), GFP_KERNEL);
59         if (!p)
60                 return;
61
62         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
69 static void pci_register_iommu_region(struct pci_pbm_info *pbm)
70 {
71         const u32 *vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
72
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
88 void pci_determine_mem_io_space(struct pci_pbm_info *pbm)
89 {
90         const struct linux_prom_pci_ranges *pbm_ranges;
91         int i, saw_mem, saw_io;
92         int num_pbm_ranges;
93
94         saw_mem = saw_io = 0;
95         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++) {
99                 const struct linux_prom_pci_ranges *pr = &pbm_ranges[i];
100                 unsigned long a;
101                 u32 parent_phys_hi, parent_phys_lo;
102                 int type;
103
104                 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
109                 type = (pr->child_phys_hi >> 24) & 0x3;
110                 a = (((unsigned long)parent_phys_hi << 32UL) |
111                      ((unsigned long)parent_phys_lo  <<  0UL));
112
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
165 /* Generic helper routines for PCI error reporting. */
166 void pci_scan_for_target_abort(struct pci_controller_info *p,
167                                struct pci_pbm_info *pbm,
168                                struct pci_bus *pbus)
169 {
170         struct pci_dev *pdev;
171         struct pci_bus *bus;
172
173         list_for_each_entry(pdev, &pbus->devices, bus_list) {
174                 u16 status, error_bits;
175
176                 pci_read_config_word(pdev, PCI_STATUS, &status);
177                 error_bits =
178                         (status & (PCI_STATUS_SIG_TARGET_ABORT |
179                                    PCI_STATUS_REC_TARGET_ABORT));
180                 if (error_bits) {
181                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
182                         printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
183                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
184                                pci_name(pdev), status);
185                 }
186         }
187
188         list_for_each_entry(bus, &pbus->children, node)
189                 pci_scan_for_target_abort(p, pbm, bus);
190 }
191
192 void pci_scan_for_master_abort(struct pci_controller_info *p,
193                                struct pci_pbm_info *pbm,
194                                struct pci_bus *pbus)
195 {
196         struct pci_dev *pdev;
197         struct pci_bus *bus;
198
199         list_for_each_entry(pdev, &pbus->devices, bus_list) {
200                 u16 status, error_bits;
201
202                 pci_read_config_word(pdev, PCI_STATUS, &status);
203                 error_bits =
204                         (status & (PCI_STATUS_REC_MASTER_ABORT));
205                 if (error_bits) {
206                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
207                         printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
208                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
209                                pci_name(pdev), status);
210                 }
211         }
212
213         list_for_each_entry(bus, &pbus->children, node)
214                 pci_scan_for_master_abort(p, pbm, bus);
215 }
216
217 void pci_scan_for_parity_error(struct pci_controller_info *p,
218                                struct pci_pbm_info *pbm,
219                                struct pci_bus *pbus)
220 {
221         struct pci_dev *pdev;
222         struct pci_bus *bus;
223
224         list_for_each_entry(pdev, &pbus->devices, bus_list) {
225                 u16 status, error_bits;
226
227                 pci_read_config_word(pdev, PCI_STATUS, &status);
228                 error_bits =
229                         (status & (PCI_STATUS_PARITY |
230                                    PCI_STATUS_DETECTED_PARITY));
231                 if (error_bits) {
232                         pci_write_config_word(pdev, PCI_STATUS, error_bits);
233                         printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
234                                p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
235                                pci_name(pdev), status);
236                 }
237         }
238
239         list_for_each_entry(bus, &pbus->children, node)
240                 pci_scan_for_parity_error(p, pbm, bus);
241 }