18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT 28c2ecf20Sopenharmony_ci/************************************************************************** 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright 2007-2010 VMware, Inc., Palo Alto, CA., USA 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the 88c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 98c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 108c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 118c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 128c2ecf20Sopenharmony_ci * the following conditions: 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the 158c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 168c2ecf20Sopenharmony_ci * of the Software. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 228c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 238c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 248c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci **************************************************************************/ 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "vmwgfx_drv.h" 328c2ecf20Sopenharmony_ci#include <drm/ttm/ttm_module.h> 338c2ecf20Sopenharmony_ci#include <drm/ttm/ttm_bo_driver.h> 348c2ecf20Sopenharmony_ci#include <drm/ttm/ttm_placement.h> 358c2ecf20Sopenharmony_ci#include <linux/idr.h> 368c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 378c2ecf20Sopenharmony_ci#include <linux/kernel.h> 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistruct vmwgfx_gmrid_man { 408c2ecf20Sopenharmony_ci struct ttm_resource_manager manager; 418c2ecf20Sopenharmony_ci spinlock_t lock; 428c2ecf20Sopenharmony_ci struct ida gmr_ida; 438c2ecf20Sopenharmony_ci uint32_t max_gmr_ids; 448c2ecf20Sopenharmony_ci uint32_t max_gmr_pages; 458c2ecf20Sopenharmony_ci uint32_t used_gmr_pages; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic struct vmwgfx_gmrid_man *to_gmrid_manager(struct ttm_resource_manager *man) 498c2ecf20Sopenharmony_ci{ 508c2ecf20Sopenharmony_ci return container_of(man, struct vmwgfx_gmrid_man, manager); 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int vmw_gmrid_man_get_node(struct ttm_resource_manager *man, 548c2ecf20Sopenharmony_ci struct ttm_buffer_object *bo, 558c2ecf20Sopenharmony_ci const struct ttm_place *place, 568c2ecf20Sopenharmony_ci struct ttm_resource *mem) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man); 598c2ecf20Sopenharmony_ci int id; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci id = ida_alloc_max(&gman->gmr_ida, gman->max_gmr_ids - 1, GFP_KERNEL); 628c2ecf20Sopenharmony_ci if (id < 0) 638c2ecf20Sopenharmony_ci return id; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci spin_lock(&gman->lock); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (gman->max_gmr_pages > 0) { 688c2ecf20Sopenharmony_ci gman->used_gmr_pages += bo->num_pages; 698c2ecf20Sopenharmony_ci if (unlikely(gman->used_gmr_pages > gman->max_gmr_pages)) 708c2ecf20Sopenharmony_ci goto nospace; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci mem->mm_node = gman; 748c2ecf20Sopenharmony_ci mem->start = id; 758c2ecf20Sopenharmony_ci mem->num_pages = bo->num_pages; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci spin_unlock(&gman->lock); 788c2ecf20Sopenharmony_ci return 0; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cinospace: 818c2ecf20Sopenharmony_ci gman->used_gmr_pages -= bo->num_pages; 828c2ecf20Sopenharmony_ci spin_unlock(&gman->lock); 838c2ecf20Sopenharmony_ci ida_free(&gman->gmr_ida, id); 848c2ecf20Sopenharmony_ci return -ENOSPC; 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic void vmw_gmrid_man_put_node(struct ttm_resource_manager *man, 888c2ecf20Sopenharmony_ci struct ttm_resource *mem) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (mem->mm_node) { 938c2ecf20Sopenharmony_ci ida_free(&gman->gmr_ida, mem->start); 948c2ecf20Sopenharmony_ci spin_lock(&gman->lock); 958c2ecf20Sopenharmony_ci gman->used_gmr_pages -= mem->num_pages; 968c2ecf20Sopenharmony_ci spin_unlock(&gman->lock); 978c2ecf20Sopenharmony_ci mem->mm_node = NULL; 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic const struct ttm_resource_manager_func vmw_gmrid_manager_func; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciint vmw_gmrid_man_init(struct vmw_private *dev_priv, int type) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci struct ttm_resource_manager *man; 1068c2ecf20Sopenharmony_ci struct vmwgfx_gmrid_man *gman = 1078c2ecf20Sopenharmony_ci kzalloc(sizeof(*gman), GFP_KERNEL); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci if (unlikely(!gman)) 1108c2ecf20Sopenharmony_ci return -ENOMEM; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci man = &gman->manager; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci man->func = &vmw_gmrid_manager_func; 1158c2ecf20Sopenharmony_ci /* TODO: This is most likely not correct */ 1168c2ecf20Sopenharmony_ci man->use_tt = true; 1178c2ecf20Sopenharmony_ci ttm_resource_manager_init(man, 0); 1188c2ecf20Sopenharmony_ci spin_lock_init(&gman->lock); 1198c2ecf20Sopenharmony_ci gman->used_gmr_pages = 0; 1208c2ecf20Sopenharmony_ci ida_init(&gman->gmr_ida); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci switch (type) { 1238c2ecf20Sopenharmony_ci case VMW_PL_GMR: 1248c2ecf20Sopenharmony_ci gman->max_gmr_ids = dev_priv->max_gmr_ids; 1258c2ecf20Sopenharmony_ci gman->max_gmr_pages = dev_priv->max_gmr_pages; 1268c2ecf20Sopenharmony_ci break; 1278c2ecf20Sopenharmony_ci case VMW_PL_MOB: 1288c2ecf20Sopenharmony_ci gman->max_gmr_ids = VMWGFX_NUM_MOB; 1298c2ecf20Sopenharmony_ci gman->max_gmr_pages = dev_priv->max_mob_pages; 1308c2ecf20Sopenharmony_ci break; 1318c2ecf20Sopenharmony_ci default: 1328c2ecf20Sopenharmony_ci BUG(); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci ttm_set_driver_manager(&dev_priv->bdev, type, &gman->manager); 1358c2ecf20Sopenharmony_ci ttm_resource_manager_set_used(man, true); 1368c2ecf20Sopenharmony_ci return 0; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_civoid vmw_gmrid_man_fini(struct vmw_private *dev_priv, int type) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, type); 1428c2ecf20Sopenharmony_ci struct vmwgfx_gmrid_man *gman = to_gmrid_manager(man); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci ttm_resource_manager_set_used(man, false); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci ttm_resource_manager_force_list_clean(&dev_priv->bdev, man); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci ttm_resource_manager_cleanup(man); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci ttm_set_driver_manager(&dev_priv->bdev, type, NULL); 1518c2ecf20Sopenharmony_ci ida_destroy(&gman->gmr_ida); 1528c2ecf20Sopenharmony_ci kfree(gman); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic const struct ttm_resource_manager_func vmw_gmrid_manager_func = { 1578c2ecf20Sopenharmony_ci .alloc = vmw_gmrid_man_get_node, 1588c2ecf20Sopenharmony_ci .free = vmw_gmrid_man_put_node, 1598c2ecf20Sopenharmony_ci}; 160