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