blob: 3cf51c6df686dd3c2dd3ac18cf8b1f667de35749 [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * iomm_table_allocate_entry
73 *
74 * Adds pci_dev entry in address translation table
75 *
76 * - Allocates the number of entries required in table base on BAR
77 * size.
78 * - Allocates starting at BASE_IO_MEMORY and increases.
79 * - The size is round up to be a multiple of entry size.
80 * - CurrentIndex is incremented to keep track of the last entry.
81 * - Builds the resource entry for allocated BARs.
82 */
Stephen Rothwell1e105902007-11-19 17:03:59 +110083static void __init iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct resource *bar_res = &dev->resource[bar_num];
86 long bar_size = pci_resource_len(dev, bar_num);
87
88 /*
89 * No space to allocate, quick exit, skip Allocation.
90 */
91 if (bar_size == 0)
92 return;
93 /*
94 * Set Resource values.
95 */
96 spin_lock(&iomm_table_lock);
97 bar_res->name = pci_io_text;
Stephen Rothwellb58b7f92006-05-19 16:42:49 +100098 bar_res->start = BASE_IO_MEMORY +
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 bar_res->end = bar_res->start + bar_size - 1;
101 /*
102 * Allocate the number of table entries needed for BAR.
103 */
104 while (bar_size > 0 ) {
105 iomm_table[current_iomm_table_entry] = dev->sysdata;
106 iobar_table[current_iomm_table_entry] = bar_num;
107 bar_size -= IOMM_TABLE_ENTRY_SIZE;
108 ++current_iomm_table_entry;
109 }
110 max_io_memory = BASE_IO_MEMORY +
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000111 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_unlock(&iomm_table_lock);
113}
114
115/*
116 * allocate_device_bars
117 *
118 * - Allocates ALL pci_dev BAR's and updates the resources with the
119 * BAR value. BARS with zero length will have the resources
120 * The HvCallPci_getBarParms is used to get the size of the BAR
121 * space. It calls iomm_table_allocate_entry to allocate
122 * each entry.
123 * - Loops through The Bar resources(0 - 5) including the ROM
124 * is resource(6).
125 */
Stephen Rothwell1e105902007-11-19 17:03:59 +1100126static void __init allocate_device_bars(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int bar_num;
129
Stephen Rothwellb58b7f92006-05-19 16:42:49 +1000130 for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 iomm_table_allocate_entry(dev, bar_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/*
135 * Log error information to system console.
136 * Filter out the device not there errors.
137 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
138 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
139 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
140 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100141static void pci_log_error(char *error, int bus, int subbus,
142 int agent, int hv_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100144 if (hv_res == 0x0302)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return;
146 printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100147 error, bus, subbus, agent, hv_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/*
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100151 * Look down the chain to find the matching Device Device
152 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100153static struct device_node *find_device_node(int bus, int devfn)
Stephen Rothwell9103eb72007-12-07 01:46:40 +1100154{
155 struct device_node *node;
156
157 for (node = NULL; (node = of_find_all_nodes(node)); ) {
158 struct pci_dn *pdn = PCI_DN(node);
159
160 if (pdn && (bus == pdn->busno) && (devfn == pdn->devfn))
161 return node;
162 }
163 return NULL;
164}
165
166/*
Stephen Rothwelld3878992005-09-28 02:50:25 +1000167 * iSeries_pci_final_fixup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169void __init iSeries_pci_final_fixup(void)
170{
171 struct pci_dev *pdev = NULL;
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000172 struct device_node *node;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100173 int num_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Fix up at the device node and pci_dev relationship */
176 mf_display_src(0xC9000100);
177
178 printk("pcibios_final_fixup\n");
179 for_each_pci_dev(pdev) {
Stephen Rothwell3f178632007-12-07 01:51:48 +1100180 struct pci_dn *pdn;
181 const u32 *agent;
182
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100183 node = find_device_node(pdev->bus->number, pdev->devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 printk("pci dev %p (%x.%x), node %p\n", pdev,
185 pdev->bus->number, pdev->devfn, node);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100186 if (!node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 printk("PCI: Device Tree not found for 0x%016lX\n",
188 (unsigned long)pdev);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100189 continue;
190 }
191
192 pdn = PCI_DN(node);
193 agent = of_get_property(node, "linux,agent-id", NULL);
194 if (pdn && agent) {
195 u8 irq = iSeries_allocate_IRQ(pdn->busno, 0,
196 pdn->bussubno);
197 int err;
198
199 err = HvCallXm_connectBusUnit(pdn->busno, pdn->bussubno,
200 *agent, irq);
201 if (err)
202 pci_log_error("Connect Bus Unit",
203 pdn->busno, pdn->bussubno, *agent, err);
204 else {
205 err = HvCallPci_configStore8(pdn->busno,
206 pdn->bussubno, *agent,
207 PCI_INTERRUPT_LINE, irq);
208 if (err)
209 pci_log_error("PciCfgStore Irq Failed!",
210 pdn->busno, pdn->bussubno,
211 *agent, err);
212 else
213 pdev->irq = irq;
214 }
215 }
216
217 num_dev++;
218 pdev->sysdata = node;
219 PCI_DN(node)->pcidev = pdev;
220 allocate_device_bars(pdev);
Stephen Rothwell72ece3b2007-12-07 01:54:57 +1100221 iSeries_Device_Information(pdev, num_dev, pdn->busno,
222 pdn->bussubno);
Stephen Rothwell3f178632007-12-07 01:51:48 +1100223 iommu_devnode_init_iSeries(pdev, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 iSeries_activate_IRQs();
226 mf_display_src(0xC9000200);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * Config space read and write functions.
231 * For now at least, we look for the device node for the bus and devfn
232 * that we are asked to access. It may be possible to translate the devfn
233 * to a subbus and deviceid more directly.
234 */
235static u64 hv_cfg_read_func[4] = {
236 HvCallPciConfigLoad8, HvCallPciConfigLoad16,
237 HvCallPciConfigLoad32, HvCallPciConfigLoad32
238};
239
240static u64 hv_cfg_write_func[4] = {
241 HvCallPciConfigStore8, HvCallPciConfigStore16,
242 HvCallPciConfigStore32, HvCallPciConfigStore32
243};
244
245/*
246 * Read PCI config space
247 */
248static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
249 int offset, int size, u32 *val)
250{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100251 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 u64 fn;
253 struct HvCallPci_LoadReturn ret;
254
255 if (node == NULL)
256 return PCIBIOS_DEVICE_NOT_FOUND;
257 if (offset > 255) {
258 *val = ~0;
259 return PCIBIOS_BAD_REGISTER_NUMBER;
260 }
261
262 fn = hv_cfg_read_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000263 HvCall3Ret16(fn, &ret, iseries_ds_addr(node), offset, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (ret.rc != 0) {
266 *val = ~0;
267 return PCIBIOS_DEVICE_NOT_FOUND; /* or something */
268 }
269
270 *val = ret.value;
271 return 0;
272}
273
274/*
275 * Write PCI config space
276 */
277
278static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
279 int offset, int size, u32 val)
280{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100281 struct device_node *node = find_device_node(bus->number, devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 u64 fn;
283 u64 ret;
284
285 if (node == NULL)
286 return PCIBIOS_DEVICE_NOT_FOUND;
287 if (offset > 255)
288 return PCIBIOS_BAD_REGISTER_NUMBER;
289
290 fn = hv_cfg_write_func[(size - 1) & 3];
Stephen Rothwell20f48cc2005-10-14 16:49:58 +1000291 ret = HvCall4(fn, iseries_ds_addr(node), offset, val, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (ret != 0)
294 return PCIBIOS_DEVICE_NOT_FOUND;
295
296 return 0;
297}
298
299static struct pci_ops iSeries_pci_ops = {
300 .read = iSeries_pci_read_config,
301 .write = iSeries_pci_write_config
302};
303
304/*
305 * Check Return Code
306 * -> On Failure, print and log information.
307 * Increment Retry Count, if exceeds max, panic partition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 *
309 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
310 * PCI: Device 23.90 ReadL Retry( 1)
311 * PCI: Device 23.90 ReadL Retry Successful(1)
312 */
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100313static int check_return_code(char *type, struct device_node *dn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700314 int *retry, u64 ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 if (ret != 0) {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100317 struct pci_dn *pdn = PCI_DN(dn);
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000318
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700319 (*retry)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 printk("PCI: %s: Device 0x%04X:%02X I/O Error(%2d): 0x%04X\n",
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100321 type, pdn->busno, pdn->devfn,
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700322 *retry, (int)ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * Bump the retry and check for retry count exceeded.
325 * If, Exceeded, panic the system.
326 */
Stephen Rothwellb9b18122007-12-07 01:48:14 +1100327 if (((*retry) > PCI_RETRY_MAX) &&
328 (limit_pci_retries > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 mf_display_src(0xB6000103);
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700330 panic_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 panic("PCI: Hardware I/O Error, SRC B6000103, "
332 "Automatic Reboot Disabled.\n");
333 }
334 return -1; /* Retry Try */
335 }
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339/*
340 * Translate the I/O Address into a device node, bar, and bar offset.
341 * Note: Make sure the passed variable end up on the stack to avoid
342 * the exposure of being device global.
343 */
Stephen Rothwell252e75a2005-09-28 14:40:40 +1000344static inline struct device_node *xlate_iomm_address(
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100345 const volatile void __iomem *addr,
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100346 u64 *dsaptr, u64 *bar_offset, const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100348 unsigned long orig_addr;
349 unsigned long base_addr;
350 unsigned long ind;
351 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100353 orig_addr = (unsigned long __force)addr;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100354 if ((orig_addr < BASE_IO_MEMORY) || (orig_addr >= max_io_memory)) {
355 static unsigned long last_jiffies;
356 static int num_printed;
357
358 if ((jiffies - last_jiffies) > 60 * HZ) {
359 last_jiffies = jiffies;
360 num_printed = 0;
361 }
362 if (num_printed++ < 10)
363 printk(KERN_ERR
364 "iSeries_%s: invalid access at IO address %p\n",
365 func, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return NULL;
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100367 }
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100368 base_addr = orig_addr - BASE_IO_MEMORY;
369 ind = base_addr / IOMM_TABLE_ENTRY_SIZE;
370 dn = iomm_table[ind];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100372 if (dn != NULL) {
373 int barnum = iobar_table[ind];
374 *dsaptr = iseries_ds_addr(dn) | (barnum << 24);
375 *bar_offset = base_addr % IOMM_TABLE_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100377 panic("PCI: Invalid PCI IO address detected!\n");
378 return dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381/*
382 * Read MM I/O Instructions for the iSeries
383 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100384 * else, data is returned in Big Endian format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100386static u8 iseries_readb(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100388 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700390 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100392 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100393 xlate_iomm_address(addr, &dsa, &bar_offset, "read_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100395 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100398 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, bar_offset, 0);
399 } while (check_return_code("RDB", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100401 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Stephen Rothwell34489382007-12-07 01:53:44 +1100404static u16 iseries_readw_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100406 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700408 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100410 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100411 xlate_iomm_address(addr, &dsa, &bar_offset, "read_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100413 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100417 bar_offset, 0);
418 } while (check_return_code("RDW", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100420 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Stephen Rothwell34489382007-12-07 01:53:44 +1100423static u32 iseries_readl_be(const volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100425 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700427 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct HvCallPci_LoadReturn ret;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100429 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100430 xlate_iomm_address(addr, &dsa, &bar_offset, "read_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100432 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100436 bar_offset, 0);
437 } while (check_return_code("RDL", dn, &retry, ret.rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100439 return ret.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442/*
443 * Write MM I/O Instructions for the iSeries
444 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Stephen Rothwell34489382007-12-07 01:53:44 +1100446static void iseries_writeb(u8 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100448 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700450 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100452 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100453 xlate_iomm_address(addr, &dsa, &bar_offset, "write_byte");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100455 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100458 rc = HvCall4(HvCallPciBarStore8, dsa, bar_offset, data, 0);
459 } while (check_return_code("WWB", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Stephen Rothwell34489382007-12-07 01:53:44 +1100462static void iseries_writew_be(u16 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100464 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700466 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100468 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100469 xlate_iomm_address(addr, &dsa, &bar_offset, "write_word");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100471 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100474 rc = HvCall4(HvCallPciBarStore16, dsa, bar_offset, data, 0);
475 } while (check_return_code("WWW", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Stephen Rothwell34489382007-12-07 01:53:44 +1100478static void iseries_writel_be(u32 data, volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100480 u64 bar_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 u64 dsa;
Stephen Rothwella2ebaf22005-06-21 17:15:47 -0700482 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 u64 rc;
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100484 struct device_node *dn =
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100485 xlate_iomm_address(addr, &dsa, &bar_offset, "write_long");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Stephen Rothwell0d416f22007-12-07 01:52:45 +1100487 if (dn == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 do {
Stephen Rothwell7a73bd72007-12-07 01:49:27 +1100490 rc = HvCall4(HvCallPciBarStore32, dsa, bar_offset, data, 0);
491 } while (check_return_code("WWL", dn, &retry, rc) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000493
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100494static u16 iseries_readw(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000495{
Stephen Rothwell34489382007-12-07 01:53:44 +1100496 return le16_to_cpu(iseries_readw_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000497}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000498
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100499static u32 iseries_readl(const volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000500{
Stephen Rothwell34489382007-12-07 01:53:44 +1100501 return le32_to_cpu(iseries_readl_be(addr));
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000502}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000503
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100504static void iseries_writew(u16 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000505{
Stephen Rothwell34489382007-12-07 01:53:44 +1100506 iseries_writew_be(cpu_to_le16(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000507}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000508
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100509static void iseries_writel(u32 data, volatile void __iomem *addr)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000510{
Stephen Rothwell34489382007-12-07 01:53:44 +1100511 iseries_writel(cpu_to_le32(data), addr);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000512}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000513
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100514static void iseries_readsb(const volatile void __iomem *addr, void *buf,
515 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000516{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100517 u8 *dst = buf;
518 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100519 *(dst++) = iseries_readb(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_readsw(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 u16 *dst = buf;
526 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100527 *(dst++) = iseries_readw_be(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_readsl(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 u32 *dst = buf;
534 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100535 *(dst++) = iseries_readl_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_writesb(volatile void __iomem *addr, const void *buf,
539 unsigned long count)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000540{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100541 const u8 *src = buf;
542 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100543 iseries_writeb(*(src++), 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_writesw(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 u16 *src = buf;
550 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100551 iseries_writew_be(*(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_writesl(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 u32 *src = buf;
558 while(count-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100559 iseries_writel_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_memset_io(volatile void __iomem *addr, int c,
563 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000564{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100565 volatile char __iomem *d = addr;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000566
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100567 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100568 iseries_writeb(c, d++);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100569}
570
571static void iseries_memcpy_fromio(void *dest, const volatile void __iomem *src,
572 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000573{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100574 char *d = dest;
575 const volatile char __iomem *s = src;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000576
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100577 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100578 *d++ = iseries_readb(s++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000579}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000580
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100581static void iseries_memcpy_toio(volatile void __iomem *dest, const void *src,
582 unsigned long n)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000583{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100584 const char *s = src;
585 volatile char __iomem *d = dest;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000586
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100587 while (n-- > 0)
Stephen Rothwell34489382007-12-07 01:53:44 +1100588 iseries_writeb(*s++, d++);
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000589}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000590
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100591/* We only set MMIO ops. The default PIO ops will be default
592 * to the MMIO ops + pci_io_base which is 0 on iSeries as
593 * expected so both should work.
594 *
595 * Note that we don't implement the readq/writeq versions as
596 * I don't know of an HV call for doing so. Thus, the default
597 * operation will be used instead, which will fault a the value
598 * return by iSeries for MMIO addresses always hits a non mapped
599 * area. This is as good as the BUG() we used to have there.
600 */
601static struct ppc_pci_io __initdata iseries_pci_io = {
602 .readb = iseries_readb,
603 .readw = iseries_readw,
604 .readl = iseries_readl,
605 .readw_be = iseries_readw_be,
606 .readl_be = iseries_readl_be,
607 .writeb = iseries_writeb,
608 .writew = iseries_writew,
609 .writel = iseries_writel,
610 .writew_be = iseries_writew_be,
611 .writel_be = iseries_writel_be,
612 .readsb = iseries_readsb,
613 .readsw = iseries_readsw,
614 .readsl = iseries_readsl,
615 .writesb = iseries_writesb,
616 .writesw = iseries_writesw,
617 .writesl = iseries_writesl,
618 .memset_io = iseries_memset_io,
619 .memcpy_fromio = iseries_memcpy_fromio,
620 .memcpy_toio = iseries_memcpy_toio,
621};
622
623/*
624 * iSeries_pcibios_init
625 *
626 * Description:
627 * This function checks for all possible system PCI host bridges that connect
628 * PCI buses. The system hypervisor is queried as to the guest partition
629 * ownership status. A pci_controller is built for any bus which is partially
630 * owned or fully owned by this guest partition.
631 */
632void __init iSeries_pcibios_init(void)
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000633{
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100634 struct pci_controller *phb;
635 struct device_node *root = of_find_node_by_path("/");
636 struct device_node *node = NULL;
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000637
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100638 /* Install IO hooks */
639 ppc_pci_io = iseries_pci_io;
640
Benjamin Herrenschmidt3d5134e2007-06-04 15:15:36 +1000641 /* iSeries has no IO space in the common sense, it needs to set
642 * the IO base to 0
643 */
644 pci_io_base = 0;
645
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100646 if (root == NULL) {
647 printk(KERN_CRIT "iSeries_pcibios_init: can't find root "
648 "of device tree\n");
649 return;
650 }
651 while ((node = of_get_next_child(root, node)) != NULL) {
652 HvBusNumber bus;
653 const u32 *busp;
654
655 if ((node->type == NULL) || (strcmp(node->type, "pci") != 0))
656 continue;
657
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000658 busp = of_get_property(node, "bus-range", NULL);
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100659 if (busp == NULL)
660 continue;
661 bus = *busp;
662 printk("bus %d appears to exist\n", bus);
663 phb = pcibios_alloc_controller(node);
664 if (phb == NULL)
665 continue;
666
Kumar Galabf440b72007-06-27 00:19:08 -0500667 phb->pci_mem_offset = bus;
Benjamin Herrenschmidt4cb3cee2006-11-11 17:25:10 +1100668 phb->first_busno = bus;
669 phb->last_busno = bus;
670 phb->ops = &iSeries_pci_ops;
671 }
672
673 of_node_put(root);
674
675 pci_devs_phb_init();
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000676}
Stephen Rothwellcaf81322006-09-21 18:00:00 +1000677