memory.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * memory.c: PROM library functions for acquiring/using memory descriptors
  4. * given to us from the ARCS firmware.
  5. *
  6. * Copyright (C) 1996 by David S. Miller
  7. * Copyright (C) 1999, 2000, 2001 by Ralf Baechle
  8. * Copyright (C) 1999, 2000 by Silicon Graphics, Inc.
  9. *
  10. * PROM library functions for acquiring/using memory descriptors given to us
  11. * from the ARCS firmware. This is only used when CONFIG_ARC_MEMORY is set
  12. * because on some machines like SGI IP27 the ARC memory configuration data
  13. * completely bogus and alternate easier to use mechanisms are available.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/sched.h>
  19. #include <linux/mm.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/swap.h>
  22. #include <asm/sgialib.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/bootinfo.h>
  26. #undef DEBUG
  27. /*
  28. * For ARC firmware memory functions the unit of meassuring memory is always
  29. * a 4k page of memory
  30. */
  31. #define ARC_PAGE_SHIFT 12
  32. struct linux_mdesc * __init ArcGetMemoryDescriptor(struct linux_mdesc *Current)
  33. {
  34. return (struct linux_mdesc *) ARC_CALL1(get_mdesc, Current);
  35. }
  36. #ifdef DEBUG /* convenient for debugging */
  37. static char *arcs_mtypes[8] = {
  38. "Exception Block",
  39. "ARCS Romvec Page",
  40. "Free/Contig RAM",
  41. "Generic Free RAM",
  42. "Bad Memory",
  43. "Standalone Program Pages",
  44. "ARCS Temp Storage Area",
  45. "ARCS Permanent Storage Area"
  46. };
  47. static char *arc_mtypes[8] = {
  48. "Exception Block",
  49. "SystemParameterBlock",
  50. "FreeMemory",
  51. "Bad Memory",
  52. "LoadedProgram",
  53. "FirmwareTemporary",
  54. "FirmwarePermanent",
  55. "FreeContiguous"
  56. };
  57. #define mtypes(a) (prom_flags & PROM_FLAG_ARCS) ? arcs_mtypes[a.arcs] \
  58. : arc_mtypes[a.arc]
  59. #endif
  60. static inline int memtype_classify_arcs(union linux_memtypes type)
  61. {
  62. switch (type.arcs) {
  63. case arcs_fcontig:
  64. case arcs_free:
  65. return BOOT_MEM_RAM;
  66. case arcs_atmp:
  67. return BOOT_MEM_ROM_DATA;
  68. case arcs_eblock:
  69. case arcs_rvpage:
  70. case arcs_bmem:
  71. case arcs_prog:
  72. case arcs_aperm:
  73. return BOOT_MEM_RESERVED;
  74. default:
  75. BUG();
  76. }
  77. while(1); /* Nuke warning. */
  78. }
  79. static inline int memtype_classify_arc(union linux_memtypes type)
  80. {
  81. switch (type.arc) {
  82. case arc_free:
  83. case arc_fcontig:
  84. return BOOT_MEM_RAM;
  85. case arc_atmp:
  86. return BOOT_MEM_ROM_DATA;
  87. case arc_eblock:
  88. case arc_rvpage:
  89. case arc_bmem:
  90. case arc_prog:
  91. case arc_aperm:
  92. return BOOT_MEM_RESERVED;
  93. default:
  94. BUG();
  95. }
  96. while(1); /* Nuke warning. */
  97. }
  98. static int __init prom_memtype_classify(union linux_memtypes type)
  99. {
  100. if (prom_flags & PROM_FLAG_ARCS) /* SGI is ``different'' ... */
  101. return memtype_classify_arcs(type);
  102. return memtype_classify_arc(type);
  103. }
  104. void __init prom_meminit(void)
  105. {
  106. struct linux_mdesc *p;
  107. #ifdef DEBUG
  108. int i = 0;
  109. printk("ARCS MEMORY DESCRIPTOR dump:\n");
  110. p = ArcGetMemoryDescriptor(PROM_NULL_MDESC);
  111. while(p) {
  112. printk("[%d,%p]: base<%08lx> pages<%08lx> type<%s>\n",
  113. i, p, p->base, p->pages, mtypes(p->type));
  114. p = ArcGetMemoryDescriptor(p);
  115. i++;
  116. }
  117. #endif
  118. p = PROM_NULL_MDESC;
  119. while ((p = ArcGetMemoryDescriptor(p))) {
  120. unsigned long base, size;
  121. long type;
  122. base = p->base << ARC_PAGE_SHIFT;
  123. size = p->pages << ARC_PAGE_SHIFT;
  124. type = prom_memtype_classify(p->type);
  125. add_memory_region(base, size, type);
  126. }
  127. }
  128. void __init prom_free_prom_memory(void)
  129. {
  130. unsigned long addr;
  131. int i;
  132. if (prom_flags & PROM_FLAG_DONT_FREE_TEMP)
  133. return;
  134. for (i = 0; i < boot_mem_map.nr_map; i++) {
  135. if (boot_mem_map.map[i].type != BOOT_MEM_ROM_DATA)
  136. continue;
  137. addr = boot_mem_map.map[i].addr;
  138. free_init_pages("prom memory",
  139. addr, addr + boot_mem_map.map[i].size);
  140. }
  141. }