pci-calgary_64.c 42 KB

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