pci-calgary.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Derived from arch/powerpc/kernel/iommu.c
  3. *
  4. * Copyright (C) IBM Corporation, 2006
  5. *
  6. * Author: Jon Mason <jdmason@us.ibm.com>
  7. * Author: Muli Ben-Yehuda <muli@il.ibm.com>
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/config.h>
  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. #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
  43. #define PCI_VENDOR_DEVICE_ID_CALGARY \
  44. (PCI_VENDOR_ID_IBM | PCI_DEVICE_ID_IBM_CALGARY << 16)
  45. /* we need these for register space address calculation */
  46. #define START_ADDRESS 0xfe000000
  47. #define CHASSIS_BASE 0
  48. #define ONE_BASED_CHASSIS_NUM 1
  49. /* register offsets inside the host bridge space */
  50. #define PHB_CSR_OFFSET 0x0110
  51. #define PHB_PLSSR_OFFSET 0x0120
  52. #define PHB_CONFIG_RW_OFFSET 0x0160
  53. #define PHB_IOBASE_BAR_LOW 0x0170
  54. #define PHB_IOBASE_BAR_HIGH 0x0180
  55. #define PHB_MEM_1_LOW 0x0190
  56. #define PHB_MEM_1_HIGH 0x01A0
  57. #define PHB_IO_ADDR_SIZE 0x01B0
  58. #define PHB_MEM_1_SIZE 0x01C0
  59. #define PHB_MEM_ST_OFFSET 0x01D0
  60. #define PHB_AER_OFFSET 0x0200
  61. #define PHB_CONFIG_0_HIGH 0x0220
  62. #define PHB_CONFIG_0_LOW 0x0230
  63. #define PHB_CONFIG_0_END 0x0240
  64. #define PHB_MEM_2_LOW 0x02B0
  65. #define PHB_MEM_2_HIGH 0x02C0
  66. #define PHB_MEM_2_SIZE_HIGH 0x02D0
  67. #define PHB_MEM_2_SIZE_LOW 0x02E0
  68. #define PHB_DOSHOLE_OFFSET 0x08E0
  69. /* PHB_CONFIG_RW */
  70. #define PHB_TCE_ENABLE 0x20000000
  71. #define PHB_SLOT_DISABLE 0x1C000000
  72. #define PHB_DAC_DISABLE 0x01000000
  73. #define PHB_MEM2_ENABLE 0x00400000
  74. #define PHB_MCSR_ENABLE 0x00100000
  75. /* TAR (Table Address Register) */
  76. #define TAR_SW_BITS 0x0000ffffffff800fUL
  77. #define TAR_VALID 0x0000000000000008UL
  78. /* CSR (Channel/DMA Status Register) */
  79. #define CSR_AGENT_MASK 0xffe0ffff
  80. #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
  81. #define MAX_NUM_CHASSIS 8 /* max number of chassis */
  82. /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
  83. #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
  84. #define PHBS_PER_CALGARY 4
  85. /* register offsets in Calgary's internal register space */
  86. static const unsigned long tar_offsets[] = {
  87. 0x0580 /* TAR0 */,
  88. 0x0588 /* TAR1 */,
  89. 0x0590 /* TAR2 */,
  90. 0x0598 /* TAR3 */
  91. };
  92. static const unsigned long split_queue_offsets[] = {
  93. 0x4870 /* SPLIT QUEUE 0 */,
  94. 0x5870 /* SPLIT QUEUE 1 */,
  95. 0x6870 /* SPLIT QUEUE 2 */,
  96. 0x7870 /* SPLIT QUEUE 3 */
  97. };
  98. static const unsigned long phb_offsets[] = {
  99. 0x8000 /* PHB0 */,
  100. 0x9000 /* PHB1 */,
  101. 0xA000 /* PHB2 */,
  102. 0xB000 /* PHB3 */
  103. };
  104. unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
  105. static int translate_empty_slots __read_mostly = 0;
  106. static int calgary_detected __read_mostly = 0;
  107. struct calgary_bus_info {
  108. void *tce_space;
  109. unsigned char translation_disabled;
  110. signed char phbid;
  111. };
  112. static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
  113. static void tce_cache_blast(struct iommu_table *tbl);
  114. /* enable this to stress test the chip's TCE cache */
  115. #ifdef CONFIG_IOMMU_DEBUG
  116. int debugging __read_mostly = 1;
  117. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  118. int expected, unsigned long start, unsigned long end)
  119. {
  120. unsigned long idx = start;
  121. BUG_ON(start >= end);
  122. while (idx < end) {
  123. if (!!test_bit(idx, bitmap) != expected)
  124. return idx;
  125. ++idx;
  126. }
  127. /* all bits have the expected value */
  128. return ~0UL;
  129. }
  130. #else /* debugging is disabled */
  131. int debugging __read_mostly = 0;
  132. static inline unsigned long verify_bit_range(unsigned long* bitmap,
  133. int expected, unsigned long start, unsigned long end)
  134. {
  135. return ~0UL;
  136. }
  137. #endif /* CONFIG_IOMMU_DEBUG */
  138. static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
  139. {
  140. unsigned int npages;
  141. npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
  142. npages >>= PAGE_SHIFT;
  143. return npages;
  144. }
  145. static inline int translate_phb(struct pci_dev* dev)
  146. {
  147. int disabled = bus_info[dev->bus->number].translation_disabled;
  148. return !disabled;
  149. }
  150. static void iommu_range_reserve(struct iommu_table *tbl,
  151. unsigned long start_addr, unsigned int npages)
  152. {
  153. unsigned long index;
  154. unsigned long end;
  155. unsigned long badbit;
  156. index = start_addr >> PAGE_SHIFT;
  157. /* bail out if we're asked to reserve a region we don't cover */
  158. if (index >= tbl->it_size)
  159. return;
  160. end = index + npages;
  161. if (end > tbl->it_size) /* don't go off the table */
  162. end = tbl->it_size;
  163. badbit = verify_bit_range(tbl->it_map, 0, index, end);
  164. if (badbit != ~0UL) {
  165. if (printk_ratelimit())
  166. printk(KERN_ERR "Calgary: entry already allocated at "
  167. "0x%lx tbl %p dma 0x%lx npages %u\n",
  168. badbit, tbl, start_addr, npages);
  169. }
  170. set_bit_string(tbl->it_map, index, npages);
  171. }
  172. static unsigned long iommu_range_alloc(struct iommu_table *tbl,
  173. unsigned int npages)
  174. {
  175. unsigned long offset;
  176. BUG_ON(npages == 0);
  177. offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
  178. tbl->it_size, npages);
  179. if (offset == ~0UL) {
  180. tce_cache_blast(tbl);
  181. offset = find_next_zero_string(tbl->it_map, 0,
  182. tbl->it_size, npages);
  183. if (offset == ~0UL) {
  184. printk(KERN_WARNING "Calgary: IOMMU full.\n");
  185. if (panic_on_overflow)
  186. panic("Calgary: fix the allocator.\n");
  187. else
  188. return bad_dma_address;
  189. }
  190. }
  191. set_bit_string(tbl->it_map, offset, npages);
  192. tbl->it_hint = offset + npages;
  193. BUG_ON(tbl->it_hint > tbl->it_size);
  194. return offset;
  195. }
  196. static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
  197. unsigned int npages, int direction)
  198. {
  199. unsigned long entry, flags;
  200. dma_addr_t ret = bad_dma_address;
  201. spin_lock_irqsave(&tbl->it_lock, flags);
  202. entry = iommu_range_alloc(tbl, npages);
  203. if (unlikely(entry == bad_dma_address))
  204. goto error;
  205. /* set the return dma address */
  206. ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
  207. /* put the TCEs in the HW table */
  208. tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
  209. direction);
  210. spin_unlock_irqrestore(&tbl->it_lock, flags);
  211. return ret;
  212. error:
  213. spin_unlock_irqrestore(&tbl->it_lock, flags);
  214. printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
  215. "iommu %p\n", npages, tbl);
  216. return bad_dma_address;
  217. }
  218. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  219. unsigned int npages)
  220. {
  221. unsigned long entry;
  222. unsigned long badbit;
  223. entry = dma_addr >> PAGE_SHIFT;
  224. BUG_ON(entry + npages > tbl->it_size);
  225. tce_free(tbl, entry, npages);
  226. badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
  227. if (badbit != ~0UL) {
  228. if (printk_ratelimit())
  229. printk(KERN_ERR "Calgary: bit is off at 0x%lx "
  230. "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
  231. badbit, tbl, dma_addr, entry, npages);
  232. }
  233. __clear_bit_string(tbl->it_map, entry, npages);
  234. }
  235. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  236. unsigned int npages)
  237. {
  238. unsigned long flags;
  239. spin_lock_irqsave(&tbl->it_lock, flags);
  240. __iommu_free(tbl, dma_addr, npages);
  241. spin_unlock_irqrestore(&tbl->it_lock, flags);
  242. }
  243. static void __calgary_unmap_sg(struct iommu_table *tbl,
  244. struct scatterlist *sglist, int nelems, int direction)
  245. {
  246. while (nelems--) {
  247. unsigned int npages;
  248. dma_addr_t dma = sglist->dma_address;
  249. unsigned int dmalen = sglist->dma_length;
  250. if (dmalen == 0)
  251. break;
  252. npages = num_dma_pages(dma, dmalen);
  253. __iommu_free(tbl, dma, npages);
  254. sglist++;
  255. }
  256. }
  257. void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
  258. int nelems, int direction)
  259. {
  260. unsigned long flags;
  261. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  262. if (!translate_phb(to_pci_dev(dev)))
  263. return;
  264. spin_lock_irqsave(&tbl->it_lock, flags);
  265. __calgary_unmap_sg(tbl, sglist, nelems, direction);
  266. spin_unlock_irqrestore(&tbl->it_lock, flags);
  267. }
  268. static int calgary_nontranslate_map_sg(struct device* dev,
  269. struct scatterlist *sg, int nelems, int direction)
  270. {
  271. int i;
  272. for (i = 0; i < nelems; i++ ) {
  273. struct scatterlist *s = &sg[i];
  274. BUG_ON(!s->page);
  275. s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
  276. s->dma_length = s->length;
  277. }
  278. return nelems;
  279. }
  280. int calgary_map_sg(struct device *dev, struct scatterlist *sg,
  281. int nelems, int direction)
  282. {
  283. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  284. unsigned long flags;
  285. unsigned long vaddr;
  286. unsigned int npages;
  287. unsigned long entry;
  288. int i;
  289. if (!translate_phb(to_pci_dev(dev)))
  290. return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
  291. spin_lock_irqsave(&tbl->it_lock, flags);
  292. for (i = 0; i < nelems; i++ ) {
  293. struct scatterlist *s = &sg[i];
  294. BUG_ON(!s->page);
  295. vaddr = (unsigned long)page_address(s->page) + s->offset;
  296. npages = num_dma_pages(vaddr, s->length);
  297. entry = iommu_range_alloc(tbl, npages);
  298. if (entry == bad_dma_address) {
  299. /* makes sure unmap knows to stop */
  300. s->dma_length = 0;
  301. goto error;
  302. }
  303. s->dma_address = (entry << PAGE_SHIFT) | s->offset;
  304. /* insert into HW table */
  305. tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
  306. direction);
  307. s->dma_length = s->length;
  308. }
  309. spin_unlock_irqrestore(&tbl->it_lock, flags);
  310. return nelems;
  311. error:
  312. __calgary_unmap_sg(tbl, sg, nelems, direction);
  313. for (i = 0; i < nelems; i++) {
  314. sg[i].dma_address = bad_dma_address;
  315. sg[i].dma_length = 0;
  316. }
  317. spin_unlock_irqrestore(&tbl->it_lock, flags);
  318. return 0;
  319. }
  320. dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
  321. size_t size, int direction)
  322. {
  323. dma_addr_t dma_handle = bad_dma_address;
  324. unsigned long uaddr;
  325. unsigned int npages;
  326. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  327. uaddr = (unsigned long)vaddr;
  328. npages = num_dma_pages(uaddr, size);
  329. if (translate_phb(to_pci_dev(dev)))
  330. dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
  331. else
  332. dma_handle = virt_to_bus(vaddr);
  333. return dma_handle;
  334. }
  335. void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
  336. size_t size, int direction)
  337. {
  338. struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
  339. unsigned int npages;
  340. if (!translate_phb(to_pci_dev(dev)))
  341. return;
  342. npages = num_dma_pages(dma_handle, size);
  343. iommu_free(tbl, dma_handle, npages);
  344. }
  345. void* calgary_alloc_coherent(struct device *dev, size_t size,
  346. dma_addr_t *dma_handle, gfp_t flag)
  347. {
  348. void *ret = NULL;
  349. dma_addr_t mapping;
  350. unsigned int npages, order;
  351. struct iommu_table *tbl;
  352. tbl = to_pci_dev(dev)->bus->self->sysdata;
  353. size = PAGE_ALIGN(size); /* size rounded up to full pages */
  354. npages = size >> PAGE_SHIFT;
  355. order = get_order(size);
  356. /* alloc enough pages (and possibly more) */
  357. ret = (void *)__get_free_pages(flag, order);
  358. if (!ret)
  359. goto error;
  360. memset(ret, 0, size);
  361. if (translate_phb(to_pci_dev(dev))) {
  362. /* set up tces to cover the allocated range */
  363. mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
  364. if (mapping == bad_dma_address)
  365. goto free;
  366. *dma_handle = mapping;
  367. } else /* non translated slot */
  368. *dma_handle = virt_to_bus(ret);
  369. return ret;
  370. free:
  371. free_pages((unsigned long)ret, get_order(size));
  372. ret = NULL;
  373. error:
  374. return ret;
  375. }
  376. static struct dma_mapping_ops calgary_dma_ops = {
  377. .alloc_coherent = calgary_alloc_coherent,
  378. .map_single = calgary_map_single,
  379. .unmap_single = calgary_unmap_single,
  380. .map_sg = calgary_map_sg,
  381. .unmap_sg = calgary_unmap_sg,
  382. };
  383. static inline int busno_to_phbid(unsigned char num)
  384. {
  385. return bus_info[num].phbid;
  386. }
  387. static inline unsigned long split_queue_offset(unsigned char num)
  388. {
  389. size_t idx = busno_to_phbid(num);
  390. return split_queue_offsets[idx];
  391. }
  392. static inline unsigned long tar_offset(unsigned char num)
  393. {
  394. size_t idx = busno_to_phbid(num);
  395. return tar_offsets[idx];
  396. }
  397. static inline unsigned long phb_offset(unsigned char num)
  398. {
  399. size_t idx = busno_to_phbid(num);
  400. return phb_offsets[idx];
  401. }
  402. static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
  403. {
  404. unsigned long target = ((unsigned long)bar) | offset;
  405. return (void __iomem*)target;
  406. }
  407. static void tce_cache_blast(struct iommu_table *tbl)
  408. {
  409. u64 val;
  410. u32 aer;
  411. int i = 0;
  412. void __iomem *bbar = tbl->bbar;
  413. void __iomem *target;
  414. /* disable arbitration on the bus */
  415. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  416. aer = readl(target);
  417. writel(0, target);
  418. /* read plssr to ensure it got there */
  419. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
  420. val = readl(target);
  421. /* poll split queues until all DMA activity is done */
  422. target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
  423. do {
  424. val = readq(target);
  425. i++;
  426. } while ((val & 0xff) != 0xff && i < 100);
  427. if (i == 100)
  428. printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
  429. "continuing anyway\n");
  430. /* invalidate TCE cache */
  431. target = calgary_reg(bbar, tar_offset(tbl->it_busno));
  432. writeq(tbl->tar_val, target);
  433. /* enable arbitration */
  434. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
  435. writel(aer, target);
  436. (void)readl(target); /* flush */
  437. }
  438. static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
  439. u64 limit)
  440. {
  441. unsigned int numpages;
  442. limit = limit | 0xfffff;
  443. limit++;
  444. numpages = ((limit - start) >> PAGE_SHIFT);
  445. iommu_range_reserve(dev->sysdata, start, numpages);
  446. }
  447. static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
  448. {
  449. void __iomem *target;
  450. u64 low, high, sizelow;
  451. u64 start, limit;
  452. struct iommu_table *tbl = dev->sysdata;
  453. unsigned char busnum = dev->bus->number;
  454. void __iomem *bbar = tbl->bbar;
  455. /* peripheral MEM_1 region */
  456. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
  457. low = be32_to_cpu(readl(target));
  458. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
  459. high = be32_to_cpu(readl(target));
  460. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
  461. sizelow = be32_to_cpu(readl(target));
  462. start = (high << 32) | low;
  463. limit = sizelow;
  464. calgary_reserve_mem_region(dev, start, limit);
  465. }
  466. static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
  467. {
  468. void __iomem *target;
  469. u32 val32;
  470. u64 low, high, sizelow, sizehigh;
  471. u64 start, limit;
  472. struct iommu_table *tbl = dev->sysdata;
  473. unsigned char busnum = dev->bus->number;
  474. void __iomem *bbar = tbl->bbar;
  475. /* is it enabled? */
  476. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  477. val32 = be32_to_cpu(readl(target));
  478. if (!(val32 & PHB_MEM2_ENABLE))
  479. return;
  480. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
  481. low = be32_to_cpu(readl(target));
  482. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
  483. high = be32_to_cpu(readl(target));
  484. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
  485. sizelow = be32_to_cpu(readl(target));
  486. target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
  487. sizehigh = be32_to_cpu(readl(target));
  488. start = (high << 32) | low;
  489. limit = (sizehigh << 32) | sizelow;
  490. calgary_reserve_mem_region(dev, start, limit);
  491. }
  492. /*
  493. * some regions of the IO address space do not get translated, so we
  494. * must not give devices IO addresses in those regions. The regions
  495. * are the 640KB-1MB region and the two PCI peripheral memory holes.
  496. * Reserve all of them in the IOMMU bitmap to avoid giving them out
  497. * later.
  498. */
  499. static void __init calgary_reserve_regions(struct pci_dev *dev)
  500. {
  501. unsigned int npages;
  502. void __iomem *bbar;
  503. unsigned char busnum;
  504. u64 start;
  505. struct iommu_table *tbl = dev->sysdata;
  506. bbar = tbl->bbar;
  507. busnum = dev->bus->number;
  508. /* reserve bad_dma_address in case it's a legal address */
  509. iommu_range_reserve(tbl, bad_dma_address, 1);
  510. /* avoid the BIOS/VGA first 640KB-1MB region */
  511. start = (640 * 1024);
  512. npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
  513. iommu_range_reserve(tbl, start, npages);
  514. /* reserve the two PCI peripheral memory regions in IO space */
  515. calgary_reserve_peripheral_mem_1(dev);
  516. calgary_reserve_peripheral_mem_2(dev);
  517. }
  518. static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
  519. {
  520. u64 val64;
  521. u64 table_phys;
  522. void __iomem *target;
  523. int ret;
  524. struct iommu_table *tbl;
  525. /* build TCE tables for each PHB */
  526. ret = build_tce_table(dev, bbar);
  527. if (ret)
  528. return ret;
  529. tbl = dev->sysdata;
  530. tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
  531. tce_free(tbl, 0, tbl->it_size);
  532. calgary_reserve_regions(dev);
  533. /* set TARs for each PHB */
  534. target = calgary_reg(bbar, tar_offset(dev->bus->number));
  535. val64 = be64_to_cpu(readq(target));
  536. /* zero out all TAR bits under sw control */
  537. val64 &= ~TAR_SW_BITS;
  538. tbl = dev->sysdata;
  539. table_phys = (u64)__pa(tbl->it_base);
  540. val64 |= table_phys;
  541. BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
  542. val64 |= (u64) specified_table_size;
  543. tbl->tar_val = cpu_to_be64(val64);
  544. writeq(tbl->tar_val, target);
  545. readq(target); /* flush */
  546. return 0;
  547. }
  548. static void __init calgary_free_bus(struct pci_dev *dev)
  549. {
  550. u64 val64;
  551. struct iommu_table *tbl = dev->sysdata;
  552. void __iomem *target;
  553. unsigned int bitmapsz;
  554. target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
  555. val64 = be64_to_cpu(readq(target));
  556. val64 &= ~TAR_SW_BITS;
  557. writeq(cpu_to_be64(val64), target);
  558. readq(target); /* flush */
  559. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  560. free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
  561. tbl->it_map = NULL;
  562. kfree(tbl);
  563. dev->sysdata = NULL;
  564. /* Can't free bootmem allocated memory after system is up :-( */
  565. bus_info[dev->bus->number].tce_space = NULL;
  566. }
  567. static void calgary_watchdog(unsigned long data)
  568. {
  569. struct pci_dev *dev = (struct pci_dev *)data;
  570. struct iommu_table *tbl = dev->sysdata;
  571. void __iomem *bbar = tbl->bbar;
  572. u32 val32;
  573. void __iomem *target;
  574. target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
  575. val32 = be32_to_cpu(readl(target));
  576. /* If no error, the agent ID in the CSR is not valid */
  577. if (val32 & CSR_AGENT_MASK) {
  578. printk(KERN_EMERG "calgary_watchdog: DMA error on bus %d, "
  579. "CSR = %#x\n", dev->bus->number, val32);
  580. writel(0, target);
  581. /* Disable bus that caused the error */
  582. target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
  583. PHB_CONFIG_RW_OFFSET);
  584. val32 = be32_to_cpu(readl(target));
  585. val32 |= PHB_SLOT_DISABLE;
  586. writel(cpu_to_be32(val32), target);
  587. readl(target); /* flush */
  588. } else {
  589. /* Reset the timer */
  590. mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
  591. }
  592. }
  593. static void __init calgary_enable_translation(struct pci_dev *dev)
  594. {
  595. u32 val32;
  596. unsigned char busnum;
  597. void __iomem *target;
  598. void __iomem *bbar;
  599. struct iommu_table *tbl;
  600. busnum = dev->bus->number;
  601. tbl = dev->sysdata;
  602. bbar = tbl->bbar;
  603. /* enable TCE in PHB Config Register */
  604. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  605. val32 = be32_to_cpu(readl(target));
  606. val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
  607. printk(KERN_INFO "Calgary: enabling translation on PHB %d\n", busnum);
  608. printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
  609. "bus.\n");
  610. writel(cpu_to_be32(val32), target);
  611. readl(target); /* flush */
  612. init_timer(&tbl->watchdog_timer);
  613. tbl->watchdog_timer.function = &calgary_watchdog;
  614. tbl->watchdog_timer.data = (unsigned long)dev;
  615. mod_timer(&tbl->watchdog_timer, jiffies);
  616. }
  617. static void __init calgary_disable_translation(struct pci_dev *dev)
  618. {
  619. u32 val32;
  620. unsigned char busnum;
  621. void __iomem *target;
  622. void __iomem *bbar;
  623. struct iommu_table *tbl;
  624. busnum = dev->bus->number;
  625. tbl = dev->sysdata;
  626. bbar = tbl->bbar;
  627. /* disable TCE in PHB Config Register */
  628. target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
  629. val32 = be32_to_cpu(readl(target));
  630. val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
  631. printk(KERN_INFO "Calgary: disabling translation on PHB %d!\n", busnum);
  632. writel(cpu_to_be32(val32), target);
  633. readl(target); /* flush */
  634. del_timer_sync(&tbl->watchdog_timer);
  635. }
  636. static inline unsigned int __init locate_register_space(struct pci_dev *dev)
  637. {
  638. int rionodeid;
  639. u32 address;
  640. rionodeid = (dev->bus->number % 15 > 4) ? 3 : 2;
  641. /*
  642. * register space address calculation as follows:
  643. * FE0MB-8MB*OneBasedChassisNumber+1MB*(RioNodeId-ChassisBase)
  644. * ChassisBase is always zero for x366/x260/x460
  645. * RioNodeId is 2 for first Calgary, 3 for second Calgary
  646. */
  647. address = START_ADDRESS -
  648. (0x800000 * (ONE_BASED_CHASSIS_NUM + dev->bus->number / 15)) +
  649. (0x100000) * (rionodeid - CHASSIS_BASE);
  650. return address;
  651. }
  652. static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
  653. {
  654. pci_dev_get(dev);
  655. dev->sysdata = NULL;
  656. dev->bus->self = dev;
  657. }
  658. static int __init calgary_init_one(struct pci_dev *dev)
  659. {
  660. u32 address;
  661. void __iomem *bbar;
  662. int ret;
  663. address = locate_register_space(dev);
  664. /* map entire 1MB of Calgary config space */
  665. bbar = ioremap_nocache(address, 1024 * 1024);
  666. if (!bbar) {
  667. ret = -ENODATA;
  668. goto done;
  669. }
  670. ret = calgary_setup_tar(dev, bbar);
  671. if (ret)
  672. goto iounmap;
  673. pci_dev_get(dev);
  674. dev->bus->self = dev;
  675. calgary_enable_translation(dev);
  676. return 0;
  677. iounmap:
  678. iounmap(bbar);
  679. done:
  680. return ret;
  681. }
  682. static int __init calgary_init(void)
  683. {
  684. int i, ret = -ENODEV;
  685. struct pci_dev *dev = NULL;
  686. for (i = 0; i < MAX_PHB_BUS_NUM; i++) {
  687. dev = pci_get_device(PCI_VENDOR_ID_IBM,
  688. PCI_DEVICE_ID_IBM_CALGARY,
  689. dev);
  690. if (!dev)
  691. break;
  692. if (!translate_phb(dev)) {
  693. calgary_init_one_nontraslated(dev);
  694. continue;
  695. }
  696. if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
  697. continue;
  698. ret = calgary_init_one(dev);
  699. if (ret)
  700. goto error;
  701. }
  702. return ret;
  703. error:
  704. for (i--; i >= 0; i--) {
  705. dev = pci_find_device_reverse(PCI_VENDOR_ID_IBM,
  706. PCI_DEVICE_ID_IBM_CALGARY,
  707. dev);
  708. if (!dev)
  709. break;
  710. if (!translate_phb(dev)) {
  711. pci_dev_put(dev);
  712. continue;
  713. }
  714. if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
  715. continue;
  716. calgary_disable_translation(dev);
  717. calgary_free_bus(dev);
  718. pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
  719. }
  720. return ret;
  721. }
  722. static inline int __init determine_tce_table_size(u64 ram)
  723. {
  724. int ret;
  725. if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
  726. return specified_table_size;
  727. /*
  728. * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
  729. * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
  730. * larger table size has twice as many entries, so shift the
  731. * max ram address by 13 to divide by 8K and then look at the
  732. * order of the result to choose between 0-7.
  733. */
  734. ret = get_order(ram >> 13);
  735. if (ret > TCE_TABLE_SIZE_8M)
  736. ret = TCE_TABLE_SIZE_8M;
  737. return ret;
  738. }
  739. void __init detect_calgary(void)
  740. {
  741. u32 val;
  742. int bus;
  743. void *tbl;
  744. int calgary_found = 0;
  745. int phb = -1;
  746. /*
  747. * if the user specified iommu=off or iommu=soft or we found
  748. * another HW IOMMU already, bail out.
  749. */
  750. if (swiotlb || no_iommu || iommu_detected)
  751. return;
  752. if (!early_pci_allowed())
  753. return;
  754. specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
  755. for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
  756. int dev;
  757. struct calgary_bus_info *info = &bus_info[bus];
  758. info->phbid = -1;
  759. if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
  760. continue;
  761. /*
  762. * There are 4 PHBs per Calgary chip. Set phb to which phb (0-3)
  763. * it is connected to releative to the clagary chip.
  764. */
  765. phb = (phb + 1) % PHBS_PER_CALGARY;
  766. if (info->translation_disabled)
  767. continue;
  768. /*
  769. * Scan the slots of the PCI bus to see if there is a device present.
  770. * The parent bus will be the zero-ith device, so start at 1.
  771. */
  772. for (dev = 1; dev < 8; dev++) {
  773. val = read_pci_config(bus, dev, 0, 0);
  774. if (val != 0xffffffff || translate_empty_slots) {
  775. tbl = alloc_tce_table();
  776. if (!tbl)
  777. goto cleanup;
  778. info->tce_space = tbl;
  779. info->phbid = phb;
  780. calgary_found = 1;
  781. break;
  782. }
  783. }
  784. }
  785. if (calgary_found) {
  786. iommu_detected = 1;
  787. calgary_detected = 1;
  788. printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
  789. printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
  790. "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
  791. debugging ? "enabled" : "disabled");
  792. }
  793. return;
  794. cleanup:
  795. for (--bus; bus >= 0; --bus) {
  796. struct calgary_bus_info *info = &bus_info[bus];
  797. if (info->tce_space)
  798. free_tce_table(info->tce_space);
  799. }
  800. }
  801. int __init calgary_iommu_init(void)
  802. {
  803. int ret;
  804. if (no_iommu || swiotlb)
  805. return -ENODEV;
  806. if (!calgary_detected)
  807. return -ENODEV;
  808. /* ok, we're trying to use Calgary - let's roll */
  809. printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
  810. ret = calgary_init();
  811. if (ret) {
  812. printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
  813. "falling back to no_iommu\n", ret);
  814. if (end_pfn > MAX_DMA32_PFN)
  815. printk(KERN_ERR "WARNING more than 4GB of memory, "
  816. "32bit PCI may malfunction.\n");
  817. return ret;
  818. }
  819. force_iommu = 1;
  820. dma_ops = &calgary_dma_ops;
  821. return 0;
  822. }
  823. static int __init calgary_parse_options(char *p)
  824. {
  825. unsigned int bridge;
  826. size_t len;
  827. char* endp;
  828. while (*p) {
  829. if (!strncmp(p, "64k", 3))
  830. specified_table_size = TCE_TABLE_SIZE_64K;
  831. else if (!strncmp(p, "128k", 4))
  832. specified_table_size = TCE_TABLE_SIZE_128K;
  833. else if (!strncmp(p, "256k", 4))
  834. specified_table_size = TCE_TABLE_SIZE_256K;
  835. else if (!strncmp(p, "512k", 4))
  836. specified_table_size = TCE_TABLE_SIZE_512K;
  837. else if (!strncmp(p, "1M", 2))
  838. specified_table_size = TCE_TABLE_SIZE_1M;
  839. else if (!strncmp(p, "2M", 2))
  840. specified_table_size = TCE_TABLE_SIZE_2M;
  841. else if (!strncmp(p, "4M", 2))
  842. specified_table_size = TCE_TABLE_SIZE_4M;
  843. else if (!strncmp(p, "8M", 2))
  844. specified_table_size = TCE_TABLE_SIZE_8M;
  845. len = strlen("translate_empty_slots");
  846. if (!strncmp(p, "translate_empty_slots", len))
  847. translate_empty_slots = 1;
  848. len = strlen("disable");
  849. if (!strncmp(p, "disable", len)) {
  850. p += len;
  851. if (*p == '=')
  852. ++p;
  853. if (*p == '\0')
  854. break;
  855. bridge = simple_strtol(p, &endp, 0);
  856. if (p == endp)
  857. break;
  858. if (bridge < MAX_PHB_BUS_NUM) {
  859. printk(KERN_INFO "Calgary: disabling "
  860. "translation for PHB 0x%x\n", bridge);
  861. bus_info[bridge].translation_disabled = 1;
  862. }
  863. }
  864. p = strpbrk(p, ",");
  865. if (!p)
  866. break;
  867. p++; /* skip ',' */
  868. }
  869. return 1;
  870. }
  871. __setup("calgary=", calgary_parse_options);