iommu.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 void 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. }
  92. static void iobmap_free(struct iommu_table *tbl, long index,
  93. long npages)
  94. {
  95. u32 *ip;
  96. unsigned long bus_addr;
  97. pr_debug("iobmap: free at: %lx, %lx\n", index, npages);
  98. bus_addr = (tbl->it_offset + index) << IOBMAP_PAGE_SHIFT;
  99. ip = ((u32 *)tbl->it_base) + index;
  100. while (npages--) {
  101. *(ip++) = iob_l2_emptyval;
  102. /* invalidate tlb, can be optimized more */
  103. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  104. bus_addr += IOBMAP_PAGE_SIZE;
  105. }
  106. }
  107. static void iommu_table_iobmap_setup(void)
  108. {
  109. pr_debug(" -> %s\n", __func__);
  110. iommu_table_iobmap.it_busno = 0;
  111. iommu_table_iobmap.it_offset = 0;
  112. /* it_size is in number of entries */
  113. iommu_table_iobmap.it_size = 0x80000000 >> IOBMAP_PAGE_SHIFT;
  114. /* Initialize the common IOMMU code */
  115. iommu_table_iobmap.it_base = (unsigned long)iob_l2_base;
  116. iommu_table_iobmap.it_index = 0;
  117. /* XXXOJN tune this to avoid IOB cache invals.
  118. * Should probably be 8 (64 bytes)
  119. */
  120. iommu_table_iobmap.it_blocksize = 4;
  121. iommu_init_table(&iommu_table_iobmap, 0);
  122. pr_debug(" <- %s\n", __func__);
  123. }
  124. static void pci_dma_bus_setup_pasemi(struct pci_bus *bus)
  125. {
  126. struct device_node *dn;
  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. dn = pci_bus_to_OF_node(bus);
  133. if (dn)
  134. PCI_DN(dn)->iommu_table = &iommu_table_iobmap;
  135. }
  136. static void pci_dma_dev_setup_pasemi(struct pci_dev *dev)
  137. {
  138. pr_debug("pci_dma_dev_setup, dev %p (%s)\n", dev, pci_name(dev));
  139. #if !defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
  140. /* For non-LPAR environment, don't translate anything for the DMA
  141. * engine. The exception to this is if the user has enabled
  142. * CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE at build time.
  143. */
  144. if (dev->vendor == 0x1959 && dev->device == 0xa007 &&
  145. !firmware_has_feature(FW_FEATURE_LPAR)) {
  146. dev->dev.archdata.dma_ops = &dma_direct_ops;
  147. return;
  148. }
  149. #endif
  150. dev->dev.archdata.dma_data = &iommu_table_iobmap;
  151. }
  152. static void pci_dma_bus_setup_null(struct pci_bus *b) { }
  153. static void pci_dma_dev_setup_null(struct pci_dev *d) { }
  154. int __init iob_init(struct device_node *dn)
  155. {
  156. unsigned long tmp;
  157. u32 regword;
  158. int i;
  159. pr_debug(" -> %s\n", __func__);
  160. /* Allocate a spare page to map all invalid IOTLB pages. */
  161. tmp = lmb_alloc(IOBMAP_PAGE_SIZE, IOBMAP_PAGE_SIZE);
  162. if (!tmp)
  163. panic("IOBMAP: Cannot allocate spare page!");
  164. /* Empty l1 is marked invalid */
  165. iob_l1_emptyval = 0;
  166. /* Empty l2 is mapped to dummy page */
  167. iob_l2_emptyval = IOBMAP_L2E_V | (tmp >> IOBMAP_PAGE_SHIFT);
  168. iob = ioremap(IOB_BASE, IOB_SIZE);
  169. if (!iob)
  170. panic("IOBMAP: Cannot map registers!");
  171. /* setup direct mapping of the L1 entries */
  172. for (i = 0; i < 64; i++) {
  173. /* Each L1 covers 32MB, i.e. 8K entries = 32K of ram */
  174. regword = IOBMAP_L1E_V | (__pa(iob_l2_base + i*0x2000) >> 12);
  175. out_le32(iob+IOB_XLT_L1_REGBASE+i*4, regword);
  176. }
  177. /* set 2GB translation window, based at 0 */
  178. regword = in_le32(iob+IOB_AD_REG);
  179. regword &= ~IOB_AD_TRNG_MASK;
  180. regword |= IOB_AD_TRNG_2G;
  181. out_le32(iob+IOB_AD_REG, regword);
  182. /* Enable translation */
  183. regword = in_le32(iob+IOBCOM_REG);
  184. regword |= IOBCOM_ATEN;
  185. out_le32(iob+IOBCOM_REG, regword);
  186. pr_debug(" <- %s\n", __func__);
  187. return 0;
  188. }
  189. /* These are called very early. */
  190. void __init iommu_init_early_pasemi(void)
  191. {
  192. int iommu_off;
  193. #ifndef CONFIG_PPC_PASEMI_IOMMU
  194. iommu_off = 1;
  195. #else
  196. iommu_off = of_chosen &&
  197. of_get_property(of_chosen, "linux,iommu-off", NULL);
  198. #endif
  199. if (iommu_off) {
  200. /* Direct I/O, IOMMU off */
  201. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_null;
  202. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_null;
  203. set_pci_dma_ops(&dma_direct_ops);
  204. return;
  205. }
  206. iob_init(NULL);
  207. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pasemi;
  208. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pasemi;
  209. ppc_md.tce_build = iobmap_build;
  210. ppc_md.tce_free = iobmap_free;
  211. set_pci_dma_ops(&dma_iommu_ops);
  212. }
  213. void __init alloc_iobmap_l2(void)
  214. {
  215. #ifndef CONFIG_PPC_PASEMI_IOMMU
  216. return;
  217. #endif
  218. /* For 2G space, 8x64 pages (2^21 bytes) is max total l2 size */
  219. iob_l2_base = (u32 *)abs_to_virt(lmb_alloc_base(1UL<<21, 1UL<<21, 0x80000000));
  220. printk(KERN_INFO "IOBMAP L2 allocated at: %p\n", iob_l2_base);
  221. }