1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2007-2010 VMware, Inc., Palo Alto, CA., USA
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"
32 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <linux/idr.h>
35 #include <linux/spinlock.h>
36 #include <linux/kernel.h>
37 
38 struct vmwgfx_gmrid_man {
39 	struct ttm_resource_manager manager;
40 	spinlock_t lock;
41 	struct ida gmr_ida;
42 	uint32_t max_gmr_ids;
43 	uint32_t max_gmr_pages;
44 	uint32_t used_gmr_pages;
45 	uint8_t type;
46 };
47 
48 static struct vmwgfx_gmrid_man *to_gmrid_manager(struct ttm_resource_manager *man)
49 {
50 	return container_of(man, struct vmwgfx_gmrid_man, manager);
51 }
52 
53 static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
54 				  struct ttm_buffer_object *bo,
55 				  const struct ttm_place *place,
56 				  struct ttm_resource **res)
57 {
58 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
59 	int id;
60 
61 	*res = kmalloc(sizeof(**res), GFP_KERNEL);
62 	if (!*res)
63 		return -ENOMEM;
64 
65 	ttm_resource_init(bo, place, *res);
66 
67 	id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
68 	if (id < 0)
69 		return id;
70 
71 	spin_lock(&gman->lock);
72 
73 	if (gman->max_gmr_pages > 0) {
74 		gman->used_gmr_pages += (*res)->num_pages;
75 		/*
76 		 * Because the graphics memory is a soft limit we can try to
77 		 * expand it instead of letting the userspace apps crash.
78 		 * We're just going to have a sane limit (half of RAM)
79 		 * on the number of MOB's that we create and will try to keep
80 		 * the system running until we reach that.
81 		 */
82 		if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages)) {
83 			const unsigned long max_graphics_pages = totalram_pages() / 2;
84 			uint32_t new_max_pages = 0;
85 
86 			DRM_WARN("vmwgfx: mob memory overflow. Consider increasing guest RAM and graphicsMemory.\n");
87 			vmw_host_printf("vmwgfx, warning: mob memory overflow. Consider increasing guest RAM and graphicsMemory.\n");
88 
89 			if (gman->max_gmr_pages > (max_graphics_pages / 2)) {
90 				DRM_WARN("vmwgfx: guest requires more than half of RAM for graphics.\n");
91 				new_max_pages = max_graphics_pages;
92 			} else
93 				new_max_pages = gman->max_gmr_pages * 2;
94 			if (new_max_pages > gman->max_gmr_pages && new_max_pages >= gman->used_gmr_pages) {
95 				DRM_WARN("vmwgfx: increasing guest mob limits to %u kB.\n",
96 					 ((new_max_pages) << (PAGE_SHIFT - 10)));
97 
98 				gman->max_gmr_pages = new_max_pages;
99 			} else {
100 				char buf[256];
101 				snprintf(buf, sizeof(buf),
102 					 "vmwgfx, error: guest graphics is out of memory (mob limit at: %ukB).\n",
103 					 ((gman->max_gmr_pages) << (PAGE_SHIFT - 10)));
104 				vmw_host_printf(buf);
105 				DRM_WARN("%s", buf);
106 				goto nospace;
107 			}
108 		}
109 	}
110 
111 	(*res)->start = id;
112 
113 	spin_unlock(&gman->lock);
114 	return 0;
115 
116 nospace:
117 	gman->used_gmr_pages -= (*res)->num_pages;
118 	spin_unlock(&gman->lock);
119 	ida_free(&gman->gmr_ida, id);
120 	kfree(*res);
121 	return -ENOSPC;
122 }
123 
124 static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
125 				   struct ttm_resource *res)
126 {
127 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
128 
129 	ida_free(&gman->gmr_ida, res->start);
130 	spin_lock(&gman->lock);
131 	gman->used_gmr_pages -= res->num_pages;
132 	spin_unlock(&gman->lock);
133 	kfree(res);
134 }
135 
136 static void vmw_gmrid_man_debug(struct ttm_resource_manager *man,
137 				struct drm_printer *printer)
138 {
139 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
140 
141 	BUG_ON(gman->type != VMW_PL_GMR && gman->type != VMW_PL_MOB);
142 
143 	drm_printf(printer, "%s's used: %u pages, max: %u pages, %u id's\n",
144 		   (gman->type == VMW_PL_MOB) ? "Mob" : "GMR",
145 		   gman->used_gmr_pages, gman->max_gmr_pages, gman->max_gmr_ids);
146 }
147 
148 static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
149 
150 int vmw_gmrid_man_init(struct vmw_private *dev_priv, int type)
151 {
152 	struct ttm_resource_manager *man;
153 	struct vmwgfx_gmrid_man *gman =
154 		kzalloc(sizeof(*gman), GFP_KERNEL);
155 
156 	if (unlikely(!gman))
157 		return -ENOMEM;
158 
159 	man = &gman->manager;
160 
161 	man->func = &vmw_gmrid_manager_func;
162 	man->use_tt = true;
163 	ttm_resource_manager_init(man, 0);
164 	spin_lock_init(&gman->lock);
165 	gman->used_gmr_pages = 0;
166 	ida_init(&gman->gmr_ida);
167 	gman->type = type;
168 
169 	switch (type) {
170 	case VMW_PL_GMR:
171 		gman->max_gmr_ids = dev_priv->max_gmr_ids;
172 		gman->max_gmr_pages = dev_priv->max_gmr_pages;
173 		break;
174 	case VMW_PL_MOB:
175 		gman->max_gmr_ids = VMWGFX_NUM_MOB;
176 		gman->max_gmr_pages = dev_priv->max_mob_pages;
177 		break;
178 	default:
179 		BUG();
180 	}
181 	ttm_set_driver_manager(&dev_priv->bdev, type, &gman->manager);
182 	ttm_resource_manager_set_used(man, true);
183 	return 0;
184 }
185 
186 void vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type)
187 {
188 	struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, type);
189 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
190 
191 	ttm_resource_manager_set_used(man, false);
192 
193 	ttm_resource_manager_evict_all(&dev_priv->bdev, man);
194 
195 	ttm_resource_manager_cleanup(man);
196 
197 	ttm_set_driver_manager(&dev_priv->bdev, type, NULL);
198 	ida_destroy(&gman->gmr_ida);
199 	kfree(gman);
200 
201 }
202 
203 static const struct ttm_resource_manager_func vmw_gmrid_manager_func = {
204 	.alloc = vmw_gmrid_man_get_node,
205 	.free = vmw_gmrid_man_put_node,
206 	.debug = vmw_gmrid_man_debug
207 };
208