pci-calgary_64.c 41 KB

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