pci-calgary.c 35 KB

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