blob: 08ac1bf2e588416ce3c33593ad533087aa20621e [file] [log] [blame]
David S. Millerccc34022008-05-01 21:28:59 -07001/* memory.c: Prom routine for acquiring various bits of information
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * about RAM on the machine, both virtual and physical.
3 *
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997 Michael A. Griffith (grif@acm.org)
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/init.h>
10
11#include <asm/openprom.h>
12#include <asm/sun4prom.h>
13#include <asm/oplib.h>
14
15/* This routine, for consistency, returns the ram parameters in the
16 * V0 prom memory descriptor format. I choose this format because I
17 * think it was the easiest to work with. I feel the religious
18 * arguments now... ;) Also, I return the linked lists sorted to
19 * prevent paging_init() upset stomach as I have not yet written
20 * the pepto-bismol kernel module yet.
21 */
22
23struct linux_prom_registers prom_reg_memlist[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct linux_mlist_v0 prom_phys_avail[64];
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Internal Prom library routine to sort a linux_mlist_v0 memory
28 * list. Used below in initialization.
29 */
30static void __init
31prom_sortmemlist(struct linux_mlist_v0 *thislist)
32{
33 int swapi = 0;
34 int i, mitr, tmpsize;
35 char *tmpaddr;
36 char *lowest;
37
Al Viro3c51f192005-05-04 05:38:51 +010038 for(i=0; thislist[i].theres_more; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 lowest = thislist[i].start_adr;
Al Viro3c51f192005-05-04 05:38:51 +010040 for(mitr = i+1; thislist[mitr-1].theres_more; mitr++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if(thislist[mitr].start_adr < lowest) {
42 lowest = thislist[mitr].start_adr;
43 swapi = mitr;
44 }
45 if(lowest == thislist[i].start_adr) continue;
46 tmpaddr = thislist[swapi].start_adr;
47 tmpsize = thislist[swapi].num_bytes;
48 for(mitr = swapi; mitr > i; mitr--) {
49 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
50 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
51 }
52 thislist[i].start_adr = tmpaddr;
53 thislist[i].num_bytes = tmpsize;
54 }
55
56 return;
57}
58
59/* Initialize the memory lists based upon the prom version. */
60void __init prom_meminit(void)
61{
62 int node = 0;
63 unsigned int iter, num_regs;
64 struct linux_mlist_v0 *mptr; /* ptr for traversal */
65
66 switch(prom_vers) {
67 case PROM_V0:
68 /* Nice, kind of easier to do in this case. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 for(mptr = (*(romvec->pv_v0mem.v0_available)), iter=0;
70 mptr; mptr=mptr->theres_more, iter++) {
71 prom_phys_avail[iter].start_adr = mptr->start_adr;
72 prom_phys_avail[iter].num_bytes = mptr->num_bytes;
73 prom_phys_avail[iter].theres_more = &prom_phys_avail[iter+1];
74 }
Al Viro3c51f192005-05-04 05:38:51 +010075 prom_phys_avail[iter-1].theres_more = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 prom_sortmemlist(prom_phys_avail);
77 break;
78 case PROM_V2:
79 case PROM_V3:
80 /* Grrr, have to traverse the prom device tree ;( */
81 node = prom_getchild(prom_root_node);
82 node = prom_searchsiblings(node, "memory");
83 num_regs = prom_getproperty(node, "available",
84 (char *) prom_reg_memlist,
85 sizeof(prom_reg_memlist));
86 num_regs = (num_regs/sizeof(struct linux_prom_registers));
87 for(iter=0; iter<num_regs; iter++) {
88 prom_phys_avail[iter].start_adr =
89 (char *) prom_reg_memlist[iter].phys_addr;
90 prom_phys_avail[iter].num_bytes =
91 (unsigned long) prom_reg_memlist[iter].reg_size;
92 prom_phys_avail[iter].theres_more =
93 &prom_phys_avail[iter+1];
94 }
Al Viro3c51f192005-05-04 05:38:51 +010095 prom_phys_avail[iter-1].theres_more = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 prom_sortmemlist(prom_phys_avail);
97 break;
98
99 case PROM_SUN4:
100#ifdef CONFIG_SUN4
101 /* how simple :) */
Al Viro3c51f192005-05-04 05:38:51 +0100102 prom_phys_avail[0].start_adr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 prom_phys_avail[0].num_bytes = *(sun4_romvec->memoryavail);
Al Viro3c51f192005-05-04 05:38:51 +0100104 prom_phys_avail[0].theres_more = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106 break;
107
108 default:
109 break;
110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/* This returns a pointer to our libraries internal v0 format
David S. Millerccc34022008-05-01 21:28:59 -0700114 * available memory list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
David S. Millerccc34022008-05-01 21:28:59 -0700116struct linux_mlist_v0 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117prom_meminfo(void)
118{
David S. Millerccc34022008-05-01 21:28:59 -0700119 return prom_phys_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}