uncached.c 7.2 KB

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