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