blob: 558e68aa6b05ca425ac7b0df6443fedf42257c12 [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
82/* PHB_CONFIG_RW */
83#define PHB_TCE_ENABLE 0x20000000
84#define PHB_SLOT_DISABLE 0x1C000000
85#define PHB_DAC_DISABLE 0x01000000
86#define PHB_MEM2_ENABLE 0x00400000
87#define PHB_MCSR_ENABLE 0x00100000
88/* TAR (Table Address Register) */
89#define TAR_SW_BITS 0x0000ffffffff800fUL
90#define TAR_VALID 0x0000000000000008UL
91/* CSR (Channel/DMA Status Register) */
92#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020093/* CCR (Calgary Configuration Register) */
94#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Jon Masone4650582006-06-26 13:58:14 +020095
96#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +020097#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +020098/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
99#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200100#define PHBS_PER_CALGARY 4
101
102/* register offsets in Calgary's internal register space */
103static const unsigned long tar_offsets[] = {
104 0x0580 /* TAR0 */,
105 0x0588 /* TAR1 */,
106 0x0590 /* TAR2 */,
107 0x0598 /* TAR3 */
108};
109
110static const unsigned long split_queue_offsets[] = {
111 0x4870 /* SPLIT QUEUE 0 */,
112 0x5870 /* SPLIT QUEUE 1 */,
113 0x6870 /* SPLIT QUEUE 2 */,
114 0x7870 /* SPLIT QUEUE 3 */
115};
116
117static const unsigned long phb_offsets[] = {
118 0x8000 /* PHB0 */,
119 0x9000 /* PHB1 */,
120 0xA000 /* PHB2 */,
121 0xB000 /* PHB3 */
122};
123
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100124/* PHB debug registers */
125
126static const unsigned long phb_debug_offsets[] = {
127 0x4000 /* PHB 0 DEBUG */,
128 0x5000 /* PHB 1 DEBUG */,
129 0x6000 /* PHB 2 DEBUG */,
130 0x7000 /* PHB 3 DEBUG */
131};
132
133/*
134 * STUFF register for each debug PHB,
135 * byte 1 = start bus number, byte 2 = end bus number
136 */
137
138#define PHB_DEBUG_STUFF_OFFSET 0x0020
139
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100140#define EMERGENCY_PAGES 32 /* = 128KB */
141
Jon Masone4650582006-06-26 13:58:14 +0200142unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
143static int translate_empty_slots __read_mostly = 0;
144static int calgary_detected __read_mostly = 0;
145
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100146static struct rio_table_hdr *rio_table_hdr __initdata;
147static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100148static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100149
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200150struct calgary_bus_info {
151 void *tce_space;
Muli Ben-Yehuda0577f1482006-09-26 10:52:31 +0200152 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200153 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100154 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200155};
156
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200157static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
158static void calgary_tce_cache_blast(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200159
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200160static struct cal_chipset_ops calgary_chip_ops = {
161 .handle_quirks = calgary_handle_quirks,
162 .tce_cache_blast = calgary_tce_cache_blast
163};
164
165static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200166
167/* enable this to stress test the chip's TCE cache */
168#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200169int debugging __read_mostly = 1;
170
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200171static inline unsigned long verify_bit_range(unsigned long* bitmap,
172 int expected, unsigned long start, unsigned long end)
173{
174 unsigned long idx = start;
175
176 BUG_ON(start >= end);
177
178 while (idx < end) {
179 if (!!test_bit(idx, bitmap) != expected)
180 return idx;
181 ++idx;
182 }
183
184 /* all bits have the expected value */
185 return ~0UL;
186}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200187#else /* debugging is disabled */
188int debugging __read_mostly = 0;
189
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200190static inline unsigned long verify_bit_range(unsigned long* bitmap,
191 int expected, unsigned long start, unsigned long end)
192{
193 return ~0UL;
194}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200195
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200196#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200197
198static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
199{
200 unsigned int npages;
201
202 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
203 npages >>= PAGE_SHIFT;
204
205 return npages;
206}
207
208static inline int translate_phb(struct pci_dev* dev)
209{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200210 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200211 return !disabled;
212}
213
214static void iommu_range_reserve(struct iommu_table *tbl,
215 unsigned long start_addr, unsigned int npages)
216{
217 unsigned long index;
218 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200219 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200220
221 index = start_addr >> PAGE_SHIFT;
222
223 /* bail out if we're asked to reserve a region we don't cover */
224 if (index >= tbl->it_size)
225 return;
226
227 end = index + npages;
228 if (end > tbl->it_size) /* don't go off the table */
229 end = tbl->it_size;
230
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200231 badbit = verify_bit_range(tbl->it_map, 0, index, end);
232 if (badbit != ~0UL) {
233 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200234 printk(KERN_ERR "Calgary: entry already allocated at "
235 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200236 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200237 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200238
239 set_bit_string(tbl->it_map, index, npages);
Jon Masone4650582006-06-26 13:58:14 +0200240}
241
242static unsigned long iommu_range_alloc(struct iommu_table *tbl,
243 unsigned int npages)
244{
245 unsigned long offset;
246
247 BUG_ON(npages == 0);
248
249 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
250 tbl->it_size, npages);
251 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200252 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200253 offset = find_next_zero_string(tbl->it_map, 0,
254 tbl->it_size, npages);
255 if (offset == ~0UL) {
256 printk(KERN_WARNING "Calgary: IOMMU full.\n");
257 if (panic_on_overflow)
258 panic("Calgary: fix the allocator.\n");
259 else
260 return bad_dma_address;
261 }
262 }
263
264 set_bit_string(tbl->it_map, offset, npages);
265 tbl->it_hint = offset + npages;
266 BUG_ON(tbl->it_hint > tbl->it_size);
267
268 return offset;
269}
270
271static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
272 unsigned int npages, int direction)
273{
274 unsigned long entry, flags;
275 dma_addr_t ret = bad_dma_address;
276
277 spin_lock_irqsave(&tbl->it_lock, flags);
278
279 entry = iommu_range_alloc(tbl, npages);
280
281 if (unlikely(entry == bad_dma_address))
282 goto error;
283
284 /* set the return dma address */
285 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
286
287 /* put the TCEs in the HW table */
288 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
289 direction);
290
291 spin_unlock_irqrestore(&tbl->it_lock, flags);
292
293 return ret;
294
295error:
296 spin_unlock_irqrestore(&tbl->it_lock, flags);
297 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
298 "iommu %p\n", npages, tbl);
299 return bad_dma_address;
300}
301
302static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
303 unsigned int npages)
304{
305 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200306 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100307 unsigned long badend;
308
309 /* were we called with bad_dma_address? */
310 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
311 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
312 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
313 "address 0x%Lx\n", dma_addr);
314 WARN_ON(1);
315 return;
316 }
Jon Masone4650582006-06-26 13:58:14 +0200317
318 entry = dma_addr >> PAGE_SHIFT;
319
320 BUG_ON(entry + npages > tbl->it_size);
321
322 tce_free(tbl, entry, npages);
323
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200324 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
325 if (badbit != ~0UL) {
326 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200327 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
328 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200329 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200330 }
331
332 __clear_bit_string(tbl->it_map, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200333}
334
335static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
336 unsigned int npages)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(&tbl->it_lock, flags);
341
342 __iommu_free(tbl, dma_addr, npages);
343
344 spin_unlock_irqrestore(&tbl->it_lock, flags);
345}
346
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200347static inline struct iommu_table *find_iommu_table(struct device *dev)
348{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200349 struct pci_dev *pdev;
350 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200351 struct iommu_table *tbl;
352
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200353 pdev = to_pci_dev(dev);
354
355 /* is the device behind a bridge? */
356 if (unlikely(pdev->bus->parent))
357 pbus = pdev->bus->parent;
358 else
359 pbus = pdev->bus;
360
361 tbl = pbus->self->sysdata;
362 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200363
364 return tbl;
365}
366
Jon Masone4650582006-06-26 13:58:14 +0200367static void __calgary_unmap_sg(struct iommu_table *tbl,
368 struct scatterlist *sglist, int nelems, int direction)
369{
370 while (nelems--) {
371 unsigned int npages;
372 dma_addr_t dma = sglist->dma_address;
373 unsigned int dmalen = sglist->dma_length;
374
375 if (dmalen == 0)
376 break;
377
378 npages = num_dma_pages(dma, dmalen);
379 __iommu_free(tbl, dma, npages);
380 sglist++;
381 }
382}
383
384void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
385 int nelems, int direction)
386{
387 unsigned long flags;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200388 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200389
390 if (!translate_phb(to_pci_dev(dev)))
391 return;
392
393 spin_lock_irqsave(&tbl->it_lock, flags);
394
395 __calgary_unmap_sg(tbl, sglist, nelems, direction);
396
397 spin_unlock_irqrestore(&tbl->it_lock, flags);
398}
399
400static int calgary_nontranslate_map_sg(struct device* dev,
401 struct scatterlist *sg, int nelems, int direction)
402{
403 int i;
404
405 for (i = 0; i < nelems; i++ ) {
406 struct scatterlist *s = &sg[i];
407 BUG_ON(!s->page);
408 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
409 s->dma_length = s->length;
410 }
411 return nelems;
412}
413
414int calgary_map_sg(struct device *dev, struct scatterlist *sg,
415 int nelems, int direction)
416{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200417 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200418 unsigned long flags;
419 unsigned long vaddr;
420 unsigned int npages;
421 unsigned long entry;
422 int i;
423
424 if (!translate_phb(to_pci_dev(dev)))
425 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
426
427 spin_lock_irqsave(&tbl->it_lock, flags);
428
429 for (i = 0; i < nelems; i++ ) {
430 struct scatterlist *s = &sg[i];
431 BUG_ON(!s->page);
432
433 vaddr = (unsigned long)page_address(s->page) + s->offset;
434 npages = num_dma_pages(vaddr, s->length);
435
436 entry = iommu_range_alloc(tbl, npages);
437 if (entry == bad_dma_address) {
438 /* makes sure unmap knows to stop */
439 s->dma_length = 0;
440 goto error;
441 }
442
443 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
444
445 /* insert into HW table */
446 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
447 direction);
448
449 s->dma_length = s->length;
450 }
451
452 spin_unlock_irqrestore(&tbl->it_lock, flags);
453
454 return nelems;
455error:
456 __calgary_unmap_sg(tbl, sg, nelems, direction);
457 for (i = 0; i < nelems; i++) {
458 sg[i].dma_address = bad_dma_address;
459 sg[i].dma_length = 0;
460 }
461 spin_unlock_irqrestore(&tbl->it_lock, flags);
462 return 0;
463}
464
465dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
466 size_t size, int direction)
467{
468 dma_addr_t dma_handle = bad_dma_address;
469 unsigned long uaddr;
470 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200471 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200472
473 uaddr = (unsigned long)vaddr;
474 npages = num_dma_pages(uaddr, size);
475
476 if (translate_phb(to_pci_dev(dev)))
477 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
478 else
479 dma_handle = virt_to_bus(vaddr);
480
481 return dma_handle;
482}
483
484void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
485 size_t size, int direction)
486{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200487 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200488 unsigned int npages;
489
490 if (!translate_phb(to_pci_dev(dev)))
491 return;
492
493 npages = num_dma_pages(dma_handle, size);
494 iommu_free(tbl, dma_handle, npages);
495}
496
497void* calgary_alloc_coherent(struct device *dev, size_t size,
498 dma_addr_t *dma_handle, gfp_t flag)
499{
500 void *ret = NULL;
501 dma_addr_t mapping;
502 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200503 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200504
505 size = PAGE_ALIGN(size); /* size rounded up to full pages */
506 npages = size >> PAGE_SHIFT;
507 order = get_order(size);
508
509 /* alloc enough pages (and possibly more) */
510 ret = (void *)__get_free_pages(flag, order);
511 if (!ret)
512 goto error;
513 memset(ret, 0, size);
514
515 if (translate_phb(to_pci_dev(dev))) {
516 /* set up tces to cover the allocated range */
517 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
518 if (mapping == bad_dma_address)
519 goto free;
520
521 *dma_handle = mapping;
522 } else /* non translated slot */
523 *dma_handle = virt_to_bus(ret);
524
525 return ret;
526
527free:
528 free_pages((unsigned long)ret, get_order(size));
529 ret = NULL;
530error:
531 return ret;
532}
533
Stephen Hemmingere6584502007-05-02 19:27:06 +0200534static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200535 .alloc_coherent = calgary_alloc_coherent,
536 .map_single = calgary_map_single,
537 .unmap_single = calgary_unmap_single,
538 .map_sg = calgary_map_sg,
539 .unmap_sg = calgary_unmap_sg,
540};
541
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100542static inline void __iomem * busno_to_bbar(unsigned char num)
543{
544 return bus_info[num].bbar;
545}
546
Jon Masone4650582006-06-26 13:58:14 +0200547static inline int busno_to_phbid(unsigned char num)
548{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200549 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200550}
551
552static inline unsigned long split_queue_offset(unsigned char num)
553{
554 size_t idx = busno_to_phbid(num);
555
556 return split_queue_offsets[idx];
557}
558
559static inline unsigned long tar_offset(unsigned char num)
560{
561 size_t idx = busno_to_phbid(num);
562
563 return tar_offsets[idx];
564}
565
566static inline unsigned long phb_offset(unsigned char num)
567{
568 size_t idx = busno_to_phbid(num);
569
570 return phb_offsets[idx];
571}
572
573static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
574{
575 unsigned long target = ((unsigned long)bar) | offset;
576 return (void __iomem*)target;
577}
578
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200579static inline int is_calioc2(unsigned short device)
580{
581 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
582}
583
584static inline int is_calgary(unsigned short device)
585{
586 return (device == PCI_DEVICE_ID_IBM_CALGARY);
587}
588
589static inline int is_cal_pci_dev(unsigned short device)
590{
591 return (is_calgary(device) || is_calioc2(device));
592}
593
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200594static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200595{
596 u64 val;
597 u32 aer;
598 int i = 0;
599 void __iomem *bbar = tbl->bbar;
600 void __iomem *target;
601
602 /* disable arbitration on the bus */
603 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
604 aer = readl(target);
605 writel(0, target);
606
607 /* read plssr to ensure it got there */
608 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
609 val = readl(target);
610
611 /* poll split queues until all DMA activity is done */
612 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
613 do {
614 val = readq(target);
615 i++;
616 } while ((val & 0xff) != 0xff && i < 100);
617 if (i == 100)
618 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
619 "continuing anyway\n");
620
621 /* invalidate TCE cache */
622 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
623 writeq(tbl->tar_val, target);
624
625 /* enable arbitration */
626 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
627 writel(aer, target);
628 (void)readl(target); /* flush */
629}
630
631static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
632 u64 limit)
633{
634 unsigned int numpages;
635
636 limit = limit | 0xfffff;
637 limit++;
638
639 numpages = ((limit - start) >> PAGE_SHIFT);
640 iommu_range_reserve(dev->sysdata, start, numpages);
641}
642
643static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
644{
645 void __iomem *target;
646 u64 low, high, sizelow;
647 u64 start, limit;
648 struct iommu_table *tbl = dev->sysdata;
649 unsigned char busnum = dev->bus->number;
650 void __iomem *bbar = tbl->bbar;
651
652 /* peripheral MEM_1 region */
653 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
654 low = be32_to_cpu(readl(target));
655 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
656 high = be32_to_cpu(readl(target));
657 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
658 sizelow = be32_to_cpu(readl(target));
659
660 start = (high << 32) | low;
661 limit = sizelow;
662
663 calgary_reserve_mem_region(dev, start, limit);
664}
665
666static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
667{
668 void __iomem *target;
669 u32 val32;
670 u64 low, high, sizelow, sizehigh;
671 u64 start, limit;
672 struct iommu_table *tbl = dev->sysdata;
673 unsigned char busnum = dev->bus->number;
674 void __iomem *bbar = tbl->bbar;
675
676 /* is it enabled? */
677 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
678 val32 = be32_to_cpu(readl(target));
679 if (!(val32 & PHB_MEM2_ENABLE))
680 return;
681
682 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
683 low = be32_to_cpu(readl(target));
684 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
685 high = be32_to_cpu(readl(target));
686 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
687 sizelow = be32_to_cpu(readl(target));
688 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
689 sizehigh = be32_to_cpu(readl(target));
690
691 start = (high << 32) | low;
692 limit = (sizehigh << 32) | sizelow;
693
694 calgary_reserve_mem_region(dev, start, limit);
695}
696
697/*
698 * some regions of the IO address space do not get translated, so we
699 * must not give devices IO addresses in those regions. The regions
700 * are the 640KB-1MB region and the two PCI peripheral memory holes.
701 * Reserve all of them in the IOMMU bitmap to avoid giving them out
702 * later.
703 */
704static void __init calgary_reserve_regions(struct pci_dev *dev)
705{
706 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200707 u64 start;
708 struct iommu_table *tbl = dev->sysdata;
709
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100710 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
711 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200712
713 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200714 /* for CalIOC2 - avoid the entire first 2MB */
715 if (is_calgary(dev->device)) {
716 start = (640 * 1024);
717 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
718 } else { /* calioc2 */
719 start = 0;
720 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
721 }
Jon Masone4650582006-06-26 13:58:14 +0200722 iommu_range_reserve(tbl, start, npages);
723
724 /* reserve the two PCI peripheral memory regions in IO space */
725 calgary_reserve_peripheral_mem_1(dev);
726 calgary_reserve_peripheral_mem_2(dev);
727}
728
729static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
730{
731 u64 val64;
732 u64 table_phys;
733 void __iomem *target;
734 int ret;
735 struct iommu_table *tbl;
736
737 /* build TCE tables for each PHB */
738 ret = build_tce_table(dev, bbar);
739 if (ret)
740 return ret;
741
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200742 tbl = dev->sysdata;
743 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
744 tce_free(tbl, 0, tbl->it_size);
745
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200746 tbl->chip_ops = &calgary_chip_ops;
747
Jon Masone4650582006-06-26 13:58:14 +0200748 calgary_reserve_regions(dev);
749
750 /* set TARs for each PHB */
751 target = calgary_reg(bbar, tar_offset(dev->bus->number));
752 val64 = be64_to_cpu(readq(target));
753
754 /* zero out all TAR bits under sw control */
755 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200756 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200757
Jon Masone4650582006-06-26 13:58:14 +0200758 val64 |= table_phys;
759
760 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
761 val64 |= (u64) specified_table_size;
762
763 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200764
Jon Masone4650582006-06-26 13:58:14 +0200765 writeq(tbl->tar_val, target);
766 readq(target); /* flush */
767
768 return 0;
769}
770
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200771static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200772{
773 u64 val64;
774 struct iommu_table *tbl = dev->sysdata;
775 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200776 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200777
778 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
779 val64 = be64_to_cpu(readq(target));
780 val64 &= ~TAR_SW_BITS;
781 writeq(cpu_to_be64(val64), target);
782 readq(target); /* flush */
783
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200784 bitmapsz = tbl->it_size / BITS_PER_BYTE;
785 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
786 tbl->it_map = NULL;
787
Jon Masone4650582006-06-26 13:58:14 +0200788 kfree(tbl);
789 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200790
791 /* Can't free bootmem allocated memory after system is up :-( */
792 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200793}
794
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200795static void calgary_dump_error_regs(struct iommu_table *tbl)
796{
797 void __iomem *bbar = tbl->bbar;
798 u32 csr, csmr, plssr, mck;
799 void __iomem *target;
800 unsigned long phboff = phb_offset(tbl->it_busno);
801 unsigned long erroff;
802 u32 errregs[7];
803 int i;
804
805 /* dump CSR */
806 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
807 csr = be32_to_cpu(readl(target));
808 /* dump PLSSR */
809 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
810 plssr = be32_to_cpu(readl(target));
811 /* dump CSMR */
812 target = calgary_reg(bbar, phboff | 0x290);
813 csmr = be32_to_cpu(readl(target));
814 /* dump mck */
815 target = calgary_reg(bbar, phboff | 0x800);
816 mck = be32_to_cpu(readl(target));
817
818 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR "
819 "0x%08x@MCK\n", csr, plssr, csmr, mck);
820
821 /* dump rest of error regs */
822 printk(KERN_EMERG "Calgary: ");
823 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
824 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
825 target = calgary_reg(bbar, phboff | erroff);
826 errregs[i] = be32_to_cpu(readl(target));
827 printk("0x%08x@0x%lx ", errregs[i], erroff);
828 }
829 printk("\n");
830}
831
Jon Masone4650582006-06-26 13:58:14 +0200832static void calgary_watchdog(unsigned long data)
833{
834 struct pci_dev *dev = (struct pci_dev *)data;
835 struct iommu_table *tbl = dev->sysdata;
836 void __iomem *bbar = tbl->bbar;
837 u32 val32;
838 void __iomem *target;
839
840 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
841 val32 = be32_to_cpu(readl(target));
842
843 /* If no error, the agent ID in the CSR is not valid */
844 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200845 printk(KERN_EMERG "Calgary: DMA error on PHB %#x\n",
846 dev->bus->number);
847 calgary_dump_error_regs(tbl);
848
849 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200850 writel(0, target);
851
852 /* Disable bus that caused the error */
853 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200854 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200855 val32 = be32_to_cpu(readl(target));
856 val32 |= PHB_SLOT_DISABLE;
857 writel(cpu_to_be32(val32), target);
858 readl(target); /* flush */
859 } else {
860 /* Reset the timer */
861 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
862 }
863}
864
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200865static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
866 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200867{
868 u64 val64;
869 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100870 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200871 u64 mask;
872
873 switch (busno_to_phbid(busnum)) {
874 case 0: phb_shift = (63 - 19);
875 break;
876 case 1: phb_shift = (63 - 23);
877 break;
878 case 2: phb_shift = (63 - 27);
879 break;
880 case 3: phb_shift = (63 - 35);
881 break;
882 default:
883 BUG_ON(busno_to_phbid(busnum));
884 }
885
886 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
887 val64 = be64_to_cpu(readq(target));
888
889 /* zero out this PHB's timer bits */
890 mask = ~(0xFUL << phb_shift);
891 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200892 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200893 writeq(cpu_to_be64(val64), target);
894 readq(target); /* flush */
895}
896
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200897static void __init calgary_handle_quirks(struct iommu_table *tbl,
898 struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200899{
900 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200901
902 /*
903 * Give split completion a longer timeout on bus 1 for aic94xx
904 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
905 */
906 if (busnum == 1)
907 calgary_set_split_completion_timeout(tbl->bbar, busnum,
908 CCR_2SEC_TIMEOUT);
909}
910
Jon Masone4650582006-06-26 13:58:14 +0200911static void __init calgary_enable_translation(struct pci_dev *dev)
912{
913 u32 val32;
914 unsigned char busnum;
915 void __iomem *target;
916 void __iomem *bbar;
917 struct iommu_table *tbl;
918
919 busnum = dev->bus->number;
920 tbl = dev->sysdata;
921 bbar = tbl->bbar;
922
923 /* enable TCE in PHB Config Register */
924 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
925 val32 = be32_to_cpu(readl(target));
926 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
927
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200928 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
929 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
930 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +0200931 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
932 "bus.\n");
933
934 writel(cpu_to_be32(val32), target);
935 readl(target); /* flush */
936
937 init_timer(&tbl->watchdog_timer);
938 tbl->watchdog_timer.function = &calgary_watchdog;
939 tbl->watchdog_timer.data = (unsigned long)dev;
940 mod_timer(&tbl->watchdog_timer, jiffies);
941}
942
943static void __init calgary_disable_translation(struct pci_dev *dev)
944{
945 u32 val32;
946 unsigned char busnum;
947 void __iomem *target;
948 void __iomem *bbar;
949 struct iommu_table *tbl;
950
951 busnum = dev->bus->number;
952 tbl = dev->sysdata;
953 bbar = tbl->bbar;
954
955 /* disable TCE in PHB Config Register */
956 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
957 val32 = be32_to_cpu(readl(target));
958 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
959
Jon Mason70d666d2006-10-05 18:47:21 +0200960 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +0200961 writel(cpu_to_be32(val32), target);
962 readl(target); /* flush */
963
964 del_timer_sync(&tbl->watchdog_timer);
965}
966
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +0200967static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200968{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200969 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200970 dev->sysdata = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200971
972 /* is the device behind a bridge? */
973 if (dev->bus->parent)
974 dev->bus->parent->self = dev;
975 else
976 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +0200977}
978
979static int __init calgary_init_one(struct pci_dev *dev)
980{
Jon Masone4650582006-06-26 13:58:14 +0200981 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200982 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +0200983 int ret;
984
Jon Masondedc9932006-10-05 18:47:21 +0200985 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
986
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100987 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +0200988 ret = calgary_setup_tar(dev, bbar);
989 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100990 goto done;
Jon Masone4650582006-06-26 13:58:14 +0200991
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200992 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200993
994 if (dev->bus->parent) {
995 if (dev->bus->parent->self)
996 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
997 "bus->parent->self!\n", dev);
998 dev->bus->parent->self = dev;
999 } else
1000 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001001
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001002 tbl = dev->sysdata;
1003 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001004
Jon Masone4650582006-06-26 13:58:14 +02001005 calgary_enable_translation(dev);
1006
1007 return 0;
1008
Jon Masone4650582006-06-26 13:58:14 +02001009done:
1010 return ret;
1011}
1012
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001013static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001014{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001015 int ret;
1016 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001017 void __iomem *bbar;
1018 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001019 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001020 u8 start_bus, end_bus;
1021 u32 val;
1022
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001023 ret = -ENODATA;
1024 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1025 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001026
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001027 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001028 continue;
1029
1030 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001031 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1032 if (!bbar)
1033 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001034
1035 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001036 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1037 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001038
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001039 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001040
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001041 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001042 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001043
1044 if (end_bus) {
1045 for (bus = start_bus; bus <= end_bus; bus++) {
1046 bus_info[bus].bbar = bbar;
1047 bus_info[bus].phbid = phb;
1048 }
1049 } else {
1050 bus_info[start_bus].bbar = bbar;
1051 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001052 }
1053 }
1054 }
1055
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001056 return 0;
1057
1058error:
1059 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1060 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1061 if (bus_info[bus].bbar)
1062 iounmap(bus_info[bus].bbar);
1063
1064 return ret;
1065}
1066
1067static int __init calgary_init(void)
1068{
1069 int ret;
1070 struct pci_dev *dev = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001071 void* tce_space;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001072
1073 ret = calgary_locate_bbars();
1074 if (ret)
1075 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001076
Jon Masondedc9932006-10-05 18:47:21 +02001077 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001078 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001079 if (!dev)
1080 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001081 if (!is_cal_pci_dev(dev->device))
1082 continue;
Jon Masone4650582006-06-26 13:58:14 +02001083 if (!translate_phb(dev)) {
1084 calgary_init_one_nontraslated(dev);
1085 continue;
1086 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001087 tce_space = bus_info[dev->bus->number].tce_space;
1088 if (!tce_space && !translate_empty_slots) {
1089 printk("Calg: %p failed tce_space check\n", dev);
Jon Masone4650582006-06-26 13:58:14 +02001090 continue;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001091 }
Jon Masone4650582006-06-26 13:58:14 +02001092 ret = calgary_init_one(dev);
1093 if (ret)
1094 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001095 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001096
1097 return ret;
1098
1099error:
Jon Masondedc9932006-10-05 18:47:21 +02001100 do {
Alan Cox7cd8b682006-12-07 02:14:03 +01001101 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001102 PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001103 if (!dev)
1104 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001105 if (!is_cal_pci_dev(dev->device))
1106 continue;
Jon Masone4650582006-06-26 13:58:14 +02001107 if (!translate_phb(dev)) {
1108 pci_dev_put(dev);
1109 continue;
1110 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001111 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001112 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001113
Jon Masone4650582006-06-26 13:58:14 +02001114 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001115 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001116 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001117 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001118
1119 return ret;
1120}
1121
1122static inline int __init determine_tce_table_size(u64 ram)
1123{
1124 int ret;
1125
1126 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1127 return specified_table_size;
1128
1129 /*
1130 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1131 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1132 * larger table size has twice as many entries, so shift the
1133 * max ram address by 13 to divide by 8K and then look at the
1134 * order of the result to choose between 0-7.
1135 */
1136 ret = get_order(ram >> 13);
1137 if (ret > TCE_TABLE_SIZE_8M)
1138 ret = TCE_TABLE_SIZE_8M;
1139
1140 return ret;
1141}
1142
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001143static int __init build_detail_arrays(void)
1144{
1145 unsigned long ptr;
1146 int i, scal_detail_size, rio_detail_size;
1147
1148 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1149 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001150 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001151 "but system has %d nodes.\n",
1152 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1153 return -ENODEV;
1154 }
1155
1156 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001157 case 2:
1158 scal_detail_size = 11;
1159 rio_detail_size = 13;
1160 break;
1161 case 3:
1162 scal_detail_size = 12;
1163 rio_detail_size = 15;
1164 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001165 default:
1166 printk(KERN_WARNING
1167 "Calgary: Invalid Rio Grande Table Version: %d\n",
1168 rio_table_hdr->version);
1169 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001170 }
1171
1172 ptr = ((unsigned long)rio_table_hdr) + 3;
1173 for (i = 0; i < rio_table_hdr->num_scal_dev;
1174 i++, ptr += scal_detail_size)
1175 scal_devs[i] = (struct scal_detail *)ptr;
1176
1177 for (i = 0; i < rio_table_hdr->num_rio_dev;
1178 i++, ptr += rio_detail_size)
1179 rio_devs[i] = (struct rio_detail *)ptr;
1180
1181 return 0;
1182}
1183
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001184static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1185{
1186 int dev;
1187 u32 val;
1188
1189 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1190 /*
1191 * FIXME: properly scan for devices accross the
1192 * PCI-to-PCI bridge on every CalIOC2 port.
1193 */
1194 return 1;
1195 }
1196
1197 for (dev = 1; dev < 8; dev++) {
1198 val = read_pci_config(bus, dev, 0, 0);
1199 if (val != 0xffffffff)
1200 break;
1201 }
1202 return (val != 0xffffffff);
1203}
1204
Jon Masone4650582006-06-26 13:58:14 +02001205void __init detect_calgary(void)
1206{
Jon Masond2105b12006-07-29 21:42:43 +02001207 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001208 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001209 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001210 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001211 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001212 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001213
1214 /*
1215 * if the user specified iommu=off or iommu=soft or we found
1216 * another HW IOMMU already, bail out.
1217 */
1218 if (swiotlb || no_iommu || iommu_detected)
1219 return;
1220
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001221 if (!use_calgary)
1222 return;
1223
Andi Kleen0637a702006-09-26 10:52:41 +02001224 if (!early_pci_allowed())
1225 return;
1226
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001227 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1228
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001229 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1230
1231 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001232 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001233 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001234 /*
1235 * The next offset is stored in the 1st word.
1236 * Only parse up until the offset increases:
1237 */
1238 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001239 /* The block id is stored in the 2nd word */
1240 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1241 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001242 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001243 break;
1244 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001245 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001246 offset = *((unsigned short *)(ptr + offset));
1247 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001248 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001249 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1250 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001251 return;
1252 }
1253
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001254 ret = build_detail_arrays();
1255 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001256 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001257 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001258 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001259
Jon Masone4650582006-06-26 13:58:14 +02001260 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1261
Jon Masond2105b12006-07-29 21:42:43 +02001262 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001263 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001264 unsigned short pci_device;
1265 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001266
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001267 val = read_pci_config(bus, 0, 0, 0);
1268 pci_device = (val & 0xFFFF0000) >> 16;
1269
1270 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001271 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001272
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001273 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001274 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001275
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001276 if (calgary_bus_has_devices(bus, pci_device) ||
1277 translate_empty_slots) {
1278 tbl = alloc_tce_table();
1279 if (!tbl)
1280 goto cleanup;
1281 info->tce_space = tbl;
1282 calgary_found = 1;
1283 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1284 info->tce_space, bus);
Jon Masond2105b12006-07-29 21:42:43 +02001285 }
Jon Masone4650582006-06-26 13:58:14 +02001286 }
1287
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001288 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1289 calgary_found ? "found" : "not found");
1290
Jon Masond2105b12006-07-29 21:42:43 +02001291 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001292 iommu_detected = 1;
1293 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001294 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1295 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1296 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1297 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001298 }
1299 return;
1300
1301cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001302 for (--bus; bus >= 0; --bus) {
1303 struct calgary_bus_info *info = &bus_info[bus];
1304
1305 if (info->tce_space)
1306 free_tce_table(info->tce_space);
1307 }
Jon Masone4650582006-06-26 13:58:14 +02001308}
1309
1310int __init calgary_iommu_init(void)
1311{
1312 int ret;
1313
1314 if (no_iommu || swiotlb)
1315 return -ENODEV;
1316
1317 if (!calgary_detected)
1318 return -ENODEV;
1319
1320 /* ok, we're trying to use Calgary - let's roll */
1321 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1322
1323 ret = calgary_init();
1324 if (ret) {
1325 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1326 "falling back to no_iommu\n", ret);
1327 if (end_pfn > MAX_DMA32_PFN)
1328 printk(KERN_ERR "WARNING more than 4GB of memory, "
1329 "32bit PCI may malfunction.\n");
1330 return ret;
1331 }
1332
1333 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001334 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001335 dma_ops = &calgary_dma_ops;
1336
1337 return 0;
1338}
1339
1340static int __init calgary_parse_options(char *p)
1341{
1342 unsigned int bridge;
1343 size_t len;
1344 char* endp;
1345
1346 while (*p) {
1347 if (!strncmp(p, "64k", 3))
1348 specified_table_size = TCE_TABLE_SIZE_64K;
1349 else if (!strncmp(p, "128k", 4))
1350 specified_table_size = TCE_TABLE_SIZE_128K;
1351 else if (!strncmp(p, "256k", 4))
1352 specified_table_size = TCE_TABLE_SIZE_256K;
1353 else if (!strncmp(p, "512k", 4))
1354 specified_table_size = TCE_TABLE_SIZE_512K;
1355 else if (!strncmp(p, "1M", 2))
1356 specified_table_size = TCE_TABLE_SIZE_1M;
1357 else if (!strncmp(p, "2M", 2))
1358 specified_table_size = TCE_TABLE_SIZE_2M;
1359 else if (!strncmp(p, "4M", 2))
1360 specified_table_size = TCE_TABLE_SIZE_4M;
1361 else if (!strncmp(p, "8M", 2))
1362 specified_table_size = TCE_TABLE_SIZE_8M;
1363
1364 len = strlen("translate_empty_slots");
1365 if (!strncmp(p, "translate_empty_slots", len))
1366 translate_empty_slots = 1;
1367
1368 len = strlen("disable");
1369 if (!strncmp(p, "disable", len)) {
1370 p += len;
1371 if (*p == '=')
1372 ++p;
1373 if (*p == '\0')
1374 break;
1375 bridge = simple_strtol(p, &endp, 0);
1376 if (p == endp)
1377 break;
1378
Jon Masond2105b12006-07-29 21:42:43 +02001379 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001380 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001381 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001382 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001383 }
1384 }
1385
1386 p = strpbrk(p, ",");
1387 if (!p)
1388 break;
1389
1390 p++; /* skip ',' */
1391 }
1392 return 1;
1393}
1394__setup("calgary=", calgary_parse_options);