sun3dvma.c 6.8 KB

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