]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/iseries/pci.c
[POWERPC] iSeries: Merge vpdinfo.c into pci.c
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / iseries / pci.c
1 /*
2  * Copyright (C) 2001 Allan Trautman, IBM Corporation
3  * Copyright (C) 2005,2007  Stephen Rothwell, IBM Corp
4  *
5  * iSeries specific routines for PCI.
6  *
7  * Based on code from pci.c and iSeries_pci.c 32bit
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/string.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29
30 #include <asm/types.h>
31 #include <asm/io.h>
32 #include <asm/irq.h>
33 #include <asm/prom.h>
34 #include <asm/machdep.h>
35 #include <asm/pci-bridge.h>
36 #include <asm/iommu.h>
37 #include <asm/abs_addr.h>
38 #include <asm/firmware.h>
39
40 #include <asm/iseries/hv_types.h>
41 #include <asm/iseries/hv_call_xm.h>
42 #include <asm/iseries/mf.h>
43 #include <asm/iseries/iommu.h>
44
45 #include <asm/ppc-pci.h>
46
47 #include "irq.h"
48 #include "pci.h"
49 #include "call_pci.h"
50
51 #define PCI_RETRY_MAX   3
52 static int limit_pci_retries = 1;       /* Set Retry Error on. */
53
54 /*
55  * Table defines
56  * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
57  */
58 #define IOMM_TABLE_MAX_ENTRIES  1024
59 #define IOMM_TABLE_ENTRY_SIZE   0x0000000000400000UL
60 #define BASE_IO_MEMORY          0xE000000000000000UL
61
62 static unsigned long max_io_memory = BASE_IO_MEMORY;
63 static long current_iomm_table_entry;
64
65 /*
66  * Lookup Tables.
67  */
68 static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
69 static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
70
71 static const char pci_io_text[] = "iSeries PCI I/O";
72 static DEFINE_SPINLOCK(iomm_table_lock);
73
74 /*
75  * Generate a Direct Select Address for the Hypervisor
76  */
77 static inline u64 iseries_ds_addr(struct device_node *node)
78 {
79         struct pci_dn *pdn = PCI_DN(node);
80
81         return ((u64)pdn->busno << 48) + ((u64)pdn->bussubno << 40)
82                         + ((u64)0x10 << 32);
83 }
84
85 /*
86  * Size of Bus VPD data
87  */
88 #define BUS_VPDSIZE      1024
89
90 /*
91  * Bus Vpd Tags
92  */
93 #define VPD_END_OF_AREA         0x79
94 #define VPD_ID_STRING           0x82
95 #define VPD_VENDOR_AREA         0x84
96
97 /*
98  * Mfg Area Tags
99  */
100 #define VPD_FRU_FRAME_ID        0x4649  /* "FI" */
101 #define VPD_SLOT_MAP_FORMAT     0x4D46  /* "MF" */
102 #define VPD_SLOT_MAP            0x534D  /* "SM" */
103
104 /*
105  * Structures of the areas
106  */
107 struct mfg_vpd_area {
108         u16     tag;
109         u8      length;
110         u8      data1;
111         u8      data2;
112 };
113 #define MFG_ENTRY_SIZE   3
114
115 struct slot_map {
116         u8      agent;
117         u8      secondary_agent;
118         u8      phb;
119         char    card_location[3];
120         char    parms[8];
121         char    reserved[2];
122 };
123 #define SLOT_ENTRY_SIZE   16
124
125 /*
126  * Parse the Slot Area
127  */
128 static void __init iseries_parse_slot_area(struct slot_map *map, int len,
129                 HvAgentId agent, u8 *phb, char card[4])
130 {
131         /*
132          * Parse Slot label until we find the one requested
133          */
134         while (len > 0) {
135                 if (map->agent == agent) {
136                         /*
137                          * If Phb wasn't found, grab the entry first one found.
138                          */
139                         if (*phb == 0xff)
140                                 *phb = map->phb;
141                         /* Found it, extract the data. */
142                         if (map->phb == *phb) {
143                                 memcpy(card, &map->card_location, 3);
144                                 card[3]  = 0;
145                                 break;
146                         }
147                 }
148                 /* Point to the next Slot */
149                 map = (struct slot_map *)((char *)map + SLOT_ENTRY_SIZE);
150                 len -= SLOT_ENTRY_SIZE;
151         }
152 }
153
154 /*
155  * Parse the Mfg Area
156  */
157 static void __init iseries_parse_mfg_area(struct mfg_vpd_area *area, int len,
158                 HvAgentId agent, u8 *phb, u8 *frame, char card[4])
159 {
160         u16 slot_map_fmt = 0;
161
162         /* Parse Mfg Data */
163         while (len > 0) {
164                 int mfg_tag_len = area->length;
165                 /* Frame ID         (FI 4649020310 ) */
166                 if (area->tag == VPD_FRU_FRAME_ID)
167                         *frame = area->data1;
168                 /* Slot Map Format  (MF 4D46020004 ) */
169                 else if (area->tag == VPD_SLOT_MAP_FORMAT)
170                         slot_map_fmt = (area->data1 * 256)
171                                 + area->data2;
172                 /* Slot Map         (SM 534D90 */
173                 else if (area->tag == VPD_SLOT_MAP) {
174                         struct slot_map *slot_map;
175
176                         if (slot_map_fmt == 0x1004)
177                                 slot_map = (struct slot_map *)((char *)area
178                                                 + MFG_ENTRY_SIZE + 1);
179                         else
180                                 slot_map = (struct slot_map *)((char *)area
181                                                 + MFG_ENTRY_SIZE);
182                         iseries_parse_slot_area(slot_map, mfg_tag_len,
183                                         agent, phb, card);
184                 }
185                 /*
186                  * Point to the next Mfg Area
187                  * Use defined size, sizeof give wrong answer
188                  */
189                 area = (struct mfg_vpd_area *)((char *)area + mfg_tag_len
190                                 + MFG_ENTRY_SIZE);
191                 len -= (mfg_tag_len + MFG_ENTRY_SIZE);
192         }
193 }
194
195 /*
196  * Look for "BUS".. Data is not Null terminated.
197  * PHBID of 0xFF indicates PHB was not found in VPD Data.
198  */
199 static u8 __init iseries_parse_phbid(u8 *area, int len)
200 {
201         while (len > 0) {
202                 if ((*area == 'B') && (*(area + 1) == 'U')
203                                 && (*(area + 2) == 'S')) {
204                         area += 3;
205                         while (*area == ' ')
206                                 area++;
207                         return *area & 0x0F;
208                 }
209                 area++;
210                 len--;
211         }
212         return 0xff;
213 }
214
215 /*
216  * Parse out the VPD Areas
217  */
218 static void __init iseries_parse_vpd(u8 *data, int data_len,
219                 HvAgentId agent, u8 *frame, char card[4])
220 {
221         u8 phb = 0xff;
222
223         while (data_len > 0) {
224                 int len;
225                 u8 tag = *data;
226
227                 if (tag == VPD_END_OF_AREA)
228                         break;
229                 len = *(data + 1) + (*(data + 2) * 256);
230                 data += 3;
231                 data_len -= 3;
232                 if (tag == VPD_ID_STRING)
233                         phb = iseries_parse_phbid(data, len);
234                 else if (tag == VPD_VENDOR_AREA)
235                         iseries_parse_mfg_area((struct mfg_vpd_area *)data, len,
236                                         agent, &phb, frame, card);
237                 /* Point to next Area. */
238                 data += len;
239                 data_len -= len;
240         }
241 }
242
243 static int __init iseries_get_location_code(u16 bus, HvAgentId agent,
244                 u8 *frame, char card[4])
245 {
246         int status = 0;
247         int bus_vpd_len = 0;
248         u8 *bus_vpd = kmalloc(BUS_VPDSIZE, GFP_KERNEL);
249
250         if (bus_vpd == NULL) {
251                 printk("PCI: Bus VPD Buffer allocation failure.\n");
252                 return 0;
253         }
254         bus_vpd_len = HvCallPci_getBusVpd(bus, iseries_hv_addr(bus_vpd),
255                                         BUS_VPDSIZE);
256         if (bus_vpd_len == 0) {
257                 printk("PCI: Bus VPD Buffer zero length.\n");
258                 goto out_free;
259         }
260         /* printk("PCI: bus_vpd: %p, %d\n",bus_vpd, bus_vpd_len); */
261         /* Make sure this is what I think it is */
262         if (*bus_vpd != VPD_ID_STRING) {
263                 printk("PCI: Bus VPD Buffer missing starting tag.\n");
264                 goto out_free;
265         }
266         iseries_parse_vpd(bus_vpd, bus_vpd_len, agent, frame, card);
267         status = 1;
268 out_free:
269         kfree(bus_vpd);
270         return status;
271 }
272
273 /*
274  * Prints the device information.
275  * - Pass in pci_dev* pointer to the device.
276  * - Pass in the device count
277  *
278  * Format:
279  * PCI: Bus  0, Device 26, Vendor 0x12AE  Frame  1, Card  C10  Ethernet
280  * controller
281  */
282 static void __init iseries_device_information(struct pci_dev *pdev, int count,
283                 u16 bus, HvSubBusNumber subbus)
284 {
285         u8 frame = 0;
286         char card[4];
287         HvAgentId agent;
288
289         agent = ISERIES_PCI_AGENTID(ISERIES_GET_DEVICE_FROM_SUBBUS(subbus),
290                         ISERIES_GET_FUNCTION_FROM_SUBBUS(subbus));
291
292         if (iseries_get_location_code(bus, agent, &frame, card)) {
293                 printk("%d. PCI: Bus%3d, Device%3d, Vendor %04X Frame%3d, "
294                         "Card %4s  0x%04X\n", count, bus,
295                         PCI_SLOT(pdev->devfn), pdev->vendor, frame,
296                         card, (int)(pdev->class >> 8));
297         }
298 }
299
300 /*
301  * iomm_table_allocate_entry
302  *
303  * Adds pci_dev entry in address translation table
304  *
305  * - Allocates the number of entries required in table base on BAR
306  *   size.
307  * - Allocates starting at BASE_IO_MEMORY and increases.
308  * - The size is round up to be a multiple of entry size.
309  * - CurrentIndex is incremented to keep track of the last entry.
310  * - Builds the resource entry for allocated BARs.
311  */
312 static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
313 {
314         struct resource *bar_res = &dev->resource[bar_num];
315         long bar_size = pci_resource_len(dev, bar_num);
316
317         /*
318          * No space to allocate, quick exit, skip Allocation.
319          */
320         if (bar_size == 0)
321                 return;
322         /*
323          * Set Resource values.
324          */
325         spin_lock(&iomm_table_lock);
326         bar_res->name = pci_io_text;
327         bar_res->start = BASE_IO_MEMORY +
328                 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
329         bar_res->end = bar_res->start + bar_size - 1;
330         /*
331          * Allocate the number of table entries needed for BAR.
332          */
333         while (bar_size > 0 ) {
334                 iomm_table[current_iomm_table_entry] = dev->sysdata;
335                 iobar_table[current_iomm_table_entry] = bar_num;
336                 bar_size -= IOMM_TABLE_ENTRY_SIZE;
337                 ++current_iomm_table_entry;
338         }
339         max_io_memory = BASE_IO_MEMORY +
340                 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
341         spin_unlock(&iomm_table_lock);
342 }
343
344 /*
345  * allocate_device_bars
346  *
347  * - Allocates ALL pci_dev BAR's and updates the resources with the
348  *   BAR value.  BARS with zero length will have the resources
349  *   The HvCallPci_getBarParms is used to get the size of the BAR
350  *   space.  It calls iomm_table_allocate_entry to allocate
351  *   each entry.
352  * - Loops through The Bar resources(0 - 5) including the ROM
353  *   is resource(6).
354  */
355 static void __init allocate_device_bars(struct pci_dev *dev)
356 {
357         int bar_num;
358
359         for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
360                 iomm_table_allocate_entry(dev, bar_num);
361 }
362
363 /*
364  * Log error information to system console.
365  * Filter out the device not there errors.
366  * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
367  * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
368  * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
369  */
370 static void pci_log_error(char *error, int bus, int subbus,
371                 int agent, int hv_res)
372 {
373         if (hv_res == 0x0302)
374                 return;
375         printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
376                error, bus, subbus, agent, hv_res);
377 }
378
379 /*
380  * Look down the chain to find the matching Device Device
381  */
382 static struct device_node *find_device_node(int bus, int devfn)
383 {
384         struct device_node *node;
385
386         for (node = NULL; (node = of_find_all_nodes(node)); ) {
387                 struct pci_dn *pdn = PCI_DN(node);
388
389                 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
390                         return node;
391         }
392         return NULL;
393 }
394
395 /*
396  * iSeries_pci_final_fixup(void)
397  */
398 void __init iSeries_pci_final_fixup(void)
399 {
400         struct pci_dev *pdev = NULL;
401         struct device_node *node;
402         int num_dev = 0;
403
404         /* Fix up at the device node and pci_dev relationship */
405         mf_display_src(0xC9000100);
406
407         printk("pcibios_final_fixup\n");
408         for_each_pci_dev(pdev) {
409                 const u32 *agent;
410                 const u32 *sub_bus;
411                 unsigned char bus = pdev->bus->number;
412
413                 node = find_device_node(bus, pdev->devfn);
414                 printk("pci dev %p (%x.%x), node %p\n", pdev, bus,
415                         pdev->devfn, node);
416                 if (!node) {
417                         printk("PCI: Device Tree not found for 0x%016lX\n",
418                                         (unsigned long)pdev);
419                         continue;
420                 }
421
422                 agent = of_get_property(node, "linux,agent-id", NULL);
423                 sub_bus = of_get_property(node, "linux,subbus", NULL);
424                 if (agent && sub_bus) {
425                         u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
426                         int err;
427
428                         err = HvCallXm_connectBusUnit(bus, *sub_bus,
429                                         *agent, irq);
430                         if (err)
431                                 pci_log_error("Connect Bus Unit",
432                                         bus, *sub_bus, *agent, err);
433                         else {
434                                 err = HvCallPci_configStore8(bus, *sub_bus,
435                                         *agent, PCI_INTERRUPT_LINE, irq);
436                                 if (err)
437                                         pci_log_error("PciCfgStore Irq Failed!",
438                                                 bus, *sub_bus, *agent, err);
439                                 else
440                                         pdev->irq = irq;
441                         }
442                 }
443
444                 num_dev++;
445                 pdev->sysdata = node;
446                 PCI_DN(node)->pcidev = pdev;
447                 allocate_device_bars(pdev);
448                 iseries_device_information(pdev, num_dev, bus, *sub_bus);
449                 iommu_devnode_init_iSeries(pdev, node);
450         }
451         iSeries_activate_IRQs();
452         mf_display_src(0xC9000200);
453 }
454
455 /*
456  * Config space read and write functions.
457  * For now at least, we look for the device node for the bus and devfn
458  * that we are asked to access.  It may be possible to translate the devfn
459  * to a subbus and deviceid more directly.
460  */
461 static u64 hv_cfg_read_func[4]  = {
462         HvCallPciConfigLoad8, HvCallPciConfigLoad16,
463         HvCallPciConfigLoad32, HvCallPciConfigLoad32
464 };
465
466 static u64 hv_cfg_write_func[4] = {
467         HvCallPciConfigStore8, HvCallPciConfigStore16,
468         HvCallPciConfigStore32, HvCallPciConfigStore32
469 };
470
471 /*
472  * Read PCI config space
473  */
474 static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
475                 int offset, int size, u32 *val)
476 {
477         struct device_node *node = find_device_node(bus->number, devfn);
478         u64 fn;
479         struct HvCallPci_LoadReturn ret;
480
481         if (node == NULL)
482                 return PCIBIOS_DEVICE_NOT_FOUND;
483         if (offset > 255) {
484                 *val = ~0;
485                 return PCIBIOS_BAD_REGISTER_NUMBER;
486         }
487
488         fn = hv_cfg_read_func[(size - 1) & 3];
489         HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
490
491         if (ret.rc != 0) {
492                 *val = ~0;
493                 return PCIBIOS_DEVICE_NOT_FOUND;        /* or something */
494         }
495
496         *val = ret.value;
497         return 0;
498 }
499
500 /*
501  * Write PCI config space
502  */
503
504 static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
505                 int offset, int size, u32 val)
506 {
507         struct device_node *node = find_device_node(bus->number, devfn);
508         u64 fn;
509         u64 ret;
510
511         if (node == NULL)
512                 return PCIBIOS_DEVICE_NOT_FOUND;
513         if (offset > 255)
514                 return PCIBIOS_BAD_REGISTER_NUMBER;
515
516         fn = hv_cfg_write_func[(size - 1) & 3];
517         ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
518
519         if (ret != 0)
520                 return PCIBIOS_DEVICE_NOT_FOUND;
521
522         return 0;
523 }
524
525 static struct pci_ops iSeries_pci_ops = {
526         .read = iSeries_pci_read_config,
527         .write = iSeries_pci_write_config
528 };
529
530 /*
531  * Check Return Code
532  * -> On Failure, print and log information.
533  *    Increment Retry Count, if exceeds max, panic partition.
534  *
535  * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
536  * PCI: Device 23.90 ReadL Retry( 1)
537  * PCI: Device 23.90 ReadL Retry Successful(1)
538  */
539 static int check_return_code(char *type, struct device_node *dn,
540                 int *retry, u64 ret)
541 {
542         if (ret != 0)  {
543                 struct pci_dn *pdn = PCI_DN(dn);
544
545                 (*retry)++;
546                 printk("PCI: %s: Device 0x%04X:%02X  I/O Error(%2d): 0x%04X\n",
547                                 type, pdn->busno, pdn->devfn,
548                                 *retry, (int)ret);
549                 /*
550                  * Bump the retry and check for retry count exceeded.
551                  * If, Exceeded, panic the system.
552                  */
553                 if (((*retry) > PCI_RETRY_MAX) &&
554                                 (limit_pci_retries > 0)) {
555                         mf_display_src(0xB6000103);
556                         panic_timeout = 0;
557                         panic("PCI: Hardware I/O Error, SRC B6000103, "
558                                         "Automatic Reboot Disabled.\n");
559                 }
560                 return -1;      /* Retry Try */
561         }
562         return 0;
563 }
564
565 /*
566  * Translate the I/O Address into a device node, bar, and bar offset.
567  * Note: Make sure the passed variable end up on the stack to avoid
568  * the exposure of being device global.
569  */
570 static inline struct device_node *xlate_iomm_address(
571                 const volatile void __iomem *addr,
572                 u64 *dsaptr, u64 *bar_offset, const char *func)
573 {
574         unsigned long orig_addr;
575         unsigned long base_addr;
576         unsigned long ind;
577         struct device_node *dn;
578
579         orig_addr = (unsigned long __force)addr;
580         if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
581                 static unsigned long last_jiffies;
582                 static int num_printed;
583
584                 if ((jiffies - last_jiffies) > 60 * HZ) {
585                         last_jiffies = jiffies;
586                         num_printed = 0;
587                 }
588                 if (num_printed++ < 10)
589                         printk(KERN_ERR
590                                 "iSeries_%s: invalid access at IO address %p\n",
591                                 func, addr);
592                 return NULL;
593         }
594         base_addr = orig_addr - BASE_IO_MEMORY;
595         ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
596         dn = iomm_table[ind];
597
598         if (dn != NULL) {
599                 int barnum = iobar_table[ind];
600                 *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
601                 *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
602         } else
603                 panic("PCI: Invalid PCI IO address detected!\n");
604         return dn;
605 }
606
607 /*
608  * Read MM I/O Instructions for the iSeries
609  * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
610  * else, data is returned in Big Endian format.
611  */
612 static u8 iseries_readb(const volatile void __iomem *addr)
613 {
614         u64 bar_offset;
615         u64 dsa;
616         int retry = 0;
617         struct HvCallPci_LoadReturn ret;
618         struct device_node *dn =
619                 xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
620
621         if (dn == NULL)
622                 return 0xff;
623         do {
624                 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
625         } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
626
627         return ret.value;
628 }
629
630 static u16 iseries_readw_be(const volatile void __iomem *addr)
631 {
632         u64 bar_offset;
633         u64 dsa;
634         int retry = 0;
635         struct HvCallPci_LoadReturn ret;
636         struct device_node *dn =
637                 xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
638
639         if (dn == NULL)
640                 return 0xffff;
641         do {
642                 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
643                                 bar_offset, 0);
644         } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
645
646         return ret.value;
647 }
648
649 static u32 iseries_readl_be(const volatile void __iomem *addr)
650 {
651         u64 bar_offset;
652         u64 dsa;
653         int retry = 0;
654         struct HvCallPci_LoadReturn ret;
655         struct device_node *dn =
656                 xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
657
658         if (dn == NULL)
659                 return 0xffffffff;
660         do {
661                 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
662                                 bar_offset, 0);
663         } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
664
665         return ret.value;
666 }
667
668 /*
669  * Write MM I/O Instructions for the iSeries
670  *
671  */
672 static void iseries_writeb(u8 data, volatile void __iomem *addr)
673 {
674         u64 bar_offset;
675         u64 dsa;
676         int retry = 0;
677         u64 rc;
678         struct device_node *dn =
679                 xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
680
681         if (dn == NULL)
682                 return;
683         do {
684                 rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
685         } while (check_return_code("WWB", dn, &retry, rc) != 0);
686 }
687
688 static void iseries_writew_be(u16 data, volatile void __iomem *addr)
689 {
690         u64 bar_offset;
691         u64 dsa;
692         int retry = 0;
693         u64 rc;
694         struct device_node *dn =
695                 xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
696
697         if (dn == NULL)
698                 return;
699         do {
700                 rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
701         } while (check_return_code("WWW", dn, &retry, rc) != 0);
702 }
703
704 static void iseries_writel_be(u32 data, volatile void __iomem *addr)
705 {
706         u64 bar_offset;
707         u64 dsa;
708         int retry = 0;
709         u64 rc;
710         struct device_node *dn =
711                 xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
712
713         if (dn == NULL)
714                 return;
715         do {
716                 rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
717         } while (check_return_code("WWL", dn, &retry, rc) != 0);
718 }
719
720 static u16 iseries_readw(const volatile void __iomem *addr)
721 {
722         return le16_to_cpu(iseries_readw_be(addr));
723 }
724
725 static u32 iseries_readl(const volatile void __iomem *addr)
726 {
727         return le32_to_cpu(iseries_readl_be(addr));
728 }
729
730 static void iseries_writew(u16 data, volatile void __iomem *addr)
731 {
732         iseries_writew_be(cpu_to_le16(data), addr);
733 }
734
735 static void iseries_writel(u32 data, volatile void __iomem *addr)
736 {
737         iseries_writel(cpu_to_le32(data), addr);
738 }
739
740 static void iseries_readsb(const volatile void __iomem *addr, void *buf,
741                            unsigned long count)
742 {
743         u8 *dst = buf;
744         while(count-- > 0)
745                 *(dst++) = iseries_readb(addr);
746 }
747
748 static void iseries_readsw(const volatile void __iomem *addr, void *buf,
749                            unsigned long count)
750 {
751         u16 *dst = buf;
752         while(count-- > 0)
753                 *(dst++) = iseries_readw_be(addr);
754 }
755
756 static void iseries_readsl(const volatile void __iomem *addr, void *buf,
757                            unsigned long count)
758 {
759         u32 *dst = buf;
760         while(count-- > 0)
761                 *(dst++) = iseries_readl_be(addr);
762 }
763
764 static void iseries_writesb(volatile void __iomem *addr, const void *buf,
765                             unsigned long count)
766 {
767         const u8 *src = buf;
768         while(count-- > 0)
769                 iseries_writeb(*(src++), addr);
770 }
771
772 static void iseries_writesw(volatile void __iomem *addr, const void *buf,
773                             unsigned long count)
774 {
775         const u16 *src = buf;
776         while(count-- > 0)
777                 iseries_writew_be(*(src++), addr);
778 }
779
780 static void iseries_writesl(volatile void __iomem *addr, const void *buf,
781                             unsigned long count)
782 {
783         const u32 *src = buf;
784         while(count-- > 0)
785                 iseries_writel_be(*(src++), addr);
786 }
787
788 static void iseries_memset_io(volatile void __iomem *addr, int c,
789                               unsigned long n)
790 {
791         volatile char __iomem *d = addr;
792
793         while (n-- > 0)
794                 iseries_writeb(c, d++);
795 }
796
797 static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
798                                   unsigned long n)
799 {
800         char *d = dest;
801         const volatile char __iomem *s = src;
802
803         while (n-- > 0)
804                 *d++ = iseries_readb(s++);
805 }
806
807 static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
808                                 unsigned long n)
809 {
810         const char *s = src;
811         volatile char __iomem *d = dest;
812
813         while (n-- > 0)
814                 iseries_writeb(*s++, d++);
815 }
816
817 /* We only set MMIO ops. The default PIO ops will be default
818  * to the MMIO ops + pci_io_base which is 0 on iSeries as
819  * expected so both should work.
820  *
821  * Note that we don't implement the readq/writeq versions as
822  * I don't know of an HV call for doing so. Thus, the default
823  * operation will be used instead, which will fault a the value
824  * return by iSeries for MMIO addresses always hits a non mapped
825  * area. This is as good as the BUG() we used to have there.
826  */
827 static struct ppc_pci_io __initdata iseries_pci_io = {
828         .readb = iseries_readb,
829         .readw = iseries_readw,
830         .readl = iseries_readl,
831         .readw_be = iseries_readw_be,
832         .readl_be = iseries_readl_be,
833         .writeb = iseries_writeb,
834         .writew = iseries_writew,
835         .writel = iseries_writel,
836         .writew_be = iseries_writew_be,
837         .writel_be = iseries_writel_be,
838         .readsb = iseries_readsb,
839         .readsw = iseries_readsw,
840         .readsl = iseries_readsl,
841         .writesb = iseries_writesb,
842         .writesw = iseries_writesw,
843         .writesl = iseries_writesl,
844         .memset_io = iseries_memset_io,
845         .memcpy_fromio = iseries_memcpy_fromio,
846         .memcpy_toio = iseries_memcpy_toio,
847 };
848
849 /*
850  * iSeries_pcibios_init
851  *
852  * Description:
853  *   This function checks for all possible system PCI host bridges that connect
854  *   PCI buses.  The system hypervisor is queried as to the guest partition
855  *   ownership status.  A pci_controller is built for any bus which is partially
856  *   owned or fully owned by this guest partition.
857  */
858 void __init iSeries_pcibios_init(void)
859 {
860         struct pci_controller *phb;
861         struct device_node *root = of_find_node_by_path("/");
862         struct device_node *node = NULL;
863
864         /* Install IO hooks */
865         ppc_pci_io = iseries_pci_io;
866
867         pci_probe_only = 1;
868
869         /* iSeries has no IO space in the common sense, it needs to set
870          * the IO base to 0
871          */
872         pci_io_base = 0;
873
874         if (root == NULL) {
875                 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
876                                 "of device tree\n");
877                 return;
878         }
879         while ((node = of_get_next_child(root, node)) != NULL) {
880                 HvBusNumber bus;
881                 const u32 *busp;
882
883                 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
884                         continue;
885
886                 busp = of_get_property(node, "bus-range", NULL);
887                 if (busp == NULL)
888                         continue;
889                 bus = *busp;
890                 printk("bus %d appears to exist\n", bus);
891                 phb = pcibios_alloc_controller(node);
892                 if (phb == NULL)
893                         continue;
894                 /* All legacy iSeries PHBs are in domain zero */
895                 phb->global_number = 0;
896
897                 phb->pci_mem_offset = bus;
898                 phb->first_busno = bus;
899                 phb->last_busno = bus;
900                 phb->ops = &iSeries_pci_ops;
901         }
902
903         of_node_put(root);
904
905         pci_devs_phb_init();
906 }
907