blob: e91db7ad0f6926153e977bd791ae6cb30a7457c0 [file] [log] [blame]
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001/* drivers/android/pmem.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Duy Truong790f06d2013-02-13 16:38:12 -08004 * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07005 *
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
Steve Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/export.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070018#include <linux/miscdevice.h>
19#include <linux/platform_device.h>
20#include <linux/fs.h>
21#include <linux/file.h>
Laura Abbott511edaf2011-12-14 13:34:53 -080022#include <linux/fmem.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070023#include <linux/mm.h>
24#include <linux/list.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070025#include <linux/debugfs.h>
26#include <linux/android_pmem.h>
27#include <linux/mempolicy.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070028#include <linux/sched.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <linux/kobject.h>
Naveen Ramaraj189f1882011-08-16 17:39:22 -070030#include <linux/pm_runtime.h>
31#include <linux/memory_alloc.h>
32#include <linux/vmalloc.h>
33#include <linux/io.h>
34#include <linux/mm_types.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070035#include <asm/io.h>
36#include <asm/uaccess.h>
37#include <asm/cacheflush.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038#include <asm/sizes.h>
Naveen Ramaraj189f1882011-08-16 17:39:22 -070039#include <asm/mach/map.h>
40#include <asm/page.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070041
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#define PMEM_MAX_DEVICES (10)
43
44#define PMEM_MAX_ORDER (128)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070045#define PMEM_MIN_ALLOC PAGE_SIZE
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#define PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS (64)
48
49#define PMEM_32BIT_WORD_ORDER (5)
50#define PMEM_BITS_PER_WORD_MASK (BITS_PER_LONG - 1)
51
52#ifdef CONFIG_ANDROID_PMEM_DEBUG
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070053#define PMEM_DEBUG 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054#else
55#define PMEM_DEBUG 0
56#endif
57
58#define SYSTEM_ALLOC_RETRY 10
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070059
60/* indicates that a refernce to this file has been taken via get_pmem_file,
61 * the file should not be released until put_pmem_file is called */
62#define PMEM_FLAGS_BUSY 0x1
63/* indicates that this is a suballocation of a larger master range */
64#define PMEM_FLAGS_CONNECTED 0x1 << 1
65/* indicates this is a master and not a sub allocation and that it is mmaped */
66#define PMEM_FLAGS_MASTERMAP 0x1 << 2
67/* submap and unsubmap flags indicate:
68 * 00: subregion has never been mmaped
69 * 10: subregion has been mmaped, reference to the mm was taken
70 * 11: subretion has ben released, refernece to the mm still held
71 * 01: subretion has been released, reference to the mm has been released
72 */
73#define PMEM_FLAGS_SUBMAP 0x1 << 3
74#define PMEM_FLAGS_UNSUBMAP 0x1 << 4
75
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070076struct pmem_data {
77 /* in alloc mode: an index into the bitmap
78 * in no_alloc mode: the size of the allocation */
79 int index;
80 /* see flags above for descriptions */
81 unsigned int flags;
82 /* protects this data field, if the mm_mmap sem will be held at the
83 * same time as this sem, the mm sem must be taken first (as this is
84 * the order for vma_open and vma_close ops */
85 struct rw_semaphore sem;
86 /* info about the mmaping process */
87 struct vm_area_struct *vma;
88 /* task struct of the mapping process */
89 struct task_struct *task;
90 /* process id of teh mapping process */
91 pid_t pid;
92 /* file descriptor of the master */
93 int master_fd;
94 /* file struct of the master */
95 struct file *master_file;
96 /* a list of currently available regions if this is a suballocation */
97 struct list_head region_list;
98 /* a linked list of data so we can access them for debugging */
99 struct list_head list;
100#if PMEM_DEBUG
101 int ref;
102#endif
103};
104
105struct pmem_bits {
106 unsigned allocated:1; /* 1 if allocated, 0 if free */
107 unsigned order:7; /* size of the region in pmem space */
108};
109
110struct pmem_region_node {
111 struct pmem_region region;
112 struct list_head list;
113};
114
115#define PMEM_DEBUG_MSGS 0
116#if PMEM_DEBUG_MSGS
117#define DLOG(fmt,args...) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 do { pr_debug("[%s:%s:%d] "fmt, __FILE__, __func__, __LINE__, \
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700119 ##args); } \
120 while (0)
121#else
122#define DLOG(x...) do {} while (0)
123#endif
124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125enum pmem_align {
126 PMEM_ALIGN_4K,
127 PMEM_ALIGN_1M,
128};
129
130#define PMEM_NAME_SIZE 16
131
132struct alloc_list {
133 void *addr; /* physical addr of allocation */
134 void *aaddr; /* aligned physical addr */
135 unsigned int size; /* total size of allocation */
136 unsigned char __iomem *vaddr; /* Virtual addr */
137 struct list_head allocs;
138};
139
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700140struct pmem_info {
141 struct miscdevice dev;
142 /* physical start address of the remaped pmem space */
143 unsigned long base;
144 /* vitual start address of the remaped pmem space */
145 unsigned char __iomem *vbase;
146 /* total size of the pmem space */
147 unsigned long size;
148 /* number of entries in the pmem space */
149 unsigned long num_entries;
150 /* pfn of the garbage page in memory */
151 unsigned long garbage_pfn;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 /* which memory type (i.e. SMI, EBI1) this PMEM device is backed by */
153 unsigned memory_type;
154
155 char name[PMEM_NAME_SIZE];
156
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700157 /* index of the garbage page in the pmem space */
158 int garbage_index;
Naveen Ramaraj189f1882011-08-16 17:39:22 -0700159 /* reserved virtual address range */
160 struct vm_struct *area;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161
162 enum pmem_allocator_type allocator_type;
163
164 int (*allocate)(const int,
165 const unsigned long,
166 const unsigned int);
167 int (*free)(int, int);
168 int (*free_space)(int, struct pmem_freespace *);
169 unsigned long (*len)(int, struct pmem_data *);
170 unsigned long (*start_addr)(int, struct pmem_data *);
171
172 /* actual size of memory element, e.g.: (4 << 10) is 4K */
173 unsigned int quantum;
174
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700175 /* indicates maps of this region should be cached, if a mix of
176 * cached and uncached is desired, set this and open the device with
177 * O_SYNC to get an uncached region */
178 unsigned cached;
179 unsigned buffered;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 union {
181 struct {
182 /* in all_or_nothing allocator mode the first mapper
183 * gets the whole space and sets this flag */
184 unsigned allocated;
185 } all_or_nothing;
186
187 struct {
188 /* the buddy allocator bitmap for the region
189 * indicating which entries are allocated and which
190 * are free.
191 */
192
193 struct pmem_bits *buddy_bitmap;
194 } buddy_bestfit;
195
196 struct {
197 unsigned int bitmap_free; /* # of zero bits/quanta */
198 uint32_t *bitmap;
199 int32_t bitmap_allocs;
200 struct {
201 short bit;
202 unsigned short quanta;
203 } *bitm_alloc;
204 } bitmap;
205
206 struct {
207 unsigned long used; /* Bytes currently allocated */
208 struct list_head alist; /* List of allocations */
209 } system_mem;
210 } allocator;
211
212 int id;
213 struct kobject kobj;
214
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700215 /* for debugging, creates a list of pmem file structs, the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 * data_list_mutex should be taken before pmem_data->sem if both are
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700217 * needed */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 struct mutex data_list_mutex;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700219 struct list_head data_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 /* arena_mutex protects the global allocation arena
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700221 *
222 * IF YOU TAKE BOTH LOCKS TAKE THEM IN THIS ORDER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 * down(pmem_data->sem) => mutex_lock(arena_mutex)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700224 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 struct mutex arena_mutex;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700226
227 long (*ioctl)(struct file *, unsigned int, unsigned long);
228 int (*release)(struct inode *, struct file *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700229 /* reference count of allocations */
230 atomic_t allocation_cnt;
231 /*
232 * request function for a region when the allocation count goes
233 * from 0 -> 1
234 */
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800235 int (*mem_request)(void *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700236 /*
237 * release function for a region when the allocation count goes
238 * from 1 -> 0
239 */
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800240 int (*mem_release)(void *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700241 /*
242 * private data for the request/release callback
243 */
244 void *region_data;
245 /*
246 * map and unmap as needed
247 */
248 int map_on_demand;
Laura Abbott511edaf2011-12-14 13:34:53 -0800249 /*
250 * memory will be reused through fmem
251 */
252 int reusable;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700253};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254#define to_pmem_info_id(a) (container_of(a, struct pmem_info, kobj)->id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700255
Laura Abbott1e36a022011-06-22 17:08:13 -0700256static void ioremap_pmem(int id);
257static void pmem_put_region(int id);
258static int pmem_get_region(int id);
259
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700260static struct pmem_info pmem[PMEM_MAX_DEVICES];
261static int id_count;
262
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263#define PMEM_SYSFS_DIR_NAME "pmem_regions" /* under /sys/kernel/ */
264static struct kset *pmem_kset;
265
266#define PMEM_IS_FREE_BUDDY(id, index) \
267 (!(pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].allocated))
268#define PMEM_BUDDY_ORDER(id, index) \
269 (pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].order)
270#define PMEM_BUDDY_INDEX(id, index) \
271 (index ^ (1 << PMEM_BUDDY_ORDER(id, index)))
272#define PMEM_BUDDY_NEXT_INDEX(id, index) \
273 (index + (1 << PMEM_BUDDY_ORDER(id, index)))
274#define PMEM_OFFSET(index) (index * pmem[id].quantum)
275#define PMEM_START_ADDR(id, index) \
276 (PMEM_OFFSET(index) + pmem[id].base)
277#define PMEM_BUDDY_LEN(id, index) \
278 ((1 << PMEM_BUDDY_ORDER(id, index)) * pmem[id].quantum)
279#define PMEM_END_ADDR(id, index) \
280 (PMEM_START_ADDR(id, index) + PMEM_LEN(id, index))
281#define PMEM_START_VADDR(id, index) \
282 (PMEM_OFFSET(id, index) + pmem[id].vbase)
283#define PMEM_END_VADDR(id, index) \
284 (PMEM_START_VADDR(id, index) + PMEM_LEN(id, index))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700285#define PMEM_REVOKED(data) (data->flags & PMEM_FLAGS_REVOKED)
286#define PMEM_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287#define PMEM_IS_SUBMAP(data) \
288 ((data->flags & PMEM_FLAGS_SUBMAP) && \
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700289 (!(data->flags & PMEM_FLAGS_UNSUBMAP)))
290
291static int pmem_release(struct inode *, struct file *);
292static int pmem_mmap(struct file *, struct vm_area_struct *);
293static int pmem_open(struct inode *, struct file *);
294static long pmem_ioctl(struct file *, unsigned int, unsigned long);
295
296struct file_operations pmem_fops = {
297 .release = pmem_release,
298 .mmap = pmem_mmap,
299 .open = pmem_open,
300 .unlocked_ioctl = pmem_ioctl,
301};
302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303#define PMEM_ATTR(_name, _mode, _show, _store) { \
304 .attr = {.name = __stringify(_name), .mode = _mode }, \
305 .show = _show, \
306 .store = _store, \
307}
308
309struct pmem_attr {
310 struct attribute attr;
311 ssize_t(*show) (const int id, char * const);
312 ssize_t(*store) (const int id, const char * const, const size_t count);
313};
314#define to_pmem_attr(a) container_of(a, struct pmem_attr, attr)
315
316#define RW_PMEM_ATTR(name) \
317static struct pmem_attr pmem_attr_## name = \
318 PMEM_ATTR(name, S_IRUGO | S_IWUSR, show_pmem_## name, store_pmem_## name)
319
320#define RO_PMEM_ATTR(name) \
321static struct pmem_attr pmem_attr_## name = \
322 PMEM_ATTR(name, S_IRUGO, show_pmem_## name, NULL)
323
324#define WO_PMEM_ATTR(name) \
325static struct pmem_attr pmem_attr_## name = \
326 PMEM_ATTR(name, S_IWUSR, NULL, store_pmem_## name)
327
328static ssize_t show_pmem(struct kobject *kobj,
329 struct attribute *attr,
330 char *buf)
331{
332 struct pmem_attr *a = to_pmem_attr(attr);
333 return a->show ? a->show(to_pmem_info_id(kobj), buf) : -EIO;
334}
335
336static ssize_t store_pmem(struct kobject *kobj, struct attribute *attr,
337 const char *buf, size_t count)
338{
339 struct pmem_attr *a = to_pmem_attr(attr);
340 return a->store ? a->store(to_pmem_info_id(kobj), buf, count) : -EIO;
341}
342
343static struct sysfs_ops pmem_ops = {
344 .show = show_pmem,
345 .store = store_pmem,
346};
347
348static ssize_t show_pmem_base(int id, char *buf)
349{
350 return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n",
351 pmem[id].base, pmem[id].base);
352}
353RO_PMEM_ATTR(base);
354
355static ssize_t show_pmem_size(int id, char *buf)
356{
357 return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n",
358 pmem[id].size, pmem[id].size);
359}
360RO_PMEM_ATTR(size);
361
362static ssize_t show_pmem_allocator_type(int id, char *buf)
363{
364 switch (pmem[id].allocator_type) {
365 case PMEM_ALLOCATORTYPE_ALLORNOTHING:
366 return scnprintf(buf, PAGE_SIZE, "%s\n", "All or Nothing");
367 case PMEM_ALLOCATORTYPE_BUDDYBESTFIT:
368 return scnprintf(buf, PAGE_SIZE, "%s\n", "Buddy Bestfit");
369 case PMEM_ALLOCATORTYPE_BITMAP:
370 return scnprintf(buf, PAGE_SIZE, "%s\n", "Bitmap");
371 case PMEM_ALLOCATORTYPE_SYSTEM:
372 return scnprintf(buf, PAGE_SIZE, "%s\n", "System heap");
373 default:
374 return scnprintf(buf, PAGE_SIZE,
375 "??? Invalid allocator type (%d) for this region! "
376 "Something isn't right.\n",
377 pmem[id].allocator_type);
378 }
379}
380RO_PMEM_ATTR(allocator_type);
381
382static ssize_t show_pmem_mapped_regions(int id, char *buf)
383{
384 struct list_head *elt;
385 int ret;
386
387 ret = scnprintf(buf, PAGE_SIZE,
388 "pid #: mapped regions (offset, len) (offset,len)...\n");
389
390 mutex_lock(&pmem[id].data_list_mutex);
391 list_for_each(elt, &pmem[id].data_list) {
392 struct pmem_data *data =
393 list_entry(elt, struct pmem_data, list);
394 struct list_head *elt2;
395
396 down_read(&data->sem);
397 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "pid %u:",
398 data->pid);
399 list_for_each(elt2, &data->region_list) {
400 struct pmem_region_node *region_node = list_entry(elt2,
401 struct pmem_region_node,
402 list);
403 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
404 "(%lx,%lx) ",
405 region_node->region.offset,
406 region_node->region.len);
407 }
408 up_read(&data->sem);
409 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
410 }
411 mutex_unlock(&pmem[id].data_list_mutex);
412 return ret;
413}
414RO_PMEM_ATTR(mapped_regions);
415
416#define PMEM_COMMON_SYSFS_ATTRS \
417 &pmem_attr_base.attr, \
418 &pmem_attr_size.attr, \
419 &pmem_attr_allocator_type.attr, \
420 &pmem_attr_mapped_regions.attr
421
422
423static ssize_t show_pmem_allocated(int id, char *buf)
424{
425 ssize_t ret;
426
427 mutex_lock(&pmem[id].arena_mutex);
428 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
429 pmem[id].allocator.all_or_nothing.allocated ?
430 "is allocated" : "is NOT allocated");
431 mutex_unlock(&pmem[id].arena_mutex);
432 return ret;
433}
434RO_PMEM_ATTR(allocated);
435
436static struct attribute *pmem_allornothing_attrs[] = {
437 PMEM_COMMON_SYSFS_ATTRS,
438
439 &pmem_attr_allocated.attr,
440
441 NULL
442};
443
444static struct kobj_type pmem_allornothing_ktype = {
445 .sysfs_ops = &pmem_ops,
446 .default_attrs = pmem_allornothing_attrs,
447};
448
449static ssize_t show_pmem_total_entries(int id, char *buf)
450{
451 return scnprintf(buf, PAGE_SIZE, "%lu\n", pmem[id].num_entries);
452}
453RO_PMEM_ATTR(total_entries);
454
455static ssize_t show_pmem_quantum_size(int id, char *buf)
456{
457 return scnprintf(buf, PAGE_SIZE, "%u (%#x)\n",
458 pmem[id].quantum, pmem[id].quantum);
459}
460RO_PMEM_ATTR(quantum_size);
461
462static ssize_t show_pmem_buddy_bitmap_dump(int id, char *buf)
463{
464 int ret, i;
465
466 mutex_lock(&pmem[id].data_list_mutex);
467 ret = scnprintf(buf, PAGE_SIZE, "index\torder\tlength\tallocated\n");
468
469 for (i = 0; i < pmem[id].num_entries && (PAGE_SIZE - ret);
470 i = PMEM_BUDDY_NEXT_INDEX(id, i))
471 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%d\t%d\t%d\t%d\n",
472 i, PMEM_BUDDY_ORDER(id, i),
473 PMEM_BUDDY_LEN(id, i),
474 !PMEM_IS_FREE_BUDDY(id, i));
475
476 mutex_unlock(&pmem[id].data_list_mutex);
477 return ret;
478}
479RO_PMEM_ATTR(buddy_bitmap_dump);
480
481#define PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS \
482 &pmem_attr_quantum_size.attr, \
483 &pmem_attr_total_entries.attr
484
485static struct attribute *pmem_buddy_bestfit_attrs[] = {
486 PMEM_COMMON_SYSFS_ATTRS,
487
488 PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS,
489
490 &pmem_attr_buddy_bitmap_dump.attr,
491
492 NULL
493};
494
495static struct kobj_type pmem_buddy_bestfit_ktype = {
496 .sysfs_ops = &pmem_ops,
497 .default_attrs = pmem_buddy_bestfit_attrs,
498};
499
500static ssize_t show_pmem_free_quanta(int id, char *buf)
501{
502 ssize_t ret;
503
504 mutex_lock(&pmem[id].arena_mutex);
505 ret = scnprintf(buf, PAGE_SIZE, "%u\n",
506 pmem[id].allocator.bitmap.bitmap_free);
507 mutex_unlock(&pmem[id].arena_mutex);
508 return ret;
509}
510RO_PMEM_ATTR(free_quanta);
511
512static ssize_t show_pmem_bits_allocated(int id, char *buf)
513{
514 ssize_t ret;
515 unsigned int i;
516
517 mutex_lock(&pmem[id].arena_mutex);
518
519 ret = scnprintf(buf, PAGE_SIZE,
520 "id: %d\nbitnum\tindex\tquanta allocated\n", id);
521
522 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++)
523 if (pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1)
524 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
525 "%u\t%u\t%u\n",
526 i,
527 pmem[id].allocator.bitmap.bitm_alloc[i].bit,
528 pmem[id].allocator.bitmap.bitm_alloc[i].quanta
529 );
530
531 mutex_unlock(&pmem[id].arena_mutex);
532 return ret;
533}
534RO_PMEM_ATTR(bits_allocated);
535
536static struct attribute *pmem_bitmap_attrs[] = {
537 PMEM_COMMON_SYSFS_ATTRS,
538
539 PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS,
540
541 &pmem_attr_free_quanta.attr,
542 &pmem_attr_bits_allocated.attr,
543
544 NULL
545};
546
547static struct attribute *pmem_system_attrs[] = {
548 PMEM_COMMON_SYSFS_ATTRS,
549
550 NULL
551};
552
553static struct kobj_type pmem_bitmap_ktype = {
554 .sysfs_ops = &pmem_ops,
555 .default_attrs = pmem_bitmap_attrs,
556};
557
558static struct kobj_type pmem_system_ktype = {
559 .sysfs_ops = &pmem_ops,
560 .default_attrs = pmem_system_attrs,
561};
562
Laura Abbott1e36a022011-06-22 17:08:13 -0700563static int pmem_allocate_from_id(const int id, const unsigned long size,
564 const unsigned int align)
565{
566 int ret;
567 ret = pmem_get_region(id);
568
569 if (ret)
570 return -1;
571
572 ret = pmem[id].allocate(id, size, align);
573
574 if (ret < 0)
575 pmem_put_region(id);
576
577 return ret;
578}
579
580static int pmem_free_from_id(const int id, const int index)
581{
582 pmem_put_region(id);
583 return pmem[id].free(id, index);
584}
585
586static int pmem_get_region(int id)
587{
588 /* Must be called with arena mutex locked */
589 atomic_inc(&pmem[id].allocation_cnt);
590 if (!pmem[id].vbase) {
591 DLOG("PMEMDEBUG: mapping for %s", pmem[id].name);
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800592 if (pmem[id].mem_request) {
593 int ret = pmem[id].mem_request(pmem[id].region_data);
594 if (ret) {
595 atomic_dec(&pmem[id].allocation_cnt);
596 return 1;
597 }
598 }
Laura Abbott1e36a022011-06-22 17:08:13 -0700599 ioremap_pmem(id);
600 }
601
602 if (pmem[id].vbase) {
603 return 0;
604 } else {
605 if (pmem[id].mem_release)
606 pmem[id].mem_release(pmem[id].region_data);
607 atomic_dec(&pmem[id].allocation_cnt);
608 return 1;
609 }
610}
611
612static void pmem_put_region(int id)
613{
614 /* Must be called with arena mutex locked */
615 if (atomic_dec_and_test(&pmem[id].allocation_cnt)) {
616 DLOG("PMEMDEBUG: unmapping for %s", pmem[id].name);
617 BUG_ON(!pmem[id].vbase);
618 if (pmem[id].map_on_demand) {
Naveen Ramaraj189f1882011-08-16 17:39:22 -0700619 /* unmap_kernel_range() flushes the caches
620 * and removes the page table entries
621 */
622 unmap_kernel_range((unsigned long)pmem[id].vbase,
623 pmem[id].size);
Laura Abbott1e36a022011-06-22 17:08:13 -0700624 pmem[id].vbase = NULL;
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800625 if (pmem[id].mem_release) {
626 int ret = pmem[id].mem_release(
627 pmem[id].region_data);
628 WARN(ret, "mem_release failed");
629 }
Laura Abbott1e36a022011-06-22 17:08:13 -0700630
631 }
632 }
633}
634
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700635static int get_id(struct file *file)
636{
637 return MINOR(file->f_dentry->d_inode->i_rdev);
638}
639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640static char *get_name(struct file *file)
641{
642 int id = get_id(file);
643 return pmem[id].name;
644}
645
646static int is_pmem_file(struct file *file)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700647{
648 int id;
649
650 if (unlikely(!file || !file->f_dentry || !file->f_dentry->d_inode))
651 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700653 id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 return (unlikely(id >= PMEM_MAX_DEVICES ||
655 file->f_dentry->d_inode->i_rdev !=
656 MKDEV(MISC_MAJOR, pmem[id].dev.minor))) ? 0 : 1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700657}
658
659static int has_allocation(struct file *file)
660{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 /* must be called with at least read lock held on
662 * ((struct pmem_data *)(file->private_data))->sem which
663 * means that file is guaranteed not to be NULL upon entry!!
664 * check is_pmem_file first if not accessed via pmem_file_ops */
665 struct pmem_data *pdata = file->private_data;
666 return pdata && pdata->index != -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700667}
668
669static int is_master_owner(struct file *file)
670{
671 struct file *master_file;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700673 int put_needed, ret = 0;
674
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 if (!has_allocation(file))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700676 return 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700677 if (PMEM_FLAGS_MASTERMAP & data->flags)
678 return 1;
679 master_file = fget_light(data->master_fd, &put_needed);
680 if (master_file && data->master_file == master_file)
681 ret = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 if (master_file)
683 fput_light(master_file, put_needed);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700684 return ret;
685}
686
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687static int pmem_free_all_or_nothing(int id, int index)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700688{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 /* caller should hold the lock on arena_mutex! */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700690 DLOG("index %d\n", index);
691
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 pmem[id].allocator.all_or_nothing.allocated = 0;
693 return 0;
694}
695
696static int pmem_free_space_all_or_nothing(int id,
697 struct pmem_freespace *fs)
698{
699 /* caller should hold the lock on arena_mutex! */
700 fs->total = (unsigned long)
701 pmem[id].allocator.all_or_nothing.allocated == 0 ?
702 pmem[id].size : 0;
703
704 fs->largest = fs->total;
705 return 0;
706}
707
708
709static int pmem_free_buddy_bestfit(int id, int index)
710{
711 /* caller should hold the lock on arena_mutex! */
712 int curr = index;
713 DLOG("index %d\n", index);
714
715
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700716 /* clean up the bitmap, merging any buddies */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 pmem[id].allocator.buddy_bestfit.buddy_bitmap[curr].allocated = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700718 /* find a slots buddy Buddy# = Slot# ^ (1 << order)
719 * if the buddy is also free merge them
720 * repeat until the buddy is not free or end of the bitmap is reached
721 */
722 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 int buddy = PMEM_BUDDY_INDEX(id, curr);
724 if (buddy < pmem[id].num_entries &&
725 PMEM_IS_FREE_BUDDY(id, buddy) &&
726 PMEM_BUDDY_ORDER(id, buddy) ==
727 PMEM_BUDDY_ORDER(id, curr)) {
728 PMEM_BUDDY_ORDER(id, buddy)++;
729 PMEM_BUDDY_ORDER(id, curr)++;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700730 curr = min(buddy, curr);
731 } else {
732 break;
733 }
734 } while (curr < pmem[id].num_entries);
735
736 return 0;
737}
738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739
740static int pmem_free_space_buddy_bestfit(int id,
741 struct pmem_freespace *fs)
742{
743 /* caller should hold the lock on arena_mutex! */
744 int curr;
745 unsigned long size;
746 fs->total = 0;
747 fs->largest = 0;
748
749 for (curr = 0; curr < pmem[id].num_entries;
750 curr = PMEM_BUDDY_NEXT_INDEX(id, curr)) {
751 if (PMEM_IS_FREE_BUDDY(id, curr)) {
752 size = PMEM_BUDDY_LEN(id, curr);
753 if (size > fs->largest)
754 fs->largest = size;
755 fs->total += size;
756 }
757 }
758 return 0;
759}
760
761
762static inline uint32_t start_mask(int bit_start)
763{
764 return (uint32_t)(~0) << (bit_start & PMEM_BITS_PER_WORD_MASK);
765}
766
767static inline uint32_t end_mask(int bit_end)
768{
769 return (uint32_t)(~0) >>
770 ((BITS_PER_LONG - bit_end) & PMEM_BITS_PER_WORD_MASK);
771}
772
773static inline int compute_total_words(int bit_end, int word_index)
774{
775 return ((bit_end + BITS_PER_LONG - 1) >>
776 PMEM_32BIT_WORD_ORDER) - word_index;
777}
778
779static void bitmap_bits_clear_all(uint32_t *bitp, int bit_start, int bit_end)
780{
781 int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words;
782
783 total_words = compute_total_words(bit_end, word_index);
784 if (total_words > 0) {
785 if (total_words == 1) {
786 bitp[word_index] &=
787 ~(start_mask(bit_start) & end_mask(bit_end));
788 } else {
789 bitp[word_index++] &= ~start_mask(bit_start);
790 if (total_words > 2) {
791 int total_bytes;
792
793 total_words -= 2;
794 total_bytes = total_words << 2;
795
796 memset(&bitp[word_index], 0, total_bytes);
797 word_index += total_words;
798 }
799 bitp[word_index] &= ~end_mask(bit_end);
800 }
801 }
802}
803
804static int pmem_free_bitmap(int id, int bitnum)
805{
806 /* caller should hold the lock on arena_mutex! */
807 int i;
808 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
809
810 DLOG("bitnum %d\n", bitnum);
811
812 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++) {
813 const int curr_bit =
814 pmem[id].allocator.bitmap.bitm_alloc[i].bit;
815
816 if (curr_bit == bitnum) {
817 const int curr_quanta =
818 pmem[id].allocator.bitmap.bitm_alloc[i].quanta;
819
820 bitmap_bits_clear_all(pmem[id].allocator.bitmap.bitmap,
821 curr_bit, curr_bit + curr_quanta);
822 pmem[id].allocator.bitmap.bitmap_free += curr_quanta;
823 pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1;
824 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
825 return 0;
826 }
827 }
828 printk(KERN_ALERT "pmem: %s: Attempt to free unallocated index %d, id"
829 " %d, pid %d(%s)\n", __func__, bitnum, id, current->pid,
830 get_task_comm(currtask_name, current));
831
832 return -1;
833}
834
835static int pmem_free_system(int id, int index)
836{
837 /* caller should hold the lock on arena_mutex! */
838 struct alloc_list *item;
839
840 DLOG("index %d\n", index);
841 if (index != 0)
842 item = (struct alloc_list *)index;
843 else
844 return 0;
845
846 if (item->vaddr != NULL) {
847 iounmap(item->vaddr);
848 kfree(__va(item->addr));
849 list_del(&item->allocs);
850 kfree(item);
851 }
852
853 return 0;
854}
855
856static int pmem_free_space_bitmap(int id, struct pmem_freespace *fs)
857{
858 int i, j;
859 int max_allocs = pmem[id].allocator.bitmap.bitmap_allocs;
860 int alloc_start = 0;
861 int next_alloc;
862 unsigned long size = 0;
863
864 fs->total = 0;
865 fs->largest = 0;
866
867 for (i = 0; i < max_allocs; i++) {
868
869 int alloc_quanta = 0;
870 int alloc_idx = 0;
871 next_alloc = pmem[id].num_entries;
872
873 /* Look for the lowest bit where next allocation starts */
874 for (j = 0; j < max_allocs; j++) {
875 const int curr_alloc = pmem[id].allocator.
876 bitmap.bitm_alloc[j].bit;
877 if (curr_alloc != -1) {
878 if (alloc_start == curr_alloc)
879 alloc_idx = j;
880 if (alloc_start >= curr_alloc)
881 continue;
882 if (curr_alloc < next_alloc)
883 next_alloc = curr_alloc;
884 }
885 }
886 alloc_quanta = pmem[id].allocator.bitmap.
887 bitm_alloc[alloc_idx].quanta;
888 size = (next_alloc - (alloc_start + alloc_quanta)) *
889 pmem[id].quantum;
890
891 if (size > fs->largest)
892 fs->largest = size;
893 fs->total += size;
894
895 if (next_alloc == pmem[id].num_entries)
896 break;
897 else
898 alloc_start = next_alloc;
899 }
900
901 return 0;
902}
903
904static int pmem_free_space_system(int id, struct pmem_freespace *fs)
905{
906 fs->total = pmem[id].size;
907 fs->largest = pmem[id].size;
908
909 return 0;
910}
911
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700912static void pmem_revoke(struct file *file, struct pmem_data *data);
913
914static int pmem_release(struct inode *inode, struct file *file)
915{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700917 struct pmem_region_node *region_node;
918 struct list_head *elt, *elt2;
919 int id = get_id(file), ret = 0;
920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921#if PMEM_DEBUG_MSGS
922 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
923#endif
924 DLOG("releasing memory pid %u(%s) file %p(%ld) dev %s(id: %d)\n",
925 current->pid, get_task_comm(currtask_name, current),
926 file, file_count(file), get_name(file), id);
927 mutex_lock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700928 /* if this file is a master, revoke all the memory in the connected
929 * files */
930 if (PMEM_FLAGS_MASTERMAP & data->flags) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700931 list_for_each(elt, &pmem[id].data_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 struct pmem_data *sub_data =
933 list_entry(elt, struct pmem_data, list);
934 int is_master;
935
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700936 down_read(&sub_data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 is_master = (PMEM_IS_SUBMAP(sub_data) &&
938 file == sub_data->master_file);
939 up_read(&sub_data->sem);
940
941 if (is_master)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700942 pmem_revoke(file, sub_data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700943 }
944 }
945 list_del(&data->list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 mutex_unlock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700947
948 down_write(&data->sem);
949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 /* if it is not a connected file and it has an allocation, free it */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700951 if (!(PMEM_FLAGS_CONNECTED & data->flags) && has_allocation(file)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -0700953 ret = pmem_free_from_id(id, data->index);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 mutex_unlock(&pmem[id].arena_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700955 }
956
957 /* if this file is a submap (mapped, connected file), downref the
958 * task struct */
959 if (PMEM_FLAGS_SUBMAP & data->flags)
960 if (data->task) {
961 put_task_struct(data->task);
962 data->task = NULL;
963 }
964
965 file->private_data = NULL;
966
967 list_for_each_safe(elt, elt2, &data->region_list) {
968 region_node = list_entry(elt, struct pmem_region_node, list);
969 list_del(elt);
970 kfree(region_node);
971 }
972 BUG_ON(!list_empty(&data->region_list));
973
974 up_write(&data->sem);
975 kfree(data);
976 if (pmem[id].release)
977 ret = pmem[id].release(inode, file);
978
979 return ret;
980}
981
982static int pmem_open(struct inode *inode, struct file *file)
983{
984 struct pmem_data *data;
985 int id = get_id(file);
986 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987#if PMEM_DEBUG_MSGS
988 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
989#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700990
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 DLOG("pid %u(%s) file %p(%ld) dev %s(id: %d)\n",
992 current->pid, get_task_comm(currtask_name, current),
993 file, file_count(file), get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700994 data = kmalloc(sizeof(struct pmem_data), GFP_KERNEL);
995 if (!data) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996 printk(KERN_ALERT "pmem: %s: unable to allocate memory for "
997 "pmem metadata.", __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700998 return -1;
999 }
1000 data->flags = 0;
1001 data->index = -1;
1002 data->task = NULL;
1003 data->vma = NULL;
1004 data->pid = 0;
1005 data->master_file = NULL;
1006#if PMEM_DEBUG
1007 data->ref = 0;
1008#endif
1009 INIT_LIST_HEAD(&data->region_list);
1010 init_rwsem(&data->sem);
1011
1012 file->private_data = data;
1013 INIT_LIST_HEAD(&data->list);
1014
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 mutex_lock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001016 list_add(&data->list, &pmem[id].data_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 mutex_unlock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001018 return ret;
1019}
1020
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021static unsigned long pmem_order(unsigned long len, int id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001022{
1023 int i;
1024
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 len = (len + pmem[id].quantum - 1)/pmem[id].quantum;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001026 len--;
1027 for (i = 0; i < sizeof(len)*8; i++)
1028 if (len >> i == 0)
1029 break;
1030 return i;
1031}
1032
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033static int pmem_allocator_all_or_nothing(const int id,
1034 const unsigned long len,
1035 const unsigned int align)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001036{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 /* caller should hold the lock on arena_mutex! */
1038 DLOG("all or nothing\n");
1039 if ((len > pmem[id].size) ||
1040 pmem[id].allocator.all_or_nothing.allocated)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001041 return -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 pmem[id].allocator.all_or_nothing.allocated = 1;
1043 return len;
1044}
1045
1046static int pmem_allocator_buddy_bestfit(const int id,
1047 const unsigned long len,
1048 unsigned int align)
1049{
1050 /* caller should hold the lock on arena_mutex! */
1051 int curr;
1052 int best_fit = -1;
1053 unsigned long order;
1054
1055 DLOG("buddy bestfit\n");
1056 order = pmem_order(len, id);
1057 if (order > PMEM_MAX_ORDER)
1058 goto out;
1059
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001060 DLOG("order %lx\n", order);
1061
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 /* Look through the bitmap.
1063 * If a free slot of the correct order is found, use it.
1064 * Otherwise, use the best fit (smallest with size > order) slot.
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001065 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 for (curr = 0;
1067 curr < pmem[id].num_entries;
1068 curr = PMEM_BUDDY_NEXT_INDEX(id, curr))
1069 if (PMEM_IS_FREE_BUDDY(id, curr)) {
1070 if (PMEM_BUDDY_ORDER(id, curr) ==
1071 (unsigned char)order) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001072 /* set the not free bit and clear others */
1073 best_fit = curr;
1074 break;
1075 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 if (PMEM_BUDDY_ORDER(id, curr) >
1077 (unsigned char)order &&
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001078 (best_fit < 0 ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079 PMEM_BUDDY_ORDER(id, curr) <
1080 PMEM_BUDDY_ORDER(id, best_fit)))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001081 best_fit = curr;
1082 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001083
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 /* if best_fit < 0, there are no suitable slots; return an error */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001085 if (best_fit < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086#if PMEM_DEBUG
1087 printk(KERN_ALERT "pmem: %s: no space left to allocate!\n",
1088 __func__);
1089#endif
1090 goto out;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001091 }
1092
1093 /* now partition the best fit:
1094 * split the slot into 2 buddies of order - 1
1095 * repeat until the slot is of the correct order
1096 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 while (PMEM_BUDDY_ORDER(id, best_fit) > (unsigned char)order) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001098 int buddy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 PMEM_BUDDY_ORDER(id, best_fit) -= 1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001100 buddy = PMEM_BUDDY_INDEX(id, best_fit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101 PMEM_BUDDY_ORDER(id, buddy) = PMEM_BUDDY_ORDER(id, best_fit);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001102 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 pmem[id].allocator.buddy_bestfit.buddy_bitmap[best_fit].allocated = 1;
1104out:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001105 return best_fit;
1106}
1107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108
1109static inline unsigned long paddr_from_bit(const int id, const int bitnum)
1110{
1111 return pmem[id].base + pmem[id].quantum * bitnum;
1112}
1113
1114static inline unsigned long bit_from_paddr(const int id,
1115 const unsigned long paddr)
1116{
1117 return (paddr - pmem[id].base) / pmem[id].quantum;
1118}
1119
1120static void bitmap_bits_set_all(uint32_t *bitp, int bit_start, int bit_end)
1121{
1122 int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words;
1123
1124 total_words = compute_total_words(bit_end, word_index);
1125 if (total_words > 0) {
1126 if (total_words == 1) {
1127 bitp[word_index] |=
1128 (start_mask(bit_start) & end_mask(bit_end));
1129 } else {
1130 bitp[word_index++] |= start_mask(bit_start);
1131 if (total_words > 2) {
1132 int total_bytes;
1133
1134 total_words -= 2;
1135 total_bytes = total_words << 2;
1136
1137 memset(&bitp[word_index], ~0, total_bytes);
1138 word_index += total_words;
1139 }
1140 bitp[word_index] |= end_mask(bit_end);
1141 }
1142 }
1143}
1144
1145static int
1146bitmap_allocate_contiguous(uint32_t *bitp, int num_bits_to_alloc,
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001147 int total_bits, int spacing, int start_bit)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148{
1149 int bit_start, last_bit, word_index;
1150
1151 if (num_bits_to_alloc <= 0)
1152 return -1;
1153
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001154 for (bit_start = start_bit; ;
1155 bit_start = ((last_bit +
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156 (word_index << PMEM_32BIT_WORD_ORDER) + spacing - 1)
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001157 & ~(spacing - 1)) + start_bit) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158 int bit_end = bit_start + num_bits_to_alloc, total_words;
1159
1160 if (bit_end > total_bits)
1161 return -1; /* out of contiguous memory */
1162
1163 word_index = bit_start >> PMEM_32BIT_WORD_ORDER;
1164 total_words = compute_total_words(bit_end, word_index);
1165
1166 if (total_words <= 0)
1167 return -1;
1168
1169 if (total_words == 1) {
1170 last_bit = fls(bitp[word_index] &
1171 (start_mask(bit_start) &
1172 end_mask(bit_end)));
1173 if (last_bit)
1174 continue;
1175 } else {
1176 int end_word = word_index + (total_words - 1);
1177 last_bit =
1178 fls(bitp[word_index] & start_mask(bit_start));
1179 if (last_bit)
1180 continue;
1181
1182 for (word_index++;
1183 word_index < end_word;
1184 word_index++) {
1185 last_bit = fls(bitp[word_index]);
1186 if (last_bit)
1187 break;
1188 }
1189 if (last_bit)
1190 continue;
1191
1192 last_bit = fls(bitp[word_index] & end_mask(bit_end));
1193 if (last_bit)
1194 continue;
1195 }
1196 bitmap_bits_set_all(bitp, bit_start, bit_end);
1197 return bit_start;
1198 }
1199 return -1;
1200}
1201
1202static int reserve_quanta(const unsigned int quanta_needed,
1203 const int id,
1204 unsigned int align)
1205{
1206 /* alignment should be a valid power of 2 */
1207 int ret = -1, start_bit = 0, spacing = 1;
1208
1209 /* Sanity check */
1210 if (quanta_needed > pmem[id].allocator.bitmap.bitmap_free) {
1211#if PMEM_DEBUG
1212 printk(KERN_ALERT "pmem: %s: request (%d) too big for"
1213 " available free (%d)\n", __func__, quanta_needed,
1214 pmem[id].allocator.bitmap.bitmap_free);
1215#endif
1216 return -1;
1217 }
1218
1219 start_bit = bit_from_paddr(id,
1220 (pmem[id].base + align - 1) & ~(align - 1));
1221 if (start_bit <= -1) {
1222#if PMEM_DEBUG
1223 printk(KERN_ALERT
1224 "pmem: %s: bit_from_paddr fails for"
1225 " %u alignment.\n", __func__, align);
1226#endif
1227 return -1;
1228 }
1229 spacing = align / pmem[id].quantum;
1230 spacing = spacing > 1 ? spacing : 1;
1231
1232 ret = bitmap_allocate_contiguous(pmem[id].allocator.bitmap.bitmap,
1233 quanta_needed,
1234 (pmem[id].size + pmem[id].quantum - 1) / pmem[id].quantum,
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001235 spacing,
1236 start_bit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237
1238#if PMEM_DEBUG
1239 if (ret < 0)
1240 printk(KERN_ALERT "pmem: %s: not enough contiguous bits free "
1241 "in bitmap! Region memory is either too fragmented or"
1242 " request is too large for available memory.\n",
1243 __func__);
1244#endif
1245
1246 return ret;
1247}
1248
1249static int pmem_allocator_bitmap(const int id,
1250 const unsigned long len,
1251 const unsigned int align)
1252{
1253 /* caller should hold the lock on arena_mutex! */
1254 int bitnum, i;
1255 unsigned int quanta_needed;
1256
1257 DLOG("bitmap id %d, len %ld, align %u\n", id, len, align);
1258 if (!pmem[id].allocator.bitmap.bitm_alloc) {
1259#if PMEM_DEBUG
1260 printk(KERN_ALERT "pmem: bitm_alloc not present! id: %d\n",
1261 id);
1262#endif
1263 return -1;
1264 }
1265
1266 quanta_needed = (len + pmem[id].quantum - 1) / pmem[id].quantum;
1267 DLOG("quantum size %u quanta needed %u free %u id %d\n",
1268 pmem[id].quantum, quanta_needed,
1269 pmem[id].allocator.bitmap.bitmap_free, id);
1270
1271 if (pmem[id].allocator.bitmap.bitmap_free < quanta_needed) {
1272#if PMEM_DEBUG
1273 printk(KERN_ALERT "pmem: memory allocation failure. "
1274 "PMEM memory region exhausted, id %d."
1275 " Unable to comply with allocation request.\n", id);
1276#endif
1277 return -1;
1278 }
1279
1280 bitnum = reserve_quanta(quanta_needed, id, align);
1281 if (bitnum == -1)
1282 goto leave;
1283
1284 for (i = 0;
1285 i < pmem[id].allocator.bitmap.bitmap_allocs &&
1286 pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1;
1287 i++)
1288 ;
1289
1290 if (i >= pmem[id].allocator.bitmap.bitmap_allocs) {
1291 void *temp;
1292 int32_t new_bitmap_allocs =
1293 pmem[id].allocator.bitmap.bitmap_allocs << 1;
1294 int j;
1295
1296 if (!new_bitmap_allocs) { /* failed sanity check!! */
1297#if PMEM_DEBUG
1298 pr_alert("pmem: bitmap_allocs number"
1299 " wrapped around to zero! Something "
1300 "is VERY wrong.\n");
1301#endif
1302 return -1;
1303 }
1304
1305 if (new_bitmap_allocs > pmem[id].num_entries) {
1306 /* failed sanity check!! */
1307#if PMEM_DEBUG
1308 pr_alert("pmem: required bitmap_allocs"
1309 " number exceeds maximum entries possible"
1310 " for current quanta\n");
1311#endif
1312 return -1;
1313 }
1314
1315 temp = krealloc(pmem[id].allocator.bitmap.bitm_alloc,
1316 new_bitmap_allocs *
1317 sizeof(*pmem[id].allocator.bitmap.bitm_alloc),
1318 GFP_KERNEL);
1319 if (!temp) {
1320#if PMEM_DEBUG
1321 pr_alert("pmem: can't realloc bitmap_allocs,"
1322 "id %d, current num bitmap allocs %d\n",
1323 id, pmem[id].allocator.bitmap.bitmap_allocs);
1324#endif
1325 return -1;
1326 }
1327 pmem[id].allocator.bitmap.bitmap_allocs = new_bitmap_allocs;
1328 pmem[id].allocator.bitmap.bitm_alloc = temp;
1329
1330 for (j = i; j < new_bitmap_allocs; j++) {
1331 pmem[id].allocator.bitmap.bitm_alloc[j].bit = -1;
1332 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
1333 }
1334
1335 DLOG("increased # of allocated regions to %d for id %d\n",
1336 pmem[id].allocator.bitmap.bitmap_allocs, id);
1337 }
1338
1339 DLOG("bitnum %d, bitm_alloc index %d\n", bitnum, i);
1340
1341 pmem[id].allocator.bitmap.bitmap_free -= quanta_needed;
1342 pmem[id].allocator.bitmap.bitm_alloc[i].bit = bitnum;
1343 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = quanta_needed;
1344leave:
1345 return bitnum;
1346}
1347
1348static int pmem_allocator_system(const int id,
1349 const unsigned long len,
1350 const unsigned int align)
1351{
1352 /* caller should hold the lock on arena_mutex! */
1353 struct alloc_list *list;
1354 unsigned long aligned_len;
1355 int count = SYSTEM_ALLOC_RETRY;
1356 void *buf;
1357
1358 DLOG("system id %d, len %ld, align %u\n", id, len, align);
1359
1360 if ((pmem[id].allocator.system_mem.used + len) > pmem[id].size) {
1361 DLOG("requested size would be larger than quota\n");
1362 return -1;
1363 }
1364
1365 /* Handle alignment */
1366 aligned_len = len + align;
1367
1368 /* Attempt allocation */
1369 list = kmalloc(sizeof(struct alloc_list), GFP_KERNEL);
1370 if (list == NULL) {
1371 printk(KERN_ERR "pmem: failed to allocate system metadata\n");
1372 return -1;
1373 }
1374 list->vaddr = NULL;
1375
1376 buf = NULL;
1377 while ((buf == NULL) && count--) {
1378 buf = kmalloc((aligned_len), GFP_KERNEL);
1379 if (buf == NULL) {
1380 DLOG("pmem: kmalloc %d temporarily failed len= %ld\n",
1381 count, aligned_len);
1382 }
1383 }
1384 if (!buf) {
1385 printk(KERN_CRIT "pmem: kmalloc failed for id= %d len= %ld\n",
1386 id, aligned_len);
1387 kfree(list);
1388 return -1;
1389 }
1390 list->size = aligned_len;
1391 list->addr = (void *)__pa(buf);
1392 list->aaddr = (void *)(((unsigned int)(list->addr) + (align - 1)) &
1393 ~(align - 1));
1394
1395 if (!pmem[id].cached)
1396 list->vaddr = ioremap(__pa(buf), aligned_len);
1397 else
1398 list->vaddr = ioremap_cached(__pa(buf), aligned_len);
1399
1400 INIT_LIST_HEAD(&list->allocs);
1401 list_add(&list->allocs, &pmem[id].allocator.system_mem.alist);
1402
1403 return (int)list;
1404}
1405
1406static pgprot_t pmem_phys_mem_access_prot(struct file *file, pgprot_t vma_prot)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001407{
1408 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409#ifdef pgprot_writecombine
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001410 if (pmem[id].cached == 0 || file->f_flags & O_SYNC)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 /* on ARMv6 and ARMv7 this expands to Normal Noncached */
1412 return pgprot_writecombine(vma_prot);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001413#endif
1414#ifdef pgprot_ext_buffered
1415 else if (pmem[id].buffered)
1416 return pgprot_ext_buffered(vma_prot);
1417#endif
1418 return vma_prot;
1419}
1420
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421static unsigned long pmem_start_addr_all_or_nothing(int id,
1422 struct pmem_data *data)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001423{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424 return PMEM_START_ADDR(id, 0);
1425}
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427static unsigned long pmem_start_addr_buddy_bestfit(int id,
1428 struct pmem_data *data)
1429{
1430 return PMEM_START_ADDR(id, data->index);
1431}
1432
1433static unsigned long pmem_start_addr_bitmap(int id, struct pmem_data *data)
1434{
1435 return data->index * pmem[id].quantum + pmem[id].base;
1436}
1437
1438static unsigned long pmem_start_addr_system(int id, struct pmem_data *data)
1439{
1440 return (unsigned long)(((struct alloc_list *)(data->index))->aaddr);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001441}
1442
1443static void *pmem_start_vaddr(int id, struct pmem_data *data)
1444{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM)
1446 return ((struct alloc_list *)(data->index))->vaddr;
1447 else
1448 return pmem[id].start_addr(id, data) - pmem[id].base + pmem[id].vbase;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001449}
1450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451static unsigned long pmem_len_all_or_nothing(int id, struct pmem_data *data)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001452{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001453 return data->index;
1454}
1455
1456static unsigned long pmem_len_buddy_bestfit(int id, struct pmem_data *data)
1457{
1458 return PMEM_BUDDY_LEN(id, data->index);
1459}
1460
1461static unsigned long pmem_len_bitmap(int id, struct pmem_data *data)
1462{
1463 int i;
1464 unsigned long ret = 0;
1465
1466 mutex_lock(&pmem[id].arena_mutex);
1467
1468 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++)
1469 if (pmem[id].allocator.bitmap.bitm_alloc[i].bit ==
1470 data->index) {
1471 ret = pmem[id].allocator.bitmap.bitm_alloc[i].quanta *
1472 pmem[id].quantum;
1473 break;
1474 }
1475
1476 mutex_unlock(&pmem[id].arena_mutex);
1477#if PMEM_DEBUG
1478 if (i >= pmem[id].allocator.bitmap.bitmap_allocs)
1479 pr_alert("pmem: %s: can't find bitnum %d in "
1480 "alloc'd array!\n", __func__, data->index);
1481#endif
1482 return ret;
1483}
1484
1485static unsigned long pmem_len_system(int id, struct pmem_data *data)
1486{
1487 unsigned long ret = 0;
1488
1489 mutex_lock(&pmem[id].arena_mutex);
1490
1491 ret = ((struct alloc_list *)data->index)->size;
1492 mutex_unlock(&pmem[id].arena_mutex);
1493
1494 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001495}
1496
1497static int pmem_map_garbage(int id, struct vm_area_struct *vma,
1498 struct pmem_data *data, unsigned long offset,
1499 unsigned long len)
1500{
1501 int i, garbage_pages = len >> PAGE_SHIFT;
1502
1503 vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP | VM_SHARED | VM_WRITE;
1504 for (i = 0; i < garbage_pages; i++) {
1505 if (vm_insert_pfn(vma, vma->vm_start + offset + (i * PAGE_SIZE),
1506 pmem[id].garbage_pfn))
1507 return -EAGAIN;
1508 }
1509 return 0;
1510}
1511
1512static int pmem_unmap_pfn_range(int id, struct vm_area_struct *vma,
1513 struct pmem_data *data, unsigned long offset,
1514 unsigned long len)
1515{
1516 int garbage_pages;
1517 DLOG("unmap offset %lx len %lx\n", offset, len);
1518
1519 BUG_ON(!PMEM_IS_PAGE_ALIGNED(len));
1520
1521 garbage_pages = len >> PAGE_SHIFT;
1522 zap_page_range(vma, vma->vm_start + offset, len, NULL);
1523 pmem_map_garbage(id, vma, data, offset, len);
1524 return 0;
1525}
1526
1527static int pmem_map_pfn_range(int id, struct vm_area_struct *vma,
1528 struct pmem_data *data, unsigned long offset,
1529 unsigned long len)
1530{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 int ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001532 DLOG("map offset %lx len %lx\n", offset, len);
1533 BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_start));
1534 BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_end));
1535 BUG_ON(!PMEM_IS_PAGE_ALIGNED(len));
1536 BUG_ON(!PMEM_IS_PAGE_ALIGNED(offset));
1537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001538 ret = io_remap_pfn_range(vma, vma->vm_start + offset,
1539 (pmem[id].start_addr(id, data) + offset) >> PAGE_SHIFT,
1540 len, vma->vm_page_prot);
1541 if (ret) {
1542#if PMEM_DEBUG
1543 pr_alert("pmem: %s: io_remap_pfn_range fails with "
1544 "return value: %d!\n", __func__, ret);
1545#endif
1546
1547 ret = -EAGAIN;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001548 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001549 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001550}
1551
1552static int pmem_remap_pfn_range(int id, struct vm_area_struct *vma,
1553 struct pmem_data *data, unsigned long offset,
1554 unsigned long len)
1555{
1556 /* hold the mm semp for the vma you are modifying when you call this */
1557 BUG_ON(!vma);
1558 zap_page_range(vma, vma->vm_start + offset, len, NULL);
1559 return pmem_map_pfn_range(id, vma, data, offset, len);
1560}
1561
1562static void pmem_vma_open(struct vm_area_struct *vma)
1563{
1564 struct file *file = vma->vm_file;
1565 struct pmem_data *data = file->private_data;
1566 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567
1568#if PMEM_DEBUG_MSGS
1569 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1570#endif
1571 DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n",
1572 get_name(file), id, current->pid,
1573 get_task_comm(currtask_name, current),
1574 current->parent->pid, file, file_count(file));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001575 /* this should never be called as we don't support copying pmem
1576 * ranges via fork */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001578 BUG_ON(!has_allocation(file));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001579 /* remap the garbage pages, forkers don't get access to the data */
1580 pmem_unmap_pfn_range(id, vma, data, 0, vma->vm_start - vma->vm_end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001581 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001582}
1583
1584static void pmem_vma_close(struct vm_area_struct *vma)
1585{
1586 struct file *file = vma->vm_file;
1587 struct pmem_data *data = file->private_data;
1588
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589#if PMEM_DEBUG_MSGS
1590 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1591#endif
1592 DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n",
1593 get_name(file), get_id(file), current->pid,
1594 get_task_comm(currtask_name, current),
1595 current->parent->pid, file, file_count(file));
1596
1597 if (unlikely(!is_pmem_file(file))) {
1598 pr_warning("pmem: something is very wrong, you are "
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001599 "closing a vm backing an allocation that doesn't "
1600 "exist!\n");
1601 return;
1602 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001604 down_write(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 if (unlikely(!has_allocation(file))) {
1606 up_write(&data->sem);
1607 pr_warning("pmem: something is very wrong, you are "
1608 "closing a vm backing an allocation that doesn't "
1609 "exist!\n");
1610 return;
1611 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001612 if (data->vma == vma) {
1613 data->vma = NULL;
1614 if ((data->flags & PMEM_FLAGS_CONNECTED) &&
1615 (data->flags & PMEM_FLAGS_SUBMAP))
1616 data->flags |= PMEM_FLAGS_UNSUBMAP;
1617 }
1618 /* the kernel is going to free this vma now anyway */
1619 up_write(&data->sem);
1620}
1621
1622static struct vm_operations_struct vm_ops = {
1623 .open = pmem_vma_open,
1624 .close = pmem_vma_close,
1625};
1626
1627static int pmem_mmap(struct file *file, struct vm_area_struct *vma)
1628{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 struct pmem_data *data = file->private_data;
Laura Abbott1e36a022011-06-22 17:08:13 -07001630 int index = -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001631 unsigned long vma_size = vma->vm_end - vma->vm_start;
1632 int ret = 0, id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633#if PMEM_DEBUG_MSGS
1634 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1635#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001636
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 if (!data) {
1638 pr_err("pmem: Invalid file descriptor, no private data\n");
1639 return -EINVAL;
1640 }
1641 DLOG("pid %u(%s) mmap vma_size %lu on dev %s(id: %d)\n", current->pid,
1642 get_task_comm(currtask_name, current), vma_size,
1643 get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001644 if (vma->vm_pgoff || !PMEM_IS_PAGE_ALIGNED(vma_size)) {
1645#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646 pr_err("pmem: mmaps must be at offset zero, aligned"
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001647 " and a multiple of pages_size.\n");
1648#endif
1649 return -EINVAL;
1650 }
1651
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001652 down_write(&data->sem);
1653 /* check this file isn't already mmaped, for submaps check this file
1654 * has never been mmaped */
1655 if ((data->flags & PMEM_FLAGS_SUBMAP) ||
1656 (data->flags & PMEM_FLAGS_UNSUBMAP)) {
1657#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 pr_err("pmem: you can only mmap a pmem file once, "
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001659 "this file is already mmaped. %x\n", data->flags);
1660#endif
1661 ret = -EINVAL;
1662 goto error;
1663 }
1664 /* if file->private_data == unalloced, alloc*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665 if (data->index == -1) {
1666 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07001667 index = pmem_allocate_from_id(id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 vma->vm_end - vma->vm_start,
1669 SZ_4K);
1670 mutex_unlock(&pmem[id].arena_mutex);
1671 /* either no space was available or an error occured */
1672 if (index == -1) {
1673 pr_err("pmem: mmap unable to allocate memory"
1674 "on %s\n", get_name(file));
1675 ret = -ENOMEM;
1676 goto error;
1677 }
1678 /* store the index of a successful allocation */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001679 data->index = index;
1680 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 if (pmem[id].len(id, data) < vma_size) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001683#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 pr_err("pmem: mmap size [%lu] does not match"
1685 " size of backing region [%lu].\n", vma_size,
1686 pmem[id].len(id, data));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001687#endif
1688 ret = -EINVAL;
1689 goto error;
1690 }
1691
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001692 vma->vm_pgoff = pmem[id].start_addr(id, data) >> PAGE_SHIFT;
1693
1694 vma->vm_page_prot = pmem_phys_mem_access_prot(file, vma->vm_page_prot);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001695
1696 if (data->flags & PMEM_FLAGS_CONNECTED) {
1697 struct pmem_region_node *region_node;
1698 struct list_head *elt;
1699 if (pmem_map_garbage(id, vma, data, 0, vma_size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 pr_alert("pmem: mmap failed in kernel!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001701 ret = -EAGAIN;
1702 goto error;
1703 }
1704 list_for_each(elt, &data->region_list) {
1705 region_node = list_entry(elt, struct pmem_region_node,
1706 list);
1707 DLOG("remapping file: %p %lx %lx\n", file,
1708 region_node->region.offset,
1709 region_node->region.len);
1710 if (pmem_remap_pfn_range(id, vma, data,
1711 region_node->region.offset,
1712 region_node->region.len)) {
1713 ret = -EAGAIN;
1714 goto error;
1715 }
1716 }
1717 data->flags |= PMEM_FLAGS_SUBMAP;
1718 get_task_struct(current->group_leader);
1719 data->task = current->group_leader;
1720 data->vma = vma;
1721#if PMEM_DEBUG
1722 data->pid = current->pid;
1723#endif
1724 DLOG("submmapped file %p vma %p pid %u\n", file, vma,
1725 current->pid);
1726 } else {
1727 if (pmem_map_pfn_range(id, vma, data, 0, vma_size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 pr_err("pmem: mmap failed in kernel!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001729 ret = -EAGAIN;
1730 goto error;
1731 }
1732 data->flags |= PMEM_FLAGS_MASTERMAP;
1733 data->pid = current->pid;
1734 }
1735 vma->vm_ops = &vm_ops;
1736error:
1737 up_write(&data->sem);
1738 return ret;
1739}
1740
1741/* the following are the api for accessing pmem regions by other drivers
1742 * from inside the kernel */
1743int get_pmem_user_addr(struct file *file, unsigned long *start,
1744 unsigned long *len)
1745{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 int ret = -1;
1747
1748 if (is_pmem_file(file)) {
1749 struct pmem_data *data = file->private_data;
1750
1751 down_read(&data->sem);
1752 if (has_allocation(file)) {
1753 if (data->vma) {
1754 *start = data->vma->vm_start;
1755 *len = data->vma->vm_end - data->vma->vm_start;
1756 } else {
1757 *start = *len = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001758#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001759 pr_err("pmem: %s: no vma present.\n",
1760 __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001761#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762 }
1763 ret = 0;
1764 }
1765 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001766 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767
1768#if PMEM_DEBUG
1769 if (ret)
1770 pr_err("pmem: %s: requested pmem data from invalid"
1771 "file.\n", __func__);
1772#endif
1773 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001774}
1775
1776int get_pmem_addr(struct file *file, unsigned long *start,
1777 unsigned long *vstart, unsigned long *len)
1778{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 int ret = -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001780
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001781 if (is_pmem_file(file)) {
1782 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001783
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784 down_read(&data->sem);
1785 if (has_allocation(file)) {
1786 int id = get_id(file);
1787
1788 *start = pmem[id].start_addr(id, data);
1789 *len = pmem[id].len(id, data);
1790 *vstart = (unsigned long)
1791 pmem_start_vaddr(id, data);
1792 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001793#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 down_write(&data->sem);
1795 data->ref++;
1796 up_write(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001797#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798 DLOG("returning start %#lx len %lu "
1799 "vstart %#lx\n",
1800 *start, *len, *vstart);
1801 ret = 0;
1802 } else {
1803 up_read(&data->sem);
1804 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001805 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001807}
1808
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809int get_pmem_file(unsigned int fd, unsigned long *start, unsigned long *vstart,
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001810 unsigned long *len, struct file **filp)
1811{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 int ret = -1;
1813 struct file *file = fget(fd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001814
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001815 if (unlikely(file == NULL)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816 pr_err("pmem: %s: requested data from file "
1817 "descriptor that doesn't exist.\n", __func__);
1818 } else {
1819#if PMEM_DEBUG_MSGS
1820 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1821#endif
1822 DLOG("filp %p rdev %d pid %u(%s) file %p(%ld)"
1823 " dev %s(id: %d)\n", filp,
1824 file->f_dentry->d_inode->i_rdev,
1825 current->pid, get_task_comm(currtask_name, current),
1826 file, file_count(file), get_name(file), get_id(file));
1827
1828 if (!get_pmem_addr(file, start, vstart, len)) {
1829 if (filp)
1830 *filp = file;
1831 ret = 0;
1832 } else {
1833 fput(file);
1834 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001835 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001837}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838EXPORT_SYMBOL(get_pmem_file);
1839
1840int get_pmem_fd(int fd, unsigned long *start, unsigned long *len)
1841{
1842 unsigned long vstart;
1843 return get_pmem_file(fd, start, &vstart, len, NULL);
1844}
1845EXPORT_SYMBOL(get_pmem_fd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001846
1847void put_pmem_file(struct file *file)
1848{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849#if PMEM_DEBUG_MSGS
1850 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001851#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852 DLOG("rdev %d pid %u(%s) file %p(%ld)" " dev %s(id: %d)\n",
1853 file->f_dentry->d_inode->i_rdev, current->pid,
1854 get_task_comm(currtask_name, current), file,
1855 file_count(file), get_name(file), get_id(file));
1856 if (is_pmem_file(file)) {
1857#if PMEM_DEBUG
1858 struct pmem_data *data = file->private_data;
1859
1860 down_write(&data->sem);
1861 if (!data->ref--) {
1862 data->ref++;
1863 pr_alert("pmem: pmem_put > pmem_get %s "
1864 "(pid %d)\n",
1865 pmem[get_id(file)].dev.name, data->pid);
1866 BUG();
1867 }
1868 up_write(&data->sem);
1869#endif
1870 fput(file);
1871 }
1872}
1873EXPORT_SYMBOL(put_pmem_file);
1874
1875void put_pmem_fd(int fd)
1876{
1877 int put_needed;
1878 struct file *file = fget_light(fd, &put_needed);
1879
1880 if (file) {
1881 put_pmem_file(file);
1882 fput_light(file, put_needed);
1883 }
1884}
1885
1886void flush_pmem_fd(int fd, unsigned long offset, unsigned long len)
1887{
1888 int fput_needed;
1889 struct file *file = fget_light(fd, &fput_needed);
1890
1891 if (file) {
1892 flush_pmem_file(file, offset, len);
1893 fput_light(file, fput_needed);
1894 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001895}
1896
1897void flush_pmem_file(struct file *file, unsigned long offset, unsigned long len)
1898{
1899 struct pmem_data *data;
1900 int id;
1901 void *vaddr;
1902 struct pmem_region_node *region_node;
1903 struct list_head *elt;
1904 void *flush_start, *flush_end;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905#ifdef CONFIG_OUTER_CACHE
1906 unsigned long phy_start, phy_end;
1907#endif
1908 if (!is_pmem_file(file))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001909 return;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001910
1911 id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001912 if (!pmem[id].cached)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001913 return;
1914
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001915 /* is_pmem_file fails if !file */
1916 data = file->private_data;
1917
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001918 down_read(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 if (!has_allocation(file))
1920 goto end;
1921
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001922 vaddr = pmem_start_vaddr(id, data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001923
1924 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM) {
1925 dmac_flush_range(vaddr,
1926 (void *)((unsigned long)vaddr +
1927 ((struct alloc_list *)(data->index))->size));
1928#ifdef CONFIG_OUTER_CACHE
1929 phy_start = pmem_start_addr_system(id, data);
1930
1931 phy_end = phy_start +
1932 ((struct alloc_list *)(data->index))->size;
1933
1934 outer_flush_range(phy_start, phy_end);
1935#endif
1936 goto end;
1937 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001938 /* if this isn't a submmapped file, flush the whole thing */
1939 if (unlikely(!(data->flags & PMEM_FLAGS_CONNECTED))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001940 dmac_flush_range(vaddr, vaddr + pmem[id].len(id, data));
1941#ifdef CONFIG_OUTER_CACHE
1942 phy_start = (unsigned long)vaddr -
1943 (unsigned long)pmem[id].vbase + pmem[id].base;
1944
1945 phy_end = phy_start + pmem[id].len(id, data);
1946
1947 outer_flush_range(phy_start, phy_end);
1948#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001949 goto end;
1950 }
1951 /* otherwise, flush the region of the file we are drawing */
1952 list_for_each(elt, &data->region_list) {
1953 region_node = list_entry(elt, struct pmem_region_node, list);
1954 if ((offset >= region_node->region.offset) &&
1955 ((offset + len) <= (region_node->region.offset +
1956 region_node->region.len))) {
1957 flush_start = vaddr + region_node->region.offset;
1958 flush_end = flush_start + region_node->region.len;
1959 dmac_flush_range(flush_start, flush_end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960#ifdef CONFIG_OUTER_CACHE
1961
1962 phy_start = (unsigned long)flush_start -
1963 (unsigned long)pmem[id].vbase + pmem[id].base;
1964
1965 phy_end = phy_start + region_node->region.len;
1966
1967 outer_flush_range(phy_start, phy_end);
1968#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001969 break;
1970 }
1971 }
1972end:
1973 up_read(&data->sem);
1974}
1975
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001976int pmem_cache_maint(struct file *file, unsigned int cmd,
1977 struct pmem_addr *pmem_addr)
1978{
1979 struct pmem_data *data;
1980 int id;
1981 unsigned long vaddr, paddr, length, offset,
1982 pmem_len, pmem_start_addr;
1983
1984 /* Called from kernel-space so file may be NULL */
1985 if (!file)
1986 return -EBADF;
1987
Shubhraprakash Das7788cad2011-11-21 13:02:22 -07001988 /*
1989 * check that the vaddr passed for flushing is valid
1990 * so that you don't crash the kernel
1991 */
1992 if (!pmem_addr->vaddr)
1993 return -EINVAL;
1994
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995 data = file->private_data;
1996 id = get_id(file);
1997
1998 if (!pmem[id].cached)
1999 return 0;
2000
2001 offset = pmem_addr->offset;
2002 length = pmem_addr->length;
2003
2004 down_read(&data->sem);
2005 if (!has_allocation(file)) {
2006 up_read(&data->sem);
2007 return -EINVAL;
2008 }
2009 pmem_len = pmem[id].len(id, data);
2010 pmem_start_addr = pmem[id].start_addr(id, data);
2011 up_read(&data->sem);
2012
2013 if (offset + length > pmem_len)
2014 return -EINVAL;
2015
2016 vaddr = pmem_addr->vaddr;
2017 paddr = pmem_start_addr + offset;
2018
2019 DLOG("pmem cache maint on dev %s(id: %d)"
2020 "(vaddr %lx paddr %lx len %lu bytes)\n",
2021 get_name(file), id, vaddr, paddr, length);
2022 if (cmd == PMEM_CLEAN_INV_CACHES)
2023 clean_and_invalidate_caches(vaddr,
2024 length, paddr);
2025 else if (cmd == PMEM_CLEAN_CACHES)
2026 clean_caches(vaddr, length, paddr);
2027 else if (cmd == PMEM_INV_CACHES)
2028 invalidate_caches(vaddr, length, paddr);
2029
2030 return 0;
2031}
2032EXPORT_SYMBOL(pmem_cache_maint);
2033
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002034static int pmem_connect(unsigned long connect, struct file *file)
2035{
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002036 int ret = 0, put_needed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037 struct file *src_file;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002038
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002039 if (!file) {
2040 pr_err("pmem: %s: NULL file pointer passed in, "
2041 "bailing out!\n", __func__);
2042 ret = -EINVAL;
2043 goto leave;
2044 }
2045
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002046 src_file = fget_light(connect, &put_needed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002048 if (!src_file) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049 pr_err("pmem: %s: src file not found!\n", __func__);
2050 ret = -EBADF;
2051 goto leave;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002052 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002054 if (src_file == file) { /* degenerative case, operator error */
2055 pr_err("pmem: %s: src_file and passed in file are "
2056 "the same; refusing to connect to self!\n", __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002057 ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058 goto put_src_file;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002059 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002060
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002061 if (unlikely(!is_pmem_file(src_file))) {
2062 pr_err("pmem: %s: src file is not a pmem file!\n",
2063 __func__);
2064 ret = -EINVAL;
2065 goto put_src_file;
2066 } else {
2067 struct pmem_data *src_data = src_file->private_data;
2068
2069 if (!src_data) {
2070 pr_err("pmem: %s: src file pointer has no"
2071 "private data, bailing out!\n", __func__);
2072 ret = -EINVAL;
2073 goto put_src_file;
2074 }
2075
2076 down_read(&src_data->sem);
2077
2078 if (unlikely(!has_allocation(src_file))) {
2079 up_read(&src_data->sem);
2080 pr_err("pmem: %s: src file has no allocation!\n",
2081 __func__);
2082 ret = -EINVAL;
2083 } else {
2084 struct pmem_data *data;
2085 int src_index = src_data->index;
2086
2087 up_read(&src_data->sem);
2088
2089 data = file->private_data;
2090 if (!data) {
2091 pr_err("pmem: %s: passed in file "
2092 "pointer has no private data, bailing"
2093 " out!\n", __func__);
2094 ret = -EINVAL;
2095 goto put_src_file;
2096 }
2097
2098 down_write(&data->sem);
2099 if (has_allocation(file) &&
2100 (data->index != src_index)) {
2101 up_write(&data->sem);
2102
2103 pr_err("pmem: %s: file is already "
2104 "mapped but doesn't match this "
2105 "src_file!\n", __func__);
2106 ret = -EINVAL;
2107 } else {
2108 data->index = src_index;
2109 data->flags |= PMEM_FLAGS_CONNECTED;
2110 data->master_fd = connect;
2111 data->master_file = src_file;
2112
2113 up_write(&data->sem);
2114
2115 DLOG("connect %p to %p\n", file, src_file);
2116 }
2117 }
2118 }
2119put_src_file:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002120 fput_light(src_file, put_needed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002121leave:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002122 return ret;
2123}
2124
2125static void pmem_unlock_data_and_mm(struct pmem_data *data,
2126 struct mm_struct *mm)
2127{
2128 up_write(&data->sem);
2129 if (mm != NULL) {
2130 up_write(&mm->mmap_sem);
2131 mmput(mm);
2132 }
2133}
2134
2135static int pmem_lock_data_and_mm(struct file *file, struct pmem_data *data,
2136 struct mm_struct **locked_mm)
2137{
2138 int ret = 0;
2139 struct mm_struct *mm = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002140#if PMEM_DEBUG_MSGS
2141 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
2142#endif
2143 DLOG("pid %u(%s) file %p(%ld)\n",
2144 current->pid, get_task_comm(currtask_name, current),
2145 file, file_count(file));
2146
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002147 *locked_mm = NULL;
2148lock_mm:
2149 down_read(&data->sem);
2150 if (PMEM_IS_SUBMAP(data)) {
2151 mm = get_task_mm(data->task);
2152 if (!mm) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002153 up_read(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002154#if PMEM_DEBUG
2155 pr_alert("pmem: can't remap - task is gone!\n");
2156#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002157 return -1;
2158 }
2159 }
2160 up_read(&data->sem);
2161
2162 if (mm)
2163 down_write(&mm->mmap_sem);
2164
2165 down_write(&data->sem);
2166 /* check that the file didn't get mmaped before we could take the
2167 * data sem, this should be safe b/c you can only submap each file
2168 * once */
2169 if (PMEM_IS_SUBMAP(data) && !mm) {
2170 pmem_unlock_data_and_mm(data, mm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171 DLOG("mapping contention, repeating mmap op\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002172 goto lock_mm;
2173 }
2174 /* now check that vma.mm is still there, it could have been
2175 * deleted by vma_close before we could get the data->sem */
2176 if ((data->flags & PMEM_FLAGS_UNSUBMAP) && (mm != NULL)) {
2177 /* might as well release this */
2178 if (data->flags & PMEM_FLAGS_SUBMAP) {
2179 put_task_struct(data->task);
2180 data->task = NULL;
2181 /* lower the submap flag to show the mm is gone */
2182 data->flags &= ~(PMEM_FLAGS_SUBMAP);
2183 }
2184 pmem_unlock_data_and_mm(data, mm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185#if PMEM_DEBUG
2186 pr_alert("pmem: vma.mm went away!\n");
2187#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002188 return -1;
2189 }
2190 *locked_mm = mm;
2191 return ret;
2192}
2193
2194int pmem_remap(struct pmem_region *region, struct file *file,
2195 unsigned operation)
2196{
2197 int ret;
2198 struct pmem_region_node *region_node;
2199 struct mm_struct *mm = NULL;
2200 struct list_head *elt, *elt2;
2201 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202 struct pmem_data *data;
2203
2204 DLOG("operation %#x, region offset %ld, region len %ld\n",
2205 operation, region->offset, region->len);
2206
2207 if (!is_pmem_file(file)) {
2208#if PMEM_DEBUG
2209 pr_err("pmem: remap request for non-pmem file descriptor\n");
2210#endif
2211 return -EINVAL;
2212 }
2213
2214 /* is_pmem_file fails if !file */
2215 data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002216
2217 /* pmem region must be aligned on a page boundry */
2218 if (unlikely(!PMEM_IS_PAGE_ALIGNED(region->offset) ||
2219 !PMEM_IS_PAGE_ALIGNED(region->len))) {
2220#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002221 pr_err("pmem: request for unaligned pmem"
2222 "suballocation %lx %lx\n",
2223 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002224#endif
2225 return -EINVAL;
2226 }
2227
2228 /* if userspace requests a region of len 0, there's nothing to do */
2229 if (region->len == 0)
2230 return 0;
2231
2232 /* lock the mm and data */
2233 ret = pmem_lock_data_and_mm(file, data, &mm);
2234 if (ret)
2235 return 0;
2236
2237 /* only the owner of the master file can remap the client fds
2238 * that back in it */
2239 if (!is_master_owner(file)) {
2240#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002241 pr_err("pmem: remap requested from non-master process\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002242#endif
2243 ret = -EINVAL;
2244 goto err;
2245 }
2246
2247 /* check that the requested range is within the src allocation */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002248 if (unlikely((region->offset > pmem[id].len(id, data)) ||
2249 (region->len > pmem[id].len(id, data)) ||
2250 (region->offset + region->len > pmem[id].len(id, data)))) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002251#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002252 pr_err("pmem: suballoc doesn't fit in src_file!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002253#endif
2254 ret = -EINVAL;
2255 goto err;
2256 }
2257
2258 if (operation == PMEM_MAP) {
2259 region_node = kmalloc(sizeof(struct pmem_region_node),
2260 GFP_KERNEL);
2261 if (!region_node) {
2262 ret = -ENOMEM;
2263#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002264 pr_alert("pmem: No space to allocate remap metadata!");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002265#endif
2266 goto err;
2267 }
2268 region_node->region = *region;
2269 list_add(&region_node->list, &data->region_list);
2270 } else if (operation == PMEM_UNMAP) {
2271 int found = 0;
2272 list_for_each_safe(elt, elt2, &data->region_list) {
2273 region_node = list_entry(elt, struct pmem_region_node,
2274 list);
2275 if (region->len == 0 ||
2276 (region_node->region.offset == region->offset &&
2277 region_node->region.len == region->len)) {
2278 list_del(elt);
2279 kfree(region_node);
2280 found = 1;
2281 }
2282 }
2283 if (!found) {
2284#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002285 pr_err("pmem: Unmap region does not map any"
2286 " mapped region!");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002287#endif
2288 ret = -EINVAL;
2289 goto err;
2290 }
2291 }
2292
2293 if (data->vma && PMEM_IS_SUBMAP(data)) {
2294 if (operation == PMEM_MAP)
2295 ret = pmem_remap_pfn_range(id, data->vma, data,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002297 else if (operation == PMEM_UNMAP)
2298 ret = pmem_unmap_pfn_range(id, data->vma, data,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002300 }
2301
2302err:
2303 pmem_unlock_data_and_mm(data, mm);
2304 return ret;
2305}
2306
2307static void pmem_revoke(struct file *file, struct pmem_data *data)
2308{
2309 struct pmem_region_node *region_node;
2310 struct list_head *elt, *elt2;
2311 struct mm_struct *mm = NULL;
2312 int id = get_id(file);
2313 int ret = 0;
2314
2315 data->master_file = NULL;
2316 ret = pmem_lock_data_and_mm(file, data, &mm);
2317 /* if lock_data_and_mm fails either the task that mapped the fd, or
2318 * the vma that mapped it have already gone away, nothing more
2319 * needs to be done */
2320 if (ret)
2321 return;
2322 /* unmap everything */
2323 /* delete the regions and region list nothing is mapped any more */
2324 if (data->vma)
2325 list_for_each_safe(elt, elt2, &data->region_list) {
2326 region_node = list_entry(elt, struct pmem_region_node,
2327 list);
2328 pmem_unmap_pfn_range(id, data->vma, data,
2329 region_node->region.offset,
2330 region_node->region.len);
2331 list_del(elt);
2332 kfree(region_node);
2333 }
2334 /* delete the master file */
2335 pmem_unlock_data_and_mm(data, mm);
2336}
2337
2338static void pmem_get_size(struct pmem_region *region, struct file *file)
2339{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002340 /* called via ioctl file op, so file guaranteed to be not NULL */
2341 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002342 int id = get_id(file);
2343
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002345 if (!has_allocation(file)) {
2346 region->offset = 0;
2347 region->len = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002348 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002349 region->offset = pmem[id].start_addr(id, data);
2350 region->len = pmem[id].len(id, data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002351 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 up_read(&data->sem);
2353 DLOG("offset 0x%lx len 0x%lx\n", region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002354}
2355
2356
2357static long pmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2358{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002359 /* called from user space as file op, so file guaranteed to be not
2360 * NULL
2361 */
2362 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002363 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364#if PMEM_DEBUG_MSGS
2365 char currtask_name[
2366 FIELD_SIZEOF(struct task_struct, comm) + 1];
2367#endif
2368
2369 DLOG("pid %u(%s) file %p(%ld) cmd %#x, dev %s(id: %d)\n",
2370 current->pid, get_task_comm(currtask_name, current),
2371 file, file_count(file), cmd, get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002372
2373 switch (cmd) {
2374 case PMEM_GET_PHYS:
2375 {
2376 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002377
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002378 DLOG("get_phys\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002380 if (!has_allocation(file)) {
2381 region.offset = 0;
2382 region.len = 0;
2383 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384 region.offset = pmem[id].start_addr(id, data);
2385 region.len = pmem[id].len(id, data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002386 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 up_read(&data->sem);
2388
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002389 if (copy_to_user((void __user *)arg, &region,
2390 sizeof(struct pmem_region)))
2391 return -EFAULT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392
2393 DLOG("pmem: successful request for "
2394 "physical address of pmem region id %d, "
2395 "offset 0x%lx, len 0x%lx\n",
2396 id, region.offset, region.len);
2397
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002398 break;
2399 }
2400 case PMEM_MAP:
2401 {
2402 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002403 DLOG("map\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002404 if (copy_from_user(&region, (void __user *)arg,
2405 sizeof(struct pmem_region)))
2406 return -EFAULT;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002407 return pmem_remap(&region, file, PMEM_MAP);
2408 }
2409 break;
2410 case PMEM_UNMAP:
2411 {
2412 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 DLOG("unmap\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002414 if (copy_from_user(&region, (void __user *)arg,
2415 sizeof(struct pmem_region)))
2416 return -EFAULT;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002417 return pmem_remap(&region, file, PMEM_UNMAP);
2418 break;
2419 }
2420 case PMEM_GET_SIZE:
2421 {
2422 struct pmem_region region;
2423 DLOG("get_size\n");
2424 pmem_get_size(&region, file);
2425 if (copy_to_user((void __user *)arg, &region,
2426 sizeof(struct pmem_region)))
2427 return -EFAULT;
2428 break;
2429 }
2430 case PMEM_GET_TOTAL_SIZE:
2431 {
2432 struct pmem_region region;
2433 DLOG("get total size\n");
2434 region.offset = 0;
2435 get_id(file);
2436 region.len = pmem[id].size;
2437 if (copy_to_user((void __user *)arg, &region,
2438 sizeof(struct pmem_region)))
2439 return -EFAULT;
2440 break;
2441 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002442 case PMEM_GET_FREE_SPACE:
2443 {
2444 struct pmem_freespace fs;
2445 DLOG("get freespace on %s(id: %d)\n",
2446 get_name(file), id);
2447
2448 mutex_lock(&pmem[id].arena_mutex);
2449 pmem[id].free_space(id, &fs);
2450 mutex_unlock(&pmem[id].arena_mutex);
2451
2452 DLOG("%s(id: %d) total free %lu, largest %lu\n",
2453 get_name(file), id, fs.total, fs.largest);
2454
2455 if (copy_to_user((void __user *)arg, &fs,
2456 sizeof(struct pmem_freespace)))
2457 return -EFAULT;
2458 break;
2459 }
2460
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002461 case PMEM_ALLOCATE:
2462 {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463 int ret = 0;
2464 DLOG("allocate, id %d\n", id);
2465 down_write(&data->sem);
2466 if (has_allocation(file)) {
2467 pr_err("pmem: Existing allocation found on "
2468 "this file descrpitor\n");
2469 up_write(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002470 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 }
2472
2473 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07002474 data->index = pmem_allocate_from_id(id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002475 arg,
2476 SZ_4K);
2477 mutex_unlock(&pmem[id].arena_mutex);
2478 ret = data->index == -1 ? -ENOMEM :
2479 data->index;
2480 up_write(&data->sem);
2481 return ret;
2482 }
2483 case PMEM_ALLOCATE_ALIGNED:
2484 {
2485 struct pmem_allocation alloc;
2486 int ret = 0;
2487
2488 if (copy_from_user(&alloc, (void __user *)arg,
2489 sizeof(struct pmem_allocation)))
2490 return -EFAULT;
2491 DLOG("allocate id align %d %u\n", id, alloc.align);
2492 down_write(&data->sem);
2493 if (has_allocation(file)) {
2494 pr_err("pmem: Existing allocation found on "
2495 "this file descrpitor\n");
2496 up_write(&data->sem);
2497 return -EINVAL;
2498 }
2499
2500 if (alloc.align & (alloc.align - 1)) {
2501 pr_err("pmem: Alignment is not a power of 2\n");
2502 return -EINVAL;
2503 }
2504
2505 if (alloc.align != SZ_4K &&
2506 (pmem[id].allocator_type !=
2507 PMEM_ALLOCATORTYPE_BITMAP)) {
2508 pr_err("pmem: Non 4k alignment requires bitmap"
2509 " allocator on %s\n", pmem[id].name);
2510 return -EINVAL;
2511 }
2512
2513 if (alloc.align > SZ_1M ||
2514 alloc.align < SZ_4K) {
2515 pr_err("pmem: Invalid Alignment (%u) "
2516 "specified\n", alloc.align);
2517 return -EINVAL;
2518 }
2519
2520 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07002521 data->index = pmem_allocate_from_id(id,
2522 alloc.size,
2523 alloc.align);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524 mutex_unlock(&pmem[id].arena_mutex);
2525 ret = data->index == -1 ? -ENOMEM :
2526 data->index;
2527 up_write(&data->sem);
2528 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002529 }
2530 case PMEM_CONNECT:
2531 DLOG("connect\n");
2532 return pmem_connect(arg, file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002533 case PMEM_CLEAN_INV_CACHES:
2534 case PMEM_CLEAN_CACHES:
2535 case PMEM_INV_CACHES:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002536 {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537 struct pmem_addr pmem_addr;
2538
2539 if (copy_from_user(&pmem_addr, (void __user *)arg,
2540 sizeof(struct pmem_addr)))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002541 return -EFAULT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002542
2543 return pmem_cache_maint(file, cmd, &pmem_addr);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002544 }
2545 default:
2546 if (pmem[id].ioctl)
2547 return pmem[id].ioctl(file, cmd, arg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002548
2549 DLOG("ioctl invalid (%#x)\n", cmd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002550 return -EINVAL;
2551 }
2552 return 0;
2553}
2554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002555static void ioremap_pmem(int id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002556{
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002557 unsigned long addr;
2558 const struct mem_type *type;
Laura Abbott1e36a022011-06-22 17:08:13 -07002559
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002560 DLOG("PMEMDEBUG: ioremaping for %s\n", pmem[id].name);
2561 if (pmem[id].map_on_demand) {
2562 addr = (unsigned long)pmem[id].area->addr;
2563 if (pmem[id].cached)
2564 type = get_mem_type(MT_DEVICE_CACHED);
2565 else
2566 type = get_mem_type(MT_DEVICE);
2567 DLOG("PMEMDEBUG: Remap phys %lx to virt %lx on %s\n",
2568 pmem[id].base, addr, pmem[id].name);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002569 if (ioremap_pages(addr, pmem[id].base, pmem[id].size, type)) {
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002570 pr_err("pmem: Failed to map pages\n");
2571 BUG();
2572 }
2573 pmem[id].vbase = pmem[id].area->addr;
2574 /* Flush the cache after installing page table entries to avoid
2575 * aliasing when these pages are remapped to user space.
2576 */
2577 flush_cache_vmap(addr, addr + pmem[id].size);
2578 } else {
2579 if (pmem[id].cached)
2580 pmem[id].vbase = ioremap_cached(pmem[id].base,
2581 pmem[id].size);
2582 #ifdef ioremap_ext_buffered
2583 else if (pmem[id].buffered)
2584 pmem[id].vbase = ioremap_ext_buffered(pmem[id].base,
2585 pmem[id].size);
2586 #endif
2587 else
2588 pmem[id].vbase = ioremap(pmem[id].base, pmem[id].size);
2589 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590}
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002591
2592int pmem_setup(struct android_pmem_platform_data *pdata,
2593 long (*ioctl)(struct file *, unsigned int, unsigned long),
2594 int (*release)(struct inode *, struct file *))
2595{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002596 int i, index = 0, id;
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002597 struct vm_struct *pmem_vma = NULL;
Vipul Gandhif752bf62012-01-09 15:34:04 -08002598 struct page *page;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 if (id_count >= PMEM_MAX_DEVICES) {
2601 pr_alert("pmem: %s: unable to register driver(%s) - no more "
2602 "devices available!\n", __func__, pdata->name);
2603 goto err_no_mem;
2604 }
2605
2606 if (!pdata->size) {
2607 pr_alert("pmem: %s: unable to register pmem driver(%s) - zero "
2608 "size passed in!\n", __func__, pdata->name);
2609 goto err_no_mem;
2610 }
2611
2612 id = id_count++;
2613
2614 pmem[id].id = id;
2615
2616 if (pmem[id].allocate) {
2617 pr_alert("pmem: %s: unable to register pmem driver - "
2618 "duplicate registration of %s!\n",
2619 __func__, pdata->name);
2620 goto err_no_mem;
2621 }
2622
2623 pmem[id].allocator_type = pdata->allocator_type;
2624
2625 /* 'quantum' is a "hidden" variable that defaults to 0 in the board
2626 * files */
2627 pmem[id].quantum = pdata->quantum ?: PMEM_MIN_ALLOC;
2628 if (pmem[id].quantum < PMEM_MIN_ALLOC ||
2629 !is_power_of_2(pmem[id].quantum)) {
2630 pr_alert("pmem: %s: unable to register pmem driver %s - "
2631 "invalid quantum value (%#x)!\n",
2632 __func__, pdata->name, pmem[id].quantum);
2633 goto err_reset_pmem_info;
2634 }
2635
2636 if (pdata->size % pmem[id].quantum) {
2637 /* bad alignment for size! */
2638 pr_alert("pmem: %s: Unable to register driver %s - "
2639 "memory region size (%#lx) is not a multiple of "
2640 "quantum size(%#x)!\n", __func__, pdata->name,
2641 pdata->size, pmem[id].quantum);
2642 goto err_reset_pmem_info;
2643 }
2644
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002645 pmem[id].cached = pdata->cached;
2646 pmem[id].buffered = pdata->buffered;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002647 pmem[id].size = pdata->size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002648 pmem[id].memory_type = pdata->memory_type;
2649 strlcpy(pmem[id].name, pdata->name, PMEM_NAME_SIZE);
2650
2651 pmem[id].num_entries = pmem[id].size / pmem[id].quantum;
2652
2653 memset(&pmem[id].kobj, 0, sizeof(pmem[0].kobj));
2654 pmem[id].kobj.kset = pmem_kset;
2655
2656 switch (pmem[id].allocator_type) {
2657 case PMEM_ALLOCATORTYPE_ALLORNOTHING:
2658 pmem[id].allocate = pmem_allocator_all_or_nothing;
2659 pmem[id].free = pmem_free_all_or_nothing;
2660 pmem[id].free_space = pmem_free_space_all_or_nothing;
2661 pmem[id].len = pmem_len_all_or_nothing;
2662 pmem[id].start_addr = pmem_start_addr_all_or_nothing;
2663 pmem[id].num_entries = 1;
2664 pmem[id].quantum = pmem[id].size;
2665 pmem[id].allocator.all_or_nothing.allocated = 0;
2666
2667 if (kobject_init_and_add(&pmem[id].kobj,
2668 &pmem_allornothing_ktype, NULL,
2669 "%s", pdata->name))
2670 goto out_put_kobj;
2671
2672 break;
2673
2674 case PMEM_ALLOCATORTYPE_BUDDYBESTFIT:
2675 pmem[id].allocator.buddy_bestfit.buddy_bitmap = kmalloc(
2676 pmem[id].num_entries * sizeof(struct pmem_bits),
2677 GFP_KERNEL);
2678 if (!pmem[id].allocator.buddy_bestfit.buddy_bitmap)
2679 goto err_reset_pmem_info;
2680
2681 memset(pmem[id].allocator.buddy_bestfit.buddy_bitmap, 0,
2682 sizeof(struct pmem_bits) * pmem[id].num_entries);
2683
2684 for (i = sizeof(pmem[id].num_entries) * 8 - 1; i >= 0; i--)
2685 if ((pmem[id].num_entries) & 1<<i) {
2686 PMEM_BUDDY_ORDER(id, index) = i;
2687 index = PMEM_BUDDY_NEXT_INDEX(id, index);
2688 }
2689 pmem[id].allocate = pmem_allocator_buddy_bestfit;
2690 pmem[id].free = pmem_free_buddy_bestfit;
2691 pmem[id].free_space = pmem_free_space_buddy_bestfit;
2692 pmem[id].len = pmem_len_buddy_bestfit;
2693 pmem[id].start_addr = pmem_start_addr_buddy_bestfit;
2694 if (kobject_init_and_add(&pmem[id].kobj,
2695 &pmem_buddy_bestfit_ktype, NULL,
2696 "%s", pdata->name))
2697 goto out_put_kobj;
2698
2699 break;
2700
2701 case PMEM_ALLOCATORTYPE_BITMAP: /* 0, default if not explicit */
2702 pmem[id].allocator.bitmap.bitm_alloc = kmalloc(
2703 PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS *
2704 sizeof(*pmem[id].allocator.bitmap.bitm_alloc),
2705 GFP_KERNEL);
2706 if (!pmem[id].allocator.bitmap.bitm_alloc) {
2707 pr_alert("pmem: %s: Unable to register pmem "
2708 "driver %s - can't allocate "
2709 "bitm_alloc!\n",
2710 __func__, pdata->name);
2711 goto err_reset_pmem_info;
2712 }
2713
2714 if (kobject_init_and_add(&pmem[id].kobj,
2715 &pmem_bitmap_ktype, NULL,
2716 "%s", pdata->name))
2717 goto out_put_kobj;
2718
2719 for (i = 0; i < PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS; i++) {
2720 pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1;
2721 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
2722 }
2723
2724 pmem[id].allocator.bitmap.bitmap_allocs =
2725 PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS;
2726
2727 pmem[id].allocator.bitmap.bitmap =
2728 kcalloc((pmem[id].num_entries + 31) / 32,
2729 sizeof(unsigned int), GFP_KERNEL);
2730 if (!pmem[id].allocator.bitmap.bitmap) {
2731 pr_alert("pmem: %s: Unable to register pmem "
2732 "driver - can't allocate bitmap!\n",
2733 __func__);
2734 goto err_cant_register_device;
2735 }
2736 pmem[id].allocator.bitmap.bitmap_free = pmem[id].num_entries;
2737
2738 pmem[id].allocate = pmem_allocator_bitmap;
2739 pmem[id].free = pmem_free_bitmap;
2740 pmem[id].free_space = pmem_free_space_bitmap;
2741 pmem[id].len = pmem_len_bitmap;
2742 pmem[id].start_addr = pmem_start_addr_bitmap;
2743
2744 DLOG("bitmap allocator id %d (%s), num_entries %u, raw size "
2745 "%lu, quanta size %u\n",
2746 id, pdata->name, pmem[id].allocator.bitmap.bitmap_free,
2747 pmem[id].size, pmem[id].quantum);
2748 break;
2749
2750 case PMEM_ALLOCATORTYPE_SYSTEM:
2751
2752 INIT_LIST_HEAD(&pmem[id].allocator.system_mem.alist);
2753
2754 pmem[id].allocator.system_mem.used = 0;
2755 pmem[id].vbase = NULL;
2756
2757 if (kobject_init_and_add(&pmem[id].kobj,
2758 &pmem_system_ktype, NULL,
2759 "%s", pdata->name))
2760 goto out_put_kobj;
2761
2762 pmem[id].allocate = pmem_allocator_system;
2763 pmem[id].free = pmem_free_system;
2764 pmem[id].free_space = pmem_free_space_system;
2765 pmem[id].len = pmem_len_system;
2766 pmem[id].start_addr = pmem_start_addr_system;
2767 pmem[id].num_entries = 0;
2768 pmem[id].quantum = PAGE_SIZE;
2769
2770 DLOG("system allocator id %d (%s), raw size %lu\n",
2771 id, pdata->name, pmem[id].size);
2772 break;
2773
2774 default:
2775 pr_alert("Invalid allocator type (%d) for pmem driver\n",
2776 pdata->allocator_type);
2777 goto err_reset_pmem_info;
2778 }
2779
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002780 pmem[id].ioctl = ioctl;
2781 pmem[id].release = release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782 mutex_init(&pmem[id].arena_mutex);
2783 mutex_init(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002784 INIT_LIST_HEAD(&pmem[id].data_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002785
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002786 pmem[id].dev.name = pdata->name;
2787 pmem[id].dev.minor = id;
2788 pmem[id].dev.fops = &pmem_fops;
Laura Abbott4b392622012-01-19 16:17:02 -08002789 pmem[id].reusable = pdata->reusable;
2790 pr_info("pmem: Initializing %s as %s\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002791 pdata->name, pdata->cached ? "cached" : "non-cached");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002793 if (misc_register(&pmem[id].dev)) {
2794 pr_alert("Unable to register pmem driver!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002795 goto err_cant_register_device;
2796 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002797
Laura Abbott4b392622012-01-19 16:17:02 -08002798 if (!pmem[id].reusable) {
2799 pmem[id].base = allocate_contiguous_memory_nomap(pmem[id].size,
2800 pmem[id].memory_type, PAGE_SIZE);
2801 if (!pmem[id].base) {
2802 pr_err("pmem: Cannot allocate from reserved memory for %s\n",
2803 pdata->name);
2804 goto err_misc_deregister;
2805 }
Vipul Gandhif752bf62012-01-09 15:34:04 -08002806 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002807
Laura Abbott511edaf2011-12-14 13:34:53 -08002808 /* reusable pmem requires map on demand */
2809 pmem[id].map_on_demand = pdata->map_on_demand || pdata->reusable;
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002810 if (pmem[id].map_on_demand) {
Laura Abbott511edaf2011-12-14 13:34:53 -08002811 if (pmem[id].reusable) {
2812 const struct fmem_data *fmem_info = fmem_get_info();
2813 pmem[id].area = fmem_info->area;
Laura Abbott4b392622012-01-19 16:17:02 -08002814 pmem[id].base = fmem_info->phys;
Laura Abbott511edaf2011-12-14 13:34:53 -08002815 } else {
2816 pmem_vma = get_vm_area(pmem[id].size, VM_IOREMAP);
2817 if (!pmem_vma) {
2818 pr_err("pmem: Failed to allocate virtual space for "
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002819 "%s\n", pdata->name);
Vipul Gandhif752bf62012-01-09 15:34:04 -08002820 goto err_free;
Laura Abbott511edaf2011-12-14 13:34:53 -08002821 }
2822 pr_err("pmem: Reserving virtual address range %lx - %lx for"
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002823 " %s\n", (unsigned long) pmem_vma->addr,
2824 (unsigned long) pmem_vma->addr + pmem[id].size,
2825 pdata->name);
Laura Abbott511edaf2011-12-14 13:34:53 -08002826 pmem[id].area = pmem_vma;
2827 }
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002828 } else
2829 pmem[id].area = NULL;
2830
Vipul Gandhif752bf62012-01-09 15:34:04 -08002831 page = alloc_page(GFP_KERNEL);
2832 if (!page) {
2833 pr_err("pmem: Failed to allocate page for %s\n", pdata->name);
2834 goto cleanup_vm;
2835 }
2836 pmem[id].garbage_pfn = page_to_pfn(page);
Laura Abbott1e36a022011-06-22 17:08:13 -07002837 atomic_set(&pmem[id].allocation_cnt, 0);
Laura Abbott1e36a022011-06-22 17:08:13 -07002838
2839 if (pdata->setup_region)
2840 pmem[id].region_data = pdata->setup_region();
2841
2842 if (pdata->request_region)
2843 pmem[id].mem_request = pdata->request_region;
2844
2845 if (pdata->release_region)
2846 pmem[id].mem_release = pdata->release_region;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002847
Laura Abbott4b392622012-01-19 16:17:02 -08002848 pr_info("allocating %lu bytes at %lx physical for %s\n",
2849 pmem[id].size, pmem[id].base, pmem[id].name);
2850
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002851 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002852
Vipul Gandhif752bf62012-01-09 15:34:04 -08002853cleanup_vm:
Laura Abbott4b392622012-01-19 16:17:02 -08002854 if (!pmem[id].reusable)
2855 remove_vm_area(pmem_vma);
Vipul Gandhif752bf62012-01-09 15:34:04 -08002856err_free:
Laura Abbott4b392622012-01-19 16:17:02 -08002857 if (!pmem[id].reusable)
2858 free_contiguous_memory_by_paddr(pmem[id].base);
Vipul Gandhif752bf62012-01-09 15:34:04 -08002859err_misc_deregister:
2860 misc_deregister(&pmem[id].dev);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002861err_cant_register_device:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862out_put_kobj:
2863 kobject_put(&pmem[id].kobj);
2864 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BUDDYBESTFIT)
2865 kfree(pmem[id].allocator.buddy_bestfit.buddy_bitmap);
2866 else if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BITMAP) {
2867 kfree(pmem[id].allocator.bitmap.bitmap);
2868 kfree(pmem[id].allocator.bitmap.bitm_alloc);
2869 }
2870err_reset_pmem_info:
2871 pmem[id].allocate = 0;
2872 pmem[id].dev.minor = -1;
2873err_no_mem:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002874 return -1;
2875}
2876
2877static int pmem_probe(struct platform_device *pdev)
2878{
2879 struct android_pmem_platform_data *pdata;
2880
2881 if (!pdev || !pdev->dev.platform_data) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 pr_alert("Unable to probe pmem!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002883 return -1;
2884 }
2885 pdata = pdev->dev.platform_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886
2887 pm_runtime_set_active(&pdev->dev);
2888 pm_runtime_enable(&pdev->dev);
2889
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002890 return pmem_setup(pdata, NULL, NULL);
2891}
2892
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002893static int pmem_remove(struct platform_device *pdev)
2894{
2895 int id = pdev->id;
2896 __free_page(pfn_to_page(pmem[id].garbage_pfn));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002897 pm_runtime_disable(&pdev->dev);
Vipul Gandhif752bf62012-01-09 15:34:04 -08002898 if (pmem[id].vbase)
2899 iounmap(pmem[id].vbase);
2900 if (pmem[id].map_on_demand && !pmem[id].reusable && pmem[id].area)
2901 free_vm_area(pmem[id].area);
2902 if (pmem[id].base)
2903 free_contiguous_memory_by_paddr(pmem[id].base);
2904 kobject_put(&pmem[id].kobj);
2905 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BUDDYBESTFIT)
2906 kfree(pmem[id].allocator.buddy_bestfit.buddy_bitmap);
2907 else if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BITMAP) {
2908 kfree(pmem[id].allocator.bitmap.bitmap);
2909 kfree(pmem[id].allocator.bitmap.bitm_alloc);
2910 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002911 misc_deregister(&pmem[id].dev);
2912 return 0;
2913}
2914
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002915static int pmem_runtime_suspend(struct device *dev)
2916{
2917 dev_dbg(dev, "pm_runtime: suspending...\n");
2918 return 0;
2919}
2920
2921static int pmem_runtime_resume(struct device *dev)
2922{
2923 dev_dbg(dev, "pm_runtime: resuming...\n");
2924 return 0;
2925}
2926
2927static const struct dev_pm_ops pmem_dev_pm_ops = {
2928 .runtime_suspend = pmem_runtime_suspend,
2929 .runtime_resume = pmem_runtime_resume,
2930};
2931
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002932static struct platform_driver pmem_driver = {
2933 .probe = pmem_probe,
2934 .remove = pmem_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002935 .driver = { .name = "android_pmem",
2936 .pm = &pmem_dev_pm_ops,
2937 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002938};
2939
2940
2941static int __init pmem_init(void)
2942{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002943 /* create /sys/kernel/<PMEM_SYSFS_DIR_NAME> directory */
2944 pmem_kset = kset_create_and_add(PMEM_SYSFS_DIR_NAME,
2945 NULL, kernel_kobj);
2946 if (!pmem_kset) {
2947 pr_err("pmem(%s):kset_create_and_add fail\n", __func__);
2948 return -ENOMEM;
2949 }
2950
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002951 return platform_driver_register(&pmem_driver);
2952}
2953
2954static void __exit pmem_exit(void)
2955{
2956 platform_driver_unregister(&pmem_driver);
2957}
2958
2959module_init(pmem_init);
2960module_exit(pmem_exit);
2961