sun3dvma.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * linux/arch/m68k/sun3/sun3dvma.c
  3. *
  4. * Copyright (C) 2000 Sam Creasey
  5. *
  6. * Contains common routines for sun3/sun3x DVMA management.
  7. */
  8. #include <linux/bootmem.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/gfp.h>
  13. #include <linux/mm.h>
  14. #include <linux/list.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable.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 = alloc_bootmem(IOMMU_TOTAL_ENTRIES * sizeof(unsigned long));
  204. dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
  205. #ifdef CONFIG_SUN3
  206. sun3_dvma_init();
  207. #endif
  208. }
  209. unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
  210. {
  211. unsigned long baddr;
  212. unsigned long off;
  213. if(!len)
  214. len = 0x800;
  215. if(!kaddr || !len) {
  216. // pr_err("error: kaddr %lx len %x\n", kaddr, len);
  217. // *(int *)4 = 0;
  218. return 0;
  219. }
  220. pr_debug("dvma_map request %08x bytes from %08lx\n", len, kaddr);
  221. off = kaddr & ~DVMA_PAGE_MASK;
  222. kaddr &= PAGE_MASK;
  223. len += off;
  224. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  225. if(align == 0)
  226. align = DVMA_PAGE_SIZE;
  227. else
  228. align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  229. baddr = get_baddr(len, align);
  230. // pr_info("using baddr %lx\n", baddr);
  231. if(!dvma_map_iommu(kaddr, baddr, len))
  232. return (baddr + off);
  233. pr_crit("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr,
  234. len);
  235. BUG();
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(dvma_map_align);
  239. void dvma_unmap(void *baddr)
  240. {
  241. unsigned long addr;
  242. addr = (unsigned long)baddr;
  243. /* check if this is a vme mapping */
  244. if(!(addr & 0x00f00000))
  245. addr |= 0xf00000;
  246. free_baddr(addr);
  247. return;
  248. }
  249. EXPORT_SYMBOL(dvma_unmap);
  250. void *dvma_malloc_align(unsigned long len, unsigned long align)
  251. {
  252. unsigned long kaddr;
  253. unsigned long baddr;
  254. unsigned long vaddr;
  255. if(!len)
  256. return NULL;
  257. pr_debug("dvma_malloc request %lx bytes\n", len);
  258. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  259. if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
  260. return NULL;
  261. if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
  262. free_pages(kaddr, get_order(len));
  263. return NULL;
  264. }
  265. vaddr = dvma_btov(baddr);
  266. if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
  267. dvma_unmap((void *)baddr);
  268. free_pages(kaddr, get_order(len));
  269. return NULL;
  270. }
  271. pr_debug("mapped %08lx bytes %08lx kern -> %08lx bus\n", len, kaddr,
  272. baddr);
  273. return (void *)vaddr;
  274. }
  275. EXPORT_SYMBOL(dvma_malloc_align);
  276. void dvma_free(void *vaddr)
  277. {
  278. return;
  279. }
  280. EXPORT_SYMBOL(dvma_free);