uncached.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2001-2008 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/efi.h>
  21. #include <linux/genalloc.h>
  22. #include <linux/gfp.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_exact_node(nid,
  83. GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  84. IA64_GRANULE_SHIFT-PAGE_SHIFT);
  85. if (!page) {
  86. mutex_unlock(&uc_pool->add_chunk_mutex);
  87. return -1;
  88. }
  89. /* convert the memory pages from cached to uncached */
  90. c_addr = (unsigned long)page_address(page);
  91. uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET;
  92. /*
  93. * There's a small race here where it's possible for someone to
  94. * access the page through /dev/mem halfway through the conversion
  95. * to uncached - not sure it's really worth bothering about
  96. */
  97. for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
  98. SetPageUncached(&page[i]);
  99. flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
  100. status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
  101. if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) {
  102. atomic_set(&uc_pool->status, 0);
  103. status = smp_call_function(uncached_ipi_visibility, uc_pool, 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, 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. * @n_pages: number of contiguous pages to allocate
  146. *
  147. * Allocate the specified number of contiguous uncached pages on the
  148. * the requested node. If not enough contiguous uncached pages are available
  149. * on the requested node, roundrobin starting with the next higher node.
  150. */
  151. unsigned long uncached_alloc_page(int starting_nid, int n_pages)
  152. {
  153. unsigned long uc_addr;
  154. struct uncached_pool *uc_pool;
  155. int nid;
  156. if (unlikely(starting_nid >= MAX_NUMNODES))
  157. return 0;
  158. if (starting_nid < 0)
  159. starting_nid = numa_node_id();
  160. nid = starting_nid;
  161. do {
  162. if (!node_state(nid, N_HIGH_MEMORY))
  163. continue;
  164. uc_pool = &uncached_pools[nid];
  165. if (uc_pool->pool == NULL)
  166. continue;
  167. do {
  168. uc_addr = gen_pool_alloc(uc_pool->pool,
  169. n_pages * PAGE_SIZE);
  170. if (uc_addr != 0)
  171. return uc_addr;
  172. } while (uncached_add_chunk(uc_pool, nid) == 0);
  173. } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(uncached_alloc_page);
  177. /*
  178. * uncached_free_page
  179. *
  180. * @uc_addr: uncached address of first page to free
  181. * @n_pages: number of contiguous pages to free
  182. *
  183. * Free the specified number of uncached pages.
  184. */
  185. void uncached_free_page(unsigned long uc_addr, int n_pages)
  186. {
  187. int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
  188. struct gen_pool *pool = uncached_pools[nid].pool;
  189. if (unlikely(pool == NULL))
  190. return;
  191. if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
  192. panic("uncached_free_page invalid address %lx\n", uc_addr);
  193. gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
  194. }
  195. EXPORT_SYMBOL(uncached_free_page);
  196. /*
  197. * uncached_build_memmap,
  198. *
  199. * @uc_start: uncached starting address of a chunk of uncached memory
  200. * @uc_end: uncached ending address of a chunk of uncached memory
  201. * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
  202. *
  203. * Called at boot time to build a map of pages that can be used for
  204. * memory special operations.
  205. */
  206. static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
  207. {
  208. int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
  209. struct gen_pool *pool = uncached_pools[nid].pool;
  210. size_t size = uc_end - uc_start;
  211. touch_softlockup_watchdog();
  212. if (pool != NULL) {
  213. memset((char *)uc_start, 0, size);
  214. (void) gen_pool_add(pool, uc_start, size, nid);
  215. }
  216. return 0;
  217. }
  218. static int __init uncached_init(void)
  219. {
  220. int nid;
  221. for_each_node_state(nid, N_ONLINE) {
  222. uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
  223. mutex_init(&uncached_pools[nid].add_chunk_mutex);
  224. }
  225. efi_memmap_walk_uc(uncached_build_memmap, NULL);
  226. return 0;
  227. }
  228. __initcall(uncached_init);