stram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 unsigned long stram_virt_offset;
  43. static int __init atari_stram_setup(char *arg)
  44. {
  45. if (!MACH_IS_ATARI)
  46. return 0;
  47. pool_size = memparse(arg, NULL);
  48. return 0;
  49. }
  50. early_param("stram_pool", atari_stram_setup);
  51. /*
  52. * This init function is called very early by atari/config.c
  53. * It initializes some internal variables needed for stram_alloc()
  54. */
  55. void __init atari_stram_init(void)
  56. {
  57. int i;
  58. /*
  59. * determine whether kernel code resides in ST-RAM
  60. * (then ST-RAM is the first memory block at virtual 0x0)
  61. */
  62. kernel_in_stram = (m68k_memory[0].addr == 0);
  63. for (i = 0; i < m68k_num_memory; ++i) {
  64. if (m68k_memory[i].addr == 0) {
  65. return;
  66. }
  67. }
  68. /* Should never come here! (There is always ST-Ram!) */
  69. panic("atari_stram_init: no ST-RAM found!");
  70. }
  71. /*
  72. * This function is called from setup_arch() to reserve the pages needed for
  73. * ST-RAM management, if the kernel resides in ST-RAM.
  74. */
  75. void __init atari_stram_reserve_pages(void *start_mem)
  76. {
  77. if (kernel_in_stram) {
  78. pr_debug("atari_stram pool: kernel in ST-RAM, using alloc_bootmem!\n");
  79. stram_pool.start = (resource_size_t)alloc_bootmem_low_pages(pool_size);
  80. stram_pool.end = stram_pool.start + pool_size - 1;
  81. request_resource(&iomem_resource, &stram_pool);
  82. stram_virt_offset = 0;
  83. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  84. pool_size, &stram_pool);
  85. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  86. stram_virt_offset);
  87. }
  88. }
  89. /*
  90. * This function is called as arch initcall to reserve the pages needed for
  91. * ST-RAM management, if the kernel does not reside in ST-RAM.
  92. */
  93. int __init atari_stram_map_pages(void)
  94. {
  95. if (!kernel_in_stram) {
  96. /*
  97. * Skip page 0, as the fhe first 2 KiB are supervisor-only!
  98. */
  99. pr_debug("atari_stram pool: kernel not in ST-RAM, using ioremap!\n");
  100. stram_pool.start = PAGE_SIZE;
  101. stram_pool.end = stram_pool.start + pool_size - 1;
  102. request_resource(&iomem_resource, &stram_pool);
  103. stram_virt_offset = (unsigned long) ioremap(stram_pool.start,
  104. resource_size(&stram_pool)) - stram_pool.start;
  105. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  106. pool_size, &stram_pool);
  107. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  108. stram_virt_offset);
  109. }
  110. return 0;
  111. }
  112. arch_initcall(atari_stram_map_pages);
  113. void *atari_stram_to_virt(unsigned long phys)
  114. {
  115. return (void *)(phys + stram_virt_offset);
  116. }
  117. EXPORT_SYMBOL(atari_stram_to_virt);
  118. unsigned long atari_stram_to_phys(void *virt)
  119. {
  120. return (unsigned long)(virt - stram_virt_offset);
  121. }
  122. EXPORT_SYMBOL(atari_stram_to_phys);
  123. void *atari_stram_alloc(unsigned long size, const char *owner)
  124. {
  125. struct resource *res;
  126. int error;
  127. pr_debug("atari_stram_alloc: allocate %lu bytes\n", size);
  128. /* round up */
  129. size = PAGE_ALIGN(size);
  130. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  131. if (!res)
  132. return NULL;
  133. res->name = owner;
  134. error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX,
  135. PAGE_SIZE, NULL, NULL);
  136. if (error < 0) {
  137. pr_err("atari_stram_alloc: allocate_resource() failed %d!\n",
  138. error);
  139. kfree(res);
  140. return NULL;
  141. }
  142. pr_debug("atari_stram_alloc: returning %pR\n", res);
  143. return atari_stram_to_virt(res->start);
  144. }
  145. EXPORT_SYMBOL(atari_stram_alloc);
  146. void atari_stram_free(void *addr)
  147. {
  148. unsigned long start = atari_stram_to_phys(addr);
  149. struct resource *res;
  150. unsigned long size;
  151. res = lookup_resource(&stram_pool, start);
  152. if (!res) {
  153. pr_err("atari_stram_free: trying to free nonexistent region "
  154. "at %p\n", addr);
  155. return;
  156. }
  157. size = resource_size(res);
  158. pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr);
  159. release_resource(res);
  160. kfree(res);
  161. }
  162. EXPORT_SYMBOL(atari_stram_free);