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