pci-calgary_64.c 40 KB

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