sun3dvma.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/gfp.h>
  11. #include <linux/mm.h>
  12. #include <linux/list.h>
  13. #include <asm/page.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/dvma.h>
  16. #undef DVMA_DEBUG
  17. #ifdef CONFIG_SUN3X
  18. extern void dvma_unmap_iommu(unsigned long baddr, int len);
  19. #else
  20. static inline void dvma_unmap_iommu(unsigned long a, int b)
  21. {
  22. }
  23. #endif
  24. #ifdef CONFIG_SUN3
  25. extern void sun3_dvma_init(void);
  26. #endif
  27. static unsigned long iommu_use[IOMMU_TOTAL_ENTRIES];
  28. #define dvma_index(baddr) ((baddr - DVMA_START) >> DVMA_PAGE_SHIFT)
  29. #define dvma_entry_use(baddr) (iommu_use[dvma_index(baddr)])
  30. struct hole {
  31. unsigned long start;
  32. unsigned long end;
  33. unsigned long size;
  34. struct list_head list;
  35. };
  36. static struct list_head hole_list;
  37. static struct list_head hole_cache;
  38. static struct hole initholes[64];
  39. #ifdef DVMA_DEBUG
  40. static unsigned long dvma_allocs;
  41. static unsigned long dvma_frees;
  42. static unsigned long long dvma_alloc_bytes;
  43. static unsigned long long dvma_free_bytes;
  44. static void print_use(void)
  45. {
  46. int i;
  47. int j = 0;
  48. printk("dvma entry usage:\n");
  49. for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
  50. if(!iommu_use[i])
  51. continue;
  52. j++;
  53. printk("dvma entry: %08lx len %08lx\n",
  54. ( i << DVMA_PAGE_SHIFT) + DVMA_START,
  55. iommu_use[i]);
  56. }
  57. printk("%d entries in use total\n", j);
  58. printk("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
  59. printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
  60. dvma_free_bytes);
  61. }
  62. static void print_holes(struct list_head *holes)
  63. {
  64. struct list_head *cur;
  65. struct hole *hole;
  66. printk("listing dvma holes\n");
  67. list_for_each(cur, holes) {
  68. hole = list_entry(cur, struct hole, list);
  69. if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
  70. continue;
  71. printk("hole: start %08lx end %08lx size %08lx\n", hole->start, hole->end, hole->size);
  72. }
  73. printk("end of hole listing...\n");
  74. }
  75. #endif /* DVMA_DEBUG */
  76. static inline int refill(void)
  77. {
  78. struct hole *hole;
  79. struct hole *prev = NULL;
  80. struct list_head *cur;
  81. int ret = 0;
  82. list_for_each(cur, &hole_list) {
  83. hole = list_entry(cur, struct hole, list);
  84. if(!prev) {
  85. prev = hole;
  86. continue;
  87. }
  88. if(hole->end == prev->start) {
  89. hole->size += prev->size;
  90. hole->end = prev->end;
  91. list_move(&(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_move(&(hole->list), &hole_cache);
  140. dvma_entry_use(hole->start) = newlen;
  141. #ifdef DVMA_DEBUG
  142. dvma_allocs++;
  143. dvma_alloc_bytes += newlen;
  144. #endif
  145. return hole->start;
  146. }
  147. }
  148. printk("unable to find dvma hole!\n");
  149. BUG();
  150. return 0;
  151. }
  152. static inline int free_baddr(unsigned long baddr)
  153. {
  154. unsigned long len;
  155. struct hole *hole;
  156. struct list_head *cur;
  157. unsigned long orig_baddr;
  158. orig_baddr = baddr;
  159. len = dvma_entry_use(baddr);
  160. dvma_entry_use(baddr) = 0;
  161. baddr &= DVMA_PAGE_MASK;
  162. dvma_unmap_iommu(baddr, len);
  163. #ifdef DVMA_DEBUG
  164. dvma_frees++;
  165. dvma_free_bytes += len;
  166. #endif
  167. list_for_each(cur, &hole_list) {
  168. hole = list_entry(cur, struct hole, list);
  169. if(hole->end == baddr) {
  170. hole->end += len;
  171. hole->size += len;
  172. return 0;
  173. } else if(hole->start == (baddr + len)) {
  174. hole->start = baddr;
  175. hole->size += len;
  176. return 0;
  177. }
  178. }
  179. hole = rmcache();
  180. hole->start = baddr;
  181. hole->end = baddr + len;
  182. hole->size = len;
  183. // list_add_tail(&(hole->list), cur);
  184. list_add(&(hole->list), cur);
  185. return 0;
  186. }
  187. void dvma_init(void)
  188. {
  189. struct hole *hole;
  190. int i;
  191. INIT_LIST_HEAD(&hole_list);
  192. INIT_LIST_HEAD(&hole_cache);
  193. /* prepare the hole cache */
  194. for(i = 0; i < 64; i++)
  195. list_add(&(initholes[i].list), &hole_cache);
  196. hole = rmcache();
  197. hole->start = DVMA_START;
  198. hole->end = DVMA_END;
  199. hole->size = DVMA_SIZE;
  200. list_add(&(hole->list), &hole_list);
  201. memset(iommu_use, 0, sizeof(iommu_use));
  202. dvma_unmap_iommu(DVMA_START, DVMA_SIZE);
  203. #ifdef CONFIG_SUN3
  204. sun3_dvma_init();
  205. #endif
  206. }
  207. inline unsigned long dvma_map_align(unsigned long kaddr, int len, int align)
  208. {
  209. unsigned long baddr;
  210. unsigned long off;
  211. if(!len)
  212. len = 0x800;
  213. if(!kaddr || !len) {
  214. // printk("error: kaddr %lx len %x\n", kaddr, len);
  215. // *(int *)4 = 0;
  216. return 0;
  217. }
  218. #ifdef DEBUG
  219. printk("dvma_map request %08lx bytes from %08lx\n",
  220. len, kaddr);
  221. #endif
  222. off = kaddr & ~DVMA_PAGE_MASK;
  223. kaddr &= PAGE_MASK;
  224. len += off;
  225. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  226. if(align == 0)
  227. align = DVMA_PAGE_SIZE;
  228. else
  229. align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  230. baddr = get_baddr(len, align);
  231. // printk("using baddr %lx\n", baddr);
  232. if(!dvma_map_iommu(kaddr, baddr, len))
  233. return (baddr + off);
  234. printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, 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. #ifdef DEBUG
  258. printk("dvma_malloc request %lx bytes\n", len);
  259. #endif
  260. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  261. if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
  262. return NULL;
  263. if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
  264. free_pages(kaddr, get_order(len));
  265. return NULL;
  266. }
  267. vaddr = dvma_btov(baddr);
  268. if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
  269. dvma_unmap((void *)baddr);
  270. free_pages(kaddr, get_order(len));
  271. return NULL;
  272. }
  273. #ifdef DEBUG
  274. printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
  275. len, kaddr, baddr);
  276. #endif
  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);