pci-calgary_64.c 40 KB

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