xref: /gfx-drm/usr/src/uts/common/drm/drm_sun_idr.h (revision 47dc10d7)
1 /*
2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /*
25  * Copyright (c) 2012 Intel Corporation.  All rights reserved.
26  */
27 
28 #ifndef __DRM_IDR_H__
29 #define __DRM_IDR_H__
30 
31 #include <sys/avl.h>
32 
33 struct idr_used_id {
34 	struct avl_node link;
35 	uint32_t id;
36 	void *obj;
37 };
38 
39 struct idr_free_id {
40 	struct idr_free_id *next;
41 	uint32_t id;
42 };
43 
44 struct idr_free_id_range {
45 	struct idr_free_id_range *next;
46 	uint32_t start;
47 	uint32_t end;
48 	uint32_t min_unused_id;
49 	struct idr_free_id *free_ids;
50 };
51 
52 struct idr {
53 	struct avl_tree used_ids;
54 	struct idr_free_id_range *free_id_ranges;
55 	kmutex_t lock;
56 };
57 
58 extern void idr_init(struct idr *idrp);
59 extern int idr_get_new_above(struct idr *idrp, void *obj, int start, int *newid);
60 extern void* idr_find(struct idr *idrp, uint32_t id);
61 extern int idr_remove(struct idr *idrp, uint32_t id);
62 extern void* idr_replace(struct idr *idrp, void *obj, uint32_t id);
63 extern int idr_pre_get(struct idr *idrp, int flag);
64 extern int idr_for_each(struct idr *idrp, int (*fn)(int id, void *obj, void *data), void *data);
65 extern void idr_remove_all(struct idr *idrp);
66 extern void idr_destroy(struct idr* idrp);
67 
68 #define DRM_GEM_OBJIDR_HASHNODE 1024
69 
70 struct idr_list {
71 	struct idr_list *next, *prev;
72 	void *obj;
73 	uint32_t	handle;
74 	caddr_t	contain_ptr;
75 };
76 
77 #define	idr_list_for_each(entry, head) \
78 	for (int key = 0; key < DRM_GEM_OBJIDR_HASHNODE; key++) \
79 		list_for_each(entry, &(head)->next[key])
80 
81 extern int idr_list_pre_get(struct idr_list *head, int flag);
82 extern void idr_list_init(struct idr_list *head);
83 extern int idr_list_get_new_above(struct idr_list *head,
84 				void *obj,
85 				int *handlep);
86 extern void *idr_list_find(struct idr_list *head, uint32_t name);
87 extern int idr_list_remove(struct idr_list *head, uint32_t name);
88 extern void idr_list_free(struct idr_list *head);
89 extern int idr_list_empty(struct idr_list *head);
90 #endif /* __DRM_IDR_H__ */
91