2 * Copyright 2009 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 #include <ttm/ttm_bo_api.h>
33 #include <ttm/ttm_bo_driver.h>
34 #include <ttm/ttm_placement.h>
35 #include <ttm/ttm_module.h>
37 #include <drm/radeon_drm.h>
38 #include "radeon_reg.h"
41 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
43 static struct radeon_device
*radeon_get_rdev(struct ttm_bo_device
*bdev
)
45 struct radeon_mman
*mman
;
46 struct radeon_device
*rdev
;
48 mman
= container_of(bdev
, struct radeon_mman
, bdev
);
49 rdev
= container_of(mman
, struct radeon_device
, mman
);
57 static int radeon_ttm_mem_global_init(struct ttm_global_reference
*ref
)
59 return ttm_mem_global_init(ref
->object
);
62 static void radeon_ttm_mem_global_release(struct ttm_global_reference
*ref
)
64 ttm_mem_global_release(ref
->object
);
67 static int radeon_ttm_global_init(struct radeon_device
*rdev
)
69 struct ttm_global_reference
*global_ref
;
72 rdev
->mman
.mem_global_referenced
= false;
73 global_ref
= &rdev
->mman
.mem_global_ref
;
74 global_ref
->global_type
= TTM_GLOBAL_TTM_MEM
;
75 global_ref
->size
= sizeof(struct ttm_mem_global
);
76 global_ref
->init
= &radeon_ttm_mem_global_init
;
77 global_ref
->release
= &radeon_ttm_mem_global_release
;
78 r
= ttm_global_item_ref(global_ref
);
80 DRM_ERROR("Failed referencing a global TTM memory object.\n");
83 rdev
->mman
.mem_global_referenced
= true;
87 static void radeon_ttm_global_fini(struct radeon_device
*rdev
)
89 if (rdev
->mman
.mem_global_referenced
) {
90 ttm_global_item_unref(&rdev
->mman
.mem_global_ref
);
91 rdev
->mman
.mem_global_referenced
= false;
95 struct ttm_backend
*radeon_ttm_backend_create(struct radeon_device
*rdev
);
97 static struct ttm_backend
*
98 radeon_create_ttm_backend_entry(struct ttm_bo_device
*bdev
)
100 struct radeon_device
*rdev
;
102 rdev
= radeon_get_rdev(bdev
);
104 if (rdev
->flags
& RADEON_IS_AGP
) {
105 return ttm_agp_backend_init(bdev
, rdev
->ddev
->agp
->bridge
);
109 return radeon_ttm_backend_create(rdev
);
113 static int radeon_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
118 static int radeon_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
119 struct ttm_mem_type_manager
*man
)
121 struct radeon_device
*rdev
;
123 rdev
= radeon_get_rdev(bdev
);
128 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
129 man
->available_caching
= TTM_PL_MASK_CACHING
;
130 man
->default_caching
= TTM_PL_FLAG_CACHED
;
134 man
->available_caching
= TTM_PL_MASK_CACHING
;
135 man
->default_caching
= TTM_PL_FLAG_CACHED
;
136 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
| TTM_MEMTYPE_FLAG_CMA
;
138 if (rdev
->flags
& RADEON_IS_AGP
) {
139 if (!(drm_core_has_AGP(rdev
->ddev
) && rdev
->ddev
->agp
)) {
140 DRM_ERROR("AGP is not enabled for memory type %u\n",
144 man
->io_offset
= rdev
->mc
.agp_base
;
145 man
->io_size
= rdev
->mc
.gtt_size
;
147 if (!rdev
->ddev
->agp
->cant_use_aperture
)
148 man
->flags
= TTM_MEMTYPE_FLAG_NEEDS_IOREMAP
|
149 TTM_MEMTYPE_FLAG_MAPPABLE
;
150 man
->available_caching
= TTM_PL_FLAG_UNCACHED
|
152 man
->default_caching
= TTM_PL_FLAG_WC
;
162 /* "On-card" video ram */
164 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
165 TTM_MEMTYPE_FLAG_NEEDS_IOREMAP
|
166 TTM_MEMTYPE_FLAG_MAPPABLE
;
167 man
->available_caching
= TTM_PL_FLAG_UNCACHED
| TTM_PL_FLAG_WC
;
168 man
->default_caching
= TTM_PL_FLAG_WC
;
170 man
->io_offset
= rdev
->mc
.aper_base
;
171 man
->io_size
= rdev
->mc
.aper_size
;
174 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
180 static uint32_t radeon_evict_flags(struct ttm_buffer_object
*bo
)
182 uint32_t cur_placement
= bo
->mem
.placement
& ~TTM_PL_MASK_MEMTYPE
;
184 switch (bo
->mem
.mem_type
) {
186 return (cur_placement
& ~TTM_PL_MASK_CACHING
) |
192 static int radeon_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
197 static void radeon_move_null(struct ttm_buffer_object
*bo
,
198 struct ttm_mem_reg
*new_mem
)
200 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
202 BUG_ON(old_mem
->mm_node
!= NULL
);
204 new_mem
->mm_node
= NULL
;
207 static int radeon_move_blit(struct ttm_buffer_object
*bo
,
208 bool evict
, int no_wait
,
209 struct ttm_mem_reg
*new_mem
,
210 struct ttm_mem_reg
*old_mem
)
212 struct radeon_device
*rdev
;
213 uint64_t old_start
, new_start
;
214 struct radeon_fence
*fence
;
217 rdev
= radeon_get_rdev(bo
->bdev
);
218 r
= radeon_fence_create(rdev
, &fence
);
222 old_start
= old_mem
->mm_node
->start
<< PAGE_SHIFT
;
223 new_start
= new_mem
->mm_node
->start
<< PAGE_SHIFT
;
225 switch (old_mem
->mem_type
) {
227 old_start
+= rdev
->mc
.vram_location
;
230 old_start
+= rdev
->mc
.gtt_location
;
233 DRM_ERROR("Unknown placement %d\n", old_mem
->mem_type
);
236 switch (new_mem
->mem_type
) {
238 new_start
+= rdev
->mc
.vram_location
;
241 new_start
+= rdev
->mc
.gtt_location
;
244 DRM_ERROR("Unknown placement %d\n", old_mem
->mem_type
);
247 if (!rdev
->cp
.ready
) {
248 DRM_ERROR("Trying to move memory with CP turned off.\n");
251 r
= radeon_copy(rdev
, old_start
, new_start
, new_mem
->num_pages
, fence
);
252 /* FIXME: handle copy error */
253 r
= ttm_bo_move_accel_cleanup(bo
, (void *)fence
, NULL
,
254 evict
, no_wait
, new_mem
);
255 radeon_fence_unref(&fence
);
259 static int radeon_move_vram_ram(struct ttm_buffer_object
*bo
,
260 bool evict
, bool interruptible
, bool no_wait
,
261 struct ttm_mem_reg
*new_mem
)
263 struct radeon_device
*rdev
;
264 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
265 struct ttm_mem_reg tmp_mem
;
266 uint32_t proposed_placement
;
269 rdev
= radeon_get_rdev(bo
->bdev
);
271 tmp_mem
.mm_node
= NULL
;
272 proposed_placement
= TTM_PL_FLAG_TT
| TTM_PL_MASK_CACHING
;
273 r
= ttm_bo_mem_space(bo
, proposed_placement
, &tmp_mem
,
274 interruptible
, no_wait
);
278 r
= ttm_tt_bind(bo
->ttm
, &tmp_mem
);
282 r
= radeon_move_blit(bo
, true, no_wait
, &tmp_mem
, old_mem
);
286 r
= ttm_bo_move_ttm(bo
, true, no_wait
, new_mem
);
288 if (tmp_mem
.mm_node
) {
289 spin_lock(&rdev
->mman
.bdev
.lru_lock
);
290 drm_mm_put_block(tmp_mem
.mm_node
);
291 spin_unlock(&rdev
->mman
.bdev
.lru_lock
);
297 static int radeon_move_ram_vram(struct ttm_buffer_object
*bo
,
298 bool evict
, bool interruptible
, bool no_wait
,
299 struct ttm_mem_reg
*new_mem
)
301 struct radeon_device
*rdev
;
302 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
303 struct ttm_mem_reg tmp_mem
;
304 uint32_t proposed_flags
;
307 rdev
= radeon_get_rdev(bo
->bdev
);
309 tmp_mem
.mm_node
= NULL
;
310 proposed_flags
= TTM_PL_FLAG_TT
| TTM_PL_MASK_CACHING
;
311 r
= ttm_bo_mem_space(bo
, proposed_flags
, &tmp_mem
,
312 interruptible
, no_wait
);
316 r
= ttm_bo_move_ttm(bo
, true, no_wait
, &tmp_mem
);
320 r
= radeon_move_blit(bo
, true, no_wait
, new_mem
, old_mem
);
325 if (tmp_mem
.mm_node
) {
326 spin_lock(&rdev
->mman
.bdev
.lru_lock
);
327 drm_mm_put_block(tmp_mem
.mm_node
);
328 spin_unlock(&rdev
->mman
.bdev
.lru_lock
);
334 static int radeon_bo_move(struct ttm_buffer_object
*bo
,
335 bool evict
, bool interruptible
, bool no_wait
,
336 struct ttm_mem_reg
*new_mem
)
338 struct radeon_device
*rdev
;
339 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
342 rdev
= radeon_get_rdev(bo
->bdev
);
343 if (old_mem
->mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
344 radeon_move_null(bo
, new_mem
);
347 if ((old_mem
->mem_type
== TTM_PL_TT
&&
348 new_mem
->mem_type
== TTM_PL_SYSTEM
) ||
349 (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
350 new_mem
->mem_type
== TTM_PL_TT
)) {
351 /* bind is enought */
352 radeon_move_null(bo
, new_mem
);
355 if (!rdev
->cp
.ready
) {
357 DRM_ERROR("CP is not ready use memcpy.\n");
358 return ttm_bo_move_memcpy(bo
, evict
, no_wait
, new_mem
);
361 if (old_mem
->mem_type
== TTM_PL_VRAM
&&
362 new_mem
->mem_type
== TTM_PL_SYSTEM
) {
363 return radeon_move_vram_ram(bo
, evict
, interruptible
,
365 } else if (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
366 new_mem
->mem_type
== TTM_PL_VRAM
) {
367 return radeon_move_ram_vram(bo
, evict
, interruptible
,
370 r
= radeon_move_blit(bo
, evict
, no_wait
, new_mem
, old_mem
);
378 const uint32_t radeon_mem_prios
[] = {
384 const uint32_t radeon_busy_prios
[] = {
390 static int radeon_sync_obj_wait(void *sync_obj
, void *sync_arg
,
391 bool lazy
, bool interruptible
)
393 return radeon_fence_wait((struct radeon_fence
*)sync_obj
, interruptible
);
396 static int radeon_sync_obj_flush(void *sync_obj
, void *sync_arg
)
401 static void radeon_sync_obj_unref(void **sync_obj
)
403 radeon_fence_unref((struct radeon_fence
**)sync_obj
);
406 static void *radeon_sync_obj_ref(void *sync_obj
)
408 return radeon_fence_ref((struct radeon_fence
*)sync_obj
);
411 static bool radeon_sync_obj_signaled(void *sync_obj
, void *sync_arg
)
413 return radeon_fence_signaled((struct radeon_fence
*)sync_obj
);
416 static struct ttm_bo_driver radeon_bo_driver
= {
417 .mem_type_prio
= radeon_mem_prios
,
418 .mem_busy_prio
= radeon_busy_prios
,
419 .num_mem_type_prio
= ARRAY_SIZE(radeon_mem_prios
),
420 .num_mem_busy_prio
= ARRAY_SIZE(radeon_busy_prios
),
421 .create_ttm_backend_entry
= &radeon_create_ttm_backend_entry
,
422 .invalidate_caches
= &radeon_invalidate_caches
,
423 .init_mem_type
= &radeon_init_mem_type
,
424 .evict_flags
= &radeon_evict_flags
,
425 .move
= &radeon_bo_move
,
426 .verify_access
= &radeon_verify_access
,
427 .sync_obj_signaled
= &radeon_sync_obj_signaled
,
428 .sync_obj_wait
= &radeon_sync_obj_wait
,
429 .sync_obj_flush
= &radeon_sync_obj_flush
,
430 .sync_obj_unref
= &radeon_sync_obj_unref
,
431 .sync_obj_ref
= &radeon_sync_obj_ref
,
434 int radeon_ttm_init(struct radeon_device
*rdev
)
438 r
= radeon_ttm_global_init(rdev
);
442 /* No others user of address space so set it to 0 */
443 r
= ttm_bo_device_init(&rdev
->mman
.bdev
,
444 rdev
->mman
.mem_global_ref
.object
,
445 &radeon_bo_driver
, DRM_FILE_PAGE_OFFSET
);
447 DRM_ERROR("failed initializing buffer object driver(%d).\n", r
);
450 r
= ttm_bo_init_mm(&rdev
->mman
.bdev
, TTM_PL_VRAM
, 0,
451 ((rdev
->mc
.aper_size
) >> PAGE_SHIFT
));
453 DRM_ERROR("Failed initializing VRAM heap.\n");
456 r
= radeon_object_create(rdev
, NULL
, 256 * 1024, true,
457 RADEON_GEM_DOMAIN_VRAM
, false,
458 &rdev
->stollen_vga_memory
);
462 r
= radeon_object_pin(rdev
->stollen_vga_memory
, RADEON_GEM_DOMAIN_VRAM
, NULL
);
464 radeon_object_unref(&rdev
->stollen_vga_memory
);
467 DRM_INFO("radeon: %uM of VRAM memory ready\n",
468 rdev
->mc
.vram_size
/ (1024 * 1024));
469 r
= ttm_bo_init_mm(&rdev
->mman
.bdev
, TTM_PL_TT
, 0,
470 ((rdev
->mc
.gtt_size
) >> PAGE_SHIFT
));
472 DRM_ERROR("Failed initializing GTT heap.\n");
475 DRM_INFO("radeon: %uM of GTT memory ready.\n",
476 rdev
->mc
.gtt_size
/ (1024 * 1024));
477 if (unlikely(rdev
->mman
.bdev
.dev_mapping
== NULL
)) {
478 rdev
->mman
.bdev
.dev_mapping
= rdev
->ddev
->dev_mapping
;
483 void radeon_ttm_fini(struct radeon_device
*rdev
)
485 if (rdev
->stollen_vga_memory
) {
486 radeon_object_unpin(rdev
->stollen_vga_memory
);
487 radeon_object_unref(&rdev
->stollen_vga_memory
);
489 ttm_bo_clean_mm(&rdev
->mman
.bdev
, TTM_PL_VRAM
);
490 ttm_bo_clean_mm(&rdev
->mman
.bdev
, TTM_PL_TT
);
491 ttm_bo_device_release(&rdev
->mman
.bdev
);
492 radeon_gart_fini(rdev
);
493 radeon_ttm_global_fini(rdev
);
494 DRM_INFO("radeon: ttm finalized\n");
497 static struct vm_operations_struct radeon_ttm_vm_ops
;
498 static struct vm_operations_struct
*ttm_vm_ops
= NULL
;
500 static int radeon_ttm_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
502 struct ttm_buffer_object
*bo
;
505 bo
= (struct ttm_buffer_object
*)vma
->vm_private_data
;
507 return VM_FAULT_NOPAGE
;
509 r
= ttm_vm_ops
->fault(vma
, vmf
);
513 int radeon_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
515 struct drm_file
*file_priv
;
516 struct radeon_device
*rdev
;
519 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
)) {
520 return drm_mmap(filp
, vma
);
523 file_priv
= (struct drm_file
*)filp
->private_data
;
524 rdev
= file_priv
->minor
->dev
->dev_private
;
528 r
= ttm_bo_mmap(filp
, vma
, &rdev
->mman
.bdev
);
529 if (unlikely(r
!= 0)) {
532 if (unlikely(ttm_vm_ops
== NULL
)) {
533 ttm_vm_ops
= vma
->vm_ops
;
534 radeon_ttm_vm_ops
= *ttm_vm_ops
;
535 radeon_ttm_vm_ops
.fault
= &radeon_ttm_fault
;
537 vma
->vm_ops
= &radeon_ttm_vm_ops
;
543 * TTM backend functions.
545 struct radeon_ttm_backend
{
546 struct ttm_backend backend
;
547 struct radeon_device
*rdev
;
548 unsigned long num_pages
;
550 struct page
*dummy_read_page
;
556 static int radeon_ttm_backend_populate(struct ttm_backend
*backend
,
557 unsigned long num_pages
,
559 struct page
*dummy_read_page
)
561 struct radeon_ttm_backend
*gtt
;
563 gtt
= container_of(backend
, struct radeon_ttm_backend
, backend
);
565 gtt
->num_pages
= num_pages
;
566 gtt
->dummy_read_page
= dummy_read_page
;
567 gtt
->populated
= true;
571 static void radeon_ttm_backend_clear(struct ttm_backend
*backend
)
573 struct radeon_ttm_backend
*gtt
;
575 gtt
= container_of(backend
, struct radeon_ttm_backend
, backend
);
578 gtt
->dummy_read_page
= NULL
;
579 gtt
->populated
= false;
584 static int radeon_ttm_backend_bind(struct ttm_backend
*backend
,
585 struct ttm_mem_reg
*bo_mem
)
587 struct radeon_ttm_backend
*gtt
;
590 gtt
= container_of(backend
, struct radeon_ttm_backend
, backend
);
591 gtt
->offset
= bo_mem
->mm_node
->start
<< PAGE_SHIFT
;
592 if (!gtt
->num_pages
) {
593 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n", gtt
->num_pages
, bo_mem
, backend
);
595 r
= radeon_gart_bind(gtt
->rdev
, gtt
->offset
,
596 gtt
->num_pages
, gtt
->pages
);
598 DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
599 gtt
->num_pages
, gtt
->offset
);
606 static int radeon_ttm_backend_unbind(struct ttm_backend
*backend
)
608 struct radeon_ttm_backend
*gtt
;
610 gtt
= container_of(backend
, struct radeon_ttm_backend
, backend
);
611 radeon_gart_unbind(gtt
->rdev
, gtt
->offset
, gtt
->num_pages
);
616 static void radeon_ttm_backend_destroy(struct ttm_backend
*backend
)
618 struct radeon_ttm_backend
*gtt
;
620 gtt
= container_of(backend
, struct radeon_ttm_backend
, backend
);
622 radeon_ttm_backend_unbind(backend
);
627 static struct ttm_backend_func radeon_backend_func
= {
628 .populate
= &radeon_ttm_backend_populate
,
629 .clear
= &radeon_ttm_backend_clear
,
630 .bind
= &radeon_ttm_backend_bind
,
631 .unbind
= &radeon_ttm_backend_unbind
,
632 .destroy
= &radeon_ttm_backend_destroy
,
635 struct ttm_backend
*radeon_ttm_backend_create(struct radeon_device
*rdev
)
637 struct radeon_ttm_backend
*gtt
;
639 gtt
= kzalloc(sizeof(struct radeon_ttm_backend
), GFP_KERNEL
);
643 gtt
->backend
.bdev
= &rdev
->mman
.bdev
;
644 gtt
->backend
.flags
= 0;
645 gtt
->backend
.func
= &radeon_backend_func
;
649 gtt
->dummy_read_page
= NULL
;
650 gtt
->populated
= false;
652 return >t
->backend
;