pci-calgary.c 40 KB

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