pci-calgary.c 25 KB

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