GVM User Suite
User tools for the GVM open source project.
api.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 2666680 Ontario Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  */
19 #include <gpu/nvidia/resman/api.h>
23 
24 #include <gpu/nvidia/resources.h>
25 
26 #include <gpu/mdev.h>
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
39 uint8_t rm_version_check(int ctl_fd, uint8_t ignore_version, const char* version)
40 {
41  if (ctl_fd == -1)
42  return 0;
43 
44  uint8_t ret = 0;
45  struct NvVersionCheck version_check = {};
46 
47  if (ignore_version)
48  version_check.cmd = 0x32;
49 
50  if (version != NULL)
51  strncpy(version_check.version, version, sizeof(version_check.version));
52 
53  ret = ioctl(ctl_fd, NV_VERSION_CHECK, &version_check) != -1;
54 
55  if (ret)
56  ret = version_check.reply == 1;
57 
58  return ret;
59 }
60 
66  int fd,
67  struct NvResource* parent,
68  uint32_t object,
69  uint32_t rm_class,
70  void* data
71 )
72 {
73  struct NvResource* ret = NULL;
74  struct RmAllocRes alloc_res = {};
75 
76  if (fd == -1)
77  return NULL;
78 
79  alloc_res.hObjectNew = object;
80  alloc_res.hClass = rm_class;
81  alloc_res.pAllocParams = data;
82 
83  if (parent != NULL) {
84  alloc_res.hRoot = parent->client;
85  alloc_res.hObjectParent = parent->object;
86  }
87 
88  if (ioctl(fd, NV_ALLOC_RES, &alloc_res) != -1) {
89  if (alloc_res.status == 0x00)
90  ret = calloc(1, sizeof(struct NvResource));
91 
92  if (ret != NULL) {
93  ret->fd = fd;
94  ret->object = alloc_res.hObjectNew;
95  ret->rm_class = rm_class;
96  ret->class_info = data;
97 
98  if (parent == NULL) {
99  ret->client = alloc_res.hObjectNew;
100  ret->parent = alloc_res.hObjectNew;
101  } else {
102  ret->client = parent->client;
103  ret->parent = parent->object;
104 
105  struct NvResource* child = parent->child;
106  while (child != NULL && child->next != NULL)
107  child = child->next;
108  if (child != NULL)
109  child->next = ret;
110  else
111  parent->child = ret;
112  }
113  }
114 
115  return ret;
116  }
117 
118  return NULL;
119 }
120 
124 uint8_t rm_free_res(int fd, struct NvResource* object)
125 {
126  struct RmFreeRes free_res = {};
127 
128  if (object == NULL || fd == -1)
129  return 0;
130 
131  free_res.hRoot = object->client;
132  free_res.hObjectParent = object->parent;
133  free_res.hObjectOld = object->object;
134 
135  return (ioctl(fd, NV_FREE_RES, &free_res) != -1 && free_res.status == 0);
136 }
137 
138 void rm_free_tree(int fd, struct NvResource* root)
139 {
140  if (root == NULL)
141  return;
142 
143  rm_free_tree(fd, root->child);
144  rm_free_tree(fd, root->next);
145 
146  rm_free_res(fd, root);
147 
148  if (root->rm_class == 0x00000080 && root->class_info != NULL) {
149  struct Nv0000CtrlGpuDeAttachIdsParams deattach_ids = {};
150  deattach_ids.gpu_ids[0] = ((struct Gpu*) root->class_info)->identifier;
151  deattach_ids.gpu_ids[1] = 0xFFFFFFFF;
152  close(root->fd);
153  _RM_CTRL(fd, root->client, root->client, NV0000_DEATTACH_IDS, deattach_ids);
154  free(root->class_info);
155  }
156 
157  free(root);
158 }
159 
164 void* rm_ctrl_res(int fd, uint32_t client, uint32_t device, uint32_t command, void* data, uint32_t size)
165 {
166  struct RmControlRes ctrl_res = {};
167 
168  if (fd == -1)
169  return NULL;
170 
171  ctrl_res.client = client;
172  ctrl_res.object = device;
173  ctrl_res.cmd = command;
174  ctrl_res.params = data;
175  ctrl_res.param_size = size;
176 
177  if (ioctl(fd, NV_CONTROL_RES, &ctrl_res) == -1)
178  return NULL;
179 
180  if (ctrl_res.status == 0)
181  return data;
182 
185  printf(
186  "Failed RM Control Mechanism:\n"
187  "\tclient: 0x%.8X\n"
188  "\tobject: 0x%.8X\n"
189  "\tcmd: 0x%.8X\n"
190  "\tflags: 0x%.8X\n"
191  "\tparams: %p\n"
192  "\tsize: 0x%.8X\n"
193  "\tstatus: 0x%.8X\n",
194  ctrl_res.client,
195  ctrl_res.object,
196  ctrl_res.cmd,
197  ctrl_res.flags,
198  ctrl_res.params,
199  ctrl_res.param_size,
200  ctrl_res.status
201  );
202 
203  return NULL;
204 }
205 
208 uint8_t rm_alloc_os_event(int fd, uint32_t client_id, uint32_t device_id)
209 {
210  struct RmAllocOsEvent event = {};
211 
212  event.client = client_id;
213  event.device = device_id;
214  event.fd = fd;
215 
216  if (ioctl(fd, NV_CREATE_OS_EVENT, &event) == -1 || event.status != 0)
217  return 0;
218 
219  return event.status == 0;
220 }
uint8_t rm_alloc_os_event(int fd, uint32_t client_id, uint32_t device_id)
Allocates an Operating System Event.
Definition: api.c:208
void * rm_ctrl_res(int fd, uint32_t client, uint32_t device, uint32_t command, void *data, uint32_t size)
Control resource command.
Definition: api.c:164
void rm_free_tree(int fd, struct NvResource *root)
Frees a resource tree for the system.
Definition: api.c:138
uint8_t rm_version_check(int ctl_fd, uint8_t ignore_version, const char *version)
Version check for the RM API.
Definition: api.c:39
struct NvResource * rm_alloc_res(int fd, struct NvResource *parent, uint32_t object, uint32_t rm_class, void *data)
Allocates a Node for a resource.
Definition: api.c:65
uint8_t rm_free_res(int fd, struct NvResource *object)
Frees a Node for a Resource.
Definition: api.c:124
#define _RM_CTRL(fd, client, object, cmd, data)
Controls a RM Resource.
Definition: api.h:148
#define NV_FREE_RES
IOCTL command to control a resource.
Definition: ioctl.h:40
#define NV_CONTROL_RES
Definition: ioctl.h:43
#define NV_VERSION_CHECK
IOCTL command to perform a version check on the system.
Definition: ioctl.h:34
#define NV_CREATE_OS_EVENT
IOCTL command to create an OS event.
Definition: ioctl.h:31
#define NV_ALLOC_RES
IOCTL command to free a resource.
Definition: ioctl.h:37
#define NV0000_DEATTACH_IDS
Command to detach a gpu id from the driver.
Definition: nv0000.h:67
GPU Base Structure.
Definition: mdev.h:33
DeAttaches the following ids to the GPU driver.
Definition: nv0000.h:73
uint32_t gpu_ids[32]
List of GPU Ids to detach.
Definition: nv0000.h:74
Resource for managing NVIDIA kernel module objects.
Definition: resources.h:39
struct NvResource * next
Next child on the level.
Definition: resources.h:48
void * class_info
Class info for the resource.
Definition: resources.h:47
uint32_t rm_class
Class of the resource.
Definition: resources.h:46
uint32_t client
All resources require a client/root.
Definition: resources.h:43
uint32_t parent
Parent of the resource.
Definition: resources.h:44
struct NvResource * child
Child of the resource.
Definition: resources.h:49
uint32_t object
Object of the resource.
Definition: resources.h:45
RM API Version Check.
Definition: types.h:33
uint32_t cmd
Command override (Allows you to hid the version).
Definition: types.h:34
uint32_t reply
Reply if the driver is correct or not.
Definition: types.h:35
char version[64]
Version for the driver.
Definition: types.h:36
Operating System Event Allocator.
Definition: types.h:43
uint32_t client
Client for the operating system event.
Definition: types.h:44
uint32_t status
Status for the allocation.
Definition: types.h:48
Resource Allocator.
Definition: types.h:55
uint32_t hRoot
Client allocating the object.
Definition: types.h:56
uint32_t hClass
Class of the new object.
Definition: types.h:59
uint32_t hObjectNew
New object we allocate.
Definition: types.h:58
uint32_t status
Status regarding execution.
Definition: types.h:61
uint32_t hObjectParent
Parent of the object we allocate.
Definition: types.h:57
void * pAllocParams
Allocation parameters.
Definition: types.h:60
Resource Control Message.
Definition: types.h:100
uint32_t flags
Flags for the control of the resource.
Definition: types.h:104
uint32_t object
Object to run the object control on.
Definition: types.h:102
uint32_t param_size
Size of the parameter.
Definition: types.h:106
uint32_t status
Status regarding execution.
Definition: types.h:107
uint32_t cmd
Command to run on the object.
Definition: types.h:103
uint32_t client
Client for running the object control.
Definition: types.h:101
void * params
Parameter to pass into the control command.
Definition: types.h:105
Resource Deallocator.
Definition: types.h:89
uint32_t status
Status regarding execution.
Definition: types.h:93
uint32_t hObjectOld
Object to deallocate.
Definition: types.h:92
uint32_t hObjectParent
Parent to deallocate the object.
Definition: types.h:91
uint32_t hRoot
Client for deallocation of the object.
Definition: types.h:90
#define calloc(x, y)
Definition: toml.c:53
#define free(x)
Definition: toml.c:52