Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights |
| 3 | * reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License |
| 7 | * as published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * SN Platform Special Memory (mspec) Support |
| 12 | * |
| 13 | * This driver exports the SN special memory (mspec) facility to user |
| 14 | * processes. |
| 15 | * There are three types of memory made available thru this driver: |
| 16 | * fetchops, uncached and cached. |
| 17 | * |
| 18 | * Fetchops are atomic memory operations that are implemented in the |
| 19 | * memory controller on SGI SN hardware. |
| 20 | * |
| 21 | * Uncached are used for memory write combining feature of the ia64 |
| 22 | * cpu. |
| 23 | * |
| 24 | * Cached are used for areas of memory that are used as cached addresses |
| 25 | * on our partition and used as uncached addresses from other partitions. |
| 26 | * Due to a design constraint of the SN2 Shub, you can not have processors |
| 27 | * on the same FSB perform both a cached and uncached reference to the |
| 28 | * same cache line. These special memory cached regions prevent the |
| 29 | * kernel from ever dropping in a TLB entry and therefore prevent the |
| 30 | * processor from ever speculating a cache line from this page. |
| 31 | */ |
| 32 | |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 33 | #include <linux/types.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/errno.h> |
| 38 | #include <linux/miscdevice.h> |
| 39 | #include <linux/spinlock.h> |
| 40 | #include <linux/mm.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 41 | #include <linux/fs.h> |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 42 | #include <linux/vmalloc.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/slab.h> |
| 45 | #include <linux/numa.h> |
| 46 | #include <asm/page.h> |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 47 | #include <asm/pgtable.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 48 | #include <linux/atomic.h> |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 49 | #include <asm/tlbflush.h> |
| 50 | #include <asm/uncached.h> |
| 51 | #include <asm/sn/addrs.h> |
| 52 | #include <asm/sn/arch.h> |
| 53 | #include <asm/sn/mspec.h> |
| 54 | #include <asm/sn/sn_cpuid.h> |
| 55 | #include <asm/sn/io.h> |
| 56 | #include <asm/sn/bte.h> |
| 57 | #include <asm/sn/shubio.h> |
| 58 | |
| 59 | |
| 60 | #define FETCHOP_ID "SGI Fetchop," |
| 61 | #define CACHED_ID "Cached," |
| 62 | #define UNCACHED_ID "Uncached" |
| 63 | #define REVISION "4.0" |
| 64 | #define MSPEC_BASENAME "mspec" |
| 65 | |
| 66 | /* |
| 67 | * Page types allocated by the device. |
| 68 | */ |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 69 | enum mspec_page_type { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 70 | MSPEC_FETCHOP = 1, |
| 71 | MSPEC_CACHED, |
| 72 | MSPEC_UNCACHED |
| 73 | }; |
| 74 | |
Jes Sorensen | 1a4b0fc | 2006-11-10 12:27:49 -0800 | [diff] [blame] | 75 | #ifdef CONFIG_SGI_SN |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 76 | static int is_sn2; |
Jes Sorensen | 1a4b0fc | 2006-11-10 12:27:49 -0800 | [diff] [blame] | 77 | #else |
| 78 | #define is_sn2 0 |
| 79 | #endif |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * One of these structures is allocated when an mspec region is mmaped. The |
| 83 | * structure is pointed to by the vma->vm_private_data field in the vma struct. |
| 84 | * This structure is used to record the addresses of the mspec pages. |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 85 | * This structure is shared by all vma's that are split off from the |
| 86 | * original vma when split_vma()'s are done. |
| 87 | * |
| 88 | * The refcnt is incremented atomically because mm->mmap_sem does not |
| 89 | * protect in fork case where multiple tasks share the vma_data. |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 90 | */ |
| 91 | struct vma_data { |
| 92 | atomic_t refcnt; /* Number of vmas sharing the data. */ |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 93 | spinlock_t lock; /* Serialize access to this structure. */ |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 94 | int count; /* Number of pages allocated. */ |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 95 | enum mspec_page_type type; /* Type of pages allocated. */ |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 96 | unsigned long vm_start; /* Original (unsplit) base. */ |
| 97 | unsigned long vm_end; /* Original (unsplit) end. */ |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 98 | unsigned long maddr[0]; /* Array of MSPEC addresses. */ |
| 99 | }; |
| 100 | |
| 101 | /* used on shub2 to clear FOP cache in the HUB */ |
| 102 | static unsigned long scratch_page[MAX_NUMNODES]; |
| 103 | #define SH2_AMO_CACHE_ENTRIES 4 |
| 104 | |
| 105 | static inline int |
| 106 | mspec_zero_block(unsigned long addr, int len) |
| 107 | { |
| 108 | int status; |
| 109 | |
| 110 | if (is_sn2) { |
| 111 | if (is_shub2()) { |
| 112 | int nid; |
| 113 | void *p; |
| 114 | int i; |
| 115 | |
| 116 | nid = nasid_to_cnodeid(get_node_number(__pa(addr))); |
| 117 | p = (void *)TO_AMO(scratch_page[nid]); |
| 118 | |
| 119 | for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) { |
| 120 | FETCHOP_LOAD_OP(p, FETCHOP_LOAD); |
| 121 | p += FETCHOP_VAR_SIZE; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len, |
| 126 | BTE_WACQUIRE | BTE_ZERO_FILL, NULL); |
| 127 | } else { |
| 128 | memset((char *) addr, 0, len); |
| 129 | status = 0; |
| 130 | } |
| 131 | return status; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * mspec_open |
| 136 | * |
| 137 | * Called when a device mapping is created by a means other than mmap |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 138 | * (via fork, munmap, etc.). Increments the reference count on the |
| 139 | * underlying mspec data so it is not freed prematurely. |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 140 | */ |
| 141 | static void |
| 142 | mspec_open(struct vm_area_struct *vma) |
| 143 | { |
| 144 | struct vma_data *vdata; |
| 145 | |
| 146 | vdata = vma->vm_private_data; |
| 147 | atomic_inc(&vdata->refcnt); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * mspec_close |
| 152 | * |
| 153 | * Called when unmapping a device mapping. Frees all mspec pages |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 154 | * belonging to all the vma's sharing this vma_data structure. |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 155 | */ |
| 156 | static void |
| 157 | mspec_close(struct vm_area_struct *vma) |
| 158 | { |
| 159 | struct vma_data *vdata; |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 160 | int index, last_index; |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 161 | unsigned long my_page; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 162 | |
| 163 | vdata = vma->vm_private_data; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 164 | |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 165 | if (!atomic_dec_and_test(&vdata->refcnt)) |
| 166 | return; |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 167 | |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 168 | last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT; |
| 169 | for (index = 0; index < last_index; index++) { |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 170 | if (vdata->maddr[index] == 0) |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 171 | continue; |
| 172 | /* |
| 173 | * Clear the page before sticking it back |
| 174 | * into the pool. |
| 175 | */ |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 176 | my_page = vdata->maddr[index]; |
| 177 | vdata->maddr[index] = 0; |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 178 | if (!mspec_zero_block(my_page, PAGE_SIZE)) |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 179 | uncached_free_page(my_page, 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 180 | else |
| 181 | printk(KERN_WARNING "mspec_close(): " |
Cliff Wickman | afa684f | 2007-09-24 21:24:41 -0700 | [diff] [blame] | 182 | "failed to zero page %ld\n", my_page); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 183 | } |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 184 | |
Tetsuo Handa | 1d5cfdb | 2016-01-22 15:11:02 -0800 | [diff] [blame] | 185 | kvfree(vdata); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 188 | /* |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 189 | * mspec_fault |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 190 | * |
| 191 | * Creates a mspec page and maps it to user space. |
| 192 | */ |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 193 | static int |
| 194 | mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 195 | { |
| 196 | unsigned long paddr, maddr; |
| 197 | unsigned long pfn; |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 198 | pgoff_t index = vmf->pgoff; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 199 | struct vma_data *vdata = vma->vm_private_data; |
| 200 | |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 201 | maddr = (volatile unsigned long) vdata->maddr[index]; |
| 202 | if (maddr == 0) { |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 203 | maddr = uncached_alloc_page(numa_node_id(), 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 204 | if (maddr == 0) |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 205 | return VM_FAULT_OOM; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 206 | |
| 207 | spin_lock(&vdata->lock); |
| 208 | if (vdata->maddr[index] == 0) { |
| 209 | vdata->count++; |
| 210 | vdata->maddr[index] = maddr; |
| 211 | } else { |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 212 | uncached_free_page(maddr, 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 213 | maddr = vdata->maddr[index]; |
| 214 | } |
| 215 | spin_unlock(&vdata->lock); |
| 216 | } |
| 217 | |
| 218 | if (vdata->type == MSPEC_FETCHOP) |
| 219 | paddr = TO_AMO(maddr); |
| 220 | else |
Jes Sorensen | 1a4b0fc | 2006-11-10 12:27:49 -0800 | [diff] [blame] | 221 | paddr = maddr & ~__IA64_UNCACHED_OFFSET; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 222 | |
| 223 | pfn = paddr >> PAGE_SHIFT; |
| 224 | |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 225 | /* |
| 226 | * vm_insert_pfn can fail with -EBUSY, but in that case it will |
| 227 | * be because another thread has installed the pte first, so it |
| 228 | * is no problem. |
| 229 | */ |
| 230 | vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
| 231 | |
| 232 | return VM_FAULT_NOPAGE; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 235 | static const struct vm_operations_struct mspec_vm_ops = { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 236 | .open = mspec_open, |
| 237 | .close = mspec_close, |
Nick Piggin | efe9e77 | 2008-07-23 21:27:00 -0700 | [diff] [blame] | 238 | .fault = mspec_fault, |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | /* |
| 242 | * mspec_mmap |
| 243 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 244 | * Called when mmapping the device. Initializes the vma with a fault handler |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 245 | * and private data structure necessary to allocate, track, and free the |
| 246 | * underlying pages. |
| 247 | */ |
| 248 | static int |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 249 | mspec_mmap(struct file *file, struct vm_area_struct *vma, |
| 250 | enum mspec_page_type type) |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 251 | { |
| 252 | struct vma_data *vdata; |
Tetsuo Handa | 1d5cfdb | 2016-01-22 15:11:02 -0800 | [diff] [blame] | 253 | int pages, vdata_size; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 254 | |
| 255 | if (vma->vm_pgoff != 0) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | if ((vma->vm_flags & VM_SHARED) == 0) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | if ((vma->vm_flags & VM_WRITE) == 0) |
| 262 | return -EPERM; |
| 263 | |
Libin | a0ea59d | 2013-05-13 10:17:39 +0800 | [diff] [blame] | 264 | pages = vma_pages(vma); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 265 | vdata_size = sizeof(struct vma_data) + pages * sizeof(long); |
| 266 | if (vdata_size <= PAGE_SIZE) |
Rakib Mullick | 658c74c | 2011-05-26 16:25:56 -0700 | [diff] [blame] | 267 | vdata = kzalloc(vdata_size, GFP_KERNEL); |
Tetsuo Handa | 1d5cfdb | 2016-01-22 15:11:02 -0800 | [diff] [blame] | 268 | else |
Rakib Mullick | 658c74c | 2011-05-26 16:25:56 -0700 | [diff] [blame] | 269 | vdata = vzalloc(vdata_size); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 270 | if (!vdata) |
| 271 | return -ENOMEM; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 272 | |
Cliff Wickman | 4191ba2 | 2007-09-18 22:46:31 -0700 | [diff] [blame] | 273 | vdata->vm_start = vma->vm_start; |
| 274 | vdata->vm_end = vma->vm_end; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 275 | vdata->type = type; |
| 276 | spin_lock_init(&vdata->lock); |
Tony Luck | a119365 | 2012-07-26 10:55:26 -0700 | [diff] [blame] | 277 | atomic_set(&vdata->refcnt, 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 278 | vma->vm_private_data = vdata; |
| 279 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 280 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 281 | if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED) |
| 282 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 283 | vma->vm_ops = &mspec_vm_ops; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | fetchop_mmap(struct file *file, struct vm_area_struct *vma) |
| 290 | { |
| 291 | return mspec_mmap(file, vma, MSPEC_FETCHOP); |
| 292 | } |
| 293 | |
| 294 | static int |
| 295 | cached_mmap(struct file *file, struct vm_area_struct *vma) |
| 296 | { |
| 297 | return mspec_mmap(file, vma, MSPEC_CACHED); |
| 298 | } |
| 299 | |
| 300 | static int |
| 301 | uncached_mmap(struct file *file, struct vm_area_struct *vma) |
| 302 | { |
| 303 | return mspec_mmap(file, vma, MSPEC_UNCACHED); |
| 304 | } |
| 305 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 306 | static const struct file_operations fetchop_fops = { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 307 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 308 | .mmap = fetchop_mmap, |
| 309 | .llseek = noop_llseek, |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | static struct miscdevice fetchop_miscdev = { |
| 313 | .minor = MISC_DYNAMIC_MINOR, |
| 314 | .name = "sgi_fetchop", |
| 315 | .fops = &fetchop_fops |
| 316 | }; |
| 317 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 318 | static const struct file_operations cached_fops = { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 319 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 320 | .mmap = cached_mmap, |
| 321 | .llseek = noop_llseek, |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | static struct miscdevice cached_miscdev = { |
| 325 | .minor = MISC_DYNAMIC_MINOR, |
| 326 | .name = "mspec_cached", |
| 327 | .fops = &cached_fops |
| 328 | }; |
| 329 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 330 | static const struct file_operations uncached_fops = { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 331 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 332 | .mmap = uncached_mmap, |
| 333 | .llseek = noop_llseek, |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | static struct miscdevice uncached_miscdev = { |
| 337 | .minor = MISC_DYNAMIC_MINOR, |
| 338 | .name = "mspec_uncached", |
| 339 | .fops = &uncached_fops |
| 340 | }; |
| 341 | |
| 342 | /* |
| 343 | * mspec_init |
| 344 | * |
| 345 | * Called at boot time to initialize the mspec facility. |
| 346 | */ |
| 347 | static int __init |
| 348 | mspec_init(void) |
| 349 | { |
| 350 | int ret; |
| 351 | int nid; |
| 352 | |
| 353 | /* |
| 354 | * The fetchop device only works on SN2 hardware, uncached and cached |
| 355 | * memory drivers should both be valid on all ia64 hardware |
| 356 | */ |
Jes Sorensen | 1a4b0fc | 2006-11-10 12:27:49 -0800 | [diff] [blame] | 357 | #ifdef CONFIG_SGI_SN |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 358 | if (ia64_platform_is("sn2")) { |
| 359 | is_sn2 = 1; |
| 360 | if (is_shub2()) { |
| 361 | ret = -ENOMEM; |
Christoph Lameter | 2dca53a | 2007-10-16 01:25:33 -0700 | [diff] [blame] | 362 | for_each_node_state(nid, N_ONLINE) { |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 363 | int actual_nid; |
| 364 | int nasid; |
| 365 | unsigned long phys; |
| 366 | |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 367 | scratch_page[nid] = uncached_alloc_page(nid, 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 368 | if (scratch_page[nid] == 0) |
| 369 | goto free_scratch_pages; |
| 370 | phys = __pa(scratch_page[nid]); |
| 371 | nasid = get_node_number(phys); |
| 372 | actual_nid = nasid_to_cnodeid(nasid); |
| 373 | if (actual_nid != nid) |
| 374 | goto free_scratch_pages; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | ret = misc_register(&fetchop_miscdev); |
| 379 | if (ret) { |
| 380 | printk(KERN_ERR |
| 381 | "%s: failed to register device %i\n", |
| 382 | FETCHOP_ID, ret); |
| 383 | goto free_scratch_pages; |
| 384 | } |
| 385 | } |
Jes Sorensen | 1a4b0fc | 2006-11-10 12:27:49 -0800 | [diff] [blame] | 386 | #endif |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 387 | ret = misc_register(&cached_miscdev); |
| 388 | if (ret) { |
| 389 | printk(KERN_ERR "%s: failed to register device %i\n", |
| 390 | CACHED_ID, ret); |
| 391 | if (is_sn2) |
| 392 | misc_deregister(&fetchop_miscdev); |
| 393 | goto free_scratch_pages; |
| 394 | } |
| 395 | ret = misc_register(&uncached_miscdev); |
| 396 | if (ret) { |
| 397 | printk(KERN_ERR "%s: failed to register device %i\n", |
| 398 | UNCACHED_ID, ret); |
| 399 | misc_deregister(&cached_miscdev); |
| 400 | if (is_sn2) |
| 401 | misc_deregister(&fetchop_miscdev); |
| 402 | goto free_scratch_pages; |
| 403 | } |
| 404 | |
| 405 | printk(KERN_INFO "%s %s initialized devices: %s %s %s\n", |
| 406 | MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "", |
| 407 | CACHED_ID, UNCACHED_ID); |
| 408 | |
| 409 | return 0; |
| 410 | |
| 411 | free_scratch_pages: |
| 412 | for_each_node(nid) { |
| 413 | if (scratch_page[nid] != 0) |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 414 | uncached_free_page(scratch_page[nid], 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 415 | } |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | static void __exit |
| 420 | mspec_exit(void) |
| 421 | { |
| 422 | int nid; |
| 423 | |
| 424 | misc_deregister(&uncached_miscdev); |
| 425 | misc_deregister(&cached_miscdev); |
| 426 | if (is_sn2) { |
| 427 | misc_deregister(&fetchop_miscdev); |
| 428 | |
| 429 | for_each_node(nid) { |
| 430 | if (scratch_page[nid] != 0) |
Dean Nelson | e4a064d | 2008-04-25 15:22:19 -0500 | [diff] [blame] | 431 | uncached_free_page(scratch_page[nid], 1); |
Jes Sorensen | 17a3b05 | 2006-09-27 01:50:11 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | module_init(mspec_init); |
| 437 | module_exit(mspec_exit); |
| 438 | |
| 439 | MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>"); |
| 440 | MODULE_DESCRIPTION("Driver for SGI SN special memory operations"); |
| 441 | MODULE_LICENSE("GPL"); |