stram.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Functions for ST-RAM allocations
  3. *
  4. * Copyright 1994-97 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/major.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/mount.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/module.h>
  23. #include <asm/setup.h>
  24. #include <asm/machdep.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/atarihw.h>
  28. #include <asm/atari_stram.h>
  29. #include <asm/io.h>
  30. /*
  31. * The ST-RAM allocator allocates memory from a pool of reserved ST-RAM of
  32. * configurable size, set aside on ST-RAM init.
  33. * As long as this pool is not exhausted, allocation of real ST-RAM can be
  34. * guaranteed.
  35. */
  36. /* set if kernel is in ST-RAM */
  37. static int kernel_in_stram;
  38. static struct resource stram_pool = {
  39. .name = "ST-RAM Pool"
  40. };
  41. static unsigned long pool_size = 1024*1024;
  42. static int __init atari_stram_setup(char *arg)
  43. {
  44. if (!MACH_IS_ATARI)
  45. return 0;
  46. pool_size = memparse(arg, NULL);
  47. return 0;
  48. }
  49. early_param("stram_pool", atari_stram_setup);
  50. /*
  51. * This init function is called very early by atari/config.c
  52. * It initializes some internal variables needed for stram_alloc()
  53. */
  54. void __init atari_stram_init(void)
  55. {
  56. int i;
  57. void *stram_start;
  58. /*
  59. * determine whether kernel code resides in ST-RAM
  60. * (then ST-RAM is the first memory block at virtual 0x0)
  61. */
  62. stram_start = phys_to_virt(0);
  63. kernel_in_stram = (stram_start == 0);
  64. for (i = 0; i < m68k_num_memory; ++i) {
  65. if (m68k_memory[i].addr == 0) {
  66. return;
  67. }
  68. }
  69. /* Should never come here! (There is always ST-Ram!) */
  70. panic("atari_stram_init: no ST-RAM found!");
  71. }
  72. /*
  73. * This function is called from setup_arch() to reserve the pages needed for
  74. * ST-RAM management.
  75. */
  76. void __init atari_stram_reserve_pages(void *start_mem)
  77. {
  78. /*
  79. * always reserve first page of ST-RAM, the first 2 KiB are
  80. * supervisor-only!
  81. */
  82. if (!kernel_in_stram)
  83. reserve_bootmem(0, PAGE_SIZE, BOOTMEM_DEFAULT);
  84. stram_pool.start = (resource_size_t)alloc_bootmem_low_pages(pool_size);
  85. stram_pool.end = stram_pool.start + pool_size - 1;
  86. request_resource(&iomem_resource, &stram_pool);
  87. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  88. pool_size, &stram_pool);
  89. }
  90. void *atari_stram_alloc(unsigned long size, const char *owner)
  91. {
  92. struct resource *res;
  93. int error;
  94. pr_debug("atari_stram_alloc: allocate %lu bytes\n", size);
  95. /* round up */
  96. size = PAGE_ALIGN(size);
  97. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  98. if (!res)
  99. return NULL;
  100. res->name = owner;
  101. error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX,
  102. PAGE_SIZE, NULL, NULL);
  103. if (error < 0) {
  104. pr_err("atari_stram_alloc: allocate_resource() failed %d!\n",
  105. error);
  106. kfree(res);
  107. return NULL;
  108. }
  109. pr_debug("atari_stram_alloc: returning %pR\n", res);
  110. return (void *)res->start;
  111. }
  112. EXPORT_SYMBOL(atari_stram_alloc);
  113. void atari_stram_free(void *addr)
  114. {
  115. unsigned long start = (unsigned long)addr;
  116. struct resource *res;
  117. unsigned long size;
  118. res = lookup_resource(&stram_pool, start);
  119. if (!res) {
  120. pr_err("atari_stram_free: trying to free nonexistent region "
  121. "at %p\n", addr);
  122. return;
  123. }
  124. size = resource_size(res);
  125. pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr);
  126. release_resource(res);
  127. kfree(res);
  128. }
  129. EXPORT_SYMBOL(atari_stram_free);