1 /*        $NetBSD: vmwgfx_gmr.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $       */
2 
3 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 /**************************************************************************
5  *
6  * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_gmr.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
32 
33 #include <drm/ttm/ttm_bo_driver.h>
34 
35 #include "vmwgfx_drv.h"
36 
37 #define VMW_PPN_SIZE (sizeof(unsigned long))
38 /* A future safe maximum remap size. */
39 #define VMW_PPN_PER_REMAP ((31 * 1024) / VMW_PPN_SIZE)
40 #define DMA_ADDR_INVALID ((dma_addr_t) 0)
41 #define DMA_PAGE_INVALID 0UL
42 
vmw_gmr2_bind(struct vmw_private * dev_priv,struct vmw_piter * iter,unsigned long num_pages,int gmr_id)43 static int vmw_gmr2_bind(struct vmw_private *dev_priv,
44                                struct vmw_piter *iter,
45                                unsigned long num_pages,
46                                int gmr_id)
47 {
48           SVGAFifoCmdDefineGMR2 define_cmd;
49           SVGAFifoCmdRemapGMR2 remap_cmd;
50           uint32_t *cmd;
51           uint32_t *cmd_orig;
52           uint32_t define_size = sizeof(define_cmd) + sizeof(*cmd);
53           uint32_t remap_num = num_pages / VMW_PPN_PER_REMAP + ((num_pages % VMW_PPN_PER_REMAP) > 0);
54           uint32_t remap_size = VMW_PPN_SIZE * num_pages + (sizeof(remap_cmd) + sizeof(*cmd)) * remap_num;
55           uint32_t remap_pos = 0;
56           uint32_t cmd_size = define_size + remap_size;
57           uint32_t i;
58 
59           cmd_orig = cmd = VMW_FIFO_RESERVE(dev_priv, cmd_size);
60           if (unlikely(cmd == NULL))
61                     return -ENOMEM;
62 
63           define_cmd.gmrId = gmr_id;
64           define_cmd.numPages = num_pages;
65 
66           *cmd++ = SVGA_CMD_DEFINE_GMR2;
67           memcpy(cmd, &define_cmd, sizeof(define_cmd));
68           cmd += sizeof(define_cmd) / sizeof(*cmd);
69 
70           /*
71            * Need to split the command if there are too many
72            * pages that goes into the gmr.
73            */
74 
75           remap_cmd.gmrId = gmr_id;
76           remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ?
77                     SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32;
78 
79           while (num_pages > 0) {
80                     unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP);
81 
82                     remap_cmd.offsetPages = remap_pos;
83                     remap_cmd.numPages = nr;
84 
85                     *cmd++ = SVGA_CMD_REMAP_GMR2;
86                     memcpy(cmd, &remap_cmd, sizeof(remap_cmd));
87                     cmd += sizeof(remap_cmd) / sizeof(*cmd);
88 
89                     for (i = 0; i < nr; ++i) {
90                               if (VMW_PPN_SIZE <= 4)
91                                         *cmd = vmw_piter_dma_addr(iter) >> PAGE_SHIFT;
92                               else
93                                         *((uint64_t *)cmd) = vmw_piter_dma_addr(iter) >>
94                                                   PAGE_SHIFT;
95 
96                               cmd += VMW_PPN_SIZE / sizeof(*cmd);
97                               vmw_piter_next(iter);
98                     }
99 
100                     num_pages -= nr;
101                     remap_pos += nr;
102           }
103 
104           BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd));
105 
106           vmw_fifo_commit(dev_priv, cmd_size);
107 
108           return 0;
109 }
110 
vmw_gmr2_unbind(struct vmw_private * dev_priv,int gmr_id)111 static void vmw_gmr2_unbind(struct vmw_private *dev_priv,
112                                   int gmr_id)
113 {
114           SVGAFifoCmdDefineGMR2 define_cmd;
115           uint32_t define_size = sizeof(define_cmd) + 4;
116           uint32_t *cmd;
117 
118           cmd = VMW_FIFO_RESERVE(dev_priv, define_size);
119           if (unlikely(cmd == NULL))
120                     return;
121 
122           define_cmd.gmrId = gmr_id;
123           define_cmd.numPages = 0;
124 
125           *cmd++ = SVGA_CMD_DEFINE_GMR2;
126           memcpy(cmd, &define_cmd, sizeof(define_cmd));
127 
128           vmw_fifo_commit(dev_priv, define_size);
129 }
130 
131 
vmw_gmr_bind(struct vmw_private * dev_priv,const struct vmw_sg_table * vsgt,unsigned long num_pages,int gmr_id)132 int vmw_gmr_bind(struct vmw_private *dev_priv,
133                      const struct vmw_sg_table *vsgt,
134                      unsigned long num_pages,
135                      int gmr_id)
136 {
137           struct vmw_piter data_iter;
138 
139           vmw_piter_start(&data_iter, vsgt, 0);
140 
141           if (unlikely(!vmw_piter_next(&data_iter)))
142                     return 0;
143 
144           if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR2)))
145                     return -EINVAL;
146 
147           return vmw_gmr2_bind(dev_priv, &data_iter, num_pages, gmr_id);
148 }
149 
150 
vmw_gmr_unbind(struct vmw_private * dev_priv,int gmr_id)151 void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id)
152 {
153           if (likely(dev_priv->capabilities & SVGA_CAP_GMR2))
154                     vmw_gmr2_unbind(dev_priv, gmr_id);
155 }
156