intmem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. alloc = 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. alloc = kmalloc(sizeof *alloc, GFP_ATOMIC);
  56. alloc->status = STATUS_FREE;
  57. alloc->size = allocation->size - size -
  58. alignment;
  59. alloc->offset = allocation->offset + size +
  60. alignment;
  61. list_add(&alloc->entry, &allocation->entry);
  62. if (alignment) {
  63. struct intmem_allocation *tmp;
  64. tmp = kmalloc(sizeof *tmp, GFP_ATOMIC);
  65. tmp->offset = allocation->offset;
  66. tmp->size = alignment;
  67. tmp->status = STATUS_FREE;
  68. allocation->offset += alignment;
  69. list_add_tail(&tmp->entry,
  70. &allocation->entry);
  71. }
  72. }
  73. allocation->status = STATUS_ALLOCATED;
  74. allocation->size = size;
  75. ret = (void*)((int)intmem_virtual + allocation->offset);
  76. }
  77. }
  78. preempt_enable();
  79. return ret;
  80. }
  81. void crisv32_intmem_free(void* addr)
  82. {
  83. struct intmem_allocation* allocation;
  84. struct intmem_allocation* tmp;
  85. if (addr == NULL)
  86. return;
  87. preempt_disable();
  88. crisv32_intmem_init();
  89. list_for_each_entry_safe(allocation, tmp, &intmem_allocations, entry) {
  90. if (allocation->offset == (int)(addr - intmem_virtual)) {
  91. struct intmem_allocation *prev =
  92. list_entry(allocation->entry.prev,
  93. struct intmem_allocation, entry);
  94. struct intmem_allocation *next =
  95. list_entry(allocation->entry.next,
  96. struct intmem_allocation, entry);
  97. allocation->status = STATUS_FREE;
  98. /* Join with prev and/or next if also free */
  99. if ((prev != &intmem_allocations) &&
  100. (prev->status == STATUS_FREE)) {
  101. prev->size += allocation->size;
  102. list_del(&allocation->entry);
  103. kfree(allocation);
  104. allocation = prev;
  105. }
  106. if ((next != &intmem_allocations) &&
  107. (next->status == STATUS_FREE)) {
  108. allocation->size += next->size;
  109. list_del(&next->entry);
  110. kfree(next);
  111. }
  112. preempt_enable();
  113. return;
  114. }
  115. }
  116. preempt_enable();
  117. }
  118. void* crisv32_intmem_phys_to_virt(unsigned long addr)
  119. {
  120. return (void *)(addr - (MEM_INTMEM_START + RESERVED_SIZE) +
  121. (unsigned long)intmem_virtual);
  122. }
  123. unsigned long crisv32_intmem_virt_to_phys(void* addr)
  124. {
  125. return (unsigned long)((unsigned long )addr -
  126. (unsigned long)intmem_virtual + MEM_INTMEM_START +
  127. RESERVED_SIZE);
  128. }
  129. device_initcall(crisv32_intmem_init);