This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-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-2004 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/bootmem.h>
10 #include <asm/sn/types.h>
11 #include <asm/sn/sn_sal.h>
12 #include <asm/sn/addrs.h>
13 #include "pci/pcibus_provider_defs.h"
14 #include "pci/pcidev.h"
15 #include "pci/pcibr_provider.h"
16 #include "xtalk/xwidgetdev.h"
17 #include <asm/sn/geo.h>
18 #include "xtalk/hubdev.h"
19 #include <asm/sn/io.h>
20 #include <asm/sn/simulator.h>
21
22 char master_baseio_wid;
23 nasid_t master_nasid = INVALID_NASID;   /* Partition Master */
24
25 struct slab_info {
26         struct hubdev_info hubdev;
27 };
28
29 struct brick {
30         moduleid_t id;          /* Module ID of this module        */
31         struct slab_info slab_info[MAX_SLABS + 1];
32 };
33
34 int sn_ioif_inited = 0;         /* SN I/O infrastructure initialized? */
35
36 /*
37  * Retrieve the DMA Flush List given nasid.  This list is needed 
38  * to implement the WAR - Flush DMA data on PIO Reads.
39  */
40 static inline uint64_t
41 sal_get_widget_dmaflush_list(u64 nasid, u64 widget_num, u64 address)
42 {
43
44         struct ia64_sal_retval ret_stuff;
45         ret_stuff.status = 0;
46         ret_stuff.v0 = 0;
47
48         SAL_CALL_NOLOCK(ret_stuff,
49                         (u64) SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
50                         (u64) nasid, (u64) widget_num, (u64) address, 0, 0, 0,
51                         0);
52         return ret_stuff.v0;
53
54 }
55
56 /*
57  * Retrieve the hub device info structure for the given nasid.
58  */
59 static inline uint64_t sal_get_hubdev_info(u64 handle, u64 address)
60 {
61
62         struct ia64_sal_retval ret_stuff;
63         ret_stuff.status = 0;
64         ret_stuff.v0 = 0;
65
66         SAL_CALL_NOLOCK(ret_stuff,
67                         (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
68                         (u64) handle, (u64) address, 0, 0, 0, 0, 0);
69         return ret_stuff.v0;
70 }
71
72 /*
73  * Retrieve the pci bus information given the bus number.
74  */
75 static inline uint64_t sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
76 {
77
78         struct ia64_sal_retval ret_stuff;
79         ret_stuff.status = 0;
80         ret_stuff.v0 = 0;
81
82         SAL_CALL_NOLOCK(ret_stuff,
83                         (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
84                         (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
85         return ret_stuff.v0;
86 }
87
88 /*
89  * Retrieve the pci device information given the bus and device|function number.
90  */
91 static inline uint64_t
92 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev, 
93                         u64 sn_irq_info)
94 {
95         struct ia64_sal_retval ret_stuff;
96         ret_stuff.status = 0;
97         ret_stuff.v0 = 0;
98
99         SAL_CALL_NOLOCK(ret_stuff,
100                         (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
101                         (u64) segment, (u64) bus_number, (u64) devfn, 
102                         (u64) pci_dev,
103                         sn_irq_info, 0, 0);
104         return ret_stuff.v0;
105 }
106
107 /*
108  * sn_alloc_pci_sysdata() - This routine allocates a pci controller
109  *      which is expected as the pci_dev and pci_bus sysdata by the Linux
110  *      PCI infrastructure.
111  */
112 static inline struct pci_controller *sn_alloc_pci_sysdata(void)
113 {
114         struct pci_controller *pci_sysdata;
115
116         pci_sysdata = kmalloc(sizeof(*pci_sysdata), GFP_KERNEL);
117         if (!pci_sysdata)
118                 BUG();
119
120         memset(pci_sysdata, 0, sizeof(*pci_sysdata));
121         return pci_sysdata;
122 }
123
124 /*
125  * sn_fixup_ionodes() - This routine initializes the HUB data strcuture for 
126  *      each node in the system.
127  */
128 static void sn_fixup_ionodes(void)
129 {
130
131         struct sn_flush_device_list *sn_flush_device_list;
132         struct hubdev_info *hubdev;
133         uint64_t status;
134         uint64_t nasid;
135         int i, widget;
136
137         for (i = 0; i < numionodes; i++) {
138                 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
139                 nasid = cnodeid_to_nasid(i);
140                 status = sal_get_hubdev_info(nasid, (uint64_t) __pa(hubdev));
141                 if (status)
142                         continue;
143
144                 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
145                         hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
146
147                 if (!hubdev->hdi_flush_nasid_list.widget_p)
148                         continue;
149
150                 hubdev->hdi_flush_nasid_list.widget_p =
151                     kmalloc((HUB_WIDGET_ID_MAX + 1) *
152                             sizeof(struct sn_flush_device_list *), GFP_KERNEL);
153
154                 memset(hubdev->hdi_flush_nasid_list.widget_p, 0x0,
155                        (HUB_WIDGET_ID_MAX + 1) *
156                        sizeof(struct sn_flush_device_list *));
157
158                 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
159                         sn_flush_device_list = kmalloc(DEV_PER_WIDGET *
160                                                        sizeof(struct
161                                                               sn_flush_device_list),
162                                                        GFP_KERNEL);
163                         memset(sn_flush_device_list, 0x0,
164                                DEV_PER_WIDGET *
165                                sizeof(struct sn_flush_device_list));
166
167                         status =
168                             sal_get_widget_dmaflush_list(nasid, widget,
169                                                          (uint64_t)
170                                                          __pa
171                                                          (sn_flush_device_list));
172                         if (status) {
173                                 kfree(sn_flush_device_list);
174                                 continue;
175                         }
176
177                         hubdev->hdi_flush_nasid_list.widget_p[widget] =
178                             sn_flush_device_list;
179                 }
180
181                 if (!(i & 1))
182                         hub_error_init(hubdev);
183                 else
184                         ice_error_init(hubdev);
185         }
186
187 }
188
189 /*
190  * sn_pci_fixup_slot() - This routine sets up a slot's resources
191  * consistent with the Linux PCI abstraction layer.  Resources acquired
192  * from our PCI provider include PIO maps to BAR space and interrupt
193  * objects.
194  */
195 static void sn_pci_fixup_slot(struct pci_dev *dev)
196 {
197         int idx;
198         int segment = 0;
199         uint64_t size;
200         struct sn_irq_info *sn_irq_info;
201         struct pci_dev *host_pci_dev;
202         int status = 0;
203
204         SN_PCIDEV_INFO(dev) = kmalloc(sizeof(struct pcidev_info), GFP_KERNEL);
205         if (SN_PCIDEV_INFO(dev) <= 0)
206                 BUG();          /* Cannot afford to run out of memory */
207         memset(SN_PCIDEV_INFO(dev), 0, sizeof(struct pcidev_info));
208
209         sn_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
210         if (sn_irq_info <= 0)
211                 BUG();          /* Cannot afford to run out of memory */
212         memset(sn_irq_info, 0, sizeof(struct sn_irq_info));
213
214         /* Call to retrieve pci device information needed by kernel. */
215         status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number, 
216                                      dev->devfn,
217                                      (u64) __pa(SN_PCIDEV_INFO(dev)),
218                                      (u64) __pa(sn_irq_info));
219         if (status)
220                 BUG();          /* Cannot get platform pci device information information */
221
222         /* Copy over PIO Mapped Addresses */
223         for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
224                 unsigned long start, end, addr;
225
226                 if (!SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx])
227                         continue;
228
229                 start = dev->resource[idx].start;
230                 end = dev->resource[idx].end;
231                 size = end - start;
232                 addr = SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx];
233                 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
234                 dev->resource[idx].start = addr;
235                 dev->resource[idx].end = addr + size;
236                 if (dev->resource[idx].flags & IORESOURCE_IO)
237                         dev->resource[idx].parent = &ioport_resource;
238                 else
239                         dev->resource[idx].parent = &iomem_resource;
240         }
241
242         /* set up host bus linkages */
243         host_pci_dev =
244             pci_find_slot(SN_PCIDEV_INFO(dev)->pdi_slot_host_handle >> 32,
245                           SN_PCIDEV_INFO(dev)->
246                           pdi_slot_host_handle & 0xffffffff);
247         SN_PCIDEV_INFO(dev)->pdi_host_pcidev_info =
248             SN_PCIDEV_INFO(host_pci_dev);
249         SN_PCIDEV_INFO(dev)->pdi_linux_pcidev = dev;
250         SN_PCIDEV_INFO(dev)->pdi_pcibus_info = SN_PCIBUS_BUSSOFT(dev->bus);
251
252         /* Only set up IRQ stuff if this device has a host bus context */
253         if (SN_PCIDEV_BUSSOFT(dev) && sn_irq_info->irq_irq) {
254                 SN_PCIDEV_INFO(dev)->pdi_sn_irq_info = sn_irq_info;
255                 dev->irq = SN_PCIDEV_INFO(dev)->pdi_sn_irq_info->irq_irq;
256                 sn_irq_fixup(dev, sn_irq_info);
257         }
258 }
259
260 /*
261  * sn_pci_controller_fixup() - This routine sets up a bus's resources
262  * consistent with the Linux PCI abstraction layer.
263  */
264 static void sn_pci_controller_fixup(int segment, int busnum)
265 {
266         int status = 0;
267         int nasid, cnode;
268         struct pci_bus *bus;
269         struct pci_controller *controller;
270         struct pcibus_bussoft *prom_bussoft_ptr;
271         struct hubdev_info *hubdev_info;
272         void *provider_soft;
273
274         status =
275             sal_get_pcibus_info((u64) segment, (u64) busnum,
276                                 (u64) ia64_tpa(&prom_bussoft_ptr));
277         if (status > 0) {
278                 return;         /* bus # does not exist */
279         }
280
281         prom_bussoft_ptr = __va(prom_bussoft_ptr);
282         controller = sn_alloc_pci_sysdata();
283         /* controller non-zero is BUG'd in sn_alloc_pci_sysdata */
284
285         bus = pci_scan_bus(busnum, &pci_root_ops, controller);
286         if (bus == NULL) {
287                 return;         /* error, or bus already scanned */
288         }
289
290         /*
291          * Per-provider fixup.  Copies the contents from prom to local
292          * area and links SN_PCIBUS_BUSSOFT().
293          *
294          * Note:  Provider is responsible for ensuring that prom_bussoft_ptr
295          * represents an asic-type that it can handle.
296          */
297
298         if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB) {
299                 return;         /* no further fixup necessary */
300         }
301
302         provider_soft = pcibr_bus_fixup(prom_bussoft_ptr);
303         if (provider_soft == NULL) {
304                 return;         /* fixup failed or not applicable */
305         }
306
307         /*
308          * Generic bus fixup goes here.  Don't reference prom_bussoft_ptr
309          * after this point.
310          */
311
312         PCI_CONTROLLER(bus) = controller;
313         SN_PCIBUS_BUSSOFT(bus) = provider_soft;
314
315         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
316         cnode = nasid_to_cnodeid(nasid);
317         hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
318         SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
319             &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
320 }
321
322 /*
323  * Ugly hack to get PCI setup until we have a proper ACPI namespace.
324  */
325
326 #define PCI_BUSES_TO_SCAN 256
327
328 static int __init sn_pci_init(void)
329 {
330         int i = 0;
331         struct pci_dev *pci_dev = NULL;
332         extern void sn_init_cpei_timer(void);
333 #ifdef CONFIG_PROC_FS
334         extern void register_sn_procfs(void);
335 #endif
336
337         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_SIMULATOR())
338                 return 0;
339
340         /*
341          * This is needed to avoid bounce limit checks in the blk layer
342          */
343         ia64_max_iommu_merge_mask = ~PAGE_MASK;
344         sn_fixup_ionodes();
345         sn_irq = kmalloc(sizeof(struct sn_irq_info *) * NR_IRQS, GFP_KERNEL);
346         if (sn_irq <= 0)
347                 BUG();          /* Canno afford to run out of memory. */
348         memset(sn_irq, 0, sizeof(struct sn_irq_info *) * NR_IRQS);
349
350         sn_init_cpei_timer();
351
352 #ifdef CONFIG_PROC_FS
353         register_sn_procfs();
354 #endif
355
356         for (i = 0; i < PCI_BUSES_TO_SCAN; i++) {
357                 sn_pci_controller_fixup(0, i);
358         }
359
360         /*
361          * Generic Linux PCI Layer has created the pci_bus and pci_dev 
362          * structures - time for us to add our SN PLatform specific 
363          * information.
364          */
365
366         while ((pci_dev =
367                 pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL) {
368                 sn_pci_fixup_slot(pci_dev);
369         }
370
371         sn_ioif_inited = 1;     /* sn I/O infrastructure now initialized */
372
373         return 0;
374 }
375
376 /*
377  * hubdev_init_node() - Creates the HUB data structure and link them to it's 
378  *      own NODE specific data area.
379  */
380 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
381 {
382
383         struct hubdev_info *hubdev_info;
384
385         if (node >= numnodes)   /* Headless/memless IO nodes */
386                 hubdev_info =
387                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(0),
388                                                              sizeof(struct
389                                                                     hubdev_info));
390         else
391                 hubdev_info =
392                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(node),
393                                                              sizeof(struct
394                                                                     hubdev_info));
395         npda->pdinfo = (void *)hubdev_info;
396
397 }
398
399 geoid_t
400 cnodeid_get_geoid(cnodeid_t cnode)
401 {
402
403         struct hubdev_info *hubdev;
404
405         hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
406         return hubdev->hdi_geoid;
407
408 }
409
410 subsys_initcall(sn_pci_init);