1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2023 Loongson Technology Corporation Limited
4  */
5 
6 #ifndef __LSDC_TTM_H__
7 #define __LSDC_TTM_H__
8 
9 #include <linux/container_of.h>
10 #include <linux/iosys-map.h>
11 #include <linux/list.h>
12 
13 #include <drm/drm_gem.h>
14 #include <drm/ttm/ttm_bo.h>
15 #include <drm/ttm/ttm_placement.h>
16 #include <drm/ttm/ttm_range_manager.h>
17 #include <drm/ttm/ttm_tt.h>
18 
19 #define LSDC_GEM_DOMAIN_SYSTEM          0x1
20 #define LSDC_GEM_DOMAIN_GTT             0x2
21 #define LSDC_GEM_DOMAIN_VRAM            0x4
22 
23 struct lsdc_bo {
24 	struct ttm_buffer_object tbo;
25 
26 	/* Protected by gem.mutex */
27 	struct list_head list;
28 
29 	struct iosys_map map;
30 
31 	unsigned int vmap_count;
32 	/* cross device driver sharing reference count */
33 	unsigned int sharing_count;
34 
35 	struct ttm_bo_kmap_obj kmap;
36 	void *kptr;
37 	bool is_iomem;
38 
39 	size_t size;
40 
41 	u32 initial_domain;
42 
43 	struct ttm_placement placement;
44 	struct ttm_place placements[4];
45 };
46 
47 static inline struct ttm_buffer_object *to_ttm_bo(struct drm_gem_object *gem)
48 {
49 	return container_of(gem, struct ttm_buffer_object, base);
50 }
51 
52 static inline struct lsdc_bo *to_lsdc_bo(struct ttm_buffer_object *tbo)
53 {
54 	return container_of(tbo, struct lsdc_bo, tbo);
55 }
56 
57 static inline struct lsdc_bo *gem_to_lsdc_bo(struct drm_gem_object *gem)
58 {
59 	return container_of(gem, struct lsdc_bo, tbo.base);
60 }
61 
62 const char *lsdc_mem_type_to_str(uint32_t mem_type);
63 const char *lsdc_domain_to_str(u32 domain);
64 
65 struct lsdc_bo *lsdc_bo_create(struct drm_device *ddev,
66 			       u32 domain,
67 			       size_t size,
68 			       bool kernel,
69 			       struct sg_table *sg,
70 			       struct dma_resv *resv);
71 
72 struct lsdc_bo *lsdc_bo_create_kernel_pinned(struct drm_device *ddev,
73 					     u32 domain,
74 					     size_t size);
75 
76 void lsdc_bo_free_kernel_pinned(struct lsdc_bo *lbo);
77 
78 int lsdc_bo_reserve(struct lsdc_bo *lbo);
79 void lsdc_bo_unreserve(struct lsdc_bo *lbo);
80 
81 int lsdc_bo_pin(struct lsdc_bo *lbo, u32 domain, u64 *gpu_addr);
82 void lsdc_bo_unpin(struct lsdc_bo *lbo);
83 
84 void lsdc_bo_ref(struct lsdc_bo *lbo);
85 void lsdc_bo_unref(struct lsdc_bo *lbo);
86 
87 u64 lsdc_bo_gpu_offset(struct lsdc_bo *lbo);
88 size_t lsdc_bo_size(struct lsdc_bo *lbo);
89 
90 int lsdc_bo_kmap(struct lsdc_bo *lbo);
91 void lsdc_bo_kunmap(struct lsdc_bo *lbo);
92 void lsdc_bo_clear(struct lsdc_bo *lbo);
93 
94 int lsdc_bo_evict_vram(struct drm_device *ddev);
95 
96 int lsdc_ttm_init(struct lsdc_device *ldev);
97 void lsdc_ttm_debugfs_init(struct lsdc_device *ldev);
98 
99 #endif
100