vmwgfx_gmr.c revision 1.1.1.2.28.1 1 /* $NetBSD: vmwgfx_gmr.c,v 1.1.1.2.28.1 2018/09/06 06:56:34 pgoyette Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
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.1.1.2.28.1 2018/09/06 06:56:34 pgoyette Exp $");
32
33 #include "vmwgfx_drv.h"
34 #include <drm/drmP.h>
35 #include <drm/ttm/ttm_bo_driver.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
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
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 DRM_ERROR("GMR2 unbind failed.\n");
121 return;
122 }
123 define_cmd.gmrId = gmr_id;
124 define_cmd.numPages = 0;
125
126 *cmd++ = SVGA_CMD_DEFINE_GMR2;
127 memcpy(cmd, &define_cmd, sizeof(define_cmd));
128
129 vmw_fifo_commit(dev_priv, define_size);
130 }
131
132
133 int vmw_gmr_bind(struct vmw_private *dev_priv,
134 const struct vmw_sg_table *vsgt,
135 unsigned long num_pages,
136 int gmr_id)
137 {
138 struct vmw_piter data_iter;
139
140 vmw_piter_start(&data_iter, vsgt, 0);
141
142 if (unlikely(!vmw_piter_next(&data_iter)))
143 return 0;
144
145 if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR2)))
146 return -EINVAL;
147
148 return vmw_gmr2_bind(dev_priv, &data_iter, num_pages, gmr_id);
149 }
150
151
152 void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id)
153 {
154 if (likely(dev_priv->capabilities & SVGA_CAP_GMR2))
155 vmw_gmr2_unbind(dev_priv, gmr_id);
156 }
157