blob: 8d9a3759f50074a481787ff2f971621843af9876 [file] [log] [blame]
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001/* drivers/android/pmem.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 * Copyright (c) 2009-2011, Code Aurora Forum. 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
17#include <linux/miscdevice.h>
18#include <linux/platform_device.h>
19#include <linux/fs.h>
20#include <linux/file.h>
21#include <linux/mm.h>
22#include <linux/list.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070023#include <linux/debugfs.h>
24#include <linux/android_pmem.h>
25#include <linux/mempolicy.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/kobject.h>
Naveen Ramaraj189f1882011-08-16 17:39:22 -070027#include <linux/pm_runtime.h>
28#include <linux/memory_alloc.h>
29#include <linux/vmalloc.h>
30#include <linux/io.h>
31#include <linux/mm_types.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070032#include <asm/io.h>
33#include <asm/uaccess.h>
34#include <asm/cacheflush.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include <asm/sizes.h>
Naveen Ramaraj189f1882011-08-16 17:39:22 -070036#include <asm/mach/map.h>
37#include <asm/page.h>
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070038
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#define PMEM_MAX_DEVICES (10)
40
41#define PMEM_MAX_ORDER (128)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070042#define PMEM_MIN_ALLOC PAGE_SIZE
43
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#define PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS (64)
45
46#define PMEM_32BIT_WORD_ORDER (5)
47#define PMEM_BITS_PER_WORD_MASK (BITS_PER_LONG - 1)
48
49#ifdef CONFIG_ANDROID_PMEM_DEBUG
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070050#define PMEM_DEBUG 1
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#else
52#define PMEM_DEBUG 0
53#endif
54
55#define SYSTEM_ALLOC_RETRY 10
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070056
57/* indicates that a refernce to this file has been taken via get_pmem_file,
58 * the file should not be released until put_pmem_file is called */
59#define PMEM_FLAGS_BUSY 0x1
60/* indicates that this is a suballocation of a larger master range */
61#define PMEM_FLAGS_CONNECTED 0x1 << 1
62/* indicates this is a master and not a sub allocation and that it is mmaped */
63#define PMEM_FLAGS_MASTERMAP 0x1 << 2
64/* submap and unsubmap flags indicate:
65 * 00: subregion has never been mmaped
66 * 10: subregion has been mmaped, reference to the mm was taken
67 * 11: subretion has ben released, refernece to the mm still held
68 * 01: subretion has been released, reference to the mm has been released
69 */
70#define PMEM_FLAGS_SUBMAP 0x1 << 3
71#define PMEM_FLAGS_UNSUBMAP 0x1 << 4
72
Rebecca Schultza4ff0e82008-07-24 11:22:53 -070073struct pmem_data {
74 /* in alloc mode: an index into the bitmap
75 * in no_alloc mode: the size of the allocation */
76 int index;
77 /* see flags above for descriptions */
78 unsigned int flags;
79 /* protects this data field, if the mm_mmap sem will be held at the
80 * same time as this sem, the mm sem must be taken first (as this is
81 * the order for vma_open and vma_close ops */
82 struct rw_semaphore sem;
83 /* info about the mmaping process */
84 struct vm_area_struct *vma;
85 /* task struct of the mapping process */
86 struct task_struct *task;
87 /* process id of teh mapping process */
88 pid_t pid;
89 /* file descriptor of the master */
90 int master_fd;
91 /* file struct of the master */
92 struct file *master_file;
93 /* a list of currently available regions if this is a suballocation */
94 struct list_head region_list;
95 /* a linked list of data so we can access them for debugging */
96 struct list_head list;
97#if PMEM_DEBUG
98 int ref;
99#endif
100};
101
102struct pmem_bits {
103 unsigned allocated:1; /* 1 if allocated, 0 if free */
104 unsigned order:7; /* size of the region in pmem space */
105};
106
107struct pmem_region_node {
108 struct pmem_region region;
109 struct list_head list;
110};
111
112#define PMEM_DEBUG_MSGS 0
113#if PMEM_DEBUG_MSGS
114#define DLOG(fmt,args...) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115 do { pr_debug("[%s:%s:%d] "fmt, __FILE__, __func__, __LINE__, \
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700116 ##args); } \
117 while (0)
118#else
119#define DLOG(x...) do {} while (0)
120#endif
121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122enum pmem_align {
123 PMEM_ALIGN_4K,
124 PMEM_ALIGN_1M,
125};
126
127#define PMEM_NAME_SIZE 16
128
129struct alloc_list {
130 void *addr; /* physical addr of allocation */
131 void *aaddr; /* aligned physical addr */
132 unsigned int size; /* total size of allocation */
133 unsigned char __iomem *vaddr; /* Virtual addr */
134 struct list_head allocs;
135};
136
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700137struct pmem_info {
138 struct miscdevice dev;
139 /* physical start address of the remaped pmem space */
140 unsigned long base;
141 /* vitual start address of the remaped pmem space */
142 unsigned char __iomem *vbase;
143 /* total size of the pmem space */
144 unsigned long size;
145 /* number of entries in the pmem space */
146 unsigned long num_entries;
147 /* pfn of the garbage page in memory */
148 unsigned long garbage_pfn;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 /* which memory type (i.e. SMI, EBI1) this PMEM device is backed by */
150 unsigned memory_type;
151
152 char name[PMEM_NAME_SIZE];
153
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700154 /* index of the garbage page in the pmem space */
155 int garbage_index;
Naveen Ramaraj189f1882011-08-16 17:39:22 -0700156 /* reserved virtual address range */
157 struct vm_struct *area;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158
159 enum pmem_allocator_type allocator_type;
160
161 int (*allocate)(const int,
162 const unsigned long,
163 const unsigned int);
164 int (*free)(int, int);
165 int (*free_space)(int, struct pmem_freespace *);
166 unsigned long (*len)(int, struct pmem_data *);
167 unsigned long (*start_addr)(int, struct pmem_data *);
168
169 /* actual size of memory element, e.g.: (4 << 10) is 4K */
170 unsigned int quantum;
171
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700172 /* indicates maps of this region should be cached, if a mix of
173 * cached and uncached is desired, set this and open the device with
174 * O_SYNC to get an uncached region */
175 unsigned cached;
176 unsigned buffered;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 union {
178 struct {
179 /* in all_or_nothing allocator mode the first mapper
180 * gets the whole space and sets this flag */
181 unsigned allocated;
182 } all_or_nothing;
183
184 struct {
185 /* the buddy allocator bitmap for the region
186 * indicating which entries are allocated and which
187 * are free.
188 */
189
190 struct pmem_bits *buddy_bitmap;
191 } buddy_bestfit;
192
193 struct {
194 unsigned int bitmap_free; /* # of zero bits/quanta */
195 uint32_t *bitmap;
196 int32_t bitmap_allocs;
197 struct {
198 short bit;
199 unsigned short quanta;
200 } *bitm_alloc;
201 } bitmap;
202
203 struct {
204 unsigned long used; /* Bytes currently allocated */
205 struct list_head alist; /* List of allocations */
206 } system_mem;
207 } allocator;
208
209 int id;
210 struct kobject kobj;
211
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700212 /* for debugging, creates a list of pmem file structs, the
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 * data_list_mutex should be taken before pmem_data->sem if both are
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700214 * needed */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 struct mutex data_list_mutex;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700216 struct list_head data_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 /* arena_mutex protects the global allocation arena
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700218 *
219 * IF YOU TAKE BOTH LOCKS TAKE THEM IN THIS ORDER:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 * down(pmem_data->sem) => mutex_lock(arena_mutex)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700221 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 struct mutex arena_mutex;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700223
224 long (*ioctl)(struct file *, unsigned int, unsigned long);
225 int (*release)(struct inode *, struct file *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700226 /* reference count of allocations */
227 atomic_t allocation_cnt;
228 /*
229 * request function for a region when the allocation count goes
230 * from 0 -> 1
231 */
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800232 int (*mem_request)(void *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700233 /*
234 * release function for a region when the allocation count goes
235 * from 1 -> 0
236 */
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800237 int (*mem_release)(void *);
Laura Abbott1e36a022011-06-22 17:08:13 -0700238 /*
239 * private data for the request/release callback
240 */
241 void *region_data;
242 /*
243 * map and unmap as needed
244 */
245 int map_on_demand;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700246};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247#define to_pmem_info_id(a) (container_of(a, struct pmem_info, kobj)->id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700248
Laura Abbott1e36a022011-06-22 17:08:13 -0700249static void ioremap_pmem(int id);
250static void pmem_put_region(int id);
251static int pmem_get_region(int id);
252
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700253static struct pmem_info pmem[PMEM_MAX_DEVICES];
254static int id_count;
255
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256#define PMEM_SYSFS_DIR_NAME "pmem_regions" /* under /sys/kernel/ */
257static struct kset *pmem_kset;
258
259#define PMEM_IS_FREE_BUDDY(id, index) \
260 (!(pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].allocated))
261#define PMEM_BUDDY_ORDER(id, index) \
262 (pmem[id].allocator.buddy_bestfit.buddy_bitmap[index].order)
263#define PMEM_BUDDY_INDEX(id, index) \
264 (index ^ (1 << PMEM_BUDDY_ORDER(id, index)))
265#define PMEM_BUDDY_NEXT_INDEX(id, index) \
266 (index + (1 << PMEM_BUDDY_ORDER(id, index)))
267#define PMEM_OFFSET(index) (index * pmem[id].quantum)
268#define PMEM_START_ADDR(id, index) \
269 (PMEM_OFFSET(index) + pmem[id].base)
270#define PMEM_BUDDY_LEN(id, index) \
271 ((1 << PMEM_BUDDY_ORDER(id, index)) * pmem[id].quantum)
272#define PMEM_END_ADDR(id, index) \
273 (PMEM_START_ADDR(id, index) + PMEM_LEN(id, index))
274#define PMEM_START_VADDR(id, index) \
275 (PMEM_OFFSET(id, index) + pmem[id].vbase)
276#define PMEM_END_VADDR(id, index) \
277 (PMEM_START_VADDR(id, index) + PMEM_LEN(id, index))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700278#define PMEM_REVOKED(data) (data->flags & PMEM_FLAGS_REVOKED)
279#define PMEM_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280#define PMEM_IS_SUBMAP(data) \
281 ((data->flags & PMEM_FLAGS_SUBMAP) && \
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700282 (!(data->flags & PMEM_FLAGS_UNSUBMAP)))
283
284static int pmem_release(struct inode *, struct file *);
285static int pmem_mmap(struct file *, struct vm_area_struct *);
286static int pmem_open(struct inode *, struct file *);
287static long pmem_ioctl(struct file *, unsigned int, unsigned long);
288
289struct file_operations pmem_fops = {
290 .release = pmem_release,
291 .mmap = pmem_mmap,
292 .open = pmem_open,
293 .unlocked_ioctl = pmem_ioctl,
294};
295
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296#define PMEM_ATTR(_name, _mode, _show, _store) { \
297 .attr = {.name = __stringify(_name), .mode = _mode }, \
298 .show = _show, \
299 .store = _store, \
300}
301
302struct pmem_attr {
303 struct attribute attr;
304 ssize_t(*show) (const int id, char * const);
305 ssize_t(*store) (const int id, const char * const, const size_t count);
306};
307#define to_pmem_attr(a) container_of(a, struct pmem_attr, attr)
308
309#define RW_PMEM_ATTR(name) \
310static struct pmem_attr pmem_attr_## name = \
311 PMEM_ATTR(name, S_IRUGO | S_IWUSR, show_pmem_## name, store_pmem_## name)
312
313#define RO_PMEM_ATTR(name) \
314static struct pmem_attr pmem_attr_## name = \
315 PMEM_ATTR(name, S_IRUGO, show_pmem_## name, NULL)
316
317#define WO_PMEM_ATTR(name) \
318static struct pmem_attr pmem_attr_## name = \
319 PMEM_ATTR(name, S_IWUSR, NULL, store_pmem_## name)
320
321static ssize_t show_pmem(struct kobject *kobj,
322 struct attribute *attr,
323 char *buf)
324{
325 struct pmem_attr *a = to_pmem_attr(attr);
326 return a->show ? a->show(to_pmem_info_id(kobj), buf) : -EIO;
327}
328
329static ssize_t store_pmem(struct kobject *kobj, struct attribute *attr,
330 const char *buf, size_t count)
331{
332 struct pmem_attr *a = to_pmem_attr(attr);
333 return a->store ? a->store(to_pmem_info_id(kobj), buf, count) : -EIO;
334}
335
336static struct sysfs_ops pmem_ops = {
337 .show = show_pmem,
338 .store = store_pmem,
339};
340
341static ssize_t show_pmem_base(int id, char *buf)
342{
343 return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n",
344 pmem[id].base, pmem[id].base);
345}
346RO_PMEM_ATTR(base);
347
348static ssize_t show_pmem_size(int id, char *buf)
349{
350 return scnprintf(buf, PAGE_SIZE, "%lu(%#lx)\n",
351 pmem[id].size, pmem[id].size);
352}
353RO_PMEM_ATTR(size);
354
355static ssize_t show_pmem_allocator_type(int id, char *buf)
356{
357 switch (pmem[id].allocator_type) {
358 case PMEM_ALLOCATORTYPE_ALLORNOTHING:
359 return scnprintf(buf, PAGE_SIZE, "%s\n", "All or Nothing");
360 case PMEM_ALLOCATORTYPE_BUDDYBESTFIT:
361 return scnprintf(buf, PAGE_SIZE, "%s\n", "Buddy Bestfit");
362 case PMEM_ALLOCATORTYPE_BITMAP:
363 return scnprintf(buf, PAGE_SIZE, "%s\n", "Bitmap");
364 case PMEM_ALLOCATORTYPE_SYSTEM:
365 return scnprintf(buf, PAGE_SIZE, "%s\n", "System heap");
366 default:
367 return scnprintf(buf, PAGE_SIZE,
368 "??? Invalid allocator type (%d) for this region! "
369 "Something isn't right.\n",
370 pmem[id].allocator_type);
371 }
372}
373RO_PMEM_ATTR(allocator_type);
374
375static ssize_t show_pmem_mapped_regions(int id, char *buf)
376{
377 struct list_head *elt;
378 int ret;
379
380 ret = scnprintf(buf, PAGE_SIZE,
381 "pid #: mapped regions (offset, len) (offset,len)...\n");
382
383 mutex_lock(&pmem[id].data_list_mutex);
384 list_for_each(elt, &pmem[id].data_list) {
385 struct pmem_data *data =
386 list_entry(elt, struct pmem_data, list);
387 struct list_head *elt2;
388
389 down_read(&data->sem);
390 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "pid %u:",
391 data->pid);
392 list_for_each(elt2, &data->region_list) {
393 struct pmem_region_node *region_node = list_entry(elt2,
394 struct pmem_region_node,
395 list);
396 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
397 "(%lx,%lx) ",
398 region_node->region.offset,
399 region_node->region.len);
400 }
401 up_read(&data->sem);
402 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
403 }
404 mutex_unlock(&pmem[id].data_list_mutex);
405 return ret;
406}
407RO_PMEM_ATTR(mapped_regions);
408
409#define PMEM_COMMON_SYSFS_ATTRS \
410 &pmem_attr_base.attr, \
411 &pmem_attr_size.attr, \
412 &pmem_attr_allocator_type.attr, \
413 &pmem_attr_mapped_regions.attr
414
415
416static ssize_t show_pmem_allocated(int id, char *buf)
417{
418 ssize_t ret;
419
420 mutex_lock(&pmem[id].arena_mutex);
421 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
422 pmem[id].allocator.all_or_nothing.allocated ?
423 "is allocated" : "is NOT allocated");
424 mutex_unlock(&pmem[id].arena_mutex);
425 return ret;
426}
427RO_PMEM_ATTR(allocated);
428
429static struct attribute *pmem_allornothing_attrs[] = {
430 PMEM_COMMON_SYSFS_ATTRS,
431
432 &pmem_attr_allocated.attr,
433
434 NULL
435};
436
437static struct kobj_type pmem_allornothing_ktype = {
438 .sysfs_ops = &pmem_ops,
439 .default_attrs = pmem_allornothing_attrs,
440};
441
442static ssize_t show_pmem_total_entries(int id, char *buf)
443{
444 return scnprintf(buf, PAGE_SIZE, "%lu\n", pmem[id].num_entries);
445}
446RO_PMEM_ATTR(total_entries);
447
448static ssize_t show_pmem_quantum_size(int id, char *buf)
449{
450 return scnprintf(buf, PAGE_SIZE, "%u (%#x)\n",
451 pmem[id].quantum, pmem[id].quantum);
452}
453RO_PMEM_ATTR(quantum_size);
454
455static ssize_t show_pmem_buddy_bitmap_dump(int id, char *buf)
456{
457 int ret, i;
458
459 mutex_lock(&pmem[id].data_list_mutex);
460 ret = scnprintf(buf, PAGE_SIZE, "index\torder\tlength\tallocated\n");
461
462 for (i = 0; i < pmem[id].num_entries && (PAGE_SIZE - ret);
463 i = PMEM_BUDDY_NEXT_INDEX(id, i))
464 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%d\t%d\t%d\t%d\n",
465 i, PMEM_BUDDY_ORDER(id, i),
466 PMEM_BUDDY_LEN(id, i),
467 !PMEM_IS_FREE_BUDDY(id, i));
468
469 mutex_unlock(&pmem[id].data_list_mutex);
470 return ret;
471}
472RO_PMEM_ATTR(buddy_bitmap_dump);
473
474#define PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS \
475 &pmem_attr_quantum_size.attr, \
476 &pmem_attr_total_entries.attr
477
478static struct attribute *pmem_buddy_bestfit_attrs[] = {
479 PMEM_COMMON_SYSFS_ATTRS,
480
481 PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS,
482
483 &pmem_attr_buddy_bitmap_dump.attr,
484
485 NULL
486};
487
488static struct kobj_type pmem_buddy_bestfit_ktype = {
489 .sysfs_ops = &pmem_ops,
490 .default_attrs = pmem_buddy_bestfit_attrs,
491};
492
493static ssize_t show_pmem_free_quanta(int id, char *buf)
494{
495 ssize_t ret;
496
497 mutex_lock(&pmem[id].arena_mutex);
498 ret = scnprintf(buf, PAGE_SIZE, "%u\n",
499 pmem[id].allocator.bitmap.bitmap_free);
500 mutex_unlock(&pmem[id].arena_mutex);
501 return ret;
502}
503RO_PMEM_ATTR(free_quanta);
504
505static ssize_t show_pmem_bits_allocated(int id, char *buf)
506{
507 ssize_t ret;
508 unsigned int i;
509
510 mutex_lock(&pmem[id].arena_mutex);
511
512 ret = scnprintf(buf, PAGE_SIZE,
513 "id: %d\nbitnum\tindex\tquanta allocated\n", id);
514
515 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++)
516 if (pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1)
517 ret += scnprintf(buf + ret, PAGE_SIZE - ret,
518 "%u\t%u\t%u\n",
519 i,
520 pmem[id].allocator.bitmap.bitm_alloc[i].bit,
521 pmem[id].allocator.bitmap.bitm_alloc[i].quanta
522 );
523
524 mutex_unlock(&pmem[id].arena_mutex);
525 return ret;
526}
527RO_PMEM_ATTR(bits_allocated);
528
529static struct attribute *pmem_bitmap_attrs[] = {
530 PMEM_COMMON_SYSFS_ATTRS,
531
532 PMEM_BITMAP_BUDDY_BESTFIT_COMMON_SYSFS_ATTRS,
533
534 &pmem_attr_free_quanta.attr,
535 &pmem_attr_bits_allocated.attr,
536
537 NULL
538};
539
540static struct attribute *pmem_system_attrs[] = {
541 PMEM_COMMON_SYSFS_ATTRS,
542
543 NULL
544};
545
546static struct kobj_type pmem_bitmap_ktype = {
547 .sysfs_ops = &pmem_ops,
548 .default_attrs = pmem_bitmap_attrs,
549};
550
551static struct kobj_type pmem_system_ktype = {
552 .sysfs_ops = &pmem_ops,
553 .default_attrs = pmem_system_attrs,
554};
555
Laura Abbott1e36a022011-06-22 17:08:13 -0700556static int pmem_allocate_from_id(const int id, const unsigned long size,
557 const unsigned int align)
558{
559 int ret;
560 ret = pmem_get_region(id);
561
562 if (ret)
563 return -1;
564
565 ret = pmem[id].allocate(id, size, align);
566
567 if (ret < 0)
568 pmem_put_region(id);
569
570 return ret;
571}
572
573static int pmem_free_from_id(const int id, const int index)
574{
575 pmem_put_region(id);
576 return pmem[id].free(id, index);
577}
578
579static int pmem_get_region(int id)
580{
581 /* Must be called with arena mutex locked */
582 atomic_inc(&pmem[id].allocation_cnt);
583 if (!pmem[id].vbase) {
584 DLOG("PMEMDEBUG: mapping for %s", pmem[id].name);
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800585 if (pmem[id].mem_request) {
586 int ret = pmem[id].mem_request(pmem[id].region_data);
587 if (ret) {
588 atomic_dec(&pmem[id].allocation_cnt);
589 return 1;
590 }
591 }
Laura Abbott1e36a022011-06-22 17:08:13 -0700592 ioremap_pmem(id);
593 }
594
595 if (pmem[id].vbase) {
596 return 0;
597 } else {
598 if (pmem[id].mem_release)
599 pmem[id].mem_release(pmem[id].region_data);
600 atomic_dec(&pmem[id].allocation_cnt);
601 return 1;
602 }
603}
604
605static void pmem_put_region(int id)
606{
607 /* Must be called with arena mutex locked */
608 if (atomic_dec_and_test(&pmem[id].allocation_cnt)) {
609 DLOG("PMEMDEBUG: unmapping for %s", pmem[id].name);
610 BUG_ON(!pmem[id].vbase);
611 if (pmem[id].map_on_demand) {
Naveen Ramaraj189f1882011-08-16 17:39:22 -0700612 /* unmap_kernel_range() flushes the caches
613 * and removes the page table entries
614 */
615 unmap_kernel_range((unsigned long)pmem[id].vbase,
616 pmem[id].size);
Laura Abbott1e36a022011-06-22 17:08:13 -0700617 pmem[id].vbase = NULL;
Laura Abbott72ae4bf2011-12-14 14:01:43 -0800618 if (pmem[id].mem_release) {
619 int ret = pmem[id].mem_release(
620 pmem[id].region_data);
621 WARN(ret, "mem_release failed");
622 }
Laura Abbott1e36a022011-06-22 17:08:13 -0700623
624 }
625 }
626}
627
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700628static int get_id(struct file *file)
629{
630 return MINOR(file->f_dentry->d_inode->i_rdev);
631}
632
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633static char *get_name(struct file *file)
634{
635 int id = get_id(file);
636 return pmem[id].name;
637}
638
639static int is_pmem_file(struct file *file)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700640{
641 int id;
642
643 if (unlikely(!file || !file->f_dentry || !file->f_dentry->d_inode))
644 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700646 id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 return (unlikely(id >= PMEM_MAX_DEVICES ||
648 file->f_dentry->d_inode->i_rdev !=
649 MKDEV(MISC_MAJOR, pmem[id].dev.minor))) ? 0 : 1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700650}
651
652static int has_allocation(struct file *file)
653{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 /* must be called with at least read lock held on
655 * ((struct pmem_data *)(file->private_data))->sem which
656 * means that file is guaranteed not to be NULL upon entry!!
657 * check is_pmem_file first if not accessed via pmem_file_ops */
658 struct pmem_data *pdata = file->private_data;
659 return pdata && pdata->index != -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700660}
661
662static int is_master_owner(struct file *file)
663{
664 struct file *master_file;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700666 int put_needed, ret = 0;
667
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 if (!has_allocation(file))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700669 return 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700670 if (PMEM_FLAGS_MASTERMAP & data->flags)
671 return 1;
672 master_file = fget_light(data->master_fd, &put_needed);
673 if (master_file && data->master_file == master_file)
674 ret = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675 if (master_file)
676 fput_light(master_file, put_needed);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700677 return ret;
678}
679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680static int pmem_free_all_or_nothing(int id, int index)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700681{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 /* caller should hold the lock on arena_mutex! */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700683 DLOG("index %d\n", index);
684
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685 pmem[id].allocator.all_or_nothing.allocated = 0;
686 return 0;
687}
688
689static int pmem_free_space_all_or_nothing(int id,
690 struct pmem_freespace *fs)
691{
692 /* caller should hold the lock on arena_mutex! */
693 fs->total = (unsigned long)
694 pmem[id].allocator.all_or_nothing.allocated == 0 ?
695 pmem[id].size : 0;
696
697 fs->largest = fs->total;
698 return 0;
699}
700
701
702static int pmem_free_buddy_bestfit(int id, int index)
703{
704 /* caller should hold the lock on arena_mutex! */
705 int curr = index;
706 DLOG("index %d\n", index);
707
708
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700709 /* clean up the bitmap, merging any buddies */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 pmem[id].allocator.buddy_bestfit.buddy_bitmap[curr].allocated = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700711 /* find a slots buddy Buddy# = Slot# ^ (1 << order)
712 * if the buddy is also free merge them
713 * repeat until the buddy is not free or end of the bitmap is reached
714 */
715 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 int buddy = PMEM_BUDDY_INDEX(id, curr);
717 if (buddy < pmem[id].num_entries &&
718 PMEM_IS_FREE_BUDDY(id, buddy) &&
719 PMEM_BUDDY_ORDER(id, buddy) ==
720 PMEM_BUDDY_ORDER(id, curr)) {
721 PMEM_BUDDY_ORDER(id, buddy)++;
722 PMEM_BUDDY_ORDER(id, curr)++;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700723 curr = min(buddy, curr);
724 } else {
725 break;
726 }
727 } while (curr < pmem[id].num_entries);
728
729 return 0;
730}
731
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732
733static int pmem_free_space_buddy_bestfit(int id,
734 struct pmem_freespace *fs)
735{
736 /* caller should hold the lock on arena_mutex! */
737 int curr;
738 unsigned long size;
739 fs->total = 0;
740 fs->largest = 0;
741
742 for (curr = 0; curr < pmem[id].num_entries;
743 curr = PMEM_BUDDY_NEXT_INDEX(id, curr)) {
744 if (PMEM_IS_FREE_BUDDY(id, curr)) {
745 size = PMEM_BUDDY_LEN(id, curr);
746 if (size > fs->largest)
747 fs->largest = size;
748 fs->total += size;
749 }
750 }
751 return 0;
752}
753
754
755static inline uint32_t start_mask(int bit_start)
756{
757 return (uint32_t)(~0) << (bit_start & PMEM_BITS_PER_WORD_MASK);
758}
759
760static inline uint32_t end_mask(int bit_end)
761{
762 return (uint32_t)(~0) >>
763 ((BITS_PER_LONG - bit_end) & PMEM_BITS_PER_WORD_MASK);
764}
765
766static inline int compute_total_words(int bit_end, int word_index)
767{
768 return ((bit_end + BITS_PER_LONG - 1) >>
769 PMEM_32BIT_WORD_ORDER) - word_index;
770}
771
772static void bitmap_bits_clear_all(uint32_t *bitp, int bit_start, int bit_end)
773{
774 int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words;
775
776 total_words = compute_total_words(bit_end, word_index);
777 if (total_words > 0) {
778 if (total_words == 1) {
779 bitp[word_index] &=
780 ~(start_mask(bit_start) & end_mask(bit_end));
781 } else {
782 bitp[word_index++] &= ~start_mask(bit_start);
783 if (total_words > 2) {
784 int total_bytes;
785
786 total_words -= 2;
787 total_bytes = total_words << 2;
788
789 memset(&bitp[word_index], 0, total_bytes);
790 word_index += total_words;
791 }
792 bitp[word_index] &= ~end_mask(bit_end);
793 }
794 }
795}
796
797static int pmem_free_bitmap(int id, int bitnum)
798{
799 /* caller should hold the lock on arena_mutex! */
800 int i;
801 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
802
803 DLOG("bitnum %d\n", bitnum);
804
805 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++) {
806 const int curr_bit =
807 pmem[id].allocator.bitmap.bitm_alloc[i].bit;
808
809 if (curr_bit == bitnum) {
810 const int curr_quanta =
811 pmem[id].allocator.bitmap.bitm_alloc[i].quanta;
812
813 bitmap_bits_clear_all(pmem[id].allocator.bitmap.bitmap,
814 curr_bit, curr_bit + curr_quanta);
815 pmem[id].allocator.bitmap.bitmap_free += curr_quanta;
816 pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1;
817 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
818 return 0;
819 }
820 }
821 printk(KERN_ALERT "pmem: %s: Attempt to free unallocated index %d, id"
822 " %d, pid %d(%s)\n", __func__, bitnum, id, current->pid,
823 get_task_comm(currtask_name, current));
824
825 return -1;
826}
827
828static int pmem_free_system(int id, int index)
829{
830 /* caller should hold the lock on arena_mutex! */
831 struct alloc_list *item;
832
833 DLOG("index %d\n", index);
834 if (index != 0)
835 item = (struct alloc_list *)index;
836 else
837 return 0;
838
839 if (item->vaddr != NULL) {
840 iounmap(item->vaddr);
841 kfree(__va(item->addr));
842 list_del(&item->allocs);
843 kfree(item);
844 }
845
846 return 0;
847}
848
849static int pmem_free_space_bitmap(int id, struct pmem_freespace *fs)
850{
851 int i, j;
852 int max_allocs = pmem[id].allocator.bitmap.bitmap_allocs;
853 int alloc_start = 0;
854 int next_alloc;
855 unsigned long size = 0;
856
857 fs->total = 0;
858 fs->largest = 0;
859
860 for (i = 0; i < max_allocs; i++) {
861
862 int alloc_quanta = 0;
863 int alloc_idx = 0;
864 next_alloc = pmem[id].num_entries;
865
866 /* Look for the lowest bit where next allocation starts */
867 for (j = 0; j < max_allocs; j++) {
868 const int curr_alloc = pmem[id].allocator.
869 bitmap.bitm_alloc[j].bit;
870 if (curr_alloc != -1) {
871 if (alloc_start == curr_alloc)
872 alloc_idx = j;
873 if (alloc_start >= curr_alloc)
874 continue;
875 if (curr_alloc < next_alloc)
876 next_alloc = curr_alloc;
877 }
878 }
879 alloc_quanta = pmem[id].allocator.bitmap.
880 bitm_alloc[alloc_idx].quanta;
881 size = (next_alloc - (alloc_start + alloc_quanta)) *
882 pmem[id].quantum;
883
884 if (size > fs->largest)
885 fs->largest = size;
886 fs->total += size;
887
888 if (next_alloc == pmem[id].num_entries)
889 break;
890 else
891 alloc_start = next_alloc;
892 }
893
894 return 0;
895}
896
897static int pmem_free_space_system(int id, struct pmem_freespace *fs)
898{
899 fs->total = pmem[id].size;
900 fs->largest = pmem[id].size;
901
902 return 0;
903}
904
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700905static void pmem_revoke(struct file *file, struct pmem_data *data);
906
907static int pmem_release(struct inode *inode, struct file *file)
908{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700910 struct pmem_region_node *region_node;
911 struct list_head *elt, *elt2;
912 int id = get_id(file), ret = 0;
913
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914#if PMEM_DEBUG_MSGS
915 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
916#endif
917 DLOG("releasing memory pid %u(%s) file %p(%ld) dev %s(id: %d)\n",
918 current->pid, get_task_comm(currtask_name, current),
919 file, file_count(file), get_name(file), id);
920 mutex_lock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700921 /* if this file is a master, revoke all the memory in the connected
922 * files */
923 if (PMEM_FLAGS_MASTERMAP & data->flags) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700924 list_for_each(elt, &pmem[id].data_list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 struct pmem_data *sub_data =
926 list_entry(elt, struct pmem_data, list);
927 int is_master;
928
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700929 down_read(&sub_data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 is_master = (PMEM_IS_SUBMAP(sub_data) &&
931 file == sub_data->master_file);
932 up_read(&sub_data->sem);
933
934 if (is_master)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700935 pmem_revoke(file, sub_data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700936 }
937 }
938 list_del(&data->list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 mutex_unlock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700940
941 down_write(&data->sem);
942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 /* if it is not a connected file and it has an allocation, free it */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700944 if (!(PMEM_FLAGS_CONNECTED & data->flags) && has_allocation(file)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -0700946 ret = pmem_free_from_id(id, data->index);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947 mutex_unlock(&pmem[id].arena_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700948 }
949
950 /* if this file is a submap (mapped, connected file), downref the
951 * task struct */
952 if (PMEM_FLAGS_SUBMAP & data->flags)
953 if (data->task) {
954 put_task_struct(data->task);
955 data->task = NULL;
956 }
957
958 file->private_data = NULL;
959
960 list_for_each_safe(elt, elt2, &data->region_list) {
961 region_node = list_entry(elt, struct pmem_region_node, list);
962 list_del(elt);
963 kfree(region_node);
964 }
965 BUG_ON(!list_empty(&data->region_list));
966
967 up_write(&data->sem);
968 kfree(data);
969 if (pmem[id].release)
970 ret = pmem[id].release(inode, file);
971
972 return ret;
973}
974
975static int pmem_open(struct inode *inode, struct file *file)
976{
977 struct pmem_data *data;
978 int id = get_id(file);
979 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980#if PMEM_DEBUG_MSGS
981 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
982#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700983
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 DLOG("pid %u(%s) file %p(%ld) dev %s(id: %d)\n",
985 current->pid, get_task_comm(currtask_name, current),
986 file, file_count(file), get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700987 data = kmalloc(sizeof(struct pmem_data), GFP_KERNEL);
988 if (!data) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 printk(KERN_ALERT "pmem: %s: unable to allocate memory for "
990 "pmem metadata.", __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -0700991 return -1;
992 }
993 data->flags = 0;
994 data->index = -1;
995 data->task = NULL;
996 data->vma = NULL;
997 data->pid = 0;
998 data->master_file = NULL;
999#if PMEM_DEBUG
1000 data->ref = 0;
1001#endif
1002 INIT_LIST_HEAD(&data->region_list);
1003 init_rwsem(&data->sem);
1004
1005 file->private_data = data;
1006 INIT_LIST_HEAD(&data->list);
1007
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 mutex_lock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001009 list_add(&data->list, &pmem[id].data_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 mutex_unlock(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001011 return ret;
1012}
1013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014static unsigned long pmem_order(unsigned long len, int id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001015{
1016 int i;
1017
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 len = (len + pmem[id].quantum - 1)/pmem[id].quantum;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001019 len--;
1020 for (i = 0; i < sizeof(len)*8; i++)
1021 if (len >> i == 0)
1022 break;
1023 return i;
1024}
1025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026static int pmem_allocator_all_or_nothing(const int id,
1027 const unsigned long len,
1028 const unsigned int align)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001029{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030 /* caller should hold the lock on arena_mutex! */
1031 DLOG("all or nothing\n");
1032 if ((len > pmem[id].size) ||
1033 pmem[id].allocator.all_or_nothing.allocated)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001034 return -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 pmem[id].allocator.all_or_nothing.allocated = 1;
1036 return len;
1037}
1038
1039static int pmem_allocator_buddy_bestfit(const int id,
1040 const unsigned long len,
1041 unsigned int align)
1042{
1043 /* caller should hold the lock on arena_mutex! */
1044 int curr;
1045 int best_fit = -1;
1046 unsigned long order;
1047
1048 DLOG("buddy bestfit\n");
1049 order = pmem_order(len, id);
1050 if (order > PMEM_MAX_ORDER)
1051 goto out;
1052
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001053 DLOG("order %lx\n", order);
1054
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055 /* Look through the bitmap.
1056 * If a free slot of the correct order is found, use it.
1057 * Otherwise, use the best fit (smallest with size > order) slot.
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001058 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 for (curr = 0;
1060 curr < pmem[id].num_entries;
1061 curr = PMEM_BUDDY_NEXT_INDEX(id, curr))
1062 if (PMEM_IS_FREE_BUDDY(id, curr)) {
1063 if (PMEM_BUDDY_ORDER(id, curr) ==
1064 (unsigned char)order) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001065 /* set the not free bit and clear others */
1066 best_fit = curr;
1067 break;
1068 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 if (PMEM_BUDDY_ORDER(id, curr) >
1070 (unsigned char)order &&
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001071 (best_fit < 0 ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 PMEM_BUDDY_ORDER(id, curr) <
1073 PMEM_BUDDY_ORDER(id, best_fit)))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001074 best_fit = curr;
1075 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 /* if best_fit < 0, there are no suitable slots; return an error */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001078 if (best_fit < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079#if PMEM_DEBUG
1080 printk(KERN_ALERT "pmem: %s: no space left to allocate!\n",
1081 __func__);
1082#endif
1083 goto out;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001084 }
1085
1086 /* now partition the best fit:
1087 * split the slot into 2 buddies of order - 1
1088 * repeat until the slot is of the correct order
1089 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 while (PMEM_BUDDY_ORDER(id, best_fit) > (unsigned char)order) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001091 int buddy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 PMEM_BUDDY_ORDER(id, best_fit) -= 1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001093 buddy = PMEM_BUDDY_INDEX(id, best_fit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 PMEM_BUDDY_ORDER(id, buddy) = PMEM_BUDDY_ORDER(id, best_fit);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001095 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 pmem[id].allocator.buddy_bestfit.buddy_bitmap[best_fit].allocated = 1;
1097out:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001098 return best_fit;
1099}
1100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101
1102static inline unsigned long paddr_from_bit(const int id, const int bitnum)
1103{
1104 return pmem[id].base + pmem[id].quantum * bitnum;
1105}
1106
1107static inline unsigned long bit_from_paddr(const int id,
1108 const unsigned long paddr)
1109{
1110 return (paddr - pmem[id].base) / pmem[id].quantum;
1111}
1112
1113static void bitmap_bits_set_all(uint32_t *bitp, int bit_start, int bit_end)
1114{
1115 int word_index = bit_start >> PMEM_32BIT_WORD_ORDER, total_words;
1116
1117 total_words = compute_total_words(bit_end, word_index);
1118 if (total_words > 0) {
1119 if (total_words == 1) {
1120 bitp[word_index] |=
1121 (start_mask(bit_start) & end_mask(bit_end));
1122 } else {
1123 bitp[word_index++] |= start_mask(bit_start);
1124 if (total_words > 2) {
1125 int total_bytes;
1126
1127 total_words -= 2;
1128 total_bytes = total_words << 2;
1129
1130 memset(&bitp[word_index], ~0, total_bytes);
1131 word_index += total_words;
1132 }
1133 bitp[word_index] |= end_mask(bit_end);
1134 }
1135 }
1136}
1137
1138static int
1139bitmap_allocate_contiguous(uint32_t *bitp, int num_bits_to_alloc,
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001140 int total_bits, int spacing, int start_bit)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141{
1142 int bit_start, last_bit, word_index;
1143
1144 if (num_bits_to_alloc <= 0)
1145 return -1;
1146
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001147 for (bit_start = start_bit; ;
1148 bit_start = ((last_bit +
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 (word_index << PMEM_32BIT_WORD_ORDER) + spacing - 1)
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001150 & ~(spacing - 1)) + start_bit) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 int bit_end = bit_start + num_bits_to_alloc, total_words;
1152
1153 if (bit_end > total_bits)
1154 return -1; /* out of contiguous memory */
1155
1156 word_index = bit_start >> PMEM_32BIT_WORD_ORDER;
1157 total_words = compute_total_words(bit_end, word_index);
1158
1159 if (total_words <= 0)
1160 return -1;
1161
1162 if (total_words == 1) {
1163 last_bit = fls(bitp[word_index] &
1164 (start_mask(bit_start) &
1165 end_mask(bit_end)));
1166 if (last_bit)
1167 continue;
1168 } else {
1169 int end_word = word_index + (total_words - 1);
1170 last_bit =
1171 fls(bitp[word_index] & start_mask(bit_start));
1172 if (last_bit)
1173 continue;
1174
1175 for (word_index++;
1176 word_index < end_word;
1177 word_index++) {
1178 last_bit = fls(bitp[word_index]);
1179 if (last_bit)
1180 break;
1181 }
1182 if (last_bit)
1183 continue;
1184
1185 last_bit = fls(bitp[word_index] & end_mask(bit_end));
1186 if (last_bit)
1187 continue;
1188 }
1189 bitmap_bits_set_all(bitp, bit_start, bit_end);
1190 return bit_start;
1191 }
1192 return -1;
1193}
1194
1195static int reserve_quanta(const unsigned int quanta_needed,
1196 const int id,
1197 unsigned int align)
1198{
1199 /* alignment should be a valid power of 2 */
1200 int ret = -1, start_bit = 0, spacing = 1;
1201
1202 /* Sanity check */
1203 if (quanta_needed > pmem[id].allocator.bitmap.bitmap_free) {
1204#if PMEM_DEBUG
1205 printk(KERN_ALERT "pmem: %s: request (%d) too big for"
1206 " available free (%d)\n", __func__, quanta_needed,
1207 pmem[id].allocator.bitmap.bitmap_free);
1208#endif
1209 return -1;
1210 }
1211
1212 start_bit = bit_from_paddr(id,
1213 (pmem[id].base + align - 1) & ~(align - 1));
1214 if (start_bit <= -1) {
1215#if PMEM_DEBUG
1216 printk(KERN_ALERT
1217 "pmem: %s: bit_from_paddr fails for"
1218 " %u alignment.\n", __func__, align);
1219#endif
1220 return -1;
1221 }
1222 spacing = align / pmem[id].quantum;
1223 spacing = spacing > 1 ? spacing : 1;
1224
1225 ret = bitmap_allocate_contiguous(pmem[id].allocator.bitmap.bitmap,
1226 quanta_needed,
1227 (pmem[id].size + pmem[id].quantum - 1) / pmem[id].quantum,
Laura Abbott6b3eb1a2011-06-12 13:29:08 -07001228 spacing,
1229 start_bit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230
1231#if PMEM_DEBUG
1232 if (ret < 0)
1233 printk(KERN_ALERT "pmem: %s: not enough contiguous bits free "
1234 "in bitmap! Region memory is either too fragmented or"
1235 " request is too large for available memory.\n",
1236 __func__);
1237#endif
1238
1239 return ret;
1240}
1241
1242static int pmem_allocator_bitmap(const int id,
1243 const unsigned long len,
1244 const unsigned int align)
1245{
1246 /* caller should hold the lock on arena_mutex! */
1247 int bitnum, i;
1248 unsigned int quanta_needed;
1249
1250 DLOG("bitmap id %d, len %ld, align %u\n", id, len, align);
1251 if (!pmem[id].allocator.bitmap.bitm_alloc) {
1252#if PMEM_DEBUG
1253 printk(KERN_ALERT "pmem: bitm_alloc not present! id: %d\n",
1254 id);
1255#endif
1256 return -1;
1257 }
1258
1259 quanta_needed = (len + pmem[id].quantum - 1) / pmem[id].quantum;
1260 DLOG("quantum size %u quanta needed %u free %u id %d\n",
1261 pmem[id].quantum, quanta_needed,
1262 pmem[id].allocator.bitmap.bitmap_free, id);
1263
1264 if (pmem[id].allocator.bitmap.bitmap_free < quanta_needed) {
1265#if PMEM_DEBUG
1266 printk(KERN_ALERT "pmem: memory allocation failure. "
1267 "PMEM memory region exhausted, id %d."
1268 " Unable to comply with allocation request.\n", id);
1269#endif
1270 return -1;
1271 }
1272
1273 bitnum = reserve_quanta(quanta_needed, id, align);
1274 if (bitnum == -1)
1275 goto leave;
1276
1277 for (i = 0;
1278 i < pmem[id].allocator.bitmap.bitmap_allocs &&
1279 pmem[id].allocator.bitmap.bitm_alloc[i].bit != -1;
1280 i++)
1281 ;
1282
1283 if (i >= pmem[id].allocator.bitmap.bitmap_allocs) {
1284 void *temp;
1285 int32_t new_bitmap_allocs =
1286 pmem[id].allocator.bitmap.bitmap_allocs << 1;
1287 int j;
1288
1289 if (!new_bitmap_allocs) { /* failed sanity check!! */
1290#if PMEM_DEBUG
1291 pr_alert("pmem: bitmap_allocs number"
1292 " wrapped around to zero! Something "
1293 "is VERY wrong.\n");
1294#endif
1295 return -1;
1296 }
1297
1298 if (new_bitmap_allocs > pmem[id].num_entries) {
1299 /* failed sanity check!! */
1300#if PMEM_DEBUG
1301 pr_alert("pmem: required bitmap_allocs"
1302 " number exceeds maximum entries possible"
1303 " for current quanta\n");
1304#endif
1305 return -1;
1306 }
1307
1308 temp = krealloc(pmem[id].allocator.bitmap.bitm_alloc,
1309 new_bitmap_allocs *
1310 sizeof(*pmem[id].allocator.bitmap.bitm_alloc),
1311 GFP_KERNEL);
1312 if (!temp) {
1313#if PMEM_DEBUG
1314 pr_alert("pmem: can't realloc bitmap_allocs,"
1315 "id %d, current num bitmap allocs %d\n",
1316 id, pmem[id].allocator.bitmap.bitmap_allocs);
1317#endif
1318 return -1;
1319 }
1320 pmem[id].allocator.bitmap.bitmap_allocs = new_bitmap_allocs;
1321 pmem[id].allocator.bitmap.bitm_alloc = temp;
1322
1323 for (j = i; j < new_bitmap_allocs; j++) {
1324 pmem[id].allocator.bitmap.bitm_alloc[j].bit = -1;
1325 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
1326 }
1327
1328 DLOG("increased # of allocated regions to %d for id %d\n",
1329 pmem[id].allocator.bitmap.bitmap_allocs, id);
1330 }
1331
1332 DLOG("bitnum %d, bitm_alloc index %d\n", bitnum, i);
1333
1334 pmem[id].allocator.bitmap.bitmap_free -= quanta_needed;
1335 pmem[id].allocator.bitmap.bitm_alloc[i].bit = bitnum;
1336 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = quanta_needed;
1337leave:
1338 return bitnum;
1339}
1340
1341static int pmem_allocator_system(const int id,
1342 const unsigned long len,
1343 const unsigned int align)
1344{
1345 /* caller should hold the lock on arena_mutex! */
1346 struct alloc_list *list;
1347 unsigned long aligned_len;
1348 int count = SYSTEM_ALLOC_RETRY;
1349 void *buf;
1350
1351 DLOG("system id %d, len %ld, align %u\n", id, len, align);
1352
1353 if ((pmem[id].allocator.system_mem.used + len) > pmem[id].size) {
1354 DLOG("requested size would be larger than quota\n");
1355 return -1;
1356 }
1357
1358 /* Handle alignment */
1359 aligned_len = len + align;
1360
1361 /* Attempt allocation */
1362 list = kmalloc(sizeof(struct alloc_list), GFP_KERNEL);
1363 if (list == NULL) {
1364 printk(KERN_ERR "pmem: failed to allocate system metadata\n");
1365 return -1;
1366 }
1367 list->vaddr = NULL;
1368
1369 buf = NULL;
1370 while ((buf == NULL) && count--) {
1371 buf = kmalloc((aligned_len), GFP_KERNEL);
1372 if (buf == NULL) {
1373 DLOG("pmem: kmalloc %d temporarily failed len= %ld\n",
1374 count, aligned_len);
1375 }
1376 }
1377 if (!buf) {
1378 printk(KERN_CRIT "pmem: kmalloc failed for id= %d len= %ld\n",
1379 id, aligned_len);
1380 kfree(list);
1381 return -1;
1382 }
1383 list->size = aligned_len;
1384 list->addr = (void *)__pa(buf);
1385 list->aaddr = (void *)(((unsigned int)(list->addr) + (align - 1)) &
1386 ~(align - 1));
1387
1388 if (!pmem[id].cached)
1389 list->vaddr = ioremap(__pa(buf), aligned_len);
1390 else
1391 list->vaddr = ioremap_cached(__pa(buf), aligned_len);
1392
1393 INIT_LIST_HEAD(&list->allocs);
1394 list_add(&list->allocs, &pmem[id].allocator.system_mem.alist);
1395
1396 return (int)list;
1397}
1398
1399static pgprot_t pmem_phys_mem_access_prot(struct file *file, pgprot_t vma_prot)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001400{
1401 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402#ifdef pgprot_writecombine
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001403 if (pmem[id].cached == 0 || file->f_flags & O_SYNC)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404 /* on ARMv6 and ARMv7 this expands to Normal Noncached */
1405 return pgprot_writecombine(vma_prot);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001406#endif
1407#ifdef pgprot_ext_buffered
1408 else if (pmem[id].buffered)
1409 return pgprot_ext_buffered(vma_prot);
1410#endif
1411 return vma_prot;
1412}
1413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414static unsigned long pmem_start_addr_all_or_nothing(int id,
1415 struct pmem_data *data)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001416{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 return PMEM_START_ADDR(id, 0);
1418}
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001419
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420static unsigned long pmem_start_addr_buddy_bestfit(int id,
1421 struct pmem_data *data)
1422{
1423 return PMEM_START_ADDR(id, data->index);
1424}
1425
1426static unsigned long pmem_start_addr_bitmap(int id, struct pmem_data *data)
1427{
1428 return data->index * pmem[id].quantum + pmem[id].base;
1429}
1430
1431static unsigned long pmem_start_addr_system(int id, struct pmem_data *data)
1432{
1433 return (unsigned long)(((struct alloc_list *)(data->index))->aaddr);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001434}
1435
1436static void *pmem_start_vaddr(int id, struct pmem_data *data)
1437{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM)
1439 return ((struct alloc_list *)(data->index))->vaddr;
1440 else
1441 return pmem[id].start_addr(id, data) - pmem[id].base + pmem[id].vbase;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001442}
1443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444static unsigned long pmem_len_all_or_nothing(int id, struct pmem_data *data)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001445{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001446 return data->index;
1447}
1448
1449static unsigned long pmem_len_buddy_bestfit(int id, struct pmem_data *data)
1450{
1451 return PMEM_BUDDY_LEN(id, data->index);
1452}
1453
1454static unsigned long pmem_len_bitmap(int id, struct pmem_data *data)
1455{
1456 int i;
1457 unsigned long ret = 0;
1458
1459 mutex_lock(&pmem[id].arena_mutex);
1460
1461 for (i = 0; i < pmem[id].allocator.bitmap.bitmap_allocs; i++)
1462 if (pmem[id].allocator.bitmap.bitm_alloc[i].bit ==
1463 data->index) {
1464 ret = pmem[id].allocator.bitmap.bitm_alloc[i].quanta *
1465 pmem[id].quantum;
1466 break;
1467 }
1468
1469 mutex_unlock(&pmem[id].arena_mutex);
1470#if PMEM_DEBUG
1471 if (i >= pmem[id].allocator.bitmap.bitmap_allocs)
1472 pr_alert("pmem: %s: can't find bitnum %d in "
1473 "alloc'd array!\n", __func__, data->index);
1474#endif
1475 return ret;
1476}
1477
1478static unsigned long pmem_len_system(int id, struct pmem_data *data)
1479{
1480 unsigned long ret = 0;
1481
1482 mutex_lock(&pmem[id].arena_mutex);
1483
1484 ret = ((struct alloc_list *)data->index)->size;
1485 mutex_unlock(&pmem[id].arena_mutex);
1486
1487 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001488}
1489
1490static int pmem_map_garbage(int id, struct vm_area_struct *vma,
1491 struct pmem_data *data, unsigned long offset,
1492 unsigned long len)
1493{
1494 int i, garbage_pages = len >> PAGE_SHIFT;
1495
1496 vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP | VM_SHARED | VM_WRITE;
1497 for (i = 0; i < garbage_pages; i++) {
1498 if (vm_insert_pfn(vma, vma->vm_start + offset + (i * PAGE_SIZE),
1499 pmem[id].garbage_pfn))
1500 return -EAGAIN;
1501 }
1502 return 0;
1503}
1504
1505static int pmem_unmap_pfn_range(int id, struct vm_area_struct *vma,
1506 struct pmem_data *data, unsigned long offset,
1507 unsigned long len)
1508{
1509 int garbage_pages;
1510 DLOG("unmap offset %lx len %lx\n", offset, len);
1511
1512 BUG_ON(!PMEM_IS_PAGE_ALIGNED(len));
1513
1514 garbage_pages = len >> PAGE_SHIFT;
1515 zap_page_range(vma, vma->vm_start + offset, len, NULL);
1516 pmem_map_garbage(id, vma, data, offset, len);
1517 return 0;
1518}
1519
1520static int pmem_map_pfn_range(int id, struct vm_area_struct *vma,
1521 struct pmem_data *data, unsigned long offset,
1522 unsigned long len)
1523{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524 int ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001525 DLOG("map offset %lx len %lx\n", offset, len);
1526 BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_start));
1527 BUG_ON(!PMEM_IS_PAGE_ALIGNED(vma->vm_end));
1528 BUG_ON(!PMEM_IS_PAGE_ALIGNED(len));
1529 BUG_ON(!PMEM_IS_PAGE_ALIGNED(offset));
1530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 ret = io_remap_pfn_range(vma, vma->vm_start + offset,
1532 (pmem[id].start_addr(id, data) + offset) >> PAGE_SHIFT,
1533 len, vma->vm_page_prot);
1534 if (ret) {
1535#if PMEM_DEBUG
1536 pr_alert("pmem: %s: io_remap_pfn_range fails with "
1537 "return value: %d!\n", __func__, ret);
1538#endif
1539
1540 ret = -EAGAIN;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001541 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001543}
1544
1545static int pmem_remap_pfn_range(int id, struct vm_area_struct *vma,
1546 struct pmem_data *data, unsigned long offset,
1547 unsigned long len)
1548{
1549 /* hold the mm semp for the vma you are modifying when you call this */
1550 BUG_ON(!vma);
1551 zap_page_range(vma, vma->vm_start + offset, len, NULL);
1552 return pmem_map_pfn_range(id, vma, data, offset, len);
1553}
1554
1555static void pmem_vma_open(struct vm_area_struct *vma)
1556{
1557 struct file *file = vma->vm_file;
1558 struct pmem_data *data = file->private_data;
1559 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560
1561#if PMEM_DEBUG_MSGS
1562 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1563#endif
1564 DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n",
1565 get_name(file), id, current->pid,
1566 get_task_comm(currtask_name, current),
1567 current->parent->pid, file, file_count(file));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001568 /* this should never be called as we don't support copying pmem
1569 * ranges via fork */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001571 BUG_ON(!has_allocation(file));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001572 /* remap the garbage pages, forkers don't get access to the data */
1573 pmem_unmap_pfn_range(id, vma, data, 0, vma->vm_start - vma->vm_end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001575}
1576
1577static void pmem_vma_close(struct vm_area_struct *vma)
1578{
1579 struct file *file = vma->vm_file;
1580 struct pmem_data *data = file->private_data;
1581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582#if PMEM_DEBUG_MSGS
1583 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1584#endif
1585 DLOG("Dev %s(id: %d) pid %u(%s) ppid %u file %p count %ld\n",
1586 get_name(file), get_id(file), current->pid,
1587 get_task_comm(currtask_name, current),
1588 current->parent->pid, file, file_count(file));
1589
1590 if (unlikely(!is_pmem_file(file))) {
1591 pr_warning("pmem: something is very wrong, you are "
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001592 "closing a vm backing an allocation that doesn't "
1593 "exist!\n");
1594 return;
1595 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001597 down_write(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 if (unlikely(!has_allocation(file))) {
1599 up_write(&data->sem);
1600 pr_warning("pmem: something is very wrong, you are "
1601 "closing a vm backing an allocation that doesn't "
1602 "exist!\n");
1603 return;
1604 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001605 if (data->vma == vma) {
1606 data->vma = NULL;
1607 if ((data->flags & PMEM_FLAGS_CONNECTED) &&
1608 (data->flags & PMEM_FLAGS_SUBMAP))
1609 data->flags |= PMEM_FLAGS_UNSUBMAP;
1610 }
1611 /* the kernel is going to free this vma now anyway */
1612 up_write(&data->sem);
1613}
1614
1615static struct vm_operations_struct vm_ops = {
1616 .open = pmem_vma_open,
1617 .close = pmem_vma_close,
1618};
1619
1620static int pmem_mmap(struct file *file, struct vm_area_struct *vma)
1621{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622 struct pmem_data *data = file->private_data;
Laura Abbott1e36a022011-06-22 17:08:13 -07001623 int index = -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001624 unsigned long vma_size = vma->vm_end - vma->vm_start;
1625 int ret = 0, id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626#if PMEM_DEBUG_MSGS
1627 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1628#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001629
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630 if (!data) {
1631 pr_err("pmem: Invalid file descriptor, no private data\n");
1632 return -EINVAL;
1633 }
1634 DLOG("pid %u(%s) mmap vma_size %lu on dev %s(id: %d)\n", current->pid,
1635 get_task_comm(currtask_name, current), vma_size,
1636 get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001637 if (vma->vm_pgoff || !PMEM_IS_PAGE_ALIGNED(vma_size)) {
1638#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001639 pr_err("pmem: mmaps must be at offset zero, aligned"
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001640 " and a multiple of pages_size.\n");
1641#endif
1642 return -EINVAL;
1643 }
1644
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001645 down_write(&data->sem);
1646 /* check this file isn't already mmaped, for submaps check this file
1647 * has never been mmaped */
1648 if ((data->flags & PMEM_FLAGS_SUBMAP) ||
1649 (data->flags & PMEM_FLAGS_UNSUBMAP)) {
1650#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001651 pr_err("pmem: you can only mmap a pmem file once, "
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001652 "this file is already mmaped. %x\n", data->flags);
1653#endif
1654 ret = -EINVAL;
1655 goto error;
1656 }
1657 /* if file->private_data == unalloced, alloc*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 if (data->index == -1) {
1659 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07001660 index = pmem_allocate_from_id(id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661 vma->vm_end - vma->vm_start,
1662 SZ_4K);
1663 mutex_unlock(&pmem[id].arena_mutex);
1664 /* either no space was available or an error occured */
1665 if (index == -1) {
1666 pr_err("pmem: mmap unable to allocate memory"
1667 "on %s\n", get_name(file));
1668 ret = -ENOMEM;
1669 goto error;
1670 }
1671 /* store the index of a successful allocation */
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001672 data->index = index;
1673 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001674
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 if (pmem[id].len(id, data) < vma_size) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001676#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 pr_err("pmem: mmap size [%lu] does not match"
1678 " size of backing region [%lu].\n", vma_size,
1679 pmem[id].len(id, data));
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001680#endif
1681 ret = -EINVAL;
1682 goto error;
1683 }
1684
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 vma->vm_pgoff = pmem[id].start_addr(id, data) >> PAGE_SHIFT;
1686
1687 vma->vm_page_prot = pmem_phys_mem_access_prot(file, vma->vm_page_prot);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001688
1689 if (data->flags & PMEM_FLAGS_CONNECTED) {
1690 struct pmem_region_node *region_node;
1691 struct list_head *elt;
1692 if (pmem_map_garbage(id, vma, data, 0, vma_size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001693 pr_alert("pmem: mmap failed in kernel!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001694 ret = -EAGAIN;
1695 goto error;
1696 }
1697 list_for_each(elt, &data->region_list) {
1698 region_node = list_entry(elt, struct pmem_region_node,
1699 list);
1700 DLOG("remapping file: %p %lx %lx\n", file,
1701 region_node->region.offset,
1702 region_node->region.len);
1703 if (pmem_remap_pfn_range(id, vma, data,
1704 region_node->region.offset,
1705 region_node->region.len)) {
1706 ret = -EAGAIN;
1707 goto error;
1708 }
1709 }
1710 data->flags |= PMEM_FLAGS_SUBMAP;
1711 get_task_struct(current->group_leader);
1712 data->task = current->group_leader;
1713 data->vma = vma;
1714#if PMEM_DEBUG
1715 data->pid = current->pid;
1716#endif
1717 DLOG("submmapped file %p vma %p pid %u\n", file, vma,
1718 current->pid);
1719 } else {
1720 if (pmem_map_pfn_range(id, vma, data, 0, vma_size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 pr_err("pmem: mmap failed in kernel!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001722 ret = -EAGAIN;
1723 goto error;
1724 }
1725 data->flags |= PMEM_FLAGS_MASTERMAP;
1726 data->pid = current->pid;
1727 }
1728 vma->vm_ops = &vm_ops;
1729error:
1730 up_write(&data->sem);
1731 return ret;
1732}
1733
1734/* the following are the api for accessing pmem regions by other drivers
1735 * from inside the kernel */
1736int get_pmem_user_addr(struct file *file, unsigned long *start,
1737 unsigned long *len)
1738{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739 int ret = -1;
1740
1741 if (is_pmem_file(file)) {
1742 struct pmem_data *data = file->private_data;
1743
1744 down_read(&data->sem);
1745 if (has_allocation(file)) {
1746 if (data->vma) {
1747 *start = data->vma->vm_start;
1748 *len = data->vma->vm_end - data->vma->vm_start;
1749 } else {
1750 *start = *len = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001751#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001752 pr_err("pmem: %s: no vma present.\n",
1753 __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001754#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 }
1756 ret = 0;
1757 }
1758 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001759 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760
1761#if PMEM_DEBUG
1762 if (ret)
1763 pr_err("pmem: %s: requested pmem data from invalid"
1764 "file.\n", __func__);
1765#endif
1766 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001767}
1768
1769int get_pmem_addr(struct file *file, unsigned long *start,
1770 unsigned long *vstart, unsigned long *len)
1771{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 int ret = -1;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001773
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001774 if (is_pmem_file(file)) {
1775 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 down_read(&data->sem);
1778 if (has_allocation(file)) {
1779 int id = get_id(file);
1780
1781 *start = pmem[id].start_addr(id, data);
1782 *len = pmem[id].len(id, data);
1783 *vstart = (unsigned long)
1784 pmem_start_vaddr(id, data);
1785 up_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001786#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 down_write(&data->sem);
1788 data->ref++;
1789 up_write(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001790#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791 DLOG("returning start %#lx len %lu "
1792 "vstart %#lx\n",
1793 *start, *len, *vstart);
1794 ret = 0;
1795 } else {
1796 up_read(&data->sem);
1797 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001798 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001800}
1801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802int get_pmem_file(unsigned int fd, unsigned long *start, unsigned long *vstart,
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001803 unsigned long *len, struct file **filp)
1804{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001805 int ret = -1;
1806 struct file *file = fget(fd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001807
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001808 if (unlikely(file == NULL)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809 pr_err("pmem: %s: requested data from file "
1810 "descriptor that doesn't exist.\n", __func__);
1811 } else {
1812#if PMEM_DEBUG_MSGS
1813 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
1814#endif
1815 DLOG("filp %p rdev %d pid %u(%s) file %p(%ld)"
1816 " dev %s(id: %d)\n", filp,
1817 file->f_dentry->d_inode->i_rdev,
1818 current->pid, get_task_comm(currtask_name, current),
1819 file, file_count(file), get_name(file), get_id(file));
1820
1821 if (!get_pmem_addr(file, start, vstart, len)) {
1822 if (filp)
1823 *filp = file;
1824 ret = 0;
1825 } else {
1826 fput(file);
1827 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001828 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001830}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001831EXPORT_SYMBOL(get_pmem_file);
1832
1833int get_pmem_fd(int fd, unsigned long *start, unsigned long *len)
1834{
1835 unsigned long vstart;
1836 return get_pmem_file(fd, start, &vstart, len, NULL);
1837}
1838EXPORT_SYMBOL(get_pmem_fd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001839
1840void put_pmem_file(struct file *file)
1841{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842#if PMEM_DEBUG_MSGS
1843 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001844#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845 DLOG("rdev %d pid %u(%s) file %p(%ld)" " dev %s(id: %d)\n",
1846 file->f_dentry->d_inode->i_rdev, current->pid,
1847 get_task_comm(currtask_name, current), file,
1848 file_count(file), get_name(file), get_id(file));
1849 if (is_pmem_file(file)) {
1850#if PMEM_DEBUG
1851 struct pmem_data *data = file->private_data;
1852
1853 down_write(&data->sem);
1854 if (!data->ref--) {
1855 data->ref++;
1856 pr_alert("pmem: pmem_put > pmem_get %s "
1857 "(pid %d)\n",
1858 pmem[get_id(file)].dev.name, data->pid);
1859 BUG();
1860 }
1861 up_write(&data->sem);
1862#endif
1863 fput(file);
1864 }
1865}
1866EXPORT_SYMBOL(put_pmem_file);
1867
1868void put_pmem_fd(int fd)
1869{
1870 int put_needed;
1871 struct file *file = fget_light(fd, &put_needed);
1872
1873 if (file) {
1874 put_pmem_file(file);
1875 fput_light(file, put_needed);
1876 }
1877}
1878
1879void flush_pmem_fd(int fd, unsigned long offset, unsigned long len)
1880{
1881 int fput_needed;
1882 struct file *file = fget_light(fd, &fput_needed);
1883
1884 if (file) {
1885 flush_pmem_file(file, offset, len);
1886 fput_light(file, fput_needed);
1887 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001888}
1889
1890void flush_pmem_file(struct file *file, unsigned long offset, unsigned long len)
1891{
1892 struct pmem_data *data;
1893 int id;
1894 void *vaddr;
1895 struct pmem_region_node *region_node;
1896 struct list_head *elt;
1897 void *flush_start, *flush_end;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898#ifdef CONFIG_OUTER_CACHE
1899 unsigned long phy_start, phy_end;
1900#endif
1901 if (!is_pmem_file(file))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001902 return;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001903
1904 id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 if (!pmem[id].cached)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001906 return;
1907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 /* is_pmem_file fails if !file */
1909 data = file->private_data;
1910
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001911 down_read(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001912 if (!has_allocation(file))
1913 goto end;
1914
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001915 vaddr = pmem_start_vaddr(id, data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916
1917 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_SYSTEM) {
1918 dmac_flush_range(vaddr,
1919 (void *)((unsigned long)vaddr +
1920 ((struct alloc_list *)(data->index))->size));
1921#ifdef CONFIG_OUTER_CACHE
1922 phy_start = pmem_start_addr_system(id, data);
1923
1924 phy_end = phy_start +
1925 ((struct alloc_list *)(data->index))->size;
1926
1927 outer_flush_range(phy_start, phy_end);
1928#endif
1929 goto end;
1930 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001931 /* if this isn't a submmapped file, flush the whole thing */
1932 if (unlikely(!(data->flags & PMEM_FLAGS_CONNECTED))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933 dmac_flush_range(vaddr, vaddr + pmem[id].len(id, data));
1934#ifdef CONFIG_OUTER_CACHE
1935 phy_start = (unsigned long)vaddr -
1936 (unsigned long)pmem[id].vbase + pmem[id].base;
1937
1938 phy_end = phy_start + pmem[id].len(id, data);
1939
1940 outer_flush_range(phy_start, phy_end);
1941#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001942 goto end;
1943 }
1944 /* otherwise, flush the region of the file we are drawing */
1945 list_for_each(elt, &data->region_list) {
1946 region_node = list_entry(elt, struct pmem_region_node, list);
1947 if ((offset >= region_node->region.offset) &&
1948 ((offset + len) <= (region_node->region.offset +
1949 region_node->region.len))) {
1950 flush_start = vaddr + region_node->region.offset;
1951 flush_end = flush_start + region_node->region.len;
1952 dmac_flush_range(flush_start, flush_end);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953#ifdef CONFIG_OUTER_CACHE
1954
1955 phy_start = (unsigned long)flush_start -
1956 (unsigned long)pmem[id].vbase + pmem[id].base;
1957
1958 phy_end = phy_start + region_node->region.len;
1959
1960 outer_flush_range(phy_start, phy_end);
1961#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07001962 break;
1963 }
1964 }
1965end:
1966 up_read(&data->sem);
1967}
1968
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001969int pmem_cache_maint(struct file *file, unsigned int cmd,
1970 struct pmem_addr *pmem_addr)
1971{
1972 struct pmem_data *data;
1973 int id;
1974 unsigned long vaddr, paddr, length, offset,
1975 pmem_len, pmem_start_addr;
1976
1977 /* Called from kernel-space so file may be NULL */
1978 if (!file)
1979 return -EBADF;
1980
Shubhraprakash Das7788cad2011-11-21 13:02:22 -07001981 /*
1982 * check that the vaddr passed for flushing is valid
1983 * so that you don't crash the kernel
1984 */
1985 if (!pmem_addr->vaddr)
1986 return -EINVAL;
1987
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988 data = file->private_data;
1989 id = get_id(file);
1990
1991 if (!pmem[id].cached)
1992 return 0;
1993
1994 offset = pmem_addr->offset;
1995 length = pmem_addr->length;
1996
1997 down_read(&data->sem);
1998 if (!has_allocation(file)) {
1999 up_read(&data->sem);
2000 return -EINVAL;
2001 }
2002 pmem_len = pmem[id].len(id, data);
2003 pmem_start_addr = pmem[id].start_addr(id, data);
2004 up_read(&data->sem);
2005
2006 if (offset + length > pmem_len)
2007 return -EINVAL;
2008
2009 vaddr = pmem_addr->vaddr;
2010 paddr = pmem_start_addr + offset;
2011
2012 DLOG("pmem cache maint on dev %s(id: %d)"
2013 "(vaddr %lx paddr %lx len %lu bytes)\n",
2014 get_name(file), id, vaddr, paddr, length);
2015 if (cmd == PMEM_CLEAN_INV_CACHES)
2016 clean_and_invalidate_caches(vaddr,
2017 length, paddr);
2018 else if (cmd == PMEM_CLEAN_CACHES)
2019 clean_caches(vaddr, length, paddr);
2020 else if (cmd == PMEM_INV_CACHES)
2021 invalidate_caches(vaddr, length, paddr);
2022
2023 return 0;
2024}
2025EXPORT_SYMBOL(pmem_cache_maint);
2026
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002027static int pmem_connect(unsigned long connect, struct file *file)
2028{
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002029 int ret = 0, put_needed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030 struct file *src_file;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002031
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032 if (!file) {
2033 pr_err("pmem: %s: NULL file pointer passed in, "
2034 "bailing out!\n", __func__);
2035 ret = -EINVAL;
2036 goto leave;
2037 }
2038
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002039 src_file = fget_light(connect, &put_needed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002041 if (!src_file) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042 pr_err("pmem: %s: src file not found!\n", __func__);
2043 ret = -EBADF;
2044 goto leave;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002045 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 if (src_file == file) { /* degenerative case, operator error */
2048 pr_err("pmem: %s: src_file and passed in file are "
2049 "the same; refusing to connect to self!\n", __func__);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002050 ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 goto put_src_file;
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 (unlikely(!is_pmem_file(src_file))) {
2055 pr_err("pmem: %s: src file is not a pmem file!\n",
2056 __func__);
2057 ret = -EINVAL;
2058 goto put_src_file;
2059 } else {
2060 struct pmem_data *src_data = src_file->private_data;
2061
2062 if (!src_data) {
2063 pr_err("pmem: %s: src file pointer has no"
2064 "private data, bailing out!\n", __func__);
2065 ret = -EINVAL;
2066 goto put_src_file;
2067 }
2068
2069 down_read(&src_data->sem);
2070
2071 if (unlikely(!has_allocation(src_file))) {
2072 up_read(&src_data->sem);
2073 pr_err("pmem: %s: src file has no allocation!\n",
2074 __func__);
2075 ret = -EINVAL;
2076 } else {
2077 struct pmem_data *data;
2078 int src_index = src_data->index;
2079
2080 up_read(&src_data->sem);
2081
2082 data = file->private_data;
2083 if (!data) {
2084 pr_err("pmem: %s: passed in file "
2085 "pointer has no private data, bailing"
2086 " out!\n", __func__);
2087 ret = -EINVAL;
2088 goto put_src_file;
2089 }
2090
2091 down_write(&data->sem);
2092 if (has_allocation(file) &&
2093 (data->index != src_index)) {
2094 up_write(&data->sem);
2095
2096 pr_err("pmem: %s: file is already "
2097 "mapped but doesn't match this "
2098 "src_file!\n", __func__);
2099 ret = -EINVAL;
2100 } else {
2101 data->index = src_index;
2102 data->flags |= PMEM_FLAGS_CONNECTED;
2103 data->master_fd = connect;
2104 data->master_file = src_file;
2105
2106 up_write(&data->sem);
2107
2108 DLOG("connect %p to %p\n", file, src_file);
2109 }
2110 }
2111 }
2112put_src_file:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002113 fput_light(src_file, put_needed);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002114leave:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002115 return ret;
2116}
2117
2118static void pmem_unlock_data_and_mm(struct pmem_data *data,
2119 struct mm_struct *mm)
2120{
2121 up_write(&data->sem);
2122 if (mm != NULL) {
2123 up_write(&mm->mmap_sem);
2124 mmput(mm);
2125 }
2126}
2127
2128static int pmem_lock_data_and_mm(struct file *file, struct pmem_data *data,
2129 struct mm_struct **locked_mm)
2130{
2131 int ret = 0;
2132 struct mm_struct *mm = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133#if PMEM_DEBUG_MSGS
2134 char currtask_name[FIELD_SIZEOF(struct task_struct, comm) + 1];
2135#endif
2136 DLOG("pid %u(%s) file %p(%ld)\n",
2137 current->pid, get_task_comm(currtask_name, current),
2138 file, file_count(file));
2139
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002140 *locked_mm = NULL;
2141lock_mm:
2142 down_read(&data->sem);
2143 if (PMEM_IS_SUBMAP(data)) {
2144 mm = get_task_mm(data->task);
2145 if (!mm) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002146 up_read(&data->sem);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002147#if PMEM_DEBUG
2148 pr_alert("pmem: can't remap - task is gone!\n");
2149#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002150 return -1;
2151 }
2152 }
2153 up_read(&data->sem);
2154
2155 if (mm)
2156 down_write(&mm->mmap_sem);
2157
2158 down_write(&data->sem);
2159 /* check that the file didn't get mmaped before we could take the
2160 * data sem, this should be safe b/c you can only submap each file
2161 * once */
2162 if (PMEM_IS_SUBMAP(data) && !mm) {
2163 pmem_unlock_data_and_mm(data, mm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 DLOG("mapping contention, repeating mmap op\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002165 goto lock_mm;
2166 }
2167 /* now check that vma.mm is still there, it could have been
2168 * deleted by vma_close before we could get the data->sem */
2169 if ((data->flags & PMEM_FLAGS_UNSUBMAP) && (mm != NULL)) {
2170 /* might as well release this */
2171 if (data->flags & PMEM_FLAGS_SUBMAP) {
2172 put_task_struct(data->task);
2173 data->task = NULL;
2174 /* lower the submap flag to show the mm is gone */
2175 data->flags &= ~(PMEM_FLAGS_SUBMAP);
2176 }
2177 pmem_unlock_data_and_mm(data, mm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178#if PMEM_DEBUG
2179 pr_alert("pmem: vma.mm went away!\n");
2180#endif
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002181 return -1;
2182 }
2183 *locked_mm = mm;
2184 return ret;
2185}
2186
2187int pmem_remap(struct pmem_region *region, struct file *file,
2188 unsigned operation)
2189{
2190 int ret;
2191 struct pmem_region_node *region_node;
2192 struct mm_struct *mm = NULL;
2193 struct list_head *elt, *elt2;
2194 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 struct pmem_data *data;
2196
2197 DLOG("operation %#x, region offset %ld, region len %ld\n",
2198 operation, region->offset, region->len);
2199
2200 if (!is_pmem_file(file)) {
2201#if PMEM_DEBUG
2202 pr_err("pmem: remap request for non-pmem file descriptor\n");
2203#endif
2204 return -EINVAL;
2205 }
2206
2207 /* is_pmem_file fails if !file */
2208 data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002209
2210 /* pmem region must be aligned on a page boundry */
2211 if (unlikely(!PMEM_IS_PAGE_ALIGNED(region->offset) ||
2212 !PMEM_IS_PAGE_ALIGNED(region->len))) {
2213#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 pr_err("pmem: request for unaligned pmem"
2215 "suballocation %lx %lx\n",
2216 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002217#endif
2218 return -EINVAL;
2219 }
2220
2221 /* if userspace requests a region of len 0, there's nothing to do */
2222 if (region->len == 0)
2223 return 0;
2224
2225 /* lock the mm and data */
2226 ret = pmem_lock_data_and_mm(file, data, &mm);
2227 if (ret)
2228 return 0;
2229
2230 /* only the owner of the master file can remap the client fds
2231 * that back in it */
2232 if (!is_master_owner(file)) {
2233#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002234 pr_err("pmem: remap requested from non-master process\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002235#endif
2236 ret = -EINVAL;
2237 goto err;
2238 }
2239
2240 /* check that the requested range is within the src allocation */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002241 if (unlikely((region->offset > pmem[id].len(id, data)) ||
2242 (region->len > pmem[id].len(id, data)) ||
2243 (region->offset + region->len > pmem[id].len(id, data)))) {
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002244#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002245 pr_err("pmem: suballoc doesn't fit in src_file!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002246#endif
2247 ret = -EINVAL;
2248 goto err;
2249 }
2250
2251 if (operation == PMEM_MAP) {
2252 region_node = kmalloc(sizeof(struct pmem_region_node),
2253 GFP_KERNEL);
2254 if (!region_node) {
2255 ret = -ENOMEM;
2256#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002257 pr_alert("pmem: No space to allocate remap metadata!");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002258#endif
2259 goto err;
2260 }
2261 region_node->region = *region;
2262 list_add(&region_node->list, &data->region_list);
2263 } else if (operation == PMEM_UNMAP) {
2264 int found = 0;
2265 list_for_each_safe(elt, elt2, &data->region_list) {
2266 region_node = list_entry(elt, struct pmem_region_node,
2267 list);
2268 if (region->len == 0 ||
2269 (region_node->region.offset == region->offset &&
2270 region_node->region.len == region->len)) {
2271 list_del(elt);
2272 kfree(region_node);
2273 found = 1;
2274 }
2275 }
2276 if (!found) {
2277#if PMEM_DEBUG
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002278 pr_err("pmem: Unmap region does not map any"
2279 " mapped region!");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002280#endif
2281 ret = -EINVAL;
2282 goto err;
2283 }
2284 }
2285
2286 if (data->vma && PMEM_IS_SUBMAP(data)) {
2287 if (operation == PMEM_MAP)
2288 ret = pmem_remap_pfn_range(id, data->vma, data,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002290 else if (operation == PMEM_UNMAP)
2291 ret = pmem_unmap_pfn_range(id, data->vma, data,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002292 region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002293 }
2294
2295err:
2296 pmem_unlock_data_and_mm(data, mm);
2297 return ret;
2298}
2299
2300static void pmem_revoke(struct file *file, struct pmem_data *data)
2301{
2302 struct pmem_region_node *region_node;
2303 struct list_head *elt, *elt2;
2304 struct mm_struct *mm = NULL;
2305 int id = get_id(file);
2306 int ret = 0;
2307
2308 data->master_file = NULL;
2309 ret = pmem_lock_data_and_mm(file, data, &mm);
2310 /* if lock_data_and_mm fails either the task that mapped the fd, or
2311 * the vma that mapped it have already gone away, nothing more
2312 * needs to be done */
2313 if (ret)
2314 return;
2315 /* unmap everything */
2316 /* delete the regions and region list nothing is mapped any more */
2317 if (data->vma)
2318 list_for_each_safe(elt, elt2, &data->region_list) {
2319 region_node = list_entry(elt, struct pmem_region_node,
2320 list);
2321 pmem_unmap_pfn_range(id, data->vma, data,
2322 region_node->region.offset,
2323 region_node->region.len);
2324 list_del(elt);
2325 kfree(region_node);
2326 }
2327 /* delete the master file */
2328 pmem_unlock_data_and_mm(data, mm);
2329}
2330
2331static void pmem_get_size(struct pmem_region *region, struct file *file)
2332{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333 /* called via ioctl file op, so file guaranteed to be not NULL */
2334 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002335 int id = get_id(file);
2336
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002338 if (!has_allocation(file)) {
2339 region->offset = 0;
2340 region->len = 0;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002341 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342 region->offset = pmem[id].start_addr(id, data);
2343 region->len = pmem[id].len(id, data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002344 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 up_read(&data->sem);
2346 DLOG("offset 0x%lx len 0x%lx\n", region->offset, region->len);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002347}
2348
2349
2350static long pmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2351{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 /* called from user space as file op, so file guaranteed to be not
2353 * NULL
2354 */
2355 struct pmem_data *data = file->private_data;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002356 int id = get_id(file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002357#if PMEM_DEBUG_MSGS
2358 char currtask_name[
2359 FIELD_SIZEOF(struct task_struct, comm) + 1];
2360#endif
2361
2362 DLOG("pid %u(%s) file %p(%ld) cmd %#x, dev %s(id: %d)\n",
2363 current->pid, get_task_comm(currtask_name, current),
2364 file, file_count(file), cmd, get_name(file), id);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002365
2366 switch (cmd) {
2367 case PMEM_GET_PHYS:
2368 {
2369 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002371 DLOG("get_phys\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 down_read(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002373 if (!has_allocation(file)) {
2374 region.offset = 0;
2375 region.len = 0;
2376 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002377 region.offset = pmem[id].start_addr(id, data);
2378 region.len = pmem[id].len(id, data);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002379 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002380 up_read(&data->sem);
2381
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002382 if (copy_to_user((void __user *)arg, &region,
2383 sizeof(struct pmem_region)))
2384 return -EFAULT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385
2386 DLOG("pmem: successful request for "
2387 "physical address of pmem region id %d, "
2388 "offset 0x%lx, len 0x%lx\n",
2389 id, region.offset, region.len);
2390
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002391 break;
2392 }
2393 case PMEM_MAP:
2394 {
2395 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002396 DLOG("map\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002397 if (copy_from_user(&region, (void __user *)arg,
2398 sizeof(struct pmem_region)))
2399 return -EFAULT;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002400 return pmem_remap(&region, file, PMEM_MAP);
2401 }
2402 break;
2403 case PMEM_UNMAP:
2404 {
2405 struct pmem_region region;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406 DLOG("unmap\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002407 if (copy_from_user(&region, (void __user *)arg,
2408 sizeof(struct pmem_region)))
2409 return -EFAULT;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002410 return pmem_remap(&region, file, PMEM_UNMAP);
2411 break;
2412 }
2413 case PMEM_GET_SIZE:
2414 {
2415 struct pmem_region region;
2416 DLOG("get_size\n");
2417 pmem_get_size(&region, file);
2418 if (copy_to_user((void __user *)arg, &region,
2419 sizeof(struct pmem_region)))
2420 return -EFAULT;
2421 break;
2422 }
2423 case PMEM_GET_TOTAL_SIZE:
2424 {
2425 struct pmem_region region;
2426 DLOG("get total size\n");
2427 region.offset = 0;
2428 get_id(file);
2429 region.len = pmem[id].size;
2430 if (copy_to_user((void __user *)arg, &region,
2431 sizeof(struct pmem_region)))
2432 return -EFAULT;
2433 break;
2434 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002435 case PMEM_GET_FREE_SPACE:
2436 {
2437 struct pmem_freespace fs;
2438 DLOG("get freespace on %s(id: %d)\n",
2439 get_name(file), id);
2440
2441 mutex_lock(&pmem[id].arena_mutex);
2442 pmem[id].free_space(id, &fs);
2443 mutex_unlock(&pmem[id].arena_mutex);
2444
2445 DLOG("%s(id: %d) total free %lu, largest %lu\n",
2446 get_name(file), id, fs.total, fs.largest);
2447
2448 if (copy_to_user((void __user *)arg, &fs,
2449 sizeof(struct pmem_freespace)))
2450 return -EFAULT;
2451 break;
2452 }
2453
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002454 case PMEM_ALLOCATE:
2455 {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002456 int ret = 0;
2457 DLOG("allocate, id %d\n", id);
2458 down_write(&data->sem);
2459 if (has_allocation(file)) {
2460 pr_err("pmem: Existing allocation found on "
2461 "this file descrpitor\n");
2462 up_write(&data->sem);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002463 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 }
2465
2466 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07002467 data->index = pmem_allocate_from_id(id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 arg,
2469 SZ_4K);
2470 mutex_unlock(&pmem[id].arena_mutex);
2471 ret = data->index == -1 ? -ENOMEM :
2472 data->index;
2473 up_write(&data->sem);
2474 return ret;
2475 }
2476 case PMEM_ALLOCATE_ALIGNED:
2477 {
2478 struct pmem_allocation alloc;
2479 int ret = 0;
2480
2481 if (copy_from_user(&alloc, (void __user *)arg,
2482 sizeof(struct pmem_allocation)))
2483 return -EFAULT;
2484 DLOG("allocate id align %d %u\n", id, alloc.align);
2485 down_write(&data->sem);
2486 if (has_allocation(file)) {
2487 pr_err("pmem: Existing allocation found on "
2488 "this file descrpitor\n");
2489 up_write(&data->sem);
2490 return -EINVAL;
2491 }
2492
2493 if (alloc.align & (alloc.align - 1)) {
2494 pr_err("pmem: Alignment is not a power of 2\n");
2495 return -EINVAL;
2496 }
2497
2498 if (alloc.align != SZ_4K &&
2499 (pmem[id].allocator_type !=
2500 PMEM_ALLOCATORTYPE_BITMAP)) {
2501 pr_err("pmem: Non 4k alignment requires bitmap"
2502 " allocator on %s\n", pmem[id].name);
2503 return -EINVAL;
2504 }
2505
2506 if (alloc.align > SZ_1M ||
2507 alloc.align < SZ_4K) {
2508 pr_err("pmem: Invalid Alignment (%u) "
2509 "specified\n", alloc.align);
2510 return -EINVAL;
2511 }
2512
2513 mutex_lock(&pmem[id].arena_mutex);
Laura Abbott1e36a022011-06-22 17:08:13 -07002514 data->index = pmem_allocate_from_id(id,
2515 alloc.size,
2516 alloc.align);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517 mutex_unlock(&pmem[id].arena_mutex);
2518 ret = data->index == -1 ? -ENOMEM :
2519 data->index;
2520 up_write(&data->sem);
2521 return ret;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002522 }
2523 case PMEM_CONNECT:
2524 DLOG("connect\n");
2525 return pmem_connect(arg, file);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526 case PMEM_CLEAN_INV_CACHES:
2527 case PMEM_CLEAN_CACHES:
2528 case PMEM_INV_CACHES:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002529 {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530 struct pmem_addr pmem_addr;
2531
2532 if (copy_from_user(&pmem_addr, (void __user *)arg,
2533 sizeof(struct pmem_addr)))
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002534 return -EFAULT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535
2536 return pmem_cache_maint(file, cmd, &pmem_addr);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002537 }
2538 default:
2539 if (pmem[id].ioctl)
2540 return pmem[id].ioctl(file, cmd, arg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541
2542 DLOG("ioctl invalid (%#x)\n", cmd);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002543 return -EINVAL;
2544 }
2545 return 0;
2546}
2547
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002548static void ioremap_pmem(int id)
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002549{
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002550 unsigned long addr;
2551 const struct mem_type *type;
Laura Abbott1e36a022011-06-22 17:08:13 -07002552
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002553 DLOG("PMEMDEBUG: ioremaping for %s\n", pmem[id].name);
2554 if (pmem[id].map_on_demand) {
2555 addr = (unsigned long)pmem[id].area->addr;
2556 if (pmem[id].cached)
2557 type = get_mem_type(MT_DEVICE_CACHED);
2558 else
2559 type = get_mem_type(MT_DEVICE);
2560 DLOG("PMEMDEBUG: Remap phys %lx to virt %lx on %s\n",
2561 pmem[id].base, addr, pmem[id].name);
2562 if (ioremap_page_range(addr, addr + pmem[id].size,
2563 pmem[id].base, __pgprot(type->prot_pte))) {
2564 pr_err("pmem: Failed to map pages\n");
2565 BUG();
2566 }
2567 pmem[id].vbase = pmem[id].area->addr;
2568 /* Flush the cache after installing page table entries to avoid
2569 * aliasing when these pages are remapped to user space.
2570 */
2571 flush_cache_vmap(addr, addr + pmem[id].size);
2572 } else {
2573 if (pmem[id].cached)
2574 pmem[id].vbase = ioremap_cached(pmem[id].base,
2575 pmem[id].size);
2576 #ifdef ioremap_ext_buffered
2577 else if (pmem[id].buffered)
2578 pmem[id].vbase = ioremap_ext_buffered(pmem[id].base,
2579 pmem[id].size);
2580 #endif
2581 else
2582 pmem[id].vbase = ioremap(pmem[id].base, pmem[id].size);
2583 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584}
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002585
2586int pmem_setup(struct android_pmem_platform_data *pdata,
2587 long (*ioctl)(struct file *, unsigned int, unsigned long),
2588 int (*release)(struct inode *, struct file *))
2589{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 int i, index = 0, id;
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002591 struct vm_struct *pmem_vma = NULL;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002592
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002593 if (id_count >= PMEM_MAX_DEVICES) {
2594 pr_alert("pmem: %s: unable to register driver(%s) - no more "
2595 "devices available!\n", __func__, pdata->name);
2596 goto err_no_mem;
2597 }
2598
2599 if (!pdata->size) {
2600 pr_alert("pmem: %s: unable to register pmem driver(%s) - zero "
2601 "size passed in!\n", __func__, pdata->name);
2602 goto err_no_mem;
2603 }
2604
2605 id = id_count++;
2606
2607 pmem[id].id = id;
2608
2609 if (pmem[id].allocate) {
2610 pr_alert("pmem: %s: unable to register pmem driver - "
2611 "duplicate registration of %s!\n",
2612 __func__, pdata->name);
2613 goto err_no_mem;
2614 }
2615
2616 pmem[id].allocator_type = pdata->allocator_type;
2617
2618 /* 'quantum' is a "hidden" variable that defaults to 0 in the board
2619 * files */
2620 pmem[id].quantum = pdata->quantum ?: PMEM_MIN_ALLOC;
2621 if (pmem[id].quantum < PMEM_MIN_ALLOC ||
2622 !is_power_of_2(pmem[id].quantum)) {
2623 pr_alert("pmem: %s: unable to register pmem driver %s - "
2624 "invalid quantum value (%#x)!\n",
2625 __func__, pdata->name, pmem[id].quantum);
2626 goto err_reset_pmem_info;
2627 }
2628
2629 if (pdata->size % pmem[id].quantum) {
2630 /* bad alignment for size! */
2631 pr_alert("pmem: %s: Unable to register driver %s - "
2632 "memory region size (%#lx) is not a multiple of "
2633 "quantum size(%#x)!\n", __func__, pdata->name,
2634 pdata->size, pmem[id].quantum);
2635 goto err_reset_pmem_info;
2636 }
2637
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002638 pmem[id].cached = pdata->cached;
2639 pmem[id].buffered = pdata->buffered;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002640 pmem[id].size = pdata->size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002641 pmem[id].memory_type = pdata->memory_type;
2642 strlcpy(pmem[id].name, pdata->name, PMEM_NAME_SIZE);
2643
2644 pmem[id].num_entries = pmem[id].size / pmem[id].quantum;
2645
2646 memset(&pmem[id].kobj, 0, sizeof(pmem[0].kobj));
2647 pmem[id].kobj.kset = pmem_kset;
2648
2649 switch (pmem[id].allocator_type) {
2650 case PMEM_ALLOCATORTYPE_ALLORNOTHING:
2651 pmem[id].allocate = pmem_allocator_all_or_nothing;
2652 pmem[id].free = pmem_free_all_or_nothing;
2653 pmem[id].free_space = pmem_free_space_all_or_nothing;
2654 pmem[id].len = pmem_len_all_or_nothing;
2655 pmem[id].start_addr = pmem_start_addr_all_or_nothing;
2656 pmem[id].num_entries = 1;
2657 pmem[id].quantum = pmem[id].size;
2658 pmem[id].allocator.all_or_nothing.allocated = 0;
2659
2660 if (kobject_init_and_add(&pmem[id].kobj,
2661 &pmem_allornothing_ktype, NULL,
2662 "%s", pdata->name))
2663 goto out_put_kobj;
2664
2665 break;
2666
2667 case PMEM_ALLOCATORTYPE_BUDDYBESTFIT:
2668 pmem[id].allocator.buddy_bestfit.buddy_bitmap = kmalloc(
2669 pmem[id].num_entries * sizeof(struct pmem_bits),
2670 GFP_KERNEL);
2671 if (!pmem[id].allocator.buddy_bestfit.buddy_bitmap)
2672 goto err_reset_pmem_info;
2673
2674 memset(pmem[id].allocator.buddy_bestfit.buddy_bitmap, 0,
2675 sizeof(struct pmem_bits) * pmem[id].num_entries);
2676
2677 for (i = sizeof(pmem[id].num_entries) * 8 - 1; i >= 0; i--)
2678 if ((pmem[id].num_entries) & 1<<i) {
2679 PMEM_BUDDY_ORDER(id, index) = i;
2680 index = PMEM_BUDDY_NEXT_INDEX(id, index);
2681 }
2682 pmem[id].allocate = pmem_allocator_buddy_bestfit;
2683 pmem[id].free = pmem_free_buddy_bestfit;
2684 pmem[id].free_space = pmem_free_space_buddy_bestfit;
2685 pmem[id].len = pmem_len_buddy_bestfit;
2686 pmem[id].start_addr = pmem_start_addr_buddy_bestfit;
2687 if (kobject_init_and_add(&pmem[id].kobj,
2688 &pmem_buddy_bestfit_ktype, NULL,
2689 "%s", pdata->name))
2690 goto out_put_kobj;
2691
2692 break;
2693
2694 case PMEM_ALLOCATORTYPE_BITMAP: /* 0, default if not explicit */
2695 pmem[id].allocator.bitmap.bitm_alloc = kmalloc(
2696 PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS *
2697 sizeof(*pmem[id].allocator.bitmap.bitm_alloc),
2698 GFP_KERNEL);
2699 if (!pmem[id].allocator.bitmap.bitm_alloc) {
2700 pr_alert("pmem: %s: Unable to register pmem "
2701 "driver %s - can't allocate "
2702 "bitm_alloc!\n",
2703 __func__, pdata->name);
2704 goto err_reset_pmem_info;
2705 }
2706
2707 if (kobject_init_and_add(&pmem[id].kobj,
2708 &pmem_bitmap_ktype, NULL,
2709 "%s", pdata->name))
2710 goto out_put_kobj;
2711
2712 for (i = 0; i < PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS; i++) {
2713 pmem[id].allocator.bitmap.bitm_alloc[i].bit = -1;
2714 pmem[id].allocator.bitmap.bitm_alloc[i].quanta = 0;
2715 }
2716
2717 pmem[id].allocator.bitmap.bitmap_allocs =
2718 PMEM_INITIAL_NUM_BITMAP_ALLOCATIONS;
2719
2720 pmem[id].allocator.bitmap.bitmap =
2721 kcalloc((pmem[id].num_entries + 31) / 32,
2722 sizeof(unsigned int), GFP_KERNEL);
2723 if (!pmem[id].allocator.bitmap.bitmap) {
2724 pr_alert("pmem: %s: Unable to register pmem "
2725 "driver - can't allocate bitmap!\n",
2726 __func__);
2727 goto err_cant_register_device;
2728 }
2729 pmem[id].allocator.bitmap.bitmap_free = pmem[id].num_entries;
2730
2731 pmem[id].allocate = pmem_allocator_bitmap;
2732 pmem[id].free = pmem_free_bitmap;
2733 pmem[id].free_space = pmem_free_space_bitmap;
2734 pmem[id].len = pmem_len_bitmap;
2735 pmem[id].start_addr = pmem_start_addr_bitmap;
2736
2737 DLOG("bitmap allocator id %d (%s), num_entries %u, raw size "
2738 "%lu, quanta size %u\n",
2739 id, pdata->name, pmem[id].allocator.bitmap.bitmap_free,
2740 pmem[id].size, pmem[id].quantum);
2741 break;
2742
2743 case PMEM_ALLOCATORTYPE_SYSTEM:
2744
2745 INIT_LIST_HEAD(&pmem[id].allocator.system_mem.alist);
2746
2747 pmem[id].allocator.system_mem.used = 0;
2748 pmem[id].vbase = NULL;
2749
2750 if (kobject_init_and_add(&pmem[id].kobj,
2751 &pmem_system_ktype, NULL,
2752 "%s", pdata->name))
2753 goto out_put_kobj;
2754
2755 pmem[id].allocate = pmem_allocator_system;
2756 pmem[id].free = pmem_free_system;
2757 pmem[id].free_space = pmem_free_space_system;
2758 pmem[id].len = pmem_len_system;
2759 pmem[id].start_addr = pmem_start_addr_system;
2760 pmem[id].num_entries = 0;
2761 pmem[id].quantum = PAGE_SIZE;
2762
2763 DLOG("system allocator id %d (%s), raw size %lu\n",
2764 id, pdata->name, pmem[id].size);
2765 break;
2766
2767 default:
2768 pr_alert("Invalid allocator type (%d) for pmem driver\n",
2769 pdata->allocator_type);
2770 goto err_reset_pmem_info;
2771 }
2772
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002773 pmem[id].ioctl = ioctl;
2774 pmem[id].release = release;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002775 mutex_init(&pmem[id].arena_mutex);
2776 mutex_init(&pmem[id].data_list_mutex);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002777 INIT_LIST_HEAD(&pmem[id].data_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002778
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002779 pmem[id].dev.name = pdata->name;
2780 pmem[id].dev.minor = id;
2781 pmem[id].dev.fops = &pmem_fops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782 pr_info("pmem: Initializing %s (user-space) as %s\n",
2783 pdata->name, pdata->cached ? "cached" : "non-cached");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002784
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002785 if (misc_register(&pmem[id].dev)) {
2786 pr_alert("Unable to register pmem driver!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002787 goto err_cant_register_device;
2788 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002789
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002790 pmem[id].base = allocate_contiguous_memory_nomap(pmem[id].size,
2791 pmem[id].memory_type, PAGE_SIZE);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002793 pr_info("allocating %lu bytes at %p (%lx physical) for %s\n",
2794 pmem[id].size, pmem[id].vbase, pmem[id].base, pmem[id].name);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002795
Naveen Ramaraj189f1882011-08-16 17:39:22 -07002796 pmem[id].map_on_demand = pdata->map_on_demand;
2797 if (pmem[id].map_on_demand) {
2798 pmem_vma = get_vm_area(pmem[id].size, VM_IOREMAP);
2799 if (!pmem_vma) {
2800 pr_err("pmem: Failed to allocate virtual space for "
2801 "%s\n", pdata->name);
2802 goto out_put_kobj;
2803 }
2804 pr_err("pmem: Reserving virtual address range %lx - %lx for"
2805 " %s\n", (unsigned long) pmem_vma->addr,
2806 (unsigned long) pmem_vma->addr + pmem[id].size,
2807 pdata->name);
2808 pmem[id].area = pmem_vma;
2809 } else
2810 pmem[id].area = NULL;
2811
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002812 pmem[id].garbage_pfn = page_to_pfn(alloc_page(GFP_KERNEL));
Laura Abbott1e36a022011-06-22 17:08:13 -07002813 atomic_set(&pmem[id].allocation_cnt, 0);
Laura Abbott1e36a022011-06-22 17:08:13 -07002814
2815 if (pdata->setup_region)
2816 pmem[id].region_data = pdata->setup_region();
2817
2818 if (pdata->request_region)
2819 pmem[id].mem_request = pdata->request_region;
2820
2821 if (pdata->release_region)
2822 pmem[id].mem_release = pdata->release_region;
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002823
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002824 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002825
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002826err_cant_register_device:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002827out_put_kobj:
2828 kobject_put(&pmem[id].kobj);
2829 if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BUDDYBESTFIT)
2830 kfree(pmem[id].allocator.buddy_bestfit.buddy_bitmap);
2831 else if (pmem[id].allocator_type == PMEM_ALLOCATORTYPE_BITMAP) {
2832 kfree(pmem[id].allocator.bitmap.bitmap);
2833 kfree(pmem[id].allocator.bitmap.bitm_alloc);
2834 }
2835err_reset_pmem_info:
2836 pmem[id].allocate = 0;
2837 pmem[id].dev.minor = -1;
2838err_no_mem:
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002839 return -1;
2840}
2841
2842static int pmem_probe(struct platform_device *pdev)
2843{
2844 struct android_pmem_platform_data *pdata;
2845
2846 if (!pdev || !pdev->dev.platform_data) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002847 pr_alert("Unable to probe pmem!\n");
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002848 return -1;
2849 }
2850 pdata = pdev->dev.platform_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002851
2852 pm_runtime_set_active(&pdev->dev);
2853 pm_runtime_enable(&pdev->dev);
2854
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002855 return pmem_setup(pdata, NULL, NULL);
2856}
2857
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002858static int pmem_remove(struct platform_device *pdev)
2859{
2860 int id = pdev->id;
2861 __free_page(pfn_to_page(pmem[id].garbage_pfn));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862 pm_runtime_disable(&pdev->dev);
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002863 misc_deregister(&pmem[id].dev);
2864 return 0;
2865}
2866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002867static int pmem_runtime_suspend(struct device *dev)
2868{
2869 dev_dbg(dev, "pm_runtime: suspending...\n");
2870 return 0;
2871}
2872
2873static int pmem_runtime_resume(struct device *dev)
2874{
2875 dev_dbg(dev, "pm_runtime: resuming...\n");
2876 return 0;
2877}
2878
2879static const struct dev_pm_ops pmem_dev_pm_ops = {
2880 .runtime_suspend = pmem_runtime_suspend,
2881 .runtime_resume = pmem_runtime_resume,
2882};
2883
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002884static struct platform_driver pmem_driver = {
2885 .probe = pmem_probe,
2886 .remove = pmem_remove,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887 .driver = { .name = "android_pmem",
2888 .pm = &pmem_dev_pm_ops,
2889 }
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002890};
2891
2892
2893static int __init pmem_init(void)
2894{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002895 /* create /sys/kernel/<PMEM_SYSFS_DIR_NAME> directory */
2896 pmem_kset = kset_create_and_add(PMEM_SYSFS_DIR_NAME,
2897 NULL, kernel_kobj);
2898 if (!pmem_kset) {
2899 pr_err("pmem(%s):kset_create_and_add fail\n", __func__);
2900 return -ENOMEM;
2901 }
2902
Rebecca Schultza4ff0e82008-07-24 11:22:53 -07002903 return platform_driver_register(&pmem_driver);
2904}
2905
2906static void __exit pmem_exit(void)
2907{
2908 platform_driver_unregister(&pmem_driver);
2909}
2910
2911module_init(pmem_init);
2912module_exit(pmem_exit);
2913