1 /**************************************************************************
3 * Copyright © 2009-2011 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
30 #include <drm/ttm/ttm_bo_driver.h>
32 #define VMW_PPN_SIZE (sizeof(unsigned long))
33 /* A future safe maximum remap size. */
34 #define VMW_PPN_PER_REMAP ((31 * 1024) / VMW_PPN_SIZE)
36 static int vmw_gmr2_bind(struct vmw_private *dev_priv,
38 unsigned long num_pages,
41 SVGAFifoCmdDefineGMR2 define_cmd;
42 SVGAFifoCmdRemapGMR2 remap_cmd;
45 uint32_t define_size = sizeof(define_cmd) + sizeof(*cmd);
46 uint32_t remap_num = num_pages / VMW_PPN_PER_REMAP + ((num_pages % VMW_PPN_PER_REMAP) > 0);
47 uint32_t remap_size = VMW_PPN_SIZE * num_pages + (sizeof(remap_cmd) + sizeof(*cmd)) * remap_num;
48 uint32_t remap_pos = 0;
49 uint32_t cmd_size = define_size + remap_size;
52 cmd_orig = cmd = vmw_fifo_reserve(dev_priv, cmd_size);
53 if (unlikely(cmd == NULL))
56 define_cmd.gmrId = gmr_id;
57 define_cmd.numPages = num_pages;
59 *cmd++ = SVGA_CMD_DEFINE_GMR2;
60 memcpy(cmd, &define_cmd, sizeof(define_cmd));
61 cmd += sizeof(define_cmd) / sizeof(*cmd);
64 * Need to split the command if there are too many
65 * pages that goes into the gmr.
68 remap_cmd.gmrId = gmr_id;
69 remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ?
70 SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32;
72 while (num_pages > 0) {
73 unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP);
75 remap_cmd.offsetPages = remap_pos;
76 remap_cmd.numPages = nr;
78 *cmd++ = SVGA_CMD_REMAP_GMR2;
79 memcpy(cmd, &remap_cmd, sizeof(remap_cmd));
80 cmd += sizeof(remap_cmd) / sizeof(*cmd);
82 for (i = 0; i < nr; ++i) {
83 if (VMW_PPN_SIZE <= 4)
84 *cmd = page_to_pfn(*pages++);
86 *((uint64_t *)cmd) = page_to_pfn(*pages++);
88 cmd += VMW_PPN_SIZE / sizeof(*cmd);
95 BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd));
97 vmw_fifo_commit(dev_priv, cmd_size);
102 static void vmw_gmr2_unbind(struct vmw_private *dev_priv,
105 SVGAFifoCmdDefineGMR2 define_cmd;
106 uint32_t define_size = sizeof(define_cmd) + 4;
109 cmd = vmw_fifo_reserve(dev_priv, define_size);
110 if (unlikely(cmd == NULL)) {
111 DRM_ERROR("GMR2 unbind failed.\n");
114 define_cmd.gmrId = gmr_id;
115 define_cmd.numPages = 0;
117 *cmd++ = SVGA_CMD_DEFINE_GMR2;
118 memcpy(cmd, &define_cmd, sizeof(define_cmd));
120 vmw_fifo_commit(dev_priv, define_size);
124 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize
125 * the number of used descriptors.
128 static int vmw_gmr_build_descriptors(struct list_head *desc_pages,
129 struct page *pages[],
130 unsigned long num_pages)
132 struct page *page, *next;
133 struct svga_guest_mem_descriptor *page_virtual = NULL;
134 struct svga_guest_mem_descriptor *desc_virtual = NULL;
135 unsigned int desc_per_page;
136 unsigned long prev_pfn;
140 desc_per_page = PAGE_SIZE /
141 sizeof(struct svga_guest_mem_descriptor) - 1;
143 while (likely(num_pages != 0)) {
144 page = alloc_page(__GFP_HIGHMEM);
145 if (unlikely(page == NULL)) {
150 list_add_tail(&page->lru, desc_pages);
153 * Point previous page terminating descriptor to this
154 * page before unmapping it.
157 if (likely(page_virtual != NULL)) {
158 desc_virtual->ppn = page_to_pfn(page);
159 kunmap_atomic(page_virtual);
162 page_virtual = kmap_atomic(page);
163 desc_virtual = page_virtual - 1;
166 while (likely(num_pages != 0)) {
167 pfn = page_to_pfn(*pages);
169 if (pfn != prev_pfn + 1) {
171 if (desc_virtual - page_virtual ==
175 (++desc_virtual)->ppn = cpu_to_le32(pfn);
176 desc_virtual->num_pages = cpu_to_le32(1);
179 le32_to_cpu(desc_virtual->num_pages);
180 desc_virtual->num_pages = cpu_to_le32(tmp + 1);
187 (++desc_virtual)->ppn = cpu_to_le32(0);
188 desc_virtual->num_pages = cpu_to_le32(0);
191 if (likely(page_virtual != NULL))
192 kunmap_atomic(page_virtual);
196 list_for_each_entry_safe(page, next, desc_pages, lru) {
197 list_del_init(&page->lru);
203 static inline void vmw_gmr_free_descriptors(struct list_head *desc_pages)
205 struct page *page, *next;
207 list_for_each_entry_safe(page, next, desc_pages, lru) {
208 list_del_init(&page->lru);
213 static void vmw_gmr_fire_descriptors(struct vmw_private *dev_priv,
214 int gmr_id, struct list_head *desc_pages)
218 if (unlikely(list_empty(desc_pages)))
221 page = list_entry(desc_pages->next, struct page, lru);
223 mutex_lock(&dev_priv->hw_mutex);
225 vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id);
227 vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, page_to_pfn(page));
230 mutex_unlock(&dev_priv->hw_mutex);
235 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize
236 * the number of used descriptors.
239 static unsigned long vmw_gmr_count_descriptors(struct page *pages[],
240 unsigned long num_pages)
242 unsigned long prev_pfn = ~(0UL);
244 unsigned long descriptors = 0;
246 while (num_pages--) {
247 pfn = page_to_pfn(*pages++);
248 if (prev_pfn + 1 != pfn)
256 int vmw_gmr_bind(struct vmw_private *dev_priv,
257 struct page *pages[],
258 unsigned long num_pages,
261 struct list_head desc_pages;
264 if (likely(dev_priv->capabilities & SVGA_CAP_GMR2))
265 return vmw_gmr2_bind(dev_priv, pages, num_pages, gmr_id);
267 if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR)))
270 if (vmw_gmr_count_descriptors(pages, num_pages) >
271 dev_priv->max_gmr_descriptors)
274 INIT_LIST_HEAD(&desc_pages);
276 ret = vmw_gmr_build_descriptors(&desc_pages, pages, num_pages);
277 if (unlikely(ret != 0))
280 vmw_gmr_fire_descriptors(dev_priv, gmr_id, &desc_pages);
281 vmw_gmr_free_descriptors(&desc_pages);
287 void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id)
289 if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) {
290 vmw_gmr2_unbind(dev_priv, gmr_id);
294 mutex_lock(&dev_priv->hw_mutex);
295 vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id);
297 vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, 0);
299 mutex_unlock(&dev_priv->hw_mutex);