2 * Copyright IBM Corp. 2008, 2009
4 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
11 #include <asm/setup.h>
13 #define ADDR2G (1ULL << 31)
15 static void find_memory_chunks(struct mem_chunk chunk[])
17 unsigned long long memsize, rnmax, rzm;
18 unsigned long addr = 0, size;
22 rnmax = sclp_get_rnmax();
23 memsize = rzm * rnmax;
26 if (sizeof(long) == 4) {
27 rzm = min(ADDR2G, rzm);
28 memsize = memsize ? min(ADDR2G, memsize) : ADDR2G;
35 if (memsize && addr + size >= memsize)
37 } while (type == tprot(addr + size));
38 if (type == CHUNK_READ_WRITE || type == CHUNK_READ_ONLY) {
45 } while (addr < memsize && i < MEMORY_CHUNKS);
48 void detect_memory_layout(struct mem_chunk chunk[])
50 unsigned long flags, flags_dat, cr0;
52 memset(chunk, 0, MEMORY_CHUNKS * sizeof(struct mem_chunk));
53 /* Disable IRQs, DAT and low address protection so tprot does the
54 * right thing and we don't get scheduled away with low address
55 * protection disabled.
57 local_irq_save(flags);
58 flags_dat = __arch_local_irq_stnsm(0xfb);
59 __ctl_store(cr0, 0, 0);
60 __ctl_clear_bit(0, 28);
61 find_memory_chunks(chunk);
62 __ctl_load(cr0, 0, 0);
63 __arch_local_irq_ssm(flags_dat);
64 local_irq_restore(flags);
66 EXPORT_SYMBOL(detect_memory_layout);
69 * Move memory chunks array from index "from" to index "to"
71 static void mem_chunk_move(struct mem_chunk chunk[], int to, int from)
73 int cnt = MEMORY_CHUNKS - to;
75 memmove(&chunk[to], &chunk[from], cnt * sizeof(struct mem_chunk));
79 * Initialize memory chunk
81 static void mem_chunk_init(struct mem_chunk *chunk, unsigned long addr,
82 unsigned long size, int type)
90 * Create memory hole with given address, size, and type
92 void create_mem_hole(struct mem_chunk chunk[], unsigned long addr,
93 unsigned long size, int type)
95 unsigned long lh_start, lh_end, lh_size, ch_start, ch_end, ch_size;
98 for (i = 0; i < MEMORY_CHUNKS; i++) {
99 if (chunk[i].size == 0)
102 /* Define chunk properties */
103 ch_start = chunk[i].addr;
104 ch_size = chunk[i].size;
105 ch_end = ch_start + ch_size - 1;
106 ch_type = chunk[i].type;
108 /* Is memory chunk hit by memory hole? */
109 if (addr + size <= ch_start)
110 continue; /* No: memory hole in front of chunk */
112 continue; /* No: memory hole after chunk */
114 /* Yes: Define local hole properties */
115 lh_start = max(addr, chunk[i].addr);
116 lh_end = min(addr + size - 1, ch_end);
117 lh_size = lh_end - lh_start + 1;
119 if (lh_start == ch_start && lh_end == ch_end) {
120 /* Hole covers complete memory chunk */
121 mem_chunk_init(&chunk[i], lh_start, lh_size, type);
122 } else if (lh_end == ch_end) {
123 /* Hole starts in memory chunk and convers chunk end */
124 mem_chunk_move(chunk, i + 1, i);
125 mem_chunk_init(&chunk[i], ch_start, ch_size - lh_size,
127 mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
129 } else if (lh_start == ch_start) {
130 /* Hole ends in memory chunk */
131 mem_chunk_move(chunk, i + 1, i);
132 mem_chunk_init(&chunk[i], lh_start, lh_size, type);
133 mem_chunk_init(&chunk[i + 1], lh_end + 1,
134 ch_size - lh_size, ch_type);
137 /* Hole splits memory chunk */
138 mem_chunk_move(chunk, i + 2, i);
139 mem_chunk_init(&chunk[i], ch_start,
140 lh_start - ch_start, ch_type);
141 mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
142 mem_chunk_init(&chunk[i + 2], lh_end + 1,
143 ch_end - lh_end, ch_type);