stram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <linux/ioport.h>
  24. #include <asm/setup.h>
  25. #include <asm/machdep.h>
  26. #include <asm/page.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/atarihw.h>
  29. #include <asm/atari_stram.h>
  30. #include <asm/io.h>
  31. /*
  32. * The ST-RAM allocator allocates memory from a pool of reserved ST-RAM of
  33. * configurable size, set aside on ST-RAM init.
  34. * As long as this pool is not exhausted, allocation of real ST-RAM can be
  35. * guaranteed.
  36. */
  37. /* set if kernel is in ST-RAM */
  38. static int kernel_in_stram;
  39. static struct resource stram_pool = {
  40. .name = "ST-RAM Pool"
  41. };
  42. static unsigned long pool_size = 1024*1024;
  43. static unsigned long stram_virt_offset;
  44. static int __init atari_stram_setup(char *arg)
  45. {
  46. if (!MACH_IS_ATARI)
  47. return 0;
  48. pool_size = memparse(arg, NULL);
  49. return 0;
  50. }
  51. early_param("stram_pool", atari_stram_setup);
  52. /*
  53. * This init function is called very early by atari/config.c
  54. * It initializes some internal variables needed for stram_alloc()
  55. */
  56. void __init atari_stram_init(void)
  57. {
  58. int i;
  59. /*
  60. * determine whether kernel code resides in ST-RAM
  61. * (then ST-RAM is the first memory block at virtual 0x0)
  62. */
  63. kernel_in_stram = (m68k_memory[0].addr == 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, if the kernel resides in ST-RAM.
  75. */
  76. void __init atari_stram_reserve_pages(void *start_mem)
  77. {
  78. if (kernel_in_stram) {
  79. pr_debug("atari_stram pool: kernel in ST-RAM, using alloc_bootmem!\n");
  80. stram_pool.start = (resource_size_t)alloc_bootmem_low_pages(pool_size);
  81. stram_pool.end = stram_pool.start + pool_size - 1;
  82. request_resource(&iomem_resource, &stram_pool);
  83. stram_virt_offset = 0;
  84. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  85. pool_size, &stram_pool);
  86. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  87. stram_virt_offset);
  88. }
  89. }
  90. /*
  91. * This function is called as arch initcall to reserve the pages needed for
  92. * ST-RAM management, if the kernel does not reside in ST-RAM.
  93. */
  94. int __init atari_stram_map_pages(void)
  95. {
  96. if (!kernel_in_stram) {
  97. /*
  98. * Skip page 0, as the fhe first 2 KiB are supervisor-only!
  99. */
  100. pr_debug("atari_stram pool: kernel not in ST-RAM, using ioremap!\n");
  101. stram_pool.start = PAGE_SIZE;
  102. stram_pool.end = stram_pool.start + pool_size - 1;
  103. request_resource(&iomem_resource, &stram_pool);
  104. stram_virt_offset = (unsigned long) ioremap(stram_pool.start,
  105. resource_size(&stram_pool)) - stram_pool.start;
  106. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  107. pool_size, &stram_pool);
  108. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  109. stram_virt_offset);
  110. }
  111. return 0;
  112. }
  113. arch_initcall(atari_stram_map_pages);
  114. void *atari_stram_to_virt(unsigned long phys)
  115. {
  116. return (void *)(phys + stram_virt_offset);
  117. }
  118. EXPORT_SYMBOL(atari_stram_to_virt);
  119. unsigned long atari_stram_to_phys(void *virt)
  120. {
  121. return (unsigned long)(virt - stram_virt_offset);
  122. }
  123. EXPORT_SYMBOL(atari_stram_to_phys);
  124. void *atari_stram_alloc(unsigned long size, const char *owner)
  125. {
  126. struct resource *res;
  127. int error;
  128. pr_debug("atari_stram_alloc: allocate %lu bytes\n", size);
  129. /* round up */
  130. size = PAGE_ALIGN(size);
  131. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  132. if (!res)
  133. return NULL;
  134. res->name = owner;
  135. error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX,
  136. PAGE_SIZE, NULL, NULL);
  137. if (error < 0) {
  138. pr_err("atari_stram_alloc: allocate_resource() failed %d!\n",
  139. error);
  140. kfree(res);
  141. return NULL;
  142. }
  143. pr_debug("atari_stram_alloc: returning %pR\n", res);
  144. return atari_stram_to_virt(res->start);
  145. }
  146. EXPORT_SYMBOL(atari_stram_alloc);
  147. void atari_stram_free(void *addr)
  148. {
  149. unsigned long start = atari_stram_to_phys(addr);
  150. struct resource *res;
  151. unsigned long size;
  152. res = lookup_resource(&stram_pool, start);
  153. if (!res) {
  154. pr_err("atari_stram_free: trying to free nonexistent region "
  155. "at %p\n", addr);
  156. return;
  157. }
  158. size = resource_size(res);
  159. pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr);
  160. release_resource(res);
  161. kfree(res);
  162. }
  163. EXPORT_SYMBOL(atari_stram_free);