alloc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
  3. *
  4. * This work is licensed under the terms of the GNU LGPL, version 2.
  5. */
  6. #include "alloc.h"
  7. #include "asm/spinlock.h"
  8. #include "asm/io.h"
  9. #define PHYS_ALLOC_NR_REGIONS 256
  10. struct phys_alloc_region {
  11. phys_addr_t base;
  12. phys_addr_t size;
  13. };
  14. static struct phys_alloc_region regions[PHYS_ALLOC_NR_REGIONS];
  15. static int nr_regions;
  16. static struct spinlock lock;
  17. static phys_addr_t base, top, align_min;
  18. void phys_alloc_show(void)
  19. {
  20. int i;
  21. spin_lock(&lock);
  22. printf("phys_alloc minimum alignment: %#" PRIx64 "\n",
  23. (u64)align_min);
  24. for (i = 0; i < nr_regions; ++i)
  25. printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
  26. (u64)regions[i].base,
  27. (u64)(regions[i].base + regions[i].size - 1),
  28. "USED");
  29. printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
  30. (u64)base, (u64)(top - 1), "FREE");
  31. spin_unlock(&lock);
  32. }
  33. void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
  34. {
  35. spin_lock(&lock);
  36. base = base_addr;
  37. top = base + size;
  38. align_min = DEFAULT_MINIMUM_ALIGNMENT;
  39. nr_regions = 0;
  40. spin_unlock(&lock);
  41. }
  42. void phys_alloc_set_minimum_alignment(phys_addr_t align)
  43. {
  44. assert(align && !(align & (align - 1)));
  45. spin_lock(&lock);
  46. align_min = align;
  47. spin_unlock(&lock);
  48. }
  49. static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
  50. phys_addr_t align, bool safe)
  51. {
  52. static bool warned = false;
  53. phys_addr_t addr, size_orig = size;
  54. u64 top_safe;
  55. spin_lock(&lock);
  56. top_safe = top;
  57. if (safe && sizeof(long) == 4)
  58. top_safe = MIN(top_safe, 1ULL << 32);
  59. align = MAX(align, align_min);
  60. addr = ALIGN(base, align);
  61. size += addr - base;
  62. if ((top_safe - base) < size) {
  63. printf("phys_alloc: requested=%#" PRIx64
  64. " (align=%#" PRIx64 "), "
  65. "need=%#" PRIx64 ", but free=%#" PRIx64 ". "
  66. "top=%#" PRIx64 ", top_safe=%#" PRIx64 "\n",
  67. (u64)size_orig, (u64)align, (u64)size, top_safe - base,
  68. (u64)top, top_safe);
  69. spin_unlock(&lock);
  70. return INVALID_PHYS_ADDR;
  71. }
  72. base += size;
  73. if (nr_regions < PHYS_ALLOC_NR_REGIONS) {
  74. regions[nr_regions].base = addr;
  75. regions[nr_regions].size = size_orig;
  76. ++nr_regions;
  77. } else if (!warned) {
  78. printf("WARNING: phys_alloc: No free log entries, "
  79. "can no longer log allocations...\n");
  80. warned = true;
  81. }
  82. spin_unlock(&lock);
  83. return addr;
  84. }
  85. static phys_addr_t phys_zalloc_aligned_safe(phys_addr_t size,
  86. phys_addr_t align, bool safe)
  87. {
  88. phys_addr_t addr = phys_alloc_aligned_safe(size, align, safe);
  89. if (addr == INVALID_PHYS_ADDR)
  90. return addr;
  91. memset(phys_to_virt(addr), 0, size);
  92. return addr;
  93. }
  94. phys_addr_t phys_alloc_aligned(phys_addr_t size, phys_addr_t align)
  95. {
  96. return phys_alloc_aligned_safe(size, align, false);
  97. }
  98. phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align)
  99. {
  100. return phys_zalloc_aligned_safe(size, align, false);
  101. }
  102. phys_addr_t phys_alloc(phys_addr_t size)
  103. {
  104. return phys_alloc_aligned(size, align_min);
  105. }
  106. phys_addr_t phys_zalloc(phys_addr_t size)
  107. {
  108. return phys_zalloc_aligned(size, align_min);
  109. }
  110. static void *early_malloc(size_t size)
  111. {
  112. phys_addr_t addr = phys_alloc_aligned_safe(size, align_min, true);
  113. if (addr == INVALID_PHYS_ADDR)
  114. return NULL;
  115. return phys_to_virt(addr);
  116. }
  117. static void *early_calloc(size_t nmemb, size_t size)
  118. {
  119. phys_addr_t addr = phys_zalloc_aligned_safe(nmemb * size,
  120. align_min, true);
  121. if (addr == INVALID_PHYS_ADDR)
  122. return NULL;
  123. return phys_to_virt(addr);
  124. }
  125. static void early_free(void *ptr __unused)
  126. {
  127. }
  128. static void *early_memalign(size_t alignment, size_t size)
  129. {
  130. phys_addr_t addr;
  131. assert(alignment && !(alignment & (alignment - 1)));
  132. addr = phys_alloc_aligned_safe(size, alignment, true);
  133. if (addr == INVALID_PHYS_ADDR)
  134. return NULL;
  135. return phys_to_virt(addr);
  136. }
  137. static struct alloc_ops early_alloc_ops = {
  138. .malloc = early_malloc,
  139. .calloc = early_calloc,
  140. .free = early_free,
  141. .memalign = early_memalign,
  142. };
  143. struct alloc_ops *alloc_ops = &early_alloc_ops;