stram.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/memblock.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)memblock_alloc_low(pool_size,
  81. PAGE_SIZE);
  82. stram_pool.end = stram_pool.start + pool_size - 1;
  83. request_resource(&iomem_resource, &stram_pool);
  84. stram_virt_offset = 0;
  85. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  86. pool_size, &stram_pool);
  87. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  88. stram_virt_offset);
  89. }
  90. }
  91. /*
  92. * This function is called as arch initcall to reserve the pages needed for
  93. * ST-RAM management, if the kernel does not reside in ST-RAM.
  94. */
  95. int __init atari_stram_map_pages(void)
  96. {
  97. if (!kernel_in_stram) {
  98. /*
  99. * Skip page 0, as the fhe first 2 KiB are supervisor-only!
  100. */
  101. pr_debug("atari_stram pool: kernel not in ST-RAM, using ioremap!\n");
  102. stram_pool.start = PAGE_SIZE;
  103. stram_pool.end = stram_pool.start + pool_size - 1;
  104. request_resource(&iomem_resource, &stram_pool);
  105. stram_virt_offset = (unsigned long) ioremap(stram_pool.start,
  106. resource_size(&stram_pool)) - stram_pool.start;
  107. pr_debug("atari_stram pool: size = %lu bytes, resource = %pR\n",
  108. pool_size, &stram_pool);
  109. pr_debug("atari_stram pool: stram_virt_offset = %lx\n",
  110. stram_virt_offset);
  111. }
  112. return 0;
  113. }
  114. arch_initcall(atari_stram_map_pages);
  115. void *atari_stram_to_virt(unsigned long phys)
  116. {
  117. return (void *)(phys + stram_virt_offset);
  118. }
  119. EXPORT_SYMBOL(atari_stram_to_virt);
  120. unsigned long atari_stram_to_phys(void *virt)
  121. {
  122. return (unsigned long)(virt - stram_virt_offset);
  123. }
  124. EXPORT_SYMBOL(atari_stram_to_phys);
  125. void *atari_stram_alloc(unsigned long size, const char *owner)
  126. {
  127. struct resource *res;
  128. int error;
  129. pr_debug("atari_stram_alloc: allocate %lu bytes\n", size);
  130. /* round up */
  131. size = PAGE_ALIGN(size);
  132. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  133. if (!res)
  134. return NULL;
  135. res->name = owner;
  136. error = allocate_resource(&stram_pool, res, size, 0, UINT_MAX,
  137. PAGE_SIZE, NULL, NULL);
  138. if (error < 0) {
  139. pr_err("atari_stram_alloc: allocate_resource() failed %d!\n",
  140. error);
  141. kfree(res);
  142. return NULL;
  143. }
  144. pr_debug("atari_stram_alloc: returning %pR\n", res);
  145. return atari_stram_to_virt(res->start);
  146. }
  147. EXPORT_SYMBOL(atari_stram_alloc);
  148. void atari_stram_free(void *addr)
  149. {
  150. unsigned long start = atari_stram_to_phys(addr);
  151. struct resource *res;
  152. unsigned long size;
  153. res = lookup_resource(&stram_pool, start);
  154. if (!res) {
  155. pr_err("atari_stram_free: trying to free nonexistent region "
  156. "at %p\n", addr);
  157. return;
  158. }
  159. size = resource_size(res);
  160. pr_debug("atari_stram_free: free %lu bytes at %p\n", size, addr);
  161. release_resource(res);
  162. kfree(res);
  163. }
  164. EXPORT_SYMBOL(atari_stram_free);