Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Derived from arch/powerpc/kernel/iommu.c |
| 3 | * |
Muli Ben-Yehuda | 9882234 | 2007-07-21 17:10:48 +0200 | [diff] [blame] | 4 | * Copyright IBM Corporation, 2006-2007 |
Jon Mason | d8d2bed | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 6 | * |
Jon Mason | d8d2bed | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 7 | * Author: Jon Mason <jdmason@kudzu.us> |
Muli Ben-Yehuda | aa0a9f3 | 2006-07-10 17:06:15 +0200 | [diff] [blame] | 8 | * Author: Muli Ben-Yehuda <muli@il.ibm.com> |
| 9 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 10 | * 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 | |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 25 | #define pr_fmt(fmt) "Calgary: " fmt |
| 26 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/string.h> |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 34 | #include <linux/crash_dump.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 35 | #include <linux/dma-mapping.h> |
Christoph Hellwig | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame^] | 36 | #include <linux/dma-direct.h> |
Akinobu Mita | a66022c | 2009-12-15 16:48:28 -0800 | [diff] [blame] | 37 | #include <linux/bitmap.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 38 | #include <linux/pci_ids.h> |
| 39 | #include <linux/pci.h> |
| 40 | #include <linux/delay.h> |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 41 | #include <linux/scatterlist.h> |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 42 | #include <linux/iommu-helper.h> |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 43 | |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 44 | #include <asm/iommu.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 45 | #include <asm/calgary.h> |
| 46 | #include <asm/tce.h> |
| 47 | #include <asm/pci-direct.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 48 | #include <asm/dma.h> |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 49 | #include <asm/rio.h> |
Akinobu Mita | ae5830a | 2008-04-19 23:55:19 +0900 | [diff] [blame] | 50 | #include <asm/bios_ebda.h> |
FUJITA Tomonori | d7b9f7b | 2009-11-10 19:46:13 +0900 | [diff] [blame] | 51 | #include <asm/x86_init.h> |
Konrad Rzeszutek Wilk | d2aa232f | 2010-08-26 13:58:02 -0400 | [diff] [blame] | 52 | #include <asm/iommu_table.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 53 | |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 54 | #define CALGARY_MAPPING_ERROR 0 |
| 55 | |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT |
| 57 | int use_calgary __read_mostly = 1; |
| 58 | #else |
| 59 | int use_calgary __read_mostly = 0; |
| 60 | #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */ |
| 61 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 62 | #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1 |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 63 | #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308 |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 64 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 65 | /* register offsets inside the host bridge space */ |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 66 | #define CALGARY_CONFIG_REG 0x0108 |
| 67 | #define PHB_CSR_OFFSET 0x0110 /* Channel Status */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 68 | #define PHB_PLSSR_OFFSET 0x0120 |
| 69 | #define PHB_CONFIG_RW_OFFSET 0x0160 |
| 70 | #define PHB_IOBASE_BAR_LOW 0x0170 |
| 71 | #define PHB_IOBASE_BAR_HIGH 0x0180 |
| 72 | #define PHB_MEM_1_LOW 0x0190 |
| 73 | #define PHB_MEM_1_HIGH 0x01A0 |
| 74 | #define PHB_IO_ADDR_SIZE 0x01B0 |
| 75 | #define PHB_MEM_1_SIZE 0x01C0 |
| 76 | #define PHB_MEM_ST_OFFSET 0x01D0 |
| 77 | #define PHB_AER_OFFSET 0x0200 |
| 78 | #define PHB_CONFIG_0_HIGH 0x0220 |
| 79 | #define PHB_CONFIG_0_LOW 0x0230 |
| 80 | #define PHB_CONFIG_0_END 0x0240 |
| 81 | #define PHB_MEM_2_LOW 0x02B0 |
| 82 | #define PHB_MEM_2_HIGH 0x02C0 |
| 83 | #define PHB_MEM_2_SIZE_HIGH 0x02D0 |
| 84 | #define PHB_MEM_2_SIZE_LOW 0x02E0 |
| 85 | #define PHB_DOSHOLE_OFFSET 0x08E0 |
| 86 | |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 87 | /* CalIOC2 specific */ |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 88 | #define PHB_SAVIOR_L2 0x0DB0 |
| 89 | #define PHB_PAGE_MIG_CTRL 0x0DA8 |
| 90 | #define PHB_PAGE_MIG_DEBUG 0x0DA0 |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 91 | #define PHB_ROOT_COMPLEX_STATUS 0x0CB0 |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 92 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 93 | /* PHB_CONFIG_RW */ |
| 94 | #define PHB_TCE_ENABLE 0x20000000 |
| 95 | #define PHB_SLOT_DISABLE 0x1C000000 |
| 96 | #define PHB_DAC_DISABLE 0x01000000 |
| 97 | #define PHB_MEM2_ENABLE 0x00400000 |
| 98 | #define PHB_MCSR_ENABLE 0x00100000 |
| 99 | /* TAR (Table Address Register) */ |
| 100 | #define TAR_SW_BITS 0x0000ffffffff800fUL |
| 101 | #define TAR_VALID 0x0000000000000008UL |
| 102 | /* CSR (Channel/DMA Status Register) */ |
| 103 | #define CSR_AGENT_MASK 0xffe0ffff |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 104 | /* CCR (Calgary Configuration Register) */ |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 105 | #define CCR_2SEC_TIMEOUT 0x000000000000000EUL |
Muli Ben-Yehuda | 00be3fa | 2007-07-21 17:10:54 +0200 | [diff] [blame] | 106 | /* PMCR/PMDR (Page Migration Control/Debug Registers */ |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 107 | #define PMR_SOFTSTOP 0x80000000 |
| 108 | #define PMR_SOFTSTOPFAULT 0x40000000 |
| 109 | #define PMR_HARDSTOP 0x20000000 |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 110 | |
Darrick J. Wong | 499a00e | 2010-06-24 14:26:47 -0700 | [diff] [blame] | 111 | /* |
| 112 | * The maximum PHB bus number. |
| 113 | * x3950M2 (rare): 8 chassis, 48 PHBs per chassis = 384 |
| 114 | * x3950M2: 4 chassis, 48 PHBs per chassis = 192 |
| 115 | * x3950 (PCIE): 8 chassis, 32 PHBs per chassis = 256 |
| 116 | * x3950 (PCIX): 8 chassis, 16 PHBs per chassis = 128 |
| 117 | */ |
Darrick J. Wong | d596043 | 2010-06-30 17:45:19 -0700 | [diff] [blame] | 118 | #define MAX_PHB_BUS_NUM 256 |
Darrick J. Wong | 499a00e | 2010-06-24 14:26:47 -0700 | [diff] [blame] | 119 | |
| 120 | #define PHBS_PER_CALGARY 4 |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 121 | |
| 122 | /* register offsets in Calgary's internal register space */ |
| 123 | static const unsigned long tar_offsets[] = { |
| 124 | 0x0580 /* TAR0 */, |
| 125 | 0x0588 /* TAR1 */, |
| 126 | 0x0590 /* TAR2 */, |
| 127 | 0x0598 /* TAR3 */ |
| 128 | }; |
| 129 | |
| 130 | static const unsigned long split_queue_offsets[] = { |
| 131 | 0x4870 /* SPLIT QUEUE 0 */, |
| 132 | 0x5870 /* SPLIT QUEUE 1 */, |
| 133 | 0x6870 /* SPLIT QUEUE 2 */, |
| 134 | 0x7870 /* SPLIT QUEUE 3 */ |
| 135 | }; |
| 136 | |
| 137 | static const unsigned long phb_offsets[] = { |
| 138 | 0x8000 /* PHB0 */, |
| 139 | 0x9000 /* PHB1 */, |
| 140 | 0xA000 /* PHB2 */, |
| 141 | 0xB000 /* PHB3 */ |
| 142 | }; |
| 143 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 144 | /* PHB debug registers */ |
| 145 | |
| 146 | static const unsigned long phb_debug_offsets[] = { |
| 147 | 0x4000 /* PHB 0 DEBUG */, |
| 148 | 0x5000 /* PHB 1 DEBUG */, |
| 149 | 0x6000 /* PHB 2 DEBUG */, |
| 150 | 0x7000 /* PHB 3 DEBUG */ |
| 151 | }; |
| 152 | |
| 153 | /* |
| 154 | * STUFF register for each debug PHB, |
| 155 | * byte 1 = start bus number, byte 2 = end bus number |
| 156 | */ |
| 157 | |
| 158 | #define PHB_DEBUG_STUFF_OFFSET 0x0020 |
| 159 | |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 160 | #define EMERGENCY_PAGES 32 /* = 128KB */ |
| 161 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 162 | unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED; |
| 163 | static int translate_empty_slots __read_mostly = 0; |
| 164 | static int calgary_detected __read_mostly = 0; |
| 165 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 166 | static struct rio_table_hdr *rio_table_hdr __initdata; |
| 167 | static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 168 | static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 169 | |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 170 | struct calgary_bus_info { |
| 171 | void *tce_space; |
Muli Ben-Yehuda | 0577f14 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 172 | unsigned char translation_disabled; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 173 | signed char phbid; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 174 | void __iomem *bbar; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 175 | }; |
| 176 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 177 | static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev); |
| 178 | static void calgary_tce_cache_blast(struct iommu_table *tbl); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 179 | static void calgary_dump_error_regs(struct iommu_table *tbl); |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 180 | static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev); |
Muli Ben-Yehuda | 00be3fa | 2007-07-21 17:10:54 +0200 | [diff] [blame] | 181 | static void calioc2_tce_cache_blast(struct iommu_table *tbl); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 182 | static void calioc2_dump_error_regs(struct iommu_table *tbl); |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 183 | static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl); |
| 184 | static void get_tce_space_from_tar(void); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 185 | |
Julia Lawall | d6b56b0 | 2015-11-28 16:58:15 +0100 | [diff] [blame] | 186 | static const struct cal_chipset_ops calgary_chip_ops = { |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 187 | .handle_quirks = calgary_handle_quirks, |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 188 | .tce_cache_blast = calgary_tce_cache_blast, |
| 189 | .dump_error_regs = calgary_dump_error_regs |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
Julia Lawall | d6b56b0 | 2015-11-28 16:58:15 +0100 | [diff] [blame] | 192 | static const struct cal_chipset_ops calioc2_chip_ops = { |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 193 | .handle_quirks = calioc2_handle_quirks, |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 194 | .tce_cache_blast = calioc2_tce_cache_blast, |
| 195 | .dump_error_regs = calioc2_dump_error_regs |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 198 | static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, }; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 199 | |
Muli Ben-Yehuda | d588ba8 | 2007-10-17 18:04:35 +0200 | [diff] [blame] | 200 | static inline int translation_enabled(struct iommu_table *tbl) |
| 201 | { |
| 202 | /* only PHBs with translation enabled have an IOMMU table */ |
| 203 | return (tbl != NULL); |
| 204 | } |
| 205 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 206 | static void iommu_range_reserve(struct iommu_table *tbl, |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 207 | unsigned long start_addr, unsigned int npages) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 208 | { |
| 209 | unsigned long index; |
| 210 | unsigned long end; |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 211 | unsigned long flags; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 212 | |
| 213 | index = start_addr >> PAGE_SHIFT; |
| 214 | |
| 215 | /* bail out if we're asked to reserve a region we don't cover */ |
| 216 | if (index >= tbl->it_size) |
| 217 | return; |
| 218 | |
| 219 | end = index + npages; |
| 220 | if (end > tbl->it_size) /* don't go off the table */ |
| 221 | end = tbl->it_size; |
| 222 | |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 223 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 224 | |
Akinobu Mita | a66022c | 2009-12-15 16:48:28 -0800 | [diff] [blame] | 225 | bitmap_set(tbl->it_map, index, npages); |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 226 | |
| 227 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 228 | } |
| 229 | |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 230 | static unsigned long iommu_range_alloc(struct device *dev, |
| 231 | struct iommu_table *tbl, |
| 232 | unsigned int npages) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 233 | { |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 234 | unsigned long flags; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 235 | unsigned long offset; |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 236 | unsigned long boundary_size; |
| 237 | |
| 238 | boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, |
| 239 | PAGE_SIZE) >> PAGE_SHIFT; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 240 | |
| 241 | BUG_ON(npages == 0); |
| 242 | |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 243 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 244 | |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 245 | offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint, |
| 246 | npages, 0, boundary_size, 0); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 247 | if (offset == ~0UL) { |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 248 | tbl->chip_ops->tce_cache_blast(tbl); |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 249 | |
| 250 | offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0, |
| 251 | npages, 0, boundary_size, 0); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 252 | if (offset == ~0UL) { |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 253 | pr_warn("IOMMU full\n"); |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 254 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 255 | if (panic_on_overflow) |
| 256 | panic("Calgary: fix the allocator.\n"); |
| 257 | else |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 258 | return CALGARY_MAPPING_ERROR; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 262 | tbl->it_hint = offset + npages; |
| 263 | BUG_ON(tbl->it_hint > tbl->it_size); |
| 264 | |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 265 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 266 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 267 | return offset; |
| 268 | } |
| 269 | |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 270 | static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl, |
| 271 | void *vaddr, unsigned int npages, int direction) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 272 | { |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 273 | unsigned long entry; |
FUJITA Tomonori | 1f7564c | 2009-11-15 21:19:54 +0900 | [diff] [blame] | 274 | dma_addr_t ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 275 | |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 276 | entry = iommu_range_alloc(dev, tbl, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 277 | |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 278 | if (unlikely(entry == CALGARY_MAPPING_ERROR)) { |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 279 | pr_warn("failed to allocate %u pages in iommu %p\n", |
| 280 | npages, tbl); |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 281 | return CALGARY_MAPPING_ERROR; |
FUJITA Tomonori | 1f7564c | 2009-11-15 21:19:54 +0900 | [diff] [blame] | 282 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 283 | |
| 284 | /* set the return dma address */ |
| 285 | ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK); |
| 286 | |
| 287 | /* put the TCEs in the HW table */ |
| 288 | tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK, |
| 289 | direction); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 290 | return ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Muli Ben-Yehuda | 3cc39bd | 2007-07-21 17:11:06 +0200 | [diff] [blame] | 293 | static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr, |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 294 | unsigned int npages) |
| 295 | { |
| 296 | unsigned long entry; |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 297 | unsigned long badend; |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 298 | unsigned long flags; |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 299 | |
| 300 | /* were we called with bad_dma_address? */ |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 301 | badend = CALGARY_MAPPING_ERROR + (EMERGENCY_PAGES * PAGE_SIZE); |
Nikola Pajkovsky | 68dee8e | 2016-11-15 09:47:49 +0100 | [diff] [blame] | 302 | if (unlikely(dma_addr < badend)) { |
Arjan van de Ven | bde78a7 | 2008-07-08 09:51:56 -0700 | [diff] [blame] | 303 | WARN(1, KERN_ERR "Calgary: driver tried unmapping bad DMA " |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 304 | "address 0x%Lx\n", dma_addr); |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 305 | return; |
| 306 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 307 | |
| 308 | entry = dma_addr >> PAGE_SHIFT; |
| 309 | |
| 310 | BUG_ON(entry + npages > tbl->it_size); |
| 311 | |
| 312 | tce_free(tbl, entry, npages); |
| 313 | |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 314 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 315 | |
Akinobu Mita | a66022c | 2009-12-15 16:48:28 -0800 | [diff] [blame] | 316 | bitmap_clear(tbl->it_map, entry, npages); |
Muli Ben-Yehuda | 820a149 | 2007-07-21 17:11:04 +0200 | [diff] [blame] | 317 | |
| 318 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 321 | static inline struct iommu_table *find_iommu_table(struct device *dev) |
| 322 | { |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 323 | struct pci_dev *pdev; |
| 324 | struct pci_bus *pbus; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 325 | struct iommu_table *tbl; |
| 326 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 327 | pdev = to_pci_dev(dev); |
| 328 | |
Darrick J. Wong | 4528752 | 2009-12-02 15:05:56 -0800 | [diff] [blame] | 329 | /* search up the device tree for an iommu */ |
Murillo Fernandes Bernardes | f055a06 | 2007-08-10 22:31:00 +0200 | [diff] [blame] | 330 | pbus = pdev->bus; |
Darrick J. Wong | 4528752 | 2009-12-02 15:05:56 -0800 | [diff] [blame] | 331 | do { |
| 332 | tbl = pci_iommu(pbus); |
| 333 | if (tbl && tbl->it_busno == pbus->number) |
| 334 | break; |
| 335 | tbl = NULL; |
Murillo Fernandes Bernardes | f055a06 | 2007-08-10 22:31:00 +0200 | [diff] [blame] | 336 | pbus = pbus->parent; |
Darrick J. Wong | 4528752 | 2009-12-02 15:05:56 -0800 | [diff] [blame] | 337 | } while (pbus); |
Muli Ben-Yehuda | 7354b07 | 2007-07-21 17:11:03 +0200 | [diff] [blame] | 338 | |
Murillo Fernandes Bernardes | f055a06 | 2007-08-10 22:31:00 +0200 | [diff] [blame] | 339 | BUG_ON(tbl && (tbl->it_busno != pbus->number)); |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 340 | |
| 341 | return tbl; |
| 342 | } |
| 343 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 344 | static void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 345 | int nelems,enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 346 | unsigned long attrs) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 347 | { |
Muli Ben-Yehuda | 3cc39bd | 2007-07-21 17:11:06 +0200 | [diff] [blame] | 348 | struct iommu_table *tbl = find_iommu_table(dev); |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 349 | struct scatterlist *s; |
| 350 | int i; |
Muli Ben-Yehuda | 3cc39bd | 2007-07-21 17:11:06 +0200 | [diff] [blame] | 351 | |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 352 | if (!translation_enabled(tbl)) |
Muli Ben-Yehuda | 3cc39bd | 2007-07-21 17:11:06 +0200 | [diff] [blame] | 353 | return; |
| 354 | |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 355 | for_each_sg(sglist, s, nelems, i) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 356 | unsigned int npages; |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 357 | dma_addr_t dma = s->dma_address; |
| 358 | unsigned int dmalen = s->dma_length; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 359 | |
| 360 | if (dmalen == 0) |
| 361 | break; |
| 362 | |
Joerg Roedel | 036b4c5 | 2008-10-15 22:02:12 -0700 | [diff] [blame] | 363 | npages = iommu_num_pages(dma, dmalen, PAGE_SIZE); |
Muli Ben-Yehuda | 3cc39bd | 2007-07-21 17:11:06 +0200 | [diff] [blame] | 364 | iommu_free(tbl, dma, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Yinghai Lu | 0b11e1c | 2007-07-21 17:11:05 +0200 | [diff] [blame] | 368 | static int calgary_map_sg(struct device *dev, struct scatterlist *sg, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 369 | int nelems, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 370 | unsigned long attrs) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 371 | { |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 372 | struct iommu_table *tbl = find_iommu_table(dev); |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 373 | struct scatterlist *s; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 374 | unsigned long vaddr; |
| 375 | unsigned int npages; |
| 376 | unsigned long entry; |
| 377 | int i; |
| 378 | |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 379 | for_each_sg(sg, s, nelems, i) { |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 380 | BUG_ON(!sg_page(s)); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 381 | |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 382 | vaddr = (unsigned long) sg_virt(s); |
Joerg Roedel | 036b4c5 | 2008-10-15 22:02:12 -0700 | [diff] [blame] | 383 | npages = iommu_num_pages(vaddr, s->length, PAGE_SIZE); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 384 | |
FUJITA Tomonori | 1b39b07 | 2008-02-04 22:28:10 -0800 | [diff] [blame] | 385 | entry = iommu_range_alloc(dev, tbl, npages); |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 386 | if (entry == CALGARY_MAPPING_ERROR) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 387 | /* makes sure unmap knows to stop */ |
| 388 | s->dma_length = 0; |
| 389 | goto error; |
| 390 | } |
| 391 | |
| 392 | s->dma_address = (entry << PAGE_SHIFT) | s->offset; |
| 393 | |
| 394 | /* insert into HW table */ |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 395 | tce_build(tbl, entry, npages, vaddr & PAGE_MASK, dir); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 396 | |
| 397 | s->dma_length = s->length; |
| 398 | } |
| 399 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 400 | return nelems; |
| 401 | error: |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 402 | calgary_unmap_sg(dev, sg, nelems, dir, 0); |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 403 | for_each_sg(sg, s, nelems, i) { |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 404 | sg->dma_address = CALGARY_MAPPING_ERROR; |
Jens Axboe | 8b87d9f | 2007-07-24 12:38:15 +0200 | [diff] [blame] | 405 | sg->dma_length = 0; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 406 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 410 | static dma_addr_t calgary_map_page(struct device *dev, struct page *page, |
| 411 | unsigned long offset, size_t size, |
| 412 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 413 | unsigned long attrs) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 414 | { |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 415 | void *vaddr = page_address(page) + offset; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 416 | unsigned long uaddr; |
| 417 | unsigned int npages; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 418 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 419 | |
| 420 | uaddr = (unsigned long)vaddr; |
Joerg Roedel | 036b4c5 | 2008-10-15 22:02:12 -0700 | [diff] [blame] | 421 | npages = iommu_num_pages(uaddr, size, PAGE_SIZE); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 422 | |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 423 | return iommu_alloc(dev, tbl, vaddr, npages, dir); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 424 | } |
| 425 | |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 426 | static void calgary_unmap_page(struct device *dev, dma_addr_t dma_addr, |
| 427 | size_t size, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 428 | unsigned long attrs) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 429 | { |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 430 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 431 | unsigned int npages; |
| 432 | |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 433 | npages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
| 434 | iommu_free(tbl, dma_addr, npages); |
| 435 | } |
| 436 | |
Yinghai Lu | 0b11e1c | 2007-07-21 17:11:05 +0200 | [diff] [blame] | 437 | static void* calgary_alloc_coherent(struct device *dev, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 438 | dma_addr_t *dma_handle, gfp_t flag, unsigned long attrs) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 439 | { |
| 440 | void *ret = NULL; |
| 441 | dma_addr_t mapping; |
| 442 | unsigned int npages, order; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 443 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 444 | |
| 445 | size = PAGE_ALIGN(size); /* size rounded up to full pages */ |
| 446 | npages = size >> PAGE_SHIFT; |
| 447 | order = get_order(size); |
| 448 | |
FUJITA Tomonori | f10ac8a | 2008-09-11 23:08:47 +0900 | [diff] [blame] | 449 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); |
| 450 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 451 | /* alloc enough pages (and possibly more) */ |
| 452 | ret = (void *)__get_free_pages(flag, order); |
| 453 | if (!ret) |
| 454 | goto error; |
| 455 | memset(ret, 0, size); |
| 456 | |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 457 | /* set up tces to cover the allocated range */ |
| 458 | mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL); |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 459 | if (mapping == CALGARY_MAPPING_ERROR) |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 460 | goto free; |
| 461 | *dma_handle = mapping; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 462 | return ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 463 | free: |
| 464 | free_pages((unsigned long)ret, get_order(size)); |
| 465 | ret = NULL; |
| 466 | error: |
| 467 | return ret; |
| 468 | } |
| 469 | |
Joerg Roedel | e4ad68b | 2008-08-19 16:32:41 +0200 | [diff] [blame] | 470 | static void calgary_free_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 471 | void *vaddr, dma_addr_t dma_handle, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 472 | unsigned long attrs) |
Joerg Roedel | e4ad68b | 2008-08-19 16:32:41 +0200 | [diff] [blame] | 473 | { |
| 474 | unsigned int npages; |
| 475 | struct iommu_table *tbl = find_iommu_table(dev); |
| 476 | |
| 477 | size = PAGE_ALIGN(size); |
| 478 | npages = size >> PAGE_SHIFT; |
| 479 | |
| 480 | iommu_free(tbl, dma_handle, npages); |
| 481 | free_pages((unsigned long)vaddr, get_order(size)); |
| 482 | } |
| 483 | |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 484 | static int calgary_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 485 | { |
| 486 | return dma_addr == CALGARY_MAPPING_ERROR; |
| 487 | } |
| 488 | |
Bart Van Assche | 5299709 | 2017-01-20 13:04:01 -0800 | [diff] [blame] | 489 | static const struct dma_map_ops calgary_dma_ops = { |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 490 | .alloc = calgary_alloc_coherent, |
| 491 | .free = calgary_free_coherent, |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 492 | .map_sg = calgary_map_sg, |
| 493 | .unmap_sg = calgary_unmap_sg, |
FUJITA Tomonori | 3991605 | 2009-01-05 23:47:24 +0900 | [diff] [blame] | 494 | .map_page = calgary_map_page, |
| 495 | .unmap_page = calgary_unmap_page, |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 496 | .mapping_error = calgary_mapping_error, |
Christoph Hellwig | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame^] | 497 | .dma_supported = dma_direct_supported, |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 498 | }; |
| 499 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 500 | static inline void __iomem * busno_to_bbar(unsigned char num) |
| 501 | { |
| 502 | return bus_info[num].bbar; |
| 503 | } |
| 504 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 505 | static inline int busno_to_phbid(unsigned char num) |
| 506 | { |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 507 | return bus_info[num].phbid; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static inline unsigned long split_queue_offset(unsigned char num) |
| 511 | { |
| 512 | size_t idx = busno_to_phbid(num); |
| 513 | |
| 514 | return split_queue_offsets[idx]; |
| 515 | } |
| 516 | |
| 517 | static inline unsigned long tar_offset(unsigned char num) |
| 518 | { |
| 519 | size_t idx = busno_to_phbid(num); |
| 520 | |
| 521 | return tar_offsets[idx]; |
| 522 | } |
| 523 | |
| 524 | static inline unsigned long phb_offset(unsigned char num) |
| 525 | { |
| 526 | size_t idx = busno_to_phbid(num); |
| 527 | |
| 528 | return phb_offsets[idx]; |
| 529 | } |
| 530 | |
| 531 | static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset) |
| 532 | { |
| 533 | unsigned long target = ((unsigned long)bar) | offset; |
| 534 | return (void __iomem*)target; |
| 535 | } |
| 536 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 537 | static inline int is_calioc2(unsigned short device) |
| 538 | { |
| 539 | return (device == PCI_DEVICE_ID_IBM_CALIOC2); |
| 540 | } |
| 541 | |
| 542 | static inline int is_calgary(unsigned short device) |
| 543 | { |
| 544 | return (device == PCI_DEVICE_ID_IBM_CALGARY); |
| 545 | } |
| 546 | |
| 547 | static inline int is_cal_pci_dev(unsigned short device) |
| 548 | { |
| 549 | return (is_calgary(device) || is_calioc2(device)); |
| 550 | } |
| 551 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 552 | static void calgary_tce_cache_blast(struct iommu_table *tbl) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 553 | { |
| 554 | u64 val; |
| 555 | u32 aer; |
| 556 | int i = 0; |
| 557 | void __iomem *bbar = tbl->bbar; |
| 558 | void __iomem *target; |
| 559 | |
| 560 | /* disable arbitration on the bus */ |
| 561 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET); |
| 562 | aer = readl(target); |
| 563 | writel(0, target); |
| 564 | |
| 565 | /* read plssr to ensure it got there */ |
| 566 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET); |
| 567 | val = readl(target); |
| 568 | |
| 569 | /* poll split queues until all DMA activity is done */ |
| 570 | target = calgary_reg(bbar, split_queue_offset(tbl->it_busno)); |
| 571 | do { |
| 572 | val = readq(target); |
| 573 | i++; |
| 574 | } while ((val & 0xff) != 0xff && i < 100); |
| 575 | if (i == 100) |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 576 | pr_warn("PCI bus not quiesced, continuing anyway\n"); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 577 | |
| 578 | /* invalidate TCE cache */ |
| 579 | target = calgary_reg(bbar, tar_offset(tbl->it_busno)); |
| 580 | writeq(tbl->tar_val, target); |
| 581 | |
| 582 | /* enable arbitration */ |
| 583 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET); |
| 584 | writel(aer, target); |
| 585 | (void)readl(target); /* flush */ |
| 586 | } |
| 587 | |
Muli Ben-Yehuda | 00be3fa | 2007-07-21 17:10:54 +0200 | [diff] [blame] | 588 | static void calioc2_tce_cache_blast(struct iommu_table *tbl) |
| 589 | { |
| 590 | void __iomem *bbar = tbl->bbar; |
| 591 | void __iomem *target; |
| 592 | u64 val64; |
| 593 | u32 val; |
| 594 | int i = 0; |
| 595 | int count = 1; |
| 596 | unsigned char bus = tbl->it_busno; |
| 597 | |
| 598 | begin: |
| 599 | printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast " |
| 600 | "sequence - count %d\n", bus, count); |
| 601 | |
| 602 | /* 1. using the Page Migration Control reg set SoftStop */ |
| 603 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL); |
| 604 | val = be32_to_cpu(readl(target)); |
| 605 | printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target); |
| 606 | val |= PMR_SOFTSTOP; |
| 607 | printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target); |
| 608 | writel(cpu_to_be32(val), target); |
| 609 | |
| 610 | /* 2. poll split queues until all DMA activity is done */ |
| 611 | printk(KERN_DEBUG "2a. starting to poll split queues\n"); |
| 612 | target = calgary_reg(bbar, split_queue_offset(bus)); |
| 613 | do { |
| 614 | val64 = readq(target); |
| 615 | i++; |
| 616 | } while ((val64 & 0xff) != 0xff && i < 100); |
| 617 | if (i == 100) |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 618 | pr_warn("CalIOC2: PCI bus not quiesced, continuing anyway\n"); |
Muli Ben-Yehuda | 00be3fa | 2007-07-21 17:10:54 +0200 | [diff] [blame] | 619 | |
| 620 | /* 3. poll Page Migration DEBUG for SoftStopFault */ |
| 621 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG); |
| 622 | val = be32_to_cpu(readl(target)); |
| 623 | printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target); |
| 624 | |
| 625 | /* 4. if SoftStopFault - goto (1) */ |
| 626 | if (val & PMR_SOFTSTOPFAULT) { |
| 627 | if (++count < 100) |
| 628 | goto begin; |
| 629 | else { |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 630 | pr_warn("CalIOC2: too many SoftStopFaults, aborting TCE cache flush sequence!\n"); |
Muli Ben-Yehuda | 00be3fa | 2007-07-21 17:10:54 +0200 | [diff] [blame] | 631 | return; /* pray for the best */ |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */ |
| 636 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL); |
| 637 | printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target); |
| 638 | val = be32_to_cpu(readl(target)); |
| 639 | printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target); |
| 640 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG); |
| 641 | val = be32_to_cpu(readl(target)); |
| 642 | printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target); |
| 643 | |
| 644 | /* 6. invalidate TCE cache */ |
| 645 | printk(KERN_DEBUG "6. invalidating TCE cache\n"); |
| 646 | target = calgary_reg(bbar, tar_offset(bus)); |
| 647 | writeq(tbl->tar_val, target); |
| 648 | |
| 649 | /* 7. Re-read PMCR */ |
| 650 | printk(KERN_DEBUG "7a. Re-reading PMCR\n"); |
| 651 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL); |
| 652 | val = be32_to_cpu(readl(target)); |
| 653 | printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target); |
| 654 | |
| 655 | /* 8. Remove HardStop */ |
| 656 | printk(KERN_DEBUG "8a. removing HardStop from PMCR\n"); |
| 657 | target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL); |
| 658 | val = 0; |
| 659 | printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target); |
| 660 | writel(cpu_to_be32(val), target); |
| 661 | val = be32_to_cpu(readl(target)); |
| 662 | printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target); |
| 663 | } |
| 664 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 665 | static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start, |
| 666 | u64 limit) |
| 667 | { |
| 668 | unsigned int numpages; |
| 669 | |
| 670 | limit = limit | 0xfffff; |
| 671 | limit++; |
| 672 | |
| 673 | numpages = ((limit - start) >> PAGE_SHIFT); |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 674 | iommu_range_reserve(pci_iommu(dev->bus), start, numpages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev) |
| 678 | { |
| 679 | void __iomem *target; |
| 680 | u64 low, high, sizelow; |
| 681 | u64 start, limit; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 682 | struct iommu_table *tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 683 | unsigned char busnum = dev->bus->number; |
| 684 | void __iomem *bbar = tbl->bbar; |
| 685 | |
| 686 | /* peripheral MEM_1 region */ |
| 687 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW); |
| 688 | low = be32_to_cpu(readl(target)); |
| 689 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH); |
| 690 | high = be32_to_cpu(readl(target)); |
| 691 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE); |
| 692 | sizelow = be32_to_cpu(readl(target)); |
| 693 | |
| 694 | start = (high << 32) | low; |
| 695 | limit = sizelow; |
| 696 | |
| 697 | calgary_reserve_mem_region(dev, start, limit); |
| 698 | } |
| 699 | |
| 700 | static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev) |
| 701 | { |
| 702 | void __iomem *target; |
| 703 | u32 val32; |
| 704 | u64 low, high, sizelow, sizehigh; |
| 705 | u64 start, limit; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 706 | struct iommu_table *tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 707 | unsigned char busnum = dev->bus->number; |
| 708 | void __iomem *bbar = tbl->bbar; |
| 709 | |
| 710 | /* is it enabled? */ |
| 711 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET); |
| 712 | val32 = be32_to_cpu(readl(target)); |
| 713 | if (!(val32 & PHB_MEM2_ENABLE)) |
| 714 | return; |
| 715 | |
| 716 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW); |
| 717 | low = be32_to_cpu(readl(target)); |
| 718 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH); |
| 719 | high = be32_to_cpu(readl(target)); |
| 720 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW); |
| 721 | sizelow = be32_to_cpu(readl(target)); |
| 722 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH); |
| 723 | sizehigh = be32_to_cpu(readl(target)); |
| 724 | |
| 725 | start = (high << 32) | low; |
| 726 | limit = (sizehigh << 32) | sizelow; |
| 727 | |
| 728 | calgary_reserve_mem_region(dev, start, limit); |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * some regions of the IO address space do not get translated, so we |
| 733 | * must not give devices IO addresses in those regions. The regions |
| 734 | * are the 640KB-1MB region and the two PCI peripheral memory holes. |
| 735 | * Reserve all of them in the IOMMU bitmap to avoid giving them out |
| 736 | * later. |
| 737 | */ |
| 738 | static void __init calgary_reserve_regions(struct pci_dev *dev) |
| 739 | { |
| 740 | unsigned int npages; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 741 | u64 start; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 742 | struct iommu_table *tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 743 | |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 744 | /* reserve EMERGENCY_PAGES from bad_dma_address and up */ |
Christoph Hellwig | 8bd17c6 | 2017-05-22 10:36:43 +0200 | [diff] [blame] | 745 | iommu_range_reserve(tbl, CALGARY_MAPPING_ERROR, EMERGENCY_PAGES); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 746 | |
| 747 | /* avoid the BIOS/VGA first 640KB-1MB region */ |
Muli Ben-Yehuda | e8f2041 | 2007-07-21 17:11:01 +0200 | [diff] [blame] | 748 | /* for CalIOC2 - avoid the entire first MB */ |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 749 | if (is_calgary(dev->device)) { |
| 750 | start = (640 * 1024); |
| 751 | npages = ((1024 - 640) * 1024) >> PAGE_SHIFT; |
| 752 | } else { /* calioc2 */ |
| 753 | start = 0; |
Muli Ben-Yehuda | e8f2041 | 2007-07-21 17:11:01 +0200 | [diff] [blame] | 754 | npages = (1 * 1024 * 1024) >> PAGE_SHIFT; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 755 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 756 | iommu_range_reserve(tbl, start, npages); |
| 757 | |
| 758 | /* reserve the two PCI peripheral memory regions in IO space */ |
| 759 | calgary_reserve_peripheral_mem_1(dev); |
| 760 | calgary_reserve_peripheral_mem_2(dev); |
| 761 | } |
| 762 | |
| 763 | static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar) |
| 764 | { |
| 765 | u64 val64; |
| 766 | u64 table_phys; |
| 767 | void __iomem *target; |
| 768 | int ret; |
| 769 | struct iommu_table *tbl; |
| 770 | |
| 771 | /* build TCE tables for each PHB */ |
| 772 | ret = build_tce_table(dev, bbar); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 776 | tbl = pci_iommu(dev->bus); |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 777 | tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space; |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 778 | |
| 779 | if (is_kdump_kernel()) |
| 780 | calgary_init_bitmap_from_tce_table(tbl); |
| 781 | else |
| 782 | tce_free(tbl, 0, tbl->it_size); |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 783 | |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 784 | if (is_calgary(dev->device)) |
| 785 | tbl->chip_ops = &calgary_chip_ops; |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 786 | else if (is_calioc2(dev->device)) |
| 787 | tbl->chip_ops = &calioc2_chip_ops; |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 788 | else |
| 789 | BUG(); |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 790 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 791 | calgary_reserve_regions(dev); |
| 792 | |
| 793 | /* set TARs for each PHB */ |
| 794 | target = calgary_reg(bbar, tar_offset(dev->bus->number)); |
| 795 | val64 = be64_to_cpu(readq(target)); |
| 796 | |
| 797 | /* zero out all TAR bits under sw control */ |
| 798 | val64 &= ~TAR_SW_BITS; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 799 | table_phys = (u64)__pa(tbl->it_base); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 800 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 801 | val64 |= table_phys; |
| 802 | |
| 803 | BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M); |
| 804 | val64 |= (u64) specified_table_size; |
| 805 | |
| 806 | tbl->tar_val = cpu_to_be64(val64); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 807 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 808 | writeq(tbl->tar_val, target); |
| 809 | readq(target); /* flush */ |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 814 | static void __init calgary_free_bus(struct pci_dev *dev) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 815 | { |
| 816 | u64 val64; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 817 | struct iommu_table *tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 818 | void __iomem *target; |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 819 | unsigned int bitmapsz; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 820 | |
| 821 | target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number)); |
| 822 | val64 = be64_to_cpu(readq(target)); |
| 823 | val64 &= ~TAR_SW_BITS; |
| 824 | writeq(cpu_to_be64(val64), target); |
| 825 | readq(target); /* flush */ |
| 826 | |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 827 | bitmapsz = tbl->it_size / BITS_PER_BYTE; |
| 828 | free_pages((unsigned long)tbl->it_map, get_order(bitmapsz)); |
| 829 | tbl->it_map = NULL; |
| 830 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 831 | kfree(tbl); |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 832 | |
| 833 | set_pci_iommu(dev->bus, NULL); |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 834 | |
| 835 | /* Can't free bootmem allocated memory after system is up :-( */ |
| 836 | bus_info[dev->bus->number].tce_space = NULL; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 839 | static void calgary_dump_error_regs(struct iommu_table *tbl) |
| 840 | { |
| 841 | void __iomem *bbar = tbl->bbar; |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 842 | void __iomem *target; |
Muli Ben-Yehuda | ddbd41b | 2007-07-21 17:10:57 +0200 | [diff] [blame] | 843 | u32 csr, plssr; |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 844 | |
| 845 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET); |
Muli Ben-Yehuda | ddbd41b | 2007-07-21 17:10:57 +0200 | [diff] [blame] | 846 | csr = be32_to_cpu(readl(target)); |
| 847 | |
| 848 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET); |
| 849 | plssr = be32_to_cpu(readl(target)); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 850 | |
| 851 | /* If no error, the agent ID in the CSR is not valid */ |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 852 | pr_emerg("DMA error on Calgary PHB 0x%x, 0x%08x@CSR 0x%08x@PLSSR\n", |
| 853 | tbl->it_busno, csr, plssr); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void calioc2_dump_error_regs(struct iommu_table *tbl) |
| 857 | { |
| 858 | void __iomem *bbar = tbl->bbar; |
| 859 | u32 csr, csmr, plssr, mck, rcstat; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 860 | void __iomem *target; |
| 861 | unsigned long phboff = phb_offset(tbl->it_busno); |
| 862 | unsigned long erroff; |
| 863 | u32 errregs[7]; |
| 864 | int i; |
| 865 | |
| 866 | /* dump CSR */ |
| 867 | target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET); |
| 868 | csr = be32_to_cpu(readl(target)); |
| 869 | /* dump PLSSR */ |
| 870 | target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET); |
| 871 | plssr = be32_to_cpu(readl(target)); |
| 872 | /* dump CSMR */ |
| 873 | target = calgary_reg(bbar, phboff | 0x290); |
| 874 | csmr = be32_to_cpu(readl(target)); |
| 875 | /* dump mck */ |
| 876 | target = calgary_reg(bbar, phboff | 0x800); |
| 877 | mck = be32_to_cpu(readl(target)); |
| 878 | |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 879 | pr_emerg("DMA error on CalIOC2 PHB 0x%x\n", tbl->it_busno); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 880 | |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 881 | pr_emerg("0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n", |
| 882 | csr, plssr, csmr, mck); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 883 | |
| 884 | /* dump rest of error regs */ |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 885 | pr_emerg(""); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 886 | for (i = 0; i < ARRAY_SIZE(errregs); i++) { |
Muli Ben-Yehuda | 7354b07 | 2007-07-21 17:11:03 +0200 | [diff] [blame] | 887 | /* err regs are at 0x810 - 0x870 */ |
| 888 | erroff = (0x810 + (i * 0x10)); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 889 | target = calgary_reg(bbar, phboff | erroff); |
| 890 | errregs[i] = be32_to_cpu(readl(target)); |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 891 | pr_cont("0x%08x@0x%lx ", errregs[i], erroff); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 892 | } |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 893 | pr_cont("\n"); |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 894 | |
| 895 | /* root complex status */ |
| 896 | target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS); |
| 897 | rcstat = be32_to_cpu(readl(target)); |
| 898 | printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat, |
| 899 | PHB_ROOT_COMPLEX_STATUS); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Kees Cook | 3142692 | 2017-10-19 16:37:43 -0700 | [diff] [blame] | 902 | static void calgary_watchdog(struct timer_list *t) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 903 | { |
Kees Cook | 3142692 | 2017-10-19 16:37:43 -0700 | [diff] [blame] | 904 | struct iommu_table *tbl = from_timer(tbl, t, watchdog_timer); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 905 | void __iomem *bbar = tbl->bbar; |
| 906 | u32 val32; |
| 907 | void __iomem *target; |
| 908 | |
| 909 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET); |
| 910 | val32 = be32_to_cpu(readl(target)); |
| 911 | |
| 912 | /* If no error, the agent ID in the CSR is not valid */ |
| 913 | if (val32 & CSR_AGENT_MASK) { |
Muli Ben-Yehuda | 8cb32dc | 2007-07-21 17:10:55 +0200 | [diff] [blame] | 914 | tbl->chip_ops->dump_error_regs(tbl); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 915 | |
| 916 | /* reset error */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 917 | writel(0, target); |
| 918 | |
| 919 | /* Disable bus that caused the error */ |
| 920 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 921 | PHB_CONFIG_RW_OFFSET); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 922 | val32 = be32_to_cpu(readl(target)); |
| 923 | val32 |= PHB_SLOT_DISABLE; |
| 924 | writel(cpu_to_be32(val32), target); |
| 925 | readl(target); /* flush */ |
| 926 | } else { |
| 927 | /* Reset the timer */ |
| 928 | mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ); |
| 929 | } |
| 930 | } |
| 931 | |
Muli Ben-Yehuda | a2b663f | 2007-07-21 17:10:47 +0200 | [diff] [blame] | 932 | static void __init calgary_set_split_completion_timeout(void __iomem *bbar, |
| 933 | unsigned char busnum, unsigned long timeout) |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 934 | { |
| 935 | u64 val64; |
| 936 | void __iomem *target; |
Muli Ben-Yehuda | 58db854 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 937 | unsigned int phb_shift = ~0; /* silence gcc */ |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 938 | u64 mask; |
| 939 | |
| 940 | switch (busno_to_phbid(busnum)) { |
| 941 | case 0: phb_shift = (63 - 19); |
| 942 | break; |
| 943 | case 1: phb_shift = (63 - 23); |
| 944 | break; |
| 945 | case 2: phb_shift = (63 - 27); |
| 946 | break; |
| 947 | case 3: phb_shift = (63 - 35); |
| 948 | break; |
| 949 | default: |
| 950 | BUG_ON(busno_to_phbid(busnum)); |
| 951 | } |
| 952 | |
| 953 | target = calgary_reg(bbar, CALGARY_CONFIG_REG); |
| 954 | val64 = be64_to_cpu(readq(target)); |
| 955 | |
| 956 | /* zero out this PHB's timer bits */ |
| 957 | mask = ~(0xFUL << phb_shift); |
| 958 | val64 &= mask; |
Muli Ben-Yehuda | a2b663f | 2007-07-21 17:10:47 +0200 | [diff] [blame] | 959 | val64 |= (timeout << phb_shift); |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 960 | writeq(cpu_to_be64(val64), target); |
| 961 | readq(target); /* flush */ |
| 962 | } |
| 963 | |
Sam Ravnborg | 31f3dff | 2008-02-01 17:49:42 +0100 | [diff] [blame] | 964 | static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev) |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 965 | { |
| 966 | unsigned char busnum = dev->bus->number; |
| 967 | void __iomem *bbar = tbl->bbar; |
| 968 | void __iomem *target; |
| 969 | u32 val; |
| 970 | |
Muli Ben-Yehuda | 8bcf770 | 2007-07-21 17:11:00 +0200 | [diff] [blame] | 971 | /* |
| 972 | * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1 |
| 973 | */ |
| 974 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2); |
| 975 | val = cpu_to_be32(readl(target)); |
| 976 | val |= 0x00800000; |
| 977 | writel(cpu_to_be32(val), target); |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Sam Ravnborg | 31f3dff | 2008-02-01 17:49:42 +0100 | [diff] [blame] | 980 | static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev) |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 981 | { |
| 982 | unsigned char busnum = dev->bus->number; |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 983 | |
| 984 | /* |
| 985 | * Give split completion a longer timeout on bus 1 for aic94xx |
| 986 | * http://bugzilla.kernel.org/show_bug.cgi?id=7180 |
| 987 | */ |
Muli Ben-Yehuda | c386010 | 2007-07-21 17:10:53 +0200 | [diff] [blame] | 988 | if (is_calgary(dev->device) && (busnum == 1)) |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 989 | calgary_set_split_completion_timeout(tbl->bbar, busnum, |
| 990 | CCR_2SEC_TIMEOUT); |
| 991 | } |
| 992 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 993 | static void __init calgary_enable_translation(struct pci_dev *dev) |
| 994 | { |
| 995 | u32 val32; |
| 996 | unsigned char busnum; |
| 997 | void __iomem *target; |
| 998 | void __iomem *bbar; |
| 999 | struct iommu_table *tbl; |
| 1000 | |
| 1001 | busnum = dev->bus->number; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 1002 | tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1003 | bbar = tbl->bbar; |
| 1004 | |
| 1005 | /* enable TCE in PHB Config Register */ |
| 1006 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET); |
| 1007 | val32 = be32_to_cpu(readl(target)); |
| 1008 | val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE; |
| 1009 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1010 | printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n", |
| 1011 | (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ? |
| 1012 | "Calgary" : "CalIOC2", busnum); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1013 | printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this " |
| 1014 | "bus.\n"); |
| 1015 | |
| 1016 | writel(cpu_to_be32(val32), target); |
| 1017 | readl(target); /* flush */ |
| 1018 | |
Kees Cook | 3142692 | 2017-10-19 16:37:43 -0700 | [diff] [blame] | 1019 | timer_setup(&tbl->watchdog_timer, calgary_watchdog, 0); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1020 | mod_timer(&tbl->watchdog_timer, jiffies); |
| 1021 | } |
| 1022 | |
| 1023 | static void __init calgary_disable_translation(struct pci_dev *dev) |
| 1024 | { |
| 1025 | u32 val32; |
| 1026 | unsigned char busnum; |
| 1027 | void __iomem *target; |
| 1028 | void __iomem *bbar; |
| 1029 | struct iommu_table *tbl; |
| 1030 | |
| 1031 | busnum = dev->bus->number; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 1032 | tbl = pci_iommu(dev->bus); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1033 | bbar = tbl->bbar; |
| 1034 | |
| 1035 | /* disable TCE in PHB Config Register */ |
| 1036 | target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET); |
| 1037 | val32 = be32_to_cpu(readl(target)); |
| 1038 | val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE); |
| 1039 | |
Jon Mason | 70d666d | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1040 | printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1041 | writel(cpu_to_be32(val32), target); |
| 1042 | readl(target); /* flush */ |
| 1043 | |
| 1044 | del_timer_sync(&tbl->watchdog_timer); |
| 1045 | } |
| 1046 | |
Muli Ben-Yehuda | a4fc520 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1047 | static void __init calgary_init_one_nontraslated(struct pci_dev *dev) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1048 | { |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1049 | pci_dev_get(dev); |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 1050 | set_pci_iommu(dev->bus, NULL); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1051 | |
| 1052 | /* is the device behind a bridge? */ |
| 1053 | if (dev->bus->parent) |
| 1054 | dev->bus->parent->self = dev; |
| 1055 | else |
| 1056 | dev->bus->self = dev; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static int __init calgary_init_one(struct pci_dev *dev) |
| 1060 | { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1061 | void __iomem *bbar; |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 1062 | struct iommu_table *tbl; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1063 | int ret; |
| 1064 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1065 | bbar = busno_to_bbar(dev->bus->number); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1066 | ret = calgary_setup_tar(dev, bbar); |
| 1067 | if (ret) |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1068 | goto done; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1069 | |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1070 | pci_dev_get(dev); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1071 | |
| 1072 | if (dev->bus->parent) { |
| 1073 | if (dev->bus->parent->self) |
| 1074 | printk(KERN_WARNING "Calgary: IEEEE, dev %p has " |
| 1075 | "bus->parent->self!\n", dev); |
| 1076 | dev->bus->parent->self = dev; |
| 1077 | } else |
| 1078 | dev->bus->self = dev; |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 1079 | |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 1080 | tbl = pci_iommu(dev->bus); |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 1081 | tbl->chip_ops->handle_quirks(tbl, dev); |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 1082 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1083 | calgary_enable_translation(dev); |
| 1084 | |
| 1085 | return 0; |
| 1086 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1087 | done: |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1091 | static int __init calgary_locate_bbars(void) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1092 | { |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1093 | int ret; |
| 1094 | int rioidx, phb, bus; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1095 | void __iomem *bbar; |
| 1096 | void __iomem *target; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1097 | unsigned long offset; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1098 | u8 start_bus, end_bus; |
| 1099 | u32 val; |
| 1100 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1101 | ret = -ENODATA; |
| 1102 | for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) { |
| 1103 | struct rio_detail *rio = rio_devs[rioidx]; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1104 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1105 | if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY)) |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1106 | continue; |
| 1107 | |
| 1108 | /* map entire 1MB of Calgary config space */ |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1109 | bbar = ioremap_nocache(rio->BBAR, 1024 * 1024); |
| 1110 | if (!bbar) |
| 1111 | goto error; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1112 | |
| 1113 | for (phb = 0; phb < PHBS_PER_CALGARY; phb++) { |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1114 | offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET; |
| 1115 | target = calgary_reg(bbar, offset); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1116 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1117 | val = be32_to_cpu(readl(target)); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1118 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1119 | start_bus = (u8)((val & 0x00FF0000) >> 16); |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1120 | end_bus = (u8)((val & 0x0000FF00) >> 8); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1121 | |
| 1122 | if (end_bus) { |
| 1123 | for (bus = start_bus; bus <= end_bus; bus++) { |
| 1124 | bus_info[bus].bbar = bbar; |
| 1125 | bus_info[bus].phbid = phb; |
| 1126 | } |
| 1127 | } else { |
| 1128 | bus_info[start_bus].bbar = bbar; |
| 1129 | bus_info[start_bus].phbid = phb; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1134 | return 0; |
| 1135 | |
| 1136 | error: |
| 1137 | /* scan bus_info and iounmap any bbars we previously ioremap'd */ |
| 1138 | for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++) |
| 1139 | if (bus_info[bus].bbar) |
| 1140 | iounmap(bus_info[bus].bbar); |
| 1141 | |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | static int __init calgary_init(void) |
| 1146 | { |
| 1147 | int ret; |
| 1148 | struct pci_dev *dev = NULL; |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1149 | struct calgary_bus_info *info; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1150 | |
| 1151 | ret = calgary_locate_bbars(); |
| 1152 | if (ret) |
| 1153 | return ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1154 | |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1155 | /* Purely for kdump kernel case */ |
| 1156 | if (is_kdump_kernel()) |
| 1157 | get_tce_space_from_tar(); |
| 1158 | |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1159 | do { |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1160 | dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1161 | if (!dev) |
| 1162 | break; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1163 | if (!is_cal_pci_dev(dev->device)) |
| 1164 | continue; |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1165 | |
| 1166 | info = &bus_info[dev->bus->number]; |
| 1167 | if (info->translation_disabled) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1168 | calgary_init_one_nontraslated(dev); |
| 1169 | continue; |
| 1170 | } |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1171 | |
| 1172 | if (!info->tce_space && !translate_empty_slots) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1173 | continue; |
Muli Ben-Yehuda | 12de257 | 2007-07-21 17:11:02 +0200 | [diff] [blame] | 1174 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1175 | ret = calgary_init_one(dev); |
| 1176 | if (ret) |
| 1177 | goto error; |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1178 | } while (1); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1179 | |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 1180 | dev = NULL; |
| 1181 | for_each_pci_dev(dev) { |
| 1182 | struct iommu_table *tbl; |
| 1183 | |
| 1184 | tbl = find_iommu_table(&dev->dev); |
| 1185 | |
| 1186 | if (translation_enabled(tbl)) |
Bart Van Assche | 5657933 | 2017-01-20 13:04:02 -0800 | [diff] [blame] | 1187 | dev->dev.dma_ops = &calgary_dma_ops; |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1190 | return ret; |
| 1191 | |
| 1192 | error: |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1193 | do { |
Greg Kroah-Hartman | a2b5d87 | 2008-02-13 09:32:03 -0800 | [diff] [blame] | 1194 | dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev); |
Muli Ben-Yehuda | 9f2dc46 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1195 | if (!dev) |
| 1196 | break; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1197 | if (!is_cal_pci_dev(dev->device)) |
| 1198 | continue; |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1199 | |
| 1200 | info = &bus_info[dev->bus->number]; |
| 1201 | if (info->translation_disabled) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1202 | pci_dev_put(dev); |
| 1203 | continue; |
| 1204 | } |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1205 | if (!info->tce_space && !translate_empty_slots) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1206 | continue; |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1207 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1208 | calgary_disable_translation(dev); |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1209 | calgary_free_bus(dev); |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1210 | pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */ |
Bart Van Assche | 5657933 | 2017-01-20 13:04:02 -0800 | [diff] [blame] | 1211 | dev->dev.dma_ops = NULL; |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1212 | } while (1); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1213 | |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
WANG Chao | 0534af0 | 2014-03-10 22:52:00 +0800 | [diff] [blame] | 1217 | static inline int __init determine_tce_table_size(void) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1218 | { |
| 1219 | int ret; |
| 1220 | |
| 1221 | if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED) |
| 1222 | return specified_table_size; |
| 1223 | |
WANG Chao | 0534af0 | 2014-03-10 22:52:00 +0800 | [diff] [blame] | 1224 | if (is_kdump_kernel() && saved_max_pfn) { |
| 1225 | /* |
| 1226 | * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to |
| 1227 | * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each |
| 1228 | * larger table size has twice as many entries, so shift the |
| 1229 | * max ram address by 13 to divide by 8K and then look at the |
| 1230 | * order of the result to choose between 0-7. |
| 1231 | */ |
| 1232 | ret = get_order((saved_max_pfn * PAGE_SIZE) >> 13); |
| 1233 | if (ret > TCE_TABLE_SIZE_8M) |
| 1234 | ret = TCE_TABLE_SIZE_8M; |
| 1235 | } else { |
| 1236 | /* |
| 1237 | * Use 8M by default (suggested by Muli) if it's not |
| 1238 | * kdump kernel and saved_max_pfn isn't set. |
| 1239 | */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1240 | ret = TCE_TABLE_SIZE_8M; |
WANG Chao | 0534af0 | 2014-03-10 22:52:00 +0800 | [diff] [blame] | 1241 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1242 | |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1246 | static int __init build_detail_arrays(void) |
| 1247 | { |
| 1248 | unsigned long ptr; |
David Howells | 85d5779 | 2008-08-18 11:58:17 +0200 | [diff] [blame] | 1249 | unsigned numnodes, i; |
| 1250 | int scal_detail_size, rio_detail_size; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1251 | |
David Howells | 85d5779 | 2008-08-18 11:58:17 +0200 | [diff] [blame] | 1252 | numnodes = rio_table_hdr->num_scal_dev; |
| 1253 | if (numnodes > MAX_NUMNODES){ |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1254 | printk(KERN_WARNING |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1255 | "Calgary: MAX_NUMNODES too low! Defined as %d, " |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1256 | "but system has %d nodes.\n", |
David Howells | 85d5779 | 2008-08-18 11:58:17 +0200 | [diff] [blame] | 1257 | MAX_NUMNODES, numnodes); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1258 | return -ENODEV; |
| 1259 | } |
| 1260 | |
| 1261 | switch (rio_table_hdr->version){ |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1262 | case 2: |
| 1263 | scal_detail_size = 11; |
| 1264 | rio_detail_size = 13; |
| 1265 | break; |
| 1266 | case 3: |
| 1267 | scal_detail_size = 12; |
| 1268 | rio_detail_size = 15; |
| 1269 | break; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1270 | default: |
| 1271 | printk(KERN_WARNING |
| 1272 | "Calgary: Invalid Rio Grande Table Version: %d\n", |
| 1273 | rio_table_hdr->version); |
| 1274 | return -EPROTO; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | ptr = ((unsigned long)rio_table_hdr) + 3; |
David Howells | 85d5779 | 2008-08-18 11:58:17 +0200 | [diff] [blame] | 1278 | for (i = 0; i < numnodes; i++, ptr += scal_detail_size) |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1279 | scal_devs[i] = (struct scal_detail *)ptr; |
| 1280 | |
| 1281 | for (i = 0; i < rio_table_hdr->num_rio_dev; |
| 1282 | i++, ptr += rio_detail_size) |
| 1283 | rio_devs[i] = (struct rio_detail *)ptr; |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1288 | static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev) |
| 1289 | { |
| 1290 | int dev; |
| 1291 | u32 val; |
| 1292 | |
| 1293 | if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) { |
| 1294 | /* |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 1295 | * FIXME: properly scan for devices across the |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1296 | * PCI-to-PCI bridge on every CalIOC2 port. |
| 1297 | */ |
| 1298 | return 1; |
| 1299 | } |
| 1300 | |
| 1301 | for (dev = 1; dev < 8; dev++) { |
| 1302 | val = read_pci_config(bus, dev, 0, 0); |
| 1303 | if (val != 0xffffffff) |
| 1304 | break; |
| 1305 | } |
| 1306 | return (val != 0xffffffff); |
| 1307 | } |
| 1308 | |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1309 | /* |
| 1310 | * calgary_init_bitmap_from_tce_table(): |
Lucas De Marchi | 0d2eb44 | 2011-03-17 16:24:16 -0300 | [diff] [blame] | 1311 | * Function for kdump case. In the second/kdump kernel initialize |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1312 | * the bitmap based on the tce table entries obtained from first kernel |
| 1313 | */ |
| 1314 | static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl) |
| 1315 | { |
| 1316 | u64 *tp; |
| 1317 | unsigned int index; |
| 1318 | tp = ((u64 *)tbl->it_base); |
| 1319 | for (index = 0 ; index < tbl->it_size; index++) { |
| 1320 | if (*tp != 0x0) |
| 1321 | set_bit(index, tbl->it_map); |
| 1322 | tp++; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * get_tce_space_from_tar(): |
| 1328 | * Function for kdump case. Get the tce tables from first kernel |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 1329 | * by reading the contents of the base address register of calgary iommu |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1330 | */ |
Marcin Slusarz | f710666 | 2008-08-17 17:50:52 +0200 | [diff] [blame] | 1331 | static void __init get_tce_space_from_tar(void) |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1332 | { |
| 1333 | int bus; |
| 1334 | void __iomem *target; |
| 1335 | unsigned long tce_space; |
| 1336 | |
| 1337 | for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) { |
| 1338 | struct calgary_bus_info *info = &bus_info[bus]; |
| 1339 | unsigned short pci_device; |
| 1340 | u32 val; |
| 1341 | |
| 1342 | val = read_pci_config(bus, 0, 0, 0); |
| 1343 | pci_device = (val & 0xFFFF0000) >> 16; |
| 1344 | |
| 1345 | if (!is_cal_pci_dev(pci_device)) |
| 1346 | continue; |
| 1347 | if (info->translation_disabled) |
| 1348 | continue; |
| 1349 | |
| 1350 | if (calgary_bus_has_devices(bus, pci_device) || |
| 1351 | translate_empty_slots) { |
| 1352 | target = calgary_reg(bus_info[bus].bbar, |
| 1353 | tar_offset(bus)); |
| 1354 | tce_space = be64_to_cpu(readq(target)); |
| 1355 | tce_space = tce_space & TAR_SW_BITS; |
| 1356 | |
| 1357 | tce_space = tce_space & (~specified_table_size); |
| 1358 | info->tce_space = (u64 *)__va(tce_space); |
| 1359 | } |
| 1360 | } |
| 1361 | return; |
| 1362 | } |
| 1363 | |
FUJITA Tomonori | f4131c6 | 2009-11-14 21:26:50 +0900 | [diff] [blame] | 1364 | static int __init calgary_iommu_init(void) |
| 1365 | { |
| 1366 | int ret; |
| 1367 | |
| 1368 | /* ok, we're trying to use Calgary - let's roll */ |
| 1369 | printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n"); |
| 1370 | |
| 1371 | ret = calgary_init(); |
| 1372 | if (ret) { |
| 1373 | printk(KERN_ERR "PCI-DMA: Calgary init failed %d, " |
| 1374 | "falling back to no_iommu\n", ret); |
| 1375 | return ret; |
| 1376 | } |
| 1377 | |
FUJITA Tomonori | f4131c6 | 2009-11-14 21:26:50 +0900 | [diff] [blame] | 1378 | return 0; |
| 1379 | } |
FUJITA Tomonori | d7b9f7b | 2009-11-10 19:46:13 +0900 | [diff] [blame] | 1380 | |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1381 | int __init detect_calgary(void) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1382 | { |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1383 | int bus; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1384 | void *tbl; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1385 | int calgary_found = 0; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1386 | unsigned long ptr; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1387 | unsigned int offset, prev_offset; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1388 | int ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1389 | |
| 1390 | /* |
| 1391 | * if the user specified iommu=off or iommu=soft or we found |
| 1392 | * another HW IOMMU already, bail out. |
| 1393 | */ |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 1394 | if (no_iommu || iommu_detected) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1395 | return -ENODEV; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1396 | |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 1397 | if (!use_calgary) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1398 | return -ENODEV; |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 1399 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 1400 | if (!early_pci_allowed()) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1401 | return -ENODEV; |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 1402 | |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1403 | printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n"); |
| 1404 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1405 | ptr = (unsigned long)phys_to_virt(get_bios_ebda()); |
| 1406 | |
| 1407 | rio_table_hdr = NULL; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1408 | prev_offset = 0; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1409 | offset = 0x180; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1410 | /* |
| 1411 | * The next offset is stored in the 1st word. |
| 1412 | * Only parse up until the offset increases: |
| 1413 | */ |
| 1414 | while (offset > prev_offset) { |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1415 | /* The block id is stored in the 2nd word */ |
| 1416 | if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){ |
| 1417 | /* set the pointer past the offset & block id */ |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1418 | rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1419 | break; |
| 1420 | } |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1421 | prev_offset = offset; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1422 | offset = *((unsigned short *)(ptr + offset)); |
| 1423 | } |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1424 | if (!rio_table_hdr) { |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1425 | printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table " |
| 1426 | "in EBDA - bailing!\n"); |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1427 | return -ENODEV; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1430 | ret = build_detail_arrays(); |
| 1431 | if (ret) { |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1432 | printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret); |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1433 | return -ENOMEM; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1434 | } |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1435 | |
WANG Chao | 0534af0 | 2014-03-10 22:52:00 +0800 | [diff] [blame] | 1436 | specified_table_size = determine_tce_table_size(); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1437 | |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1438 | for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) { |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1439 | struct calgary_bus_info *info = &bus_info[bus]; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1440 | unsigned short pci_device; |
| 1441 | u32 val; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1442 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1443 | val = read_pci_config(bus, 0, 0, 0); |
| 1444 | pci_device = (val & 0xFFFF0000) >> 16; |
| 1445 | |
| 1446 | if (!is_cal_pci_dev(pci_device)) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1447 | continue; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1448 | |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1449 | if (info->translation_disabled) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1450 | continue; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1451 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1452 | if (calgary_bus_has_devices(bus, pci_device) || |
| 1453 | translate_empty_slots) { |
Chandru | 95b68de | 2008-07-25 01:47:55 -0700 | [diff] [blame] | 1454 | /* |
| 1455 | * If it is kdump kernel, find and use tce tables |
| 1456 | * from first kernel, else allocate tce tables here |
| 1457 | */ |
| 1458 | if (!is_kdump_kernel()) { |
| 1459 | tbl = alloc_tce_table(); |
| 1460 | if (!tbl) |
| 1461 | goto cleanup; |
| 1462 | info->tce_space = tbl; |
| 1463 | } |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame] | 1464 | calgary_found = 1; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1465 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1468 | printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n", |
| 1469 | calgary_found ? "found" : "not found"); |
| 1470 | |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1471 | if (calgary_found) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1472 | iommu_detected = 1; |
| 1473 | calgary_detected = 1; |
Muli Ben-Yehuda | de68465 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 1474 | printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n"); |
FUJITA Tomonori | 7e05575 | 2009-04-14 12:12:29 +0900 | [diff] [blame] | 1475 | printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d\n", |
| 1476 | specified_table_size); |
Alexis Bruemmer | 1956a96 | 2008-07-25 19:44:51 -0700 | [diff] [blame] | 1477 | |
FUJITA Tomonori | d7b9f7b | 2009-11-10 19:46:13 +0900 | [diff] [blame] | 1478 | x86_init.iommu.iommu_init = calgary_iommu_init; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1479 | } |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1480 | return calgary_found; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1481 | |
| 1482 | cleanup: |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1483 | for (--bus; bus >= 0; --bus) { |
| 1484 | struct calgary_bus_info *info = &bus_info[bus]; |
| 1485 | |
| 1486 | if (info->tce_space) |
| 1487 | free_tce_table(info->tce_space); |
| 1488 | } |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 1489 | return -ENOMEM; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1492 | static int __init calgary_parse_options(char *p) |
| 1493 | { |
| 1494 | unsigned int bridge; |
Shuah Khan | 74bc491 | 2012-05-20 17:24:28 -0600 | [diff] [blame] | 1495 | unsigned long val; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1496 | size_t len; |
Shuah Khan | 74bc491 | 2012-05-20 17:24:28 -0600 | [diff] [blame] | 1497 | ssize_t ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1498 | |
| 1499 | while (*p) { |
| 1500 | if (!strncmp(p, "64k", 3)) |
| 1501 | specified_table_size = TCE_TABLE_SIZE_64K; |
| 1502 | else if (!strncmp(p, "128k", 4)) |
| 1503 | specified_table_size = TCE_TABLE_SIZE_128K; |
| 1504 | else if (!strncmp(p, "256k", 4)) |
| 1505 | specified_table_size = TCE_TABLE_SIZE_256K; |
| 1506 | else if (!strncmp(p, "512k", 4)) |
| 1507 | specified_table_size = TCE_TABLE_SIZE_512K; |
| 1508 | else if (!strncmp(p, "1M", 2)) |
| 1509 | specified_table_size = TCE_TABLE_SIZE_1M; |
| 1510 | else if (!strncmp(p, "2M", 2)) |
| 1511 | specified_table_size = TCE_TABLE_SIZE_2M; |
| 1512 | else if (!strncmp(p, "4M", 2)) |
| 1513 | specified_table_size = TCE_TABLE_SIZE_4M; |
| 1514 | else if (!strncmp(p, "8M", 2)) |
| 1515 | specified_table_size = TCE_TABLE_SIZE_8M; |
| 1516 | |
| 1517 | len = strlen("translate_empty_slots"); |
| 1518 | if (!strncmp(p, "translate_empty_slots", len)) |
| 1519 | translate_empty_slots = 1; |
| 1520 | |
| 1521 | len = strlen("disable"); |
| 1522 | if (!strncmp(p, "disable", len)) { |
| 1523 | p += len; |
| 1524 | if (*p == '=') |
| 1525 | ++p; |
| 1526 | if (*p == '\0') |
| 1527 | break; |
Shuah Khan | 74bc491 | 2012-05-20 17:24:28 -0600 | [diff] [blame] | 1528 | ret = kstrtoul(p, 0, &val); |
| 1529 | if (ret) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1530 | break; |
| 1531 | |
Shuah Khan | 74bc491 | 2012-05-20 17:24:28 -0600 | [diff] [blame] | 1532 | bridge = val; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1533 | if (bridge < MAX_PHB_BUS_NUM) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1534 | printk(KERN_INFO "Calgary: disabling " |
Jon Mason | 70d666d | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1535 | "translation for PHB %#x\n", bridge); |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1536 | bus_info[bridge].translation_disabled = 1; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | p = strpbrk(p, ","); |
| 1541 | if (!p) |
| 1542 | break; |
| 1543 | |
| 1544 | p++; /* skip ',' */ |
| 1545 | } |
| 1546 | return 1; |
| 1547 | } |
| 1548 | __setup("calgary=", calgary_parse_options); |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1549 | |
| 1550 | static void __init calgary_fixup_one_tce_space(struct pci_dev *dev) |
| 1551 | { |
| 1552 | struct iommu_table *tbl; |
| 1553 | unsigned int npages; |
| 1554 | int i; |
| 1555 | |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 1556 | tbl = pci_iommu(dev->bus); |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1557 | |
| 1558 | for (i = 0; i < 4; i++) { |
| 1559 | struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i]; |
| 1560 | |
| 1561 | /* Don't give out TCEs that map MEM resources */ |
| 1562 | if (!(r->flags & IORESOURCE_MEM)) |
| 1563 | continue; |
| 1564 | |
| 1565 | /* 0-based? we reserve the whole 1st MB anyway */ |
| 1566 | if (!r->start) |
| 1567 | continue; |
| 1568 | |
| 1569 | /* cover the whole region */ |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1570 | npages = resource_size(r) >> PAGE_SHIFT; |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1571 | npages++; |
| 1572 | |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1573 | iommu_range_reserve(tbl, r->start, npages); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | static int __init calgary_fixup_tce_spaces(void) |
| 1578 | { |
| 1579 | struct pci_dev *dev = NULL; |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1580 | struct calgary_bus_info *info; |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1581 | |
| 1582 | if (no_iommu || swiotlb || !calgary_detected) |
| 1583 | return -ENODEV; |
| 1584 | |
Muli Ben-Yehuda | 12de257 | 2007-07-21 17:11:02 +0200 | [diff] [blame] | 1585 | printk(KERN_DEBUG "Calgary: fixing up tce spaces\n"); |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1586 | |
| 1587 | do { |
| 1588 | dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev); |
| 1589 | if (!dev) |
| 1590 | break; |
| 1591 | if (!is_cal_pci_dev(dev->device)) |
| 1592 | continue; |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1593 | |
| 1594 | info = &bus_info[dev->bus->number]; |
| 1595 | if (info->translation_disabled) |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1596 | continue; |
| 1597 | |
Muli Ben-Yehuda | bc3c605 | 2007-10-17 18:04:39 +0200 | [diff] [blame] | 1598 | if (!info->tce_space) |
Muli Ben-Yehuda | 07877cf | 2007-07-21 17:10:58 +0200 | [diff] [blame] | 1599 | continue; |
| 1600 | |
| 1601 | calgary_fixup_one_tce_space(dev); |
| 1602 | |
| 1603 | } while (1); |
| 1604 | |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * We need to be call after pcibios_assign_resources (fs_initcall level) |
| 1610 | * and before device_initcall. |
| 1611 | */ |
| 1612 | rootfs_initcall(calgary_fixup_tce_spaces); |
Konrad Rzeszutek Wilk | d2aa232f | 2010-08-26 13:58:02 -0400 | [diff] [blame] | 1613 | |
| 1614 | IOMMU_INIT_POST(detect_calgary); |