Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / vmwgfx_gmrid_manager.c
blobbe325a62c1781c17329fd6fbccafb12b859999e1
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2007-2010 VMware, Inc., Palo Alto, CA., USA
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:
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.
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include "vmwgfx_drv.h"
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <linux/idr.h>
36 #include <linux/spinlock.h>
37 #include <linux/kernel.h>
39 struct vmwgfx_gmrid_man {
40 struct ttm_resource_manager manager;
41 spinlock_t lock;
42 struct ida gmr_ida;
43 uint32_t max_gmr_ids;
44 uint32_t max_gmr_pages;
45 uint32_t used_gmr_pages;
48 static struct vmwgfx_gmrid_man *to_gmrid_manager(struct ttm_resource_manager *man)
50 return container_of(man, struct vmwgfx_gmrid_man, manager);
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 *mem)
58 struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
59 int id;
61 id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL);
62 if (id < 0)
63 return id;
65 spin_lock(&gman->lock);
67 if (gman->max_gmr_pages > 0) {
68 gman->used_gmr_pages += bo->num_pages;
69 if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages))
70 goto nospace;
73 mem->mm_node = gman;
74 mem->start = id;
75 mem->num_pages = bo->num_pages;
77 spin_unlock(&gman->lock);
78 return 0;
80 nospace:
81 gman->used_gmr_pages -= bo->num_pages;
82 spin_unlock(&gman->lock);
83 ida_free(&gman->gmr_ida, id);
84 return -ENOSPC;
87 static void vmw_gmrid_man_put_node(struct ttm_resource_manager *man,
88 struct ttm_resource *mem)
90 struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
92 if (mem->mm_node) {
93 ida_free(&gman->gmr_ida, mem->start);
94 spin_lock(&gman->lock);
95 gman->used_gmr_pages -= mem->num_pages;
96 spin_unlock(&gman->lock);
97 mem->mm_node = NULL;
101 static const struct ttm_resource_manager_func vmw_gmrid_manager_func;
103 int vmw_gmrid_man_init(struct vmw_private *dev_priv, int type)
105 struct ttm_resource_manager *man;
106 struct vmwgfx_gmrid_man *gman =
107 kzalloc(sizeof(*gman), GFP_KERNEL);
109 if (unlikely(!gman))
110 return -ENOMEM;
112 man = &gman->manager;
114 man->func = &vmw_gmrid_manager_func;
115 /* TODO: This is most likely not correct */
116 man->use_tt = true;
117 ttm_resource_manager_init(man, 0);
118 spin_lock_init(&gman->lock);
119 gman->used_gmr_pages = 0;
120 ida_init(&gman->gmr_ida);
122 switch (type) {
123 case VMW_PL_GMR:
124 gman->max_gmr_ids = dev_priv->max_gmr_ids;
125 gman->max_gmr_pages = dev_priv->max_gmr_pages;
126 break;
127 case VMW_PL_MOB:
128 gman->max_gmr_ids = VMWGFX_NUM_MOB;
129 gman->max_gmr_pages = dev_priv->max_mob_pages;
130 break;
131 default:
132 BUG();
134 ttm_set_driver_manager(&dev_priv->bdev, type, &gman->manager);
135 ttm_resource_manager_set_used(man, true);
136 return 0;
139 void vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type)
141 struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, type);
142 struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man);
144 ttm_resource_manager_set_used(man, false);
146 ttm_resource_manager_evict_all(&dev_priv->bdev, man);
148 ttm_resource_manager_cleanup(man);
150 ttm_set_driver_manager(&dev_priv->bdev, type, NULL);
151 ida_destroy(&gman->gmr_ida);
152 kfree(gman);
156 static const struct ttm_resource_manager_func vmw_gmrid_manager_func = {
157 .alloc = vmw_gmrid_man_get_node,
158 .free = vmw_gmrid_man_put_node,