1ccc34028d46230f715eeda4c8cce27e919934fadDavid S. Miller/* memory.c: Prom routine for acquiring various bits of information
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *           about RAM on the machine, both virtual and physical.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
49f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 1997 Michael A. Griffith (grif@acm.org)
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
99f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller#include <linux/sort.h>
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/openprom.h>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/oplib.h>
149f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller#include <asm/page.h>
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
169f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Millerstatic int __init prom_meminit_v0(void)
179f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller{
189f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	struct linux_mlist_v0 *p;
199f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	int index;
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
219f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	index = 0;
229f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	for (p = *(romvec->pv_v0mem.v0_available); p; p = p->theres_more) {
239f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		sp_banks[index].base_addr = (unsigned long) p->start_adr;
249f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		sp_banks[index].num_bytes = p->num_bytes;
259f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		index++;
269f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	}
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
289f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	return index;
299f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller}
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
319f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Millerstatic int __init prom_meminit_v2(void)
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
339f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	struct linux_prom_registers reg[64];
348d1255627d4ce9cb4b9d0a1c44b6c18d92e84a99Andres Salomon	phandle node;
358d1255627d4ce9cb4b9d0a1c44b6c18d92e84a99Andres Salomon	int size, num_ents, i;
369f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
379f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	node = prom_searchsiblings(prom_getchild(prom_root_node), "memory");
389f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	size = prom_getproperty(node, "available", (char *) reg, sizeof(reg));
399f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	num_ents = size / sizeof(struct linux_prom_registers);
409f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
419f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	for (i = 0; i < num_ents; i++) {
429f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		sp_banks[i].base_addr = reg[i].phys_addr;
439f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		sp_banks[i].num_bytes = reg[i].reg_size;
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
469f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	return num_ents;
479f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller}
489f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
499f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Millerstatic int sp_banks_cmp(const void *a, const void *b)
509f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller{
519f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	const struct sparc_phys_banks *x = a, *y = b;
529f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
539f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	if (x->base_addr > y->base_addr)
549f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		return 1;
559f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	if (x->base_addr < y->base_addr)
569f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		return -1;
579f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	return 0;
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Initialize the memory lists based upon the prom version. */
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid __init prom_meminit(void)
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
639f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	int i, num_ents = 0;
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
659f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	switch (prom_vers) {
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PROM_V0:
679f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		num_ents = prom_meminit_v0();
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
699f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PROM_V2:
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case PROM_V3:
729f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		num_ents = prom_meminit_v2();
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
779f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	}
789f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	sort(sp_banks, num_ents, sizeof(struct sparc_phys_banks),
799f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	     sp_banks_cmp, NULL);
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
819f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	/* Sentinel.  */
829f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	sp_banks[num_ents].base_addr = 0xdeadbeef;
839f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	sp_banks[num_ents].num_bytes = 0;
849f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller
859f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller	for (i = 0; i < num_ents; i++)
869f2b2a5f68c27c00f1e1f1922de5aa2f24505ed8David S. Miller		sp_banks[i].num_bytes &= PAGE_MASK;
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
88