Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2007-2010 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 "vmwgfx_drv.h" |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 32 | #include <drm/ttm/ttm_module.h> |
| 33 | #include <drm/ttm/ttm_bo_driver.h> |
| 34 | #include <drm/ttm/ttm_placement.h> |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 35 | #include <linux/idr.h> |
| 36 | #include <linux/spinlock.h> |
| 37 | #include <linux/kernel.h> |
| 38 | |
| 39 | struct vmwgfx_gmrid_man { |
| 40 | spinlock_t lock; |
| 41 | struct ida gmr_ida; |
| 42 | uint32_t max_gmr_ids; |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 43 | uint32_t max_gmr_pages; |
| 44 | uint32_t used_gmr_pages; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man, |
| 48 | struct ttm_buffer_object *bo, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 49 | const struct ttm_place *place, |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 50 | struct ttm_mem_reg *mem) |
| 51 | { |
| 52 | struct vmwgfx_gmrid_man *gman = |
| 53 | (struct vmwgfx_gmrid_man *)man->priv; |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 54 | int ret = 0; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 55 | int id; |
| 56 | |
| 57 | mem->mm_node = NULL; |
| 58 | |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 59 | spin_lock(&gman->lock); |
| 60 | |
| 61 | if (gman->max_gmr_pages > 0) { |
| 62 | gman->used_gmr_pages += bo->num_pages; |
| 63 | if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages)) |
| 64 | goto out_err_locked; |
| 65 | } |
| 66 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 67 | do { |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 68 | spin_unlock(&gman->lock); |
| 69 | if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0)) { |
| 70 | ret = -ENOMEM; |
| 71 | goto out_err; |
| 72 | } |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 73 | spin_lock(&gman->lock); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 74 | |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 75 | ret = ida_get_new(&gman->gmr_ida, &id); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 76 | if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) { |
| 77 | ida_remove(&gman->gmr_ida, id); |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 78 | ret = 0; |
| 79 | goto out_err_locked; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 80 | } |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 81 | } while (ret == -EAGAIN); |
| 82 | |
| 83 | if (likely(ret == 0)) { |
| 84 | mem->mm_node = gman; |
| 85 | mem->start = id; |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 86 | mem->num_pages = bo->num_pages; |
| 87 | } else |
| 88 | goto out_err_locked; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 89 | |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 90 | spin_unlock(&gman->lock); |
| 91 | return 0; |
| 92 | |
| 93 | out_err: |
| 94 | spin_lock(&gman->lock); |
| 95 | out_err_locked: |
| 96 | gman->used_gmr_pages -= bo->num_pages; |
| 97 | spin_unlock(&gman->lock); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man, |
| 102 | struct ttm_mem_reg *mem) |
| 103 | { |
| 104 | struct vmwgfx_gmrid_man *gman = |
| 105 | (struct vmwgfx_gmrid_man *)man->priv; |
| 106 | |
| 107 | if (mem->mm_node) { |
| 108 | spin_lock(&gman->lock); |
| 109 | ida_remove(&gman->gmr_ida, mem->start); |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 110 | gman->used_gmr_pages -= mem->num_pages; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 111 | spin_unlock(&gman->lock); |
| 112 | mem->mm_node = NULL; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man, |
| 117 | unsigned long p_size) |
| 118 | { |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 119 | struct vmw_private *dev_priv = |
| 120 | container_of(man->bdev, struct vmw_private, bdev); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 121 | struct vmwgfx_gmrid_man *gman = |
| 122 | kzalloc(sizeof(*gman), GFP_KERNEL); |
| 123 | |
| 124 | if (unlikely(gman == NULL)) |
| 125 | return -ENOMEM; |
| 126 | |
| 127 | spin_lock_init(&gman->lock); |
Thomas Hellstrom | fb17f18 | 2011-08-31 07:42:53 +0000 | [diff] [blame] | 128 | gman->used_gmr_pages = 0; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 129 | ida_init(&gman->gmr_ida); |
Thomas Hellstrom | 6da768a | 2012-11-21 11:06:22 +0100 | [diff] [blame] | 130 | |
| 131 | switch (p_size) { |
| 132 | case VMW_PL_GMR: |
| 133 | gman->max_gmr_ids = dev_priv->max_gmr_ids; |
| 134 | gman->max_gmr_pages = dev_priv->max_gmr_pages; |
| 135 | break; |
| 136 | case VMW_PL_MOB: |
| 137 | gman->max_gmr_ids = VMWGFX_NUM_MOB; |
| 138 | gman->max_gmr_pages = dev_priv->max_mob_pages; |
| 139 | break; |
| 140 | default: |
| 141 | BUG(); |
| 142 | } |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 143 | man->priv = (void *) gman; |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man) |
| 148 | { |
| 149 | struct vmwgfx_gmrid_man *gman = |
| 150 | (struct vmwgfx_gmrid_man *)man->priv; |
| 151 | |
| 152 | if (gman) { |
| 153 | ida_destroy(&gman->gmr_ida); |
| 154 | kfree(gman); |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man, |
| 160 | const char *prefix) |
| 161 | { |
| 162 | printk(KERN_INFO "%s: No debug info available for the GMR " |
| 163 | "id manager.\n", prefix); |
| 164 | } |
| 165 | |
| 166 | const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = { |
| 167 | vmw_gmrid_man_init, |
| 168 | vmw_gmrid_man_takedown, |
| 169 | vmw_gmrid_man_get_node, |
| 170 | vmw_gmrid_man_put_node, |
| 171 | vmw_gmrid_man_debug |
| 172 | }; |