iommu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2005-2008, PA Semi, Inc
  3. *
  4. * Maintained by: Olof Johansson <olof@lixom.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #undef DEBUG
  20. #include <linux/types.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pci.h>
  23. #include <asm/iommu.h>
  24. #include <asm/machdep.h>
  25. #include <asm/abs_addr.h>
  26. #include <asm/firmware.h>
  27. #define IOBMAP_PAGE_SHIFT 12
  28. #define IOBMAP_PAGE_SIZE (1 << IOBMAP_PAGE_SHIFT)
  29. #define IOBMAP_PAGE_MASK (IOBMAP_PAGE_SIZE - 1)
  30. #define IOB_BASE 0xe0000000
  31. #define IOB_SIZE 0x3000
  32. /* Configuration registers */
  33. #define IOBCAP_REG 0x40
  34. #define IOBCOM_REG 0x100
  35. /* Enable IOB address translation */
  36. #define IOBCOM_ATEN 0x00000100
  37. /* Address decode configuration register */
  38. #define IOB_AD_REG 0x14c
  39. /* IOBCOM_AD_REG fields */
  40. #define IOB_AD_VGPRT 0x00000e00
  41. #define IOB_AD_VGAEN 0x00000100
  42. /* Direct mapping settings */
  43. #define IOB_AD_MPSEL_MASK 0x00000030
  44. #define IOB_AD_MPSEL_B38 0x00000000
  45. #define IOB_AD_MPSEL_B40 0x00000010
  46. #define IOB_AD_MPSEL_B42 0x00000020
  47. /* Translation window size / enable */
  48. #define IOB_AD_TRNG_MASK 0x00000003
  49. #define IOB_AD_TRNG_256M 0x00000000
  50. #define IOB_AD_TRNG_2G 0x00000001
  51. #define IOB_AD_TRNG_128G 0x00000003
  52. #define IOB_TABLEBASE_REG 0x154
  53. /* Base of the 64 4-byte L1 registers */
  54. #define IOB_XLT_L1_REGBASE 0x2b00
  55. /* Register to invalidate TLB entries */
  56. #define IOB_AT_INVAL_TLB_REG 0x2d00
  57. /* The top two bits of the level 1 entry contains valid and type flags */
  58. #define IOBMAP_L1E_V 0x40000000
  59. #define IOBMAP_L1E_V_B 0x80000000
  60. /* For big page entries, the bottom two bits contains flags */
  61. #define IOBMAP_L1E_BIG_CACHED 0x00000002
  62. #define IOBMAP_L1E_BIG_PRIORITY 0x00000001
  63. /* For regular level 2 entries, top 2 bits contain valid and cache flags */
  64. #define IOBMAP_L2E_V 0x80000000
  65. #define IOBMAP_L2E_V_CACHED 0xc0000000
  66. static void __iomem *iob;
  67. static u32 iob_l1_emptyval;
  68. static u32 iob_l2_emptyval;
  69. static u32 *iob_l2_base;
  70. static struct iommu_table iommu_table_iobmap;
  71. static int iommu_table_iobmap_inited;
  72. static int iobmap_build(struct iommu_table *tbl, long index,
  73. long npages, unsigned long uaddr,
  74. enum dma_data_direction direction,
  75. struct dma_attrs *attrs)
  76. {
  77. u32 *ip;
  78. u32 rpn;
  79. unsigned long bus_addr;
  80. pr_debug("iobmap: build at: %lx, %lx, addr: %lx\n", index, npages, uaddr);
  81. bus_addr = (tbl->it_offset + index) << IOBMAP_PAGE_SHIFT;
  82. ip = ((u32 *)tbl->it_base) + index;
  83. while (npages--) {
  84. rpn = virt_to_abs(uaddr) >> IOBMAP_PAGE_SHIFT;
  85. *(ip++) = IOBMAP_L2E_V | rpn;
  86. /* invalidate tlb, can be optimized more */
  87. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  88. uaddr += IOBMAP_PAGE_SIZE;
  89. bus_addr += IOBMAP_PAGE_SIZE;
  90. }
  91. return 0;
  92. }
  93. static void iobmap_free(struct iommu_table *tbl, long index,
  94. long npages)
  95. {
  96. u32 *ip;
  97. unsigned long bus_addr;
  98. pr_debug("iobmap: free at: %lx, %lx\n", index, npages);
  99. bus_addr = (tbl->it_offset + index) << IOBMAP_PAGE_SHIFT;
  100. ip = ((u32 *)tbl->it_base) + index;
  101. while (npages--) {
  102. *(ip++) = iob_l2_emptyval;
  103. /* invalidate tlb, can be optimized more */
  104. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  105. bus_addr += IOBMAP_PAGE_SIZE;
  106. }
  107. }
  108. static void iommu_table_iobmap_setup(void)
  109. {
  110. pr_debug(" -> %s\n", __func__);
  111. iommu_table_iobmap.it_busno = 0;
  112. iommu_table_iobmap.it_offset = 0;
  113. /* it_size is in number of entries */
  114. iommu_table_iobmap.it_size = 0x80000000 >> IOBMAP_PAGE_SHIFT;
  115. /* Initialize the common IOMMU code */
  116. iommu_table_iobmap.it_base = (unsigned long)iob_l2_base;
  117. iommu_table_iobmap.it_index = 0;
  118. /* XXXOJN tune this to avoid IOB cache invals.
  119. * Should probably be 8 (64 bytes)
  120. */
  121. iommu_table_iobmap.it_blocksize = 4;
  122. iommu_init_table(&iommu_table_iobmap, 0);
  123. pr_debug(" <- %s\n", __func__);
  124. }
  125. static void pci_dma_bus_setup_pasemi(struct pci_bus *bus)
  126. {
  127. pr_debug("pci_dma_bus_setup, bus %p, bus->self %p\n", bus, bus->self);
  128. if (!iommu_table_iobmap_inited) {
  129. iommu_table_iobmap_inited = 1;
  130. iommu_table_iobmap_setup();
  131. }
  132. }
  133. static void pci_dma_dev_setup_pasemi(struct pci_dev *dev)
  134. {
  135. pr_debug("pci_dma_dev_setup, dev %p (%s)\n", dev, pci_name(dev));
  136. #if !defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
  137. /* For non-LPAR environment, don't translate anything for the DMA
  138. * engine. The exception to this is if the user has enabled
  139. * CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE at build time.
  140. */
  141. if (dev->vendor == 0x1959 && dev->device == 0xa007 &&
  142. !firmware_has_feature(FW_FEATURE_LPAR)) {
  143. dev->dev.archdata.dma_ops = &dma_direct_ops;
  144. return;
  145. }
  146. #endif
  147. set_iommu_table_base(&dev->dev, &iommu_table_iobmap);
  148. }
  149. int __init iob_init(struct device_node *dn)
  150. {
  151. unsigned long tmp;
  152. u32 regword;
  153. int i;
  154. pr_debug(" -> %s\n", __func__);
  155. /* Allocate a spare page to map all invalid IOTLB pages. */
  156. tmp = memblock_alloc(IOBMAP_PAGE_SIZE, IOBMAP_PAGE_SIZE);
  157. if (!tmp)
  158. panic("IOBMAP: Cannot allocate spare page!");
  159. /* Empty l1 is marked invalid */
  160. iob_l1_emptyval = 0;
  161. /* Empty l2 is mapped to dummy page */
  162. iob_l2_emptyval = IOBMAP_L2E_V | (tmp >> IOBMAP_PAGE_SHIFT);
  163. iob = ioremap(IOB_BASE, IOB_SIZE);
  164. if (!iob)
  165. panic("IOBMAP: Cannot map registers!");
  166. /* setup direct mapping of the L1 entries */
  167. for (i = 0; i < 64; i++) {
  168. /* Each L1 covers 32MB, i.e. 8K entries = 32K of ram */
  169. regword = IOBMAP_L1E_V | (__pa(iob_l2_base + i*0x2000) >> 12);
  170. out_le32(iob+IOB_XLT_L1_REGBASE+i*4, regword);
  171. }
  172. /* set 2GB translation window, based at 0 */
  173. regword = in_le32(iob+IOB_AD_REG);
  174. regword &= ~IOB_AD_TRNG_MASK;
  175. regword |= IOB_AD_TRNG_2G;
  176. out_le32(iob+IOB_AD_REG, regword);
  177. /* Enable translation */
  178. regword = in_le32(iob+IOBCOM_REG);
  179. regword |= IOBCOM_ATEN;
  180. out_le32(iob+IOBCOM_REG, regword);
  181. pr_debug(" <- %s\n", __func__);
  182. return 0;
  183. }
  184. /* These are called very early. */
  185. void __init iommu_init_early_pasemi(void)
  186. {
  187. int iommu_off;
  188. #ifndef CONFIG_PPC_PASEMI_IOMMU
  189. iommu_off = 1;
  190. #else
  191. iommu_off = of_chosen &&
  192. of_get_property(of_chosen, "linux,iommu-off", NULL);
  193. #endif
  194. if (iommu_off)
  195. return;
  196. iob_init(NULL);
  197. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pasemi;
  198. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pasemi;
  199. ppc_md.tce_build = iobmap_build;
  200. ppc_md.tce_free = iobmap_free;
  201. set_pci_dma_ops(&dma_iommu_ops);
  202. }
  203. void __init alloc_iobmap_l2(void)
  204. {
  205. #ifndef CONFIG_PPC_PASEMI_IOMMU
  206. return;
  207. #endif
  208. /* For 2G space, 8x64 pages (2^21 bytes) is max total l2 size */
  209. iob_l2_base = (u32 *)abs_to_virt(memblock_alloc_base(1UL<<21, 1UL<<21, 0x80000000));
  210. printk(KERN_INFO "IOBMAP L2 allocated at: %p\n", iob_l2_base);
  211. }