blob: a2f9c536929244179c70aa3edddcefb54151f496 [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
Jon Masone4650582006-06-26 13:58:14 +020055/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020056#define CALGARY_CONFIG_REG 0x0108
57#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020058#define PHB_PLSSR_OFFSET 0x0120
59#define PHB_CONFIG_RW_OFFSET 0x0160
60#define PHB_IOBASE_BAR_LOW 0x0170
61#define PHB_IOBASE_BAR_HIGH 0x0180
62#define PHB_MEM_1_LOW 0x0190
63#define PHB_MEM_1_HIGH 0x01A0
64#define PHB_IO_ADDR_SIZE 0x01B0
65#define PHB_MEM_1_SIZE 0x01C0
66#define PHB_MEM_ST_OFFSET 0x01D0
67#define PHB_AER_OFFSET 0x0200
68#define PHB_CONFIG_0_HIGH 0x0220
69#define PHB_CONFIG_0_LOW 0x0230
70#define PHB_CONFIG_0_END 0x0240
71#define PHB_MEM_2_LOW 0x02B0
72#define PHB_MEM_2_HIGH 0x02C0
73#define PHB_MEM_2_SIZE_HIGH 0x02D0
74#define PHB_MEM_2_SIZE_LOW 0x02E0
75#define PHB_DOSHOLE_OFFSET 0x08E0
76
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020077/* CalIOC2 specific */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020078#define PHB_SAVIOR_L2 0x0DB0
79#define PHB_PAGE_MIG_CTRL 0x0DA8
80#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020081#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020082
Jon Masone4650582006-06-26 13:58:14 +020083/* PHB_CONFIG_RW */
84#define PHB_TCE_ENABLE 0x20000000
85#define PHB_SLOT_DISABLE 0x1C000000
86#define PHB_DAC_DISABLE 0x01000000
87#define PHB_MEM2_ENABLE 0x00400000
88#define PHB_MCSR_ENABLE 0x00100000
89/* TAR (Table Address Register) */
90#define TAR_SW_BITS 0x0000ffffffff800fUL
91#define TAR_VALID 0x0000000000000008UL
92/* CSR (Channel/DMA Status Register) */
93#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020094/* CCR (Calgary Configuration Register) */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020095#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +020096/* PMCR/PMDR (Page Migration Control/Debug Registers */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020097#define PMR_SOFTSTOP 0x80000000
98#define PMR_SOFTSTOPFAULT 0x40000000
99#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200100
101#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200102#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200103/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
104#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200105#define PHBS_PER_CALGARY 4
106
107/* register offsets in Calgary's internal register space */
108static const unsigned long tar_offsets[] = {
109 0x0580 /* TAR0 */,
110 0x0588 /* TAR1 */,
111 0x0590 /* TAR2 */,
112 0x0598 /* TAR3 */
113};
114
115static const unsigned long split_queue_offsets[] = {
116 0x4870 /* SPLIT QUEUE 0 */,
117 0x5870 /* SPLIT QUEUE 1 */,
118 0x6870 /* SPLIT QUEUE 2 */,
119 0x7870 /* SPLIT QUEUE 3 */
120};
121
122static const unsigned long phb_offsets[] = {
123 0x8000 /* PHB0 */,
124 0x9000 /* PHB1 */,
125 0xA000 /* PHB2 */,
126 0xB000 /* PHB3 */
127};
128
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100129/* PHB debug registers */
130
131static const unsigned long phb_debug_offsets[] = {
132 0x4000 /* PHB 0 DEBUG */,
133 0x5000 /* PHB 1 DEBUG */,
134 0x6000 /* PHB 2 DEBUG */,
135 0x7000 /* PHB 3 DEBUG */
136};
137
138/*
139 * STUFF register for each debug PHB,
140 * byte 1 = start bus number, byte 2 = end bus number
141 */
142
143#define PHB_DEBUG_STUFF_OFFSET 0x0020
144
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100145#define EMERGENCY_PAGES 32 /* = 128KB */
146
Jon Masone4650582006-06-26 13:58:14 +0200147unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
148static int translate_empty_slots __read_mostly = 0;
149static int calgary_detected __read_mostly = 0;
150
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100151static struct rio_table_hdr *rio_table_hdr __initdata;
152static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100153static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100154
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200155struct calgary_bus_info {
156 void *tce_space;
Muli Ben-Yehuda0577f1482006-09-26 10:52:31 +0200157 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200158 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100159 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200160};
161
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200162static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
163static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200164static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200165static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200166static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200167static void calioc2_dump_error_regs(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200168
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200169static struct cal_chipset_ops calgary_chip_ops = {
170 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200171 .tce_cache_blast = calgary_tce_cache_blast,
172 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200173};
174
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200175static struct cal_chipset_ops calioc2_chip_ops = {
176 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200177 .tce_cache_blast = calioc2_tce_cache_blast,
178 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200179};
180
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200181static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200182
183/* enable this to stress test the chip's TCE cache */
184#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200185int debugging __read_mostly = 1;
186
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200187static inline unsigned long verify_bit_range(unsigned long* bitmap,
188 int expected, unsigned long start, unsigned long end)
189{
190 unsigned long idx = start;
191
192 BUG_ON(start >= end);
193
194 while (idx < end) {
195 if (!!test_bit(idx, bitmap) != expected)
196 return idx;
197 ++idx;
198 }
199
200 /* all bits have the expected value */
201 return ~0UL;
202}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200203#else /* debugging is disabled */
204int debugging __read_mostly = 0;
205
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200206static inline unsigned long verify_bit_range(unsigned long* bitmap,
207 int expected, unsigned long start, unsigned long end)
208{
209 return ~0UL;
210}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200211
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200212#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200213
214static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
215{
216 unsigned int npages;
217
218 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
219 npages >>= PAGE_SHIFT;
220
221 return npages;
222}
223
224static inline int translate_phb(struct pci_dev* dev)
225{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200226 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200227 return !disabled;
228}
229
230static void iommu_range_reserve(struct iommu_table *tbl,
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200231 unsigned long start_addr, unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200232{
233 unsigned long index;
234 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200235 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200236
237 index = start_addr >> PAGE_SHIFT;
238
239 /* bail out if we're asked to reserve a region we don't cover */
240 if (index >= tbl->it_size)
241 return;
242
243 end = index + npages;
244 if (end > tbl->it_size) /* don't go off the table */
245 end = tbl->it_size;
246
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200247 badbit = verify_bit_range(tbl->it_map, 0, index, end);
248 if (badbit != ~0UL) {
249 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200250 printk(KERN_ERR "Calgary: entry already allocated at "
251 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200252 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200253 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200254
255 set_bit_string(tbl->it_map, index, npages);
Jon Masone4650582006-06-26 13:58:14 +0200256}
257
258static unsigned long iommu_range_alloc(struct iommu_table *tbl,
259 unsigned int npages)
260{
261 unsigned long offset;
262
263 BUG_ON(npages == 0);
264
265 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
266 tbl->it_size, npages);
267 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200268 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200269 offset = find_next_zero_string(tbl->it_map, 0,
270 tbl->it_size, npages);
271 if (offset == ~0UL) {
272 printk(KERN_WARNING "Calgary: IOMMU full.\n");
273 if (panic_on_overflow)
274 panic("Calgary: fix the allocator.\n");
275 else
276 return bad_dma_address;
277 }
278 }
279
280 set_bit_string(tbl->it_map, offset, npages);
281 tbl->it_hint = offset + npages;
282 BUG_ON(tbl->it_hint > tbl->it_size);
283
284 return offset;
285}
286
287static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
288 unsigned int npages, int direction)
289{
290 unsigned long entry, flags;
291 dma_addr_t ret = bad_dma_address;
292
293 spin_lock_irqsave(&tbl->it_lock, flags);
294
295 entry = iommu_range_alloc(tbl, npages);
296
297 if (unlikely(entry == bad_dma_address))
298 goto error;
299
300 /* set the return dma address */
301 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
302
303 /* put the TCEs in the HW table */
304 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
305 direction);
306
307 spin_unlock_irqrestore(&tbl->it_lock, flags);
308
309 return ret;
310
311error:
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
313 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
314 "iommu %p\n", npages, tbl);
315 return bad_dma_address;
316}
317
318static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
319 unsigned int npages)
320{
321 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200322 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100323 unsigned long badend;
324
325 /* were we called with bad_dma_address? */
326 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
327 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
328 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
329 "address 0x%Lx\n", dma_addr);
330 WARN_ON(1);
331 return;
332 }
Jon Masone4650582006-06-26 13:58:14 +0200333
334 entry = dma_addr >> PAGE_SHIFT;
335
336 BUG_ON(entry + npages > tbl->it_size);
337
338 tce_free(tbl, entry, npages);
339
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200340 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
341 if (badbit != ~0UL) {
342 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200343 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
344 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200345 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200346 }
347
348 __clear_bit_string(tbl->it_map, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200349}
350
351static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
352 unsigned int npages)
353{
354 unsigned long flags;
355
356 spin_lock_irqsave(&tbl->it_lock, flags);
357
358 __iommu_free(tbl, dma_addr, npages);
359
360 spin_unlock_irqrestore(&tbl->it_lock, flags);
361}
362
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200363static inline struct iommu_table *find_iommu_table(struct device *dev)
364{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200365 struct pci_dev *pdev;
366 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200367 struct iommu_table *tbl;
368
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200369 pdev = to_pci_dev(dev);
370
371 /* is the device behind a bridge? */
372 if (unlikely(pdev->bus->parent))
373 pbus = pdev->bus->parent;
374 else
375 pbus = pdev->bus;
376
377 tbl = pbus->self->sysdata;
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200378
379 BUG_ON(pdev->bus->parent &&
380 (tbl->it_busno != pdev->bus->parent->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200381
382 return tbl;
383}
384
Jon Masone4650582006-06-26 13:58:14 +0200385static void __calgary_unmap_sg(struct iommu_table *tbl,
386 struct scatterlist *sglist, int nelems, int direction)
387{
388 while (nelems--) {
389 unsigned int npages;
390 dma_addr_t dma = sglist->dma_address;
391 unsigned int dmalen = sglist->dma_length;
392
393 if (dmalen == 0)
394 break;
395
396 npages = num_dma_pages(dma, dmalen);
397 __iommu_free(tbl, dma, npages);
398 sglist++;
399 }
400}
401
402void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
403 int nelems, int direction)
404{
405 unsigned long flags;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200406 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200407
408 if (!translate_phb(to_pci_dev(dev)))
409 return;
410
411 spin_lock_irqsave(&tbl->it_lock, flags);
412
413 __calgary_unmap_sg(tbl, sglist, nelems, direction);
414
415 spin_unlock_irqrestore(&tbl->it_lock, flags);
416}
417
418static int calgary_nontranslate_map_sg(struct device* dev,
419 struct scatterlist *sg, int nelems, int direction)
420{
421 int i;
422
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200423 for (i = 0; i < nelems; i++ ) {
Jon Masone4650582006-06-26 13:58:14 +0200424 struct scatterlist *s = &sg[i];
425 BUG_ON(!s->page);
426 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
427 s->dma_length = s->length;
428 }
429 return nelems;
430}
431
432int calgary_map_sg(struct device *dev, struct scatterlist *sg,
433 int nelems, int direction)
434{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200435 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200436 unsigned long flags;
437 unsigned long vaddr;
438 unsigned int npages;
439 unsigned long entry;
440 int i;
441
442 if (!translate_phb(to_pci_dev(dev)))
443 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
444
445 spin_lock_irqsave(&tbl->it_lock, flags);
446
447 for (i = 0; i < nelems; i++ ) {
448 struct scatterlist *s = &sg[i];
449 BUG_ON(!s->page);
450
451 vaddr = (unsigned long)page_address(s->page) + s->offset;
452 npages = num_dma_pages(vaddr, s->length);
453
454 entry = iommu_range_alloc(tbl, npages);
455 if (entry == bad_dma_address) {
456 /* makes sure unmap knows to stop */
457 s->dma_length = 0;
458 goto error;
459 }
460
461 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
462
463 /* insert into HW table */
464 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
465 direction);
466
467 s->dma_length = s->length;
468 }
469
470 spin_unlock_irqrestore(&tbl->it_lock, flags);
471
472 return nelems;
473error:
474 __calgary_unmap_sg(tbl, sg, nelems, direction);
475 for (i = 0; i < nelems; i++) {
476 sg[i].dma_address = bad_dma_address;
477 sg[i].dma_length = 0;
478 }
479 spin_unlock_irqrestore(&tbl->it_lock, flags);
480 return 0;
481}
482
483dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
484 size_t size, int direction)
485{
486 dma_addr_t dma_handle = bad_dma_address;
487 unsigned long uaddr;
488 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200489 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200490
491 uaddr = (unsigned long)vaddr;
492 npages = num_dma_pages(uaddr, size);
493
494 if (translate_phb(to_pci_dev(dev)))
495 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
496 else
497 dma_handle = virt_to_bus(vaddr);
498
499 return dma_handle;
500}
501
502void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
503 size_t size, int direction)
504{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200505 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200506 unsigned int npages;
507
508 if (!translate_phb(to_pci_dev(dev)))
509 return;
510
511 npages = num_dma_pages(dma_handle, size);
512 iommu_free(tbl, dma_handle, npages);
513}
514
515void* calgary_alloc_coherent(struct device *dev, size_t size,
516 dma_addr_t *dma_handle, gfp_t flag)
517{
518 void *ret = NULL;
519 dma_addr_t mapping;
520 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200521 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200522
523 size = PAGE_ALIGN(size); /* size rounded up to full pages */
524 npages = size >> PAGE_SHIFT;
525 order = get_order(size);
526
527 /* alloc enough pages (and possibly more) */
528 ret = (void *)__get_free_pages(flag, order);
529 if (!ret)
530 goto error;
531 memset(ret, 0, size);
532
533 if (translate_phb(to_pci_dev(dev))) {
534 /* set up tces to cover the allocated range */
535 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
536 if (mapping == bad_dma_address)
537 goto free;
538
539 *dma_handle = mapping;
540 } else /* non translated slot */
541 *dma_handle = virt_to_bus(ret);
542
543 return ret;
544
545free:
546 free_pages((unsigned long)ret, get_order(size));
547 ret = NULL;
548error:
549 return ret;
550}
551
Stephen Hemmingere6584502007-05-02 19:27:06 +0200552static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200553 .alloc_coherent = calgary_alloc_coherent,
554 .map_single = calgary_map_single,
555 .unmap_single = calgary_unmap_single,
556 .map_sg = calgary_map_sg,
557 .unmap_sg = calgary_unmap_sg,
558};
559
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100560static inline void __iomem * busno_to_bbar(unsigned char num)
561{
562 return bus_info[num].bbar;
563}
564
Jon Masone4650582006-06-26 13:58:14 +0200565static inline int busno_to_phbid(unsigned char num)
566{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200567 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200568}
569
570static inline unsigned long split_queue_offset(unsigned char num)
571{
572 size_t idx = busno_to_phbid(num);
573
574 return split_queue_offsets[idx];
575}
576
577static inline unsigned long tar_offset(unsigned char num)
578{
579 size_t idx = busno_to_phbid(num);
580
581 return tar_offsets[idx];
582}
583
584static inline unsigned long phb_offset(unsigned char num)
585{
586 size_t idx = busno_to_phbid(num);
587
588 return phb_offsets[idx];
589}
590
591static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
592{
593 unsigned long target = ((unsigned long)bar) | offset;
594 return (void __iomem*)target;
595}
596
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200597static inline int is_calioc2(unsigned short device)
598{
599 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
600}
601
602static inline int is_calgary(unsigned short device)
603{
604 return (device == PCI_DEVICE_ID_IBM_CALGARY);
605}
606
607static inline int is_cal_pci_dev(unsigned short device)
608{
609 return (is_calgary(device) || is_calioc2(device));
610}
611
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200612static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200613{
614 u64 val;
615 u32 aer;
616 int i = 0;
617 void __iomem *bbar = tbl->bbar;
618 void __iomem *target;
619
620 /* disable arbitration on the bus */
621 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
622 aer = readl(target);
623 writel(0, target);
624
625 /* read plssr to ensure it got there */
626 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
627 val = readl(target);
628
629 /* poll split queues until all DMA activity is done */
630 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
631 do {
632 val = readq(target);
633 i++;
634 } while ((val & 0xff) != 0xff && i < 100);
635 if (i == 100)
636 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
637 "continuing anyway\n");
638
639 /* invalidate TCE cache */
640 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
641 writeq(tbl->tar_val, target);
642
643 /* enable arbitration */
644 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
645 writel(aer, target);
646 (void)readl(target); /* flush */
647}
648
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200649static void calioc2_tce_cache_blast(struct iommu_table *tbl)
650{
651 void __iomem *bbar = tbl->bbar;
652 void __iomem *target;
653 u64 val64;
654 u32 val;
655 int i = 0;
656 int count = 1;
657 unsigned char bus = tbl->it_busno;
658
659begin:
660 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
661 "sequence - count %d\n", bus, count);
662
663 /* 1. using the Page Migration Control reg set SoftStop */
664 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
665 val = be32_to_cpu(readl(target));
666 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
667 val |= PMR_SOFTSTOP;
668 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
669 writel(cpu_to_be32(val), target);
670
671 /* 2. poll split queues until all DMA activity is done */
672 printk(KERN_DEBUG "2a. starting to poll split queues\n");
673 target = calgary_reg(bbar, split_queue_offset(bus));
674 do {
675 val64 = readq(target);
676 i++;
677 } while ((val64 & 0xff) != 0xff && i < 100);
678 if (i == 100)
679 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
680 "continuing anyway\n");
681
682 /* 3. poll Page Migration DEBUG for SoftStopFault */
683 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
684 val = be32_to_cpu(readl(target));
685 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
686
687 /* 4. if SoftStopFault - goto (1) */
688 if (val & PMR_SOFTSTOPFAULT) {
689 if (++count < 100)
690 goto begin;
691 else {
692 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
693 "aborting TCE cache flush sequence!\n");
694 return; /* pray for the best */
695 }
696 }
697
698 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
699 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
700 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
701 val = be32_to_cpu(readl(target));
702 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
703 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
704 val = be32_to_cpu(readl(target));
705 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
706
707 /* 6. invalidate TCE cache */
708 printk(KERN_DEBUG "6. invalidating TCE cache\n");
709 target = calgary_reg(bbar, tar_offset(bus));
710 writeq(tbl->tar_val, target);
711
712 /* 7. Re-read PMCR */
713 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
714 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
715 val = be32_to_cpu(readl(target));
716 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
717
718 /* 8. Remove HardStop */
719 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
720 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
721 val = 0;
722 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
723 writel(cpu_to_be32(val), target);
724 val = be32_to_cpu(readl(target));
725 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
726}
727
Jon Masone4650582006-06-26 13:58:14 +0200728static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
729 u64 limit)
730{
731 unsigned int numpages;
732
733 limit = limit | 0xfffff;
734 limit++;
735
736 numpages = ((limit - start) >> PAGE_SHIFT);
737 iommu_range_reserve(dev->sysdata, start, numpages);
738}
739
740static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
741{
742 void __iomem *target;
743 u64 low, high, sizelow;
744 u64 start, limit;
745 struct iommu_table *tbl = dev->sysdata;
746 unsigned char busnum = dev->bus->number;
747 void __iomem *bbar = tbl->bbar;
748
749 /* peripheral MEM_1 region */
750 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
751 low = be32_to_cpu(readl(target));
752 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
753 high = be32_to_cpu(readl(target));
754 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
755 sizelow = be32_to_cpu(readl(target));
756
757 start = (high << 32) | low;
758 limit = sizelow;
759
760 calgary_reserve_mem_region(dev, start, limit);
761}
762
763static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
764{
765 void __iomem *target;
766 u32 val32;
767 u64 low, high, sizelow, sizehigh;
768 u64 start, limit;
769 struct iommu_table *tbl = dev->sysdata;
770 unsigned char busnum = dev->bus->number;
771 void __iomem *bbar = tbl->bbar;
772
773 /* is it enabled? */
774 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
775 val32 = be32_to_cpu(readl(target));
776 if (!(val32 & PHB_MEM2_ENABLE))
777 return;
778
779 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
780 low = be32_to_cpu(readl(target));
781 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
782 high = be32_to_cpu(readl(target));
783 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
784 sizelow = be32_to_cpu(readl(target));
785 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
786 sizehigh = be32_to_cpu(readl(target));
787
788 start = (high << 32) | low;
789 limit = (sizehigh << 32) | sizelow;
790
791 calgary_reserve_mem_region(dev, start, limit);
792}
793
794/*
795 * some regions of the IO address space do not get translated, so we
796 * must not give devices IO addresses in those regions. The regions
797 * are the 640KB-1MB region and the two PCI peripheral memory holes.
798 * Reserve all of them in the IOMMU bitmap to avoid giving them out
799 * later.
800 */
801static void __init calgary_reserve_regions(struct pci_dev *dev)
802{
803 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200804 u64 start;
805 struct iommu_table *tbl = dev->sysdata;
806
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100807 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
808 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200809
810 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200811 /* for CalIOC2 - avoid the entire first MB */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200812 if (is_calgary(dev->device)) {
813 start = (640 * 1024);
814 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
815 } else { /* calioc2 */
816 start = 0;
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200817 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200818 }
Jon Masone4650582006-06-26 13:58:14 +0200819 iommu_range_reserve(tbl, start, npages);
820
821 /* reserve the two PCI peripheral memory regions in IO space */
822 calgary_reserve_peripheral_mem_1(dev);
823 calgary_reserve_peripheral_mem_2(dev);
824}
825
826static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
827{
828 u64 val64;
829 u64 table_phys;
830 void __iomem *target;
831 int ret;
832 struct iommu_table *tbl;
833
834 /* build TCE tables for each PHB */
835 ret = build_tce_table(dev, bbar);
836 if (ret)
837 return ret;
838
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200839 tbl = dev->sysdata;
840 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
841 tce_free(tbl, 0, tbl->it_size);
842
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200843 if (is_calgary(dev->device))
844 tbl->chip_ops = &calgary_chip_ops;
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200845 else if (is_calioc2(dev->device))
846 tbl->chip_ops = &calioc2_chip_ops;
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200847 else
848 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200849
Jon Masone4650582006-06-26 13:58:14 +0200850 calgary_reserve_regions(dev);
851
852 /* set TARs for each PHB */
853 target = calgary_reg(bbar, tar_offset(dev->bus->number));
854 val64 = be64_to_cpu(readq(target));
855
856 /* zero out all TAR bits under sw control */
857 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200858 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200859
Jon Masone4650582006-06-26 13:58:14 +0200860 val64 |= table_phys;
861
862 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
863 val64 |= (u64) specified_table_size;
864
865 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200866
Jon Masone4650582006-06-26 13:58:14 +0200867 writeq(tbl->tar_val, target);
868 readq(target); /* flush */
869
870 return 0;
871}
872
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200873static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200874{
875 u64 val64;
876 struct iommu_table *tbl = dev->sysdata;
877 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200878 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200879
880 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
881 val64 = be64_to_cpu(readq(target));
882 val64 &= ~TAR_SW_BITS;
883 writeq(cpu_to_be64(val64), target);
884 readq(target); /* flush */
885
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200886 bitmapsz = tbl->it_size / BITS_PER_BYTE;
887 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
888 tbl->it_map = NULL;
889
Jon Masone4650582006-06-26 13:58:14 +0200890 kfree(tbl);
891 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200892
893 /* Can't free bootmem allocated memory after system is up :-( */
894 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200895}
896
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200897static void calgary_dump_error_regs(struct iommu_table *tbl)
898{
899 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200900 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200901 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200902
903 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200904 csr = be32_to_cpu(readl(target));
905
906 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
907 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200908
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, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200911 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200912}
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++) {
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200946 /* err regs are at 0x810 - 0x870 */
947 erroff = (0x810 + (i * 0x10));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200948 target = calgary_reg(bbar, phboff | erroff);
949 errregs[i] = be32_to_cpu(readl(target));
950 printk("0x%08x@0x%lx ", errregs[i], erroff);
951 }
952 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200953
954 /* root complex status */
955 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
956 rcstat = be32_to_cpu(readl(target));
957 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
958 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200959}
960
Jon Masone4650582006-06-26 13:58:14 +0200961static void calgary_watchdog(unsigned long data)
962{
963 struct pci_dev *dev = (struct pci_dev *)data;
964 struct iommu_table *tbl = dev->sysdata;
965 void __iomem *bbar = tbl->bbar;
966 u32 val32;
967 void __iomem *target;
968
969 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
970 val32 = be32_to_cpu(readl(target));
971
972 /* If no error, the agent ID in the CSR is not valid */
973 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200974 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200975
976 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200977 writel(0, target);
978
979 /* Disable bus that caused the error */
980 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200981 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200982 val32 = be32_to_cpu(readl(target));
983 val32 |= PHB_SLOT_DISABLE;
984 writel(cpu_to_be32(val32), target);
985 readl(target); /* flush */
986 } else {
987 /* Reset the timer */
988 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
989 }
990}
991
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200992static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
993 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200994{
995 u64 val64;
996 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100997 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200998 u64 mask;
999
1000 switch (busno_to_phbid(busnum)) {
1001 case 0: phb_shift = (63 - 19);
1002 break;
1003 case 1: phb_shift = (63 - 23);
1004 break;
1005 case 2: phb_shift = (63 - 27);
1006 break;
1007 case 3: phb_shift = (63 - 35);
1008 break;
1009 default:
1010 BUG_ON(busno_to_phbid(busnum));
1011 }
1012
1013 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1014 val64 = be64_to_cpu(readq(target));
1015
1016 /* zero out this PHB's timer bits */
1017 mask = ~(0xFUL << phb_shift);
1018 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001019 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001020 writeq(cpu_to_be64(val64), target);
1021 readq(target); /* flush */
1022}
1023
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001024static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1025{
1026 unsigned char busnum = dev->bus->number;
1027 void __iomem *bbar = tbl->bbar;
1028 void __iomem *target;
1029 u32 val;
1030
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +02001031 /*
1032 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1033 */
1034 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1035 val = cpu_to_be32(readl(target));
1036 val |= 0x00800000;
1037 writel(cpu_to_be32(val), target);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001038}
1039
1040static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001041{
1042 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001043
1044 /*
1045 * Give split completion a longer timeout on bus 1 for aic94xx
1046 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1047 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001048 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001049 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1050 CCR_2SEC_TIMEOUT);
1051}
1052
Jon Masone4650582006-06-26 13:58:14 +02001053static void __init calgary_enable_translation(struct pci_dev *dev)
1054{
1055 u32 val32;
1056 unsigned char busnum;
1057 void __iomem *target;
1058 void __iomem *bbar;
1059 struct iommu_table *tbl;
1060
1061 busnum = dev->bus->number;
1062 tbl = dev->sysdata;
1063 bbar = tbl->bbar;
1064
1065 /* enable TCE in PHB Config Register */
1066 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1067 val32 = be32_to_cpu(readl(target));
1068 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1069
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001070 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1071 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1072 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001073 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1074 "bus.\n");
1075
1076 writel(cpu_to_be32(val32), target);
1077 readl(target); /* flush */
1078
1079 init_timer(&tbl->watchdog_timer);
1080 tbl->watchdog_timer.function = &calgary_watchdog;
1081 tbl->watchdog_timer.data = (unsigned long)dev;
1082 mod_timer(&tbl->watchdog_timer, jiffies);
1083}
1084
1085static void __init calgary_disable_translation(struct pci_dev *dev)
1086{
1087 u32 val32;
1088 unsigned char busnum;
1089 void __iomem *target;
1090 void __iomem *bbar;
1091 struct iommu_table *tbl;
1092
1093 busnum = dev->bus->number;
1094 tbl = dev->sysdata;
1095 bbar = tbl->bbar;
1096
1097 /* disable TCE in PHB Config Register */
1098 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1099 val32 = be32_to_cpu(readl(target));
1100 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1101
Jon Mason70d666d2006-10-05 18:47:21 +02001102 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001103 writel(cpu_to_be32(val32), target);
1104 readl(target); /* flush */
1105
1106 del_timer_sync(&tbl->watchdog_timer);
1107}
1108
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001109static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001110{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001111 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +02001112 dev->sysdata = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001113
1114 /* is the device behind a bridge? */
1115 if (dev->bus->parent)
1116 dev->bus->parent->self = dev;
1117 else
1118 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001119}
1120
1121static int __init calgary_init_one(struct pci_dev *dev)
1122{
Jon Masone4650582006-06-26 13:58:14 +02001123 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001124 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001125 int ret;
1126
Jon Masondedc9932006-10-05 18:47:21 +02001127 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1128
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001129 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001130 ret = calgary_setup_tar(dev, bbar);
1131 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001132 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001133
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001134 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001135
1136 if (dev->bus->parent) {
1137 if (dev->bus->parent->self)
1138 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1139 "bus->parent->self!\n", dev);
1140 dev->bus->parent->self = dev;
1141 } else
1142 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001143
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001144 tbl = dev->sysdata;
1145 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001146
Jon Masone4650582006-06-26 13:58:14 +02001147 calgary_enable_translation(dev);
1148
1149 return 0;
1150
Jon Masone4650582006-06-26 13:58:14 +02001151done:
1152 return ret;
1153}
1154
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001155static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001156{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001157 int ret;
1158 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001159 void __iomem *bbar;
1160 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001161 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001162 u8 start_bus, end_bus;
1163 u32 val;
1164
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001165 ret = -ENODATA;
1166 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1167 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001168
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001169 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001170 continue;
1171
1172 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001173 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1174 if (!bbar)
1175 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001176
1177 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001178 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1179 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001180
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001181 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001182
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001183 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001184 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001185
1186 if (end_bus) {
1187 for (bus = start_bus; bus <= end_bus; bus++) {
1188 bus_info[bus].bbar = bbar;
1189 bus_info[bus].phbid = phb;
1190 }
1191 } else {
1192 bus_info[start_bus].bbar = bbar;
1193 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001194 }
1195 }
1196 }
1197
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001198 return 0;
1199
1200error:
1201 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1202 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1203 if (bus_info[bus].bbar)
1204 iounmap(bus_info[bus].bbar);
1205
1206 return ret;
1207}
1208
1209static int __init calgary_init(void)
1210{
1211 int ret;
1212 struct pci_dev *dev = NULL;
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +02001213 void *tce_space;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001214
1215 ret = calgary_locate_bbars();
1216 if (ret)
1217 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001218
Jon Masondedc9932006-10-05 18:47:21 +02001219 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001220 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001221 if (!dev)
1222 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001223 if (!is_cal_pci_dev(dev->device))
1224 continue;
Jon Masone4650582006-06-26 13:58:14 +02001225 if (!translate_phb(dev)) {
1226 calgary_init_one_nontraslated(dev);
1227 continue;
1228 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001229 tce_space = bus_info[dev->bus->number].tce_space;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001230 if (!tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001231 continue;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +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;
Jon Masond2105b12006-07-29 21:42:43 +02001424 }
Jon Masone4650582006-06-26 13:58:14 +02001425 }
1426
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001427 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1428 calgary_found ? "found" : "not found");
1429
Jon Masond2105b12006-07-29 21:42:43 +02001430 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001431 iommu_detected = 1;
1432 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001433 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1434 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1435 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1436 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001437 }
1438 return;
1439
1440cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001441 for (--bus; bus >= 0; --bus) {
1442 struct calgary_bus_info *info = &bus_info[bus];
1443
1444 if (info->tce_space)
1445 free_tce_table(info->tce_space);
1446 }
Jon Masone4650582006-06-26 13:58:14 +02001447}
1448
1449int __init calgary_iommu_init(void)
1450{
1451 int ret;
1452
1453 if (no_iommu || swiotlb)
1454 return -ENODEV;
1455
1456 if (!calgary_detected)
1457 return -ENODEV;
1458
1459 /* ok, we're trying to use Calgary - let's roll */
1460 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1461
1462 ret = calgary_init();
1463 if (ret) {
1464 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1465 "falling back to no_iommu\n", ret);
1466 if (end_pfn > MAX_DMA32_PFN)
1467 printk(KERN_ERR "WARNING more than 4GB of memory, "
1468 "32bit PCI may malfunction.\n");
1469 return ret;
1470 }
1471
1472 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001473 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001474 dma_ops = &calgary_dma_ops;
1475
1476 return 0;
1477}
1478
1479static int __init calgary_parse_options(char *p)
1480{
1481 unsigned int bridge;
1482 size_t len;
1483 char* endp;
1484
1485 while (*p) {
1486 if (!strncmp(p, "64k", 3))
1487 specified_table_size = TCE_TABLE_SIZE_64K;
1488 else if (!strncmp(p, "128k", 4))
1489 specified_table_size = TCE_TABLE_SIZE_128K;
1490 else if (!strncmp(p, "256k", 4))
1491 specified_table_size = TCE_TABLE_SIZE_256K;
1492 else if (!strncmp(p, "512k", 4))
1493 specified_table_size = TCE_TABLE_SIZE_512K;
1494 else if (!strncmp(p, "1M", 2))
1495 specified_table_size = TCE_TABLE_SIZE_1M;
1496 else if (!strncmp(p, "2M", 2))
1497 specified_table_size = TCE_TABLE_SIZE_2M;
1498 else if (!strncmp(p, "4M", 2))
1499 specified_table_size = TCE_TABLE_SIZE_4M;
1500 else if (!strncmp(p, "8M", 2))
1501 specified_table_size = TCE_TABLE_SIZE_8M;
1502
1503 len = strlen("translate_empty_slots");
1504 if (!strncmp(p, "translate_empty_slots", len))
1505 translate_empty_slots = 1;
1506
1507 len = strlen("disable");
1508 if (!strncmp(p, "disable", len)) {
1509 p += len;
1510 if (*p == '=')
1511 ++p;
1512 if (*p == '\0')
1513 break;
1514 bridge = simple_strtol(p, &endp, 0);
1515 if (p == endp)
1516 break;
1517
Jon Masond2105b12006-07-29 21:42:43 +02001518 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001519 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001520 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001521 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001522 }
1523 }
1524
1525 p = strpbrk(p, ",");
1526 if (!p)
1527 break;
1528
1529 p++; /* skip ',' */
1530 }
1531 return 1;
1532}
1533__setup("calgary=", calgary_parse_options);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001534
1535static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1536{
1537 struct iommu_table *tbl;
1538 unsigned int npages;
1539 int i;
1540
1541 tbl = dev->sysdata;
1542
1543 for (i = 0; i < 4; i++) {
1544 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1545
1546 /* Don't give out TCEs that map MEM resources */
1547 if (!(r->flags & IORESOURCE_MEM))
1548 continue;
1549
1550 /* 0-based? we reserve the whole 1st MB anyway */
1551 if (!r->start)
1552 continue;
1553
1554 /* cover the whole region */
1555 npages = (r->end - r->start) >> PAGE_SHIFT;
1556 npages++;
1557
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001558 iommu_range_reserve(tbl, r->start, npages);
1559 }
1560}
1561
1562static int __init calgary_fixup_tce_spaces(void)
1563{
1564 struct pci_dev *dev = NULL;
1565 void *tce_space;
1566
1567 if (no_iommu || swiotlb || !calgary_detected)
1568 return -ENODEV;
1569
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001570 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001571
1572 do {
1573 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1574 if (!dev)
1575 break;
1576 if (!is_cal_pci_dev(dev->device))
1577 continue;
1578 if (!translate_phb(dev))
1579 continue;
1580
1581 tce_space = bus_info[dev->bus->number].tce_space;
1582 if (!tce_space)
1583 continue;
1584
1585 calgary_fixup_one_tce_space(dev);
1586
1587 } while (1);
1588
1589 return 0;
1590}
1591
1592/*
1593 * We need to be call after pcibios_assign_resources (fs_initcall level)
1594 * and before device_initcall.
1595 */
1596rootfs_initcall(calgary_fixup_tce_spaces);