blob: 125b7c31fafc48bcbbfa26c5269d2de013e4746b [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 Deucher03eec932012-07-17 14:02:39 -040034 * 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 Glisse771fe6b2009-06-05 14:42:42 +020054 * Common GART table functions.
55 */
Alex Deucher03eec932012-07-17 14:02:39 -040056/**
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 Glisse771fe6b2009-06-05 14:42:42 +020066int 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 Glissec9a1be92011-11-03 11:16:49 -040082 rdev->gart.ptr = ptr;
83 memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020084 return 0;
85}
86
Alex Deucher03eec932012-07-17 14:02:39 -040087/**
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 Glisse771fe6b2009-06-05 14:42:42 +020096void radeon_gart_table_ram_free(struct radeon_device *rdev)
97{
Jerome Glissec9a1be92011-11-03 11:16:49 -040098 if (rdev->gart.ptr == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020099 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 Glissec9a1be92011-11-03 11:16:49 -0400104 set_memory_wb((unsigned long)rdev->gart.ptr,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105 rdev->gart.table_size >> PAGE_SHIFT);
106 }
107#endif
108 pci_free_consistent(rdev->pdev, rdev->gart.table_size,
Jerome Glissec9a1be92011-11-03 11:16:49 -0400109 (void *)rdev->gart.ptr,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110 rdev->gart.table_addr);
Jerome Glissec9a1be92011-11-03 11:16:49 -0400111 rdev->gart.ptr = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112 rdev->gart.table_addr = 0;
113}
114
Alex Deucher03eec932012-07-17 14:02:39 -0400115/**
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 Glisse771fe6b2009-06-05 14:42:42 +0200125int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
126{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200127 int r;
128
Jerome Glissec9a1be92011-11-03 11:16:49 -0400129 if (rdev->gart.robj == NULL) {
Daniel Vetter441921d2011-02-18 17:59:16 +0100130 r = radeon_bo_create(rdev, rdev->gart.table_size,
Alex Deucher268b2512010-11-17 19:00:26 -0500131 PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400132 NULL, &rdev->gart.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200133 if (r) {
134 return r;
135 }
136 }
Jerome Glisse4aac0472009-09-14 18:29:49 +0200137 return 0;
138}
139
Alex Deucher03eec932012-07-17 14:02:39 -0400140/**
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 Glisse4aac0472009-09-14 18:29:49 +0200150int radeon_gart_table_vram_pin(struct radeon_device *rdev)
151{
152 uint64_t gpu_addr;
153 int r;
154
Jerome Glissec9a1be92011-11-03 11:16:49 -0400155 r = radeon_bo_reserve(rdev->gart.robj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100156 if (unlikely(r != 0))
157 return r;
Jerome Glissec9a1be92011-11-03 11:16:49 -0400158 r = radeon_bo_pin(rdev->gart.robj,
Jerome Glisse4c788672009-11-20 14:29:23 +0100159 RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200160 if (r) {
Jerome Glissec9a1be92011-11-03 11:16:49 -0400161 radeon_bo_unreserve(rdev->gart.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162 return r;
163 }
Jerome Glissec9a1be92011-11-03 11:16:49 -0400164 r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
Jerome Glisse4c788672009-11-20 14:29:23 +0100165 if (r)
Jerome Glissec9a1be92011-11-03 11:16:49 -0400166 radeon_bo_unpin(rdev->gart.robj);
167 radeon_bo_unreserve(rdev->gart.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168 rdev->gart.table_addr = gpu_addr;
Jerome Glisse4c788672009-11-20 14:29:23 +0100169 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200170}
171
Alex Deucher03eec932012-07-17 14:02:39 -0400172/**
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 Glissec9a1be92011-11-03 11:16:49 -0400180void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181{
Jerome Glisse4c788672009-11-20 14:29:23 +0100182 int r;
183
Jerome Glissec9a1be92011-11-03 11:16:49 -0400184 if (rdev->gart.robj == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200185 return;
186 }
Jerome Glissec9a1be92011-11-03 11:16:49 -0400187 r = radeon_bo_reserve(rdev->gart.robj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100188 if (likely(r == 0)) {
Jerome Glissec9a1be92011-11-03 11:16:49 -0400189 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 Glisse4c788672009-11-20 14:29:23 +0100193 }
Jerome Glissec9a1be92011-11-03 11:16:49 -0400194}
195
Alex Deucher03eec932012-07-17 14:02:39 -0400196/**
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 Glissec9a1be92011-11-03 11:16:49 -0400205void 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 Glisse771fe6b2009-06-05 14:42:42 +0200212}
213
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200214/*
215 * Common gart functions.
216 */
Alex Deucher03eec932012-07-17 14:02:39 -0400217/**
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 Glisse771fe6b2009-06-05 14:42:42 +0200227void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
228 int pages)
229{
230 unsigned t;
231 unsigned p;
232 int i, j;
Dave Airlie82568562010-02-05 16:00:07 +1000233 u64 page_base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200234
235 if (!rdev->gart.ready) {
Tormod Voldenfcf4de52011-08-31 21:54:07 +0000236 WARN(1, "trying to unbind memory from uninitialized GART !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200237 return;
238 }
Matt Turnera77f1712009-10-14 00:34:41 -0400239 t = offset / RADEON_GPU_PAGE_SIZE;
240 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200241 for (i = 0; i < pages; i++, p++) {
242 if (rdev->gart.pages[p]) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200243 rdev->gart.pages[p] = NULL;
Dave Airlie82568562010-02-05 16:00:07 +1000244 rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
245 page_base = rdev->gart.pages_addr[p];
Matt Turnera77f1712009-10-14 00:34:41 -0400246 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
Jerome Glissec9a1be92011-11-03 11:16:49 -0400247 if (rdev->gart.ptr) {
248 radeon_gart_set_page(rdev, t, page_base);
249 }
Dave Airlie82568562010-02-05 16:00:07 +1000250 page_base += RADEON_GPU_PAGE_SIZE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200251 }
252 }
253 }
254 mb();
255 radeon_gart_tlb_flush(rdev);
256}
257
Alex Deucher03eec932012-07-17 14:02:39 -0400258/**
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 Glisse771fe6b2009-06-05 14:42:42 +0200271int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
Konrad Rzeszutek Wilkc39d3512010-12-02 11:04:29 -0500272 int pages, struct page **pagelist, dma_addr_t *dma_addr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273{
274 unsigned t;
275 unsigned p;
276 uint64_t page_base;
277 int i, j;
278
279 if (!rdev->gart.ready) {
Tormod Voldenfcf4de52011-08-31 21:54:07 +0000280 WARN(1, "trying to bind memory to uninitialized GART !\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281 return -EINVAL;
282 }
Matt Turnera77f1712009-10-14 00:34:41 -0400283 t = offset / RADEON_GPU_PAGE_SIZE;
284 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200285
286 for (i = 0; i < pages; i++, p++) {
Konrad Rzeszutek Wilkc52494f2011-10-17 17:15:08 -0400287 rdev->gart.pages_addr[p] = dma_addr[i];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200288 rdev->gart.pages[p] = pagelist[i];
Jerome Glissec9a1be92011-11-03 11:16:49 -0400289 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 Glisse771fe6b2009-06-05 14:42:42 +0200295 }
296 }
297 mb();
298 radeon_gart_tlb_flush(rdev);
299 return 0;
300}
301
Alex Deucher03eec932012-07-17 14:02:39 -0400302/**
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 Airlie82568562010-02-05 16:00:07 +1000310void radeon_gart_restore(struct radeon_device *rdev)
311{
312 int i, j, t;
313 u64 page_base;
314
Jerome Glissec9a1be92011-11-03 11:16:49 -0400315 if (!rdev->gart.ptr) {
316 return;
317 }
Dave Airlie82568562010-02-05 16:00:07 +1000318 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 Deucher03eec932012-07-17 14:02:39 -0400329/**
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 Glisse771fe6b2009-06-05 14:42:42 +0200337int radeon_gart_init(struct radeon_device *rdev)
338{
Dave Airlie82568562010-02-05 16:00:07 +1000339 int r, i;
340
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200341 if (rdev->gart.pages) {
342 return 0;
343 }
Matt Turnera77f1712009-10-14 00:34:41 -0400344 /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
345 if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346 DRM_ERROR("Page size is smaller than GPU page size!\n");
347 return -EINVAL;
348 }
Dave Airlie82568562010-02-05 16:00:07 +1000349 r = radeon_dummy_page_init(rdev);
350 if (r)
351 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200352 /* Compute table size */
353 rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
Matt Turnera77f1712009-10-14 00:34:41 -0400354 rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200355 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 Airlie82568562010-02-05 16:00:07 +1000370 /* 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 Glisse771fe6b2009-06-05 14:42:42 +0200374 return 0;
375}
376
Alex Deucher03eec932012-07-17 14:02:39 -0400377/**
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 Glisse771fe6b2009-06-05 14:42:42 +0200384void 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 Deucher92656d72011-04-12 13:32:13 -0400395
396 radeon_dummy_page_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200397}
Jerome Glisse721604a2012-01-05 22:11:05 -0500398
399/*
Alex Deucher09db8642012-07-17 14:02:40 -0400400 * 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 Glisse721604a2012-01-05 22:11:05 -0500420 * vm helpers
421 *
422 * TODO bind a default page at vm initialization for default address
423 */
Christian Königc6105f22012-07-05 14:32:00 +0200424
Alex Deucher09db8642012-07-17 14:02:40 -0400425/**
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 Glisse721604a2012-01-05 22:11:05 -0500433int radeon_vm_manager_init(struct radeon_device *rdev)
434{
Christian Königc6105f22012-07-05 14:32:00 +0200435 struct radeon_vm *vm;
436 struct radeon_bo_va *bo_va;
Jerome Glisse721604a2012-01-05 22:11:05 -0500437 int r;
438
Christian Königc6105f22012-07-05 14:32:00 +0200439 if (!rdev->vm_manager.enabled) {
440 /* mark first vm as always in use, it's the system one */
Dave Airliee6b0b6a2012-07-20 00:53:28 -0400441 /* allocate enough for 2 full VM pts */
Christian Königc6105f22012-07-05 14:32:00 +0200442 r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager,
Dave Airliee6b0b6a2012-07-20 00:53:28 -0400443 rdev->vm_manager.max_pfn * 8 * 2,
Christian Königc6105f22012-07-05 14:32:00 +0200444 RADEON_GEM_DOMAIN_VRAM);
445 if (r) {
446 dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n",
447 (rdev->vm_manager.max_pfn * 8) >> 10);
448 return r;
449 }
Alex Deucher67e915e2012-01-06 09:38:15 -0500450
Christian König05b07142012-08-06 20:21:10 +0200451 r = radeon_asic_vm_init(rdev);
Christian Königc6105f22012-07-05 14:32:00 +0200452 if (r)
453 return r;
454
Alex Deucher67e915e2012-01-06 09:38:15 -0500455 rdev->vm_manager.enabled = true;
456
Christian Königc6105f22012-07-05 14:32:00 +0200457 r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager);
458 if (r)
459 return r;
460 }
461
462 /* restore page table */
463 list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) {
464 if (vm->id == -1)
465 continue;
466
467 list_for_each_entry(bo_va, &vm->va, vm_list) {
468 struct ttm_mem_reg *mem = NULL;
469 if (bo_va->valid)
470 mem = &bo_va->bo->tbo.mem;
471
472 bo_va->valid = false;
473 r = radeon_vm_bo_update_pte(rdev, vm, bo_va->bo, mem);
474 if (r) {
475 DRM_ERROR("Failed to update pte for vm %d!\n", vm->id);
476 }
477 }
478
Christian König05b07142012-08-06 20:21:10 +0200479 r = radeon_asic_vm_bind(rdev, vm, vm->id);
Christian Königc6105f22012-07-05 14:32:00 +0200480 if (r) {
481 DRM_ERROR("Failed to bind vm %d!\n", vm->id);
482 }
483 }
484 return 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500485}
486
Christian König36ff39c2012-05-09 10:07:08 +0200487/* global mutex must be lock */
Alex Deucher09db8642012-07-17 14:02:40 -0400488/**
489 * radeon_vm_unbind_locked - unbind a specific vm
490 *
491 * @rdev: radeon_device pointer
492 * @vm: vm to unbind
493 *
494 * Unbind the requested vm (cayman+).
495 * Wait for use of the VM to finish, then unbind the page table,
496 * and free the page table memory.
497 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500498static void radeon_vm_unbind_locked(struct radeon_device *rdev,
499 struct radeon_vm *vm)
500{
501 struct radeon_bo_va *bo_va;
502
503 if (vm->id == -1) {
504 return;
505 }
506
507 /* wait for vm use to end */
Christian König35e56bd2012-06-25 15:13:50 +0200508 while (vm->fence) {
509 int r;
510 r = radeon_fence_wait(vm->fence, false);
511 if (r)
512 DRM_ERROR("error while waiting for fence: %d\n", r);
513 if (r == -EDEADLK) {
514 mutex_unlock(&rdev->vm_manager.lock);
515 r = radeon_gpu_reset(rdev);
516 mutex_lock(&rdev->vm_manager.lock);
517 if (!r)
518 continue;
519 }
520 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500521 }
Christian König35e56bd2012-06-25 15:13:50 +0200522 radeon_fence_unref(&vm->fence);
Christian König9b40e5d2012-08-08 12:22:43 +0200523 radeon_fence_unref(&vm->last_flush);
Jerome Glisse721604a2012-01-05 22:11:05 -0500524
525 /* hw unbind */
Jerome Glisse721604a2012-01-05 22:11:05 -0500526 rdev->vm_manager.use_bitmap &= ~(1 << vm->id);
527 list_del_init(&vm->list);
528 vm->id = -1;
Christian König557017a2012-05-09 15:34:54 +0200529 radeon_sa_bo_free(rdev, &vm->sa_bo, NULL);
Jerome Glisse721604a2012-01-05 22:11:05 -0500530 vm->pt = NULL;
531
532 list_for_each_entry(bo_va, &vm->va, vm_list) {
533 bo_va->valid = false;
534 }
535}
536
Alex Deucher09db8642012-07-17 14:02:40 -0400537/**
538 * radeon_vm_manager_fini - tear down the vm manager
539 *
540 * @rdev: radeon_device pointer
541 *
542 * Tear down the VM manager (cayman+).
543 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500544void radeon_vm_manager_fini(struct radeon_device *rdev)
545{
Jerome Glisse721604a2012-01-05 22:11:05 -0500546 struct radeon_vm *vm, *tmp;
547
Christian Königc6105f22012-07-05 14:32:00 +0200548 if (!rdev->vm_manager.enabled)
549 return;
550
Christian König36ff39c2012-05-09 10:07:08 +0200551 mutex_lock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500552 /* unbind all active vm */
553 list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) {
554 radeon_vm_unbind_locked(rdev, vm);
555 }
Christian König05b07142012-08-06 20:21:10 +0200556 radeon_asic_vm_fini(rdev);
Christian König36ff39c2012-05-09 10:07:08 +0200557 mutex_unlock(&rdev->vm_manager.lock);
Christian Königc6105f22012-07-05 14:32:00 +0200558
559 radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager);
560 radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager);
561 rdev->vm_manager.enabled = false;
Jerome Glisse721604a2012-01-05 22:11:05 -0500562}
563
Christian König36ff39c2012-05-09 10:07:08 +0200564/* global mutex must be locked */
Alex Deucher09db8642012-07-17 14:02:40 -0400565/**
566 * radeon_vm_unbind - locked version of unbind
567 *
568 * @rdev: radeon_device pointer
569 * @vm: vm to unbind
570 *
571 * Locked version that wraps radeon_vm_unbind_locked (cayman+).
572 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500573void radeon_vm_unbind(struct radeon_device *rdev, struct radeon_vm *vm)
574{
575 mutex_lock(&vm->mutex);
576 radeon_vm_unbind_locked(rdev, vm);
577 mutex_unlock(&vm->mutex);
578}
579
Christian König36ff39c2012-05-09 10:07:08 +0200580/* global and local mutex must be locked */
Alex Deucher09db8642012-07-17 14:02:40 -0400581/**
582 * radeon_vm_bind - bind a page table to a VMID
583 *
584 * @rdev: radeon_device pointer
585 * @vm: vm to bind
586 *
587 * Bind the requested vm (cayman+).
588 * Suballocate memory for the page table, allocate a VMID
589 * and bind the page table to it, and finally start to populate
590 * the page table.
591 * Returns 0 for success, error for failure.
592 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500593int radeon_vm_bind(struct radeon_device *rdev, struct radeon_vm *vm)
594{
595 struct radeon_vm *vm_evict;
596 unsigned i;
597 int id = -1, r;
598
599 if (vm == NULL) {
600 return -EINVAL;
601 }
602
603 if (vm->id != -1) {
604 /* update lru */
605 list_del_init(&vm->list);
606 list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
607 return 0;
608 }
609
610retry:
611 r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, &vm->sa_bo,
612 RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8),
Christian König557017a2012-05-09 15:34:54 +0200613 RADEON_GPU_PAGE_SIZE, false);
Jerome Glisse721604a2012-01-05 22:11:05 -0500614 if (r) {
615 if (list_empty(&rdev->vm_manager.lru_vm)) {
616 return r;
617 }
618 vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list);
619 radeon_vm_unbind(rdev, vm_evict);
620 goto retry;
621 }
Christian König2e0d9912012-05-09 15:34:53 +0200622 vm->pt = radeon_sa_bo_cpu_addr(vm->sa_bo);
623 vm->pt_gpu_addr = radeon_sa_bo_gpu_addr(vm->sa_bo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500624 memset(vm->pt, 0, RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8));
625
626retry_id:
627 /* search for free vm */
628 for (i = 0; i < rdev->vm_manager.nvm; i++) {
629 if (!(rdev->vm_manager.use_bitmap & (1 << i))) {
630 id = i;
631 break;
632 }
633 }
634 /* evict vm if necessary */
635 if (id == -1) {
636 vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list);
637 radeon_vm_unbind(rdev, vm_evict);
638 goto retry_id;
639 }
640
641 /* do hw bind */
Christian König05b07142012-08-06 20:21:10 +0200642 r = radeon_asic_vm_bind(rdev, vm, id);
Christian König9b40e5d2012-08-08 12:22:43 +0200643 radeon_fence_unref(&vm->last_flush);
Jerome Glisse721604a2012-01-05 22:11:05 -0500644 if (r) {
Christian König557017a2012-05-09 15:34:54 +0200645 radeon_sa_bo_free(rdev, &vm->sa_bo, NULL);
Jerome Glisse721604a2012-01-05 22:11:05 -0500646 return r;
647 }
648 rdev->vm_manager.use_bitmap |= 1 << id;
649 vm->id = id;
650 list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
Jerome Glissec507f7e2012-05-09 15:34:58 +0200651 return radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo,
652 &rdev->ring_tmp_bo.bo->tbo.mem);
Jerome Glisse721604a2012-01-05 22:11:05 -0500653}
654
655/* object have to be reserved */
Alex Deucher09db8642012-07-17 14:02:40 -0400656/**
657 * radeon_vm_bo_add - add a bo to a specific vm
658 *
659 * @rdev: radeon_device pointer
660 * @vm: requested vm
661 * @bo: radeon buffer object
662 * @offset: requested offset of the buffer in the VM address space
663 * @flags: attributes of pages (read/write/valid/etc.)
664 *
665 * Add @bo into the requested vm (cayman+).
666 * Add @bo to the list of bos associated with the vm and validate
667 * the offset requested within the vm address space.
668 * Returns 0 for success, error for failure.
669 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500670int radeon_vm_bo_add(struct radeon_device *rdev,
671 struct radeon_vm *vm,
672 struct radeon_bo *bo,
673 uint64_t offset,
674 uint32_t flags)
675{
676 struct radeon_bo_va *bo_va, *tmp;
677 struct list_head *head;
678 uint64_t size = radeon_bo_size(bo), last_offset = 0;
679 unsigned last_pfn;
680
681 bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
682 if (bo_va == NULL) {
683 return -ENOMEM;
684 }
685 bo_va->vm = vm;
686 bo_va->bo = bo;
687 bo_va->soffset = offset;
688 bo_va->eoffset = offset + size;
689 bo_va->flags = flags;
690 bo_va->valid = false;
691 INIT_LIST_HEAD(&bo_va->bo_list);
692 INIT_LIST_HEAD(&bo_va->vm_list);
693 /* make sure object fit at this offset */
694 if (bo_va->soffset >= bo_va->eoffset) {
695 kfree(bo_va);
696 return -EINVAL;
697 }
698
699 last_pfn = bo_va->eoffset / RADEON_GPU_PAGE_SIZE;
700 if (last_pfn > rdev->vm_manager.max_pfn) {
701 kfree(bo_va);
702 dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
703 last_pfn, rdev->vm_manager.max_pfn);
704 return -EINVAL;
705 }
706
707 mutex_lock(&vm->mutex);
708 if (last_pfn > vm->last_pfn) {
Christian Königbb409152012-06-03 16:09:43 +0200709 /* release mutex and lock in right order */
710 mutex_unlock(&vm->mutex);
Christian König36ff39c2012-05-09 10:07:08 +0200711 mutex_lock(&rdev->vm_manager.lock);
Christian Königbb409152012-06-03 16:09:43 +0200712 mutex_lock(&vm->mutex);
713 /* and check again */
714 if (last_pfn > vm->last_pfn) {
715 /* grow va space 32M by 32M */
716 unsigned align = ((32 << 20) >> 12) - 1;
717 radeon_vm_unbind_locked(rdev, vm);
718 vm->last_pfn = (last_pfn + align) & ~align;
719 }
Christian König36ff39c2012-05-09 10:07:08 +0200720 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500721 }
722 head = &vm->va;
723 last_offset = 0;
724 list_for_each_entry(tmp, &vm->va, vm_list) {
725 if (bo_va->soffset >= last_offset && bo_va->eoffset < tmp->soffset) {
726 /* bo can be added before this one */
727 break;
728 }
729 if (bo_va->soffset >= tmp->soffset && bo_va->soffset < tmp->eoffset) {
730 /* bo and tmp overlap, invalid offset */
Jerome Glisse721604a2012-01-05 22:11:05 -0500731 dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
732 bo, (unsigned)bo_va->soffset, tmp->bo,
733 (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
Dan Carpenter55ba70c2012-01-09 15:44:50 +0300734 kfree(bo_va);
Jerome Glisse721604a2012-01-05 22:11:05 -0500735 mutex_unlock(&vm->mutex);
736 return -EINVAL;
737 }
738 last_offset = tmp->eoffset;
739 head = &tmp->vm_list;
740 }
741 list_add(&bo_va->vm_list, head);
742 list_add_tail(&bo_va->bo_list, &bo->va);
743 mutex_unlock(&vm->mutex);
744 return 0;
745}
746
Alex Deucher09db8642012-07-17 14:02:40 -0400747/**
748 * radeon_vm_get_addr - get the physical address of the page
749 *
750 * @rdev: radeon_device pointer
751 * @mem: ttm mem
752 * @pfn: pfn
753 *
754 * Look up the physical address of the page that the pte resolves
755 * to (cayman+).
756 * Returns the physical address of the page.
757 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500758static u64 radeon_vm_get_addr(struct radeon_device *rdev,
759 struct ttm_mem_reg *mem,
760 unsigned pfn)
761{
762 u64 addr = 0;
763
764 switch (mem->mem_type) {
765 case TTM_PL_VRAM:
766 addr = (mem->start << PAGE_SHIFT);
767 addr += pfn * RADEON_GPU_PAGE_SIZE;
768 addr += rdev->vm_manager.vram_base_offset;
769 break;
770 case TTM_PL_TT:
771 /* offset inside page table */
772 addr = mem->start << PAGE_SHIFT;
773 addr += pfn * RADEON_GPU_PAGE_SIZE;
774 addr = addr >> PAGE_SHIFT;
775 /* page table offset */
776 addr = rdev->gart.pages_addr[addr];
777 /* in case cpu page size != gpu page size*/
778 addr += (pfn * RADEON_GPU_PAGE_SIZE) & (~PAGE_MASK);
779 break;
780 default:
781 break;
782 }
783 return addr;
784}
785
Christian König36ff39c2012-05-09 10:07:08 +0200786/* object have to be reserved & global and local mutex must be locked */
Alex Deucher09db8642012-07-17 14:02:40 -0400787/**
788 * radeon_vm_bo_update_pte - map a bo into the vm page table
789 *
790 * @rdev: radeon_device pointer
791 * @vm: requested vm
792 * @bo: radeon buffer object
793 * @mem: ttm mem
794 *
795 * Fill in the page table entries for @bo (cayman+).
796 * Returns 0 for success, -EINVAL for failure.
797 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500798int radeon_vm_bo_update_pte(struct radeon_device *rdev,
799 struct radeon_vm *vm,
800 struct radeon_bo *bo,
801 struct ttm_mem_reg *mem)
802{
803 struct radeon_bo_va *bo_va;
804 unsigned ngpu_pages, i;
805 uint64_t addr = 0, pfn;
806 uint32_t flags;
807
808 /* nothing to do if vm isn't bound */
809 if (vm->id == -1)
Jesper Juhl04bd27a2012-02-26 23:51:53 +0100810 return 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500811
812 bo_va = radeon_bo_va(bo, vm);
813 if (bo_va == NULL) {
814 dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
815 return -EINVAL;
816 }
817
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400818 if (bo_va->valid && mem)
Jerome Glisse721604a2012-01-05 22:11:05 -0500819 return 0;
820
821 ngpu_pages = radeon_bo_ngpu_pages(bo);
822 bo_va->flags &= ~RADEON_VM_PAGE_VALID;
823 bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
824 if (mem) {
825 if (mem->mem_type != TTM_PL_SYSTEM) {
826 bo_va->flags |= RADEON_VM_PAGE_VALID;
827 bo_va->valid = true;
828 }
829 if (mem->mem_type == TTM_PL_TT) {
830 bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
831 }
832 }
833 pfn = bo_va->soffset / RADEON_GPU_PAGE_SIZE;
Christian König05b07142012-08-06 20:21:10 +0200834 flags = radeon_asic_vm_page_flags(rdev, bo_va->vm, bo_va->flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500835 for (i = 0, addr = 0; i < ngpu_pages; i++) {
836 if (mem && bo_va->valid) {
837 addr = radeon_vm_get_addr(rdev, mem, i);
838 }
Christian König05b07142012-08-06 20:21:10 +0200839 radeon_asic_vm_set_page(rdev, bo_va->vm, i + pfn, addr, flags);
Jerome Glisse721604a2012-01-05 22:11:05 -0500840 }
Christian König9b40e5d2012-08-08 12:22:43 +0200841 radeon_fence_unref(&vm->last_flush);
Jerome Glisse721604a2012-01-05 22:11:05 -0500842 return 0;
843}
844
845/* object have to be reserved */
Alex Deucher09db8642012-07-17 14:02:40 -0400846/**
847 * radeon_vm_bo_rmv - remove a bo to a specific vm
848 *
849 * @rdev: radeon_device pointer
850 * @vm: requested vm
851 * @bo: radeon buffer object
852 *
853 * Remove @bo from the requested vm (cayman+).
854 * Remove @bo from the list of bos associated with the vm and
855 * remove the ptes for @bo in the page table.
856 * Returns 0 for success.
857 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500858int radeon_vm_bo_rmv(struct radeon_device *rdev,
859 struct radeon_vm *vm,
860 struct radeon_bo *bo)
861{
862 struct radeon_bo_va *bo_va;
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400863 int r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500864
865 bo_va = radeon_bo_va(bo, vm);
866 if (bo_va == NULL)
867 return 0;
868
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400869 /* wait for va use to end */
870 while (bo_va->fence) {
871 r = radeon_fence_wait(bo_va->fence, false);
872 if (r) {
873 DRM_ERROR("error while waiting for fence: %d\n", r);
874 }
875 if (r == -EDEADLK) {
876 r = radeon_gpu_reset(rdev);
877 if (!r)
878 continue;
879 }
880 break;
881 }
882 radeon_fence_unref(&bo_va->fence);
883
Christian König36ff39c2012-05-09 10:07:08 +0200884 mutex_lock(&rdev->vm_manager.lock);
Christian Königbb409152012-06-03 16:09:43 +0200885 mutex_lock(&vm->mutex);
Jerome Glisse721604a2012-01-05 22:11:05 -0500886 radeon_vm_bo_update_pte(rdev, vm, bo, NULL);
Christian König36ff39c2012-05-09 10:07:08 +0200887 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500888 list_del(&bo_va->vm_list);
Dan Carpentera7eef882012-01-09 15:45:41 +0300889 mutex_unlock(&vm->mutex);
Sebastian Biemueller108b0d32012-02-29 11:04:52 -0500890 list_del(&bo_va->bo_list);
Jerome Glisse721604a2012-01-05 22:11:05 -0500891
892 kfree(bo_va);
893 return 0;
894}
895
Alex Deucher09db8642012-07-17 14:02:40 -0400896/**
897 * radeon_vm_bo_invalidate - mark the bo as invalid
898 *
899 * @rdev: radeon_device pointer
900 * @vm: requested vm
901 * @bo: radeon buffer object
902 *
903 * Mark @bo as invalid (cayman+).
904 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500905void radeon_vm_bo_invalidate(struct radeon_device *rdev,
906 struct radeon_bo *bo)
907{
908 struct radeon_bo_va *bo_va;
909
910 BUG_ON(!atomic_read(&bo->tbo.reserved));
911 list_for_each_entry(bo_va, &bo->va, bo_list) {
912 bo_va->valid = false;
913 }
914}
915
Alex Deucher09db8642012-07-17 14:02:40 -0400916/**
917 * radeon_vm_init - initialize a vm instance
918 *
919 * @rdev: radeon_device pointer
920 * @vm: requested vm
921 *
922 * Init @vm (cayman+).
923 * Map the IB pool and any other shared objects into the VM
924 * by default as it's used by all VMs.
925 * Returns 0 for success, error for failure.
926 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500927int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
928{
929 int r;
930
931 vm->id = -1;
932 vm->fence = NULL;
933 mutex_init(&vm->mutex);
934 INIT_LIST_HEAD(&vm->list);
935 INIT_LIST_HEAD(&vm->va);
Alex Deucherc21b3282012-06-28 17:53:07 -0400936 /* SI requires equal sized PTs for all VMs, so always set
937 * last_pfn to max_pfn. cayman allows variable sized
938 * pts so we can grow then as needed. Once we switch
939 * to two level pts we can unify this again.
940 */
941 if (rdev->family >= CHIP_TAHITI)
942 vm->last_pfn = rdev->vm_manager.max_pfn;
943 else
944 vm->last_pfn = 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500945 /* map the ib pool buffer at 0 in virtual address space, set
946 * read only
947 */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200948 r = radeon_vm_bo_add(rdev, vm, rdev->ring_tmp_bo.bo, 0,
Jerome Glisse721604a2012-01-05 22:11:05 -0500949 RADEON_VM_PAGE_READABLE | RADEON_VM_PAGE_SNOOPED);
950 return r;
951}
952
Alex Deucher09db8642012-07-17 14:02:40 -0400953/**
Dmitrii Cherkasovf59abbf2012-08-13 10:53:29 -0400954 * radeon_vm_fini - tear down a vm instance
Alex Deucher09db8642012-07-17 14:02:40 -0400955 *
956 * @rdev: radeon_device pointer
957 * @vm: requested vm
958 *
959 * Tear down @vm (cayman+).
960 * Unbind the VM and remove all bos from the vm bo list
961 */
Jerome Glisse721604a2012-01-05 22:11:05 -0500962void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
963{
964 struct radeon_bo_va *bo_va, *tmp;
965 int r;
966
Christian König36ff39c2012-05-09 10:07:08 +0200967 mutex_lock(&rdev->vm_manager.lock);
Christian Königbb409152012-06-03 16:09:43 +0200968 mutex_lock(&vm->mutex);
Jerome Glisse721604a2012-01-05 22:11:05 -0500969 radeon_vm_unbind_locked(rdev, vm);
Christian König36ff39c2012-05-09 10:07:08 +0200970 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500971
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400972 /* remove all bo at this point non are busy any more because unbind
973 * waited for the last vm fence to signal
974 */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200975 r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
Jerome Glisse721604a2012-01-05 22:11:05 -0500976 if (!r) {
Jerome Glissec507f7e2012-05-09 15:34:58 +0200977 bo_va = radeon_bo_va(rdev->ring_tmp_bo.bo, vm);
Jerome Glisse721604a2012-01-05 22:11:05 -0500978 list_del_init(&bo_va->bo_list);
979 list_del_init(&bo_va->vm_list);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400980 radeon_fence_unref(&bo_va->fence);
Jerome Glissec507f7e2012-05-09 15:34:58 +0200981 radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
Jerome Glisse721604a2012-01-05 22:11:05 -0500982 kfree(bo_va);
983 }
984 if (!list_empty(&vm->va)) {
985 dev_err(rdev->dev, "still active bo inside vm\n");
986 }
987 list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
988 list_del_init(&bo_va->vm_list);
989 r = radeon_bo_reserve(bo_va->bo, false);
990 if (!r) {
991 list_del_init(&bo_va->bo_list);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400992 radeon_fence_unref(&bo_va->fence);
Jerome Glisse721604a2012-01-05 22:11:05 -0500993 radeon_bo_unreserve(bo_va->bo);
994 kfree(bo_va);
995 }
996 }
997 mutex_unlock(&vm->mutex);
998}