Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 1 | /* |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights reserved. |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * A simple uncached page allocator using the generic allocator. This |
| 9 | * allocator first utilizes the spare (spill) pages found in the EFI |
| 10 | * memmap and will then start converting cached pages to uncached ones |
| 11 | * at a granule at a time. Node awareness is implemented by having a |
| 12 | * pool of pages per node. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/efi.h> |
| 23 | #include <linux/genalloc.h> |
| 24 | #include <asm/page.h> |
| 25 | #include <asm/pal.h> |
| 26 | #include <asm/system.h> |
| 27 | #include <asm/pgtable.h> |
| 28 | #include <asm/atomic.h> |
| 29 | #include <asm/tlbflush.h> |
| 30 | #include <asm/sn/arch.h> |
| 31 | |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 32 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 33 | extern void __init efi_memmap_walk_uc(efi_freemem_callback_t, void *); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 34 | |
| 35 | #define MAX_UNCACHED_GRANULES 5 |
| 36 | static int allocated_granules; |
| 37 | |
| 38 | struct gen_pool *uncached_pool[MAX_NUMNODES]; |
| 39 | |
| 40 | |
| 41 | static void uncached_ipi_visibility(void *data) |
| 42 | { |
| 43 | int status; |
| 44 | |
| 45 | status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); |
| 46 | if ((status != PAL_VISIBILITY_OK) && |
| 47 | (status != PAL_VISIBILITY_OK_REMOTE_NEEDED)) |
| 48 | printk(KERN_DEBUG "pal_prefetch_visibility() returns %i on " |
Jes Sorensen | 3bd7f01 | 2005-12-16 11:00:03 -0500 | [diff] [blame] | 49 | "CPU %i\n", status, raw_smp_processor_id()); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | static void uncached_ipi_mc_drain(void *data) |
| 54 | { |
| 55 | int status; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 56 | |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 57 | status = ia64_pal_mc_drain(); |
| 58 | if (status) |
| 59 | printk(KERN_WARNING "ia64_pal_mc_drain() failed with %i on " |
Jes Sorensen | 3bd7f01 | 2005-12-16 11:00:03 -0500 | [diff] [blame] | 60 | "CPU %i\n", status, raw_smp_processor_id()); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 64 | /* |
| 65 | * Add a new chunk of uncached memory pages to the specified pool. |
| 66 | * |
| 67 | * @pool: pool to add new chunk of uncached memory to |
| 68 | * @nid: node id of node to allocate memory from, or -1 |
| 69 | * |
| 70 | * This is accomplished by first allocating a granule of cached memory pages |
| 71 | * and then converting them to uncached memory pages. |
| 72 | */ |
| 73 | static int uncached_add_chunk(struct gen_pool *pool, int nid) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 74 | { |
| 75 | struct page *page; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 76 | int status, i; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 77 | unsigned long c_addr, uc_addr; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 78 | |
| 79 | if (allocated_granules >= MAX_UNCACHED_GRANULES) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 80 | return -1; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 81 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 82 | /* attempt to allocate a granule's worth of cached memory pages */ |
| 83 | |
| 84 | page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO, |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 85 | IA64_GRANULE_SHIFT-PAGE_SHIFT); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 86 | if (!page) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 87 | return -1; |
| 88 | |
| 89 | /* convert the memory pages from cached to uncached */ |
| 90 | |
| 91 | c_addr = (unsigned long)page_address(page); |
| 92 | uc_addr = c_addr - PAGE_OFFSET + __IA64_UNCACHED_OFFSET; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * There's a small race here where it's possible for someone to |
| 96 | * access the page through /dev/mem halfway through the conversion |
| 97 | * to uncached - not sure it's really worth bothering about |
| 98 | */ |
| 99 | for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) |
| 100 | SetPageUncached(&page[i]); |
| 101 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 102 | flush_tlb_kernel_range(uc_addr, uc_adddr + IA64_GRANULE_SIZE); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 103 | |
| 104 | status = ia64_pal_prefetch_visibility(PAL_VISIBILITY_PHYSICAL); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 105 | if (!status) { |
| 106 | status = smp_call_function(uncached_ipi_visibility, NULL, 0, 1); |
| 107 | if (status) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 108 | goto failed; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 111 | preempt_disable(); |
| 112 | |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 113 | if (ia64_platform_is("sn2")) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 114 | sn_flush_all_caches(uc_addr, IA64_GRANULE_SIZE); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 115 | else |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 116 | flush_icache_range(uc_addr, uc_addr + IA64_GRANULE_SIZE); |
| 117 | |
| 118 | /* flush the just introduced uncached translation from the TLB */ |
| 119 | local_flush_tlb_all(); |
| 120 | |
| 121 | preempt_enable(); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 122 | |
| 123 | ia64_pal_mc_drain(); |
| 124 | status = smp_call_function(uncached_ipi_mc_drain, NULL, 0, 1); |
| 125 | if (status) |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 126 | goto failed; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 127 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 128 | /* |
| 129 | * The chunk of memory pages has been converted to uncached so now we |
| 130 | * can add it to the pool. |
| 131 | */ |
| 132 | status = gen_pool_add(pool, uc_addr, IA64_GRANULE_SIZE, nid); |
| 133 | if (status) |
| 134 | goto failed; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 135 | |
| 136 | allocated_granules++; |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 137 | return 0; |
| 138 | |
| 139 | /* failed to convert or add the chunk so give it back to the kernel */ |
| 140 | failed: |
| 141 | for (i = 0; i < (IA64_GRANULE_SIZE / PAGE_SIZE); i++) |
| 142 | ClearPageUncached(&page[i]); |
| 143 | |
| 144 | free_pages(c_addr, IA64_GRANULE_SHIFT-PAGE_SHIFT); |
| 145 | return -1; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | /* |
| 150 | * uncached_alloc_page |
| 151 | * |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 152 | * @starting_nid: node id of node to start with, or -1 |
| 153 | * |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 154 | * Allocate 1 uncached page. Allocates on the requested node. If no |
| 155 | * uncached pages are available on the requested node, roundrobin starting |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 156 | * with the next higher node. |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 157 | */ |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 158 | unsigned long uncached_alloc_page(int starting_nid) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 159 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 160 | unsigned long uc_addr; |
| 161 | struct gen_pool *pool; |
| 162 | int nid; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 163 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 164 | if (unlikely(starting_nid >= MAX_NUMNODES)) |
| 165 | return 0; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 166 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 167 | if (starting_nid < 0) |
| 168 | starting_nid = numa_node_id(); |
| 169 | nid = starting_nid; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 170 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 171 | do { |
| 172 | if (!node_online(nid)) |
| 173 | continue; |
| 174 | pool = uncached_pool[nid]; |
| 175 | if (pool == NULL) |
| 176 | continue; |
| 177 | do { |
| 178 | uc_addr = gen_pool_alloc(pool, PAGE_SIZE); |
| 179 | if (uc_addr != 0) |
| 180 | return uc_addr; |
| 181 | } while (uncached_add_chunk(pool, nid) == 0); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 182 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 183 | } while ((nid = (nid + 1) % MAX_NUMNODES) != starting_nid); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 184 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 185 | return 0; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL(uncached_alloc_page); |
| 188 | |
| 189 | |
| 190 | /* |
| 191 | * uncached_free_page |
| 192 | * |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 193 | * @uc_addr: uncached address of page to free |
| 194 | * |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 195 | * Free a single uncached page. |
| 196 | */ |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 197 | void uncached_free_page(unsigned long uc_addr) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 198 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 199 | int nid = paddr_to_nid(uc_addr - __IA64_UNCACHED_OFFSET); |
| 200 | struct gen_pool *pool = uncached_pool[nid]; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 201 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 202 | if (unlikely(pool == NULL)) |
| 203 | return; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 204 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 205 | if ((uc_addr & (0XFUL << 60)) != __IA64_UNCACHED_OFFSET) |
| 206 | panic("uncached_free_page invalid address %lx\n", uc_addr); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 207 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 208 | gen_pool_free(pool, uc_addr, PAGE_SIZE); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL(uncached_free_page); |
| 211 | |
| 212 | |
| 213 | /* |
| 214 | * uncached_build_memmap, |
| 215 | * |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 216 | * @uc_start: uncached starting address of a chunk of uncached memory |
| 217 | * @uc_end: uncached ending address of a chunk of uncached memory |
| 218 | * @arg: ignored, (NULL argument passed in on call to efi_memmap_walk_uc()) |
| 219 | * |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 220 | * Called at boot time to build a map of pages that can be used for |
| 221 | * memory special operations. |
| 222 | */ |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 223 | static int __init uncached_build_memmap(unsigned long uc_start, |
| 224 | unsigned long uc_end, void *arg) |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 225 | { |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 226 | int nid = paddr_to_nid(uc_start - __IA64_UNCACHED_OFFSET); |
| 227 | struct gen_pool *pool = uncached_pool[nid]; |
| 228 | size_t size = uc_end - uc_start; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 229 | |
John Hawkes | 386d1d5 | 2006-01-18 23:46:53 -0800 | [diff] [blame] | 230 | touch_softlockup_watchdog(); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 231 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 232 | if (pool != NULL) { |
| 233 | memset((char *)uc_start, 0, size); |
| 234 | (void) gen_pool_add(pool, uc_start, size, nid); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 235 | } |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 240 | static int __init uncached_init(void) |
| 241 | { |
| 242 | int nid; |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 243 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 244 | for_each_online_node(nid) { |
| 245 | uncached_pool[nid] = gen_pool_create(PAGE_SHIFT, nid); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Dean Nelson | 929f972 | 2006-06-23 02:03:21 -0700 | [diff] [blame^] | 248 | efi_memmap_walk_uc(uncached_build_memmap, NULL); |
Jes Sorensen | f14f75b | 2005-06-21 17:15:02 -0700 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | __initcall(uncached_init); |