mem_detect.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright IBM Corp. 2008, 2009
  3. *
  4. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <asm/ipl.h>
  9. #include <asm/sclp.h>
  10. #include <asm/setup.h>
  11. #define ADDR2G (1ULL << 31)
  12. static void find_memory_chunks(struct mem_chunk chunk[])
  13. {
  14. unsigned long long memsize, rnmax, rzm;
  15. unsigned long addr = 0, size;
  16. int i = 0, type;
  17. rzm = sclp_get_rzm();
  18. rnmax = sclp_get_rnmax();
  19. memsize = rzm * rnmax;
  20. if (!rzm)
  21. rzm = 1ULL << 17;
  22. if (sizeof(long) == 4) {
  23. rzm = min(ADDR2G, rzm);
  24. memsize = memsize ? min(ADDR2G, memsize) : ADDR2G;
  25. }
  26. do {
  27. size = 0;
  28. type = tprot(addr);
  29. do {
  30. size += rzm;
  31. if (memsize && addr + size >= memsize)
  32. break;
  33. } while (type == tprot(addr + size));
  34. if (type == CHUNK_READ_WRITE || type == CHUNK_READ_ONLY) {
  35. chunk[i].addr = addr;
  36. chunk[i].size = size;
  37. chunk[i].type = type;
  38. i++;
  39. }
  40. addr += size;
  41. } while (addr < memsize && i < MEMORY_CHUNKS);
  42. }
  43. void detect_memory_layout(struct mem_chunk chunk[])
  44. {
  45. unsigned long flags, cr0;
  46. memset(chunk, 0, MEMORY_CHUNKS * sizeof(struct mem_chunk));
  47. /* Disable IRQs, DAT and low address protection so tprot does the
  48. * right thing and we don't get scheduled away with low address
  49. * protection disabled.
  50. */
  51. flags = __arch_local_irq_stnsm(0xf8);
  52. __ctl_store(cr0, 0, 0);
  53. __ctl_clear_bit(0, 28);
  54. find_memory_chunks(chunk);
  55. __ctl_load(cr0, 0, 0);
  56. arch_local_irq_restore(flags);
  57. }
  58. EXPORT_SYMBOL(detect_memory_layout);
  59. /*
  60. * Create memory hole with given address, size, and type
  61. */
  62. void create_mem_hole(struct mem_chunk chunks[], unsigned long addr,
  63. unsigned long size, int type)
  64. {
  65. unsigned long start, end, new_size;
  66. int i;
  67. for (i = 0; i < MEMORY_CHUNKS; i++) {
  68. if (chunks[i].size == 0)
  69. continue;
  70. if (addr + size < chunks[i].addr)
  71. continue;
  72. if (addr >= chunks[i].addr + chunks[i].size)
  73. continue;
  74. start = max(addr, chunks[i].addr);
  75. end = min(addr + size, chunks[i].addr + chunks[i].size);
  76. new_size = end - start;
  77. if (new_size == 0)
  78. continue;
  79. if (start == chunks[i].addr &&
  80. end == chunks[i].addr + chunks[i].size) {
  81. /* Remove chunk */
  82. chunks[i].type = type;
  83. } else if (start == chunks[i].addr) {
  84. /* Make chunk smaller at start */
  85. if (i >= MEMORY_CHUNKS - 1)
  86. panic("Unable to create memory hole");
  87. memmove(&chunks[i + 1], &chunks[i],
  88. sizeof(struct mem_chunk) *
  89. (MEMORY_CHUNKS - (i + 1)));
  90. chunks[i + 1].addr = chunks[i].addr + new_size;
  91. chunks[i + 1].size = chunks[i].size - new_size;
  92. chunks[i].size = new_size;
  93. chunks[i].type = type;
  94. i += 1;
  95. } else if (end == chunks[i].addr + chunks[i].size) {
  96. /* Make chunk smaller at end */
  97. if (i >= MEMORY_CHUNKS - 1)
  98. panic("Unable to create memory hole");
  99. memmove(&chunks[i + 1], &chunks[i],
  100. sizeof(struct mem_chunk) *
  101. (MEMORY_CHUNKS - (i + 1)));
  102. chunks[i + 1].addr = start;
  103. chunks[i + 1].size = new_size;
  104. chunks[i + 1].type = type;
  105. chunks[i].size -= new_size;
  106. i += 1;
  107. } else {
  108. /* Create memory hole */
  109. if (i >= MEMORY_CHUNKS - 2)
  110. panic("Unable to create memory hole");
  111. memmove(&chunks[i + 2], &chunks[i],
  112. sizeof(struct mem_chunk) *
  113. (MEMORY_CHUNKS - (i + 2)));
  114. chunks[i + 1].addr = addr;
  115. chunks[i + 1].size = size;
  116. chunks[i + 1].type = type;
  117. chunks[i + 2].addr = addr + size;
  118. chunks[i + 2].size =
  119. chunks[i].addr + chunks[i].size - (addr + size);
  120. chunks[i + 2].type = chunks[i].type;
  121. chunks[i].size = addr - chunks[i].addr;
  122. i += 2;
  123. }
  124. }
  125. }