pci-calgary_64.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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_page(struct device *dev, struct page *page,
  363. unsigned long offset, size_t size,
  364. enum dma_data_direction dir,
  365. struct dma_attrs *attrs)
  366. {
  367. void *vaddr = page_address(page) + offset;
  368. unsigned long uaddr;
  369. unsigned int npages;
  370. struct iommu_table *tbl = find_iommu_table(dev);
  371. uaddr = (unsigned long)vaddr;
  372. npages = iommu_num_pages(uaddr, size, PAGE_SIZE);
  373. return iommu_alloc(dev, tbl, vaddr, npages, dir);
  374. }
  375. static void calgary_unmap_page(struct device *dev, dma_addr_t dma_addr,
  376. size_t size, enum dma_data_direction dir,
  377. struct dma_attrs *attrs)
  378. {
  379. struct iommu_table *tbl = find_iommu_table(dev);
  380. unsigned int npages;
  381. npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  382. iommu_free(tbl, dma_addr, npages);
  383. }
  384. static void* calgary_alloc_coherent(struct device *dev, size_t size,
  385. dma_addr_t *dma_handle, gfp_t flag)
  386. {
  387. void *ret = NULL;
  388. dma_addr_t mapping;
  389. unsigned int npages, order;
  390. struct iommu_table *tbl = find_iommu_table(dev);
  391. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  392. npages = size >> PAGE_SHIFT;
  393. order = get_order(size);
  394. flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  395. /* alloc enough pages (and possibly more) */
  396. ret = (void *)__get_free_pages(flag, order);
  397. if (!ret)
  398. goto error;
  399. memset(ret, 0, size);
  400. /* set up tces to cover the allocated range */
  401. mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
  402. if (mapping == bad_dma_address)
  403. goto free;
  404. *dma_handle = mapping;
  405. return ret;
  406. free:
  407. free_pages((unsigned long)ret, get_order(size));
  408. ret = NULL;
  409. error:
  410. return ret;
  411. }
  412. static void calgary_free_coherent(struct device *dev, size_t size,
  413. void *vaddr, dma_addr_t dma_handle)
  414. {
  415. unsigned int npages;
  416. struct iommu_table *tbl = find_iommu_table(dev);
  417. size = PAGE_ALIGN(size);
  418. npages = size >> PAGE_SHIFT;
  419. iommu_free(tbl, dma_handle, npages);
  420. free_pages((unsigned long)vaddr, get_order(size));
  421. }
  422. static struct dma_mapping_ops calgary_dma_ops = {
  423. .alloc_coherent = calgary_alloc_coherent,
  424. .free_coherent = calgary_free_coherent,
  425. .map_sg = calgary_map_sg,
  426. .unmap_sg = calgary_unmap_sg,
  427. .map_page = calgary_map_page,
  428. .unmap_page = calgary_unmap_page,
  429. };
  430. static inline void __iomem * busno_to_bbar(unsigned char num)
  431. {
  432. return bus_info[num].bbar;
  433. }
  434. static inline int busno_to_phbid(unsigned char num)
  435. {
  436. return bus_info[num].phbid;
  437. }
  438. static inline unsigned long split_queue_offset(unsigned char num)
  439. {
  440. size_t idx = busno_to_phbid(num);
  441. return split_queue_offsets[idx];
  442. }
  443. static inline unsigned long tar_offset(unsigned char num)
  444. {
  445. size_t idx = busno_to_phbid(num);
  446. return tar_offsets[idx];
  447. }
  448. static inline unsigned long phb_offset(unsigned char num)
  449. {
  450. size_t idx = busno_to_phbid(num);
  451. return phb_offsets[idx];
  452. }
  453. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  454. {
  455. unsigned long target = ((unsigned long)bar) | offset;
  456. return (void __iomem*)target;
  457. }
  458. static inline int is_calioc2(unsigned short device)
  459. {
  460. return (device == PCI_DEVICE_ID_IBM_CALIOC2);
  461. }
  462. static inline int is_calgary(unsigned short device)
  463. {
  464. return (device == PCI_DEVICE_ID_IBM_CALGARY);
  465. }
  466. static inline int is_cal_pci_dev(unsigned short device)
  467. {
  468. return (is_calgary(device) || is_calioc2(device));
  469. }
  470. static void calgary_tce_cache_blast(struct iommu_table *tbl)
  471. {
  472. u64 val;
  473. u32 aer;
  474. int i = 0;
  475. void __iomem *bbar = tbl->bbar;
  476. void __iomem *target;
  477. /* disable arbitration on the bus */
  478. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  479. aer = readl(target);
  480. writel(0, target);
  481. /* read plssr to ensure it got there */
  482. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  483. val = readl(target);
  484. /* poll split queues until all DMA activity is done */
  485. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  486. do {
  487. val = readq(target);
  488. i++;
  489. } while ((val & 0xff) != 0xff && i < 100);
  490. if (i == 100)
  491. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  492. "continuing anyway\n");
  493. /* invalidate TCE cache */
  494. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  495. writeq(tbl->tar_val, target);
  496. /* enable arbitration */
  497. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  498. writel(aer, target);
  499. (void)readl(target); /* flush */
  500. }
  501. static void calioc2_tce_cache_blast(struct iommu_table *tbl)
  502. {
  503. void __iomem *bbar = tbl->bbar;
  504. void __iomem *target;
  505. u64 val64;
  506. u32 val;
  507. int i = 0;
  508. int count = 1;
  509. unsigned char bus = tbl->it_busno;
  510. begin:
  511. printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
  512. "sequence - count %d\n", bus, count);
  513. /* 1. using the Page Migration Control reg set SoftStop */
  514. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  515. val = be32_to_cpu(readl(target));
  516. printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
  517. val |= PMR_SOFTSTOP;
  518. printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
  519. writel(cpu_to_be32(val), target);
  520. /* 2. poll split queues until all DMA activity is done */
  521. printk(KERN_DEBUG "2a. starting to poll split queues\n");
  522. target = calgary_reg(bbar, split_queue_offset(bus));
  523. do {
  524. val64 = readq(target);
  525. i++;
  526. } while ((val64 & 0xff) != 0xff && i < 100);
  527. if (i == 100)
  528. printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
  529. "continuing anyway\n");
  530. /* 3. poll Page Migration DEBUG for SoftStopFault */
  531. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  532. val = be32_to_cpu(readl(target));
  533. printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
  534. /* 4. if SoftStopFault - goto (1) */
  535. if (val & PMR_SOFTSTOPFAULT) {
  536. if (++count < 100)
  537. goto begin;
  538. else {
  539. printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
  540. "aborting TCE cache flush sequence!\n");
  541. return; /* pray for the best */
  542. }
  543. }
  544. /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
  545. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  546. printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
  547. val = be32_to_cpu(readl(target));
  548. printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
  549. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  550. val = be32_to_cpu(readl(target));
  551. printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
  552. /* 6. invalidate TCE cache */
  553. printk(KERN_DEBUG "6. invalidating TCE cache\n");
  554. target = calgary_reg(bbar, tar_offset(bus));
  555. writeq(tbl->tar_val, target);
  556. /* 7. Re-read PMCR */
  557. printk(KERN_DEBUG "7a. Re-reading PMCR\n");
  558. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  559. val = be32_to_cpu(readl(target));
  560. printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
  561. /* 8. Remove HardStop */
  562. printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
  563. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  564. val = 0;
  565. printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
  566. writel(cpu_to_be32(val), target);
  567. val = be32_to_cpu(readl(target));
  568. printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
  569. }
  570. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  571. u64 limit)
  572. {
  573. unsigned int numpages;
  574. limit = limit | 0xfffff;
  575. limit++;
  576. numpages = ((limit - start) >> PAGE_SHIFT);
  577. iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
  578. }
  579. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  580. {
  581. void __iomem *target;
  582. u64 low, high, sizelow;
  583. u64 start, limit;
  584. struct iommu_table *tbl = pci_iommu(dev->bus);
  585. unsigned char busnum = dev->bus->number;
  586. void __iomem *bbar = tbl->bbar;
  587. /* peripheral MEM_1 region */
  588. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  589. low = be32_to_cpu(readl(target));
  590. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  591. high = be32_to_cpu(readl(target));
  592. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  593. sizelow = be32_to_cpu(readl(target));
  594. start = (high << 32) | low;
  595. limit = sizelow;
  596. calgary_reserve_mem_region(dev, start, limit);
  597. }
  598. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  599. {
  600. void __iomem *target;
  601. u32 val32;
  602. u64 low, high, sizelow, sizehigh;
  603. u64 start, limit;
  604. struct iommu_table *tbl = pci_iommu(dev->bus);
  605. unsigned char busnum = dev->bus->number;
  606. void __iomem *bbar = tbl->bbar;
  607. /* is it enabled? */
  608. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  609. val32 = be32_to_cpu(readl(target));
  610. if (!(val32 & PHB_MEM2_ENABLE))
  611. return;
  612. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  613. low = be32_to_cpu(readl(target));
  614. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  615. high = be32_to_cpu(readl(target));
  616. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  617. sizelow = be32_to_cpu(readl(target));
  618. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  619. sizehigh = be32_to_cpu(readl(target));
  620. start = (high << 32) | low;
  621. limit = (sizehigh << 32) | sizelow;
  622. calgary_reserve_mem_region(dev, start, limit);
  623. }
  624. /*
  625. * some regions of the IO address space do not get translated, so we
  626. * must not give devices IO addresses in those regions. The regions
  627. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  628. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  629. * later.
  630. */
  631. static void __init calgary_reserve_regions(struct pci_dev *dev)
  632. {
  633. unsigned int npages;
  634. u64 start;
  635. struct iommu_table *tbl = pci_iommu(dev->bus);
  636. /* reserve EMERGENCY_PAGES from bad_dma_address and up */
  637. iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
  638. /* avoid the BIOS/VGA first 640KB-1MB region */
  639. /* for CalIOC2 - avoid the entire first MB */
  640. if (is_calgary(dev->device)) {
  641. start = (640 * 1024);
  642. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  643. } else { /* calioc2 */
  644. start = 0;
  645. npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
  646. }
  647. iommu_range_reserve(tbl, start, npages);
  648. /* reserve the two PCI peripheral memory regions in IO space */
  649. calgary_reserve_peripheral_mem_1(dev);
  650. calgary_reserve_peripheral_mem_2(dev);
  651. }
  652. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  653. {
  654. u64 val64;
  655. u64 table_phys;
  656. void __iomem *target;
  657. int ret;
  658. struct iommu_table *tbl;
  659. /* build TCE tables for each PHB */
  660. ret = build_tce_table(dev, bbar);
  661. if (ret)
  662. return ret;
  663. tbl = pci_iommu(dev->bus);
  664. tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
  665. if (is_kdump_kernel())
  666. calgary_init_bitmap_from_tce_table(tbl);
  667. else
  668. tce_free(tbl, 0, tbl->it_size);
  669. if (is_calgary(dev->device))
  670. tbl->chip_ops = &calgary_chip_ops;
  671. else if (is_calioc2(dev->device))
  672. tbl->chip_ops = &calioc2_chip_ops;
  673. else
  674. BUG();
  675. calgary_reserve_regions(dev);
  676. /* set TARs for each PHB */
  677. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  678. val64 = be64_to_cpu(readq(target));
  679. /* zero out all TAR bits under sw control */
  680. val64 &= ~TAR_SW_BITS;
  681. table_phys = (u64)__pa(tbl->it_base);
  682. val64 |= table_phys;
  683. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  684. val64 |= (u64) specified_table_size;
  685. tbl->tar_val = cpu_to_be64(val64);
  686. writeq(tbl->tar_val, target);
  687. readq(target); /* flush */
  688. return 0;
  689. }
  690. static void __init calgary_free_bus(struct pci_dev *dev)
  691. {
  692. u64 val64;
  693. struct iommu_table *tbl = pci_iommu(dev->bus);
  694. void __iomem *target;
  695. unsigned int bitmapsz;
  696. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  697. val64 = be64_to_cpu(readq(target));
  698. val64 &= ~TAR_SW_BITS;
  699. writeq(cpu_to_be64(val64), target);
  700. readq(target); /* flush */
  701. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  702. free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
  703. tbl->it_map = NULL;
  704. kfree(tbl);
  705. set_pci_iommu(dev->bus, NULL);
  706. /* Can't free bootmem allocated memory after system is up :-( */
  707. bus_info[dev->bus->number].tce_space = NULL;
  708. }
  709. static void calgary_dump_error_regs(struct iommu_table *tbl)
  710. {
  711. void __iomem *bbar = tbl->bbar;
  712. void __iomem *target;
  713. u32 csr, plssr;
  714. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  715. csr = be32_to_cpu(readl(target));
  716. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  717. plssr = be32_to_cpu(readl(target));
  718. /* If no error, the agent ID in the CSR is not valid */
  719. printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
  720. "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
  721. }
  722. static void calioc2_dump_error_regs(struct iommu_table *tbl)
  723. {
  724. void __iomem *bbar = tbl->bbar;
  725. u32 csr, csmr, plssr, mck, rcstat;
  726. void __iomem *target;
  727. unsigned long phboff = phb_offset(tbl->it_busno);
  728. unsigned long erroff;
  729. u32 errregs[7];
  730. int i;
  731. /* dump CSR */
  732. target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
  733. csr = be32_to_cpu(readl(target));
  734. /* dump PLSSR */
  735. target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
  736. plssr = be32_to_cpu(readl(target));
  737. /* dump CSMR */
  738. target = calgary_reg(bbar, phboff | 0x290);
  739. csmr = be32_to_cpu(readl(target));
  740. /* dump mck */
  741. target = calgary_reg(bbar, phboff | 0x800);
  742. mck = be32_to_cpu(readl(target));
  743. printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
  744. tbl->it_busno);
  745. printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
  746. csr, plssr, csmr, mck);
  747. /* dump rest of error regs */
  748. printk(KERN_EMERG "Calgary: ");
  749. for (i = 0; i < ARRAY_SIZE(errregs); i++) {
  750. /* err regs are at 0x810 - 0x870 */
  751. erroff = (0x810 + (i * 0x10));
  752. target = calgary_reg(bbar, phboff | erroff);
  753. errregs[i] = be32_to_cpu(readl(target));
  754. printk("0x%08x@0x%lx ", errregs[i], erroff);
  755. }
  756. printk("\n");
  757. /* root complex status */
  758. target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
  759. rcstat = be32_to_cpu(readl(target));
  760. printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
  761. PHB_ROOT_COMPLEX_STATUS);
  762. }
  763. static void calgary_watchdog(unsigned long data)
  764. {
  765. struct pci_dev *dev = (struct pci_dev *)data;
  766. struct iommu_table *tbl = pci_iommu(dev->bus);
  767. void __iomem *bbar = tbl->bbar;
  768. u32 val32;
  769. void __iomem *target;
  770. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  771. val32 = be32_to_cpu(readl(target));
  772. /* If no error, the agent ID in the CSR is not valid */
  773. if (val32 & CSR_AGENT_MASK) {
  774. tbl->chip_ops->dump_error_regs(tbl);
  775. /* reset error */
  776. writel(0, target);
  777. /* Disable bus that caused the error */
  778. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  779. PHB_CONFIG_RW_OFFSET);
  780. val32 = be32_to_cpu(readl(target));
  781. val32 |= PHB_SLOT_DISABLE;
  782. writel(cpu_to_be32(val32), target);
  783. readl(target); /* flush */
  784. } else {
  785. /* Reset the timer */
  786. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  787. }
  788. }
  789. static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
  790. unsigned char busnum, unsigned long timeout)
  791. {
  792. u64 val64;
  793. void __iomem *target;
  794. unsigned int phb_shift = ~0; /* silence gcc */
  795. u64 mask;
  796. switch (busno_to_phbid(busnum)) {
  797. case 0: phb_shift = (63 - 19);
  798. break;
  799. case 1: phb_shift = (63 - 23);
  800. break;
  801. case 2: phb_shift = (63 - 27);
  802. break;
  803. case 3: phb_shift = (63 - 35);
  804. break;
  805. default:
  806. BUG_ON(busno_to_phbid(busnum));
  807. }
  808. target = calgary_reg(bbar, CALGARY_CONFIG_REG);
  809. val64 = be64_to_cpu(readq(target));
  810. /* zero out this PHB's timer bits */
  811. mask = ~(0xFUL << phb_shift);
  812. val64 &= mask;
  813. val64 |= (timeout << phb_shift);
  814. writeq(cpu_to_be64(val64), target);
  815. readq(target); /* flush */
  816. }
  817. static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  818. {
  819. unsigned char busnum = dev->bus->number;
  820. void __iomem *bbar = tbl->bbar;
  821. void __iomem *target;
  822. u32 val;
  823. /*
  824. * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
  825. */
  826. target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
  827. val = cpu_to_be32(readl(target));
  828. val |= 0x00800000;
  829. writel(cpu_to_be32(val), target);
  830. }
  831. static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  832. {
  833. unsigned char busnum = dev->bus->number;
  834. /*
  835. * Give split completion a longer timeout on bus 1 for aic94xx
  836. * http://bugzilla.kernel.org/show_bug.cgi?id=7180
  837. */
  838. if (is_calgary(dev->device) && (busnum == 1))
  839. calgary_set_split_completion_timeout(tbl->bbar, busnum,
  840. CCR_2SEC_TIMEOUT);
  841. }
  842. static void __init calgary_enable_translation(struct pci_dev *dev)
  843. {
  844. u32 val32;
  845. unsigned char busnum;
  846. void __iomem *target;
  847. void __iomem *bbar;
  848. struct iommu_table *tbl;
  849. busnum = dev->bus->number;
  850. tbl = pci_iommu(dev->bus);
  851. bbar = tbl->bbar;
  852. /* enable TCE in PHB Config Register */
  853. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  854. val32 = be32_to_cpu(readl(target));
  855. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  856. printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
  857. (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
  858. "Calgary" : "CalIOC2", busnum);
  859. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  860. "bus.\n");
  861. writel(cpu_to_be32(val32), target);
  862. readl(target); /* flush */
  863. init_timer(&tbl->watchdog_timer);
  864. tbl->watchdog_timer.function = &calgary_watchdog;
  865. tbl->watchdog_timer.data = (unsigned long)dev;
  866. mod_timer(&tbl->watchdog_timer, jiffies);
  867. }
  868. static void __init calgary_disable_translation(struct pci_dev *dev)
  869. {
  870. u32 val32;
  871. unsigned char busnum;
  872. void __iomem *target;
  873. void __iomem *bbar;
  874. struct iommu_table *tbl;
  875. busnum = dev->bus->number;
  876. tbl = pci_iommu(dev->bus);
  877. bbar = tbl->bbar;
  878. /* disable TCE in PHB Config Register */
  879. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  880. val32 = be32_to_cpu(readl(target));
  881. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  882. printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
  883. writel(cpu_to_be32(val32), target);
  884. readl(target); /* flush */
  885. del_timer_sync(&tbl->watchdog_timer);
  886. }
  887. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  888. {
  889. pci_dev_get(dev);
  890. set_pci_iommu(dev->bus, NULL);
  891. /* is the device behind a bridge? */
  892. if (dev->bus->parent)
  893. dev->bus->parent->self = dev;
  894. else
  895. dev->bus->self = dev;
  896. }
  897. static int __init calgary_init_one(struct pci_dev *dev)
  898. {
  899. void __iomem *bbar;
  900. struct iommu_table *tbl;
  901. int ret;
  902. BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
  903. bbar = busno_to_bbar(dev->bus->number);
  904. ret = calgary_setup_tar(dev, bbar);
  905. if (ret)
  906. goto done;
  907. pci_dev_get(dev);
  908. if (dev->bus->parent) {
  909. if (dev->bus->parent->self)
  910. printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
  911. "bus->parent->self!\n", dev);
  912. dev->bus->parent->self = dev;
  913. } else
  914. dev->bus->self = dev;
  915. tbl = pci_iommu(dev->bus);
  916. tbl->chip_ops->handle_quirks(tbl, dev);
  917. calgary_enable_translation(dev);
  918. return 0;
  919. done:
  920. return ret;
  921. }
  922. static int __init calgary_locate_bbars(void)
  923. {
  924. int ret;
  925. int rioidx, phb, bus;
  926. void __iomem *bbar;
  927. void __iomem *target;
  928. unsigned long offset;
  929. u8 start_bus, end_bus;
  930. u32 val;
  931. ret = -ENODATA;
  932. for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
  933. struct rio_detail *rio = rio_devs[rioidx];
  934. if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
  935. continue;
  936. /* map entire 1MB of Calgary config space */
  937. bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
  938. if (!bbar)
  939. goto error;
  940. for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
  941. offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
  942. target = calgary_reg(bbar, offset);
  943. val = be32_to_cpu(readl(target));
  944. start_bus = (u8)((val & 0x00FF0000) >> 16);
  945. end_bus = (u8)((val & 0x0000FF00) >> 8);
  946. if (end_bus) {
  947. for (bus = start_bus; bus <= end_bus; bus++) {
  948. bus_info[bus].bbar = bbar;
  949. bus_info[bus].phbid = phb;
  950. }
  951. } else {
  952. bus_info[start_bus].bbar = bbar;
  953. bus_info[start_bus].phbid = phb;
  954. }
  955. }
  956. }
  957. return 0;
  958. error:
  959. /* scan bus_info and iounmap any bbars we previously ioremap'd */
  960. for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
  961. if (bus_info[bus].bbar)
  962. iounmap(bus_info[bus].bbar);
  963. return ret;
  964. }
  965. static int __init calgary_init(void)
  966. {
  967. int ret;
  968. struct pci_dev *dev = NULL;
  969. struct calgary_bus_info *info;
  970. ret = calgary_locate_bbars();
  971. if (ret)
  972. return ret;
  973. /* Purely for kdump kernel case */
  974. if (is_kdump_kernel())
  975. get_tce_space_from_tar();
  976. do {
  977. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  978. if (!dev)
  979. break;
  980. if (!is_cal_pci_dev(dev->device))
  981. continue;
  982. info = &bus_info[dev->bus->number];
  983. if (info->translation_disabled) {
  984. calgary_init_one_nontraslated(dev);
  985. continue;
  986. }
  987. if (!info->tce_space && !translate_empty_slots)
  988. continue;
  989. ret = calgary_init_one(dev);
  990. if (ret)
  991. goto error;
  992. } while (1);
  993. dev = NULL;
  994. for_each_pci_dev(dev) {
  995. struct iommu_table *tbl;
  996. tbl = find_iommu_table(&dev->dev);
  997. if (translation_enabled(tbl))
  998. dev->dev.archdata.dma_ops = &calgary_dma_ops;
  999. }
  1000. return ret;
  1001. error:
  1002. do {
  1003. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1004. if (!dev)
  1005. break;
  1006. if (!is_cal_pci_dev(dev->device))
  1007. continue;
  1008. info = &bus_info[dev->bus->number];
  1009. if (info->translation_disabled) {
  1010. pci_dev_put(dev);
  1011. continue;
  1012. }
  1013. if (!info->tce_space && !translate_empty_slots)
  1014. continue;
  1015. calgary_disable_translation(dev);
  1016. calgary_free_bus(dev);
  1017. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  1018. dev->dev.archdata.dma_ops = NULL;
  1019. } while (1);
  1020. return ret;
  1021. }
  1022. static inline int __init determine_tce_table_size(u64 ram)
  1023. {
  1024. int ret;
  1025. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  1026. return specified_table_size;
  1027. /*
  1028. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  1029. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  1030. * larger table size has twice as many entries, so shift the
  1031. * max ram address by 13 to divide by 8K and then look at the
  1032. * order of the result to choose between 0-7.
  1033. */
  1034. ret = get_order(ram >> 13);
  1035. if (ret > TCE_TABLE_SIZE_8M)
  1036. ret = TCE_TABLE_SIZE_8M;
  1037. return ret;
  1038. }
  1039. static int __init build_detail_arrays(void)
  1040. {
  1041. unsigned long ptr;
  1042. unsigned numnodes, i;
  1043. int scal_detail_size, rio_detail_size;
  1044. numnodes = rio_table_hdr->num_scal_dev;
  1045. if (numnodes > MAX_NUMNODES){
  1046. printk(KERN_WARNING
  1047. "Calgary: MAX_NUMNODES too low! Defined as %d, "
  1048. "but system has %d nodes.\n",
  1049. MAX_NUMNODES, numnodes);
  1050. return -ENODEV;
  1051. }
  1052. switch (rio_table_hdr->version){
  1053. case 2:
  1054. scal_detail_size = 11;
  1055. rio_detail_size = 13;
  1056. break;
  1057. case 3:
  1058. scal_detail_size = 12;
  1059. rio_detail_size = 15;
  1060. break;
  1061. default:
  1062. printk(KERN_WARNING
  1063. "Calgary: Invalid Rio Grande Table Version: %d\n",
  1064. rio_table_hdr->version);
  1065. return -EPROTO;
  1066. }
  1067. ptr = ((unsigned long)rio_table_hdr) + 3;
  1068. for (i = 0; i < numnodes; i++, ptr += scal_detail_size)
  1069. scal_devs[i] = (struct scal_detail *)ptr;
  1070. for (i = 0; i < rio_table_hdr->num_rio_dev;
  1071. i++, ptr += rio_detail_size)
  1072. rio_devs[i] = (struct rio_detail *)ptr;
  1073. return 0;
  1074. }
  1075. static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
  1076. {
  1077. int dev;
  1078. u32 val;
  1079. if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
  1080. /*
  1081. * FIXME: properly scan for devices accross the
  1082. * PCI-to-PCI bridge on every CalIOC2 port.
  1083. */
  1084. return 1;
  1085. }
  1086. for (dev = 1; dev < 8; dev++) {
  1087. val = read_pci_config(bus, dev, 0, 0);
  1088. if (val != 0xffffffff)
  1089. break;
  1090. }
  1091. return (val != 0xffffffff);
  1092. }
  1093. /*
  1094. * calgary_init_bitmap_from_tce_table():
  1095. * Funtion for kdump case. In the second/kdump kernel initialize
  1096. * the bitmap based on the tce table entries obtained from first kernel
  1097. */
  1098. static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl)
  1099. {
  1100. u64 *tp;
  1101. unsigned int index;
  1102. tp = ((u64 *)tbl->it_base);
  1103. for (index = 0 ; index < tbl->it_size; index++) {
  1104. if (*tp != 0x0)
  1105. set_bit(index, tbl->it_map);
  1106. tp++;
  1107. }
  1108. }
  1109. /*
  1110. * get_tce_space_from_tar():
  1111. * Function for kdump case. Get the tce tables from first kernel
  1112. * by reading the contents of the base adress register of calgary iommu
  1113. */
  1114. static void __init get_tce_space_from_tar(void)
  1115. {
  1116. int bus;
  1117. void __iomem *target;
  1118. unsigned long tce_space;
  1119. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  1120. struct calgary_bus_info *info = &bus_info[bus];
  1121. unsigned short pci_device;
  1122. u32 val;
  1123. val = read_pci_config(bus, 0, 0, 0);
  1124. pci_device = (val & 0xFFFF0000) >> 16;
  1125. if (!is_cal_pci_dev(pci_device))
  1126. continue;
  1127. if (info->translation_disabled)
  1128. continue;
  1129. if (calgary_bus_has_devices(bus, pci_device) ||
  1130. translate_empty_slots) {
  1131. target = calgary_reg(bus_info[bus].bbar,
  1132. tar_offset(bus));
  1133. tce_space = be64_to_cpu(readq(target));
  1134. tce_space = tce_space & TAR_SW_BITS;
  1135. tce_space = tce_space & (~specified_table_size);
  1136. info->tce_space = (u64 *)__va(tce_space);
  1137. }
  1138. }
  1139. return;
  1140. }
  1141. void __init detect_calgary(void)
  1142. {
  1143. int bus;
  1144. void *tbl;
  1145. int calgary_found = 0;
  1146. unsigned long ptr;
  1147. unsigned int offset, prev_offset;
  1148. int ret;
  1149. /*
  1150. * if the user specified iommu=off or iommu=soft or we found
  1151. * another HW IOMMU already, bail out.
  1152. */
  1153. if (swiotlb || no_iommu || iommu_detected)
  1154. return;
  1155. if (!use_calgary)
  1156. return;
  1157. if (!early_pci_allowed())
  1158. return;
  1159. printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
  1160. ptr = (unsigned long)phys_to_virt(get_bios_ebda());
  1161. rio_table_hdr = NULL;
  1162. prev_offset = 0;
  1163. offset = 0x180;
  1164. /*
  1165. * The next offset is stored in the 1st word.
  1166. * Only parse up until the offset increases:
  1167. */
  1168. while (offset > prev_offset) {
  1169. /* The block id is stored in the 2nd word */
  1170. if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
  1171. /* set the pointer past the offset & block id */
  1172. rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
  1173. break;
  1174. }
  1175. prev_offset = offset;
  1176. offset = *((unsigned short *)(ptr + offset));
  1177. }
  1178. if (!rio_table_hdr) {
  1179. printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
  1180. "in EBDA - bailing!\n");
  1181. return;
  1182. }
  1183. ret = build_detail_arrays();
  1184. if (ret) {
  1185. printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
  1186. return;
  1187. }
  1188. specified_table_size = determine_tce_table_size((is_kdump_kernel() ?
  1189. saved_max_pfn : max_pfn) * PAGE_SIZE);
  1190. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  1191. struct calgary_bus_info *info = &bus_info[bus];
  1192. unsigned short pci_device;
  1193. u32 val;
  1194. val = read_pci_config(bus, 0, 0, 0);
  1195. pci_device = (val & 0xFFFF0000) >> 16;
  1196. if (!is_cal_pci_dev(pci_device))
  1197. continue;
  1198. if (info->translation_disabled)
  1199. continue;
  1200. if (calgary_bus_has_devices(bus, pci_device) ||
  1201. translate_empty_slots) {
  1202. /*
  1203. * If it is kdump kernel, find and use tce tables
  1204. * from first kernel, else allocate tce tables here
  1205. */
  1206. if (!is_kdump_kernel()) {
  1207. tbl = alloc_tce_table();
  1208. if (!tbl)
  1209. goto cleanup;
  1210. info->tce_space = tbl;
  1211. }
  1212. calgary_found = 1;
  1213. }
  1214. }
  1215. printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
  1216. calgary_found ? "found" : "not found");
  1217. if (calgary_found) {
  1218. iommu_detected = 1;
  1219. calgary_detected = 1;
  1220. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
  1221. printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
  1222. "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
  1223. debugging ? "enabled" : "disabled");
  1224. /* swiotlb for devices that aren't behind the Calgary. */
  1225. if (max_pfn > MAX_DMA32_PFN)
  1226. swiotlb = 1;
  1227. }
  1228. return;
  1229. cleanup:
  1230. for (--bus; bus >= 0; --bus) {
  1231. struct calgary_bus_info *info = &bus_info[bus];
  1232. if (info->tce_space)
  1233. free_tce_table(info->tce_space);
  1234. }
  1235. }
  1236. int __init calgary_iommu_init(void)
  1237. {
  1238. int ret;
  1239. if (no_iommu || (swiotlb && !calgary_detected))
  1240. return -ENODEV;
  1241. if (!calgary_detected)
  1242. return -ENODEV;
  1243. /* ok, we're trying to use Calgary - let's roll */
  1244. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  1245. ret = calgary_init();
  1246. if (ret) {
  1247. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  1248. "falling back to no_iommu\n", ret);
  1249. return ret;
  1250. }
  1251. force_iommu = 1;
  1252. bad_dma_address = 0x0;
  1253. /* dma_ops is set to swiotlb or nommu */
  1254. if (!dma_ops)
  1255. dma_ops = &nommu_dma_ops;
  1256. return 0;
  1257. }
  1258. static int __init calgary_parse_options(char *p)
  1259. {
  1260. unsigned int bridge;
  1261. size_t len;
  1262. char* endp;
  1263. while (*p) {
  1264. if (!strncmp(p, "64k", 3))
  1265. specified_table_size = TCE_TABLE_SIZE_64K;
  1266. else if (!strncmp(p, "128k", 4))
  1267. specified_table_size = TCE_TABLE_SIZE_128K;
  1268. else if (!strncmp(p, "256k", 4))
  1269. specified_table_size = TCE_TABLE_SIZE_256K;
  1270. else if (!strncmp(p, "512k", 4))
  1271. specified_table_size = TCE_TABLE_SIZE_512K;
  1272. else if (!strncmp(p, "1M", 2))
  1273. specified_table_size = TCE_TABLE_SIZE_1M;
  1274. else if (!strncmp(p, "2M", 2))
  1275. specified_table_size = TCE_TABLE_SIZE_2M;
  1276. else if (!strncmp(p, "4M", 2))
  1277. specified_table_size = TCE_TABLE_SIZE_4M;
  1278. else if (!strncmp(p, "8M", 2))
  1279. specified_table_size = TCE_TABLE_SIZE_8M;
  1280. len = strlen("translate_empty_slots");
  1281. if (!strncmp(p, "translate_empty_slots", len))
  1282. translate_empty_slots = 1;
  1283. len = strlen("disable");
  1284. if (!strncmp(p, "disable", len)) {
  1285. p += len;
  1286. if (*p == '=')
  1287. ++p;
  1288. if (*p == '\0')
  1289. break;
  1290. bridge = simple_strtoul(p, &endp, 0);
  1291. if (p == endp)
  1292. break;
  1293. if (bridge < MAX_PHB_BUS_NUM) {
  1294. printk(KERN_INFO "Calgary: disabling "
  1295. "translation for PHB %#x\n", bridge);
  1296. bus_info[bridge].translation_disabled = 1;
  1297. }
  1298. }
  1299. p = strpbrk(p, ",");
  1300. if (!p)
  1301. break;
  1302. p++; /* skip ',' */
  1303. }
  1304. return 1;
  1305. }
  1306. __setup("calgary=", calgary_parse_options);
  1307. static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
  1308. {
  1309. struct iommu_table *tbl;
  1310. unsigned int npages;
  1311. int i;
  1312. tbl = pci_iommu(dev->bus);
  1313. for (i = 0; i < 4; i++) {
  1314. struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
  1315. /* Don't give out TCEs that map MEM resources */
  1316. if (!(r->flags & IORESOURCE_MEM))
  1317. continue;
  1318. /* 0-based? we reserve the whole 1st MB anyway */
  1319. if (!r->start)
  1320. continue;
  1321. /* cover the whole region */
  1322. npages = (r->end - r->start) >> PAGE_SHIFT;
  1323. npages++;
  1324. iommu_range_reserve(tbl, r->start, npages);
  1325. }
  1326. }
  1327. static int __init calgary_fixup_tce_spaces(void)
  1328. {
  1329. struct pci_dev *dev = NULL;
  1330. struct calgary_bus_info *info;
  1331. if (no_iommu || swiotlb || !calgary_detected)
  1332. return -ENODEV;
  1333. printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
  1334. do {
  1335. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  1336. if (!dev)
  1337. break;
  1338. if (!is_cal_pci_dev(dev->device))
  1339. continue;
  1340. info = &bus_info[dev->bus->number];
  1341. if (info->translation_disabled)
  1342. continue;
  1343. if (!info->tce_space)
  1344. continue;
  1345. calgary_fixup_one_tce_space(dev);
  1346. } while (1);
  1347. return 0;
  1348. }
  1349. /*
  1350. * We need to be call after pcibios_assign_resources (fs_initcall level)
  1351. * and before device_initcall.
  1352. */
  1353. rootfs_initcall(calgary_fixup_tce_spaces);