xref: /linux/drivers/gpu/drm/xe/compat-i915-headers/gem/i915_gem_stolen.h (revision 6315d93541f8a5f77c5ef5c4f25233e66d189603)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #ifndef _I915_GEM_STOLEN_H_
7 #define _I915_GEM_STOLEN_H_
8 
9 #include "xe_ttm_stolen_mgr.h"
10 #include "xe_res_cursor.h"
11 
12 struct xe_bo;
13 
14 struct i915_stolen_fb {
15 	struct xe_bo *bo;
16 };
17 
18 static inline int i915_gem_stolen_insert_node_in_range(struct xe_device *xe,
19 						       struct i915_stolen_fb *fb,
20 						       u32 size, u32 align,
21 						       u32 start, u32 end)
22 {
23 	struct xe_bo *bo;
24 	int err;
25 	u32 flags = XE_BO_FLAG_PINNED | XE_BO_FLAG_STOLEN;
26 
27 	if (start < SZ_4K)
28 		start = SZ_4K;
29 
30 	if (align) {
31 		size = ALIGN(size, align);
32 		start = ALIGN(start, align);
33 	}
34 
35 	bo = xe_bo_create_locked_range(xe, xe_device_get_root_tile(xe),
36 				       NULL, size, start, end,
37 				       ttm_bo_type_kernel, flags, 0);
38 	if (IS_ERR(bo)) {
39 		err = PTR_ERR(bo);
40 		bo = NULL;
41 		return err;
42 	}
43 	err = xe_bo_pin(bo);
44 	xe_bo_unlock_vm_held(bo);
45 
46 	if (err) {
47 		xe_bo_put(fb->bo);
48 		bo = NULL;
49 	}
50 
51 	fb->bo = bo;
52 
53 	return err;
54 }
55 
56 static inline int i915_gem_stolen_insert_node(struct xe_device *xe,
57 					      struct i915_stolen_fb *fb,
58 					      u32 size, u32 align)
59 {
60 	/* Not used on xe */
61 	BUG_ON(1);
62 	return -ENODEV;
63 }
64 
65 static inline void i915_gem_stolen_remove_node(struct xe_device *xe,
66 					       struct i915_stolen_fb *fb)
67 {
68 	xe_bo_unpin_map_no_vm(fb->bo);
69 	fb->bo = NULL;
70 }
71 
72 #define i915_gem_stolen_initialized(xe) (!!ttm_manager_type(&(xe)->ttm, XE_PL_STOLEN))
73 #define i915_gem_stolen_node_allocated(fb) (!!((fb)->bo))
74 
75 static inline u32 i915_gem_stolen_node_offset(struct i915_stolen_fb *fb)
76 {
77 	struct xe_res_cursor res;
78 
79 	xe_res_first(fb->bo->ttm.resource, 0, 4096, &res);
80 	return res.start;
81 }
82 
83 /* Used for < gen4. These are not supported by Xe */
84 #define i915_gem_stolen_area_address(xe) (!WARN_ON(1))
85 /* Used for gen9 specific WA. Gen9 is not supported by Xe */
86 #define i915_gem_stolen_area_size(xe) (!WARN_ON(1))
87 
88 #define i915_gem_stolen_node_address(xe, fb) (xe_ttm_stolen_gpu_offset(xe) + \
89 					 i915_gem_stolen_node_offset(fb))
90 #define i915_gem_stolen_node_size(fb) ((u64)((fb)->bo->ttm.base.size))
91 
92 #endif
93