Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame^] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | /* |
| 28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 29 | */ |
| 30 | |
| 31 | #include "ttm/ttm_module.h" |
| 32 | #include "ttm/ttm_bo_driver.h" |
| 33 | #include "ttm/ttm_placement.h" |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/sched.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/file.h> |
| 39 | #include <linux/module.h> |
| 40 | |
| 41 | static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man, |
| 42 | struct ttm_buffer_object *bo, |
| 43 | struct ttm_placement *placement, |
| 44 | struct ttm_mem_reg *mem) |
| 45 | { |
| 46 | struct ttm_bo_global *glob = man->bdev->glob; |
| 47 | struct drm_mm *mm = man->priv; |
| 48 | struct drm_mm_node *node = NULL; |
| 49 | unsigned long lpfn; |
| 50 | int ret; |
| 51 | |
| 52 | lpfn = placement->lpfn; |
| 53 | if (!lpfn) |
| 54 | lpfn = man->size; |
| 55 | do { |
| 56 | ret = drm_mm_pre_get(mm); |
| 57 | if (unlikely(ret)) |
| 58 | return ret; |
| 59 | |
| 60 | spin_lock(&glob->lru_lock); |
| 61 | node = drm_mm_search_free_in_range(mm, |
| 62 | mem->num_pages, mem->page_alignment, |
| 63 | placement->fpfn, lpfn, 1); |
| 64 | if (unlikely(node == NULL)) { |
| 65 | spin_unlock(&glob->lru_lock); |
| 66 | return 0; |
| 67 | } |
| 68 | node = drm_mm_get_block_atomic_range(node, mem->num_pages, |
| 69 | mem->page_alignment, |
| 70 | placement->fpfn, |
| 71 | lpfn); |
| 72 | spin_unlock(&glob->lru_lock); |
| 73 | } while (node == NULL); |
| 74 | |
| 75 | mem->mm_node = node; |
| 76 | mem->start = node->start; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man, |
| 81 | struct ttm_mem_reg *mem) |
| 82 | { |
| 83 | struct ttm_bo_global *glob = man->bdev->glob; |
| 84 | |
| 85 | if (mem->mm_node) { |
| 86 | spin_lock(&glob->lru_lock); |
| 87 | drm_mm_put_block(mem->mm_node); |
| 88 | spin_unlock(&glob->lru_lock); |
| 89 | mem->mm_node = NULL; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static int ttm_bo_man_init(struct ttm_mem_type_manager *man, |
| 94 | unsigned long p_size) |
| 95 | { |
| 96 | struct drm_mm *mm; |
| 97 | int ret; |
| 98 | |
| 99 | mm = kzalloc(sizeof(*mm), GFP_KERNEL); |
| 100 | if (!mm) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | ret = drm_mm_init(mm, 0, p_size); |
| 104 | if (ret) { |
| 105 | kfree(mm); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | man->priv = mm; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man) |
| 114 | { |
| 115 | struct ttm_bo_global *glob = man->bdev->glob; |
| 116 | struct drm_mm *mm = man->priv; |
| 117 | int ret = 0; |
| 118 | |
| 119 | spin_lock(&glob->lru_lock); |
| 120 | if (drm_mm_clean(mm)) { |
| 121 | drm_mm_takedown(mm); |
| 122 | kfree(mm); |
| 123 | man->priv = NULL; |
| 124 | } else |
| 125 | ret = -EBUSY; |
| 126 | spin_unlock(&glob->lru_lock); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static void ttm_bo_man_debug(struct ttm_mem_type_manager *man, |
| 131 | const char *prefix) |
| 132 | { |
| 133 | struct ttm_bo_global *glob = man->bdev->glob; |
| 134 | struct drm_mm *mm = man->priv; |
| 135 | |
| 136 | spin_lock(&glob->lru_lock); |
| 137 | drm_mm_debug_table(mm, prefix); |
| 138 | spin_unlock(&glob->lru_lock); |
| 139 | } |
| 140 | |
| 141 | const struct ttm_mem_type_manager_func ttm_bo_manager_func = { |
| 142 | ttm_bo_man_init, |
| 143 | ttm_bo_man_takedown, |
| 144 | ttm_bo_man_get_node, |
| 145 | ttm_bo_man_put_node, |
| 146 | ttm_bo_man_debug |
| 147 | }; |
| 148 | EXPORT_SYMBOL(ttm_bo_manager_func); |