Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Authors: Dave Airlie |
| 23 | * Alon Levy |
| 24 | */ |
| 25 | |
| 26 | #include "qxl_drv.h" |
| 27 | #include "qxl_object.h" |
| 28 | |
| 29 | #include <linux/io-mapping.h> |
| 30 | static void qxl_ttm_bo_destroy(struct ttm_buffer_object *tbo) |
| 31 | { |
| 32 | struct qxl_bo *bo; |
| 33 | struct qxl_device *qdev; |
| 34 | |
| 35 | bo = container_of(tbo, struct qxl_bo, tbo); |
| 36 | qdev = (struct qxl_device *)bo->gem_base.dev->dev_private; |
| 37 | |
| 38 | qxl_surface_evict(qdev, bo, false); |
| 39 | qxl_fence_fini(&bo->fence); |
| 40 | mutex_lock(&qdev->gem.mutex); |
| 41 | list_del_init(&bo->list); |
| 42 | mutex_unlock(&qdev->gem.mutex); |
| 43 | drm_gem_object_release(&bo->gem_base); |
| 44 | kfree(bo); |
| 45 | } |
| 46 | |
| 47 | bool qxl_ttm_bo_is_qxl_bo(struct ttm_buffer_object *bo) |
| 48 | { |
| 49 | if (bo->destroy == &qxl_ttm_bo_destroy) |
| 50 | return true; |
| 51 | return false; |
| 52 | } |
| 53 | |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 54 | void qxl_ttm_placement_from_domain(struct qxl_bo *qbo, u32 domain, bool pinned) |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 55 | { |
| 56 | u32 c = 0; |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 57 | u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 58 | |
| 59 | qbo->placement.fpfn = 0; |
| 60 | qbo->placement.lpfn = 0; |
| 61 | qbo->placement.placement = qbo->placements; |
| 62 | qbo->placement.busy_placement = qbo->placements; |
Dave Airlie | 62c8ba7 | 2013-04-16 13:36:00 +1000 | [diff] [blame] | 63 | if (domain == QXL_GEM_DOMAIN_VRAM) |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 64 | qbo->placements[c++] = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_VRAM | pflag; |
Dave Airlie | 62c8ba7 | 2013-04-16 13:36:00 +1000 | [diff] [blame] | 65 | if (domain == QXL_GEM_DOMAIN_SURFACE) |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 66 | qbo->placements[c++] = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_PRIV0 | pflag; |
Dave Airlie | 62c8ba7 | 2013-04-16 13:36:00 +1000 | [diff] [blame] | 67 | if (domain == QXL_GEM_DOMAIN_CPU) |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 68 | qbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM | pflag; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 69 | if (!c) |
| 70 | qbo->placements[c++] = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; |
| 71 | qbo->placement.num_placement = c; |
| 72 | qbo->placement.num_busy_placement = c; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | int qxl_bo_create(struct qxl_device *qdev, |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 77 | unsigned long size, bool kernel, bool pinned, u32 domain, |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 78 | struct qxl_surface *surf, |
| 79 | struct qxl_bo **bo_ptr) |
| 80 | { |
| 81 | struct qxl_bo *bo; |
| 82 | enum ttm_bo_type type; |
| 83 | int r; |
| 84 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 85 | if (kernel) |
| 86 | type = ttm_bo_type_kernel; |
| 87 | else |
| 88 | type = ttm_bo_type_device; |
| 89 | *bo_ptr = NULL; |
| 90 | bo = kzalloc(sizeof(struct qxl_bo), GFP_KERNEL); |
| 91 | if (bo == NULL) |
| 92 | return -ENOMEM; |
| 93 | size = roundup(size, PAGE_SIZE); |
| 94 | r = drm_gem_object_init(qdev->ddev, &bo->gem_base, size); |
| 95 | if (unlikely(r)) { |
| 96 | kfree(bo); |
| 97 | return r; |
| 98 | } |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 99 | bo->type = domain; |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 100 | bo->pin_count = pinned ? 1 : 0; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 101 | bo->surface_id = 0; |
| 102 | qxl_fence_init(qdev, &bo->fence); |
| 103 | INIT_LIST_HEAD(&bo->list); |
Dave Airlie | 8002db6 | 2013-07-23 14:16:42 +1000 | [diff] [blame] | 104 | |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 105 | if (surf) |
| 106 | bo->surf = *surf; |
| 107 | |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 108 | qxl_ttm_placement_from_domain(bo, domain, pinned); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 109 | |
| 110 | r = ttm_bo_init(&qdev->mman.bdev, &bo->tbo, size, type, |
| 111 | &bo->placement, 0, !kernel, NULL, size, |
| 112 | NULL, &qxl_ttm_bo_destroy); |
| 113 | if (unlikely(r != 0)) { |
| 114 | if (r != -ERESTARTSYS) |
| 115 | dev_err(qdev->dev, |
| 116 | "object_init failed for (%lu, 0x%08X)\n", |
| 117 | size, domain); |
| 118 | return r; |
| 119 | } |
| 120 | *bo_ptr = bo; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int qxl_bo_kmap(struct qxl_bo *bo, void **ptr) |
| 125 | { |
| 126 | bool is_iomem; |
| 127 | int r; |
| 128 | |
| 129 | if (bo->kptr) { |
| 130 | if (ptr) |
| 131 | *ptr = bo->kptr; |
| 132 | return 0; |
| 133 | } |
| 134 | r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); |
| 135 | if (r) |
| 136 | return r; |
| 137 | bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); |
| 138 | if (ptr) |
| 139 | *ptr = bo->kptr; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev, |
| 144 | struct qxl_bo *bo, int page_offset) |
| 145 | { |
| 146 | struct ttm_mem_type_manager *man = &bo->tbo.bdev->man[bo->tbo.mem.mem_type]; |
| 147 | void *rptr; |
| 148 | int ret; |
| 149 | struct io_mapping *map; |
| 150 | |
| 151 | if (bo->tbo.mem.mem_type == TTM_PL_VRAM) |
| 152 | map = qdev->vram_mapping; |
| 153 | else if (bo->tbo.mem.mem_type == TTM_PL_PRIV0) |
| 154 | map = qdev->surface_mapping; |
| 155 | else |
| 156 | goto fallback; |
| 157 | |
| 158 | (void) ttm_mem_io_lock(man, false); |
| 159 | ret = ttm_mem_io_reserve(bo->tbo.bdev, &bo->tbo.mem); |
| 160 | ttm_mem_io_unlock(man); |
| 161 | |
| 162 | return io_mapping_map_atomic_wc(map, bo->tbo.mem.bus.offset + page_offset); |
| 163 | fallback: |
| 164 | if (bo->kptr) { |
| 165 | rptr = bo->kptr + (page_offset * PAGE_SIZE); |
| 166 | return rptr; |
| 167 | } |
| 168 | |
| 169 | ret = qxl_bo_kmap(bo, &rptr); |
| 170 | if (ret) |
| 171 | return NULL; |
| 172 | |
| 173 | rptr += page_offset * PAGE_SIZE; |
| 174 | return rptr; |
| 175 | } |
| 176 | |
| 177 | void qxl_bo_kunmap(struct qxl_bo *bo) |
| 178 | { |
| 179 | if (bo->kptr == NULL) |
| 180 | return; |
| 181 | bo->kptr = NULL; |
| 182 | ttm_bo_kunmap(&bo->kmap); |
| 183 | } |
| 184 | |
| 185 | void qxl_bo_kunmap_atomic_page(struct qxl_device *qdev, |
| 186 | struct qxl_bo *bo, void *pmap) |
| 187 | { |
| 188 | struct ttm_mem_type_manager *man = &bo->tbo.bdev->man[bo->tbo.mem.mem_type]; |
| 189 | struct io_mapping *map; |
| 190 | |
| 191 | if (bo->tbo.mem.mem_type == TTM_PL_VRAM) |
| 192 | map = qdev->vram_mapping; |
| 193 | else if (bo->tbo.mem.mem_type == TTM_PL_PRIV0) |
| 194 | map = qdev->surface_mapping; |
| 195 | else |
| 196 | goto fallback; |
| 197 | |
| 198 | io_mapping_unmap_atomic(pmap); |
| 199 | |
| 200 | (void) ttm_mem_io_lock(man, false); |
| 201 | ttm_mem_io_free(bo->tbo.bdev, &bo->tbo.mem); |
| 202 | ttm_mem_io_unlock(man); |
| 203 | return ; |
| 204 | fallback: |
| 205 | qxl_bo_kunmap(bo); |
| 206 | } |
| 207 | |
| 208 | void qxl_bo_unref(struct qxl_bo **bo) |
| 209 | { |
| 210 | struct ttm_buffer_object *tbo; |
| 211 | |
| 212 | if ((*bo) == NULL) |
| 213 | return; |
| 214 | tbo = &((*bo)->tbo); |
| 215 | ttm_bo_unref(&tbo); |
| 216 | if (tbo == NULL) |
| 217 | *bo = NULL; |
| 218 | } |
| 219 | |
| 220 | struct qxl_bo *qxl_bo_ref(struct qxl_bo *bo) |
| 221 | { |
| 222 | ttm_bo_reference(&bo->tbo); |
| 223 | return bo; |
| 224 | } |
| 225 | |
| 226 | int qxl_bo_pin(struct qxl_bo *bo, u32 domain, u64 *gpu_addr) |
| 227 | { |
| 228 | struct qxl_device *qdev = (struct qxl_device *)bo->gem_base.dev->dev_private; |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 229 | int r; |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 230 | |
| 231 | if (bo->pin_count) { |
| 232 | bo->pin_count++; |
| 233 | if (gpu_addr) |
| 234 | *gpu_addr = qxl_bo_gpu_offset(bo); |
| 235 | return 0; |
| 236 | } |
Dave Airlie | 4f49ec9 | 2013-07-23 14:06:07 +1000 | [diff] [blame] | 237 | qxl_ttm_placement_from_domain(bo, domain, true); |
Dave Airlie | f64122c | 2013-02-25 14:47:55 +1000 | [diff] [blame] | 238 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
| 239 | if (likely(r == 0)) { |
| 240 | bo->pin_count = 1; |
| 241 | if (gpu_addr != NULL) |
| 242 | *gpu_addr = qxl_bo_gpu_offset(bo); |
| 243 | } |
| 244 | if (unlikely(r != 0)) |
| 245 | dev_err(qdev->dev, "%p pin failed\n", bo); |
| 246 | return r; |
| 247 | } |
| 248 | |
| 249 | int qxl_bo_unpin(struct qxl_bo *bo) |
| 250 | { |
| 251 | struct qxl_device *qdev = (struct qxl_device *)bo->gem_base.dev->dev_private; |
| 252 | int r, i; |
| 253 | |
| 254 | if (!bo->pin_count) { |
| 255 | dev_warn(qdev->dev, "%p unpin not necessary\n", bo); |
| 256 | return 0; |
| 257 | } |
| 258 | bo->pin_count--; |
| 259 | if (bo->pin_count) |
| 260 | return 0; |
| 261 | for (i = 0; i < bo->placement.num_placement; i++) |
| 262 | bo->placements[i] &= ~TTM_PL_FLAG_NO_EVICT; |
| 263 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
| 264 | if (unlikely(r != 0)) |
| 265 | dev_err(qdev->dev, "%p validate failed for unpin\n", bo); |
| 266 | return r; |
| 267 | } |
| 268 | |
| 269 | void qxl_bo_force_delete(struct qxl_device *qdev) |
| 270 | { |
| 271 | struct qxl_bo *bo, *n; |
| 272 | |
| 273 | if (list_empty(&qdev->gem.objects)) |
| 274 | return; |
| 275 | dev_err(qdev->dev, "Userspace still has active objects !\n"); |
| 276 | list_for_each_entry_safe(bo, n, &qdev->gem.objects, list) { |
| 277 | mutex_lock(&qdev->ddev->struct_mutex); |
| 278 | dev_err(qdev->dev, "%p %p %lu %lu force free\n", |
| 279 | &bo->gem_base, bo, (unsigned long)bo->gem_base.size, |
| 280 | *((unsigned long *)&bo->gem_base.refcount)); |
| 281 | mutex_lock(&qdev->gem.mutex); |
| 282 | list_del_init(&bo->list); |
| 283 | mutex_unlock(&qdev->gem.mutex); |
| 284 | /* this should unref the ttm bo */ |
| 285 | drm_gem_object_unreference(&bo->gem_base); |
| 286 | mutex_unlock(&qdev->ddev->struct_mutex); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | int qxl_bo_init(struct qxl_device *qdev) |
| 291 | { |
| 292 | return qxl_ttm_init(qdev); |
| 293 | } |
| 294 | |
| 295 | void qxl_bo_fini(struct qxl_device *qdev) |
| 296 | { |
| 297 | qxl_ttm_fini(qdev); |
| 298 | } |
| 299 | |
| 300 | int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo) |
| 301 | { |
| 302 | int ret; |
| 303 | if (bo->type == QXL_GEM_DOMAIN_SURFACE && bo->surface_id == 0) { |
| 304 | /* allocate a surface id for this surface now */ |
| 305 | ret = qxl_surface_id_alloc(qdev, bo); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
| 309 | ret = qxl_hw_surface_alloc(qdev, bo, NULL); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
Dave Airlie | b86487a | 2013-07-04 14:59:34 +1000 | [diff] [blame] | 316 | int qxl_surf_evict(struct qxl_device *qdev) |
| 317 | { |
| 318 | return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_PRIV0); |
| 319 | } |
Dave Airlie | d84300b | 2013-07-04 15:02:33 +1000 | [diff] [blame] | 320 | |
| 321 | int qxl_vram_evict(struct qxl_device *qdev) |
| 322 | { |
| 323 | return ttm_bo_evict_mm(&qdev->mman.bdev, TTM_PL_VRAM); |
| 324 | } |