Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 3 | * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 31 | #include <drm/ttm/ttm_module.h> |
| 32 | #include <drm/ttm/ttm_bo_driver.h> |
| 33 | #include <drm/ttm/ttm_placement.h> |
| 34 | #include <drm/drm_mm.h> |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 35 | #include <linux/slab.h> |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 36 | #include <linux/spinlock.h> |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 39 | /** |
| 40 | * Currently we use a spinlock for the lock, but a mutex *may* be |
| 41 | * more appropriate to reduce scheduling latency if the range manager |
| 42 | * ends up with very fragmented allocation patterns. |
| 43 | */ |
| 44 | |
| 45 | struct ttm_range_manager { |
| 46 | struct drm_mm mm; |
| 47 | spinlock_t lock; |
| 48 | }; |
| 49 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 50 | static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man, |
| 51 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame^] | 52 | const struct ttm_place *place, |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 53 | struct ttm_mem_reg *mem) |
| 54 | { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 55 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
| 56 | struct drm_mm *mm = &rman->mm; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 57 | struct drm_mm_node *node = NULL; |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 58 | enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 59 | unsigned long lpfn; |
| 60 | int ret; |
| 61 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame^] | 62 | lpfn = place->lpfn; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 63 | if (!lpfn) |
| 64 | lpfn = man->size; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 65 | |
David Herrmann | 78af329 | 2013-07-27 13:37:59 +0200 | [diff] [blame] | 66 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 67 | if (!node) |
| 68 | return -ENOMEM; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 69 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame^] | 70 | if (place->flags & TTM_PL_FLAG_TOPDOWN) |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 71 | aflags = DRM_MM_CREATE_TOP; |
| 72 | |
David Herrmann | 78af329 | 2013-07-27 13:37:59 +0200 | [diff] [blame] | 73 | spin_lock(&rman->lock); |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 74 | ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages, |
| 75 | mem->page_alignment, 0, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame^] | 76 | place->fpfn, lpfn, |
Lauri Kasanen | 62347f9 | 2014-04-02 20:03:57 +0300 | [diff] [blame] | 77 | DRM_MM_SEARCH_BEST, |
| 78 | aflags); |
David Herrmann | 78af329 | 2013-07-27 13:37:59 +0200 | [diff] [blame] | 79 | spin_unlock(&rman->lock); |
| 80 | |
| 81 | if (unlikely(ret)) { |
| 82 | kfree(node); |
| 83 | } else { |
| 84 | mem->mm_node = node; |
| 85 | mem->start = node->start; |
| 86 | } |
| 87 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man, |
| 92 | struct ttm_mem_reg *mem) |
| 93 | { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 94 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 95 | |
| 96 | if (mem->mm_node) { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 97 | spin_lock(&rman->lock); |
David Herrmann | 78af329 | 2013-07-27 13:37:59 +0200 | [diff] [blame] | 98 | drm_mm_remove_node(mem->mm_node); |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 99 | spin_unlock(&rman->lock); |
David Herrmann | 78af329 | 2013-07-27 13:37:59 +0200 | [diff] [blame] | 100 | |
| 101 | kfree(mem->mm_node); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 102 | mem->mm_node = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static int ttm_bo_man_init(struct ttm_mem_type_manager *man, |
| 107 | unsigned long p_size) |
| 108 | { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 109 | struct ttm_range_manager *rman; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 110 | |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 111 | rman = kzalloc(sizeof(*rman), GFP_KERNEL); |
| 112 | if (!rman) |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 113 | return -ENOMEM; |
| 114 | |
David Herrmann | 77ef8bb | 2013-07-01 20:32:58 +0200 | [diff] [blame] | 115 | drm_mm_init(&rman->mm, 0, p_size); |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 116 | spin_lock_init(&rman->lock); |
| 117 | man->priv = rman; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man) |
| 122 | { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 123 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
| 124 | struct drm_mm *mm = &rman->mm; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 125 | |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 126 | spin_lock(&rman->lock); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 127 | if (drm_mm_clean(mm)) { |
| 128 | drm_mm_takedown(mm); |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 129 | spin_unlock(&rman->lock); |
| 130 | kfree(rman); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 131 | man->priv = NULL; |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | spin_unlock(&rman->lock); |
| 135 | return -EBUSY; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void ttm_bo_man_debug(struct ttm_mem_type_manager *man, |
| 139 | const char *prefix) |
| 140 | { |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 141 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 142 | |
Thomas Hellstrom | d7a67cb | 2010-10-29 10:46:45 +0200 | [diff] [blame] | 143 | spin_lock(&rman->lock); |
| 144 | drm_mm_debug_table(&rman->mm, prefix); |
| 145 | spin_unlock(&rman->lock); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | const struct ttm_mem_type_manager_func ttm_bo_manager_func = { |
| 149 | ttm_bo_man_init, |
| 150 | ttm_bo_man_takedown, |
| 151 | ttm_bo_man_get_node, |
| 152 | ttm_bo_man_put_node, |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 153 | ttm_bo_man_debug |
| 154 | }; |
| 155 | EXPORT_SYMBOL(ttm_bo_manager_func); |