pci-calgary_64.c 40 KB

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