Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright © 2012 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 | |
| 30 | /* |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 31 | * If we set up the screen target otable, screen objects stop working. |
| 32 | */ |
| 33 | |
| 34 | #define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE) ? 0 : 1) |
| 35 | |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 36 | #ifdef CONFIG_64BIT |
| 37 | #define VMW_PPN_SIZE 8 |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 38 | #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0 |
| 39 | #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1 |
| 40 | #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2 |
| 41 | #else |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 42 | #define VMW_PPN_SIZE 4 |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 43 | #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0 |
| 44 | #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1 |
| 45 | #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2 |
| 46 | #endif |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * struct vmw_mob - Structure containing page table and metadata for a |
| 50 | * Guest Memory OBject. |
| 51 | * |
| 52 | * @num_pages Number of pages that make up the page table. |
| 53 | * @pt_level The indirection level of the page table. 0-2. |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 54 | * @pt_root_page DMA address of the level 0 page of the page table. |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 55 | */ |
| 56 | struct vmw_mob { |
| 57 | struct ttm_buffer_object *pt_bo; |
| 58 | unsigned long num_pages; |
| 59 | unsigned pt_level; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 60 | dma_addr_t pt_root_page; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 61 | uint32_t id; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * struct vmw_otable - Guest Memory OBject table metadata |
| 66 | * |
| 67 | * @size: Size of the table (page-aligned). |
| 68 | * @page_table: Pointer to a struct vmw_mob holding the page table. |
| 69 | */ |
| 70 | struct vmw_otable { |
| 71 | unsigned long size; |
| 72 | struct vmw_mob *page_table; |
| 73 | }; |
| 74 | |
| 75 | static int vmw_mob_pt_populate(struct vmw_private *dev_priv, |
| 76 | struct vmw_mob *mob); |
| 77 | static void vmw_mob_pt_setup(struct vmw_mob *mob, |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 78 | struct vmw_piter data_iter, |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 79 | unsigned long num_data_pages); |
| 80 | |
| 81 | /* |
| 82 | * vmw_setup_otable_base - Issue an object table base setup command to |
| 83 | * the device |
| 84 | * |
| 85 | * @dev_priv: Pointer to a device private structure |
| 86 | * @type: Type of object table base |
| 87 | * @offset Start of table offset into dev_priv::otable_bo |
| 88 | * @otable Pointer to otable metadata; |
| 89 | * |
| 90 | * This function returns -ENOMEM if it fails to reserve fifo space, |
| 91 | * and may block waiting for fifo space. |
| 92 | */ |
| 93 | static int vmw_setup_otable_base(struct vmw_private *dev_priv, |
| 94 | SVGAOTableType type, |
| 95 | unsigned long offset, |
| 96 | struct vmw_otable *otable) |
| 97 | { |
| 98 | struct { |
| 99 | SVGA3dCmdHeader header; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 100 | SVGA3dCmdSetOTableBase64 body; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 101 | } *cmd; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 102 | struct vmw_mob *mob; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 103 | const struct vmw_sg_table *vsgt; |
| 104 | struct vmw_piter iter; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 105 | int ret; |
| 106 | |
| 107 | BUG_ON(otable->page_table != NULL); |
| 108 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 109 | vsgt = vmw_bo_sg_table(dev_priv->otable_bo); |
| 110 | vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT); |
| 111 | WARN_ON(!vmw_piter_next(&iter)); |
| 112 | |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 113 | mob = vmw_mob_create(otable->size >> PAGE_SHIFT); |
| 114 | if (unlikely(mob == NULL)) { |
| 115 | DRM_ERROR("Failed creating OTable page table.\n"); |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | if (otable->size <= PAGE_SIZE) { |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 120 | mob->pt_level = VMW_MOBFMT_PTDEPTH_0; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 121 | mob->pt_root_page = vmw_piter_dma_addr(&iter); |
| 122 | } else if (vsgt->num_regions == 1) { |
| 123 | mob->pt_level = SVGA3D_MOBFMT_RANGE; |
| 124 | mob->pt_root_page = vmw_piter_dma_addr(&iter); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 125 | } else { |
| 126 | ret = vmw_mob_pt_populate(dev_priv, mob); |
| 127 | if (unlikely(ret != 0)) |
| 128 | goto out_no_populate; |
| 129 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 130 | vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT); |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 131 | mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); |
| 135 | if (unlikely(cmd == NULL)) { |
| 136 | DRM_ERROR("Failed reserving FIFO space for OTable setup.\n"); |
Dave Jones | cd9a21a | 2014-01-30 21:27:25 -0500 | [diff] [blame] | 137 | ret = -ENOMEM; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 138 | goto out_no_fifo; |
| 139 | } |
| 140 | |
| 141 | memset(cmd, 0, sizeof(*cmd)); |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 142 | cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 143 | cmd->header.size = sizeof(cmd->body); |
| 144 | cmd->body.type = type; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 145 | cmd->body.baseAddress = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 146 | cmd->body.sizeInBytes = otable->size; |
| 147 | cmd->body.validSizeInBytes = 0; |
| 148 | cmd->body.ptDepth = mob->pt_level; |
| 149 | |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 150 | /* |
| 151 | * The device doesn't support this, But the otable size is |
| 152 | * determined at compile-time, so this BUG shouldn't trigger |
| 153 | * randomly. |
| 154 | */ |
| 155 | BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2); |
| 156 | |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 157 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); |
| 158 | otable->page_table = mob; |
| 159 | |
| 160 | return 0; |
| 161 | |
| 162 | out_no_fifo: |
| 163 | out_no_populate: |
| 164 | vmw_mob_destroy(mob); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * vmw_takedown_otable_base - Issue an object table base takedown command |
| 170 | * to the device |
| 171 | * |
| 172 | * @dev_priv: Pointer to a device private structure |
| 173 | * @type: Type of object table base |
| 174 | * |
| 175 | */ |
| 176 | static void vmw_takedown_otable_base(struct vmw_private *dev_priv, |
| 177 | SVGAOTableType type, |
| 178 | struct vmw_otable *otable) |
| 179 | { |
| 180 | struct { |
| 181 | SVGA3dCmdHeader header; |
| 182 | SVGA3dCmdSetOTableBase body; |
| 183 | } *cmd; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 184 | struct ttm_buffer_object *bo; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 185 | |
| 186 | if (otable->page_table == NULL) |
| 187 | return; |
| 188 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 189 | bo = otable->page_table->pt_bo; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 190 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); |
| 191 | if (unlikely(cmd == NULL)) |
| 192 | DRM_ERROR("Failed reserving FIFO space for OTable setup.\n"); |
| 193 | |
| 194 | memset(cmd, 0, sizeof(*cmd)); |
| 195 | cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE; |
| 196 | cmd->header.size = sizeof(cmd->body); |
| 197 | cmd->body.type = type; |
| 198 | cmd->body.baseAddress = 0; |
| 199 | cmd->body.sizeInBytes = 0; |
| 200 | cmd->body.validSizeInBytes = 0; |
| 201 | cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID; |
| 202 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); |
| 203 | |
| 204 | if (bo) { |
| 205 | int ret; |
| 206 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 207 | ret = ttm_bo_reserve(bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 208 | BUG_ON(ret != 0); |
| 209 | |
| 210 | vmw_fence_single_bo(bo, NULL); |
| 211 | ttm_bo_unreserve(bo); |
| 212 | } |
| 213 | |
| 214 | vmw_mob_destroy(otable->page_table); |
| 215 | otable->page_table = NULL; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * vmw_otables_setup - Set up guest backed memory object tables |
| 220 | * |
| 221 | * @dev_priv: Pointer to a device private structure |
| 222 | * |
| 223 | * Takes care of the device guest backed surface |
| 224 | * initialization, by setting up the guest backed memory object tables. |
| 225 | * Returns 0 on success and various error codes on failure. A succesful return |
| 226 | * means the object tables can be taken down using the vmw_otables_takedown |
| 227 | * function. |
| 228 | */ |
| 229 | int vmw_otables_setup(struct vmw_private *dev_priv) |
| 230 | { |
| 231 | unsigned long offset; |
| 232 | unsigned long bo_size; |
| 233 | struct vmw_otable *otables; |
| 234 | SVGAOTableType i; |
| 235 | int ret; |
| 236 | |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 237 | otables = kzalloc(SVGA_OTABLE_DX9_MAX * sizeof(*otables), |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 238 | GFP_KERNEL); |
| 239 | if (unlikely(otables == NULL)) { |
| 240 | DRM_ERROR("Failed to allocate space for otable " |
| 241 | "metadata.\n"); |
| 242 | return -ENOMEM; |
| 243 | } |
| 244 | |
| 245 | otables[SVGA_OTABLE_MOB].size = |
| 246 | VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE; |
| 247 | otables[SVGA_OTABLE_SURFACE].size = |
| 248 | VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE; |
| 249 | otables[SVGA_OTABLE_CONTEXT].size = |
| 250 | VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE; |
| 251 | otables[SVGA_OTABLE_SHADER].size = |
| 252 | VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE; |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 253 | otables[SVGA_OTABLE_SCREEN_TARGET].size = |
| 254 | VMWGFX_NUM_GB_SCREEN_TARGET * |
| 255 | SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 256 | |
| 257 | bo_size = 0; |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 258 | for (i = 0; i < SVGA_OTABLE_DX9_MAX; ++i) { |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 259 | otables[i].size = |
| 260 | (otables[i].size + PAGE_SIZE - 1) & PAGE_MASK; |
| 261 | bo_size += otables[i].size; |
| 262 | } |
| 263 | |
| 264 | ret = ttm_bo_create(&dev_priv->bdev, bo_size, |
| 265 | ttm_bo_type_device, |
| 266 | &vmw_sys_ne_placement, |
| 267 | 0, false, NULL, |
| 268 | &dev_priv->otable_bo); |
| 269 | |
| 270 | if (unlikely(ret != 0)) |
| 271 | goto out_no_bo; |
| 272 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 273 | ret = ttm_bo_reserve(dev_priv->otable_bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 274 | BUG_ON(ret != 0); |
| 275 | ret = vmw_bo_driver.ttm_tt_populate(dev_priv->otable_bo->ttm); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 276 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 277 | goto out_unreserve; |
| 278 | ret = vmw_bo_map_dma(dev_priv->otable_bo); |
| 279 | if (unlikely(ret != 0)) |
| 280 | goto out_unreserve; |
| 281 | |
| 282 | ttm_bo_unreserve(dev_priv->otable_bo); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 283 | |
| 284 | offset = 0; |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 285 | for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i) { |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 286 | ret = vmw_setup_otable_base(dev_priv, i, offset, |
| 287 | &otables[i]); |
| 288 | if (unlikely(ret != 0)) |
| 289 | goto out_no_setup; |
| 290 | offset += otables[i].size; |
| 291 | } |
| 292 | |
| 293 | dev_priv->otables = otables; |
| 294 | return 0; |
| 295 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 296 | out_unreserve: |
| 297 | ttm_bo_unreserve(dev_priv->otable_bo); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 298 | out_no_setup: |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 299 | for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i) |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 300 | vmw_takedown_otable_base(dev_priv, i, &otables[i]); |
| 301 | |
| 302 | ttm_bo_unref(&dev_priv->otable_bo); |
| 303 | out_no_bo: |
| 304 | kfree(otables); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* |
| 310 | * vmw_otables_takedown - Take down guest backed memory object tables |
| 311 | * |
| 312 | * @dev_priv: Pointer to a device private structure |
| 313 | * |
| 314 | * Take down the Guest Memory Object tables. |
| 315 | */ |
| 316 | void vmw_otables_takedown(struct vmw_private *dev_priv) |
| 317 | { |
| 318 | SVGAOTableType i; |
| 319 | struct ttm_buffer_object *bo = dev_priv->otable_bo; |
| 320 | int ret; |
| 321 | |
Thomas Hellstrom | 7cba906 | 2014-01-09 11:03:18 +0100 | [diff] [blame] | 322 | for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i) |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 323 | vmw_takedown_otable_base(dev_priv, i, |
| 324 | &dev_priv->otables[i]); |
| 325 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 326 | ret = ttm_bo_reserve(bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 327 | BUG_ON(ret != 0); |
| 328 | |
| 329 | vmw_fence_single_bo(bo, NULL); |
| 330 | ttm_bo_unreserve(bo); |
| 331 | |
| 332 | ttm_bo_unref(&dev_priv->otable_bo); |
| 333 | kfree(dev_priv->otables); |
| 334 | dev_priv->otables = NULL; |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /* |
| 339 | * vmw_mob_calculate_pt_pages - Calculate the number of page table pages |
| 340 | * needed for a guest backed memory object. |
| 341 | * |
| 342 | * @data_pages: Number of data pages in the memory object buffer. |
| 343 | */ |
| 344 | static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages) |
| 345 | { |
| 346 | unsigned long data_size = data_pages * PAGE_SIZE; |
| 347 | unsigned long tot_size = 0; |
| 348 | |
| 349 | while (likely(data_size > PAGE_SIZE)) { |
| 350 | data_size = DIV_ROUND_UP(data_size, PAGE_SIZE); |
| 351 | data_size *= VMW_PPN_SIZE; |
| 352 | tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK; |
| 353 | } |
| 354 | |
| 355 | return tot_size >> PAGE_SHIFT; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * vmw_mob_create - Create a mob, but don't populate it. |
| 360 | * |
| 361 | * @data_pages: Number of data pages of the underlying buffer object. |
| 362 | */ |
| 363 | struct vmw_mob *vmw_mob_create(unsigned long data_pages) |
| 364 | { |
| 365 | struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL); |
| 366 | |
| 367 | if (unlikely(mob == NULL)) |
| 368 | return NULL; |
| 369 | |
| 370 | mob->num_pages = vmw_mob_calculate_pt_pages(data_pages); |
| 371 | |
| 372 | return mob; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * vmw_mob_pt_populate - Populate the mob pagetable |
| 377 | * |
| 378 | * @mob: Pointer to the mob the pagetable of which we want to |
| 379 | * populate. |
| 380 | * |
| 381 | * This function allocates memory to be used for the pagetable, and |
| 382 | * adjusts TTM memory accounting accordingly. Returns ENOMEM if |
| 383 | * memory resources aren't sufficient and may cause TTM buffer objects |
| 384 | * to be swapped out by using the TTM memory accounting function. |
| 385 | */ |
| 386 | static int vmw_mob_pt_populate(struct vmw_private *dev_priv, |
| 387 | struct vmw_mob *mob) |
| 388 | { |
| 389 | int ret; |
| 390 | BUG_ON(mob->pt_bo != NULL); |
| 391 | |
| 392 | ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE, |
| 393 | ttm_bo_type_device, |
| 394 | &vmw_sys_ne_placement, |
| 395 | 0, false, NULL, &mob->pt_bo); |
| 396 | if (unlikely(ret != 0)) |
| 397 | return ret; |
| 398 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 399 | ret = ttm_bo_reserve(mob->pt_bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 400 | |
| 401 | BUG_ON(ret != 0); |
| 402 | ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 403 | if (unlikely(ret != 0)) |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 404 | goto out_unreserve; |
| 405 | ret = vmw_bo_map_dma(mob->pt_bo); |
| 406 | if (unlikely(ret != 0)) |
| 407 | goto out_unreserve; |
| 408 | |
| 409 | ttm_bo_unreserve(mob->pt_bo); |
| 410 | |
| 411 | return 0; |
| 412 | |
| 413 | out_unreserve: |
| 414 | ttm_bo_unreserve(mob->pt_bo); |
| 415 | ttm_bo_unref(&mob->pt_bo); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 420 | /** |
| 421 | * vmw_mob_assign_ppn - Assign a value to a page table entry |
| 422 | * |
| 423 | * @addr: Pointer to pointer to page table entry. |
| 424 | * @val: The page table entry |
| 425 | * |
| 426 | * Assigns a value to a page table entry pointed to by *@addr and increments |
| 427 | * *@addr according to the page table entry size. |
| 428 | */ |
| 429 | #if (VMW_PPN_SIZE == 8) |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 430 | static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val) |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 431 | { |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 432 | *((__le64 *) *addr) = cpu_to_le64(val >> PAGE_SHIFT); |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 433 | *addr += 2; |
| 434 | } |
| 435 | #else |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 436 | static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val) |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 437 | { |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 438 | *(*addr)++ = cpu_to_le32(val >> PAGE_SHIFT); |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 439 | } |
| 440 | #endif |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * vmw_mob_build_pt - Build a pagetable |
| 444 | * |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 445 | * @data_addr: Array of DMA addresses to the underlying buffer |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 446 | * object's data pages. |
| 447 | * @num_data_pages: Number of buffer object data pages. |
| 448 | * @pt_pages: Array of page pointers to the page table pages. |
| 449 | * |
| 450 | * Returns the number of page table pages actually used. |
| 451 | * Uses atomic kmaps of highmem pages to avoid TLB thrashing. |
| 452 | */ |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 453 | static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter, |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 454 | unsigned long num_data_pages, |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 455 | struct vmw_piter *pt_iter) |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 456 | { |
| 457 | unsigned long pt_size = num_data_pages * VMW_PPN_SIZE; |
| 458 | unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE); |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 459 | unsigned long pt_page; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 460 | __le32 *addr, *save_addr; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 461 | unsigned long i; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 462 | struct page *page; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 463 | |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 464 | for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) { |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 465 | page = vmw_piter_page(pt_iter); |
| 466 | |
| 467 | save_addr = addr = kmap_atomic(page); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 468 | |
| 469 | for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) { |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 470 | vmw_mob_assign_ppn(&addr, |
| 471 | vmw_piter_dma_addr(data_iter)); |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 472 | if (unlikely(--num_data_pages == 0)) |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 473 | break; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 474 | WARN_ON(!vmw_piter_next(data_iter)); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 475 | } |
| 476 | kunmap_atomic(save_addr); |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 477 | vmw_piter_next(pt_iter); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return num_pt_pages; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * vmw_mob_build_pt - Set up a multilevel mob pagetable |
| 485 | * |
| 486 | * @mob: Pointer to a mob whose page table needs setting up. |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 487 | * @data_addr Array of DMA addresses to the buffer object's data |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 488 | * pages. |
| 489 | * @num_data_pages: Number of buffer object data pages. |
| 490 | * |
| 491 | * Uses tail recursion to set up a multilevel mob page table. |
| 492 | */ |
| 493 | static void vmw_mob_pt_setup(struct vmw_mob *mob, |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 494 | struct vmw_piter data_iter, |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 495 | unsigned long num_data_pages) |
| 496 | { |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 497 | unsigned long num_pt_pages = 0; |
| 498 | struct ttm_buffer_object *bo = mob->pt_bo; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 499 | struct vmw_piter save_pt_iter; |
| 500 | struct vmw_piter pt_iter; |
| 501 | const struct vmw_sg_table *vsgt; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 502 | int ret; |
| 503 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 504 | ret = ttm_bo_reserve(bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 505 | BUG_ON(ret != 0); |
| 506 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 507 | vsgt = vmw_bo_sg_table(bo); |
| 508 | vmw_piter_start(&pt_iter, vsgt, 0); |
| 509 | BUG_ON(!vmw_piter_next(&pt_iter)); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 510 | mob->pt_level = 0; |
| 511 | while (likely(num_data_pages > 1)) { |
| 512 | ++mob->pt_level; |
| 513 | BUG_ON(mob->pt_level > 2); |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 514 | save_pt_iter = pt_iter; |
| 515 | num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages, |
| 516 | &pt_iter); |
| 517 | data_iter = save_pt_iter; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 518 | num_data_pages = num_pt_pages; |
| 519 | } |
| 520 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 521 | mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 522 | ttm_bo_unreserve(bo); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary. |
| 527 | * |
| 528 | * @mob: Pointer to a mob to destroy. |
| 529 | */ |
| 530 | void vmw_mob_destroy(struct vmw_mob *mob) |
| 531 | { |
| 532 | if (mob->pt_bo) |
| 533 | ttm_bo_unref(&mob->pt_bo); |
| 534 | kfree(mob); |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * vmw_mob_unbind - Hide a mob from the device. |
| 539 | * |
| 540 | * @dev_priv: Pointer to a device private. |
| 541 | * @mob_id: Device id of the mob to unbind. |
| 542 | */ |
| 543 | void vmw_mob_unbind(struct vmw_private *dev_priv, |
| 544 | struct vmw_mob *mob) |
| 545 | { |
| 546 | struct { |
| 547 | SVGA3dCmdHeader header; |
| 548 | SVGA3dCmdDestroyGBMob body; |
| 549 | } *cmd; |
| 550 | int ret; |
| 551 | struct ttm_buffer_object *bo = mob->pt_bo; |
| 552 | |
| 553 | if (bo) { |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 554 | ret = ttm_bo_reserve(bo, false, true, false, NULL); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 555 | /* |
| 556 | * Noone else should be using this buffer. |
| 557 | */ |
| 558 | BUG_ON(ret != 0); |
| 559 | } |
| 560 | |
| 561 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); |
| 562 | if (unlikely(cmd == NULL)) { |
| 563 | DRM_ERROR("Failed reserving FIFO space for Memory " |
| 564 | "Object unbinding.\n"); |
| 565 | } |
| 566 | cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB; |
| 567 | cmd->header.size = sizeof(cmd->body); |
| 568 | cmd->body.mobid = mob->id; |
| 569 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); |
| 570 | if (bo) { |
| 571 | vmw_fence_single_bo(bo, NULL); |
| 572 | ttm_bo_unreserve(bo); |
| 573 | } |
| 574 | vmw_3d_resource_dec(dev_priv, false); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * vmw_mob_bind - Make a mob visible to the device after first |
| 579 | * populating it if necessary. |
| 580 | * |
| 581 | * @dev_priv: Pointer to a device private. |
| 582 | * @mob: Pointer to the mob we're making visible. |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 583 | * @data_addr: Array of DMA addresses to the data pages of the underlying |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 584 | * buffer object. |
| 585 | * @num_data_pages: Number of data pages of the underlying buffer |
| 586 | * object. |
| 587 | * @mob_id: Device id of the mob to bind |
| 588 | * |
| 589 | * This function is intended to be interfaced with the ttm_tt backend |
| 590 | * code. |
| 591 | */ |
| 592 | int vmw_mob_bind(struct vmw_private *dev_priv, |
| 593 | struct vmw_mob *mob, |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 594 | const struct vmw_sg_table *vsgt, |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 595 | unsigned long num_data_pages, |
| 596 | int32_t mob_id) |
| 597 | { |
| 598 | int ret; |
| 599 | bool pt_set_up = false; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 600 | struct vmw_piter data_iter; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 601 | struct { |
| 602 | SVGA3dCmdHeader header; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 603 | SVGA3dCmdDefineGBMob64 body; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 604 | } *cmd; |
| 605 | |
| 606 | mob->id = mob_id; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 607 | vmw_piter_start(&data_iter, vsgt, 0); |
| 608 | if (unlikely(!vmw_piter_next(&data_iter))) |
| 609 | return 0; |
| 610 | |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 611 | if (likely(num_data_pages == 1)) { |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 612 | mob->pt_level = VMW_MOBFMT_PTDEPTH_0; |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 613 | mob->pt_root_page = vmw_piter_dma_addr(&data_iter); |
| 614 | } else if (vsgt->num_regions == 1) { |
| 615 | mob->pt_level = SVGA3D_MOBFMT_RANGE; |
| 616 | mob->pt_root_page = vmw_piter_dma_addr(&data_iter); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 617 | } else if (unlikely(mob->pt_bo == NULL)) { |
| 618 | ret = vmw_mob_pt_populate(dev_priv, mob); |
| 619 | if (unlikely(ret != 0)) |
| 620 | return ret; |
| 621 | |
Thomas Hellstrom | 0fd53cf | 2013-10-24 13:27:38 -0700 | [diff] [blame] | 622 | vmw_mob_pt_setup(mob, data_iter, num_data_pages); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 623 | pt_set_up = true; |
Thomas Hellstrom | f2a0dcb | 2014-01-15 10:04:07 +0100 | [diff] [blame] | 624 | mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | (void) vmw_3d_resource_inc(dev_priv, false); |
| 628 | |
| 629 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd)); |
| 630 | if (unlikely(cmd == NULL)) { |
| 631 | DRM_ERROR("Failed reserving FIFO space for Memory " |
| 632 | "Object binding.\n"); |
| 633 | goto out_no_cmd_space; |
| 634 | } |
| 635 | |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 636 | cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64; |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 637 | cmd->header.size = sizeof(cmd->body); |
| 638 | cmd->body.mobid = mob_id; |
| 639 | cmd->body.ptDepth = mob->pt_level; |
Thomas Hellstrom | 3e894a6 | 2014-01-20 11:33:04 +0100 | [diff] [blame] | 640 | cmd->body.base = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT); |
Thomas Hellstrom | 3530bdc | 2012-11-21 10:49:52 +0100 | [diff] [blame] | 641 | cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE; |
| 642 | |
| 643 | vmw_fifo_commit(dev_priv, sizeof(*cmd)); |
| 644 | |
| 645 | return 0; |
| 646 | |
| 647 | out_no_cmd_space: |
| 648 | vmw_3d_resource_dec(dev_priv, false); |
| 649 | if (pt_set_up) |
| 650 | ttm_bo_unref(&mob->pt_bo); |
| 651 | |
| 652 | return -ENOMEM; |
| 653 | } |