sun3dvma.c 6.9 KB

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