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 | /* |
| 34 | * Common GART table functions. |
| 35 | */ |
| 36 | int radeon_gart_table_ram_alloc(struct radeon_device *rdev) |
| 37 | { |
| 38 | void *ptr; |
| 39 | |
| 40 | ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size, |
| 41 | &rdev->gart.table_addr); |
| 42 | if (ptr == NULL) { |
| 43 | return -ENOMEM; |
| 44 | } |
| 45 | #ifdef CONFIG_X86 |
| 46 | if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 || |
| 47 | rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) { |
| 48 | set_memory_uc((unsigned long)ptr, |
| 49 | rdev->gart.table_size >> PAGE_SHIFT); |
| 50 | } |
| 51 | #endif |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 52 | rdev->gart.ptr = ptr; |
| 53 | memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void radeon_gart_table_ram_free(struct radeon_device *rdev) |
| 58 | { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 59 | if (rdev->gart.ptr == NULL) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | #ifdef CONFIG_X86 |
| 63 | if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 || |
| 64 | rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 65 | set_memory_wb((unsigned long)rdev->gart.ptr, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 66 | rdev->gart.table_size >> PAGE_SHIFT); |
| 67 | } |
| 68 | #endif |
| 69 | pci_free_consistent(rdev->pdev, rdev->gart.table_size, |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 70 | (void *)rdev->gart.ptr, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 71 | rdev->gart.table_addr); |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 72 | rdev->gart.ptr = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 73 | rdev->gart.table_addr = 0; |
| 74 | } |
| 75 | |
| 76 | int radeon_gart_table_vram_alloc(struct radeon_device *rdev) |
| 77 | { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 78 | int r; |
| 79 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 80 | if (rdev->gart.robj == NULL) { |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 81 | r = radeon_bo_create(rdev, rdev->gart.table_size, |
Alex Deucher | 268b251 | 2010-11-17 19:00:26 -0500 | [diff] [blame] | 82 | PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM, |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 83 | &rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 84 | if (r) { |
| 85 | return r; |
| 86 | } |
| 87 | } |
Jerome Glisse | 4aac047 | 2009-09-14 18:29:49 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int radeon_gart_table_vram_pin(struct radeon_device *rdev) |
| 92 | { |
| 93 | uint64_t gpu_addr; |
| 94 | int r; |
| 95 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 96 | r = radeon_bo_reserve(rdev->gart.robj, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 97 | if (unlikely(r != 0)) |
| 98 | return r; |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 99 | r = radeon_bo_pin(rdev->gart.robj, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 100 | RADEON_GEM_DOMAIN_VRAM, &gpu_addr); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 101 | if (r) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 102 | radeon_bo_unreserve(rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 103 | return r; |
| 104 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 105 | r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 106 | if (r) |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 107 | radeon_bo_unpin(rdev->gart.robj); |
| 108 | radeon_bo_unreserve(rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 109 | rdev->gart.table_addr = gpu_addr; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 110 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 113 | void radeon_gart_table_vram_unpin(struct radeon_device *rdev) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 114 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 115 | int r; |
| 116 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 117 | if (rdev->gart.robj == NULL) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 118 | return; |
| 119 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 120 | r = radeon_bo_reserve(rdev->gart.robj, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 121 | if (likely(r == 0)) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 122 | radeon_bo_kunmap(rdev->gart.robj); |
| 123 | radeon_bo_unpin(rdev->gart.robj); |
| 124 | radeon_bo_unreserve(rdev->gart.robj); |
| 125 | rdev->gart.ptr = NULL; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 126 | } |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void radeon_gart_table_vram_free(struct radeon_device *rdev) |
| 130 | { |
| 131 | if (rdev->gart.robj == NULL) { |
| 132 | return; |
| 133 | } |
| 134 | radeon_gart_table_vram_unpin(rdev); |
| 135 | radeon_bo_unref(&rdev->gart.robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Common gart functions. |
| 143 | */ |
| 144 | void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset, |
| 145 | int pages) |
| 146 | { |
| 147 | unsigned t; |
| 148 | unsigned p; |
| 149 | int i, j; |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 150 | u64 page_base; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 151 | |
| 152 | if (!rdev->gart.ready) { |
Tormod Volden | fcf4de5 | 2011-08-31 21:54:07 +0000 | [diff] [blame] | 153 | WARN(1, "trying to unbind memory from uninitialized GART !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 156 | t = offset / RADEON_GPU_PAGE_SIZE; |
| 157 | p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 158 | for (i = 0; i < pages; i++, p++) { |
| 159 | if (rdev->gart.pages[p]) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 160 | rdev->gart.pages[p] = NULL; |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 161 | rdev->gart.pages_addr[p] = rdev->dummy_page.addr; |
| 162 | page_base = rdev->gart.pages_addr[p]; |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 163 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 164 | if (rdev->gart.ptr) { |
| 165 | radeon_gart_set_page(rdev, t, page_base); |
| 166 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 167 | page_base += RADEON_GPU_PAGE_SIZE; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | mb(); |
| 172 | radeon_gart_tlb_flush(rdev); |
| 173 | } |
| 174 | |
| 175 | int radeon_gart_bind(struct radeon_device *rdev, unsigned offset, |
Konrad Rzeszutek Wilk | c39d351 | 2010-12-02 11:04:29 -0500 | [diff] [blame] | 176 | int pages, struct page **pagelist, dma_addr_t *dma_addr) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 177 | { |
| 178 | unsigned t; |
| 179 | unsigned p; |
| 180 | uint64_t page_base; |
| 181 | int i, j; |
| 182 | |
| 183 | if (!rdev->gart.ready) { |
Tormod Volden | fcf4de5 | 2011-08-31 21:54:07 +0000 | [diff] [blame] | 184 | WARN(1, "trying to bind memory to uninitialized GART !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 187 | t = offset / RADEON_GPU_PAGE_SIZE; |
| 188 | p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 189 | |
| 190 | for (i = 0; i < pages; i++, p++) { |
Konrad Rzeszutek Wilk | c52494f | 2011-10-17 17:15:08 -0400 | [diff] [blame^] | 191 | rdev->gart.pages_addr[p] = dma_addr[i]; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 192 | rdev->gart.pages[p] = pagelist[i]; |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 193 | if (rdev->gart.ptr) { |
| 194 | page_base = rdev->gart.pages_addr[p]; |
| 195 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
| 196 | radeon_gart_set_page(rdev, t, page_base); |
| 197 | page_base += RADEON_GPU_PAGE_SIZE; |
| 198 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | mb(); |
| 202 | radeon_gart_tlb_flush(rdev); |
| 203 | return 0; |
| 204 | } |
| 205 | |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 206 | void radeon_gart_restore(struct radeon_device *rdev) |
| 207 | { |
| 208 | int i, j, t; |
| 209 | u64 page_base; |
| 210 | |
Jerome Glisse | c9a1be9 | 2011-11-03 11:16:49 -0400 | [diff] [blame] | 211 | if (!rdev->gart.ptr) { |
| 212 | return; |
| 213 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 214 | for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) { |
| 215 | page_base = rdev->gart.pages_addr[i]; |
| 216 | for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) { |
| 217 | radeon_gart_set_page(rdev, t, page_base); |
| 218 | page_base += RADEON_GPU_PAGE_SIZE; |
| 219 | } |
| 220 | } |
| 221 | mb(); |
| 222 | radeon_gart_tlb_flush(rdev); |
| 223 | } |
| 224 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 225 | int radeon_gart_init(struct radeon_device *rdev) |
| 226 | { |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 227 | int r, i; |
| 228 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 229 | if (rdev->gart.pages) { |
| 230 | return 0; |
| 231 | } |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 232 | /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */ |
| 233 | if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 234 | DRM_ERROR("Page size is smaller than GPU page size!\n"); |
| 235 | return -EINVAL; |
| 236 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 237 | r = radeon_dummy_page_init(rdev); |
| 238 | if (r) |
| 239 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 240 | /* Compute table size */ |
| 241 | rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE; |
Matt Turner | a77f171 | 2009-10-14 00:34:41 -0400 | [diff] [blame] | 242 | 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] | 243 | DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n", |
| 244 | rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages); |
| 245 | /* Allocate pages table */ |
| 246 | rdev->gart.pages = kzalloc(sizeof(void *) * rdev->gart.num_cpu_pages, |
| 247 | GFP_KERNEL); |
| 248 | if (rdev->gart.pages == NULL) { |
| 249 | radeon_gart_fini(rdev); |
| 250 | return -ENOMEM; |
| 251 | } |
| 252 | rdev->gart.pages_addr = kzalloc(sizeof(dma_addr_t) * |
| 253 | rdev->gart.num_cpu_pages, GFP_KERNEL); |
| 254 | if (rdev->gart.pages_addr == NULL) { |
| 255 | radeon_gart_fini(rdev); |
| 256 | return -ENOMEM; |
| 257 | } |
Dave Airlie | 8256856 | 2010-02-05 16:00:07 +1000 | [diff] [blame] | 258 | /* set GART entry to point to the dummy page by default */ |
| 259 | for (i = 0; i < rdev->gart.num_cpu_pages; i++) { |
| 260 | rdev->gart.pages_addr[i] = rdev->dummy_page.addr; |
| 261 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | void radeon_gart_fini(struct radeon_device *rdev) |
| 266 | { |
| 267 | if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) { |
| 268 | /* unbind pages */ |
| 269 | radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages); |
| 270 | } |
| 271 | rdev->gart.ready = false; |
| 272 | kfree(rdev->gart.pages); |
| 273 | kfree(rdev->gart.pages_addr); |
| 274 | rdev->gart.pages = NULL; |
| 275 | rdev->gart.pages_addr = NULL; |
Alex Deucher | 92656d7 | 2011-04-12 13:32:13 -0400 | [diff] [blame] | 276 | |
| 277 | radeon_dummy_page_fini(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 278 | } |