blob: 849a0995d97090cd95e359c254b37efd3a25b819 [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>
Chandru95b68de2008-07-25 01:47:55 -070032#include <linux/crash_dump.h>
Jon Masone4650582006-06-26 13:58:14 +020033#include <linux/dma-mapping.h>
Jon Masone4650582006-06-26 13:58:14 +020034#include <linux/bitops.h>
35#include <linux/pci_ids.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
Jens Axboe8b87d9f2007-07-24 12:38:15 +020038#include <linux/scatterlist.h>
FUJITA Tomonori1b39b072008-02-04 22:28:10 -080039#include <linux/iommu-helper.h>
Alexis Bruemmer1956a962008-07-25 19:44:51 -070040
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Jon Masone4650582006-06-26 13:58:14 +020042#include <asm/calgary.h>
43#include <asm/tce.h>
44#include <asm/pci-direct.h>
45#include <asm/system.h>
46#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010047#include <asm/rio.h>
Akinobu Mitaae5830a2008-04-19 23:55:19 +090048#include <asm/bios_ebda.h>
FUJITA Tomonorid7b9f7b2009-11-10 19:46:13 +090049#include <asm/x86_init.h>
Jon Masone4650582006-06-26 13:58:14 +020050
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010051#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
52int use_calgary __read_mostly = 1;
53#else
54int use_calgary __read_mostly = 0;
55#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
56
Jon Masone4650582006-06-26 13:58:14 +020057#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020058#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020059
Jon Masone4650582006-06-26 13:58:14 +020060/* 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 */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020083#define PHB_SAVIOR_L2 0x0DB0
84#define PHB_PAGE_MIG_CTRL 0x0DA8
85#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020086#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020087
Jon Masone4650582006-06-26 13:58:14 +020088/* PHB_CONFIG_RW */
89#define PHB_TCE_ENABLE 0x20000000
90#define PHB_SLOT_DISABLE 0x1C000000
91#define PHB_DAC_DISABLE 0x01000000
92#define PHB_MEM2_ENABLE 0x00400000
93#define PHB_MCSR_ENABLE 0x00100000
94/* TAR (Table Address Register) */
95#define TAR_SW_BITS 0x0000ffffffff800fUL
96#define TAR_VALID 0x0000000000000008UL
97/* CSR (Channel/DMA Status Register) */
98#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020099/* CCR (Calgary Configuration Register) */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200100#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200101/* PMCR/PMDR (Page Migration Control/Debug Registers */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200102#define PMR_SOFTSTOP 0x80000000
103#define PMR_SOFTSTOPFAULT 0x40000000
104#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200105
106#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200107#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200108/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
109#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200110#define PHBS_PER_CALGARY 4
111
112/* register offsets in Calgary's internal register space */
113static const unsigned long tar_offsets[] = {
114 0x0580 /* TAR0 */,
115 0x0588 /* TAR1 */,
116 0x0590 /* TAR2 */,
117 0x0598 /* TAR3 */
118};
119
120static const unsigned long split_queue_offsets[] = {
121 0x4870 /* SPLIT QUEUE 0 */,
122 0x5870 /* SPLIT QUEUE 1 */,
123 0x6870 /* SPLIT QUEUE 2 */,
124 0x7870 /* SPLIT QUEUE 3 */
125};
126
127static const unsigned long phb_offsets[] = {
128 0x8000 /* PHB0 */,
129 0x9000 /* PHB1 */,
130 0xA000 /* PHB2 */,
131 0xB000 /* PHB3 */
132};
133
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100134/* PHB debug registers */
135
136static const unsigned long phb_debug_offsets[] = {
137 0x4000 /* PHB 0 DEBUG */,
138 0x5000 /* PHB 1 DEBUG */,
139 0x6000 /* PHB 2 DEBUG */,
140 0x7000 /* PHB 3 DEBUG */
141};
142
143/*
144 * STUFF register for each debug PHB,
145 * byte 1 = start bus number, byte 2 = end bus number
146 */
147
148#define PHB_DEBUG_STUFF_OFFSET 0x0020
149
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100150#define EMERGENCY_PAGES 32 /* = 128KB */
151
Jon Masone4650582006-06-26 13:58:14 +0200152unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
153static int translate_empty_slots __read_mostly = 0;
154static int calgary_detected __read_mostly = 0;
155
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100156static struct rio_table_hdr *rio_table_hdr __initdata;
157static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100158static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100159
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200160struct calgary_bus_info {
161 void *tce_space;
Muli Ben-Yehuda0577f1482006-09-26 10:52:31 +0200162 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200163 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100164 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200165};
166
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200167static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
168static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200169static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200170static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200171static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200172static void calioc2_dump_error_regs(struct iommu_table *tbl);
Chandru95b68de2008-07-25 01:47:55 -0700173static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl);
174static void get_tce_space_from_tar(void);
Jon Masone4650582006-06-26 13:58:14 +0200175
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200176static struct cal_chipset_ops calgary_chip_ops = {
177 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200178 .tce_cache_blast = calgary_tce_cache_blast,
179 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200180};
181
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200182static struct cal_chipset_ops calioc2_chip_ops = {
183 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200184 .tce_cache_blast = calioc2_tce_cache_blast,
185 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200186};
187
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200188static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200189
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200190static inline int translation_enabled(struct iommu_table *tbl)
191{
192 /* only PHBs with translation enabled have an IOMMU table */
193 return (tbl != NULL);
194}
195
Jon Masone4650582006-06-26 13:58:14 +0200196static void iommu_range_reserve(struct iommu_table *tbl,
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200197 unsigned long start_addr, unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200198{
199 unsigned long index;
200 unsigned long end;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200201 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200202
203 index = start_addr >> PAGE_SHIFT;
204
205 /* bail out if we're asked to reserve a region we don't cover */
206 if (index >= tbl->it_size)
207 return;
208
209 end = index + npages;
210 if (end > tbl->it_size) /* don't go off the table */
211 end = tbl->it_size;
212
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200213 spin_lock_irqsave(&tbl->it_lock, flags);
214
FUJITA Tomonorid26dbc52008-09-22 22:35:07 +0900215 iommu_area_reserve(tbl->it_map, index, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200216
217 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200218}
219
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800220static unsigned long iommu_range_alloc(struct device *dev,
221 struct iommu_table *tbl,
222 unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200223{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200224 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200225 unsigned long offset;
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800226 unsigned long boundary_size;
227
228 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
229 PAGE_SIZE) >> PAGE_SHIFT;
Jon Masone4650582006-06-26 13:58:14 +0200230
231 BUG_ON(npages == 0);
232
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200233 spin_lock_irqsave(&tbl->it_lock, flags);
234
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800235 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint,
236 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200237 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200238 tbl->chip_ops->tce_cache_blast(tbl);
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800239
240 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0,
241 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200242 if (offset == ~0UL) {
243 printk(KERN_WARNING "Calgary: IOMMU full.\n");
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200244 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200245 if (panic_on_overflow)
246 panic("Calgary: fix the allocator.\n");
247 else
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900248 return DMA_ERROR_CODE;
Jon Masone4650582006-06-26 13:58:14 +0200249 }
250 }
251
Jon Masone4650582006-06-26 13:58:14 +0200252 tbl->it_hint = offset + npages;
253 BUG_ON(tbl->it_hint > tbl->it_size);
254
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200255 spin_unlock_irqrestore(&tbl->it_lock, flags);
256
Jon Masone4650582006-06-26 13:58:14 +0200257 return offset;
258}
259
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800260static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
261 void *vaddr, unsigned int npages, int direction)
Jon Masone4650582006-06-26 13:58:14 +0200262{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200263 unsigned long entry;
FUJITA Tomonori1f7564c2009-11-15 21:19:54 +0900264 dma_addr_t ret;
Jon Masone4650582006-06-26 13:58:14 +0200265
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800266 entry = iommu_range_alloc(dev, tbl, npages);
Jon Masone4650582006-06-26 13:58:14 +0200267
FUJITA Tomonori1f7564c2009-11-15 21:19:54 +0900268 if (unlikely(entry == DMA_ERROR_CODE)) {
269 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
270 "iommu %p\n", npages, tbl);
271 return DMA_ERROR_CODE;
272 }
Jon Masone4650582006-06-26 13:58:14 +0200273
274 /* set the return dma address */
275 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
276
277 /* put the TCEs in the HW table */
278 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
279 direction);
Jon Masone4650582006-06-26 13:58:14 +0200280 return ret;
Jon Masone4650582006-06-26 13:58:14 +0200281}
282
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200283static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
Jon Masone4650582006-06-26 13:58:14 +0200284 unsigned int npages)
285{
286 unsigned long entry;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100287 unsigned long badend;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200288 unsigned long flags;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100289
290 /* were we called with bad_dma_address? */
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900291 badend = DMA_ERROR_CODE + (EMERGENCY_PAGES * PAGE_SIZE);
292 if (unlikely((dma_addr >= DMA_ERROR_CODE) && (dma_addr < badend))) {
Arjan van de Venbde78a72008-07-08 09:51:56 -0700293 WARN(1, KERN_ERR "Calgary: driver tried unmapping bad DMA "
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100294 "address 0x%Lx\n", dma_addr);
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100295 return;
296 }
Jon Masone4650582006-06-26 13:58:14 +0200297
298 entry = dma_addr >> PAGE_SHIFT;
299
300 BUG_ON(entry + npages > tbl->it_size);
301
302 tce_free(tbl, entry, npages);
303
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200304 spin_lock_irqsave(&tbl->it_lock, flags);
305
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800306 iommu_area_free(tbl->it_map, entry, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200307
308 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200309}
310
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200311static inline struct iommu_table *find_iommu_table(struct device *dev)
312{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200313 struct pci_dev *pdev;
314 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200315 struct iommu_table *tbl;
316
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200317 pdev = to_pci_dev(dev);
318
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200319 pbus = pdev->bus;
320
321 /* is the device behind a bridge? Look for the root bus */
322 while (pbus->parent)
323 pbus = pbus->parent;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200324
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300325 tbl = pci_iommu(pbus);
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200326
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200327 BUG_ON(tbl && (tbl->it_busno != pbus->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200328
329 return tbl;
330}
331
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900332static void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
333 int nelems,enum dma_data_direction dir,
334 struct dma_attrs *attrs)
Jon Masone4650582006-06-26 13:58:14 +0200335{
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200336 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200337 struct scatterlist *s;
338 int i;
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200339
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +0200340 if (!translation_enabled(tbl))
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200341 return;
342
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200343 for_each_sg(sglist, s, nelems, i) {
Jon Masone4650582006-06-26 13:58:14 +0200344 unsigned int npages;
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200345 dma_addr_t dma = s->dma_address;
346 unsigned int dmalen = s->dma_length;
Jon Masone4650582006-06-26 13:58:14 +0200347
348 if (dmalen == 0)
349 break;
350
Joerg Roedel036b4c52008-10-15 22:02:12 -0700351 npages = iommu_num_pages(dma, dmalen, PAGE_SIZE);
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200352 iommu_free(tbl, dma, npages);
Jon Masone4650582006-06-26 13:58:14 +0200353 }
354}
355
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200356static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900357 int nelems, enum dma_data_direction dir,
358 struct dma_attrs *attrs)
Jon Masone4650582006-06-26 13:58:14 +0200359{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200360 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200361 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200362 unsigned long vaddr;
363 unsigned int npages;
364 unsigned long entry;
365 int i;
366
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200367 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200368 BUG_ON(!sg_page(s));
Jon Masone4650582006-06-26 13:58:14 +0200369
Jens Axboe58b053e2007-10-22 20:02:46 +0200370 vaddr = (unsigned long) sg_virt(s);
Joerg Roedel036b4c52008-10-15 22:02:12 -0700371 npages = iommu_num_pages(vaddr, s->length, PAGE_SIZE);
Jon Masone4650582006-06-26 13:58:14 +0200372
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800373 entry = iommu_range_alloc(dev, tbl, npages);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900374 if (entry == DMA_ERROR_CODE) {
Jon Masone4650582006-06-26 13:58:14 +0200375 /* makes sure unmap knows to stop */
376 s->dma_length = 0;
377 goto error;
378 }
379
380 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
381
382 /* insert into HW table */
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900383 tce_build(tbl, entry, npages, vaddr & PAGE_MASK, dir);
Jon Masone4650582006-06-26 13:58:14 +0200384
385 s->dma_length = s->length;
386 }
387
Jon Masone4650582006-06-26 13:58:14 +0200388 return nelems;
389error:
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900390 calgary_unmap_sg(dev, sg, nelems, dir, NULL);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200391 for_each_sg(sg, s, nelems, i) {
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900392 sg->dma_address = DMA_ERROR_CODE;
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200393 sg->dma_length = 0;
Jon Masone4650582006-06-26 13:58:14 +0200394 }
Jon Masone4650582006-06-26 13:58:14 +0200395 return 0;
396}
397
FUJITA Tomonori39916052009-01-05 23:47:24 +0900398static dma_addr_t calgary_map_page(struct device *dev, struct page *page,
399 unsigned long offset, size_t size,
400 enum dma_data_direction dir,
401 struct dma_attrs *attrs)
Jon Masone4650582006-06-26 13:58:14 +0200402{
FUJITA Tomonori39916052009-01-05 23:47:24 +0900403 void *vaddr = page_address(page) + offset;
Jon Masone4650582006-06-26 13:58:14 +0200404 unsigned long uaddr;
405 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200406 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200407
408 uaddr = (unsigned long)vaddr;
Joerg Roedel036b4c52008-10-15 22:02:12 -0700409 npages = iommu_num_pages(uaddr, size, PAGE_SIZE);
Jon Masone4650582006-06-26 13:58:14 +0200410
FUJITA Tomonori39916052009-01-05 23:47:24 +0900411 return iommu_alloc(dev, tbl, vaddr, npages, dir);
Jon Masone4650582006-06-26 13:58:14 +0200412}
413
FUJITA Tomonori39916052009-01-05 23:47:24 +0900414static void calgary_unmap_page(struct device *dev, dma_addr_t dma_addr,
415 size_t size, enum dma_data_direction dir,
416 struct dma_attrs *attrs)
Jon Masone4650582006-06-26 13:58:14 +0200417{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200418 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200419 unsigned int npages;
420
FUJITA Tomonori39916052009-01-05 23:47:24 +0900421 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
422 iommu_free(tbl, dma_addr, npages);
423}
424
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200425static void* calgary_alloc_coherent(struct device *dev, size_t size,
Jon Masone4650582006-06-26 13:58:14 +0200426 dma_addr_t *dma_handle, gfp_t flag)
427{
428 void *ret = NULL;
429 dma_addr_t mapping;
430 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200431 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200432
433 size = PAGE_ALIGN(size); /* size rounded up to full pages */
434 npages = size >> PAGE_SHIFT;
435 order = get_order(size);
436
FUJITA Tomonorif10ac8a2008-09-11 23:08:47 +0900437 flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
438
Jon Masone4650582006-06-26 13:58:14 +0200439 /* alloc enough pages (and possibly more) */
440 ret = (void *)__get_free_pages(flag, order);
441 if (!ret)
442 goto error;
443 memset(ret, 0, size);
444
Alexis Bruemmer1956a962008-07-25 19:44:51 -0700445 /* set up tces to cover the allocated range */
446 mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900447 if (mapping == DMA_ERROR_CODE)
Alexis Bruemmer1956a962008-07-25 19:44:51 -0700448 goto free;
449 *dma_handle = mapping;
Jon Masone4650582006-06-26 13:58:14 +0200450 return ret;
Jon Masone4650582006-06-26 13:58:14 +0200451free:
452 free_pages((unsigned long)ret, get_order(size));
453 ret = NULL;
454error:
455 return ret;
456}
457
Joerg Roedele4ad68b2008-08-19 16:32:41 +0200458static void calgary_free_coherent(struct device *dev, size_t size,
459 void *vaddr, dma_addr_t dma_handle)
460{
461 unsigned int npages;
462 struct iommu_table *tbl = find_iommu_table(dev);
463
464 size = PAGE_ALIGN(size);
465 npages = size >> PAGE_SHIFT;
466
467 iommu_free(tbl, dma_handle, npages);
468 free_pages((unsigned long)vaddr, get_order(size));
469}
470
FUJITA Tomonori160c1d82009-01-05 23:59:02 +0900471static struct dma_map_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200472 .alloc_coherent = calgary_alloc_coherent,
Joerg Roedele4ad68b2008-08-19 16:32:41 +0200473 .free_coherent = calgary_free_coherent,
Jon Masone4650582006-06-26 13:58:14 +0200474 .map_sg = calgary_map_sg,
475 .unmap_sg = calgary_unmap_sg,
FUJITA Tomonori39916052009-01-05 23:47:24 +0900476 .map_page = calgary_map_page,
477 .unmap_page = calgary_unmap_page,
Jon Masone4650582006-06-26 13:58:14 +0200478};
479
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100480static inline void __iomem * busno_to_bbar(unsigned char num)
481{
482 return bus_info[num].bbar;
483}
484
Jon Masone4650582006-06-26 13:58:14 +0200485static inline int busno_to_phbid(unsigned char num)
486{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200487 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200488}
489
490static inline unsigned long split_queue_offset(unsigned char num)
491{
492 size_t idx = busno_to_phbid(num);
493
494 return split_queue_offsets[idx];
495}
496
497static inline unsigned long tar_offset(unsigned char num)
498{
499 size_t idx = busno_to_phbid(num);
500
501 return tar_offsets[idx];
502}
503
504static inline unsigned long phb_offset(unsigned char num)
505{
506 size_t idx = busno_to_phbid(num);
507
508 return phb_offsets[idx];
509}
510
511static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
512{
513 unsigned long target = ((unsigned long)bar) | offset;
514 return (void __iomem*)target;
515}
516
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200517static inline int is_calioc2(unsigned short device)
518{
519 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
520}
521
522static inline int is_calgary(unsigned short device)
523{
524 return (device == PCI_DEVICE_ID_IBM_CALGARY);
525}
526
527static inline int is_cal_pci_dev(unsigned short device)
528{
529 return (is_calgary(device) || is_calioc2(device));
530}
531
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200532static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200533{
534 u64 val;
535 u32 aer;
536 int i = 0;
537 void __iomem *bbar = tbl->bbar;
538 void __iomem *target;
539
540 /* disable arbitration on the bus */
541 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
542 aer = readl(target);
543 writel(0, target);
544
545 /* read plssr to ensure it got there */
546 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
547 val = readl(target);
548
549 /* poll split queues until all DMA activity is done */
550 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
551 do {
552 val = readq(target);
553 i++;
554 } while ((val & 0xff) != 0xff && i < 100);
555 if (i == 100)
556 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
557 "continuing anyway\n");
558
559 /* invalidate TCE cache */
560 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
561 writeq(tbl->tar_val, target);
562
563 /* enable arbitration */
564 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
565 writel(aer, target);
566 (void)readl(target); /* flush */
567}
568
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200569static void calioc2_tce_cache_blast(struct iommu_table *tbl)
570{
571 void __iomem *bbar = tbl->bbar;
572 void __iomem *target;
573 u64 val64;
574 u32 val;
575 int i = 0;
576 int count = 1;
577 unsigned char bus = tbl->it_busno;
578
579begin:
580 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
581 "sequence - count %d\n", bus, count);
582
583 /* 1. using the Page Migration Control reg set SoftStop */
584 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
585 val = be32_to_cpu(readl(target));
586 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
587 val |= PMR_SOFTSTOP;
588 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
589 writel(cpu_to_be32(val), target);
590
591 /* 2. poll split queues until all DMA activity is done */
592 printk(KERN_DEBUG "2a. starting to poll split queues\n");
593 target = calgary_reg(bbar, split_queue_offset(bus));
594 do {
595 val64 = readq(target);
596 i++;
597 } while ((val64 & 0xff) != 0xff && i < 100);
598 if (i == 100)
599 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
600 "continuing anyway\n");
601
602 /* 3. poll Page Migration DEBUG for SoftStopFault */
603 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
604 val = be32_to_cpu(readl(target));
605 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
606
607 /* 4. if SoftStopFault - goto (1) */
608 if (val & PMR_SOFTSTOPFAULT) {
609 if (++count < 100)
610 goto begin;
611 else {
612 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
613 "aborting TCE cache flush sequence!\n");
614 return; /* pray for the best */
615 }
616 }
617
618 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
619 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
620 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
621 val = be32_to_cpu(readl(target));
622 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
623 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
624 val = be32_to_cpu(readl(target));
625 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
626
627 /* 6. invalidate TCE cache */
628 printk(KERN_DEBUG "6. invalidating TCE cache\n");
629 target = calgary_reg(bbar, tar_offset(bus));
630 writeq(tbl->tar_val, target);
631
632 /* 7. Re-read PMCR */
633 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
634 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
635 val = be32_to_cpu(readl(target));
636 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
637
638 /* 8. Remove HardStop */
639 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
640 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
641 val = 0;
642 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
643 writel(cpu_to_be32(val), target);
644 val = be32_to_cpu(readl(target));
645 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
646}
647
Jon Masone4650582006-06-26 13:58:14 +0200648static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
649 u64 limit)
650{
651 unsigned int numpages;
652
653 limit = limit | 0xfffff;
654 limit++;
655
656 numpages = ((limit - start) >> PAGE_SHIFT);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300657 iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
Jon Masone4650582006-06-26 13:58:14 +0200658}
659
660static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
661{
662 void __iomem *target;
663 u64 low, high, sizelow;
664 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300665 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200666 unsigned char busnum = dev->bus->number;
667 void __iomem *bbar = tbl->bbar;
668
669 /* peripheral MEM_1 region */
670 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
671 low = be32_to_cpu(readl(target));
672 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
673 high = be32_to_cpu(readl(target));
674 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
675 sizelow = be32_to_cpu(readl(target));
676
677 start = (high << 32) | low;
678 limit = sizelow;
679
680 calgary_reserve_mem_region(dev, start, limit);
681}
682
683static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
684{
685 void __iomem *target;
686 u32 val32;
687 u64 low, high, sizelow, sizehigh;
688 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300689 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200690 unsigned char busnum = dev->bus->number;
691 void __iomem *bbar = tbl->bbar;
692
693 /* is it enabled? */
694 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
695 val32 = be32_to_cpu(readl(target));
696 if (!(val32 & PHB_MEM2_ENABLE))
697 return;
698
699 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
700 low = be32_to_cpu(readl(target));
701 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
702 high = be32_to_cpu(readl(target));
703 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
704 sizelow = be32_to_cpu(readl(target));
705 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
706 sizehigh = be32_to_cpu(readl(target));
707
708 start = (high << 32) | low;
709 limit = (sizehigh << 32) | sizelow;
710
711 calgary_reserve_mem_region(dev, start, limit);
712}
713
714/*
715 * some regions of the IO address space do not get translated, so we
716 * must not give devices IO addresses in those regions. The regions
717 * are the 640KB-1MB region and the two PCI peripheral memory holes.
718 * Reserve all of them in the IOMMU bitmap to avoid giving them out
719 * later.
720 */
721static void __init calgary_reserve_regions(struct pci_dev *dev)
722{
723 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200724 u64 start;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300725 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200726
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100727 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +0900728 iommu_range_reserve(tbl, DMA_ERROR_CODE, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200729
730 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200731 /* for CalIOC2 - avoid the entire first MB */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200732 if (is_calgary(dev->device)) {
733 start = (640 * 1024);
734 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
735 } else { /* calioc2 */
736 start = 0;
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200737 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200738 }
Jon Masone4650582006-06-26 13:58:14 +0200739 iommu_range_reserve(tbl, start, npages);
740
741 /* reserve the two PCI peripheral memory regions in IO space */
742 calgary_reserve_peripheral_mem_1(dev);
743 calgary_reserve_peripheral_mem_2(dev);
744}
745
746static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
747{
748 u64 val64;
749 u64 table_phys;
750 void __iomem *target;
751 int ret;
752 struct iommu_table *tbl;
753
754 /* build TCE tables for each PHB */
755 ret = build_tce_table(dev, bbar);
756 if (ret)
757 return ret;
758
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300759 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200760 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
Chandru95b68de2008-07-25 01:47:55 -0700761
762 if (is_kdump_kernel())
763 calgary_init_bitmap_from_tce_table(tbl);
764 else
765 tce_free(tbl, 0, tbl->it_size);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200766
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200767 if (is_calgary(dev->device))
768 tbl->chip_ops = &calgary_chip_ops;
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200769 else if (is_calioc2(dev->device))
770 tbl->chip_ops = &calioc2_chip_ops;
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200771 else
772 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200773
Jon Masone4650582006-06-26 13:58:14 +0200774 calgary_reserve_regions(dev);
775
776 /* set TARs for each PHB */
777 target = calgary_reg(bbar, tar_offset(dev->bus->number));
778 val64 = be64_to_cpu(readq(target));
779
780 /* zero out all TAR bits under sw control */
781 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200782 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200783
Jon Masone4650582006-06-26 13:58:14 +0200784 val64 |= table_phys;
785
786 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
787 val64 |= (u64) specified_table_size;
788
789 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200790
Jon Masone4650582006-06-26 13:58:14 +0200791 writeq(tbl->tar_val, target);
792 readq(target); /* flush */
793
794 return 0;
795}
796
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200797static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200798{
799 u64 val64;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300800 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200801 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200802 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200803
804 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
805 val64 = be64_to_cpu(readq(target));
806 val64 &= ~TAR_SW_BITS;
807 writeq(cpu_to_be64(val64), target);
808 readq(target); /* flush */
809
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200810 bitmapsz = tbl->it_size / BITS_PER_BYTE;
811 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
812 tbl->it_map = NULL;
813
Jon Masone4650582006-06-26 13:58:14 +0200814 kfree(tbl);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300815
816 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200817
818 /* Can't free bootmem allocated memory after system is up :-( */
819 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200820}
821
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200822static void calgary_dump_error_regs(struct iommu_table *tbl)
823{
824 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200825 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200826 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200827
828 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200829 csr = be32_to_cpu(readl(target));
830
831 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
832 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200833
834 /* If no error, the agent ID in the CSR is not valid */
835 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200836 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200837}
838
839static void calioc2_dump_error_regs(struct iommu_table *tbl)
840{
841 void __iomem *bbar = tbl->bbar;
842 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200843 void __iomem *target;
844 unsigned long phboff = phb_offset(tbl->it_busno);
845 unsigned long erroff;
846 u32 errregs[7];
847 int i;
848
849 /* dump CSR */
850 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
851 csr = be32_to_cpu(readl(target));
852 /* dump PLSSR */
853 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
854 plssr = be32_to_cpu(readl(target));
855 /* dump CSMR */
856 target = calgary_reg(bbar, phboff | 0x290);
857 csmr = be32_to_cpu(readl(target));
858 /* dump mck */
859 target = calgary_reg(bbar, phboff | 0x800);
860 mck = be32_to_cpu(readl(target));
861
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200862 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
863 tbl->it_busno);
864
865 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
866 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200867
868 /* dump rest of error regs */
869 printk(KERN_EMERG "Calgary: ");
870 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200871 /* err regs are at 0x810 - 0x870 */
872 erroff = (0x810 + (i * 0x10));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200873 target = calgary_reg(bbar, phboff | erroff);
874 errregs[i] = be32_to_cpu(readl(target));
875 printk("0x%08x@0x%lx ", errregs[i], erroff);
876 }
877 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200878
879 /* root complex status */
880 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
881 rcstat = be32_to_cpu(readl(target));
882 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
883 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200884}
885
Jon Masone4650582006-06-26 13:58:14 +0200886static void calgary_watchdog(unsigned long data)
887{
888 struct pci_dev *dev = (struct pci_dev *)data;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300889 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200890 void __iomem *bbar = tbl->bbar;
891 u32 val32;
892 void __iomem *target;
893
894 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
895 val32 = be32_to_cpu(readl(target));
896
897 /* If no error, the agent ID in the CSR is not valid */
898 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200899 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200900
901 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200902 writel(0, target);
903
904 /* Disable bus that caused the error */
905 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200906 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200907 val32 = be32_to_cpu(readl(target));
908 val32 |= PHB_SLOT_DISABLE;
909 writel(cpu_to_be32(val32), target);
910 readl(target); /* flush */
911 } else {
912 /* Reset the timer */
913 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
914 }
915}
916
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200917static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
918 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200919{
920 u64 val64;
921 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100922 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200923 u64 mask;
924
925 switch (busno_to_phbid(busnum)) {
926 case 0: phb_shift = (63 - 19);
927 break;
928 case 1: phb_shift = (63 - 23);
929 break;
930 case 2: phb_shift = (63 - 27);
931 break;
932 case 3: phb_shift = (63 - 35);
933 break;
934 default:
935 BUG_ON(busno_to_phbid(busnum));
936 }
937
938 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
939 val64 = be64_to_cpu(readq(target));
940
941 /* zero out this PHB's timer bits */
942 mask = ~(0xFUL << phb_shift);
943 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200944 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200945 writeq(cpu_to_be64(val64), target);
946 readq(target); /* flush */
947}
948
Sam Ravnborg31f3dff2008-02-01 17:49:42 +0100949static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200950{
951 unsigned char busnum = dev->bus->number;
952 void __iomem *bbar = tbl->bbar;
953 void __iomem *target;
954 u32 val;
955
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200956 /*
957 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
958 */
959 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
960 val = cpu_to_be32(readl(target));
961 val |= 0x00800000;
962 writel(cpu_to_be32(val), target);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200963}
964
Sam Ravnborg31f3dff2008-02-01 17:49:42 +0100965static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200966{
967 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200968
969 /*
970 * Give split completion a longer timeout on bus 1 for aic94xx
971 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
972 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200973 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +0200974 calgary_set_split_completion_timeout(tbl->bbar, busnum,
975 CCR_2SEC_TIMEOUT);
976}
977
Jon Masone4650582006-06-26 13:58:14 +0200978static void __init calgary_enable_translation(struct pci_dev *dev)
979{
980 u32 val32;
981 unsigned char busnum;
982 void __iomem *target;
983 void __iomem *bbar;
984 struct iommu_table *tbl;
985
986 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300987 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200988 bbar = tbl->bbar;
989
990 /* enable TCE in PHB Config Register */
991 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
992 val32 = be32_to_cpu(readl(target));
993 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
994
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200995 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
996 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
997 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +0200998 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
999 "bus.\n");
1000
1001 writel(cpu_to_be32(val32), target);
1002 readl(target); /* flush */
1003
1004 init_timer(&tbl->watchdog_timer);
1005 tbl->watchdog_timer.function = &calgary_watchdog;
1006 tbl->watchdog_timer.data = (unsigned long)dev;
1007 mod_timer(&tbl->watchdog_timer, jiffies);
1008}
1009
1010static void __init calgary_disable_translation(struct pci_dev *dev)
1011{
1012 u32 val32;
1013 unsigned char busnum;
1014 void __iomem *target;
1015 void __iomem *bbar;
1016 struct iommu_table *tbl;
1017
1018 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001019 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001020 bbar = tbl->bbar;
1021
1022 /* disable TCE in PHB Config Register */
1023 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1024 val32 = be32_to_cpu(readl(target));
1025 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1026
Jon Mason70d666d2006-10-05 18:47:21 +02001027 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001028 writel(cpu_to_be32(val32), target);
1029 readl(target); /* flush */
1030
1031 del_timer_sync(&tbl->watchdog_timer);
1032}
1033
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001034static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001035{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001036 pci_dev_get(dev);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001037 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001038
1039 /* is the device behind a bridge? */
1040 if (dev->bus->parent)
1041 dev->bus->parent->self = dev;
1042 else
1043 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001044}
1045
1046static int __init calgary_init_one(struct pci_dev *dev)
1047{
Jon Masone4650582006-06-26 13:58:14 +02001048 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001049 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001050 int ret;
1051
Jon Masondedc9932006-10-05 18:47:21 +02001052 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1053
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001054 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001055 ret = calgary_setup_tar(dev, bbar);
1056 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001057 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001058
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001059 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001060
1061 if (dev->bus->parent) {
1062 if (dev->bus->parent->self)
1063 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1064 "bus->parent->self!\n", dev);
1065 dev->bus->parent->self = dev;
1066 } else
1067 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001068
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001069 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001070 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001071
Jon Masone4650582006-06-26 13:58:14 +02001072 calgary_enable_translation(dev);
1073
1074 return 0;
1075
Jon Masone4650582006-06-26 13:58:14 +02001076done:
1077 return ret;
1078}
1079
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001080static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001081{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001082 int ret;
1083 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001084 void __iomem *bbar;
1085 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001086 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001087 u8 start_bus, end_bus;
1088 u32 val;
1089
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001090 ret = -ENODATA;
1091 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1092 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001093
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001094 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001095 continue;
1096
1097 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001098 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1099 if (!bbar)
1100 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001101
1102 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001103 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1104 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001105
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001106 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001107
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001108 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001109 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001110
1111 if (end_bus) {
1112 for (bus = start_bus; bus <= end_bus; bus++) {
1113 bus_info[bus].bbar = bbar;
1114 bus_info[bus].phbid = phb;
1115 }
1116 } else {
1117 bus_info[start_bus].bbar = bbar;
1118 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001119 }
1120 }
1121 }
1122
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001123 return 0;
1124
1125error:
1126 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1127 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1128 if (bus_info[bus].bbar)
1129 iounmap(bus_info[bus].bbar);
1130
1131 return ret;
1132}
1133
1134static int __init calgary_init(void)
1135{
1136 int ret;
1137 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001138 struct calgary_bus_info *info;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001139
1140 ret = calgary_locate_bbars();
1141 if (ret)
1142 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001143
Chandru95b68de2008-07-25 01:47:55 -07001144 /* Purely for kdump kernel case */
1145 if (is_kdump_kernel())
1146 get_tce_space_from_tar();
1147
Jon Masondedc9932006-10-05 18:47:21 +02001148 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001149 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001150 if (!dev)
1151 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001152 if (!is_cal_pci_dev(dev->device))
1153 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001154
1155 info = &bus_info[dev->bus->number];
1156 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001157 calgary_init_one_nontraslated(dev);
1158 continue;
1159 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001160
1161 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001162 continue;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001163
Jon Masone4650582006-06-26 13:58:14 +02001164 ret = calgary_init_one(dev);
1165 if (ret)
1166 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001167 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001168
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001169 dev = NULL;
1170 for_each_pci_dev(dev) {
1171 struct iommu_table *tbl;
1172
1173 tbl = find_iommu_table(&dev->dev);
1174
1175 if (translation_enabled(tbl))
1176 dev->dev.archdata.dma_ops = &calgary_dma_ops;
1177 }
1178
Jon Masone4650582006-06-26 13:58:14 +02001179 return ret;
1180
1181error:
Jon Masondedc9932006-10-05 18:47:21 +02001182 do {
Greg Kroah-Hartmana2b5d872008-02-13 09:32:03 -08001183 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001184 if (!dev)
1185 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001186 if (!is_cal_pci_dev(dev->device))
1187 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001188
1189 info = &bus_info[dev->bus->number];
1190 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001191 pci_dev_put(dev);
1192 continue;
1193 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001194 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001195 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001196
Jon Masone4650582006-06-26 13:58:14 +02001197 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001198 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001199 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001200 dev->dev.archdata.dma_ops = NULL;
Jon Masondedc9932006-10-05 18:47:21 +02001201 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001202
1203 return ret;
1204}
1205
1206static inline int __init determine_tce_table_size(u64 ram)
1207{
1208 int ret;
1209
1210 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1211 return specified_table_size;
1212
1213 /*
1214 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1215 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1216 * larger table size has twice as many entries, so shift the
1217 * max ram address by 13 to divide by 8K and then look at the
1218 * order of the result to choose between 0-7.
1219 */
1220 ret = get_order(ram >> 13);
1221 if (ret > TCE_TABLE_SIZE_8M)
1222 ret = TCE_TABLE_SIZE_8M;
1223
1224 return ret;
1225}
1226
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001227static int __init build_detail_arrays(void)
1228{
1229 unsigned long ptr;
David Howells85d57792008-08-18 11:58:17 +02001230 unsigned numnodes, i;
1231 int scal_detail_size, rio_detail_size;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001232
David Howells85d57792008-08-18 11:58:17 +02001233 numnodes = rio_table_hdr->num_scal_dev;
1234 if (numnodes > MAX_NUMNODES){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001235 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001236 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001237 "but system has %d nodes.\n",
David Howells85d57792008-08-18 11:58:17 +02001238 MAX_NUMNODES, numnodes);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001239 return -ENODEV;
1240 }
1241
1242 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001243 case 2:
1244 scal_detail_size = 11;
1245 rio_detail_size = 13;
1246 break;
1247 case 3:
1248 scal_detail_size = 12;
1249 rio_detail_size = 15;
1250 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001251 default:
1252 printk(KERN_WARNING
1253 "Calgary: Invalid Rio Grande Table Version: %d\n",
1254 rio_table_hdr->version);
1255 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001256 }
1257
1258 ptr = ((unsigned long)rio_table_hdr) + 3;
David Howells85d57792008-08-18 11:58:17 +02001259 for (i = 0; i < numnodes; i++, ptr += scal_detail_size)
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001260 scal_devs[i] = (struct scal_detail *)ptr;
1261
1262 for (i = 0; i < rio_table_hdr->num_rio_dev;
1263 i++, ptr += rio_detail_size)
1264 rio_devs[i] = (struct rio_detail *)ptr;
1265
1266 return 0;
1267}
1268
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001269static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1270{
1271 int dev;
1272 u32 val;
1273
1274 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1275 /*
1276 * FIXME: properly scan for devices accross the
1277 * PCI-to-PCI bridge on every CalIOC2 port.
1278 */
1279 return 1;
1280 }
1281
1282 for (dev = 1; dev < 8; dev++) {
1283 val = read_pci_config(bus, dev, 0, 0);
1284 if (val != 0xffffffff)
1285 break;
1286 }
1287 return (val != 0xffffffff);
1288}
1289
Chandru95b68de2008-07-25 01:47:55 -07001290/*
1291 * calgary_init_bitmap_from_tce_table():
1292 * Funtion for kdump case. In the second/kdump kernel initialize
1293 * the bitmap based on the tce table entries obtained from first kernel
1294 */
1295static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl)
1296{
1297 u64 *tp;
1298 unsigned int index;
1299 tp = ((u64 *)tbl->it_base);
1300 for (index = 0 ; index < tbl->it_size; index++) {
1301 if (*tp != 0x0)
1302 set_bit(index, tbl->it_map);
1303 tp++;
1304 }
1305}
1306
1307/*
1308 * get_tce_space_from_tar():
1309 * Function for kdump case. Get the tce tables from first kernel
1310 * by reading the contents of the base adress register of calgary iommu
1311 */
Marcin Slusarzf7106662008-08-17 17:50:52 +02001312static void __init get_tce_space_from_tar(void)
Chandru95b68de2008-07-25 01:47:55 -07001313{
1314 int bus;
1315 void __iomem *target;
1316 unsigned long tce_space;
1317
1318 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1319 struct calgary_bus_info *info = &bus_info[bus];
1320 unsigned short pci_device;
1321 u32 val;
1322
1323 val = read_pci_config(bus, 0, 0, 0);
1324 pci_device = (val & 0xFFFF0000) >> 16;
1325
1326 if (!is_cal_pci_dev(pci_device))
1327 continue;
1328 if (info->translation_disabled)
1329 continue;
1330
1331 if (calgary_bus_has_devices(bus, pci_device) ||
1332 translate_empty_slots) {
1333 target = calgary_reg(bus_info[bus].bbar,
1334 tar_offset(bus));
1335 tce_space = be64_to_cpu(readq(target));
1336 tce_space = tce_space & TAR_SW_BITS;
1337
1338 tce_space = tce_space & (~specified_table_size);
1339 info->tce_space = (u64 *)__va(tce_space);
1340 }
1341 }
1342 return;
1343}
1344
FUJITA Tomonorif4131c62009-11-14 21:26:50 +09001345static int __init calgary_iommu_init(void)
1346{
1347 int ret;
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 return ret;
1357 }
1358
FUJITA Tomonorif4131c62009-11-14 21:26:50 +09001359 return 0;
1360}
FUJITA Tomonorid7b9f7b2009-11-10 19:46:13 +09001361
Jon Masone4650582006-06-26 13:58:14 +02001362void __init detect_calgary(void)
1363{
Jon Masond2105b12006-07-29 21:42:43 +02001364 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001365 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001366 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001367 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001368 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001369 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001370
1371 /*
1372 * if the user specified iommu=off or iommu=soft or we found
1373 * another HW IOMMU already, bail out.
1374 */
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09001375 if (no_iommu || iommu_detected)
Jon Masone4650582006-06-26 13:58:14 +02001376 return;
1377
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001378 if (!use_calgary)
1379 return;
1380
Andi Kleen0637a702006-09-26 10:52:41 +02001381 if (!early_pci_allowed())
1382 return;
1383
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001384 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1385
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001386 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1387
1388 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001389 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001390 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001391 /*
1392 * The next offset is stored in the 1st word.
1393 * Only parse up until the offset increases:
1394 */
1395 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001396 /* The block id is stored in the 2nd word */
1397 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1398 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001399 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001400 break;
1401 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001402 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001403 offset = *((unsigned short *)(ptr + offset));
1404 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001405 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001406 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1407 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001408 return;
1409 }
1410
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001411 ret = build_detail_arrays();
1412 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001413 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001414 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001415 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001416
Chandru95b68de2008-07-25 01:47:55 -07001417 specified_table_size = determine_tce_table_size((is_kdump_kernel() ?
1418 saved_max_pfn : max_pfn) * PAGE_SIZE);
Jon Masone4650582006-06-26 13:58:14 +02001419
Jon Masond2105b12006-07-29 21:42:43 +02001420 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001421 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001422 unsigned short pci_device;
1423 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001424
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001425 val = read_pci_config(bus, 0, 0, 0);
1426 pci_device = (val & 0xFFFF0000) >> 16;
1427
1428 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001429 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001430
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001431 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001432 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001433
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001434 if (calgary_bus_has_devices(bus, pci_device) ||
1435 translate_empty_slots) {
Chandru95b68de2008-07-25 01:47:55 -07001436 /*
1437 * If it is kdump kernel, find and use tce tables
1438 * from first kernel, else allocate tce tables here
1439 */
1440 if (!is_kdump_kernel()) {
1441 tbl = alloc_tce_table();
1442 if (!tbl)
1443 goto cleanup;
1444 info->tce_space = tbl;
1445 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001446 calgary_found = 1;
Jon Masond2105b12006-07-29 21:42:43 +02001447 }
Jon Masone4650582006-06-26 13:58:14 +02001448 }
1449
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001450 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1451 calgary_found ? "found" : "not found");
1452
Jon Masond2105b12006-07-29 21:42:43 +02001453 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001454 iommu_detected = 1;
1455 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001456 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
FUJITA Tomonori7e055752009-04-14 12:12:29 +09001457 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d\n",
1458 specified_table_size);
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001459
FUJITA Tomonorid7b9f7b2009-11-10 19:46:13 +09001460 x86_init.iommu.iommu_init = calgary_iommu_init;
Jon Masone4650582006-06-26 13:58:14 +02001461 }
1462 return;
1463
1464cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001465 for (--bus; bus >= 0; --bus) {
1466 struct calgary_bus_info *info = &bus_info[bus];
1467
1468 if (info->tce_space)
1469 free_tce_table(info->tce_space);
1470 }
Jon Masone4650582006-06-26 13:58:14 +02001471}
1472
Jon Masone4650582006-06-26 13:58:14 +02001473static int __init calgary_parse_options(char *p)
1474{
1475 unsigned int bridge;
1476 size_t len;
1477 char* endp;
1478
1479 while (*p) {
1480 if (!strncmp(p, "64k", 3))
1481 specified_table_size = TCE_TABLE_SIZE_64K;
1482 else if (!strncmp(p, "128k", 4))
1483 specified_table_size = TCE_TABLE_SIZE_128K;
1484 else if (!strncmp(p, "256k", 4))
1485 specified_table_size = TCE_TABLE_SIZE_256K;
1486 else if (!strncmp(p, "512k", 4))
1487 specified_table_size = TCE_TABLE_SIZE_512K;
1488 else if (!strncmp(p, "1M", 2))
1489 specified_table_size = TCE_TABLE_SIZE_1M;
1490 else if (!strncmp(p, "2M", 2))
1491 specified_table_size = TCE_TABLE_SIZE_2M;
1492 else if (!strncmp(p, "4M", 2))
1493 specified_table_size = TCE_TABLE_SIZE_4M;
1494 else if (!strncmp(p, "8M", 2))
1495 specified_table_size = TCE_TABLE_SIZE_8M;
1496
1497 len = strlen("translate_empty_slots");
1498 if (!strncmp(p, "translate_empty_slots", len))
1499 translate_empty_slots = 1;
1500
1501 len = strlen("disable");
1502 if (!strncmp(p, "disable", len)) {
1503 p += len;
1504 if (*p == '=')
1505 ++p;
1506 if (*p == '\0')
1507 break;
Julia Lawalleff79ae2008-11-25 14:13:03 +01001508 bridge = simple_strtoul(p, &endp, 0);
Jon Masone4650582006-06-26 13:58:14 +02001509 if (p == endp)
1510 break;
1511
Jon Masond2105b12006-07-29 21:42:43 +02001512 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001513 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001514 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001515 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001516 }
1517 }
1518
1519 p = strpbrk(p, ",");
1520 if (!p)
1521 break;
1522
1523 p++; /* skip ',' */
1524 }
1525 return 1;
1526}
1527__setup("calgary=", calgary_parse_options);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001528
1529static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1530{
1531 struct iommu_table *tbl;
1532 unsigned int npages;
1533 int i;
1534
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001535 tbl = pci_iommu(dev->bus);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001536
1537 for (i = 0; i < 4; i++) {
1538 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1539
1540 /* Don't give out TCEs that map MEM resources */
1541 if (!(r->flags & IORESOURCE_MEM))
1542 continue;
1543
1544 /* 0-based? we reserve the whole 1st MB anyway */
1545 if (!r->start)
1546 continue;
1547
1548 /* cover the whole region */
1549 npages = (r->end - r->start) >> PAGE_SHIFT;
1550 npages++;
1551
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001552 iommu_range_reserve(tbl, r->start, npages);
1553 }
1554}
1555
1556static int __init calgary_fixup_tce_spaces(void)
1557{
1558 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001559 struct calgary_bus_info *info;
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001560
1561 if (no_iommu || swiotlb || !calgary_detected)
1562 return -ENODEV;
1563
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001564 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001565
1566 do {
1567 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1568 if (!dev)
1569 break;
1570 if (!is_cal_pci_dev(dev->device))
1571 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001572
1573 info = &bus_info[dev->bus->number];
1574 if (info->translation_disabled)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001575 continue;
1576
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001577 if (!info->tce_space)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001578 continue;
1579
1580 calgary_fixup_one_tce_space(dev);
1581
1582 } while (1);
1583
1584 return 0;
1585}
1586
1587/*
1588 * We need to be call after pcibios_assign_resources (fs_initcall level)
1589 * and before device_initcall.
1590 */
1591rootfs_initcall(calgary_fixup_tce_spaces);