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