sun3dvma.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. printk("dvma entry usage:\n");
  51. for(i = 0; i < IOMMU_TOTAL_ENTRIES; i++) {
  52. if(!iommu_use[i])
  53. continue;
  54. j++;
  55. printk("dvma entry: %08lx len %08lx\n",
  56. ( i << DVMA_PAGE_SHIFT) + DVMA_START,
  57. iommu_use[i]);
  58. }
  59. printk("%d entries in use total\n", j);
  60. printk("allocation/free calls: %lu/%lu\n", dvma_allocs, dvma_frees);
  61. printk("allocation/free bytes: %Lx/%Lx\n", dvma_alloc_bytes,
  62. dvma_free_bytes);
  63. }
  64. static void print_holes(struct list_head *holes)
  65. {
  66. struct list_head *cur;
  67. struct hole *hole;
  68. printk("listing dvma holes\n");
  69. list_for_each(cur, holes) {
  70. hole = list_entry(cur, struct hole, list);
  71. if((hole->start == 0) && (hole->end == 0) && (hole->size == 0))
  72. continue;
  73. printk("hole: start %08lx end %08lx size %08lx\n", hole->start, hole->end, hole->size);
  74. }
  75. printk("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. printk("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. printk("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. printk("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. // printk("error: kaddr %lx len %x\n", kaddr, len);
  217. // *(int *)4 = 0;
  218. return 0;
  219. }
  220. #ifdef DEBUG
  221. printk("dvma_map request %08lx bytes from %08lx\n",
  222. len, kaddr);
  223. #endif
  224. off = kaddr & ~DVMA_PAGE_MASK;
  225. kaddr &= PAGE_MASK;
  226. len += off;
  227. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  228. if(align == 0)
  229. align = DVMA_PAGE_SIZE;
  230. else
  231. align = ((align + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  232. baddr = get_baddr(len, align);
  233. // printk("using baddr %lx\n", baddr);
  234. if(!dvma_map_iommu(kaddr, baddr, len))
  235. return (baddr + off);
  236. printk("dvma_map failed kaddr %lx baddr %lx len %x\n", kaddr, baddr, len);
  237. BUG();
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(dvma_map_align);
  241. void dvma_unmap(void *baddr)
  242. {
  243. unsigned long addr;
  244. addr = (unsigned long)baddr;
  245. /* check if this is a vme mapping */
  246. if(!(addr & 0x00f00000))
  247. addr |= 0xf00000;
  248. free_baddr(addr);
  249. return;
  250. }
  251. EXPORT_SYMBOL(dvma_unmap);
  252. void *dvma_malloc_align(unsigned long len, unsigned long align)
  253. {
  254. unsigned long kaddr;
  255. unsigned long baddr;
  256. unsigned long vaddr;
  257. if(!len)
  258. return NULL;
  259. #ifdef DEBUG
  260. printk("dvma_malloc request %lx bytes\n", len);
  261. #endif
  262. len = ((len + (DVMA_PAGE_SIZE-1)) & DVMA_PAGE_MASK);
  263. if((kaddr = __get_free_pages(GFP_ATOMIC, get_order(len))) == 0)
  264. return NULL;
  265. if((baddr = (unsigned long)dvma_map_align(kaddr, len, align)) == 0) {
  266. free_pages(kaddr, get_order(len));
  267. return NULL;
  268. }
  269. vaddr = dvma_btov(baddr);
  270. if(dvma_map_cpu(kaddr, vaddr, len) < 0) {
  271. dvma_unmap((void *)baddr);
  272. free_pages(kaddr, get_order(len));
  273. return NULL;
  274. }
  275. #ifdef DEBUG
  276. printk("mapped %08lx bytes %08lx kern -> %08lx bus\n",
  277. len, kaddr, baddr);
  278. #endif
  279. return (void *)vaddr;
  280. }
  281. EXPORT_SYMBOL(dvma_malloc_align);
  282. void dvma_free(void *vaddr)
  283. {
  284. return;
  285. }
  286. EXPORT_SYMBOL(dvma_free);