pci-calgary_64.c 40 KB

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