intmem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Simple allocator for internal RAM in ETRAX FS
  3. *
  4. * Copyright (c) 2004 Axis Communications AB.
  5. */
  6. #include <linux/list.h>
  7. #include <linux/slab.h>
  8. #include <asm/io.h>
  9. #include <memmap.h>
  10. #define STATUS_FREE 0
  11. #define STATUS_ALLOCATED 1
  12. #ifdef CONFIG_ETRAX_L2CACHE
  13. #define RESERVED_SIZE 66*1024
  14. #else
  15. #define RESERVED_SIZE 0
  16. #endif
  17. struct intmem_allocation {
  18. struct list_head entry;
  19. unsigned int size;
  20. unsigned offset;
  21. char status;
  22. };
  23. static struct list_head intmem_allocations;
  24. static void* intmem_virtual;
  25. static void crisv32_intmem_init(void)
  26. {
  27. static int initiated = 0;
  28. if (!initiated) {
  29. struct intmem_allocation* alloc =
  30. (struct intmem_allocation*)kmalloc(sizeof *alloc, GFP_KERNEL);
  31. INIT_LIST_HEAD(&intmem_allocations);
  32. intmem_virtual = ioremap(MEM_INTMEM_START + RESERVED_SIZE,
  33. MEM_INTMEM_SIZE - RESERVED_SIZE);
  34. initiated = 1;
  35. alloc->size = MEM_INTMEM_SIZE - RESERVED_SIZE;
  36. alloc->offset = 0;
  37. alloc->status = STATUS_FREE;
  38. list_add_tail(&alloc->entry, &intmem_allocations);
  39. }
  40. }
  41. void* crisv32_intmem_alloc(unsigned size, unsigned align)
  42. {
  43. struct intmem_allocation* allocation;
  44. struct intmem_allocation* tmp;
  45. void* ret = NULL;
  46. preempt_disable();
  47. crisv32_intmem_init();
  48. list_for_each_entry_safe(allocation, tmp, &intmem_allocations, entry) {
  49. int alignment = allocation->offset % align;
  50. alignment = alignment ? align - alignment : alignment;
  51. if (allocation->status == STATUS_FREE &&
  52. allocation->size >= size + alignment) {
  53. if (allocation->size > size + alignment) {
  54. struct intmem_allocation* alloc =
  55. (struct intmem_allocation*)
  56. kmalloc(sizeof *alloc, GFP_ATOMIC);
  57. alloc->status = STATUS_FREE;
  58. alloc->size = allocation->size - size -
  59. alignment;
  60. alloc->offset = allocation->offset + size +
  61. alignment;
  62. list_add(&alloc->entry, &allocation->entry);
  63. if (alignment) {
  64. struct intmem_allocation *tmp;
  65. tmp = (struct intmem_allocation *)
  66. kmalloc(sizeof *tmp,
  67. GFP_ATOMIC);
  68. tmp->offset = allocation->offset;
  69. tmp->size = alignment;
  70. tmp->status = STATUS_FREE;
  71. allocation->offset += alignment;
  72. list_add_tail(&tmp->entry,
  73. &allocation->entry);
  74. }
  75. }
  76. allocation->status = STATUS_ALLOCATED;
  77. allocation->size = size;
  78. ret = (void*)((int)intmem_virtual + allocation->offset);
  79. }
  80. }
  81. preempt_enable();
  82. return ret;
  83. }
  84. void crisv32_intmem_free(void* addr)
  85. {
  86. struct intmem_allocation* allocation;
  87. struct intmem_allocation* tmp;
  88. if (addr == NULL)
  89. return;
  90. preempt_disable();
  91. crisv32_intmem_init();
  92. list_for_each_entry_safe(allocation, tmp, &intmem_allocations, entry) {
  93. if (allocation->offset == (int)(addr - intmem_virtual)) {
  94. struct intmem_allocation *prev =
  95. list_entry(allocation->entry.prev,
  96. struct intmem_allocation, entry);
  97. struct intmem_allocation *next =
  98. list_entry(allocation->entry.next,
  99. struct intmem_allocation, entry);
  100. allocation->status = STATUS_FREE;
  101. /* Join with prev and/or next if also free */
  102. if ((prev != &intmem_allocations) &&
  103. (prev->status == STATUS_FREE)) {
  104. prev->size += allocation->size;
  105. list_del(&allocation->entry);
  106. kfree(allocation);
  107. allocation = prev;
  108. }
  109. if ((next != &intmem_allocations) &&
  110. (next->status == STATUS_FREE)) {
  111. allocation->size += next->size;
  112. list_del(&next->entry);
  113. kfree(next);
  114. }
  115. preempt_enable();
  116. return;
  117. }
  118. }
  119. preempt_enable();
  120. }
  121. void* crisv32_intmem_phys_to_virt(unsigned long addr)
  122. {
  123. return (void *)(addr - (MEM_INTMEM_START + RESERVED_SIZE) +
  124. (unsigned long)intmem_virtual);
  125. }
  126. unsigned long crisv32_intmem_virt_to_phys(void* addr)
  127. {
  128. return (unsigned long)((unsigned long )addr -
  129. (unsigned long)intmem_virtual + MEM_INTMEM_START +
  130. RESERVED_SIZE);
  131. }
  132. module_init(crisv32_intmem_init);