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> 2265fddcfcSMike Rapoport #include <linux/pgtable.h> 23*8ff059b8SArd Biesheuvel #include <asm/efi.h> 24f14f75b8SJes Sorensen #include <asm/page.h> 25f14f75b8SJes Sorensen #include <asm/pal.h> 2660063497SArun Sharma #include <linux/atomic.h> 27f14f75b8SJes Sorensen #include <asm/tlbflush.h> 28f14f75b8SJes Sorensen 29eca7994fSDean Nelson struct uncached_pool { 30eca7994fSDean Nelson struct gen_pool *pool; 31eca7994fSDean Nelson struct mutex add_chunk_mutex; /* serialize adding a converted chunk */ 32eca7994fSDean Nelson int nchunks_added; /* #of converted chunks added to pool */ 33eca7994fSDean Nelson atomic_t status; /* smp called function's return status*/ 34eca7994fSDean Nelson }; 35f14f75b8SJes Sorensen 36eca7994fSDean Nelson #define MAX_CONVERTED_CHUNKS_PER_NODE 2 37eca7994fSDean Nelson 38eca7994fSDean Nelson struct uncached_pool uncached_pools[MAX_NUMNODES]; 39f14f75b8SJes Sorensen 40f14f75b8SJes Sorensen 41f14f75b8SJes Sorensen static void uncached_ipi_visibility(void *data) 42f14f75b8SJes Sorensen { 43f14f75b8SJes Sorensen int status; 44eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 45f14f75b8SJes Sorensen 46f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 47f14f75b8SJes Sorensen if ((status != PAL_VISIBILITY_OK) && 48f14f75b8SJes Sorensen (status != PAL_VISIBILITY_OK_REMOTE_NEEDED)) 49eca7994fSDean Nelson atomic_inc(&uc_pool->status); 50f14f75b8SJes Sorensen } 51f14f75b8SJes Sorensen 52f14f75b8SJes Sorensen 53f14f75b8SJes Sorensen static void uncached_ipi_mc_drain(void *data) 54f14f75b8SJes Sorensen { 55f14f75b8SJes Sorensen int status; 56eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 57929f9727SDean Nelson 58f14f75b8SJes Sorensen status = ia64_pal_mc_drain(); 59eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 60eca7994fSDean Nelson atomic_inc(&uc_pool->status); 61f14f75b8SJes Sorensen } 62f14f75b8SJes Sorensen 63f14f75b8SJes Sorensen 64929f9727SDean Nelson /* 65929f9727SDean Nelson * Add a new chunk of uncached memory pages to the specified pool. 66929f9727SDean Nelson * 67929f9727SDean Nelson * @pool: pool to add new chunk of uncached memory to 68929f9727SDean Nelson * @nid: node id of node to allocate memory from, or -1 69929f9727SDean Nelson * 70929f9727SDean Nelson * This is accomplished by first allocating a granule of cached memory pages 71929f9727SDean Nelson * and then converting them to uncached memory pages. 72929f9727SDean Nelson */ 73eca7994fSDean Nelson static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid) 74f14f75b8SJes Sorensen { 75f14f75b8SJes Sorensen struct page *page; 76eca7994fSDean Nelson int status, i, nchunks_added = uc_pool->nchunks_added; 77929f9727SDean Nelson unsigned long c_addr, uc_addr; 78f14f75b8SJes Sorensen 79eca7994fSDean Nelson if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0) 80eca7994fSDean Nelson return -1; /* interrupted by a signal */ 81eca7994fSDean Nelson 82eca7994fSDean Nelson if (uc_pool->nchunks_added > nchunks_added) { 83eca7994fSDean Nelson /* someone added a new chunk while we were waiting */ 84eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 85eca7994fSDean Nelson return 0; 86eca7994fSDean Nelson } 87eca7994fSDean Nelson 88eca7994fSDean Nelson if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) { 89eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 90929f9727SDean Nelson return -1; 91eca7994fSDean Nelson } 92f14f75b8SJes Sorensen 93929f9727SDean Nelson /* attempt to allocate a granule's worth of cached memory pages */ 94929f9727SDean Nelson 9596db800fSVlastimil Babka page = __alloc_pages_node(nid, 96e97ca8e5SJohannes Weiner GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE, 97f14f75b8SJes Sorensen IA64_GRANULE_SHIFT-PAGE_SHIFT); 98eca7994fSDean Nelson if (!page) { 99eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 100929f9727SDean Nelson return -1; 101eca7994fSDean Nelson } 102929f9727SDean Nelson 103929f9727SDean Nelson /* convert the memory pages from cached to uncached */ 104929f9727SDean Nelson 105929f9727SDean Nelson c_addr = (unsigned long)page_address(page); 106929f9727SDean Nelson uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET; 107f14f75b8SJes Sorensen 108f14f75b8SJes Sorensen /* 109f14f75b8SJes Sorensen * There's a small race here where it's possible for someone to 110f14f75b8SJes Sorensen * access the page through /dev/mem halfway through the conversion 111f14f75b8SJes Sorensen * to uncached - not sure it's really worth bothering about 112f14f75b8SJes Sorensen */ 113f14f75b8SJes Sorensen for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 114f14f75b8SJes Sorensen SetPageUncached(&page[i]); 115f14f75b8SJes Sorensen 116285fbd66SJan Beulich flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 117f14f75b8SJes Sorensen 118f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 119eca7994fSDean Nelson if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) { 120eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 121caa75932SNadav Amit smp_call_function(uncached_ipi_visibility, uc_pool, 1); 122caa75932SNadav Amit if (atomic_read(&uc_pool->status)) 123929f9727SDean Nelson goto failed; 124eca7994fSDean Nelson } else if (status != PAL_VISIBILITY_OK) 125eca7994fSDean Nelson goto failed; 126f14f75b8SJes Sorensen 127929f9727SDean Nelson preempt_disable(); 128929f9727SDean Nelson 129929f9727SDean Nelson flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 130929f9727SDean Nelson 131929f9727SDean Nelson /* flush the just introduced uncached translation from the TLB */ 132929f9727SDean Nelson local_flush_tlb_all(); 133929f9727SDean Nelson 134929f9727SDean Nelson preempt_enable(); 135f14f75b8SJes Sorensen 136eca7994fSDean Nelson status = ia64_pal_mc_drain(); 137eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 138eca7994fSDean Nelson goto failed; 139eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 140caa75932SNadav Amit smp_call_function(uncached_ipi_mc_drain, uc_pool, 1); 141caa75932SNadav Amit if (atomic_read(&uc_pool->status)) 142929f9727SDean Nelson goto failed; 143f14f75b8SJes Sorensen 144929f9727SDean Nelson /* 145929f9727SDean Nelson * The chunk of memory pages has been converted to uncached so now we 146929f9727SDean Nelson * can add it to the pool. 147929f9727SDean Nelson */ 148eca7994fSDean Nelson status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid); 149929f9727SDean Nelson if (status) 150929f9727SDean Nelson goto failed; 151f14f75b8SJes Sorensen 152eca7994fSDean Nelson uc_pool->nchunks_added++; 153eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 154929f9727SDean Nelson return 0; 155929f9727SDean Nelson 156929f9727SDean Nelson /* failed to convert or add the chunk so give it back to the kernel */ 157929f9727SDean Nelson failed: 158929f9727SDean Nelson for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 159929f9727SDean Nelson ClearPageUncached(&page[i]); 160929f9727SDean Nelson 161929f9727SDean Nelson free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT); 162eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 163929f9727SDean Nelson return -1; 164f14f75b8SJes Sorensen } 165f14f75b8SJes Sorensen 166f14f75b8SJes Sorensen 167f14f75b8SJes Sorensen /* 168f14f75b8SJes Sorensen * uncached_alloc_page 169f14f75b8SJes Sorensen * 170929f9727SDean Nelson * @starting_nid: node id of node to start with, or -1 171e4a064dfSDean Nelson * @n_pages: number of contiguous pages to allocate 172929f9727SDean Nelson * 173e4a064dfSDean Nelson * Allocate the specified number of contiguous uncached pages on the 174e4a064dfSDean Nelson * the requested node. If not enough contiguous uncached pages are available 175e4a064dfSDean Nelson * on the requested node, roundrobin starting with the next higher node. 176f14f75b8SJes Sorensen */ 177e4a064dfSDean Nelson unsigned long uncached_alloc_page(int starting_nid, int n_pages) 178f14f75b8SJes Sorensen { 179929f9727SDean Nelson unsigned long uc_addr; 180eca7994fSDean Nelson struct uncached_pool *uc_pool; 181929f9727SDean Nelson int nid; 182f14f75b8SJes Sorensen 183929f9727SDean Nelson if (unlikely(starting_nid >= MAX_NUMNODES)) 184929f9727SDean Nelson return 0; 185f14f75b8SJes Sorensen 186929f9727SDean Nelson if (starting_nid < 0) 187929f9727SDean Nelson starting_nid = numa_node_id(); 188929f9727SDean Nelson nid = starting_nid; 189f14f75b8SJes Sorensen 190929f9727SDean Nelson do { 1912dca53a9SChristoph Lameter if (!node_state(nid, N_HIGH_MEMORY)) 192f14f75b8SJes Sorensen continue; 193eca7994fSDean Nelson uc_pool = &uncached_pools[nid]; 194eca7994fSDean Nelson if (uc_pool->pool == NULL) 195929f9727SDean Nelson continue; 196929f9727SDean Nelson do { 197e4a064dfSDean Nelson uc_addr = gen_pool_alloc(uc_pool->pool, 198e4a064dfSDean Nelson n_pages * PAGE_SIZE); 199929f9727SDean Nelson if (uc_addr != 0) 200929f9727SDean Nelson return uc_addr; 201eca7994fSDean Nelson } while (uncached_add_chunk(uc_pool, nid) == 0); 202f14f75b8SJes Sorensen 203929f9727SDean Nelson } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); 204929f9727SDean Nelson 205929f9727SDean Nelson return 0; 206f14f75b8SJes Sorensen } 207f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page); 208f14f75b8SJes Sorensen 209f14f75b8SJes Sorensen 210f14f75b8SJes Sorensen /* 211f14f75b8SJes Sorensen * uncached_free_page 212f14f75b8SJes Sorensen * 213e4a064dfSDean Nelson * @uc_addr: uncached address of first page to free 214e4a064dfSDean Nelson * @n_pages: number of contiguous pages to free 215929f9727SDean Nelson * 216e4a064dfSDean Nelson * Free the specified number of uncached pages. 217f14f75b8SJes Sorensen */ 218e4a064dfSDean Nelson void uncached_free_page(unsigned long uc_addr, int n_pages) 219f14f75b8SJes Sorensen { 220929f9727SDean Nelson int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); 221eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 222f14f75b8SJes Sorensen 223929f9727SDean Nelson if (unlikely(pool == NULL)) 224929f9727SDean Nelson return; 225f14f75b8SJes Sorensen 226929f9727SDean Nelson if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) 227929f9727SDean Nelson panic("uncached_free_page invalid address %lx\n", uc_addr); 228f14f75b8SJes Sorensen 229e4a064dfSDean Nelson gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE); 230f14f75b8SJes Sorensen } 231f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page); 232f14f75b8SJes Sorensen 233f14f75b8SJes Sorensen 234f14f75b8SJes Sorensen /* 235f14f75b8SJes Sorensen * uncached_build_memmap, 236f14f75b8SJes Sorensen * 237929f9727SDean Nelson * @uc_start: uncached starting address of a chunk of uncached memory 238929f9727SDean Nelson * @uc_end: uncached ending address of a chunk of uncached memory 239929f9727SDean Nelson * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) 240929f9727SDean Nelson * 241f14f75b8SJes Sorensen * Called at boot time to build a map of pages that can be used for 242f14f75b8SJes Sorensen * memory special operations. 243f14f75b8SJes Sorensen */ 244e088a4adSMatthew Wilcox static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg) 245f14f75b8SJes Sorensen { 246929f9727SDean Nelson int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET); 247eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 248929f9727SDean Nelson size_t size = uc_end - uc_start; 249f14f75b8SJes Sorensen 250386d1d50SJohn Hawkes touch_softlockup_watchdog(); 251f14f75b8SJes Sorensen 252929f9727SDean Nelson if (pool != NULL) { 253929f9727SDean Nelson memset((char *)uc_start, 0, size); 254929f9727SDean Nelson (void) gen_pool_add(pool, uc_start, size, nid); 255f14f75b8SJes Sorensen } 256f14f75b8SJes Sorensen return 0; 257f14f75b8SJes Sorensen } 258f14f75b8SJes Sorensen 259f14f75b8SJes Sorensen 260929f9727SDean Nelson static int __init uncached_init(void) 261929f9727SDean Nelson { 262929f9727SDean Nelson int nid; 263f14f75b8SJes Sorensen 2642dca53a9SChristoph Lameter for_each_node_state(nid, N_ONLINE) { 265eca7994fSDean Nelson uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid); 266eca7994fSDean Nelson mutex_init(&uncached_pools[nid].add_chunk_mutex); 267f14f75b8SJes Sorensen } 268f14f75b8SJes Sorensen 269929f9727SDean Nelson efi_memmap_walk_uc(uncached_build_memmap, NULL); 270f14f75b8SJes Sorensen return 0; 271f14f75b8SJes Sorensen } 272f14f75b8SJes Sorensen 273f14f75b8SJes Sorensen __initcall(uncached_init); 274