summaryrefslogtreecommitdiff
path: root/drivers/accel/amdxdna/amdxdna_gem.h
blob: 1e90e32bf3cd79d952c035fd44a83d84779ea2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2024, Advanced Micro Devices, Inc.
 */

#ifndef _AMDXDNA_GEM_H_
#define _AMDXDNA_GEM_H_

#include <drm/drm_gem_shmem_helper.h>
#include <linux/hmm.h>
#include <linux/iova.h>
#include "amdxdna_pci_drv.h"

struct amdxdna_umap {
	struct mmu_interval_notifier	notifier;
	struct hmm_range		range;
	struct work_struct		hmm_unreg_work;
	struct amdxdna_gem_obj		*abo;
	struct list_head		node;
	struct kref			refcnt;
	bool				invalid;
	bool				unmapped;
};

struct amdxdna_mem {
	void				*kva;
	u64				dma_addr;
	size_t				size;
	struct list_head		umap_list;
	bool				map_invalid;
	/*
	 * Cache the first mmap uva as PASID addr, which can be accessed by driver
	 * without taking notifier_lock.
	 */
	u64				uva;
};

struct amdxdna_gem_obj {
	struct drm_gem_shmem_object	base;
	struct amdxdna_client		*client;
	u8				type;
	bool				pinned;
	struct mutex			lock; /* Protects: pinned, mem.kva, open_ref */
	struct amdxdna_mem		mem;
	int				open_ref;

	/* Below members are initialized when needed */
	struct drm_mm_node		mm_node; /* For AMDXDNA_BO_DEV */
	u32				heap_start_id;
	u32				heap_end_id;
	u64				dev_addr; /* For heap bo */
	u32				assigned_hwctx;
	struct dma_buf			*dma_buf;
	struct dma_buf_attachment	*attach;

	/* True, if BO is managed by XRT, not application */
	bool				internal;
	/* True, if BO is not exportable */
	bool				private_buffer;
};

#define to_gobj(obj)    (&(obj)->base.base)
#define is_import_bo(obj) ((obj)->attach)

static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj)
{
	return container_of(gobj, struct amdxdna_gem_obj, base.base);
}

struct amdxdna_gem_obj *amdxdna_gem_get_obj(struct amdxdna_client *client,
					    u32 bo_hdl, u8 bo_type);
static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
{
	drm_gem_object_put(to_gobj(abo));
}

/*
 * Obtain the user virtual address for accessing the BO.
 * It can be used for device to access the BO when PASID is enabled.
 */
static inline u64 amdxdna_gem_uva(struct amdxdna_gem_obj *abo)
{
	return abo->mem.uva;
}

void *amdxdna_gem_vmap(struct amdxdna_gem_obj *abo);
u64 amdxdna_gem_dev_addr(struct amdxdna_gem_obj *abo);

static inline u64 amdxdna_dev_bo_offset(struct amdxdna_gem_obj *abo)
{
	return amdxdna_gem_dev_addr(abo) - to_xdna_dev(to_gobj(abo)->dev)->dev_info->dev_mem_base;
}

static inline u64 amdxdna_obj_dma_addr(struct amdxdna_gem_obj *abo)
{
	/*
	 * amdxdna_gem_obj_open() calls amdxdna_dma_map_bo() only when PASID is
	 * off, leaving mem.dma_addr at AMDXDNA_INVALID_ADDR when PASID is on.
	 * Avoid dereferencing abo->client, which is cleared to NULL by
	 * amdxdna_gem_obj_close() while internal kernel references remain.
	 */
	return (abo->mem.dma_addr != AMDXDNA_INVALID_ADDR) ?
		abo->mem.dma_addr : amdxdna_gem_uva(abo);
}

void amdxdna_umap_put(struct amdxdna_umap *mapp);

struct drm_gem_object *
amdxdna_gem_create_shmem_object_cb(struct drm_device *dev, size_t size);
struct drm_gem_object *
amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
struct amdxdna_gem_obj *
amdxdna_drm_create_dev_bo(struct drm_device *dev,
			  struct amdxdna_drm_create_bo *args, struct drm_file *filp);

int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo);
int amdxdna_gem_pin(struct amdxdna_gem_obj *abo);
void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo);

int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
int amdxdna_drm_get_bo_usage(struct drm_device *dev, struct amdxdna_drm_get_array *args);

#endif /* _AMDXDNA_GEM_H_ */