pci-calgary_64.c 40 KB

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