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 };
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 * mem)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 *mem)
56 {
57 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
58 	int id;
59 
60 	id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
61 	if (id < 0)
62 		return id;
63 
64 	spin_lock(&gman->lock);
65 
66 	if (gman->max_gmr_pages > 0) {
67 		gman->used_gmr_pages += mem->num_pages;
68 		if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
69 			goto nospace;
70 	}
71 
72 	mem->mm_node = gman;
73 	mem->start = id;
74 
75 	spin_unlock(&gman->lock);
76 	return 0;
77 
78 nospace:
79 	gman->used_gmr_pages -= mem->num_pages;
80 	spin_unlock(&gman->lock);
81 	ida_free(&gman->gmr_ida, id);
82 	return -ENOSPC;
83 }
84 
vmw_gmrid_man_put_node(struct ttm_resource_manager * man,struct ttm_resource * mem)85 static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
86 				   struct ttm_resource *mem)
87 {
88 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
89 
90 	if (mem->mm_node) {
91 		ida_free(&gman->gmr_ida, mem->start);
92 		spin_lock(&gman->lock);
93 		gman->used_gmr_pages -= mem->num_pages;
94 		spin_unlock(&gman->lock);
95 		mem->mm_node = NULL;
96 	}
97 }
98 
99 static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
100 
vmw_gmrid_man_init(struct vmw_private * dev_priv,int type)101 int vmw_gmrid_man_init(struct vmw_private *dev_priv, int type)
102 {
103 	struct ttm_resource_manager *man;
104 	struct vmwgfx_gmrid_man *gman =
105 		kzalloc(sizeof(*gman), GFP_KERNEL);
106 
107 	if (unlikely(!gman))
108 		return -ENOMEM;
109 
110 	man = &gman->manager;
111 
112 	man->func = &vmw_gmrid_manager_func;
113 	/* TODO: This is most likely not correct */
114 	man->use_tt = true;
115 	ttm_resource_manager_init(man, 0);
116 	spin_lock_init(&gman->lock);
117 	gman->used_gmr_pages = 0;
118 	ida_init(&gman->gmr_ida);
119 
120 	switch (type) {
121 	case VMW_PL_GMR:
122 		gman->max_gmr_ids = dev_priv->max_gmr_ids;
123 		gman->max_gmr_pages = dev_priv->max_gmr_pages;
124 		break;
125 	case VMW_PL_MOB:
126 		gman->max_gmr_ids = VMWGFX_NUM_MOB;
127 		gman->max_gmr_pages = dev_priv->max_mob_pages;
128 		break;
129 	default:
130 		BUG();
131 	}
132 	ttm_set_driver_manager(&dev_priv->bdev, type, &gman->manager);
133 	ttm_resource_manager_set_used(man, true);
134 	return 0;
135 }
136 
vmw_gmrid_man_fini(struct vmw_private * dev_priv,int type)137 void vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type)
138 {
139 	struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, type);
140 	struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
141 
142 	ttm_resource_manager_set_used(man, false);
143 
144 	ttm_resource_manager_evict_all(&dev_priv->bdev, man);
145 
146 	ttm_resource_manager_cleanup(man);
147 
148 	ttm_set_driver_manager(&dev_priv->bdev, type, NULL);
149 	ida_destroy(&gman->gmr_ida);
150 	kfree(gman);
151 
152 }
153 
154 static const struct ttm_resource_manager_func vmw_gmrid_manager_func = {
155 	.alloc = vmw_gmrid_man_get_node,
156 	.free = vmw_gmrid_man_put_node,
157 };
158