/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- ram_bootstrap
- ram_stealmem
- ram_getsize
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <types.h>
31 #include <lib.h>
32 #include <vm.h>
33 #include <mainbus.h>
34
35
36 vaddr_t firstfree; /* first free virtual address; set by start.S */
37
38 static paddr_t firstpaddr; /* address of first free physical page */
39 static paddr_t lastpaddr; /* one past end of last free physical page */
40
41 /*
42 * Called very early in system boot to figure out how much physical
43 * RAM is available.
44 */
45 void
46 ram_bootstrap(void)
47 {
48 size_t ramsize;
49
50 /* Get size of RAM. */
51 ramsize = mainbus_ramsize();
52
53 /*
54 * This is the same as the last physical address, as long as
55 * we have less than 508 megabytes of memory. If we had more,
56 * various annoying properties of the MIPS architecture would
57 * force the RAM to be discontiguous. This is not a case we
58 * are going to worry about.
59 */
60 if (ramsize > 508*1024*1024) {
61 ramsize = 508*1024*1024;
62 }
63
64 lastpaddr = ramsize;
65
66 /*
67 * Get first free virtual address from where start.S saved it.
68 * Convert to physical address.
69 */
70 firstpaddr = firstfree - MIPS_KSEG0;
71
72 kprintf("%uk physical memory available\n",
73 (lastpaddr-firstpaddr)/1024);
74 }
75
76 /*
77 * This function is for allocating physical memory prior to VM
78 * initialization.
79 *
80 * The pages it hands back will not be reported to the VM system when
81 * the VM system calls ram_getsize(). If it's desired to free up these
82 * pages later on after bootup is complete, some mechanism for adding
83 * them to the VM system's page management must be implemented.
84 * Alternatively, one can do enough VM initialization early so that
85 * this function is never needed.
86 *
87 * Note: while the error return value of 0 is a legal physical address,
88 * it's not a legal *allocatable* physical address, because it's the
89 * page with the exception handlers on it.
90 *
91 * This function should not be called once the VM system is initialized,
92 * so it is not synchronized.
93 */
94 paddr_t
95 ram_stealmem(unsigned long npages)
96 {
97 size_t size;
98 paddr_t paddr;
99
100 size = npages * PAGE_SIZE;
101
102 if (firstpaddr + size > lastpaddr) {
103 return 0;
104 }
105
106 paddr = firstpaddr;
107 firstpaddr += size;
108
109 return paddr;
110 }
111
112 /*
113 * This function is intended to be called by the VM system when it
114 * initializes in order to find out what memory it has available to
115 * manage.
116 *
117 * This function should not be called once the VM system is initialized,
118 * so it is not synchronized.
119 */
120 void
121 ram_getsize(paddr_t *lo, paddr_t *hi)
122 {
123 *lo = firstpaddr;
124 *hi = lastpaddr;
125 firstpaddr = lastpaddr = 0;
126 }