Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright © 2009 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 "ttm/ttm_bo_driver.h" |
| 30 | #include "ttm/ttm_placement.h" |
| 31 | |
| 32 | static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM | |
| 33 | TTM_PL_FLAG_CACHED; |
| 34 | |
| 35 | static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM | |
| 36 | TTM_PL_FLAG_CACHED | |
| 37 | TTM_PL_FLAG_NO_EVICT; |
| 38 | |
| 39 | static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM | |
| 40 | TTM_PL_FLAG_CACHED; |
| 41 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 42 | static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR | |
| 43 | TTM_PL_FLAG_CACHED; |
| 44 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 45 | struct ttm_placement vmw_vram_placement = { |
| 46 | .fpfn = 0, |
| 47 | .lpfn = 0, |
| 48 | .num_placement = 1, |
| 49 | .placement = &vram_placement_flags, |
| 50 | .num_busy_placement = 1, |
| 51 | .busy_placement = &vram_placement_flags |
| 52 | }; |
| 53 | |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 54 | static uint32_t vram_gmr_placement_flags[] = { |
| 55 | TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED, |
| 56 | VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED |
| 57 | }; |
| 58 | |
| 59 | struct ttm_placement vmw_vram_gmr_placement = { |
| 60 | .fpfn = 0, |
| 61 | .lpfn = 0, |
| 62 | .num_placement = 2, |
| 63 | .placement = vram_gmr_placement_flags, |
| 64 | .num_busy_placement = 1, |
| 65 | .busy_placement = &gmr_placement_flags |
| 66 | }; |
| 67 | |
Thomas Hellstrom | 8ba5152 | 2010-01-16 16:05:05 +0100 | [diff] [blame] | 68 | struct ttm_placement vmw_vram_sys_placement = { |
| 69 | .fpfn = 0, |
| 70 | .lpfn = 0, |
| 71 | .num_placement = 1, |
| 72 | .placement = &vram_placement_flags, |
| 73 | .num_busy_placement = 1, |
| 74 | .busy_placement = &sys_placement_flags |
| 75 | }; |
| 76 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 77 | struct ttm_placement vmw_vram_ne_placement = { |
| 78 | .fpfn = 0, |
| 79 | .lpfn = 0, |
| 80 | .num_placement = 1, |
| 81 | .placement = &vram_ne_placement_flags, |
| 82 | .num_busy_placement = 1, |
| 83 | .busy_placement = &vram_ne_placement_flags |
| 84 | }; |
| 85 | |
| 86 | struct ttm_placement vmw_sys_placement = { |
| 87 | .fpfn = 0, |
| 88 | .lpfn = 0, |
| 89 | .num_placement = 1, |
| 90 | .placement = &sys_placement_flags, |
| 91 | .num_busy_placement = 1, |
| 92 | .busy_placement = &sys_placement_flags |
| 93 | }; |
| 94 | |
| 95 | struct vmw_ttm_backend { |
| 96 | struct ttm_backend backend; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 97 | struct page **pages; |
| 98 | unsigned long num_pages; |
| 99 | struct vmw_private *dev_priv; |
| 100 | int gmr_id; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static int vmw_ttm_populate(struct ttm_backend *backend, |
| 104 | unsigned long num_pages, struct page **pages, |
| 105 | struct page *dummy_read_page) |
| 106 | { |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 107 | struct vmw_ttm_backend *vmw_be = |
| 108 | container_of(backend, struct vmw_ttm_backend, backend); |
| 109 | |
| 110 | vmw_be->pages = pages; |
| 111 | vmw_be->num_pages = num_pages; |
| 112 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int vmw_ttm_bind(struct ttm_backend *backend, struct ttm_mem_reg *bo_mem) |
| 117 | { |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 118 | struct vmw_ttm_backend *vmw_be = |
| 119 | container_of(backend, struct vmw_ttm_backend, backend); |
| 120 | |
| 121 | vmw_be->gmr_id = bo_mem->start; |
| 122 | |
| 123 | return vmw_gmr_bind(vmw_be->dev_priv, vmw_be->pages, |
| 124 | vmw_be->num_pages, vmw_be->gmr_id); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static int vmw_ttm_unbind(struct ttm_backend *backend) |
| 128 | { |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 129 | struct vmw_ttm_backend *vmw_be = |
| 130 | container_of(backend, struct vmw_ttm_backend, backend); |
| 131 | |
| 132 | vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static void vmw_ttm_clear(struct ttm_backend *backend) |
| 137 | { |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 138 | struct vmw_ttm_backend *vmw_be = |
| 139 | container_of(backend, struct vmw_ttm_backend, backend); |
| 140 | |
| 141 | vmw_be->pages = NULL; |
| 142 | vmw_be->num_pages = 0; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void vmw_ttm_destroy(struct ttm_backend *backend) |
| 146 | { |
| 147 | struct vmw_ttm_backend *vmw_be = |
| 148 | container_of(backend, struct vmw_ttm_backend, backend); |
| 149 | |
| 150 | kfree(vmw_be); |
| 151 | } |
| 152 | |
| 153 | static struct ttm_backend_func vmw_ttm_func = { |
| 154 | .populate = vmw_ttm_populate, |
| 155 | .clear = vmw_ttm_clear, |
| 156 | .bind = vmw_ttm_bind, |
| 157 | .unbind = vmw_ttm_unbind, |
| 158 | .destroy = vmw_ttm_destroy, |
| 159 | }; |
| 160 | |
| 161 | struct ttm_backend *vmw_ttm_backend_init(struct ttm_bo_device *bdev) |
| 162 | { |
| 163 | struct vmw_ttm_backend *vmw_be; |
| 164 | |
| 165 | vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL); |
| 166 | if (!vmw_be) |
| 167 | return NULL; |
| 168 | |
| 169 | vmw_be->backend.func = &vmw_ttm_func; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 170 | vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 171 | |
| 172 | return &vmw_be->backend; |
| 173 | } |
| 174 | |
| 175 | int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) |
| 176 | { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, |
| 181 | struct ttm_mem_type_manager *man) |
| 182 | { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 183 | switch (type) { |
| 184 | case TTM_PL_SYSTEM: |
| 185 | /* System memory */ |
| 186 | |
| 187 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 188 | man->available_caching = TTM_PL_FLAG_CACHED; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 189 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 190 | break; |
| 191 | case TTM_PL_VRAM: |
| 192 | /* "On-card" video ram */ |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 193 | man->func = &ttm_bo_manager_func; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 194 | man->gpu_offset = 0; |
Jerome Glisse | 96bf8b8 | 2010-04-09 14:39:26 +0200 | [diff] [blame] | 195 | man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE; |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 196 | man->available_caching = TTM_PL_FLAG_CACHED; |
| 197 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 198 | break; |
| 199 | case VMW_PL_GMR: |
| 200 | /* |
| 201 | * "Guest Memory Regions" is an aperture like feature with |
| 202 | * one slot per bo. There is an upper limit of the number of |
| 203 | * slots as well as the bo size. |
| 204 | */ |
| 205 | man->func = &vmw_gmrid_manager_func; |
| 206 | man->gpu_offset = 0; |
| 207 | man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE; |
| 208 | man->available_caching = TTM_PL_FLAG_CACHED; |
| 209 | man->default_caching = TTM_PL_FLAG_CACHED; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 210 | break; |
| 211 | default: |
| 212 | DRM_ERROR("Unsupported memory type %u\n", (unsigned)type); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | void vmw_evict_flags(struct ttm_buffer_object *bo, |
| 219 | struct ttm_placement *placement) |
| 220 | { |
| 221 | *placement = vmw_sys_placement; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * FIXME: Proper access checks on buffers. |
| 226 | */ |
| 227 | |
| 228 | static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp) |
| 229 | { |
| 230 | return 0; |
| 231 | } |
| 232 | |
Jerome Glisse | 96bf8b8 | 2010-04-09 14:39:26 +0200 | [diff] [blame] | 233 | static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 234 | { |
| 235 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 236 | struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev); |
| 237 | |
| 238 | mem->bus.addr = NULL; |
| 239 | mem->bus.is_iomem = false; |
| 240 | mem->bus.offset = 0; |
| 241 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 242 | mem->bus.base = 0; |
| 243 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 244 | return -EINVAL; |
| 245 | switch (mem->mem_type) { |
| 246 | case TTM_PL_SYSTEM: |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 247 | case VMW_PL_GMR: |
Jerome Glisse | 96bf8b8 | 2010-04-09 14:39:26 +0200 | [diff] [blame] | 248 | return 0; |
| 249 | case TTM_PL_VRAM: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 250 | mem->bus.offset = mem->start << PAGE_SHIFT; |
Jerome Glisse | 96bf8b8 | 2010-04-09 14:39:26 +0200 | [diff] [blame] | 251 | mem->bus.base = dev_priv->vram_start; |
| 252 | mem->bus.is_iomem = true; |
| 253 | break; |
| 254 | default: |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 261 | { |
| 262 | } |
| 263 | |
| 264 | static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo) |
| 265 | { |
| 266 | return 0; |
| 267 | } |
| 268 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 269 | /** |
| 270 | * FIXME: We're using the old vmware polling method to sync. |
| 271 | * Do this with fences instead. |
| 272 | */ |
| 273 | |
| 274 | static void *vmw_sync_obj_ref(void *sync_obj) |
| 275 | { |
| 276 | return sync_obj; |
| 277 | } |
| 278 | |
| 279 | static void vmw_sync_obj_unref(void **sync_obj) |
| 280 | { |
| 281 | *sync_obj = NULL; |
| 282 | } |
| 283 | |
| 284 | static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg) |
| 285 | { |
| 286 | struct vmw_private *dev_priv = (struct vmw_private *)sync_arg; |
| 287 | |
| 288 | mutex_lock(&dev_priv->hw_mutex); |
| 289 | vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC); |
| 290 | mutex_unlock(&dev_priv->hw_mutex); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg) |
| 295 | { |
| 296 | struct vmw_private *dev_priv = (struct vmw_private *)sync_arg; |
| 297 | uint32_t sequence = (unsigned long) sync_obj; |
| 298 | |
| 299 | return vmw_fence_signaled(dev_priv, sequence); |
| 300 | } |
| 301 | |
| 302 | static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg, |
| 303 | bool lazy, bool interruptible) |
| 304 | { |
| 305 | struct vmw_private *dev_priv = (struct vmw_private *)sync_arg; |
| 306 | uint32_t sequence = (unsigned long) sync_obj; |
| 307 | |
| 308 | return vmw_wait_fence(dev_priv, false, sequence, false, 3*HZ); |
| 309 | } |
| 310 | |
| 311 | struct ttm_bo_driver vmw_bo_driver = { |
| 312 | .create_ttm_backend_entry = vmw_ttm_backend_init, |
| 313 | .invalidate_caches = vmw_invalidate_caches, |
| 314 | .init_mem_type = vmw_init_mem_type, |
| 315 | .evict_flags = vmw_evict_flags, |
| 316 | .move = NULL, |
| 317 | .verify_access = vmw_verify_access, |
| 318 | .sync_obj_signaled = vmw_sync_obj_signaled, |
| 319 | .sync_obj_wait = vmw_sync_obj_wait, |
| 320 | .sync_obj_flush = vmw_sync_obj_flush, |
| 321 | .sync_obj_unref = vmw_sync_obj_unref, |
Thomas Hellstrom | effe110 | 2010-01-13 22:28:39 +0100 | [diff] [blame] | 322 | .sync_obj_ref = vmw_sync_obj_ref, |
Thomas Hellstrom | 135cba0 | 2010-10-26 21:21:47 +0200 | [diff] [blame^] | 323 | .move_notify = NULL, |
| 324 | .swap_notify = NULL, |
Jerome Glisse | 96bf8b8 | 2010-04-09 14:39:26 +0200 | [diff] [blame] | 325 | .fault_reserve_notify = &vmw_ttm_fault_reserve_notify, |
| 326 | .io_mem_reserve = &vmw_ttm_io_mem_reserve, |
| 327 | .io_mem_free = &vmw_ttm_io_mem_free, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 328 | }; |