2 * Copyright (C) 2001-2006 Silicon Graphics, Inc. All rights
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.
11 * SN Platform Special Memory (mspec) Support
13 * This driver exports the SN special memory (mspec) facility to user
15 * There are three types of memory made available thru this driver:
16 * fetchops, uncached and cached.
18 * Fetchops are atomic memory operations that are implemented in the
19 * memory controller on SGI SN hardware.
21 * Uncached are used for memory write combining feature of the ia64
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.
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>
42 #include <linux/vmalloc.h>
43 #include <linux/string.h>
44 #include <linux/slab.h>
45 #include <linux/numa.h>
47 #include <asm/pgtable.h>
48 #include <linux/atomic.h>
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>
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"
67 * Page types allocated by the device.
69 enum mspec_page_type
{
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.
85 * This structure is shared by all vma's that are split off from the
86 * original vma when split_vma()'s are done.
88 * The refcnt is incremented atomically because mm->mmap_sem does not
89 * protect in fork case where multiple tasks share the vma_data.
92 atomic_t refcnt
; /* Number of vmas sharing the data. */
93 spinlock_t lock
; /* Serialize access to this structure. */
94 int count
; /* Number of pages allocated. */
95 enum mspec_page_type type
; /* Type of pages allocated. */
96 unsigned long vm_start
; /* Original (unsplit) base. */
97 unsigned long vm_end
; /* Original (unsplit) end. */
98 unsigned long maddr
[0]; /* Array of MSPEC addresses. */
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
106 mspec_zero_block(unsigned long addr
, int len
)
116 nid
= nasid_to_cnodeid(get_node_number(__pa(addr
)));
117 p
= (void *)TO_AMO(scratch_page
[nid
]);
119 for (i
=0; i
< SH2_AMO_CACHE_ENTRIES
; i
++) {
120 FETCHOP_LOAD_OP(p
, FETCHOP_LOAD
);
121 p
+= FETCHOP_VAR_SIZE
;
125 status
= bte_copy(0, addr
& ~__IA64_UNCACHED_OFFSET
, len
,
126 BTE_WACQUIRE
| BTE_ZERO_FILL
, NULL
);
128 memset((char *) addr
, 0, len
);
137 * Called when a device mapping is created by a means other than mmap
138 * (via fork, munmap, etc.). Increments the reference count on the
139 * underlying mspec data so it is not freed prematurely.
142 mspec_open(struct vm_area_struct
*vma
)
144 struct vma_data
*vdata
;
146 vdata
= vma
->vm_private_data
;
147 atomic_inc(&vdata
->refcnt
);
153 * Called when unmapping a device mapping. Frees all mspec pages
154 * belonging to all the vma's sharing this vma_data structure.
157 mspec_close(struct vm_area_struct
*vma
)
159 struct vma_data
*vdata
;
160 int index
, last_index
;
161 unsigned long my_page
;
163 vdata
= vma
->vm_private_data
;
165 if (!atomic_dec_and_test(&vdata
->refcnt
))
168 last_index
= (vdata
->vm_end
- vdata
->vm_start
) >> PAGE_SHIFT
;
169 for (index
= 0; index
< last_index
; index
++) {
170 if (vdata
->maddr
[index
] == 0)
173 * Clear the page before sticking it back
176 my_page
= vdata
->maddr
[index
];
177 vdata
->maddr
[index
] = 0;
178 if (!mspec_zero_block(my_page
, PAGE_SIZE
))
179 uncached_free_page(my_page
, 1);
181 printk(KERN_WARNING
"mspec_close(): "
182 "failed to zero page %ld\n", my_page
);
191 * Creates a mspec page and maps it to user space.
194 mspec_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
196 unsigned long paddr
, maddr
;
198 pgoff_t index
= vmf
->pgoff
;
199 struct vma_data
*vdata
= vma
->vm_private_data
;
201 maddr
= (volatile unsigned long) vdata
->maddr
[index
];
203 maddr
= uncached_alloc_page(numa_node_id(), 1);
207 spin_lock(&vdata
->lock
);
208 if (vdata
->maddr
[index
] == 0) {
210 vdata
->maddr
[index
] = maddr
;
212 uncached_free_page(maddr
, 1);
213 maddr
= vdata
->maddr
[index
];
215 spin_unlock(&vdata
->lock
);
218 if (vdata
->type
== MSPEC_FETCHOP
)
219 paddr
= TO_AMO(maddr
);
221 paddr
= maddr
& ~__IA64_UNCACHED_OFFSET
;
223 pfn
= paddr
>> PAGE_SHIFT
;
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
230 vm_insert_pfn(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
232 return VM_FAULT_NOPAGE
;
235 static const struct vm_operations_struct mspec_vm_ops
= {
237 .close
= mspec_close
,
238 .fault
= mspec_fault
,
244 * Called when mmapping the device. Initializes the vma with a fault handler
245 * and private data structure necessary to allocate, track, and free the
249 mspec_mmap(struct file
*file
, struct vm_area_struct
*vma
,
250 enum mspec_page_type type
)
252 struct vma_data
*vdata
;
253 int pages
, vdata_size
;
255 if (vma
->vm_pgoff
!= 0)
258 if ((vma
->vm_flags
& VM_SHARED
) == 0)
261 if ((vma
->vm_flags
& VM_WRITE
) == 0)
264 pages
= vma_pages(vma
);
265 vdata_size
= sizeof(struct vma_data
) + pages
* sizeof(long);
266 if (vdata_size
<= PAGE_SIZE
)
267 vdata
= kzalloc(vdata_size
, GFP_KERNEL
);
269 vdata
= vzalloc(vdata_size
);
273 vdata
->vm_start
= vma
->vm_start
;
274 vdata
->vm_end
= vma
->vm_end
;
276 spin_lock_init(&vdata
->lock
);
277 atomic_set(&vdata
->refcnt
, 1);
278 vma
->vm_private_data
= vdata
;
280 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
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
;
289 fetchop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
291 return mspec_mmap(file
, vma
, MSPEC_FETCHOP
);
295 cached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
297 return mspec_mmap(file
, vma
, MSPEC_CACHED
);
301 uncached_mmap(struct file
*file
, struct vm_area_struct
*vma
)
303 return mspec_mmap(file
, vma
, MSPEC_UNCACHED
);
306 static const struct file_operations fetchop_fops
= {
307 .owner
= THIS_MODULE
,
308 .mmap
= fetchop_mmap
,
309 .llseek
= noop_llseek
,
312 static struct miscdevice fetchop_miscdev
= {
313 .minor
= MISC_DYNAMIC_MINOR
,
314 .name
= "sgi_fetchop",
315 .fops
= &fetchop_fops
318 static const struct file_operations cached_fops
= {
319 .owner
= THIS_MODULE
,
321 .llseek
= noop_llseek
,
324 static struct miscdevice cached_miscdev
= {
325 .minor
= MISC_DYNAMIC_MINOR
,
326 .name
= "mspec_cached",
330 static const struct file_operations uncached_fops
= {
331 .owner
= THIS_MODULE
,
332 .mmap
= uncached_mmap
,
333 .llseek
= noop_llseek
,
336 static struct miscdevice uncached_miscdev
= {
337 .minor
= MISC_DYNAMIC_MINOR
,
338 .name
= "mspec_uncached",
339 .fops
= &uncached_fops
345 * Called at boot time to initialize the mspec facility.
354 * The fetchop device only works on SN2 hardware, uncached and cached
355 * memory drivers should both be valid on all ia64 hardware
358 if (ia64_platform_is("sn2")) {
362 for_each_node_state(nid
, N_ONLINE
) {
367 scratch_page
[nid
] = uncached_alloc_page(nid
, 1);
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
;
378 ret
= misc_register(&fetchop_miscdev
);
381 "%s: failed to register device %i\n",
383 goto free_scratch_pages
;
387 ret
= misc_register(&cached_miscdev
);
389 printk(KERN_ERR
"%s: failed to register device %i\n",
392 misc_deregister(&fetchop_miscdev
);
393 goto free_scratch_pages
;
395 ret
= misc_register(&uncached_miscdev
);
397 printk(KERN_ERR
"%s: failed to register device %i\n",
399 misc_deregister(&cached_miscdev
);
401 misc_deregister(&fetchop_miscdev
);
402 goto free_scratch_pages
;
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
);
413 if (scratch_page
[nid
] != 0)
414 uncached_free_page(scratch_page
[nid
], 1);
424 misc_deregister(&uncached_miscdev
);
425 misc_deregister(&cached_miscdev
);
427 misc_deregister(&fetchop_miscdev
);
430 if (scratch_page
[nid
] != 0)
431 uncached_free_page(scratch_page
[nid
], 1);
436 module_init(mspec_init
);
437 module_exit(mspec_exit
);
439 MODULE_AUTHOR("Silicon Graphics, Inc. <linux-altix@sgi.com>");
440 MODULE_DESCRIPTION("Driver for SGI SN special memory operations");
441 MODULE_LICENSE("GPL");