2015-04-20 10:04:22 -06:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
2015-08-07 10:20:51 -06:00
|
|
|
*/
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file amdgpu_device.c
|
|
|
|
*
|
|
|
|
* Implementation of functions for AMD GPU device
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-29 13:09:04 -06:00
|
|
|
#include <unistd.h>
|
2018-07-17 03:04:31 -06:00
|
|
|
#include <fcntl.h>
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
#include "xf86drm.h"
|
|
|
|
#include "amdgpu_drm.h"
|
|
|
|
#include "amdgpu_internal.h"
|
2015-08-16 21:09:08 -06:00
|
|
|
#include "util_math.h"
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
#define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
|
|
|
|
|
2019-06-20 02:49:01 -06:00
|
|
|
static pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static amdgpu_device_handle dev_list;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
2018-08-01 12:44:44 -06:00
|
|
|
static int fd_compare(int fd1, int fd2)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
2015-04-30 06:13:28 -06:00
|
|
|
char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
|
|
|
|
char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (name1 == NULL || name2 == NULL) {
|
|
|
|
free(name1);
|
|
|
|
free(name2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = strcmp(name1, name2);
|
|
|
|
free(name1);
|
|
|
|
free(name2);
|
|
|
|
|
|
|
|
return result;
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the authenticated form fd,
|
|
|
|
*
|
|
|
|
* \param fd - \c [in] File descriptor for AMD GPU device
|
|
|
|
* \param auth - \c [out] Pointer to output the fd is authenticated or not
|
|
|
|
* A render node fd, output auth = 0
|
|
|
|
* A legacy fd, get the authenticated for compatibility root
|
|
|
|
*
|
|
|
|
* \return 0 on success\n
|
|
|
|
* >0 - AMD specific error code\n
|
|
|
|
* <0 - Negative POSIX Error code
|
|
|
|
*/
|
|
|
|
static int amdgpu_get_auth(int fd, int *auth)
|
|
|
|
{
|
|
|
|
int r = 0;
|
2015-06-04 10:57:50 -06:00
|
|
|
drm_client_t client = {};
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
|
|
|
|
*auth = 0;
|
|
|
|
else {
|
|
|
|
client.idx = 0;
|
|
|
|
r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
|
|
|
|
if (!r)
|
|
|
|
*auth = client.auth;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-08-07 10:09:35 -06:00
|
|
|
static void amdgpu_device_free_internal(amdgpu_device_handle dev)
|
|
|
|
{
|
2019-06-20 02:49:01 -06:00
|
|
|
amdgpu_device_handle *node = &dev_list;
|
2018-08-01 12:44:44 -06:00
|
|
|
|
|
|
|
while (*node != dev && (*node)->next)
|
|
|
|
node = &(*node)->next;
|
|
|
|
*node = (*node)->next;
|
|
|
|
|
2018-05-10 17:22:17 -06:00
|
|
|
close(dev->fd);
|
|
|
|
if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
|
|
|
|
close(dev->flink_fd);
|
|
|
|
|
2023-12-18 06:53:10 -07:00
|
|
|
amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_32);
|
|
|
|
amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_low);
|
|
|
|
amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high_32);
|
|
|
|
amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high);
|
2018-08-02 02:47:02 -06:00
|
|
|
handle_table_fini(&dev->bo_handles);
|
2018-08-02 02:56:11 -06:00
|
|
|
handle_table_fini(&dev->bo_flink_names);
|
2015-08-07 10:09:35 -06:00
|
|
|
pthread_mutex_destroy(&dev->bo_table_mutex);
|
2017-11-30 10:52:06 -07:00
|
|
|
free(dev->marketing_name);
|
2015-08-07 10:09:35 -06:00
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment between two amdgpu_device pointers with reference counting.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* struct amdgpu_device *dst = ... , *src = ...;
|
|
|
|
*
|
|
|
|
* dst = src;
|
|
|
|
* // No reference counting. Only use this when you need to move
|
|
|
|
* // a reference from one pointer to another.
|
|
|
|
*
|
|
|
|
* amdgpu_device_reference(&dst, src);
|
|
|
|
* // Reference counters are updated. dst is decremented and src is
|
|
|
|
* // incremented. dst is freed if its reference counter is 0.
|
|
|
|
*/
|
|
|
|
static void amdgpu_device_reference(struct amdgpu_device **dst,
|
2018-09-13 15:57:13 -06:00
|
|
|
struct amdgpu_device *src)
|
2015-08-07 10:09:35 -06:00
|
|
|
{
|
|
|
|
if (update_references(&(*dst)->refcount, &src->refcount))
|
|
|
|
amdgpu_device_free_internal(*dst);
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_device_initialize(int fd,
|
|
|
|
uint32_t *major_version,
|
|
|
|
uint32_t *minor_version,
|
|
|
|
amdgpu_device_handle *device_handle)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
|
|
|
struct amdgpu_device *dev;
|
|
|
|
drmVersionPtr version;
|
|
|
|
int r;
|
|
|
|
int flag_auth = 0;
|
|
|
|
int flag_authexist=0;
|
2015-05-06 10:59:02 -06:00
|
|
|
uint32_t accel_working = 0;
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
*device_handle = NULL;
|
|
|
|
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_lock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
r = amdgpu_get_auth(fd, &flag_auth);
|
|
|
|
if (r) {
|
2017-09-04 13:05:26 -06:00
|
|
|
fprintf(stderr, "%s: amdgpu_get_auth (1) failed (%i)\n",
|
|
|
|
__func__, r);
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return r;
|
|
|
|
}
|
2018-08-01 12:44:44 -06:00
|
|
|
|
2019-06-20 02:49:01 -06:00
|
|
|
for (dev = dev_list; dev; dev = dev->next)
|
2018-08-01 12:44:44 -06:00
|
|
|
if (fd_compare(dev->fd, fd) == 0)
|
|
|
|
break;
|
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
if (dev) {
|
|
|
|
r = amdgpu_get_auth(dev->fd, &flag_authexist);
|
|
|
|
if (r) {
|
2017-09-04 13:05:26 -06:00
|
|
|
fprintf(stderr, "%s: amdgpu_get_auth (2) failed (%i)\n",
|
|
|
|
__func__, r);
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if ((flag_auth) && (!flag_authexist)) {
|
2018-07-17 03:04:31 -06:00
|
|
|
dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
|
|
|
*major_version = dev->major_version;
|
|
|
|
*minor_version = dev->minor_version;
|
|
|
|
amdgpu_device_reference(device_handle, dev);
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = calloc(1, sizeof(struct amdgpu_device));
|
|
|
|
if (!dev) {
|
2017-09-04 13:05:26 -06:00
|
|
|
fprintf(stderr, "%s: calloc failed\n", __func__);
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-07-29 13:09:04 -06:00
|
|
|
dev->fd = -1;
|
|
|
|
dev->flink_fd = -1;
|
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
atomic_set(&dev->refcount, 1);
|
|
|
|
|
|
|
|
version = drmGetVersion(fd);
|
|
|
|
if (version->version_major != 3) {
|
|
|
|
fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
|
|
|
|
"only compatible with 3.x.x.\n",
|
|
|
|
__func__,
|
|
|
|
version->version_major,
|
|
|
|
version->version_minor,
|
|
|
|
version->version_patchlevel);
|
|
|
|
drmFreeVersion(version);
|
|
|
|
r = -EBADF;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-07-17 03:04:31 -06:00
|
|
|
dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
|
2015-07-29 13:09:04 -06:00
|
|
|
dev->flink_fd = dev->fd;
|
2015-04-20 10:04:22 -06:00
|
|
|
dev->major_version = version->version_major;
|
|
|
|
dev->minor_version = version->version_minor;
|
|
|
|
drmFreeVersion(version);
|
|
|
|
|
|
|
|
pthread_mutex_init(&dev->bo_table_mutex, NULL);
|
|
|
|
|
|
|
|
/* Check if acceleration is working. */
|
|
|
|
r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
|
2017-09-04 13:05:26 -06:00
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "%s: amdgpu_query_info(ACCEL_WORKING) failed (%i)\n",
|
|
|
|
__func__, r);
|
2015-04-20 10:04:22 -06:00
|
|
|
goto cleanup;
|
2017-09-04 13:05:26 -06:00
|
|
|
}
|
2015-04-20 10:04:22 -06:00
|
|
|
if (!accel_working) {
|
2017-09-04 13:05:26 -06:00
|
|
|
fprintf(stderr, "%s: AMDGPU_INFO_ACCEL_WORKING = 0\n", __func__);
|
2015-04-20 10:04:22 -06:00
|
|
|
r = -EBADF;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = amdgpu_query_gpu_info_init(dev);
|
2017-09-04 13:05:26 -06:00
|
|
|
if (r) {
|
|
|
|
fprintf(stderr, "%s: amdgpu_query_gpu_info_init failed\n", __func__);
|
2015-04-20 10:04:22 -06:00
|
|
|
goto cleanup;
|
2017-09-04 13:05:26 -06:00
|
|
|
}
|
2015-04-20 10:04:22 -06:00
|
|
|
|
2023-12-18 07:02:30 -07:00
|
|
|
amdgpu_va_manager_init(&dev->va_mgr,
|
|
|
|
dev->dev_info.virtual_address_offset,
|
|
|
|
dev->dev_info.virtual_address_max,
|
|
|
|
dev->dev_info.high_va_offset,
|
|
|
|
dev->dev_info.high_va_max,
|
|
|
|
dev->dev_info.virtual_address_alignment);
|
2018-02-26 04:30:36 -07:00
|
|
|
|
2017-11-30 10:52:06 -07:00
|
|
|
amdgpu_parse_asic_ids(dev);
|
2017-05-31 14:22:50 -06:00
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
*major_version = dev->major_version;
|
|
|
|
*minor_version = dev->minor_version;
|
|
|
|
*device_handle = dev;
|
2019-06-20 02:49:01 -06:00
|
|
|
dev->next = dev_list;
|
|
|
|
dev_list = dev;
|
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
2015-07-29 13:09:04 -06:00
|
|
|
if (dev->fd >= 0)
|
|
|
|
close(dev->fd);
|
2015-04-20 10:04:22 -06:00
|
|
|
free(dev);
|
2019-06-20 02:49:01 -06:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_device_deinitialize(amdgpu_device_handle dev)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
2024-02-25 08:53:29 -07:00
|
|
|
pthread_mutex_lock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
amdgpu_device_reference(&dev, NULL);
|
2024-02-25 08:53:29 -07:00
|
|
|
pthread_mutex_unlock(&dev_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2016-09-12 09:14:11 -06:00
|
|
|
|
2021-11-15 17:41:49 -07:00
|
|
|
drm_public int amdgpu_device_get_fd(amdgpu_device_handle device_handle)
|
|
|
|
{
|
|
|
|
return device_handle->fd;
|
|
|
|
}
|
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public const char *amdgpu_get_marketing_name(amdgpu_device_handle dev)
|
2016-09-12 09:14:11 -06:00
|
|
|
{
|
2022-09-26 15:19:27 -06:00
|
|
|
if (dev->marketing_name)
|
|
|
|
return dev->marketing_name;
|
|
|
|
else
|
|
|
|
return "AMD Radeon Graphics";
|
2016-09-12 09:14:11 -06:00
|
|
|
}
|
2018-02-02 10:15:00 -07:00
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_query_sw_info(amdgpu_device_handle dev,
|
|
|
|
enum amdgpu_sw_info info,
|
|
|
|
void *value)
|
2018-02-02 10:15:00 -07:00
|
|
|
{
|
|
|
|
uint32_t *val32 = (uint32_t*)value;
|
|
|
|
|
|
|
|
switch (info) {
|
|
|
|
case amdgpu_sw_info_address32_hi:
|
2023-12-18 06:53:10 -07:00
|
|
|
if (dev->va_mgr.vamgr_high_32.va_max)
|
|
|
|
*val32 = (dev->va_mgr.vamgr_high_32.va_max - 1) >> 32;
|
2018-02-26 06:11:52 -07:00
|
|
|
else
|
2023-12-18 06:53:10 -07:00
|
|
|
*val32 = (dev->va_mgr.vamgr_32.va_max - 1) >> 32;
|
2018-02-02 10:15:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|