Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 3 | * Copyright © 2009-2011 VMware, Inc., Palo Alto, CA., USA |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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" |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/ttm/ttm_bo_driver.h> |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 31 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 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) |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 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; |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 43 | uint32_t *cmd; |
| 44 | uint32_t *cmd_orig; |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 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; |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 50 | uint32_t i; |
| 51 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 52 | cmd_orig = cmd = vmw_fifo_reserve(dev_priv, cmd_size); |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 53 | if (unlikely(cmd == NULL)) |
| 54 | return -ENOMEM; |
| 55 | |
| 56 | define_cmd.gmrId = gmr_id; |
| 57 | define_cmd.numPages = num_pages; |
| 58 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 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 | |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 68 | remap_cmd.gmrId = gmr_id; |
| 69 | remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ? |
| 70 | SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32; |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 71 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 72 | while (num_pages > 0) { |
| 73 | unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP); |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 74 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 75 | remap_cmd.offsetPages = remap_pos; |
| 76 | remap_cmd.numPages = nr; |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 77 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 78 | *cmd++ = SVGA_CMD_REMAP_GMR2; |
| 79 | memcpy(cmd, &remap_cmd, sizeof(remap_cmd)); |
| 80 | cmd += sizeof(remap_cmd) / sizeof(*cmd); |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 81 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 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; |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Jakob Bornecrantz | 6e4dcff | 2013-08-29 02:32:53 +0200 | [diff] [blame^] | 95 | BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd)); |
| 96 | |
| 97 | vmw_fifo_commit(dev_priv, cmd_size); |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 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 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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); |
Cong Wang | 1c9c20f | 2011-11-25 23:14:20 +0800 | [diff] [blame] | 159 | kunmap_atomic(page_virtual); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Cong Wang | 1c9c20f | 2011-11-25 23:14:20 +0800 | [diff] [blame] | 162 | page_virtual = kmap_atomic(page); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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)) |
Cong Wang | 1c9c20f | 2011-11-25 23:14:20 +0800 | [diff] [blame] | 192 | kunmap_atomic(page_virtual); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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[], |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 240 | unsigned long num_pages) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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, |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 257 | struct page *pages[], |
| 258 | unsigned long num_pages, |
| 259 | int gmr_id) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 260 | { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 261 | struct list_head desc_pages; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 262 | int ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 263 | |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 264 | if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) |
| 265 | return vmw_gmr2_bind(dev_priv, pages, num_pages, gmr_id); |
| 266 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 267 | if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR))) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 270 | if (vmw_gmr_count_descriptors(pages, num_pages) > |
| 271 | dev_priv->max_gmr_descriptors) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 272 | return -EINVAL; |
| 273 | |
| 274 | INIT_LIST_HEAD(&desc_pages); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 275 | |
| 276 | ret = vmw_gmr_build_descriptors(&desc_pages, pages, num_pages); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 277 | if (unlikely(ret != 0)) |
| 278 | return ret; |
| 279 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 280 | vmw_gmr_fire_descriptors(dev_priv, gmr_id, &desc_pages); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 281 | vmw_gmr_free_descriptors(&desc_pages); |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 282 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 283 | return 0; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame] | 286 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 287 | void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id) |
| 288 | { |
Thomas Hellstrom | 2de59d0 | 2011-08-31 09:42:55 +0200 | [diff] [blame] | 289 | if (likely(dev_priv->capabilities & SVGA_CAP_GMR2)) { |
| 290 | vmw_gmr2_unbind(dev_priv, gmr_id); |
| 291 | return; |
| 292 | } |
| 293 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 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 | } |