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