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. printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
  286. "address 0x%Lx\n", dma_addr);
  287. WARN_ON(1);
  288. return;
  289. }
  290. entry = dma_addr >> PAGE_SHIFT;
  291. BUG_ON(entry + npages > tbl->it_size);
  292. tce_free(tbl, entry, npages);
  293. spin_lock_irqsave(&tbl->it_lock, flags);
  294. badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
  295. if (badbit != ~0UL) {
  296. if (printk_ratelimit())
  297. printk(KERN_ERR "Calgary: bit is off at 0x%lx "
  298. "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
  299. badbit, tbl, dma_addr, entry, npages);
  300. }
  301. iommu_area_free(tbl->it_map, entry, npages);
  302. spin_unlock_irqrestore(&tbl->it_lock, flags);
  303. }
  304. static inline struct iommu_table *find_iommu_table(struct device *dev)
  305. {
  306. struct pci_dev *pdev;
  307. struct pci_bus *pbus;
  308. struct iommu_table *tbl;
  309. pdev = to_pci_dev(dev);
  310. pbus = pdev->bus;
  311. /* is the device behind a bridge? Look for the root bus */
  312. while (pbus->parent)
  313. pbus = pbus->parent;
  314. tbl = pci_iommu(pbus);
  315. BUG_ON(tbl && (tbl->it_busno != pbus->number));
  316. return tbl;
  317. }
  318. static void calgary_unmap_sg(struct device *dev,
  319. struct scatterlist *sglist, int nelems, int direction)
  320. {
  321. struct iommu_table *tbl = find_iommu_table(dev);
  322. struct scatterlist *s;
  323. int i;
  324. if (!translation_enabled(tbl))
  325. return;
  326. for_each_sg(sglist, s, nelems, i) {
  327. unsigned int npages;
  328. dma_addr_t dma = s->dma_address;
  329. unsigned int dmalen = s->dma_length;
  330. if (dmalen == 0)
  331. break;
  332. npages = num_dma_pages(dma, dmalen);
  333. iommu_free(tbl, dma, npages);
  334. }
  335. }
  336. static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
  337. int nelems, int direction)
  338. {
  339. struct iommu_table *tbl = find_iommu_table(dev);
  340. struct scatterlist *s;
  341. unsigned long vaddr;
  342. unsigned int npages;
  343. unsigned long entry;
  344. int i;
  345. for_each_sg(sg, s, nelems, i) {
  346. BUG_ON(!sg_page(s));
  347. vaddr = (unsigned long) sg_virt(s);
  348. npages = num_dma_pages(vaddr, s->length);
  349. entry = iommu_range_alloc(dev, tbl, npages);
  350. if (entry == bad_dma_address) {
  351. /* makes sure unmap knows to stop */
  352. s->dma_length = 0;
  353. goto error;
  354. }
  355. s->dma_address = (entry << PAGE_SHIFT) | s->offset;
  356. /* insert into HW table */
  357. tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
  358. direction);
  359. s->dma_length = s->length;
  360. }
  361. return nelems;
  362. error:
  363. calgary_unmap_sg(dev, sg, nelems, direction);
  364. for_each_sg(sg, s, nelems, i) {
  365. sg->dma_address = bad_dma_address;
  366. sg->dma_length = 0;
  367. }
  368. return 0;
  369. }
  370. static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
  371. size_t size, int direction)
  372. {
  373. void *vaddr = phys_to_virt(paddr);
  374. unsigned long uaddr;
  375. unsigned int npages;
  376. struct iommu_table *tbl = find_iommu_table(dev);
  377. uaddr = (unsigned long)vaddr;
  378. npages = num_dma_pages(uaddr, size);
  379. return iommu_alloc(dev, tbl, vaddr, npages, direction);
  380. }
  381. static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
  382. size_t size, int direction)
  383. {
  384. struct iommu_table *tbl = find_iommu_table(dev);
  385. unsigned int npages;
  386. npages = num_dma_pages(dma_handle, size);
  387. iommu_free(tbl, dma_handle, npages);
  388. }
  389. static void* calgary_alloc_coherent(struct device *dev, size_t size,
  390. dma_addr_t *dma_handle, gfp_t flag)
  391. {
  392. void *ret = NULL;
  393. dma_addr_t mapping;
  394. unsigned int npages, order;
  395. struct iommu_table *tbl = find_iommu_table(dev);
  396. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  397. npages = size >> PAGE_SHIFT;
  398. order = get_order(size);
  399. /* alloc enough pages (and possibly more) */
  400. ret = (void *)__get_free_pages(flag, order);
  401. if (!ret)
  402. goto error;
  403. memset(ret, 0, size);
  404. /* set up tces to cover the allocated range */
  405. mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
  406. if (mapping == bad_dma_address)
  407. goto free;
  408. *dma_handle = mapping;
  409. return ret;
  410. free:
  411. free_pages((unsigned long)ret, get_order(size));
  412. ret = NULL;
  413. error:
  414. return ret;
  415. }
  416. static struct dma_mapping_ops calgary_dma_ops = {
  417. .alloc_coherent = calgary_alloc_coherent,
  418. .map_single = calgary_map_single,
  419. .unmap_single = calgary_unmap_single,
  420. .map_sg = calgary_map_sg,
  421. .unmap_sg = calgary_unmap_sg,
  422. };
  423. static inline void __iomem * busno_to_bbar(unsigned char num)
  424. {
  425. return bus_info[num].bbar;
  426. }
  427. static inline int busno_to_phbid(unsigned char num)
  428. {
  429. return bus_info[num].phbid;
  430. }
  431. static inline unsigned long split_queue_offset(unsigned char num)
  432. {
  433. size_t idx = busno_to_phbid(num);
  434. return split_queue_offsets[idx];
  435. }
  436. static inline unsigned long tar_offset(unsigned char num)
  437. {
  438. size_t idx = busno_to_phbid(num);
  439. return tar_offsets[idx];
  440. }
  441. static inline unsigned long phb_offset(unsigned char num)
  442. {
  443. size_t idx = busno_to_phbid(num);
  444. return phb_offsets[idx];
  445. }
  446. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  447. {
  448. unsigned long target = ((unsigned long)bar) | offset;
  449. return (void __iomem*)target;
  450. }
  451. static inline int is_calioc2(unsigned short device)
  452. {
  453. return (device == PCI_DEVICE_ID_IBM_CALIOC2);
  454. }
  455. static inline int is_calgary(unsigned short device)
  456. {
  457. return (device == PCI_DEVICE_ID_IBM_CALGARY);
  458. }
  459. static inline int is_cal_pci_dev(unsigned short device)
  460. {
  461. return (is_calgary(device) || is_calioc2(device));
  462. }
  463. static void calgary_tce_cache_blast(struct iommu_table *tbl)
  464. {
  465. u64 val;
  466. u32 aer;
  467. int i = 0;
  468. void __iomem *bbar = tbl->bbar;
  469. void __iomem *target;
  470. /* disable arbitration on the bus */
  471. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  472. aer = readl(target);
  473. writel(0, target);
  474. /* read plssr to ensure it got there */
  475. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  476. val = readl(target);
  477. /* poll split queues until all DMA activity is done */
  478. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  479. do {
  480. val = readq(target);
  481. i++;
  482. } while ((val & 0xff) != 0xff && i < 100);
  483. if (i == 100)
  484. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  485. "continuing anyway\n");
  486. /* invalidate TCE cache */
  487. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  488. writeq(tbl->tar_val, target);
  489. /* enable arbitration */
  490. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  491. writel(aer, target);
  492. (void)readl(target); /* flush */
  493. }
  494. static void calioc2_tce_cache_blast(struct iommu_table *tbl)
  495. {
  496. void __iomem *bbar = tbl->bbar;
  497. void __iomem *target;
  498. u64 val64;
  499. u32 val;
  500. int i = 0;
  501. int count = 1;
  502. unsigned char bus = tbl->it_busno;
  503. begin:
  504. printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
  505. "sequence - count %d\n", bus, count);
  506. /* 1. using the Page Migration Control reg set SoftStop */
  507. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  508. val = be32_to_cpu(readl(target));
  509. printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
  510. val |= PMR_SOFTSTOP;
  511. printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
  512. writel(cpu_to_be32(val), target);
  513. /* 2. poll split queues until all DMA activity is done */
  514. printk(KERN_DEBUG "2a. starting to poll split queues\n");
  515. target = calgary_reg(bbar, split_queue_offset(bus));
  516. do {
  517. val64 = readq(target);
  518. i++;
  519. } while ((val64 & 0xff) != 0xff && i < 100);
  520. if (i == 100)
  521. printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
  522. "continuing anyway\n");
  523. /* 3. poll Page Migration DEBUG for SoftStopFault */
  524. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  525. val = be32_to_cpu(readl(target));
  526. printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
  527. /* 4. if SoftStopFault - goto (1) */
  528. if (val & PMR_SOFTSTOPFAULT) {
  529. if (++count < 100)
  530. goto begin;
  531. else {
  532. printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
  533. "aborting TCE cache flush sequence!\n");
  534. return; /* pray for the best */
  535. }
  536. }
  537. /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
  538. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  539. printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
  540. val = be32_to_cpu(readl(target));
  541. printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
  542. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
  543. val = be32_to_cpu(readl(target));
  544. printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
  545. /* 6. invalidate TCE cache */
  546. printk(KERN_DEBUG "6. invalidating TCE cache\n");
  547. target = calgary_reg(bbar, tar_offset(bus));
  548. writeq(tbl->tar_val, target);
  549. /* 7. Re-read PMCR */
  550. printk(KERN_DEBUG "7a. Re-reading PMCR\n");
  551. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  552. val = be32_to_cpu(readl(target));
  553. printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
  554. /* 8. Remove HardStop */
  555. printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
  556. target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
  557. val = 0;
  558. printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
  559. writel(cpu_to_be32(val), target);
  560. val = be32_to_cpu(readl(target));
  561. printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
  562. }
  563. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  564. u64 limit)
  565. {
  566. unsigned int numpages;
  567. limit = limit | 0xfffff;
  568. limit++;
  569. numpages = ((limit - start) >> PAGE_SHIFT);
  570. iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
  571. }
  572. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  573. {
  574. void __iomem *target;
  575. u64 low, high, sizelow;
  576. u64 start, limit;
  577. struct iommu_table *tbl = pci_iommu(dev->bus);
  578. unsigned char busnum = dev->bus->number;
  579. void __iomem *bbar = tbl->bbar;
  580. /* peripheral MEM_1 region */
  581. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  582. low = be32_to_cpu(readl(target));
  583. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  584. high = be32_to_cpu(readl(target));
  585. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  586. sizelow = be32_to_cpu(readl(target));
  587. start = (high << 32) | low;
  588. limit = sizelow;
  589. calgary_reserve_mem_region(dev, start, limit);
  590. }
  591. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  592. {
  593. void __iomem *target;
  594. u32 val32;
  595. u64 low, high, sizelow, sizehigh;
  596. u64 start, limit;
  597. struct iommu_table *tbl = pci_iommu(dev->bus);
  598. unsigned char busnum = dev->bus->number;
  599. void __iomem *bbar = tbl->bbar;
  600. /* is it enabled? */
  601. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  602. val32 = be32_to_cpu(readl(target));
  603. if (!(val32 & PHB_MEM2_ENABLE))
  604. return;
  605. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  606. low = be32_to_cpu(readl(target));
  607. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  608. high = be32_to_cpu(readl(target));
  609. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  610. sizelow = be32_to_cpu(readl(target));
  611. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  612. sizehigh = be32_to_cpu(readl(target));
  613. start = (high << 32) | low;
  614. limit = (sizehigh << 32) | sizelow;
  615. calgary_reserve_mem_region(dev, start, limit);
  616. }
  617. /*
  618. * some regions of the IO address space do not get translated, so we
  619. * must not give devices IO addresses in those regions. The regions
  620. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  621. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  622. * later.
  623. */
  624. static void __init calgary_reserve_regions(struct pci_dev *dev)
  625. {
  626. unsigned int npages;
  627. u64 start;
  628. struct iommu_table *tbl = pci_iommu(dev->bus);
  629. /* reserve EMERGENCY_PAGES from bad_dma_address and up */
  630. iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
  631. /* avoid the BIOS/VGA first 640KB-1MB region */
  632. /* for CalIOC2 - avoid the entire first MB */
  633. if (is_calgary(dev->device)) {
  634. start = (640 * 1024);
  635. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  636. } else { /* calioc2 */
  637. start = 0;
  638. npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
  639. }
  640. iommu_range_reserve(tbl, start, npages);
  641. /* reserve the two PCI peripheral memory regions in IO space */
  642. calgary_reserve_peripheral_mem_1(dev);
  643. calgary_reserve_peripheral_mem_2(dev);
  644. }
  645. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  646. {
  647. u64 val64;
  648. u64 table_phys;
  649. void __iomem *target;
  650. int ret;
  651. struct iommu_table *tbl;
  652. /* build TCE tables for each PHB */
  653. ret = build_tce_table(dev, bbar);
  654. if (ret)
  655. return ret;
  656. tbl = pci_iommu(dev->bus);
  657. tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
  658. if (is_kdump_kernel())
  659. calgary_init_bitmap_from_tce_table(tbl);
  660. else
  661. tce_free(tbl, 0, tbl->it_size);
  662. if (is_calgary(dev->device))
  663. tbl->chip_ops = &calgary_chip_ops;
  664. else if (is_calioc2(dev->device))
  665. tbl->chip_ops = &calioc2_chip_ops;
  666. else
  667. BUG();
  668. calgary_reserve_regions(dev);
  669. /* set TARs for each PHB */
  670. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  671. val64 = be64_to_cpu(readq(target));
  672. /* zero out all TAR bits under sw control */
  673. val64 &= ~TAR_SW_BITS;
  674. table_phys = (u64)__pa(tbl->it_base);
  675. val64 |= table_phys;
  676. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  677. val64 |= (u64) specified_table_size;
  678. tbl->tar_val = cpu_to_be64(val64);
  679. writeq(tbl->tar_val, target);
  680. readq(target); /* flush */
  681. return 0;
  682. }
  683. static void __init calgary_free_bus(struct pci_dev *dev)
  684. {
  685. u64 val64;
  686. struct iommu_table *tbl = pci_iommu(dev->bus);
  687. void __iomem *target;
  688. unsigned int bitmapsz;
  689. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  690. val64 = be64_to_cpu(readq(target));
  691. val64 &= ~TAR_SW_BITS;
  692. writeq(cpu_to_be64(val64), target);
  693. readq(target); /* flush */
  694. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  695. free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
  696. tbl->it_map = NULL;
  697. kfree(tbl);
  698. set_pci_iommu(dev->bus, NULL);
  699. /* Can't free bootmem allocated memory after system is up :-( */
  700. bus_info[dev->bus->number].tce_space = NULL;
  701. }
  702. static void calgary_dump_error_regs(struct iommu_table *tbl)
  703. {
  704. void __iomem *bbar = tbl->bbar;
  705. void __iomem *target;
  706. u32 csr, plssr;
  707. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  708. csr = be32_to_cpu(readl(target));
  709. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  710. plssr = be32_to_cpu(readl(target));
  711. /* If no error, the agent ID in the CSR is not valid */
  712. printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
  713. "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
  714. }
  715. static void calioc2_dump_error_regs(struct iommu_table *tbl)
  716. {
  717. void __iomem *bbar = tbl->bbar;
  718. u32 csr, csmr, plssr, mck, rcstat;
  719. void __iomem *target;
  720. unsigned long phboff = phb_offset(tbl->it_busno);
  721. unsigned long erroff;
  722. u32 errregs[7];
  723. int i;
  724. /* dump CSR */
  725. target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
  726. csr = be32_to_cpu(readl(target));
  727. /* dump PLSSR */
  728. target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
  729. plssr = be32_to_cpu(readl(target));
  730. /* dump CSMR */
  731. target = calgary_reg(bbar, phboff | 0x290);
  732. csmr = be32_to_cpu(readl(target));
  733. /* dump mck */
  734. target = calgary_reg(bbar, phboff | 0x800);
  735. mck = be32_to_cpu(readl(target));
  736. printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
  737. tbl->it_busno);
  738. printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
  739. csr, plssr, csmr, mck);
  740. /* dump rest of error regs */
  741. printk(KERN_EMERG "Calgary: ");
  742. for (i = 0; i < ARRAY_SIZE(errregs); i++) {
  743. /* err regs are at 0x810 - 0x870 */
  744. erroff = (0x810 + (i * 0x10));
  745. target = calgary_reg(bbar, phboff | erroff);
  746. errregs[i] = be32_to_cpu(readl(target));
  747. printk("0x%08x@0x%lx ", errregs[i], erroff);
  748. }
  749. printk("\n");
  750. /* root complex status */
  751. target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
  752. rcstat = be32_to_cpu(readl(target));
  753. printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
  754. PHB_ROOT_COMPLEX_STATUS);
  755. }
  756. static void calgary_watchdog(unsigned long data)
  757. {
  758. struct pci_dev *dev = (struct pci_dev *)data;
  759. struct iommu_table *tbl = pci_iommu(dev->bus);
  760. void __iomem *bbar = tbl->bbar;
  761. u32 val32;
  762. void __iomem *target;
  763. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  764. val32 = be32_to_cpu(readl(target));
  765. /* If no error, the agent ID in the CSR is not valid */
  766. if (val32 & CSR_AGENT_MASK) {
  767. tbl->chip_ops->dump_error_regs(tbl);
  768. /* reset error */
  769. writel(0, target);
  770. /* Disable bus that caused the error */
  771. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  772. PHB_CONFIG_RW_OFFSET);
  773. val32 = be32_to_cpu(readl(target));
  774. val32 |= PHB_SLOT_DISABLE;
  775. writel(cpu_to_be32(val32), target);
  776. readl(target); /* flush */
  777. } else {
  778. /* Reset the timer */
  779. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  780. }
  781. }
  782. static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
  783. unsigned char busnum, unsigned long timeout)
  784. {
  785. u64 val64;
  786. void __iomem *target;
  787. unsigned int phb_shift = ~0; /* silence gcc */
  788. u64 mask;
  789. switch (busno_to_phbid(busnum)) {
  790. case 0: phb_shift = (63 - 19);
  791. break;
  792. case 1: phb_shift = (63 - 23);
  793. break;
  794. case 2: phb_shift = (63 - 27);
  795. break;
  796. case 3: phb_shift = (63 - 35);
  797. break;
  798. default:
  799. BUG_ON(busno_to_phbid(busnum));
  800. }
  801. target = calgary_reg(bbar, CALGARY_CONFIG_REG);
  802. val64 = be64_to_cpu(readq(target));
  803. /* zero out this PHB's timer bits */
  804. mask = ~(0xFUL << phb_shift);
  805. val64 &= mask;
  806. val64 |= (timeout << phb_shift);
  807. writeq(cpu_to_be64(val64), target);
  808. readq(target); /* flush */
  809. }
  810. static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  811. {
  812. unsigned char busnum = dev->bus->number;
  813. void __iomem *bbar = tbl->bbar;
  814. void __iomem *target;
  815. u32 val;
  816. /*
  817. * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
  818. */
  819. target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
  820. val = cpu_to_be32(readl(target));
  821. val |= 0x00800000;
  822. writel(cpu_to_be32(val), target);
  823. }
  824. static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
  825. {
  826. unsigned char busnum = dev->bus->number;
  827. /*
  828. * Give split completion a longer timeout on bus 1 for aic94xx
  829. * http://bugzilla.kernel.org/show_bug.cgi?id=7180
  830. */
  831. if (is_calgary(dev->device) && (busnum == 1))
  832. calgary_set_split_completion_timeout(tbl->bbar, busnum,
  833. CCR_2SEC_TIMEOUT);
  834. }
  835. static void __init calgary_enable_translation(struct pci_dev *dev)
  836. {
  837. u32 val32;
  838. unsigned char busnum;
  839. void __iomem *target;
  840. void __iomem *bbar;
  841. struct iommu_table *tbl;
  842. busnum = dev->bus->number;
  843. tbl = pci_iommu(dev->bus);
  844. bbar = tbl->bbar;
  845. /* enable TCE in PHB Config Register */
  846. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  847. val32 = be32_to_cpu(readl(target));
  848. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  849. printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
  850. (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
  851. "Calgary" : "CalIOC2", busnum);
  852. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  853. "bus.\n");
  854. writel(cpu_to_be32(val32), target);
  855. readl(target); /* flush */
  856. init_timer(&tbl->watchdog_timer);
  857. tbl->watchdog_timer.function = &calgary_watchdog;
  858. tbl->watchdog_timer.data = (unsigned long)dev;
  859. mod_timer(&tbl->watchdog_timer, jiffies);
  860. }
  861. static void __init calgary_disable_translation(struct pci_dev *dev)
  862. {
  863. u32 val32;
  864. unsigned char busnum;
  865. void __iomem *target;
  866. void __iomem *bbar;
  867. struct iommu_table *tbl;
  868. busnum = dev->bus->number;
  869. tbl = pci_iommu(dev->bus);
  870. bbar = tbl->bbar;
  871. /* disable TCE in PHB Config Register */
  872. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  873. val32 = be32_to_cpu(readl(target));
  874. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  875. printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
  876. writel(cpu_to_be32(val32), target);
  877. readl(target); /* flush */
  878. del_timer_sync(&tbl->watchdog_timer);
  879. }
  880. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  881. {
  882. pci_dev_get(dev);
  883. set_pci_iommu(dev->bus, NULL);
  884. /* is the device behind a bridge? */
  885. if (dev->bus->parent)
  886. dev->bus->parent->self = dev;
  887. else
  888. dev->bus->self = dev;
  889. }
  890. static int __init calgary_init_one(struct pci_dev *dev)
  891. {
  892. void __iomem *bbar;
  893. struct iommu_table *tbl;
  894. int ret;
  895. BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
  896. bbar = busno_to_bbar(dev->bus->number);
  897. ret = calgary_setup_tar(dev, bbar);
  898. if (ret)
  899. goto done;
  900. pci_dev_get(dev);
  901. if (dev->bus->parent) {
  902. if (dev->bus->parent->self)
  903. printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
  904. "bus->parent->self!\n", dev);
  905. dev->bus->parent->self = dev;
  906. } else
  907. dev->bus->self = dev;
  908. tbl = pci_iommu(dev->bus);
  909. tbl->chip_ops->handle_quirks(tbl, dev);
  910. calgary_enable_translation(dev);
  911. return 0;
  912. done:
  913. return ret;
  914. }
  915. static int __init calgary_locate_bbars(void)
  916. {
  917. int ret;
  918. int rioidx, phb, bus;
  919. void __iomem *bbar;
  920. void __iomem *target;
  921. unsigned long offset;
  922. u8 start_bus, end_bus;
  923. u32 val;
  924. ret = -ENODATA;
  925. for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
  926. struct rio_detail *rio = rio_devs[rioidx];
  927. if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
  928. continue;
  929. /* map entire 1MB of Calgary config space */
  930. bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
  931. if (!bbar)
  932. goto error;
  933. for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
  934. offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
  935. target = calgary_reg(bbar, offset);
  936. val = be32_to_cpu(readl(target));
  937. start_bus = (u8)((val & 0x00FF0000) >> 16);
  938. end_bus = (u8)((val & 0x0000FF00) >> 8);
  939. if (end_bus) {
  940. for (bus = start_bus; bus <= end_bus; bus++) {
  941. bus_info[bus].bbar = bbar;
  942. bus_info[bus].phbid = phb;
  943. }
  944. } else {
  945. bus_info[start_bus].bbar = bbar;
  946. bus_info[start_bus].phbid = phb;
  947. }
  948. }
  949. }
  950. return 0;
  951. error:
  952. /* scan bus_info and iounmap any bbars we previously ioremap'd */
  953. for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
  954. if (bus_info[bus].bbar)
  955. iounmap(bus_info[bus].bbar);
  956. return ret;
  957. }
  958. static int __init calgary_init(void)
  959. {
  960. int ret;
  961. struct pci_dev *dev = NULL;
  962. struct calgary_bus_info *info;
  963. ret = calgary_locate_bbars();
  964. if (ret)
  965. return ret;
  966. /* Purely for kdump kernel case */
  967. if (is_kdump_kernel())
  968. get_tce_space_from_tar();
  969. do {
  970. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  971. if (!dev)
  972. break;
  973. if (!is_cal_pci_dev(dev->device))
  974. continue;
  975. info = &bus_info[dev->bus->number];
  976. if (info->translation_disabled) {
  977. calgary_init_one_nontraslated(dev);
  978. continue;
  979. }
  980. if (!info->tce_space && !translate_empty_slots)
  981. continue;
  982. ret = calgary_init_one(dev);
  983. if (ret)
  984. goto error;
  985. } while (1);
  986. dev = NULL;
  987. for_each_pci_dev(dev) {
  988. struct iommu_table *tbl;
  989. tbl = find_iommu_table(&dev->dev);
  990. if (translation_enabled(tbl))
  991. dev->dev.archdata.dma_ops = &calgary_dma_ops;
  992. }
  993. return ret;
  994. error:
  995. do {
  996. dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
  997. if (!dev)
  998. break;
  999. if (!is_cal_pci_dev(dev->device))
  1000. continue;
  1001. info = &bus_info[dev->bus->number];
  1002. if (info->translation_disabled) {
  1003. pci_dev_put(dev);
  1004. continue;
  1005. }
  1006. if (!info->tce_space && !translate_empty_slots)
  1007. continue;
  1008. calgary_disable_translation(dev);
  1009. calgary_free_bus(dev);
  1010. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  1011. dev->dev.archdata.dma_ops = NULL;
  1012. } while (1);
  1013. return ret;
  1014. }
  1015. static inline int __init determine_tce_table_size(u64 ram)
  1016. {
  1017. int ret;
  1018. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  1019. return specified_table_size;
  1020. /*
  1021. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  1022. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  1023. * larger table size has twice as many entries, so shift the
  1024. * max ram address by 13 to divide by 8K and then look at the
  1025. * order of the result to choose between 0-7.
  1026. */
  1027. ret = get_order(ram >> 13);
  1028. if (ret > TCE_TABLE_SIZE_8M)
  1029. ret = TCE_TABLE_SIZE_8M;
  1030. return ret;
  1031. }
  1032. static int __init build_detail_arrays(void)
  1033. {
  1034. unsigned long ptr;
  1035. int i, scal_detail_size, rio_detail_size;
  1036. if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
  1037. printk(KERN_WARNING
  1038. "Calgary: MAX_NUMNODES too low! Defined as %d, "
  1039. "but system has %d nodes.\n",
  1040. MAX_NUMNODES, rio_table_hdr->num_scal_dev);
  1041. return -ENODEV;
  1042. }
  1043. switch (rio_table_hdr->version){
  1044. case 2:
  1045. scal_detail_size = 11;
  1046. rio_detail_size = 13;
  1047. break;
  1048. case 3:
  1049. scal_detail_size = 12;
  1050. rio_detail_size = 15;
  1051. break;
  1052. default:
  1053. printk(KERN_WARNING
  1054. "Calgary: Invalid Rio Grande Table Version: %d\n",
  1055. rio_table_hdr->version);
  1056. return -EPROTO;
  1057. }
  1058. ptr = ((unsigned long)rio_table_hdr) + 3;
  1059. for (i = 0; i < rio_table_hdr->num_scal_dev;
  1060. 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 get_tce_space_from_tar()
  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);