stram.c 4.9 KB

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