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_bo_driver.h>
30 #include <drm/ttm/ttm_device.h>
31 #include <drm/ttm/ttm_placement.h>
32 #include <drm/ttm/ttm_resource.h>
33 #include <linux/slab.h>
34 
35 
36 static int vmw_sys_man_alloc(struct ttm_resource_manager *man,
37 			     struct ttm_buffer_object *bo,
38 			     const struct ttm_place *place,
39 			     struct ttm_resource **res)
40 {
41 	*res = kzalloc(sizeof(**res), GFP_KERNEL);
42 	if (!*res)
43 		return -ENOMEM;
44 
45 	ttm_resource_init(bo, place, *res);
46 	return 0;
47 }
48 
49 static void vmw_sys_man_free(struct ttm_resource_manager *man,
50 			     struct ttm_resource *res)
51 {
52 	ttm_resource_fini(man, res);
53 	kfree(res);
54 }
55 
56 static const struct ttm_resource_manager_func vmw_sys_manager_func = {
57 	.alloc = vmw_sys_man_alloc,
58 	.free = vmw_sys_man_free,
59 };
60 
61 int vmw_sys_man_init(struct vmw_private *dev_priv)
62 {
63 	struct ttm_device *bdev = &dev_priv->bdev;
64 	struct ttm_resource_manager *man =
65 			kzalloc(sizeof(*man), GFP_KERNEL);
66 
67 	if (!man)
68 		return -ENOMEM;
69 
70 	man->use_tt = true;
71 	man->func = &vmw_sys_manager_func;
72 
73 	ttm_resource_manager_init(man, bdev, 0);
74 	ttm_set_driver_manager(bdev, VMW_PL_SYSTEM, man);
75 	ttm_resource_manager_set_used(man, true);
76 	return 0;
77 }
78 
79 void vmw_sys_man_fini(struct vmw_private *dev_priv)
80 {
81 	struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev,
82 							    VMW_PL_SYSTEM);
83 
84 	ttm_resource_manager_evict_all(&dev_priv->bdev, man);
85 
86 	ttm_resource_manager_set_used(man, false);
87 	ttm_resource_manager_cleanup(man);
88 
89 	ttm_set_driver_manager(&dev_priv->bdev, VMW_PL_SYSTEM, NULL);
90 	kfree(man);
91 }
92