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