/** * memblock_isolate_range - isolate given range into disjoint memblocks * @type: memblock type to isolate range for * @base: base of range to isolate * @size: size of range to isolate * @start_rgn: out parameter for the start of isolated region * @end_rgn: out parameter for the end of isolated region * * Walk @type and ensure that regions don't cross the boundaries defined by * [@base,@base+@size). Crossing regions are split at the boundaries, * which may create at most two more regions. The index of the first * region inside the range is returned in *@start_rgn and end in *@end_rgn. * * RETURNS: * 0 on success, -errno on failure. */ static int __init_memblock memblock_isolate_range(struct memblock_type *type, phys_addr_t base, phys_addr_t size, int *start_rgn, int *end_rgn) { phys_addr_t end = base + memblock_cap_size(base, &size); int i; *start_rgn = *end_rgn = 0; /* we'll create at most two more regions */ while (type->cnt + 2 > type->max) if (memblock_double_array(type) < 0) return -ENOMEM; for (i = 0; i < type->cnt; i++) { struct memblock_region *rgn = &type->regions[i]; phys_addr_t rbase = rgn->base; phys_addr_t rend = rbase + rgn->size; if (rbase >= end) break; if (rend <= base) continue; if (rbase < base) { /* * @rgn intersects from below. Split and continue * to process the next region - the new top half. */ rgn->base = base; rgn->size -= base - rbase; type->total_size -= base - rbase; memblock_insert_region(type, i, rbase, base - rbase, memblock_get_region_node(rgn)); } else if (rend > end) { /* * @rgn intersects from above. Split and redo the * current region - the new bottom half. */ rgn->base = end; rgn->size -= end - rbase; type->total_size -= end - rbase; memblock_insert_region(type, i--, rbase, end - rbase, memblock_get_region_node(rgn)); } else { /* @rgn is fully contained, record it */ if (!*end_rgn) *start_rgn = i; *end_rgn = i + 1; } } return 0; }
/** * __next_free_mem_range - next function for for_each_free_mem_range() * @idx: pointer to u64 loop variable * @nid: nid: node selector, %MAX_NUMNODES for all nodes * @p_start: ptr to phys_addr_t for start address of the range, can be %NULL * @p_end: ptr to phys_addr_t for end address of the range, can be %NULL * @p_nid: ptr to int for nid of the range, can be %NULL * * Find the first free area from *@idx which matches @nid, fill the out * parameters, and update *@idx for the next iteration. The lower 32bit of * *@idx contains index into memory region and the upper 32bit indexes the * areas before each reserved region. For example, if reserved regions * look like the following, * * 0:[0-16), 1:[32-48), 2:[128-130) * * The upper 32bit indexes the following regions. * * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) * * As both region arrays are sorted, the function advances the two indices * in lockstep and returns each intersection. */ void __init_memblock __next_free_mem_range(u64 *idx, int nid, phys_addr_t *out_start, phys_addr_t *out_end, int *out_nid) { struct memblock_type *mem = &memblock.memory; struct memblock_type *rsv = &memblock.reserved; unsigned int mi = *idx & 0xffffffff; unsigned int ri = *idx >> 32; for ( ; mi < mem->cnt; mi++) { struct memblock_region *m = &mem->regions[mi]; phys_addr_t m_start = m->base; phys_addr_t m_end = m->base + m->size; /* only memory regions are associated with nodes, check it */ if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m)) continue; /* scan areas before each reservation for intersection */ for ( ; ri < rsv->cnt + 1; ri++) { struct memblock_region *r = &rsv->regions[ri]; phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0; phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX; /* if ri advanced past mi, break out to advance mi */ if (r_start >= m_end) break; /* if the two regions intersect, we're done */ if (m_start < r_end) { if (out_start) *out_start = max(m_start, r_start); if (out_end) *out_end = min(m_end, r_end); if (out_nid) *out_nid = memblock_get_region_node(m); /* * The region which ends first is advanced * for the next iteration. */ if (m_end <= r_end) mi++; else ri++; *idx = (u32)mi | (u64)ri << 32; return; } } } /* signal end of iteration */ *idx = ULLONG_MAX; }
static void __init_memblock memblock_dump(struct memblock_type *type, char *name) { unsigned long long base, size; int i; pr_info(" %s.cnt = 0x%lx\n", name, type->cnt); for (i = 0; i < type->cnt; i++) { struct memblock_region *rgn = &type->regions[i]; char nid_buf[32] = ""; base = rgn->base; size = rgn->size; #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP if (memblock_get_region_node(rgn) != MAX_NUMNODES) snprintf(nid_buf, sizeof(nid_buf), " on node %d", memblock_get_region_node(rgn)); #endif pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s\n", name, i, base, base + size - 1, size, nid_buf); } }
/** * memblock_merge_regions - merge neighboring compatible regions * @type: memblock type to scan * * Scan @type and merge neighboring compatible regions. */ static void __init_memblock memblock_merge_regions(struct memblock_type *type) { int i = 0; /* cnt never goes below 1 */ while (i < type->cnt - 1) { struct memblock_region *this = &type->regions[i]; struct memblock_region *next = &type->regions[i + 1]; if (this->base + this->size != next->base || memblock_get_region_node(this) != memblock_get_region_node(next)) { BUG_ON(this->base + this->size > next->base); i++; continue; } this->size += next->size; memmove(next, next + 1, (type->cnt - (i + 1)) * sizeof(*next)); type->cnt--; } }
for_each_memblock(memory, reg) { int nid = memblock_get_region_node(reg); memory_present(nid, memblock_region_memory_base_pfn(reg), memblock_region_memory_end_pfn(reg)); }