iommu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2005-2007, 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. #define IOBMAP_PAGE_SHIFT 12
  27. #define IOBMAP_PAGE_SIZE (1 << IOBMAP_PAGE_SHIFT)
  28. #define IOBMAP_PAGE_MASK (IOBMAP_PAGE_SIZE - 1)
  29. #define IOB_BASE 0xe0000000
  30. #define IOB_SIZE 0x3000
  31. /* Configuration registers */
  32. #define IOBCAP_REG 0x10
  33. #define IOBCOM_REG 0x40
  34. /* Enable IOB address translation */
  35. #define IOBCOM_ATEN 0x00000100
  36. /* Address decode configuration register */
  37. #define IOB_AD_REG 0x53
  38. /* IOBCOM_AD_REG fields */
  39. #define IOB_AD_VGPRT 0x00000e00
  40. #define IOB_AD_VGAEN 0x00000100
  41. /* Direct mapping settings */
  42. #define IOB_AD_MPSEL_MASK 0x00000030
  43. #define IOB_AD_MPSEL_B38 0x00000000
  44. #define IOB_AD_MPSEL_B40 0x00000010
  45. #define IOB_AD_MPSEL_B42 0x00000020
  46. /* Translation window size / enable */
  47. #define IOB_AD_TRNG_MASK 0x00000003
  48. #define IOB_AD_TRNG_256M 0x00000000
  49. #define IOB_AD_TRNG_2G 0x00000001
  50. #define IOB_AD_TRNG_128G 0x00000003
  51. #define IOB_TABLEBASE_REG 0x55
  52. /* Base of the 64 4-byte L1 registers */
  53. #define IOB_XLT_L1_REGBASE 0xac0
  54. /* Register to invalidate TLB entries */
  55. #define IOB_AT_INVAL_TLB_REG 0xb40
  56. /* The top two bits of the level 1 entry contains valid and type flags */
  57. #define IOBMAP_L1E_V 0x40000000
  58. #define IOBMAP_L1E_V_B 0x80000000
  59. /* For big page entries, the bottom two bits contains flags */
  60. #define IOBMAP_L1E_BIG_CACHED 0x00000002
  61. #define IOBMAP_L1E_BIG_PRIORITY 0x00000001
  62. /* For regular level 2 entries, top 2 bits contain valid and cache flags */
  63. #define IOBMAP_L2E_V 0x80000000
  64. #define IOBMAP_L2E_V_CACHED 0xc0000000
  65. static u32 __iomem *iob;
  66. static u32 iob_l1_emptyval;
  67. static u32 iob_l2_emptyval;
  68. static u32 *iob_l2_base;
  69. static struct iommu_table iommu_table_iobmap;
  70. static int iommu_table_iobmap_inited;
  71. static void iobmap_build(struct iommu_table *tbl, long index,
  72. long npages, unsigned long uaddr,
  73. enum dma_data_direction direction)
  74. {
  75. u32 *ip;
  76. u32 rpn;
  77. unsigned long bus_addr;
  78. pr_debug("iobmap: build at: %lx, %lx, addr: %lx\n", index, npages, uaddr);
  79. bus_addr = (tbl->it_offset + index) << PAGE_SHIFT;
  80. ip = ((u32 *)tbl->it_base) + index;
  81. while (npages--) {
  82. rpn = virt_to_abs(uaddr) >> IOBMAP_PAGE_SHIFT;
  83. *(ip++) = IOBMAP_L2E_V | rpn;
  84. /* invalidate tlb, can be optimized more */
  85. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  86. uaddr += IOBMAP_PAGE_SIZE;
  87. bus_addr += IOBMAP_PAGE_SIZE;
  88. }
  89. }
  90. static void iobmap_free(struct iommu_table *tbl, long index,
  91. long npages)
  92. {
  93. u32 *ip;
  94. unsigned long bus_addr;
  95. pr_debug("iobmap: free at: %lx, %lx\n", index, npages);
  96. bus_addr = (tbl->it_offset + index) << PAGE_SHIFT;
  97. ip = ((u32 *)tbl->it_base) + index;
  98. while (npages--) {
  99. *(ip++) = iob_l2_emptyval;
  100. /* invalidate tlb, can be optimized more */
  101. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  102. bus_addr += IOBMAP_PAGE_SIZE;
  103. }
  104. }
  105. static void iommu_table_iobmap_setup(void)
  106. {
  107. pr_debug(" -> %s\n", __func__);
  108. iommu_table_iobmap.it_busno = 0;
  109. iommu_table_iobmap.it_offset = 0;
  110. /* it_size is in number of entries */
  111. iommu_table_iobmap.it_size = 0x80000000 >> PAGE_SHIFT;
  112. /* Initialize the common IOMMU code */
  113. iommu_table_iobmap.it_base = (unsigned long)iob_l2_base;
  114. iommu_table_iobmap.it_index = 0;
  115. /* XXXOJN tune this to avoid IOB cache invals.
  116. * Should probably be 8 (64 bytes)
  117. */
  118. iommu_table_iobmap.it_blocksize = 4;
  119. iommu_init_table(&iommu_table_iobmap, 0);
  120. pr_debug(" <- %s\n", __func__);
  121. }
  122. static void pci_dma_bus_setup_pasemi(struct pci_bus *bus)
  123. {
  124. struct device_node *dn;
  125. pr_debug("pci_dma_bus_setup, bus %p, bus->self %p\n", bus, bus->self);
  126. if (!iommu_table_iobmap_inited) {
  127. iommu_table_iobmap_inited = 1;
  128. iommu_table_iobmap_setup();
  129. }
  130. dn = pci_bus_to_OF_node(bus);
  131. if (dn)
  132. PCI_DN(dn)->iommu_table = &iommu_table_iobmap;
  133. }
  134. static void pci_dma_dev_setup_pasemi(struct pci_dev *dev)
  135. {
  136. pr_debug("pci_dma_dev_setup, dev %p (%s)\n", dev, pci_name(dev));
  137. /* DMA device is untranslated, but all other PCI-e goes through
  138. * the IOMMU
  139. */
  140. if (dev->vendor == 0x1959 && dev->device == 0xa007)
  141. dev->dev.archdata.dma_ops = &dma_direct_ops;
  142. else
  143. dev->dev.archdata.dma_data = &iommu_table_iobmap;
  144. }
  145. static void pci_dma_bus_setup_null(struct pci_bus *b) { }
  146. static void pci_dma_dev_setup_null(struct pci_dev *d) { }
  147. int iob_init(struct device_node *dn)
  148. {
  149. unsigned long tmp;
  150. u32 regword;
  151. int i;
  152. pr_debug(" -> %s\n", __func__);
  153. /* Allocate a spare page to map all invalid IOTLB pages. */
  154. tmp = lmb_alloc(IOBMAP_PAGE_SIZE, IOBMAP_PAGE_SIZE);
  155. if (!tmp)
  156. panic("IOBMAP: Cannot allocate spare page!");
  157. /* Empty l1 is marked invalid */
  158. iob_l1_emptyval = 0;
  159. /* Empty l2 is mapped to dummy page */
  160. iob_l2_emptyval = IOBMAP_L2E_V | (tmp >> IOBMAP_PAGE_SHIFT);
  161. iob = ioremap(IOB_BASE, IOB_SIZE);
  162. if (!iob)
  163. panic("IOBMAP: Cannot map registers!");
  164. /* setup direct mapping of the L1 entries */
  165. for (i = 0; i < 64; i++) {
  166. /* Each L1 covers 32MB, i.e. 8K entries = 32K of ram */
  167. regword = IOBMAP_L1E_V | (__pa(iob_l2_base + i*0x2000) >> 12);
  168. out_le32(iob+IOB_XLT_L1_REGBASE+i, regword);
  169. }
  170. /* set 2GB translation window, based at 0 */
  171. regword = in_le32(iob+IOB_AD_REG);
  172. regword &= ~IOB_AD_TRNG_MASK;
  173. regword |= IOB_AD_TRNG_2G;
  174. out_le32(iob+IOB_AD_REG, regword);
  175. /* Enable translation */
  176. regword = in_le32(iob+IOBCOM_REG);
  177. regword |= IOBCOM_ATEN;
  178. out_le32(iob+IOBCOM_REG, regword);
  179. pr_debug(" <- %s\n", __func__);
  180. return 0;
  181. }
  182. /* These are called very early. */
  183. void iommu_init_early_pasemi(void)
  184. {
  185. int iommu_off;
  186. #ifndef CONFIG_PPC_PASEMI_IOMMU
  187. iommu_off = 1;
  188. #else
  189. iommu_off = of_chosen &&
  190. of_get_property(of_chosen, "linux,iommu-off", NULL);
  191. #endif
  192. if (iommu_off) {
  193. /* Direct I/O, IOMMU off */
  194. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_null;
  195. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_null;
  196. set_pci_dma_ops(&dma_direct_ops);
  197. return;
  198. }
  199. iob_init(NULL);
  200. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pasemi;
  201. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pasemi;
  202. ppc_md.tce_build = iobmap_build;
  203. ppc_md.tce_free = iobmap_free;
  204. set_pci_dma_ops(&dma_iommu_ops);
  205. }
  206. void __init alloc_iobmap_l2(void)
  207. {
  208. #ifndef CONFIG_PPC_PASEMI_IOMMU
  209. return;
  210. #endif
  211. /* For 2G space, 8x64 pages (2^21 bytes) is max total l2 size */
  212. iob_l2_base = (u32 *)abs_to_virt(lmb_alloc_base(1UL<<21, 1UL<<21, 0x80000000));
  213. printk(KERN_INFO "IOBMAP L2 allocated at: %p\n", iob_l2_base);
  214. }