xref: /linux/drivers/gpu/drm/drm_ras.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2026 Intel Corporation
4  */
5 
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/netdevice.h>
9 #include <linux/xarray.h>
10 #include <net/genetlink.h>
11 
12 #include <drm/drm_ras.h>
13 
14 #include "drm_ras_nl.h"
15 
16 /**
17  * DOC: DRM RAS Node Management
18  *
19  * This module provides the infrastructure to manage RAS (Reliability,
20  * Availability, and Serviceability) nodes for DRM drivers. Each
21  * DRM driver may register one or more RAS nodes, which represent
22  * logical components capable of reporting error counters and other
23  * reliability metrics.
24  *
25  * The nodes are stored in a global xarray `drm_ras_xa` to allow
26  * efficient lookup by ID. Nodes can be registered or unregistered
27  * dynamically at runtime.
28  *
29  * A Generic Netlink family `drm_ras` exposes the below operations to
30  * userspace:
31  *
32  * 1. LIST_NODES: Dump all currently registered RAS nodes.
33  *    The user receives an array of node IDs, names, and types.
34  *
35  * 2. GET_ERROR_COUNTER: Get error counters of a given node.
36  *    Userspace must provide Node ID, Error ID (Optional for specific counter).
37  *    Returns all counters of a node if only Node ID is provided or specific
38  *    error counters.
39  *
40  * 3. CLEAR_ERROR_COUNTER: Clear error counter of a given node.
41  *    Userspace must provide Node ID, Error ID.
42  *    Clears specific error counter of a node if supported.
43  *
44  * 4. ERROR_REPORT: Subscribe to this multicast group to receive error events
45  *
46  * 5. ERROR_EVENT: Report an error event to userspace. The event contains device, node
47  *    and error information that triggered the event.
48  *
49  * 6. GET_ERROR_THRESHOLD: Query error threshold of a given counter.
50  *    Userspace must provide Node ID and Error ID.
51  *    Returns the error threshold of a specific counter.
52  *
53  * 7. SET_ERROR_THRESHOLD: Set error threshold of a given counter.
54  *    Userspace must provide Node ID, Error ID and threshold to be set.
55  *
56  * Node registration:
57  *
58  * - drm_ras_node_register(): Registers a new node and assigns
59  *   it a unique ID in the xarray.
60  * - drm_ras_node_unregister(): Removes a previously registered
61  *   node from the xarray.
62  *
63  * Node type:
64  *
65  * - ERROR_COUNTER:
66  *     + Currently, only error counters are supported.
67  *     + The driver must implement the query_error_counter() callback to provide
68  *       the name and the value of the error counter.
69  *     + The driver must provide a error_counter_range.last value informing the
70  *       last valid error ID.
71  *     + The driver can provide a error_counter_range.first value informing the
72  *       first valid error ID.
73  *     + The error counters in the driver doesn't need to be contiguous, but the
74  *       driver must return -ENOENT to the query_error_counter as an indication
75  *       that the ID should be skipped and not listed in the netlink API.
76  *     + The driver can optionally implement query_error_threshold() and
77  *       set_error_threshold() callbacks to facilitate getting/setting error
78  *       threshold of the counter. Threshold in RAS context means the number of
79  *       errors the hardware is expected to accumulate before it raises them to
80  *       software. This is to have a fine grained control over error notifications
81  *       that are raised by the hardware.
82  *     + The driver is responsible for error threshold bounds checking.
83  *
84  * Netlink handlers:
85  *
86  * - drm_ras_nl_list_nodes_dumpit(): Implements the LIST_NODES
87  *   operation, iterating over the xarray.
88  * - drm_ras_nl_get_error_counter_dumpit(): Implements the GET_ERROR_COUNTER dumpit
89  *   operation, fetching all counters from a specific node.
90  * - drm_ras_nl_get_error_counter_doit(): Implements the GET_ERROR_COUNTER doit
91  *   operation, fetching a counter value from a specific node.
92  * - drm_ras_nl_clear_error_counter_doit(): Implements the CLEAR_ERROR_COUNTER doit
93  *   operation, clearing a counter value from a specific node.
94  * - drm_ras_nl_get_error_threshold_doit(): Implements the GET_ERROR_THRESHOLD doit
95  *   operation, fetching the error threshold of a specific counter.
96  * - drm_ras_nl_set_error_threshold_doit(): Implements the SET_ERROR_THRESHOLD doit
97  *   operation, setting the error threshold of a specific counter.
98  */
99 
100 static DEFINE_XARRAY_ALLOC(drm_ras_xa);
101 
102 /*
103  * The netlink callback context carries dump state across multiple dumpit calls
104  */
105 struct drm_ras_ctx {
106 	/* Which xarray id to restart the dump from */
107 	unsigned long restart;
108 };
109 
110 /**
111  * drm_ras_nl_list_nodes_dumpit() - Dump all registered RAS nodes
112  * @skb: Netlink message buffer
113  * @cb: Callback context for multi-part dumps
114  *
115  * Iterates over all registered RAS nodes in the global xarray and appends
116  * their attributes (ID, name, type) to the given netlink message buffer.
117  * Uses @cb->ctx to track progress in case the message buffer fills up, allowing
118  * multi-part dump support. On buffer overflow, updates the context to resume
119  * from the last node on the next invocation.
120  *
121  * Return: 0 if all nodes fit in @skb, number of bytes added to @skb if
122  *          the buffer filled up (requires multi-part continuation), or
123  *          a negative error code on failure.
124  */
125 int drm_ras_nl_list_nodes_dumpit(struct sk_buff *skb,
126 				 struct netlink_callback *cb)
127 {
128 	const struct genl_info *info = genl_info_dump(cb);
129 	struct drm_ras_ctx *ctx = (void *)cb->ctx;
130 	struct drm_ras_node *node;
131 	struct nlattr *hdr;
132 	unsigned long id;
133 	int ret;
134 
135 	xa_for_each_start(&drm_ras_xa, id, node, ctx->restart) {
136 		hdr = genlmsg_iput(skb, info);
137 		if (!hdr) {
138 			ret = -EMSGSIZE;
139 			break;
140 		}
141 
142 		ret = nla_put_u32(skb, DRM_RAS_A_NODE_ATTRS_NODE_ID, node->id);
143 		if (ret) {
144 			genlmsg_cancel(skb, hdr);
145 			break;
146 		}
147 
148 		ret = nla_put_string(skb, DRM_RAS_A_NODE_ATTRS_DEVICE_NAME,
149 				     node->device_name);
150 		if (ret) {
151 			genlmsg_cancel(skb, hdr);
152 			break;
153 		}
154 
155 		ret = nla_put_string(skb, DRM_RAS_A_NODE_ATTRS_NODE_NAME,
156 				     node->node_name);
157 		if (ret) {
158 			genlmsg_cancel(skb, hdr);
159 			break;
160 		}
161 
162 		ret = nla_put_u32(skb, DRM_RAS_A_NODE_ATTRS_NODE_TYPE,
163 				  node->type);
164 		if (ret) {
165 			genlmsg_cancel(skb, hdr);
166 			break;
167 		}
168 
169 		genlmsg_end(skb, hdr);
170 	}
171 
172 	if (ret == -EMSGSIZE)
173 		ctx->restart = id;
174 
175 	return ret;
176 }
177 
178 static int get_node_error_counter(u32 node_id, u32 error_id,
179 				  const char **name, u32 *value)
180 {
181 	struct drm_ras_node *node;
182 
183 	node = xa_load(&drm_ras_xa, node_id);
184 	if (!node || !node->query_error_counter)
185 		return -ENOENT;
186 
187 	if (error_id < node->error_counter_range.first ||
188 	    error_id > node->error_counter_range.last)
189 		return -EINVAL;
190 
191 	return node->query_error_counter(node, error_id, name, value);
192 }
193 
194 static int get_node_error_threshold(u32 node_id, u32 error_id, const char **name, u32 *threshold)
195 {
196 	struct drm_ras_node *node;
197 
198 	node = xa_load(&drm_ras_xa, node_id);
199 	if (!node)
200 		return -ENOENT;
201 
202 	if (!node->query_error_threshold)
203 		return -EOPNOTSUPP;
204 
205 	if (error_id < node->error_counter_range.first || error_id > node->error_counter_range.last)
206 		return -EINVAL;
207 
208 	return node->query_error_threshold(node, error_id, name, threshold);
209 }
210 
211 static int set_node_error_threshold(u32 node_id, u32 error_id, u32 threshold)
212 {
213 	struct drm_ras_node *node;
214 
215 	node = xa_load(&drm_ras_xa, node_id);
216 	if (!node)
217 		return -ENOENT;
218 
219 	if (!node->set_error_threshold)
220 		return -EOPNOTSUPP;
221 
222 	if (error_id < node->error_counter_range.first || error_id > node->error_counter_range.last)
223 		return -EINVAL;
224 
225 	return node->set_error_threshold(node, error_id, threshold);
226 }
227 
228 static int msg_reply_value(struct sk_buff *msg, u32 error_id,
229 			   const char *error_name, u32 value)
230 {
231 	int ret;
232 
233 	ret = nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID, error_id);
234 	if (ret)
235 		return ret;
236 
237 	ret = nla_put_string(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_NAME,
238 			     error_name);
239 	if (ret)
240 		return ret;
241 
242 	return nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_VALUE,
243 			   value);
244 }
245 
246 static int msg_put_error_event_attrs(struct sk_buff *msg, struct drm_ras_node *node,
247 				     u32 error_id, const char *error_name, u32 value)
248 {
249 	int ret;
250 
251 	ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_DEVICE_NAME, node->device_name);
252 	if (ret)
253 		return ret;
254 
255 	ret = nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_NODE_ID, node->id);
256 	if (ret)
257 		return ret;
258 
259 	ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_NODE_NAME, node->node_name);
260 	if (ret)
261 		return ret;
262 
263 	ret = nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_ID, error_id);
264 	if (ret)
265 		return ret;
266 
267 	ret = nla_put_string(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_NAME, error_name);
268 	if (ret)
269 		return ret;
270 
271 	return nla_put_u32(msg, DRM_RAS_A_ERROR_EVENT_ATTRS_ERROR_VALUE, value);
272 }
273 
274 static int msg_reply_threshold(struct sk_buff *msg, u32 error_id, const char *error_name,
275 			       u32 threshold)
276 {
277 	int ret;
278 
279 	ret = nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID, error_id);
280 	if (ret)
281 		return ret;
282 
283 	ret = nla_put_string(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_NAME, error_name);
284 	if (ret)
285 		return ret;
286 
287 	return nla_put_u32(msg, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD, threshold);
288 }
289 
290 static int doit_reply_value(struct genl_info *info, u32 node_id,
291 			    u32 error_id)
292 {
293 	struct sk_buff *msg;
294 	struct nlattr *hdr;
295 	const char *error_name;
296 	u32 value;
297 	int ret;
298 
299 	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
300 	if (!msg)
301 		return -ENOMEM;
302 
303 	hdr = genlmsg_iput(msg, info);
304 	if (!hdr) {
305 		ret = -EMSGSIZE;
306 		goto free_msg;
307 	}
308 
309 	ret = get_node_error_counter(node_id, error_id,
310 				     &error_name, &value);
311 	if (ret)
312 		goto cancel_msg;
313 
314 	ret = msg_reply_value(msg, error_id, error_name, value);
315 	if (ret)
316 		goto cancel_msg;
317 
318 	genlmsg_end(msg, hdr);
319 
320 	return genlmsg_reply(msg, info);
321 
322 cancel_msg:
323 	genlmsg_cancel(msg, hdr);
324 free_msg:
325 	nlmsg_free(msg);
326 	return ret;
327 }
328 
329 static int doit_reply_threshold(struct genl_info *info, u32 node_id, u32 error_id)
330 {
331 	const char *error_name;
332 	struct sk_buff *msg;
333 	struct nlattr *hdr;
334 	u32 threshold;
335 	int ret;
336 
337 	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
338 	if (!msg)
339 		return -ENOMEM;
340 
341 	hdr = genlmsg_iput(msg, info);
342 	if (!hdr) {
343 		ret = -EMSGSIZE;
344 		goto free_msg;
345 	}
346 
347 	ret = get_node_error_threshold(node_id, error_id, &error_name, &threshold);
348 	if (ret)
349 		goto cancel_msg;
350 
351 	ret = msg_reply_threshold(msg, error_id, error_name, threshold);
352 	if (ret)
353 		goto cancel_msg;
354 
355 	genlmsg_end(msg, hdr);
356 
357 	return genlmsg_reply(msg, info);
358 
359 cancel_msg:
360 	genlmsg_cancel(msg, hdr);
361 free_msg:
362 	nlmsg_free(msg);
363 	return ret;
364 }
365 
366 /**
367  * drm_ras_nl_error_event() - Report an error event
368  * @node: Node structure
369  * @error_id: ID of the error
370  * @error_name: Name of the error
371  * @value: Value of the error counter
372  *
373  * Report an error-event to userspace using the error-report multicast group.
374  *
375  * Context: Process context only. Uses %GFP_KERNEL and multicasts to all
376  *          netns, which is unbounded work; callers in interrupt handlers
377  *          or other atomic context must defer event.
378  *
379  * Return: 0 on success, or negative errno on failure.
380  */
381 int drm_ras_nl_error_event(struct drm_ras_node *node, u32 error_id, const char *error_name,
382 			   u32 value)
383 {
384 	struct genl_info info;
385 	struct sk_buff *msg;
386 	struct nlattr *hdr;
387 	int ret;
388 
389 	if (!node || !error_name)
390 		return -EINVAL;
391 
392 	/* Check the node is currently registered */
393 	if (xa_load(&drm_ras_xa, node->id) != node)
394 		return -ENOENT;
395 
396 	/* Currently only Error Counter events are supported */
397 	if (node->type != DRM_RAS_NODE_TYPE_ERROR_COUNTER)
398 		return -EOPNOTSUPP;
399 
400 	/* Check the error ID is within the valid range */
401 	if (error_id < node->error_counter_range.first ||
402 	    error_id > node->error_counter_range.last)
403 		return -EINVAL;
404 
405 	genl_info_init_ntf(&info, &drm_ras_nl_family, DRM_RAS_CMD_ERROR_EVENT);
406 
407 	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
408 	if (!msg)
409 		return -ENOMEM;
410 
411 	hdr = genlmsg_iput(msg, &info);
412 	if (!hdr) {
413 		ret = -EMSGSIZE;
414 		goto free_msg;
415 	}
416 
417 	ret = msg_put_error_event_attrs(msg, node, error_id, error_name, value);
418 	if (ret)
419 		goto cancel_msg;
420 
421 	genlmsg_end(msg, hdr);
422 	genlmsg_multicast_allns(&drm_ras_nl_family, msg, 0, DRM_RAS_NLGRP_ERROR_REPORT);
423 
424 	return 0;
425 
426 cancel_msg:
427 	genlmsg_cancel(msg, hdr);
428 free_msg:
429 	nlmsg_free(msg);
430 	return ret;
431 }
432 EXPORT_SYMBOL(drm_ras_nl_error_event);
433 
434 /**
435  * drm_ras_nl_get_error_counter_dumpit() - Dump all Error Counters
436  * @skb: Netlink message buffer
437  * @cb: Callback context for multi-part dumps
438  *
439  * Iterates over all error counters in a given Node and appends
440  * their attributes (ID, name, value) to the given netlink message buffer.
441  * Uses @cb->ctx to track progress in case the message buffer fills up, allowing
442  * multi-part dump support. On buffer overflow, updates the context to resume
443  * from the last node on the next invocation.
444  *
445  * Return: 0 if all errors fit in @skb, number of bytes added to @skb if
446  *          the buffer filled up (requires multi-part continuation), or
447  *          a negative error code on failure.
448  */
449 int drm_ras_nl_get_error_counter_dumpit(struct sk_buff *skb,
450 					struct netlink_callback *cb)
451 {
452 	const struct genl_info *info = genl_info_dump(cb);
453 	struct drm_ras_ctx *ctx = (void *)cb->ctx;
454 	struct drm_ras_node *node;
455 	struct nlattr *hdr;
456 	const char *error_name;
457 	u32 node_id, error_id, value;
458 	int ret;
459 
460 	if (!info->attrs || GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID))
461 		return -EINVAL;
462 
463 	node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
464 
465 	node = xa_load(&drm_ras_xa, node_id);
466 	if (!node)
467 		return -ENOENT;
468 
469 	for (error_id = max(node->error_counter_range.first, ctx->restart);
470 	     error_id <= node->error_counter_range.last;
471 	     error_id++) {
472 		ret = get_node_error_counter(node_id, error_id,
473 					     &error_name, &value);
474 		/*
475 		 * For non-contiguous range, driver return -ENOENT as indication
476 		 * to skip this ID when listing all errors.
477 		 */
478 		if (ret == -ENOENT)
479 			continue;
480 		if (ret)
481 			return ret;
482 
483 		hdr = genlmsg_iput(skb, info);
484 
485 		if (!hdr) {
486 			ret = -EMSGSIZE;
487 			break;
488 		}
489 
490 		ret = msg_reply_value(skb, error_id, error_name, value);
491 		if (ret) {
492 			genlmsg_cancel(skb, hdr);
493 			break;
494 		}
495 
496 		genlmsg_end(skb, hdr);
497 	}
498 
499 	if (ret == -EMSGSIZE)
500 		ctx->restart = error_id;
501 
502 	return ret;
503 }
504 
505 /**
506  * drm_ras_nl_get_error_counter_doit() - Query an error counter of an node
507  * @skb: Netlink message buffer
508  * @info: Generic Netlink info containing attributes of the request
509  *
510  * Extracts the node ID and error ID from the netlink attributes and
511  * retrieves the current value of the corresponding error counter. Sends the
512  * result back to the requesting user via the standard Genl reply.
513  *
514  * Return: 0 on success, or negative errno on failure.
515  */
516 int drm_ras_nl_get_error_counter_doit(struct sk_buff *skb,
517 				      struct genl_info *info)
518 {
519 	u32 node_id, error_id;
520 
521 	if (!info->attrs ||
522 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
523 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
524 		return -EINVAL;
525 
526 	node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
527 	error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
528 
529 	return doit_reply_value(info, node_id, error_id);
530 }
531 
532 /**
533  * drm_ras_nl_clear_error_counter_doit() - Clear an error counter of a node
534  * @skb: Netlink message buffer
535  * @info: Generic Netlink info containing attributes of the request
536  *
537  * Extracts the node ID and error ID from the netlink attributes and
538  * clears the current value.
539  *
540  * Return: 0 on success, or negative errno on failure.
541  */
542 int drm_ras_nl_clear_error_counter_doit(struct sk_buff *skb,
543 					struct genl_info *info)
544 {
545 	struct drm_ras_node *node;
546 	u32 node_id, error_id;
547 
548 	if (!info->attrs ||
549 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
550 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
551 		return -EINVAL;
552 
553 	node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
554 	error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
555 
556 	node = xa_load(&drm_ras_xa, node_id);
557 	if (!node || !node->clear_error_counter)
558 		return -ENOENT;
559 
560 	if (error_id < node->error_counter_range.first ||
561 	    error_id > node->error_counter_range.last)
562 		return -EINVAL;
563 
564 	return node->clear_error_counter(node, error_id);
565 }
566 
567 /**
568  * drm_ras_nl_get_error_threshold_doit() - Query error threshold of a counter
569  * @skb: Netlink message buffer
570  * @info: Generic Netlink info containing attributes of the request
571  *
572  * Extracts the Node ID and Error ID from the netlink attributes and retrieves
573  * the error threshold of the corresponding counter. Sends the result back to
574  * the requesting user via the standard Genl reply.
575  *
576  * Return: 0 on success, or negative errno on failure.
577  */
578 int drm_ras_nl_get_error_threshold_doit(struct sk_buff *skb, struct genl_info *info)
579 {
580 	u32 node_id, error_id;
581 
582 	if (!info->attrs ||
583 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
584 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID))
585 		return -EINVAL;
586 
587 	node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
588 	error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
589 
590 	return doit_reply_threshold(info, node_id, error_id);
591 }
592 
593 /**
594  * drm_ras_nl_set_error_threshold_doit() - Set error threshold of a counter
595  * @skb: Netlink message buffer
596  * @info: Generic Netlink info containing attributes of the request
597  *
598  * Extracts the Node ID, Error ID and threshold from the netlink attributes and
599  * sets the error threshold of the corresponding counter.
600  *
601  * Return: 0 on success, or negative errno on failure.
602  */
603 int drm_ras_nl_set_error_threshold_doit(struct sk_buff *skb, struct genl_info *info)
604 {
605 	u32 node_id, error_id, threshold;
606 
607 	if (!info->attrs ||
608 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID) ||
609 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID) ||
610 	    GENL_REQ_ATTR_CHECK(info, DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD))
611 		return -EINVAL;
612 
613 	node_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_NODE_ID]);
614 	error_id = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_ID]);
615 	threshold = nla_get_u32(info->attrs[DRM_RAS_A_ERROR_COUNTER_ATTRS_ERROR_THRESHOLD]);
616 
617 	return set_node_error_threshold(node_id, error_id, threshold);
618 }
619 
620 /**
621  * drm_ras_node_register() - Register a new RAS node
622  * @node: Node structure to register
623  *
624  * Adds the given RAS node to the global node xarray and assigns it
625  * a unique ID. Both @node->name and @node->type must be valid.
626  *
627  * Return: 0 on success, or negative errno on failure:
628  */
629 int drm_ras_node_register(struct drm_ras_node *node)
630 {
631 	if (!node->device_name || !node->node_name)
632 		return -EINVAL;
633 
634 	/* Currently, only Error Counter Endpoints are supported */
635 	if (node->type != DRM_RAS_NODE_TYPE_ERROR_COUNTER)
636 		return -EINVAL;
637 
638 	/* Mandatory entries for Error Counter Node */
639 	if (node->type == DRM_RAS_NODE_TYPE_ERROR_COUNTER &&
640 	    (!node->error_counter_range.last || !node->query_error_counter))
641 		return -EINVAL;
642 
643 	return xa_alloc(&drm_ras_xa, &node->id, node, xa_limit_32b, GFP_KERNEL);
644 }
645 EXPORT_SYMBOL(drm_ras_node_register);
646 
647 /**
648  * drm_ras_node_unregister() - Unregister a previously registered node
649  * @node: Node structure to unregister
650  *
651  * Removes the given node from the global node xarray using its ID.
652  */
653 void drm_ras_node_unregister(struct drm_ras_node *node)
654 {
655 	xa_erase(&drm_ras_xa, node->id);
656 }
657 EXPORT_SYMBOL(drm_ras_node_unregister);
658