Naveen Ramaraj | 8973895 | 2013-02-13 15:24:57 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 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 | |
| 13 | #include <linux/slab.h> |
| 14 | #include <mach/ocmem_priv.h> |
| 15 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 16 | static DEFINE_MUTEX(ocmem_eviction_lock); |
| 17 | static DECLARE_BITMAP(evicted, OCMEM_CLIENT_MAX); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 18 | |
| 19 | static struct ocmem_handle *generate_handle(void) |
| 20 | { |
| 21 | struct ocmem_handle *handle = NULL; |
| 22 | |
| 23 | handle = kzalloc(sizeof(struct ocmem_handle), GFP_KERNEL); |
| 24 | if (!handle) { |
| 25 | pr_err("ocmem: Unable to generate buffer handle\n"); |
| 26 | return NULL; |
| 27 | } |
| 28 | mutex_init(&handle->handle_mutex); |
| 29 | return handle; |
| 30 | } |
| 31 | |
| 32 | static int free_handle(struct ocmem_handle *handle) |
| 33 | { |
| 34 | if (!handle) |
| 35 | return -EINVAL; |
| 36 | |
| 37 | mutex_destroy(&handle->handle_mutex); |
| 38 | kfree(handle); |
| 39 | handle = NULL; |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int __ocmem_free(int id, struct ocmem_buf *buf) |
| 44 | { |
| 45 | int ret = 0; |
| 46 | struct ocmem_handle *handle = buffer_to_handle(buf); |
| 47 | |
| 48 | if (!handle) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | mutex_lock(&handle->handle_mutex); |
| 52 | ret = process_free(id, handle); |
| 53 | mutex_unlock(&handle->handle_mutex); |
| 54 | |
Naveen Ramaraj | 8973895 | 2013-02-13 15:24:57 -0800 | [diff] [blame] | 55 | if (ret) { |
| 56 | pr_err("ocmem: Free failed for client %s\n", get_name(id)); |
| 57 | return ret; |
| 58 | } |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 59 | free_handle(handle); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 63 | static int __ocmem_shrink(int id, struct ocmem_buf *buf, unsigned long len) |
| 64 | { |
| 65 | int ret = 0; |
| 66 | struct ocmem_handle *handle = buffer_to_handle(buf); |
| 67 | |
| 68 | if (!handle) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | mutex_lock(&handle->handle_mutex); |
| 72 | ret = process_shrink(id, handle, len); |
| 73 | mutex_unlock(&handle->handle_mutex); |
| 74 | |
| 75 | if (ret) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 81 | static struct ocmem_buf *__ocmem_allocate_range(int id, unsigned long min, |
| 82 | unsigned long max, unsigned long step, bool block, bool wait) |
| 83 | { |
| 84 | struct ocmem_handle *handle = NULL; |
| 85 | int ret = 0; |
| 86 | |
| 87 | handle = generate_handle(); |
| 88 | if (!handle) { |
| 89 | pr_err("ocmem: Unable to generate handle\n"); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | mutex_lock(&handle->handle_mutex); |
| 94 | ret = process_allocate(id, handle, min, max, step, block, wait); |
| 95 | mutex_unlock(&handle->handle_mutex); |
| 96 | if (ret) { |
| 97 | pr_err("ocmem allocation failed\n"); |
| 98 | free_handle(handle); |
| 99 | return NULL; |
| 100 | } else |
| 101 | return handle_to_buffer(handle); |
| 102 | } |
| 103 | |
| 104 | struct ocmem_buf *ocmem_allocate(int client_id, unsigned long size) |
| 105 | { |
| 106 | bool can_block = false; |
| 107 | bool can_wait = true; |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 108 | struct ocmem_buf *buffer; |
| 109 | struct timeval start_time; |
| 110 | struct timeval end_time; |
| 111 | unsigned int delay; |
| 112 | struct ocmem_zone *zone; |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 113 | |
| 114 | if (!check_id(client_id)) { |
| 115 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 119 | if (!zone_active(client_id)) { |
| 120 | pr_err("ocmem: Client %s (id: %d) not allowed to use OCMEM\n", |
| 121 | get_name(client_id), client_id); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 125 | if (size < OCMEM_MIN_ALLOC) { |
| 126 | pr_err("ocmem: requested size %lx must be at least %x\n", |
| 127 | size, OCMEM_MIN_ALLOC); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | if (!IS_ALIGNED(size, OCMEM_MIN_ALIGN)) { |
| 132 | pr_err("ocmem: Invalid alignment, size must be %x aligned\n", |
| 133 | OCMEM_MIN_ALIGN); |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 137 | zone = get_zone(client_id); |
| 138 | if (!zone) { |
| 139 | pr_err("ocmem: Zone not found for client %d\n", client_id); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | do_gettimeofday(&start_time); |
| 144 | |
| 145 | buffer = __ocmem_allocate_range(client_id, size, size, |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 146 | size, can_block, can_wait); |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 147 | |
| 148 | do_gettimeofday(&end_time); |
| 149 | |
| 150 | if (!buffer) |
| 151 | return NULL; |
| 152 | |
| 153 | delay = (end_time.tv_sec * USEC_PER_SEC + end_time.tv_usec) |
| 154 | - (start_time.tv_sec * USEC_PER_SEC + start_time.tv_usec); |
| 155 | |
| 156 | if (delay > zone->max_alloc_time) |
| 157 | zone->max_alloc_time = delay; |
| 158 | if (delay < zone->min_alloc_time) |
| 159 | zone->min_alloc_time = delay; |
| 160 | zone->total_alloc_time += delay; |
| 161 | inc_ocmem_stat(zone, NR_SYNC_ALLOCATIONS); |
| 162 | |
| 163 | return buffer; |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 164 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 165 | EXPORT_SYMBOL(ocmem_allocate); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 166 | |
| 167 | struct ocmem_buf *ocmem_allocate_nowait(int client_id, unsigned long size) |
| 168 | { |
| 169 | bool can_block = false; |
| 170 | bool can_wait = false; |
| 171 | |
| 172 | if (!check_id(client_id)) { |
| 173 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 177 | if (!zone_active(client_id)) { |
| 178 | pr_err("ocmem: Client %s (id: %d) not allowed to use OCMEM\n", |
| 179 | get_name(client_id), client_id); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 183 | if (size < OCMEM_MIN_ALLOC) { |
| 184 | pr_err("ocmem: requested size %lx must be at least %x\n", |
| 185 | size, OCMEM_MIN_ALLOC); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | if (!IS_ALIGNED(size, OCMEM_MIN_ALIGN)) { |
| 190 | pr_err("ocmem: Invalid alignment, size must be %x aligned\n", |
| 191 | OCMEM_MIN_ALIGN); |
| 192 | return NULL; |
| 193 | } |
| 194 | return __ocmem_allocate_range(client_id, size, size, |
| 195 | size, can_block, can_wait); |
| 196 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 197 | EXPORT_SYMBOL(ocmem_allocate_nowait); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 198 | |
| 199 | struct ocmem_buf *ocmem_allocate_range(int client_id, unsigned long min, |
| 200 | unsigned long goal, unsigned long step) |
| 201 | { |
| 202 | bool can_block = true; |
| 203 | bool can_wait = false; |
| 204 | |
| 205 | if (!check_id(client_id)) { |
| 206 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 210 | if (!zone_active(client_id)) { |
| 211 | pr_err("ocmem: Client %s (id: %d) not allowed to use OCMEM\n", |
| 212 | get_name(client_id), client_id); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 216 | /* Asynchronous API requires notifier registration */ |
| 217 | if (!check_notifier(client_id)) { |
| 218 | pr_err("ocmem: No notifier registered for client %d\n", |
| 219 | client_id); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | if (min < OCMEM_MIN_ALLOC) { |
| 224 | pr_err("ocmem: requested min size %lx must be at least %x\n", |
| 225 | min, OCMEM_MIN_ALLOC); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | if (!IS_ALIGNED(min | goal | step, OCMEM_MIN_ALIGN)) { |
| 230 | pr_err("ocmem: Invalid alignment, args must be %x aligned\n", |
| 231 | OCMEM_MIN_ALIGN); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | return __ocmem_allocate_range(client_id, min, goal, |
| 236 | step, can_block, can_wait); |
| 237 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 238 | EXPORT_SYMBOL(ocmem_allocate_range); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 239 | |
| 240 | struct ocmem_buf *ocmem_allocate_nb(int client_id, unsigned long size) |
| 241 | { |
| 242 | bool can_block = true; |
| 243 | bool can_wait = false; |
| 244 | |
| 245 | if (!check_id(client_id)) { |
| 246 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | /* Asynchronous API requires notifier registration */ |
| 251 | if (!check_notifier(client_id)) { |
| 252 | pr_err("ocmem: No notifier registered for client %d\n", |
| 253 | client_id); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 257 | if (!zone_active(client_id)) { |
| 258 | pr_err("ocmem: Client %s (id: %d) not allowed to use OCMEM\n", |
| 259 | get_name(client_id), client_id); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 263 | if (size < OCMEM_MIN_ALLOC) { |
| 264 | pr_err("ocmem: requested size %lx must be at least %x\n", |
| 265 | size, OCMEM_MIN_ALLOC); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | if (!IS_ALIGNED(size, OCMEM_MIN_ALIGN)) { |
| 270 | pr_err("ocmem: Invalid alignment, args must be %x aligned\n", |
| 271 | OCMEM_MIN_ALIGN); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | return __ocmem_allocate_range(client_id, 0, size, size, |
| 276 | can_block, can_wait); |
| 277 | |
| 278 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 279 | EXPORT_SYMBOL(ocmem_allocate_nb); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 280 | |
| 281 | int ocmem_free(int client_id, struct ocmem_buf *buffer) |
| 282 | { |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 283 | int rc; |
| 284 | struct timeval start_time; |
| 285 | struct timeval end_time; |
| 286 | unsigned int delay; |
| 287 | struct ocmem_zone *zone; |
| 288 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 289 | if (!check_id(client_id)) { |
| 290 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 294 | if (!zone_active(client_id)) { |
| 295 | pr_err("ocmem: Client %s (id: %d) not allowed to use OCMEM\n", |
| 296 | get_name(client_id), client_id); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 300 | zone = get_zone(client_id); |
| 301 | if (!zone) { |
| 302 | pr_err("ocmem: Zone not found for client %d\n", client_id); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 306 | if (!buffer) { |
| 307 | pr_err("ocmem: Invalid buffer\n"); |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | |
Neeti Desai | f69aa13 | 2013-02-28 17:25:01 -0800 | [diff] [blame] | 311 | do_gettimeofday(&start_time); |
| 312 | |
| 313 | rc = __ocmem_free(client_id, buffer); |
| 314 | |
| 315 | do_gettimeofday(&end_time); |
| 316 | |
| 317 | if (rc < 0) |
| 318 | return rc; |
| 319 | |
| 320 | delay = (end_time.tv_sec * USEC_PER_SEC + end_time.tv_usec) |
| 321 | - (start_time.tv_sec * USEC_PER_SEC + start_time.tv_usec); |
| 322 | |
| 323 | if (delay > zone->max_free_time) |
| 324 | zone->max_free_time = delay; |
| 325 | if (delay < zone->min_free_time) |
| 326 | zone->min_free_time = delay; |
| 327 | zone->total_free_time += delay; |
| 328 | inc_ocmem_stat(zone, NR_FREES); |
| 329 | |
| 330 | return rc; |
| 331 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 332 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 333 | EXPORT_SYMBOL(ocmem_free); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 334 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 335 | int ocmem_shrink(int client_id, struct ocmem_buf *buffer, unsigned long len) |
| 336 | { |
| 337 | if (!buffer) |
| 338 | return -EINVAL; |
| 339 | if (len >= buffer->len) |
| 340 | return -EINVAL; |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 341 | |
| 342 | if (!zone_active(client_id)) { |
| 343 | pr_err("ocmem: Client id: %s (id: %d) not allowed to use OCMEM\n", |
| 344 | get_name(client_id), client_id); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 348 | return __ocmem_shrink(client_id, buffer, len); |
| 349 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 350 | EXPORT_SYMBOL(ocmem_shrink); |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 351 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 352 | int pre_validate_chunk_list(struct ocmem_map_list *list) |
| 353 | { |
| 354 | int i = 0; |
| 355 | struct ocmem_chunk *chunks; |
| 356 | |
| 357 | if (!list) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | if (list->num_chunks > OCMEM_MAX_CHUNKS || list->num_chunks == 0) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | chunks = list->chunks; |
| 364 | |
| 365 | if (!chunks) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | for (i = 0; i < list->num_chunks; i++) { |
| 369 | if (!chunks[i].ddr_paddr || |
Naveen Ramaraj | 6652259 | 2012-11-19 11:33:09 -0800 | [diff] [blame] | 370 | !IS_ALIGNED(chunks[i].ddr_paddr, MIN_CHUNK_SIZE) || |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 371 | chunks[i].size < MIN_CHUNK_SIZE || |
| 372 | !IS_ALIGNED(chunks[i].size, MIN_CHUNK_SIZE)) { |
| 373 | pr_err("Invalid ocmem chunk at index %d (p: %lx, size %lx)\n", |
| 374 | i, chunks[i].ddr_paddr, chunks[i].size); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 375 | return -EINVAL; |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 376 | } |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int ocmem_map(int client_id, struct ocmem_buf *buffer, |
| 382 | struct ocmem_map_list *list) |
| 383 | { |
| 384 | int ret = 0; |
| 385 | struct ocmem_handle *handle = NULL; |
| 386 | |
| 387 | if (!check_id(client_id)) { |
| 388 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 392 | if (!zone_active(client_id)) { |
| 393 | pr_err("ocmem: Client id: %s (id: %d) not allowed to use OCMEM\n", |
| 394 | get_name(client_id), client_id); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 398 | /* Asynchronous API requires notifier registration */ |
| 399 | if (!check_notifier(client_id)) { |
| 400 | pr_err("ocmem: No notifier registered for client %d\n", |
| 401 | client_id); |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | |
| 405 | if (!buffer) { |
| 406 | pr_err("ocmem: Invalid buffer\n"); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 410 | if (pre_validate_chunk_list(list) != 0) |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 411 | return -EINVAL; |
| 412 | |
| 413 | handle = buffer_to_handle(buffer); |
| 414 | |
| 415 | if (!handle) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | mutex_lock(&handle->handle_mutex); |
| 419 | ret = process_xfer(client_id, handle, list, TO_OCMEM); |
| 420 | mutex_unlock(&handle->handle_mutex); |
| 421 | return ret; |
| 422 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 423 | EXPORT_SYMBOL(ocmem_map); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 424 | |
| 425 | int ocmem_unmap(int client_id, struct ocmem_buf *buffer, |
| 426 | struct ocmem_map_list *list) |
| 427 | { |
| 428 | |
| 429 | int ret = 0; |
| 430 | struct ocmem_handle *handle = NULL; |
| 431 | |
| 432 | if (!check_id(client_id)) { |
| 433 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
Naveen Ramaraj | 4d5e354 | 2012-08-12 21:55:49 -0700 | [diff] [blame] | 437 | if (!zone_active(client_id)) { |
| 438 | pr_err("ocmem: Client id: %s (id: %d) not allowed to use OCMEM\n", |
| 439 | get_name(client_id), client_id); |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 443 | /* Asynchronous API requires notifier registration */ |
| 444 | if (!check_notifier(client_id)) { |
| 445 | pr_err("ocmem: No notifier registered for client %d\n", |
| 446 | client_id); |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | if (!buffer) { |
| 451 | pr_err("ocmem: Invalid buffer\n"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 455 | if (pre_validate_chunk_list(list) != 0) |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | |
| 458 | handle = buffer_to_handle(buffer); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 459 | mutex_lock(&handle->handle_mutex); |
| 460 | ret = process_xfer(client_id, handle, list, TO_DDR); |
| 461 | mutex_unlock(&handle->handle_mutex); |
| 462 | return ret; |
| 463 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 464 | EXPORT_SYMBOL(ocmem_unmap); |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 465 | |
Neeti Desai | dad1d8e | 2013-01-09 19:42:06 -0800 | [diff] [blame] | 466 | int ocmem_drop(int client_id, struct ocmem_buf *buffer, |
| 467 | struct ocmem_map_list *list) |
| 468 | { |
| 469 | int ret = 0; |
| 470 | struct ocmem_handle *handle = NULL; |
| 471 | |
| 472 | if (!check_id(client_id)) { |
| 473 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 474 | return -EINVAL; |
| 475 | } |
| 476 | |
| 477 | if (!zone_active(client_id)) { |
| 478 | pr_err("ocmem: Client id: %s (id: %d) not allowed to use OCMEM\n", |
| 479 | get_name(client_id), client_id); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | if (!buffer) { |
| 484 | pr_err("ocmem: Invalid buffer\n"); |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | if (pre_validate_chunk_list(list) != 0) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | handle = buffer_to_handle(buffer); |
| 492 | mutex_lock(&handle->handle_mutex); |
| 493 | ret = process_drop(client_id, handle, list); |
| 494 | mutex_unlock(&handle->handle_mutex); |
| 495 | return ret; |
| 496 | } |
| 497 | EXPORT_SYMBOL(ocmem_drop); |
| 498 | |
| 499 | |
Naveen Ramaraj | 55ed890 | 2012-09-26 13:18:06 -0700 | [diff] [blame] | 500 | int ocmem_dump(int client_id, struct ocmem_buf *buffer, |
| 501 | unsigned long dst_phys_addr) |
| 502 | { |
| 503 | int ret = 0; |
| 504 | struct ocmem_handle *handle = NULL; |
| 505 | |
| 506 | if (!check_id(client_id)) { |
| 507 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 508 | return -EINVAL; |
| 509 | } |
| 510 | |
| 511 | if (!zone_active(client_id)) { |
| 512 | pr_err("ocmem: Client id: %s (id: %d) not allowed to use OCMEM\n", |
| 513 | get_name(client_id), client_id); |
| 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | if (!buffer) { |
| 518 | pr_err("ocmem: Invalid buffer\n"); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | |
| 522 | handle = buffer_to_handle(buffer); |
| 523 | mutex_lock(&handle->handle_mutex); |
| 524 | ret = process_dump(client_id, handle, dst_phys_addr); |
| 525 | mutex_unlock(&handle->handle_mutex); |
| 526 | return ret; |
| 527 | } |
| 528 | EXPORT_SYMBOL(ocmem_dump); |
| 529 | |
Naveen Ramaraj | 0f5e7ab | 2012-04-24 19:10:23 -0700 | [diff] [blame] | 530 | unsigned long get_max_quota(int client_id) |
| 531 | { |
| 532 | if (!check_id(client_id)) { |
| 533 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 534 | return 0x0; |
| 535 | } |
| 536 | return process_quota(client_id); |
| 537 | } |
Naveen Ramaraj | bb93faa | 2013-04-08 15:07:52 -0700 | [diff] [blame] | 538 | EXPORT_SYMBOL(get_max_quota); |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 539 | |
| 540 | /* Synchronous eviction/restore calls */ |
| 541 | /* Only a single eviction or restoration is allowed */ |
| 542 | /* Evictions/Restorations cannot be concurrent with other maps */ |
| 543 | int ocmem_evict(int client_id) |
| 544 | { |
| 545 | int ret = 0; |
| 546 | |
| 547 | if (!check_id(client_id)) { |
| 548 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 549 | return -EINVAL; |
| 550 | } |
| 551 | |
| 552 | mutex_lock(&ocmem_eviction_lock); |
| 553 | if (test_bit(client_id, evicted)) { |
| 554 | pr_err("ocmem: Previous eviction was not restored by %d\n", |
| 555 | client_id); |
| 556 | mutex_unlock(&ocmem_eviction_lock); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | ret = process_evict(client_id); |
| 561 | if (ret == 0) |
| 562 | set_bit(client_id, evicted); |
| 563 | |
| 564 | mutex_unlock(&ocmem_eviction_lock); |
| 565 | return ret; |
| 566 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 567 | EXPORT_SYMBOL(ocmem_evict); |
Naveen Ramaraj | cc4ec15 | 2012-05-14 09:55:29 -0700 | [diff] [blame] | 568 | |
| 569 | int ocmem_restore(int client_id) |
| 570 | { |
| 571 | int ret = 0; |
| 572 | |
| 573 | if (!check_id(client_id)) { |
| 574 | pr_err("ocmem: Invalid client id: %d\n", client_id); |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | |
| 578 | mutex_lock(&ocmem_eviction_lock); |
| 579 | if (!test_bit(client_id, evicted)) { |
| 580 | pr_err("ocmem: No previous eviction by %d\n", client_id); |
| 581 | mutex_unlock(&ocmem_eviction_lock); |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | ret = process_restore(client_id); |
| 585 | clear_bit(client_id, evicted); |
| 586 | mutex_unlock(&ocmem_eviction_lock); |
| 587 | return ret; |
| 588 | } |
Naveen Ramaraj | fdfb0cc | 2012-09-06 23:16:48 -0700 | [diff] [blame] | 589 | EXPORT_SYMBOL(ocmem_restore); |
Naveen Ramaraj | 99b0756 | 2012-05-28 20:57:09 -0700 | [diff] [blame] | 590 | |
| 591 | /* Wrappers until power control is transitioned to clients */ |
| 592 | enum ocmem_power_state ocmem_get_power_state(int client_id, |
| 593 | struct ocmem_buf *buffer) |
| 594 | { |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | int ocmem_set_power_state(int client_id, struct ocmem_buf *buffer, |
| 599 | enum ocmem_power_state new_state) |
| 600 | { |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | struct ocmem_vectors *ocmem_get_vectors(int client_id, |
| 605 | struct ocmem_buf *buffer) |
| 606 | { |
| 607 | return NULL; |
| 608 | } |