blob: 3071a30356ceb748488bc072441f6408e64a7c50 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Allan Trautman, IBM Corporation
3 *
4 * iSeries specific routines for PCI.
Stephen Rothwelld3878992005-09-28 02:50:25 +10005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on code from pci.c and iSeries_pci.c 32bit
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Stephen Rothwelld3878992005-09-28 02:50:25 +100012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Stephen Rothwelld3878992005-09-28 02:50:25 +100017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/kernel.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100023#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/string.h>
25#include <linux/init.h>
26#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pci.h>
28
29#include <asm/io.h>
30#include <asm/irq.h>
31#include <asm/prom.h>
32#include <asm/machdep.h>
33#include <asm/pci-bridge.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/iommu.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100035#include <asm/abs_addr.h>
Stephen Rothwellcaf81322006-09-21 18:00:00 +100036#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Kelly Daly8021b8a2005-11-02 11:41:12 +110038#include <asm/iseries/hv_call_xm.h>
Kelly Dalybbc8b622005-11-02 15:10:38 +110039#include <asm/iseries/mf.h>
Stephen Rothwellc7f0e8c2006-04-27 17:23:32 +100040#include <asm/iseries/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Stephen Rothwelld3878992005-09-28 02:50:25 +100042#include <asm/ppc-pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100044#include "irq.h"
Stephen Rothwell426c1a12005-10-14 14:51:42 +100045#include "pci.h"
Stephen Rothwellc6d2ea92005-10-14 17:16:17 +100046#include "call_pci.h"
Stephen Rothwellb08567cb2005-09-28 23:37:01 +100047
Stephen Rothwellb9b18122007-12-07 01:48:14 +110048#define PCI_RETRY_MAX 3
49static int limit_pci_retries = 1; /* Set Retry Error on. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Table defines
53 * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
54 */
55#define IOMM_TABLE_MAX_ENTRIES 1024
56#define IOMM_TABLE_ENTRY_SIZE 0x0000000000400000UL
57#define BASE_IO_MEMORY 0xE000000000000000UL
58
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100059static unsigned long max_io_memory = BASE_IO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static long current_iomm_table_entry;
61
62/*
63 * Lookup Tables.
64 */
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100065static struct device_node *iomm_table[IOMM_TABLE_MAX_ENTRIES];
66static u8 iobar_table[IOMM_TABLE_MAX_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100068static const char pci_io_text[] = "iSeries PCI I/O";
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static DEFINE_SPINLOCK(iomm_table_lock);
70
71/*
Stephen Rothwell2cd10082007-12-07 01:55:50 +110072 * Generate a Direct Select Address for the Hypervisor
73 */
74static inline u64 iseries_ds_addr(struct device_node *node)
75{
76 struct pci_dn *pdn = PCI_DN(node);
77
78 return ((u64)pdn->busno << 48) + ((u64)pdn->bussubno << 40)
79 + ((u64)0x10 << 32);
80}
81
82/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * iomm_table_allocate_entry
84 *
85 * Adds pci_dev entry in address translation table
86 *
87 * - Allocates the number of entries required in table base on BAR
88 * size.
89 * - Allocates starting at BASE_IO_MEMORY and increases.
90 * - The size is round up to be a multiple of entry size.
91 * - CurrentIndex is incremented to keep track of the last entry.
92 * - Builds the resource entry for allocated BARs.
93 */
Stephen Rothwell1e105902007-11-19 17:03:59 +110094static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct resource *bar_res = &dev->resource[bar_num];
97 long bar_size = pci_resource_len(dev, bar_num);
98
99 /*
100 * No space to allocate, quick exit, skip Allocation.
101 */
102 if (bar_size == 0)
103 return;
104 /*
105 * Set Resource values.
106 */
107 spin_lock(&iomm_table_lock);
108 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000109 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 bar_res->end = bar_res->start + bar_size - 1;
112 /*
113 * Allocate the number of table entries needed for BAR.
114 */
115 while (bar_size > 0 ) {
116 iomm_table[current_iomm_table_entry] = dev->sysdata;
117 iobar_table[current_iomm_table_entry] = bar_num;
118 bar_size -= IOMM_TABLE_ENTRY_SIZE;
119 ++current_iomm_table_entry;
120 }
121 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000122 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 spin_unlock(&iomm_table_lock);
124}
125
126/*
127 * allocate_device_bars
128 *
129 * - Allocates ALL pci_dev BAR's and updates the resources with the
130 * BAR value. BARS with zero length will have the resources
131 * The HvCallPci_getBarParms is used to get the size of the BAR
132 * space. It calls iomm_table_allocate_entry to allocate
133 * each entry.
134 * - Loops through The Bar resources(0 - 5) including the ROM
135 * is resource(6).
136 */
Stephen Rothwell1e105902007-11-19 17:03:59 +1100137static void __init allocate_device_bars(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int bar_num;
140
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000141 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/*
146 * Log error information to system console.
147 * Filter out the device not there errors.
148 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
149 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
150 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
151 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100152static void pci_log_error(char *error, int bus, int subbus,
153 int agent, int hv_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100155 if (hv_res == 0x0302)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return;
157 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100158 error, bus, subbus, agent, hv_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100162 * Look down the chain to find the matching Device Device
163 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100164static struct device_node *find_device_node(int bus, int devfn)
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100165{
166 struct device_node *node;
167
168 for (node = NULL; (node = of_find_all_nodes(node)); ) {
169 struct pci_dn *pdn = PCI_DN(node);
170
171 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
172 return node;
173 }
174 return NULL;
175}
176
177/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000178 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
180void __init iSeries_pci_final_fixup(void)
181{
182 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000183 struct device_node *node;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100184 int num_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Fix up at the device node and pci_dev relationship */
187 mf_display_src(0xC9000100);
188
189 printk("pcibios_final_fixup\n");
190 for_each_pci_dev(pdev) {
Stephen Rothwell3f178632007-12-07 01:51:48 +1100191 const u32 *agent;
Stephen Rothwell3b32c162007-12-07 01:56:55 +1100192 const u32 *sub_bus;
Stephen Rothwell53761742007-12-07 01:58:01 +1100193 unsigned char bus = pdev->bus->number;
Stephen Rothwell3f178632007-12-07 01:51:48 +1100194
Stephen Rothwell53761742007-12-07 01:58:01 +1100195 node = find_device_node(bus, pdev->devfn);
196 printk("pci dev %p (%x.%x), node %p\n", pdev, bus,
197 pdev->devfn, node);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100198 if (!node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 printk("PCI: Device Tree not found for 0x%016lX\n",
200 (unsigned long)pdev);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100201 continue;
202 }
203
Stephen Rothwell3f178632007-12-07 01:51:48 +1100204 agent = of_get_property(node, "linux,agent-id", NULL);
Stephen Rothwell3b32c162007-12-07 01:56:55 +1100205 sub_bus = of_get_property(node, "linux,subbus", NULL);
Stephen Rothwell53761742007-12-07 01:58:01 +1100206 if (agent && sub_bus) {
207 u8 irq = iSeries_allocate_IRQ(bus, 0, *sub_bus);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100208 int err;
209
Stephen Rothwell53761742007-12-07 01:58:01 +1100210 err = HvCallXm_connectBusUnit(bus, *sub_bus,
Stephen Rothwell3f178632007-12-07 01:51:48 +1100211 *agent, irq);
212 if (err)
213 pci_log_error("Connect Bus Unit",
Stephen Rothwell53761742007-12-07 01:58:01 +1100214 bus, *sub_bus, *agent, err);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100215 else {
Stephen Rothwell53761742007-12-07 01:58:01 +1100216 err = HvCallPci_configStore8(bus, *sub_bus,
217 *agent, PCI_INTERRUPT_LINE, irq);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100218 if (err)
219 pci_log_error("PciCfgStore Irq Failed!",
Stephen Rothwell53761742007-12-07 01:58:01 +1100220 bus, *sub_bus, *agent, err);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100221 else
222 pdev->irq = irq;
223 }
224 }
225
226 num_dev++;
227 pdev->sysdata = node;
228 PCI_DN(node)->pcidev = pdev;
229 allocate_device_bars(pdev);
Stephen Rothwell53761742007-12-07 01:58:01 +1100230 iSeries_Device_Information(pdev, num_dev, bus, *sub_bus);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100231 iommu_devnode_init_iSeries(pdev, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 iSeries_activate_IRQs();
234 mf_display_src(0xC9000200);
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * Config space read and write functions.
239 * For now at least, we look for the device node for the bus and devfn
240 * that we are asked to access. It may be possible to translate the devfn
241 * to a subbus and deviceid more directly.
242 */
243static u64 hv_cfg_read_func[4] = {
244 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
245 HvCallPciConfigLoad32, HvCallPciConfigLoad32
246};
247
248static u64 hv_cfg_write_func[4] = {
249 HvCallPciConfigStore8, HvCallPciConfigStore16,
250 HvCallPciConfigStore32, HvCallPciConfigStore32
251};
252
253/*
254 * Read PCI config space
255 */
256static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
257 int offset, int size, u32 *val)
258{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100259 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 u64 fn;
261 struct HvCallPci_LoadReturn ret;
262
263 if (node == NULL)
264 return PCIBIOS_DEVICE_NOT_FOUND;
265 if (offset > 255) {
266 *val = ~0;
267 return PCIBIOS_BAD_REGISTER_NUMBER;
268 }
269
270 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000271 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (ret.rc != 0) {
274 *val = ~0;
275 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
276 }
277
278 *val = ret.value;
279 return 0;
280}
281
282/*
283 * Write PCI config space
284 */
285
286static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
287 int offset, int size, u32 val)
288{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100289 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 u64 fn;
291 u64 ret;
292
293 if (node == NULL)
294 return PCIBIOS_DEVICE_NOT_FOUND;
295 if (offset > 255)
296 return PCIBIOS_BAD_REGISTER_NUMBER;
297
298 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000299 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (ret != 0)
302 return PCIBIOS_DEVICE_NOT_FOUND;
303
304 return 0;
305}
306
307static struct pci_ops iSeries_pci_ops = {
308 .read = iSeries_pci_read_config,
309 .write = iSeries_pci_write_config
310};
311
312/*
313 * Check Return Code
314 * -> On Failure, print and log information.
315 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *
317 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
318 * PCI: Device 23.90 ReadL Retry( 1)
319 * PCI: Device 23.90 ReadL Retry Successful(1)
320 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100321static int check_return_code(char *type, struct device_node *dn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700322 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 if (ret != 0) {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100325 struct pci_dn *pdn = PCI_DN(dn);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000326
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700327 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100329 type, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700330 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Bump the retry and check for retry count exceeded.
333 * If, Exceeded, panic the system.
334 */
Stephen Rothwellb9b18122007-12-07 01:48:14 +1100335 if (((*retry) > PCI_RETRY_MAX) &&
336 (limit_pci_retries > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700338 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 panic("PCI: Hardware I/O Error, SRC B6000103, "
340 "Automatic Reboot Disabled.\n");
341 }
342 return -1; /* Retry Try */
343 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/*
348 * Translate the I/O Address into a device node, bar, and bar offset.
349 * Note: Make sure the passed variable end up on the stack to avoid
350 * the exposure of being device global.
351 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000352static inline struct device_node *xlate_iomm_address(
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100353 const volatile void __iomem *addr,
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100354 u64 *dsaptr, u64 *bar_offset, const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100356 unsigned long orig_addr;
357 unsigned long base_addr;
358 unsigned long ind;
359 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100361 orig_addr = (unsigned long __force)addr;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100362 if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
363 static unsigned long last_jiffies;
364 static int num_printed;
365
366 if ((jiffies - last_jiffies) > 60 * HZ) {
367 last_jiffies = jiffies;
368 num_printed = 0;
369 }
370 if (num_printed++ < 10)
371 printk(KERN_ERR
372 "iSeries_%s: invalid access at IO address %p\n",
373 func, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100375 }
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100376 base_addr = orig_addr - BASE_IO_MEMORY;
377 ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
378 dn = iomm_table[ind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100380 if (dn != NULL) {
381 int barnum = iobar_table[ind];
382 *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
383 *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100385 panic("PCI: Invalid PCI IO address detected!\n");
386 return dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
390 * Read MM I/O Instructions for the iSeries
391 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100392 * else, data is returned in Big Endian format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100394static u8 iseries_readb(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100396 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700398 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100400 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100401 xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100403 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100406 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
407 } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100409 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Stephen Rothwell34489382007-12-07 01:53:44 +1100412static u16 iseries_readw_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100414 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700416 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100418 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100419 xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100421 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100425 bar_offset, 0);
426 } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100428 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Stephen Rothwell34489382007-12-07 01:53:44 +1100431static u32 iseries_readl_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100433 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700435 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100437 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100438 xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100440 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100444 bar_offset, 0);
445 } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100447 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450/*
451 * Write MM I/O Instructions for the iSeries
452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100454static void iseries_writeb(u8 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100456 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700458 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100460 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100461 xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100463 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100466 rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
467 } while (check_return_code("WWB", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Stephen Rothwell34489382007-12-07 01:53:44 +1100470static void iseries_writew_be(u16 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100472 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700474 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100476 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100477 xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100479 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100482 rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
483 } while (check_return_code("WWW", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Stephen Rothwell34489382007-12-07 01:53:44 +1100486static void iseries_writel_be(u32 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100488 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700490 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100492 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100493 xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100495 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100498 rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
499 } while (check_return_code("WWL", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000501
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100502static u16 iseries_readw(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000503{
Stephen Rothwell34489382007-12-07 01:53:44 +1100504 return le16_to_cpu(iseries_readw_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000505}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000506
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100507static u32 iseries_readl(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000508{
Stephen Rothwell34489382007-12-07 01:53:44 +1100509 return le32_to_cpu(iseries_readl_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000510}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000511
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100512static void iseries_writew(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000513{
Stephen Rothwell34489382007-12-07 01:53:44 +1100514 iseries_writew_be(cpu_to_le16(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000515}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000516
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100517static void iseries_writel(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000518{
Stephen Rothwell34489382007-12-07 01:53:44 +1100519 iseries_writel(cpu_to_le32(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000520}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000521
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100522static void iseries_readsb(const volatile void __iomem *addr, void *buf,
523 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000524{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100525 u8 *dst = buf;
526 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100527 *(dst++) = iseries_readb(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000528}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000529
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100530static void iseries_readsw(const volatile void __iomem *addr, void *buf,
531 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000532{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100533 u16 *dst = buf;
534 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100535 *(dst++) = iseries_readw_be(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000536}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000537
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100538static void iseries_readsl(const volatile void __iomem *addr, void *buf,
539 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000540{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100541 u32 *dst = buf;
542 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100543 *(dst++) = iseries_readl_be(addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000544}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000545
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100546static void iseries_writesb(volatile void __iomem *addr, const void *buf,
547 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000548{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100549 const u8 *src = buf;
550 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100551 iseries_writeb(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000552}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000553
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100554static void iseries_writesw(volatile void __iomem *addr, const void *buf,
555 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000556{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100557 const u16 *src = buf;
558 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100559 iseries_writew_be(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000560}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000561
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100562static void iseries_writesl(volatile void __iomem *addr, const void *buf,
563 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000564{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100565 const u32 *src = buf;
566 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100567 iseries_writel_be(*(src++), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000568}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000569
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100570static void iseries_memset_io(volatile void __iomem *addr, int c,
571 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000572{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100573 volatile char __iomem *d = addr;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000574
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100575 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100576 iseries_writeb(c, d++);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100577}
578
579static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
580 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000581{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100582 char *d = dest;
583 const volatile char __iomem *s = src;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000584
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100585 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100586 *d++ = iseries_readb(s++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000587}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000588
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100589static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
590 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000591{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100592 const char *s = src;
593 volatile char __iomem *d = dest;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000594
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100595 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100596 iseries_writeb(*s++, d++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000597}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000598
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100599/* We only set MMIO ops. The default PIO ops will be default
600 * to the MMIO ops + pci_io_base which is 0 on iSeries as
601 * expected so both should work.
602 *
603 * Note that we don't implement the readq/writeq versions as
604 * I don't know of an HV call for doing so. Thus, the default
605 * operation will be used instead, which will fault a the value
606 * return by iSeries for MMIO addresses always hits a non mapped
607 * area. This is as good as the BUG() we used to have there.
608 */
609static struct ppc_pci_io __initdata iseries_pci_io = {
610 .readb = iseries_readb,
611 .readw = iseries_readw,
612 .readl = iseries_readl,
613 .readw_be = iseries_readw_be,
614 .readl_be = iseries_readl_be,
615 .writeb = iseries_writeb,
616 .writew = iseries_writew,
617 .writel = iseries_writel,
618 .writew_be = iseries_writew_be,
619 .writel_be = iseries_writel_be,
620 .readsb = iseries_readsb,
621 .readsw = iseries_readsw,
622 .readsl = iseries_readsl,
623 .writesb = iseries_writesb,
624 .writesw = iseries_writesw,
625 .writesl = iseries_writesl,
626 .memset_io = iseries_memset_io,
627 .memcpy_fromio = iseries_memcpy_fromio,
628 .memcpy_toio = iseries_memcpy_toio,
629};
630
631/*
632 * iSeries_pcibios_init
633 *
634 * Description:
635 * This function checks for all possible system PCI host bridges that connect
636 * PCI buses. The system hypervisor is queried as to the guest partition
637 * ownership status. A pci_controller is built for any bus which is partially
638 * owned or fully owned by this guest partition.
639 */
640void __init iSeries_pcibios_init(void)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000641{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100642 struct pci_controller *phb;
643 struct device_node *root = of_find_node_by_path("/");
644 struct device_node *node = NULL;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000645
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100646 /* Install IO hooks */
647 ppc_pci_io = iseries_pci_io;
648
Stephen Rothwell9ccc4fd2007-12-07 02:03:23 +1100649 pci_probe_only = 1;
650
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000651 /* iSeries has no IO space in the common sense, it needs to set
652 * the IO base to 0
653 */
654 pci_io_base = 0;
655
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100656 if (root == NULL) {
657 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
658 "of device tree\n");
659 return;
660 }
661 while ((node = of_get_next_child(root, node)) != NULL) {
662 HvBusNumber bus;
663 const u32 *busp;
664
665 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
666 continue;
667
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000668 busp = of_get_property(node, "bus-range", NULL);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100669 if (busp == NULL)
670 continue;
671 bus = *busp;
672 printk("bus %d appears to exist\n", bus);
673 phb = pcibios_alloc_controller(node);
674 if (phb == NULL)
675 continue;
Stephen Rothwell6207e812007-12-07 02:04:33 +1100676 /* All legacy iSeries PHBs are in domain zero */
677 phb->global_number = 0;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100678
Kumar Galabf440b72007-06-27 00:19:08 -0500679 phb->pci_mem_offset = bus;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100680 phb->first_busno = bus;
681 phb->last_busno = bus;
682 phb->ops = &iSeries_pci_ops;
683 }
684
685 of_node_put(root);
686
687 pci_devs_phb_init();
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000688}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000689