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/slab.h> 22f14f75b8SJes Sorensen #include <linux/efi.h> 23f14f75b8SJes Sorensen #include <linux/genalloc.h> 24f14f75b8SJes Sorensen #include <asm/page.h> 25f14f75b8SJes Sorensen #include <asm/pal.h> 26f14f75b8SJes Sorensen #include <asm/system.h> 27f14f75b8SJes Sorensen #include <asm/pgtable.h> 28f14f75b8SJes Sorensen #include <asm/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 101980128f2SChristoph Lameter page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO | GFP_THISNODE, 102f14f75b8SJes Sorensen IA64_GRANULE_SHIFT-PAGE_SHIFT); 103eca7994fSDean Nelson if (!page) { 104eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 105929f9727SDean Nelson return -1; 106eca7994fSDean Nelson } 107929f9727SDean Nelson 108929f9727SDean Nelson /* convert the memory pages from cached to uncached */ 109929f9727SDean Nelson 110929f9727SDean Nelson c_addr = (unsigned long)page_address(page); 111929f9727SDean Nelson uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET; 112f14f75b8SJes Sorensen 113f14f75b8SJes Sorensen /* 114f14f75b8SJes Sorensen * There's a small race here where it's possible for someone to 115f14f75b8SJes Sorensen * access the page through /dev/mem halfway through the conversion 116f14f75b8SJes Sorensen * to uncached - not sure it's really worth bothering about 117f14f75b8SJes Sorensen */ 118f14f75b8SJes Sorensen for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 119f14f75b8SJes Sorensen SetPageUncached(&page[i]); 120f14f75b8SJes Sorensen 121285fbd66SJan Beulich flush_tlb_kernel_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 122f14f75b8SJes Sorensen 123f14f75b8SJes Sorensen status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); 124eca7994fSDean Nelson if (status == PAL_VISIBILITY_OK_REMOTE_NEEDED) { 125eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 126*8691e5a8SJens Axboe status = smp_call_function(uncached_ipi_visibility, uc_pool, 1); 127eca7994fSDean Nelson if (status || atomic_read(&uc_pool->status)) 128929f9727SDean Nelson goto failed; 129eca7994fSDean Nelson } else if (status != PAL_VISIBILITY_OK) 130eca7994fSDean Nelson goto failed; 131f14f75b8SJes Sorensen 132929f9727SDean Nelson preempt_disable(); 133929f9727SDean Nelson 134f14f75b8SJes Sorensen if (ia64_platform_is("sn2")) 135929f9727SDean Nelson sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE); 136f14f75b8SJes Sorensen else 137929f9727SDean Nelson flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); 138929f9727SDean Nelson 139929f9727SDean Nelson /* flush the just introduced uncached translation from the TLB */ 140929f9727SDean Nelson local_flush_tlb_all(); 141929f9727SDean Nelson 142929f9727SDean Nelson preempt_enable(); 143f14f75b8SJes Sorensen 144eca7994fSDean Nelson status = ia64_pal_mc_drain(); 145eca7994fSDean Nelson if (status != PAL_STATUS_SUCCESS) 146eca7994fSDean Nelson goto failed; 147eca7994fSDean Nelson atomic_set(&uc_pool->status, 0); 148*8691e5a8SJens Axboe status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 1); 149eca7994fSDean Nelson if (status || atomic_read(&uc_pool->status)) 150929f9727SDean Nelson goto failed; 151f14f75b8SJes Sorensen 152929f9727SDean Nelson /* 153929f9727SDean Nelson * The chunk of memory pages has been converted to uncached so now we 154929f9727SDean Nelson * can add it to the pool. 155929f9727SDean Nelson */ 156eca7994fSDean Nelson status = gen_pool_add(uc_pool->pool, uc_addr, IA64_GRANULE_SIZE, nid); 157929f9727SDean Nelson if (status) 158929f9727SDean Nelson goto failed; 159f14f75b8SJes Sorensen 160eca7994fSDean Nelson uc_pool->nchunks_added++; 161eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 162929f9727SDean Nelson return 0; 163929f9727SDean Nelson 164929f9727SDean Nelson /* failed to convert or add the chunk so give it back to the kernel */ 165929f9727SDean Nelson failed: 166929f9727SDean Nelson for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) 167929f9727SDean Nelson ClearPageUncached(&page[i]); 168929f9727SDean Nelson 169929f9727SDean Nelson free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT); 170eca7994fSDean Nelson mutex_unlock(&uc_pool->add_chunk_mutex); 171929f9727SDean Nelson return -1; 172f14f75b8SJes Sorensen } 173f14f75b8SJes Sorensen 174f14f75b8SJes Sorensen 175f14f75b8SJes Sorensen /* 176f14f75b8SJes Sorensen * uncached_alloc_page 177f14f75b8SJes Sorensen * 178929f9727SDean Nelson * @starting_nid: node id of node to start with, or -1 179e4a064dfSDean Nelson * @n_pages: number of contiguous pages to allocate 180929f9727SDean Nelson * 181e4a064dfSDean Nelson * Allocate the specified number of contiguous uncached pages on the 182e4a064dfSDean Nelson * the requested node. If not enough contiguous uncached pages are available 183e4a064dfSDean Nelson * on the requested node, roundrobin starting with the next higher node. 184f14f75b8SJes Sorensen */ 185e4a064dfSDean Nelson unsigned long uncached_alloc_page(int starting_nid, int n_pages) 186f14f75b8SJes Sorensen { 187929f9727SDean Nelson unsigned long uc_addr; 188eca7994fSDean Nelson struct uncached_pool *uc_pool; 189929f9727SDean Nelson int nid; 190f14f75b8SJes Sorensen 191929f9727SDean Nelson if (unlikely(starting_nid >= MAX_NUMNODES)) 192929f9727SDean Nelson return 0; 193f14f75b8SJes Sorensen 194929f9727SDean Nelson if (starting_nid < 0) 195929f9727SDean Nelson starting_nid = numa_node_id(); 196929f9727SDean Nelson nid = starting_nid; 197f14f75b8SJes Sorensen 198929f9727SDean Nelson do { 1992dca53a9SChristoph Lameter if (!node_state(nid, N_HIGH_MEMORY)) 200f14f75b8SJes Sorensen continue; 201eca7994fSDean Nelson uc_pool = &uncached_pools[nid]; 202eca7994fSDean Nelson if (uc_pool->pool == NULL) 203929f9727SDean Nelson continue; 204929f9727SDean Nelson do { 205e4a064dfSDean Nelson uc_addr = gen_pool_alloc(uc_pool->pool, 206e4a064dfSDean Nelson n_pages * PAGE_SIZE); 207929f9727SDean Nelson if (uc_addr != 0) 208929f9727SDean Nelson return uc_addr; 209eca7994fSDean Nelson } while (uncached_add_chunk(uc_pool, nid) == 0); 210f14f75b8SJes Sorensen 211929f9727SDean Nelson } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); 212929f9727SDean Nelson 213929f9727SDean Nelson return 0; 214f14f75b8SJes Sorensen } 215f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page); 216f14f75b8SJes Sorensen 217f14f75b8SJes Sorensen 218f14f75b8SJes Sorensen /* 219f14f75b8SJes Sorensen * uncached_free_page 220f14f75b8SJes Sorensen * 221e4a064dfSDean Nelson * @uc_addr: uncached address of first page to free 222e4a064dfSDean Nelson * @n_pages: number of contiguous pages to free 223929f9727SDean Nelson * 224e4a064dfSDean Nelson * Free the specified number of uncached pages. 225f14f75b8SJes Sorensen */ 226e4a064dfSDean Nelson void uncached_free_page(unsigned long uc_addr, int n_pages) 227f14f75b8SJes Sorensen { 228929f9727SDean Nelson int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); 229eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 230f14f75b8SJes Sorensen 231929f9727SDean Nelson if (unlikely(pool == NULL)) 232929f9727SDean Nelson return; 233f14f75b8SJes Sorensen 234929f9727SDean Nelson if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) 235929f9727SDean Nelson panic("uncached_free_page invalid address %lx\n", uc_addr); 236f14f75b8SJes Sorensen 237e4a064dfSDean Nelson gen_pool_free(pool, uc_addr, n_pages * PAGE_SIZE); 238f14f75b8SJes Sorensen } 239f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page); 240f14f75b8SJes Sorensen 241f14f75b8SJes Sorensen 242f14f75b8SJes Sorensen /* 243f14f75b8SJes Sorensen * uncached_build_memmap, 244f14f75b8SJes Sorensen * 245929f9727SDean Nelson * @uc_start: uncached starting address of a chunk of uncached memory 246929f9727SDean Nelson * @uc_end: uncached ending address of a chunk of uncached memory 247929f9727SDean Nelson * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) 248929f9727SDean Nelson * 249f14f75b8SJes Sorensen * Called at boot time to build a map of pages that can be used for 250f14f75b8SJes Sorensen * memory special operations. 251f14f75b8SJes Sorensen */ 252929f9727SDean Nelson static int __init uncached_build_memmap(unsigned long uc_start, 253929f9727SDean Nelson unsigned long 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