uncached.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. *
  8. * A simple uncached page allocator using the generic allocator. This
  9. * allocator first utilizes the spare (spill) pages found in the EFI
  10. * memmap and will then start converting cached pages to uncached ones
  11. * at a granule at a time. Node awareness is implemented by having a
  12. * pool of pages per node.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/efi.h>
  22. #include <linux/genalloc.h>
  23. #include <asm/page.h>
  24. #include <asm/pal.h>
  25. #include <asm/system.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/atomic.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/sn/arch.h>
  30. extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
  31. struct uncached_pool {
  32. struct gen_pool *pool;
  33. struct mutex add_chunk_mutex; /* serialize adding a converted chunk */
  34. int nchunks_added; /* #of converted chunks added to pool */
  35. atomic_t status; /* smp called function's return status*/
  36. };
  37. #define MAX_CONVERTED_CHUNKS_PER_NODE 2
  38. struct uncached_pool uncached_pools[MAX_NUMNODES];
  39. static void uncached_ipi_visibility(void *data)
  40. {
  41. int status;
  42. struct uncached_pool *uc_pool = (struct uncached_pool *)data;
  43. status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
  44. if ((status != PAL_VISIBILITY_OK) &&
  45. (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
  46. atomic_inc(&uc_pool->status);
  47. }
  48. static void uncached_ipi_mc_drain(void *data)
  49. {
  50. int status;
  51. struct uncached_pool *uc_pool = (struct uncached_pool *)data;
  52. status = ia64_pal_mc_drain();
  53. if (status != PAL_STATUS_SUCCESS)
  54. atomic_inc(&uc_pool->status);
  55. }
  56. /*
  57. * Add a new chunk of uncached memory pages to the specified pool.
  58. *
  59. * @pool: pool to add new chunk of uncached memory to
  60. * @nid: node id of node to allocate memory from, or -1
  61. *
  62. * This is accomplished by first allocating a granule of cached memory pages
  63. * and then converting them to uncached memory pages.
  64. */
  65. static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
  66. {
  67. struct page *page;
  68. int status, i, nchunks_added = uc_pool->nchunks_added;
  69. unsigned long c_addr, uc_addr;
  70. if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
  71. return -1; /* interrupted by a signal */
  72. if (uc_pool->nchunks_added > nchunks_added) {
  73. /* someone added a new chunk while we were waiting */
  74. mutex_unlock(&uc_pool->add_chunk_mutex);
  75. return 0;
  76. }
  77. if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
  78. mutex_unlock(&uc_pool->add_chunk_mutex);
  79. return -1;
  80. }
  81. /* attempt to allocate a granule's worth of cached memory pages */
  82. page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  83. IA64_GRANULE_SHIFT-PAGE_SHIFT);
  84. if (!page) {
  85. mutex_unlock(&uc_pool->add_chunk_mutex);
  86. return -1;
  87. }
  88. /* convert the memory pages from cached to uncached */
  89. c_addr = (unsigned long)page_address(page);
  90. uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
  91. /*
  92. * There's a small race here where it's possible for someone to
  93. * access the page through /dev/mem halfway through the conversion
  94. * to uncached - not sure it's really worth bothering about
  95. */
  96. for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
  97. SetPageUncached(&page[i]);
  98. flush_tlb_kernel_range(uc_addr, uc_adddr + IA64_GRANULE_SIZE);
  99. status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
  100. if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
  101. atomic_set(&uc_pool->status, 0);
  102. status = smp_call_function(uncached_ipi_visibility, uc_pool,
  103. 0, 1);
  104. if (status || atomic_read(&uc_pool->status))
  105. goto failed;
  106. } else if (status != PAL_VISIBILITY_OK)
  107. goto failed;
  108. preempt_disable();
  109. if (ia64_platform_is("sn2"))
  110. sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
  111. else
  112. flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
  113. /* flush the just introduced uncached translation from the TLB */
  114. local_flush_tlb_all();
  115. preempt_enable();
  116. status = ia64_pal_mc_drain();
  117. if (status != PAL_STATUS_SUCCESS)
  118. goto failed;
  119. atomic_set(&uc_pool->status, 0);
  120. status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 0, 1);
  121. if (status || atomic_read(&uc_pool->status))
  122. goto failed;
  123. /*
  124. * The chunk of memory pages has been converted to uncached so now we
  125. * can add it to the pool.
  126. */
  127. status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
  128. if (status)
  129. goto failed;
  130. uc_pool->nchunks_added++;
  131. mutex_unlock(&uc_pool->add_chunk_mutex);
  132. return 0;
  133. /* failed to convert or add the chunk so give it back to the kernel */
  134. failed:
  135. for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
  136. ClearPageUncached(&page[i]);
  137. free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
  138. mutex_unlock(&uc_pool->add_chunk_mutex);
  139. return -1;
  140. }
  141. /*
  142. * uncached_alloc_page
  143. *
  144. * @starting_nid: node id of node to start with, or -1
  145. *
  146. * Allocate 1 uncached page. Allocates on the requested node. If no
  147. * uncached pages are available on the requested node, roundrobin starting
  148. * with the next higher node.
  149. */
  150. unsigned long uncached_alloc_page(int starting_nid)
  151. {
  152. unsigned long uc_addr;
  153. struct uncached_pool *uc_pool;
  154. int nid;
  155. if (unlikely(starting_nid >= MAX_NUMNODES))
  156. return 0;
  157. if (starting_nid < 0)
  158. starting_nid = numa_node_id();
  159. nid = starting_nid;
  160. do {
  161. if (!node_online(nid))
  162. continue;
  163. uc_pool = &uncached_pools[nid];
  164. if (uc_pool->pool == NULL)
  165. continue;
  166. do {
  167. uc_addr = gen_pool_alloc(uc_pool->pool, PAGE_SIZE);
  168. if (uc_addr != 0)
  169. return uc_addr;
  170. } while (uncached_add_chunk(uc_pool, nid) == 0);
  171. } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(uncached_alloc_page);
  175. /*
  176. * uncached_free_page
  177. *
  178. * @uc_addr: uncached address of page to free
  179. *
  180. * Free a single uncached page.
  181. */
  182. void uncached_free_page(unsigned long uc_addr)
  183. {
  184. int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
  185. struct gen_pool *pool = uncached_pools[nid].pool;
  186. if (unlikely(pool == NULL))
  187. return;
  188. if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
  189. panic("uncached_free_page invalid address %lx\n", uc_addr);
  190. gen_pool_free(pool, uc_addr, PAGE_SIZE);
  191. }
  192. EXPORT_SYMBOL(uncached_free_page);
  193. /*
  194. * uncached_build_memmap,
  195. *
  196. * @uc_start: uncached starting address of a chunk of uncached memory
  197. * @uc_end: uncached ending address of a chunk of uncached memory
  198. * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
  199. *
  200. * Called at boot time to build a map of pages that can be used for
  201. * memory special operations.
  202. */
  203. static int __init uncached_build_memmap(unsigned long uc_start,
  204. unsigned long uc_end, void *arg)
  205. {
  206. int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
  207. struct gen_pool *pool = uncached_pools[nid].pool;
  208. size_t size = uc_end - uc_start;
  209. touch_softlockup_watchdog();
  210. if (pool != NULL) {
  211. memset((char *)uc_start, 0, size);
  212. (void) gen_pool_add(pool, uc_start, size, nid);
  213. }
  214. return 0;
  215. }
  216. static int __init uncached_init(void)
  217. {
  218. int nid;
  219. for_each_online_node(nid) {
  220. uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
  221. mutex_init(&uncached_pools[nid].add_chunk_mutex);
  222. }
  223. efi_memmap_walk_uc(uncached_build_memmap, NULL);
  224. return 0;
  225. }
  226. __initcall(uncached_init);