amdgpu_internal.h revision 9bd392ad
13f012e29Smrg/*
23f012e29Smrg * Copyright © 2014 Advanced Micro Devices, Inc.
33f012e29Smrg * All Rights Reserved.
43f012e29Smrg *
53f012e29Smrg * Permission is hereby granted, free of charge, to any person obtaining a
63f012e29Smrg * copy of this software and associated documentation files (the "Software"),
73f012e29Smrg * to deal in the Software without restriction, including without limitation
83f012e29Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
93f012e29Smrg * and/or sell copies of the Software, and to permit persons to whom the
103f012e29Smrg * Software is furnished to do so, subject to the following conditions:
113f012e29Smrg *
123f012e29Smrg * The above copyright notice and this permission notice shall be included in
133f012e29Smrg * all copies or substantial portions of the Software.
143f012e29Smrg *
153f012e29Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163f012e29Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173f012e29Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
183f012e29Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
193f012e29Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
203f012e29Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
213f012e29Smrg * OTHER DEALINGS IN THE SOFTWARE.
223f012e29Smrg *
233f012e29Smrg */
243f012e29Smrg
253f012e29Smrg#ifndef _AMDGPU_INTERNAL_H_
263f012e29Smrg#define _AMDGPU_INTERNAL_H_
273f012e29Smrg
283f012e29Smrg#include <assert.h>
293f012e29Smrg#include <pthread.h>
303f012e29Smrg
313f012e29Smrg#include "libdrm_macros.h"
323f012e29Smrg#include "xf86atomic.h"
333f012e29Smrg#include "amdgpu.h"
343f012e29Smrg#include "util_double_list.h"
357cdc0497Smrg#include "handle_table.h"
363f012e29Smrg
373f012e29Smrg#define AMDGPU_CS_MAX_RINGS 8
383f012e29Smrg/* do not use below macro if b is not power of 2 aligned value */
393f012e29Smrg#define __round_mask(x, y) ((__typeof__(x))((y)-1))
403f012e29Smrg#define ROUND_UP(x, y) ((((x)-1) | __round_mask(x, y))+1)
413f012e29Smrg#define ROUND_DOWN(x, y) ((x) & ~__round_mask(x, y))
423f012e29Smrg
433f012e29Smrg#define AMDGPU_INVALID_VA_ADDRESS	0xffffffffffffffff
443f012e29Smrg#define AMDGPU_NULL_SUBMIT_SEQ		0
453f012e29Smrg
463f012e29Smrgstruct amdgpu_bo_va_hole {
473f012e29Smrg	struct list_head list;
483f012e29Smrg	uint64_t offset;
493f012e29Smrg	uint64_t size;
503f012e29Smrg};
513f012e29Smrg
523f012e29Smrgstruct amdgpu_bo_va_mgr {
533f012e29Smrg	uint64_t va_max;
543f012e29Smrg	struct list_head va_holes;
553f012e29Smrg	pthread_mutex_t bo_va_mutex;
563f012e29Smrg	uint32_t va_alignment;
573f012e29Smrg};
583f012e29Smrg
593f012e29Smrgstruct amdgpu_va {
603f012e29Smrg	amdgpu_device_handle dev;
613f012e29Smrg	uint64_t address;
623f012e29Smrg	uint64_t size;
633f012e29Smrg	enum amdgpu_gpu_va_range range;
643f012e29Smrg	struct amdgpu_bo_va_mgr *vamgr;
653f012e29Smrg};
663f012e29Smrg
673f012e29Smrgstruct amdgpu_device {
683f012e29Smrg	atomic_t refcount;
697cdc0497Smrg	struct amdgpu_device *next;
703f012e29Smrg	int fd;
713f012e29Smrg	int flink_fd;
723f012e29Smrg	unsigned major_version;
733f012e29Smrg	unsigned minor_version;
743f012e29Smrg
7500a23bdaSmrg	char *marketing_name;
763f012e29Smrg	/** List of buffer handles. Protected by bo_table_mutex. */
777cdc0497Smrg	struct handle_table bo_handles;
783f012e29Smrg	/** List of buffer GEM flink names. Protected by bo_table_mutex. */
797cdc0497Smrg	struct handle_table bo_flink_names;
803f012e29Smrg	/** This protects all hash tables. */
813f012e29Smrg	pthread_mutex_t bo_table_mutex;
823f012e29Smrg	struct drm_amdgpu_info_device dev_info;
833f012e29Smrg	struct amdgpu_gpu_info info;
8400a23bdaSmrg	/** The VA manager for the lower virtual address space */
85d8807b2fSmrg	struct amdgpu_bo_va_mgr vamgr;
863f012e29Smrg	/** The VA manager for the 32bit address space */
87d8807b2fSmrg	struct amdgpu_bo_va_mgr vamgr_32;
8800a23bdaSmrg	/** The VA manager for the high virtual address space */
8900a23bdaSmrg	struct amdgpu_bo_va_mgr vamgr_high;
9000a23bdaSmrg	/** The VA manager for the 32bit high address space */
9100a23bdaSmrg	struct amdgpu_bo_va_mgr vamgr_high_32;
923f012e29Smrg};
933f012e29Smrg
943f012e29Smrgstruct amdgpu_bo {
953f012e29Smrg	atomic_t refcount;
963f012e29Smrg	struct amdgpu_device *dev;
973f012e29Smrg
983f012e29Smrg	uint64_t alloc_size;
993f012e29Smrg
1003f012e29Smrg	uint32_t handle;
1013f012e29Smrg	uint32_t flink_name;
1023f012e29Smrg
1033f012e29Smrg	pthread_mutex_t cpu_access_mutex;
1043f012e29Smrg	void *cpu_ptr;
1059bd392adSmrg	int64_t cpu_map_count;
1063f012e29Smrg};
1073f012e29Smrg
1083f012e29Smrgstruct amdgpu_bo_list {
1093f012e29Smrg	struct amdgpu_device *dev;
1103f012e29Smrg
1113f012e29Smrg	uint32_t handle;
1123f012e29Smrg};
1133f012e29Smrg
1143f012e29Smrgstruct amdgpu_context {
1153f012e29Smrg	struct amdgpu_device *dev;
1163f012e29Smrg	/** Mutex for accessing fences and to maintain command submissions
1173f012e29Smrg	    in good sequence. */
1183f012e29Smrg	pthread_mutex_t sequence_mutex;
1193f012e29Smrg	/* context id*/
1203f012e29Smrg	uint32_t id;
1213f012e29Smrg	uint64_t last_seq[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
1223f012e29Smrg	struct list_head sem_list[AMDGPU_HW_IP_NUM][AMDGPU_HW_IP_INSTANCE_MAX_COUNT][AMDGPU_CS_MAX_RINGS];
1233f012e29Smrg};
1243f012e29Smrg
1253f012e29Smrg/**
1263f012e29Smrg * Structure describing sw semaphore based on scheduler
1273f012e29Smrg *
1283f012e29Smrg */
1293f012e29Smrgstruct amdgpu_semaphore {
1303f012e29Smrg	atomic_t refcount;
1313f012e29Smrg	struct list_head list;
1323f012e29Smrg	struct amdgpu_cs_fence signal_fence;
1333f012e29Smrg};
1343f012e29Smrg
1353f012e29Smrg/**
1363f012e29Smrg * Functions.
1373f012e29Smrg */
1383f012e29Smrg
1393f012e29Smrgdrm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
1403f012e29Smrg		       uint64_t max, uint64_t alignment);
1413f012e29Smrg
1423f012e29Smrgdrm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
1433f012e29Smrg
14400a23bdaSmrgdrm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
145d8807b2fSmrg
1463f012e29Smrgdrm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
1473f012e29Smrg
1483f012e29Smrgdrm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
1493f012e29Smrg
1503f012e29Smrg/**
1513f012e29Smrg * Inline functions.
1523f012e29Smrg */
1533f012e29Smrg
1543f012e29Smrg/**
1553f012e29Smrg * Increment src and decrement dst as if we were updating references
1563f012e29Smrg * for an assignment between 2 pointers of some objects.
1573f012e29Smrg *
1583f012e29Smrg * \return  true if dst is 0
1593f012e29Smrg */
1603f012e29Smrgstatic inline bool update_references(atomic_t *dst, atomic_t *src)
1613f012e29Smrg{
1623f012e29Smrg	if (dst != src) {
1633f012e29Smrg		/* bump src first */
1643f012e29Smrg		if (src) {
1653f012e29Smrg			assert(atomic_read(src) > 0);
1663f012e29Smrg			atomic_inc(src);
1673f012e29Smrg		}
1683f012e29Smrg		if (dst) {
1693f012e29Smrg			assert(atomic_read(dst) > 0);
1703f012e29Smrg			return atomic_dec_and_test(dst);
1713f012e29Smrg		}
1723f012e29Smrg	}
1733f012e29Smrg	return false;
1743f012e29Smrg}
1753f012e29Smrg
1763f012e29Smrg#endif
177