blob: 6959b5c45df4546f28c2ef962fdc197d7afbfe3a [file] [log] [blame]
Jon Masone4650582006-06-26 13:58:14 +02001/*
2 * Derived from arch/powerpc/kernel/iommu.c
3 *
Muli Ben-Yehuda98822342007-07-21 17:10:48 +02004 * Copyright IBM Corporation, 2006-2007
Jon Masond8d2bed2006-10-05 18:47:21 +02005 * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
Jon Masone4650582006-06-26 13:58:14 +02006 *
Jon Masond8d2bed2006-10-05 18:47:21 +02007 * Author: Jon Mason <jdmason@kudzu.us>
Muli Ben-Yehudaaa0a9f32006-07-10 17:06:15 +02008 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
9
Jon Masone4650582006-06-26 13:58:14 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
Jon Masone4650582006-06-26 13:58:14 +020025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/spinlock.h>
31#include <linux/string.h>
32#include <linux/dma-mapping.h>
Jon Masone4650582006-06-26 13:58:14 +020033#include <linux/bitops.h>
34#include <linux/pci_ids.h>
35#include <linux/pci.h>
36#include <linux/delay.h>
Jens Axboe8b87d9f2007-07-24 12:38:15 +020037#include <linux/scatterlist.h>
FUJITA Tomonori1b39b072008-02-04 22:28:10 -080038#include <linux/iommu-helper.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020039#include <asm/gart.h>
Jon Masone4650582006-06-26 13:58:14 +020040#include <asm/calgary.h>
41#include <asm/tce.h>
42#include <asm/pci-direct.h>
43#include <asm/system.h>
44#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010045#include <asm/rio.h>
Akinobu Mitaae5830a2008-04-19 23:55:19 +090046#include <asm/bios_ebda.h>
Jon Masone4650582006-06-26 13:58:14 +020047
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010048#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
49int use_calgary __read_mostly = 1;
50#else
51int use_calgary __read_mostly = 0;
52#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
53
Jon Masone4650582006-06-26 13:58:14 +020054#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020055#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020056
Jon Masone4650582006-06-26 13:58:14 +020057/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020058#define CALGARY_CONFIG_REG 0x0108
59#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020060#define PHB_PLSSR_OFFSET 0x0120
61#define PHB_CONFIG_RW_OFFSET 0x0160
62#define PHB_IOBASE_BAR_LOW 0x0170
63#define PHB_IOBASE_BAR_HIGH 0x0180
64#define PHB_MEM_1_LOW 0x0190
65#define PHB_MEM_1_HIGH 0x01A0
66#define PHB_IO_ADDR_SIZE 0x01B0
67#define PHB_MEM_1_SIZE 0x01C0
68#define PHB_MEM_ST_OFFSET 0x01D0
69#define PHB_AER_OFFSET 0x0200
70#define PHB_CONFIG_0_HIGH 0x0220
71#define PHB_CONFIG_0_LOW 0x0230
72#define PHB_CONFIG_0_END 0x0240
73#define PHB_MEM_2_LOW 0x02B0
74#define PHB_MEM_2_HIGH 0x02C0
75#define PHB_MEM_2_SIZE_HIGH 0x02D0
76#define PHB_MEM_2_SIZE_LOW 0x02E0
77#define PHB_DOSHOLE_OFFSET 0x08E0
78
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020079/* CalIOC2 specific */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020080#define PHB_SAVIOR_L2 0x0DB0
81#define PHB_PAGE_MIG_CTRL 0x0DA8
82#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020083#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020084
Jon Masone4650582006-06-26 13:58:14 +020085/* PHB_CONFIG_RW */
86#define PHB_TCE_ENABLE 0x20000000
87#define PHB_SLOT_DISABLE 0x1C000000
88#define PHB_DAC_DISABLE 0x01000000
89#define PHB_MEM2_ENABLE 0x00400000
90#define PHB_MCSR_ENABLE 0x00100000
91/* TAR (Table Address Register) */
92#define TAR_SW_BITS 0x0000ffffffff800fUL
93#define TAR_VALID 0x0000000000000008UL
94/* CSR (Channel/DMA Status Register) */
95#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020096/* CCR (Calgary Configuration Register) */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020097#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +020098/* PMCR/PMDR (Page Migration Control/Debug Registers */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020099#define PMR_SOFTSTOP 0x80000000
100#define PMR_SOFTSTOPFAULT 0x40000000
101#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200102
103#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200104#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200105/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
106#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200107#define PHBS_PER_CALGARY 4
108
109/* register offsets in Calgary's internal register space */
110static const unsigned long tar_offsets[] = {
111 0x0580 /* TAR0 */,
112 0x0588 /* TAR1 */,
113 0x0590 /* TAR2 */,
114 0x0598 /* TAR3 */
115};
116
117static const unsigned long split_queue_offsets[] = {
118 0x4870 /* SPLIT QUEUE 0 */,
119 0x5870 /* SPLIT QUEUE 1 */,
120 0x6870 /* SPLIT QUEUE 2 */,
121 0x7870 /* SPLIT QUEUE 3 */
122};
123
124static const unsigned long phb_offsets[] = {
125 0x8000 /* PHB0 */,
126 0x9000 /* PHB1 */,
127 0xA000 /* PHB2 */,
128 0xB000 /* PHB3 */
129};
130
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100131/* PHB debug registers */
132
133static const unsigned long phb_debug_offsets[] = {
134 0x4000 /* PHB 0 DEBUG */,
135 0x5000 /* PHB 1 DEBUG */,
136 0x6000 /* PHB 2 DEBUG */,
137 0x7000 /* PHB 3 DEBUG */
138};
139
140/*
141 * STUFF register for each debug PHB,
142 * byte 1 = start bus number, byte 2 = end bus number
143 */
144
145#define PHB_DEBUG_STUFF_OFFSET 0x0020
146
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100147#define EMERGENCY_PAGES 32 /* = 128KB */
148
Jon Masone4650582006-06-26 13:58:14 +0200149unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
150static int translate_empty_slots __read_mostly = 0;
151static int calgary_detected __read_mostly = 0;
152
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100153static struct rio_table_hdr *rio_table_hdr __initdata;
154static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100155static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100156
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200157struct calgary_bus_info {
158 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200159 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200160 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100161 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200162};
163
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200164static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
165static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200166static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200167static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200168static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200169static void calioc2_dump_error_regs(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200170
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200171static struct cal_chipset_ops calgary_chip_ops = {
172 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200173 .tce_cache_blast = calgary_tce_cache_blast,
174 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200175};
176
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200177static struct cal_chipset_ops calioc2_chip_ops = {
178 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200179 .tce_cache_blast = calioc2_tce_cache_blast,
180 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200181};
182
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200183static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200184
185/* enable this to stress test the chip's TCE cache */
186#ifdef CONFIG_IOMMU_DEBUG
Adrian Bunked652602008-01-30 13:30:31 +0100187static int debugging = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200188
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200189static inline unsigned long verify_bit_range(unsigned long* bitmap,
190 int expected, unsigned long start, unsigned long end)
191{
192 unsigned long idx = start;
193
194 BUG_ON(start >= end);
195
196 while (idx < end) {
197 if (!!test_bit(idx, bitmap) != expected)
198 return idx;
199 ++idx;
200 }
201
202 /* all bits have the expected value */
203 return ~0UL;
204}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200205#else /* debugging is disabled */
Adrian Bunked652602008-01-30 13:30:31 +0100206static int debugging;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200207
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200208static inline unsigned long verify_bit_range(unsigned long* bitmap,
209 int expected, unsigned long start, unsigned long end)
210{
211 return ~0UL;
212}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200213
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200214#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200215
216static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
217{
218 unsigned int npages;
219
220 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
221 npages >>= PAGE_SHIFT;
222
223 return npages;
224}
225
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200226static inline int translation_enabled(struct iommu_table *tbl)
227{
228 /* only PHBs with translation enabled have an IOMMU table */
229 return (tbl != NULL);
230}
231
Jon Masone4650582006-06-26 13:58:14 +0200232static void iommu_range_reserve(struct iommu_table *tbl,
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200233 unsigned long start_addr, unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200234{
235 unsigned long index;
236 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200237 unsigned long badbit;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200238 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200239
240 index = start_addr >> PAGE_SHIFT;
241
242 /* bail out if we're asked to reserve a region we don't cover */
243 if (index >= tbl->it_size)
244 return;
245
246 end = index + npages;
247 if (end > tbl->it_size) /* don't go off the table */
248 end = tbl->it_size;
249
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200250 spin_lock_irqsave(&tbl->it_lock, flags);
251
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200252 badbit = verify_bit_range(tbl->it_map, 0, index, end);
253 if (badbit != ~0UL) {
254 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200255 printk(KERN_ERR "Calgary: entry already allocated at "
256 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200257 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200258 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200259
260 set_bit_string(tbl->it_map, index, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200261
262 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200263}
264
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800265static unsigned long iommu_range_alloc(struct device *dev,
266 struct iommu_table *tbl,
267 unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200268{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200269 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200270 unsigned long offset;
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800271 unsigned long boundary_size;
272
273 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
274 PAGE_SIZE) >> PAGE_SHIFT;
Jon Masone4650582006-06-26 13:58:14 +0200275
276 BUG_ON(npages == 0);
277
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200278 spin_lock_irqsave(&tbl->it_lock, flags);
279
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800280 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint,
281 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200282 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200283 tbl->chip_ops->tce_cache_blast(tbl);
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800284
285 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0,
286 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200287 if (offset == ~0UL) {
288 printk(KERN_WARNING "Calgary: IOMMU full.\n");
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200289 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200290 if (panic_on_overflow)
291 panic("Calgary: fix the allocator.\n");
292 else
293 return bad_dma_address;
294 }
295 }
296
Jon Masone4650582006-06-26 13:58:14 +0200297 tbl->it_hint = offset + npages;
298 BUG_ON(tbl->it_hint > tbl->it_size);
299
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200300 spin_unlock_irqrestore(&tbl->it_lock, flags);
301
Jon Masone4650582006-06-26 13:58:14 +0200302 return offset;
303}
304
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800305static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
306 void *vaddr, unsigned int npages, int direction)
Jon Masone4650582006-06-26 13:58:14 +0200307{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200308 unsigned long entry;
Jon Masone4650582006-06-26 13:58:14 +0200309 dma_addr_t ret = bad_dma_address;
310
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800311 entry = iommu_range_alloc(dev, tbl, npages);
Jon Masone4650582006-06-26 13:58:14 +0200312
313 if (unlikely(entry == bad_dma_address))
314 goto error;
315
316 /* set the return dma address */
317 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
318
319 /* put the TCEs in the HW table */
320 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
321 direction);
322
Jon Masone4650582006-06-26 13:58:14 +0200323 return ret;
324
325error:
Jon Masone4650582006-06-26 13:58:14 +0200326 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
327 "iommu %p\n", npages, tbl);
328 return bad_dma_address;
329}
330
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200331static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
Jon Masone4650582006-06-26 13:58:14 +0200332 unsigned int npages)
333{
334 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200335 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100336 unsigned long badend;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200337 unsigned long flags;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100338
339 /* were we called with bad_dma_address? */
340 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
341 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
342 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
343 "address 0x%Lx\n", dma_addr);
344 WARN_ON(1);
345 return;
346 }
Jon Masone4650582006-06-26 13:58:14 +0200347
348 entry = dma_addr >> PAGE_SHIFT;
349
350 BUG_ON(entry + npages > tbl->it_size);
351
352 tce_free(tbl, entry, npages);
353
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200354 spin_lock_irqsave(&tbl->it_lock, flags);
355
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200356 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
357 if (badbit != ~0UL) {
358 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200359 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
360 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200361 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200362 }
363
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800364 iommu_area_free(tbl->it_map, entry, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200365
366 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200367}
368
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200369static inline struct iommu_table *find_iommu_table(struct device *dev)
370{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200371 struct pci_dev *pdev;
372 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200373 struct iommu_table *tbl;
374
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200375 pdev = to_pci_dev(dev);
376
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200377 pbus = pdev->bus;
378
379 /* is the device behind a bridge? Look for the root bus */
380 while (pbus->parent)
381 pbus = pbus->parent;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200382
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300383 tbl = pci_iommu(pbus);
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200384
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200385 BUG_ON(tbl && (tbl->it_busno != pbus->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200386
387 return tbl;
388}
389
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200390static void calgary_unmap_sg(struct device *dev,
Jon Masone4650582006-06-26 13:58:14 +0200391 struct scatterlist *sglist, int nelems, int direction)
392{
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200393 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200394 struct scatterlist *s;
395 int i;
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200396
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +0200397 if (!translation_enabled(tbl))
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200398 return;
399
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200400 for_each_sg(sglist, s, nelems, i) {
Jon Masone4650582006-06-26 13:58:14 +0200401 unsigned int npages;
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200402 dma_addr_t dma = s->dma_address;
403 unsigned int dmalen = s->dma_length;
Jon Masone4650582006-06-26 13:58:14 +0200404
405 if (dmalen == 0)
406 break;
407
408 npages = num_dma_pages(dma, dmalen);
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200409 iommu_free(tbl, dma, npages);
Jon Masone4650582006-06-26 13:58:14 +0200410 }
411}
412
Jon Masone4650582006-06-26 13:58:14 +0200413static int calgary_nontranslate_map_sg(struct device* dev,
414 struct scatterlist *sg, int nelems, int direction)
415{
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200416 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200417 int i;
418
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200419 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200420 struct page *p = sg_page(s);
421
422 BUG_ON(!p);
423 s->dma_address = virt_to_bus(sg_virt(s));
Jon Masone4650582006-06-26 13:58:14 +0200424 s->dma_length = s->length;
425 }
426 return nelems;
427}
428
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200429static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
Jon Masone4650582006-06-26 13:58:14 +0200430 int nelems, int direction)
431{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200432 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200433 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200434 unsigned long vaddr;
435 unsigned int npages;
436 unsigned long entry;
437 int i;
438
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200439 if (!translation_enabled(tbl))
Jon Masone4650582006-06-26 13:58:14 +0200440 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
441
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200442 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200443 BUG_ON(!sg_page(s));
Jon Masone4650582006-06-26 13:58:14 +0200444
Jens Axboe58b053e2007-10-22 20:02:46 +0200445 vaddr = (unsigned long) sg_virt(s);
Jon Masone4650582006-06-26 13:58:14 +0200446 npages = num_dma_pages(vaddr, s->length);
447
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800448 entry = iommu_range_alloc(dev, tbl, npages);
Jon Masone4650582006-06-26 13:58:14 +0200449 if (entry == bad_dma_address) {
450 /* makes sure unmap knows to stop */
451 s->dma_length = 0;
452 goto error;
453 }
454
455 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
456
457 /* insert into HW table */
458 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
459 direction);
460
461 s->dma_length = s->length;
462 }
463
Jon Masone4650582006-06-26 13:58:14 +0200464 return nelems;
465error:
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200466 calgary_unmap_sg(dev, sg, nelems, direction);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200467 for_each_sg(sg, s, nelems, i) {
468 sg->dma_address = bad_dma_address;
469 sg->dma_length = 0;
Jon Masone4650582006-06-26 13:58:14 +0200470 }
Jon Masone4650582006-06-26 13:58:14 +0200471 return 0;
472}
473
Ingo Molnar2be62142008-04-19 19:19:56 +0200474static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
Jon Masone4650582006-06-26 13:58:14 +0200475 size_t size, int direction)
476{
477 dma_addr_t dma_handle = bad_dma_address;
Ingo Molnar2be62142008-04-19 19:19:56 +0200478 void *vaddr = phys_to_virt(paddr);
Jon Masone4650582006-06-26 13:58:14 +0200479 unsigned long uaddr;
480 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200481 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200482
483 uaddr = (unsigned long)vaddr;
484 npages = num_dma_pages(uaddr, size);
485
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200486 if (translation_enabled(tbl))
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800487 dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction);
Jon Masone4650582006-06-26 13:58:14 +0200488 else
489 dma_handle = virt_to_bus(vaddr);
490
491 return dma_handle;
492}
493
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200494static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
Jon Masone4650582006-06-26 13:58:14 +0200495 size_t size, int direction)
496{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200497 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200498 unsigned int npages;
499
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200500 if (!translation_enabled(tbl))
Jon Masone4650582006-06-26 13:58:14 +0200501 return;
502
503 npages = num_dma_pages(dma_handle, size);
504 iommu_free(tbl, dma_handle, npages);
505}
506
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200507static void* calgary_alloc_coherent(struct device *dev, size_t size,
Jon Masone4650582006-06-26 13:58:14 +0200508 dma_addr_t *dma_handle, gfp_t flag)
509{
510 void *ret = NULL;
511 dma_addr_t mapping;
512 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200513 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200514
515 size = PAGE_ALIGN(size); /* size rounded up to full pages */
516 npages = size >> PAGE_SHIFT;
517 order = get_order(size);
518
519 /* alloc enough pages (and possibly more) */
520 ret = (void *)__get_free_pages(flag, order);
521 if (!ret)
522 goto error;
523 memset(ret, 0, size);
524
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200525 if (translation_enabled(tbl)) {
Jon Masone4650582006-06-26 13:58:14 +0200526 /* set up tces to cover the allocated range */
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800527 mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
Jon Masone4650582006-06-26 13:58:14 +0200528 if (mapping == bad_dma_address)
529 goto free;
530
531 *dma_handle = mapping;
532 } else /* non translated slot */
533 *dma_handle = virt_to_bus(ret);
534
535 return ret;
536
537free:
538 free_pages((unsigned long)ret, get_order(size));
539 ret = NULL;
540error:
541 return ret;
542}
543
Stephen Hemmingere6584502007-05-02 19:27:06 +0200544static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200545 .alloc_coherent = calgary_alloc_coherent,
546 .map_single = calgary_map_single,
547 .unmap_single = calgary_unmap_single,
548 .map_sg = calgary_map_sg,
549 .unmap_sg = calgary_unmap_sg,
550};
551
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100552static inline void __iomem * busno_to_bbar(unsigned char num)
553{
554 return bus_info[num].bbar;
555}
556
Jon Masone4650582006-06-26 13:58:14 +0200557static inline int busno_to_phbid(unsigned char num)
558{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200559 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200560}
561
562static inline unsigned long split_queue_offset(unsigned char num)
563{
564 size_t idx = busno_to_phbid(num);
565
566 return split_queue_offsets[idx];
567}
568
569static inline unsigned long tar_offset(unsigned char num)
570{
571 size_t idx = busno_to_phbid(num);
572
573 return tar_offsets[idx];
574}
575
576static inline unsigned long phb_offset(unsigned char num)
577{
578 size_t idx = busno_to_phbid(num);
579
580 return phb_offsets[idx];
581}
582
583static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
584{
585 unsigned long target = ((unsigned long)bar) | offset;
586 return (void __iomem*)target;
587}
588
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200589static inline int is_calioc2(unsigned short device)
590{
591 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
592}
593
594static inline int is_calgary(unsigned short device)
595{
596 return (device == PCI_DEVICE_ID_IBM_CALGARY);
597}
598
599static inline int is_cal_pci_dev(unsigned short device)
600{
601 return (is_calgary(device) || is_calioc2(device));
602}
603
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200604static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200605{
606 u64 val;
607 u32 aer;
608 int i = 0;
609 void __iomem *bbar = tbl->bbar;
610 void __iomem *target;
611
612 /* disable arbitration on the bus */
613 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
614 aer = readl(target);
615 writel(0, target);
616
617 /* read plssr to ensure it got there */
618 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
619 val = readl(target);
620
621 /* poll split queues until all DMA activity is done */
622 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
623 do {
624 val = readq(target);
625 i++;
626 } while ((val & 0xff) != 0xff && i < 100);
627 if (i == 100)
628 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
629 "continuing anyway\n");
630
631 /* invalidate TCE cache */
632 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
633 writeq(tbl->tar_val, target);
634
635 /* enable arbitration */
636 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
637 writel(aer, target);
638 (void)readl(target); /* flush */
639}
640
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200641static void calioc2_tce_cache_blast(struct iommu_table *tbl)
642{
643 void __iomem *bbar = tbl->bbar;
644 void __iomem *target;
645 u64 val64;
646 u32 val;
647 int i = 0;
648 int count = 1;
649 unsigned char bus = tbl->it_busno;
650
651begin:
652 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
653 "sequence - count %d\n", bus, count);
654
655 /* 1. using the Page Migration Control reg set SoftStop */
656 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
657 val = be32_to_cpu(readl(target));
658 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
659 val |= PMR_SOFTSTOP;
660 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
661 writel(cpu_to_be32(val), target);
662
663 /* 2. poll split queues until all DMA activity is done */
664 printk(KERN_DEBUG "2a. starting to poll split queues\n");
665 target = calgary_reg(bbar, split_queue_offset(bus));
666 do {
667 val64 = readq(target);
668 i++;
669 } while ((val64 & 0xff) != 0xff && i < 100);
670 if (i == 100)
671 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
672 "continuing anyway\n");
673
674 /* 3. poll Page Migration DEBUG for SoftStopFault */
675 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
676 val = be32_to_cpu(readl(target));
677 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
678
679 /* 4. if SoftStopFault - goto (1) */
680 if (val & PMR_SOFTSTOPFAULT) {
681 if (++count < 100)
682 goto begin;
683 else {
684 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
685 "aborting TCE cache flush sequence!\n");
686 return; /* pray for the best */
687 }
688 }
689
690 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
691 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
692 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
693 val = be32_to_cpu(readl(target));
694 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
695 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
696 val = be32_to_cpu(readl(target));
697 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
698
699 /* 6. invalidate TCE cache */
700 printk(KERN_DEBUG "6. invalidating TCE cache\n");
701 target = calgary_reg(bbar, tar_offset(bus));
702 writeq(tbl->tar_val, target);
703
704 /* 7. Re-read PMCR */
705 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
706 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
707 val = be32_to_cpu(readl(target));
708 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
709
710 /* 8. Remove HardStop */
711 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
712 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
713 val = 0;
714 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
715 writel(cpu_to_be32(val), target);
716 val = be32_to_cpu(readl(target));
717 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
718}
719
Jon Masone4650582006-06-26 13:58:14 +0200720static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
721 u64 limit)
722{
723 unsigned int numpages;
724
725 limit = limit | 0xfffff;
726 limit++;
727
728 numpages = ((limit - start) >> PAGE_SHIFT);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300729 iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
Jon Masone4650582006-06-26 13:58:14 +0200730}
731
732static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
733{
734 void __iomem *target;
735 u64 low, high, sizelow;
736 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300737 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200738 unsigned char busnum = dev->bus->number;
739 void __iomem *bbar = tbl->bbar;
740
741 /* peripheral MEM_1 region */
742 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
743 low = be32_to_cpu(readl(target));
744 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
745 high = be32_to_cpu(readl(target));
746 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
747 sizelow = be32_to_cpu(readl(target));
748
749 start = (high << 32) | low;
750 limit = sizelow;
751
752 calgary_reserve_mem_region(dev, start, limit);
753}
754
755static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
756{
757 void __iomem *target;
758 u32 val32;
759 u64 low, high, sizelow, sizehigh;
760 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300761 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200762 unsigned char busnum = dev->bus->number;
763 void __iomem *bbar = tbl->bbar;
764
765 /* is it enabled? */
766 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
767 val32 = be32_to_cpu(readl(target));
768 if (!(val32 & PHB_MEM2_ENABLE))
769 return;
770
771 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
772 low = be32_to_cpu(readl(target));
773 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
774 high = be32_to_cpu(readl(target));
775 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
776 sizelow = be32_to_cpu(readl(target));
777 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
778 sizehigh = be32_to_cpu(readl(target));
779
780 start = (high << 32) | low;
781 limit = (sizehigh << 32) | sizelow;
782
783 calgary_reserve_mem_region(dev, start, limit);
784}
785
786/*
787 * some regions of the IO address space do not get translated, so we
788 * must not give devices IO addresses in those regions. The regions
789 * are the 640KB-1MB region and the two PCI peripheral memory holes.
790 * Reserve all of them in the IOMMU bitmap to avoid giving them out
791 * later.
792 */
793static void __init calgary_reserve_regions(struct pci_dev *dev)
794{
795 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200796 u64 start;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300797 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200798
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100799 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
800 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200801
802 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200803 /* for CalIOC2 - avoid the entire first MB */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200804 if (is_calgary(dev->device)) {
805 start = (640 * 1024);
806 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
807 } else { /* calioc2 */
808 start = 0;
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200809 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200810 }
Jon Masone4650582006-06-26 13:58:14 +0200811 iommu_range_reserve(tbl, start, npages);
812
813 /* reserve the two PCI peripheral memory regions in IO space */
814 calgary_reserve_peripheral_mem_1(dev);
815 calgary_reserve_peripheral_mem_2(dev);
816}
817
818static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
819{
820 u64 val64;
821 u64 table_phys;
822 void __iomem *target;
823 int ret;
824 struct iommu_table *tbl;
825
826 /* build TCE tables for each PHB */
827 ret = build_tce_table(dev, bbar);
828 if (ret)
829 return ret;
830
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300831 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200832 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
833 tce_free(tbl, 0, tbl->it_size);
834
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200835 if (is_calgary(dev->device))
836 tbl->chip_ops = &calgary_chip_ops;
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200837 else if (is_calioc2(dev->device))
838 tbl->chip_ops = &calioc2_chip_ops;
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200839 else
840 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200841
Jon Masone4650582006-06-26 13:58:14 +0200842 calgary_reserve_regions(dev);
843
844 /* set TARs for each PHB */
845 target = calgary_reg(bbar, tar_offset(dev->bus->number));
846 val64 = be64_to_cpu(readq(target));
847
848 /* zero out all TAR bits under sw control */
849 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200850 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200851
Jon Masone4650582006-06-26 13:58:14 +0200852 val64 |= table_phys;
853
854 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
855 val64 |= (u64) specified_table_size;
856
857 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200858
Jon Masone4650582006-06-26 13:58:14 +0200859 writeq(tbl->tar_val, target);
860 readq(target); /* flush */
861
862 return 0;
863}
864
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200865static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200866{
867 u64 val64;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300868 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200869 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200870 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200871
872 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
873 val64 = be64_to_cpu(readq(target));
874 val64 &= ~TAR_SW_BITS;
875 writeq(cpu_to_be64(val64), target);
876 readq(target); /* flush */
877
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200878 bitmapsz = tbl->it_size / BITS_PER_BYTE;
879 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
880 tbl->it_map = NULL;
881
Jon Masone4650582006-06-26 13:58:14 +0200882 kfree(tbl);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300883
884 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200885
886 /* Can't free bootmem allocated memory after system is up :-( */
887 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200888}
889
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200890static void calgary_dump_error_regs(struct iommu_table *tbl)
891{
892 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200893 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200894 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200895
896 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200897 csr = be32_to_cpu(readl(target));
898
899 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
900 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200901
902 /* If no error, the agent ID in the CSR is not valid */
903 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200904 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200905}
906
907static void calioc2_dump_error_regs(struct iommu_table *tbl)
908{
909 void __iomem *bbar = tbl->bbar;
910 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200911 void __iomem *target;
912 unsigned long phboff = phb_offset(tbl->it_busno);
913 unsigned long erroff;
914 u32 errregs[7];
915 int i;
916
917 /* dump CSR */
918 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
919 csr = be32_to_cpu(readl(target));
920 /* dump PLSSR */
921 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
922 plssr = be32_to_cpu(readl(target));
923 /* dump CSMR */
924 target = calgary_reg(bbar, phboff | 0x290);
925 csmr = be32_to_cpu(readl(target));
926 /* dump mck */
927 target = calgary_reg(bbar, phboff | 0x800);
928 mck = be32_to_cpu(readl(target));
929
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200930 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
931 tbl->it_busno);
932
933 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
934 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200935
936 /* dump rest of error regs */
937 printk(KERN_EMERG "Calgary: ");
938 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200939 /* err regs are at 0x810 - 0x870 */
940 erroff = (0x810 + (i * 0x10));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200941 target = calgary_reg(bbar, phboff | erroff);
942 errregs[i] = be32_to_cpu(readl(target));
943 printk("0x%08x@0x%lx ", errregs[i], erroff);
944 }
945 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200946
947 /* root complex status */
948 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
949 rcstat = be32_to_cpu(readl(target));
950 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
951 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200952}
953
Jon Masone4650582006-06-26 13:58:14 +0200954static void calgary_watchdog(unsigned long data)
955{
956 struct pci_dev *dev = (struct pci_dev *)data;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300957 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200958 void __iomem *bbar = tbl->bbar;
959 u32 val32;
960 void __iomem *target;
961
962 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
963 val32 = be32_to_cpu(readl(target));
964
965 /* If no error, the agent ID in the CSR is not valid */
966 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200967 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200968
969 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200970 writel(0, target);
971
972 /* Disable bus that caused the error */
973 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200974 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200975 val32 = be32_to_cpu(readl(target));
976 val32 |= PHB_SLOT_DISABLE;
977 writel(cpu_to_be32(val32), target);
978 readl(target); /* flush */
979 } else {
980 /* Reset the timer */
981 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
982 }
983}
984
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200985static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
986 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200987{
988 u64 val64;
989 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100990 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200991 u64 mask;
992
993 switch (busno_to_phbid(busnum)) {
994 case 0: phb_shift = (63 - 19);
995 break;
996 case 1: phb_shift = (63 - 23);
997 break;
998 case 2: phb_shift = (63 - 27);
999 break;
1000 case 3: phb_shift = (63 - 35);
1001 break;
1002 default:
1003 BUG_ON(busno_to_phbid(busnum));
1004 }
1005
1006 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1007 val64 = be64_to_cpu(readq(target));
1008
1009 /* zero out this PHB's timer bits */
1010 mask = ~(0xFUL << phb_shift);
1011 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001012 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001013 writeq(cpu_to_be64(val64), target);
1014 readq(target); /* flush */
1015}
1016
Sam Ravnborg31f3dff2008-02-01 17:49:42 +01001017static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001018{
1019 unsigned char busnum = dev->bus->number;
1020 void __iomem *bbar = tbl->bbar;
1021 void __iomem *target;
1022 u32 val;
1023
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +02001024 /*
1025 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1026 */
1027 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1028 val = cpu_to_be32(readl(target));
1029 val |= 0x00800000;
1030 writel(cpu_to_be32(val), target);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001031}
1032
Sam Ravnborg31f3dff2008-02-01 17:49:42 +01001033static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001034{
1035 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001036
1037 /*
1038 * Give split completion a longer timeout on bus 1 for aic94xx
1039 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1040 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001041 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001042 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1043 CCR_2SEC_TIMEOUT);
1044}
1045
Jon Masone4650582006-06-26 13:58:14 +02001046static void __init calgary_enable_translation(struct pci_dev *dev)
1047{
1048 u32 val32;
1049 unsigned char busnum;
1050 void __iomem *target;
1051 void __iomem *bbar;
1052 struct iommu_table *tbl;
1053
1054 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001055 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001056 bbar = tbl->bbar;
1057
1058 /* enable TCE in PHB Config Register */
1059 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1060 val32 = be32_to_cpu(readl(target));
1061 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1062
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001063 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1064 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1065 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001066 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1067 "bus.\n");
1068
1069 writel(cpu_to_be32(val32), target);
1070 readl(target); /* flush */
1071
1072 init_timer(&tbl->watchdog_timer);
1073 tbl->watchdog_timer.function = &calgary_watchdog;
1074 tbl->watchdog_timer.data = (unsigned long)dev;
1075 mod_timer(&tbl->watchdog_timer, jiffies);
1076}
1077
1078static void __init calgary_disable_translation(struct pci_dev *dev)
1079{
1080 u32 val32;
1081 unsigned char busnum;
1082 void __iomem *target;
1083 void __iomem *bbar;
1084 struct iommu_table *tbl;
1085
1086 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001087 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001088 bbar = tbl->bbar;
1089
1090 /* disable TCE in PHB Config Register */
1091 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1092 val32 = be32_to_cpu(readl(target));
1093 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1094
Jon Mason70d666d2006-10-05 18:47:21 +02001095 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001096 writel(cpu_to_be32(val32), target);
1097 readl(target); /* flush */
1098
1099 del_timer_sync(&tbl->watchdog_timer);
1100}
1101
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001102static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001103{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001104 pci_dev_get(dev);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001105 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001106
1107 /* is the device behind a bridge? */
1108 if (dev->bus->parent)
1109 dev->bus->parent->self = dev;
1110 else
1111 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001112}
1113
1114static int __init calgary_init_one(struct pci_dev *dev)
1115{
Jon Masone4650582006-06-26 13:58:14 +02001116 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001117 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001118 int ret;
1119
Jon Masondedc9932006-10-05 18:47:21 +02001120 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1121
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001122 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001123 ret = calgary_setup_tar(dev, bbar);
1124 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001125 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001126
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001127 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001128
1129 if (dev->bus->parent) {
1130 if (dev->bus->parent->self)
1131 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1132 "bus->parent->self!\n", dev);
1133 dev->bus->parent->self = dev;
1134 } else
1135 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001136
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001137 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001138 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001139
Jon Masone4650582006-06-26 13:58:14 +02001140 calgary_enable_translation(dev);
1141
1142 return 0;
1143
Jon Masone4650582006-06-26 13:58:14 +02001144done:
1145 return ret;
1146}
1147
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001148static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001149{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001150 int ret;
1151 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001152 void __iomem *bbar;
1153 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001154 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001155 u8 start_bus, end_bus;
1156 u32 val;
1157
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001158 ret = -ENODATA;
1159 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1160 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001161
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001162 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001163 continue;
1164
1165 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001166 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1167 if (!bbar)
1168 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001169
1170 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001171 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1172 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001173
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001174 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001175
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001176 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001177 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001178
1179 if (end_bus) {
1180 for (bus = start_bus; bus <= end_bus; bus++) {
1181 bus_info[bus].bbar = bbar;
1182 bus_info[bus].phbid = phb;
1183 }
1184 } else {
1185 bus_info[start_bus].bbar = bbar;
1186 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001187 }
1188 }
1189 }
1190
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001191 return 0;
1192
1193error:
1194 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1195 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1196 if (bus_info[bus].bbar)
1197 iounmap(bus_info[bus].bbar);
1198
1199 return ret;
1200}
1201
1202static int __init calgary_init(void)
1203{
1204 int ret;
1205 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001206 struct calgary_bus_info *info;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001207
1208 ret = calgary_locate_bbars();
1209 if (ret)
1210 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001211
Jon Masondedc9932006-10-05 18:47:21 +02001212 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001213 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001214 if (!dev)
1215 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001216 if (!is_cal_pci_dev(dev->device))
1217 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001218
1219 info = &bus_info[dev->bus->number];
1220 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001221 calgary_init_one_nontraslated(dev);
1222 continue;
1223 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001224
1225 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001226 continue;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001227
Jon Masone4650582006-06-26 13:58:14 +02001228 ret = calgary_init_one(dev);
1229 if (ret)
1230 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001231 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001232
1233 return ret;
1234
1235error:
Jon Masondedc9932006-10-05 18:47:21 +02001236 do {
Greg Kroah-Hartmana2b5d872008-02-13 09:32:03 -08001237 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001238 if (!dev)
1239 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001240 if (!is_cal_pci_dev(dev->device))
1241 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001242
1243 info = &bus_info[dev->bus->number];
1244 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001245 pci_dev_put(dev);
1246 continue;
1247 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001248 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001249 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001250
Jon Masone4650582006-06-26 13:58:14 +02001251 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001252 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001253 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001254 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001255
1256 return ret;
1257}
1258
1259static inline int __init determine_tce_table_size(u64 ram)
1260{
1261 int ret;
1262
1263 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1264 return specified_table_size;
1265
1266 /*
1267 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1268 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1269 * larger table size has twice as many entries, so shift the
1270 * max ram address by 13 to divide by 8K and then look at the
1271 * order of the result to choose between 0-7.
1272 */
1273 ret = get_order(ram >> 13);
1274 if (ret > TCE_TABLE_SIZE_8M)
1275 ret = TCE_TABLE_SIZE_8M;
1276
1277 return ret;
1278}
1279
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001280static int __init build_detail_arrays(void)
1281{
1282 unsigned long ptr;
1283 int i, scal_detail_size, rio_detail_size;
1284
1285 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1286 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001287 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001288 "but system has %d nodes.\n",
1289 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1290 return -ENODEV;
1291 }
1292
1293 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001294 case 2:
1295 scal_detail_size = 11;
1296 rio_detail_size = 13;
1297 break;
1298 case 3:
1299 scal_detail_size = 12;
1300 rio_detail_size = 15;
1301 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001302 default:
1303 printk(KERN_WARNING
1304 "Calgary: Invalid Rio Grande Table Version: %d\n",
1305 rio_table_hdr->version);
1306 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001307 }
1308
1309 ptr = ((unsigned long)rio_table_hdr) + 3;
1310 for (i = 0; i < rio_table_hdr->num_scal_dev;
1311 i++, ptr += scal_detail_size)
1312 scal_devs[i] = (struct scal_detail *)ptr;
1313
1314 for (i = 0; i < rio_table_hdr->num_rio_dev;
1315 i++, ptr += rio_detail_size)
1316 rio_devs[i] = (struct rio_detail *)ptr;
1317
1318 return 0;
1319}
1320
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001321static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1322{
1323 int dev;
1324 u32 val;
1325
1326 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1327 /*
1328 * FIXME: properly scan for devices accross the
1329 * PCI-to-PCI bridge on every CalIOC2 port.
1330 */
1331 return 1;
1332 }
1333
1334 for (dev = 1; dev < 8; dev++) {
1335 val = read_pci_config(bus, dev, 0, 0);
1336 if (val != 0xffffffff)
1337 break;
1338 }
1339 return (val != 0xffffffff);
1340}
1341
Jon Masone4650582006-06-26 13:58:14 +02001342void __init detect_calgary(void)
1343{
Jon Masond2105b12006-07-29 21:42:43 +02001344 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001345 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001346 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001347 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001348 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001349 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001350
1351 /*
1352 * if the user specified iommu=off or iommu=soft or we found
1353 * another HW IOMMU already, bail out.
1354 */
1355 if (swiotlb || no_iommu || iommu_detected)
1356 return;
1357
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001358 if (!use_calgary)
1359 return;
1360
Andi Kleen0637a702006-09-26 10:52:41 +02001361 if (!early_pci_allowed())
1362 return;
1363
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001364 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1365
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001366 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1367
1368 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001369 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001370 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001371 /*
1372 * The next offset is stored in the 1st word.
1373 * Only parse up until the offset increases:
1374 */
1375 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001376 /* The block id is stored in the 2nd word */
1377 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1378 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001379 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001380 break;
1381 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001382 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001383 offset = *((unsigned short *)(ptr + offset));
1384 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001385 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001386 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1387 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001388 return;
1389 }
1390
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001391 ret = build_detail_arrays();
1392 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001393 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001394 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001395 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001396
Yinghai Luc987d122008-06-24 22:14:09 -07001397 specified_table_size = determine_tce_table_size(max_pfn * PAGE_SIZE);
Jon Masone4650582006-06-26 13:58:14 +02001398
Jon Masond2105b12006-07-29 21:42:43 +02001399 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001400 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001401 unsigned short pci_device;
1402 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001403
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001404 val = read_pci_config(bus, 0, 0, 0);
1405 pci_device = (val & 0xFFFF0000) >> 16;
1406
1407 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001408 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001409
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001410 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001411 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001412
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001413 if (calgary_bus_has_devices(bus, pci_device) ||
1414 translate_empty_slots) {
1415 tbl = alloc_tce_table();
1416 if (!tbl)
1417 goto cleanup;
1418 info->tce_space = tbl;
1419 calgary_found = 1;
Jon Masond2105b12006-07-29 21:42:43 +02001420 }
Jon Masone4650582006-06-26 13:58:14 +02001421 }
1422
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001423 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1424 calgary_found ? "found" : "not found");
1425
Jon Masond2105b12006-07-29 21:42:43 +02001426 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001427 iommu_detected = 1;
1428 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001429 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1430 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1431 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1432 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001433 }
1434 return;
1435
1436cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001437 for (--bus; bus >= 0; --bus) {
1438 struct calgary_bus_info *info = &bus_info[bus];
1439
1440 if (info->tce_space)
1441 free_tce_table(info->tce_space);
1442 }
Jon Masone4650582006-06-26 13:58:14 +02001443}
1444
1445int __init calgary_iommu_init(void)
1446{
1447 int ret;
1448
1449 if (no_iommu || swiotlb)
1450 return -ENODEV;
1451
1452 if (!calgary_detected)
1453 return -ENODEV;
1454
1455 /* ok, we're trying to use Calgary - let's roll */
1456 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1457
1458 ret = calgary_init();
1459 if (ret) {
1460 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1461 "falling back to no_iommu\n", ret);
Yinghai Luc987d122008-06-24 22:14:09 -07001462 if (max_pfn > MAX_DMA32_PFN)
Jon Masone4650582006-06-26 13:58:14 +02001463 printk(KERN_ERR "WARNING more than 4GB of memory, "
1464 "32bit PCI may malfunction.\n");
1465 return ret;
1466 }
1467
1468 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001469 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001470 dma_ops = &calgary_dma_ops;
1471
1472 return 0;
1473}
1474
1475static int __init calgary_parse_options(char *p)
1476{
1477 unsigned int bridge;
1478 size_t len;
1479 char* endp;
1480
1481 while (*p) {
1482 if (!strncmp(p, "64k", 3))
1483 specified_table_size = TCE_TABLE_SIZE_64K;
1484 else if (!strncmp(p, "128k", 4))
1485 specified_table_size = TCE_TABLE_SIZE_128K;
1486 else if (!strncmp(p, "256k", 4))
1487 specified_table_size = TCE_TABLE_SIZE_256K;
1488 else if (!strncmp(p, "512k", 4))
1489 specified_table_size = TCE_TABLE_SIZE_512K;
1490 else if (!strncmp(p, "1M", 2))
1491 specified_table_size = TCE_TABLE_SIZE_1M;
1492 else if (!strncmp(p, "2M", 2))
1493 specified_table_size = TCE_TABLE_SIZE_2M;
1494 else if (!strncmp(p, "4M", 2))
1495 specified_table_size = TCE_TABLE_SIZE_4M;
1496 else if (!strncmp(p, "8M", 2))
1497 specified_table_size = TCE_TABLE_SIZE_8M;
1498
1499 len = strlen("translate_empty_slots");
1500 if (!strncmp(p, "translate_empty_slots", len))
1501 translate_empty_slots = 1;
1502
1503 len = strlen("disable");
1504 if (!strncmp(p, "disable", len)) {
1505 p += len;
1506 if (*p == '=')
1507 ++p;
1508 if (*p == '\0')
1509 break;
1510 bridge = simple_strtol(p, &endp, 0);
1511 if (p == endp)
1512 break;
1513
Jon Masond2105b12006-07-29 21:42:43 +02001514 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001515 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001516 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001517 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001518 }
1519 }
1520
1521 p = strpbrk(p, ",");
1522 if (!p)
1523 break;
1524
1525 p++; /* skip ',' */
1526 }
1527 return 1;
1528}
1529__setup("calgary=", calgary_parse_options);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001530
1531static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1532{
1533 struct iommu_table *tbl;
1534 unsigned int npages;
1535 int i;
1536
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001537 tbl = pci_iommu(dev->bus);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001538
1539 for (i = 0; i < 4; i++) {
1540 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1541
1542 /* Don't give out TCEs that map MEM resources */
1543 if (!(r->flags & IORESOURCE_MEM))
1544 continue;
1545
1546 /* 0-based? we reserve the whole 1st MB anyway */
1547 if (!r->start)
1548 continue;
1549
1550 /* cover the whole region */
1551 npages = (r->end - r->start) >> PAGE_SHIFT;
1552 npages++;
1553
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001554 iommu_range_reserve(tbl, r->start, npages);
1555 }
1556}
1557
1558static int __init calgary_fixup_tce_spaces(void)
1559{
1560 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001561 struct calgary_bus_info *info;
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001562
1563 if (no_iommu || swiotlb || !calgary_detected)
1564 return -ENODEV;
1565
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001566 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001567
1568 do {
1569 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1570 if (!dev)
1571 break;
1572 if (!is_cal_pci_dev(dev->device))
1573 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001574
1575 info = &bus_info[dev->bus->number];
1576 if (info->translation_disabled)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001577 continue;
1578
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001579 if (!info->tce_space)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001580 continue;
1581
1582 calgary_fixup_one_tce_space(dev);
1583
1584 } while (1);
1585
1586 return 0;
1587}
1588
1589/*
1590 * We need to be call after pcibios_assign_resources (fs_initcall level)
1591 * and before device_initcall.
1592 */
1593rootfs_initcall(calgary_fixup_tce_spaces);