Pull ngam-maule-steiner into release branch
[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/addrs.h>
13 #include <asm/sn/geo.h>
14 #include <asm/sn/io.h>
15 #include <asm/sn/pcibr_provider.h>
16 #include <asm/sn/pcibus_provider_defs.h>
17 #include <asm/sn/pcidev.h>
18 #include <asm/sn/simulator.h>
19 #include <asm/sn/sn_sal.h>
20 #include <asm/sn/tioca_provider.h>
21 #include <asm/sn/tioce_provider.h>
22 #include "xtalk/hubdev.h"
23 #include "xtalk/xwidgetdev.h"
24
25 nasid_t master_nasid = INVALID_NASID;   /* Partition Master */
26
27 static struct list_head sn_sysdata_list;
28
29 /* sysdata list struct */
30 struct sysdata_el {
31         struct list_head entry;
32         void *sysdata;
33 };
34
35 struct slab_info {
36         struct hubdev_info hubdev;
37 };
38
39 struct brick {
40         moduleid_t id;          /* Module ID of this module        */
41         struct slab_info slab_info[MAX_SLABS + 1];
42 };
43
44 int sn_ioif_inited = 0;         /* SN I/O infrastructure initialized? */
45
46 struct sn_pcibus_provider *sn_pci_provider[PCIIO_ASIC_MAX_TYPES];       /* indexed by asic type */
47
48 static int max_segment_number = 0; /* Default highest segment number */
49 static int max_pcibus_number = 255; /* Default highest pci bus number */
50
51 /*
52  * Hooks and struct for unsupported pci providers
53  */
54
55 static dma_addr_t
56 sn_default_pci_map(struct pci_dev *pdev, unsigned long paddr, size_t size)
57 {
58         return 0;
59 }
60
61 static void
62 sn_default_pci_unmap(struct pci_dev *pdev, dma_addr_t addr, int direction)
63 {
64         return;
65 }
66
67 static void *
68 sn_default_pci_bus_fixup(struct pcibus_bussoft *soft, struct pci_controller *controller)
69 {
70         return NULL;
71 }
72
73 static struct sn_pcibus_provider sn_pci_default_provider = {
74         .dma_map = sn_default_pci_map,
75         .dma_map_consistent = sn_default_pci_map,
76         .dma_unmap = sn_default_pci_unmap,
77         .bus_fixup = sn_default_pci_bus_fixup,
78 };
79
80 /*
81  * Retrieve the DMA Flush List given nasid.  This list is needed 
82  * to implement the WAR - Flush DMA data on PIO Reads.
83  */
84 static inline uint64_t
85 sal_get_widget_dmaflush_list(u64 nasid, u64 widget_num, u64 address)
86 {
87
88         struct ia64_sal_retval ret_stuff;
89         ret_stuff.status = 0;
90         ret_stuff.v0 = 0;
91
92         SAL_CALL_NOLOCK(ret_stuff,
93                         (u64) SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
94                         (u64) nasid, (u64) widget_num, (u64) address, 0, 0, 0,
95                         0);
96         return ret_stuff.v0;
97
98 }
99
100 /*
101  * Retrieve the hub device info structure for the given nasid.
102  */
103 static inline uint64_t sal_get_hubdev_info(u64 handle, u64 address)
104 {
105
106         struct ia64_sal_retval ret_stuff;
107         ret_stuff.status = 0;
108         ret_stuff.v0 = 0;
109
110         SAL_CALL_NOLOCK(ret_stuff,
111                         (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
112                         (u64) handle, (u64) address, 0, 0, 0, 0, 0);
113         return ret_stuff.v0;
114 }
115
116 /*
117  * Retrieve the pci bus information given the bus number.
118  */
119 static inline uint64_t sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
120 {
121
122         struct ia64_sal_retval ret_stuff;
123         ret_stuff.status = 0;
124         ret_stuff.v0 = 0;
125
126         SAL_CALL_NOLOCK(ret_stuff,
127                         (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
128                         (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
129         return ret_stuff.v0;
130 }
131
132 /*
133  * Retrieve the pci device information given the bus and device|function number.
134  */
135 static inline uint64_t
136 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev, 
137                         u64 sn_irq_info)
138 {
139         struct ia64_sal_retval ret_stuff;
140         ret_stuff.status = 0;
141         ret_stuff.v0 = 0;
142
143         SAL_CALL_NOLOCK(ret_stuff,
144                         (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
145                         (u64) segment, (u64) bus_number, (u64) devfn, 
146                         (u64) pci_dev,
147                         sn_irq_info, 0, 0);
148         return ret_stuff.v0;
149 }
150
151 /*
152  * sn_fixup_ionodes() - This routine initializes the HUB data strcuture for 
153  *      each node in the system.
154  */
155 static void sn_fixup_ionodes(void)
156 {
157
158         struct sn_flush_device_list *sn_flush_device_list;
159         struct hubdev_info *hubdev;
160         uint64_t status;
161         uint64_t nasid;
162         int i, widget;
163
164         /*
165          * Get SGI Specific HUB chipset information.
166          * Inform Prom that this kernel can support domain bus numbering.
167          */
168         for (i = 0; i < numionodes; i++) {
169                 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
170                 nasid = cnodeid_to_nasid(i);
171                 hubdev->max_segment_number = 0xffffffff;
172                 hubdev->max_pcibus_number = 0xff;
173                 status = sal_get_hubdev_info(nasid, (uint64_t) __pa(hubdev));
174                 if (status)
175                         continue;
176
177                 /* Save the largest Domain and pcibus numbers found. */
178                 if (hubdev->max_segment_number) {
179                         /*
180                          * Dealing with a Prom that supports segments.
181                          */
182                         max_segment_number = hubdev->max_segment_number;
183                         max_pcibus_number = hubdev->max_pcibus_number;
184                 }
185
186                 /* Attach the error interrupt handlers */
187                 if (nasid & 1)
188                         ice_error_init(hubdev);
189                 else
190                         hub_error_init(hubdev);
191
192                 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
193                         hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
194
195                 if (!hubdev->hdi_flush_nasid_list.widget_p)
196                         continue;
197
198                 hubdev->hdi_flush_nasid_list.widget_p =
199                     kmalloc((HUB_WIDGET_ID_MAX + 1) *
200                             sizeof(struct sn_flush_device_list *), GFP_KERNEL);
201
202                 memset(hubdev->hdi_flush_nasid_list.widget_p, 0x0,
203                        (HUB_WIDGET_ID_MAX + 1) *
204                        sizeof(struct sn_flush_device_list *));
205
206                 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
207                         sn_flush_device_list = kmalloc(DEV_PER_WIDGET *
208                                                        sizeof(struct
209                                                               sn_flush_device_list),
210                                                        GFP_KERNEL);
211                         memset(sn_flush_device_list, 0x0,
212                                DEV_PER_WIDGET *
213                                sizeof(struct sn_flush_device_list));
214
215                         status =
216                             sal_get_widget_dmaflush_list(nasid, widget,
217                                                          (uint64_t)
218                                                          __pa
219                                                          (sn_flush_device_list));
220                         if (status) {
221                                 kfree(sn_flush_device_list);
222                                 continue;
223                         }
224
225                         spin_lock_init(&sn_flush_device_list->sfdl_flush_lock);
226                         hubdev->hdi_flush_nasid_list.widget_p[widget] =
227                             sn_flush_device_list;
228                 }
229
230         }
231
232 }
233
234 void sn_pci_unfixup_slot(struct pci_dev *dev)
235 {
236         struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
237
238         sn_irq_unfixup(dev);
239         pci_dev_put(host_pci_dev);
240         pci_dev_put(dev);
241 }
242
243 /*
244  * sn_pci_fixup_slot() - This routine sets up a slot's resources
245  * consistent with the Linux PCI abstraction layer.  Resources acquired
246  * from our PCI provider include PIO maps to BAR space and interrupt
247  * objects.
248  */
249 void sn_pci_fixup_slot(struct pci_dev *dev)
250 {
251         int idx;
252         int segment = pci_domain_nr(dev->bus);
253         int status = 0;
254         struct pcibus_bussoft *bs;
255         struct pci_bus *host_pci_bus;
256         struct pci_dev *host_pci_dev;
257         struct sn_irq_info *sn_irq_info;
258         unsigned long size;
259         unsigned int bus_no, devfn;
260
261         pci_dev_get(dev); /* for the sysdata pointer */
262         dev->sysdata = kmalloc(sizeof(struct pcidev_info), GFP_KERNEL);
263         if (SN_PCIDEV_INFO(dev) <= 0)
264                 BUG();          /* Cannot afford to run out of memory */
265         memset(SN_PCIDEV_INFO(dev), 0, sizeof(struct pcidev_info));
266
267         sn_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
268         if (sn_irq_info <= 0)
269                 BUG();          /* Cannot afford to run out of memory */
270         memset(sn_irq_info, 0, sizeof(struct sn_irq_info));
271
272         /* Call to retrieve pci device information needed by kernel. */
273         status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number, 
274                                      dev->devfn,
275                                      (u64) __pa(SN_PCIDEV_INFO(dev)),
276                                      (u64) __pa(sn_irq_info));
277         if (status)
278                 BUG(); /* Cannot get platform pci device information */
279
280         /* Copy over PIO Mapped Addresses */
281         for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
282                 unsigned long start, end, addr;
283
284                 if (!SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx])
285                         continue;
286
287                 start = dev->resource[idx].start;
288                 end = dev->resource[idx].end;
289                 size = end - start;
290                 addr = SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx];
291                 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
292                 dev->resource[idx].start = addr;
293                 dev->resource[idx].end = addr + size;
294                 if (dev->resource[idx].flags & IORESOURCE_IO)
295                         dev->resource[idx].parent = &ioport_resource;
296                 else
297                         dev->resource[idx].parent = &iomem_resource;
298         }
299
300         /*
301          * Using the PROMs values for the PCI host bus, get the Linux
302          * PCI host_pci_dev struct and set up host bus linkages
303          */
304
305         bus_no = (SN_PCIDEV_INFO(dev)->pdi_slot_host_handle >> 32) & 0xff;
306         devfn = SN_PCIDEV_INFO(dev)->pdi_slot_host_handle & 0xffffffff;
307         host_pci_bus = pci_find_bus(segment, bus_no);
308         host_pci_dev = pci_get_slot(host_pci_bus, devfn);
309
310         SN_PCIDEV_INFO(dev)->host_pci_dev = host_pci_dev;
311         SN_PCIDEV_INFO(dev)->pdi_host_pcidev_info =
312                                                 SN_PCIDEV_INFO(host_pci_dev);
313         SN_PCIDEV_INFO(dev)->pdi_linux_pcidev = dev;
314         bs = SN_PCIBUS_BUSSOFT(dev->bus);
315         SN_PCIDEV_INFO(dev)->pdi_pcibus_info = bs;
316
317         if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
318                 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
319         } else {
320                 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
321         }
322
323         /* Only set up IRQ stuff if this device has a host bus context */
324         if (bs && sn_irq_info->irq_irq) {
325                 SN_PCIDEV_INFO(dev)->pdi_sn_irq_info = sn_irq_info;
326                 dev->irq = SN_PCIDEV_INFO(dev)->pdi_sn_irq_info->irq_irq;
327                 sn_irq_fixup(dev, sn_irq_info);
328         } else {
329                 SN_PCIDEV_INFO(dev)->pdi_sn_irq_info = NULL;
330                 kfree(sn_irq_info);
331         }
332 }
333
334 /*
335  * sn_pci_controller_fixup() - This routine sets up a bus's resources
336  * consistent with the Linux PCI abstraction layer.
337  */
338 void sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
339 {
340         int status = 0;
341         int nasid, cnode;
342         struct pci_controller *controller;
343         struct pcibus_bussoft *prom_bussoft_ptr;
344         struct hubdev_info *hubdev_info;
345         void *provider_soft = NULL;
346         struct sn_pcibus_provider *provider;
347
348         status = sal_get_pcibus_info((u64) segment, (u64) busnum,
349                                      (u64) ia64_tpa(&prom_bussoft_ptr));
350         if (status > 0)
351                 return;         /*bus # does not exist */
352         prom_bussoft_ptr = __va(prom_bussoft_ptr);
353
354         controller = kcalloc(1,sizeof(struct pci_controller), GFP_KERNEL);
355         controller->segment = segment;
356         if (!controller)
357                 BUG();
358
359         if (bus == NULL) {
360                 bus = pci_scan_bus(busnum, &pci_root_ops, controller);
361                 if (bus == NULL)
362                         goto error_return; /* error, or bus already scanned */
363                 bus->sysdata = NULL;
364         }
365
366         if (bus->sysdata)
367                 goto error_return; /* sysdata already alloc'd */
368
369         /*
370          * Per-provider fixup.  Copies the contents from prom to local
371          * area and links SN_PCIBUS_BUSSOFT().
372          */
373
374         if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES)
375                 goto error_return; /* unsupported asic type */
376
377         if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
378                 goto error_return; /* no further fixup necessary */
379
380         provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
381         if (provider == NULL)
382                 goto error_return; /* no provider registerd for this asic */
383
384         bus->sysdata = controller;
385         if (provider->bus_fixup)
386                 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr, controller);
387
388         if (provider_soft == NULL) {
389                 /* fixup failed or not applicable */
390                 bus->sysdata = NULL;
391                 goto error_return;
392         }
393
394         /*
395          * Generic bus fixup goes here.  Don't reference prom_bussoft_ptr
396          * after this point.
397          */
398
399         PCI_CONTROLLER(bus)->platform_data = provider_soft;
400         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
401         cnode = nasid_to_cnodeid(nasid);
402         hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
403         SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
404             &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
405
406         /*
407          * If the node information we obtained during the fixup phase is invalid
408          * then set controller->node to -1 (undetermined)
409          */
410         if (controller->node >= num_online_nodes()) {
411                 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
412
413                 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
414                                     "L_IO=%lx L_MEM=%lx BASE=%lx\n",
415                         b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
416                         b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
417                 printk(KERN_WARNING "on node %d but only %d nodes online."
418                         "Association set to undetermined.\n",
419                         controller->node, num_online_nodes());
420                 controller->node = -1;
421         }
422         return;
423
424 error_return:
425
426         kfree(controller);
427         return;
428 }
429
430 void sn_bus_store_sysdata(struct pci_dev *dev)
431 {
432         struct sysdata_el *element;
433
434         element = kcalloc(1, sizeof(struct sysdata_el), GFP_KERNEL);
435         if (!element) {
436                 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
437                 return;
438         }
439         element->sysdata = dev->sysdata;
440         list_add(&element->entry, &sn_sysdata_list);
441 }
442
443 void sn_bus_free_sysdata(void)
444 {
445         struct sysdata_el *element;
446         struct list_head *list;
447
448 sn_sysdata_free_start:
449         list_for_each(list, &sn_sysdata_list) {
450                 element = list_entry(list, struct sysdata_el, entry);
451                 list_del(&element->entry);
452                 kfree(element->sysdata);
453                 kfree(element);
454                 goto sn_sysdata_free_start;
455         }
456         return;
457 }
458
459 /*
460  * Ugly hack to get PCI setup until we have a proper ACPI namespace.
461  */
462
463 #define PCI_BUSES_TO_SCAN 256
464
465 static int __init sn_pci_init(void)
466 {
467         int i = 0;
468         int j = 0;
469         struct pci_dev *pci_dev = NULL;
470         extern void sn_init_cpei_timer(void);
471 #ifdef CONFIG_PROC_FS
472         extern void register_sn_procfs(void);
473 #endif
474
475         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
476                 return 0;
477
478         /*
479          * prime sn_pci_provider[].  Individial provider init routines will
480          * override their respective default entries.
481          */
482
483         for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
484                 sn_pci_provider[i] = &sn_pci_default_provider;
485
486         pcibr_init_provider();
487         tioca_init_provider();
488         tioce_init_provider();
489
490         /*
491          * This is needed to avoid bounce limit checks in the blk layer
492          */
493         ia64_max_iommu_merge_mask = ~PAGE_MASK;
494         sn_fixup_ionodes();
495         sn_irq_lh_init();
496         INIT_LIST_HEAD(&sn_sysdata_list);
497         sn_init_cpei_timer();
498
499 #ifdef CONFIG_PROC_FS
500         register_sn_procfs();
501 #endif
502
503         /* busses are not known yet ... */
504         for (i = 0; i <= max_segment_number; i++)
505                 for (j = 0; j <= max_pcibus_number; j++)
506                         sn_pci_controller_fixup(i, j, NULL);
507
508         /*
509          * Generic Linux PCI Layer has created the pci_bus and pci_dev 
510          * structures - time for us to add our SN PLatform specific 
511          * information.
512          */
513
514         while ((pci_dev =
515                 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL)
516                 sn_pci_fixup_slot(pci_dev);
517
518         sn_ioif_inited = 1;     /* sn I/O infrastructure now initialized */
519
520         return 0;
521 }
522
523 /*
524  * hubdev_init_node() - Creates the HUB data structure and link them to it's 
525  *      own NODE specific data area.
526  */
527 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
528 {
529
530         struct hubdev_info *hubdev_info;
531
532         if (node >= num_online_nodes()) /* Headless/memless IO nodes */
533                 hubdev_info =
534                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(0),
535                                                              sizeof(struct
536                                                                     hubdev_info));
537         else
538                 hubdev_info =
539                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(node),
540                                                              sizeof(struct
541                                                                     hubdev_info));
542         npda->pdinfo = (void *)hubdev_info;
543
544 }
545
546 geoid_t
547 cnodeid_get_geoid(cnodeid_t cnode)
548 {
549
550         struct hubdev_info *hubdev;
551
552         hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
553         return hubdev->hdi_geoid;
554
555 }
556
557 subsys_initcall(sn_pci_init);
558 EXPORT_SYMBOL(sn_pci_fixup_slot);
559 EXPORT_SYMBOL(sn_pci_unfixup_slot);
560 EXPORT_SYMBOL(sn_pci_controller_fixup);
561 EXPORT_SYMBOL(sn_bus_store_sysdata);
562 EXPORT_SYMBOL(sn_bus_free_sysdata);