1f14f75b8SJes Sorensen /* 2e4a064dfSDean Nelson * Copyright (C) 2001-2008 Silicon Graphics, Inc. All rights reserved. 3f14f75b8SJes Sorensen * 4f14f75b8SJes Sorensen * This program is free software; you can redistribute it and/or modify it 5f14f75b8SJes Sorensen * under the terms of version 2 of the GNU General Public License 6f14f75b8SJes Sorensen * as published by the Free Software Foundation. 7f14f75b8SJes Sorensen * 8f14f75b8SJes Sorensen * A simple uncached page allocator using the generic allocator. This 9f14f75b8SJes Sorensen * allocator first utilizes the spare (spill) pages found in the EFI 10f14f75b8SJes Sorensen * memmap and will then start converting cached pages to uncached ones 11f14f75b8SJes Sorensen * at a granule at a time. Node awareness is implemented by having a 12f14f75b8SJes Sorensen * pool of pages per node. 13f14f75b8SJes Sorensen */ 14f14f75b8SJes Sorensen 15f14f75b8SJes Sorensen #include <linux/types.h> 16f14f75b8SJes Sorensen #include <linux/kernel.h> 17f14f75b8SJes Sorensen #include <linux/module.h> 18f14f75b8SJes Sorensen #include <linux/init.h> 19f14f75b8SJes Sorensen #include <linux/errno.h> 20f14f75b8SJes Sorensen #include <linux/string.h> 21f14f75b8SJes Sorensen #include <linux/efi.h> 22*38b8d208SIngo Molnar #include <linux/nmi.h> 23f14f75b8SJes Sorensen #include <linux/genalloc.h> 245a0e3ad6STejun Heo #include <linux/gfp.h> 25f14f75b8SJes Sorensen #include <asm/page.h> 26f14f75b8SJes Sorensen #include <asm/pal.h> 27f14f75b8SJes Sorensen #include <asm/pgtable.h> 2860063497SArun Sharma #include <linux/atomic.h> 29f14f75b8SJes Sorensen #include <asm/tlbflush.h> 30f14f75b8SJes Sorensen #include <asm/sn/arch.h> 31f14f75b8SJes Sorensen 32f14f75b8SJes Sorensen 33929f9727SDean Nelson extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *); 34f14f75b8SJes Sorensen 35eca7994fSDean Nelson struct uncached_pool { 36eca7994fSDean Nelson struct gen_pool *pool; 37eca7994fSDean Nelson struct mutex add_chunk_mutex; /* serialize adding a converted chunk */ 38eca7994fSDean Nelson int nchunks_added; /* #of converted chunks added to pool */ 39eca7994fSDean Nelson atomic_t status; /* smp called function's return status*/ 40eca7994fSDean Nelson }; 41f14f75b8SJes Sorensen 42eca7994fSDean Nelson #define MAX_CONVERTED_CHUNKS_PER_NODE 2 43eca7994fSDean Nelson 44eca7994fSDean Nelson struct uncached_pool uncached_pools[MAX_NUMNODES]; 45f14f75b8SJes Sorensen 46f14f75b8SJes Sorensen 47f14f75b8SJes Sorensen static void uncached_ipi_visibility(void *data) 48f14f75b8SJes Sorensen { 49f14f75b8SJes Sorensen int status; 50eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 51f14f75b8SJes Sorensen 52f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 53f14f75b8SJes Sorensen if ((status != PAL_VISIBILITY_OK) && 54f14f75b8SJes Sorensen (status != PAL_VISIBILITY_OK_REMOTE_NEEDED)) 55eca7994fSDean Nelson atomic_inc(&uc_pool->status); 56f14f75b8SJes Sorensen } 57f14f75b8SJes Sorensen 58f14f75b8SJes Sorensen 59f14f75b8SJes Sorensen static void uncached_ipi_mc_drain(void *data) 60f14f75b8SJes Sorensen { 61f14f75b8SJes Sorensen int status; 62eca7994fSDean Nelson struct uncached_pool *uc_pool = (struct uncached_pool *)data; 63929f9727SDean Nelson 64f14f75b8SJes Sorensen status = ia64_pal_mc_drain(); 65eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 66eca7994fSDean Nelson atomic_inc(&uc_pool->status); 67f14f75b8SJes Sorensen } 68f14f75b8SJes Sorensen 69f14f75b8SJes Sorensen 70929f9727SDean Nelson /* 71929f9727SDean Nelson * Add a new chunk of uncached memory pages to the specified pool. 72929f9727SDean Nelson * 73929f9727SDean Nelson * @pool: pool to add new chunk of uncached memory to 74929f9727SDean Nelson * @nid: node id of node to allocate memory from, or -1 75929f9727SDean Nelson * 76929f9727SDean Nelson * This is accomplished by first allocating a granule of cached memory pages 77929f9727SDean Nelson * and then converting them to uncached memory pages. 78929f9727SDean Nelson */ 79eca7994fSDean Nelson static int uncached_add_chunk(struct uncached_pool *uc_pool, int nid) 80f14f75b8SJes Sorensen { 81f14f75b8SJes Sorensen struct page *page; 82eca7994fSDean Nelson int status, i, nchunks_added = uc_pool->nchunks_added; 83929f9727SDean Nelson unsigned long c_addr, uc_addr; 84f14f75b8SJes Sorensen 85eca7994fSDean Nelson if (mutex_lock_interruptible(&uc_pool->add_chunk_mutex) != 0) 86eca7994fSDean Nelson return -1; /* interrupted by a signal */ 87eca7994fSDean Nelson 88eca7994fSDean Nelson if (uc_pool->nchunks_added > nchunks_added) { 89eca7994fSDean Nelson /* someone added a new chunk while we were waiting */ 90eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 91eca7994fSDean Nelson return 0; 92eca7994fSDean Nelson } 93eca7994fSDean Nelson 94eca7994fSDean Nelson if (uc_pool->nchunks_added >= MAX_CONVERTED_CHUNKS_PER_NODE) { 95eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 96929f9727SDean Nelson return -1; 97eca7994fSDean Nelson } 98f14f75b8SJes Sorensen 99929f9727SDean Nelson /* attempt to allocate a granule's worth of cached memory pages */ 100929f9727SDean Nelson 10196db800fSVlastimil Babka page = __alloc_pages_node(nid, 102e97ca8e5SJohannes Weiner GFP_KERNEL | __GFP_ZERO | __GFP_THISNODE, 103f14f75b8SJes Sorensen IA64_GRANULE_SHIFT-PAGE_SHIFT); 104eca7994fSDean Nelson if (!page) { 105eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 106929f9727SDean Nelson return -1; 107eca7994fSDean Nelson } 108929f9727SDean Nelson 109929f9727SDean Nelson /* convert the memory pages from cached to uncached */ 110929f9727SDean Nelson 111929f9727SDean Nelson c_addr = (unsigned long)page_address(page); 112929f9727SDean Nelson uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET; 113f14f75b8SJes Sorensen 114f14f75b8SJes Sorensen /* 115f14f75b8SJes Sorensen * There's a small race here where it's possible for someone to 116f14f75b8SJes Sorensen * access the page through /dev/mem halfway through the conversion 117f14f75b8SJes Sorensen * to uncached - not sure it's really worth bothering about 118f14f75b8SJes Sorensen */ 119f14f75b8SJes Sorensen for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 120f14f75b8SJes Sorensen SetPageUncached(&page[i]); 121f14f75b8SJes Sorensen 122285fbd66SJan Beulich flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 123f14f75b8SJes Sorensen 124f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 125eca7994fSDean Nelson if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) { 126eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 1278691e5a8SJens Axboe status = smp_call_function(uncached_ipi_visibility, uc_pool, 1); 128eca7994fSDean Nelson if (status || atomic_read(&uc_pool->status)) 129929f9727SDean Nelson goto failed; 130eca7994fSDean Nelson } else if (status != PAL_VISIBILITY_OK) 131eca7994fSDean Nelson goto failed; 132f14f75b8SJes Sorensen 133929f9727SDean Nelson preempt_disable(); 134929f9727SDean Nelson 135f14f75b8SJes Sorensen if (ia64_platform_is("sn2")) 136929f9727SDean Nelson sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE); 137f14f75b8SJes Sorensen else 138929f9727SDean Nelson flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 139929f9727SDean Nelson 140929f9727SDean Nelson /* flush the just introduced uncached translation from the TLB */ 141929f9727SDean Nelson local_flush_tlb_all(); 142929f9727SDean Nelson 143929f9727SDean Nelson preempt_enable(); 144f14f75b8SJes Sorensen 145eca7994fSDean Nelson status = ia64_pal_mc_drain(); 146eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 147eca7994fSDean Nelson goto failed; 148eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 1498691e5a8SJens Axboe status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1); 150eca7994fSDean Nelson if (status || atomic_read(&uc_pool->status)) 151929f9727SDean Nelson goto failed; 152f14f75b8SJes Sorensen 153929f9727SDean Nelson /* 154929f9727SDean Nelson * The chunk of memory pages has been converted to uncached so now we 155929f9727SDean Nelson * can add it to the pool. 156929f9727SDean Nelson */ 157eca7994fSDean Nelson status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid); 158929f9727SDean Nelson if (status) 159929f9727SDean Nelson goto failed; 160f14f75b8SJes Sorensen 161eca7994fSDean Nelson uc_pool->nchunks_added++; 162eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 163929f9727SDean Nelson return 0; 164929f9727SDean Nelson 165929f9727SDean Nelson /* failed to convert or add the chunk so give it back to the kernel */ 166929f9727SDean Nelson failed: 167929f9727SDean Nelson for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 168929f9727SDean Nelson ClearPageUncached(&page[i]); 169929f9727SDean Nelson 170929f9727SDean Nelson free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT); 171eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 172929f9727SDean Nelson return -1; 173f14f75b8SJes Sorensen } 174f14f75b8SJes Sorensen 175f14f75b8SJes Sorensen 176f14f75b8SJes Sorensen /* 177f14f75b8SJes Sorensen * uncached_alloc_page 178f14f75b8SJes Sorensen * 179929f9727SDean Nelson * @starting_nid: node id of node to start with, or -1 180e4a064dfSDean Nelson * @n_pages: number of contiguous pages to allocate 181929f9727SDean Nelson * 182e4a064dfSDean Nelson * Allocate the specified number of contiguous uncached pages on the 183e4a064dfSDean Nelson * the requested node. If not enough contiguous uncached pages are available 184e4a064dfSDean Nelson * on the requested node, roundrobin starting with the next higher node. 185f14f75b8SJes Sorensen */ 186e4a064dfSDean Nelson unsigned long uncached_alloc_page(int starting_nid, int n_pages) 187f14f75b8SJes Sorensen { 188929f9727SDean Nelson unsigned long uc_addr; 189eca7994fSDean Nelson struct uncached_pool *uc_pool; 190929f9727SDean Nelson int nid; 191f14f75b8SJes Sorensen 192929f9727SDean Nelson if (unlikely(starting_nid >= MAX_NUMNODES)) 193929f9727SDean Nelson return 0; 194f14f75b8SJes Sorensen 195929f9727SDean Nelson if (starting_nid < 0) 196929f9727SDean Nelson starting_nid = numa_node_id(); 197929f9727SDean Nelson nid = starting_nid; 198f14f75b8SJes Sorensen 199929f9727SDean Nelson do { 2002dca53a9SChristoph Lameter if (!node_state(nid, N_HIGH_MEMORY)) 201f14f75b8SJes Sorensen continue; 202eca7994fSDean Nelson uc_pool = &uncached_pools[nid]; 203eca7994fSDean Nelson if (uc_pool->pool == NULL) 204929f9727SDean Nelson continue; 205929f9727SDean Nelson do { 206e4a064dfSDean Nelson uc_addr = gen_pool_alloc(uc_pool->pool, 207e4a064dfSDean Nelson n_pages * PAGE_SIZE); 208929f9727SDean Nelson if (uc_addr != 0) 209929f9727SDean Nelson return uc_addr; 210eca7994fSDean Nelson } while (uncached_add_chunk(uc_pool, nid) == 0); 211f14f75b8SJes Sorensen 212929f9727SDean Nelson } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); 213929f9727SDean Nelson 214929f9727SDean Nelson return 0; 215f14f75b8SJes Sorensen } 216f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page); 217f14f75b8SJes Sorensen 218f14f75b8SJes Sorensen 219f14f75b8SJes Sorensen /* 220f14f75b8SJes Sorensen * uncached_free_page 221f14f75b8SJes Sorensen * 222e4a064dfSDean Nelson * @uc_addr: uncached address of first page to free 223e4a064dfSDean Nelson * @n_pages: number of contiguous pages to free 224929f9727SDean Nelson * 225e4a064dfSDean Nelson * Free the specified number of uncached pages. 226f14f75b8SJes Sorensen */ 227e4a064dfSDean Nelson void uncached_free_page(unsigned long uc_addr, int n_pages) 228f14f75b8SJes Sorensen { 229929f9727SDean Nelson int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); 230eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 231f14f75b8SJes Sorensen 232929f9727SDean Nelson if (unlikely(pool == NULL)) 233929f9727SDean Nelson return; 234f14f75b8SJes Sorensen 235929f9727SDean Nelson if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) 236929f9727SDean Nelson panic("uncached_free_page invalid address %lx\n", uc_addr); 237f14f75b8SJes Sorensen 238e4a064dfSDean Nelson gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE); 239f14f75b8SJes Sorensen } 240f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page); 241f14f75b8SJes Sorensen 242f14f75b8SJes Sorensen 243f14f75b8SJes Sorensen /* 244f14f75b8SJes Sorensen * uncached_build_memmap, 245f14f75b8SJes Sorensen * 246929f9727SDean Nelson * @uc_start: uncached starting address of a chunk of uncached memory 247929f9727SDean Nelson * @uc_end: uncached ending address of a chunk of uncached memory 248929f9727SDean Nelson * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) 249929f9727SDean Nelson * 250f14f75b8SJes Sorensen * Called at boot time to build a map of pages that can be used for 251f14f75b8SJes Sorensen * memory special operations. 252f14f75b8SJes Sorensen */ 253e088a4adSMatthew Wilcox static int __init uncached_build_memmap(u64 uc_start, u64 uc_end, void *arg) 254f14f75b8SJes Sorensen { 255929f9727SDean Nelson int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET); 256eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 257929f9727SDean Nelson size_t size = uc_end - uc_start; 258f14f75b8SJes Sorensen 259386d1d50SJohn Hawkes touch_softlockup_watchdog(); 260f14f75b8SJes Sorensen 261929f9727SDean Nelson if (pool != NULL) { 262929f9727SDean Nelson memset((char *)uc_start, 0, size); 263929f9727SDean Nelson (void) gen_pool_add(pool, uc_start, size, nid); 264f14f75b8SJes Sorensen } 265f14f75b8SJes Sorensen return 0; 266f14f75b8SJes Sorensen } 267f14f75b8SJes Sorensen 268f14f75b8SJes Sorensen 269929f9727SDean Nelson static int __init uncached_init(void) 270929f9727SDean Nelson { 271929f9727SDean Nelson int nid; 272f14f75b8SJes Sorensen 2732dca53a9SChristoph Lameter for_each_node_state(nid, N_ONLINE) { 274eca7994fSDean Nelson uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid); 275eca7994fSDean Nelson mutex_init(&uncached_pools[nid].add_chunk_mutex); 276f14f75b8SJes Sorensen } 277f14f75b8SJes Sorensen 278929f9727SDean Nelson efi_memmap_walk_uc(uncached_build_memmap, NULL); 279f14f75b8SJes Sorensen return 0; 280f14f75b8SJes Sorensen } 281f14f75b8SJes Sorensen 282f14f75b8SJes Sorensen __initcall(uncached_init); 283