ACPICA: minimal patch to integrate new tables into Linux
[pandora-kernel.git] / arch / ia64 / sn / kernel / io_common.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) 2006 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/bootmem.h>
10 #include <asm/sn/types.h>
11 #include <asm/sn/addrs.h>
12 #include <asm/sn/sn_feature_sets.h>
13 #include <asm/sn/geo.h>
14 #include <asm/sn/io.h>
15 #include <asm/sn/l1.h>
16 #include <asm/sn/module.h>
17 #include <asm/sn/pcibr_provider.h>
18 #include <asm/sn/pcibus_provider_defs.h>
19 #include <asm/sn/pcidev.h>
20 #include <asm/sn/simulator.h>
21 #include <asm/sn/sn_sal.h>
22 #include <asm/sn/tioca_provider.h>
23 #include <asm/sn/tioce_provider.h>
24 #include "xtalk/hubdev.h"
25 #include "xtalk/xwidgetdev.h"
26 #include <linux/acpi.h>
27 #include <asm/sn/sn2/sn_hwperf.h>
28
29 extern void sn_init_cpei_timer(void);
30 extern void register_sn_procfs(void);
31 extern void sn_acpi_bus_fixup(struct pci_bus *);
32 extern void sn_bus_fixup(struct pci_bus *);
33 extern void sn_acpi_slot_fixup(struct pci_dev *, struct pcidev_info *);
34 extern void sn_more_slot_fixup(struct pci_dev *, struct pcidev_info *);
35 extern void sn_legacy_pci_window_fixup(struct pci_controller *, u64, u64);
36 extern void sn_io_acpi_init(void);
37 extern void sn_io_init(void);
38
39 static struct list_head sn_sysdata_list;
40
41 /* sysdata list struct */
42 struct sysdata_el {
43         struct list_head entry;
44         void *sysdata;
45 };
46
47 int sn_ioif_inited;             /* SN I/O infrastructure initialized? */
48
49 struct sn_pcibus_provider *sn_pci_provider[PCIIO_ASIC_MAX_TYPES];       /* indexed by asic type */
50
51 int sn_acpi_base_support()
52 {
53         struct acpi_table_header *header;
54         (void)acpi_get_table_by_index(ACPI_TABLE_INDEX_DSDT, &header);
55         if (header && header->oem_revision >= 0x20101)
56                 return 1;
57         return 0;
58 }
59
60 /*
61  * Hooks and struct for unsupported pci providers
62  */
63
64 static dma_addr_t
65 sn_default_pci_map(struct pci_dev *pdev, unsigned long paddr, size_t size, int type)
66 {
67         return 0;
68 }
69
70 static void
71 sn_default_pci_unmap(struct pci_dev *pdev, dma_addr_t addr, int direction)
72 {
73         return;
74 }
75
76 static void *
77 sn_default_pci_bus_fixup(struct pcibus_bussoft *soft, struct pci_controller *controller)
78 {
79         return NULL;
80 }
81
82 static struct sn_pcibus_provider sn_pci_default_provider = {
83         .dma_map = sn_default_pci_map,
84         .dma_map_consistent = sn_default_pci_map,
85         .dma_unmap = sn_default_pci_unmap,
86         .bus_fixup = sn_default_pci_bus_fixup,
87 };
88
89 /*
90  * Retrieve the DMA Flush List given nasid, widget, and device.
91  * This list is needed to implement the WAR - Flush DMA data on PIO Reads.
92  */
93 static inline u64
94 sal_get_device_dmaflush_list(u64 nasid, u64 widget_num, u64 device_num,
95                              u64 address)
96 {
97         struct ia64_sal_retval ret_stuff;
98         ret_stuff.status = 0;
99         ret_stuff.v0 = 0;
100
101         SAL_CALL_NOLOCK(ret_stuff,
102                         (u64) SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST,
103                         (u64) nasid, (u64) widget_num,
104                         (u64) device_num, (u64) address, 0, 0, 0);
105         return ret_stuff.status;
106 }
107
108 /*
109  * Retrieve the pci device information given the bus and device|function number.
110  */
111 static inline u64
112 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev,
113                     u64 sn_irq_info)
114 {
115         struct ia64_sal_retval ret_stuff;
116         ret_stuff.status = 0;
117         ret_stuff.v0 = 0;
118
119         SAL_CALL_NOLOCK(ret_stuff,
120                         (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
121                         (u64) segment, (u64) bus_number, (u64) devfn,
122                         (u64) pci_dev,
123                         sn_irq_info, 0, 0);
124         return ret_stuff.v0;
125 }
126
127 /*
128  * sn_pcidev_info_get() - Retrieve the pcidev_info struct for the specified
129  *                        device.
130  */
131 inline struct pcidev_info *
132 sn_pcidev_info_get(struct pci_dev *dev)
133 {
134         struct pcidev_info *pcidev;
135
136         list_for_each_entry(pcidev,
137                             &(SN_PLATFORM_DATA(dev)->pcidev_info), pdi_list) {
138                 if (pcidev->pdi_linux_pcidev == dev)
139                         return pcidev;
140         }
141         return NULL;
142 }
143
144 /* Older PROM flush WAR
145  *
146  * 01/16/06 -- This war will be in place until a new official PROM is released.
147  * Additionally note that the struct sn_flush_device_war also has to be
148  * removed from arch/ia64/sn/include/xtalk/hubdev.h
149  */
150 static u8 war_implemented = 0;
151
152 static s64 sn_device_fixup_war(u64 nasid, u64 widget, int device,
153                                struct sn_flush_device_common *common)
154 {
155         struct sn_flush_device_war *war_list;
156         struct sn_flush_device_war *dev_entry;
157         struct ia64_sal_retval isrv = {0,0,0,0};
158
159         if (!war_implemented) {
160                 printk(KERN_WARNING "PROM version < 4.50 -- implementing old "
161                        "PROM flush WAR\n");
162                 war_implemented = 1;
163         }
164
165         war_list = kzalloc(DEV_PER_WIDGET * sizeof(*war_list), GFP_KERNEL);
166         if (!war_list)
167                 BUG();
168
169         SAL_CALL_NOLOCK(isrv, SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
170                         nasid, widget, __pa(war_list), 0, 0, 0 ,0);
171         if (isrv.status)
172                 panic("sn_device_fixup_war failed: %s\n",
173                       ia64_sal_strerror(isrv.status));
174
175         dev_entry = war_list + device;
176         memcpy(common,dev_entry, sizeof(*common));
177         kfree(war_list);
178
179         return isrv.status;
180 }
181
182 /*
183  * sn_common_hubdev_init() - This routine is called to initialize the HUB data
184  *                           structure for each node in the system.
185  */
186 void __init
187 sn_common_hubdev_init(struct hubdev_info *hubdev)
188 {
189
190         struct sn_flush_device_kernel *sn_flush_device_kernel;
191         struct sn_flush_device_kernel *dev_entry;
192         s64 status;
193         int widget, device, size;
194
195         /* Attach the error interrupt handlers */
196         if (hubdev->hdi_nasid & 1)      /* If TIO */
197                 ice_error_init(hubdev);
198         else
199                 hub_error_init(hubdev);
200
201         for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
202                 hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
203
204         if (!hubdev->hdi_flush_nasid_list.widget_p)
205                 return;
206
207         size = (HUB_WIDGET_ID_MAX + 1) *
208                 sizeof(struct sn_flush_device_kernel *);
209         hubdev->hdi_flush_nasid_list.widget_p =
210                 kzalloc(size, GFP_KERNEL);
211         if (!hubdev->hdi_flush_nasid_list.widget_p)
212                 BUG();
213
214         for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
215                 size = DEV_PER_WIDGET *
216                         sizeof(struct sn_flush_device_kernel);
217                 sn_flush_device_kernel = kzalloc(size, GFP_KERNEL);
218                 if (!sn_flush_device_kernel)
219                         BUG();
220
221                 dev_entry = sn_flush_device_kernel;
222                 for (device = 0; device < DEV_PER_WIDGET;
223                      device++, dev_entry++) {
224                         size = sizeof(struct sn_flush_device_common);
225                         dev_entry->common = kzalloc(size, GFP_KERNEL);
226                         if (!dev_entry->common)
227                                 BUG();
228                         if (sn_prom_feature_available(PRF_DEVICE_FLUSH_LIST))
229                                 status = sal_get_device_dmaflush_list(
230                                              hubdev->hdi_nasid, widget, device,
231                                              (u64)(dev_entry->common));
232                         else
233                                 status = sn_device_fixup_war(hubdev->hdi_nasid,
234                                                              widget, device,
235                                                              dev_entry->common);
236                         if (status != SALRET_OK)
237                                 panic("SAL call failed: %s\n",
238                                       ia64_sal_strerror(status));
239
240                         spin_lock_init(&dev_entry->sfdl_flush_lock);
241                 }
242
243                 if (sn_flush_device_kernel)
244                         hubdev->hdi_flush_nasid_list.widget_p[widget] =
245                                                          sn_flush_device_kernel;
246         }
247 }
248
249 void sn_pci_unfixup_slot(struct pci_dev *dev)
250 {
251         struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
252
253         sn_irq_unfixup(dev);
254         pci_dev_put(host_pci_dev);
255         pci_dev_put(dev);
256 }
257
258 /*
259  * sn_pci_fixup_slot() - This routine sets up a slot's resources consistent
260  *                       with the Linux PCI abstraction layer. Resources
261  *                       acquired from our PCI provider include PIO maps
262  *                       to BAR space and interrupt objects.
263  */
264 void sn_pci_fixup_slot(struct pci_dev *dev)
265 {
266         int segment = pci_domain_nr(dev->bus);
267         int status = 0;
268         struct pcibus_bussoft *bs;
269         struct pci_bus *host_pci_bus;
270         struct pci_dev *host_pci_dev;
271         struct pcidev_info *pcidev_info;
272         struct sn_irq_info *sn_irq_info;
273         unsigned int bus_no, devfn;
274
275         pci_dev_get(dev); /* for the sysdata pointer */
276         pcidev_info = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
277         if (!pcidev_info)
278                 BUG();          /* Cannot afford to run out of memory */
279
280         sn_irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
281         if (!sn_irq_info)
282                 BUG();          /* Cannot afford to run out of memory */
283
284         /* Call to retrieve pci device information needed by kernel. */
285         status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number,
286                                      dev->devfn,
287                                      (u64) __pa(pcidev_info),
288                                      (u64) __pa(sn_irq_info));
289         if (status)
290                 BUG(); /* Cannot get platform pci device information */
291
292         /* Add pcidev_info to list in pci_controller.platform_data */
293         list_add_tail(&pcidev_info->pdi_list,
294                       &(SN_PLATFORM_DATA(dev->bus)->pcidev_info));
295
296         if (sn_acpi_base_support())
297                 sn_acpi_slot_fixup(dev, pcidev_info);
298         else
299                 sn_more_slot_fixup(dev, pcidev_info);
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 = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
306         devfn = pcidev_info->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         pcidev_info->host_pci_dev = host_pci_dev;
311         pcidev_info->pdi_linux_pcidev = dev;
312         pcidev_info->pdi_host_pcidev_info = SN_PCIDEV_INFO(host_pci_dev);
313         bs = SN_PCIBUS_BUSSOFT(dev->bus);
314         pcidev_info->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                 pcidev_info->pdi_sn_irq_info = sn_irq_info;
325                 dev->irq = pcidev_info->pdi_sn_irq_info->irq_irq;
326                 sn_irq_fixup(dev, sn_irq_info);
327         } else {
328                 pcidev_info->pdi_sn_irq_info = NULL;
329                 kfree(sn_irq_info);
330         }
331 }
332
333 /*
334  * sn_common_bus_fixup - Perform platform specific bus fixup.
335  *                       Execute the ASIC specific fixup routine
336  *                       for this bus.
337  */
338 void
339 sn_common_bus_fixup(struct pci_bus *bus,
340                     struct pcibus_bussoft *prom_bussoft_ptr)
341 {
342         int cnode;
343         struct pci_controller *controller;
344         struct hubdev_info *hubdev_info;
345         int nasid;
346         void *provider_soft;
347         struct sn_pcibus_provider *provider;
348         struct sn_platform_data *sn_platform_data;
349
350         controller = PCI_CONTROLLER(bus);
351         /*
352          * Per-provider fixup.  Copies the bus soft structure from prom
353          * to local area and links SN_PCIBUS_BUSSOFT().
354          */
355
356         if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES) {
357                 printk(KERN_WARNING "sn_common_bus_fixup: Unsupported asic type, %d",
358                        prom_bussoft_ptr->bs_asic_type);
359                 return;
360         }
361
362         if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
363                 return; /* no further fixup necessary */
364
365         provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
366         if (provider == NULL)
367                 panic("sn_common_bus_fixup: No provider registered for this asic type, %d",
368                       prom_bussoft_ptr->bs_asic_type);
369
370         if (provider->bus_fixup)
371                 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr,
372                                  controller);
373         else
374                 provider_soft = NULL;
375
376         /*
377          * Generic bus fixup goes here.  Don't reference prom_bussoft_ptr
378          * after this point.
379          */
380         controller->platform_data = kzalloc(sizeof(struct sn_platform_data),
381                                             GFP_KERNEL);
382         if (controller->platform_data == NULL)
383                 BUG();
384         sn_platform_data =
385                         (struct sn_platform_data *) controller->platform_data;
386         sn_platform_data->provider_soft = provider_soft;
387         INIT_LIST_HEAD(&((struct sn_platform_data *)
388                          controller->platform_data)->pcidev_info);
389         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
390         cnode = nasid_to_cnodeid(nasid);
391         hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
392         SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
393             &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
394
395         /*
396          * If the node information we obtained during the fixup phase is
397          * invalid then set controller->node to -1 (undetermined)
398          */
399         if (controller->node >= num_online_nodes()) {
400                 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
401
402                 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
403                        "L_IO=%lx L_MEM=%lx BASE=%lx\n",
404                        b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
405                        b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
406                 printk(KERN_WARNING "on node %d but only %d nodes online."
407                        "Association set to undetermined.\n",
408                        controller->node, num_online_nodes());
409                 controller->node = -1;
410         }
411 }
412
413 void sn_bus_store_sysdata(struct pci_dev *dev)
414 {
415         struct sysdata_el *element;
416
417         element = kzalloc(sizeof(struct sysdata_el), GFP_KERNEL);
418         if (!element) {
419                 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
420                 return;
421         }
422         element->sysdata = SN_PCIDEV_INFO(dev);
423         list_add(&element->entry, &sn_sysdata_list);
424 }
425
426 void sn_bus_free_sysdata(void)
427 {
428         struct sysdata_el *element;
429         struct list_head *list, *safe;
430
431         list_for_each_safe(list, safe, &sn_sysdata_list) {
432                 element = list_entry(list, struct sysdata_el, entry);
433                 list_del(&element->entry);
434                 list_del(&(((struct pcidev_info *)
435                              (element->sysdata))->pdi_list));
436                 kfree(element->sysdata);
437                 kfree(element);
438         }
439         return;
440 }
441
442 /*
443  * hubdev_init_node() - Creates the HUB data structure and link them to it's
444  *                      own NODE specific data area.
445  */
446 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
447 {
448         struct hubdev_info *hubdev_info;
449         int size;
450         pg_data_t *pg;
451
452         size = sizeof(struct hubdev_info);
453
454         if (node >= num_online_nodes()) /* Headless/memless IO nodes */
455                 pg = NODE_DATA(0);
456         else
457                 pg = NODE_DATA(node);
458
459         hubdev_info = (struct hubdev_info *)alloc_bootmem_node(pg, size);
460
461         npda->pdinfo = (void *)hubdev_info;
462 }
463
464 geoid_t
465 cnodeid_get_geoid(cnodeid_t cnode)
466 {
467         struct hubdev_info *hubdev;
468
469         hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
470         return hubdev->hdi_geoid;
471 }
472
473 void sn_generate_path(struct pci_bus *pci_bus, char *address)
474 {
475         nasid_t nasid;
476         cnodeid_t cnode;
477         geoid_t geoid;
478         moduleid_t moduleid;
479         u16 bricktype;
480
481         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(pci_bus)->bs_base);
482         cnode = nasid_to_cnodeid(nasid);
483         geoid = cnodeid_get_geoid(cnode);
484         moduleid = geo_module(geoid);
485
486         sprintf(address, "module_%c%c%c%c%.2d",
487                 '0'+RACK_GET_CLASS(MODULE_GET_RACK(moduleid)),
488                 '0'+RACK_GET_GROUP(MODULE_GET_RACK(moduleid)),
489                 '0'+RACK_GET_NUM(MODULE_GET_RACK(moduleid)),
490                 MODULE_GET_BTCHAR(moduleid), MODULE_GET_BPOS(moduleid));
491
492         /* Tollhouse requires slot id to be displayed */
493         bricktype = MODULE_GET_BTYPE(moduleid);
494         if ((bricktype == L1_BRICKTYPE_191010) ||
495             (bricktype == L1_BRICKTYPE_1932))
496                         sprintf(address, "%s^%d", address, geo_slot(geoid));
497 }
498
499 /*
500  * sn_pci_fixup_bus() - Perform SN specific setup of software structs
501  *                      (pcibus_bussoft, pcidev_info) and hardware
502  *                      registers, for the specified bus and devices under it.
503  */
504 void __devinit
505 sn_pci_fixup_bus(struct pci_bus *bus)
506 {
507
508         if (sn_acpi_base_support())
509                 sn_acpi_bus_fixup(bus);
510         else
511                 sn_bus_fixup(bus);
512 }
513
514 /*
515  * sn_io_early_init - Perform early IO (and some non-IO) initialization.
516  *                    In particular, setup the sn_pci_provider[] array.
517  *                    This needs to be done prior to any bus scanning
518  *                    (acpi_scan_init()) in the ACPI case, as the SN
519  *                    bus fixup code will reference the array.
520  */
521 static int __init
522 sn_io_early_init(void)
523 {
524         int i;
525
526         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
527                 return 0;
528
529         /*
530          * prime sn_pci_provider[].  Individial provider init routines will
531          * override their respective default entries.
532          */
533
534         for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
535                 sn_pci_provider[i] = &sn_pci_default_provider;
536
537         pcibr_init_provider();
538         tioca_init_provider();
539         tioce_init_provider();
540
541         /*
542          * This is needed to avoid bounce limit checks in the blk layer
543          */
544         ia64_max_iommu_merge_mask = ~PAGE_MASK;
545
546         sn_irq_lh_init();
547         INIT_LIST_HEAD(&sn_sysdata_list);
548         sn_init_cpei_timer();
549
550 #ifdef CONFIG_PROC_FS
551         register_sn_procfs();
552 #endif
553
554         printk(KERN_INFO "ACPI  DSDT OEM Rev 0x%x\n",
555                acpi_gbl_DSDT->oem_revision);
556         if (sn_acpi_base_support())
557                 sn_io_acpi_init();
558         else
559                 sn_io_init();
560         return 0;
561 }
562
563 arch_initcall(sn_io_early_init);
564
565 /*
566  * sn_io_late_init() - Perform any final platform specific IO initialization.
567  */
568
569 int __init
570 sn_io_late_init(void)
571 {
572         struct pci_bus *bus;
573         struct pcibus_bussoft *bussoft;
574         cnodeid_t cnode;
575         nasid_t nasid;
576         cnodeid_t near_cnode;
577
578         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
579                 return 0;
580
581         /*
582          * Setup closest node in pci_controller->node for
583          * PIC, TIOCP, TIOCE (TIOCA does it during bus fixup using
584          * info from the PROM).
585          */
586         bus = NULL;
587         while ((bus = pci_find_next_bus(bus)) != NULL) {
588                 bussoft = SN_PCIBUS_BUSSOFT(bus);
589                 nasid = NASID_GET(bussoft->bs_base);
590                 cnode = nasid_to_cnodeid(nasid);
591                 if ((bussoft->bs_asic_type == PCIIO_ASIC_TYPE_TIOCP) ||
592                     (bussoft->bs_asic_type == PCIIO_ASIC_TYPE_TIOCE)) {
593                         /* TIO PCI Bridge: find nearest node with CPUs */
594                         int e = sn_hwperf_get_nearest_node(cnode, NULL,
595                                                            &near_cnode);
596                         if (e < 0) {
597                                 near_cnode = (cnodeid_t)-1; /* use any node */
598                                 printk(KERN_WARNING "pcibr_bus_fixup: failed "
599                                        "to find near node with CPUs to TIO "
600                                        "node %d, err=%d\n", cnode, e);
601                         }
602                         PCI_CONTROLLER(bus)->node = near_cnode;
603                 } else if (bussoft->bs_asic_type == PCIIO_ASIC_TYPE_PIC) {
604                         PCI_CONTROLLER(bus)->node = cnode;
605                 }
606         }
607
608         sn_ioif_inited = 1;     /* SN I/O infrastructure now initialized */
609
610         return 0;
611 }
612
613 fs_initcall(sn_io_late_init);
614
615 EXPORT_SYMBOL(sn_pci_fixup_slot);
616 EXPORT_SYMBOL(sn_pci_unfixup_slot);
617 EXPORT_SYMBOL(sn_bus_store_sysdata);
618 EXPORT_SYMBOL(sn_bus_free_sysdata);
619 EXPORT_SYMBOL(sn_generate_path);
620