]>
Commit | Line | Data |
---|---|---|
17a3b050 JS |
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 | ||
17a3b050 JS |
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> | |
4e950f6f | 41 | #include <linux/fs.h> |
17a3b050 JS |
42 | #include <linux/vmalloc.h> |
43 | #include <linux/string.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/numa.h> | |
f7d88d24 | 46 | #include <linux/refcount.h> |
17a3b050 | 47 | #include <asm/page.h> |
17a3b050 | 48 | #include <asm/pgtable.h> |
60063497 | 49 | #include <linux/atomic.h> |
17a3b050 JS |
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 | */ | |
4191ba26 | 70 | enum mspec_page_type { |
17a3b050 JS |
71 | MSPEC_FETCHOP = 1, |
72 | MSPEC_CACHED, | |
73 | MSPEC_UNCACHED | |
74 | }; | |
75 | ||
1a4b0fc5 | 76 | #ifdef CONFIG_SGI_SN |
17a3b050 | 77 | static int is_sn2; |
1a4b0fc5 JS |
78 | #else |
79 | #define is_sn2 0 | |
80 | #endif | |
17a3b050 JS |
81 | |
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. | |
4191ba26 CW |
86 | * 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. | |
17a3b050 JS |
91 | */ |
92 | struct vma_data { | |
f7d88d24 | 93 | refcount_t refcnt; /* Number of vmas sharing the data. */ |
4191ba26 | 94 | spinlock_t lock; /* Serialize access to this structure. */ |
17a3b050 | 95 | int count; /* Number of pages allocated. */ |
4191ba26 | 96 | enum mspec_page_type type; /* Type of pages allocated. */ |
4191ba26 CW |
97 | unsigned long vm_start; /* Original (unsplit) base. */ |
98 | unsigned long vm_end; /* Original (unsplit) end. */ | |
17a3b050 JS |
99 | unsigned long maddr[0]; /* Array of MSPEC addresses. */ |
100 | }; | |
101 | ||
102 | /* used on shub2 to clear FOP cache in the HUB */ | |
103 | static unsigned long scratch_page[MAX_NUMNODES]; | |
104 | #define SH2_AMO_CACHE_ENTRIES 4 | |
105 | ||
106 | static inline int | |
107 | mspec_zero_block(unsigned long addr, int len) | |
108 | { | |
109 | int status; | |
110 | ||
111 | if (is_sn2) { | |
112 | if (is_shub2()) { | |
113 | int nid; | |
114 | void *p; | |
115 | int i; | |
116 | ||
117 | nid = nasid_to_cnodeid(get_node_number(__pa(addr))); | |
118 | p = (void *)TO_AMO(scratch_page[nid]); | |
119 | ||
120 | for (i=0; i < SH2_AMO_CACHE_ENTRIES; i++) { | |
121 | FETCHOP_LOAD_OP(p, FETCHOP_LOAD); | |
122 | p += FETCHOP_VAR_SIZE; | |
123 | } | |
124 | } | |
125 | ||
126 | status = bte_copy(0, addr & ~__IA64_UNCACHED_OFFSET, len, | |
127 | BTE_WACQUIRE | BTE_ZERO_FILL, NULL); | |
128 | } else { | |
129 | memset((char *) addr, 0, len); | |
130 | status = 0; | |
131 | } | |
132 | return status; | |
133 | } | |
134 | ||
135 | /* | |
136 | * mspec_open | |
137 | * | |
138 | * Called when a device mapping is created by a means other than mmap | |
4191ba26 CW |
139 | * (via fork, munmap, etc.). Increments the reference count on the |
140 | * underlying mspec data so it is not freed prematurely. | |
17a3b050 JS |
141 | */ |
142 | static void | |
143 | mspec_open(struct vm_area_struct *vma) | |
144 | { | |
145 | struct vma_data *vdata; | |
146 | ||
147 | vdata = vma->vm_private_data; | |
f7d88d24 | 148 | refcount_inc(&vdata->refcnt); |
17a3b050 JS |
149 | } |
150 | ||
151 | /* | |
152 | * mspec_close | |
153 | * | |
154 | * Called when unmapping a device mapping. Frees all mspec pages | |
afa684f6 | 155 | * belonging to all the vma's sharing this vma_data structure. |
17a3b050 JS |
156 | */ |
157 | static void | |
158 | mspec_close(struct vm_area_struct *vma) | |
159 | { | |
160 | struct vma_data *vdata; | |
afa684f6 | 161 | int index, last_index; |
4191ba26 | 162 | unsigned long my_page; |
17a3b050 JS |
163 | |
164 | vdata = vma->vm_private_data; | |
17a3b050 | 165 | |
f7d88d24 | 166 | if (!refcount_dec_and_test(&vdata->refcnt)) |
afa684f6 | 167 | return; |
4191ba26 | 168 | |
afa684f6 CW |
169 | last_index = (vdata->vm_end - vdata->vm_start) >> PAGE_SHIFT; |
170 | for (index = 0; index < last_index; index++) { | |
4191ba26 | 171 | if (vdata->maddr[index] == 0) |
17a3b050 JS |
172 | continue; |
173 | /* | |
174 | * Clear the page before sticking it back | |
175 | * into the pool. | |
176 | */ | |
4191ba26 CW |
177 | my_page = vdata->maddr[index]; |
178 | vdata->maddr[index] = 0; | |
afa684f6 | 179 | if (!mspec_zero_block(my_page, PAGE_SIZE)) |
e4a064df | 180 | uncached_free_page(my_page, 1); |
17a3b050 JS |
181 | else |
182 | printk(KERN_WARNING "mspec_close(): " | |
afa684f6 | 183 | "failed to zero page %ld\n", my_page); |
17a3b050 | 184 | } |
4191ba26 | 185 | |
1d5cfdb0 | 186 | kvfree(vdata); |
17a3b050 JS |
187 | } |
188 | ||
17a3b050 | 189 | /* |
efe9e779 | 190 | * mspec_fault |
17a3b050 JS |
191 | * |
192 | * Creates a mspec page and maps it to user space. | |
193 | */ | |
efe9e779 | 194 | static int |
11bac800 | 195 | mspec_fault(struct vm_fault *vmf) |
17a3b050 JS |
196 | { |
197 | unsigned long paddr, maddr; | |
198 | unsigned long pfn; | |
efe9e779 | 199 | pgoff_t index = vmf->pgoff; |
11bac800 | 200 | struct vma_data *vdata = vmf->vma->vm_private_data; |
17a3b050 | 201 | |
17a3b050 JS |
202 | maddr = (volatile unsigned long) vdata->maddr[index]; |
203 | if (maddr == 0) { | |
e4a064df | 204 | maddr = uncached_alloc_page(numa_node_id(), 1); |
17a3b050 | 205 | if (maddr == 0) |
efe9e779 | 206 | return VM_FAULT_OOM; |
17a3b050 JS |
207 | |
208 | spin_lock(&vdata->lock); | |
209 | if (vdata->maddr[index] == 0) { | |
210 | vdata->count++; | |
211 | vdata->maddr[index] = maddr; | |
212 | } else { | |
e4a064df | 213 | uncached_free_page(maddr, 1); |
17a3b050 JS |
214 | maddr = vdata->maddr[index]; |
215 | } | |
216 | spin_unlock(&vdata->lock); | |
217 | } | |
218 | ||
219 | if (vdata->type == MSPEC_FETCHOP) | |
220 | paddr = TO_AMO(maddr); | |
221 | else | |
1a4b0fc5 | 222 | paddr = maddr & ~__IA64_UNCACHED_OFFSET; |
17a3b050 JS |
223 | |
224 | pfn = paddr >> PAGE_SHIFT; | |
225 | ||
efe9e779 NP |
226 | /* |
227 | * vm_insert_pfn can fail with -EBUSY, but in that case it will | |
228 | * be because another thread has installed the pte first, so it | |
229 | * is no problem. | |
230 | */ | |
11bac800 | 231 | vm_insert_pfn(vmf->vma, vmf->address, pfn); |
efe9e779 NP |
232 | |
233 | return VM_FAULT_NOPAGE; | |
17a3b050 JS |
234 | } |
235 | ||
f0f37e2f | 236 | static const struct vm_operations_struct mspec_vm_ops = { |
17a3b050 JS |
237 | .open = mspec_open, |
238 | .close = mspec_close, | |
efe9e779 | 239 | .fault = mspec_fault, |
17a3b050 JS |
240 | }; |
241 | ||
242 | /* | |
243 | * mspec_mmap | |
244 | * | |
af901ca1 | 245 | * Called when mmapping the device. Initializes the vma with a fault handler |
17a3b050 JS |
246 | * and private data structure necessary to allocate, track, and free the |
247 | * underlying pages. | |
248 | */ | |
249 | static int | |
4191ba26 CW |
250 | mspec_mmap(struct file *file, struct vm_area_struct *vma, |
251 | enum mspec_page_type type) | |
17a3b050 JS |
252 | { |
253 | struct vma_data *vdata; | |
1d5cfdb0 | 254 | int pages, vdata_size; |
17a3b050 JS |
255 | |
256 | if (vma->vm_pgoff != 0) | |
257 | return -EINVAL; | |
258 | ||
259 | if ((vma->vm_flags & VM_SHARED) == 0) | |
260 | return -EINVAL; | |
261 | ||
262 | if ((vma->vm_flags & VM_WRITE) == 0) | |
263 | return -EPERM; | |
264 | ||
a0ea59d5 | 265 | pages = vma_pages(vma); |
17a3b050 JS |
266 | vdata_size = sizeof(struct vma_data) + pages * sizeof(long); |
267 | if (vdata_size <= PAGE_SIZE) | |
658c74cf | 268 | vdata = kzalloc(vdata_size, GFP_KERNEL); |
1d5cfdb0 | 269 | else |
658c74cf | 270 | vdata = vzalloc(vdata_size); |
17a3b050 JS |
271 | if (!vdata) |
272 | return -ENOMEM; | |
17a3b050 | 273 | |
4191ba26 CW |
274 | vdata->vm_start = vma->vm_start; |
275 | vdata->vm_end = vma->vm_end; | |
17a3b050 JS |
276 | vdata->type = type; |
277 | spin_lock_init(&vdata->lock); | |
f7d88d24 | 278 | refcount_set(&vdata->refcnt, 1); |
17a3b050 JS |
279 | vma->vm_private_data = vdata; |
280 | ||
314e51b9 | 281 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; |
17a3b050 JS |
282 | if (vdata->type == MSPEC_FETCHOP || vdata->type == MSPEC_UNCACHED) |
283 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
284 | vma->vm_ops = &mspec_vm_ops; | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | static int | |
290 | fetchop_mmap(struct file *file, struct vm_area_struct *vma) | |
291 | { | |
292 | return mspec_mmap(file, vma, MSPEC_FETCHOP); | |
293 | } | |
294 | ||
295 | static int | |
296 | cached_mmap(struct file *file, struct vm_area_struct *vma) | |
297 | { | |
298 | return mspec_mmap(file, vma, MSPEC_CACHED); | |
299 | } | |
300 | ||
301 | static int | |
302 | uncached_mmap(struct file *file, struct vm_area_struct *vma) | |
303 | { | |
304 | return mspec_mmap(file, vma, MSPEC_UNCACHED); | |
305 | } | |
306 | ||
2b8693c0 | 307 | static const struct file_operations fetchop_fops = { |
17a3b050 | 308 | .owner = THIS_MODULE, |
6038f373 AB |
309 | .mmap = fetchop_mmap, |
310 | .llseek = noop_llseek, | |
17a3b050 JS |
311 | }; |
312 | ||
313 | static struct miscdevice fetchop_miscdev = { | |
314 | .minor = MISC_DYNAMIC_MINOR, | |
315 | .name = "sgi_fetchop", | |
316 | .fops = &fetchop_fops | |
317 | }; | |
318 | ||
2b8693c0 | 319 | static const struct file_operations cached_fops = { |
17a3b050 | 320 | .owner = THIS_MODULE, |
6038f373 AB |
321 | .mmap = cached_mmap, |
322 | .llseek = noop_llseek, | |
17a3b050 JS |
323 | }; |
324 | ||
325 | static struct miscdevice cached_miscdev = { | |
326 | .minor = MISC_DYNAMIC_MINOR, | |
327 | .name = "mspec_cached", | |
328 | .fops = &cached_fops | |
329 | }; | |
330 | ||
2b8693c0 | 331 | static const struct file_operations uncached_fops = { |
17a3b050 | 332 | .owner = THIS_MODULE, |
6038f373 AB |
333 | .mmap = uncached_mmap, |
334 | .llseek = noop_llseek, | |
17a3b050 JS |
335 | }; |
336 | ||
337 | static struct miscdevice uncached_miscdev = { | |
338 | .minor = MISC_DYNAMIC_MINOR, | |
339 | .name = "mspec_uncached", | |
340 | .fops = &uncached_fops | |
341 | }; | |
342 | ||
343 | /* | |
344 | * mspec_init | |
345 | * | |
346 | * Called at boot time to initialize the mspec facility. | |
347 | */ | |
348 | static int __init | |
349 | mspec_init(void) | |
350 | { | |
351 | int ret; | |
352 | int nid; | |
353 | ||
354 | /* | |
355 | * The fetchop device only works on SN2 hardware, uncached and cached | |
356 | * memory drivers should both be valid on all ia64 hardware | |
357 | */ | |
1a4b0fc5 | 358 | #ifdef CONFIG_SGI_SN |
17a3b050 JS |
359 | if (ia64_platform_is("sn2")) { |
360 | is_sn2 = 1; | |
361 | if (is_shub2()) { | |
362 | ret = -ENOMEM; | |
2dca53a9 | 363 | for_each_node_state(nid, N_ONLINE) { |
17a3b050 JS |
364 | int actual_nid; |
365 | int nasid; | |
366 | unsigned long phys; | |
367 | ||
e4a064df | 368 | scratch_page[nid] = uncached_alloc_page(nid, 1); |
17a3b050 JS |
369 | if (scratch_page[nid] == 0) |
370 | goto free_scratch_pages; | |
371 | phys = __pa(scratch_page[nid]); | |
372 | nasid = get_node_number(phys); | |
373 | actual_nid = nasid_to_cnodeid(nasid); | |
374 | if (actual_nid != nid) | |
375 | goto free_scratch_pages; | |
376 | } | |
377 | } | |
378 | ||
379 | ret = misc_register(&fetchop_miscdev); | |
380 | if (ret) { | |
381 | printk(KERN_ERR | |
382 | "%s: failed to register device %i\n", | |
383 | FETCHOP_ID, ret); | |
384 | goto free_scratch_pages; | |
385 | } | |
386 | } | |
1a4b0fc5 | 387 | #endif |
17a3b050 JS |
388 | ret = misc_register(&cached_miscdev); |
389 | if (ret) { | |
390 | printk(KERN_ERR "%s: failed to register device %i\n", | |
391 | CACHED_ID, ret); | |
392 | if (is_sn2) | |
393 | misc_deregister(&fetchop_miscdev); | |
394 | goto free_scratch_pages; | |
395 | } | |
396 | ret = misc_register(&uncached_miscdev); | |
397 | if (ret) { | |
398 | printk(KERN_ERR "%s: failed to register device %i\n", | |
399 | UNCACHED_ID, ret); | |
400 | misc_deregister(&cached_miscdev); | |
401 | if (is_sn2) | |
402 | misc_deregister(&fetchop_miscdev); | |
403 | goto free_scratch_pages; | |
404 | } | |
405 | ||
406 | printk(KERN_INFO "%s %s initialized devices: %s %s %s\n", | |
407 | MSPEC_BASENAME, REVISION, is_sn2 ? FETCHOP_ID : "", | |
408 | CACHED_ID, UNCACHED_ID); | |
409 | ||
410 | return 0; | |
411 | ||
412 | free_scratch_pages: | |
413 | for_each_node(nid) { | |
414 | if (scratch_page[nid] != 0) | |
e4a064df | 415 | uncached_free_page(scratch_page[nid], 1); |
17a3b050 JS |
416 | } |
417 | return ret; | |
418 | } | |
419 | ||
420 | static void __exit | |
421 | mspec_exit(void) | |
422 | { | |
423 | int nid; | |
424 | ||
425 | misc_deregister(&uncached_miscdev); | |
426 | misc_deregister(&cached_miscdev); | |
427 | if (is_sn2) { | |
428 | misc_deregister(&fetchop_miscdev); | |
429 | ||
430 | for_each_node(nid) { | |
431 | if (scratch_page[nid] != 0) | |
e4a064df | 432 | uncached_free_page(scratch_page[nid], 1); |
17a3b050 JS |
433 | } |
434 | } | |
435 | } | |
436 | ||
437 | module_init(mspec_init); | |
438 | module_exit(mspec_exit); | |
439 | ||
440 | MODULE_AUTHOR("Silicon Graphics, Inc. <[email protected]>"); | |
441 | MODULE_DESCRIPTION("Driver for SGI SN special memory operations"); | |
442 | MODULE_LICENSE("GPL"); |