blob: 26f8bdde3529b8a5b222a52aa34ebc8da185232a [file] [log] [blame]
Thomas Hellstrom135cba02010-10-26 21:21:47 +02001/**************************************************************************
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 Howells760285e2012-10-02 18:01:07 +010032#include <drm/ttm/ttm_module.h>
33#include <drm/ttm/ttm_bo_driver.h>
34#include <drm/ttm/ttm_placement.h>
Thomas Hellstrom135cba02010-10-26 21:21:47 +020035#include <linux/idr.h>
36#include <linux/spinlock.h>
37#include <linux/kernel.h>
38
39struct vmwgfx_gmrid_man {
40 spinlock_t lock;
41 struct ida gmr_ida;
42 uint32_t max_gmr_ids;
Thomas Hellstromfb17f182011-08-31 07:42:53 +000043 uint32_t max_gmr_pages;
44 uint32_t used_gmr_pages;
Thomas Hellstrom135cba02010-10-26 21:21:47 +020045};
46
47static int vmw_gmrid_man_get_node(struct ttm_mem_type_manager *man,
48 struct ttm_buffer_object *bo,
49 struct ttm_placement *placement,
Christian Könige3f20272014-07-03 09:02:23 +020050 uint32_t flags,
Thomas Hellstrom135cba02010-10-26 21:21:47 +020051 struct ttm_mem_reg *mem)
52{
53 struct vmwgfx_gmrid_man *gman =
54 (struct vmwgfx_gmrid_man *)man->priv;
Thomas Hellstromfb17f182011-08-31 07:42:53 +000055 int ret = 0;
Thomas Hellstrom135cba02010-10-26 21:21:47 +020056 int id;
57
58 mem->mm_node = NULL;
59
Thomas Hellstromfb17f182011-08-31 07:42:53 +000060 spin_lock(&gman->lock);
61
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))
65 goto out_err_locked;
66 }
67
Thomas Hellstrom135cba02010-10-26 21:21:47 +020068 do {
Thomas Hellstromfb17f182011-08-31 07:42:53 +000069 spin_unlock(&gman->lock);
70 if (unlikely(ida_pre_get(&gman->gmr_ida, GFP_KERNEL) == 0)) {
71 ret = -ENOMEM;
72 goto out_err;
73 }
Thomas Hellstrom135cba02010-10-26 21:21:47 +020074 spin_lock(&gman->lock);
Thomas Hellstrom135cba02010-10-26 21:21:47 +020075
Thomas Hellstromfb17f182011-08-31 07:42:53 +000076 ret = ida_get_new(&gman->gmr_ida, &id);
Thomas Hellstrom135cba02010-10-26 21:21:47 +020077 if (unlikely(ret == 0 && id >= gman->max_gmr_ids)) {
78 ida_remove(&gman->gmr_ida, id);
Thomas Hellstromfb17f182011-08-31 07:42:53 +000079 ret = 0;
80 goto out_err_locked;
Thomas Hellstrom135cba02010-10-26 21:21:47 +020081 }
Thomas Hellstrom135cba02010-10-26 21:21:47 +020082 } while (ret == -EAGAIN);
83
84 if (likely(ret == 0)) {
85 mem->mm_node = gman;
86 mem->start = id;
Thomas Hellstromfb17f182011-08-31 07:42:53 +000087 mem->num_pages = bo->num_pages;
88 } else
89 goto out_err_locked;
Thomas Hellstrom135cba02010-10-26 21:21:47 +020090
Thomas Hellstromfb17f182011-08-31 07:42:53 +000091 spin_unlock(&gman->lock);
92 return 0;
93
94out_err:
95 spin_lock(&gman->lock);
96out_err_locked:
97 gman->used_gmr_pages -= bo->num_pages;
98 spin_unlock(&gman->lock);
Thomas Hellstrom135cba02010-10-26 21:21:47 +020099 return ret;
100}
101
102static void vmw_gmrid_man_put_node(struct ttm_mem_type_manager *man,
103 struct ttm_mem_reg *mem)
104{
105 struct vmwgfx_gmrid_man *gman =
106 (struct vmwgfx_gmrid_man *)man->priv;
107
108 if (mem->mm_node) {
109 spin_lock(&gman->lock);
110 ida_remove(&gman->gmr_ida, mem->start);
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000111 gman->used_gmr_pages -= mem->num_pages;
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200112 spin_unlock(&gman->lock);
113 mem->mm_node = NULL;
114 }
115}
116
117static int vmw_gmrid_man_init(struct ttm_mem_type_manager *man,
118 unsigned long p_size)
119{
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000120 struct vmw_private *dev_priv =
121 container_of(man->bdev, struct vmw_private, bdev);
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200122 struct vmwgfx_gmrid_man *gman =
123 kzalloc(sizeof(*gman), GFP_KERNEL);
124
125 if (unlikely(gman == NULL))
126 return -ENOMEM;
127
128 spin_lock_init(&gman->lock);
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000129 gman->used_gmr_pages = 0;
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200130 ida_init(&gman->gmr_ida);
Thomas Hellstrom6da768a2012-11-21 11:06:22 +0100131
132 switch (p_size) {
133 case VMW_PL_GMR:
134 gman->max_gmr_ids = dev_priv->max_gmr_ids;
135 gman->max_gmr_pages = dev_priv->max_gmr_pages;
136 break;
137 case VMW_PL_MOB:
138 gman->max_gmr_ids = VMWGFX_NUM_MOB;
139 gman->max_gmr_pages = dev_priv->max_mob_pages;
140 break;
141 default:
142 BUG();
143 }
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200144 man->priv = (void *) gman;
145 return 0;
146}
147
148static int vmw_gmrid_man_takedown(struct ttm_mem_type_manager *man)
149{
150 struct vmwgfx_gmrid_man *gman =
151 (struct vmwgfx_gmrid_man *)man->priv;
152
153 if (gman) {
154 ida_destroy(&gman->gmr_ida);
155 kfree(gman);
156 }
157 return 0;
158}
159
160static void vmw_gmrid_man_debug(struct ttm_mem_type_manager *man,
161 const char *prefix)
162{
163 printk(KERN_INFO "%s: No debug info available for the GMR "
164 "id manager.\n", prefix);
165}
166
167const struct ttm_mem_type_manager_func vmw_gmrid_manager_func = {
168 vmw_gmrid_man_init,
169 vmw_gmrid_man_takedown,
170 vmw_gmrid_man_get_node,
171 vmw_gmrid_man_put_node,
172 vmw_gmrid_man_debug
173};