162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR MIT */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2021 VMware, Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
662306a36Sopenharmony_ci * obtaining a copy of this software and associated documentation
762306a36Sopenharmony_ci * files (the "Software"), to deal in the Software without
862306a36Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
962306a36Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
1062306a36Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
1162306a36Sopenharmony_ci * furnished to do so, subject to the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be
1462306a36Sopenharmony_ci * included in all copies or substantial portions of the Software.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1762306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1862306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1962306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2062306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2162306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2262306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2362306a36Sopenharmony_ci * SOFTWARE.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci */
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "vmwgfx_drv.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <drm/ttm/ttm_device.h>
3062306a36Sopenharmony_ci#include <drm/ttm/ttm_placement.h>
3162306a36Sopenharmony_ci#include <drm/ttm/ttm_resource.h>
3262306a36Sopenharmony_ci#include <linux/slab.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistatic int vmw_sys_man_alloc(struct ttm_resource_manager *man,
3662306a36Sopenharmony_ci			     struct ttm_buffer_object *bo,
3762306a36Sopenharmony_ci			     const struct ttm_place *place,
3862306a36Sopenharmony_ci			     struct ttm_resource **res)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	*res = kzalloc(sizeof(**res), GFP_KERNEL);
4162306a36Sopenharmony_ci	if (!*res)
4262306a36Sopenharmony_ci		return -ENOMEM;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	ttm_resource_init(bo, place, *res);
4562306a36Sopenharmony_ci	return 0;
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic void vmw_sys_man_free(struct ttm_resource_manager *man,
4962306a36Sopenharmony_ci			     struct ttm_resource *res)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	ttm_resource_fini(man, res);
5262306a36Sopenharmony_ci	kfree(res);
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic const struct ttm_resource_manager_func vmw_sys_manager_func = {
5662306a36Sopenharmony_ci	.alloc = vmw_sys_man_alloc,
5762306a36Sopenharmony_ci	.free = vmw_sys_man_free,
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciint vmw_sys_man_init(struct vmw_private *dev_priv)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	struct ttm_device *bdev = &dev_priv->bdev;
6362306a36Sopenharmony_ci	struct ttm_resource_manager *man =
6462306a36Sopenharmony_ci			kzalloc(sizeof(*man), GFP_KERNEL);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	if (!man)
6762306a36Sopenharmony_ci		return -ENOMEM;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	man->use_tt = true;
7062306a36Sopenharmony_ci	man->func = &vmw_sys_manager_func;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	ttm_resource_manager_init(man, bdev, 0);
7362306a36Sopenharmony_ci	ttm_set_driver_manager(bdev, VMW_PL_SYSTEM, man);
7462306a36Sopenharmony_ci	ttm_resource_manager_set_used(man, true);
7562306a36Sopenharmony_ci	return 0;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_civoid vmw_sys_man_fini(struct vmw_private *dev_priv)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev,
8162306a36Sopenharmony_ci							    VMW_PL_SYSTEM);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	ttm_resource_manager_evict_all(&dev_priv->bdev, man);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	ttm_resource_manager_set_used(man, false);
8662306a36Sopenharmony_ci	ttm_resource_manager_cleanup(man);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	ttm_set_driver_manager(&dev_priv->bdev, VMW_PL_SYSTEM, NULL);
8962306a36Sopenharmony_ci	kfree(man);
9062306a36Sopenharmony_ci}
91