uncached.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/pgtable.h>
  26. #include <linux/atomic.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/sn/arch.h>
  29. extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *);
  30. struct uncached_pool {
  31. struct gen_pool *pool;
  32. struct mutex add_chunk_mutex; /* serialize adding a converted chunk */
  33. int nchunks_added; /* #of converted chunks added to pool */
  34. atomic_t status; /* smp called function's return status*/
  35. };
  36. #define MAX_CONVERTED_CHUNKS_PER_NODE 2
  37. struct uncached_pool uncached_pools[MAX_NUMNODES];
  38. static void uncached_ipi_visibility(void *data)
  39. {
  40. int status;
  41. struct uncached_pool *uc_pool = (struct uncached_pool *)data;
  42. status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL);
  43. if ((status != PAL_VISIBILITY_OK) &&
  44. (status != PAL_VISIBILITY_OK_REMOTE_NEEDED))
  45. atomic_inc(&uc_pool->status);
  46. }
  47. static void uncached_ipi_mc_drain(void *data)
  48. {
  49. int status;
  50. struct uncached_pool *uc_pool = (struct uncached_pool *)data;
  51. status = ia64_pal_mc_drain();
  52. if (status != PAL_STATUS_SUCCESS)
  53. atomic_inc(&uc_pool->status);
  54. }
  55. /*
  56. * Add a new chunk of uncached memory pages to the specified pool.
  57. *
  58. * @pool: pool to add new chunk of uncached memory to
  59. * @nid: node id of node to allocate memory from, or -1
  60. *
  61. * This is accomplished by first allocating a granule of cached memory pages
  62. * and then converting them to uncached memory pages.
  63. */
  64. static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid)
  65. {
  66. struct page *page;
  67. int status, i, nchunks_added = uc_pool->nchunks_added;
  68. unsigned long c_addr, uc_addr;
  69. if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0)
  70. return -1; /* interrupted by a signal */
  71. if (uc_pool->nchunks_added > nchunks_added) {
  72. /* someone added a new chunk while we were waiting */
  73. mutex_unlock(&uc_pool->add_chunk_mutex);
  74. return 0;
  75. }
  76. if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) {
  77. mutex_unlock(&uc_pool->add_chunk_mutex);
  78. return -1;
  79. }
  80. /* attempt to allocate a granule's worth of cached memory pages */
  81. page = __alloc_pages_node(nid,
  82. 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_addr + 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, 1);
  103. if (status || atomic_read(&uc_pool->status))
  104. goto failed;
  105. } else if (status != PAL_VISIBILITY_OK)
  106. goto failed;
  107. preempt_disable();
  108. if (ia64_platform_is("sn2"))
  109. sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE);
  110. else
  111. flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE);
  112. /* flush the just introduced uncached translation from the TLB */
  113. local_flush_tlb_all();
  114. preempt_enable();
  115. status = ia64_pal_mc_drain();
  116. if (status != PAL_STATUS_SUCCESS)
  117. goto failed;
  118. atomic_set(&uc_pool->status, 0);
  119. status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1);
  120. if (status || atomic_read(&uc_pool->status))
  121. goto failed;
  122. /*
  123. * The chunk of memory pages has been converted to uncached so now we
  124. * can add it to the pool.
  125. */
  126. status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid);
  127. if (status)
  128. goto failed;
  129. uc_pool->nchunks_added++;
  130. mutex_unlock(&uc_pool->add_chunk_mutex);
  131. return 0;
  132. /* failed to convert or add the chunk so give it back to the kernel */
  133. failed:
  134. for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++)
  135. ClearPageUncached(&page[i]);
  136. free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT);
  137. mutex_unlock(&uc_pool->add_chunk_mutex);
  138. return -1;
  139. }
  140. /*
  141. * uncached_alloc_page
  142. *
  143. * @starting_nid: node id of node to start with, or -1
  144. * @n_pages: number of contiguous pages to allocate
  145. *
  146. * Allocate the specified number of contiguous uncached pages on the
  147. * the requested node. If not enough contiguous uncached pages are available
  148. * on the requested node, roundrobin starting with the next higher node.
  149. */
  150. unsigned long uncached_alloc_page(int starting_nid, int n_pages)
  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_state(nid, N_HIGH_MEMORY))
  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,
  168. n_pages * PAGE_SIZE);
  169. if (uc_addr != 0)
  170. return uc_addr;
  171. } while (uncached_add_chunk(uc_pool, nid) == 0);
  172. } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(uncached_alloc_page);
  176. /*
  177. * uncached_free_page
  178. *
  179. * @uc_addr: uncached address of first page to free
  180. * @n_pages: number of contiguous pages to free
  181. *
  182. * Free the specified number of uncached pages.
  183. */
  184. void uncached_free_page(unsigned long uc_addr, int n_pages)
  185. {
  186. int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET);
  187. struct gen_pool *pool = uncached_pools[nid].pool;
  188. if (unlikely(pool == NULL))
  189. return;
  190. if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET)
  191. panic("uncached_free_page invalid address %lx\n", uc_addr);
  192. gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE);
  193. }
  194. EXPORT_SYMBOL(uncached_free_page);
  195. /*
  196. * uncached_build_memmap,
  197. *
  198. * @uc_start: uncached starting address of a chunk of uncached memory
  199. * @uc_end: uncached ending address of a chunk of uncached memory
  200. * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc())
  201. *
  202. * Called at boot time to build a map of pages that can be used for
  203. * memory special operations.
  204. */
  205. static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg)
  206. {
  207. int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET);
  208. struct gen_pool *pool = uncached_pools[nid].pool;
  209. size_t size = uc_end - uc_start;
  210. touch_softlockup_watchdog();
  211. if (pool != NULL) {
  212. memset((char *)uc_start, 0, size);
  213. (void) gen_pool_add(pool, uc_start, size, nid);
  214. }
  215. return 0;
  216. }
  217. static int __init uncached_init(void)
  218. {
  219. int nid;
  220. for_each_node_state(nid, N_ONLINE) {
  221. uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid);
  222. mutex_init(&uncached_pools[nid].add_chunk_mutex);
  223. }
  224. efi_memmap_walk_uc(uncached_build_memmap, NULL);
  225. return 0;
  226. }
  227. __initcall(uncached_init);