io-unit.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * io-unit.c: IO-UNIT specific routines for memory management.
  4. *
  5. * Copyright (C) 1997,1998 Jakub Jelinek ([email protected])
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <linux/bitops.h>
  13. #include <linux/dma-map-ops.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <asm/io.h>
  17. #include <asm/io-unit.h>
  18. #include <asm/mxcc.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/dma.h>
  22. #include <asm/oplib.h>
  23. #include "mm_32.h"
  24. /* #define IOUNIT_DEBUG */
  25. #ifdef IOUNIT_DEBUG
  26. #define IOD(x) printk(x)
  27. #else
  28. #define IOD(x) do { } while (0)
  29. #endif
  30. #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
  31. #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
  32. static const struct dma_map_ops iounit_dma_ops;
  33. static void __init iounit_iommu_init(struct platform_device *op)
  34. {
  35. struct iounit_struct *iounit;
  36. iopte_t __iomem *xpt;
  37. iopte_t __iomem *xptend;
  38. iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
  39. if (!iounit) {
  40. prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
  41. prom_halt();
  42. }
  43. iounit->limit[0] = IOUNIT_BMAP1_START;
  44. iounit->limit[1] = IOUNIT_BMAP2_START;
  45. iounit->limit[2] = IOUNIT_BMAPM_START;
  46. iounit->limit[3] = IOUNIT_BMAPM_END;
  47. iounit->rotor[1] = IOUNIT_BMAP2_START;
  48. iounit->rotor[2] = IOUNIT_BMAPM_START;
  49. xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
  50. if (!xpt) {
  51. prom_printf("SUN4D: Cannot map External Page Table.");
  52. prom_halt();
  53. }
  54. op->dev.archdata.iommu = iounit;
  55. iounit->page_table = xpt;
  56. spin_lock_init(&iounit->lock);
  57. xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
  58. for (; xpt < xptend; xpt++)
  59. sbus_writel(0, xpt);
  60. op->dev.dma_ops = &iounit_dma_ops;
  61. }
  62. static int __init iounit_init(void)
  63. {
  64. extern void sun4d_init_sbi_irq(void);
  65. struct device_node *dp;
  66. for_each_node_by_name(dp, "sbi") {
  67. struct platform_device *op = of_find_device_by_node(dp);
  68. iounit_iommu_init(op);
  69. of_propagate_archdata(op);
  70. }
  71. sun4d_init_sbi_irq();
  72. return 0;
  73. }
  74. subsys_initcall(iounit_init);
  75. /* One has to hold iounit->lock to call this */
  76. static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
  77. {
  78. int i, j, k, npages;
  79. unsigned long rotor, scan, limit;
  80. iopte_t iopte;
  81. npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  82. /* A tiny bit of magic ingredience :) */
  83. switch (npages) {
  84. case 1: i = 0x0231; break;
  85. case 2: i = 0x0132; break;
  86. default: i = 0x0213; break;
  87. }
  88. IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
  89. next: j = (i & 15);
  90. rotor = iounit->rotor[j - 1];
  91. limit = iounit->limit[j];
  92. scan = rotor;
  93. nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
  94. if (scan + npages > limit) {
  95. if (limit != rotor) {
  96. limit = rotor;
  97. scan = iounit->limit[j - 1];
  98. goto nexti;
  99. }
  100. i >>= 4;
  101. if (!(i & 15))
  102. panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
  103. goto next;
  104. }
  105. for (k = 1, scan++; k < npages; k++)
  106. if (test_bit(scan++, iounit->bmap))
  107. goto nexti;
  108. iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
  109. scan -= npages;
  110. iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
  111. vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
  112. for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
  113. set_bit(scan, iounit->bmap);
  114. sbus_writel(iopte_val(iopte), &iounit->page_table[scan]);
  115. }
  116. IOD(("%08lx\n", vaddr));
  117. return vaddr;
  118. }
  119. static dma_addr_t iounit_map_page(struct device *dev, struct page *page,
  120. unsigned long offset, size_t len, enum dma_data_direction dir,
  121. unsigned long attrs)
  122. {
  123. void *vaddr = page_address(page) + offset;
  124. struct iounit_struct *iounit = dev->archdata.iommu;
  125. unsigned long ret, flags;
  126. /* XXX So what is maxphys for us and how do drivers know it? */
  127. if (!len || len > 256 * 1024)
  128. return DMA_MAPPING_ERROR;
  129. spin_lock_irqsave(&iounit->lock, flags);
  130. ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
  131. spin_unlock_irqrestore(&iounit->lock, flags);
  132. return ret;
  133. }
  134. static int iounit_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
  135. enum dma_data_direction dir, unsigned long attrs)
  136. {
  137. struct iounit_struct *iounit = dev->archdata.iommu;
  138. struct scatterlist *sg;
  139. unsigned long flags;
  140. int i;
  141. /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
  142. spin_lock_irqsave(&iounit->lock, flags);
  143. for_each_sg(sgl, sg, nents, i) {
  144. sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
  145. sg->dma_length = sg->length;
  146. }
  147. spin_unlock_irqrestore(&iounit->lock, flags);
  148. return nents;
  149. }
  150. static void iounit_unmap_page(struct device *dev, dma_addr_t vaddr, size_t len,
  151. enum dma_data_direction dir, unsigned long attrs)
  152. {
  153. struct iounit_struct *iounit = dev->archdata.iommu;
  154. unsigned long flags;
  155. spin_lock_irqsave(&iounit->lock, flags);
  156. len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  157. vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  158. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  159. for (len += vaddr; vaddr < len; vaddr++)
  160. clear_bit(vaddr, iounit->bmap);
  161. spin_unlock_irqrestore(&iounit->lock, flags);
  162. }
  163. static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl,
  164. int nents, enum dma_data_direction dir, unsigned long attrs)
  165. {
  166. struct iounit_struct *iounit = dev->archdata.iommu;
  167. unsigned long flags, vaddr, len;
  168. struct scatterlist *sg;
  169. int i;
  170. spin_lock_irqsave(&iounit->lock, flags);
  171. for_each_sg(sgl, sg, nents, i) {
  172. len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  173. vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
  174. IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
  175. for (len += vaddr; vaddr < len; vaddr++)
  176. clear_bit(vaddr, iounit->bmap);
  177. }
  178. spin_unlock_irqrestore(&iounit->lock, flags);
  179. }
  180. #ifdef CONFIG_SBUS
  181. static void *iounit_alloc(struct device *dev, size_t len,
  182. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  183. {
  184. struct iounit_struct *iounit = dev->archdata.iommu;
  185. unsigned long va, addr, page, end, ret;
  186. pgprot_t dvma_prot;
  187. iopte_t __iomem *iopte;
  188. /* XXX So what is maxphys for us and how do drivers know it? */
  189. if (!len || len > 256 * 1024)
  190. return NULL;
  191. len = PAGE_ALIGN(len);
  192. va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
  193. if (!va)
  194. return NULL;
  195. addr = ret = sparc_dma_alloc_resource(dev, len);
  196. if (!addr)
  197. goto out_free_pages;
  198. *dma_handle = addr;
  199. dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
  200. end = PAGE_ALIGN((addr + len));
  201. while(addr < end) {
  202. page = va;
  203. {
  204. pmd_t *pmdp;
  205. pte_t *ptep;
  206. long i;
  207. pmdp = pmd_off_k(addr);
  208. ptep = pte_offset_map(pmdp, addr);
  209. set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
  210. i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
  211. iopte = iounit->page_table + i;
  212. sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte);
  213. }
  214. addr += PAGE_SIZE;
  215. va += PAGE_SIZE;
  216. }
  217. flush_cache_all();
  218. flush_tlb_all();
  219. return (void *)ret;
  220. out_free_pages:
  221. free_pages(va, get_order(len));
  222. return NULL;
  223. }
  224. static void iounit_free(struct device *dev, size_t size, void *cpu_addr,
  225. dma_addr_t dma_addr, unsigned long attrs)
  226. {
  227. /* XXX Somebody please fill this in */
  228. }
  229. #endif
  230. static const struct dma_map_ops iounit_dma_ops = {
  231. #ifdef CONFIG_SBUS
  232. .alloc = iounit_alloc,
  233. .free = iounit_free,
  234. #endif
  235. .map_page = iounit_map_page,
  236. .unmap_page = iounit_unmap_page,
  237. .map_sg = iounit_map_sg,
  238. .unmap_sg = iounit_unmap_sg,
  239. };