Merge branches 'misc', 'eeepc-laptop' and 'bugzilla-14445' into release
[safe/jmp/linux-2.6] / 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 #include <asm/sn/acpi.h>
29
30 extern void sn_init_cpei_timer(void);
31 extern void register_sn_procfs(void);
32 extern void sn_io_acpi_init(void);
33 extern void sn_io_init(void);
34
35
36 static struct list_head sn_sysdata_list;
37
38 /* sysdata list struct */
39 struct sysdata_el {
40         struct list_head entry;
41         void *sysdata;
42 };
43
44 int sn_ioif_inited;             /* SN I/O infrastructure initialized? */
45
46 int sn_acpi_rev;                /* SN ACPI revision */
47 EXPORT_SYMBOL_GPL(sn_acpi_rev);
48
49 struct sn_pcibus_provider *sn_pci_provider[PCIIO_ASIC_MAX_TYPES];       /* indexed by asic type */
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, int type)
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, widget, and device.
82  * This list is needed to implement the WAR - Flush DMA data on PIO Reads.
83  */
84 static inline u64
85 sal_get_device_dmaflush_list(u64 nasid, u64 widget_num, u64 device_num,
86                              u64 address)
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_DEVICE_DMAFLUSH_LIST,
94                         (u64) nasid, (u64) widget_num,
95                         (u64) device_num, (u64) address, 0, 0, 0);
96         return ret_stuff.status;
97 }
98
99 /*
100  * sn_pcidev_info_get() - Retrieve the pcidev_info struct for the specified
101  *                        device.
102  */
103 inline struct pcidev_info *
104 sn_pcidev_info_get(struct pci_dev *dev)
105 {
106         struct pcidev_info *pcidev;
107
108         list_for_each_entry(pcidev,
109                             &(SN_PLATFORM_DATA(dev)->pcidev_info), pdi_list) {
110                 if (pcidev->pdi_linux_pcidev == dev)
111                         return pcidev;
112         }
113         return NULL;
114 }
115
116 /* Older PROM flush WAR
117  *
118  * 01/16/06 -- This war will be in place until a new official PROM is released.
119  * Additionally note that the struct sn_flush_device_war also has to be
120  * removed from arch/ia64/sn/include/xtalk/hubdev.h
121  */
122
123 static s64 sn_device_fixup_war(u64 nasid, u64 widget, int device,
124                                struct sn_flush_device_common *common)
125 {
126         struct sn_flush_device_war *war_list;
127         struct sn_flush_device_war *dev_entry;
128         struct ia64_sal_retval isrv = {0,0,0,0};
129
130         printk_once(KERN_WARNING
131                 "PROM version < 4.50 -- implementing old PROM flush WAR\n");
132
133         war_list = kzalloc(DEV_PER_WIDGET * sizeof(*war_list), GFP_KERNEL);
134         BUG_ON(!war_list);
135
136         SAL_CALL_NOLOCK(isrv, SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
137                         nasid, widget, __pa(war_list), 0, 0, 0 ,0);
138         if (isrv.status)
139                 panic("sn_device_fixup_war failed: %s\n",
140                       ia64_sal_strerror(isrv.status));
141
142         dev_entry = war_list + device;
143         memcpy(common,dev_entry, sizeof(*common));
144         kfree(war_list);
145
146         return isrv.status;
147 }
148
149 /*
150  * sn_common_hubdev_init() - This routine is called to initialize the HUB data
151  *                           structure for each node in the system.
152  */
153 void __init
154 sn_common_hubdev_init(struct hubdev_info *hubdev)
155 {
156
157         struct sn_flush_device_kernel *sn_flush_device_kernel;
158         struct sn_flush_device_kernel *dev_entry;
159         s64 status;
160         int widget, device, size;
161
162         /* Attach the error interrupt handlers */
163         if (hubdev->hdi_nasid & 1)      /* If TIO */
164                 ice_error_init(hubdev);
165         else
166                 hub_error_init(hubdev);
167
168         for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
169                 hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
170
171         if (!hubdev->hdi_flush_nasid_list.widget_p)
172                 return;
173
174         size = (HUB_WIDGET_ID_MAX + 1) *
175                 sizeof(struct sn_flush_device_kernel *);
176         hubdev->hdi_flush_nasid_list.widget_p =
177                 kzalloc(size, GFP_KERNEL);
178         BUG_ON(!hubdev->hdi_flush_nasid_list.widget_p);
179
180         for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
181                 size = DEV_PER_WIDGET *
182                         sizeof(struct sn_flush_device_kernel);
183                 sn_flush_device_kernel = kzalloc(size, GFP_KERNEL);
184                 BUG_ON(!sn_flush_device_kernel);
185
186                 dev_entry = sn_flush_device_kernel;
187                 for (device = 0; device < DEV_PER_WIDGET;
188                      device++, dev_entry++) {
189                         size = sizeof(struct sn_flush_device_common);
190                         dev_entry->common = kzalloc(size, GFP_KERNEL);
191                         BUG_ON(!dev_entry->common);
192                         if (sn_prom_feature_available(PRF_DEVICE_FLUSH_LIST))
193                                 status = sal_get_device_dmaflush_list(
194                                              hubdev->hdi_nasid, widget, device,
195                                              (u64)(dev_entry->common));
196                         else
197                                 status = sn_device_fixup_war(hubdev->hdi_nasid,
198                                                              widget, device,
199                                                              dev_entry->common);
200                         if (status != SALRET_OK)
201                                 panic("SAL call failed: %s\n",
202                                       ia64_sal_strerror(status));
203
204                         spin_lock_init(&dev_entry->sfdl_flush_lock);
205                 }
206
207                 if (sn_flush_device_kernel)
208                         hubdev->hdi_flush_nasid_list.widget_p[widget] =
209                                                          sn_flush_device_kernel;
210         }
211 }
212
213 void sn_pci_unfixup_slot(struct pci_dev *dev)
214 {
215         struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
216
217         sn_irq_unfixup(dev);
218         pci_dev_put(host_pci_dev);
219         pci_dev_put(dev);
220 }
221
222 /*
223  * sn_pci_fixup_slot()
224  */
225 void sn_pci_fixup_slot(struct pci_dev *dev, struct pcidev_info *pcidev_info,
226                        struct sn_irq_info *sn_irq_info)
227 {
228         int segment = pci_domain_nr(dev->bus);
229         struct pcibus_bussoft *bs;
230         struct pci_bus *host_pci_bus;
231         struct pci_dev *host_pci_dev;
232         unsigned int bus_no, devfn;
233
234         pci_dev_get(dev); /* for the sysdata pointer */
235
236         /* Add pcidev_info to list in pci_controller.platform_data */
237         list_add_tail(&pcidev_info->pdi_list,
238                       &(SN_PLATFORM_DATA(dev->bus)->pcidev_info));
239         /*
240          * Using the PROMs values for the PCI host bus, get the Linux
241          * PCI host_pci_dev struct and set up host bus linkages
242          */
243
244         bus_no = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
245         devfn = pcidev_info->pdi_slot_host_handle & 0xffffffff;
246         host_pci_bus = pci_find_bus(segment, bus_no);
247         host_pci_dev = pci_get_slot(host_pci_bus, devfn);
248
249         pcidev_info->host_pci_dev = host_pci_dev;
250         pcidev_info->pdi_linux_pcidev = dev;
251         pcidev_info->pdi_host_pcidev_info = SN_PCIDEV_INFO(host_pci_dev);
252         bs = SN_PCIBUS_BUSSOFT(dev->bus);
253         pcidev_info->pdi_pcibus_info = bs;
254
255         if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
256                 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
257         } else {
258                 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
259         }
260
261         /* Only set up IRQ stuff if this device has a host bus context */
262         if (bs && sn_irq_info->irq_irq) {
263                 pcidev_info->pdi_sn_irq_info = sn_irq_info;
264                 dev->irq = pcidev_info->pdi_sn_irq_info->irq_irq;
265                 sn_irq_fixup(dev, sn_irq_info);
266         } else {
267                 pcidev_info->pdi_sn_irq_info = NULL;
268                 kfree(sn_irq_info);
269         }
270 }
271
272 /*
273  * sn_common_bus_fixup - Perform platform specific bus fixup.
274  *                       Execute the ASIC specific fixup routine
275  *                       for this bus.
276  */
277 void
278 sn_common_bus_fixup(struct pci_bus *bus,
279                     struct pcibus_bussoft *prom_bussoft_ptr)
280 {
281         int cnode;
282         struct pci_controller *controller;
283         struct hubdev_info *hubdev_info;
284         int nasid;
285         void *provider_soft;
286         struct sn_pcibus_provider *provider;
287         struct sn_platform_data *sn_platform_data;
288
289         controller = PCI_CONTROLLER(bus);
290         /*
291          * Per-provider fixup.  Copies the bus soft structure from prom
292          * to local area and links SN_PCIBUS_BUSSOFT().
293          */
294
295         if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES) {
296                 printk(KERN_WARNING "sn_common_bus_fixup: Unsupported asic type, %d",
297                        prom_bussoft_ptr->bs_asic_type);
298                 return;
299         }
300
301         if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
302                 return; /* no further fixup necessary */
303
304         provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
305         if (provider == NULL)
306                 panic("sn_common_bus_fixup: No provider registered for this asic type, %d",
307                       prom_bussoft_ptr->bs_asic_type);
308
309         if (provider->bus_fixup)
310                 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr,
311                                  controller);
312         else
313                 provider_soft = NULL;
314
315         /*
316          * Generic bus fixup goes here.  Don't reference prom_bussoft_ptr
317          * after this point.
318          */
319         controller->platform_data = kzalloc(sizeof(struct sn_platform_data),
320                                             GFP_KERNEL);
321         BUG_ON(controller->platform_data == NULL);
322         sn_platform_data =
323                         (struct sn_platform_data *) controller->platform_data;
324         sn_platform_data->provider_soft = provider_soft;
325         INIT_LIST_HEAD(&((struct sn_platform_data *)
326                          controller->platform_data)->pcidev_info);
327         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
328         cnode = nasid_to_cnodeid(nasid);
329         hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
330         SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
331             &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
332
333         /*
334          * If the node information we obtained during the fixup phase is
335          * invalid then set controller->node to -1 (undetermined)
336          */
337         if (controller->node >= num_online_nodes()) {
338                 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
339
340                 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u "
341                        "L_IO=%llx L_MEM=%llx BASE=%llx\n",
342                        b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
343                        b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
344                 printk(KERN_WARNING "on node %d but only %d nodes online."
345                        "Association set to undetermined.\n",
346                        controller->node, num_online_nodes());
347                 controller->node = -1;
348         }
349 }
350
351 void sn_bus_store_sysdata(struct pci_dev *dev)
352 {
353         struct sysdata_el *element;
354
355         element = kzalloc(sizeof(struct sysdata_el), GFP_KERNEL);
356         if (!element) {
357                 dev_dbg(&dev->dev, "%s: out of memory!\n", __func__);
358                 return;
359         }
360         element->sysdata = SN_PCIDEV_INFO(dev);
361         list_add(&element->entry, &sn_sysdata_list);
362 }
363
364 void sn_bus_free_sysdata(void)
365 {
366         struct sysdata_el *element;
367         struct list_head *list, *safe;
368
369         list_for_each_safe(list, safe, &sn_sysdata_list) {
370                 element = list_entry(list, struct sysdata_el, entry);
371                 list_del(&element->entry);
372                 list_del(&(((struct pcidev_info *)
373                              (element->sysdata))->pdi_list));
374                 kfree(element->sysdata);
375                 kfree(element);
376         }
377         return;
378 }
379
380 /*
381  * hubdev_init_node() - Creates the HUB data structure and link them to it's
382  *                      own NODE specific data area.
383  */
384 void __init hubdev_init_node(nodepda_t * npda, cnodeid_t node)
385 {
386         struct hubdev_info *hubdev_info;
387         int size;
388         pg_data_t *pg;
389
390         size = sizeof(struct hubdev_info);
391
392         if (node >= num_online_nodes()) /* Headless/memless IO nodes */
393                 pg = NODE_DATA(0);
394         else
395                 pg = NODE_DATA(node);
396
397         hubdev_info = (struct hubdev_info *)alloc_bootmem_node(pg, size);
398
399         npda->pdinfo = (void *)hubdev_info;
400 }
401
402 geoid_t
403 cnodeid_get_geoid(cnodeid_t cnode)
404 {
405         struct hubdev_info *hubdev;
406
407         hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
408         return hubdev->hdi_geoid;
409 }
410
411 void sn_generate_path(struct pci_bus *pci_bus, char *address)
412 {
413         nasid_t nasid;
414         cnodeid_t cnode;
415         geoid_t geoid;
416         moduleid_t moduleid;
417         u16 bricktype;
418
419         nasid = NASID_GET(SN_PCIBUS_BUSSOFT(pci_bus)->bs_base);
420         cnode = nasid_to_cnodeid(nasid);
421         geoid = cnodeid_get_geoid(cnode);
422         moduleid = geo_module(geoid);
423
424         sprintf(address, "module_%c%c%c%c%.2d",
425                 '0'+RACK_GET_CLASS(MODULE_GET_RACK(moduleid)),
426                 '0'+RACK_GET_GROUP(MODULE_GET_RACK(moduleid)),
427                 '0'+RACK_GET_NUM(MODULE_GET_RACK(moduleid)),
428                 MODULE_GET_BTCHAR(moduleid), MODULE_GET_BPOS(moduleid));
429
430         /* Tollhouse requires slot id to be displayed */
431         bricktype = MODULE_GET_BTYPE(moduleid);
432         if ((bricktype == L1_BRICKTYPE_191010) ||
433             (bricktype == L1_BRICKTYPE_1932))
434                         sprintf(address + strlen(address), "^%d",
435                                                 geo_slot(geoid));
436 }
437
438 void __devinit
439 sn_pci_fixup_bus(struct pci_bus *bus)
440 {
441
442         if (SN_ACPI_BASE_SUPPORT())
443                 sn_acpi_bus_fixup(bus);
444         else
445                 sn_bus_fixup(bus);
446 }
447
448 /*
449  * sn_io_early_init - Perform early IO (and some non-IO) initialization.
450  *                    In particular, setup the sn_pci_provider[] array.
451  *                    This needs to be done prior to any bus scanning
452  *                    (acpi_scan_init()) in the ACPI case, as the SN
453  *                    bus fixup code will reference the array.
454  */
455 static int __init
456 sn_io_early_init(void)
457 {
458         int i;
459
460         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
461                 return 0;
462
463         /* we set the acpi revision to that of the DSDT table OEM rev. */
464         {
465                 struct acpi_table_header *header = NULL;
466
467                 acpi_get_table(ACPI_SIG_DSDT, 1, &header);
468                 BUG_ON(header == NULL);
469                 sn_acpi_rev = header->oem_revision;
470         }
471
472         /*
473          * prime sn_pci_provider[].  Individual provider init routines will
474          * override their respective default entries.
475          */
476
477         for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
478                 sn_pci_provider[i] = &sn_pci_default_provider;
479
480         pcibr_init_provider();
481         tioca_init_provider();
482         tioce_init_provider();
483
484         /*
485          * This is needed to avoid bounce limit checks in the blk layer
486          */
487         ia64_max_iommu_merge_mask = ~PAGE_MASK;
488
489         sn_irq_lh_init();
490         INIT_LIST_HEAD(&sn_sysdata_list);
491         sn_init_cpei_timer();
492
493 #ifdef CONFIG_PROC_FS
494         register_sn_procfs();
495 #endif
496
497         {
498                 struct acpi_table_header *header;
499                 (void)acpi_get_table(ACPI_SIG_DSDT, 1, &header);
500                 printk(KERN_INFO "ACPI  DSDT OEM Rev 0x%x\n",
501                         header->oem_revision);
502         }
503         if (SN_ACPI_BASE_SUPPORT())
504                 sn_io_acpi_init();
505         else
506                 sn_io_init();
507         return 0;
508 }
509
510 arch_initcall(sn_io_early_init);
511
512 /*
513  * sn_io_late_init() - Perform any final platform specific IO initialization.
514  */
515
516 int __init
517 sn_io_late_init(void)
518 {
519         struct pci_bus *bus;
520         struct pcibus_bussoft *bussoft;
521         cnodeid_t cnode;
522         nasid_t nasid;
523         cnodeid_t near_cnode;
524
525         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
526                 return 0;
527
528         /*
529          * Setup closest node in pci_controller->node for
530          * PIC, TIOCP, TIOCE (TIOCA does it during bus fixup using
531          * info from the PROM).
532          */
533         bus = NULL;
534         while ((bus = pci_find_next_bus(bus)) != NULL) {
535                 bussoft = SN_PCIBUS_BUSSOFT(bus);
536                 nasid = NASID_GET(bussoft->bs_base);
537                 cnode = nasid_to_cnodeid(nasid);
538                 if ((bussoft->bs_asic_type == PCIIO_ASIC_TYPE_TIOCP) ||
539                     (bussoft->bs_asic_type == PCIIO_ASIC_TYPE_TIOCE) ||
540                     (bussoft->bs_asic_type == PCIIO_ASIC_TYPE_PIC)) {
541                         /* PCI Bridge: find nearest node with CPUs */
542                         int e = sn_hwperf_get_nearest_node(cnode, NULL,
543                                                            &near_cnode);
544                         if (e < 0) {
545                                 near_cnode = (cnodeid_t)-1; /* use any node */
546                                 printk(KERN_WARNING "sn_io_late_init: failed "
547                                        "to find near node with CPUs for "
548                                        "node %d, err=%d\n", cnode, e);
549                         }
550                         PCI_CONTROLLER(bus)->node = near_cnode;
551                 }
552         }
553
554         sn_ioif_inited = 1;     /* SN I/O infrastructure now initialized */
555
556         return 0;
557 }
558
559 fs_initcall(sn_io_late_init);
560
561 EXPORT_SYMBOL(sn_pci_unfixup_slot);
562 EXPORT_SYMBOL(sn_bus_store_sysdata);
563 EXPORT_SYMBOL(sn_bus_free_sysdata);
564 EXPORT_SYMBOL(sn_generate_path);
565