sun3dvma.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/config.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. 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_del(&(prev->list));
  91. list_add(&(prev->list), &hole_cache);
  92. ret++;
  93. }
  94. }
  95. return ret;
  96. }
  97. static inline struct hole *rmcache(void)
  98. {
  99. struct hole *ret;
  100. if(list_empty(&hole_cache)) {
  101. if(!refill()) {
  102. printk("out of dvma hole cache!\n");
  103. BUG();
  104. }
  105. }
  106. ret = list_entry(hole_cache.next, struct hole, list);
  107. list_del(&(ret->list));
  108. return ret;
  109. }
  110. static inline unsigned long get_baddr(int len, unsigned long align)
  111. {
  112. struct list_head *cur;
  113. struct hole *hole;
  114. if(list_empty(&hole_list)) {
  115. #ifdef DVMA_DEBUG
  116. printk("out of dvma holes! (printing hole cache)\n");
  117. print_holes(&hole_cache);
  118. print_use();
  119. #endif
  120. BUG();
  121. }
  122. list_for_each(cur, &hole_list) {
  123. unsigned long newlen;
  124. hole = list_entry(cur, struct hole, list);
  125. if(align > DVMA_PAGE_SIZE)
  126. newlen = len + ((hole->end - len) & (align-1));
  127. else
  128. newlen = len;
  129. if(hole->size > newlen) {
  130. hole->end -= newlen;
  131. hole->size -= newlen;
  132. dvma_entry_use(hole->end) = newlen;
  133. #ifdef DVMA_DEBUG
  134. dvma_allocs++;
  135. dvma_alloc_bytes += newlen;
  136. #endif
  137. return hole->end;
  138. } else if(hole->size == newlen) {
  139. list_del(&(hole->list));
  140. list_add(&(hole->list), &hole_cache);
  141. dvma_entry_use(hole->start) = newlen;
  142. #ifdef DVMA_DEBUG
  143. dvma_allocs++;
  144. dvma_alloc_bytes += newlen;
  145. #endif
  146. return hole->start;
  147. }
  148. }
  149. printk("unable to find dvma hole!\n");
  150. BUG();
  151. return 0;
  152. }
  153. static inline int free_baddr(unsigned long baddr)
  154. {
  155. unsigned long len;
  156. struct hole *hole;
  157. struct list_head *cur;
  158. unsigned long orig_baddr;
  159. orig_baddr = baddr;
  160. len = dvma_entry_use(baddr);
  161. dvma_entry_use(baddr) = 0;
  162. baddr &= DVMA_PAGE_MASK;
  163. dvma_unmap_iommu(baddr, len);
  164. #ifdef DVMA_DEBUG
  165. dvma_frees++;
  166. dvma_free_bytes += len;
  167. #endif
  168. list_for_each(cur, &hole_list) {
  169. hole = list_entry(cur, struct hole, list);
  170. if(hole->end == baddr) {
  171. hole->end += len;
  172. hole->size += len;
  173. return 0;
  174. } else if(hole->start == (baddr + len)) {
  175. hole->start = baddr;
  176. hole->size += len;
  177. return 0;
  178. }
  179. }
  180. hole = rmcache();
  181. hole->start = baddr;
  182. hole->end = baddr + len;
  183. hole->size = len;
  184. // list_add_tail(&(hole->list), cur);
  185. list_add(&(hole->list), cur);
  186. return 0;
  187. }
  188. void dvma_init(void)
  189. {
  190. struct hole *hole;
  191. int i;
  192. INIT_LIST_HEAD(&hole_list);
  193. INIT_LIST_HEAD(&hole_cache);
  194. /* prepare the hole cache */
  195. for(i = 0; i < 64; i++)
  196. list_add(&(initholes[i].list), &hole_cache);
  197. hole = rmcache();
  198. hole->start = DVMA_START;
  199. hole->end = DVMA_END;
  200. hole->size = DVMA_SIZE;
  201. list_add(&(hole->list), &hole_list);
  202. memset(iommu_use, 0, sizeof(iommu_use));
  203. dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
  204. #ifdef CONFIG_SUN3
  205. sun3_dvma_init();
  206. #endif
  207. }
  208. inline unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
  209. {
  210. unsigned long baddr;
  211. unsigned long off;
  212. if(!len)
  213. len = 0x800;
  214. if(!kaddr || !len) {
  215. // printk("error: kaddr %lx len %x\n", kaddr, len);
  216. // *(int *)4 = 0;
  217. return 0;
  218. }
  219. #ifdef DEBUG
  220. printk("dvma_map request %08lx bytes from %08lx\n",
  221. len, kaddr);
  222. #endif
  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. // printk("using baddr %lx\n", baddr);
  233. if(!dvma_map_iommu(kaddr, baddr, len))
  234. return (baddr + off);
  235. printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len);
  236. BUG();
  237. return 0;
  238. }
  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. 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. void dvma_free(void *vaddr)
  279. {
  280. return;
  281. }