18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT 28c2ecf20Sopenharmony_ci/************************************************************************** 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright 2009-2015 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#include <drm/ttm/ttm_bo_driver.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include "vmwgfx_drv.h" 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define VMW_PPN_SIZE (sizeof(unsigned long)) 338c2ecf20Sopenharmony_ci/* A future safe maximum remap size. */ 348c2ecf20Sopenharmony_ci#define VMW_PPN_PER_REMAP ((31 * 1024) / VMW_PPN_SIZE) 358c2ecf20Sopenharmony_ci#define DMA_ADDR_INVALID ((dma_addr_t) 0) 368c2ecf20Sopenharmony_ci#define DMA_PAGE_INVALID 0UL 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic int vmw_gmr2_bind(struct vmw_private *dev_priv, 398c2ecf20Sopenharmony_ci struct vmw_piter *iter, 408c2ecf20Sopenharmony_ci unsigned long num_pages, 418c2ecf20Sopenharmony_ci int gmr_id) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci SVGAFifoCmdDefineGMR2 define_cmd; 448c2ecf20Sopenharmony_ci SVGAFifoCmdRemapGMR2 remap_cmd; 458c2ecf20Sopenharmony_ci uint32_t *cmd; 468c2ecf20Sopenharmony_ci uint32_t *cmd_orig; 478c2ecf20Sopenharmony_ci uint32_t define_size = sizeof(define_cmd) + sizeof(*cmd); 488c2ecf20Sopenharmony_ci uint32_t remap_num = num_pages / VMW_PPN_PER_REMAP + ((num_pages % VMW_PPN_PER_REMAP) > 0); 498c2ecf20Sopenharmony_ci uint32_t remap_size = VMW_PPN_SIZE * num_pages + (sizeof(remap_cmd) + sizeof(*cmd)) * remap_num; 508c2ecf20Sopenharmony_ci uint32_t remap_pos = 0; 518c2ecf20Sopenharmony_ci uint32_t cmd_size = define_size + remap_size; 528c2ecf20Sopenharmony_ci uint32_t i; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci cmd_orig = cmd = VMW_FIFO_RESERVE(dev_priv, cmd_size); 558c2ecf20Sopenharmony_ci if (unlikely(cmd == NULL)) 568c2ecf20Sopenharmony_ci return -ENOMEM; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci define_cmd.gmrId = gmr_id; 598c2ecf20Sopenharmony_ci define_cmd.numPages = num_pages; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci *cmd++ = SVGA_CMD_DEFINE_GMR2; 628c2ecf20Sopenharmony_ci memcpy(cmd, &define_cmd, sizeof(define_cmd)); 638c2ecf20Sopenharmony_ci cmd += sizeof(define_cmd) / sizeof(*cmd); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* 668c2ecf20Sopenharmony_ci * Need to split the command if there are too many 678c2ecf20Sopenharmony_ci * pages that goes into the gmr. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci remap_cmd.gmrId = gmr_id; 718c2ecf20Sopenharmony_ci remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ? 728c2ecf20Sopenharmony_ci SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci while (num_pages > 0) { 758c2ecf20Sopenharmony_ci unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci remap_cmd.offsetPages = remap_pos; 788c2ecf20Sopenharmony_ci remap_cmd.numPages = nr; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci *cmd++ = SVGA_CMD_REMAP_GMR2; 818c2ecf20Sopenharmony_ci memcpy(cmd, &remap_cmd, sizeof(remap_cmd)); 828c2ecf20Sopenharmony_ci cmd += sizeof(remap_cmd) / sizeof(*cmd); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci for (i = 0; i < nr; ++i) { 858c2ecf20Sopenharmony_ci if (VMW_PPN_SIZE <= 4) 868c2ecf20Sopenharmony_ci *cmd = vmw_piter_dma_addr(iter) >> PAGE_SHIFT; 878c2ecf20Sopenharmony_ci else 888c2ecf20Sopenharmony_ci *((uint64_t *)cmd) = vmw_piter_dma_addr(iter) >> 898c2ecf20Sopenharmony_ci PAGE_SHIFT; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci cmd += VMW_PPN_SIZE / sizeof(*cmd); 928c2ecf20Sopenharmony_ci vmw_piter_next(iter); 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci num_pages -= nr; 968c2ecf20Sopenharmony_ci remap_pos += nr; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd)); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci vmw_fifo_commit(dev_priv, cmd_size); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic void vmw_gmr2_unbind(struct vmw_private *dev_priv, 1078c2ecf20Sopenharmony_ci int gmr_id) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci SVGAFifoCmdDefineGMR2 define_cmd; 1108c2ecf20Sopenharmony_ci uint32_t define_size = sizeof(define_cmd) + 4; 1118c2ecf20Sopenharmony_ci uint32_t *cmd; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci cmd = VMW_FIFO_RESERVE(dev_priv, define_size); 1148c2ecf20Sopenharmony_ci if (unlikely(cmd == NULL)) 1158c2ecf20Sopenharmony_ci return; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci define_cmd.gmrId = gmr_id; 1188c2ecf20Sopenharmony_ci define_cmd.numPages = 0; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci *cmd++ = SVGA_CMD_DEFINE_GMR2; 1218c2ecf20Sopenharmony_ci memcpy(cmd, &define_cmd, sizeof(define_cmd)); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci vmw_fifo_commit(dev_priv, define_size); 1248c2ecf20Sopenharmony_ci} 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ciint vmw_gmr_bind(struct vmw_private *dev_priv, 1288c2ecf20Sopenharmony_ci const struct vmw_sg_table *vsgt, 1298c2ecf20Sopenharmony_ci unsigned long num_pages, 1308c2ecf20Sopenharmony_ci int gmr_id) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci struct vmw_piter data_iter; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci vmw_piter_start(&data_iter, vsgt, 0); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (unlikely(!vmw_piter_next(&data_iter))) 1378c2ecf20Sopenharmony_ci return 0; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR2))) 1408c2ecf20Sopenharmony_ci return -EINVAL; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return vmw_gmr2_bind(dev_priv, &data_iter, num_pages, gmr_id); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_civoid vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) 1498c2ecf20Sopenharmony_ci vmw_gmr2_unbind(dev_priv, gmr_id); 1508c2ecf20Sopenharmony_ci} 151