pci-calgary_64.c 40 KB

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