1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ccc34028SDavid S. Miller /* memory.c: Prom routine for acquiring various bits of information
31da177e4SLinus Torvalds * about RAM on the machine, both virtual and physical.
41da177e4SLinus Torvalds *
59f2b2a5fSDavid S. Miller * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
61da177e4SLinus Torvalds * Copyright (C) 1997 Michael A. Griffith (grif@acm.org)
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/kernel.h>
109f2b2a5fSDavid S. Miller #include <linux/sort.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #include <asm/openprom.h>
141da177e4SLinus Torvalds #include <asm/oplib.h>
159f2b2a5fSDavid S. Miller #include <asm/page.h>
161da177e4SLinus Torvalds
prom_meminit_v0(void)179f2b2a5fSDavid S. Miller static int __init prom_meminit_v0(void)
181da177e4SLinus Torvalds {
199f2b2a5fSDavid S. Miller struct linux_mlist_v0 *p;
209f2b2a5fSDavid S. Miller int index;
211da177e4SLinus Torvalds
229f2b2a5fSDavid S. Miller index = 0;
239f2b2a5fSDavid S. Miller for (p = *(romvec->pv_v0mem.v0_available); p; p = p->theres_more) {
249f2b2a5fSDavid S. Miller sp_banks[index].base_addr = (unsigned long) p->start_adr;
259f2b2a5fSDavid S. Miller sp_banks[index].num_bytes = p->num_bytes;
269f2b2a5fSDavid S. Miller index++;
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds
299f2b2a5fSDavid S. Miller return index;
309f2b2a5fSDavid S. Miller }
319f2b2a5fSDavid S. Miller
prom_meminit_v2(void)329f2b2a5fSDavid S. Miller static int __init prom_meminit_v2(void)
339f2b2a5fSDavid S. Miller {
349f2b2a5fSDavid S. Miller struct linux_prom_registers reg[64];
358d125562SAndres Salomon phandle node;
368d125562SAndres Salomon int size, num_ents, i;
379f2b2a5fSDavid S. Miller
389f2b2a5fSDavid S. Miller node = prom_searchsiblings(prom_getchild(prom_root_node), "memory");
399f2b2a5fSDavid S. Miller size = prom_getproperty(node, "available", (char *) reg, sizeof(reg));
409f2b2a5fSDavid S. Miller num_ents = size / sizeof(struct linux_prom_registers);
419f2b2a5fSDavid S. Miller
429f2b2a5fSDavid S. Miller for (i = 0; i < num_ents; i++) {
439f2b2a5fSDavid S. Miller sp_banks[i].base_addr = reg[i].phys_addr;
449f2b2a5fSDavid S. Miller sp_banks[i].num_bytes = reg[i].reg_size;
459f2b2a5fSDavid S. Miller }
469f2b2a5fSDavid S. Miller
479f2b2a5fSDavid S. Miller return num_ents;
489f2b2a5fSDavid S. Miller }
499f2b2a5fSDavid S. Miller
sp_banks_cmp(const void * a,const void * b)509f2b2a5fSDavid S. Miller static int sp_banks_cmp(const void *a, const void *b)
519f2b2a5fSDavid S. Miller {
529f2b2a5fSDavid S. Miller const struct sparc_phys_banks *x = a, *y = b;
539f2b2a5fSDavid S. Miller
549f2b2a5fSDavid S. Miller if (x->base_addr > y->base_addr)
559f2b2a5fSDavid S. Miller return 1;
569f2b2a5fSDavid S. Miller if (x->base_addr < y->base_addr)
579f2b2a5fSDavid S. Miller return -1;
589f2b2a5fSDavid S. Miller return 0;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds /* Initialize the memory lists based upon the prom version. */
prom_meminit(void)621da177e4SLinus Torvalds void __init prom_meminit(void)
631da177e4SLinus Torvalds {
649f2b2a5fSDavid S. Miller int i, num_ents = 0;
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds switch (prom_vers) {
671da177e4SLinus Torvalds case PROM_V0:
689f2b2a5fSDavid S. Miller num_ents = prom_meminit_v0();
691da177e4SLinus Torvalds break;
709f2b2a5fSDavid S. Miller
711da177e4SLinus Torvalds case PROM_V2:
721da177e4SLinus Torvalds case PROM_V3:
739f2b2a5fSDavid S. Miller num_ents = prom_meminit_v2();
741da177e4SLinus Torvalds break;
751da177e4SLinus Torvalds
761da177e4SLinus Torvalds default:
771da177e4SLinus Torvalds break;
781da177e4SLinus Torvalds }
799f2b2a5fSDavid S. Miller sort(sp_banks, num_ents, sizeof(struct sparc_phys_banks),
809f2b2a5fSDavid S. Miller sp_banks_cmp, NULL);
811da177e4SLinus Torvalds
829f2b2a5fSDavid S. Miller /* Sentinel. */
839f2b2a5fSDavid S. Miller sp_banks[num_ents].base_addr = 0xdeadbeef;
849f2b2a5fSDavid S. Miller sp_banks[num_ents].num_bytes = 0;
859f2b2a5fSDavid S. Miller
869f2b2a5fSDavid S. Miller for (i = 0; i < num_ents; i++)
879f2b2a5fSDavid S. Miller sp_banks[i].num_bytes &= PAGE_MASK;
881da177e4SLinus Torvalds }
89