David S. Miller | ccc3402 | 2008-05-01 21:28:59 -0700 | [diff] [blame^] | 1 | /* memory.c: Prom routine for acquiring various bits of information |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #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 | |
| 23 | struct linux_prom_registers prom_reg_memlist[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct linux_mlist_v0 prom_phys_avail[64]; |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* Internal Prom library routine to sort a linux_mlist_v0 memory |
| 28 | * list. Used below in initialization. |
| 29 | */ |
| 30 | static void __init |
| 31 | prom_sortmemlist(struct linux_mlist_v0 *thislist) |
| 32 | { |
| 33 | int swapi = 0; |
| 34 | int i, mitr, tmpsize; |
| 35 | char *tmpaddr; |
| 36 | char *lowest; |
| 37 | |
Al Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 38 | for(i=0; thislist[i].theres_more; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | lowest = thislist[i].start_adr; |
Al Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 40 | for(mitr = i+1; thislist[mitr-1].theres_more; mitr++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | 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. */ |
| 60 | void __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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | 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 Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 75 | prom_phys_avail[iter-1].theres_more = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | 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 Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 95 | prom_phys_avail[iter-1].theres_more = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | prom_sortmemlist(prom_phys_avail); |
| 97 | break; |
| 98 | |
| 99 | case PROM_SUN4: |
| 100 | #ifdef CONFIG_SUN4 |
| 101 | /* how simple :) */ |
Al Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 102 | prom_phys_avail[0].start_adr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | prom_phys_avail[0].num_bytes = *(sun4_romvec->memoryavail); |
Al Viro | 3c51f19 | 2005-05-04 05:38:51 +0100 | [diff] [blame] | 104 | prom_phys_avail[0].theres_more = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #endif |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | break; |
| 110 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* This returns a pointer to our libraries internal v0 format |
David S. Miller | ccc3402 | 2008-05-01 21:28:59 -0700 | [diff] [blame^] | 114 | * available memory list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
David S. Miller | ccc3402 | 2008-05-01 21:28:59 -0700 | [diff] [blame^] | 116 | struct linux_mlist_v0 * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | prom_meminfo(void) |
| 118 | { |
David S. Miller | ccc3402 | 2008-05-01 21:28:59 -0700 | [diff] [blame^] | 119 | return prom_phys_avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |