1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3  * Copyright 2021 VMware, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  */
26 
27 #include "vmwgfx_drv.h"
28 
29 #include <drm/ttm/ttm_device.h>
30 #include <drm/ttm/ttm_placement.h>
31 #include <drm/ttm/ttm_resource.h>
32 #include <linux/slab.h>
33 
34 
vmw_sys_man_alloc(struct ttm_resource_manager * man,struct ttm_buffer_object * bo,const struct ttm_place * place,struct ttm_resource ** res)35 static int vmw_sys_man_alloc(struct ttm_resource_manager *man,
36 			     struct ttm_buffer_object *bo,
37 			     const struct ttm_place *place,
38 			     struct ttm_resource **res)
39 {
40 	*res = kzalloc(sizeof(**res), GFP_KERNEL);
41 	if (!*res)
42 		return -ENOMEM;
43 
44 	ttm_resource_init(bo, place, *res);
45 	return 0;
46 }
47 
vmw_sys_man_free(struct ttm_resource_manager * man,struct ttm_resource * res)48 static void vmw_sys_man_free(struct ttm_resource_manager *man,
49 			     struct ttm_resource *res)
50 {
51 	ttm_resource_fini(man, res);
52 	kfree(res);
53 }
54 
55 static const struct ttm_resource_manager_func vmw_sys_manager_func = {
56 	.alloc = vmw_sys_man_alloc,
57 	.free = vmw_sys_man_free,
58 };
59 
vmw_sys_man_init(struct vmw_private * dev_priv)60 int vmw_sys_man_init(struct vmw_private *dev_priv)
61 {
62 	struct ttm_device *bdev = &dev_priv->bdev;
63 	struct ttm_resource_manager *man =
64 			kzalloc(sizeof(*man), GFP_KERNEL);
65 
66 	if (!man)
67 		return -ENOMEM;
68 
69 	man->use_tt = true;
70 	man->func = &vmw_sys_manager_func;
71 
72 	ttm_resource_manager_init(man, bdev, 0);
73 	ttm_set_driver_manager(bdev, VMW_PL_SYSTEM, man);
74 	ttm_resource_manager_set_used(man, true);
75 	return 0;
76 }
77 
vmw_sys_man_fini(struct vmw_private * dev_priv)78 void vmw_sys_man_fini(struct vmw_private *dev_priv)
79 {
80 	struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev,
81 							    VMW_PL_SYSTEM);
82 
83 	ttm_resource_manager_evict_all(&dev_priv->bdev, man);
84 
85 	ttm_resource_manager_set_used(man, false);
86 	ttm_resource_manager_cleanup(man);
87 
88 	ttm_set_driver_manager(&dev_priv->bdev, VMW_PL_SYSTEM, NULL);
89 	kfree(man);
90 }
91