blob: 966a95bc974b2c0489c7c4f33b52d1b3872238a0 [file] [log] [blame]
Jes Sorensen17a3b052006-09-27 01:50:11 -07001/*
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 Sorensen17a3b052006-09-27 01:50:11 -070033#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 Dobriyan4e950f62007-07-30 02:36:13 +040041#include <linux/fs.h>
Jes Sorensen17a3b052006-09-27 01:50:11 -070042#include <linux/vmalloc.h>
43#include <linux/string.h>
44#include <linux/slab.h>
45#include <linux/numa.h>
46#include <asm/page.h>
47#include <asm/system.h>
48#include <asm/pgtable.h>
49#include <asm/atomic.h>
50#include <asm/tlbflush.h>
51#include <asm/uncached.h>
52#include <asm/sn/addrs.h>
53#include <asm/sn/arch.h>
54#include <asm/sn/mspec.h>
55#include <asm/sn/sn_cpuid.h>
56#include <asm/sn/io.h>
57#include <asm/sn/bte.h>
58#include <asm/sn/shubio.h>
59
60
61#define FETCHOP_ID "SGI Fetchop,"
62#define CACHED_ID "Cached,"
63#define UNCACHED_ID "Uncached"
64#define REVISION "4.0"
65#define MSPEC_BASENAME "mspec"
66
67/*
68 * Page types allocated by the device.
69 */
Cliff Wickman4191ba22007-09-18 22:46:31 -070070enum mspec_page_type {
Jes Sorensen17a3b052006-09-27 01:50:11 -070071 MSPEC_FETCHOP = 1,
72 MSPEC_CACHED,
73 MSPEC_UNCACHED
74};
75
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080076#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -070077static int is_sn2;
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -080078#else
79#define is_sn2 0
80#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -070081
82/*
83 * One of these structures is allocated when an mspec region is mmaped. The
84 * structure is pointed to by the vma->vm_private_data field in the vma struct.
85 * This structure is used to record the addresses of the mspec pages.
Cliff Wickman4191ba22007-09-18 22:46:31 -070086 * This structure is shared by all vma's that are split off from the
87 * original vma when split_vma()'s are done.
88 *
89 * The refcnt is incremented atomically because mm->mmap_sem does not
90 * protect in fork case where multiple tasks share the vma_data.
Jes Sorensen17a3b052006-09-27 01:50:11 -070091 */
92struct vma_data {
93 atomic_t refcnt; /* Number of vmas sharing the data. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070094 spinlock_t lock; /* Serialize access to this structure. */
Jes Sorensen17a3b052006-09-27 01:50:11 -070095 int count; /* Number of pages allocated. */
Cliff Wickman4191ba22007-09-18 22:46:31 -070096 enum mspec_page_type type; /* Type of pages allocated. */
97 int flags; /* See VMD_xxx below. */
98 unsigned long vm_start; /* Original (unsplit) base. */
99 unsigned long vm_end; /* Original (unsplit) end. */
Jes Sorensen17a3b052006-09-27 01:50:11 -0700100 unsigned long maddr[0]; /* Array of MSPEC addresses. */
101};
102
Cliff Wickman4191ba22007-09-18 22:46:31 -0700103#define VMD_VMALLOCED 0x1 /* vmalloc'd rather than kmalloc'd */
104
Jes Sorensen17a3b052006-09-27 01:50:11 -0700105/* used on shub2 to clear FOP cache in the HUB */
106static unsigned long scratch_page[MAX_NUMNODES];
107#define SH2_AMO_CACHE_ENTRIES 4
108
109static inline int
110mspec_zero_block(unsigned long addr, int len)
111{
112 int status;
113
114 if (is_sn2) {
115 if (is_shub2()) {
116 int nid;
117 void *p;
118 int i;
119
120 nid = nasid_to_cnodeid(get_node_number(__pa(addr)));
121 p = (void *)TO_AMO(scratch_page[nid]);
122
123 for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) {
124 FETCHOP_LOAD_OP(p, FETCHOP_LOAD);
125 p += FETCHOP_VAR_SIZE;
126 }
127 }
128
129 status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len,
130 BTE_WACQUIRE | BTE_ZERO_FILL, NULL);
131 } else {
132 memset((char *) addr, 0, len);
133 status = 0;
134 }
135 return status;
136}
137
138/*
139 * mspec_open
140 *
141 * Called when a device mapping is created by a means other than mmap
Cliff Wickman4191ba22007-09-18 22:46:31 -0700142 * (via fork, munmap, etc.). Increments the reference count on the
143 * underlying mspec data so it is not freed prematurely.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700144 */
145static void
146mspec_open(struct vm_area_struct *vma)
147{
148 struct vma_data *vdata;
149
150 vdata = vma->vm_private_data;
151 atomic_inc(&vdata->refcnt);
152}
153
154/*
155 * mspec_close
156 *
157 * Called when unmapping a device mapping. Frees all mspec pages
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700158 * belonging to all the vma's sharing this vma_data structure.
Jes Sorensen17a3b052006-09-27 01:50:11 -0700159 */
160static void
161mspec_close(struct vm_area_struct *vma)
162{
163 struct vma_data *vdata;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700164 int index, last_index;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700165 unsigned long my_page;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700166
167 vdata = vma->vm_private_data;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700168
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700169 if (!atomic_dec_and_test(&vdata->refcnt))
170 return;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700171
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700172 last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT;
173 for (index = 0; index < last_index; index++) {
Cliff Wickman4191ba22007-09-18 22:46:31 -0700174 if (vdata->maddr[index] == 0)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700175 continue;
176 /*
177 * Clear the page before sticking it back
178 * into the pool.
179 */
Cliff Wickman4191ba22007-09-18 22:46:31 -0700180 my_page = vdata->maddr[index];
181 vdata->maddr[index] = 0;
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700182 if (!mspec_zero_block(my_page, PAGE_SIZE))
Dean Nelsone4a064d2008-04-25 15:22:19 -0500183 uncached_free_page(my_page, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700184 else
185 printk(KERN_WARNING "mspec_close(): "
Cliff Wickmanafa684f2007-09-24 21:24:41 -0700186 "failed to zero page %ld\n", my_page);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700187 }
Cliff Wickman4191ba22007-09-18 22:46:31 -0700188
189 if (vdata->flags & VMD_VMALLOCED)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700190 vfree(vdata);
Cliff Wickman4191ba22007-09-18 22:46:31 -0700191 else
192 kfree(vdata);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700193}
194
Jes Sorensen17a3b052006-09-27 01:50:11 -0700195/*
Nick Pigginefe9e772008-07-23 21:27:00 -0700196 * mspec_fault
Jes Sorensen17a3b052006-09-27 01:50:11 -0700197 *
198 * Creates a mspec page and maps it to user space.
199 */
Nick Pigginefe9e772008-07-23 21:27:00 -0700200static int
201mspec_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700202{
203 unsigned long paddr, maddr;
204 unsigned long pfn;
Nick Pigginefe9e772008-07-23 21:27:00 -0700205 pgoff_t index = vmf->pgoff;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700206 struct vma_data *vdata = vma->vm_private_data;
207
Jes Sorensen17a3b052006-09-27 01:50:11 -0700208 maddr = (volatile unsigned long) vdata->maddr[index];
209 if (maddr == 0) {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500210 maddr = uncached_alloc_page(numa_node_id(), 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700211 if (maddr == 0)
Nick Pigginefe9e772008-07-23 21:27:00 -0700212 return VM_FAULT_OOM;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700213
214 spin_lock(&vdata->lock);
215 if (vdata->maddr[index] == 0) {
216 vdata->count++;
217 vdata->maddr[index] = maddr;
218 } else {
Dean Nelsone4a064d2008-04-25 15:22:19 -0500219 uncached_free_page(maddr, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700220 maddr = vdata->maddr[index];
221 }
222 spin_unlock(&vdata->lock);
223 }
224
225 if (vdata->type == MSPEC_FETCHOP)
226 paddr = TO_AMO(maddr);
227 else
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800228 paddr = maddr & ~__IA64_UNCACHED_OFFSET;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700229
230 pfn = paddr >> PAGE_SHIFT;
231
Nick Pigginefe9e772008-07-23 21:27:00 -0700232 /*
233 * vm_insert_pfn can fail with -EBUSY, but in that case it will
234 * be because another thread has installed the pte first, so it
235 * is no problem.
236 */
237 vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
238
239 return VM_FAULT_NOPAGE;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700240}
241
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400242static const struct vm_operations_struct mspec_vm_ops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700243 .open = mspec_open,
244 .close = mspec_close,
Nick Pigginefe9e772008-07-23 21:27:00 -0700245 .fault = mspec_fault,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700246};
247
248/*
249 * mspec_mmap
250 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200251 * Called when mmapping the device. Initializes the vma with a fault handler
Jes Sorensen17a3b052006-09-27 01:50:11 -0700252 * and private data structure necessary to allocate, track, and free the
253 * underlying pages.
254 */
255static int
Cliff Wickman4191ba22007-09-18 22:46:31 -0700256mspec_mmap(struct file *file, struct vm_area_struct *vma,
257 enum mspec_page_type type)
Jes Sorensen17a3b052006-09-27 01:50:11 -0700258{
259 struct vma_data *vdata;
Cliff Wickman4191ba22007-09-18 22:46:31 -0700260 int pages, vdata_size, flags = 0;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700261
262 if (vma->vm_pgoff != 0)
263 return -EINVAL;
264
265 if ((vma->vm_flags & VM_SHARED) == 0)
266 return -EINVAL;
267
268 if ((vma->vm_flags & VM_WRITE) == 0)
269 return -EPERM;
270
271 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
272 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
273 if (vdata_size <= PAGE_SIZE)
274 vdata = kmalloc(vdata_size, GFP_KERNEL);
Cliff Wickman4191ba22007-09-18 22:46:31 -0700275 else {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700276 vdata = vmalloc(vdata_size);
Cliff Wickman4191ba22007-09-18 22:46:31 -0700277 flags = VMD_VMALLOCED;
278 }
Jes Sorensen17a3b052006-09-27 01:50:11 -0700279 if (!vdata)
280 return -ENOMEM;
281 memset(vdata, 0, vdata_size);
282
Cliff Wickman4191ba22007-09-18 22:46:31 -0700283 vdata->vm_start = vma->vm_start;
284 vdata->vm_end = vma->vm_end;
285 vdata->flags = flags;
Jes Sorensen17a3b052006-09-27 01:50:11 -0700286 vdata->type = type;
287 spin_lock_init(&vdata->lock);
288 vdata->refcnt = ATOMIC_INIT(1);
289 vma->vm_private_data = vdata;
290
Nick Piggin2f987352008-02-02 03:08:53 +0100291 vma->vm_flags |= (VM_IO | VM_RESERVED | VM_PFNMAP | VM_DONTEXPAND);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700292 if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED)
293 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
294 vma->vm_ops = &mspec_vm_ops;
295
296 return 0;
297}
298
299static int
300fetchop_mmap(struct file *file, struct vm_area_struct *vma)
301{
302 return mspec_mmap(file, vma, MSPEC_FETCHOP);
303}
304
305static int
306cached_mmap(struct file *file, struct vm_area_struct *vma)
307{
308 return mspec_mmap(file, vma, MSPEC_CACHED);
309}
310
311static int
312uncached_mmap(struct file *file, struct vm_area_struct *vma)
313{
314 return mspec_mmap(file, vma, MSPEC_UNCACHED);
315}
316
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800317static const struct file_operations fetchop_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700318 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200319 .mmap = fetchop_mmap,
320 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700321};
322
323static struct miscdevice fetchop_miscdev = {
324 .minor = MISC_DYNAMIC_MINOR,
325 .name = "sgi_fetchop",
326 .fops = &fetchop_fops
327};
328
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800329static const struct file_operations cached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700330 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200331 .mmap = cached_mmap,
332 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700333};
334
335static struct miscdevice cached_miscdev = {
336 .minor = MISC_DYNAMIC_MINOR,
337 .name = "mspec_cached",
338 .fops = &cached_fops
339};
340
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800341static const struct file_operations uncached_fops = {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700342 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200343 .mmap = uncached_mmap,
344 .llseek = noop_llseek,
Jes Sorensen17a3b052006-09-27 01:50:11 -0700345};
346
347static struct miscdevice uncached_miscdev = {
348 .minor = MISC_DYNAMIC_MINOR,
349 .name = "mspec_uncached",
350 .fops = &uncached_fops
351};
352
353/*
354 * mspec_init
355 *
356 * Called at boot time to initialize the mspec facility.
357 */
358static int __init
359mspec_init(void)
360{
361 int ret;
362 int nid;
363
364 /*
365 * The fetchop device only works on SN2 hardware, uncached and cached
366 * memory drivers should both be valid on all ia64 hardware
367 */
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800368#ifdef CONFIG_SGI_SN
Jes Sorensen17a3b052006-09-27 01:50:11 -0700369 if (ia64_platform_is("sn2")) {
370 is_sn2 = 1;
371 if (is_shub2()) {
372 ret = -ENOMEM;
Christoph Lameter2dca53a2007-10-16 01:25:33 -0700373 for_each_node_state(nid, N_ONLINE) {
Jes Sorensen17a3b052006-09-27 01:50:11 -0700374 int actual_nid;
375 int nasid;
376 unsigned long phys;
377
Dean Nelsone4a064d2008-04-25 15:22:19 -0500378 scratch_page[nid] = uncached_alloc_page(nid, 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700379 if (scratch_page[nid] == 0)
380 goto free_scratch_pages;
381 phys = __pa(scratch_page[nid]);
382 nasid = get_node_number(phys);
383 actual_nid = nasid_to_cnodeid(nasid);
384 if (actual_nid != nid)
385 goto free_scratch_pages;
386 }
387 }
388
389 ret = misc_register(&fetchop_miscdev);
390 if (ret) {
391 printk(KERN_ERR
392 "%s: failed to register device %i\n",
393 FETCHOP_ID, ret);
394 goto free_scratch_pages;
395 }
396 }
Jes Sorensen1a4b0fc2006-11-10 12:27:49 -0800397#endif
Jes Sorensen17a3b052006-09-27 01:50:11 -0700398 ret = misc_register(&cached_miscdev);
399 if (ret) {
400 printk(KERN_ERR "%s: failed to register device %i\n",
401 CACHED_ID, ret);
402 if (is_sn2)
403 misc_deregister(&fetchop_miscdev);
404 goto free_scratch_pages;
405 }
406 ret = misc_register(&uncached_miscdev);
407 if (ret) {
408 printk(KERN_ERR "%s: failed to register device %i\n",
409 UNCACHED_ID, ret);
410 misc_deregister(&cached_miscdev);
411 if (is_sn2)
412 misc_deregister(&fetchop_miscdev);
413 goto free_scratch_pages;
414 }
415
416 printk(KERN_INFO "%s %s initialized devices: %s %s %s\n",
417 MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "",
418 CACHED_ID, UNCACHED_ID);
419
420 return 0;
421
422 free_scratch_pages:
423 for_each_node(nid) {
424 if (scratch_page[nid] != 0)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500425 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700426 }
427 return ret;
428}
429
430static void __exit
431mspec_exit(void)
432{
433 int nid;
434
435 misc_deregister(&uncached_miscdev);
436 misc_deregister(&cached_miscdev);
437 if (is_sn2) {
438 misc_deregister(&fetchop_miscdev);
439
440 for_each_node(nid) {
441 if (scratch_page[nid] != 0)
Dean Nelsone4a064d2008-04-25 15:22:19 -0500442 uncached_free_page(scratch_page[nid], 1);
Jes Sorensen17a3b052006-09-27 01:50:11 -0700443 }
444 }
445}
446
447module_init(mspec_init);
448module_exit(mspec_exit);
449
450MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
451MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
452MODULE_LICENSE("GPL");