xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/radeon/mkregtable.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: MIT
2*4882a593Smuzhiyun /* utility to create the register check tables
3*4882a593Smuzhiyun  * this includes inlined list.h safe for userspace.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2009 Jerome Glisse
6*4882a593Smuzhiyun  * Copyright 2009 Red Hat Inc.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Authors:
9*4882a593Smuzhiyun  * 	Jerome Glisse
10*4882a593Smuzhiyun  * 	Dave Airlie
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <sys/types.h>
14*4882a593Smuzhiyun #include <stdlib.h>
15*4882a593Smuzhiyun #include <string.h>
16*4882a593Smuzhiyun #include <stdio.h>
17*4882a593Smuzhiyun #include <regex.h>
18*4882a593Smuzhiyun #include <libgen.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
21*4882a593Smuzhiyun /**
22*4882a593Smuzhiyun  * container_of - cast a member of a structure out to the containing structure
23*4882a593Smuzhiyun  * @ptr:    the pointer to the member.
24*4882a593Smuzhiyun  * @type:   the type of the container struct this is embedded in.
25*4882a593Smuzhiyun  * @member: the name of the member within the struct.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun #define container_of(ptr, type, member) ({          \
29*4882a593Smuzhiyun 	const typeof(((type *)0)->member)*__mptr = (ptr);    \
30*4882a593Smuzhiyun 		     (type *)((char *)__mptr - offsetof(type, member)); })
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * Simple doubly linked list implementation.
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Some of the internal functions ("__xxx") are useful when
36*4882a593Smuzhiyun  * manipulating whole lists rather than single entries, as
37*4882a593Smuzhiyun  * sometimes we already know the next/prev entries and we can
38*4882a593Smuzhiyun  * generate better code by using them directly rather than
39*4882a593Smuzhiyun  * using the generic single-entry routines.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun struct list_head {
43*4882a593Smuzhiyun 	struct list_head *next, *prev;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 
INIT_LIST_HEAD(struct list_head * list)47*4882a593Smuzhiyun static inline void INIT_LIST_HEAD(struct list_head *list)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	list->next = list;
50*4882a593Smuzhiyun 	list->prev = list;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * Insert a new entry between two known consecutive entries.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * This is only for internal list manipulation where we know
57*4882a593Smuzhiyun  * the prev/next entries already!
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun #ifndef CONFIG_DEBUG_LIST
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)60*4882a593Smuzhiyun static inline void __list_add(struct list_head *new,
61*4882a593Smuzhiyun 			      struct list_head *prev, struct list_head *next)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	next->prev = new;
64*4882a593Smuzhiyun 	new->next = next;
65*4882a593Smuzhiyun 	new->prev = prev;
66*4882a593Smuzhiyun 	prev->next = new;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun #else
69*4882a593Smuzhiyun extern void __list_add(struct list_head *new,
70*4882a593Smuzhiyun 		       struct list_head *prev, struct list_head *next);
71*4882a593Smuzhiyun #endif
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /**
74*4882a593Smuzhiyun  * list_add_tail - add a new entry
75*4882a593Smuzhiyun  * @new: new entry to be added
76*4882a593Smuzhiyun  * @head: list head to add it before
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Insert a new entry before the specified head.
79*4882a593Smuzhiyun  * This is useful for implementing queues.
80*4882a593Smuzhiyun  */
list_add_tail(struct list_head * new,struct list_head * head)81*4882a593Smuzhiyun static inline void list_add_tail(struct list_head *new, struct list_head *head)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	__list_add(new, head->prev, head);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /**
87*4882a593Smuzhiyun  * list_entry - get the struct for this entry
88*4882a593Smuzhiyun  * @ptr:	the &struct list_head pointer.
89*4882a593Smuzhiyun  * @type:	the type of the struct this is embedded in.
90*4882a593Smuzhiyun  * @member:	the name of the list_head within the struct.
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun #define list_entry(ptr, type, member) \
93*4882a593Smuzhiyun 	container_of(ptr, type, member)
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun  * list_for_each_entry	-	iterate over list of given type
97*4882a593Smuzhiyun  * @pos:	the type * to use as a loop cursor.
98*4882a593Smuzhiyun  * @head:	the head for your list.
99*4882a593Smuzhiyun  * @member:	the name of the list_head within the struct.
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun #define list_for_each_entry(pos, head, member)				\
102*4882a593Smuzhiyun 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
103*4882a593Smuzhiyun 	     &pos->member != (head); 	\
104*4882a593Smuzhiyun 	     pos = list_entry(pos->member.next, typeof(*pos), member))
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun struct offset {
107*4882a593Smuzhiyun 	struct list_head list;
108*4882a593Smuzhiyun 	unsigned offset;
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun struct table {
112*4882a593Smuzhiyun 	struct list_head offsets;
113*4882a593Smuzhiyun 	unsigned offset_max;
114*4882a593Smuzhiyun 	unsigned nentry;
115*4882a593Smuzhiyun 	unsigned *table;
116*4882a593Smuzhiyun 	char *gpu_prefix;
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
offset_new(unsigned o)119*4882a593Smuzhiyun static struct offset *offset_new(unsigned o)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct offset *offset;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	offset = (struct offset *)malloc(sizeof(struct offset));
124*4882a593Smuzhiyun 	if (offset) {
125*4882a593Smuzhiyun 		INIT_LIST_HEAD(&offset->list);
126*4882a593Smuzhiyun 		offset->offset = o;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 	return offset;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
table_offset_add(struct table * t,struct offset * offset)131*4882a593Smuzhiyun static void table_offset_add(struct table *t, struct offset *offset)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	list_add_tail(&offset->list, &t->offsets);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
table_init(struct table * t)136*4882a593Smuzhiyun static void table_init(struct table *t)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	INIT_LIST_HEAD(&t->offsets);
139*4882a593Smuzhiyun 	t->offset_max = 0;
140*4882a593Smuzhiyun 	t->nentry = 0;
141*4882a593Smuzhiyun 	t->table = NULL;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
table_print(struct table * t)144*4882a593Smuzhiyun static void table_print(struct table *t)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	unsigned nlloop, i, j, n, c, id;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	nlloop = (t->nentry + 3) / 4;
149*4882a593Smuzhiyun 	c = t->nentry;
150*4882a593Smuzhiyun 	printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
151*4882a593Smuzhiyun 	       t->nentry);
152*4882a593Smuzhiyun 	for (i = 0, id = 0; i < nlloop; i++) {
153*4882a593Smuzhiyun 		n = 4;
154*4882a593Smuzhiyun 		if (n > c)
155*4882a593Smuzhiyun 			n = c;
156*4882a593Smuzhiyun 		c -= n;
157*4882a593Smuzhiyun 		for (j = 0; j < n; j++) {
158*4882a593Smuzhiyun 			if (j == 0)
159*4882a593Smuzhiyun 				printf("\t");
160*4882a593Smuzhiyun 			else
161*4882a593Smuzhiyun 				printf(" ");
162*4882a593Smuzhiyun 			printf("0x%08X,", t->table[id++]);
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 		printf("\n");
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 	printf("};\n");
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
table_build(struct table * t)169*4882a593Smuzhiyun static int table_build(struct table *t)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct offset *offset;
172*4882a593Smuzhiyun 	unsigned i, m;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	t->nentry = ((t->offset_max >> 2) + 31) / 32;
175*4882a593Smuzhiyun 	t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
176*4882a593Smuzhiyun 	if (t->table == NULL)
177*4882a593Smuzhiyun 		return -1;
178*4882a593Smuzhiyun 	memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
179*4882a593Smuzhiyun 	list_for_each_entry(offset, &t->offsets, list) {
180*4882a593Smuzhiyun 		i = (offset->offset >> 2) / 32;
181*4882a593Smuzhiyun 		m = (offset->offset >> 2) & 31;
182*4882a593Smuzhiyun 		m = 1 << m;
183*4882a593Smuzhiyun 		t->table[i] ^= m;
184*4882a593Smuzhiyun 	}
185*4882a593Smuzhiyun 	return 0;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun static char gpu_name[10];
parser_auth(struct table * t,const char * filename)189*4882a593Smuzhiyun static int parser_auth(struct table *t, const char *filename)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	FILE *file;
192*4882a593Smuzhiyun 	regex_t mask_rex;
193*4882a593Smuzhiyun 	regmatch_t match[4];
194*4882a593Smuzhiyun 	char buf[1024];
195*4882a593Smuzhiyun 	size_t end;
196*4882a593Smuzhiyun 	int len;
197*4882a593Smuzhiyun 	int done = 0;
198*4882a593Smuzhiyun 	int r;
199*4882a593Smuzhiyun 	unsigned o;
200*4882a593Smuzhiyun 	struct offset *offset;
201*4882a593Smuzhiyun 	char last_reg_s[10];
202*4882a593Smuzhiyun 	int last_reg;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (regcomp
205*4882a593Smuzhiyun 	    (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
206*4882a593Smuzhiyun 		fprintf(stderr, "Failed to compile regular expression\n");
207*4882a593Smuzhiyun 		return -1;
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 	file = fopen(filename, "r");
210*4882a593Smuzhiyun 	if (file == NULL) {
211*4882a593Smuzhiyun 		fprintf(stderr, "Failed to open: %s\n", filename);
212*4882a593Smuzhiyun 		return -1;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 	fseek(file, 0, SEEK_END);
215*4882a593Smuzhiyun 	end = ftell(file);
216*4882a593Smuzhiyun 	fseek(file, 0, SEEK_SET);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* get header */
219*4882a593Smuzhiyun 	if (fgets(buf, 1024, file) == NULL) {
220*4882a593Smuzhiyun 		fclose(file);
221*4882a593Smuzhiyun 		return -1;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	/* first line will contain the last register
225*4882a593Smuzhiyun 	 * and gpu name */
226*4882a593Smuzhiyun 	sscanf(buf, "%9s %9s", gpu_name, last_reg_s);
227*4882a593Smuzhiyun 	t->gpu_prefix = gpu_name;
228*4882a593Smuzhiyun 	last_reg = strtol(last_reg_s, NULL, 16);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	do {
231*4882a593Smuzhiyun 		if (fgets(buf, 1024, file) == NULL) {
232*4882a593Smuzhiyun 			fclose(file);
233*4882a593Smuzhiyun 			return -1;
234*4882a593Smuzhiyun 		}
235*4882a593Smuzhiyun 		len = strlen(buf);
236*4882a593Smuzhiyun 		if (ftell(file) == end)
237*4882a593Smuzhiyun 			done = 1;
238*4882a593Smuzhiyun 		if (len) {
239*4882a593Smuzhiyun 			r = regexec(&mask_rex, buf, 4, match, 0);
240*4882a593Smuzhiyun 			if (r == REG_NOMATCH) {
241*4882a593Smuzhiyun 			} else if (r) {
242*4882a593Smuzhiyun 				fprintf(stderr,
243*4882a593Smuzhiyun 					"Error matching regular expression %d in %s\n",
244*4882a593Smuzhiyun 					r, filename);
245*4882a593Smuzhiyun 				fclose(file);
246*4882a593Smuzhiyun 				return -1;
247*4882a593Smuzhiyun 			} else {
248*4882a593Smuzhiyun 				buf[match[0].rm_eo] = 0;
249*4882a593Smuzhiyun 				buf[match[1].rm_eo] = 0;
250*4882a593Smuzhiyun 				buf[match[2].rm_eo] = 0;
251*4882a593Smuzhiyun 				o = strtol(&buf[match[1].rm_so], NULL, 16);
252*4882a593Smuzhiyun 				offset = offset_new(o);
253*4882a593Smuzhiyun 				table_offset_add(t, offset);
254*4882a593Smuzhiyun 				if (o > t->offset_max)
255*4882a593Smuzhiyun 					t->offset_max = o;
256*4882a593Smuzhiyun 			}
257*4882a593Smuzhiyun 		}
258*4882a593Smuzhiyun 	} while (!done);
259*4882a593Smuzhiyun 	fclose(file);
260*4882a593Smuzhiyun 	if (t->offset_max < last_reg)
261*4882a593Smuzhiyun 		t->offset_max = last_reg;
262*4882a593Smuzhiyun 	return table_build(t);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
main(int argc,char * argv[])265*4882a593Smuzhiyun int main(int argc, char *argv[])
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	struct table t;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (argc != 2) {
270*4882a593Smuzhiyun 		fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
271*4882a593Smuzhiyun 		exit(1);
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 	table_init(&t);
274*4882a593Smuzhiyun 	if (parser_auth(&t, argv[1])) {
275*4882a593Smuzhiyun 		fprintf(stderr, "Failed to parse file %s\n", argv[1]);
276*4882a593Smuzhiyun 		return -1;
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 	table_print(&t);
279*4882a593Smuzhiyun 	return 0;
280*4882a593Smuzhiyun }
281