diff options
Diffstat (limited to 'drivers/xen/xencomm.c')
| -rw-r--r-- | drivers/xen/xencomm.c | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/drivers/xen/xencomm.c b/drivers/xen/xencomm.c new file mode 100644 index 000000000000..797cb4e31f07 --- /dev/null +++ b/drivers/xen/xencomm.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | /* | ||
| 2 | * This program is free software; you can redistribute it and/or modify | ||
| 3 | * it under the terms of the GNU General Public License as published by | ||
| 4 | * the Free Software Foundation; either version 2 of the License, or | ||
| 5 | * (at your option) any later version. | ||
| 6 | * | ||
| 7 | * This program is distributed in the hope that it will be useful, | ||
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 10 | * GNU General Public License for more details. | ||
| 11 | * | ||
| 12 | * You should have received a copy of the GNU General Public License | ||
| 13 | * along with this program; if not, write to the Free Software | ||
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 15 | * | ||
| 16 | * Copyright (C) IBM Corp. 2006 | ||
| 17 | * | ||
| 18 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <linux/gfp.h> | ||
| 22 | #include <linux/mm.h> | ||
| 23 | #include <asm/page.h> | ||
| 24 | #include <xen/xencomm.h> | ||
| 25 | #include <xen/interface/xen.h> | ||
| 26 | #ifdef __ia64__ | ||
| 27 | #include <asm/xen/xencomm.h> /* for is_kern_addr() */ | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifdef HAVE_XEN_PLATFORM_COMPAT_H | ||
| 31 | #include <xen/platform-compat.h> | ||
| 32 | #endif | ||
| 33 | |||
| 34 | static int xencomm_init(struct xencomm_desc *desc, | ||
| 35 | void *buffer, unsigned long bytes) | ||
| 36 | { | ||
| 37 | unsigned long recorded = 0; | ||
| 38 | int i = 0; | ||
| 39 | |||
| 40 | while ((recorded < bytes) && (i < desc->nr_addrs)) { | ||
| 41 | unsigned long vaddr = (unsigned long)buffer + recorded; | ||
| 42 | unsigned long paddr; | ||
| 43 | int offset; | ||
| 44 | int chunksz; | ||
| 45 | |||
| 46 | offset = vaddr % PAGE_SIZE; /* handle partial pages */ | ||
| 47 | chunksz = min(PAGE_SIZE - offset, bytes - recorded); | ||
| 48 | |||
| 49 | paddr = xencomm_vtop(vaddr); | ||
| 50 | if (paddr == ~0UL) { | ||
| 51 | printk(KERN_DEBUG "%s: couldn't translate vaddr %lx\n", | ||
| 52 | __func__, vaddr); | ||
| 53 | return -EINVAL; | ||
| 54 | } | ||
| 55 | |||
| 56 | desc->address[i++] = paddr; | ||
| 57 | recorded += chunksz; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (recorded < bytes) { | ||
| 61 | printk(KERN_DEBUG | ||
| 62 | "%s: could only translate %ld of %ld bytes\n", | ||
| 63 | __func__, recorded, bytes); | ||
| 64 | return -ENOSPC; | ||
| 65 | } | ||
| 66 | |||
| 67 | /* mark remaining addresses invalid (just for safety) */ | ||
| 68 | while (i < desc->nr_addrs) | ||
| 69 | desc->address[i++] = XENCOMM_INVALID; | ||
| 70 | |||
| 71 | desc->magic = XENCOMM_MAGIC; | ||
| 72 | |||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | static struct xencomm_desc *xencomm_alloc(gfp_t gfp_mask, | ||
| 77 | void *buffer, unsigned long bytes) | ||
| 78 | { | ||
| 79 | struct xencomm_desc *desc; | ||
| 80 | unsigned long buffer_ulong = (unsigned long)buffer; | ||
| 81 | unsigned long start = buffer_ulong & PAGE_MASK; | ||
| 82 | unsigned long end = (buffer_ulong + bytes) | ~PAGE_MASK; | ||
| 83 | unsigned long nr_addrs = (end - start + 1) >> PAGE_SHIFT; | ||
| 84 | unsigned long size = sizeof(*desc) + | ||
| 85 | sizeof(desc->address[0]) * nr_addrs; | ||
| 86 | |||
| 87 | /* | ||
| 88 | * slab allocator returns at least sizeof(void*) aligned pointer. | ||
| 89 | * When sizeof(*desc) > sizeof(void*), struct xencomm_desc might | ||
| 90 | * cross page boundary. | ||
| 91 | */ | ||
| 92 | if (sizeof(*desc) > sizeof(void *)) { | ||
| 93 | unsigned long order = get_order(size); | ||
| 94 | desc = (struct xencomm_desc *)__get_free_pages(gfp_mask, | ||
| 95 | order); | ||
| 96 | if (desc == NULL) | ||
| 97 | return NULL; | ||
| 98 | |||
| 99 | desc->nr_addrs = | ||
| 100 | ((PAGE_SIZE << order) - sizeof(struct xencomm_desc)) / | ||
| 101 | sizeof(*desc->address); | ||
| 102 | } else { | ||
| 103 | desc = kmalloc(size, gfp_mask); | ||
| 104 | if (desc == NULL) | ||
| 105 | return NULL; | ||
| 106 | |||
| 107 | desc->nr_addrs = nr_addrs; | ||
| 108 | } | ||
| 109 | return desc; | ||
| 110 | } | ||
| 111 | |||
| 112 | void xencomm_free(struct xencomm_handle *desc) | ||
| 113 | { | ||
| 114 | if (desc && !((ulong)desc & XENCOMM_INLINE_FLAG)) { | ||
| 115 | struct xencomm_desc *desc__ = (struct xencomm_desc *)desc; | ||
| 116 | if (sizeof(*desc__) > sizeof(void *)) { | ||
| 117 | unsigned long size = sizeof(*desc__) + | ||
| 118 | sizeof(desc__->address[0]) * desc__->nr_addrs; | ||
| 119 | unsigned long order = get_order(size); | ||
| 120 | free_pages((unsigned long)__va(desc), order); | ||
| 121 | } else | ||
| 122 | kfree(__va(desc)); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | static int xencomm_create(void *buffer, unsigned long bytes, | ||
| 127 | struct xencomm_desc **ret, gfp_t gfp_mask) | ||
| 128 | { | ||
| 129 | struct xencomm_desc *desc; | ||
| 130 | int rc; | ||
| 131 | |||
| 132 | pr_debug("%s: %p[%ld]\n", __func__, buffer, bytes); | ||
| 133 | |||
| 134 | if (bytes == 0) { | ||
| 135 | /* don't create a descriptor; Xen recognizes NULL. */ | ||
| 136 | BUG_ON(buffer != NULL); | ||
| 137 | *ret = NULL; | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | BUG_ON(buffer == NULL); /* 'bytes' is non-zero */ | ||
| 142 | |||
| 143 | desc = xencomm_alloc(gfp_mask, buffer, bytes); | ||
| 144 | if (!desc) { | ||
| 145 | printk(KERN_DEBUG "%s failure\n", "xencomm_alloc"); | ||
| 146 | return -ENOMEM; | ||
| 147 | } | ||
| 148 | |||
| 149 | rc = xencomm_init(desc, buffer, bytes); | ||
| 150 | if (rc) { | ||
| 151 | printk(KERN_DEBUG "%s failure: %d\n", "xencomm_init", rc); | ||
| 152 | xencomm_free((struct xencomm_handle *)__pa(desc)); | ||
| 153 | return rc; | ||
| 154 | } | ||
| 155 | |||
| 156 | *ret = desc; | ||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | /* check if memory address is within VMALLOC region */ | ||
| 161 | static int is_phys_contiguous(unsigned long addr) | ||
| 162 | { | ||
| 163 | if (!is_kernel_addr(addr)) | ||
| 164 | return 0; | ||
| 165 | |||
| 166 | return (addr < VMALLOC_START) || (addr >= VMALLOC_END); | ||
| 167 | } | ||
| 168 | |||
| 169 | static struct xencomm_handle *xencomm_create_inline(void *ptr) | ||
| 170 | { | ||
| 171 | unsigned long paddr; | ||
| 172 | |||
| 173 | BUG_ON(!is_phys_contiguous((unsigned long)ptr)); | ||
| 174 | |||
| 175 | paddr = (unsigned long)xencomm_pa(ptr); | ||
| 176 | BUG_ON(paddr & XENCOMM_INLINE_FLAG); | ||
| 177 | return (struct xencomm_handle *)(paddr | XENCOMM_INLINE_FLAG); | ||
| 178 | } | ||
| 179 | |||
| 180 | /* "mini" routine, for stack-based communications: */ | ||
| 181 | static int xencomm_create_mini(void *buffer, | ||
| 182 | unsigned long bytes, struct xencomm_mini *xc_desc, | ||
| 183 | struct xencomm_desc **ret) | ||
| 184 | { | ||
| 185 | int rc = 0; | ||
| 186 | struct xencomm_desc *desc; | ||
| 187 | BUG_ON(((unsigned long)xc_desc) % sizeof(*xc_desc) != 0); | ||
| 188 | |||
| 189 | desc = (void *)xc_desc; | ||
| 190 | |||
| 191 | desc->nr_addrs = XENCOMM_MINI_ADDRS; | ||
| 192 | |||
| 193 | rc = xencomm_init(desc, buffer, bytes); | ||
| 194 | if (!rc) | ||
| 195 | *ret = desc; | ||
| 196 | |||
| 197 | return rc; | ||
| 198 | } | ||
| 199 | |||
| 200 | struct xencomm_handle *xencomm_map(void *ptr, unsigned long bytes) | ||
| 201 | { | ||
| 202 | int rc; | ||
| 203 | struct xencomm_desc *desc; | ||
| 204 | |||
| 205 | if (is_phys_contiguous((unsigned long)ptr)) | ||
| 206 | return xencomm_create_inline(ptr); | ||
| 207 | |||
| 208 | rc = xencomm_create(ptr, bytes, &desc, GFP_KERNEL); | ||
| 209 | |||
| 210 | if (rc || desc == NULL) | ||
| 211 | return NULL; | ||
| 212 | |||
| 213 | return xencomm_pa(desc); | ||
| 214 | } | ||
| 215 | |||
| 216 | struct xencomm_handle *__xencomm_map_no_alloc(void *ptr, unsigned long bytes, | ||
| 217 | struct xencomm_mini *xc_desc) | ||
| 218 | { | ||
| 219 | int rc; | ||
| 220 | struct xencomm_desc *desc = NULL; | ||
| 221 | |||
| 222 | if (is_phys_contiguous((unsigned long)ptr)) | ||
| 223 | return xencomm_create_inline(ptr); | ||
| 224 | |||
| 225 | rc = xencomm_create_mini(ptr, bytes, xc_desc, | ||
| 226 | &desc); | ||
| 227 | |||
| 228 | if (rc) | ||
| 229 | return NULL; | ||
| 230 | |||
| 231 | return xencomm_pa(desc); | ||
| 232 | } | ||
