pci-calgary_64.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /*
  2. * Derived from arch/powerpc/kernel/iommu.c
  3. *
  4. * Copyright IBM Corporation, 2006-2007
  5. * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
  6. *
  7. * Author: Jon Mason <jdmason@kudzu.us>
  8. * Author: Muli Ben-Yehuda <muli@il.ibm.com>
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/string.h>
  30. #include <linux/crash_dump.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/bitops.h>
  33. #include <linux/pci_ids.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/iommu-helper.h>
  38. #include <asm/iommu.h>
  39. #include <asm/calgary.h>
  40. #include <asm/tce.h>
  41. #include <asm/pci-direct.h>
  42. #include <asm/system.h>
  43. #include <asm/dma.h>
  44. #include <asm/rio.h>
  45. #include <asm/bios_ebda.h>
  46. #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
  47. int use_calgary __read_mostly = 1;
  48. #else
  49. int use_calgary __read_mostly = 0;
  50. #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
  51. #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
  52. #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
  53. /* register offsets inside the host bridge space */
  54. #define CALGARY_CONFIG_REG 0x0108
  55. #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
  56. #define PHB_PLSSR_OFFSET 0x0120
  57. #define PHB_CONFIG_RW_OFFSET 0x0160
  58. #define PHB_IOBASE_BAR_LOW 0x0170
  59. #define PHB_IOBASE_BAR_HIGH 0x0180
  60. #define PHB_MEM_1_LOW 0x0190
  61. #define PHB_MEM_1_HIGH 0x01A0
  62. #define PHB_IO_ADDR_SIZE 0x01B0
  63. #define PHB_MEM_1_SIZE 0x01C0
  64. #define PHB_MEM_ST_OFFSET 0x01D0
  65. #define PHB_AER_OFFSET 0x0200
  66. #define PHB_CONFIG_0_HIGH 0x0220
  67. #define PHB_CONFIG_0_LOW 0x0230
  68. #define PHB_CONFIG_0_END 0x0240
  69. #define PHB_MEM_2_LOW 0x02B0
  70. #define PHB_MEM_2_HIGH 0x02C0
  71. #define PHB_MEM_2_SIZE_HIGH 0x02D0
  72. #define PHB_MEM_2_SIZE_LOW 0x02E0
  73. #define PHB_DOSHOLE_OFFSET 0x08E0
  74. /* CalIOC2 specific */
  75. #define PHB_SAVIOR_L2 0x0DB0
  76. #define PHB_PAGE_MIG_CTRL 0x0DA8
  77. #define PHB_PAGE_MIG_DEBUG 0x0DA0
  78. #define PHB_ROOT_COMPLEX_STATUS 0x0CB0
  79. /* PHB_CONFIG_RW */
  80. #define PHB_TCE_ENABLE 0x20000000
  81. #define PHB_SLOT_DISABLE 0x1C000000
  82. #define PHB_DAC_DISABLE 0x01000000
  83. #define PHB_MEM2_ENABLE 0x00400000
  84. #define PHB_MCSR_ENABLE 0x00100000
  85. /* TAR (Table Address Register) */
  86. #define TAR_SW_BITS 0x0000ffffffff800fUL
  87. #define TAR_VALID 0x0000000000000008UL
  88. /* CSR (Channel/DMA Status Register) */
  89. #define CSR_AGENT_MASK 0xffe0ffff
  90. /* CCR (Calgary Configuration Register) */
  91. #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
  92. /* PMCR/PMDR (Page Migration Control/Debug Registers */
  93. #define PMR_SOFTSTOP 0x80000000
  94. #define PMR_SOFTSTOPFAULT 0x40000000
  95. #define PMR_HARDSTOP 0x20000000
  96. #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
  97. #define MAX_NUM_CHASSIS 8 /* max number of chassis */
  98. /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
  99. #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
  100. #define PHBS_PER_CALGARY 4
  101. /* register offsets in Calgary's internal register space */
  102. static const unsigned long tar_offsets[] = {
  103. 0x0580 /* TAR0 */,
  104. 0x0588 /* TAR1 */,
  105. 0x0590 /* TAR2 */,
  106. 0x0598 /* TAR3 */
  107. };
  108. static const unsigned long split_queue_offsets[] = {
  109. 0x4870 /* SPLIT QUEUE 0 */,
  110. 0x5870 /* SPLIT QUEUE 1 */,
  111. 0x6870 /* SPLIT QUEUE 2 */,
  112. 0x7870 /* SPLIT QUEUE 3 */
  113. };
  114. static const unsigned long phb_offsets[] = {
  115. 0x8000 /* PHB0 */,
  116. 0x9000 /* PHB1 */,
  117. 0xA000 /* PHB2 */,
  118. 0xB000 /* PHB3 */
  119. };
  120. /* PHB debug registers */
  121. static const unsigned long phb_debug_offsets[] = {
  122. 0x4000 /* PHB 0 DEBUG */,
  123. 0x5000 /* PHB 1 DEBUG */,
  124. 0x6000 /* PHB 2 DEBUG */,
  125. 0x7000 /* PHB 3 DEBUG */
  126. };
  127. /*
  128. * STUFF register for each debug PHB,
  129. * byte 1 = start bus number, byte 2 = end bus number
  130. */
  131. #define PHB_DEBUG_STUFF_OFFSET 0x0020
  132. #define EMERGENCY_PAGES 32 /* = 128KB */
  133. unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
  134. static int translate_empty_slots __read_mostly = 0;
  135. static int calgary_detected __read_mostly = 0;
  136. static struct rio_table_hdr *rio_table_hdr __initdata;
  137. static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
  138. static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
  139. struct calgary_bus_info {
  140. void *tce_space;
  141. unsigned char translation_disabled;
  142. signed char phbid;
  143. void __iomem *bbar;
  144. };
  145. static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
  146. static void calgary_tce_cache_blast(struct iommu_table *tbl);
  147. static void calgary_dump_error_regs(struct iommu_table *tbl);
  148. static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
  149. static void calioc2_tce_cache_blast(struct iommu_table *tbl);
  150. static void calioc2_dump_error_regs(struct iommu_table *tbl);
  151. static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl);
  152. static void get_tce_space_from_tar(void);
  153. static struct cal_chipset_ops calgary_chip_ops = {
  154. .handle_quirks = calgary_handle_quirks,
  155. .tce_cache_blast = calgary_tce_cache_blast,
  156. .dump_error_regs = calgary_dump_error_regs
  157. };
  158. static struct cal_chipset_ops calioc2_chip_ops = {
  159. .handle_quirks = calioc2_handle_quirks,
  160. .tce_cache_blast = calioc2_tce_cache_blast,
  161. .dump_error_regs = calioc2_dump_error_regs
  162. };
  163. static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
  164. /* enable this to stress test the chip's TCE cache */
  165. #ifdef CONFIG_IOMMU_DEBUG
  166. static int debugging = 1;
  167. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  168. int expected, unsigned long start, unsigned long end)
  169. {
  170. unsigned long idx = start;
  171. BUG_ON(start >= end);
  172. while (idx < end) {
  173. if (!!test_bit(idx, bitmap) != expected)
  174. return idx;
  175. ++idx;
  176. }
  177. /* all bits have the expected value */
  178. return ~0UL;
  179. }
  180. #else /* debugging is disabled */
  181. static int debugging;
  182. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  183. int expected, unsigned long start, unsigned long end)
  184. {
  185. return ~0UL;
  186. }
  187. #endif /* CONFIG_IOMMU_DEBUG */
  188. static inline int translation_enabled(struct iommu_table *tbl)
  189. {
  190. /* only PHBs with translation enabled have an IOMMU table */
  191. return (tbl != NULL);
  192. }
  193. static void iommu_range_reserve(struct iommu_table *tbl,
  194. unsigned long start_addr, unsigned int npages)
  195. {
  196. unsigned long index;
  197. unsigned long end;
  198. unsigned long badbit;
  199. unsigned long flags;
  200. index = start_addr >> PAGE_SHIFT;
  201. /* bail out if we're asked to reserve a region we don't cover */
  202. if (index >= tbl->it_size)
  203. return;
  204. end = index + npages;
  205. if (end > tbl->it_size) /* don't go off the table */
  206. end = tbl->it_size;
  207. spin_lock_irqsave(&tbl->it_lock, flags);
  208. badbit = verify_bit_range(tbl->it_map, 0, index, end);
  209. if (badbit != ~0UL) {
  210. if (printk_ratelimit())
  211. printk(KERN_ERR "Calgary: entry already allocated at "
  212. "0x%lx tbl %p dma 0x%lx npages %u\n",
  213. badbit, tbl, start_addr, npages);
  214. }
  215. iommu_area_reserve(tbl->it_map, index, npages);
  216. spin_unlock_irqrestore(&tbl->it_lock, flags);
  217. }
  218. static unsigned long iommu_range_alloc(struct device *dev,
  219. struct iommu_table *tbl,
  220. unsigned int npages)
  221. {
  222. unsigned long flags;
  223. unsigned long offset;
  224. unsigned long boundary_size;
  225. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  226. PAGE_SIZE) >> PAGE_SHIFT;
  227. BUG_ON(npages == 0);
  228. spin_lock_irqsave(&tbl->it_lock, flags);
  229. offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint,
  230. npages, 0, boundary_size, 0);
  231. if (offset == ~0UL) {
  232. tbl->chip_ops->tce_cache_blast(tbl);
  233. offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0,
  234. npages, 0, boundary_size, 0);
  235. if (offset == ~0UL) {
  236. printk(KERN_WARNING "Calgary: IOMMU full.\n");
  237. spin_unlock_irqrestore(&tbl->it_lock, flags);
  238. if (panic_on_overflow)
  239. panic("Calgary: fix the allocator.\n");
  240. else
  241. return bad_dma_address;
  242. }
  243. }
  244. tbl->it_hint = offset + npages;
  245. BUG_ON(tbl->it_hint > tbl->it_size);
  246. spin_unlock_irqrestore(&tbl->it_lock, flags);
  247. return offset;
  248. }
  249. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  250. void *vaddr, unsigned int npages, int direction)
  251. {
  252. unsigned long entry;
  253. dma_addr_t ret = bad_dma_address;
  254. entry = iommu_range_alloc(dev, tbl, npages);
  255. if (unlikely(entry == bad_dma_address))
  256. goto error;
  257. /* set the return dma address */
  258. ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
  259. /* put the TCEs in the HW table */
  260. tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
  261. direction);
  262. return ret;
  263. error:
  264. printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
  265. "iommu %p\n", npages, tbl);
  266. return bad_dma_address;
  267. }
  268. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  269. unsigned int npages)
  270. {
  271. unsigned long entry;
  272. unsigned long badbit;
  273. unsigned long badend;
  274. unsigned long flags;
  275. /* were we called with bad_dma_address? */
  276. badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
  277. if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
  278. WARN(1, KERN_ERR "Calgary: driver tried unmapping bad DMA "
  279. "address 0x%Lx\n", dma_addr);
  280. return;
  281. }
  282. entry = dma_addr >> PAGE_SHIFT;
  283. BUG_ON(entry + npages > tbl->it_size);
  284. tce_free(tbl, entry, npages);
  285. spin_lock_irqsave(&tbl->it_lock, flags);
  286. badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
  287. if (badbit != ~0UL) {
  288. if (printk_ratelimit())
  289. printk(KERN_ERR "Calgary: bit is off at 0x%lx "
  290. "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
  291. badbit, tbl, dma_addr, entry, npages);
  292. }
  293. iommu_area_free(tbl->it_map, entry, npages);
  294. spin_unlock_irqrestore(&tbl->it_lock, flags);
  295. }
  296. static inline struct iommu_table *find_iommu_table(struct device *dev)
  297. {
  298. struct pci_dev *pdev;
  299. struct pci_bus *pbus;
  300. struct iommu_table *tbl;
  301. pdev = to_pci_dev(dev);
  302. pbus = pdev->bus;
  303. /* is the device behind a bridge? Look for the root bus */
  304. while (pbus->parent)
  305. pbus = pbus->parent;
  306. tbl = pci_iommu(pbus);
  307. BUG_ON(tbl && (tbl->it_busno != pbus->number));
  308. return tbl;
  309. }
  310. static void calgary_unmap_sg(struct device *dev,
  311. struct scatterlist *sglist, int nelems, int direction)
  312. {
  313. struct iommu_table *tbl = find_iommu_table(dev);
  314. struct scatterlist *s;
  315. int i;
  316. if (!translation_enabled(tbl))
  317. return;
  318. for_each_sg(sglist, s, nelems, i) {
  319. unsigned int npages;
  320. dma_addr_t dma = s->dma_address;
  321. unsigned int dmalen = s->dma_length;
  322. if (dmalen == 0)
  323. break;
  324. npages = iommu_num_pages(dma, dmalen, PAGE_SIZE);
  325. iommu_free(tbl, dma, npages);
  326. }
  327. }
  328. static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
  329. int nelems, int direction)
  330. {
  331. struct iommu_table *tbl = find_iommu_table(dev);
  332. struct scatterlist *s;
  333. unsigned long vaddr;
  334. unsigned int npages;
  335. unsigned long entry;
  336. int i;
  337. for_each_sg(sg, s, nelems, i) {
  338. BUG_ON(!sg_page(s));
  339. vaddr = (unsigned long) sg_virt(s);
  340. npages = iommu_num_pages(vaddr, s->length, PAGE_SIZE);
  341. entry = iommu_range_alloc(dev, tbl, npages);
  342. if (entry == bad_dma_address) {
  343. /* makes sure unmap knows to stop */
  344. s->dma_length = 0;
  345. goto error;
  346. }
  347. s->dma_address = (entry << PAGE_SHIFT) | s->offset;
  348. /* insert into HW table */
  349. tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
  350. direction);
  351. s->dma_length = s->length;
  352. }
  353. return nelems;
  354. error:
  355. calgary_unmap_sg(dev, sg, nelems, direction);
  356. for_each_sg(sg, s, nelems, i) {
  357. sg->dma_address = bad_dma_address;
  358. sg->dma_length = 0;
  359. }
  360. return 0;
  361. }
  362. static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
  363. size_t size, int direction)
  364. {
  365. void *vaddr = phys_to_virt(paddr);
  366. unsigned long uaddr;
  367. unsigned int npages;
  368. struct iommu_table *tbl = find_iommu_table(dev);
  369. uaddr = (unsigned long)vaddr;
  370. npages = iommu_num_pages(uaddr, size, PAGE_SIZE);
  371. return iommu_alloc(dev, tbl, vaddr, npages, direction);
  372. }
  373. static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
  374. size_t size, int direction)
  375. {
  376. struct iommu_table *tbl = find_iommu_table(dev);
  377. unsigned int npages;
  378. npages = iommu_num_pages(dma_handle, size, PAGE_SIZE);
  379. iommu_free(tbl, dma_handle, npages);
  380. }
  381. static void* calgary_alloc_coherent(struct device *dev, size_t size,
  382. dma_addr_t *dma_handle, gfp_t flag)
  383. {
  384. void *ret = NULL;
  385. dma_addr_t mapping;
  386. unsigned int npages, order;
  387. struct iommu_table *tbl = find_iommu_table(dev);
  388. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  389. npages = size >> PAGE_SHIFT;
  390. order = get_order(size);
  391. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  392. /* alloc enough pages (and possibly more) */
  393. ret = (void *)__get_free_pages(flag, order);
  394. if (!ret)
  395. goto error;
  396. memset(ret, 0, size);
  397. /* set up tces to cover the allocated range */
  398. mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
  399. if (mapping == bad_dma_address)
  400. goto free;
  401. *dma_handle = mapping;
  402. return ret;
  403. free:
  404. free_pages((unsigned long)ret, get_order(size));
  405. ret = NULL;
  406. error:
  407. return ret;
  408. }
  409. static void calgary_free_coherent(struct device *dev, size_t size,
  410. void *vaddr, dma_addr_t dma_handle)
  411. {
  412. unsigned int npages;
  413. struct iommu_table *tbl = find_iommu_table(dev);
  414. size = PAGE_ALIGN(size);
  415. npages = size >> PAGE_SHIFT;
  416. iommu_free(tbl, dma_handle, npages);
  417. free_pages((unsigned long)vaddr, get_order(size));
  418. }
  419. static struct dma_mapping_ops calgary_dma_ops = {
  420. .alloc_coherent = calgary_alloc_coherent,
  421. .free_coherent = calgary_free_coherent,
  422. .map_single = calgary_map_single,
  423. .unmap_single = calgary_unmap_single,
  424. .map_sg = calgary_map_sg,
  425. .unmap_sg = calgary_unmap_sg,
  426. };
  427. static inline void __iomem * busno_to_bbar(unsigned char num)
  428. {
  429. return bus_info[num].bbar;
  430. }
  431. static inline int busno_to_phbid(unsigned char num)
  432. {
  433. return bus_info[num].phbid;
  434. }
  435. static inline unsigned long split_queue_offset(unsigned char num)
  436. {
  437. size_t idx = busno_to_phbid(num);
  438. return split_queue_offsets[idx];
  439. }
  440. static inline unsigned long tar_offset(unsigned char num)
  441. {
  442. size_t idx = busno_to_phbid(num);
  443. return tar_offsets[idx];
  444. }
  445. static inline unsigned long phb_offset(unsigned char num)
  446. {
  447. size_t idx = busno_to_phbid(num);
  448. return phb_offsets[idx];
  449. }
  450. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  451. {
  452. unsigned long target = ((unsigned long)bar) | offset;
  453. return (void __iomem*)target;
  454. }
  455. static inline int is_calioc2(unsigned short device)
  456. {
  457. return (device == PCI_DEVICE_ID_IBM_CALIOC2);
  458. }
  459. static inline int is_calgary(unsigned short device)
  460. {
  461. return (device == PCI_DEVICE_ID_IBM_CALGARY);
  462. }
  463. static inline int is_cal_pci_dev(unsigned short device)
  464. {
  465. return (is_calgary(device) || is_calioc2(device));
  466. }
  467. static void calgary_tce_cache_blast(struct iommu_table *tbl)
  468. {
  469. u64 val;
  470. u32 aer;
  471. int i = 0;
  472. void __iomem *bbar = tbl->bbar;
  473. void __iomem *target;
  474. /* disable arbitration on the bus */
  475. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  476. aer = readl(target);
  477. writel(0, target);
  478. /* read plssr to ensure it got there */
  479. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  480. val = readl(target);
  481. /* poll split queues until all DMA activity is done */
  482. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  483. do {
  484. val = readq(target);
  485. i++;
  486. } while ((val & 0xff) != 0xff && i < 100);
  487. if (i == 100)
  488. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  489. "continuing anyway\n");
  490. /* invalidate TCE cache */
  491. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  492. writeq(tbl->tar_val, target);
  493. /* enable arbitration */
  494. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  495. writel(aer, target);
  496. (void)readl(target); /* flush */
  497. }
  498. static void calioc2_tce_cache_blast(struct iommu_table *tbl)
  499. {
  500. void __iomem *bbar = tbl->bbar;
  501. void __iomem *target;
  502. u64 val64;
  503. u32 val;
  504. int i = 0;
  505. int count = 1;
  506. unsigned char bus = tbl->it_busno;
  507. begin:
  508. printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
  509. "sequence - count %d\n", bus, count);
  510. /* 1. using the Page Migration Control reg set SoftStop */
  511. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  512. val = be32_to_cpu(readl(target));
  513. printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
  514. val |= PMR_SOFTSTOP;
  515. printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
  516. writel(cpu_to_be32(val), target);
  517. /* 2. poll split queues until all DMA activity is done */
  518. printk(KERN_DEBUG "2a. starting to poll split queues\n");
  519. target = calgary_reg(bbar, split_queue_offset(bus));
  520. do {
  521. val64 = readq(target);
  522. i++;
  523. } while ((val64 & 0xff) != 0xff && i < 100);
  524. if (i == 100)
  525. printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
  526. "continuing anyway\n");
  527. /* 3. poll Page Migration DEBUG for SoftStopFault */
  528. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  529. val = be32_to_cpu(readl(target));
  530. printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
  531. /* 4. if SoftStopFault - goto (1) */
  532. if (val & PMR_SOFTSTOPFAULT) {
  533. if (++count < 100)
  534. goto begin;
  535. else {
  536. printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
  537. "aborting TCE cache flush sequence!\n");
  538. return; /* pray for the best */
  539. }
  540. }
  541. /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
  542. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  543. printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
  544. val = be32_to_cpu(readl(target));
  545. printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
  546. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  547. val = be32_to_cpu(readl(target));
  548. printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
  549. /* 6. invalidate TCE cache */
  550. printk(KERN_DEBUG "6. invalidating TCE cache\n");
  551. target = calgary_reg(bbar, tar_offset(bus));
  552. writeq(tbl->tar_val, target);
  553. /* 7. Re-read PMCR */
  554. printk(KERN_DEBUG "7a. Re-reading PMCR\n");
  555. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  556. val = be32_to_cpu(readl(target));
  557. printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
  558. /* 8. Remove HardStop */
  559. printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
  560. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  561. val = 0;
  562. printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
  563. writel(cpu_to_be32(val), target);
  564. val = be32_to_cpu(readl(target));
  565. printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
  566. }
  567. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  568. u64 limit)
  569. {
  570. unsigned int numpages;
  571. limit = limit | 0xfffff;
  572. limit++;
  573. numpages = ((limit - start) >> PAGE_SHIFT);
  574. iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
  575. }
  576. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  577. {
  578. void __iomem *target;
  579. u64 low, high, sizelow;
  580. u64 start, limit;
  581. struct iommu_table *tbl = pci_iommu(dev->bus);
  582. unsigned char busnum = dev->bus->number;
  583. void __iomem *bbar = tbl->bbar;
  584. /* peripheral MEM_1 region */
  585. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  586. low = be32_to_cpu(readl(target));
  587. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  588. high = be32_to_cpu(readl(target));
  589. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  590. sizelow = be32_to_cpu(readl(target));
  591. start = (high << 32) | low;
  592. limit = sizelow;
  593. calgary_reserve_mem_region(dev, start, limit);
  594. }
  595. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  596. {
  597. void __iomem *target;
  598. u32 val32;
  599. u64 low, high, sizelow, sizehigh;
  600. u64 start, limit;
  601. struct iommu_table *tbl = pci_iommu(dev->bus);
  602. unsigned char busnum = dev->bus->number;
  603. void __iomem *bbar = tbl->bbar;
  604. /* is it enabled? */
  605. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  606. val32 = be32_to_cpu(readl(target));
  607. if (!(val32 & PHB_MEM2_ENABLE))
  608. return;
  609. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  610. low = be32_to_cpu(readl(target));
  611. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  612. high = be32_to_cpu(readl(target));
  613. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  614. sizelow = be32_to_cpu(readl(target));
  615. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  616. sizehigh = be32_to_cpu(readl(target));
  617. start = (high << 32) | low;
  618. limit = (sizehigh << 32) | sizelow;
  619. calgary_reserve_mem_region(dev, start, limit);
  620. }
  621. /*
  622. * some regions of the IO address space do not get translated, so we
  623. * must not give devices IO addresses in those regions. The regions
  624. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  625. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  626. * later.
  627. */
  628. static void __init calgary_reserve_regions(struct pci_dev *dev)
  629. {
  630. unsigned int npages;
  631. u64 start;
  632. struct iommu_table *tbl = pci_iommu(dev->bus);
  633. /* reserve EMERGENCY_PAGES from bad_dma_address and up */
  634. iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
  635. /* avoid the BIOS/VGA first 640KB-1MB region */
  636. /* for CalIOC2 - avoid the entire first MB */
  637. if (is_calgary(dev->device)) {
  638. start = (640 * 1024);
  639. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  640. } else { /* calioc2 */
  641. start = 0;
  642. npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
  643. }
  644. iommu_range_reserve(tbl, start, npages);
  645. /* reserve the two PCI peripheral memory regions in IO space */
  646. calgary_reserve_peripheral_mem_1(dev);
  647. calgary_reserve_peripheral_mem_2(dev);
  648. }
  649. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  650. {
  651. u64 val64;
  652. u64 table_phys;
  653. void __iomem *target;
  654. int ret;
  655. struct iommu_table *tbl;
  656. /* build TCE tables for each PHB */
  657. ret = build_tce_table(dev, bbar);
  658. if (ret)
  659. return ret;
  660. tbl = pci_iommu(dev->bus);
  661. tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
  662. if (is_kdump_kernel())
  663. calgary_init_bitmap_from_tce_table(tbl);
  664. else
  665. tce_free(tbl, 0, tbl->it_size);
  666. if (is_calgary(dev->device))
  667. tbl->chip_ops = &calgary_chip_ops;
  668. else if (is_calioc2(dev->device))
  669. tbl->chip_ops = &calioc2_chip_ops;
  670. else
  671. BUG();
  672. calgary_reserve_regions(dev);
  673. /* set TARs for each PHB */
  674. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  675. val64 = be64_to_cpu(readq(target));
  676. /* zero out all TAR bits under sw control */
  677. val64 &= ~TAR_SW_BITS;
  678. table_phys = (u64)__pa(tbl->it_base);
  679. val64 |= table_phys;
  680. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  681. val64 |= (u64) specified_table_size;
  682. tbl->tar_val = cpu_to_be64(val64);
  683. writeq(tbl->tar_val, target);
  684. readq(target); /* flush */
  685. return 0;
  686. }
  687. static void __init calgary_free_bus(struct pci_dev *dev)
  688. {
  689. u64 val64;
  690. struct iommu_table *tbl = pci_iommu(dev->bus);
  691. void __iomem *target;
  692. unsigned int bitmapsz;
  693. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  694. val64 = be64_to_cpu(readq(target));
  695. val64 &= ~TAR_SW_BITS;
  696. writeq(cpu_to_be64(val64), target);
  697. readq(target); /* flush */
  698. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  699. free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
  700. tbl->it_map = NULL;
  701. kfree(tbl);
  702. set_pci_iommu(dev->bus, NULL);
  703. /* Can't free bootmem allocated memory after system is up :-( */
  704. bus_info[dev->bus->number].tce_space = NULL;
  705. }
  706. static void calgary_dump_error_regs(struct iommu_table *tbl)
  707. {
  708. void __iomem *bbar = tbl->bbar;
  709. void __iomem *target;
  710. u32 csr, plssr;
  711. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  712. csr = be32_to_cpu(readl(target));
  713. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  714. plssr = be32_to_cpu(readl(target));
  715. /* If no error, the agent ID in the CSR is not valid */
  716. printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
  717. "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
  718. }
  719. static void calioc2_dump_error_regs(struct iommu_table *tbl)
  720. {
  721. void __iomem *bbar = tbl->bbar;
  722. u32 csr, csmr, plssr, mck, rcstat;
  723. void __iomem *target;
  724. unsigned long phboff = phb_offset(tbl->it_busno);
  725. unsigned long erroff;
  726. u32 errregs[7];
  727. int i;
  728. /* dump CSR */
  729. target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
  730. csr = be32_to_cpu(readl(target));
  731. /* dump PLSSR */
  732. target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
  733. plssr = be32_to_cpu(readl(target));
  734. /* dump CSMR */
  735. target = calgary_reg(bbar, phboff | 0x290);
  736. csmr = be32_to_cpu(readl(target));
  737. /* dump mck */
  738. target = calgary_reg(bbar, phboff | 0x800);
  739. mck = be32_to_cpu(readl(target));
  740. printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
  741. tbl->it_busno);
  742. printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
  743. csr, plssr, csmr, mck);
  744. /* dump rest of error regs */
  745. printk(KERN_EMERG "Calgary: ");
  746. for (i = 0; i < ARRAY_SIZE(errregs); i++) {
  747. /* err regs are at 0x810 - 0x870 */
  748. erroff = (0x810 + (i * 0x10));
  749. target = calgary_reg(bbar, phboff | erroff);
  750. errregs[i] = be32_to_cpu(readl(target));
  751. printk("0x%08x@0x%lx ", errregs[i], erroff);
  752. }
  753. printk("\n");
  754. /* root complex status */
  755. target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
  756. rcstat = be32_to_cpu(readl(target));
  757. printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
  758. PHB_ROOT_COMPLEX_STATUS);
  759. }
  760. static void calgary_watchdog(unsigned long data)
  761. {
  762. struct pci_dev *dev = (struct pci_dev *)data;
  763. struct iommu_table *tbl = pci_iommu(dev->bus);
  764. void __iomem *bbar = tbl->bbar;
  765. u32 val32;
  766. void __iomem *target;
  767. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  768. val32 = be32_to_cpu(readl(target));
  769. /* If no error, the agent ID in the CSR is not valid */
  770. if (val32 & CSR_AGENT_MASK) {
  771. tbl->chip_ops->dump_error_regs(tbl);
  772. /* reset error */
  773. writel(0, target);
  774. /* Disable bus that caused the error */
  775. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  776. PHB_CONFIG_RW_OFFSET);
  777. val32 = be32_to_cpu(readl(target));
  778. val32 |= PHB_SLOT_DISABLE;
  779. writel(cpu_to_be32(val32), target);
  780. readl(target); /* flush */
  781. } else {
  782. /* Reset the timer */
  783. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  784. }
  785. }
  786. static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
  787. unsigned char busnum, unsigned long timeout)
  788. {
  789. u64 val64;
  790. void __iomem *target;
  791. unsigned int phb_shift = ~0; /* silence gcc */
  792. u64 mask;
  793. switch (busno_to_phbid(busnum)) {
  794. case 0: phb_shift = (63 - 19);
  795. break;
  796. case 1: phb_shift = (63 - 23);
  797. break;
  798. case 2: phb_shift = (63 - 27);
  799. break;
  800. case 3: phb_shift = (63 - 35);
  801. break;
  802. default:
  803. BUG_ON(busno_to_phbid(busnum));
  804. }
  805. target = calgary_reg(bbar, CALGARY_CONFIG_REG);
  806. val64 = be64_to_cpu(readq(target));
  807. /* zero out this PHB's timer bits */
  808. mask = ~(0xFUL << phb_shift);
  809. val64 &= mask;
  810. val64 |= (timeout << phb_shift);
  811. writeq(cpu_to_be64(val64), target);
  812. readq(target); /* flush */
  813. }
  814. static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  815. {
  816. unsigned char busnum = dev->bus->number;
  817. void __iomem *bbar = tbl->bbar;
  818. void __iomem *target;
  819. u32 val;
  820. /*
  821. * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
  822. */
  823. target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
  824. val = cpu_to_be32(readl(target));
  825. val |= 0x00800000;
  826. writel(cpu_to_be32(val), target);
  827. }
  828. static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  829. {
  830. unsigned char busnum = dev->bus->number;
  831. /*
  832. * Give split completion a longer timeout on bus 1 for aic94xx
  833. * http://bugzilla.kernel.org/show_bug.cgi?id=7180
  834. */
  835. if (is_calgary(dev->device) && (busnum == 1))
  836. calgary_set_split_completion_timeout(tbl->bbar, busnum,
  837. CCR_2SEC_TIMEOUT);
  838. }
  839. static void __init calgary_enable_translation(struct pci_dev *dev)
  840. {
  841. u32 val32;
  842. unsigned char busnum;
  843. void __iomem *target;
  844. void __iomem *bbar;
  845. struct iommu_table *tbl;
  846. busnum = dev->bus->number;
  847. tbl = pci_iommu(dev->bus);
  848. bbar = tbl->bbar;
  849. /* enable TCE in PHB Config Register */
  850. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  851. val32 = be32_to_cpu(readl(target));
  852. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  853. printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
  854. (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
  855. "Calgary" : "CalIOC2", busnum);
  856. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  857. "bus.\n");
  858. writel(cpu_to_be32(val32), target);
  859. readl(target); /* flush */
  860. init_timer(&tbl->watchdog_timer);
  861. tbl->watchdog_timer.function = &calgary_watchdog;
  862. tbl->watchdog_timer.data = (unsigned long)dev;
  863. mod_timer(&tbl->watchdog_timer, jiffies);
  864. }
  865. static void __init calgary_disable_translation(struct pci_dev *dev)
  866. {
  867. u32 val32;
  868. unsigned char busnum;
  869. void __iomem *target;
  870. void __iomem *bbar;
  871. struct iommu_table *tbl;
  872. busnum = dev->bus->number;
  873. tbl = pci_iommu(dev->bus);
  874. bbar = tbl->bbar;
  875. /* disable TCE in PHB Config Register */
  876. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  877. val32 = be32_to_cpu(readl(target));
  878. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  879. printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
  880. writel(cpu_to_be32(val32), target);
  881. readl(target); /* flush */
  882. del_timer_sync(&tbl->watchdog_timer);
  883. }
  884. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  885. {
  886. pci_dev_get(dev);
  887. set_pci_iommu(dev->bus, NULL);
  888. /* is the device behind a bridge? */
  889. if (dev->bus->parent)
  890. dev->bus->parent->self = dev;
  891. else
  892. dev->bus->self = dev;
  893. }
  894. static int __init calgary_init_one(struct pci_dev *dev)
  895. {
  896. void __iomem *bbar;
  897. struct iommu_table *tbl;
  898. int ret;
  899. BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
  900. bbar = busno_to_bbar(dev->bus->number);
  901. ret = calgary_setup_tar(dev, bbar);
  902. if (ret)
  903. goto done;
  904. pci_dev_get(dev);
  905. if (dev->bus->parent) {
  906. if (dev->bus->parent->self)
  907. printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
  908. "bus->parent->self!\n", dev);
  909. dev->bus->parent->self = dev;
  910. } else
  911. dev->bus->self = dev;
  912. tbl = pci_iommu(dev->bus);
  913. tbl->chip_ops->handle_quirks(tbl, dev);
  914. calgary_enable_translation(dev);
  915. return 0;
  916. done:
  917. return ret;
  918. }
  919. static int __init calgary_locate_bbars(void)
  920. {
  921. int ret;
  922. int rioidx, phb, bus;
  923. void __iomem *bbar;
  924. void __iomem *target;
  925. unsigned long offset;
  926. u8 start_bus, end_bus;
  927. u32 val;
  928. ret = -ENODATA;
  929. for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
  930. struct rio_detail *rio = rio_devs[rioidx];
  931. if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
  932. continue;
  933. /* map entire 1MB of Calgary config space */
  934. bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
  935. if (!bbar)
  936. goto error;
  937. for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
  938. offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
  939. target = calgary_reg(bbar, offset);
  940. val = be32_to_cpu(readl(target));
  941. start_bus = (u8)((val & 0x00FF0000) >> 16);
  942. end_bus = (u8)((val & 0x0000FF00) >> 8);
  943. if (end_bus) {
  944. for (bus = start_bus; bus <= end_bus; bus++) {
  945. bus_info[bus].bbar = bbar;
  946. bus_info[bus].phbid = phb;
  947. }
  948. } else {
  949. bus_info[start_bus].bbar = bbar;
  950. bus_info[start_bus].phbid = phb;
  951. }
  952. }
  953. }
  954. return 0;
  955. error:
  956. /* scan bus_info and iounmap any bbars we previously ioremap'd */
  957. for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
  958. if (bus_info[bus].bbar)
  959. iounmap(bus_info[bus].bbar);
  960. return ret;
  961. }
  962. static int __init calgary_init(void)
  963. {
  964. int ret;
  965. struct pci_dev *dev = NULL;
  966. struct calgary_bus_info *info;
  967. ret = calgary_locate_bbars();
  968. if (ret)
  969. return ret;
  970. /* Purely for kdump kernel case */
  971. if (is_kdump_kernel())
  972. get_tce_space_from_tar();
  973. do {
  974. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  975. if (!dev)
  976. break;
  977. if (!is_cal_pci_dev(dev->device))
  978. continue;
  979. info = &bus_info[dev->bus->number];
  980. if (info->translation_disabled) {
  981. calgary_init_one_nontraslated(dev);
  982. continue;
  983. }
  984. if (!info->tce_space && !translate_empty_slots)
  985. continue;
  986. ret = calgary_init_one(dev);
  987. if (ret)
  988. goto error;
  989. } while (1);
  990. dev = NULL;
  991. for_each_pci_dev(dev) {
  992. struct iommu_table *tbl;
  993. tbl = find_iommu_table(&dev->dev);
  994. if (translation_enabled(tbl))
  995. dev->dev.archdata.dma_ops = &calgary_dma_ops;
  996. }
  997. return ret;
  998. error:
  999. do {
  1000. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1001. if (!dev)
  1002. break;
  1003. if (!is_cal_pci_dev(dev->device))
  1004. continue;
  1005. info = &bus_info[dev->bus->number];
  1006. if (info->translation_disabled) {
  1007. pci_dev_put(dev);
  1008. continue;
  1009. }
  1010. if (!info->tce_space && !translate_empty_slots)
  1011. continue;
  1012. calgary_disable_translation(dev);
  1013. calgary_free_bus(dev);
  1014. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  1015. dev->dev.archdata.dma_ops = NULL;
  1016. } while (1);
  1017. return ret;
  1018. }
  1019. static inline int __init determine_tce_table_size(u64 ram)
  1020. {
  1021. int ret;
  1022. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  1023. return specified_table_size;
  1024. /*
  1025. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  1026. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  1027. * larger table size has twice as many entries, so shift the
  1028. * max ram address by 13 to divide by 8K and then look at the
  1029. * order of the result to choose between 0-7.
  1030. */
  1031. ret = get_order(ram >> 13);
  1032. if (ret > TCE_TABLE_SIZE_8M)
  1033. ret = TCE_TABLE_SIZE_8M;
  1034. return ret;
  1035. }
  1036. static int __init build_detail_arrays(void)
  1037. {
  1038. unsigned long ptr;
  1039. unsigned numnodes, i;
  1040. int scal_detail_size, rio_detail_size;
  1041. numnodes = rio_table_hdr->num_scal_dev;
  1042. if (numnodes > MAX_NUMNODES){
  1043. printk(KERN_WARNING
  1044. "Calgary: MAX_NUMNODES too low! Defined as %d, "
  1045. "but system has %d nodes.\n",
  1046. MAX_NUMNODES, numnodes);
  1047. return -ENODEV;
  1048. }
  1049. switch (rio_table_hdr->version){
  1050. case 2:
  1051. scal_detail_size = 11;
  1052. rio_detail_size = 13;
  1053. break;
  1054. case 3:
  1055. scal_detail_size = 12;
  1056. rio_detail_size = 15;
  1057. break;
  1058. default:
  1059. printk(KERN_WARNING
  1060. "Calgary: Invalid Rio Grande Table Version: %d\n",
  1061. rio_table_hdr->version);
  1062. return -EPROTO;
  1063. }
  1064. ptr = ((unsigned long)rio_table_hdr) + 3;
  1065. for (i = 0; i < numnodes; i++, ptr += scal_detail_size)
  1066. scal_devs[i] = (struct scal_detail *)ptr;
  1067. for (i = 0; i < rio_table_hdr->num_rio_dev;
  1068. i++, ptr += rio_detail_size)
  1069. rio_devs[i] = (struct rio_detail *)ptr;
  1070. return 0;
  1071. }
  1072. static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
  1073. {
  1074. int dev;
  1075. u32 val;
  1076. if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
  1077. /*
  1078. * FIXME: properly scan for devices accross the
  1079. * PCI-to-PCI bridge on every CalIOC2 port.
  1080. */
  1081. return 1;
  1082. }
  1083. for (dev = 1; dev < 8; dev++) {
  1084. val = read_pci_config(bus, dev, 0, 0);
  1085. if (val != 0xffffffff)
  1086. break;
  1087. }
  1088. return (val != 0xffffffff);
  1089. }
  1090. /*
  1091. * calgary_init_bitmap_from_tce_table():
  1092. * Funtion for kdump case. In the second/kdump kernel initialize
  1093. * the bitmap based on the tce table entries obtained from first kernel
  1094. */
  1095. static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl)
  1096. {
  1097. u64 *tp;
  1098. unsigned int index;
  1099. tp = ((u64 *)tbl->it_base);
  1100. for (index = 0 ; index < tbl->it_size; index++) {
  1101. if (*tp != 0x0)
  1102. set_bit(index, tbl->it_map);
  1103. tp++;
  1104. }
  1105. }
  1106. /*
  1107. * get_tce_space_from_tar():
  1108. * Function for kdump case. Get the tce tables from first kernel
  1109. * by reading the contents of the base adress register of calgary iommu
  1110. */
  1111. static void __init get_tce_space_from_tar(void)
  1112. {
  1113. int bus;
  1114. void __iomem *target;
  1115. unsigned long tce_space;
  1116. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  1117. struct calgary_bus_info *info = &bus_info[bus];
  1118. unsigned short pci_device;
  1119. u32 val;
  1120. val = read_pci_config(bus, 0, 0, 0);
  1121. pci_device = (val & 0xFFFF0000) >> 16;
  1122. if (!is_cal_pci_dev(pci_device))
  1123. continue;
  1124. if (info->translation_disabled)
  1125. continue;
  1126. if (calgary_bus_has_devices(bus, pci_device) ||
  1127. translate_empty_slots) {
  1128. target = calgary_reg(bus_info[bus].bbar,
  1129. tar_offset(bus));
  1130. tce_space = be64_to_cpu(readq(target));
  1131. tce_space = tce_space & TAR_SW_BITS;
  1132. tce_space = tce_space & (~specified_table_size);
  1133. info->tce_space = (u64 *)__va(tce_space);
  1134. }
  1135. }
  1136. return;
  1137. }
  1138. void __init detect_calgary(void)
  1139. {
  1140. int bus;
  1141. void *tbl;
  1142. int calgary_found = 0;
  1143. unsigned long ptr;
  1144. unsigned int offset, prev_offset;
  1145. int ret;
  1146. /*
  1147. * if the user specified iommu=off or iommu=soft or we found
  1148. * another HW IOMMU already, bail out.
  1149. */
  1150. if (swiotlb || no_iommu || iommu_detected)
  1151. return;
  1152. if (!use_calgary)
  1153. return;
  1154. if (!early_pci_allowed())
  1155. return;
  1156. printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
  1157. ptr = (unsigned long)phys_to_virt(get_bios_ebda());
  1158. rio_table_hdr = NULL;
  1159. prev_offset = 0;
  1160. offset = 0x180;
  1161. /*
  1162. * The next offset is stored in the 1st word.
  1163. * Only parse up until the offset increases:
  1164. */
  1165. while (offset > prev_offset) {
  1166. /* The block id is stored in the 2nd word */
  1167. if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
  1168. /* set the pointer past the offset & block id */
  1169. rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
  1170. break;
  1171. }
  1172. prev_offset = offset;
  1173. offset = *((unsigned short *)(ptr + offset));
  1174. }
  1175. if (!rio_table_hdr) {
  1176. printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
  1177. "in EBDA - bailing!\n");
  1178. return;
  1179. }
  1180. ret = build_detail_arrays();
  1181. if (ret) {
  1182. printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
  1183. return;
  1184. }
  1185. specified_table_size = determine_tce_table_size((is_kdump_kernel() ?
  1186. saved_max_pfn : max_pfn) * PAGE_SIZE);
  1187. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  1188. struct calgary_bus_info *info = &bus_info[bus];
  1189. unsigned short pci_device;
  1190. u32 val;
  1191. val = read_pci_config(bus, 0, 0, 0);
  1192. pci_device = (val & 0xFFFF0000) >> 16;
  1193. if (!is_cal_pci_dev(pci_device))
  1194. continue;
  1195. if (info->translation_disabled)
  1196. continue;
  1197. if (calgary_bus_has_devices(bus, pci_device) ||
  1198. translate_empty_slots) {
  1199. /*
  1200. * If it is kdump kernel, find and use tce tables
  1201. * from first kernel, else allocate tce tables here
  1202. */
  1203. if (!is_kdump_kernel()) {
  1204. tbl = alloc_tce_table();
  1205. if (!tbl)
  1206. goto cleanup;
  1207. info->tce_space = tbl;
  1208. }
  1209. calgary_found = 1;
  1210. }
  1211. }
  1212. printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
  1213. calgary_found ? "found" : "not found");
  1214. if (calgary_found) {
  1215. iommu_detected = 1;
  1216. calgary_detected = 1;
  1217. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
  1218. printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
  1219. "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
  1220. debugging ? "enabled" : "disabled");
  1221. /* swiotlb for devices that aren't behind the Calgary. */
  1222. if (max_pfn > MAX_DMA32_PFN)
  1223. swiotlb = 1;
  1224. }
  1225. return;
  1226. cleanup:
  1227. for (--bus; bus >= 0; --bus) {
  1228. struct calgary_bus_info *info = &bus_info[bus];
  1229. if (info->tce_space)
  1230. free_tce_table(info->tce_space);
  1231. }
  1232. }
  1233. int __init calgary_iommu_init(void)
  1234. {
  1235. int ret;
  1236. if (no_iommu || (swiotlb && !calgary_detected))
  1237. return -ENODEV;
  1238. if (!calgary_detected)
  1239. return -ENODEV;
  1240. /* ok, we're trying to use Calgary - let's roll */
  1241. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  1242. ret = calgary_init();
  1243. if (ret) {
  1244. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  1245. "falling back to no_iommu\n", ret);
  1246. return ret;
  1247. }
  1248. force_iommu = 1;
  1249. bad_dma_address = 0x0;
  1250. /* dma_ops is set to swiotlb or nommu */
  1251. if (!dma_ops)
  1252. dma_ops = &nommu_dma_ops;
  1253. return 0;
  1254. }
  1255. static int __init calgary_parse_options(char *p)
  1256. {
  1257. unsigned int bridge;
  1258. size_t len;
  1259. char* endp;
  1260. while (*p) {
  1261. if (!strncmp(p, "64k", 3))
  1262. specified_table_size = TCE_TABLE_SIZE_64K;
  1263. else if (!strncmp(p, "128k", 4))
  1264. specified_table_size = TCE_TABLE_SIZE_128K;
  1265. else if (!strncmp(p, "256k", 4))
  1266. specified_table_size = TCE_TABLE_SIZE_256K;
  1267. else if (!strncmp(p, "512k", 4))
  1268. specified_table_size = TCE_TABLE_SIZE_512K;
  1269. else if (!strncmp(p, "1M", 2))
  1270. specified_table_size = TCE_TABLE_SIZE_1M;
  1271. else if (!strncmp(p, "2M", 2))
  1272. specified_table_size = TCE_TABLE_SIZE_2M;
  1273. else if (!strncmp(p, "4M", 2))
  1274. specified_table_size = TCE_TABLE_SIZE_4M;
  1275. else if (!strncmp(p, "8M", 2))
  1276. specified_table_size = TCE_TABLE_SIZE_8M;
  1277. len = strlen("translate_empty_slots");
  1278. if (!strncmp(p, "translate_empty_slots", len))
  1279. translate_empty_slots = 1;
  1280. len = strlen("disable");
  1281. if (!strncmp(p, "disable", len)) {
  1282. p += len;
  1283. if (*p == '=')
  1284. ++p;
  1285. if (*p == '\0')
  1286. break;
  1287. bridge = simple_strtol(p, &endp, 0);
  1288. if (p == endp)
  1289. break;
  1290. if (bridge < MAX_PHB_BUS_NUM) {
  1291. printk(KERN_INFO "Calgary: disabling "
  1292. "translation for PHB %#x\n", bridge);
  1293. bus_info[bridge].translation_disabled = 1;
  1294. }
  1295. }
  1296. p = strpbrk(p, ",");
  1297. if (!p)
  1298. break;
  1299. p++; /* skip ',' */
  1300. }
  1301. return 1;
  1302. }
  1303. __setup("calgary=", calgary_parse_options);
  1304. static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
  1305. {
  1306. struct iommu_table *tbl;
  1307. unsigned int npages;
  1308. int i;
  1309. tbl = pci_iommu(dev->bus);
  1310. for (i = 0; i < 4; i++) {
  1311. struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
  1312. /* Don't give out TCEs that map MEM resources */
  1313. if (!(r->flags & IORESOURCE_MEM))
  1314. continue;
  1315. /* 0-based? we reserve the whole 1st MB anyway */
  1316. if (!r->start)
  1317. continue;
  1318. /* cover the whole region */
  1319. npages = (r->end - r->start) >> PAGE_SHIFT;
  1320. npages++;
  1321. iommu_range_reserve(tbl, r->start, npages);
  1322. }
  1323. }
  1324. static int __init calgary_fixup_tce_spaces(void)
  1325. {
  1326. struct pci_dev *dev = NULL;
  1327. struct calgary_bus_info *info;
  1328. if (no_iommu || swiotlb || !calgary_detected)
  1329. return -ENODEV;
  1330. printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
  1331. do {
  1332. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1333. if (!dev)
  1334. break;
  1335. if (!is_cal_pci_dev(dev->device))
  1336. continue;
  1337. info = &bus_info[dev->bus->number];
  1338. if (info->translation_disabled)
  1339. continue;
  1340. if (!info->tce_space)
  1341. continue;
  1342. calgary_fixup_one_tce_space(dev);
  1343. } while (1);
  1344. return 0;
  1345. }
  1346. /*
  1347. * We need to be call after pcibios_assign_resources (fs_initcall level)
  1348. * and before device_initcall.
  1349. */
  1350. rootfs_initcall(calgary_fixup_tce_spaces);