Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2007-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 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 29 | */ |
| 30 | |
| 31 | #include "ttm/ttm_bo_driver.h" |
| 32 | #include "ttm/ttm_placement.h" |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/highmem.h> |
| 35 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 37 | #include <linux/vmalloc.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | |
| 40 | void ttm_bo_free_old_node(struct ttm_buffer_object *bo) |
| 41 | { |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 42 | ttm_bo_mem_put(bo, &bo->mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int ttm_bo_move_ttm(struct ttm_buffer_object *bo, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 46 | bool evict, bool no_wait_reserve, |
| 47 | bool no_wait_gpu, struct ttm_mem_reg *new_mem) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 48 | { |
| 49 | struct ttm_tt *ttm = bo->ttm; |
| 50 | struct ttm_mem_reg *old_mem = &bo->mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 51 | int ret; |
| 52 | |
| 53 | if (old_mem->mem_type != TTM_PL_SYSTEM) { |
| 54 | ttm_tt_unbind(ttm); |
| 55 | ttm_bo_free_old_node(bo); |
| 56 | ttm_flag_masked(&old_mem->placement, TTM_PL_FLAG_SYSTEM, |
| 57 | TTM_PL_MASK_MEM); |
| 58 | old_mem->mem_type = TTM_PL_SYSTEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ret = ttm_tt_set_placement_caching(ttm, new_mem->placement); |
| 62 | if (unlikely(ret != 0)) |
| 63 | return ret; |
| 64 | |
| 65 | if (new_mem->mem_type != TTM_PL_SYSTEM) { |
| 66 | ret = ttm_tt_bind(ttm, new_mem); |
| 67 | if (unlikely(ret != 0)) |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | *old_mem = *new_mem; |
| 72 | new_mem->mm_node = NULL; |
Austin Yuan | 110b20c | 2010-01-21 13:45:40 +0800 | [diff] [blame] | 73 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | EXPORT_SYMBOL(ttm_bo_move_ttm); |
| 77 | |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 78 | int ttm_mem_io_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 79 | { |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 80 | int ret; |
| 81 | |
Jerome Glisse | 0c321c7 | 2010-04-07 10:21:27 +0000 | [diff] [blame] | 82 | if (!mem->bus.io_reserved) { |
| 83 | mem->bus.io_reserved = true; |
| 84 | ret = bdev->driver->io_mem_reserve(bdev, mem); |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 85 | if (unlikely(ret != 0)) |
| 86 | return ret; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void ttm_mem_io_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 92 | { |
| 93 | if (bdev->driver->io_mem_reserve) { |
| 94 | if (mem->bus.io_reserved) { |
| 95 | mem->bus.io_reserved = false; |
| 96 | bdev->driver->io_mem_free(bdev, mem); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 101 | int ttm_mem_reg_ioremap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem, |
| 102 | void **virtual) |
| 103 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 104 | int ret; |
| 105 | void *addr; |
| 106 | |
| 107 | *virtual = NULL; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 108 | ret = ttm_mem_io_reserve(bdev, mem); |
Jerome Glisse | 9e51159c | 2010-05-05 11:02:44 +0200 | [diff] [blame] | 109 | if (ret || !mem->bus.is_iomem) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 110 | return ret; |
| 111 | |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 112 | if (mem->bus.addr) { |
| 113 | addr = mem->bus.addr; |
| 114 | } else { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 115 | if (mem->placement & TTM_PL_FLAG_WC) |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 116 | addr = ioremap_wc(mem->bus.base + mem->bus.offset, mem->bus.size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 117 | else |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 118 | addr = ioremap_nocache(mem->bus.base + mem->bus.offset, mem->bus.size); |
| 119 | if (!addr) { |
| 120 | ttm_mem_io_free(bdev, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 121 | return -ENOMEM; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 122 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 123 | } |
| 124 | *virtual = addr; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void ttm_mem_reg_iounmap(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem, |
| 129 | void *virtual) |
| 130 | { |
| 131 | struct ttm_mem_type_manager *man; |
| 132 | |
| 133 | man = &bdev->man[mem->mem_type]; |
| 134 | |
Jerome Glisse | 0c321c7 | 2010-04-07 10:21:27 +0000 | [diff] [blame] | 135 | if (virtual && mem->bus.addr == NULL) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 136 | iounmap(virtual); |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 137 | ttm_mem_io_free(bdev, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int ttm_copy_io_page(void *dst, void *src, unsigned long page) |
| 141 | { |
| 142 | uint32_t *dstP = |
| 143 | (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT)); |
| 144 | uint32_t *srcP = |
| 145 | (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT)); |
| 146 | |
| 147 | int i; |
| 148 | for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i) |
| 149 | iowrite32(ioread32(srcP++), dstP++); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src, |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 154 | unsigned long page, |
| 155 | pgprot_t prot) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 156 | { |
| 157 | struct page *d = ttm_tt_get_page(ttm, page); |
| 158 | void *dst; |
| 159 | |
| 160 | if (!d) |
| 161 | return -ENOMEM; |
| 162 | |
| 163 | src = (void *)((unsigned long)src + (page << PAGE_SHIFT)); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 164 | |
| 165 | #ifdef CONFIG_X86 |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 166 | dst = kmap_atomic_prot(d, prot); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 167 | #else |
Thomas Hellstrom | 6d0897b | 2009-07-31 10:47:51 +0200 | [diff] [blame] | 168 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 169 | dst = vmap(&d, 1, 0, prot); |
| 170 | else |
| 171 | dst = kmap(d); |
| 172 | #endif |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 173 | if (!dst) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | memcpy_fromio(dst, src, PAGE_SIZE); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 177 | |
| 178 | #ifdef CONFIG_X86 |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 179 | kunmap_atomic(dst); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 180 | #else |
Thomas Hellstrom | 6d0897b | 2009-07-31 10:47:51 +0200 | [diff] [blame] | 181 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 182 | vunmap(dst); |
| 183 | else |
| 184 | kunmap(d); |
| 185 | #endif |
| 186 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst, |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 191 | unsigned long page, |
| 192 | pgprot_t prot) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 193 | { |
| 194 | struct page *s = ttm_tt_get_page(ttm, page); |
| 195 | void *src; |
| 196 | |
| 197 | if (!s) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT)); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 201 | #ifdef CONFIG_X86 |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 202 | src = kmap_atomic_prot(s, prot); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 203 | #else |
Thomas Hellstrom | 6d0897b | 2009-07-31 10:47:51 +0200 | [diff] [blame] | 204 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 205 | src = vmap(&s, 1, 0, prot); |
| 206 | else |
| 207 | src = kmap(s); |
| 208 | #endif |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 209 | if (!src) |
| 210 | return -ENOMEM; |
| 211 | |
| 212 | memcpy_toio(dst, src, PAGE_SIZE); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 213 | |
| 214 | #ifdef CONFIG_X86 |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 215 | kunmap_atomic(src); |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 216 | #else |
Thomas Hellstrom | 6d0897b | 2009-07-31 10:47:51 +0200 | [diff] [blame] | 217 | if (pgprot_val(prot) != pgprot_val(PAGE_KERNEL)) |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 218 | vunmap(src); |
| 219 | else |
| 220 | kunmap(s); |
| 221 | #endif |
| 222 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int ttm_bo_move_memcpy(struct ttm_buffer_object *bo, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 227 | bool evict, bool no_wait_reserve, bool no_wait_gpu, |
| 228 | struct ttm_mem_reg *new_mem) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 229 | { |
| 230 | struct ttm_bo_device *bdev = bo->bdev; |
| 231 | struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type]; |
| 232 | struct ttm_tt *ttm = bo->ttm; |
| 233 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 234 | struct ttm_mem_reg old_copy = *old_mem; |
| 235 | void *old_iomap; |
| 236 | void *new_iomap; |
| 237 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 238 | unsigned long i; |
| 239 | unsigned long page; |
| 240 | unsigned long add = 0; |
| 241 | int dir; |
| 242 | |
| 243 | ret = ttm_mem_reg_ioremap(bdev, old_mem, &old_iomap); |
| 244 | if (ret) |
| 245 | return ret; |
| 246 | ret = ttm_mem_reg_ioremap(bdev, new_mem, &new_iomap); |
| 247 | if (ret) |
| 248 | goto out; |
| 249 | |
| 250 | if (old_iomap == NULL && new_iomap == NULL) |
| 251 | goto out2; |
| 252 | if (old_iomap == NULL && ttm == NULL) |
| 253 | goto out2; |
| 254 | |
| 255 | add = 0; |
| 256 | dir = 1; |
| 257 | |
| 258 | if ((old_mem->mem_type == new_mem->mem_type) && |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 259 | (new_mem->start < old_mem->start + old_mem->size)) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 260 | dir = -1; |
| 261 | add = new_mem->num_pages - 1; |
| 262 | } |
| 263 | |
| 264 | for (i = 0; i < new_mem->num_pages; ++i) { |
| 265 | page = i * dir + add; |
Thomas Hellstrom | 542c6f6 | 2009-07-24 09:57:34 +0200 | [diff] [blame] | 266 | if (old_iomap == NULL) { |
| 267 | pgprot_t prot = ttm_io_prot(old_mem->placement, |
| 268 | PAGE_KERNEL); |
| 269 | ret = ttm_copy_ttm_io_page(ttm, new_iomap, page, |
| 270 | prot); |
| 271 | } else if (new_iomap == NULL) { |
| 272 | pgprot_t prot = ttm_io_prot(new_mem->placement, |
| 273 | PAGE_KERNEL); |
| 274 | ret = ttm_copy_io_ttm_page(ttm, old_iomap, page, |
| 275 | prot); |
| 276 | } else |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 277 | ret = ttm_copy_io_page(new_iomap, old_iomap, page); |
| 278 | if (ret) |
| 279 | goto out1; |
| 280 | } |
| 281 | mb(); |
| 282 | out2: |
| 283 | ttm_bo_free_old_node(bo); |
| 284 | |
| 285 | *old_mem = *new_mem; |
| 286 | new_mem->mm_node = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 287 | |
| 288 | if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && (ttm != NULL)) { |
| 289 | ttm_tt_unbind(ttm); |
| 290 | ttm_tt_destroy(ttm); |
| 291 | bo->ttm = NULL; |
| 292 | } |
| 293 | |
| 294 | out1: |
| 295 | ttm_mem_reg_iounmap(bdev, new_mem, new_iomap); |
| 296 | out: |
| 297 | ttm_mem_reg_iounmap(bdev, &old_copy, old_iomap); |
| 298 | return ret; |
| 299 | } |
| 300 | EXPORT_SYMBOL(ttm_bo_move_memcpy); |
| 301 | |
| 302 | static void ttm_transfered_destroy(struct ttm_buffer_object *bo) |
| 303 | { |
| 304 | kfree(bo); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * ttm_buffer_object_transfer |
| 309 | * |
| 310 | * @bo: A pointer to a struct ttm_buffer_object. |
| 311 | * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object, |
| 312 | * holding the data of @bo with the old placement. |
| 313 | * |
| 314 | * This is a utility function that may be called after an accelerated move |
| 315 | * has been scheduled. A new buffer object is created as a placeholder for |
| 316 | * the old data while it's being copied. When that buffer object is idle, |
| 317 | * it can be destroyed, releasing the space of the old placement. |
| 318 | * Returns: |
| 319 | * !0: Failure. |
| 320 | */ |
| 321 | |
| 322 | static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo, |
| 323 | struct ttm_buffer_object **new_obj) |
| 324 | { |
| 325 | struct ttm_buffer_object *fbo; |
| 326 | struct ttm_bo_device *bdev = bo->bdev; |
| 327 | struct ttm_bo_driver *driver = bdev->driver; |
| 328 | |
| 329 | fbo = kzalloc(sizeof(*fbo), GFP_KERNEL); |
| 330 | if (!fbo) |
| 331 | return -ENOMEM; |
| 332 | |
| 333 | *fbo = *bo; |
| 334 | |
| 335 | /** |
| 336 | * Fix up members that we shouldn't copy directly: |
| 337 | * TODO: Explicit member copy would probably be better here. |
| 338 | */ |
| 339 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 340 | init_waitqueue_head(&fbo->event_queue); |
| 341 | INIT_LIST_HEAD(&fbo->ddestroy); |
| 342 | INIT_LIST_HEAD(&fbo->lru); |
| 343 | INIT_LIST_HEAD(&fbo->swap); |
| 344 | fbo->vm_node = NULL; |
Francisco Jerez | 0fbecd4 | 2010-09-21 02:15:15 +0200 | [diff] [blame] | 345 | atomic_set(&fbo->cpu_writers, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 346 | |
| 347 | fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 348 | kref_init(&fbo->list_kref); |
| 349 | kref_init(&fbo->kref); |
| 350 | fbo->destroy = &ttm_transfered_destroy; |
| 351 | |
| 352 | *new_obj = fbo; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp) |
| 357 | { |
| 358 | #if defined(__i386__) || defined(__x86_64__) |
| 359 | if (caching_flags & TTM_PL_FLAG_WC) |
| 360 | tmp = pgprot_writecombine(tmp); |
| 361 | else if (boot_cpu_data.x86 > 3) |
| 362 | tmp = pgprot_noncached(tmp); |
| 363 | |
| 364 | #elif defined(__powerpc__) |
| 365 | if (!(caching_flags & TTM_PL_FLAG_CACHED)) { |
| 366 | pgprot_val(tmp) |= _PAGE_NO_CACHE; |
| 367 | if (caching_flags & TTM_PL_FLAG_UNCACHED) |
| 368 | pgprot_val(tmp) |= _PAGE_GUARDED; |
| 369 | } |
| 370 | #endif |
| 371 | #if defined(__ia64__) |
| 372 | if (caching_flags & TTM_PL_FLAG_WC) |
| 373 | tmp = pgprot_writecombine(tmp); |
| 374 | else |
| 375 | tmp = pgprot_noncached(tmp); |
| 376 | #endif |
| 377 | #if defined(__sparc__) |
| 378 | if (!(caching_flags & TTM_PL_FLAG_CACHED)) |
| 379 | tmp = pgprot_noncached(tmp); |
| 380 | #endif |
| 381 | return tmp; |
| 382 | } |
Thomas Hellstrom | 4bfd75c | 2009-12-06 21:46:27 +0100 | [diff] [blame] | 383 | EXPORT_SYMBOL(ttm_io_prot); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 384 | |
| 385 | static int ttm_bo_ioremap(struct ttm_buffer_object *bo, |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 386 | unsigned long offset, |
| 387 | unsigned long size, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 388 | struct ttm_bo_kmap_obj *map) |
| 389 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 390 | struct ttm_mem_reg *mem = &bo->mem; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 391 | |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 392 | if (bo->mem.bus.addr) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 393 | map->bo_kmap_type = ttm_bo_map_premapped; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 394 | map->virtual = (void *)(((u8 *)bo->mem.bus.addr) + offset); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 395 | } else { |
| 396 | map->bo_kmap_type = ttm_bo_map_iomap; |
| 397 | if (mem->placement & TTM_PL_FLAG_WC) |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 398 | map->virtual = ioremap_wc(bo->mem.bus.base + bo->mem.bus.offset + offset, |
| 399 | size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 400 | else |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 401 | map->virtual = ioremap_nocache(bo->mem.bus.base + bo->mem.bus.offset + offset, |
| 402 | size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 403 | } |
| 404 | return (!map->virtual) ? -ENOMEM : 0; |
| 405 | } |
| 406 | |
| 407 | static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, |
| 408 | unsigned long start_page, |
| 409 | unsigned long num_pages, |
| 410 | struct ttm_bo_kmap_obj *map) |
| 411 | { |
| 412 | struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot; |
| 413 | struct ttm_tt *ttm = bo->ttm; |
| 414 | struct page *d; |
| 415 | int i; |
| 416 | |
| 417 | BUG_ON(!ttm); |
| 418 | if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) { |
| 419 | /* |
| 420 | * We're mapping a single page, and the desired |
| 421 | * page protection is consistent with the bo. |
| 422 | */ |
| 423 | |
| 424 | map->bo_kmap_type = ttm_bo_map_kmap; |
| 425 | map->page = ttm_tt_get_page(ttm, start_page); |
| 426 | map->virtual = kmap(map->page); |
| 427 | } else { |
| 428 | /* |
| 429 | * Populate the part we're mapping; |
| 430 | */ |
| 431 | for (i = start_page; i < start_page + num_pages; ++i) { |
| 432 | d = ttm_tt_get_page(ttm, i); |
| 433 | if (!d) |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * We need to use vmap to get the desired page protection |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 439 | * or to make the buffer object look contiguous. |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 440 | */ |
| 441 | prot = (mem->placement & TTM_PL_FLAG_CACHED) ? |
| 442 | PAGE_KERNEL : |
| 443 | ttm_io_prot(mem->placement, PAGE_KERNEL); |
| 444 | map->bo_kmap_type = ttm_bo_map_vmap; |
| 445 | map->virtual = vmap(ttm->pages + start_page, num_pages, |
| 446 | 0, prot); |
| 447 | } |
| 448 | return (!map->virtual) ? -ENOMEM : 0; |
| 449 | } |
| 450 | |
| 451 | int ttm_bo_kmap(struct ttm_buffer_object *bo, |
| 452 | unsigned long start_page, unsigned long num_pages, |
| 453 | struct ttm_bo_kmap_obj *map) |
| 454 | { |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 455 | unsigned long offset, size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 456 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 457 | |
| 458 | BUG_ON(!list_empty(&bo->swap)); |
| 459 | map->virtual = NULL; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 460 | map->bo = bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 461 | if (num_pages > bo->num_pages) |
| 462 | return -EINVAL; |
| 463 | if (start_page > bo->num_pages) |
| 464 | return -EINVAL; |
| 465 | #if 0 |
| 466 | if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC)) |
| 467 | return -EPERM; |
| 468 | #endif |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 469 | ret = ttm_mem_io_reserve(bo->bdev, &bo->mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 470 | if (ret) |
| 471 | return ret; |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 472 | if (!bo->mem.bus.is_iomem) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 473 | return ttm_bo_kmap_ttm(bo, start_page, num_pages, map); |
| 474 | } else { |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 475 | offset = start_page << PAGE_SHIFT; |
| 476 | size = num_pages << PAGE_SHIFT; |
| 477 | return ttm_bo_ioremap(bo, offset, size, map); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | EXPORT_SYMBOL(ttm_bo_kmap); |
| 481 | |
| 482 | void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map) |
| 483 | { |
| 484 | if (!map->virtual) |
| 485 | return; |
| 486 | switch (map->bo_kmap_type) { |
| 487 | case ttm_bo_map_iomap: |
| 488 | iounmap(map->virtual); |
Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 489 | ttm_mem_io_free(map->bo->bdev, &map->bo->mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 490 | break; |
| 491 | case ttm_bo_map_vmap: |
| 492 | vunmap(map->virtual); |
| 493 | break; |
| 494 | case ttm_bo_map_kmap: |
| 495 | kunmap(map->page); |
| 496 | break; |
| 497 | case ttm_bo_map_premapped: |
| 498 | break; |
| 499 | default: |
| 500 | BUG(); |
| 501 | } |
| 502 | map->virtual = NULL; |
| 503 | map->page = NULL; |
| 504 | } |
| 505 | EXPORT_SYMBOL(ttm_bo_kunmap); |
| 506 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 507 | int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo, |
| 508 | void *sync_obj, |
| 509 | void *sync_obj_arg, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 510 | bool evict, bool no_wait_reserve, |
| 511 | bool no_wait_gpu, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 512 | struct ttm_mem_reg *new_mem) |
| 513 | { |
| 514 | struct ttm_bo_device *bdev = bo->bdev; |
| 515 | struct ttm_bo_driver *driver = bdev->driver; |
| 516 | struct ttm_mem_type_manager *man = &bdev->man[new_mem->mem_type]; |
| 517 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 518 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 519 | struct ttm_buffer_object *ghost_obj; |
| 520 | void *tmp_obj = NULL; |
| 521 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame^] | 522 | spin_lock(&bdev->fence_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 523 | if (bo->sync_obj) { |
| 524 | tmp_obj = bo->sync_obj; |
| 525 | bo->sync_obj = NULL; |
| 526 | } |
| 527 | bo->sync_obj = driver->sync_obj_ref(sync_obj); |
| 528 | bo->sync_obj_arg = sync_obj_arg; |
| 529 | if (evict) { |
| 530 | ret = ttm_bo_wait(bo, false, false, false); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame^] | 531 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 4677f15 | 2009-07-21 17:45:13 +0200 | [diff] [blame] | 532 | if (tmp_obj) |
| 533 | driver->sync_obj_unref(&tmp_obj); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 534 | if (ret) |
| 535 | return ret; |
| 536 | |
| 537 | ttm_bo_free_old_node(bo); |
| 538 | if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 539 | (bo->ttm != NULL)) { |
| 540 | ttm_tt_unbind(bo->ttm); |
| 541 | ttm_tt_destroy(bo->ttm); |
| 542 | bo->ttm = NULL; |
| 543 | } |
| 544 | } else { |
| 545 | /** |
| 546 | * This should help pipeline ordinary buffer moves. |
| 547 | * |
| 548 | * Hang old buffer memory on a new buffer object, |
| 549 | * and leave it to be released when the GPU |
| 550 | * operation has completed. |
| 551 | */ |
| 552 | |
| 553 | set_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame^] | 554 | spin_unlock(&bdev->fence_lock); |
Thomas Hellstrom | 4677f15 | 2009-07-21 17:45:13 +0200 | [diff] [blame] | 555 | if (tmp_obj) |
| 556 | driver->sync_obj_unref(&tmp_obj); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 557 | |
| 558 | ret = ttm_buffer_object_transfer(bo, &ghost_obj); |
| 559 | if (ret) |
| 560 | return ret; |
| 561 | |
| 562 | /** |
| 563 | * If we're not moving to fixed memory, the TTM object |
| 564 | * needs to stay alive. Otherwhise hang it on the ghost |
| 565 | * bo to be unbound and destroyed. |
| 566 | */ |
| 567 | |
| 568 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
| 569 | ghost_obj->ttm = NULL; |
| 570 | else |
| 571 | bo->ttm = NULL; |
| 572 | |
| 573 | ttm_bo_unreserve(ghost_obj); |
| 574 | ttm_bo_unref(&ghost_obj); |
| 575 | } |
| 576 | |
| 577 | *old_mem = *new_mem; |
| 578 | new_mem->mm_node = NULL; |
Austin Yuan | 110b20c | 2010-01-21 13:45:40 +0800 | [diff] [blame] | 579 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | EXPORT_SYMBOL(ttm_bo_move_accel_cleanup); |