memory.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/memblock.h>
  21. #include <linux/swap.h>
  22. #include <asm/sgialib.h>
  23. #include <asm/page.h>
  24. #include <asm/bootinfo.h>
  25. #undef DEBUG
  26. #define MAX_PROM_MEM 5
  27. static phys_addr_t prom_mem_base[MAX_PROM_MEM] __initdata;
  28. static phys_addr_t prom_mem_size[MAX_PROM_MEM] __initdata;
  29. static unsigned int nr_prom_mem __initdata;
  30. /*
  31. * For ARC firmware memory functions the unit of measuring memory is always
  32. * a 4k page of memory
  33. */
  34. #define ARC_PAGE_SHIFT 12
  35. struct linux_mdesc * __init ArcGetMemoryDescriptor(struct linux_mdesc *Current)
  36. {
  37. return (struct linux_mdesc *) ARC_CALL1(get_mdesc, Current);
  38. }
  39. #ifdef DEBUG /* convenient for debugging */
  40. static char *arcs_mtypes[8] = {
  41. "Exception Block",
  42. "ARCS Romvec Page",
  43. "Free/Contig RAM",
  44. "Generic Free RAM",
  45. "Bad Memory",
  46. "Standalone Program Pages",
  47. "ARCS Temp Storage Area",
  48. "ARCS Permanent Storage Area"
  49. };
  50. static char *arc_mtypes[8] = {
  51. "Exception Block",
  52. "SystemParameterBlock",
  53. "FreeMemory",
  54. "Bad Memory",
  55. "LoadedProgram",
  56. "FirmwareTemporary",
  57. "FirmwarePermanent",
  58. "FreeContiguous"
  59. };
  60. #define mtypes(a) (prom_flags & PROM_FLAG_ARCS) ? arcs_mtypes[a.arcs] \
  61. : arc_mtypes[a.arc]
  62. #endif
  63. enum {
  64. mem_free, mem_prom_used, mem_reserved
  65. };
  66. static inline int memtype_classify_arcs(union linux_memtypes type)
  67. {
  68. switch (type.arcs) {
  69. case arcs_fcontig:
  70. case arcs_free:
  71. return mem_free;
  72. case arcs_atmp:
  73. return mem_prom_used;
  74. case arcs_eblock:
  75. case arcs_rvpage:
  76. case arcs_bmem:
  77. case arcs_prog:
  78. case arcs_aperm:
  79. return mem_reserved;
  80. default:
  81. BUG();
  82. }
  83. while(1); /* Nuke warning. */
  84. }
  85. static inline int memtype_classify_arc(union linux_memtypes type)
  86. {
  87. switch (type.arc) {
  88. case arc_free:
  89. case arc_fcontig:
  90. return mem_free;
  91. case arc_atmp:
  92. return mem_prom_used;
  93. case arc_eblock:
  94. case arc_rvpage:
  95. case arc_bmem:
  96. case arc_prog:
  97. case arc_aperm:
  98. return mem_reserved;
  99. default:
  100. BUG();
  101. }
  102. while(1); /* Nuke warning. */
  103. }
  104. static int __init prom_memtype_classify(union linux_memtypes type)
  105. {
  106. if (prom_flags & PROM_FLAG_ARCS) /* SGI is ``different'' ... */
  107. return memtype_classify_arcs(type);
  108. return memtype_classify_arc(type);
  109. }
  110. void __weak __init prom_meminit(void)
  111. {
  112. struct linux_mdesc *p;
  113. #ifdef DEBUG
  114. int i = 0;
  115. printk("ARCS MEMORY DESCRIPTOR dump:\n");
  116. p = ArcGetMemoryDescriptor(PROM_NULL_MDESC);
  117. while(p) {
  118. printk("[%d,%p]: base<%08lx> pages<%08lx> type<%s>\n",
  119. i, p, p->base, p->pages, mtypes(p->type));
  120. p = ArcGetMemoryDescriptor(p);
  121. i++;
  122. }
  123. #endif
  124. nr_prom_mem = 0;
  125. p = PROM_NULL_MDESC;
  126. while ((p = ArcGetMemoryDescriptor(p))) {
  127. unsigned long base, size;
  128. long type;
  129. base = p->base << ARC_PAGE_SHIFT;
  130. size = p->pages << ARC_PAGE_SHIFT;
  131. type = prom_memtype_classify(p->type);
  132. /* ignore mirrored RAM on IP28/IP30 */
  133. if (base < PHYS_OFFSET)
  134. continue;
  135. memblock_add(base, size);
  136. if (type == mem_reserved)
  137. memblock_reserve(base, size);
  138. if (type == mem_prom_used) {
  139. memblock_reserve(base, size);
  140. if (nr_prom_mem >= 5) {
  141. pr_err("Too many ROM DATA regions");
  142. continue;
  143. }
  144. prom_mem_base[nr_prom_mem] = base;
  145. prom_mem_size[nr_prom_mem] = size;
  146. nr_prom_mem++;
  147. }
  148. }
  149. }
  150. void __weak __init prom_cleanup(void)
  151. {
  152. }
  153. void __init prom_free_prom_memory(void)
  154. {
  155. int i;
  156. if (prom_flags & PROM_FLAG_DONT_FREE_TEMP)
  157. return;
  158. for (i = 0; i < nr_prom_mem; i++) {
  159. free_init_pages("prom memory",
  160. prom_mem_base[i], prom_mem_base[i] + prom_mem_size[i]);
  161. }
  162. /*
  163. * at this point it isn't safe to call PROM functions
  164. * give platforms a way to do PROM cleanups
  165. */
  166. prom_cleanup();
  167. }