Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Advanced Micro Devices, Inc. |
| 3 | * Copyright 2008 Red Hat Inc. |
| 4 | * Copyright 2009 Jerome Glisse. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | * OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: Dave Airlie |
| 25 | * Alex Deucher |
| 26 | * Jerome Glisse |
| 27 | */ |
| 28 | #include "drmP.h" |
| 29 | #include "radeon_drm.h" |
| 30 | #include "radeon.h" |
| 31 | #include "radeon_reg.h" |
| 32 | |
| 33 | /* |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 34 | * GART |
| 35 | * The GART (Graphics Aperture Remapping Table) is an aperture |
| 36 | * in the GPU's address space. System pages can be mapped into |
| 37 | * the aperture and look like contiguous pages from the GPU's |
| 38 | * perspective. A page table maps the pages in the aperture |
| 39 | * to the actual backing pages in system memory. |
| 40 | * |
| 41 | * Radeon GPUs support both an internal GART, as described above, |
| 42 | * and AGP. AGP works similarly, but the GART table is configured |
| 43 | * and maintained by the northbridge rather than the driver. |
| 44 | * Radeon hw has a separate AGP aperture that is programmed to |
| 45 | * point to the AGP aperture provided by the northbridge and the |
| 46 | * requests are passed through to the northbridge aperture. |
| 47 | * Both AGP and internal GART can be used at the same time, however |
| 48 | * that is not currently supported by the driver. |
| 49 | * |
| 50 | * This file handles the common internal GART management. |
| 51 | */ |
| 52 | |
| 53 | /* |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 54 | * Common GART table functions. |
| 55 | */ |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 56 | /** |
| 57 | * radeon_gart_table_ram_alloc - allocate system ram for gart page table |
| 58 | * |
| 59 | * @rdev: radeon_device pointer |
| 60 | * |
| 61 | * Allocate system memory for GART page table |
| 62 | * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the |
| 63 | * gart table to be in system memory. |
| 64 | * Returns 0 for success, -ENOMEM for failure. |
| 65 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 66 | int radeon_gart_table_ram_alloc(struct radeon_device *rdev) |
| 67 | { |
| 68 | void *ptr; |
| 69 | |
| 70 | ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size, |
| 71 | &rdev->gart.table_addr); |
| 72 | if (ptr == NULL) { |
| 73 | return -ENOMEM; |
| 74 | } |
| 75 | #ifdef CONFIG_X86 |
| 76 | if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 || |
| 77 | rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) { |
| 78 | set_memory_uc((unsigned long)ptr, |
| 79 | rdev->gart.table_size >> PAGE_SHIFT); |
| 80 | } |
| 81 | #endif |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 82 | rdev->gart.ptr = ptr; |
| 83 | memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 87 | /** |
| 88 | * radeon_gart_table_ram_free - free system ram for gart page table |
| 89 | * |
| 90 | * @rdev: radeon_device pointer |
| 91 | * |
| 92 | * Free system memory for GART page table |
| 93 | * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the |
| 94 | * gart table to be in system memory. |
| 95 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 96 | void radeon_gart_table_ram_free(struct radeon_device *rdev) |
| 97 | { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 98 | if (rdev->gart.ptr == NULL) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | #ifdef CONFIG_X86 |
| 102 | if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 || |
| 103 | rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 104 | set_memory_wb((unsigned long)rdev->gart.ptr, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 105 | rdev->gart.table_size >> PAGE_SHIFT); |
| 106 | } |
| 107 | #endif |
| 108 | pci_free_consistent(rdev->pdev, rdev->gart.table_size, |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 109 | (void *)rdev->gart.ptr, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 110 | rdev->gart.table_addr); |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 111 | rdev->gart.ptr = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 112 | rdev->gart.table_addr = 0; |
| 113 | } |
| 114 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 115 | /** |
| 116 | * radeon_gart_table_vram_alloc - allocate vram for gart page table |
| 117 | * |
| 118 | * @rdev: radeon_device pointer |
| 119 | * |
| 120 | * Allocate video memory for GART page table |
| 121 | * (pcie r4xx, r5xx+). These asics require the |
| 122 | * gart table to be in video memory. |
| 123 | * Returns 0 for success, error for failure. |
| 124 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 125 | int radeon_gart_table_vram_alloc(struct radeon_device *rdev) |
| 126 | { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 127 | int r; |
| 128 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 129 | if (rdev->gart.robj == NULL) { |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 130 | r = radeon_bo_create(rdev, rdev->gart.table_size, |
Alex Deucher | 268b251 | 2010-11-17 19:00:26 -0500 | [diff] [blame] | 131 | PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM, |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 132 | NULL, &rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 133 | if (r) { |
| 134 | return r; |
| 135 | } |
| 136 | } |
Jerome Glisse | 4aac047 | 2009-09-14 18:29:49 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 140 | /** |
| 141 | * radeon_gart_table_vram_pin - pin gart page table in vram |
| 142 | * |
| 143 | * @rdev: radeon_device pointer |
| 144 | * |
| 145 | * Pin the GART page table in vram so it will not be moved |
| 146 | * by the memory manager (pcie r4xx, r5xx+). These asics require the |
| 147 | * gart table to be in video memory. |
| 148 | * Returns 0 for success, error for failure. |
| 149 | */ |
Jerome Glisse | 4aac047 | 2009-09-14 18:29:49 +0200 | [diff] [blame] | 150 | int radeon_gart_table_vram_pin(struct radeon_device *rdev) |
| 151 | { |
| 152 | uint64_t gpu_addr; |
| 153 | int r; |
| 154 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 155 | r = radeon_bo_reserve(rdev->gart.robj, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 156 | if (unlikely(r != 0)) |
| 157 | return r; |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 158 | r = radeon_bo_pin(rdev->gart.robj, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 159 | RADEON_GEM_DOMAIN_VRAM, &gpu_addr); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 160 | if (r) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 161 | radeon_bo_unreserve(rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 162 | return r; |
| 163 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 164 | r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 165 | if (r) |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 166 | radeon_bo_unpin(rdev->gart.robj); |
| 167 | radeon_bo_unreserve(rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 168 | rdev->gart.table_addr = gpu_addr; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 169 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 172 | /** |
| 173 | * radeon_gart_table_vram_unpin - unpin gart page table in vram |
| 174 | * |
| 175 | * @rdev: radeon_device pointer |
| 176 | * |
| 177 | * Unpin the GART page table in vram (pcie r4xx, r5xx+). |
| 178 | * These asics require the gart table to be in video memory. |
| 179 | */ |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 180 | void radeon_gart_table_vram_unpin(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 181 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 182 | int r; |
| 183 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 184 | if (rdev->gart.robj == NULL) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 185 | return; |
| 186 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 187 | r = radeon_bo_reserve(rdev->gart.robj, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 188 | if (likely(r == 0)) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 189 | radeon_bo_kunmap(rdev->gart.robj); |
| 190 | radeon_bo_unpin(rdev->gart.robj); |
| 191 | radeon_bo_unreserve(rdev->gart.robj); |
| 192 | rdev->gart.ptr = NULL; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 193 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 196 | /** |
| 197 | * radeon_gart_table_vram_free - free gart page table vram |
| 198 | * |
| 199 | * @rdev: radeon_device pointer |
| 200 | * |
| 201 | * Free the video memory used for the GART page table |
| 202 | * (pcie r4xx, r5xx+). These asics require the gart table to |
| 203 | * be in video memory. |
| 204 | */ |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 205 | void radeon_gart_table_vram_free(struct radeon_device *rdev) |
| 206 | { |
| 207 | if (rdev->gart.robj == NULL) { |
| 208 | return; |
| 209 | } |
| 210 | radeon_gart_table_vram_unpin(rdev); |
| 211 | radeon_bo_unref(&rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 214 | /* |
| 215 | * Common gart functions. |
| 216 | */ |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 217 | /** |
| 218 | * radeon_gart_unbind - unbind pages from the gart page table |
| 219 | * |
| 220 | * @rdev: radeon_device pointer |
| 221 | * @offset: offset into the GPU's gart aperture |
| 222 | * @pages: number of pages to unbind |
| 223 | * |
| 224 | * Unbinds the requested pages from the gart page table and |
| 225 | * replaces them with the dummy page (all asics). |
| 226 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 227 | void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset, |
| 228 | int pages) |
| 229 | { |
| 230 | unsigned t; |
| 231 | unsigned p; |
| 232 | int i, j; |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 233 | u64 page_base; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 234 | |
| 235 | if (!rdev->gart.ready) { |
Tormod Volden | fcf4de5 | 2011-08-31 21:54:07 +0000 | [diff] [blame] | 236 | WARN(1, "trying to unbind memory from uninitialized GART !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 237 | return; |
| 238 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 239 | t = offset / RADEON_GPU_PAGE_SIZE; |
| 240 | p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 241 | for (i = 0; i < pages; i++, p++) { |
| 242 | if (rdev->gart.pages[p]) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 243 | rdev->gart.pages[p] = NULL; |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 244 | rdev->gart.pages_addr[p] = rdev->dummy_page.addr; |
| 245 | page_base = rdev->gart.pages_addr[p]; |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 246 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 247 | if (rdev->gart.ptr) { |
| 248 | radeon_gart_set_page(rdev, t, page_base); |
| 249 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 250 | page_base += RADEON_GPU_PAGE_SIZE; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | mb(); |
| 255 | radeon_gart_tlb_flush(rdev); |
| 256 | } |
| 257 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 258 | /** |
| 259 | * radeon_gart_bind - bind pages into the gart page table |
| 260 | * |
| 261 | * @rdev: radeon_device pointer |
| 262 | * @offset: offset into the GPU's gart aperture |
| 263 | * @pages: number of pages to bind |
| 264 | * @pagelist: pages to bind |
| 265 | * @dma_addr: DMA addresses of pages |
| 266 | * |
| 267 | * Binds the requested pages to the gart page table |
| 268 | * (all asics). |
| 269 | * Returns 0 for success, -EINVAL for failure. |
| 270 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 271 | int radeon_gart_bind(struct radeon_device *rdev, unsigned offset, |
Konrad Rzeszutek Wilk | c39d351 | 2010-12-02 11:04:29 -0500 | [diff] [blame] | 272 | int pages, struct page **pagelist, dma_addr_t *dma_addr) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 273 | { |
| 274 | unsigned t; |
| 275 | unsigned p; |
| 276 | uint64_t page_base; |
| 277 | int i, j; |
| 278 | |
| 279 | if (!rdev->gart.ready) { |
Tormod Volden | fcf4de5 | 2011-08-31 21:54:07 +0000 | [diff] [blame] | 280 | WARN(1, "trying to bind memory to uninitialized GART !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 281 | return -EINVAL; |
| 282 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 283 | t = offset / RADEON_GPU_PAGE_SIZE; |
| 284 | p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 285 | |
| 286 | for (i = 0; i < pages; i++, p++) { |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame] | 287 | rdev->gart.pages_addr[p] = dma_addr[i]; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 288 | rdev->gart.pages[p] = pagelist[i]; |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 289 | if (rdev->gart.ptr) { |
| 290 | page_base = rdev->gart.pages_addr[p]; |
| 291 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
| 292 | radeon_gart_set_page(rdev, t, page_base); |
| 293 | page_base += RADEON_GPU_PAGE_SIZE; |
| 294 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | mb(); |
| 298 | radeon_gart_tlb_flush(rdev); |
| 299 | return 0; |
| 300 | } |
| 301 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 302 | /** |
| 303 | * radeon_gart_restore - bind all pages in the gart page table |
| 304 | * |
| 305 | * @rdev: radeon_device pointer |
| 306 | * |
| 307 | * Binds all pages in the gart page table (all asics). |
| 308 | * Used to rebuild the gart table on device startup or resume. |
| 309 | */ |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 310 | void radeon_gart_restore(struct radeon_device *rdev) |
| 311 | { |
| 312 | int i, j, t; |
| 313 | u64 page_base; |
| 314 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 315 | if (!rdev->gart.ptr) { |
| 316 | return; |
| 317 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 318 | for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) { |
| 319 | page_base = rdev->gart.pages_addr[i]; |
| 320 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
| 321 | radeon_gart_set_page(rdev, t, page_base); |
| 322 | page_base += RADEON_GPU_PAGE_SIZE; |
| 323 | } |
| 324 | } |
| 325 | mb(); |
| 326 | radeon_gart_tlb_flush(rdev); |
| 327 | } |
| 328 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 329 | /** |
| 330 | * radeon_gart_init - init the driver info for managing the gart |
| 331 | * |
| 332 | * @rdev: radeon_device pointer |
| 333 | * |
| 334 | * Allocate the dummy page and init the gart driver info (all asics). |
| 335 | * Returns 0 for success, error for failure. |
| 336 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 337 | int radeon_gart_init(struct radeon_device *rdev) |
| 338 | { |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 339 | int r, i; |
| 340 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 341 | if (rdev->gart.pages) { |
| 342 | return 0; |
| 343 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 344 | /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */ |
| 345 | if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 346 | DRM_ERROR("Page size is smaller than GPU page size!\n"); |
| 347 | return -EINVAL; |
| 348 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 349 | r = radeon_dummy_page_init(rdev); |
| 350 | if (r) |
| 351 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 352 | /* Compute table size */ |
| 353 | rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE; |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 354 | rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 355 | DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n", |
| 356 | rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages); |
| 357 | /* Allocate pages table */ |
| 358 | rdev->gart.pages = kzalloc(sizeof(void *) * rdev->gart.num_cpu_pages, |
| 359 | GFP_KERNEL); |
| 360 | if (rdev->gart.pages == NULL) { |
| 361 | radeon_gart_fini(rdev); |
| 362 | return -ENOMEM; |
| 363 | } |
| 364 | rdev->gart.pages_addr = kzalloc(sizeof(dma_addr_t) * |
| 365 | rdev->gart.num_cpu_pages, GFP_KERNEL); |
| 366 | if (rdev->gart.pages_addr == NULL) { |
| 367 | radeon_gart_fini(rdev); |
| 368 | return -ENOMEM; |
| 369 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 370 | /* set GART entry to point to the dummy page by default */ |
| 371 | for (i = 0; i < rdev->gart.num_cpu_pages; i++) { |
| 372 | rdev->gart.pages_addr[i] = rdev->dummy_page.addr; |
| 373 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Alex Deucher | 03eec93 | 2012-07-17 14:02:39 -0400 | [diff] [blame] | 377 | /** |
| 378 | * radeon_gart_fini - tear down the driver info for managing the gart |
| 379 | * |
| 380 | * @rdev: radeon_device pointer |
| 381 | * |
| 382 | * Tear down the gart driver info and free the dummy page (all asics). |
| 383 | */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 384 | void radeon_gart_fini(struct radeon_device *rdev) |
| 385 | { |
| 386 | if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) { |
| 387 | /* unbind pages */ |
| 388 | radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages); |
| 389 | } |
| 390 | rdev->gart.ready = false; |
| 391 | kfree(rdev->gart.pages); |
| 392 | kfree(rdev->gart.pages_addr); |
| 393 | rdev->gart.pages = NULL; |
| 394 | rdev->gart.pages_addr = NULL; |
Alex Deucher | 92656d7 | 2011-04-12 13:32:13 -0400 | [diff] [blame] | 395 | |
| 396 | radeon_dummy_page_fini(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 397 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 398 | |
| 399 | /* |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 400 | * GPUVM |
| 401 | * GPUVM is similar to the legacy gart on older asics, however |
| 402 | * rather than there being a single global gart table |
| 403 | * for the entire GPU, there are multiple VM page tables active |
| 404 | * at any given time. The VM page tables can contain a mix |
| 405 | * vram pages and system memory pages and system memory pages |
| 406 | * can be mapped as snooped (cached system pages) or unsnooped |
| 407 | * (uncached system pages). |
| 408 | * Each VM has an ID associated with it and there is a page table |
| 409 | * associated with each VMID. When execting a command buffer, |
| 410 | * the kernel tells the the ring what VMID to use for that command |
| 411 | * buffer. VMIDs are allocated dynamically as commands are submitted. |
| 412 | * The userspace drivers maintain their own address space and the kernel |
| 413 | * sets up their pages tables accordingly when they submit their |
| 414 | * command buffers and a VMID is assigned. |
| 415 | * Cayman/Trinity support up to 8 active VMs at any given time; |
| 416 | * SI supports 16. |
| 417 | */ |
| 418 | |
| 419 | /* |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 420 | * vm helpers |
| 421 | * |
| 422 | * TODO bind a default page at vm initialization for default address |
| 423 | */ |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 424 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 425 | /** |
| 426 | * radeon_vm_manager_init - init the vm manager |
| 427 | * |
| 428 | * @rdev: radeon_device pointer |
| 429 | * |
| 430 | * Init the vm manager (cayman+). |
| 431 | * Returns 0 for success, error for failure. |
| 432 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 433 | int radeon_vm_manager_init(struct radeon_device *rdev) |
| 434 | { |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 435 | struct radeon_vm *vm; |
| 436 | struct radeon_bo_va *bo_va; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 437 | int r; |
| 438 | |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 439 | if (!rdev->vm_manager.enabled) { |
Dave Airlie | e6b0b6a | 2012-07-20 00:53:28 -0400 | [diff] [blame] | 440 | /* allocate enough for 2 full VM pts */ |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 441 | r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager, |
Dave Airlie | e6b0b6a | 2012-07-20 00:53:28 -0400 | [diff] [blame] | 442 | rdev->vm_manager.max_pfn * 8 * 2, |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 443 | RADEON_GEM_DOMAIN_VRAM); |
| 444 | if (r) { |
| 445 | dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n", |
| 446 | (rdev->vm_manager.max_pfn * 8) >> 10); |
| 447 | return r; |
| 448 | } |
Alex Deucher | 67e915e | 2012-01-06 09:38:15 -0500 | [diff] [blame] | 449 | |
Christian König | 05b0714 | 2012-08-06 20:21:10 +0200 | [diff] [blame] | 450 | r = radeon_asic_vm_init(rdev); |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 451 | if (r) |
| 452 | return r; |
Christian König | 089a786 | 2012-08-11 11:54:05 +0200 | [diff] [blame] | 453 | |
Alex Deucher | 67e915e | 2012-01-06 09:38:15 -0500 | [diff] [blame] | 454 | rdev->vm_manager.enabled = true; |
| 455 | |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 456 | r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager); |
| 457 | if (r) |
| 458 | return r; |
| 459 | } |
| 460 | |
| 461 | /* restore page table */ |
| 462 | list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) { |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 463 | if (vm->sa_bo == NULL) |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 464 | continue; |
| 465 | |
| 466 | list_for_each_entry(bo_va, &vm->va, vm_list) { |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 467 | bo_va->valid = false; |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 468 | } |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 469 | } |
| 470 | return 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 471 | } |
| 472 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 473 | /** |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 474 | * radeon_vm_free_pt - free the page table for a specific vm |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 475 | * |
| 476 | * @rdev: radeon_device pointer |
| 477 | * @vm: vm to unbind |
| 478 | * |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 479 | * Free the page table of a specific vm (cayman+). |
| 480 | * |
| 481 | * Global and local mutex must be lock! |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 482 | */ |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 483 | static void radeon_vm_free_pt(struct radeon_device *rdev, |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 484 | struct radeon_vm *vm) |
| 485 | { |
| 486 | struct radeon_bo_va *bo_va; |
| 487 | |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 488 | if (!vm->sa_bo) |
| 489 | return; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 490 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 491 | list_del_init(&vm->list); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 492 | radeon_sa_bo_free(rdev, &vm->sa_bo, vm->fence); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 493 | vm->pt = NULL; |
| 494 | |
| 495 | list_for_each_entry(bo_va, &vm->va, vm_list) { |
| 496 | bo_va->valid = false; |
| 497 | } |
| 498 | } |
| 499 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 500 | /** |
| 501 | * radeon_vm_manager_fini - tear down the vm manager |
| 502 | * |
| 503 | * @rdev: radeon_device pointer |
| 504 | * |
| 505 | * Tear down the VM manager (cayman+). |
| 506 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 507 | void radeon_vm_manager_fini(struct radeon_device *rdev) |
| 508 | { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 509 | struct radeon_vm *vm, *tmp; |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 510 | int i; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 511 | |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 512 | if (!rdev->vm_manager.enabled) |
| 513 | return; |
| 514 | |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 515 | mutex_lock(&rdev->vm_manager.lock); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 516 | /* free all allocated page tables */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 517 | list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) { |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 518 | mutex_lock(&vm->mutex); |
| 519 | radeon_vm_free_pt(rdev, vm); |
| 520 | mutex_unlock(&vm->mutex); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 521 | } |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 522 | for (i = 0; i < RADEON_NUM_VM; ++i) { |
| 523 | radeon_fence_unref(&rdev->vm_manager.active[i]); |
| 524 | } |
Christian König | 05b0714 | 2012-08-06 20:21:10 +0200 | [diff] [blame] | 525 | radeon_asic_vm_fini(rdev); |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 526 | mutex_unlock(&rdev->vm_manager.lock); |
Christian König | c6105f2 | 2012-07-05 14:32:00 +0200 | [diff] [blame] | 527 | |
| 528 | radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager); |
| 529 | radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager); |
| 530 | rdev->vm_manager.enabled = false; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 533 | /** |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 534 | * radeon_vm_alloc_pt - allocates a page table for a VM |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 535 | * |
| 536 | * @rdev: radeon_device pointer |
| 537 | * @vm: vm to bind |
| 538 | * |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 539 | * Allocate a page table for the requested vm (cayman+). |
| 540 | * Also starts to populate the page table. |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 541 | * Returns 0 for success, error for failure. |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 542 | * |
| 543 | * Global and local mutex must be locked! |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 544 | */ |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 545 | int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm) |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 546 | { |
| 547 | struct radeon_vm *vm_evict; |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 548 | int r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 549 | |
| 550 | if (vm == NULL) { |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 554 | if (vm->sa_bo != NULL) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 555 | /* update lru */ |
| 556 | list_del_init(&vm->list); |
| 557 | list_add_tail(&vm->list, &rdev->vm_manager.lru_vm); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | retry: |
| 562 | r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, &vm->sa_bo, |
| 563 | RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8), |
Christian König | 557017a | 2012-05-09 15:34:54 +0200 | [diff] [blame] | 564 | RADEON_GPU_PAGE_SIZE, false); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 565 | if (r == -ENOMEM) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 566 | if (list_empty(&rdev->vm_manager.lru_vm)) { |
| 567 | return r; |
| 568 | } |
| 569 | vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 570 | mutex_lock(&vm_evict->mutex); |
| 571 | radeon_vm_free_pt(rdev, vm_evict); |
| 572 | mutex_unlock(&vm_evict->mutex); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 573 | goto retry; |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 574 | |
| 575 | } else if (r) { |
| 576 | return r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 577 | } |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 578 | |
Christian König | 2e0d991 | 2012-05-09 15:34:53 +0200 | [diff] [blame] | 579 | vm->pt = radeon_sa_bo_cpu_addr(vm->sa_bo); |
| 580 | vm->pt_gpu_addr = radeon_sa_bo_gpu_addr(vm->sa_bo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 581 | memset(vm->pt, 0, RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8)); |
| 582 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 583 | list_add_tail(&vm->list, &rdev->vm_manager.lru_vm); |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 584 | return radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, |
| 585 | &rdev->ring_tmp_bo.bo->tbo.mem); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 586 | } |
| 587 | |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 588 | /** |
| 589 | * radeon_vm_grab_id - allocate the next free VMID |
| 590 | * |
| 591 | * @rdev: radeon_device pointer |
| 592 | * @vm: vm to allocate id for |
| 593 | * @ring: ring we want to submit job to |
| 594 | * |
| 595 | * Allocate an id for the vm (cayman+). |
| 596 | * Returns the fence we need to sync to (if any). |
| 597 | * |
| 598 | * Global and local mutex must be locked! |
| 599 | */ |
| 600 | struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev, |
| 601 | struct radeon_vm *vm, int ring) |
| 602 | { |
| 603 | struct radeon_fence *best[RADEON_NUM_RINGS] = {}; |
| 604 | unsigned choices[2] = {}; |
| 605 | unsigned i; |
| 606 | |
| 607 | /* check if the id is still valid */ |
| 608 | if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id]) |
| 609 | return NULL; |
| 610 | |
| 611 | /* we definately need to flush */ |
| 612 | radeon_fence_unref(&vm->last_flush); |
| 613 | |
| 614 | /* skip over VMID 0, since it is the system VM */ |
| 615 | for (i = 1; i < rdev->vm_manager.nvm; ++i) { |
| 616 | struct radeon_fence *fence = rdev->vm_manager.active[i]; |
| 617 | |
| 618 | if (fence == NULL) { |
| 619 | /* found a free one */ |
| 620 | vm->id = i; |
| 621 | return NULL; |
| 622 | } |
| 623 | |
| 624 | if (radeon_fence_is_earlier(fence, best[fence->ring])) { |
| 625 | best[fence->ring] = fence; |
| 626 | choices[fence->ring == ring ? 0 : 1] = i; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | for (i = 0; i < 2; ++i) { |
| 631 | if (choices[i]) { |
| 632 | vm->id = choices[i]; |
| 633 | return rdev->vm_manager.active[choices[i]]; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* should never happen */ |
| 638 | BUG(); |
| 639 | return NULL; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * radeon_vm_fence - remember fence for vm |
| 644 | * |
| 645 | * @rdev: radeon_device pointer |
| 646 | * @vm: vm we want to fence |
| 647 | * @fence: fence to remember |
| 648 | * |
| 649 | * Fence the vm (cayman+). |
| 650 | * Set the fence used to protect page table and id. |
| 651 | * |
| 652 | * Global and local mutex must be locked! |
| 653 | */ |
| 654 | void radeon_vm_fence(struct radeon_device *rdev, |
| 655 | struct radeon_vm *vm, |
| 656 | struct radeon_fence *fence) |
| 657 | { |
| 658 | radeon_fence_unref(&rdev->vm_manager.active[vm->id]); |
| 659 | rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence); |
| 660 | |
| 661 | radeon_fence_unref(&vm->fence); |
| 662 | vm->fence = radeon_fence_ref(fence); |
| 663 | } |
| 664 | |
Christian König | 421ca7a | 2012-09-11 16:10:00 +0200 | [diff] [blame] | 665 | /** |
| 666 | * radeon_vm_bo_find - find the bo_va for a specific vm & bo |
| 667 | * |
| 668 | * @vm: requested vm |
| 669 | * @bo: requested buffer object |
| 670 | * |
| 671 | * Find @bo inside the requested vm (cayman+). |
| 672 | * Search inside the @bos vm list for the requested vm |
| 673 | * Returns the found bo_va or NULL if none is found |
| 674 | * |
| 675 | * Object has to be reserved! |
| 676 | */ |
| 677 | struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm, |
| 678 | struct radeon_bo *bo) |
| 679 | { |
| 680 | struct radeon_bo_va *bo_va; |
| 681 | |
| 682 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
| 683 | if (bo_va->vm == vm) { |
| 684 | return bo_va; |
| 685 | } |
| 686 | } |
| 687 | return NULL; |
| 688 | } |
| 689 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 690 | /** |
| 691 | * radeon_vm_bo_add - add a bo to a specific vm |
| 692 | * |
| 693 | * @rdev: radeon_device pointer |
| 694 | * @vm: requested vm |
| 695 | * @bo: radeon buffer object |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 696 | * |
| 697 | * Add @bo into the requested vm (cayman+). |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 698 | * Add @bo to the list of bos associated with the vm |
| 699 | * Returns newly added bo_va or NULL for failure |
| 700 | * |
| 701 | * Object has to be reserved! |
| 702 | */ |
| 703 | struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev, |
| 704 | struct radeon_vm *vm, |
| 705 | struct radeon_bo *bo) |
| 706 | { |
| 707 | struct radeon_bo_va *bo_va; |
| 708 | |
| 709 | bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL); |
| 710 | if (bo_va == NULL) { |
| 711 | return NULL; |
| 712 | } |
| 713 | bo_va->vm = vm; |
| 714 | bo_va->bo = bo; |
| 715 | bo_va->soffset = 0; |
| 716 | bo_va->eoffset = 0; |
| 717 | bo_va->flags = 0; |
| 718 | bo_va->valid = false; |
| 719 | bo_va->ref_count = 1; |
| 720 | INIT_LIST_HEAD(&bo_va->bo_list); |
| 721 | INIT_LIST_HEAD(&bo_va->vm_list); |
| 722 | |
| 723 | mutex_lock(&vm->mutex); |
| 724 | list_add(&bo_va->vm_list, &vm->va); |
| 725 | list_add_tail(&bo_va->bo_list, &bo->va); |
| 726 | mutex_unlock(&vm->mutex); |
| 727 | |
| 728 | return bo_va; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * radeon_vm_bo_set_addr - set bos virtual address inside a vm |
| 733 | * |
| 734 | * @rdev: radeon_device pointer |
| 735 | * @bo_va: bo_va to store the address |
| 736 | * @soffset: requested offset of the buffer in the VM address space |
| 737 | * @flags: attributes of pages (read/write/valid/etc.) |
| 738 | * |
| 739 | * Set offset of @bo_va (cayman+). |
| 740 | * Validate and set the offset requested within the vm address space. |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 741 | * Returns 0 for success, error for failure. |
Christian König | 421ca7a | 2012-09-11 16:10:00 +0200 | [diff] [blame] | 742 | * |
| 743 | * Object has to be reserved! |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 744 | */ |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 745 | int radeon_vm_bo_set_addr(struct radeon_device *rdev, |
| 746 | struct radeon_bo_va *bo_va, |
| 747 | uint64_t soffset, |
| 748 | uint32_t flags) |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 749 | { |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 750 | uint64_t size = radeon_bo_size(bo_va->bo); |
| 751 | uint64_t eoffset, last_offset = 0; |
| 752 | struct radeon_vm *vm = bo_va->vm; |
| 753 | struct radeon_bo_va *tmp; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 754 | struct list_head *head; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 755 | unsigned last_pfn; |
| 756 | |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 757 | if (soffset) { |
| 758 | /* make sure object fit at this offset */ |
| 759 | eoffset = soffset + size; |
| 760 | if (soffset >= eoffset) { |
| 761 | return -EINVAL; |
| 762 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 763 | |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 764 | last_pfn = eoffset / RADEON_GPU_PAGE_SIZE; |
| 765 | if (last_pfn > rdev->vm_manager.max_pfn) { |
| 766 | dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n", |
| 767 | last_pfn, rdev->vm_manager.max_pfn); |
| 768 | return -EINVAL; |
| 769 | } |
| 770 | |
| 771 | } else { |
| 772 | eoffset = last_pfn = 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | mutex_lock(&vm->mutex); |
| 776 | if (last_pfn > vm->last_pfn) { |
Christian König | bb40915 | 2012-06-03 16:09:43 +0200 | [diff] [blame] | 777 | /* release mutex and lock in right order */ |
| 778 | mutex_unlock(&vm->mutex); |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 779 | mutex_lock(&rdev->vm_manager.lock); |
Christian König | bb40915 | 2012-06-03 16:09:43 +0200 | [diff] [blame] | 780 | mutex_lock(&vm->mutex); |
| 781 | /* and check again */ |
| 782 | if (last_pfn > vm->last_pfn) { |
| 783 | /* grow va space 32M by 32M */ |
| 784 | unsigned align = ((32 << 20) >> 12) - 1; |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 785 | radeon_vm_free_pt(rdev, vm); |
Christian König | bb40915 | 2012-06-03 16:09:43 +0200 | [diff] [blame] | 786 | vm->last_pfn = (last_pfn + align) & ~align; |
| 787 | } |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 788 | mutex_unlock(&rdev->vm_manager.lock); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 789 | } |
| 790 | head = &vm->va; |
| 791 | last_offset = 0; |
| 792 | list_for_each_entry(tmp, &vm->va, vm_list) { |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 793 | if (bo_va == tmp) { |
| 794 | /* skip over currently modified bo */ |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | if (soffset >= last_offset && eoffset <= tmp->soffset) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 799 | /* bo can be added before this one */ |
| 800 | break; |
| 801 | } |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 802 | if (eoffset > tmp->soffset && soffset < tmp->eoffset) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 803 | /* bo and tmp overlap, invalid offset */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 804 | dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n", |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 805 | bo_va->bo, (unsigned)bo_va->soffset, tmp->bo, |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 806 | (unsigned)tmp->soffset, (unsigned)tmp->eoffset); |
| 807 | mutex_unlock(&vm->mutex); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | last_offset = tmp->eoffset; |
| 811 | head = &tmp->vm_list; |
| 812 | } |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 813 | |
| 814 | bo_va->soffset = soffset; |
| 815 | bo_va->eoffset = eoffset; |
| 816 | bo_va->flags = flags; |
| 817 | bo_va->valid = false; |
| 818 | list_move(&bo_va->vm_list, head); |
| 819 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 820 | mutex_unlock(&vm->mutex); |
| 821 | return 0; |
| 822 | } |
| 823 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 824 | /** |
| 825 | * radeon_vm_get_addr - get the physical address of the page |
| 826 | * |
| 827 | * @rdev: radeon_device pointer |
| 828 | * @mem: ttm mem |
| 829 | * @pfn: pfn |
| 830 | * |
| 831 | * Look up the physical address of the page that the pte resolves |
| 832 | * to (cayman+). |
| 833 | * Returns the physical address of the page. |
| 834 | */ |
Christian König | 089a786 | 2012-08-11 11:54:05 +0200 | [diff] [blame] | 835 | u64 radeon_vm_get_addr(struct radeon_device *rdev, |
| 836 | struct ttm_mem_reg *mem, |
| 837 | unsigned pfn) |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 838 | { |
| 839 | u64 addr = 0; |
| 840 | |
| 841 | switch (mem->mem_type) { |
| 842 | case TTM_PL_VRAM: |
| 843 | addr = (mem->start << PAGE_SHIFT); |
| 844 | addr += pfn * RADEON_GPU_PAGE_SIZE; |
| 845 | addr += rdev->vm_manager.vram_base_offset; |
| 846 | break; |
| 847 | case TTM_PL_TT: |
| 848 | /* offset inside page table */ |
| 849 | addr = mem->start << PAGE_SHIFT; |
| 850 | addr += pfn * RADEON_GPU_PAGE_SIZE; |
| 851 | addr = addr >> PAGE_SHIFT; |
| 852 | /* page table offset */ |
| 853 | addr = rdev->gart.pages_addr[addr]; |
| 854 | /* in case cpu page size != gpu page size*/ |
| 855 | addr += (pfn * RADEON_GPU_PAGE_SIZE) & (~PAGE_MASK); |
| 856 | break; |
| 857 | default: |
| 858 | break; |
| 859 | } |
| 860 | return addr; |
| 861 | } |
| 862 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 863 | /** |
| 864 | * radeon_vm_bo_update_pte - map a bo into the vm page table |
| 865 | * |
| 866 | * @rdev: radeon_device pointer |
| 867 | * @vm: requested vm |
| 868 | * @bo: radeon buffer object |
| 869 | * @mem: ttm mem |
| 870 | * |
| 871 | * Fill in the page table entries for @bo (cayman+). |
| 872 | * Returns 0 for success, -EINVAL for failure. |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 873 | * |
| 874 | * Object have to be reserved & global and local mutex must be locked! |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 875 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 876 | int radeon_vm_bo_update_pte(struct radeon_device *rdev, |
| 877 | struct radeon_vm *vm, |
| 878 | struct radeon_bo *bo, |
| 879 | struct ttm_mem_reg *mem) |
| 880 | { |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 881 | unsigned ridx = rdev->asic->vm.pt_ring_index; |
| 882 | struct radeon_ring *ring = &rdev->ring[ridx]; |
| 883 | struct radeon_semaphore *sem = NULL; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 884 | struct radeon_bo_va *bo_va; |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 885 | unsigned ngpu_pages, ndw; |
Christian König | 089a786 | 2012-08-11 11:54:05 +0200 | [diff] [blame] | 886 | uint64_t pfn; |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 887 | int r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 888 | |
| 889 | /* nothing to do if vm isn't bound */ |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 890 | if (vm->sa_bo == NULL) |
Jesper Juhl | 04bd27a | 2012-02-26 23:51:53 +0100 | [diff] [blame] | 891 | return 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 892 | |
Christian König | 421ca7a | 2012-09-11 16:10:00 +0200 | [diff] [blame] | 893 | bo_va = radeon_vm_bo_find(vm, bo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 894 | if (bo_va == NULL) { |
| 895 | dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm); |
| 896 | return -EINVAL; |
| 897 | } |
| 898 | |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 899 | if (!bo_va->soffset) { |
| 900 | dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n", |
| 901 | bo, vm); |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 905 | if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL)) |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 906 | return 0; |
| 907 | |
| 908 | ngpu_pages = radeon_bo_ngpu_pages(bo); |
| 909 | bo_va->flags &= ~RADEON_VM_PAGE_VALID; |
| 910 | bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM; |
| 911 | if (mem) { |
| 912 | if (mem->mem_type != TTM_PL_SYSTEM) { |
| 913 | bo_va->flags |= RADEON_VM_PAGE_VALID; |
| 914 | bo_va->valid = true; |
| 915 | } |
| 916 | if (mem->mem_type == TTM_PL_TT) { |
| 917 | bo_va->flags |= RADEON_VM_PAGE_SYSTEM; |
| 918 | } |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 919 | if (!bo_va->valid) { |
| 920 | mem = NULL; |
| 921 | } |
| 922 | } else { |
| 923 | bo_va->valid = false; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 924 | } |
Christian König | 089a786 | 2012-08-11 11:54:05 +0200 | [diff] [blame] | 925 | pfn = bo_va->soffset / RADEON_GPU_PAGE_SIZE; |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 926 | |
| 927 | if (vm->fence && radeon_fence_signaled(vm->fence)) { |
| 928 | radeon_fence_unref(&vm->fence); |
| 929 | } |
| 930 | |
| 931 | if (vm->fence && vm->fence->ring != ridx) { |
| 932 | r = radeon_semaphore_create(rdev, &sem); |
| 933 | if (r) { |
| 934 | return r; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* estimate number of dw needed */ |
| 939 | ndw = 32; |
| 940 | ndw += (ngpu_pages >> 12) * 3; |
| 941 | ndw += ngpu_pages * 2; |
| 942 | |
| 943 | r = radeon_ring_lock(rdev, ring, ndw); |
| 944 | if (r) { |
| 945 | return r; |
| 946 | } |
| 947 | |
| 948 | if (sem && radeon_fence_need_sync(vm->fence, ridx)) { |
| 949 | radeon_semaphore_sync_rings(rdev, sem, vm->fence->ring, ridx); |
| 950 | radeon_fence_note_sync(vm->fence, ridx); |
| 951 | } |
| 952 | |
| 953 | radeon_asic_vm_set_page(rdev, vm, pfn, mem, ngpu_pages, bo_va->flags); |
| 954 | |
| 955 | radeon_fence_unref(&vm->fence); |
| 956 | r = radeon_fence_emit(rdev, &vm->fence, ridx); |
| 957 | if (r) { |
| 958 | radeon_ring_unlock_undo(rdev, ring); |
| 959 | return r; |
| 960 | } |
| 961 | radeon_ring_unlock_commit(rdev, ring); |
| 962 | radeon_semaphore_free(rdev, &sem, vm->fence); |
Christian König | 9b40e5d | 2012-08-08 12:22:43 +0200 | [diff] [blame] | 963 | radeon_fence_unref(&vm->last_flush); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 967 | /** |
| 968 | * radeon_vm_bo_rmv - remove a bo to a specific vm |
| 969 | * |
| 970 | * @rdev: radeon_device pointer |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 971 | * @bo_va: requested bo_va |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 972 | * |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 973 | * Remove @bo_va->bo from the requested vm (cayman+). |
| 974 | * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and |
| 975 | * remove the ptes for @bo_va in the page table. |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 976 | * Returns 0 for success. |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 977 | * |
| 978 | * Object have to be reserved! |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 979 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 980 | int radeon_vm_bo_rmv(struct radeon_device *rdev, |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 981 | struct radeon_bo_va *bo_va) |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 982 | { |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 983 | int r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 984 | |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 985 | mutex_lock(&rdev->vm_manager.lock); |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 986 | mutex_lock(&bo_va->vm->mutex); |
| 987 | r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL); |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 988 | mutex_unlock(&rdev->vm_manager.lock); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 989 | list_del(&bo_va->vm_list); |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 990 | mutex_unlock(&bo_va->vm->mutex); |
Sebastian Biemueller | 108b0d3 | 2012-02-29 11:04:52 -0500 | [diff] [blame] | 991 | list_del(&bo_va->bo_list); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 992 | |
| 993 | kfree(bo_va); |
Christian König | 2a6f1ab | 2012-08-11 15:00:30 +0200 | [diff] [blame] | 994 | return r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 995 | } |
| 996 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 997 | /** |
| 998 | * radeon_vm_bo_invalidate - mark the bo as invalid |
| 999 | * |
| 1000 | * @rdev: radeon_device pointer |
| 1001 | * @vm: requested vm |
| 1002 | * @bo: radeon buffer object |
| 1003 | * |
| 1004 | * Mark @bo as invalid (cayman+). |
| 1005 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1006 | void radeon_vm_bo_invalidate(struct radeon_device *rdev, |
| 1007 | struct radeon_bo *bo) |
| 1008 | { |
| 1009 | struct radeon_bo_va *bo_va; |
| 1010 | |
| 1011 | BUG_ON(!atomic_read(&bo->tbo.reserved)); |
| 1012 | list_for_each_entry(bo_va, &bo->va, bo_list) { |
| 1013 | bo_va->valid = false; |
| 1014 | } |
| 1015 | } |
| 1016 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 1017 | /** |
| 1018 | * radeon_vm_init - initialize a vm instance |
| 1019 | * |
| 1020 | * @rdev: radeon_device pointer |
| 1021 | * @vm: requested vm |
| 1022 | * |
| 1023 | * Init @vm (cayman+). |
| 1024 | * Map the IB pool and any other shared objects into the VM |
| 1025 | * by default as it's used by all VMs. |
| 1026 | * Returns 0 for success, error for failure. |
| 1027 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1028 | int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm) |
| 1029 | { |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 1030 | struct radeon_bo_va *bo_va; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1031 | int r; |
| 1032 | |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 1033 | vm->id = 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1034 | vm->fence = NULL; |
| 1035 | mutex_init(&vm->mutex); |
| 1036 | INIT_LIST_HEAD(&vm->list); |
| 1037 | INIT_LIST_HEAD(&vm->va); |
Alex Deucher | c21b328 | 2012-06-28 17:53:07 -0400 | [diff] [blame] | 1038 | /* SI requires equal sized PTs for all VMs, so always set |
| 1039 | * last_pfn to max_pfn. cayman allows variable sized |
| 1040 | * pts so we can grow then as needed. Once we switch |
| 1041 | * to two level pts we can unify this again. |
| 1042 | */ |
| 1043 | if (rdev->family >= CHIP_TAHITI) |
| 1044 | vm->last_pfn = rdev->vm_manager.max_pfn; |
| 1045 | else |
| 1046 | vm->last_pfn = 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1047 | /* map the ib pool buffer at 0 in virtual address space, set |
| 1048 | * read only |
| 1049 | */ |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame^] | 1050 | bo_va = radeon_vm_bo_add(rdev, vm, rdev->ring_tmp_bo.bo); |
| 1051 | r = radeon_vm_bo_set_addr(rdev, bo_va, RADEON_VA_IB_OFFSET, |
| 1052 | RADEON_VM_PAGE_READABLE | |
| 1053 | RADEON_VM_PAGE_SNOOPED); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1054 | return r; |
| 1055 | } |
| 1056 | |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 1057 | /** |
Dmitrii Cherkasov | f59abbf | 2012-08-13 10:53:29 -0400 | [diff] [blame] | 1058 | * radeon_vm_fini - tear down a vm instance |
Alex Deucher | 09db864 | 2012-07-17 14:02:40 -0400 | [diff] [blame] | 1059 | * |
| 1060 | * @rdev: radeon_device pointer |
| 1061 | * @vm: requested vm |
| 1062 | * |
| 1063 | * Tear down @vm (cayman+). |
| 1064 | * Unbind the VM and remove all bos from the vm bo list |
| 1065 | */ |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1066 | void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm) |
| 1067 | { |
| 1068 | struct radeon_bo_va *bo_va, *tmp; |
| 1069 | int r; |
| 1070 | |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 1071 | mutex_lock(&rdev->vm_manager.lock); |
Christian König | bb40915 | 2012-06-03 16:09:43 +0200 | [diff] [blame] | 1072 | mutex_lock(&vm->mutex); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 1073 | radeon_vm_free_pt(rdev, vm); |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 1074 | mutex_unlock(&rdev->vm_manager.lock); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1075 | |
Jerome Glisse | e43b5ec | 2012-08-06 12:32:21 -0400 | [diff] [blame] | 1076 | /* remove all bo at this point non are busy any more because unbind |
| 1077 | * waited for the last vm fence to signal |
| 1078 | */ |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 1079 | r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1080 | if (!r) { |
Christian König | 421ca7a | 2012-09-11 16:10:00 +0200 | [diff] [blame] | 1081 | bo_va = radeon_vm_bo_find(vm, rdev->ring_tmp_bo.bo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1082 | list_del_init(&bo_va->bo_list); |
| 1083 | list_del_init(&bo_va->vm_list); |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 1084 | radeon_bo_unreserve(rdev->ring_tmp_bo.bo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1085 | kfree(bo_va); |
| 1086 | } |
| 1087 | if (!list_empty(&vm->va)) { |
| 1088 | dev_err(rdev->dev, "still active bo inside vm\n"); |
| 1089 | } |
| 1090 | list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) { |
| 1091 | list_del_init(&bo_va->vm_list); |
| 1092 | r = radeon_bo_reserve(bo_va->bo, false); |
| 1093 | if (!r) { |
| 1094 | list_del_init(&bo_va->bo_list); |
| 1095 | radeon_bo_unreserve(bo_va->bo); |
| 1096 | kfree(bo_va); |
| 1097 | } |
| 1098 | } |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 1099 | radeon_fence_unref(&vm->fence); |
| 1100 | radeon_fence_unref(&vm->last_flush); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 1101 | mutex_unlock(&vm->mutex); |
| 1102 | } |