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