/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <types.h>30#include <lib.h>31#include <vm.h>32#include <mainbus.h>333435vaddr_t firstfree; /* first free virtual address; set by start.S */36paddr_t firstpaddr; /* address of first free physical page */37paddr_t lastpaddr; /* one past end of last free physical page */3839/*40* Called very early in system boot to figure out how much physical41* RAM is available.42*/43void44ram_bootstrap(void)45{46size_t ramsize;4748/* Get size of RAM. */49ramsize = mainbus_ramsize();5051/*52* This is the same as the last physical address, as long as53* we have less than 508 megabytes of memory. If we had more,54* various annoying properties of the MIPS architecture would55* force the RAM to be discontiguous. This is not a case we56* are going to worry about.57*/58if (ramsize > 508*1024*1024) {59ramsize = 508*1024*1024;60}6162lastpaddr = ramsize;6364/*65* Get first free virtual address from where start.S saved it.66* Convert to physical address.67*/68firstpaddr = firstfree - MIPS_KSEG0;6970kprintf("%uk physical memory available\n",71(lastpaddr-firstpaddr)/1024);72}7374/*75* This function is for allocating physical memory prior to VM76* initialization.77*78* The pages it hands back will not be reported to the VM system when79* the VM system calls ram_getsize(). If it's desired to free up these80* pages later on after bootup is complete, some mechanism for adding81* them to the VM system's page management must be implemented.82* Alternatively, one can do enough VM initialization early so that83* this function is never needed.84*85* Note: while the error return value of 0 is a legal physical address,86* it's not a legal *allocatable* physical address, because it's the87* page with the exception handlers on it.88*89* This function should not be called once the VM system is initialized,90* so it is not synchronized.91*/92paddr_t93ram_stealmem(unsigned long npages)94{95size_t size;96paddr_t paddr;9798size = npages * PAGE_SIZE;99100if (firstpaddr + size > lastpaddr) {101return 0;102}103104paddr = firstpaddr;105firstpaddr += size;106107return paddr;108}109110/*111* This function is intended to be called by the VM system when it112* initializes in order to find out what memory it has available to113* manage.114*115* This function should not be called once the VM system is initialized,116* so it is not synchronized.117*/118void119ram_getsize(paddr_t *lo, paddr_t *hi)120{121*lo = firstpaddr;122*hi = lastpaddr;123firstpaddr = lastpaddr = 0;124}125126127