1 /**************************************************************************
3 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "vmwgfx_drv.h"
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <linux/idr.h>
36 #include <linux/spinlock.h>
37 #include <linux/kernel.h>
39 struct vmwgfx_gmrid_man {
43 uint32_t max_gmr_pages;
44 uint32_t used_gmr_pages;
47 static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
48 struct ttm_buffer_object *bo,
49 struct ttm_placement *placement,
51 struct ttm_mem_reg *mem)
53 struct vmwgfx_gmrid_man *gman =
54 (struct vmwgfx_gmrid_man *)man->priv;
60 spin_lock(&gman->lock);
62 if (gman->max_gmr_pages > 0) {
63 gman->used_gmr_pages += bo->num_pages;
64 if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
69 spin_unlock(&gman->lock);
70 if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0)) {
74 spin_lock(&gman->lock);
76 ret = ida_get_new(&gman->gmr_ida, &id);
77 if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) {
78 ida_remove(&gman->gmr_ida, id);
82 } while (ret == -EAGAIN);
84 if (likely(ret == 0)) {
87 mem->num_pages = bo->num_pages;
91 spin_unlock(&gman->lock);
95 spin_lock(&gman->lock);
97 gman->used_gmr_pages -= bo->num_pages;
98 spin_unlock(&gman->lock);
102 static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
103 struct ttm_mem_reg *mem)
105 struct vmwgfx_gmrid_man *gman =
106 (struct vmwgfx_gmrid_man *)man->priv;
109 spin_lock(&gman->lock);
110 ida_remove(&gman->gmr_ida, mem->start);
111 gman->used_gmr_pages -= mem->num_pages;
112 spin_unlock(&gman->lock);
117 static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
118 unsigned long p_size)
120 struct vmw_private *dev_priv =
121 container_of(man->bdev, struct vmw_private, bdev);
122 struct vmwgfx_gmrid_man *gman =
123 kzalloc(sizeof(*gman), GFP_KERNEL);
125 if (unlikely(gman == NULL))
128 spin_lock_init(&gman->lock);
129 gman->used_gmr_pages = 0;
130 ida_init(&gman->gmr_ida);
134 gman->max_gmr_ids = dev_priv->max_gmr_ids;
135 gman->max_gmr_pages = dev_priv->max_gmr_pages;
138 gman->max_gmr_ids = VMWGFX_NUM_MOB;
139 gman->max_gmr_pages = dev_priv->max_mob_pages;
144 man->priv = (void *) gman;
148 static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
150 struct vmwgfx_gmrid_man *gman =
151 (struct vmwgfx_gmrid_man *)man->priv;
154 ida_destroy(&gman->gmr_ida);
160 static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
163 printk(KERN_INFO "%s: No debug info available for the GMR "
164 "id manager.\n", prefix);
167 const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
169 vmw_gmrid_man_takedown,
170 vmw_gmrid_man_get_node,
171 vmw_gmrid_man_put_node,