memory.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Carsten Langgaard, carstenl@mips.com
  3. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  17. *
  18. * PROM library functions for acquiring/using memory descriptors given to
  19. * us from the YAMON.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/init.h>
  23. #include <linux/mm.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/string.h>
  26. #include <asm/bootinfo.h>
  27. #include <asm/page.h>
  28. #include <asm/mips-boards/prom.h>
  29. /*#define DEBUG*/
  30. enum yamon_memtypes {
  31. yamon_dontuse,
  32. yamon_prom,
  33. yamon_free,
  34. };
  35. struct prom_pmemblock mdesc[PROM_MAX_PMEMBLOCKS];
  36. #ifdef DEBUG
  37. static char *mtypes[3] = {
  38. "Dont use memory",
  39. "YAMON PROM memory",
  40. "Free memmory",
  41. };
  42. #endif
  43. /* References to section boundaries */
  44. extern char _end;
  45. #define PFN_ALIGN(x) (((unsigned long)(x) + (PAGE_SIZE - 1)) & PAGE_MASK)
  46. struct prom_pmemblock * __init prom_getmdesc(void)
  47. {
  48. char *memsize_str;
  49. unsigned int memsize;
  50. char cmdline[CL_SIZE], *ptr;
  51. /* Check the command line first for a memsize directive */
  52. strcpy(cmdline, arcs_cmdline);
  53. ptr = strstr(cmdline, "memsize=");
  54. if (ptr && (ptr != cmdline) && (*(ptr - 1) != ' '))
  55. ptr = strstr(ptr, " memsize=");
  56. if (ptr) {
  57. memsize = memparse(ptr + 8, &ptr);
  58. }
  59. else {
  60. /* otherwise look in the environment */
  61. memsize_str = prom_getenv("memsize");
  62. if (!memsize_str) {
  63. prom_printf("memsize not set in boot prom, set to default (32Mb)\n");
  64. memsize = 0x02000000;
  65. } else {
  66. #ifdef DEBUG
  67. prom_printf("prom_memsize = %s\n", memsize_str);
  68. #endif
  69. memsize = simple_strtol(memsize_str, NULL, 0);
  70. }
  71. }
  72. memset(mdesc, 0, sizeof(mdesc));
  73. mdesc[0].type = yamon_dontuse;
  74. mdesc[0].base = 0x00000000;
  75. mdesc[0].size = 0x00001000;
  76. mdesc[1].type = yamon_prom;
  77. mdesc[1].base = 0x00001000;
  78. mdesc[1].size = 0x000ef000;
  79. #ifdef CONFIG_MIPS_MALTA
  80. /*
  81. * The area 0x000f0000-0x000fffff is allocated for BIOS memory by the
  82. * south bridge and PCI access always forwarded to the ISA Bus and
  83. * BIOSCS# is always generated.
  84. * This mean that this area can't be used as DMA memory for PCI
  85. * devices.
  86. */
  87. mdesc[2].type = yamon_dontuse;
  88. mdesc[2].base = 0x000f0000;
  89. mdesc[2].size = 0x00010000;
  90. #else
  91. mdesc[2].type = yamon_prom;
  92. mdesc[2].base = 0x000f0000;
  93. mdesc[2].size = 0x00010000;
  94. #endif
  95. mdesc[3].type = yamon_dontuse;
  96. mdesc[3].base = 0x00100000;
  97. mdesc[3].size = CPHYSADDR(PFN_ALIGN(&_end)) - mdesc[3].base;
  98. mdesc[4].type = yamon_free;
  99. mdesc[4].base = CPHYSADDR(PFN_ALIGN(&_end));
  100. mdesc[4].size = memsize - mdesc[4].base;
  101. return &mdesc[0];
  102. }
  103. static int __init prom_memtype_classify (unsigned int type)
  104. {
  105. switch (type) {
  106. case yamon_free:
  107. return BOOT_MEM_RAM;
  108. case yamon_prom:
  109. return BOOT_MEM_ROM_DATA;
  110. default:
  111. return BOOT_MEM_RESERVED;
  112. }
  113. }
  114. void __init prom_meminit(void)
  115. {
  116. struct prom_pmemblock *p;
  117. #ifdef DEBUG
  118. prom_printf("YAMON MEMORY DESCRIPTOR dump:\n");
  119. p = prom_getmdesc();
  120. while (p->size) {
  121. int i = 0;
  122. prom_printf("[%d,%p]: base<%08lx> size<%08lx> type<%s>\n",
  123. i, p, p->base, p->size, mtypes[p->type]);
  124. p++;
  125. i++;
  126. }
  127. #endif
  128. p = prom_getmdesc();
  129. while (p->size) {
  130. long type;
  131. unsigned long base, size;
  132. type = prom_memtype_classify (p->type);
  133. base = p->base;
  134. size = p->size;
  135. add_memory_region(base, size, type);
  136. p++;
  137. }
  138. }
  139. unsigned long __init prom_free_prom_memory(void)
  140. {
  141. unsigned long freed = 0;
  142. unsigned long addr;
  143. int i;
  144. for (i = 0; i < boot_mem_map.nr_map; i++) {
  145. if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
  146. continue;
  147. addr = boot_mem_map.map[i].addr;
  148. while (addr < boot_mem_map.map[i].addr
  149. + boot_mem_map.map[i].size) {
  150. ClearPageReserved(virt_to_page(__va(addr)));
  151. set_page_count(virt_to_page(__va(addr)), 1);
  152. free_page((unsigned long)__va(addr));
  153. addr += PAGE_SIZE;
  154. freed += PAGE_SIZE;
  155. }
  156. }
  157. printk("Freeing prom memory: %ldkb freed\n", freed >> 10);
  158. return freed;
  159. }