990224a44121f01367186d26ee814b1a384c373e
[sfrench/cifs-2.6.git] / arch / ia64 / sn / kernel / io_init.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1992 - 1997, 2000-2006 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <asm/sn/types.h>
10 #include <asm/sn/addrs.h>
11 #include <asm/sn/io.h>
12 #include <asm/sn/module.h>
13 #include <asm/sn/intr.h>
14 #include <asm/sn/pcibus_provider_defs.h>
15 #include <asm/sn/pcidev.h>
16 #include <asm/sn/sn_sal.h>
17 #include "xtalk/hubdev.h"
18
19 /*
20  * The code in this file will only be executed when running with
21  * a PROM that does _not_ have base ACPI IO support.
22  * (i.e., SN_ACPI_BASE_SUPPORT() == 0)
23  */
24
25 static int max_segment_number;           /* Default highest segment number */
26 static int max_pcibus_number = 255;     /* Default highest pci bus number */
27
28
29 /*
30  * Retrieve the hub device info structure for the given nasid.
31  */
32 static inline u64 sal_get_hubdev_info(u64 handle, u64 address)
33 {
34         struct ia64_sal_retval ret_stuff;
35         ret_stuff.status = 0;
36         ret_stuff.v0 = 0;
37
38         SAL_CALL_NOLOCK(ret_stuff,
39                         (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
40                         (u64) handle, (u64) address, 0, 0, 0, 0, 0);
41         return ret_stuff.v0;
42 }
43
44 /*
45  * Retrieve the pci bus information given the bus number.
46  */
47 static inline u64 sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
48 {
49         struct ia64_sal_retval ret_stuff;
50         ret_stuff.status = 0;
51         ret_stuff.v0 = 0;
52
53         SAL_CALL_NOLOCK(ret_stuff,
54                         (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
55                         (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
56         return ret_stuff.v0;
57 }
58
59
60 /*
61  * sn_fixup_ionodes() - This routine initializes the HUB data structure for
62  *                      each node in the system. This function is only
63  *                      executed when running with a non-ACPI capable PROM.
64  */
65 static void __init sn_fixup_ionodes(void)
66 {
67
68         struct hubdev_info *hubdev;
69         u64 status;
70         u64 nasid;
71         int i;
72         extern void sn_common_hubdev_init(struct hubdev_info *);
73
74         /*
75          * Get SGI Specific HUB chipset information.
76          * Inform Prom that this kernel can support domain bus numbering.
77          */
78         for (i = 0; i < num_cnodes; i++) {
79                 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
80                 nasid = cnodeid_to_nasid(i);
81                 hubdev->max_segment_number = 0xffffffff;
82                 hubdev->max_pcibus_number = 0xff;
83                 status = sal_get_hubdev_info(nasid, (u64) __pa(hubdev));
84                 if (status)
85                         continue;
86
87                 /* Save the largest Domain and pcibus numbers found. */
88                 if (hubdev->max_segment_number) {
89                         /*
90                          * Dealing with a Prom that supports segments.
91                          */
92                         max_segment_number = hubdev->max_segment_number;
93                         max_pcibus_number = hubdev->max_pcibus_number;
94                 }
95                 sn_common_hubdev_init(hubdev);
96         }
97 }
98
99 /*
100  * sn_pci_legacy_window_fixup - Create PCI controller windows for
101  *                              legacy IO and MEM space. This needs to
102  *                              be done here, as the PROM does not have
103  *                              ACPI support defining the root buses
104  *                              and their resources (_CRS),
105  */
106 static void
107 sn_legacy_pci_window_fixup(struct pci_controller *controller,
108                            u64 legacy_io, u64 legacy_mem)
109 {
110                 controller->window = kcalloc(2, sizeof(struct pci_window),
111                                              GFP_KERNEL);
112                 if (controller->window == NULL)
113                         BUG();
114                 controller->window[0].offset = legacy_io;
115                 controller->window[0].resource.name = "legacy_io";
116                 controller->window[0].resource.flags = IORESOURCE_IO;
117                 controller->window[0].resource.start = legacy_io;
118                 controller->window[0].resource.end =
119                                 controller->window[0].resource.start + 0xffff;
120                 controller->window[0].resource.parent = &ioport_resource;
121                 controller->window[1].offset = legacy_mem;
122                 controller->window[1].resource.name = "legacy_mem";
123                 controller->window[1].resource.flags = IORESOURCE_MEM;
124                 controller->window[1].resource.start = legacy_mem;
125                 controller->window[1].resource.end =
126                        controller->window[1].resource.start + (1024 * 1024) - 1;
127                 controller->window[1].resource.parent = &iomem_resource;
128                 controller->windows = 2;
129 }
130
131 /*
132  * sn_pci_window_fixup() - Create a pci_window for each device resource.
133  *                         It will setup pci_windows for use by
134  *                         pcibios_bus_to_resource(), pcibios_resource_to_bus(),
135  *                         etc.
136  */
137 static void
138 sn_pci_window_fixup(struct pci_dev *dev, unsigned int count,
139                     s64 * pci_addrs)
140 {
141         struct pci_controller *controller = PCI_CONTROLLER(dev->bus);
142         unsigned int i;
143         unsigned int idx;
144         unsigned int new_count;
145         struct pci_window *new_window;
146
147         if (count == 0)
148                 return;
149         idx = controller->windows;
150         new_count = controller->windows + count;
151         new_window = kcalloc(new_count, sizeof(struct pci_window), GFP_KERNEL);
152         if (new_window == NULL)
153                 BUG();
154         if (controller->window) {
155                 memcpy(new_window, controller->window,
156                        sizeof(struct pci_window) * controller->windows);
157                 kfree(controller->window);
158         }
159
160         /* Setup a pci_window for each device resource. */
161         for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
162                 if (pci_addrs[i] == -1)
163                         continue;
164
165                 new_window[idx].offset = dev->resource[i].start - pci_addrs[i];
166                 new_window[idx].resource = dev->resource[i];
167                 idx++;
168         }
169
170         controller->windows = new_count;
171         controller->window = new_window;
172 }
173
174 /*
175  * sn_more_slot_fixup() - We are not running with an ACPI capable PROM,
176  *                        and need to convert the pci_dev->resource
177  *                        'start' and 'end' addresses to mapped addresses,
178  *                        and setup the pci_controller->window array entries.
179  */
180 void
181 sn_more_slot_fixup(struct pci_dev *dev, struct pcidev_info *pcidev_info)
182 {
183         unsigned int count = 0;
184         int idx;
185         s64 pci_addrs[PCI_ROM_RESOURCE + 1];
186         unsigned long addr, end, size, start;
187
188         /* Copy over PIO Mapped Addresses */
189         for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
190
191                 if (!pcidev_info->pdi_pio_mapped_addr[idx]) {
192                         pci_addrs[idx] = -1;
193                         continue;
194                 }
195
196                 start = dev->resource[idx].start;
197                 end = dev->resource[idx].end;
198                 size = end - start;
199                 if (size == 0) {
200                         pci_addrs[idx] = -1;
201                         continue;
202                 }
203                 pci_addrs[idx] = start;
204                 count++;
205                 addr = pcidev_info->pdi_pio_mapped_addr[idx];
206                 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
207                 dev->resource[idx].start = addr;
208                 dev->resource[idx].end = addr + size;
209                 if (dev->resource[idx].flags & IORESOURCE_IO)
210                         dev->resource[idx].parent = &ioport_resource;
211                 else
212                         dev->resource[idx].parent = &iomem_resource;
213         }
214         /* Create a pci_window in the pci_controller struct for
215          * each device resource.
216          */
217         if (count > 0)
218                 sn_pci_window_fixup(dev, count, pci_addrs);
219 }
220
221 /*
222  * sn_pci_controller_fixup() - This routine sets up a bus's resources
223  *                             consistent with the Linux PCI abstraction layer.
224  */
225 static void
226 sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
227 {
228         s64 status = 0;
229         struct pci_controller *controller;
230         struct pcibus_bussoft *prom_bussoft_ptr;
231
232
233         status = sal_get_pcibus_info((u64) segment, (u64) busnum,
234                                      (u64) ia64_tpa(&prom_bussoft_ptr));
235         if (status > 0)
236                 return;         /*bus # does not exist */
237         prom_bussoft_ptr = __va(prom_bussoft_ptr);
238
239         controller = kzalloc(sizeof(*controller), GFP_KERNEL);
240         if (!controller)
241                 BUG();
242         controller->segment = segment;
243
244         /*
245          * Temporarily save the prom_bussoft_ptr for use by sn_bus_fixup().
246          * (platform_data will be overwritten later in sn_common_bus_fixup())
247          */
248         controller->platform_data = prom_bussoft_ptr;
249
250         bus = pci_scan_bus(busnum, &pci_root_ops, controller);
251         if (bus == NULL)
252                 goto error_return; /* error, or bus already scanned */
253
254         bus->sysdata = controller;
255
256         return;
257
258 error_return:
259
260         kfree(controller);
261         return;
262 }
263
264 /*
265  * sn_bus_fixup
266  */
267 void
268 sn_bus_fixup(struct pci_bus *bus)
269 {
270         struct pci_dev *pci_dev = NULL;
271         struct pcibus_bussoft *prom_bussoft_ptr;
272         extern void sn_common_bus_fixup(struct pci_bus *,
273                                         struct pcibus_bussoft *);
274
275
276         if (!bus->parent) {  /* If root bus */
277                 prom_bussoft_ptr = PCI_CONTROLLER(bus)->platform_data;
278                 if (prom_bussoft_ptr == NULL) {
279                         printk(KERN_ERR
280                                "sn_bus_fixup: 0x%04x:0x%02x Unable to "
281                                "obtain prom_bussoft_ptr\n",
282                                pci_domain_nr(bus), bus->number);
283                         return;
284                 }
285                 sn_common_bus_fixup(bus, prom_bussoft_ptr);
286                 sn_legacy_pci_window_fixup(PCI_CONTROLLER(bus),
287                                            prom_bussoft_ptr->bs_legacy_io,
288                                            prom_bussoft_ptr->bs_legacy_mem);
289         }
290         list_for_each_entry(pci_dev, &bus->devices, bus_list) {
291                 sn_pci_fixup_slot(pci_dev);
292         }
293
294 }
295
296 /*
297  * sn_io_init - PROM does not have ACPI support to define nodes or root buses,
298  *              so we need to do things the hard way, including initiating the
299  *              bus scanning ourselves.
300  */
301
302 void __init sn_io_init(void)
303 {
304         int i, j;
305
306         sn_fixup_ionodes();
307
308         /* busses are not known yet ... */
309         for (i = 0; i <= max_segment_number; i++)
310                 for (j = 0; j <= max_pcibus_number; j++)
311                         sn_pci_controller_fixup(i, j, NULL);
312 }