pci-calgary_64.c 40 KB

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