sun3dvma.c 7.0 KB

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