ctvmem.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctvmem.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of virtual memory management object
  12. * for card device.
  13. *
  14. * @Author Liu Chun
  15. * @Date Apr 1 2008
  16. */
  17. #include "ctvmem.h"
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <asm/page.h> /* for PAGE_SIZE macro definition */
  21. #include <linux/io.h>
  22. #include <asm/pgtable.h>
  23. #define CT_PTES_PER_PAGE (PAGE_SIZE / sizeof(void *))
  24. #define CT_ADDRS_PER_PAGE (CT_PTES_PER_PAGE * PAGE_SIZE)
  25. /* *
  26. * Find or create vm block based on requested @size.
  27. * @size must be page aligned.
  28. * */
  29. static struct ct_vm_block *
  30. get_vm_block(struct ct_vm *vm, unsigned int size)
  31. {
  32. struct ct_vm_block *block = NULL, *entry = NULL;
  33. struct list_head *pos = NULL;
  34. mutex_lock(&vm->lock);
  35. list_for_each(pos, &vm->unused) {
  36. entry = list_entry(pos, struct ct_vm_block, list);
  37. if (entry->size >= size)
  38. break; /* found a block that is big enough */
  39. }
  40. if (pos == &vm->unused)
  41. goto out;
  42. if (entry->size == size) {
  43. /* Move the vm node from unused list to used list directly */
  44. list_del(&entry->list);
  45. list_add(&entry->list, &vm->used);
  46. vm->size -= size;
  47. block = entry;
  48. goto out;
  49. }
  50. block = kzalloc(sizeof(*block), GFP_KERNEL);
  51. if (NULL == block)
  52. goto out;
  53. block->addr = entry->addr;
  54. block->size = size;
  55. list_add(&block->list, &vm->used);
  56. entry->addr += size;
  57. entry->size -= size;
  58. vm->size -= size;
  59. out:
  60. mutex_unlock(&vm->lock);
  61. return block;
  62. }
  63. static void put_vm_block(struct ct_vm *vm, struct ct_vm_block *block)
  64. {
  65. struct ct_vm_block *entry = NULL, *pre_ent = NULL;
  66. struct list_head *pos = NULL, *pre = NULL;
  67. mutex_lock(&vm->lock);
  68. list_del(&block->list);
  69. vm->size += block->size;
  70. list_for_each(pos, &vm->unused) {
  71. entry = list_entry(pos, struct ct_vm_block, list);
  72. if (entry->addr >= (block->addr + block->size))
  73. break; /* found a position */
  74. }
  75. if (pos == &vm->unused) {
  76. list_add_tail(&block->list, &vm->unused);
  77. entry = block;
  78. } else {
  79. if ((block->addr + block->size) == entry->addr) {
  80. entry->addr = block->addr;
  81. entry->size += block->size;
  82. kfree(block);
  83. } else {
  84. __list_add(&block->list, pos->prev, pos);
  85. entry = block;
  86. }
  87. }
  88. pos = &entry->list;
  89. pre = pos->prev;
  90. while (pre != &vm->unused) {
  91. entry = list_entry(pos, struct ct_vm_block, list);
  92. pre_ent = list_entry(pre, struct ct_vm_block, list);
  93. if ((pre_ent->addr + pre_ent->size) > entry->addr)
  94. break;
  95. pre_ent->size += entry->size;
  96. list_del(pos);
  97. kfree(entry);
  98. pos = pre;
  99. pre = pos->prev;
  100. }
  101. mutex_unlock(&vm->lock);
  102. }
  103. /* Map host addr (kmalloced/vmalloced) to device logical addr. */
  104. static struct ct_vm_block *
  105. ct_vm_map(struct ct_vm *vm, void *host_addr, int size)
  106. {
  107. struct ct_vm_block *block = NULL;
  108. unsigned long pte_start;
  109. unsigned long i;
  110. unsigned long pages;
  111. unsigned long start_phys;
  112. unsigned long *ptp;
  113. /* do mapping */
  114. if ((unsigned long)host_addr >= VMALLOC_START) {
  115. printk(KERN_ERR "ctxfi: "
  116. "Fail! Not support vmalloced addr now!\n");
  117. return NULL;
  118. }
  119. if (size > vm->size) {
  120. printk(KERN_ERR "ctxfi: Fail! No sufficient device virtural "
  121. "memory space available!\n");
  122. return NULL;
  123. }
  124. start_phys = (virt_to_phys(host_addr) & PAGE_MASK);
  125. pages = (PAGE_ALIGN(virt_to_phys(host_addr) + size)
  126. - start_phys) >> PAGE_SHIFT;
  127. ptp = vm->ptp[0];
  128. block = get_vm_block(vm, (pages << PAGE_SHIFT));
  129. if (block == NULL) {
  130. printk(KERN_ERR "ctxfi: No virtual memory block that is big "
  131. "enough to allocate!\n");
  132. return NULL;
  133. }
  134. pte_start = (block->addr >> PAGE_SHIFT);
  135. for (i = 0; i < pages; i++)
  136. ptp[pte_start+i] = start_phys + (i << PAGE_SHIFT);
  137. block->addr += (virt_to_phys(host_addr) & (~PAGE_MASK));
  138. block->size = size;
  139. return block;
  140. }
  141. static void ct_vm_unmap(struct ct_vm *vm, struct ct_vm_block *block)
  142. {
  143. /* do unmapping */
  144. block->size = ((block->addr + block->size + PAGE_SIZE - 1)
  145. & PAGE_MASK) - (block->addr & PAGE_MASK);
  146. block->addr &= PAGE_MASK;
  147. put_vm_block(vm, block);
  148. }
  149. /* *
  150. * return the host (kmalloced) addr of the @index-th device
  151. * page talbe page on success, or NULL on failure.
  152. * The first returned NULL indicates the termination.
  153. * */
  154. static void *
  155. ct_get_ptp_virt(struct ct_vm *vm, int index)
  156. {
  157. void *addr;
  158. addr = (index >= CT_PTP_NUM) ? NULL : vm->ptp[index];
  159. return addr;
  160. }
  161. int ct_vm_create(struct ct_vm **rvm)
  162. {
  163. struct ct_vm *vm;
  164. struct ct_vm_block *block;
  165. int i;
  166. *rvm = NULL;
  167. vm = kzalloc(sizeof(*vm), GFP_KERNEL);
  168. if (NULL == vm)
  169. return -ENOMEM;
  170. mutex_init(&vm->lock);
  171. /* Allocate page table pages */
  172. for (i = 0; i < CT_PTP_NUM; i++) {
  173. vm->ptp[i] = kmalloc(PAGE_SIZE, GFP_KERNEL);
  174. if (NULL == vm->ptp[i])
  175. break;
  176. }
  177. if (!i) {
  178. /* no page table pages are allocated */
  179. kfree(vm);
  180. return -ENOMEM;
  181. }
  182. vm->size = CT_ADDRS_PER_PAGE * i;
  183. /* Initialise remaining ptps */
  184. for (; i < CT_PTP_NUM; i++)
  185. vm->ptp[i] = NULL;
  186. vm->map = ct_vm_map;
  187. vm->unmap = ct_vm_unmap;
  188. vm->get_ptp_virt = ct_get_ptp_virt;
  189. INIT_LIST_HEAD(&vm->unused);
  190. INIT_LIST_HEAD(&vm->used);
  191. block = kzalloc(sizeof(*block), GFP_KERNEL);
  192. if (NULL != block) {
  193. block->addr = 0;
  194. block->size = vm->size;
  195. list_add(&block->list, &vm->unused);
  196. }
  197. *rvm = vm;
  198. return 0;
  199. }
  200. /* The caller must ensure no mapping pages are being used
  201. * by hardware before calling this function */
  202. void ct_vm_destroy(struct ct_vm *vm)
  203. {
  204. int i;
  205. struct list_head *pos = NULL;
  206. struct ct_vm_block *entry = NULL;
  207. /* free used and unused list nodes */
  208. while (!list_empty(&vm->used)) {
  209. pos = vm->used.next;
  210. list_del(pos);
  211. entry = list_entry(pos, struct ct_vm_block, list);
  212. kfree(entry);
  213. }
  214. while (!list_empty(&vm->unused)) {
  215. pos = vm->unused.next;
  216. list_del(pos);
  217. entry = list_entry(pos, struct ct_vm_block, list);
  218. kfree(entry);
  219. }
  220. /* free allocated page table pages */
  221. for (i = 0; i < CT_PTP_NUM; i++)
  222. kfree(vm->ptp[i]);
  223. vm->size = 0;
  224. kfree(vm);
  225. }