[IA64-SGI] - New SN hardware support - use_alias_space
[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 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                         hubdev->hdi_flush_nasid_list.widget_p[widget] =
226                             sn_flush_device_list;
227                 }
228
229         }
230
231 }
232
233 void sn_pci_unfixup_slot(struct pci_dev *dev)
234 {
235         struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
236
237         sn_irq_unfixup(dev);
238         pci_dev_put(host_pci_dev);
239         pci_dev_put(dev);
240 }
241
242 /*
243  * sn_pci_fixup_slot() - This routine sets up a slot's resources
244  * consistent with the Linux PCI abstraction layer.  Resources acquired
245  * from our PCI provider include PIO maps to BAR space and interrupt
246  * objects.
247  */
248 void sn_pci_fixup_slot(struct pci_dev *dev)
249 {
250         int idx;
251         int segment = pci_domain_nr(dev->bus);
252         int status = 0;
253         struct pcibus_bussoft *bs;
254         struct pci_bus *host_pci_bus;
255         struct pci_dev *host_pci_dev;
256         struct sn_irq_info *sn_irq_info;
257         unsigned long size;
258         unsigned int bus_no, devfn;
259
260         pci_dev_get(dev); /* for the sysdata pointer */
261         dev->sysdata = kmalloc(sizeof(struct pcidev_info), GFP_KERNEL);
262         if (SN_PCIDEV_INFO(dev) <= 0)
263                 BUG();          /* Cannot afford to run out of memory */
264         memset(SN_PCIDEV_INFO(dev), 0, sizeof(struct pcidev_info));
265
266         sn_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
267         if (sn_irq_info <= 0)
268                 BUG();          /* Cannot afford to run out of memory */
269         memset(sn_irq_info, 0, sizeof(struct sn_irq_info));
270
271         /* Call to retrieve pci device information needed by kernel. */
272         status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number, 
273                                      dev->devfn,
274                                      (u64) __pa(SN_PCIDEV_INFO(dev)),
275                                      (u64) __pa(sn_irq_info));
276         if (status)
277                 BUG(); /* Cannot get platform pci device information */
278
279         /* Copy over PIO Mapped Addresses */
280         for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
281                 unsigned long start, end, addr;
282
283                 if (!SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx])
284                         continue;
285
286                 start = dev->resource[idx].start;
287                 end = dev->resource[idx].end;
288                 size = end - start;
289                 addr = SN_PCIDEV_INFO(dev)->pdi_pio_mapped_addr[idx];
290                 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
291                 dev->resource[idx].start = addr;
292                 dev->resource[idx].end = addr + size;
293                 if (dev->resource[idx].flags & IORESOURCE_IO)
294                         dev->resource[idx].parent = &ioport_resource;
295                 else
296                         dev->resource[idx].parent = &iomem_resource;
297         }
298
299         /*
300          * Using the PROMs values for the PCI host bus, get the Linux
301          * PCI host_pci_dev struct and set up host bus linkages
302          */
303
304         bus_no = (SN_PCIDEV_INFO(dev)->pdi_slot_host_handle >> 32) & 0xff;
305         devfn = SN_PCIDEV_INFO(dev)->pdi_slot_host_handle & 0xffffffff;
306         host_pci_bus = pci_find_bus(segment, bus_no);
307         host_pci_dev = pci_get_slot(host_pci_bus, devfn);
308
309         SN_PCIDEV_INFO(dev)->host_pci_dev = host_pci_dev;
310         SN_PCIDEV_INFO(dev)->pdi_host_pcidev_info =
311                                                 SN_PCIDEV_INFO(host_pci_dev);
312         SN_PCIDEV_INFO(dev)->pdi_linux_pcidev = dev;
313         bs = SN_PCIBUS_BUSSOFT(dev->bus);
314         SN_PCIDEV_INFO(dev)->pdi_pcibus_info = bs;
315
316         if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
317                 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
318         } else {
319                 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
320         }
321
322         /* Only set up IRQ stuff if this device has a host bus context */
323         if (bs && sn_irq_info->irq_irq) {
324                 SN_PCIDEV_INFO(dev)->pdi_sn_irq_info = sn_irq_info;
325                 dev->irq = SN_PCIDEV_INFO(dev)->pdi_sn_irq_info->irq_irq;
326                 sn_irq_fixup(dev, sn_irq_info);
327         } else {
328                 SN_PCIDEV_INFO(dev)->pdi_sn_irq_info = NULL;
329                 kfree(sn_irq_info);
330         }
331 }
332
333 /*
334  * sn_pci_controller_fixup() - This routine sets up a bus's resources
335  * consistent with the Linux PCI abstraction layer.
336  */
337 void sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
338 {
339         int status = 0;
340         int nasid, cnode;
341         struct pci_controller *controller;
342         struct pcibus_bussoft *prom_bussoft_ptr;
343         struct hubdev_info *hubdev_info;
344         void *provider_soft;
345         struct sn_pcibus_provider *provider;
346
347         status = sal_get_pcibus_info((u64) segment, (u64) busnum,
348                                      (u64) ia64_tpa(&prom_bussoft_ptr));
349         if (status > 0)
350                 return;         /*bus # does not exist */
351         prom_bussoft_ptr = __va(prom_bussoft_ptr);
352
353         controller = kcalloc(1,sizeof(struct pci_controller), GFP_KERNEL);
354         controller->segment = segment;
355         if (!controller)
356                 BUG();
357
358         if (bus == NULL) {
359                 bus = pci_scan_bus(busnum, &pci_root_ops, controller);
360                 if (bus == NULL)
361                         return; /* error, or bus already scanned */
362                 bus->sysdata = NULL;
363         }
364
365         if (bus->sysdata)
366                 goto error_return; /* sysdata already alloc'd */
367
368         /*
369          * Per-provider fixup.  Copies the contents from prom to local
370          * area and links SN_PCIBUS_BUSSOFT().
371          */
372
373         if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES)
374                 return;         /* unsupported asic type */
375
376         if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
377                 goto error_return; /* no further fixup necessary */
378
379         provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
380         if (provider == NULL)
381                 return;         /* no provider registerd for this asic */
382
383         provider_soft = NULL;
384         if (provider->bus_fixup)
385                 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr, controller);
386
387         if (provider_soft == NULL)
388                 return;         /* fixup failed or not applicable */
389
390         /*
391          * Generic bus fixup goes here.  Don't reference prom_bussoft_ptr
392          * after this point.
393          */
394
395         bus->sysdata = controller;
396         PCI_CONTROLLER(bus)->platform_data = provider_soft;
397         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
398         cnode = nasid_to_cnodeid(nasid);
399         hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
400         SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
401             &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
402
403         /*
404          * If the node information we obtained during the fixup phase is invalid
405          * then set controller->node to -1 (undetermined)
406          */
407         if (controller->node >= num_online_nodes()) {
408                 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
409
410                 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
411                                     "L_IO=%lx L_MEM=%lx BASE=%lx\n",
412                         b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
413                         b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
414                 printk(KERN_WARNING "on node %d but only %d nodes online."
415                         "Association set to undetermined.\n",
416                         controller->node, num_online_nodes());
417                 controller->node = -1;
418         }
419         return;
420
421 error_return:
422
423         kfree(controller);
424         return;
425 }
426
427 void sn_bus_store_sysdata(struct pci_dev *dev)
428 {
429         struct sysdata_el *element;
430
431         element = kcalloc(1, sizeof(struct sysdata_el), GFP_KERNEL);
432         if (!element) {
433                 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
434                 return;
435         }
436         element->sysdata = dev->sysdata;
437         list_add(&element->entry, &sn_sysdata_list);
438 }
439
440 void sn_bus_free_sysdata(void)
441 {
442         struct sysdata_el *element;
443         struct list_head *list;
444
445 sn_sysdata_free_start:
446         list_for_each(list, &sn_sysdata_list) {
447                 element = list_entry(list, struct sysdata_el, entry);
448                 list_del(&element->entry);
449                 kfree(element->sysdata);
450                 kfree(element);
451                 goto sn_sysdata_free_start;
452         }
453         return;
454 }
455
456 /*
457  * Ugly hack to get PCI setup until we have a proper ACPI namespace.
458  */
459
460 #define PCI_BUSES_TO_SCAN 256
461
462 static int __init sn_pci_init(void)
463 {
464         int i = 0;
465         int j = 0;
466         struct pci_dev *pci_dev = NULL;
467         extern void sn_init_cpei_timer(void);
468 #ifdef CONFIG_PROC_FS
469         extern void register_sn_procfs(void);
470 #endif
471
472         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
473                 return 0;
474
475         /*
476          * prime sn_pci_provider[].  Individial provider init routines will
477          * override their respective default entries.
478          */
479
480         for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
481                 sn_pci_provider[i] = &sn_pci_default_provider;
482
483         pcibr_init_provider();
484         tioca_init_provider();
485         tioce_init_provider();
486
487         /*
488          * This is needed to avoid bounce limit checks in the blk layer
489          */
490         ia64_max_iommu_merge_mask = ~PAGE_MASK;
491         sn_fixup_ionodes();
492         sn_irq_lh_init();
493         INIT_LIST_HEAD(&sn_sysdata_list);
494         sn_init_cpei_timer();
495
496 #ifdef CONFIG_PROC_FS
497         register_sn_procfs();
498 #endif
499
500         /* busses are not known yet ... */
501         for (i = 0; i <= max_segment_number; i++)
502                 for (j = 0; j <= max_pcibus_number; j++)
503                         sn_pci_controller_fixup(i, j, NULL);
504
505         /*
506          * Generic Linux PCI Layer has created the pci_bus and pci_dev 
507          * structures - time for us to add our SN PLatform specific 
508          * information.
509          */
510
511         while ((pci_dev =
512                 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL)
513                 sn_pci_fixup_slot(pci_dev);
514
515         sn_ioif_inited = 1;     /* sn I/O infrastructure now initialized */
516
517         return 0;
518 }
519
520 /*
521  * hubdev_init_node() - Creates the HUB data structure and link them to it's 
522  *      own NODE specific data area.
523  */
524 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
525 {
526
527         struct hubdev_info *hubdev_info;
528
529         if (node >= num_online_nodes()) /* Headless/memless IO nodes */
530                 hubdev_info =
531                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(0),
532                                                              sizeof(struct
533                                                                     hubdev_info));
534         else
535                 hubdev_info =
536                     (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(node),
537                                                              sizeof(struct
538                                                                     hubdev_info));
539         npda->pdinfo = (void *)hubdev_info;
540
541 }
542
543 geoid_t
544 cnodeid_get_geoid(cnodeid_t cnode)
545 {
546
547         struct hubdev_info *hubdev;
548
549         hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
550         return hubdev->hdi_geoid;
551
552 }
553
554 subsys_initcall(sn_pci_init);
555 EXPORT_SYMBOL(sn_pci_fixup_slot);
556 EXPORT_SYMBOL(sn_pci_unfixup_slot);
557 EXPORT_SYMBOL(sn_pci_controller_fixup);
558 EXPORT_SYMBOL(sn_bus_store_sysdata);
559 EXPORT_SYMBOL(sn_bus_free_sysdata);