iommu.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 IOBMAP_PAGE_FACTOR (PAGE_SHIFT - IOBMAP_PAGE_SHIFT)
  30. #define IOB_BASE 0xe0000000
  31. #define IOB_SIZE 0x3000
  32. /* Configuration registers */
  33. #define IOBCAP_REG 0x10
  34. #define IOBCOM_REG 0x40
  35. /* Enable IOB address translation */
  36. #define IOBCOM_ATEN 0x00000100
  37. /* Address decode configuration register */
  38. #define IOB_AD_REG 0x53
  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 0x55
  53. /* Base of the 64 4-byte L1 registers */
  54. #define IOB_XLT_L1_REGBASE 0xac0
  55. /* Register to invalidate TLB entries */
  56. #define IOB_AT_INVAL_TLB_REG 0xb40
  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 u32 *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. {
  76. u32 *ip;
  77. u32 rpn;
  78. unsigned long bus_addr;
  79. pr_debug("iobmap: build at: %lx, %lx, addr: %lx\n", index, npages, uaddr);
  80. bus_addr = (tbl->it_offset + index) << PAGE_SHIFT;
  81. npages <<= IOBMAP_PAGE_FACTOR;
  82. index <<= IOBMAP_PAGE_FACTOR;
  83. ip = ((u32 *)tbl->it_base) + index;
  84. while (npages--) {
  85. rpn = virt_to_abs(uaddr) >> IOBMAP_PAGE_SHIFT;
  86. *(ip++) = IOBMAP_L2E_V | rpn;
  87. /* invalidate tlb, can be optimized more */
  88. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  89. uaddr += IOBMAP_PAGE_SIZE;
  90. bus_addr += IOBMAP_PAGE_SIZE;
  91. }
  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) << PAGE_SHIFT;
  100. npages <<= IOBMAP_PAGE_FACTOR;
  101. index <<= IOBMAP_PAGE_FACTOR;
  102. ip = ((u32 *)tbl->it_base) + index;
  103. while (npages--) {
  104. *(ip++) = iob_l2_emptyval;
  105. /* invalidate tlb, can be optimized more */
  106. out_le32(iob+IOB_AT_INVAL_TLB_REG, bus_addr >> 14);
  107. bus_addr += IOBMAP_PAGE_SIZE;
  108. }
  109. }
  110. static void iommu_table_iobmap_setup(void)
  111. {
  112. pr_debug(" -> %s\n", __func__);
  113. iommu_table_iobmap.it_busno = 0;
  114. iommu_table_iobmap.it_offset = 0;
  115. /* it_size is in number of entries */
  116. iommu_table_iobmap.it_size = 0x80000000 >> PAGE_SHIFT;
  117. /* Initialize the common IOMMU code */
  118. iommu_table_iobmap.it_base = (unsigned long)iob_l2_base;
  119. iommu_table_iobmap.it_index = 0;
  120. /* XXXOJN tune this to avoid IOB cache invals.
  121. * Should probably be 8 (64 bytes)
  122. */
  123. iommu_table_iobmap.it_blocksize = 4;
  124. iommu_init_table(&iommu_table_iobmap, 0);
  125. pr_debug(" <- %s\n", __func__);
  126. }
  127. static void pci_dma_bus_setup_pasemi(struct pci_bus *bus)
  128. {
  129. struct device_node *dn;
  130. pr_debug("pci_dma_bus_setup, bus %p, bus->self %p\n", bus, bus->self);
  131. if (!iommu_table_iobmap_inited) {
  132. iommu_table_iobmap_inited = 1;
  133. iommu_table_iobmap_setup();
  134. }
  135. dn = pci_bus_to_OF_node(bus);
  136. if (dn)
  137. PCI_DN(dn)->iommu_table = &iommu_table_iobmap;
  138. }
  139. static void pci_dma_dev_setup_pasemi(struct pci_dev *dev)
  140. {
  141. pr_debug("pci_dma_dev_setup, dev %p (%s)\n", dev, pci_name(dev));
  142. /* DMA device is untranslated, but all other PCI-e goes through
  143. * the IOMMU
  144. */
  145. if (dev->vendor == 0x1959 && dev->device == 0xa007)
  146. dev->dev.archdata.dma_ops = &dma_direct_ops;
  147. else
  148. dev->dev.archdata.dma_data = &iommu_table_iobmap;
  149. }
  150. static void pci_dma_bus_setup_null(struct pci_bus *b) { }
  151. static void pci_dma_dev_setup_null(struct pci_dev *d) { }
  152. int iob_init(struct device_node *dn)
  153. {
  154. unsigned long tmp;
  155. u32 regword;
  156. int i;
  157. pr_debug(" -> %s\n", __func__);
  158. /* Allocate a spare page to map all invalid IOTLB pages. */
  159. tmp = lmb_alloc(IOBMAP_PAGE_SIZE, IOBMAP_PAGE_SIZE);
  160. if (!tmp)
  161. panic("IOBMAP: Cannot allocate spare page!");
  162. /* Empty l1 is marked invalid */
  163. iob_l1_emptyval = 0;
  164. /* Empty l2 is mapped to dummy page */
  165. iob_l2_emptyval = IOBMAP_L2E_V | (tmp >> IOBMAP_PAGE_SHIFT);
  166. iob = ioremap(IOB_BASE, IOB_SIZE);
  167. if (!iob)
  168. panic("IOBMAP: Cannot map registers!");
  169. /* setup direct mapping of the L1 entries */
  170. for (i = 0; i < 64; i++) {
  171. /* Each L1 covers 32MB, i.e. 8K entries = 32K of ram */
  172. regword = IOBMAP_L1E_V | (__pa(iob_l2_base + i*0x2000) >> 12);
  173. out_le32(iob+IOB_XLT_L1_REGBASE+i, regword);
  174. }
  175. /* set 2GB translation window, based at 0 */
  176. regword = in_le32(iob+IOB_AD_REG);
  177. regword &= ~IOB_AD_TRNG_MASK;
  178. regword |= IOB_AD_TRNG_2G;
  179. out_le32(iob+IOB_AD_REG, regword);
  180. /* Enable translation */
  181. regword = in_le32(iob+IOBCOM_REG);
  182. regword |= IOBCOM_ATEN;
  183. out_le32(iob+IOBCOM_REG, regword);
  184. pr_debug(" <- %s\n", __func__);
  185. return 0;
  186. }
  187. /* These are called very early. */
  188. void iommu_init_early_pasemi(void)
  189. {
  190. int iommu_off;
  191. #ifndef CONFIG_PPC_PASEMI_IOMMU
  192. iommu_off = 1;
  193. #else
  194. iommu_off = of_chosen &&
  195. get_property(of_chosen, "linux,iommu-off", NULL);
  196. #endif
  197. if (iommu_off) {
  198. /* Direct I/O, IOMMU off */
  199. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_null;
  200. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_null;
  201. pci_dma_ops = &dma_direct_ops;
  202. return;
  203. }
  204. iob_init(NULL);
  205. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pasemi;
  206. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pasemi;
  207. ppc_md.tce_build = iobmap_build;
  208. ppc_md.tce_free = iobmap_free;
  209. pci_dma_ops = &dma_iommu_ops;
  210. }
  211. void __init alloc_iobmap_l2(void)
  212. {
  213. #ifndef CONFIG_PPC_PASEMI_IOMMU
  214. return;
  215. #endif
  216. /* For 2G space, 8x64 pages (2^21 bytes) is max total l2 size */
  217. iob_l2_base = (u32 *)abs_to_virt(lmb_alloc_base(1UL<<21, 1UL<<21, 0x80000000));
  218. printk(KERN_INFO "IOBMAP L2 allocated at: %p\n", iob_l2_base);
  219. }