pci-calgary_64.c 40 KB

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