Whamcloud - gitweb
922a1b875ae5a840b6fee0de61f8e83bc35fe1d3
[fs/lustre-release.git] / lnet / include / libcfs / darwin / darwin-mem.h
1 #ifndef __LIBCFS_DARWIN_CFS_MEM_H__
2 #define __LIBCFS_DARWIN_CFS_MEM_H__
3
4 #ifndef __LIBCFS_LIBCFS_H__
5 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
6 #endif
7
8 #ifdef __KERNEL__
9
10 #include <sys/types.h>
11 #include <sys/systm.h>
12
13 #include <sys/vm.h>
14 #include <sys/kernel.h>
15 #include <sys/ubc.h>
16 #include <sys/uio.h>
17 #include <sys/malloc.h>
18 #include <sys/mbuf.h>
19 #include <sys/lockf.h>
20
21 #include <mach/mach_types.h>
22 #include <mach/vm_types.h>
23 #include <vm/pmap.h>
24 #include <vm/vm_kern.h>
25 #include <mach/machine/vm_param.h>
26 #include <kern/thread_call.h>
27 #include <sys/param.h>
28 #include <sys/vm.h>
29
30 #include <libcfs/darwin/darwin-types.h>
31 #include <libcfs/darwin/darwin-sync.h>
32 #include <libcfs/darwin/darwin-lock.h>
33 #include <libcfs/list.h>
34
35 /*
36  * Page of OSX
37  *
38  * There is no page in OSX, however, we need page in lustre.
39  */
40 #define PAGE_MASK                               (~(PAGE_SIZE-1))
41 #define _ALIGN_UP(addr,size)                    (((addr)+((size)-1))&(~((size)-1)))
42 #define _ALIGN(addr,size)                       _ALIGN_UP(addr,size)
43 #define PAGE_ALIGN(addr)                        _ALIGN(addr, PAGE_SIZE)
44
45 /*
46  * Basic xnu_page struct, should be binary compatibility with
47  * all page types in xnu (we have only xnu_raw_page, xll_page now)
48  */
49
50 /* Variable sized pages are not supported */
51
52 #define CFS_PAGE_SHIFT  12
53 #define CFS_PAGE_SIZE   (1 << CFS_PAGE_SHIFT)
54 #define PAGE_CACHE_SIZE CFS_PAGE_SIZE
55 #define CFS_PAGE_MASK   (~(CFS_PAGE_SIZE - 1))
56
57 enum {
58         XNU_PAGE_RAW,
59         XNU_PAGE_XLL,
60         XNU_PAGE_NTYPES
61 };
62
63 typedef __u32 page_off_t;
64
65 /*
66  * For XNU we have our own page cache built on top of underlying BSD/MACH
67  * infrastructure. In particular, we have two disjoint types of pages:
68  *
69  *    - "raw" pages (XNU_PAGE_RAW): these are just buffers mapped into KVM,
70  *    based on UPLs, and
71  *
72  *    - "xll" pages (XNU_PAGE_XLL): these are used by file system to cache
73  *    file data, owned by file system objects, hashed, lrued, etc.
74  *
75  * cfs_page_t has to cover both of them, because core Lustre code is based on
76  * the Linux assumption that page is _both_ memory buffer and file system
77  * caching entity.
78  *
79  * To achieve this, all types of pages supported on XNU has to start from
80  * common header that contains only "page type". Common cfs_page_t operations
81  * dispatch through operation vector based on page type.
82  *
83  */
84 typedef struct xnu_page {
85         int type;
86 } cfs_page_t;
87
88 struct xnu_page_ops {
89         void *(*page_map)        (cfs_page_t *);
90         void  (*page_unmap)      (cfs_page_t *);
91         void *(*page_address)    (cfs_page_t *);
92 };
93
94 void xnu_page_ops_register(int type, struct xnu_page_ops *ops);
95 void xnu_page_ops_unregister(int type);
96
97 /*
98  * raw page, no cache object, just like buffer
99  */
100 struct xnu_raw_page {
101         struct xnu_page header;
102         vm_address_t    virtual;
103         upl_t           upl;
104         int             order;
105         atomic_t        count;
106         void           *private;
107 };
108
109 /*
110  * Public interface to lustre
111  *
112  * - cfs_alloc_pages(f, o)
113  * - cfs_alloc_page(f)
114  * - cfs_free_pages(p, o)
115  * - cfs_free_page(p)
116  * - cfs_kmap(p)
117  * - cfs_kunmap(p)
118  * - cfs_page_address(p)
119  */
120
121 /*
122  * Of all functions above only cfs_kmap(), cfs_kunmap(), and
123  * cfs_page_address() can be called on file system pages. The rest is for raw
124  * pages only.
125  */
126
127 cfs_page_t *cfs_alloc_pages(u_int32_t flags, u_int32_t order);
128 cfs_page_t *cfs_alloc_page(u_int32_t flags);
129 void cfs_free_pages(cfs_page_t *pages, int order);
130 void cfs_free_page(cfs_page_t *page);
131 void cfs_get_page(cfs_page_t *page);
132 int cfs_put_page_testzero(cfs_page_t *page);
133 int cfs_page_count(cfs_page_t *page);
134 void cfs_set_page_count(cfs_page_t *page, int v);
135
136 void *cfs_page_address(cfs_page_t *pg);
137 void *cfs_kmap(cfs_page_t *pg);
138 void cfs_kunmap(cfs_page_t *pg);
139
140 /*
141  * Memory allocator
142  */
143
144 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
145 extern void  cfs_free(void *addr);
146
147 extern void *cfs_alloc_large(size_t nr_bytes);
148 extern void  cfs_free_large(void *addr);
149
150 /*
151  * Slab:
152  *
153  * No slab in OSX, use zone allocator to fake slab
154  */
155 #define SLAB_HWCACHE_ALIGN              0
156
157 typedef struct cfs_mem_cache {
158         struct list_head        link;
159         zone_t                  zone;
160         int                     size;
161         char                    name [ZONE_NAME_MAX_LEN];
162 } cfs_mem_cache_t;
163
164 #define KMEM_CACHE_MAX_COUNT    64
165 #define KMEM_MAX_ZONE           8192
166
167 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long,
168                                                void (*)(void *, cfs_mem_cache_t *, unsigned long),
169                                                void (*)(void *, cfs_mem_cache_t *, unsigned long));
170 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
171 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
172 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
173
174 /*
175  * Misc
176  */
177 /* XXX fix me */
178 #define num_physpages                   (64 * 1024)
179
180 #define CFS_DECL_MMSPACE                
181 #define CFS_MMSPACE_OPEN                do {} while(0)
182 #define CFS_MMSPACE_CLOSE               do {} while(0)
183
184 #define copy_from_user(kaddr, uaddr, size)      copyin((caddr_t)uaddr, (caddr_t)kaddr, size)
185 #define copy_to_user(uaddr, kaddr, size)        copyout((caddr_t)kaddr, (caddr_t)uaddr, size)
186
187 #if defined (__ppc__)
188 #define mb()  __asm__ __volatile__ ("sync" : : : "memory")
189 #define rmb()  __asm__ __volatile__ ("sync" : : : "memory")
190 #define wmb()  __asm__ __volatile__ ("eieio" : : : "memory")
191 #elif defined (__i386__)
192 #define mb()    __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
193 #define rmb()   mb()
194 #define wmb()   __asm__ __volatile__ ("": : :"memory")
195 #else
196 #error architecture not supported
197 #endif
198
199 #else   /* !__KERNEL__ */
200
201 typedef struct cfs_page{
202         void    *foo;
203 } cfs_page_t;
204 #endif  /* __KERNEL__ */
205
206 #endif  /* __XNU_CFS_MEM_H__ */