sun3dvma.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/m68k/sun3/sun3dvma.c
  4. *
  5. * Copyright (C) 2000 Sam Creasey
  6. *
  7. * Contains common routines for sun3/sun3x DVMA management.
  8. */
  9. #include <linux/memblock.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/gfp.h>
  14. #include <linux/mm.h>
  15. #include <linux/list.h>
  16. #include <asm/page.h>
  17. #include <asm/dvma.h>
  18. #undef DVMA_DEBUG
  19. static unsigned long *iommu_use;
  20. #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
  21. #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
  22. struct hole {
  23. unsigned long start;
  24. unsigned long end;
  25. unsigned long size;
  26. struct list_head list;
  27. };
  28. static struct list_head hole_list;
  29. static struct list_head hole_cache;
  30. static struct hole initholes[64];
  31. #ifdef DVMA_DEBUG
  32. static unsigned long dvma_allocs;
  33. static unsigned long dvma_frees;
  34. static unsigned long long dvma_alloc_bytes;
  35. static unsigned long long dvma_free_bytes;
  36. static void print_use(void)
  37. {
  38. int i;
  39. int j = 0;
  40. pr_info("dvma entry usage:\n");
  41. for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
  42. if(!iommu_use[i])
  43. continue;
  44. j++;
  45. pr_info("dvma entry: %08x len %08lx\n",
  46. (i << DVMA_PAGE_SHIFT) + DVMA_START, iommu_use[i]);
  47. }
  48. pr_info("%d entries in use total\n", j);
  49. pr_info("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
  50. pr_info("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
  51. dvma_free_bytes);
  52. }
  53. static void print_holes(struct list_head *holes)
  54. {
  55. struct list_head *cur;
  56. struct hole *hole;
  57. pr_info("listing dvma holes\n");
  58. list_for_each(cur, holes) {
  59. hole = list_entry(cur, struct hole, list);
  60. if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
  61. continue;
  62. pr_info("hole: start %08lx end %08lx size %08lx\n",
  63. hole->start, hole->end, hole->size);
  64. }
  65. pr_info("end of hole listing...\n");
  66. }
  67. #endif /* DVMA_DEBUG */
  68. static inline int refill(void)
  69. {
  70. struct hole *hole;
  71. struct hole *prev = NULL;
  72. struct list_head *cur;
  73. int ret = 0;
  74. list_for_each(cur, &hole_list) {
  75. hole = list_entry(cur, struct hole, list);
  76. if(!prev) {
  77. prev = hole;
  78. continue;
  79. }
  80. if(hole->end == prev->start) {
  81. hole->size += prev->size;
  82. hole->end = prev->end;
  83. list_move(&(prev->list), &hole_cache);
  84. ret++;
  85. }
  86. }
  87. return ret;
  88. }
  89. static inline struct hole *rmcache(void)
  90. {
  91. struct hole *ret;
  92. if(list_empty(&hole_cache)) {
  93. if(!refill()) {
  94. pr_crit("out of dvma hole cache!\n");
  95. BUG();
  96. }
  97. }
  98. ret = list_entry(hole_cache.next, struct hole, list);
  99. list_del(&(ret->list));
  100. return ret;
  101. }
  102. static inline unsigned long get_baddr(int len, unsigned long align)
  103. {
  104. struct list_head *cur;
  105. struct hole *hole;
  106. if(list_empty(&hole_list)) {
  107. #ifdef DVMA_DEBUG
  108. pr_crit("out of dvma holes! (printing hole cache)\n");
  109. print_holes(&hole_cache);
  110. print_use();
  111. #endif
  112. BUG();
  113. }
  114. list_for_each(cur, &hole_list) {
  115. unsigned long newlen;
  116. hole = list_entry(cur, struct hole, list);
  117. if(align > DVMA_PAGE_SIZE)
  118. newlen = len + ((hole->end - len) & (align-1));
  119. else
  120. newlen = len;
  121. if(hole->size > newlen) {
  122. hole->end -= newlen;
  123. hole->size -= newlen;
  124. dvma_entry_use(hole->end) = newlen;
  125. #ifdef DVMA_DEBUG
  126. dvma_allocs++;
  127. dvma_alloc_bytes += newlen;
  128. #endif
  129. return hole->end;
  130. } else if(hole->size == newlen) {
  131. list_move(&(hole->list), &hole_cache);
  132. dvma_entry_use(hole->start) = newlen;
  133. #ifdef DVMA_DEBUG
  134. dvma_allocs++;
  135. dvma_alloc_bytes += newlen;
  136. #endif
  137. return hole->start;
  138. }
  139. }
  140. pr_crit("unable to find dvma hole!\n");
  141. BUG();
  142. return 0;
  143. }
  144. static inline int free_baddr(unsigned long baddr)
  145. {
  146. unsigned long len;
  147. struct hole *hole;
  148. struct list_head *cur;
  149. len = dvma_entry_use(baddr);
  150. dvma_entry_use(baddr) = 0;
  151. baddr &= DVMA_PAGE_MASK;
  152. dvma_unmap_iommu(baddr, len);
  153. #ifdef DVMA_DEBUG
  154. dvma_frees++;
  155. dvma_free_bytes += len;
  156. #endif
  157. list_for_each(cur, &hole_list) {
  158. hole = list_entry(cur, struct hole, list);
  159. if(hole->end == baddr) {
  160. hole->end += len;
  161. hole->size += len;
  162. return 0;
  163. } else if(hole->start == (baddr + len)) {
  164. hole->start = baddr;
  165. hole->size += len;
  166. return 0;
  167. }
  168. }
  169. hole = rmcache();
  170. hole->start = baddr;
  171. hole->end = baddr + len;
  172. hole->size = len;
  173. // list_add_tail(&(hole->list), cur);
  174. list_add(&(hole->list), cur);
  175. return 0;
  176. }
  177. void __init dvma_init(void)
  178. {
  179. struct hole *hole;
  180. int i;
  181. INIT_LIST_HEAD(&hole_list);
  182. INIT_LIST_HEAD(&hole_cache);
  183. /* prepare the hole cache */
  184. for(i = 0; i < 64; i++)
  185. list_add(&(initholes[i].list), &hole_cache);
  186. hole = rmcache();
  187. hole->start = DVMA_START;
  188. hole->end = DVMA_END;
  189. hole->size = DVMA_SIZE;
  190. list_add(&(hole->list), &hole_list);
  191. iommu_use = memblock_alloc(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long),
  192. SMP_CACHE_BYTES);
  193. if (!iommu_use)
  194. panic("%s: Failed to allocate %zu bytes\n", __func__,
  195. IOMMU_TOTAL_ENTRIES * sizeof(unsigned long));
  196. dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
  197. sun3_dvma_init();
  198. }
  199. unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
  200. {
  201. unsigned long baddr;
  202. unsigned long off;
  203. if(!len)
  204. len = 0x800;
  205. if(!kaddr || !len) {
  206. // pr_err("error: kaddr %lx len %x\n", kaddr, len);
  207. // *(int *)4 = 0;
  208. return 0;
  209. }
  210. pr_debug("dvma_map request %08x bytes from %08lx\n", len, kaddr);
  211. off = kaddr & ~DVMA_PAGE_MASK;
  212. kaddr &= PAGE_MASK;
  213. len += off;
  214. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  215. if(align == 0)
  216. align = DVMA_PAGE_SIZE;
  217. else
  218. align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  219. baddr = get_baddr(len, align);
  220. // pr_info("using baddr %lx\n", baddr);
  221. if(!dvma_map_iommu(kaddr, baddr, len))
  222. return (baddr + off);
  223. pr_crit("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr,
  224. len);
  225. BUG();
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(dvma_map_align);
  229. void dvma_unmap(void *baddr)
  230. {
  231. unsigned long addr;
  232. addr = (unsigned long)baddr;
  233. /* check if this is a vme mapping */
  234. if(!(addr & 0x00f00000))
  235. addr |= 0xf00000;
  236. free_baddr(addr);
  237. return;
  238. }
  239. EXPORT_SYMBOL(dvma_unmap);
  240. void *dvma_malloc_align(unsigned long len, unsigned long align)
  241. {
  242. unsigned long kaddr;
  243. unsigned long baddr;
  244. unsigned long vaddr;
  245. if(!len)
  246. return NULL;
  247. pr_debug("dvma_malloc request %lx bytes\n", len);
  248. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  249. if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
  250. return NULL;
  251. if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
  252. free_pages(kaddr, get_order(len));
  253. return NULL;
  254. }
  255. vaddr = dvma_btov(baddr);
  256. if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
  257. dvma_unmap((void *)baddr);
  258. free_pages(kaddr, get_order(len));
  259. return NULL;
  260. }
  261. pr_debug("mapped %08lx bytes %08lx kern -> %08lx bus\n", len, kaddr,
  262. baddr);
  263. return (void *)vaddr;
  264. }
  265. EXPORT_SYMBOL(dvma_malloc_align);
  266. void dvma_free(void *vaddr)
  267. {
  268. return;
  269. }
  270. EXPORT_SYMBOL(dvma_free);