Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1 | /* drivers/android/pmem.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 4 | * Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/miscdevice.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/list.h> |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 23 | #include <linux/debugfs.h> |
| 24 | #include <linux/android_pmem.h> |
| 25 | #include <linux/mempolicy.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | #include <linux/kobject.h> |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/memory_alloc.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/mm_types.h> |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 32 | #include <asm/io.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/cacheflush.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #include <asm/sizes.h> |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 36 | #include <asm/mach/map.h> |
| 37 | #include <asm/page.h> |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 38 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | #define PMEM_MAX_DEVICES (10) |
| 40 | |
| 41 | #define PMEM_MAX_ORDER (128) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 42 | #define PMEM_MIN_ALLOC PAGE_SIZE |
| 43 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | #define PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS (64) |
| 45 | |
| 46 | #define PMEM_32BIT_WORD_ORDER (5) |
| 47 | #define PMEM_BITS_PER_WORD_MASK (BITS_PER_LONG - 1) |
| 48 | |
| 49 | #ifdef CONFIG_ANDROID_PMEM_DEBUG |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 50 | #define PMEM_DEBUG 1 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | #else |
| 52 | #define PMEM_DEBUG 0 |
| 53 | #endif |
| 54 | |
| 55 | #define SYSTEM_ALLOC_RETRY 10 |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 56 | |
| 57 | /* indicates that a refernce to this file has been taken via get_pmem_file, |
| 58 | * the file should not be released until put_pmem_file is called */ |
| 59 | #define PMEM_FLAGS_BUSY 0x1 |
| 60 | /* indicates that this is a suballocation of a larger master range */ |
| 61 | #define PMEM_FLAGS_CONNECTED 0x1 << 1 |
| 62 | /* indicates this is a master and not a sub allocation and that it is mmaped */ |
| 63 | #define PMEM_FLAGS_MASTERMAP 0x1 << 2 |
| 64 | /* submap and unsubmap flags indicate: |
| 65 | * 00: subregion has never been mmaped |
| 66 | * 10: subregion has been mmaped, reference to the mm was taken |
| 67 | * 11: subretion has ben released, refernece to the mm still held |
| 68 | * 01: subretion has been released, reference to the mm has been released |
| 69 | */ |
| 70 | #define PMEM_FLAGS_SUBMAP 0x1 << 3 |
| 71 | #define PMEM_FLAGS_UNSUBMAP 0x1 << 4 |
| 72 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 73 | struct pmem_data { |
| 74 | /* in alloc mode: an index into the bitmap |
| 75 | * in no_alloc mode: the size of the allocation */ |
| 76 | int index; |
| 77 | /* see flags above for descriptions */ |
| 78 | unsigned int flags; |
| 79 | /* protects this data field, if the mm_mmap sem will be held at the |
| 80 | * same time as this sem, the mm sem must be taken first (as this is |
| 81 | * the order for vma_open and vma_close ops */ |
| 82 | struct rw_semaphore sem; |
| 83 | /* info about the mmaping process */ |
| 84 | struct vm_area_struct *vma; |
| 85 | /* task struct of the mapping process */ |
| 86 | struct task_struct *task; |
| 87 | /* process id of teh mapping process */ |
| 88 | pid_t pid; |
| 89 | /* file descriptor of the master */ |
| 90 | int master_fd; |
| 91 | /* file struct of the master */ |
| 92 | struct file *master_file; |
| 93 | /* a list of currently available regions if this is a suballocation */ |
| 94 | struct list_head region_list; |
| 95 | /* a linked list of data so we can access them for debugging */ |
| 96 | struct list_head list; |
| 97 | #if PMEM_DEBUG |
| 98 | int ref; |
| 99 | #endif |
| 100 | }; |
| 101 | |
| 102 | struct pmem_bits { |
| 103 | unsigned allocated:1; /* 1 if allocated, 0 if free */ |
| 104 | unsigned order:7; /* size of the region in pmem space */ |
| 105 | }; |
| 106 | |
| 107 | struct pmem_region_node { |
| 108 | struct pmem_region region; |
| 109 | struct list_head list; |
| 110 | }; |
| 111 | |
| 112 | #define PMEM_DEBUG_MSGS 0 |
| 113 | #if PMEM_DEBUG_MSGS |
| 114 | #define DLOG(fmt,args...) \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | do { pr_debug("[%s:%s:%d] "fmt, __FILE__, __func__, __LINE__, \ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 116 | ##args); } \ |
| 117 | while (0) |
| 118 | #else |
| 119 | #define DLOG(x...) do {} while (0) |
| 120 | #endif |
| 121 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 122 | enum pmem_align { |
| 123 | PMEM_ALIGN_4K, |
| 124 | PMEM_ALIGN_1M, |
| 125 | }; |
| 126 | |
| 127 | #define PMEM_NAME_SIZE 16 |
| 128 | |
| 129 | struct alloc_list { |
| 130 | void *addr; /* physical addr of allocation */ |
| 131 | void *aaddr; /* aligned physical addr */ |
| 132 | unsigned int size; /* total size of allocation */ |
| 133 | unsigned char __iomem *vaddr; /* Virtual addr */ |
| 134 | struct list_head allocs; |
| 135 | }; |
| 136 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 137 | struct pmem_info { |
| 138 | struct miscdevice dev; |
| 139 | /* physical start address of the remaped pmem space */ |
| 140 | unsigned long base; |
| 141 | /* vitual start address of the remaped pmem space */ |
| 142 | unsigned char __iomem *vbase; |
| 143 | /* total size of the pmem space */ |
| 144 | unsigned long size; |
| 145 | /* number of entries in the pmem space */ |
| 146 | unsigned long num_entries; |
| 147 | /* pfn of the garbage page in memory */ |
| 148 | unsigned long garbage_pfn; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 149 | /* which memory type (i.e. SMI, EBI1) this PMEM device is backed by */ |
| 150 | unsigned memory_type; |
| 151 | |
| 152 | char name[PMEM_NAME_SIZE]; |
| 153 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 154 | /* index of the garbage page in the pmem space */ |
| 155 | int garbage_index; |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 156 | /* reserved virtual address range */ |
| 157 | struct vm_struct *area; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | |
| 159 | enum pmem_allocator_type allocator_type; |
| 160 | |
| 161 | int (*allocate)(const int, |
| 162 | const unsigned long, |
| 163 | const unsigned int); |
| 164 | int (*free)(int, int); |
| 165 | int (*free_space)(int, struct pmem_freespace *); |
| 166 | unsigned long (*len)(int, struct pmem_data *); |
| 167 | unsigned long (*start_addr)(int, struct pmem_data *); |
| 168 | |
| 169 | /* actual size of memory element, e.g.: (4 << 10) is 4K */ |
| 170 | unsigned int quantum; |
| 171 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 172 | /* indicates maps of this region should be cached, if a mix of |
| 173 | * cached and uncached is desired, set this and open the device with |
| 174 | * O_SYNC to get an uncached region */ |
| 175 | unsigned cached; |
| 176 | unsigned buffered; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | union { |
| 178 | struct { |
| 179 | /* in all_or_nothing allocator mode the first mapper |
| 180 | * gets the whole space and sets this flag */ |
| 181 | unsigned allocated; |
| 182 | } all_or_nothing; |
| 183 | |
| 184 | struct { |
| 185 | /* the buddy allocator bitmap for the region |
| 186 | * indicating which entries are allocated and which |
| 187 | * are free. |
| 188 | */ |
| 189 | |
| 190 | struct pmem_bits *buddy_bitmap; |
| 191 | } buddy_bestfit; |
| 192 | |
| 193 | struct { |
| 194 | unsigned int bitmap_free; /* # of zero bits/quanta */ |
| 195 | uint32_t *bitmap; |
| 196 | int32_t bitmap_allocs; |
| 197 | struct { |
| 198 | short bit; |
| 199 | unsigned short quanta; |
| 200 | } *bitm_alloc; |
| 201 | } bitmap; |
| 202 | |
| 203 | struct { |
| 204 | unsigned long used; /* Bytes currently allocated */ |
| 205 | struct list_head alist; /* List of allocations */ |
| 206 | } system_mem; |
| 207 | } allocator; |
| 208 | |
| 209 | int id; |
| 210 | struct kobject kobj; |
| 211 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 212 | /* for debugging, creates a list of pmem file structs, the |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | * data_list_mutex should be taken before pmem_data->sem if both are |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 214 | * needed */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 215 | struct mutex data_list_mutex; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 216 | struct list_head data_list; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 217 | /* arena_mutex protects the global allocation arena |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 218 | * |
| 219 | * IF YOU TAKE BOTH LOCKS TAKE THEM IN THIS ORDER: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | * down(pmem_data->sem) => mutex_lock(arena_mutex) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 221 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | struct mutex arena_mutex; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 223 | |
| 224 | long (*ioctl)(struct file *, unsigned int, unsigned long); |
| 225 | int (*release)(struct inode *, struct file *); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 226 | /* reference count of allocations */ |
| 227 | atomic_t allocation_cnt; |
| 228 | /* |
| 229 | * request function for a region when the allocation count goes |
| 230 | * from 0 -> 1 |
| 231 | */ |
| 232 | void (*mem_request)(void *); |
| 233 | /* |
| 234 | * release function for a region when the allocation count goes |
| 235 | * from 1 -> 0 |
| 236 | */ |
| 237 | void (*mem_release)(void *); |
| 238 | /* |
| 239 | * private data for the request/release callback |
| 240 | */ |
| 241 | void *region_data; |
| 242 | /* |
| 243 | * map and unmap as needed |
| 244 | */ |
| 245 | int map_on_demand; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 246 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 247 | #define to_pmem_info_id(a) (container_of(a, struct pmem_info, kobj)->id) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 248 | |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 249 | static void ioremap_pmem(int id); |
| 250 | static void pmem_put_region(int id); |
| 251 | static int pmem_get_region(int id); |
| 252 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 253 | static struct pmem_info pmem[PMEM_MAX_DEVICES]; |
| 254 | static int id_count; |
| 255 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | #define PMEM_SYSFS_DIR_NAME "pmem_regions" /* under /sys/kernel/ */ |
| 257 | static struct kset *pmem_kset; |
| 258 | |
| 259 | #define PMEM_IS_FREE_BUDDY(id, index) \ |
| 260 | (!(pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].allocated)) |
| 261 | #define PMEM_BUDDY_ORDER(id, index) \ |
| 262 | (pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].order) |
| 263 | #define PMEM_BUDDY_INDEX(id, index) \ |
| 264 | (index ^ (1 << PMEM_BUDDY_ORDER(id, index))) |
| 265 | #define PMEM_BUDDY_NEXT_INDEX(id, index) \ |
| 266 | (index + (1 << PMEM_BUDDY_ORDER(id, index))) |
| 267 | #define PMEM_OFFSET(index) (index * pmem[id].quantum) |
| 268 | #define PMEM_START_ADDR(id, index) \ |
| 269 | (PMEM_OFFSET(index) + pmem[id].base) |
| 270 | #define PMEM_BUDDY_LEN(id, index) \ |
| 271 | ((1 << PMEM_BUDDY_ORDER(id, index)) * pmem[id].quantum) |
| 272 | #define PMEM_END_ADDR(id, index) \ |
| 273 | (PMEM_START_ADDR(id, index) + PMEM_LEN(id, index)) |
| 274 | #define PMEM_START_VADDR(id, index) \ |
| 275 | (PMEM_OFFSET(id, index) + pmem[id].vbase) |
| 276 | #define PMEM_END_VADDR(id, index) \ |
| 277 | (PMEM_START_VADDR(id, index) + PMEM_LEN(id, index)) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 278 | #define PMEM_REVOKED(data) (data->flags & PMEM_FLAGS_REVOKED) |
| 279 | #define PMEM_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK))) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | #define PMEM_IS_SUBMAP(data) \ |
| 281 | ((data->flags & PMEM_FLAGS_SUBMAP) && \ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 282 | (!(data->flags & PMEM_FLAGS_UNSUBMAP))) |
| 283 | |
| 284 | static int pmem_release(struct inode *, struct file *); |
| 285 | static int pmem_mmap(struct file *, struct vm_area_struct *); |
| 286 | static int pmem_open(struct inode *, struct file *); |
| 287 | static long pmem_ioctl(struct file *, unsigned int, unsigned long); |
| 288 | |
| 289 | struct file_operations pmem_fops = { |
| 290 | .release = pmem_release, |
| 291 | .mmap = pmem_mmap, |
| 292 | .open = pmem_open, |
| 293 | .unlocked_ioctl = pmem_ioctl, |
| 294 | }; |
| 295 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 296 | #define PMEM_ATTR(_name, _mode, _show, _store) { \ |
| 297 | .attr = {.name = __stringify(_name), .mode = _mode }, \ |
| 298 | .show = _show, \ |
| 299 | .store = _store, \ |
| 300 | } |
| 301 | |
| 302 | struct pmem_attr { |
| 303 | struct attribute attr; |
| 304 | ssize_t(*show) (const int id, char * const); |
| 305 | ssize_t(*store) (const int id, const char * const, const size_t count); |
| 306 | }; |
| 307 | #define to_pmem_attr(a) container_of(a, struct pmem_attr, attr) |
| 308 | |
| 309 | #define RW_PMEM_ATTR(name) \ |
| 310 | static struct pmem_attr pmem_attr_## name = \ |
| 311 | PMEM_ATTR(name, S_IRUGO | S_IWUSR, show_pmem_## name, store_pmem_## name) |
| 312 | |
| 313 | #define RO_PMEM_ATTR(name) \ |
| 314 | static struct pmem_attr pmem_attr_## name = \ |
| 315 | PMEM_ATTR(name, S_IRUGO, show_pmem_## name, NULL) |
| 316 | |
| 317 | #define WO_PMEM_ATTR(name) \ |
| 318 | static struct pmem_attr pmem_attr_## name = \ |
| 319 | PMEM_ATTR(name, S_IWUSR, NULL, store_pmem_## name) |
| 320 | |
| 321 | static ssize_t show_pmem(struct kobject *kobj, |
| 322 | struct attribute *attr, |
| 323 | char *buf) |
| 324 | { |
| 325 | struct pmem_attr *a = to_pmem_attr(attr); |
| 326 | return a->show ? a->show(to_pmem_info_id(kobj), buf) : -EIO; |
| 327 | } |
| 328 | |
| 329 | static ssize_t store_pmem(struct kobject *kobj, struct attribute *attr, |
| 330 | const char *buf, size_t count) |
| 331 | { |
| 332 | struct pmem_attr *a = to_pmem_attr(attr); |
| 333 | return a->store ? a->store(to_pmem_info_id(kobj), buf, count) : -EIO; |
| 334 | } |
| 335 | |
| 336 | static struct sysfs_ops pmem_ops = { |
| 337 | .show = show_pmem, |
| 338 | .store = store_pmem, |
| 339 | }; |
| 340 | |
| 341 | static ssize_t show_pmem_base(int id, char *buf) |
| 342 | { |
| 343 | return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n", |
| 344 | pmem[id].base, pmem[id].base); |
| 345 | } |
| 346 | RO_PMEM_ATTR(base); |
| 347 | |
| 348 | static ssize_t show_pmem_size(int id, char *buf) |
| 349 | { |
| 350 | return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n", |
| 351 | pmem[id].size, pmem[id].size); |
| 352 | } |
| 353 | RO_PMEM_ATTR(size); |
| 354 | |
| 355 | static ssize_t show_pmem_allocator_type(int id, char *buf) |
| 356 | { |
| 357 | switch (pmem[id].allocator_type) { |
| 358 | case PMEM_ALLOCATORTYPE_ALLORNOTHING: |
| 359 | return scnprintf(buf, PAGE_SIZE, "%s\n", "All or Nothing"); |
| 360 | case PMEM_ALLOCATORTYPE_BUDDYBESTFIT: |
| 361 | return scnprintf(buf, PAGE_SIZE, "%s\n", "Buddy Bestfit"); |
| 362 | case PMEM_ALLOCATORTYPE_BITMAP: |
| 363 | return scnprintf(buf, PAGE_SIZE, "%s\n", "Bitmap"); |
| 364 | case PMEM_ALLOCATORTYPE_SYSTEM: |
| 365 | return scnprintf(buf, PAGE_SIZE, "%s\n", "System heap"); |
| 366 | default: |
| 367 | return scnprintf(buf, PAGE_SIZE, |
| 368 | "??? Invalid allocator type (%d) for this region! " |
| 369 | "Something isn't right.\n", |
| 370 | pmem[id].allocator_type); |
| 371 | } |
| 372 | } |
| 373 | RO_PMEM_ATTR(allocator_type); |
| 374 | |
| 375 | static ssize_t show_pmem_mapped_regions(int id, char *buf) |
| 376 | { |
| 377 | struct list_head *elt; |
| 378 | int ret; |
| 379 | |
| 380 | ret = scnprintf(buf, PAGE_SIZE, |
| 381 | "pid #: mapped regions (offset, len) (offset,len)...\n"); |
| 382 | |
| 383 | mutex_lock(&pmem[id].data_list_mutex); |
| 384 | list_for_each(elt, &pmem[id].data_list) { |
| 385 | struct pmem_data *data = |
| 386 | list_entry(elt, struct pmem_data, list); |
| 387 | struct list_head *elt2; |
| 388 | |
| 389 | down_read(&data->sem); |
| 390 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "pid %u:", |
| 391 | data->pid); |
| 392 | list_for_each(elt2, &data->region_list) { |
| 393 | struct pmem_region_node *region_node = list_entry(elt2, |
| 394 | struct pmem_region_node, |
| 395 | list); |
| 396 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, |
| 397 | "(%lx,%lx) ", |
| 398 | region_node->region.offset, |
| 399 | region_node->region.len); |
| 400 | } |
| 401 | up_read(&data->sem); |
| 402 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 403 | } |
| 404 | mutex_unlock(&pmem[id].data_list_mutex); |
| 405 | return ret; |
| 406 | } |
| 407 | RO_PMEM_ATTR(mapped_regions); |
| 408 | |
| 409 | #define PMEM_COMMON_SYSFS_ATTRS \ |
| 410 | &pmem_attr_base.attr, \ |
| 411 | &pmem_attr_size.attr, \ |
| 412 | &pmem_attr_allocator_type.attr, \ |
| 413 | &pmem_attr_mapped_regions.attr |
| 414 | |
| 415 | |
| 416 | static ssize_t show_pmem_allocated(int id, char *buf) |
| 417 | { |
| 418 | ssize_t ret; |
| 419 | |
| 420 | mutex_lock(&pmem[id].arena_mutex); |
| 421 | ret = scnprintf(buf, PAGE_SIZE, "%s\n", |
| 422 | pmem[id].allocator.all_or_nothing.allocated ? |
| 423 | "is allocated" : "is NOT allocated"); |
| 424 | mutex_unlock(&pmem[id].arena_mutex); |
| 425 | return ret; |
| 426 | } |
| 427 | RO_PMEM_ATTR(allocated); |
| 428 | |
| 429 | static struct attribute *pmem_allornothing_attrs[] = { |
| 430 | PMEM_COMMON_SYSFS_ATTRS, |
| 431 | |
| 432 | &pmem_attr_allocated.attr, |
| 433 | |
| 434 | NULL |
| 435 | }; |
| 436 | |
| 437 | static struct kobj_type pmem_allornothing_ktype = { |
| 438 | .sysfs_ops = &pmem_ops, |
| 439 | .default_attrs = pmem_allornothing_attrs, |
| 440 | }; |
| 441 | |
| 442 | static ssize_t show_pmem_total_entries(int id, char *buf) |
| 443 | { |
| 444 | return scnprintf(buf, PAGE_SIZE, "%lu\n", pmem[id].num_entries); |
| 445 | } |
| 446 | RO_PMEM_ATTR(total_entries); |
| 447 | |
| 448 | static ssize_t show_pmem_quantum_size(int id, char *buf) |
| 449 | { |
| 450 | return scnprintf(buf, PAGE_SIZE, "%u (%#x)\n", |
| 451 | pmem[id].quantum, pmem[id].quantum); |
| 452 | } |
| 453 | RO_PMEM_ATTR(quantum_size); |
| 454 | |
| 455 | static ssize_t show_pmem_buddy_bitmap_dump(int id, char *buf) |
| 456 | { |
| 457 | int ret, i; |
| 458 | |
| 459 | mutex_lock(&pmem[id].data_list_mutex); |
| 460 | ret = scnprintf(buf, PAGE_SIZE, "index\torder\tlength\tallocated\n"); |
| 461 | |
| 462 | for (i = 0; i < pmem[id].num_entries && (PAGE_SIZE - ret); |
| 463 | i = PMEM_BUDDY_NEXT_INDEX(id, i)) |
| 464 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%d\t%d\t%d\t%d\n", |
| 465 | i, PMEM_BUDDY_ORDER(id, i), |
| 466 | PMEM_BUDDY_LEN(id, i), |
| 467 | !PMEM_IS_FREE_BUDDY(id, i)); |
| 468 | |
| 469 | mutex_unlock(&pmem[id].data_list_mutex); |
| 470 | return ret; |
| 471 | } |
| 472 | RO_PMEM_ATTR(buddy_bitmap_dump); |
| 473 | |
| 474 | #define PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS \ |
| 475 | &pmem_attr_quantum_size.attr, \ |
| 476 | &pmem_attr_total_entries.attr |
| 477 | |
| 478 | static struct attribute *pmem_buddy_bestfit_attrs[] = { |
| 479 | PMEM_COMMON_SYSFS_ATTRS, |
| 480 | |
| 481 | PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS, |
| 482 | |
| 483 | &pmem_attr_buddy_bitmap_dump.attr, |
| 484 | |
| 485 | NULL |
| 486 | }; |
| 487 | |
| 488 | static struct kobj_type pmem_buddy_bestfit_ktype = { |
| 489 | .sysfs_ops = &pmem_ops, |
| 490 | .default_attrs = pmem_buddy_bestfit_attrs, |
| 491 | }; |
| 492 | |
| 493 | static ssize_t show_pmem_free_quanta(int id, char *buf) |
| 494 | { |
| 495 | ssize_t ret; |
| 496 | |
| 497 | mutex_lock(&pmem[id].arena_mutex); |
| 498 | ret = scnprintf(buf, PAGE_SIZE, "%u\n", |
| 499 | pmem[id].allocator.bitmap.bitmap_free); |
| 500 | mutex_unlock(&pmem[id].arena_mutex); |
| 501 | return ret; |
| 502 | } |
| 503 | RO_PMEM_ATTR(free_quanta); |
| 504 | |
| 505 | static ssize_t show_pmem_bits_allocated(int id, char *buf) |
| 506 | { |
| 507 | ssize_t ret; |
| 508 | unsigned int i; |
| 509 | |
| 510 | mutex_lock(&pmem[id].arena_mutex); |
| 511 | |
| 512 | ret = scnprintf(buf, PAGE_SIZE, |
| 513 | "id: %d\nbitnum\tindex\tquanta allocated\n", id); |
| 514 | |
| 515 | for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++) |
| 516 | if (pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1) |
| 517 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, |
| 518 | "%u\t%u\t%u\n", |
| 519 | i, |
| 520 | pmem[id].allocator.bitmap.bitm_alloc[i].bit, |
| 521 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta |
| 522 | ); |
| 523 | |
| 524 | mutex_unlock(&pmem[id].arena_mutex); |
| 525 | return ret; |
| 526 | } |
| 527 | RO_PMEM_ATTR(bits_allocated); |
| 528 | |
| 529 | static struct attribute *pmem_bitmap_attrs[] = { |
| 530 | PMEM_COMMON_SYSFS_ATTRS, |
| 531 | |
| 532 | PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS, |
| 533 | |
| 534 | &pmem_attr_free_quanta.attr, |
| 535 | &pmem_attr_bits_allocated.attr, |
| 536 | |
| 537 | NULL |
| 538 | }; |
| 539 | |
| 540 | static struct attribute *pmem_system_attrs[] = { |
| 541 | PMEM_COMMON_SYSFS_ATTRS, |
| 542 | |
| 543 | NULL |
| 544 | }; |
| 545 | |
| 546 | static struct kobj_type pmem_bitmap_ktype = { |
| 547 | .sysfs_ops = &pmem_ops, |
| 548 | .default_attrs = pmem_bitmap_attrs, |
| 549 | }; |
| 550 | |
| 551 | static struct kobj_type pmem_system_ktype = { |
| 552 | .sysfs_ops = &pmem_ops, |
| 553 | .default_attrs = pmem_system_attrs, |
| 554 | }; |
| 555 | |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 556 | static int pmem_allocate_from_id(const int id, const unsigned long size, |
| 557 | const unsigned int align) |
| 558 | { |
| 559 | int ret; |
| 560 | ret = pmem_get_region(id); |
| 561 | |
| 562 | if (ret) |
| 563 | return -1; |
| 564 | |
| 565 | ret = pmem[id].allocate(id, size, align); |
| 566 | |
| 567 | if (ret < 0) |
| 568 | pmem_put_region(id); |
| 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | static int pmem_free_from_id(const int id, const int index) |
| 574 | { |
| 575 | pmem_put_region(id); |
| 576 | return pmem[id].free(id, index); |
| 577 | } |
| 578 | |
| 579 | static int pmem_get_region(int id) |
| 580 | { |
| 581 | /* Must be called with arena mutex locked */ |
| 582 | atomic_inc(&pmem[id].allocation_cnt); |
| 583 | if (!pmem[id].vbase) { |
| 584 | DLOG("PMEMDEBUG: mapping for %s", pmem[id].name); |
| 585 | if (pmem[id].mem_request) |
| 586 | pmem[id].mem_request(pmem[id].region_data); |
| 587 | ioremap_pmem(id); |
| 588 | } |
| 589 | |
| 590 | if (pmem[id].vbase) { |
| 591 | return 0; |
| 592 | } else { |
| 593 | if (pmem[id].mem_release) |
| 594 | pmem[id].mem_release(pmem[id].region_data); |
| 595 | atomic_dec(&pmem[id].allocation_cnt); |
| 596 | return 1; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | static void pmem_put_region(int id) |
| 601 | { |
| 602 | /* Must be called with arena mutex locked */ |
| 603 | if (atomic_dec_and_test(&pmem[id].allocation_cnt)) { |
| 604 | DLOG("PMEMDEBUG: unmapping for %s", pmem[id].name); |
| 605 | BUG_ON(!pmem[id].vbase); |
| 606 | if (pmem[id].map_on_demand) { |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 607 | /* unmap_kernel_range() flushes the caches |
| 608 | * and removes the page table entries |
| 609 | */ |
| 610 | unmap_kernel_range((unsigned long)pmem[id].vbase, |
| 611 | pmem[id].size); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 612 | pmem[id].vbase = NULL; |
| 613 | if (pmem[id].mem_release) |
| 614 | pmem[id].mem_release(pmem[id].region_data); |
| 615 | |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 620 | static int get_id(struct file *file) |
| 621 | { |
| 622 | return MINOR(file->f_dentry->d_inode->i_rdev); |
| 623 | } |
| 624 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | static char *get_name(struct file *file) |
| 626 | { |
| 627 | int id = get_id(file); |
| 628 | return pmem[id].name; |
| 629 | } |
| 630 | |
| 631 | static int is_pmem_file(struct file *file) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 632 | { |
| 633 | int id; |
| 634 | |
| 635 | if (unlikely(!file || !file->f_dentry || !file->f_dentry->d_inode)) |
| 636 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 638 | id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 639 | return (unlikely(id >= PMEM_MAX_DEVICES || |
| 640 | file->f_dentry->d_inode->i_rdev != |
| 641 | MKDEV(MISC_MAJOR, pmem[id].dev.minor))) ? 0 : 1; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static int has_allocation(struct file *file) |
| 645 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 646 | /* must be called with at least read lock held on |
| 647 | * ((struct pmem_data *)(file->private_data))->sem which |
| 648 | * means that file is guaranteed not to be NULL upon entry!! |
| 649 | * check is_pmem_file first if not accessed via pmem_file_ops */ |
| 650 | struct pmem_data *pdata = file->private_data; |
| 651 | return pdata && pdata->index != -1; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int is_master_owner(struct file *file) |
| 655 | { |
| 656 | struct file *master_file; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 657 | struct pmem_data *data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 658 | int put_needed, ret = 0; |
| 659 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 660 | if (!has_allocation(file)) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 661 | return 0; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 662 | if (PMEM_FLAGS_MASTERMAP & data->flags) |
| 663 | return 1; |
| 664 | master_file = fget_light(data->master_fd, &put_needed); |
| 665 | if (master_file && data->master_file == master_file) |
| 666 | ret = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 667 | if (master_file) |
| 668 | fput_light(master_file, put_needed); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 669 | return ret; |
| 670 | } |
| 671 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 672 | static int pmem_free_all_or_nothing(int id, int index) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 673 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 674 | /* caller should hold the lock on arena_mutex! */ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 675 | DLOG("index %d\n", index); |
| 676 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 677 | pmem[id].allocator.all_or_nothing.allocated = 0; |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static int pmem_free_space_all_or_nothing(int id, |
| 682 | struct pmem_freespace *fs) |
| 683 | { |
| 684 | /* caller should hold the lock on arena_mutex! */ |
| 685 | fs->total = (unsigned long) |
| 686 | pmem[id].allocator.all_or_nothing.allocated == 0 ? |
| 687 | pmem[id].size : 0; |
| 688 | |
| 689 | fs->largest = fs->total; |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | static int pmem_free_buddy_bestfit(int id, int index) |
| 695 | { |
| 696 | /* caller should hold the lock on arena_mutex! */ |
| 697 | int curr = index; |
| 698 | DLOG("index %d\n", index); |
| 699 | |
| 700 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 701 | /* clean up the bitmap, merging any buddies */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 702 | pmem[id].allocator.buddy_bestfit.buddy_bitmap[curr].allocated = 0; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 703 | /* find a slots buddy Buddy# = Slot# ^ (1 << order) |
| 704 | * if the buddy is also free merge them |
| 705 | * repeat until the buddy is not free or end of the bitmap is reached |
| 706 | */ |
| 707 | do { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 708 | int buddy = PMEM_BUDDY_INDEX(id, curr); |
| 709 | if (buddy < pmem[id].num_entries && |
| 710 | PMEM_IS_FREE_BUDDY(id, buddy) && |
| 711 | PMEM_BUDDY_ORDER(id, buddy) == |
| 712 | PMEM_BUDDY_ORDER(id, curr)) { |
| 713 | PMEM_BUDDY_ORDER(id, buddy)++; |
| 714 | PMEM_BUDDY_ORDER(id, curr)++; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 715 | curr = min(buddy, curr); |
| 716 | } else { |
| 717 | break; |
| 718 | } |
| 719 | } while (curr < pmem[id].num_entries); |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 724 | |
| 725 | static int pmem_free_space_buddy_bestfit(int id, |
| 726 | struct pmem_freespace *fs) |
| 727 | { |
| 728 | /* caller should hold the lock on arena_mutex! */ |
| 729 | int curr; |
| 730 | unsigned long size; |
| 731 | fs->total = 0; |
| 732 | fs->largest = 0; |
| 733 | |
| 734 | for (curr = 0; curr < pmem[id].num_entries; |
| 735 | curr = PMEM_BUDDY_NEXT_INDEX(id, curr)) { |
| 736 | if (PMEM_IS_FREE_BUDDY(id, curr)) { |
| 737 | size = PMEM_BUDDY_LEN(id, curr); |
| 738 | if (size > fs->largest) |
| 739 | fs->largest = size; |
| 740 | fs->total += size; |
| 741 | } |
| 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | static inline uint32_t start_mask(int bit_start) |
| 748 | { |
| 749 | return (uint32_t)(~0) << (bit_start & PMEM_BITS_PER_WORD_MASK); |
| 750 | } |
| 751 | |
| 752 | static inline uint32_t end_mask(int bit_end) |
| 753 | { |
| 754 | return (uint32_t)(~0) >> |
| 755 | ((BITS_PER_LONG - bit_end) & PMEM_BITS_PER_WORD_MASK); |
| 756 | } |
| 757 | |
| 758 | static inline int compute_total_words(int bit_end, int word_index) |
| 759 | { |
| 760 | return ((bit_end + BITS_PER_LONG - 1) >> |
| 761 | PMEM_32BIT_WORD_ORDER) - word_index; |
| 762 | } |
| 763 | |
| 764 | static void bitmap_bits_clear_all(uint32_t *bitp, int bit_start, int bit_end) |
| 765 | { |
| 766 | int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words; |
| 767 | |
| 768 | total_words = compute_total_words(bit_end, word_index); |
| 769 | if (total_words > 0) { |
| 770 | if (total_words == 1) { |
| 771 | bitp[word_index] &= |
| 772 | ~(start_mask(bit_start) & end_mask(bit_end)); |
| 773 | } else { |
| 774 | bitp[word_index++] &= ~start_mask(bit_start); |
| 775 | if (total_words > 2) { |
| 776 | int total_bytes; |
| 777 | |
| 778 | total_words -= 2; |
| 779 | total_bytes = total_words << 2; |
| 780 | |
| 781 | memset(&bitp[word_index], 0, total_bytes); |
| 782 | word_index += total_words; |
| 783 | } |
| 784 | bitp[word_index] &= ~end_mask(bit_end); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | static int pmem_free_bitmap(int id, int bitnum) |
| 790 | { |
| 791 | /* caller should hold the lock on arena_mutex! */ |
| 792 | int i; |
| 793 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 794 | |
| 795 | DLOG("bitnum %d\n", bitnum); |
| 796 | |
| 797 | for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++) { |
| 798 | const int curr_bit = |
| 799 | pmem[id].allocator.bitmap.bitm_alloc[i].bit; |
| 800 | |
| 801 | if (curr_bit == bitnum) { |
| 802 | const int curr_quanta = |
| 803 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta; |
| 804 | |
| 805 | bitmap_bits_clear_all(pmem[id].allocator.bitmap.bitmap, |
| 806 | curr_bit, curr_bit + curr_quanta); |
| 807 | pmem[id].allocator.bitmap.bitmap_free += curr_quanta; |
| 808 | pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1; |
| 809 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0; |
| 810 | return 0; |
| 811 | } |
| 812 | } |
| 813 | printk(KERN_ALERT "pmem: %s: Attempt to free unallocated index %d, id" |
| 814 | " %d, pid %d(%s)\n", __func__, bitnum, id, current->pid, |
| 815 | get_task_comm(currtask_name, current)); |
| 816 | |
| 817 | return -1; |
| 818 | } |
| 819 | |
| 820 | static int pmem_free_system(int id, int index) |
| 821 | { |
| 822 | /* caller should hold the lock on arena_mutex! */ |
| 823 | struct alloc_list *item; |
| 824 | |
| 825 | DLOG("index %d\n", index); |
| 826 | if (index != 0) |
| 827 | item = (struct alloc_list *)index; |
| 828 | else |
| 829 | return 0; |
| 830 | |
| 831 | if (item->vaddr != NULL) { |
| 832 | iounmap(item->vaddr); |
| 833 | kfree(__va(item->addr)); |
| 834 | list_del(&item->allocs); |
| 835 | kfree(item); |
| 836 | } |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int pmem_free_space_bitmap(int id, struct pmem_freespace *fs) |
| 842 | { |
| 843 | int i, j; |
| 844 | int max_allocs = pmem[id].allocator.bitmap.bitmap_allocs; |
| 845 | int alloc_start = 0; |
| 846 | int next_alloc; |
| 847 | unsigned long size = 0; |
| 848 | |
| 849 | fs->total = 0; |
| 850 | fs->largest = 0; |
| 851 | |
| 852 | for (i = 0; i < max_allocs; i++) { |
| 853 | |
| 854 | int alloc_quanta = 0; |
| 855 | int alloc_idx = 0; |
| 856 | next_alloc = pmem[id].num_entries; |
| 857 | |
| 858 | /* Look for the lowest bit where next allocation starts */ |
| 859 | for (j = 0; j < max_allocs; j++) { |
| 860 | const int curr_alloc = pmem[id].allocator. |
| 861 | bitmap.bitm_alloc[j].bit; |
| 862 | if (curr_alloc != -1) { |
| 863 | if (alloc_start == curr_alloc) |
| 864 | alloc_idx = j; |
| 865 | if (alloc_start >= curr_alloc) |
| 866 | continue; |
| 867 | if (curr_alloc < next_alloc) |
| 868 | next_alloc = curr_alloc; |
| 869 | } |
| 870 | } |
| 871 | alloc_quanta = pmem[id].allocator.bitmap. |
| 872 | bitm_alloc[alloc_idx].quanta; |
| 873 | size = (next_alloc - (alloc_start + alloc_quanta)) * |
| 874 | pmem[id].quantum; |
| 875 | |
| 876 | if (size > fs->largest) |
| 877 | fs->largest = size; |
| 878 | fs->total += size; |
| 879 | |
| 880 | if (next_alloc == pmem[id].num_entries) |
| 881 | break; |
| 882 | else |
| 883 | alloc_start = next_alloc; |
| 884 | } |
| 885 | |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | static int pmem_free_space_system(int id, struct pmem_freespace *fs) |
| 890 | { |
| 891 | fs->total = pmem[id].size; |
| 892 | fs->largest = pmem[id].size; |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 897 | static void pmem_revoke(struct file *file, struct pmem_data *data); |
| 898 | |
| 899 | static int pmem_release(struct inode *inode, struct file *file) |
| 900 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 901 | struct pmem_data *data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 902 | struct pmem_region_node *region_node; |
| 903 | struct list_head *elt, *elt2; |
| 904 | int id = get_id(file), ret = 0; |
| 905 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 906 | #if PMEM_DEBUG_MSGS |
| 907 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 908 | #endif |
| 909 | DLOG("releasing memory pid %u(%s) file %p(%ld) dev %s(id: %d)\n", |
| 910 | current->pid, get_task_comm(currtask_name, current), |
| 911 | file, file_count(file), get_name(file), id); |
| 912 | mutex_lock(&pmem[id].data_list_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 913 | /* if this file is a master, revoke all the memory in the connected |
| 914 | * files */ |
| 915 | if (PMEM_FLAGS_MASTERMAP & data->flags) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 916 | list_for_each(elt, &pmem[id].data_list) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 917 | struct pmem_data *sub_data = |
| 918 | list_entry(elt, struct pmem_data, list); |
| 919 | int is_master; |
| 920 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 921 | down_read(&sub_data->sem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 922 | is_master = (PMEM_IS_SUBMAP(sub_data) && |
| 923 | file == sub_data->master_file); |
| 924 | up_read(&sub_data->sem); |
| 925 | |
| 926 | if (is_master) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 927 | pmem_revoke(file, sub_data); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | list_del(&data->list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 931 | mutex_unlock(&pmem[id].data_list_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 932 | |
| 933 | down_write(&data->sem); |
| 934 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 935 | /* if it is not a connected file and it has an allocation, free it */ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 936 | if (!(PMEM_FLAGS_CONNECTED & data->flags) && has_allocation(file)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 937 | mutex_lock(&pmem[id].arena_mutex); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 938 | ret = pmem_free_from_id(id, data->index); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | mutex_unlock(&pmem[id].arena_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* if this file is a submap (mapped, connected file), downref the |
| 943 | * task struct */ |
| 944 | if (PMEM_FLAGS_SUBMAP & data->flags) |
| 945 | if (data->task) { |
| 946 | put_task_struct(data->task); |
| 947 | data->task = NULL; |
| 948 | } |
| 949 | |
| 950 | file->private_data = NULL; |
| 951 | |
| 952 | list_for_each_safe(elt, elt2, &data->region_list) { |
| 953 | region_node = list_entry(elt, struct pmem_region_node, list); |
| 954 | list_del(elt); |
| 955 | kfree(region_node); |
| 956 | } |
| 957 | BUG_ON(!list_empty(&data->region_list)); |
| 958 | |
| 959 | up_write(&data->sem); |
| 960 | kfree(data); |
| 961 | if (pmem[id].release) |
| 962 | ret = pmem[id].release(inode, file); |
| 963 | |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | static int pmem_open(struct inode *inode, struct file *file) |
| 968 | { |
| 969 | struct pmem_data *data; |
| 970 | int id = get_id(file); |
| 971 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 972 | #if PMEM_DEBUG_MSGS |
| 973 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 974 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 975 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 976 | DLOG("pid %u(%s) file %p(%ld) dev %s(id: %d)\n", |
| 977 | current->pid, get_task_comm(currtask_name, current), |
| 978 | file, file_count(file), get_name(file), id); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 979 | data = kmalloc(sizeof(struct pmem_data), GFP_KERNEL); |
| 980 | if (!data) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 981 | printk(KERN_ALERT "pmem: %s: unable to allocate memory for " |
| 982 | "pmem metadata.", __func__); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 983 | return -1; |
| 984 | } |
| 985 | data->flags = 0; |
| 986 | data->index = -1; |
| 987 | data->task = NULL; |
| 988 | data->vma = NULL; |
| 989 | data->pid = 0; |
| 990 | data->master_file = NULL; |
| 991 | #if PMEM_DEBUG |
| 992 | data->ref = 0; |
| 993 | #endif |
| 994 | INIT_LIST_HEAD(&data->region_list); |
| 995 | init_rwsem(&data->sem); |
| 996 | |
| 997 | file->private_data = data; |
| 998 | INIT_LIST_HEAD(&data->list); |
| 999 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1000 | mutex_lock(&pmem[id].data_list_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1001 | list_add(&data->list, &pmem[id].data_list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1002 | mutex_unlock(&pmem[id].data_list_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1003 | return ret; |
| 1004 | } |
| 1005 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1006 | static unsigned long pmem_order(unsigned long len, int id) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1007 | { |
| 1008 | int i; |
| 1009 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1010 | len = (len + pmem[id].quantum - 1)/pmem[id].quantum; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1011 | len--; |
| 1012 | for (i = 0; i < sizeof(len)*8; i++) |
| 1013 | if (len >> i == 0) |
| 1014 | break; |
| 1015 | return i; |
| 1016 | } |
| 1017 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1018 | static int pmem_allocator_all_or_nothing(const int id, |
| 1019 | const unsigned long len, |
| 1020 | const unsigned int align) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1021 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1022 | /* caller should hold the lock on arena_mutex! */ |
| 1023 | DLOG("all or nothing\n"); |
| 1024 | if ((len > pmem[id].size) || |
| 1025 | pmem[id].allocator.all_or_nothing.allocated) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1026 | return -1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1027 | pmem[id].allocator.all_or_nothing.allocated = 1; |
| 1028 | return len; |
| 1029 | } |
| 1030 | |
| 1031 | static int pmem_allocator_buddy_bestfit(const int id, |
| 1032 | const unsigned long len, |
| 1033 | unsigned int align) |
| 1034 | { |
| 1035 | /* caller should hold the lock on arena_mutex! */ |
| 1036 | int curr; |
| 1037 | int best_fit = -1; |
| 1038 | unsigned long order; |
| 1039 | |
| 1040 | DLOG("buddy bestfit\n"); |
| 1041 | order = pmem_order(len, id); |
| 1042 | if (order > PMEM_MAX_ORDER) |
| 1043 | goto out; |
| 1044 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1045 | DLOG("order %lx\n", order); |
| 1046 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1047 | /* Look through the bitmap. |
| 1048 | * If a free slot of the correct order is found, use it. |
| 1049 | * Otherwise, use the best fit (smallest with size > order) slot. |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1050 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1051 | for (curr = 0; |
| 1052 | curr < pmem[id].num_entries; |
| 1053 | curr = PMEM_BUDDY_NEXT_INDEX(id, curr)) |
| 1054 | if (PMEM_IS_FREE_BUDDY(id, curr)) { |
| 1055 | if (PMEM_BUDDY_ORDER(id, curr) == |
| 1056 | (unsigned char)order) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1057 | /* set the not free bit and clear others */ |
| 1058 | best_fit = curr; |
| 1059 | break; |
| 1060 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | if (PMEM_BUDDY_ORDER(id, curr) > |
| 1062 | (unsigned char)order && |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1063 | (best_fit < 0 || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1064 | PMEM_BUDDY_ORDER(id, curr) < |
| 1065 | PMEM_BUDDY_ORDER(id, best_fit))) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1066 | best_fit = curr; |
| 1067 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1068 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1069 | /* if best_fit < 0, there are no suitable slots; return an error */ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1070 | if (best_fit < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1071 | #if PMEM_DEBUG |
| 1072 | printk(KERN_ALERT "pmem: %s: no space left to allocate!\n", |
| 1073 | __func__); |
| 1074 | #endif |
| 1075 | goto out; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* now partition the best fit: |
| 1079 | * split the slot into 2 buddies of order - 1 |
| 1080 | * repeat until the slot is of the correct order |
| 1081 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1082 | while (PMEM_BUDDY_ORDER(id, best_fit) > (unsigned char)order) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1083 | int buddy; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | PMEM_BUDDY_ORDER(id, best_fit) -= 1; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1085 | buddy = PMEM_BUDDY_INDEX(id, best_fit); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1086 | PMEM_BUDDY_ORDER(id, buddy) = PMEM_BUDDY_ORDER(id, best_fit); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1087 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1088 | pmem[id].allocator.buddy_bestfit.buddy_bitmap[best_fit].allocated = 1; |
| 1089 | out: |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1090 | return best_fit; |
| 1091 | } |
| 1092 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1093 | |
| 1094 | static inline unsigned long paddr_from_bit(const int id, const int bitnum) |
| 1095 | { |
| 1096 | return pmem[id].base + pmem[id].quantum * bitnum; |
| 1097 | } |
| 1098 | |
| 1099 | static inline unsigned long bit_from_paddr(const int id, |
| 1100 | const unsigned long paddr) |
| 1101 | { |
| 1102 | return (paddr - pmem[id].base) / pmem[id].quantum; |
| 1103 | } |
| 1104 | |
| 1105 | static void bitmap_bits_set_all(uint32_t *bitp, int bit_start, int bit_end) |
| 1106 | { |
| 1107 | int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words; |
| 1108 | |
| 1109 | total_words = compute_total_words(bit_end, word_index); |
| 1110 | if (total_words > 0) { |
| 1111 | if (total_words == 1) { |
| 1112 | bitp[word_index] |= |
| 1113 | (start_mask(bit_start) & end_mask(bit_end)); |
| 1114 | } else { |
| 1115 | bitp[word_index++] |= start_mask(bit_start); |
| 1116 | if (total_words > 2) { |
| 1117 | int total_bytes; |
| 1118 | |
| 1119 | total_words -= 2; |
| 1120 | total_bytes = total_words << 2; |
| 1121 | |
| 1122 | memset(&bitp[word_index], ~0, total_bytes); |
| 1123 | word_index += total_words; |
| 1124 | } |
| 1125 | bitp[word_index] |= end_mask(bit_end); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static int |
| 1131 | bitmap_allocate_contiguous(uint32_t *bitp, int num_bits_to_alloc, |
Laura Abbott | 6b3eb1a | 2011-06-12 13:29:08 -0700 | [diff] [blame] | 1132 | int total_bits, int spacing, int start_bit) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1133 | { |
| 1134 | int bit_start, last_bit, word_index; |
| 1135 | |
| 1136 | if (num_bits_to_alloc <= 0) |
| 1137 | return -1; |
| 1138 | |
Laura Abbott | 6b3eb1a | 2011-06-12 13:29:08 -0700 | [diff] [blame] | 1139 | for (bit_start = start_bit; ; |
| 1140 | bit_start = ((last_bit + |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1141 | (word_index << PMEM_32BIT_WORD_ORDER) + spacing - 1) |
Laura Abbott | 6b3eb1a | 2011-06-12 13:29:08 -0700 | [diff] [blame] | 1142 | & ~(spacing - 1)) + start_bit) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1143 | int bit_end = bit_start + num_bits_to_alloc, total_words; |
| 1144 | |
| 1145 | if (bit_end > total_bits) |
| 1146 | return -1; /* out of contiguous memory */ |
| 1147 | |
| 1148 | word_index = bit_start >> PMEM_32BIT_WORD_ORDER; |
| 1149 | total_words = compute_total_words(bit_end, word_index); |
| 1150 | |
| 1151 | if (total_words <= 0) |
| 1152 | return -1; |
| 1153 | |
| 1154 | if (total_words == 1) { |
| 1155 | last_bit = fls(bitp[word_index] & |
| 1156 | (start_mask(bit_start) & |
| 1157 | end_mask(bit_end))); |
| 1158 | if (last_bit) |
| 1159 | continue; |
| 1160 | } else { |
| 1161 | int end_word = word_index + (total_words - 1); |
| 1162 | last_bit = |
| 1163 | fls(bitp[word_index] & start_mask(bit_start)); |
| 1164 | if (last_bit) |
| 1165 | continue; |
| 1166 | |
| 1167 | for (word_index++; |
| 1168 | word_index < end_word; |
| 1169 | word_index++) { |
| 1170 | last_bit = fls(bitp[word_index]); |
| 1171 | if (last_bit) |
| 1172 | break; |
| 1173 | } |
| 1174 | if (last_bit) |
| 1175 | continue; |
| 1176 | |
| 1177 | last_bit = fls(bitp[word_index] & end_mask(bit_end)); |
| 1178 | if (last_bit) |
| 1179 | continue; |
| 1180 | } |
| 1181 | bitmap_bits_set_all(bitp, bit_start, bit_end); |
| 1182 | return bit_start; |
| 1183 | } |
| 1184 | return -1; |
| 1185 | } |
| 1186 | |
| 1187 | static int reserve_quanta(const unsigned int quanta_needed, |
| 1188 | const int id, |
| 1189 | unsigned int align) |
| 1190 | { |
| 1191 | /* alignment should be a valid power of 2 */ |
| 1192 | int ret = -1, start_bit = 0, spacing = 1; |
| 1193 | |
| 1194 | /* Sanity check */ |
| 1195 | if (quanta_needed > pmem[id].allocator.bitmap.bitmap_free) { |
| 1196 | #if PMEM_DEBUG |
| 1197 | printk(KERN_ALERT "pmem: %s: request (%d) too big for" |
| 1198 | " available free (%d)\n", __func__, quanta_needed, |
| 1199 | pmem[id].allocator.bitmap.bitmap_free); |
| 1200 | #endif |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
| 1204 | start_bit = bit_from_paddr(id, |
| 1205 | (pmem[id].base + align - 1) & ~(align - 1)); |
| 1206 | if (start_bit <= -1) { |
| 1207 | #if PMEM_DEBUG |
| 1208 | printk(KERN_ALERT |
| 1209 | "pmem: %s: bit_from_paddr fails for" |
| 1210 | " %u alignment.\n", __func__, align); |
| 1211 | #endif |
| 1212 | return -1; |
| 1213 | } |
| 1214 | spacing = align / pmem[id].quantum; |
| 1215 | spacing = spacing > 1 ? spacing : 1; |
| 1216 | |
| 1217 | ret = bitmap_allocate_contiguous(pmem[id].allocator.bitmap.bitmap, |
| 1218 | quanta_needed, |
| 1219 | (pmem[id].size + pmem[id].quantum - 1) / pmem[id].quantum, |
Laura Abbott | 6b3eb1a | 2011-06-12 13:29:08 -0700 | [diff] [blame] | 1220 | spacing, |
| 1221 | start_bit); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1222 | |
| 1223 | #if PMEM_DEBUG |
| 1224 | if (ret < 0) |
| 1225 | printk(KERN_ALERT "pmem: %s: not enough contiguous bits free " |
| 1226 | "in bitmap! Region memory is either too fragmented or" |
| 1227 | " request is too large for available memory.\n", |
| 1228 | __func__); |
| 1229 | #endif |
| 1230 | |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | static int pmem_allocator_bitmap(const int id, |
| 1235 | const unsigned long len, |
| 1236 | const unsigned int align) |
| 1237 | { |
| 1238 | /* caller should hold the lock on arena_mutex! */ |
| 1239 | int bitnum, i; |
| 1240 | unsigned int quanta_needed; |
| 1241 | |
| 1242 | DLOG("bitmap id %d, len %ld, align %u\n", id, len, align); |
| 1243 | if (!pmem[id].allocator.bitmap.bitm_alloc) { |
| 1244 | #if PMEM_DEBUG |
| 1245 | printk(KERN_ALERT "pmem: bitm_alloc not present! id: %d\n", |
| 1246 | id); |
| 1247 | #endif |
| 1248 | return -1; |
| 1249 | } |
| 1250 | |
| 1251 | quanta_needed = (len + pmem[id].quantum - 1) / pmem[id].quantum; |
| 1252 | DLOG("quantum size %u quanta needed %u free %u id %d\n", |
| 1253 | pmem[id].quantum, quanta_needed, |
| 1254 | pmem[id].allocator.bitmap.bitmap_free, id); |
| 1255 | |
| 1256 | if (pmem[id].allocator.bitmap.bitmap_free < quanta_needed) { |
| 1257 | #if PMEM_DEBUG |
| 1258 | printk(KERN_ALERT "pmem: memory allocation failure. " |
| 1259 | "PMEM memory region exhausted, id %d." |
| 1260 | " Unable to comply with allocation request.\n", id); |
| 1261 | #endif |
| 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | bitnum = reserve_quanta(quanta_needed, id, align); |
| 1266 | if (bitnum == -1) |
| 1267 | goto leave; |
| 1268 | |
| 1269 | for (i = 0; |
| 1270 | i < pmem[id].allocator.bitmap.bitmap_allocs && |
| 1271 | pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1; |
| 1272 | i++) |
| 1273 | ; |
| 1274 | |
| 1275 | if (i >= pmem[id].allocator.bitmap.bitmap_allocs) { |
| 1276 | void *temp; |
| 1277 | int32_t new_bitmap_allocs = |
| 1278 | pmem[id].allocator.bitmap.bitmap_allocs << 1; |
| 1279 | int j; |
| 1280 | |
| 1281 | if (!new_bitmap_allocs) { /* failed sanity check!! */ |
| 1282 | #if PMEM_DEBUG |
| 1283 | pr_alert("pmem: bitmap_allocs number" |
| 1284 | " wrapped around to zero! Something " |
| 1285 | "is VERY wrong.\n"); |
| 1286 | #endif |
| 1287 | return -1; |
| 1288 | } |
| 1289 | |
| 1290 | if (new_bitmap_allocs > pmem[id].num_entries) { |
| 1291 | /* failed sanity check!! */ |
| 1292 | #if PMEM_DEBUG |
| 1293 | pr_alert("pmem: required bitmap_allocs" |
| 1294 | " number exceeds maximum entries possible" |
| 1295 | " for current quanta\n"); |
| 1296 | #endif |
| 1297 | return -1; |
| 1298 | } |
| 1299 | |
| 1300 | temp = krealloc(pmem[id].allocator.bitmap.bitm_alloc, |
| 1301 | new_bitmap_allocs * |
| 1302 | sizeof(*pmem[id].allocator.bitmap.bitm_alloc), |
| 1303 | GFP_KERNEL); |
| 1304 | if (!temp) { |
| 1305 | #if PMEM_DEBUG |
| 1306 | pr_alert("pmem: can't realloc bitmap_allocs," |
| 1307 | "id %d, current num bitmap allocs %d\n", |
| 1308 | id, pmem[id].allocator.bitmap.bitmap_allocs); |
| 1309 | #endif |
| 1310 | return -1; |
| 1311 | } |
| 1312 | pmem[id].allocator.bitmap.bitmap_allocs = new_bitmap_allocs; |
| 1313 | pmem[id].allocator.bitmap.bitm_alloc = temp; |
| 1314 | |
| 1315 | for (j = i; j < new_bitmap_allocs; j++) { |
| 1316 | pmem[id].allocator.bitmap.bitm_alloc[j].bit = -1; |
| 1317 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0; |
| 1318 | } |
| 1319 | |
| 1320 | DLOG("increased # of allocated regions to %d for id %d\n", |
| 1321 | pmem[id].allocator.bitmap.bitmap_allocs, id); |
| 1322 | } |
| 1323 | |
| 1324 | DLOG("bitnum %d, bitm_alloc index %d\n", bitnum, i); |
| 1325 | |
| 1326 | pmem[id].allocator.bitmap.bitmap_free -= quanta_needed; |
| 1327 | pmem[id].allocator.bitmap.bitm_alloc[i].bit = bitnum; |
| 1328 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta = quanta_needed; |
| 1329 | leave: |
| 1330 | return bitnum; |
| 1331 | } |
| 1332 | |
| 1333 | static int pmem_allocator_system(const int id, |
| 1334 | const unsigned long len, |
| 1335 | const unsigned int align) |
| 1336 | { |
| 1337 | /* caller should hold the lock on arena_mutex! */ |
| 1338 | struct alloc_list *list; |
| 1339 | unsigned long aligned_len; |
| 1340 | int count = SYSTEM_ALLOC_RETRY; |
| 1341 | void *buf; |
| 1342 | |
| 1343 | DLOG("system id %d, len %ld, align %u\n", id, len, align); |
| 1344 | |
| 1345 | if ((pmem[id].allocator.system_mem.used + len) > pmem[id].size) { |
| 1346 | DLOG("requested size would be larger than quota\n"); |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
| 1350 | /* Handle alignment */ |
| 1351 | aligned_len = len + align; |
| 1352 | |
| 1353 | /* Attempt allocation */ |
| 1354 | list = kmalloc(sizeof(struct alloc_list), GFP_KERNEL); |
| 1355 | if (list == NULL) { |
| 1356 | printk(KERN_ERR "pmem: failed to allocate system metadata\n"); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | list->vaddr = NULL; |
| 1360 | |
| 1361 | buf = NULL; |
| 1362 | while ((buf == NULL) && count--) { |
| 1363 | buf = kmalloc((aligned_len), GFP_KERNEL); |
| 1364 | if (buf == NULL) { |
| 1365 | DLOG("pmem: kmalloc %d temporarily failed len= %ld\n", |
| 1366 | count, aligned_len); |
| 1367 | } |
| 1368 | } |
| 1369 | if (!buf) { |
| 1370 | printk(KERN_CRIT "pmem: kmalloc failed for id= %d len= %ld\n", |
| 1371 | id, aligned_len); |
| 1372 | kfree(list); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | list->size = aligned_len; |
| 1376 | list->addr = (void *)__pa(buf); |
| 1377 | list->aaddr = (void *)(((unsigned int)(list->addr) + (align - 1)) & |
| 1378 | ~(align - 1)); |
| 1379 | |
| 1380 | if (!pmem[id].cached) |
| 1381 | list->vaddr = ioremap(__pa(buf), aligned_len); |
| 1382 | else |
| 1383 | list->vaddr = ioremap_cached(__pa(buf), aligned_len); |
| 1384 | |
| 1385 | INIT_LIST_HEAD(&list->allocs); |
| 1386 | list_add(&list->allocs, &pmem[id].allocator.system_mem.alist); |
| 1387 | |
| 1388 | return (int)list; |
| 1389 | } |
| 1390 | |
| 1391 | static pgprot_t pmem_phys_mem_access_prot(struct file *file, pgprot_t vma_prot) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1392 | { |
| 1393 | int id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1394 | #ifdef pgprot_writecombine |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1395 | if (pmem[id].cached == 0 || file->f_flags & O_SYNC) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1396 | /* on ARMv6 and ARMv7 this expands to Normal Noncached */ |
| 1397 | return pgprot_writecombine(vma_prot); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1398 | #endif |
| 1399 | #ifdef pgprot_ext_buffered |
| 1400 | else if (pmem[id].buffered) |
| 1401 | return pgprot_ext_buffered(vma_prot); |
| 1402 | #endif |
| 1403 | return vma_prot; |
| 1404 | } |
| 1405 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1406 | static unsigned long pmem_start_addr_all_or_nothing(int id, |
| 1407 | struct pmem_data *data) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1408 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1409 | return PMEM_START_ADDR(id, 0); |
| 1410 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1411 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1412 | static unsigned long pmem_start_addr_buddy_bestfit(int id, |
| 1413 | struct pmem_data *data) |
| 1414 | { |
| 1415 | return PMEM_START_ADDR(id, data->index); |
| 1416 | } |
| 1417 | |
| 1418 | static unsigned long pmem_start_addr_bitmap(int id, struct pmem_data *data) |
| 1419 | { |
| 1420 | return data->index * pmem[id].quantum + pmem[id].base; |
| 1421 | } |
| 1422 | |
| 1423 | static unsigned long pmem_start_addr_system(int id, struct pmem_data *data) |
| 1424 | { |
| 1425 | return (unsigned long)(((struct alloc_list *)(data->index))->aaddr); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static void *pmem_start_vaddr(int id, struct pmem_data *data) |
| 1429 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1430 | if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM) |
| 1431 | return ((struct alloc_list *)(data->index))->vaddr; |
| 1432 | else |
| 1433 | return pmem[id].start_addr(id, data) - pmem[id].base + pmem[id].vbase; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1436 | static unsigned long pmem_len_all_or_nothing(int id, struct pmem_data *data) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1437 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1438 | return data->index; |
| 1439 | } |
| 1440 | |
| 1441 | static unsigned long pmem_len_buddy_bestfit(int id, struct pmem_data *data) |
| 1442 | { |
| 1443 | return PMEM_BUDDY_LEN(id, data->index); |
| 1444 | } |
| 1445 | |
| 1446 | static unsigned long pmem_len_bitmap(int id, struct pmem_data *data) |
| 1447 | { |
| 1448 | int i; |
| 1449 | unsigned long ret = 0; |
| 1450 | |
| 1451 | mutex_lock(&pmem[id].arena_mutex); |
| 1452 | |
| 1453 | for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++) |
| 1454 | if (pmem[id].allocator.bitmap.bitm_alloc[i].bit == |
| 1455 | data->index) { |
| 1456 | ret = pmem[id].allocator.bitmap.bitm_alloc[i].quanta * |
| 1457 | pmem[id].quantum; |
| 1458 | break; |
| 1459 | } |
| 1460 | |
| 1461 | mutex_unlock(&pmem[id].arena_mutex); |
| 1462 | #if PMEM_DEBUG |
| 1463 | if (i >= pmem[id].allocator.bitmap.bitmap_allocs) |
| 1464 | pr_alert("pmem: %s: can't find bitnum %d in " |
| 1465 | "alloc'd array!\n", __func__, data->index); |
| 1466 | #endif |
| 1467 | return ret; |
| 1468 | } |
| 1469 | |
| 1470 | static unsigned long pmem_len_system(int id, struct pmem_data *data) |
| 1471 | { |
| 1472 | unsigned long ret = 0; |
| 1473 | |
| 1474 | mutex_lock(&pmem[id].arena_mutex); |
| 1475 | |
| 1476 | ret = ((struct alloc_list *)data->index)->size; |
| 1477 | mutex_unlock(&pmem[id].arena_mutex); |
| 1478 | |
| 1479 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | static int pmem_map_garbage(int id, struct vm_area_struct *vma, |
| 1483 | struct pmem_data *data, unsigned long offset, |
| 1484 | unsigned long len) |
| 1485 | { |
| 1486 | int i, garbage_pages = len >> PAGE_SHIFT; |
| 1487 | |
| 1488 | vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP | VM_SHARED | VM_WRITE; |
| 1489 | for (i = 0; i < garbage_pages; i++) { |
| 1490 | if (vm_insert_pfn(vma, vma->vm_start + offset + (i * PAGE_SIZE), |
| 1491 | pmem[id].garbage_pfn)) |
| 1492 | return -EAGAIN; |
| 1493 | } |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static int pmem_unmap_pfn_range(int id, struct vm_area_struct *vma, |
| 1498 | struct pmem_data *data, unsigned long offset, |
| 1499 | unsigned long len) |
| 1500 | { |
| 1501 | int garbage_pages; |
| 1502 | DLOG("unmap offset %lx len %lx\n", offset, len); |
| 1503 | |
| 1504 | BUG_ON(!PMEM_IS_PAGE_ALIGNED(len)); |
| 1505 | |
| 1506 | garbage_pages = len >> PAGE_SHIFT; |
| 1507 | zap_page_range(vma, vma->vm_start + offset, len, NULL); |
| 1508 | pmem_map_garbage(id, vma, data, offset, len); |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static int pmem_map_pfn_range(int id, struct vm_area_struct *vma, |
| 1513 | struct pmem_data *data, unsigned long offset, |
| 1514 | unsigned long len) |
| 1515 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1516 | int ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1517 | DLOG("map offset %lx len %lx\n", offset, len); |
| 1518 | BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_start)); |
| 1519 | BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_end)); |
| 1520 | BUG_ON(!PMEM_IS_PAGE_ALIGNED(len)); |
| 1521 | BUG_ON(!PMEM_IS_PAGE_ALIGNED(offset)); |
| 1522 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1523 | ret = io_remap_pfn_range(vma, vma->vm_start + offset, |
| 1524 | (pmem[id].start_addr(id, data) + offset) >> PAGE_SHIFT, |
| 1525 | len, vma->vm_page_prot); |
| 1526 | if (ret) { |
| 1527 | #if PMEM_DEBUG |
| 1528 | pr_alert("pmem: %s: io_remap_pfn_range fails with " |
| 1529 | "return value: %d!\n", __func__, ret); |
| 1530 | #endif |
| 1531 | |
| 1532 | ret = -EAGAIN; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1533 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1534 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | static int pmem_remap_pfn_range(int id, struct vm_area_struct *vma, |
| 1538 | struct pmem_data *data, unsigned long offset, |
| 1539 | unsigned long len) |
| 1540 | { |
| 1541 | /* hold the mm semp for the vma you are modifying when you call this */ |
| 1542 | BUG_ON(!vma); |
| 1543 | zap_page_range(vma, vma->vm_start + offset, len, NULL); |
| 1544 | return pmem_map_pfn_range(id, vma, data, offset, len); |
| 1545 | } |
| 1546 | |
| 1547 | static void pmem_vma_open(struct vm_area_struct *vma) |
| 1548 | { |
| 1549 | struct file *file = vma->vm_file; |
| 1550 | struct pmem_data *data = file->private_data; |
| 1551 | int id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1552 | |
| 1553 | #if PMEM_DEBUG_MSGS |
| 1554 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 1555 | #endif |
| 1556 | DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n", |
| 1557 | get_name(file), id, current->pid, |
| 1558 | get_task_comm(currtask_name, current), |
| 1559 | current->parent->pid, file, file_count(file)); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1560 | /* this should never be called as we don't support copying pmem |
| 1561 | * ranges via fork */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1562 | down_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1563 | BUG_ON(!has_allocation(file)); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1564 | /* remap the garbage pages, forkers don't get access to the data */ |
| 1565 | pmem_unmap_pfn_range(id, vma, data, 0, vma->vm_start - vma->vm_end); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1566 | up_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | static void pmem_vma_close(struct vm_area_struct *vma) |
| 1570 | { |
| 1571 | struct file *file = vma->vm_file; |
| 1572 | struct pmem_data *data = file->private_data; |
| 1573 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1574 | #if PMEM_DEBUG_MSGS |
| 1575 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 1576 | #endif |
| 1577 | DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n", |
| 1578 | get_name(file), get_id(file), current->pid, |
| 1579 | get_task_comm(currtask_name, current), |
| 1580 | current->parent->pid, file, file_count(file)); |
| 1581 | |
| 1582 | if (unlikely(!is_pmem_file(file))) { |
| 1583 | pr_warning("pmem: something is very wrong, you are " |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1584 | "closing a vm backing an allocation that doesn't " |
| 1585 | "exist!\n"); |
| 1586 | return; |
| 1587 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1588 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1589 | down_write(&data->sem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1590 | if (unlikely(!has_allocation(file))) { |
| 1591 | up_write(&data->sem); |
| 1592 | pr_warning("pmem: something is very wrong, you are " |
| 1593 | "closing a vm backing an allocation that doesn't " |
| 1594 | "exist!\n"); |
| 1595 | return; |
| 1596 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1597 | if (data->vma == vma) { |
| 1598 | data->vma = NULL; |
| 1599 | if ((data->flags & PMEM_FLAGS_CONNECTED) && |
| 1600 | (data->flags & PMEM_FLAGS_SUBMAP)) |
| 1601 | data->flags |= PMEM_FLAGS_UNSUBMAP; |
| 1602 | } |
| 1603 | /* the kernel is going to free this vma now anyway */ |
| 1604 | up_write(&data->sem); |
| 1605 | } |
| 1606 | |
| 1607 | static struct vm_operations_struct vm_ops = { |
| 1608 | .open = pmem_vma_open, |
| 1609 | .close = pmem_vma_close, |
| 1610 | }; |
| 1611 | |
| 1612 | static int pmem_mmap(struct file *file, struct vm_area_struct *vma) |
| 1613 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1614 | struct pmem_data *data = file->private_data; |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 1615 | int index = -1; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1616 | unsigned long vma_size = vma->vm_end - vma->vm_start; |
| 1617 | int ret = 0, id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1618 | #if PMEM_DEBUG_MSGS |
| 1619 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 1620 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1621 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1622 | if (!data) { |
| 1623 | pr_err("pmem: Invalid file descriptor, no private data\n"); |
| 1624 | return -EINVAL; |
| 1625 | } |
| 1626 | DLOG("pid %u(%s) mmap vma_size %lu on dev %s(id: %d)\n", current->pid, |
| 1627 | get_task_comm(currtask_name, current), vma_size, |
| 1628 | get_name(file), id); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1629 | if (vma->vm_pgoff || !PMEM_IS_PAGE_ALIGNED(vma_size)) { |
| 1630 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1631 | pr_err("pmem: mmaps must be at offset zero, aligned" |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1632 | " and a multiple of pages_size.\n"); |
| 1633 | #endif |
| 1634 | return -EINVAL; |
| 1635 | } |
| 1636 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1637 | down_write(&data->sem); |
| 1638 | /* check this file isn't already mmaped, for submaps check this file |
| 1639 | * has never been mmaped */ |
| 1640 | if ((data->flags & PMEM_FLAGS_SUBMAP) || |
| 1641 | (data->flags & PMEM_FLAGS_UNSUBMAP)) { |
| 1642 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1643 | pr_err("pmem: you can only mmap a pmem file once, " |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1644 | "this file is already mmaped. %x\n", data->flags); |
| 1645 | #endif |
| 1646 | ret = -EINVAL; |
| 1647 | goto error; |
| 1648 | } |
| 1649 | /* if file->private_data == unalloced, alloc*/ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1650 | if (data->index == -1) { |
| 1651 | mutex_lock(&pmem[id].arena_mutex); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 1652 | index = pmem_allocate_from_id(id, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1653 | vma->vm_end - vma->vm_start, |
| 1654 | SZ_4K); |
| 1655 | mutex_unlock(&pmem[id].arena_mutex); |
| 1656 | /* either no space was available or an error occured */ |
| 1657 | if (index == -1) { |
| 1658 | pr_err("pmem: mmap unable to allocate memory" |
| 1659 | "on %s\n", get_name(file)); |
| 1660 | ret = -ENOMEM; |
| 1661 | goto error; |
| 1662 | } |
| 1663 | /* store the index of a successful allocation */ |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1664 | data->index = index; |
| 1665 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1666 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1667 | if (pmem[id].len(id, data) < vma_size) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1668 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1669 | pr_err("pmem: mmap size [%lu] does not match" |
| 1670 | " size of backing region [%lu].\n", vma_size, |
| 1671 | pmem[id].len(id, data)); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1672 | #endif |
| 1673 | ret = -EINVAL; |
| 1674 | goto error; |
| 1675 | } |
| 1676 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1677 | vma->vm_pgoff = pmem[id].start_addr(id, data) >> PAGE_SHIFT; |
| 1678 | |
| 1679 | vma->vm_page_prot = pmem_phys_mem_access_prot(file, vma->vm_page_prot); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1680 | |
| 1681 | if (data->flags & PMEM_FLAGS_CONNECTED) { |
| 1682 | struct pmem_region_node *region_node; |
| 1683 | struct list_head *elt; |
| 1684 | if (pmem_map_garbage(id, vma, data, 0, vma_size)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1685 | pr_alert("pmem: mmap failed in kernel!\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1686 | ret = -EAGAIN; |
| 1687 | goto error; |
| 1688 | } |
| 1689 | list_for_each(elt, &data->region_list) { |
| 1690 | region_node = list_entry(elt, struct pmem_region_node, |
| 1691 | list); |
| 1692 | DLOG("remapping file: %p %lx %lx\n", file, |
| 1693 | region_node->region.offset, |
| 1694 | region_node->region.len); |
| 1695 | if (pmem_remap_pfn_range(id, vma, data, |
| 1696 | region_node->region.offset, |
| 1697 | region_node->region.len)) { |
| 1698 | ret = -EAGAIN; |
| 1699 | goto error; |
| 1700 | } |
| 1701 | } |
| 1702 | data->flags |= PMEM_FLAGS_SUBMAP; |
| 1703 | get_task_struct(current->group_leader); |
| 1704 | data->task = current->group_leader; |
| 1705 | data->vma = vma; |
| 1706 | #if PMEM_DEBUG |
| 1707 | data->pid = current->pid; |
| 1708 | #endif |
| 1709 | DLOG("submmapped file %p vma %p pid %u\n", file, vma, |
| 1710 | current->pid); |
| 1711 | } else { |
| 1712 | if (pmem_map_pfn_range(id, vma, data, 0, vma_size)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1713 | pr_err("pmem: mmap failed in kernel!\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1714 | ret = -EAGAIN; |
| 1715 | goto error; |
| 1716 | } |
| 1717 | data->flags |= PMEM_FLAGS_MASTERMAP; |
| 1718 | data->pid = current->pid; |
| 1719 | } |
| 1720 | vma->vm_ops = &vm_ops; |
| 1721 | error: |
| 1722 | up_write(&data->sem); |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | /* the following are the api for accessing pmem regions by other drivers |
| 1727 | * from inside the kernel */ |
| 1728 | int get_pmem_user_addr(struct file *file, unsigned long *start, |
| 1729 | unsigned long *len) |
| 1730 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1731 | int ret = -1; |
| 1732 | |
| 1733 | if (is_pmem_file(file)) { |
| 1734 | struct pmem_data *data = file->private_data; |
| 1735 | |
| 1736 | down_read(&data->sem); |
| 1737 | if (has_allocation(file)) { |
| 1738 | if (data->vma) { |
| 1739 | *start = data->vma->vm_start; |
| 1740 | *len = data->vma->vm_end - data->vma->vm_start; |
| 1741 | } else { |
| 1742 | *start = *len = 0; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1743 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1744 | pr_err("pmem: %s: no vma present.\n", |
| 1745 | __func__); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1746 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1747 | } |
| 1748 | ret = 0; |
| 1749 | } |
| 1750 | up_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1751 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1752 | |
| 1753 | #if PMEM_DEBUG |
| 1754 | if (ret) |
| 1755 | pr_err("pmem: %s: requested pmem data from invalid" |
| 1756 | "file.\n", __func__); |
| 1757 | #endif |
| 1758 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | int get_pmem_addr(struct file *file, unsigned long *start, |
| 1762 | unsigned long *vstart, unsigned long *len) |
| 1763 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1764 | int ret = -1; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1765 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1766 | if (is_pmem_file(file)) { |
| 1767 | struct pmem_data *data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1768 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1769 | down_read(&data->sem); |
| 1770 | if (has_allocation(file)) { |
| 1771 | int id = get_id(file); |
| 1772 | |
| 1773 | *start = pmem[id].start_addr(id, data); |
| 1774 | *len = pmem[id].len(id, data); |
| 1775 | *vstart = (unsigned long) |
| 1776 | pmem_start_vaddr(id, data); |
| 1777 | up_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1778 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1779 | down_write(&data->sem); |
| 1780 | data->ref++; |
| 1781 | up_write(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1782 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1783 | DLOG("returning start %#lx len %lu " |
| 1784 | "vstart %#lx\n", |
| 1785 | *start, *len, *vstart); |
| 1786 | ret = 0; |
| 1787 | } else { |
| 1788 | up_read(&data->sem); |
| 1789 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1790 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1791 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1794 | int get_pmem_file(unsigned int fd, unsigned long *start, unsigned long *vstart, |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1795 | unsigned long *len, struct file **filp) |
| 1796 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1797 | int ret = -1; |
| 1798 | struct file *file = fget(fd); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1799 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1800 | if (unlikely(file == NULL)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1801 | pr_err("pmem: %s: requested data from file " |
| 1802 | "descriptor that doesn't exist.\n", __func__); |
| 1803 | } else { |
| 1804 | #if PMEM_DEBUG_MSGS |
| 1805 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 1806 | #endif |
| 1807 | DLOG("filp %p rdev %d pid %u(%s) file %p(%ld)" |
| 1808 | " dev %s(id: %d)\n", filp, |
| 1809 | file->f_dentry->d_inode->i_rdev, |
| 1810 | current->pid, get_task_comm(currtask_name, current), |
| 1811 | file, file_count(file), get_name(file), get_id(file)); |
| 1812 | |
| 1813 | if (!get_pmem_addr(file, start, vstart, len)) { |
| 1814 | if (filp) |
| 1815 | *filp = file; |
| 1816 | ret = 0; |
| 1817 | } else { |
| 1818 | fput(file); |
| 1819 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1820 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1821 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1822 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1823 | EXPORT_SYMBOL(get_pmem_file); |
| 1824 | |
| 1825 | int get_pmem_fd(int fd, unsigned long *start, unsigned long *len) |
| 1826 | { |
| 1827 | unsigned long vstart; |
| 1828 | return get_pmem_file(fd, start, &vstart, len, NULL); |
| 1829 | } |
| 1830 | EXPORT_SYMBOL(get_pmem_fd); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1831 | |
| 1832 | void put_pmem_file(struct file *file) |
| 1833 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1834 | #if PMEM_DEBUG_MSGS |
| 1835 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1836 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1837 | DLOG("rdev %d pid %u(%s) file %p(%ld)" " dev %s(id: %d)\n", |
| 1838 | file->f_dentry->d_inode->i_rdev, current->pid, |
| 1839 | get_task_comm(currtask_name, current), file, |
| 1840 | file_count(file), get_name(file), get_id(file)); |
| 1841 | if (is_pmem_file(file)) { |
| 1842 | #if PMEM_DEBUG |
| 1843 | struct pmem_data *data = file->private_data; |
| 1844 | |
| 1845 | down_write(&data->sem); |
| 1846 | if (!data->ref--) { |
| 1847 | data->ref++; |
| 1848 | pr_alert("pmem: pmem_put > pmem_get %s " |
| 1849 | "(pid %d)\n", |
| 1850 | pmem[get_id(file)].dev.name, data->pid); |
| 1851 | BUG(); |
| 1852 | } |
| 1853 | up_write(&data->sem); |
| 1854 | #endif |
| 1855 | fput(file); |
| 1856 | } |
| 1857 | } |
| 1858 | EXPORT_SYMBOL(put_pmem_file); |
| 1859 | |
| 1860 | void put_pmem_fd(int fd) |
| 1861 | { |
| 1862 | int put_needed; |
| 1863 | struct file *file = fget_light(fd, &put_needed); |
| 1864 | |
| 1865 | if (file) { |
| 1866 | put_pmem_file(file); |
| 1867 | fput_light(file, put_needed); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | void flush_pmem_fd(int fd, unsigned long offset, unsigned long len) |
| 1872 | { |
| 1873 | int fput_needed; |
| 1874 | struct file *file = fget_light(fd, &fput_needed); |
| 1875 | |
| 1876 | if (file) { |
| 1877 | flush_pmem_file(file, offset, len); |
| 1878 | fput_light(file, fput_needed); |
| 1879 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | void flush_pmem_file(struct file *file, unsigned long offset, unsigned long len) |
| 1883 | { |
| 1884 | struct pmem_data *data; |
| 1885 | int id; |
| 1886 | void *vaddr; |
| 1887 | struct pmem_region_node *region_node; |
| 1888 | struct list_head *elt; |
| 1889 | void *flush_start, *flush_end; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1890 | #ifdef CONFIG_OUTER_CACHE |
| 1891 | unsigned long phy_start, phy_end; |
| 1892 | #endif |
| 1893 | if (!is_pmem_file(file)) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1894 | return; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1895 | |
| 1896 | id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1897 | if (!pmem[id].cached) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1898 | return; |
| 1899 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1900 | /* is_pmem_file fails if !file */ |
| 1901 | data = file->private_data; |
| 1902 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1903 | down_read(&data->sem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1904 | if (!has_allocation(file)) |
| 1905 | goto end; |
| 1906 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1907 | vaddr = pmem_start_vaddr(id, data); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1908 | |
| 1909 | if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM) { |
| 1910 | dmac_flush_range(vaddr, |
| 1911 | (void *)((unsigned long)vaddr + |
| 1912 | ((struct alloc_list *)(data->index))->size)); |
| 1913 | #ifdef CONFIG_OUTER_CACHE |
| 1914 | phy_start = pmem_start_addr_system(id, data); |
| 1915 | |
| 1916 | phy_end = phy_start + |
| 1917 | ((struct alloc_list *)(data->index))->size; |
| 1918 | |
| 1919 | outer_flush_range(phy_start, phy_end); |
| 1920 | #endif |
| 1921 | goto end; |
| 1922 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1923 | /* if this isn't a submmapped file, flush the whole thing */ |
| 1924 | if (unlikely(!(data->flags & PMEM_FLAGS_CONNECTED))) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1925 | dmac_flush_range(vaddr, vaddr + pmem[id].len(id, data)); |
| 1926 | #ifdef CONFIG_OUTER_CACHE |
| 1927 | phy_start = (unsigned long)vaddr - |
| 1928 | (unsigned long)pmem[id].vbase + pmem[id].base; |
| 1929 | |
| 1930 | phy_end = phy_start + pmem[id].len(id, data); |
| 1931 | |
| 1932 | outer_flush_range(phy_start, phy_end); |
| 1933 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1934 | goto end; |
| 1935 | } |
| 1936 | /* otherwise, flush the region of the file we are drawing */ |
| 1937 | list_for_each(elt, &data->region_list) { |
| 1938 | region_node = list_entry(elt, struct pmem_region_node, list); |
| 1939 | if ((offset >= region_node->region.offset) && |
| 1940 | ((offset + len) <= (region_node->region.offset + |
| 1941 | region_node->region.len))) { |
| 1942 | flush_start = vaddr + region_node->region.offset; |
| 1943 | flush_end = flush_start + region_node->region.len; |
| 1944 | dmac_flush_range(flush_start, flush_end); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1945 | #ifdef CONFIG_OUTER_CACHE |
| 1946 | |
| 1947 | phy_start = (unsigned long)flush_start - |
| 1948 | (unsigned long)pmem[id].vbase + pmem[id].base; |
| 1949 | |
| 1950 | phy_end = phy_start + region_node->region.len; |
| 1951 | |
| 1952 | outer_flush_range(phy_start, phy_end); |
| 1953 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 1954 | break; |
| 1955 | } |
| 1956 | } |
| 1957 | end: |
| 1958 | up_read(&data->sem); |
| 1959 | } |
| 1960 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1961 | int pmem_cache_maint(struct file *file, unsigned int cmd, |
| 1962 | struct pmem_addr *pmem_addr) |
| 1963 | { |
| 1964 | struct pmem_data *data; |
| 1965 | int id; |
| 1966 | unsigned long vaddr, paddr, length, offset, |
| 1967 | pmem_len, pmem_start_addr; |
| 1968 | |
| 1969 | /* Called from kernel-space so file may be NULL */ |
| 1970 | if (!file) |
| 1971 | return -EBADF; |
| 1972 | |
Shubhraprakash Das | 7788cad | 2011-11-21 13:02:22 -0700 | [diff] [blame^] | 1973 | /* |
| 1974 | * check that the vaddr passed for flushing is valid |
| 1975 | * so that you don't crash the kernel |
| 1976 | */ |
| 1977 | if (!pmem_addr->vaddr) |
| 1978 | return -EINVAL; |
| 1979 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1980 | data = file->private_data; |
| 1981 | id = get_id(file); |
| 1982 | |
| 1983 | if (!pmem[id].cached) |
| 1984 | return 0; |
| 1985 | |
| 1986 | offset = pmem_addr->offset; |
| 1987 | length = pmem_addr->length; |
| 1988 | |
| 1989 | down_read(&data->sem); |
| 1990 | if (!has_allocation(file)) { |
| 1991 | up_read(&data->sem); |
| 1992 | return -EINVAL; |
| 1993 | } |
| 1994 | pmem_len = pmem[id].len(id, data); |
| 1995 | pmem_start_addr = pmem[id].start_addr(id, data); |
| 1996 | up_read(&data->sem); |
| 1997 | |
| 1998 | if (offset + length > pmem_len) |
| 1999 | return -EINVAL; |
| 2000 | |
| 2001 | vaddr = pmem_addr->vaddr; |
| 2002 | paddr = pmem_start_addr + offset; |
| 2003 | |
| 2004 | DLOG("pmem cache maint on dev %s(id: %d)" |
| 2005 | "(vaddr %lx paddr %lx len %lu bytes)\n", |
| 2006 | get_name(file), id, vaddr, paddr, length); |
| 2007 | if (cmd == PMEM_CLEAN_INV_CACHES) |
| 2008 | clean_and_invalidate_caches(vaddr, |
| 2009 | length, paddr); |
| 2010 | else if (cmd == PMEM_CLEAN_CACHES) |
| 2011 | clean_caches(vaddr, length, paddr); |
| 2012 | else if (cmd == PMEM_INV_CACHES) |
| 2013 | invalidate_caches(vaddr, length, paddr); |
| 2014 | |
| 2015 | return 0; |
| 2016 | } |
| 2017 | EXPORT_SYMBOL(pmem_cache_maint); |
| 2018 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2019 | static int pmem_connect(unsigned long connect, struct file *file) |
| 2020 | { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2021 | int ret = 0, put_needed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2022 | struct file *src_file; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2023 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2024 | if (!file) { |
| 2025 | pr_err("pmem: %s: NULL file pointer passed in, " |
| 2026 | "bailing out!\n", __func__); |
| 2027 | ret = -EINVAL; |
| 2028 | goto leave; |
| 2029 | } |
| 2030 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2031 | src_file = fget_light(connect, &put_needed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2032 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2033 | if (!src_file) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2034 | pr_err("pmem: %s: src file not found!\n", __func__); |
| 2035 | ret = -EBADF; |
| 2036 | goto leave; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2037 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2038 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2039 | if (src_file == file) { /* degenerative case, operator error */ |
| 2040 | pr_err("pmem: %s: src_file and passed in file are " |
| 2041 | "the same; refusing to connect to self!\n", __func__); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2042 | ret = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2043 | goto put_src_file; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2044 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2045 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2046 | if (unlikely(!is_pmem_file(src_file))) { |
| 2047 | pr_err("pmem: %s: src file is not a pmem file!\n", |
| 2048 | __func__); |
| 2049 | ret = -EINVAL; |
| 2050 | goto put_src_file; |
| 2051 | } else { |
| 2052 | struct pmem_data *src_data = src_file->private_data; |
| 2053 | |
| 2054 | if (!src_data) { |
| 2055 | pr_err("pmem: %s: src file pointer has no" |
| 2056 | "private data, bailing out!\n", __func__); |
| 2057 | ret = -EINVAL; |
| 2058 | goto put_src_file; |
| 2059 | } |
| 2060 | |
| 2061 | down_read(&src_data->sem); |
| 2062 | |
| 2063 | if (unlikely(!has_allocation(src_file))) { |
| 2064 | up_read(&src_data->sem); |
| 2065 | pr_err("pmem: %s: src file has no allocation!\n", |
| 2066 | __func__); |
| 2067 | ret = -EINVAL; |
| 2068 | } else { |
| 2069 | struct pmem_data *data; |
| 2070 | int src_index = src_data->index; |
| 2071 | |
| 2072 | up_read(&src_data->sem); |
| 2073 | |
| 2074 | data = file->private_data; |
| 2075 | if (!data) { |
| 2076 | pr_err("pmem: %s: passed in file " |
| 2077 | "pointer has no private data, bailing" |
| 2078 | " out!\n", __func__); |
| 2079 | ret = -EINVAL; |
| 2080 | goto put_src_file; |
| 2081 | } |
| 2082 | |
| 2083 | down_write(&data->sem); |
| 2084 | if (has_allocation(file) && |
| 2085 | (data->index != src_index)) { |
| 2086 | up_write(&data->sem); |
| 2087 | |
| 2088 | pr_err("pmem: %s: file is already " |
| 2089 | "mapped but doesn't match this " |
| 2090 | "src_file!\n", __func__); |
| 2091 | ret = -EINVAL; |
| 2092 | } else { |
| 2093 | data->index = src_index; |
| 2094 | data->flags |= PMEM_FLAGS_CONNECTED; |
| 2095 | data->master_fd = connect; |
| 2096 | data->master_file = src_file; |
| 2097 | |
| 2098 | up_write(&data->sem); |
| 2099 | |
| 2100 | DLOG("connect %p to %p\n", file, src_file); |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | put_src_file: |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2105 | fput_light(src_file, put_needed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2106 | leave: |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2107 | return ret; |
| 2108 | } |
| 2109 | |
| 2110 | static void pmem_unlock_data_and_mm(struct pmem_data *data, |
| 2111 | struct mm_struct *mm) |
| 2112 | { |
| 2113 | up_write(&data->sem); |
| 2114 | if (mm != NULL) { |
| 2115 | up_write(&mm->mmap_sem); |
| 2116 | mmput(mm); |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | static int pmem_lock_data_and_mm(struct file *file, struct pmem_data *data, |
| 2121 | struct mm_struct **locked_mm) |
| 2122 | { |
| 2123 | int ret = 0; |
| 2124 | struct mm_struct *mm = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2125 | #if PMEM_DEBUG_MSGS |
| 2126 | char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 2127 | #endif |
| 2128 | DLOG("pid %u(%s) file %p(%ld)\n", |
| 2129 | current->pid, get_task_comm(currtask_name, current), |
| 2130 | file, file_count(file)); |
| 2131 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2132 | *locked_mm = NULL; |
| 2133 | lock_mm: |
| 2134 | down_read(&data->sem); |
| 2135 | if (PMEM_IS_SUBMAP(data)) { |
| 2136 | mm = get_task_mm(data->task); |
| 2137 | if (!mm) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2138 | up_read(&data->sem); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2139 | #if PMEM_DEBUG |
| 2140 | pr_alert("pmem: can't remap - task is gone!\n"); |
| 2141 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2142 | return -1; |
| 2143 | } |
| 2144 | } |
| 2145 | up_read(&data->sem); |
| 2146 | |
| 2147 | if (mm) |
| 2148 | down_write(&mm->mmap_sem); |
| 2149 | |
| 2150 | down_write(&data->sem); |
| 2151 | /* check that the file didn't get mmaped before we could take the |
| 2152 | * data sem, this should be safe b/c you can only submap each file |
| 2153 | * once */ |
| 2154 | if (PMEM_IS_SUBMAP(data) && !mm) { |
| 2155 | pmem_unlock_data_and_mm(data, mm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2156 | DLOG("mapping contention, repeating mmap op\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2157 | goto lock_mm; |
| 2158 | } |
| 2159 | /* now check that vma.mm is still there, it could have been |
| 2160 | * deleted by vma_close before we could get the data->sem */ |
| 2161 | if ((data->flags & PMEM_FLAGS_UNSUBMAP) && (mm != NULL)) { |
| 2162 | /* might as well release this */ |
| 2163 | if (data->flags & PMEM_FLAGS_SUBMAP) { |
| 2164 | put_task_struct(data->task); |
| 2165 | data->task = NULL; |
| 2166 | /* lower the submap flag to show the mm is gone */ |
| 2167 | data->flags &= ~(PMEM_FLAGS_SUBMAP); |
| 2168 | } |
| 2169 | pmem_unlock_data_and_mm(data, mm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2170 | #if PMEM_DEBUG |
| 2171 | pr_alert("pmem: vma.mm went away!\n"); |
| 2172 | #endif |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2173 | return -1; |
| 2174 | } |
| 2175 | *locked_mm = mm; |
| 2176 | return ret; |
| 2177 | } |
| 2178 | |
| 2179 | int pmem_remap(struct pmem_region *region, struct file *file, |
| 2180 | unsigned operation) |
| 2181 | { |
| 2182 | int ret; |
| 2183 | struct pmem_region_node *region_node; |
| 2184 | struct mm_struct *mm = NULL; |
| 2185 | struct list_head *elt, *elt2; |
| 2186 | int id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2187 | struct pmem_data *data; |
| 2188 | |
| 2189 | DLOG("operation %#x, region offset %ld, region len %ld\n", |
| 2190 | operation, region->offset, region->len); |
| 2191 | |
| 2192 | if (!is_pmem_file(file)) { |
| 2193 | #if PMEM_DEBUG |
| 2194 | pr_err("pmem: remap request for non-pmem file descriptor\n"); |
| 2195 | #endif |
| 2196 | return -EINVAL; |
| 2197 | } |
| 2198 | |
| 2199 | /* is_pmem_file fails if !file */ |
| 2200 | data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2201 | |
| 2202 | /* pmem region must be aligned on a page boundry */ |
| 2203 | if (unlikely(!PMEM_IS_PAGE_ALIGNED(region->offset) || |
| 2204 | !PMEM_IS_PAGE_ALIGNED(region->len))) { |
| 2205 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2206 | pr_err("pmem: request for unaligned pmem" |
| 2207 | "suballocation %lx %lx\n", |
| 2208 | region->offset, region->len); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2209 | #endif |
| 2210 | return -EINVAL; |
| 2211 | } |
| 2212 | |
| 2213 | /* if userspace requests a region of len 0, there's nothing to do */ |
| 2214 | if (region->len == 0) |
| 2215 | return 0; |
| 2216 | |
| 2217 | /* lock the mm and data */ |
| 2218 | ret = pmem_lock_data_and_mm(file, data, &mm); |
| 2219 | if (ret) |
| 2220 | return 0; |
| 2221 | |
| 2222 | /* only the owner of the master file can remap the client fds |
| 2223 | * that back in it */ |
| 2224 | if (!is_master_owner(file)) { |
| 2225 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2226 | pr_err("pmem: remap requested from non-master process\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2227 | #endif |
| 2228 | ret = -EINVAL; |
| 2229 | goto err; |
| 2230 | } |
| 2231 | |
| 2232 | /* check that the requested range is within the src allocation */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2233 | if (unlikely((region->offset > pmem[id].len(id, data)) || |
| 2234 | (region->len > pmem[id].len(id, data)) || |
| 2235 | (region->offset + region->len > pmem[id].len(id, data)))) { |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2236 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2237 | pr_err("pmem: suballoc doesn't fit in src_file!\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2238 | #endif |
| 2239 | ret = -EINVAL; |
| 2240 | goto err; |
| 2241 | } |
| 2242 | |
| 2243 | if (operation == PMEM_MAP) { |
| 2244 | region_node = kmalloc(sizeof(struct pmem_region_node), |
| 2245 | GFP_KERNEL); |
| 2246 | if (!region_node) { |
| 2247 | ret = -ENOMEM; |
| 2248 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2249 | pr_alert("pmem: No space to allocate remap metadata!"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2250 | #endif |
| 2251 | goto err; |
| 2252 | } |
| 2253 | region_node->region = *region; |
| 2254 | list_add(®ion_node->list, &data->region_list); |
| 2255 | } else if (operation == PMEM_UNMAP) { |
| 2256 | int found = 0; |
| 2257 | list_for_each_safe(elt, elt2, &data->region_list) { |
| 2258 | region_node = list_entry(elt, struct pmem_region_node, |
| 2259 | list); |
| 2260 | if (region->len == 0 || |
| 2261 | (region_node->region.offset == region->offset && |
| 2262 | region_node->region.len == region->len)) { |
| 2263 | list_del(elt); |
| 2264 | kfree(region_node); |
| 2265 | found = 1; |
| 2266 | } |
| 2267 | } |
| 2268 | if (!found) { |
| 2269 | #if PMEM_DEBUG |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2270 | pr_err("pmem: Unmap region does not map any" |
| 2271 | " mapped region!"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2272 | #endif |
| 2273 | ret = -EINVAL; |
| 2274 | goto err; |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | if (data->vma && PMEM_IS_SUBMAP(data)) { |
| 2279 | if (operation == PMEM_MAP) |
| 2280 | ret = pmem_remap_pfn_range(id, data->vma, data, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2281 | region->offset, region->len); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2282 | else if (operation == PMEM_UNMAP) |
| 2283 | ret = pmem_unmap_pfn_range(id, data->vma, data, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2284 | region->offset, region->len); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | err: |
| 2288 | pmem_unlock_data_and_mm(data, mm); |
| 2289 | return ret; |
| 2290 | } |
| 2291 | |
| 2292 | static void pmem_revoke(struct file *file, struct pmem_data *data) |
| 2293 | { |
| 2294 | struct pmem_region_node *region_node; |
| 2295 | struct list_head *elt, *elt2; |
| 2296 | struct mm_struct *mm = NULL; |
| 2297 | int id = get_id(file); |
| 2298 | int ret = 0; |
| 2299 | |
| 2300 | data->master_file = NULL; |
| 2301 | ret = pmem_lock_data_and_mm(file, data, &mm); |
| 2302 | /* if lock_data_and_mm fails either the task that mapped the fd, or |
| 2303 | * the vma that mapped it have already gone away, nothing more |
| 2304 | * needs to be done */ |
| 2305 | if (ret) |
| 2306 | return; |
| 2307 | /* unmap everything */ |
| 2308 | /* delete the regions and region list nothing is mapped any more */ |
| 2309 | if (data->vma) |
| 2310 | list_for_each_safe(elt, elt2, &data->region_list) { |
| 2311 | region_node = list_entry(elt, struct pmem_region_node, |
| 2312 | list); |
| 2313 | pmem_unmap_pfn_range(id, data->vma, data, |
| 2314 | region_node->region.offset, |
| 2315 | region_node->region.len); |
| 2316 | list_del(elt); |
| 2317 | kfree(region_node); |
| 2318 | } |
| 2319 | /* delete the master file */ |
| 2320 | pmem_unlock_data_and_mm(data, mm); |
| 2321 | } |
| 2322 | |
| 2323 | static void pmem_get_size(struct pmem_region *region, struct file *file) |
| 2324 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2325 | /* called via ioctl file op, so file guaranteed to be not NULL */ |
| 2326 | struct pmem_data *data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2327 | int id = get_id(file); |
| 2328 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2329 | down_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2330 | if (!has_allocation(file)) { |
| 2331 | region->offset = 0; |
| 2332 | region->len = 0; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2333 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2334 | region->offset = pmem[id].start_addr(id, data); |
| 2335 | region->len = pmem[id].len(id, data); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2336 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2337 | up_read(&data->sem); |
| 2338 | DLOG("offset 0x%lx len 0x%lx\n", region->offset, region->len); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | |
| 2342 | static long pmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 2343 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2344 | /* called from user space as file op, so file guaranteed to be not |
| 2345 | * NULL |
| 2346 | */ |
| 2347 | struct pmem_data *data = file->private_data; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2348 | int id = get_id(file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2349 | #if PMEM_DEBUG_MSGS |
| 2350 | char currtask_name[ |
| 2351 | FIELD_SIZEOF(struct task_struct, comm) + 1]; |
| 2352 | #endif |
| 2353 | |
| 2354 | DLOG("pid %u(%s) file %p(%ld) cmd %#x, dev %s(id: %d)\n", |
| 2355 | current->pid, get_task_comm(currtask_name, current), |
| 2356 | file, file_count(file), cmd, get_name(file), id); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2357 | |
| 2358 | switch (cmd) { |
| 2359 | case PMEM_GET_PHYS: |
| 2360 | { |
| 2361 | struct pmem_region region; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2362 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2363 | DLOG("get_phys\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2364 | down_read(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2365 | if (!has_allocation(file)) { |
| 2366 | region.offset = 0; |
| 2367 | region.len = 0; |
| 2368 | } else { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2369 | region.offset = pmem[id].start_addr(id, data); |
| 2370 | region.len = pmem[id].len(id, data); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2371 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2372 | up_read(&data->sem); |
| 2373 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2374 | if (copy_to_user((void __user *)arg, ®ion, |
| 2375 | sizeof(struct pmem_region))) |
| 2376 | return -EFAULT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2377 | |
| 2378 | DLOG("pmem: successful request for " |
| 2379 | "physical address of pmem region id %d, " |
| 2380 | "offset 0x%lx, len 0x%lx\n", |
| 2381 | id, region.offset, region.len); |
| 2382 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2383 | break; |
| 2384 | } |
| 2385 | case PMEM_MAP: |
| 2386 | { |
| 2387 | struct pmem_region region; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2388 | DLOG("map\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2389 | if (copy_from_user(®ion, (void __user *)arg, |
| 2390 | sizeof(struct pmem_region))) |
| 2391 | return -EFAULT; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2392 | return pmem_remap(®ion, file, PMEM_MAP); |
| 2393 | } |
| 2394 | break; |
| 2395 | case PMEM_UNMAP: |
| 2396 | { |
| 2397 | struct pmem_region region; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2398 | DLOG("unmap\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2399 | if (copy_from_user(®ion, (void __user *)arg, |
| 2400 | sizeof(struct pmem_region))) |
| 2401 | return -EFAULT; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2402 | return pmem_remap(®ion, file, PMEM_UNMAP); |
| 2403 | break; |
| 2404 | } |
| 2405 | case PMEM_GET_SIZE: |
| 2406 | { |
| 2407 | struct pmem_region region; |
| 2408 | DLOG("get_size\n"); |
| 2409 | pmem_get_size(®ion, file); |
| 2410 | if (copy_to_user((void __user *)arg, ®ion, |
| 2411 | sizeof(struct pmem_region))) |
| 2412 | return -EFAULT; |
| 2413 | break; |
| 2414 | } |
| 2415 | case PMEM_GET_TOTAL_SIZE: |
| 2416 | { |
| 2417 | struct pmem_region region; |
| 2418 | DLOG("get total size\n"); |
| 2419 | region.offset = 0; |
| 2420 | get_id(file); |
| 2421 | region.len = pmem[id].size; |
| 2422 | if (copy_to_user((void __user *)arg, ®ion, |
| 2423 | sizeof(struct pmem_region))) |
| 2424 | return -EFAULT; |
| 2425 | break; |
| 2426 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2427 | case PMEM_GET_FREE_SPACE: |
| 2428 | { |
| 2429 | struct pmem_freespace fs; |
| 2430 | DLOG("get freespace on %s(id: %d)\n", |
| 2431 | get_name(file), id); |
| 2432 | |
| 2433 | mutex_lock(&pmem[id].arena_mutex); |
| 2434 | pmem[id].free_space(id, &fs); |
| 2435 | mutex_unlock(&pmem[id].arena_mutex); |
| 2436 | |
| 2437 | DLOG("%s(id: %d) total free %lu, largest %lu\n", |
| 2438 | get_name(file), id, fs.total, fs.largest); |
| 2439 | |
| 2440 | if (copy_to_user((void __user *)arg, &fs, |
| 2441 | sizeof(struct pmem_freespace))) |
| 2442 | return -EFAULT; |
| 2443 | break; |
| 2444 | } |
| 2445 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2446 | case PMEM_ALLOCATE: |
| 2447 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2448 | int ret = 0; |
| 2449 | DLOG("allocate, id %d\n", id); |
| 2450 | down_write(&data->sem); |
| 2451 | if (has_allocation(file)) { |
| 2452 | pr_err("pmem: Existing allocation found on " |
| 2453 | "this file descrpitor\n"); |
| 2454 | up_write(&data->sem); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2455 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | mutex_lock(&pmem[id].arena_mutex); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 2459 | data->index = pmem_allocate_from_id(id, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2460 | arg, |
| 2461 | SZ_4K); |
| 2462 | mutex_unlock(&pmem[id].arena_mutex); |
| 2463 | ret = data->index == -1 ? -ENOMEM : |
| 2464 | data->index; |
| 2465 | up_write(&data->sem); |
| 2466 | return ret; |
| 2467 | } |
| 2468 | case PMEM_ALLOCATE_ALIGNED: |
| 2469 | { |
| 2470 | struct pmem_allocation alloc; |
| 2471 | int ret = 0; |
| 2472 | |
| 2473 | if (copy_from_user(&alloc, (void __user *)arg, |
| 2474 | sizeof(struct pmem_allocation))) |
| 2475 | return -EFAULT; |
| 2476 | DLOG("allocate id align %d %u\n", id, alloc.align); |
| 2477 | down_write(&data->sem); |
| 2478 | if (has_allocation(file)) { |
| 2479 | pr_err("pmem: Existing allocation found on " |
| 2480 | "this file descrpitor\n"); |
| 2481 | up_write(&data->sem); |
| 2482 | return -EINVAL; |
| 2483 | } |
| 2484 | |
| 2485 | if (alloc.align & (alloc.align - 1)) { |
| 2486 | pr_err("pmem: Alignment is not a power of 2\n"); |
| 2487 | return -EINVAL; |
| 2488 | } |
| 2489 | |
| 2490 | if (alloc.align != SZ_4K && |
| 2491 | (pmem[id].allocator_type != |
| 2492 | PMEM_ALLOCATORTYPE_BITMAP)) { |
| 2493 | pr_err("pmem: Non 4k alignment requires bitmap" |
| 2494 | " allocator on %s\n", pmem[id].name); |
| 2495 | return -EINVAL; |
| 2496 | } |
| 2497 | |
| 2498 | if (alloc.align > SZ_1M || |
| 2499 | alloc.align < SZ_4K) { |
| 2500 | pr_err("pmem: Invalid Alignment (%u) " |
| 2501 | "specified\n", alloc.align); |
| 2502 | return -EINVAL; |
| 2503 | } |
| 2504 | |
| 2505 | mutex_lock(&pmem[id].arena_mutex); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 2506 | data->index = pmem_allocate_from_id(id, |
| 2507 | alloc.size, |
| 2508 | alloc.align); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2509 | mutex_unlock(&pmem[id].arena_mutex); |
| 2510 | ret = data->index == -1 ? -ENOMEM : |
| 2511 | data->index; |
| 2512 | up_write(&data->sem); |
| 2513 | return ret; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2514 | } |
| 2515 | case PMEM_CONNECT: |
| 2516 | DLOG("connect\n"); |
| 2517 | return pmem_connect(arg, file); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2518 | case PMEM_CLEAN_INV_CACHES: |
| 2519 | case PMEM_CLEAN_CACHES: |
| 2520 | case PMEM_INV_CACHES: |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2521 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2522 | struct pmem_addr pmem_addr; |
| 2523 | |
| 2524 | if (copy_from_user(&pmem_addr, (void __user *)arg, |
| 2525 | sizeof(struct pmem_addr))) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2526 | return -EFAULT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2527 | |
| 2528 | return pmem_cache_maint(file, cmd, &pmem_addr); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2529 | } |
| 2530 | default: |
| 2531 | if (pmem[id].ioctl) |
| 2532 | return pmem[id].ioctl(file, cmd, arg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2533 | |
| 2534 | DLOG("ioctl invalid (%#x)\n", cmd); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2535 | return -EINVAL; |
| 2536 | } |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2540 | static void ioremap_pmem(int id) |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2541 | { |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 2542 | unsigned long addr; |
| 2543 | const struct mem_type *type; |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 2544 | |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 2545 | DLOG("PMEMDEBUG: ioremaping for %s\n", pmem[id].name); |
| 2546 | if (pmem[id].map_on_demand) { |
| 2547 | addr = (unsigned long)pmem[id].area->addr; |
| 2548 | if (pmem[id].cached) |
| 2549 | type = get_mem_type(MT_DEVICE_CACHED); |
| 2550 | else |
| 2551 | type = get_mem_type(MT_DEVICE); |
| 2552 | DLOG("PMEMDEBUG: Remap phys %lx to virt %lx on %s\n", |
| 2553 | pmem[id].base, addr, pmem[id].name); |
| 2554 | if (ioremap_page_range(addr, addr + pmem[id].size, |
| 2555 | pmem[id].base, __pgprot(type->prot_pte))) { |
| 2556 | pr_err("pmem: Failed to map pages\n"); |
| 2557 | BUG(); |
| 2558 | } |
| 2559 | pmem[id].vbase = pmem[id].area->addr; |
| 2560 | /* Flush the cache after installing page table entries to avoid |
| 2561 | * aliasing when these pages are remapped to user space. |
| 2562 | */ |
| 2563 | flush_cache_vmap(addr, addr + pmem[id].size); |
| 2564 | } else { |
| 2565 | if (pmem[id].cached) |
| 2566 | pmem[id].vbase = ioremap_cached(pmem[id].base, |
| 2567 | pmem[id].size); |
| 2568 | #ifdef ioremap_ext_buffered |
| 2569 | else if (pmem[id].buffered) |
| 2570 | pmem[id].vbase = ioremap_ext_buffered(pmem[id].base, |
| 2571 | pmem[id].size); |
| 2572 | #endif |
| 2573 | else |
| 2574 | pmem[id].vbase = ioremap(pmem[id].base, pmem[id].size); |
| 2575 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2576 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2577 | |
| 2578 | int pmem_setup(struct android_pmem_platform_data *pdata, |
| 2579 | long (*ioctl)(struct file *, unsigned int, unsigned long), |
| 2580 | int (*release)(struct inode *, struct file *)) |
| 2581 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2582 | int i, index = 0, id; |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 2583 | struct vm_struct *pmem_vma = NULL; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2584 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2585 | if (id_count >= PMEM_MAX_DEVICES) { |
| 2586 | pr_alert("pmem: %s: unable to register driver(%s) - no more " |
| 2587 | "devices available!\n", __func__, pdata->name); |
| 2588 | goto err_no_mem; |
| 2589 | } |
| 2590 | |
| 2591 | if (!pdata->size) { |
| 2592 | pr_alert("pmem: %s: unable to register pmem driver(%s) - zero " |
| 2593 | "size passed in!\n", __func__, pdata->name); |
| 2594 | goto err_no_mem; |
| 2595 | } |
| 2596 | |
| 2597 | id = id_count++; |
| 2598 | |
| 2599 | pmem[id].id = id; |
| 2600 | |
| 2601 | if (pmem[id].allocate) { |
| 2602 | pr_alert("pmem: %s: unable to register pmem driver - " |
| 2603 | "duplicate registration of %s!\n", |
| 2604 | __func__, pdata->name); |
| 2605 | goto err_no_mem; |
| 2606 | } |
| 2607 | |
| 2608 | pmem[id].allocator_type = pdata->allocator_type; |
| 2609 | |
| 2610 | /* 'quantum' is a "hidden" variable that defaults to 0 in the board |
| 2611 | * files */ |
| 2612 | pmem[id].quantum = pdata->quantum ?: PMEM_MIN_ALLOC; |
| 2613 | if (pmem[id].quantum < PMEM_MIN_ALLOC || |
| 2614 | !is_power_of_2(pmem[id].quantum)) { |
| 2615 | pr_alert("pmem: %s: unable to register pmem driver %s - " |
| 2616 | "invalid quantum value (%#x)!\n", |
| 2617 | __func__, pdata->name, pmem[id].quantum); |
| 2618 | goto err_reset_pmem_info; |
| 2619 | } |
| 2620 | |
| 2621 | if (pdata->size % pmem[id].quantum) { |
| 2622 | /* bad alignment for size! */ |
| 2623 | pr_alert("pmem: %s: Unable to register driver %s - " |
| 2624 | "memory region size (%#lx) is not a multiple of " |
| 2625 | "quantum size(%#x)!\n", __func__, pdata->name, |
| 2626 | pdata->size, pmem[id].quantum); |
| 2627 | goto err_reset_pmem_info; |
| 2628 | } |
| 2629 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2630 | pmem[id].cached = pdata->cached; |
| 2631 | pmem[id].buffered = pdata->buffered; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2632 | pmem[id].size = pdata->size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2633 | pmem[id].memory_type = pdata->memory_type; |
| 2634 | strlcpy(pmem[id].name, pdata->name, PMEM_NAME_SIZE); |
| 2635 | |
| 2636 | pmem[id].num_entries = pmem[id].size / pmem[id].quantum; |
| 2637 | |
| 2638 | memset(&pmem[id].kobj, 0, sizeof(pmem[0].kobj)); |
| 2639 | pmem[id].kobj.kset = pmem_kset; |
| 2640 | |
| 2641 | switch (pmem[id].allocator_type) { |
| 2642 | case PMEM_ALLOCATORTYPE_ALLORNOTHING: |
| 2643 | pmem[id].allocate = pmem_allocator_all_or_nothing; |
| 2644 | pmem[id].free = pmem_free_all_or_nothing; |
| 2645 | pmem[id].free_space = pmem_free_space_all_or_nothing; |
| 2646 | pmem[id].len = pmem_len_all_or_nothing; |
| 2647 | pmem[id].start_addr = pmem_start_addr_all_or_nothing; |
| 2648 | pmem[id].num_entries = 1; |
| 2649 | pmem[id].quantum = pmem[id].size; |
| 2650 | pmem[id].allocator.all_or_nothing.allocated = 0; |
| 2651 | |
| 2652 | if (kobject_init_and_add(&pmem[id].kobj, |
| 2653 | &pmem_allornothing_ktype, NULL, |
| 2654 | "%s", pdata->name)) |
| 2655 | goto out_put_kobj; |
| 2656 | |
| 2657 | break; |
| 2658 | |
| 2659 | case PMEM_ALLOCATORTYPE_BUDDYBESTFIT: |
| 2660 | pmem[id].allocator.buddy_bestfit.buddy_bitmap = kmalloc( |
| 2661 | pmem[id].num_entries * sizeof(struct pmem_bits), |
| 2662 | GFP_KERNEL); |
| 2663 | if (!pmem[id].allocator.buddy_bestfit.buddy_bitmap) |
| 2664 | goto err_reset_pmem_info; |
| 2665 | |
| 2666 | memset(pmem[id].allocator.buddy_bestfit.buddy_bitmap, 0, |
| 2667 | sizeof(struct pmem_bits) * pmem[id].num_entries); |
| 2668 | |
| 2669 | for (i = sizeof(pmem[id].num_entries) * 8 - 1; i >= 0; i--) |
| 2670 | if ((pmem[id].num_entries) & 1<<i) { |
| 2671 | PMEM_BUDDY_ORDER(id, index) = i; |
| 2672 | index = PMEM_BUDDY_NEXT_INDEX(id, index); |
| 2673 | } |
| 2674 | pmem[id].allocate = pmem_allocator_buddy_bestfit; |
| 2675 | pmem[id].free = pmem_free_buddy_bestfit; |
| 2676 | pmem[id].free_space = pmem_free_space_buddy_bestfit; |
| 2677 | pmem[id].len = pmem_len_buddy_bestfit; |
| 2678 | pmem[id].start_addr = pmem_start_addr_buddy_bestfit; |
| 2679 | if (kobject_init_and_add(&pmem[id].kobj, |
| 2680 | &pmem_buddy_bestfit_ktype, NULL, |
| 2681 | "%s", pdata->name)) |
| 2682 | goto out_put_kobj; |
| 2683 | |
| 2684 | break; |
| 2685 | |
| 2686 | case PMEM_ALLOCATORTYPE_BITMAP: /* 0, default if not explicit */ |
| 2687 | pmem[id].allocator.bitmap.bitm_alloc = kmalloc( |
| 2688 | PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS * |
| 2689 | sizeof(*pmem[id].allocator.bitmap.bitm_alloc), |
| 2690 | GFP_KERNEL); |
| 2691 | if (!pmem[id].allocator.bitmap.bitm_alloc) { |
| 2692 | pr_alert("pmem: %s: Unable to register pmem " |
| 2693 | "driver %s - can't allocate " |
| 2694 | "bitm_alloc!\n", |
| 2695 | __func__, pdata->name); |
| 2696 | goto err_reset_pmem_info; |
| 2697 | } |
| 2698 | |
| 2699 | if (kobject_init_and_add(&pmem[id].kobj, |
| 2700 | &pmem_bitmap_ktype, NULL, |
| 2701 | "%s", pdata->name)) |
| 2702 | goto out_put_kobj; |
| 2703 | |
| 2704 | for (i = 0; i < PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS; i++) { |
| 2705 | pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1; |
| 2706 | pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0; |
| 2707 | } |
| 2708 | |
| 2709 | pmem[id].allocator.bitmap.bitmap_allocs = |
| 2710 | PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS; |
| 2711 | |
| 2712 | pmem[id].allocator.bitmap.bitmap = |
| 2713 | kcalloc((pmem[id].num_entries + 31) / 32, |
| 2714 | sizeof(unsigned int), GFP_KERNEL); |
| 2715 | if (!pmem[id].allocator.bitmap.bitmap) { |
| 2716 | pr_alert("pmem: %s: Unable to register pmem " |
| 2717 | "driver - can't allocate bitmap!\n", |
| 2718 | __func__); |
| 2719 | goto err_cant_register_device; |
| 2720 | } |
| 2721 | pmem[id].allocator.bitmap.bitmap_free = pmem[id].num_entries; |
| 2722 | |
| 2723 | pmem[id].allocate = pmem_allocator_bitmap; |
| 2724 | pmem[id].free = pmem_free_bitmap; |
| 2725 | pmem[id].free_space = pmem_free_space_bitmap; |
| 2726 | pmem[id].len = pmem_len_bitmap; |
| 2727 | pmem[id].start_addr = pmem_start_addr_bitmap; |
| 2728 | |
| 2729 | DLOG("bitmap allocator id %d (%s), num_entries %u, raw size " |
| 2730 | "%lu, quanta size %u\n", |
| 2731 | id, pdata->name, pmem[id].allocator.bitmap.bitmap_free, |
| 2732 | pmem[id].size, pmem[id].quantum); |
| 2733 | break; |
| 2734 | |
| 2735 | case PMEM_ALLOCATORTYPE_SYSTEM: |
| 2736 | |
| 2737 | INIT_LIST_HEAD(&pmem[id].allocator.system_mem.alist); |
| 2738 | |
| 2739 | pmem[id].allocator.system_mem.used = 0; |
| 2740 | pmem[id].vbase = NULL; |
| 2741 | |
| 2742 | if (kobject_init_and_add(&pmem[id].kobj, |
| 2743 | &pmem_system_ktype, NULL, |
| 2744 | "%s", pdata->name)) |
| 2745 | goto out_put_kobj; |
| 2746 | |
| 2747 | pmem[id].allocate = pmem_allocator_system; |
| 2748 | pmem[id].free = pmem_free_system; |
| 2749 | pmem[id].free_space = pmem_free_space_system; |
| 2750 | pmem[id].len = pmem_len_system; |
| 2751 | pmem[id].start_addr = pmem_start_addr_system; |
| 2752 | pmem[id].num_entries = 0; |
| 2753 | pmem[id].quantum = PAGE_SIZE; |
| 2754 | |
| 2755 | DLOG("system allocator id %d (%s), raw size %lu\n", |
| 2756 | id, pdata->name, pmem[id].size); |
| 2757 | break; |
| 2758 | |
| 2759 | default: |
| 2760 | pr_alert("Invalid allocator type (%d) for pmem driver\n", |
| 2761 | pdata->allocator_type); |
| 2762 | goto err_reset_pmem_info; |
| 2763 | } |
| 2764 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2765 | pmem[id].ioctl = ioctl; |
| 2766 | pmem[id].release = release; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2767 | mutex_init(&pmem[id].arena_mutex); |
| 2768 | mutex_init(&pmem[id].data_list_mutex); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2769 | INIT_LIST_HEAD(&pmem[id].data_list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2770 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2771 | pmem[id].dev.name = pdata->name; |
| 2772 | pmem[id].dev.minor = id; |
| 2773 | pmem[id].dev.fops = &pmem_fops; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2774 | pr_info("pmem: Initializing %s (user-space) as %s\n", |
| 2775 | pdata->name, pdata->cached ? "cached" : "non-cached"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2776 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2777 | if (misc_register(&pmem[id].dev)) { |
| 2778 | pr_alert("Unable to register pmem driver!\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2779 | goto err_cant_register_device; |
| 2780 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2781 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2782 | pmem[id].base = allocate_contiguous_memory_nomap(pmem[id].size, |
| 2783 | pmem[id].memory_type, PAGE_SIZE); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2784 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2785 | pr_info("allocating %lu bytes at %p (%lx physical) for %s\n", |
| 2786 | pmem[id].size, pmem[id].vbase, pmem[id].base, pmem[id].name); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2787 | |
Naveen Ramaraj | 189f188 | 2011-08-16 17:39:22 -0700 | [diff] [blame] | 2788 | pmem[id].map_on_demand = pdata->map_on_demand; |
| 2789 | if (pmem[id].map_on_demand) { |
| 2790 | pmem_vma = get_vm_area(pmem[id].size, VM_IOREMAP); |
| 2791 | if (!pmem_vma) { |
| 2792 | pr_err("pmem: Failed to allocate virtual space for " |
| 2793 | "%s\n", pdata->name); |
| 2794 | goto out_put_kobj; |
| 2795 | } |
| 2796 | pr_err("pmem: Reserving virtual address range %lx - %lx for" |
| 2797 | " %s\n", (unsigned long) pmem_vma->addr, |
| 2798 | (unsigned long) pmem_vma->addr + pmem[id].size, |
| 2799 | pdata->name); |
| 2800 | pmem[id].area = pmem_vma; |
| 2801 | } else |
| 2802 | pmem[id].area = NULL; |
| 2803 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2804 | pmem[id].garbage_pfn = page_to_pfn(alloc_page(GFP_KERNEL)); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 2805 | atomic_set(&pmem[id].allocation_cnt, 0); |
Laura Abbott | 1e36a02 | 2011-06-22 17:08:13 -0700 | [diff] [blame] | 2806 | |
| 2807 | if (pdata->setup_region) |
| 2808 | pmem[id].region_data = pdata->setup_region(); |
| 2809 | |
| 2810 | if (pdata->request_region) |
| 2811 | pmem[id].mem_request = pdata->request_region; |
| 2812 | |
| 2813 | if (pdata->release_region) |
| 2814 | pmem[id].mem_release = pdata->release_region; |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2815 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2816 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2817 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2818 | err_cant_register_device: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2819 | out_put_kobj: |
| 2820 | kobject_put(&pmem[id].kobj); |
| 2821 | if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BUDDYBESTFIT) |
| 2822 | kfree(pmem[id].allocator.buddy_bestfit.buddy_bitmap); |
| 2823 | else if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BITMAP) { |
| 2824 | kfree(pmem[id].allocator.bitmap.bitmap); |
| 2825 | kfree(pmem[id].allocator.bitmap.bitm_alloc); |
| 2826 | } |
| 2827 | err_reset_pmem_info: |
| 2828 | pmem[id].allocate = 0; |
| 2829 | pmem[id].dev.minor = -1; |
| 2830 | err_no_mem: |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2831 | return -1; |
| 2832 | } |
| 2833 | |
| 2834 | static int pmem_probe(struct platform_device *pdev) |
| 2835 | { |
| 2836 | struct android_pmem_platform_data *pdata; |
| 2837 | |
| 2838 | if (!pdev || !pdev->dev.platform_data) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2839 | pr_alert("Unable to probe pmem!\n"); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2840 | return -1; |
| 2841 | } |
| 2842 | pdata = pdev->dev.platform_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2843 | |
| 2844 | pm_runtime_set_active(&pdev->dev); |
| 2845 | pm_runtime_enable(&pdev->dev); |
| 2846 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2847 | return pmem_setup(pdata, NULL, NULL); |
| 2848 | } |
| 2849 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2850 | static int pmem_remove(struct platform_device *pdev) |
| 2851 | { |
| 2852 | int id = pdev->id; |
| 2853 | __free_page(pfn_to_page(pmem[id].garbage_pfn)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2854 | pm_runtime_disable(&pdev->dev); |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2855 | misc_deregister(&pmem[id].dev); |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2859 | static int pmem_runtime_suspend(struct device *dev) |
| 2860 | { |
| 2861 | dev_dbg(dev, "pm_runtime: suspending...\n"); |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | static int pmem_runtime_resume(struct device *dev) |
| 2866 | { |
| 2867 | dev_dbg(dev, "pm_runtime: resuming...\n"); |
| 2868 | return 0; |
| 2869 | } |
| 2870 | |
| 2871 | static const struct dev_pm_ops pmem_dev_pm_ops = { |
| 2872 | .runtime_suspend = pmem_runtime_suspend, |
| 2873 | .runtime_resume = pmem_runtime_resume, |
| 2874 | }; |
| 2875 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2876 | static struct platform_driver pmem_driver = { |
| 2877 | .probe = pmem_probe, |
| 2878 | .remove = pmem_remove, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2879 | .driver = { .name = "android_pmem", |
| 2880 | .pm = &pmem_dev_pm_ops, |
| 2881 | } |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2882 | }; |
| 2883 | |
| 2884 | |
| 2885 | static int __init pmem_init(void) |
| 2886 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2887 | /* create /sys/kernel/<PMEM_SYSFS_DIR_NAME> directory */ |
| 2888 | pmem_kset = kset_create_and_add(PMEM_SYSFS_DIR_NAME, |
| 2889 | NULL, kernel_kobj); |
| 2890 | if (!pmem_kset) { |
| 2891 | pr_err("pmem(%s):kset_create_and_add fail\n", __func__); |
| 2892 | return -ENOMEM; |
| 2893 | } |
| 2894 | |
Rebecca Schultz | a4ff0e8 | 2008-07-24 11:22:53 -0700 | [diff] [blame] | 2895 | return platform_driver_register(&pmem_driver); |
| 2896 | } |
| 2897 | |
| 2898 | static void __exit pmem_exit(void) |
| 2899 | { |
| 2900 | platform_driver_unregister(&pmem_driver); |
| 2901 | } |
| 2902 | |
| 2903 | module_init(pmem_init); |
| 2904 | module_exit(pmem_exit); |
| 2905 | |