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