125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f14f75b8SJes Sorensen /* 3e4a064dfSDean Nelson * Copyright (C) 2001-2008 Silicon Graphics, Inc. All rights reserved. 4f14f75b8SJes Sorensen * 5f14f75b8SJes Sorensen * A simple uncached page allocator using the generic allocator. This 6f14f75b8SJes Sorensen * allocator first utilizes the spare (spill) pages found in the EFI 7f14f75b8SJes Sorensen * memmap and will then start converting cached pages to uncached ones 8f14f75b8SJes Sorensen * at a granule at a time. Node awareness is implemented by having a 9f14f75b8SJes Sorensen * pool of pages per node. 10f14f75b8SJes Sorensen */ 11f14f75b8SJes Sorensen 12f14f75b8SJes Sorensen #include <linux/types.h> 13f14f75b8SJes Sorensen #include <linux/kernel.h> 14f14f75b8SJes Sorensen #include <linux/module.h> 15f14f75b8SJes Sorensen #include <linux/init.h> 16f14f75b8SJes Sorensen #include <linux/errno.h> 17f14f75b8SJes Sorensen #include <linux/string.h> 18f14f75b8SJes Sorensen #include <linux/efi.h> 1938b8d208SIngo Molnar #include <linux/nmi.h> 20f14f75b8SJes Sorensen #include <linux/genalloc.h> 215a0e3ad6STejun Heo #include <linux/gfp.h> 22*65fddcfcSMike Rapoport #include <linux/pgtable.h> 23f14f75b8SJes Sorensen #include <asm/page.h> 24f14f75b8SJes Sorensen #include <asm/pal.h> 2560063497SArun Sharma #include <linux/atomic.h> 26f14f75b8SJes Sorensen #include <asm/tlbflush.h> 27f14f75b8SJes Sorensen 28f14f75b8SJes Sorensen 29929f9727SDean Nelson extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *); 30f14f75b8SJes Sorensen 31eca7994fSDean Nelson struct uncached_pool { 32eca7994fSDean Nelson struct gen_pool *pool; 33eca7994fSDean Nelson struct mutex add_chunk_mutex; /* serialize adding a converted chunk */ 34eca7994fSDean Nelson int nchunks_added; /* #of converted chunks added to pool */ 35eca7994fSDean Nelson atomic_t status; /* smp called function's return status*/ 36eca7994fSDean Nelson }; 37f14f75b8SJes Sorensen 38eca7994fSDean Nelson #define MAX_CONVERTED_CHUNKS_PER_NODE 2 39eca7994fSDean Nelson 40eca7994fSDean Nelson struct uncached_pool uncached_pools[MAX_NUMNODES]; 41f14f75b8SJes Sorensen 42f14f75b8SJes Sorensen 43f14f75b8SJes Sorensen static void uncached_ipi_visibility(void *data) 44f14f75b8SJes Sorensen { 45f14f75b8SJes Sorensen int status; 46eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 47f14f75b8SJes Sorensen 48f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 49f14f75b8SJes Sorensen if ((status != PAL_VISIBILITY_OK) && 50f14f75b8SJes Sorensen (status != PAL_VISIBILITY_OK_REMOTE_NEEDED)) 51eca7994fSDean Nelson atomic_inc(&uc_pool->status); 52f14f75b8SJes Sorensen } 53f14f75b8SJes Sorensen 54f14f75b8SJes Sorensen 55f14f75b8SJes Sorensen static void uncached_ipi_mc_drain(void *data) 56f14f75b8SJes Sorensen { 57f14f75b8SJes Sorensen int status; 58eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 59929f9727SDean Nelson 60f14f75b8SJes Sorensen status = ia64_pal_mc_drain(); 61eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 62eca7994fSDean Nelson atomic_inc(&uc_pool->status); 63f14f75b8SJes Sorensen } 64f14f75b8SJes Sorensen 65f14f75b8SJes Sorensen 66929f9727SDean Nelson /* 67929f9727SDean Nelson * Add a new chunk of uncached memory pages to the specified pool. 68929f9727SDean Nelson * 69929f9727SDean Nelson * @pool: pool to add new chunk of uncached memory to 70929f9727SDean Nelson * @nid: node id of node to allocate memory from, or -1 71929f9727SDean Nelson * 72929f9727SDean Nelson * This is accomplished by first allocating a granule of cached memory pages 73929f9727SDean Nelson * and then converting them to uncached memory pages. 74929f9727SDean Nelson */ 75eca7994fSDean Nelson static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid) 76f14f75b8SJes Sorensen { 77f14f75b8SJes Sorensen struct page *page; 78eca7994fSDean Nelson int status, i, nchunks_added = uc_pool->nchunks_added; 79929f9727SDean Nelson unsigned long c_addr, uc_addr; 80f14f75b8SJes Sorensen 81eca7994fSDean Nelson if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0) 82eca7994fSDean Nelson return -1; /* interrupted by a signal */ 83eca7994fSDean Nelson 84eca7994fSDean Nelson if (uc_pool->nchunks_added > nchunks_added) { 85eca7994fSDean Nelson /* someone added a new chunk while we were waiting */ 86eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 87eca7994fSDean Nelson return 0; 88eca7994fSDean Nelson } 89eca7994fSDean Nelson 90eca7994fSDean Nelson if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) { 91eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 92929f9727SDean Nelson return -1; 93eca7994fSDean Nelson } 94f14f75b8SJes Sorensen 95929f9727SDean Nelson /* attempt to allocate a granule's worth of cached memory pages */ 96929f9727SDean Nelson 9796db800fSVlastimil Babka page = __alloc_pages_node(nid, 98e97ca8e5SJohannes Weiner GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE, 99f14f75b8SJes Sorensen IA64_GRANULE_SHIFT-PAGE_SHIFT); 100eca7994fSDean Nelson if (!page) { 101eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 102929f9727SDean Nelson return -1; 103eca7994fSDean Nelson } 104929f9727SDean Nelson 105929f9727SDean Nelson /* convert the memory pages from cached to uncached */ 106929f9727SDean Nelson 107929f9727SDean Nelson c_addr = (unsigned long)page_address(page); 108929f9727SDean Nelson uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET; 109f14f75b8SJes Sorensen 110f14f75b8SJes Sorensen /* 111f14f75b8SJes Sorensen * There's a small race here where it's possible for someone to 112f14f75b8SJes Sorensen * access the page through /dev/mem halfway through the conversion 113f14f75b8SJes Sorensen * to uncached - not sure it's really worth bothering about 114f14f75b8SJes Sorensen */ 115f14f75b8SJes Sorensen for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 116f14f75b8SJes Sorensen SetPageUncached(&page[i]); 117f14f75b8SJes Sorensen 118285fbd66SJan Beulich flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 119f14f75b8SJes Sorensen 120f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 121eca7994fSDean Nelson if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) { 122eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 123caa75932SNadav Amit smp_call_function(uncached_ipi_visibility, uc_pool, 1); 124caa75932SNadav Amit if (atomic_read(&uc_pool->status)) 125929f9727SDean Nelson goto failed; 126eca7994fSDean Nelson } else if (status != PAL_VISIBILITY_OK) 127eca7994fSDean Nelson goto failed; 128f14f75b8SJes Sorensen 129929f9727SDean Nelson preempt_disable(); 130929f9727SDean Nelson 131929f9727SDean Nelson flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 132929f9727SDean Nelson 133929f9727SDean Nelson /* flush the just introduced uncached translation from the TLB */ 134929f9727SDean Nelson local_flush_tlb_all(); 135929f9727SDean Nelson 136929f9727SDean Nelson preempt_enable(); 137f14f75b8SJes Sorensen 138eca7994fSDean Nelson status = ia64_pal_mc_drain(); 139eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 140eca7994fSDean Nelson goto failed; 141eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 142caa75932SNadav Amit smp_call_function(uncached_ipi_mc_drain, uc_pool, 1); 143caa75932SNadav Amit if (atomic_read(&uc_pool->status)) 144929f9727SDean Nelson goto failed; 145f14f75b8SJes Sorensen 146929f9727SDean Nelson /* 147929f9727SDean Nelson * The chunk of memory pages has been converted to uncached so now we 148929f9727SDean Nelson * can add it to the pool. 149929f9727SDean Nelson */ 150eca7994fSDean Nelson status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid); 151929f9727SDean Nelson if (status) 152929f9727SDean Nelson goto failed; 153f14f75b8SJes Sorensen 154eca7994fSDean Nelson uc_pool->nchunks_added++; 155eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 156929f9727SDean Nelson return 0; 157929f9727SDean Nelson 158929f9727SDean Nelson /* failed to convert or add the chunk so give it back to the kernel */ 159929f9727SDean Nelson failed: 160929f9727SDean Nelson for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 161929f9727SDean Nelson ClearPageUncached(&page[i]); 162929f9727SDean Nelson 163929f9727SDean Nelson free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT); 164eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 165929f9727SDean Nelson return -1; 166f14f75b8SJes Sorensen } 167f14f75b8SJes Sorensen 168f14f75b8SJes Sorensen 169f14f75b8SJes Sorensen /* 170f14f75b8SJes Sorensen * uncached_alloc_page 171f14f75b8SJes Sorensen * 172929f9727SDean Nelson * @starting_nid: node id of node to start with, or -1 173e4a064dfSDean Nelson * @n_pages: number of contiguous pages to allocate 174929f9727SDean Nelson * 175e4a064dfSDean Nelson * Allocate the specified number of contiguous uncached pages on the 176e4a064dfSDean Nelson * the requested node. If not enough contiguous uncached pages are available 177e4a064dfSDean Nelson * on the requested node, roundrobin starting with the next higher node. 178f14f75b8SJes Sorensen */ 179e4a064dfSDean Nelson unsigned long uncached_alloc_page(int starting_nid, int n_pages) 180f14f75b8SJes Sorensen { 181929f9727SDean Nelson unsigned long uc_addr; 182eca7994fSDean Nelson struct uncached_pool *uc_pool; 183929f9727SDean Nelson int nid; 184f14f75b8SJes Sorensen 185929f9727SDean Nelson if (unlikely(starting_nid >= MAX_NUMNODES)) 186929f9727SDean Nelson return 0; 187f14f75b8SJes Sorensen 188929f9727SDean Nelson if (starting_nid < 0) 189929f9727SDean Nelson starting_nid = numa_node_id(); 190929f9727SDean Nelson nid = starting_nid; 191f14f75b8SJes Sorensen 192929f9727SDean Nelson do { 1932dca53a9SChristoph Lameter if (!node_state(nid, N_HIGH_MEMORY)) 194f14f75b8SJes Sorensen continue; 195eca7994fSDean Nelson uc_pool = &uncached_pools[nid]; 196eca7994fSDean Nelson if (uc_pool->pool == NULL) 197929f9727SDean Nelson continue; 198929f9727SDean Nelson do { 199e4a064dfSDean Nelson uc_addr = gen_pool_alloc(uc_pool->pool, 200e4a064dfSDean Nelson n_pages * PAGE_SIZE); 201929f9727SDean Nelson if (uc_addr != 0) 202929f9727SDean Nelson return uc_addr; 203eca7994fSDean Nelson } while (uncached_add_chunk(uc_pool, nid) == 0); 204f14f75b8SJes Sorensen 205929f9727SDean Nelson } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); 206929f9727SDean Nelson 207929f9727SDean Nelson return 0; 208f14f75b8SJes Sorensen } 209f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page); 210f14f75b8SJes Sorensen 211f14f75b8SJes Sorensen 212f14f75b8SJes Sorensen /* 213f14f75b8SJes Sorensen * uncached_free_page 214f14f75b8SJes Sorensen * 215e4a064dfSDean Nelson * @uc_addr: uncached address of first page to free 216e4a064dfSDean Nelson * @n_pages: number of contiguous pages to free 217929f9727SDean Nelson * 218e4a064dfSDean Nelson * Free the specified number of uncached pages. 219f14f75b8SJes Sorensen */ 220e4a064dfSDean Nelson void uncached_free_page(unsigned long uc_addr, int n_pages) 221f14f75b8SJes Sorensen { 222929f9727SDean Nelson int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); 223eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 224f14f75b8SJes Sorensen 225929f9727SDean Nelson if (unlikely(pool == NULL)) 226929f9727SDean Nelson return; 227f14f75b8SJes Sorensen 228929f9727SDean Nelson if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) 229929f9727SDean Nelson panic("uncached_free_page invalid address %lx\n", uc_addr); 230f14f75b8SJes Sorensen 231e4a064dfSDean Nelson gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE); 232f14f75b8SJes Sorensen } 233f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page); 234f14f75b8SJes Sorensen 235f14f75b8SJes Sorensen 236f14f75b8SJes Sorensen /* 237f14f75b8SJes Sorensen * uncached_build_memmap, 238f14f75b8SJes Sorensen * 239929f9727SDean Nelson * @uc_start: uncached starting address of a chunk of uncached memory 240929f9727SDean Nelson * @uc_end: uncached ending address of a chunk of uncached memory 241929f9727SDean Nelson * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) 242929f9727SDean Nelson * 243f14f75b8SJes Sorensen * Called at boot time to build a map of pages that can be used for 244f14f75b8SJes Sorensen * memory special operations. 245f14f75b8SJes Sorensen */ 246e088a4adSMatthew Wilcox static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg) 247f14f75b8SJes Sorensen { 248929f9727SDean Nelson int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET); 249eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 250929f9727SDean Nelson size_t size = uc_end - uc_start; 251f14f75b8SJes Sorensen 252386d1d50SJohn Hawkes touch_softlockup_watchdog(); 253f14f75b8SJes Sorensen 254929f9727SDean Nelson if (pool != NULL) { 255929f9727SDean Nelson memset((char *)uc_start, 0, size); 256929f9727SDean Nelson (void) gen_pool_add(pool, uc_start, size, nid); 257f14f75b8SJes Sorensen } 258f14f75b8SJes Sorensen return 0; 259f14f75b8SJes Sorensen } 260f14f75b8SJes Sorensen 261f14f75b8SJes Sorensen 262929f9727SDean Nelson static int __init uncached_init(void) 263929f9727SDean Nelson { 264929f9727SDean Nelson int nid; 265f14f75b8SJes Sorensen 2662dca53a9SChristoph Lameter for_each_node_state(nid, N_ONLINE) { 267eca7994fSDean Nelson uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid); 268eca7994fSDean Nelson mutex_init(&uncached_pools[nid].add_chunk_mutex); 269f14f75b8SJes Sorensen } 270f14f75b8SJes Sorensen 271929f9727SDean Nelson efi_memmap_walk_uc(uncached_build_memmap, NULL); 272f14f75b8SJes Sorensen return 0; 273f14f75b8SJes Sorensen } 274f14f75b8SJes Sorensen 275f14f75b8SJes Sorensen __initcall(uncached_init); 276