iommu.c 6.8 KB

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