1 /************************************************************************** 2 * 3 * Copyright © 2009-2011 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 5 * 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: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 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. 25 * 26 **************************************************************************/ 27 28 #include "vmwgfx_drv.h" 29 #include <drm/drmP.h> 30 #include <drm/ttm/ttm_bo_driver.h> 31 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) 35 36 static int vmw_gmr2_bind(struct vmw_private *dev_priv, 37 struct page *pages[], 38 unsigned long num_pages, 39 int gmr_id) 40 { 41 SVGAFifoCmdDefineGMR2 define_cmd; 42 SVGAFifoCmdRemapGMR2 remap_cmd; 43 uint32_t *cmd; 44 uint32_t *cmd_orig; 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; 50 uint32_t i; 51 52 cmd_orig = cmd = vmw_fifo_reserve(dev_priv, cmd_size); 53 if (unlikely(cmd == NULL)) 54 return -ENOMEM; 55 56 define_cmd.gmrId = gmr_id; 57 define_cmd.numPages = num_pages; 58 59 *cmd++ = SVGA_CMD_DEFINE_GMR2; 60 memcpy(cmd, &define_cmd, sizeof(define_cmd)); 61 cmd += sizeof(define_cmd) / sizeof(*cmd); 62 63 /* 64 * Need to split the command if there are too many 65 * pages that goes into the gmr. 66 */ 67 68 remap_cmd.gmrId = gmr_id; 69 remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ? 70 SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32; 71 72 while (num_pages > 0) { 73 unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP); 74 75 remap_cmd.offsetPages = remap_pos; 76 remap_cmd.numPages = nr; 77 78 *cmd++ = SVGA_CMD_REMAP_GMR2; 79 memcpy(cmd, &remap_cmd, sizeof(remap_cmd)); 80 cmd += sizeof(remap_cmd) / sizeof(*cmd); 81 82 for (i = 0; i < nr; ++i) { 83 if (VMW_PPN_SIZE <= 4) 84 *cmd = page_to_pfn(*pages++); 85 else 86 *((uint64_t *)cmd) = page_to_pfn(*pages++); 87 88 cmd += VMW_PPN_SIZE / sizeof(*cmd); 89 } 90 91 num_pages -= nr; 92 remap_pos += nr; 93 } 94 95 BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd)); 96 97 vmw_fifo_commit(dev_priv, cmd_size); 98 99 return 0; 100 } 101 102 static void vmw_gmr2_unbind(struct vmw_private *dev_priv, 103 int gmr_id) 104 { 105 SVGAFifoCmdDefineGMR2 define_cmd; 106 uint32_t define_size = sizeof(define_cmd) + 4; 107 uint32_t *cmd; 108 109 cmd = vmw_fifo_reserve(dev_priv, define_size); 110 if (unlikely(cmd == NULL)) { 111 DRM_ERROR("GMR2 unbind failed.\n"); 112 return; 113 } 114 define_cmd.gmrId = gmr_id; 115 define_cmd.numPages = 0; 116 117 *cmd++ = SVGA_CMD_DEFINE_GMR2; 118 memcpy(cmd, &define_cmd, sizeof(define_cmd)); 119 120 vmw_fifo_commit(dev_priv, define_size); 121 } 122 123 /** 124 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize 125 * the number of used descriptors. 126 */ 127 128 static int vmw_gmr_build_descriptors(struct list_head *desc_pages, 129 struct page *pages[], 130 unsigned long num_pages) 131 { 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; 137 unsigned long pfn; 138 int ret; 139 140 desc_per_page = PAGE_SIZE / 141 sizeof(struct svga_guest_mem_descriptor) - 1; 142 143 while (likely(num_pages != 0)) { 144 page = alloc_page(__GFP_HIGHMEM); 145 if (unlikely(page == NULL)) { 146 ret = -ENOMEM; 147 goto out_err; 148 } 149 150 list_add_tail(&page->lru, desc_pages); 151 152 /* 153 * Point previous page terminating descriptor to this 154 * page before unmapping it. 155 */ 156 157 if (likely(page_virtual != NULL)) { 158 desc_virtual->ppn = page_to_pfn(page); 159 kunmap_atomic(page_virtual); 160 } 161 162 page_virtual = kmap_atomic(page); 163 desc_virtual = page_virtual - 1; 164 prev_pfn = ~(0UL); 165 166 while (likely(num_pages != 0)) { 167 pfn = page_to_pfn(*pages); 168 169 if (pfn != prev_pfn + 1) { 170 171 if (desc_virtual - page_virtual == 172 desc_per_page - 1) 173 break; 174 175 (++desc_virtual)->ppn = cpu_to_le32(pfn); 176 desc_virtual->num_pages = cpu_to_le32(1); 177 } else { 178 uint32_t tmp = 179 le32_to_cpu(desc_virtual->num_pages); 180 desc_virtual->num_pages = cpu_to_le32(tmp + 1); 181 } 182 prev_pfn = pfn; 183 --num_pages; 184 ++pages; 185 } 186 187 (++desc_virtual)->ppn = cpu_to_le32(0); 188 desc_virtual->num_pages = cpu_to_le32(0); 189 } 190 191 if (likely(page_virtual != NULL)) 192 kunmap_atomic(page_virtual); 193 194 return 0; 195 out_err: 196 list_for_each_entry_safe(page, next, desc_pages, lru) { 197 list_del_init(&page->lru); 198 __free_page(page); 199 } 200 return ret; 201 } 202 203 static inline void vmw_gmr_free_descriptors(struct list_head *desc_pages) 204 { 205 struct page *page, *next; 206 207 list_for_each_entry_safe(page, next, desc_pages, lru) { 208 list_del_init(&page->lru); 209 __free_page(page); 210 } 211 } 212 213 static void vmw_gmr_fire_descriptors(struct vmw_private *dev_priv, 214 int gmr_id, struct list_head *desc_pages) 215 { 216 struct page *page; 217 218 if (unlikely(list_empty(desc_pages))) 219 return; 220 221 page = list_entry(desc_pages->next, struct page, lru); 222 223 mutex_lock(&dev_priv->hw_mutex); 224 225 vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id); 226 wmb(); 227 vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, page_to_pfn(page)); 228 mb(); 229 230 mutex_unlock(&dev_priv->hw_mutex); 231 232 } 233 234 /** 235 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize 236 * the number of used descriptors. 237 */ 238 239 static unsigned long vmw_gmr_count_descriptors(struct page *pages[], 240 unsigned long num_pages) 241 { 242 unsigned long prev_pfn = ~(0UL); 243 unsigned long pfn; 244 unsigned long descriptors = 0; 245 246 while (num_pages--) { 247 pfn = page_to_pfn(*pages++); 248 if (prev_pfn + 1 != pfn) 249 ++descriptors; 250 prev_pfn = pfn; 251 } 252 253 return descriptors; 254 } 255 256 int vmw_gmr_bind(struct vmw_private *dev_priv, 257 struct page *pages[], 258 unsigned long num_pages, 259 int gmr_id) 260 { 261 struct list_head desc_pages; 262 int ret; 263 264 if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) 265 return vmw_gmr2_bind(dev_priv, pages, num_pages, gmr_id); 266 267 if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR))) 268 return -EINVAL; 269 270 if (vmw_gmr_count_descriptors(pages, num_pages) > 271 dev_priv->max_gmr_descriptors) 272 return -EINVAL; 273 274 INIT_LIST_HEAD(&desc_pages); 275 276 ret = vmw_gmr_build_descriptors(&desc_pages, pages, num_pages); 277 if (unlikely(ret != 0)) 278 return ret; 279 280 vmw_gmr_fire_descriptors(dev_priv, gmr_id, &desc_pages); 281 vmw_gmr_free_descriptors(&desc_pages); 282 283 return 0; 284 } 285 286 287 void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id) 288 { 289 if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) { 290 vmw_gmr2_unbind(dev_priv, gmr_id); 291 return; 292 } 293 294 mutex_lock(&dev_priv->hw_mutex); 295 vmw_write(dev_priv, SVGA_REG_GMR_ID, gmr_id); 296 wmb(); 297 vmw_write(dev_priv, SVGA_REG_GMR_DESCRIPTOR, 0); 298 mb(); 299 mutex_unlock(&dev_priv->hw_mutex); 300 } 301