1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Memory Map commands			File: ui_memcmds.c
5    *
6    *  Memory Manager user interface
7    *
8    *  Author:  Mitch Lichtenberg
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#include "cfe.h"
49#include "lib_arena.h"
50#include "ui_command.h"
51#include "cfe_mem.h"
52
53
54const static char * const cfe_arenatypes[] = {
55    "Reserved",
56    "DRAM (available)",
57    "Memory Controller (unused)",
58    "DRAM (in use by firmware)",
59    "ROM",
60    "I/O Registers",
61    "Not available",
62    "L2 Cache",
63    "LDT/PCI",
64    "DRAM (used by boot program)",
65    "DRAM (manually reserved)",
66    NULL};
67
68extern arena_t cfe_arena;
69
70
71
72int ui_init_memcmds(void);
73
74static int ui_cmd_physmap(ui_cmdline_t *cmd,int argc,char *argv[]);
75static int ui_cmd_heapstats(ui_cmdline_t *cmd,int argc,char *argv[]);
76static int ui_cmd_reserve(ui_cmdline_t *cmd,int argc,char *argv[]);
77
78
79int ui_init_memcmds(void)
80{
81    cmd_addcmd("show memory",
82	       ui_cmd_physmap,
83	       NULL,
84	       "Display the system physical memory map.",
85	       "show memory [-a]\n\n"
86	       "This command displays the arena, or system physical memory map\n"
87	       "You can use this command to determine the areas of RAM that will\n"
88	       "be made available to operating systems.\n",
89	       "-a;Display all entries in the map, not just the blocks\n"
90	       "of available memory.");
91
92    cmd_addcmd("show heap",
93	       ui_cmd_heapstats,
94	       NULL,
95	       "Display information about CFE's heap",
96	       "show heap\n\n"
97	       "This is a debugging command that can be used to determine the health\n"
98	       "of CFE's internal memory manager.",
99	       "");
100
101    cmd_addcmd("reserve",
102	       ui_cmd_reserve,
103	       NULL,
104	       "Mark a region of memory as reserved",
105	       "reserve start-addr end-addr\n\n"
106	       "This command marks a region of memory as 'reserved' in the arena to prevent\n"
107	       "an application that uses the CFE memory APIs from using the reserved region.\n"
108	       "The region must currently be of type 'available DRAM' for this command to work.\n",
109	       "");
110
111    return 0;
112}
113
114static int ui_cmd_heapstats(ui_cmdline_t *cmd,int argc,char *argv[])
115{
116    int res;
117    memstats_t stats;
118
119    res = KMEMSTATS(&stats);
120
121    xprintf("\n");
122    xprintf("Total bytes:       %d\n",stats.mem_totalbytes);
123    xprintf("Free bytes:        %d\n",stats.mem_freebytes);
124    xprintf("Free nodes:        %d\n",stats.mem_freenodes);
125    xprintf("Allocated bytes:   %d\n",stats.mem_allocbytes);
126    xprintf("Allocated nodes:   %d\n",stats.mem_allocnodes);
127    xprintf("Largest free node: %d\n",stats.mem_largest);
128    xprintf("Heap status:       %s\n",(res == 0) ? "CONSISTENT" : "CORRUPT!");
129    xprintf("\n");
130
131    return res;
132}
133
134
135#define PHYSMAP_FLG_ALL		1
136#define PHYSMAP_FLG_AVAIL	2
137
138static int ui_cmd_physmap(ui_cmdline_t *cmd,int argc,char *argv[])
139{
140    arena_node_t *node;
141    queue_t *qb;
142    arena_t *arena = &cfe_arena;
143    int flags = 0;
144
145    if (cmd_sw_isset(cmd,"-a")) {
146	flags |= PHYSMAP_FLG_ALL;
147	}
148    else {
149	flags = PHYSMAP_FLG_AVAIL;
150	}
151
152
153    xprintf("Range Start  Range End    Range Size     Description\n");
154    xprintf("------------ ------------ -------------- --------------------\n");
155
156    for (qb = (arena->arena_list.q_next); qb != &(arena->arena_list);
157	 qb = qb->q_next) {
158	node = (arena_node_t *) qb;
159
160	if ((flags & PHYSMAP_FLG_ALL) ||
161	    ((flags & PHYSMAP_FLG_AVAIL) && (node->an_type == MEMTYPE_DRAM_AVAILABLE)))  {
162
163	    xprintf("%012llX-%012llX (%012llX) ",
164		    node->an_address,
165		    node->an_address+node->an_length-1,
166		    node->an_length);
167	    if (node->an_type > MEMTYPE_MAX) xprintf("Unknown type %d\n",node->an_type);
168	    else xprintf("%s\n",cfe_arenatypes[node->an_type]);
169	    }
170
171	}
172
173    return 0;
174}
175
176static int ui_cmd_reserve(ui_cmdline_t *cmd,int argc,char *argv[])
177{
178    char *x;
179    uint64_t start,end;
180    arena_node_t *node;
181    queue_t *qb;
182    arena_t *arena = &cfe_arena;
183
184    if ((x = cmd_getarg(cmd,0))) start = lib_xtoq(x);
185    else return ui_showusage(cmd);
186
187    if ((x = cmd_getarg(cmd,1))) end = lib_xtoq(x);
188    else return ui_showusage(cmd);
189
190    for (qb = (arena->arena_list.q_next); qb != &(arena->arena_list);
191	 qb = qb->q_next) {
192	node = (arena_node_t *) qb;
193	if ((node->an_type == MEMTYPE_DRAM_AVAILABLE) &&
194	    (node->an_address >= start) &&
195	    (end <= (node->an_address+node->an_length))) {
196	    break;
197	    }
198	}
199
200    if (qb == &(arena->arena_list)) {
201	printf("Could not mark memory as reserved.   Specified region must be available DRAM\n"
202	       "before its type can be changed to 'reserved.'");
203	return CFE_ERR_INV_PARAM;
204	}
205
206    arena_markrange(&cfe_arena,start,end-start,MEMTYPE_DRAM_MANUALLY_RESERVED,NULL);
207
208    return 0;
209}
210