xref: /linux/drivers/gpu/drm/xe/xe_sa.h (revision f9f0b4a1f35d39a1a2a2f8ec46eb7b81efc70a63)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 #ifndef _XE_SA_H_
6 #define _XE_SA_H_
7 
8 #include <linux/sizes.h>
9 #include <linux/types.h>
10 
11 #include "xe_bo.h"
12 #include "xe_sa_types.h"
13 
14 struct dma_fence;
15 struct xe_tile;
16 
17 #define XE_SA_BO_MANAGER_FLAG_SHADOW	BIT(0)
18 struct xe_sa_manager *__xe_sa_bo_manager_init(struct xe_tile *tile, u32 size,
19 					      u32 guard, u32 align, u32 flags);
20 struct drm_suballoc *__xe_sa_bo_new(struct xe_sa_manager *sa_manager, u32 size, gfp_t gfp);
21 
22 static inline struct xe_sa_manager *xe_sa_bo_manager_init(struct xe_tile *tile, u32 size, u32 align)
23 {
24 	return __xe_sa_bo_manager_init(tile, size, SZ_4K, align, 0);
25 }
26 
27 /**
28  * xe_sa_bo_new() - Make a suballocation.
29  * @sa_manager: the &xe_sa_manager
30  * @size: number of bytes we want to suballocate
31  *
32  * Try to make a suballocation of size @size.
33  *
34  * Return: a &drm_suballoc, or an ERR_PTR.
35  */
36 static inline struct drm_suballoc *xe_sa_bo_new(struct xe_sa_manager *sa_manager, u32 size)
37 {
38 	return __xe_sa_bo_new(sa_manager, size, GFP_KERNEL);
39 }
40 
41 struct drm_suballoc *xe_sa_bo_alloc(gfp_t gfp);
42 int xe_sa_bo_init(struct xe_sa_manager *sa_manager, struct drm_suballoc *sa, size_t size);
43 void xe_sa_bo_flush_write(struct drm_suballoc *sa_bo);
44 void xe_sa_bo_sync_read(struct drm_suballoc *sa_bo);
45 void xe_sa_bo_free(struct drm_suballoc *sa_bo, struct dma_fence *fence);
46 
47 static inline struct xe_sa_manager *
48 to_xe_sa_manager(struct drm_suballoc_manager *mng)
49 {
50 	return container_of(mng, struct xe_sa_manager, base);
51 }
52 
53 /**
54  * xe_sa_manager_gpu_addr - Retrieve GPU address of a back storage BO
55  * within suballocator.
56  * @sa_manager: the &xe_sa_manager struct instance
57  * Return: GGTT address of the back storage BO.
58  */
59 static inline u64 xe_sa_manager_gpu_addr(struct xe_sa_manager *sa_manager)
60 {
61 	return xe_bo_ggtt_addr(sa_manager->bo);
62 }
63 
64 static inline u64 xe_sa_bo_gpu_addr(struct drm_suballoc *sa)
65 {
66 	return xe_sa_manager_gpu_addr(to_xe_sa_manager(sa->manager)) +
67 		drm_suballoc_soffset(sa);
68 }
69 
70 static inline void *xe_sa_bo_cpu_addr(struct drm_suballoc *sa)
71 {
72 	return to_xe_sa_manager(sa->manager)->cpu_ptr +
73 		drm_suballoc_soffset(sa);
74 }
75 
76 void xe_sa_bo_swap_shadow(struct xe_sa_manager *sa_manager);
77 void xe_sa_bo_sync_shadow(struct drm_suballoc *sa_bo);
78 
79 /**
80  * xe_sa_bo_swap_guard() - Retrieve the SA BO swap guard within sub-allocator.
81  * @sa_manager: the &xe_sa_manager
82  *
83  * Return: Sub alloctor swap guard mutex.
84  */
85 static inline struct mutex *xe_sa_bo_swap_guard(struct xe_sa_manager *sa_manager)
86 {
87 	return &sa_manager->swap_guard;
88 }
89 
90 #endif
91