pci-calgary_64.c 41 KB

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