1f14f75b8SJes Sorensen /* 2929f9727SDean Nelson * Copyright (C) 2001-2006 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 121*285fbd66SJan 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); 126eca7994fSDean Nelson status = smp_call_function(uncached_ipi_visibility, uc_pool, 127eca7994fSDean Nelson 0, 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); 149eca7994fSDean Nelson status = smp_call_function(uncached_ipi_mc_drain, uc_pool, 0, 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 180929f9727SDean Nelson * 181f14f75b8SJes Sorensen * Allocate 1 uncached page. Allocates on the requested node. If no 182f14f75b8SJes Sorensen * uncached pages are available on the requested node, roundrobin starting 183929f9727SDean Nelson * with the next higher node. 184f14f75b8SJes Sorensen */ 185929f9727SDean Nelson unsigned long uncached_alloc_page(int starting_nid) 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 { 205eca7994fSDean Nelson uc_addr = gen_pool_alloc(uc_pool->pool, PAGE_SIZE); 206929f9727SDean Nelson if (uc_addr != 0) 207929f9727SDean Nelson return uc_addr; 208eca7994fSDean Nelson } while (uncached_add_chunk(uc_pool, nid) == 0); 209f14f75b8SJes Sorensen 210929f9727SDean Nelson } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); 211929f9727SDean Nelson 212929f9727SDean Nelson return 0; 213f14f75b8SJes Sorensen } 214f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_alloc_page); 215f14f75b8SJes Sorensen 216f14f75b8SJes Sorensen 217f14f75b8SJes Sorensen /* 218f14f75b8SJes Sorensen * uncached_free_page 219f14f75b8SJes Sorensen * 220929f9727SDean Nelson * @uc_addr: uncached address of page to free 221929f9727SDean Nelson * 222f14f75b8SJes Sorensen * Free a single uncached page. 223f14f75b8SJes Sorensen */ 224929f9727SDean Nelson void uncached_free_page(unsigned long uc_addr) 225f14f75b8SJes Sorensen { 226929f9727SDean Nelson int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); 227eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 228f14f75b8SJes Sorensen 229929f9727SDean Nelson if (unlikely(pool == NULL)) 230929f9727SDean Nelson return; 231f14f75b8SJes Sorensen 232929f9727SDean Nelson if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) 233929f9727SDean Nelson panic("uncached_free_page invalid address %lx\n", uc_addr); 234f14f75b8SJes Sorensen 235929f9727SDean Nelson gen_pool_free(pool, uc_addr, PAGE_SIZE); 236f14f75b8SJes Sorensen } 237f14f75b8SJes Sorensen EXPORT_SYMBOL(uncached_free_page); 238f14f75b8SJes Sorensen 239f14f75b8SJes Sorensen 240f14f75b8SJes Sorensen /* 241f14f75b8SJes Sorensen * uncached_build_memmap, 242f14f75b8SJes Sorensen * 243929f9727SDean Nelson * @uc_start: uncached starting address of a chunk of uncached memory 244929f9727SDean Nelson * @uc_end: uncached ending address of a chunk of uncached memory 245929f9727SDean Nelson * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) 246929f9727SDean Nelson * 247f14f75b8SJes Sorensen * Called at boot time to build a map of pages that can be used for 248f14f75b8SJes Sorensen * memory special operations. 249f14f75b8SJes Sorensen */ 250929f9727SDean Nelson static int __init uncached_build_memmap(unsigned long uc_start, 251929f9727SDean Nelson unsigned long uc_end, void *arg) 252f14f75b8SJes Sorensen { 253929f9727SDean Nelson int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET); 254eca7994fSDean Nelson struct gen_pool *pool = uncached_pools[nid].pool; 255929f9727SDean Nelson size_t size = uc_end - uc_start; 256f14f75b8SJes Sorensen 257386d1d50SJohn Hawkes touch_softlockup_watchdog(); 258f14f75b8SJes Sorensen 259929f9727SDean Nelson if (pool != NULL) { 260929f9727SDean Nelson memset((char *)uc_start, 0, size); 261929f9727SDean Nelson (void) gen_pool_add(pool, uc_start, size, nid); 262f14f75b8SJes Sorensen } 263f14f75b8SJes Sorensen return 0; 264f14f75b8SJes Sorensen } 265f14f75b8SJes Sorensen 266f14f75b8SJes Sorensen 267929f9727SDean Nelson static int __init uncached_init(void) 268929f9727SDean Nelson { 269929f9727SDean Nelson int nid; 270f14f75b8SJes Sorensen 2712dca53a9SChristoph Lameter for_each_node_state(nid, N_ONLINE) { 272eca7994fSDean Nelson uncached_pools[nid].pool = gen_pool_create(PAGE_SHIFT, nid); 273eca7994fSDean Nelson mutex_init(&uncached_pools[nid].add_chunk_mutex); 274f14f75b8SJes Sorensen } 275f14f75b8SJes Sorensen 276929f9727SDean Nelson efi_memmap_walk_uc(uncached_build_memmap, NULL); 277f14f75b8SJes Sorensen return 0; 278f14f75b8SJes Sorensen } 279f14f75b8SJes Sorensen 280f14f75b8SJes Sorensen __initcall(uncached_init); 281