blob: 68634f192de89fd9656ea1d828adcd5ecfbc2eae [file] [log] [blame]
Jon Masone4650582006-06-26 13:58:14 +02001/*
2 * Derived from arch/powerpc/kernel/iommu.c
3 *
Muli Ben-Yehuda98822342007-07-21 17:10:48 +02004 * Copyright IBM Corporation, 2006-2007
Jon Masond8d2bed2006-10-05 18:47:21 +02005 * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
Jon Masone4650582006-06-26 13:58:14 +02006 *
Jon Masond8d2bed2006-10-05 18:47:21 +02007 * Author: Jon Mason <jdmason@kudzu.us>
Muli Ben-Yehudaaa0a9f32006-07-10 17:06:15 +02008 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
9
Jon Masone4650582006-06-26 13:58:14 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
Jon Masone4650582006-06-26 13:58:14 +020025#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/spinlock.h>
31#include <linux/string.h>
32#include <linux/dma-mapping.h>
33#include <linux/init.h>
34#include <linux/bitops.h>
35#include <linux/pci_ids.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <asm/proto.h>
39#include <asm/calgary.h>
40#include <asm/tce.h>
41#include <asm/pci-direct.h>
42#include <asm/system.h>
43#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010044#include <asm/rio.h>
Jon Masone4650582006-06-26 13:58:14 +020045
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010046#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
47int use_calgary __read_mostly = 1;
48#else
49int use_calgary __read_mostly = 0;
50#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
51
Jon Masone4650582006-06-26 13:58:14 +020052#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020053#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020054
55/* we need these for register space address calculation */
56#define START_ADDRESS 0xfe000000
57#define CHASSIS_BASE 0
58#define ONE_BASED_CHASSIS_NUM 1
59
60/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020061#define CALGARY_CONFIG_REG 0x0108
62#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020063#define PHB_PLSSR_OFFSET 0x0120
64#define PHB_CONFIG_RW_OFFSET 0x0160
65#define PHB_IOBASE_BAR_LOW 0x0170
66#define PHB_IOBASE_BAR_HIGH 0x0180
67#define PHB_MEM_1_LOW 0x0190
68#define PHB_MEM_1_HIGH 0x01A0
69#define PHB_IO_ADDR_SIZE 0x01B0
70#define PHB_MEM_1_SIZE 0x01C0
71#define PHB_MEM_ST_OFFSET 0x01D0
72#define PHB_AER_OFFSET 0x0200
73#define PHB_CONFIG_0_HIGH 0x0220
74#define PHB_CONFIG_0_LOW 0x0230
75#define PHB_CONFIG_0_END 0x0240
76#define PHB_MEM_2_LOW 0x02B0
77#define PHB_MEM_2_HIGH 0x02C0
78#define PHB_MEM_2_SIZE_HIGH 0x02D0
79#define PHB_MEM_2_SIZE_LOW 0x02E0
80#define PHB_DOSHOLE_OFFSET 0x08E0
81
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020082/* CalIOC2 specific */
83#define PHB_SAVIOR_L2 0x0DB0
84
Jon Masone4650582006-06-26 13:58:14 +020085/* PHB_CONFIG_RW */
86#define PHB_TCE_ENABLE 0x20000000
87#define PHB_SLOT_DISABLE 0x1C000000
88#define PHB_DAC_DISABLE 0x01000000
89#define PHB_MEM2_ENABLE 0x00400000
90#define PHB_MCSR_ENABLE 0x00100000
91/* TAR (Table Address Register) */
92#define TAR_SW_BITS 0x0000ffffffff800fUL
93#define TAR_VALID 0x0000000000000008UL
94/* CSR (Channel/DMA Status Register) */
95#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020096/* CCR (Calgary Configuration Register) */
97#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Jon Masone4650582006-06-26 13:58:14 +020098
99#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200100#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200101/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
102#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200103#define PHBS_PER_CALGARY 4
104
105/* register offsets in Calgary's internal register space */
106static const unsigned long tar_offsets[] = {
107 0x0580 /* TAR0 */,
108 0x0588 /* TAR1 */,
109 0x0590 /* TAR2 */,
110 0x0598 /* TAR3 */
111};
112
113static const unsigned long split_queue_offsets[] = {
114 0x4870 /* SPLIT QUEUE 0 */,
115 0x5870 /* SPLIT QUEUE 1 */,
116 0x6870 /* SPLIT QUEUE 2 */,
117 0x7870 /* SPLIT QUEUE 3 */
118};
119
120static const unsigned long phb_offsets[] = {
121 0x8000 /* PHB0 */,
122 0x9000 /* PHB1 */,
123 0xA000 /* PHB2 */,
124 0xB000 /* PHB3 */
125};
126
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100127/* PHB debug registers */
128
129static const unsigned long phb_debug_offsets[] = {
130 0x4000 /* PHB 0 DEBUG */,
131 0x5000 /* PHB 1 DEBUG */,
132 0x6000 /* PHB 2 DEBUG */,
133 0x7000 /* PHB 3 DEBUG */
134};
135
136/*
137 * STUFF register for each debug PHB,
138 * byte 1 = start bus number, byte 2 = end bus number
139 */
140
141#define PHB_DEBUG_STUFF_OFFSET 0x0020
142
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100143#define EMERGENCY_PAGES 32 /* = 128KB */
144
Jon Masone4650582006-06-26 13:58:14 +0200145unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
146static int translate_empty_slots __read_mostly = 0;
147static int calgary_detected __read_mostly = 0;
148
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100149static struct rio_table_hdr *rio_table_hdr __initdata;
150static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100151static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100152
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200153struct calgary_bus_info {
154 void *tce_space;
Muli Ben-Yehuda0577f1482006-09-26 10:52:31 +0200155 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200156 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100157 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200158};
159
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200160static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
161static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200162static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Jon Masone4650582006-06-26 13:58:14 +0200163
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200164static struct cal_chipset_ops calgary_chip_ops = {
165 .handle_quirks = calgary_handle_quirks,
166 .tce_cache_blast = calgary_tce_cache_blast
167};
168
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200169static struct cal_chipset_ops calioc2_chip_ops = {
170 .handle_quirks = calioc2_handle_quirks,
171 .tce_cache_blast = NULL
172};
173
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200174static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200175
176/* enable this to stress test the chip's TCE cache */
177#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200178int debugging __read_mostly = 1;
179
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200180static inline unsigned long verify_bit_range(unsigned long* bitmap,
181 int expected, unsigned long start, unsigned long end)
182{
183 unsigned long idx = start;
184
185 BUG_ON(start >= end);
186
187 while (idx < end) {
188 if (!!test_bit(idx, bitmap) != expected)
189 return idx;
190 ++idx;
191 }
192
193 /* all bits have the expected value */
194 return ~0UL;
195}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200196#else /* debugging is disabled */
197int debugging __read_mostly = 0;
198
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200199static inline unsigned long verify_bit_range(unsigned long* bitmap,
200 int expected, unsigned long start, unsigned long end)
201{
202 return ~0UL;
203}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200204
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200205#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200206
207static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
208{
209 unsigned int npages;
210
211 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
212 npages >>= PAGE_SHIFT;
213
214 return npages;
215}
216
217static inline int translate_phb(struct pci_dev* dev)
218{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200219 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200220 return !disabled;
221}
222
223static void iommu_range_reserve(struct iommu_table *tbl,
224 unsigned long start_addr, unsigned int npages)
225{
226 unsigned long index;
227 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200228 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200229
230 index = start_addr >> PAGE_SHIFT;
231
232 /* bail out if we're asked to reserve a region we don't cover */
233 if (index >= tbl->it_size)
234 return;
235
236 end = index + npages;
237 if (end > tbl->it_size) /* don't go off the table */
238 end = tbl->it_size;
239
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200240 badbit = verify_bit_range(tbl->it_map, 0, index, end);
241 if (badbit != ~0UL) {
242 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200243 printk(KERN_ERR "Calgary: entry already allocated at "
244 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200245 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200246 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200247
248 set_bit_string(tbl->it_map, index, npages);
Jon Masone4650582006-06-26 13:58:14 +0200249}
250
251static unsigned long iommu_range_alloc(struct iommu_table *tbl,
252 unsigned int npages)
253{
254 unsigned long offset;
255
256 BUG_ON(npages == 0);
257
258 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
259 tbl->it_size, npages);
260 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200261 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200262 offset = find_next_zero_string(tbl->it_map, 0,
263 tbl->it_size, npages);
264 if (offset == ~0UL) {
265 printk(KERN_WARNING "Calgary: IOMMU full.\n");
266 if (panic_on_overflow)
267 panic("Calgary: fix the allocator.\n");
268 else
269 return bad_dma_address;
270 }
271 }
272
273 set_bit_string(tbl->it_map, offset, npages);
274 tbl->it_hint = offset + npages;
275 BUG_ON(tbl->it_hint > tbl->it_size);
276
277 return offset;
278}
279
280static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
281 unsigned int npages, int direction)
282{
283 unsigned long entry, flags;
284 dma_addr_t ret = bad_dma_address;
285
286 spin_lock_irqsave(&tbl->it_lock, flags);
287
288 entry = iommu_range_alloc(tbl, npages);
289
290 if (unlikely(entry == bad_dma_address))
291 goto error;
292
293 /* set the return dma address */
294 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
295
296 /* put the TCEs in the HW table */
297 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
298 direction);
299
300 spin_unlock_irqrestore(&tbl->it_lock, flags);
301
302 return ret;
303
304error:
305 spin_unlock_irqrestore(&tbl->it_lock, flags);
306 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
307 "iommu %p\n", npages, tbl);
308 return bad_dma_address;
309}
310
311static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
312 unsigned int npages)
313{
314 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200315 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100316 unsigned long badend;
317
318 /* were we called with bad_dma_address? */
319 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
320 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
321 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
322 "address 0x%Lx\n", dma_addr);
323 WARN_ON(1);
324 return;
325 }
Jon Masone4650582006-06-26 13:58:14 +0200326
327 entry = dma_addr >> PAGE_SHIFT;
328
329 BUG_ON(entry + npages > tbl->it_size);
330
331 tce_free(tbl, entry, npages);
332
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200333 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
334 if (badbit != ~0UL) {
335 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200336 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
337 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200338 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200339 }
340
341 __clear_bit_string(tbl->it_map, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200342}
343
344static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
345 unsigned int npages)
346{
347 unsigned long flags;
348
349 spin_lock_irqsave(&tbl->it_lock, flags);
350
351 __iommu_free(tbl, dma_addr, npages);
352
353 spin_unlock_irqrestore(&tbl->it_lock, flags);
354}
355
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200356static inline struct iommu_table *find_iommu_table(struct device *dev)
357{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200358 struct pci_dev *pdev;
359 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200360 struct iommu_table *tbl;
361
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200362 pdev = to_pci_dev(dev);
363
364 /* is the device behind a bridge? */
365 if (unlikely(pdev->bus->parent))
366 pbus = pdev->bus->parent;
367 else
368 pbus = pdev->bus;
369
370 tbl = pbus->self->sysdata;
371 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200372
373 return tbl;
374}
375
Jon Masone4650582006-06-26 13:58:14 +0200376static void __calgary_unmap_sg(struct iommu_table *tbl,
377 struct scatterlist *sglist, int nelems, int direction)
378{
379 while (nelems--) {
380 unsigned int npages;
381 dma_addr_t dma = sglist->dma_address;
382 unsigned int dmalen = sglist->dma_length;
383
384 if (dmalen == 0)
385 break;
386
387 npages = num_dma_pages(dma, dmalen);
388 __iommu_free(tbl, dma, npages);
389 sglist++;
390 }
391}
392
393void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
394 int nelems, int direction)
395{
396 unsigned long flags;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200397 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200398
399 if (!translate_phb(to_pci_dev(dev)))
400 return;
401
402 spin_lock_irqsave(&tbl->it_lock, flags);
403
404 __calgary_unmap_sg(tbl, sglist, nelems, direction);
405
406 spin_unlock_irqrestore(&tbl->it_lock, flags);
407}
408
409static int calgary_nontranslate_map_sg(struct device* dev,
410 struct scatterlist *sg, int nelems, int direction)
411{
412 int i;
413
414 for (i = 0; i < nelems; i++ ) {
415 struct scatterlist *s = &sg[i];
416 BUG_ON(!s->page);
417 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
418 s->dma_length = s->length;
419 }
420 return nelems;
421}
422
423int calgary_map_sg(struct device *dev, struct scatterlist *sg,
424 int nelems, int direction)
425{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200426 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200427 unsigned long flags;
428 unsigned long vaddr;
429 unsigned int npages;
430 unsigned long entry;
431 int i;
432
433 if (!translate_phb(to_pci_dev(dev)))
434 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
435
436 spin_lock_irqsave(&tbl->it_lock, flags);
437
438 for (i = 0; i < nelems; i++ ) {
439 struct scatterlist *s = &sg[i];
440 BUG_ON(!s->page);
441
442 vaddr = (unsigned long)page_address(s->page) + s->offset;
443 npages = num_dma_pages(vaddr, s->length);
444
445 entry = iommu_range_alloc(tbl, npages);
446 if (entry == bad_dma_address) {
447 /* makes sure unmap knows to stop */
448 s->dma_length = 0;
449 goto error;
450 }
451
452 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
453
454 /* insert into HW table */
455 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
456 direction);
457
458 s->dma_length = s->length;
459 }
460
461 spin_unlock_irqrestore(&tbl->it_lock, flags);
462
463 return nelems;
464error:
465 __calgary_unmap_sg(tbl, sg, nelems, direction);
466 for (i = 0; i < nelems; i++) {
467 sg[i].dma_address = bad_dma_address;
468 sg[i].dma_length = 0;
469 }
470 spin_unlock_irqrestore(&tbl->it_lock, flags);
471 return 0;
472}
473
474dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
475 size_t size, int direction)
476{
477 dma_addr_t dma_handle = bad_dma_address;
478 unsigned long uaddr;
479 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200480 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200481
482 uaddr = (unsigned long)vaddr;
483 npages = num_dma_pages(uaddr, size);
484
485 if (translate_phb(to_pci_dev(dev)))
486 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
487 else
488 dma_handle = virt_to_bus(vaddr);
489
490 return dma_handle;
491}
492
493void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
494 size_t size, int direction)
495{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200496 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200497 unsigned int npages;
498
499 if (!translate_phb(to_pci_dev(dev)))
500 return;
501
502 npages = num_dma_pages(dma_handle, size);
503 iommu_free(tbl, dma_handle, npages);
504}
505
506void* calgary_alloc_coherent(struct device *dev, size_t size,
507 dma_addr_t *dma_handle, gfp_t flag)
508{
509 void *ret = NULL;
510 dma_addr_t mapping;
511 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200512 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200513
514 size = PAGE_ALIGN(size); /* size rounded up to full pages */
515 npages = size >> PAGE_SHIFT;
516 order = get_order(size);
517
518 /* alloc enough pages (and possibly more) */
519 ret = (void *)__get_free_pages(flag, order);
520 if (!ret)
521 goto error;
522 memset(ret, 0, size);
523
524 if (translate_phb(to_pci_dev(dev))) {
525 /* set up tces to cover the allocated range */
526 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
527 if (mapping == bad_dma_address)
528 goto free;
529
530 *dma_handle = mapping;
531 } else /* non translated slot */
532 *dma_handle = virt_to_bus(ret);
533
534 return ret;
535
536free:
537 free_pages((unsigned long)ret, get_order(size));
538 ret = NULL;
539error:
540 return ret;
541}
542
Stephen Hemmingere6584502007-05-02 19:27:06 +0200543static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200544 .alloc_coherent = calgary_alloc_coherent,
545 .map_single = calgary_map_single,
546 .unmap_single = calgary_unmap_single,
547 .map_sg = calgary_map_sg,
548 .unmap_sg = calgary_unmap_sg,
549};
550
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100551static inline void __iomem * busno_to_bbar(unsigned char num)
552{
553 return bus_info[num].bbar;
554}
555
Jon Masone4650582006-06-26 13:58:14 +0200556static inline int busno_to_phbid(unsigned char num)
557{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200558 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200559}
560
561static inline unsigned long split_queue_offset(unsigned char num)
562{
563 size_t idx = busno_to_phbid(num);
564
565 return split_queue_offsets[idx];
566}
567
568static inline unsigned long tar_offset(unsigned char num)
569{
570 size_t idx = busno_to_phbid(num);
571
572 return tar_offsets[idx];
573}
574
575static inline unsigned long phb_offset(unsigned char num)
576{
577 size_t idx = busno_to_phbid(num);
578
579 return phb_offsets[idx];
580}
581
582static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
583{
584 unsigned long target = ((unsigned long)bar) | offset;
585 return (void __iomem*)target;
586}
587
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200588static inline int is_calioc2(unsigned short device)
589{
590 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
591}
592
593static inline int is_calgary(unsigned short device)
594{
595 return (device == PCI_DEVICE_ID_IBM_CALGARY);
596}
597
598static inline int is_cal_pci_dev(unsigned short device)
599{
600 return (is_calgary(device) || is_calioc2(device));
601}
602
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200603static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200604{
605 u64 val;
606 u32 aer;
607 int i = 0;
608 void __iomem *bbar = tbl->bbar;
609 void __iomem *target;
610
611 /* disable arbitration on the bus */
612 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
613 aer = readl(target);
614 writel(0, target);
615
616 /* read plssr to ensure it got there */
617 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
618 val = readl(target);
619
620 /* poll split queues until all DMA activity is done */
621 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
622 do {
623 val = readq(target);
624 i++;
625 } while ((val & 0xff) != 0xff && i < 100);
626 if (i == 100)
627 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
628 "continuing anyway\n");
629
630 /* invalidate TCE cache */
631 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
632 writeq(tbl->tar_val, target);
633
634 /* enable arbitration */
635 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
636 writel(aer, target);
637 (void)readl(target); /* flush */
638}
639
640static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
641 u64 limit)
642{
643 unsigned int numpages;
644
645 limit = limit | 0xfffff;
646 limit++;
647
648 numpages = ((limit - start) >> PAGE_SHIFT);
649 iommu_range_reserve(dev->sysdata, start, numpages);
650}
651
652static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
653{
654 void __iomem *target;
655 u64 low, high, sizelow;
656 u64 start, limit;
657 struct iommu_table *tbl = dev->sysdata;
658 unsigned char busnum = dev->bus->number;
659 void __iomem *bbar = tbl->bbar;
660
661 /* peripheral MEM_1 region */
662 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
663 low = be32_to_cpu(readl(target));
664 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
665 high = be32_to_cpu(readl(target));
666 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
667 sizelow = be32_to_cpu(readl(target));
668
669 start = (high << 32) | low;
670 limit = sizelow;
671
672 calgary_reserve_mem_region(dev, start, limit);
673}
674
675static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
676{
677 void __iomem *target;
678 u32 val32;
679 u64 low, high, sizelow, sizehigh;
680 u64 start, limit;
681 struct iommu_table *tbl = dev->sysdata;
682 unsigned char busnum = dev->bus->number;
683 void __iomem *bbar = tbl->bbar;
684
685 /* is it enabled? */
686 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
687 val32 = be32_to_cpu(readl(target));
688 if (!(val32 & PHB_MEM2_ENABLE))
689 return;
690
691 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
692 low = be32_to_cpu(readl(target));
693 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
694 high = be32_to_cpu(readl(target));
695 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
696 sizelow = be32_to_cpu(readl(target));
697 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
698 sizehigh = be32_to_cpu(readl(target));
699
700 start = (high << 32) | low;
701 limit = (sizehigh << 32) | sizelow;
702
703 calgary_reserve_mem_region(dev, start, limit);
704}
705
706/*
707 * some regions of the IO address space do not get translated, so we
708 * must not give devices IO addresses in those regions. The regions
709 * are the 640KB-1MB region and the two PCI peripheral memory holes.
710 * Reserve all of them in the IOMMU bitmap to avoid giving them out
711 * later.
712 */
713static void __init calgary_reserve_regions(struct pci_dev *dev)
714{
715 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200716 u64 start;
717 struct iommu_table *tbl = dev->sysdata;
718
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100719 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
720 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200721
722 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200723 /* for CalIOC2 - avoid the entire first 2MB */
724 if (is_calgary(dev->device)) {
725 start = (640 * 1024);
726 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
727 } else { /* calioc2 */
728 start = 0;
729 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
730 }
Jon Masone4650582006-06-26 13:58:14 +0200731 iommu_range_reserve(tbl, start, npages);
732
733 /* reserve the two PCI peripheral memory regions in IO space */
734 calgary_reserve_peripheral_mem_1(dev);
735 calgary_reserve_peripheral_mem_2(dev);
736}
737
738static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
739{
740 u64 val64;
741 u64 table_phys;
742 void __iomem *target;
743 int ret;
744 struct iommu_table *tbl;
745
746 /* build TCE tables for each PHB */
747 ret = build_tce_table(dev, bbar);
748 if (ret)
749 return ret;
750
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200751 tbl = dev->sysdata;
752 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
753 tce_free(tbl, 0, tbl->it_size);
754
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200755 if (is_calgary(dev->device))
756 tbl->chip_ops = &calgary_chip_ops;
757 else if (is_calioc2(dev->device))
758 tbl->chip_ops = &calioc2_chip_ops;
759 else
760 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200761
Jon Masone4650582006-06-26 13:58:14 +0200762 calgary_reserve_regions(dev);
763
764 /* set TARs for each PHB */
765 target = calgary_reg(bbar, tar_offset(dev->bus->number));
766 val64 = be64_to_cpu(readq(target));
767
768 /* zero out all TAR bits under sw control */
769 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200770 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200771
Jon Masone4650582006-06-26 13:58:14 +0200772 val64 |= table_phys;
773
774 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
775 val64 |= (u64) specified_table_size;
776
777 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200778
Jon Masone4650582006-06-26 13:58:14 +0200779 writeq(tbl->tar_val, target);
780 readq(target); /* flush */
781
782 return 0;
783}
784
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200785static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200786{
787 u64 val64;
788 struct iommu_table *tbl = dev->sysdata;
789 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200790 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200791
792 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
793 val64 = be64_to_cpu(readq(target));
794 val64 &= ~TAR_SW_BITS;
795 writeq(cpu_to_be64(val64), target);
796 readq(target); /* flush */
797
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200798 bitmapsz = tbl->it_size / BITS_PER_BYTE;
799 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
800 tbl->it_map = NULL;
801
Jon Masone4650582006-06-26 13:58:14 +0200802 kfree(tbl);
803 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200804
805 /* Can't free bootmem allocated memory after system is up :-( */
806 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200807}
808
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200809static void calgary_dump_error_regs(struct iommu_table *tbl)
810{
811 void __iomem *bbar = tbl->bbar;
812 u32 csr, csmr, plssr, mck;
813 void __iomem *target;
814 unsigned long phboff = phb_offset(tbl->it_busno);
815 unsigned long erroff;
816 u32 errregs[7];
817 int i;
818
819 /* dump CSR */
820 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
821 csr = be32_to_cpu(readl(target));
822 /* dump PLSSR */
823 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
824 plssr = be32_to_cpu(readl(target));
825 /* dump CSMR */
826 target = calgary_reg(bbar, phboff | 0x290);
827 csmr = be32_to_cpu(readl(target));
828 /* dump mck */
829 target = calgary_reg(bbar, phboff | 0x800);
830 mck = be32_to_cpu(readl(target));
831
832 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR "
833 "0x%08x@MCK\n", csr, plssr, csmr, mck);
834
835 /* dump rest of error regs */
836 printk(KERN_EMERG "Calgary: ");
837 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
838 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
839 target = calgary_reg(bbar, phboff | erroff);
840 errregs[i] = be32_to_cpu(readl(target));
841 printk("0x%08x@0x%lx ", errregs[i], erroff);
842 }
843 printk("\n");
844}
845
Jon Masone4650582006-06-26 13:58:14 +0200846static void calgary_watchdog(unsigned long data)
847{
848 struct pci_dev *dev = (struct pci_dev *)data;
849 struct iommu_table *tbl = dev->sysdata;
850 void __iomem *bbar = tbl->bbar;
851 u32 val32;
852 void __iomem *target;
853
854 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
855 val32 = be32_to_cpu(readl(target));
856
857 /* If no error, the agent ID in the CSR is not valid */
858 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200859 printk(KERN_EMERG "Calgary: DMA error on PHB %#x\n",
860 dev->bus->number);
861 calgary_dump_error_regs(tbl);
862
863 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200864 writel(0, target);
865
866 /* Disable bus that caused the error */
867 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200868 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200869 val32 = be32_to_cpu(readl(target));
870 val32 |= PHB_SLOT_DISABLE;
871 writel(cpu_to_be32(val32), target);
872 readl(target); /* flush */
873 } else {
874 /* Reset the timer */
875 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
876 }
877}
878
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200879static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
880 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200881{
882 u64 val64;
883 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100884 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200885 u64 mask;
886
887 switch (busno_to_phbid(busnum)) {
888 case 0: phb_shift = (63 - 19);
889 break;
890 case 1: phb_shift = (63 - 23);
891 break;
892 case 2: phb_shift = (63 - 27);
893 break;
894 case 3: phb_shift = (63 - 35);
895 break;
896 default:
897 BUG_ON(busno_to_phbid(busnum));
898 }
899
900 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
901 val64 = be64_to_cpu(readq(target));
902
903 /* zero out this PHB's timer bits */
904 mask = ~(0xFUL << phb_shift);
905 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200906 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200907 writeq(cpu_to_be64(val64), target);
908 readq(target); /* flush */
909}
910
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200911static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
912{
913 unsigned char busnum = dev->bus->number;
914 void __iomem *bbar = tbl->bbar;
915 void __iomem *target;
916 u32 val;
917
918 /*
919 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
920 */
921 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
922 val = cpu_to_be32(readl(target));
923 val |= 0x00800000;
924 writel(cpu_to_be32(val), target);
925}
926
927static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200928{
929 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200930
931 /*
932 * Give split completion a longer timeout on bus 1 for aic94xx
933 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
934 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200935 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200936 calgary_set_split_completion_timeout(tbl->bbar, busnum,
937 CCR_2SEC_TIMEOUT);
938}
939
Jon Masone4650582006-06-26 13:58:14 +0200940static void __init calgary_enable_translation(struct pci_dev *dev)
941{
942 u32 val32;
943 unsigned char busnum;
944 void __iomem *target;
945 void __iomem *bbar;
946 struct iommu_table *tbl;
947
948 busnum = dev->bus->number;
949 tbl = dev->sysdata;
950 bbar = tbl->bbar;
951
952 /* enable TCE in PHB Config Register */
953 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
954 val32 = be32_to_cpu(readl(target));
955 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
956
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200957 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
958 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
959 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +0200960 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
961 "bus.\n");
962
963 writel(cpu_to_be32(val32), target);
964 readl(target); /* flush */
965
966 init_timer(&tbl->watchdog_timer);
967 tbl->watchdog_timer.function = &calgary_watchdog;
968 tbl->watchdog_timer.data = (unsigned long)dev;
969 mod_timer(&tbl->watchdog_timer, jiffies);
970}
971
972static void __init calgary_disable_translation(struct pci_dev *dev)
973{
974 u32 val32;
975 unsigned char busnum;
976 void __iomem *target;
977 void __iomem *bbar;
978 struct iommu_table *tbl;
979
980 busnum = dev->bus->number;
981 tbl = dev->sysdata;
982 bbar = tbl->bbar;
983
984 /* disable TCE in PHB Config Register */
985 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
986 val32 = be32_to_cpu(readl(target));
987 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
988
Jon Mason70d666d2006-10-05 18:47:21 +0200989 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +0200990 writel(cpu_to_be32(val32), target);
991 readl(target); /* flush */
992
993 del_timer_sync(&tbl->watchdog_timer);
994}
995
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +0200996static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200997{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200998 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200999 dev->sysdata = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001000
1001 /* is the device behind a bridge? */
1002 if (dev->bus->parent)
1003 dev->bus->parent->self = dev;
1004 else
1005 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001006}
1007
1008static int __init calgary_init_one(struct pci_dev *dev)
1009{
Jon Masone4650582006-06-26 13:58:14 +02001010 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001011 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001012 int ret;
1013
Jon Masondedc9932006-10-05 18:47:21 +02001014 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1015
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001016 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001017 ret = calgary_setup_tar(dev, bbar);
1018 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001019 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001020
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001021 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001022
1023 if (dev->bus->parent) {
1024 if (dev->bus->parent->self)
1025 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1026 "bus->parent->self!\n", dev);
1027 dev->bus->parent->self = dev;
1028 } else
1029 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001030
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001031 tbl = dev->sysdata;
1032 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001033
Jon Masone4650582006-06-26 13:58:14 +02001034 calgary_enable_translation(dev);
1035
1036 return 0;
1037
Jon Masone4650582006-06-26 13:58:14 +02001038done:
1039 return ret;
1040}
1041
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001042static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001043{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001044 int ret;
1045 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001046 void __iomem *bbar;
1047 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001048 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001049 u8 start_bus, end_bus;
1050 u32 val;
1051
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001052 ret = -ENODATA;
1053 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1054 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001055
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001056 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001057 continue;
1058
1059 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001060 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1061 if (!bbar)
1062 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001063
1064 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001065 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1066 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001067
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001068 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001069
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001070 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001071 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001072
1073 if (end_bus) {
1074 for (bus = start_bus; bus <= end_bus; bus++) {
1075 bus_info[bus].bbar = bbar;
1076 bus_info[bus].phbid = phb;
1077 }
1078 } else {
1079 bus_info[start_bus].bbar = bbar;
1080 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001081 }
1082 }
1083 }
1084
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001085 return 0;
1086
1087error:
1088 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1089 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1090 if (bus_info[bus].bbar)
1091 iounmap(bus_info[bus].bbar);
1092
1093 return ret;
1094}
1095
1096static int __init calgary_init(void)
1097{
1098 int ret;
1099 struct pci_dev *dev = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001100 void* tce_space;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001101
1102 ret = calgary_locate_bbars();
1103 if (ret)
1104 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001105
Jon Masondedc9932006-10-05 18:47:21 +02001106 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001107 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001108 if (!dev)
1109 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001110 if (!is_cal_pci_dev(dev->device))
1111 continue;
Jon Masone4650582006-06-26 13:58:14 +02001112 if (!translate_phb(dev)) {
1113 calgary_init_one_nontraslated(dev);
1114 continue;
1115 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001116 tce_space = bus_info[dev->bus->number].tce_space;
1117 if (!tce_space && !translate_empty_slots) {
1118 printk("Calg: %p failed tce_space check\n", dev);
Jon Masone4650582006-06-26 13:58:14 +02001119 continue;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001120 }
Jon Masone4650582006-06-26 13:58:14 +02001121 ret = calgary_init_one(dev);
1122 if (ret)
1123 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001124 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001125
1126 return ret;
1127
1128error:
Jon Masondedc9932006-10-05 18:47:21 +02001129 do {
Alan Cox7cd8b682006-12-07 02:14:03 +01001130 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001131 PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001132 if (!dev)
1133 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001134 if (!is_cal_pci_dev(dev->device))
1135 continue;
Jon Masone4650582006-06-26 13:58:14 +02001136 if (!translate_phb(dev)) {
1137 pci_dev_put(dev);
1138 continue;
1139 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001140 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001141 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001142
Jon Masone4650582006-06-26 13:58:14 +02001143 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001144 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001145 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001146 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001147
1148 return ret;
1149}
1150
1151static inline int __init determine_tce_table_size(u64 ram)
1152{
1153 int ret;
1154
1155 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1156 return specified_table_size;
1157
1158 /*
1159 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1160 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1161 * larger table size has twice as many entries, so shift the
1162 * max ram address by 13 to divide by 8K and then look at the
1163 * order of the result to choose between 0-7.
1164 */
1165 ret = get_order(ram >> 13);
1166 if (ret > TCE_TABLE_SIZE_8M)
1167 ret = TCE_TABLE_SIZE_8M;
1168
1169 return ret;
1170}
1171
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001172static int __init build_detail_arrays(void)
1173{
1174 unsigned long ptr;
1175 int i, scal_detail_size, rio_detail_size;
1176
1177 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1178 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001179 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001180 "but system has %d nodes.\n",
1181 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1182 return -ENODEV;
1183 }
1184
1185 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001186 case 2:
1187 scal_detail_size = 11;
1188 rio_detail_size = 13;
1189 break;
1190 case 3:
1191 scal_detail_size = 12;
1192 rio_detail_size = 15;
1193 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001194 default:
1195 printk(KERN_WARNING
1196 "Calgary: Invalid Rio Grande Table Version: %d\n",
1197 rio_table_hdr->version);
1198 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001199 }
1200
1201 ptr = ((unsigned long)rio_table_hdr) + 3;
1202 for (i = 0; i < rio_table_hdr->num_scal_dev;
1203 i++, ptr += scal_detail_size)
1204 scal_devs[i] = (struct scal_detail *)ptr;
1205
1206 for (i = 0; i < rio_table_hdr->num_rio_dev;
1207 i++, ptr += rio_detail_size)
1208 rio_devs[i] = (struct rio_detail *)ptr;
1209
1210 return 0;
1211}
1212
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001213static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1214{
1215 int dev;
1216 u32 val;
1217
1218 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1219 /*
1220 * FIXME: properly scan for devices accross the
1221 * PCI-to-PCI bridge on every CalIOC2 port.
1222 */
1223 return 1;
1224 }
1225
1226 for (dev = 1; dev < 8; dev++) {
1227 val = read_pci_config(bus, dev, 0, 0);
1228 if (val != 0xffffffff)
1229 break;
1230 }
1231 return (val != 0xffffffff);
1232}
1233
Jon Masone4650582006-06-26 13:58:14 +02001234void __init detect_calgary(void)
1235{
Jon Masond2105b12006-07-29 21:42:43 +02001236 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001237 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001238 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001239 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001240 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001241 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001242
1243 /*
1244 * if the user specified iommu=off or iommu=soft or we found
1245 * another HW IOMMU already, bail out.
1246 */
1247 if (swiotlb || no_iommu || iommu_detected)
1248 return;
1249
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001250 if (!use_calgary)
1251 return;
1252
Andi Kleen0637a702006-09-26 10:52:41 +02001253 if (!early_pci_allowed())
1254 return;
1255
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001256 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1257
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001258 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1259
1260 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001261 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001262 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001263 /*
1264 * The next offset is stored in the 1st word.
1265 * Only parse up until the offset increases:
1266 */
1267 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001268 /* The block id is stored in the 2nd word */
1269 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1270 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001271 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001272 break;
1273 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001274 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001275 offset = *((unsigned short *)(ptr + offset));
1276 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001277 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001278 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1279 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001280 return;
1281 }
1282
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001283 ret = build_detail_arrays();
1284 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001285 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001286 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001287 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001288
Jon Masone4650582006-06-26 13:58:14 +02001289 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1290
Jon Masond2105b12006-07-29 21:42:43 +02001291 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001292 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001293 unsigned short pci_device;
1294 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001295
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001296 val = read_pci_config(bus, 0, 0, 0);
1297 pci_device = (val & 0xFFFF0000) >> 16;
1298
1299 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001300 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001301
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001302 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001303 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001304
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001305 if (calgary_bus_has_devices(bus, pci_device) ||
1306 translate_empty_slots) {
1307 tbl = alloc_tce_table();
1308 if (!tbl)
1309 goto cleanup;
1310 info->tce_space = tbl;
1311 calgary_found = 1;
1312 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1313 info->tce_space, bus);
Jon Masond2105b12006-07-29 21:42:43 +02001314 }
Jon Masone4650582006-06-26 13:58:14 +02001315 }
1316
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001317 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1318 calgary_found ? "found" : "not found");
1319
Jon Masond2105b12006-07-29 21:42:43 +02001320 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001321 iommu_detected = 1;
1322 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001323 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1324 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1325 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1326 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001327 }
1328 return;
1329
1330cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001331 for (--bus; bus >= 0; --bus) {
1332 struct calgary_bus_info *info = &bus_info[bus];
1333
1334 if (info->tce_space)
1335 free_tce_table(info->tce_space);
1336 }
Jon Masone4650582006-06-26 13:58:14 +02001337}
1338
1339int __init calgary_iommu_init(void)
1340{
1341 int ret;
1342
1343 if (no_iommu || swiotlb)
1344 return -ENODEV;
1345
1346 if (!calgary_detected)
1347 return -ENODEV;
1348
1349 /* ok, we're trying to use Calgary - let's roll */
1350 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1351
1352 ret = calgary_init();
1353 if (ret) {
1354 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1355 "falling back to no_iommu\n", ret);
1356 if (end_pfn > MAX_DMA32_PFN)
1357 printk(KERN_ERR "WARNING more than 4GB of memory, "
1358 "32bit PCI may malfunction.\n");
1359 return ret;
1360 }
1361
1362 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001363 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001364 dma_ops = &calgary_dma_ops;
1365
1366 return 0;
1367}
1368
1369static int __init calgary_parse_options(char *p)
1370{
1371 unsigned int bridge;
1372 size_t len;
1373 char* endp;
1374
1375 while (*p) {
1376 if (!strncmp(p, "64k", 3))
1377 specified_table_size = TCE_TABLE_SIZE_64K;
1378 else if (!strncmp(p, "128k", 4))
1379 specified_table_size = TCE_TABLE_SIZE_128K;
1380 else if (!strncmp(p, "256k", 4))
1381 specified_table_size = TCE_TABLE_SIZE_256K;
1382 else if (!strncmp(p, "512k", 4))
1383 specified_table_size = TCE_TABLE_SIZE_512K;
1384 else if (!strncmp(p, "1M", 2))
1385 specified_table_size = TCE_TABLE_SIZE_1M;
1386 else if (!strncmp(p, "2M", 2))
1387 specified_table_size = TCE_TABLE_SIZE_2M;
1388 else if (!strncmp(p, "4M", 2))
1389 specified_table_size = TCE_TABLE_SIZE_4M;
1390 else if (!strncmp(p, "8M", 2))
1391 specified_table_size = TCE_TABLE_SIZE_8M;
1392
1393 len = strlen("translate_empty_slots");
1394 if (!strncmp(p, "translate_empty_slots", len))
1395 translate_empty_slots = 1;
1396
1397 len = strlen("disable");
1398 if (!strncmp(p, "disable", len)) {
1399 p += len;
1400 if (*p == '=')
1401 ++p;
1402 if (*p == '\0')
1403 break;
1404 bridge = simple_strtol(p, &endp, 0);
1405 if (p == endp)
1406 break;
1407
Jon Masond2105b12006-07-29 21:42:43 +02001408 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001409 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001410 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001411 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001412 }
1413 }
1414
1415 p = strpbrk(p, ",");
1416 if (!p)
1417 break;
1418
1419 p++; /* skip ',' */
1420 }
1421 return 1;
1422}
1423__setup("calgary=", calgary_parse_options);