memory.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * memory.c: memory initialisation code.
  4. *
  5. * Copyright (C) 1998 Harald Koerfgen, Frieder Streffer and Paul M. Antoine
  6. * Copyright (C) 2000, 2002 Maciej W. Rozycki
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/memblock.h>
  12. #include <linux/types.h>
  13. #include <asm/addrspace.h>
  14. #include <asm/dec/machtype.h>
  15. #include <asm/dec/prom.h>
  16. #include <asm/page.h>
  17. #include <asm/sections.h>
  18. volatile unsigned long mem_err; /* So we know an error occurred */
  19. /*
  20. * Probe memory in 4MB chunks, waiting for an error to tell us we've fallen
  21. * off the end of real memory. Only suitable for the 2100/3100's (PMAX).
  22. */
  23. #define CHUNK_SIZE 0x400000
  24. static __init void pmax_setup_memory_region(void)
  25. {
  26. volatile unsigned char *memory_page, dummy;
  27. char old_handler[0x80];
  28. extern char genexcept_early;
  29. /* Install exception handler */
  30. memcpy(&old_handler, (void *)(CKSEG0 + 0x80), 0x80);
  31. memcpy((void *)(CKSEG0 + 0x80), &genexcept_early, 0x80);
  32. /* read unmapped and uncached (KSEG1)
  33. * DECstations have at least 4MB RAM
  34. * Assume less than 480MB of RAM, as this is max for 5000/2xx
  35. * FIXME this should be replaced by the first free page!
  36. */
  37. for (memory_page = (unsigned char *)CKSEG1 + CHUNK_SIZE;
  38. mem_err == 0 && memory_page < (unsigned char *)CKSEG1 + 0x1e00000;
  39. memory_page += CHUNK_SIZE) {
  40. dummy = *memory_page;
  41. }
  42. memcpy((void *)(CKSEG0 + 0x80), &old_handler, 0x80);
  43. memblock_add(0, (unsigned long)memory_page - CKSEG1 - CHUNK_SIZE);
  44. }
  45. /*
  46. * Use the REX prom calls to get hold of the memory bitmap, and thence
  47. * determine memory size.
  48. */
  49. static __init void rex_setup_memory_region(void)
  50. {
  51. int i, bitmap_size;
  52. unsigned long mem_start = 0, mem_size = 0;
  53. memmap *bm;
  54. /* some free 64k */
  55. bm = (memmap *)CKSEG0ADDR(0x28000);
  56. bitmap_size = rex_getbitmap(bm);
  57. for (i = 0; i < bitmap_size; i++) {
  58. /* FIXME: very simplistically only add full sets of pages */
  59. if (bm->bitmap[i] == 0xff)
  60. mem_size += (8 * bm->pagesize);
  61. else if (!mem_size)
  62. mem_start += (8 * bm->pagesize);
  63. else {
  64. memblock_add(mem_start, mem_size);
  65. mem_start += mem_size + (8 * bm->pagesize);
  66. mem_size = 0;
  67. }
  68. }
  69. if (mem_size)
  70. memblock_add(mem_start, mem_size);
  71. }
  72. void __init prom_meminit(u32 magic)
  73. {
  74. if (!prom_is_rex(magic))
  75. pmax_setup_memory_region();
  76. else
  77. rex_setup_memory_region();
  78. }
  79. void __init prom_free_prom_memory(void)
  80. {
  81. unsigned long end;
  82. /*
  83. * Free everything below the kernel itself but leave
  84. * the first page reserved for the exception handlers.
  85. */
  86. #if IS_ENABLED(CONFIG_DECLANCE)
  87. /*
  88. * Leave 128 KB reserved for Lance memory for
  89. * IOASIC DECstations.
  90. *
  91. * XXX: save this address for use in dec_lance.c?
  92. */
  93. if (IOASIC)
  94. end = __pa(&_text) - 0x00020000;
  95. else
  96. #endif
  97. end = __pa(&_text);
  98. free_init_pages("unused PROM memory", PAGE_SIZE, end);
  99. }