1d722e3fbSopenharmony_ci/*
2d722e3fbSopenharmony_ci * Copyright © 2014 Advanced Micro Devices, Inc.
3d722e3fbSopenharmony_ci * All Rights Reserved.
4d722e3fbSopenharmony_ci *
5d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation
8d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11d722e3fbSopenharmony_ci *
12d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in
13d722e3fbSopenharmony_ci * all copies or substantial portions of the Software.
14d722e3fbSopenharmony_ci *
15d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
22d722e3fbSopenharmony_ci *
23d722e3fbSopenharmony_ci */
24d722e3fbSopenharmony_ci
25d722e3fbSopenharmony_ci#ifndef _AMDGPU_INTERNAL_H_
26d722e3fbSopenharmony_ci#define _AMDGPU_INTERNAL_H_
27d722e3fbSopenharmony_ci
28d722e3fbSopenharmony_ci#include <assert.h>
29d722e3fbSopenharmony_ci#include <pthread.h>
30d722e3fbSopenharmony_ci
31d722e3fbSopenharmony_ci#include "libdrm_macros.h"
32d722e3fbSopenharmony_ci#include "xf86atomic.h"
33d722e3fbSopenharmony_ci#include "amdgpu.h"
34d722e3fbSopenharmony_ci#include "util_double_list.h"
35d722e3fbSopenharmony_ci#include "handle_table.h"
36d722e3fbSopenharmony_ci
37d722e3fbSopenharmony_ci#define AMDGPU_CS_MAX_RINGS 8
38d722e3fbSopenharmony_ci/* do not use below macro if b is not power of 2 aligned value */
39d722e3fbSopenharmony_ci#define __round_mask(x, y) ((__typeof__(x))((y)-1))
40d722e3fbSopenharmony_ci#define ROUND_UP(x, y) ((((x)-1) | __round_mask(x, y))+1)
41d722e3fbSopenharmony_ci#define ROUND_DOWN(x, y) ((x) & ~__round_mask(x, y))
42d722e3fbSopenharmony_ci
43d722e3fbSopenharmony_ci#define AMDGPU_INVALID_VA_ADDRESS	0xffffffffffffffff
44d722e3fbSopenharmony_ci#define AMDGPU_NULL_SUBMIT_SEQ		0
45d722e3fbSopenharmony_ci
46d722e3fbSopenharmony_cistruct amdgpu_bo_va_hole {
47d722e3fbSopenharmony_ci	struct list_head list;
48d722e3fbSopenharmony_ci	uint64_t offset;
49d722e3fbSopenharmony_ci	uint64_t size;
50d722e3fbSopenharmony_ci};
51d722e3fbSopenharmony_ci
52d722e3fbSopenharmony_cistruct amdgpu_bo_va_mgr {
53d722e3fbSopenharmony_ci	uint64_t va_max;
54d722e3fbSopenharmony_ci	struct list_head va_holes;
55d722e3fbSopenharmony_ci	pthread_mutex_t bo_va_mutex;
56d722e3fbSopenharmony_ci	uint32_t va_alignment;
57d722e3fbSopenharmony_ci};
58d722e3fbSopenharmony_ci
59d722e3fbSopenharmony_cistruct amdgpu_va {
60d722e3fbSopenharmony_ci	amdgpu_device_handle dev;
61d722e3fbSopenharmony_ci	uint64_t address;
62d722e3fbSopenharmony_ci	uint64_t size;
63d722e3fbSopenharmony_ci	enum amdgpu_gpu_va_range range;
64d722e3fbSopenharmony_ci	struct amdgpu_bo_va_mgr *vamgr;
65d722e3fbSopenharmony_ci};
66d722e3fbSopenharmony_ci
67d722e3fbSopenharmony_cistruct amdgpu_device {
68d722e3fbSopenharmony_ci	atomic_t refcount;
69d722e3fbSopenharmony_ci	struct amdgpu_device *next;
70d722e3fbSopenharmony_ci	int fd;
71d722e3fbSopenharmony_ci	int flink_fd;
72d722e3fbSopenharmony_ci	unsigned major_version;
73d722e3fbSopenharmony_ci	unsigned minor_version;
74d722e3fbSopenharmony_ci
75d722e3fbSopenharmony_ci	char *marketing_name;
76d722e3fbSopenharmony_ci	/** List of buffer handles. Protected by bo_table_mutex. */
77d722e3fbSopenharmony_ci	struct handle_table bo_handles;
78d722e3fbSopenharmony_ci	/** List of buffer GEM flink names. Protected by bo_table_mutex. */
79d722e3fbSopenharmony_ci	struct handle_table bo_flink_names;
80d722e3fbSopenharmony_ci	/** This protects all hash tables. */
81d722e3fbSopenharmony_ci	pthread_mutex_t bo_table_mutex;
82d722e3fbSopenharmony_ci	struct drm_amdgpu_info_device dev_info;
83d722e3fbSopenharmony_ci	struct amdgpu_gpu_info info;
84d722e3fbSopenharmony_ci	/** The VA manager for the lower virtual address space */
85d722e3fbSopenharmony_ci	struct amdgpu_bo_va_mgr vamgr;
86d722e3fbSopenharmony_ci	/** The VA manager for the 32bit address space */
87d722e3fbSopenharmony_ci	struct amdgpu_bo_va_mgr vamgr_32;
88d722e3fbSopenharmony_ci	/** The VA manager for the high virtual address space */
89d722e3fbSopenharmony_ci	struct amdgpu_bo_va_mgr vamgr_high;
90d722e3fbSopenharmony_ci	/** The VA manager for the 32bit high address space */
91d722e3fbSopenharmony_ci	struct amdgpu_bo_va_mgr vamgr_high_32;
92d722e3fbSopenharmony_ci};
93d722e3fbSopenharmony_ci
94d722e3fbSopenharmony_cistruct amdgpu_bo {
95d722e3fbSopenharmony_ci	atomic_t refcount;
96d722e3fbSopenharmony_ci	struct amdgpu_device *dev;
97d722e3fbSopenharmony_ci
98d722e3fbSopenharmony_ci	uint64_t alloc_size;
99d722e3fbSopenharmony_ci
100d722e3fbSopenharmony_ci	uint32_t handle;
101d722e3fbSopenharmony_ci	uint32_t flink_name;
102d722e3fbSopenharmony_ci
103d722e3fbSopenharmony_ci	pthread_mutex_t cpu_access_mutex;
104d722e3fbSopenharmony_ci	void *cpu_ptr;
105d722e3fbSopenharmony_ci	int64_t cpu_map_count;
106d722e3fbSopenharmony_ci};
107d722e3fbSopenharmony_ci
108d722e3fbSopenharmony_cistruct amdgpu_bo_list {
109d722e3fbSopenharmony_ci	struct amdgpu_device *dev;
110d722e3fbSopenharmony_ci
111d722e3fbSopenharmony_ci	uint32_t handle;
112d722e3fbSopenharmony_ci};
113d722e3fbSopenharmony_ci
114d722e3fbSopenharmony_cistruct amdgpu_context {
115d722e3fbSopenharmony_ci	struct amdgpu_device *dev;
116d722e3fbSopenharmony_ci	/** Mutex for accessing fences and to maintain command submissions
117d722e3fbSopenharmony_ci	    in good sequence. */
118d722e3fbSopenharmony_ci	pthread_mutex_t sequence_mutex;
119d722e3fbSopenharmony_ci	/* context id*/
120d722e3fbSopenharmony_ci	uint32_t id;
121d722e3fbSopenharmony_ci	uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
122d722e3fbSopenharmony_ci	struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
123d722e3fbSopenharmony_ci};
124d722e3fbSopenharmony_ci
125d722e3fbSopenharmony_ci/**
126d722e3fbSopenharmony_ci * Structure describing sw semaphore based on scheduler
127d722e3fbSopenharmony_ci *
128d722e3fbSopenharmony_ci */
129d722e3fbSopenharmony_cistruct amdgpu_semaphore {
130d722e3fbSopenharmony_ci	atomic_t refcount;
131d722e3fbSopenharmony_ci	struct list_head list;
132d722e3fbSopenharmony_ci	struct amdgpu_cs_fence signal_fence;
133d722e3fbSopenharmony_ci};
134d722e3fbSopenharmony_ci
135d722e3fbSopenharmony_ci/**
136d722e3fbSopenharmony_ci * Functions.
137d722e3fbSopenharmony_ci */
138d722e3fbSopenharmony_ci
139d722e3fbSopenharmony_cidrm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
140d722e3fbSopenharmony_ci		       uint64_t max, uint64_t alignment);
141d722e3fbSopenharmony_ci
142d722e3fbSopenharmony_cidrm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
143d722e3fbSopenharmony_ci
144d722e3fbSopenharmony_cidrm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
145d722e3fbSopenharmony_ci
146d722e3fbSopenharmony_cidrm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
147d722e3fbSopenharmony_ci
148d722e3fbSopenharmony_cidrm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
149d722e3fbSopenharmony_ci
150d722e3fbSopenharmony_ci/**
151d722e3fbSopenharmony_ci * Inline functions.
152d722e3fbSopenharmony_ci */
153d722e3fbSopenharmony_ci
154d722e3fbSopenharmony_ci/**
155d722e3fbSopenharmony_ci * Increment src and decrement dst as if we were updating references
156d722e3fbSopenharmony_ci * for an assignment between 2 pointers of some objects.
157d722e3fbSopenharmony_ci *
158d722e3fbSopenharmony_ci * \return  true if dst is 0
159d722e3fbSopenharmony_ci */
160d722e3fbSopenharmony_cistatic inline bool update_references(atomic_t *dst, atomic_t *src)
161d722e3fbSopenharmony_ci{
162d722e3fbSopenharmony_ci	if (dst != src) {
163d722e3fbSopenharmony_ci		/* bump src first */
164d722e3fbSopenharmony_ci		if (src) {
165d722e3fbSopenharmony_ci			assert(atomic_read(src) > 0);
166d722e3fbSopenharmony_ci			atomic_inc(src);
167d722e3fbSopenharmony_ci		}
168d722e3fbSopenharmony_ci		if (dst) {
169d722e3fbSopenharmony_ci			assert(atomic_read(dst) > 0);
170d722e3fbSopenharmony_ci			return atomic_dec_and_test(dst);
171d722e3fbSopenharmony_ci		}
172d722e3fbSopenharmony_ci	}
173d722e3fbSopenharmony_ci	return false;
174d722e3fbSopenharmony_ci}
175d722e3fbSopenharmony_ci
176d722e3fbSopenharmony_ci#endif
177