sun3dvma.c 6.7 KB

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