blob: f5828db15220aab13e4a5ba7c8c698d08f659a61 [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>
33#include <linux/init.h>
34#include <linux/bitops.h>
35#include <linux/pci_ids.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <asm/proto.h>
39#include <asm/calgary.h>
40#include <asm/tce.h>
41#include <asm/pci-direct.h>
42#include <asm/system.h>
43#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010044#include <asm/rio.h>
Jon Masone4650582006-06-26 13:58:14 +020045
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010046#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
47int use_calgary __read_mostly = 1;
48#else
49int use_calgary __read_mostly = 0;
50#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
51
Jon Masone4650582006-06-26 13:58:14 +020052#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020053#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020054
55/* we need these for register space address calculation */
56#define START_ADDRESS 0xfe000000
57#define CHASSIS_BASE 0
58#define ONE_BASED_CHASSIS_NUM 1
59
60/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020061#define CALGARY_CONFIG_REG 0x0108
62#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020063#define PHB_PLSSR_OFFSET 0x0120
64#define PHB_CONFIG_RW_OFFSET 0x0160
65#define PHB_IOBASE_BAR_LOW 0x0170
66#define PHB_IOBASE_BAR_HIGH 0x0180
67#define PHB_MEM_1_LOW 0x0190
68#define PHB_MEM_1_HIGH 0x01A0
69#define PHB_IO_ADDR_SIZE 0x01B0
70#define PHB_MEM_1_SIZE 0x01C0
71#define PHB_MEM_ST_OFFSET 0x01D0
72#define PHB_AER_OFFSET 0x0200
73#define PHB_CONFIG_0_HIGH 0x0220
74#define PHB_CONFIG_0_LOW 0x0230
75#define PHB_CONFIG_0_END 0x0240
76#define PHB_MEM_2_LOW 0x02B0
77#define PHB_MEM_2_HIGH 0x02C0
78#define PHB_MEM_2_SIZE_HIGH 0x02D0
79#define PHB_MEM_2_SIZE_LOW 0x02E0
80#define PHB_DOSHOLE_OFFSET 0x08E0
81
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020082/* CalIOC2 specific */
83#define PHB_SAVIOR_L2 0x0DB0
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +020084#define PHB_PAGE_MIG_CTRL 0x0DA8
85#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020086#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020087
Jon Masone4650582006-06-26 13:58:14 +020088/* PHB_CONFIG_RW */
89#define PHB_TCE_ENABLE 0x20000000
90#define PHB_SLOT_DISABLE 0x1C000000
91#define PHB_DAC_DISABLE 0x01000000
92#define PHB_MEM2_ENABLE 0x00400000
93#define PHB_MCSR_ENABLE 0x00100000
94/* TAR (Table Address Register) */
95#define TAR_SW_BITS 0x0000ffffffff800fUL
96#define TAR_VALID 0x0000000000000008UL
97/* CSR (Channel/DMA Status Register) */
98#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020099/* CCR (Calgary Configuration Register) */
100#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200101/* PMCR/PMDR (Page Migration Control/Debug Registers */
102#define PMR_SOFTSTOP 0x80000000
103#define PMR_SOFTSTOPFAULT 0x40000000
104#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200105
106#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200107#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200108/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
109#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200110#define PHBS_PER_CALGARY 4
111
112/* register offsets in Calgary's internal register space */
113static const unsigned long tar_offsets[] = {
114 0x0580 /* TAR0 */,
115 0x0588 /* TAR1 */,
116 0x0590 /* TAR2 */,
117 0x0598 /* TAR3 */
118};
119
120static const unsigned long split_queue_offsets[] = {
121 0x4870 /* SPLIT QUEUE 0 */,
122 0x5870 /* SPLIT QUEUE 1 */,
123 0x6870 /* SPLIT QUEUE 2 */,
124 0x7870 /* SPLIT QUEUE 3 */
125};
126
127static const unsigned long phb_offsets[] = {
128 0x8000 /* PHB0 */,
129 0x9000 /* PHB1 */,
130 0xA000 /* PHB2 */,
131 0xB000 /* PHB3 */
132};
133
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100134/* PHB debug registers */
135
136static const unsigned long phb_debug_offsets[] = {
137 0x4000 /* PHB 0 DEBUG */,
138 0x5000 /* PHB 1 DEBUG */,
139 0x6000 /* PHB 2 DEBUG */,
140 0x7000 /* PHB 3 DEBUG */
141};
142
143/*
144 * STUFF register for each debug PHB,
145 * byte 1 = start bus number, byte 2 = end bus number
146 */
147
148#define PHB_DEBUG_STUFF_OFFSET 0x0020
149
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100150#define EMERGENCY_PAGES 32 /* = 128KB */
151
Jon Masone4650582006-06-26 13:58:14 +0200152unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
153static int translate_empty_slots __read_mostly = 0;
154static int calgary_detected __read_mostly = 0;
155
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100156static struct rio_table_hdr *rio_table_hdr __initdata;
157static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100158static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100159
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200160struct calgary_bus_info {
161 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200162 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200163 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100164 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200165};
166
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200167static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
168static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200169static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200170static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200171static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200172static void calioc2_dump_error_regs(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200173
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200174static struct cal_chipset_ops calgary_chip_ops = {
175 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200176 .tce_cache_blast = calgary_tce_cache_blast,
177 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200178};
179
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200180static struct cal_chipset_ops calioc2_chip_ops = {
181 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200182 .tce_cache_blast = calioc2_tce_cache_blast,
183 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200184};
185
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200186static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200187
188/* enable this to stress test the chip's TCE cache */
189#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200190int debugging __read_mostly = 1;
191
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200192static inline unsigned long verify_bit_range(unsigned long* bitmap,
193 int expected, unsigned long start, unsigned long end)
194{
195 unsigned long idx = start;
196
197 BUG_ON(start >= end);
198
199 while (idx < end) {
200 if (!!test_bit(idx, bitmap) != expected)
201 return idx;
202 ++idx;
203 }
204
205 /* all bits have the expected value */
206 return ~0UL;
207}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200208#else /* debugging is disabled */
209int debugging __read_mostly = 0;
210
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200211static inline unsigned long verify_bit_range(unsigned long* bitmap,
212 int expected, unsigned long start, unsigned long end)
213{
214 return ~0UL;
215}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200216
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200217#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200218
219static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
220{
221 unsigned int npages;
222
223 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
224 npages >>= PAGE_SHIFT;
225
226 return npages;
227}
228
229static inline int translate_phb(struct pci_dev* dev)
230{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200231 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200232 return !disabled;
233}
234
235static void iommu_range_reserve(struct iommu_table *tbl,
236 unsigned long start_addr, unsigned int npages)
237{
238 unsigned long index;
239 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200240 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200241
242 index = start_addr >> PAGE_SHIFT;
243
244 /* bail out if we're asked to reserve a region we don't cover */
245 if (index >= tbl->it_size)
246 return;
247
248 end = index + npages;
249 if (end > tbl->it_size) /* don't go off the table */
250 end = tbl->it_size;
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);
Jon Masone4650582006-06-26 13:58:14 +0200261}
262
263static unsigned long iommu_range_alloc(struct iommu_table *tbl,
264 unsigned int npages)
265{
266 unsigned long offset;
267
268 BUG_ON(npages == 0);
269
270 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
271 tbl->it_size, npages);
272 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200273 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200274 offset = find_next_zero_string(tbl->it_map, 0,
275 tbl->it_size, npages);
276 if (offset == ~0UL) {
277 printk(KERN_WARNING "Calgary: IOMMU full.\n");
278 if (panic_on_overflow)
279 panic("Calgary: fix the allocator.\n");
280 else
281 return bad_dma_address;
282 }
283 }
284
285 set_bit_string(tbl->it_map, offset, npages);
286 tbl->it_hint = offset + npages;
287 BUG_ON(tbl->it_hint > tbl->it_size);
288
289 return offset;
290}
291
292static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
293 unsigned int npages, int direction)
294{
295 unsigned long entry, flags;
296 dma_addr_t ret = bad_dma_address;
297
298 spin_lock_irqsave(&tbl->it_lock, flags);
299
300 entry = iommu_range_alloc(tbl, npages);
301
302 if (unlikely(entry == bad_dma_address))
303 goto error;
304
305 /* set the return dma address */
306 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
307
308 /* put the TCEs in the HW table */
309 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
310 direction);
311
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
313
314 return ret;
315
316error:
317 spin_unlock_irqrestore(&tbl->it_lock, flags);
318 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
319 "iommu %p\n", npages, tbl);
320 return bad_dma_address;
321}
322
323static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
324 unsigned int npages)
325{
326 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200327 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100328 unsigned long badend;
329
330 /* were we called with bad_dma_address? */
331 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
332 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
333 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
334 "address 0x%Lx\n", dma_addr);
335 WARN_ON(1);
336 return;
337 }
Jon Masone4650582006-06-26 13:58:14 +0200338
339 entry = dma_addr >> PAGE_SHIFT;
340
341 BUG_ON(entry + npages > tbl->it_size);
342
343 tce_free(tbl, entry, npages);
344
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200345 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
346 if (badbit != ~0UL) {
347 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200348 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
349 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200350 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200351 }
352
353 __clear_bit_string(tbl->it_map, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200354}
355
356static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
357 unsigned int npages)
358{
359 unsigned long flags;
360
361 spin_lock_irqsave(&tbl->it_lock, flags);
362
363 __iommu_free(tbl, dma_addr, npages);
364
365 spin_unlock_irqrestore(&tbl->it_lock, flags);
366}
367
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200368static inline struct iommu_table *find_iommu_table(struct device *dev)
369{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200370 struct pci_dev *pdev;
371 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200372 struct iommu_table *tbl;
373
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200374 pdev = to_pci_dev(dev);
375
376 /* is the device behind a bridge? */
377 if (unlikely(pdev->bus->parent))
378 pbus = pdev->bus->parent;
379 else
380 pbus = pdev->bus;
381
382 tbl = pbus->self->sysdata;
383 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200384
385 return tbl;
386}
387
Jon Masone4650582006-06-26 13:58:14 +0200388static void __calgary_unmap_sg(struct iommu_table *tbl,
389 struct scatterlist *sglist, int nelems, int direction)
390{
391 while (nelems--) {
392 unsigned int npages;
393 dma_addr_t dma = sglist->dma_address;
394 unsigned int dmalen = sglist->dma_length;
395
396 if (dmalen == 0)
397 break;
398
399 npages = num_dma_pages(dma, dmalen);
400 __iommu_free(tbl, dma, npages);
401 sglist++;
402 }
403}
404
405void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
406 int nelems, int direction)
407{
408 unsigned long flags;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200409 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200410
411 if (!translate_phb(to_pci_dev(dev)))
412 return;
413
414 spin_lock_irqsave(&tbl->it_lock, flags);
415
416 __calgary_unmap_sg(tbl, sglist, nelems, direction);
417
418 spin_unlock_irqrestore(&tbl->it_lock, flags);
419}
420
421static int calgary_nontranslate_map_sg(struct device* dev,
422 struct scatterlist *sg, int nelems, int direction)
423{
424 int i;
425
426 for (i = 0; i < nelems; i++ ) {
427 struct scatterlist *s = &sg[i];
428 BUG_ON(!s->page);
429 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
430 s->dma_length = s->length;
431 }
432 return nelems;
433}
434
435int calgary_map_sg(struct device *dev, struct scatterlist *sg,
436 int nelems, int direction)
437{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200438 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200439 unsigned long flags;
440 unsigned long vaddr;
441 unsigned int npages;
442 unsigned long entry;
443 int i;
444
445 if (!translate_phb(to_pci_dev(dev)))
446 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
447
448 spin_lock_irqsave(&tbl->it_lock, flags);
449
450 for (i = 0; i < nelems; i++ ) {
451 struct scatterlist *s = &sg[i];
452 BUG_ON(!s->page);
453
454 vaddr = (unsigned long)page_address(s->page) + s->offset;
455 npages = num_dma_pages(vaddr, s->length);
456
457 entry = iommu_range_alloc(tbl, npages);
458 if (entry == bad_dma_address) {
459 /* makes sure unmap knows to stop */
460 s->dma_length = 0;
461 goto error;
462 }
463
464 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
465
466 /* insert into HW table */
467 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
468 direction);
469
470 s->dma_length = s->length;
471 }
472
473 spin_unlock_irqrestore(&tbl->it_lock, flags);
474
475 return nelems;
476error:
477 __calgary_unmap_sg(tbl, sg, nelems, direction);
478 for (i = 0; i < nelems; i++) {
479 sg[i].dma_address = bad_dma_address;
480 sg[i].dma_length = 0;
481 }
482 spin_unlock_irqrestore(&tbl->it_lock, flags);
483 return 0;
484}
485
486dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
487 size_t size, int direction)
488{
489 dma_addr_t dma_handle = bad_dma_address;
490 unsigned long uaddr;
491 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200492 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200493
494 uaddr = (unsigned long)vaddr;
495 npages = num_dma_pages(uaddr, size);
496
497 if (translate_phb(to_pci_dev(dev)))
498 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
499 else
500 dma_handle = virt_to_bus(vaddr);
501
502 return dma_handle;
503}
504
505void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
506 size_t size, int direction)
507{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200508 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200509 unsigned int npages;
510
511 if (!translate_phb(to_pci_dev(dev)))
512 return;
513
514 npages = num_dma_pages(dma_handle, size);
515 iommu_free(tbl, dma_handle, npages);
516}
517
518void* calgary_alloc_coherent(struct device *dev, size_t size,
519 dma_addr_t *dma_handle, gfp_t flag)
520{
521 void *ret = NULL;
522 dma_addr_t mapping;
523 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200524 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200525
526 size = PAGE_ALIGN(size); /* size rounded up to full pages */
527 npages = size >> PAGE_SHIFT;
528 order = get_order(size);
529
530 /* alloc enough pages (and possibly more) */
531 ret = (void *)__get_free_pages(flag, order);
532 if (!ret)
533 goto error;
534 memset(ret, 0, size);
535
536 if (translate_phb(to_pci_dev(dev))) {
537 /* set up tces to cover the allocated range */
538 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
539 if (mapping == bad_dma_address)
540 goto free;
541
542 *dma_handle = mapping;
543 } else /* non translated slot */
544 *dma_handle = virt_to_bus(ret);
545
546 return ret;
547
548free:
549 free_pages((unsigned long)ret, get_order(size));
550 ret = NULL;
551error:
552 return ret;
553}
554
Stephen Hemmingere6584502007-05-02 19:27:06 +0200555static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200556 .alloc_coherent = calgary_alloc_coherent,
557 .map_single = calgary_map_single,
558 .unmap_single = calgary_unmap_single,
559 .map_sg = calgary_map_sg,
560 .unmap_sg = calgary_unmap_sg,
561};
562
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100563static inline void __iomem * busno_to_bbar(unsigned char num)
564{
565 return bus_info[num].bbar;
566}
567
Jon Masone4650582006-06-26 13:58:14 +0200568static inline int busno_to_phbid(unsigned char num)
569{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200570 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200571}
572
573static inline unsigned long split_queue_offset(unsigned char num)
574{
575 size_t idx = busno_to_phbid(num);
576
577 return split_queue_offsets[idx];
578}
579
580static inline unsigned long tar_offset(unsigned char num)
581{
582 size_t idx = busno_to_phbid(num);
583
584 return tar_offsets[idx];
585}
586
587static inline unsigned long phb_offset(unsigned char num)
588{
589 size_t idx = busno_to_phbid(num);
590
591 return phb_offsets[idx];
592}
593
594static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
595{
596 unsigned long target = ((unsigned long)bar) | offset;
597 return (void __iomem*)target;
598}
599
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200600static inline int is_calioc2(unsigned short device)
601{
602 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
603}
604
605static inline int is_calgary(unsigned short device)
606{
607 return (device == PCI_DEVICE_ID_IBM_CALGARY);
608}
609
610static inline int is_cal_pci_dev(unsigned short device)
611{
612 return (is_calgary(device) || is_calioc2(device));
613}
614
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200615static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200616{
617 u64 val;
618 u32 aer;
619 int i = 0;
620 void __iomem *bbar = tbl->bbar;
621 void __iomem *target;
622
623 /* disable arbitration on the bus */
624 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
625 aer = readl(target);
626 writel(0, target);
627
628 /* read plssr to ensure it got there */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
630 val = readl(target);
631
632 /* poll split queues until all DMA activity is done */
633 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
634 do {
635 val = readq(target);
636 i++;
637 } while ((val & 0xff) != 0xff && i < 100);
638 if (i == 100)
639 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
640 "continuing anyway\n");
641
642 /* invalidate TCE cache */
643 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
644 writeq(tbl->tar_val, target);
645
646 /* enable arbitration */
647 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
648 writel(aer, target);
649 (void)readl(target); /* flush */
650}
651
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200652static void calioc2_tce_cache_blast(struct iommu_table *tbl)
653{
654 void __iomem *bbar = tbl->bbar;
655 void __iomem *target;
656 u64 val64;
657 u32 val;
658 int i = 0;
659 int count = 1;
660 unsigned char bus = tbl->it_busno;
661
662begin:
663 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
664 "sequence - count %d\n", bus, count);
665
666 /* 1. using the Page Migration Control reg set SoftStop */
667 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
668 val = be32_to_cpu(readl(target));
669 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
670 val |= PMR_SOFTSTOP;
671 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
672 writel(cpu_to_be32(val), target);
673
674 /* 2. poll split queues until all DMA activity is done */
675 printk(KERN_DEBUG "2a. starting to poll split queues\n");
676 target = calgary_reg(bbar, split_queue_offset(bus));
677 do {
678 val64 = readq(target);
679 i++;
680 } while ((val64 & 0xff) != 0xff && i < 100);
681 if (i == 100)
682 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
683 "continuing anyway\n");
684
685 /* 3. poll Page Migration DEBUG for SoftStopFault */
686 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
687 val = be32_to_cpu(readl(target));
688 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
689
690 /* 4. if SoftStopFault - goto (1) */
691 if (val & PMR_SOFTSTOPFAULT) {
692 if (++count < 100)
693 goto begin;
694 else {
695 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
696 "aborting TCE cache flush sequence!\n");
697 return; /* pray for the best */
698 }
699 }
700
701 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
702 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
703 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
704 val = be32_to_cpu(readl(target));
705 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
706 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
707 val = be32_to_cpu(readl(target));
708 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
709
710 /* 6. invalidate TCE cache */
711 printk(KERN_DEBUG "6. invalidating TCE cache\n");
712 target = calgary_reg(bbar, tar_offset(bus));
713 writeq(tbl->tar_val, target);
714
715 /* 7. Re-read PMCR */
716 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
717 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
718 val = be32_to_cpu(readl(target));
719 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
720
721 /* 8. Remove HardStop */
722 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
723 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
724 val = 0;
725 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
726 writel(cpu_to_be32(val), target);
727 val = be32_to_cpu(readl(target));
728 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
729}
730
Jon Masone4650582006-06-26 13:58:14 +0200731static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
732 u64 limit)
733{
734 unsigned int numpages;
735
736 limit = limit | 0xfffff;
737 limit++;
738
739 numpages = ((limit - start) >> PAGE_SHIFT);
740 iommu_range_reserve(dev->sysdata, start, numpages);
741}
742
743static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
744{
745 void __iomem *target;
746 u64 low, high, sizelow;
747 u64 start, limit;
748 struct iommu_table *tbl = dev->sysdata;
749 unsigned char busnum = dev->bus->number;
750 void __iomem *bbar = tbl->bbar;
751
752 /* peripheral MEM_1 region */
753 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
754 low = be32_to_cpu(readl(target));
755 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
756 high = be32_to_cpu(readl(target));
757 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
758 sizelow = be32_to_cpu(readl(target));
759
760 start = (high << 32) | low;
761 limit = sizelow;
762
763 calgary_reserve_mem_region(dev, start, limit);
764}
765
766static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
767{
768 void __iomem *target;
769 u32 val32;
770 u64 low, high, sizelow, sizehigh;
771 u64 start, limit;
772 struct iommu_table *tbl = dev->sysdata;
773 unsigned char busnum = dev->bus->number;
774 void __iomem *bbar = tbl->bbar;
775
776 /* is it enabled? */
777 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
778 val32 = be32_to_cpu(readl(target));
779 if (!(val32 & PHB_MEM2_ENABLE))
780 return;
781
782 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
783 low = be32_to_cpu(readl(target));
784 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
785 high = be32_to_cpu(readl(target));
786 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
787 sizelow = be32_to_cpu(readl(target));
788 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
789 sizehigh = be32_to_cpu(readl(target));
790
791 start = (high << 32) | low;
792 limit = (sizehigh << 32) | sizelow;
793
794 calgary_reserve_mem_region(dev, start, limit);
795}
796
797/*
798 * some regions of the IO address space do not get translated, so we
799 * must not give devices IO addresses in those regions. The regions
800 * are the 640KB-1MB region and the two PCI peripheral memory holes.
801 * Reserve all of them in the IOMMU bitmap to avoid giving them out
802 * later.
803 */
804static void __init calgary_reserve_regions(struct pci_dev *dev)
805{
806 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200807 u64 start;
808 struct iommu_table *tbl = dev->sysdata;
809
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100810 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
811 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200812
813 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200814 /* for CalIOC2 - avoid the entire first 2MB */
815 if (is_calgary(dev->device)) {
816 start = (640 * 1024);
817 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
818 } else { /* calioc2 */
819 start = 0;
820 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
821 }
Jon Masone4650582006-06-26 13:58:14 +0200822 iommu_range_reserve(tbl, start, npages);
823
824 /* reserve the two PCI peripheral memory regions in IO space */
825 calgary_reserve_peripheral_mem_1(dev);
826 calgary_reserve_peripheral_mem_2(dev);
827}
828
829static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
830{
831 u64 val64;
832 u64 table_phys;
833 void __iomem *target;
834 int ret;
835 struct iommu_table *tbl;
836
837 /* build TCE tables for each PHB */
838 ret = build_tce_table(dev, bbar);
839 if (ret)
840 return ret;
841
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200842 tbl = dev->sysdata;
843 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
844 tce_free(tbl, 0, tbl->it_size);
845
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200846 if (is_calgary(dev->device))
847 tbl->chip_ops = &calgary_chip_ops;
848 else if (is_calioc2(dev->device))
849 tbl->chip_ops = &calioc2_chip_ops;
850 else
851 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200852
Jon Masone4650582006-06-26 13:58:14 +0200853 calgary_reserve_regions(dev);
854
855 /* set TARs for each PHB */
856 target = calgary_reg(bbar, tar_offset(dev->bus->number));
857 val64 = be64_to_cpu(readq(target));
858
859 /* zero out all TAR bits under sw control */
860 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200861 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200862
Jon Masone4650582006-06-26 13:58:14 +0200863 val64 |= table_phys;
864
865 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
866 val64 |= (u64) specified_table_size;
867
868 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200869
Jon Masone4650582006-06-26 13:58:14 +0200870 writeq(tbl->tar_val, target);
871 readq(target); /* flush */
872
873 return 0;
874}
875
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200876static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200877{
878 u64 val64;
879 struct iommu_table *tbl = dev->sysdata;
880 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200881 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200882
883 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
884 val64 = be64_to_cpu(readq(target));
885 val64 &= ~TAR_SW_BITS;
886 writeq(cpu_to_be64(val64), target);
887 readq(target); /* flush */
888
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200889 bitmapsz = tbl->it_size / BITS_PER_BYTE;
890 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
891 tbl->it_map = NULL;
892
Jon Masone4650582006-06-26 13:58:14 +0200893 kfree(tbl);
894 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200895
896 /* Can't free bootmem allocated memory after system is up :-( */
897 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200898}
899
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200900static void calgary_dump_error_regs(struct iommu_table *tbl)
901{
902 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200903 u32 val32;
904 void __iomem *target;
905
906 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
907 val32 = be32_to_cpu(readl(target));
908
909 /* If no error, the agent ID in the CSR is not valid */
910 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
911 "CSR = 0x%08x\n", tbl->it_busno, val32);
912}
913
914static void calioc2_dump_error_regs(struct iommu_table *tbl)
915{
916 void __iomem *bbar = tbl->bbar;
917 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200918 void __iomem *target;
919 unsigned long phboff = phb_offset(tbl->it_busno);
920 unsigned long erroff;
921 u32 errregs[7];
922 int i;
923
924 /* dump CSR */
925 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
926 csr = be32_to_cpu(readl(target));
927 /* dump PLSSR */
928 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
929 plssr = be32_to_cpu(readl(target));
930 /* dump CSMR */
931 target = calgary_reg(bbar, phboff | 0x290);
932 csmr = be32_to_cpu(readl(target));
933 /* dump mck */
934 target = calgary_reg(bbar, phboff | 0x800);
935 mck = be32_to_cpu(readl(target));
936
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200937 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
938 tbl->it_busno);
939
940 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
941 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200942
943 /* dump rest of error regs */
944 printk(KERN_EMERG "Calgary: ");
945 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
946 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
947 target = calgary_reg(bbar, phboff | erroff);
948 errregs[i] = be32_to_cpu(readl(target));
949 printk("0x%08x@0x%lx ", errregs[i], erroff);
950 }
951 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200952
953 /* root complex status */
954 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
955 rcstat = be32_to_cpu(readl(target));
956 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
957 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200958}
959
Jon Masone4650582006-06-26 13:58:14 +0200960static void calgary_watchdog(unsigned long data)
961{
962 struct pci_dev *dev = (struct pci_dev *)data;
963 struct iommu_table *tbl = dev->sysdata;
964 void __iomem *bbar = tbl->bbar;
965 u32 val32;
966 void __iomem *target;
967
968 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
969 val32 = be32_to_cpu(readl(target));
970
971 /* If no error, the agent ID in the CSR is not valid */
972 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200973 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200974
975 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200976 writel(0, target);
977
978 /* Disable bus that caused the error */
979 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200980 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200981 val32 = be32_to_cpu(readl(target));
982 val32 |= PHB_SLOT_DISABLE;
983 writel(cpu_to_be32(val32), target);
984 readl(target); /* flush */
985 } else {
986 /* Reset the timer */
987 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
988 }
989}
990
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200991static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
992 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200993{
994 u64 val64;
995 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100996 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200997 u64 mask;
998
999 switch (busno_to_phbid(busnum)) {
1000 case 0: phb_shift = (63 - 19);
1001 break;
1002 case 1: phb_shift = (63 - 23);
1003 break;
1004 case 2: phb_shift = (63 - 27);
1005 break;
1006 case 3: phb_shift = (63 - 35);
1007 break;
1008 default:
1009 BUG_ON(busno_to_phbid(busnum));
1010 }
1011
1012 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1013 val64 = be64_to_cpu(readq(target));
1014
1015 /* zero out this PHB's timer bits */
1016 mask = ~(0xFUL << phb_shift);
1017 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001018 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001019 writeq(cpu_to_be64(val64), target);
1020 readq(target); /* flush */
1021}
1022
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001023static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1024{
1025 unsigned char busnum = dev->bus->number;
1026 void __iomem *bbar = tbl->bbar;
1027 void __iomem *target;
1028 u32 val;
1029
1030 /*
1031 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1032 */
1033 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1034 val = cpu_to_be32(readl(target));
1035 val |= 0x00800000;
1036 writel(cpu_to_be32(val), target);
1037}
1038
1039static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001040{
1041 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001042
1043 /*
1044 * Give split completion a longer timeout on bus 1 for aic94xx
1045 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1046 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001047 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001048 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1049 CCR_2SEC_TIMEOUT);
1050}
1051
Jon Masone4650582006-06-26 13:58:14 +02001052static void __init calgary_enable_translation(struct pci_dev *dev)
1053{
1054 u32 val32;
1055 unsigned char busnum;
1056 void __iomem *target;
1057 void __iomem *bbar;
1058 struct iommu_table *tbl;
1059
1060 busnum = dev->bus->number;
1061 tbl = dev->sysdata;
1062 bbar = tbl->bbar;
1063
1064 /* enable TCE in PHB Config Register */
1065 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1066 val32 = be32_to_cpu(readl(target));
1067 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1068
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001069 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1070 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1071 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001072 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1073 "bus.\n");
1074
1075 writel(cpu_to_be32(val32), target);
1076 readl(target); /* flush */
1077
1078 init_timer(&tbl->watchdog_timer);
1079 tbl->watchdog_timer.function = &calgary_watchdog;
1080 tbl->watchdog_timer.data = (unsigned long)dev;
1081 mod_timer(&tbl->watchdog_timer, jiffies);
1082}
1083
1084static void __init calgary_disable_translation(struct pci_dev *dev)
1085{
1086 u32 val32;
1087 unsigned char busnum;
1088 void __iomem *target;
1089 void __iomem *bbar;
1090 struct iommu_table *tbl;
1091
1092 busnum = dev->bus->number;
1093 tbl = dev->sysdata;
1094 bbar = tbl->bbar;
1095
1096 /* disable TCE in PHB Config Register */
1097 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1098 val32 = be32_to_cpu(readl(target));
1099 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1100
Jon Mason70d666d2006-10-05 18:47:21 +02001101 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001102 writel(cpu_to_be32(val32), target);
1103 readl(target); /* flush */
1104
1105 del_timer_sync(&tbl->watchdog_timer);
1106}
1107
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001108static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001109{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001110 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +02001111 dev->sysdata = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001112
1113 /* is the device behind a bridge? */
1114 if (dev->bus->parent)
1115 dev->bus->parent->self = dev;
1116 else
1117 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001118}
1119
1120static int __init calgary_init_one(struct pci_dev *dev)
1121{
Jon Masone4650582006-06-26 13:58:14 +02001122 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001123 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001124 int ret;
1125
Jon Masondedc9932006-10-05 18:47:21 +02001126 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1127
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001128 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001129 ret = calgary_setup_tar(dev, bbar);
1130 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001131 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001132
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001133 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001134
1135 if (dev->bus->parent) {
1136 if (dev->bus->parent->self)
1137 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1138 "bus->parent->self!\n", dev);
1139 dev->bus->parent->self = dev;
1140 } else
1141 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001142
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001143 tbl = dev->sysdata;
1144 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001145
Jon Masone4650582006-06-26 13:58:14 +02001146 calgary_enable_translation(dev);
1147
1148 return 0;
1149
Jon Masone4650582006-06-26 13:58:14 +02001150done:
1151 return ret;
1152}
1153
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001154static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001155{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001156 int ret;
1157 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001158 void __iomem *bbar;
1159 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001160 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001161 u8 start_bus, end_bus;
1162 u32 val;
1163
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001164 ret = -ENODATA;
1165 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1166 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001167
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001168 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001169 continue;
1170
1171 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001172 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1173 if (!bbar)
1174 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001175
1176 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001177 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1178 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001179
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001180 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001181
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001182 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001183 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001184
1185 if (end_bus) {
1186 for (bus = start_bus; bus <= end_bus; bus++) {
1187 bus_info[bus].bbar = bbar;
1188 bus_info[bus].phbid = phb;
1189 }
1190 } else {
1191 bus_info[start_bus].bbar = bbar;
1192 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001193 }
1194 }
1195 }
1196
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001197 return 0;
1198
1199error:
1200 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1201 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1202 if (bus_info[bus].bbar)
1203 iounmap(bus_info[bus].bbar);
1204
1205 return ret;
1206}
1207
1208static int __init calgary_init(void)
1209{
1210 int ret;
1211 struct pci_dev *dev = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001212 void* tce_space;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001213
1214 ret = calgary_locate_bbars();
1215 if (ret)
1216 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001217
Jon Masondedc9932006-10-05 18:47:21 +02001218 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001219 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001220 if (!dev)
1221 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001222 if (!is_cal_pci_dev(dev->device))
1223 continue;
Jon Masone4650582006-06-26 13:58:14 +02001224 if (!translate_phb(dev)) {
1225 calgary_init_one_nontraslated(dev);
1226 continue;
1227 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001228 tce_space = bus_info[dev->bus->number].tce_space;
1229 if (!tce_space && !translate_empty_slots) {
1230 printk("Calg: %p failed tce_space check\n", dev);
Jon Masone4650582006-06-26 13:58:14 +02001231 continue;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001232 }
Jon Masone4650582006-06-26 13:58:14 +02001233 ret = calgary_init_one(dev);
1234 if (ret)
1235 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001236 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001237
1238 return ret;
1239
1240error:
Jon Masondedc9932006-10-05 18:47:21 +02001241 do {
Alan Cox7cd8b682006-12-07 02:14:03 +01001242 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001243 PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001244 if (!dev)
1245 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001246 if (!is_cal_pci_dev(dev->device))
1247 continue;
Jon Masone4650582006-06-26 13:58:14 +02001248 if (!translate_phb(dev)) {
1249 pci_dev_put(dev);
1250 continue;
1251 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001252 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001253 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001254
Jon Masone4650582006-06-26 13:58:14 +02001255 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001256 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001257 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001258 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001259
1260 return ret;
1261}
1262
1263static inline int __init determine_tce_table_size(u64 ram)
1264{
1265 int ret;
1266
1267 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1268 return specified_table_size;
1269
1270 /*
1271 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1272 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1273 * larger table size has twice as many entries, so shift the
1274 * max ram address by 13 to divide by 8K and then look at the
1275 * order of the result to choose between 0-7.
1276 */
1277 ret = get_order(ram >> 13);
1278 if (ret > TCE_TABLE_SIZE_8M)
1279 ret = TCE_TABLE_SIZE_8M;
1280
1281 return ret;
1282}
1283
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001284static int __init build_detail_arrays(void)
1285{
1286 unsigned long ptr;
1287 int i, scal_detail_size, rio_detail_size;
1288
1289 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1290 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001291 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001292 "but system has %d nodes.\n",
1293 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1294 return -ENODEV;
1295 }
1296
1297 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001298 case 2:
1299 scal_detail_size = 11;
1300 rio_detail_size = 13;
1301 break;
1302 case 3:
1303 scal_detail_size = 12;
1304 rio_detail_size = 15;
1305 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001306 default:
1307 printk(KERN_WARNING
1308 "Calgary: Invalid Rio Grande Table Version: %d\n",
1309 rio_table_hdr->version);
1310 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001311 }
1312
1313 ptr = ((unsigned long)rio_table_hdr) + 3;
1314 for (i = 0; i < rio_table_hdr->num_scal_dev;
1315 i++, ptr += scal_detail_size)
1316 scal_devs[i] = (struct scal_detail *)ptr;
1317
1318 for (i = 0; i < rio_table_hdr->num_rio_dev;
1319 i++, ptr += rio_detail_size)
1320 rio_devs[i] = (struct rio_detail *)ptr;
1321
1322 return 0;
1323}
1324
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001325static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1326{
1327 int dev;
1328 u32 val;
1329
1330 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1331 /*
1332 * FIXME: properly scan for devices accross the
1333 * PCI-to-PCI bridge on every CalIOC2 port.
1334 */
1335 return 1;
1336 }
1337
1338 for (dev = 1; dev < 8; dev++) {
1339 val = read_pci_config(bus, dev, 0, 0);
1340 if (val != 0xffffffff)
1341 break;
1342 }
1343 return (val != 0xffffffff);
1344}
1345
Jon Masone4650582006-06-26 13:58:14 +02001346void __init detect_calgary(void)
1347{
Jon Masond2105b12006-07-29 21:42:43 +02001348 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001349 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001350 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001351 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001352 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001353 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001354
1355 /*
1356 * if the user specified iommu=off or iommu=soft or we found
1357 * another HW IOMMU already, bail out.
1358 */
1359 if (swiotlb || no_iommu || iommu_detected)
1360 return;
1361
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001362 if (!use_calgary)
1363 return;
1364
Andi Kleen0637a702006-09-26 10:52:41 +02001365 if (!early_pci_allowed())
1366 return;
1367
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001368 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1369
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001370 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1371
1372 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001373 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001374 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001375 /*
1376 * The next offset is stored in the 1st word.
1377 * Only parse up until the offset increases:
1378 */
1379 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001380 /* The block id is stored in the 2nd word */
1381 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1382 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001383 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001384 break;
1385 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001386 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001387 offset = *((unsigned short *)(ptr + offset));
1388 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001389 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001390 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1391 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001392 return;
1393 }
1394
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001395 ret = build_detail_arrays();
1396 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001397 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001398 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001399 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001400
Jon Masone4650582006-06-26 13:58:14 +02001401 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1402
Jon Masond2105b12006-07-29 21:42:43 +02001403 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001404 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001405 unsigned short pci_device;
1406 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001407
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001408 val = read_pci_config(bus, 0, 0, 0);
1409 pci_device = (val & 0xFFFF0000) >> 16;
1410
1411 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001412 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001413
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001414 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001415 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001416
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001417 if (calgary_bus_has_devices(bus, pci_device) ||
1418 translate_empty_slots) {
1419 tbl = alloc_tce_table();
1420 if (!tbl)
1421 goto cleanup;
1422 info->tce_space = tbl;
1423 calgary_found = 1;
1424 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1425 info->tce_space, bus);
Jon Masond2105b12006-07-29 21:42:43 +02001426 }
Jon Masone4650582006-06-26 13:58:14 +02001427 }
1428
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001429 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1430 calgary_found ? "found" : "not found");
1431
Jon Masond2105b12006-07-29 21:42:43 +02001432 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001433 iommu_detected = 1;
1434 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001435 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1436 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1437 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1438 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001439 }
1440 return;
1441
1442cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001443 for (--bus; bus >= 0; --bus) {
1444 struct calgary_bus_info *info = &bus_info[bus];
1445
1446 if (info->tce_space)
1447 free_tce_table(info->tce_space);
1448 }
Jon Masone4650582006-06-26 13:58:14 +02001449}
1450
1451int __init calgary_iommu_init(void)
1452{
1453 int ret;
1454
1455 if (no_iommu || swiotlb)
1456 return -ENODEV;
1457
1458 if (!calgary_detected)
1459 return -ENODEV;
1460
1461 /* ok, we're trying to use Calgary - let's roll */
1462 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1463
1464 ret = calgary_init();
1465 if (ret) {
1466 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1467 "falling back to no_iommu\n", ret);
1468 if (end_pfn > MAX_DMA32_PFN)
1469 printk(KERN_ERR "WARNING more than 4GB of memory, "
1470 "32bit PCI may malfunction.\n");
1471 return ret;
1472 }
1473
1474 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001475 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001476 dma_ops = &calgary_dma_ops;
1477
1478 return 0;
1479}
1480
1481static int __init calgary_parse_options(char *p)
1482{
1483 unsigned int bridge;
1484 size_t len;
1485 char* endp;
1486
1487 while (*p) {
1488 if (!strncmp(p, "64k", 3))
1489 specified_table_size = TCE_TABLE_SIZE_64K;
1490 else if (!strncmp(p, "128k", 4))
1491 specified_table_size = TCE_TABLE_SIZE_128K;
1492 else if (!strncmp(p, "256k", 4))
1493 specified_table_size = TCE_TABLE_SIZE_256K;
1494 else if (!strncmp(p, "512k", 4))
1495 specified_table_size = TCE_TABLE_SIZE_512K;
1496 else if (!strncmp(p, "1M", 2))
1497 specified_table_size = TCE_TABLE_SIZE_1M;
1498 else if (!strncmp(p, "2M", 2))
1499 specified_table_size = TCE_TABLE_SIZE_2M;
1500 else if (!strncmp(p, "4M", 2))
1501 specified_table_size = TCE_TABLE_SIZE_4M;
1502 else if (!strncmp(p, "8M", 2))
1503 specified_table_size = TCE_TABLE_SIZE_8M;
1504
1505 len = strlen("translate_empty_slots");
1506 if (!strncmp(p, "translate_empty_slots", len))
1507 translate_empty_slots = 1;
1508
1509 len = strlen("disable");
1510 if (!strncmp(p, "disable", len)) {
1511 p += len;
1512 if (*p == '=')
1513 ++p;
1514 if (*p == '\0')
1515 break;
1516 bridge = simple_strtol(p, &endp, 0);
1517 if (p == endp)
1518 break;
1519
Jon Masond2105b12006-07-29 21:42:43 +02001520 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001521 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001522 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001523 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001524 }
1525 }
1526
1527 p = strpbrk(p, ",");
1528 if (!p)
1529 break;
1530
1531 p++; /* skip ',' */
1532 }
1533 return 1;
1534}
1535__setup("calgary=", calgary_parse_options);