ram.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <types.h>
  30. #include <lib.h>
  31. #include <vm.h>
  32. #include <mainbus.h>
  33. vaddr_t firstfree; /* first free virtual address; set by start.S */
  34. static paddr_t firstpaddr; /* address of first free physical page */
  35. static paddr_t lastpaddr; /* one past end of last free physical page */
  36. /*
  37. * Called very early in system boot to figure out how much physical
  38. * RAM is available.
  39. */
  40. void
  41. ram_bootstrap(void)
  42. {
  43. size_t ramsize;
  44. /* Get size of RAM. */
  45. ramsize = mainbus_ramsize();
  46. /*
  47. * This is the same as the last physical address, as long as
  48. * we have less than 508 megabytes of memory. If we had more,
  49. * various annoying properties of the MIPS architecture would
  50. * force the RAM to be discontiguous. This is not a case we
  51. * are going to worry about.
  52. */
  53. if (ramsize > 508*1024*1024) {
  54. ramsize = 508*1024*1024;
  55. }
  56. lastpaddr = ramsize;
  57. /*
  58. * Get first free virtual address from where start.S saved it.
  59. * Convert to physical address.
  60. */
  61. firstpaddr = firstfree - MIPS_KSEG0;
  62. kprintf("%uk physical memory available\n",
  63. (lastpaddr-firstpaddr)/1024);
  64. }
  65. /*
  66. * This function is for allocating physical memory prior to VM
  67. * initialization.
  68. *
  69. * The pages it hands back will not be reported to the VM system when
  70. * the VM system calls ram_getsize(). If it's desired to free up these
  71. * pages later on after bootup is complete, some mechanism for adding
  72. * them to the VM system's page management must be implemented.
  73. * Alternatively, one can do enough VM initialization early so that
  74. * this function is never needed.
  75. *
  76. * Note: while the error return value of 0 is a legal physical address,
  77. * it's not a legal *allocatable* physical address, because it's the
  78. * page with the exception handlers on it.
  79. *
  80. * This function should not be called once the VM system is initialized,
  81. * so it is not synchronized.
  82. */
  83. paddr_t
  84. ram_stealmem(unsigned long npages)
  85. {
  86. size_t size;
  87. paddr_t paddr;
  88. size = npages * PAGE_SIZE;
  89. if (firstpaddr + size > lastpaddr) {
  90. return 0;
  91. }
  92. paddr = firstpaddr;
  93. firstpaddr += size;
  94. return paddr;
  95. }
  96. /*
  97. * This function is intended to be called by the VM system when it
  98. * initializes in order to find out what memory it has available to
  99. * manage.
  100. *
  101. * This function should not be called once the VM system is initialized,
  102. * so it is not synchronized.
  103. */
  104. void
  105. ram_getsize(paddr_t *lo, paddr_t *hi)
  106. {
  107. *lo = firstpaddr;
  108. *hi = lastpaddr;
  109. firstpaddr = lastpaddr = 0;
  110. }