xref: /openbsd/sys/dev/pci/drm/radeon/mkregtable.c (revision 264ca280)
1 /*	$OpenBSD: mkregtable.c,v 1.1 2013/08/12 04:11:53 jsg Exp $	*/
2 /* utility to create the register check tables
3  * this includes inlined list.h safe for userspace.
4  *
5  * Copyright 2009 Jerome Glisse
6  * Copyright 2009 Red Hat Inc.
7  *
8  * Authors:
9  * 	Jerome Glisse
10  * 	Dave Airlie
11  */
12 
13 #include <sys/types.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <regex.h>
18 #include <libgen.h>
19 
20 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
21 /**
22  * container_of - cast a member of a structure out to the containing structure
23  * @ptr:    the pointer to the member.
24  * @type:   the type of the container struct this is embedded in.
25  * @member: the name of the member within the struct.
26  *
27  */
28 #define container_of(ptr, type, member) ({          \
29 	const typeof(((type *)0)->member)*__mptr = (ptr);    \
30 		     (type *)((char *)__mptr - offsetof(type, member)); })
31 
32 /*
33  * Simple doubly linked list implementation.
34  *
35  * Some of the internal functions ("__xxx") are useful when
36  * manipulating whole lists rather than single entries, as
37  * sometimes we already know the next/prev entries and we can
38  * generate better code by using them directly rather than
39  * using the generic single-entry routines.
40  */
41 
42 struct list_head {
43 	struct list_head *next, *prev;
44 };
45 
46 #define LIST_HEAD_INIT(name) { &(name), &(name) }
47 
48 #define LIST_HEAD(name) \
49 	struct list_head name = LIST_HEAD_INIT(name)
50 
51 static inline void INIT_LIST_HEAD(struct list_head *list)
52 {
53 	list->next = list;
54 	list->prev = list;
55 }
56 
57 /*
58  * Insert a new entry between two known consecutive entries.
59  *
60  * This is only for internal list manipulation where we know
61  * the prev/next entries already!
62  */
63 #ifndef CONFIG_DEBUG_LIST
64 static inline void __list_add(struct list_head *new,
65 			      struct list_head *prev, struct list_head *next)
66 {
67 	next->prev = new;
68 	new->next = next;
69 	new->prev = prev;
70 	prev->next = new;
71 }
72 #else
73 extern void __list_add(struct list_head *new,
74 		       struct list_head *prev, struct list_head *next);
75 #endif
76 
77 /**
78  * list_add - add a new entry
79  * @new: new entry to be added
80  * @head: list head to add it after
81  *
82  * Insert a new entry after the specified head.
83  * This is good for implementing stacks.
84  */
85 static inline void list_add(struct list_head *new, struct list_head *head)
86 {
87 	__list_add(new, head, head->next);
88 }
89 
90 /**
91  * list_add_tail - add a new entry
92  * @new: new entry to be added
93  * @head: list head to add it before
94  *
95  * Insert a new entry before the specified head.
96  * This is useful for implementing queues.
97  */
98 static inline void list_add_tail(struct list_head *new, struct list_head *head)
99 {
100 	__list_add(new, head->prev, head);
101 }
102 
103 /*
104  * Delete a list entry by making the prev/next entries
105  * point to each other.
106  *
107  * This is only for internal list manipulation where we know
108  * the prev/next entries already!
109  */
110 static inline void __list_del(struct list_head *prev, struct list_head *next)
111 {
112 	next->prev = prev;
113 	prev->next = next;
114 }
115 
116 /**
117  * list_del - deletes entry from list.
118  * @entry: the element to delete from the list.
119  * Note: list_empty() on entry does not return true after this, the entry is
120  * in an undefined state.
121  */
122 #ifndef CONFIG_DEBUG_LIST
123 static inline void list_del(struct list_head *entry)
124 {
125 	__list_del(entry->prev, entry->next);
126 	entry->next = (void *)0xDEADBEEF;
127 	entry->prev = (void *)0xBEEFDEAD;
128 }
129 #else
130 extern void list_del(struct list_head *entry);
131 #endif
132 
133 /**
134  * list_replace - replace old entry by new one
135  * @old : the element to be replaced
136  * @new : the new element to insert
137  *
138  * If @old was empty, it will be overwritten.
139  */
140 static inline void list_replace(struct list_head *old, struct list_head *new)
141 {
142 	new->next = old->next;
143 	new->next->prev = new;
144 	new->prev = old->prev;
145 	new->prev->next = new;
146 }
147 
148 static inline void list_replace_init(struct list_head *old,
149 				     struct list_head *new)
150 {
151 	list_replace(old, new);
152 	INIT_LIST_HEAD(old);
153 }
154 
155 /**
156  * list_del_init - deletes entry from list and reinitialize it.
157  * @entry: the element to delete from the list.
158  */
159 static inline void list_del_init(struct list_head *entry)
160 {
161 	__list_del(entry->prev, entry->next);
162 	INIT_LIST_HEAD(entry);
163 }
164 
165 /**
166  * list_move - delete from one list and add as another's head
167  * @list: the entry to move
168  * @head: the head that will precede our entry
169  */
170 static inline void list_move(struct list_head *list, struct list_head *head)
171 {
172 	__list_del(list->prev, list->next);
173 	list_add(list, head);
174 }
175 
176 /**
177  * list_move_tail - delete from one list and add as another's tail
178  * @list: the entry to move
179  * @head: the head that will follow our entry
180  */
181 static inline void list_move_tail(struct list_head *list,
182 				  struct list_head *head)
183 {
184 	__list_del(list->prev, list->next);
185 	list_add_tail(list, head);
186 }
187 
188 /**
189  * list_is_last - tests whether @list is the last entry in list @head
190  * @list: the entry to test
191  * @head: the head of the list
192  */
193 static inline int list_is_last(const struct list_head *list,
194 			       const struct list_head *head)
195 {
196 	return list->next == head;
197 }
198 
199 /**
200  * list_empty - tests whether a list is empty
201  * @head: the list to test.
202  */
203 static inline int list_empty(const struct list_head *head)
204 {
205 	return head->next == head;
206 }
207 
208 /**
209  * list_empty_careful - tests whether a list is empty and not being modified
210  * @head: the list to test
211  *
212  * Description:
213  * tests whether a list is empty _and_ checks that no other CPU might be
214  * in the process of modifying either member (next or prev)
215  *
216  * NOTE: using list_empty_careful() without synchronization
217  * can only be safe if the only activity that can happen
218  * to the list entry is list_del_init(). Eg. it cannot be used
219  * if another CPU could re-list_add() it.
220  */
221 static inline int list_empty_careful(const struct list_head *head)
222 {
223 	struct list_head *next = head->next;
224 	return (next == head) && (next == head->prev);
225 }
226 
227 /**
228  * list_is_singular - tests whether a list has just one entry.
229  * @head: the list to test.
230  */
231 static inline int list_is_singular(const struct list_head *head)
232 {
233 	return !list_empty(head) && (head->next == head->prev);
234 }
235 
236 static inline void __list_cut_position(struct list_head *list,
237 				       struct list_head *head,
238 				       struct list_head *entry)
239 {
240 	struct list_head *new_first = entry->next;
241 	list->next = head->next;
242 	list->next->prev = list;
243 	list->prev = entry;
244 	entry->next = list;
245 	head->next = new_first;
246 	new_first->prev = head;
247 }
248 
249 /**
250  * list_cut_position - cut a list into two
251  * @list: a new list to add all removed entries
252  * @head: a list with entries
253  * @entry: an entry within head, could be the head itself
254  *	and if so we won't cut the list
255  *
256  * This helper moves the initial part of @head, up to and
257  * including @entry, from @head to @list. You should
258  * pass on @entry an element you know is on @head. @list
259  * should be an empty list or a list you do not care about
260  * losing its data.
261  *
262  */
263 static inline void list_cut_position(struct list_head *list,
264 				     struct list_head *head,
265 				     struct list_head *entry)
266 {
267 	if (list_empty(head))
268 		return;
269 	if (list_is_singular(head) && (head->next != entry && head != entry))
270 		return;
271 	if (entry == head)
272 		INIT_LIST_HEAD(list);
273 	else
274 		__list_cut_position(list, head, entry);
275 }
276 
277 static inline void __list_splice(const struct list_head *list,
278 				 struct list_head *prev, struct list_head *next)
279 {
280 	struct list_head *first = list->next;
281 	struct list_head *last = list->prev;
282 
283 	first->prev = prev;
284 	prev->next = first;
285 
286 	last->next = next;
287 	next->prev = last;
288 }
289 
290 /**
291  * list_splice - join two lists, this is designed for stacks
292  * @list: the new list to add.
293  * @head: the place to add it in the first list.
294  */
295 static inline void list_splice(const struct list_head *list,
296 			       struct list_head *head)
297 {
298 	if (!list_empty(list))
299 		__list_splice(list, head, head->next);
300 }
301 
302 /**
303  * list_splice_tail - join two lists, each list being a queue
304  * @list: the new list to add.
305  * @head: the place to add it in the first list.
306  */
307 static inline void list_splice_tail(struct list_head *list,
308 				    struct list_head *head)
309 {
310 	if (!list_empty(list))
311 		__list_splice(list, head->prev, head);
312 }
313 
314 /**
315  * list_splice_init - join two lists and reinitialise the emptied list.
316  * @list: the new list to add.
317  * @head: the place to add it in the first list.
318  *
319  * The list at @list is reinitialised
320  */
321 static inline void list_splice_init(struct list_head *list,
322 				    struct list_head *head)
323 {
324 	if (!list_empty(list)) {
325 		__list_splice(list, head, head->next);
326 		INIT_LIST_HEAD(list);
327 	}
328 }
329 
330 /**
331  * list_splice_tail_init - join two lists and reinitialise the emptied list
332  * @list: the new list to add.
333  * @head: the place to add it in the first list.
334  *
335  * Each of the lists is a queue.
336  * The list at @list is reinitialised
337  */
338 static inline void list_splice_tail_init(struct list_head *list,
339 					 struct list_head *head)
340 {
341 	if (!list_empty(list)) {
342 		__list_splice(list, head->prev, head);
343 		INIT_LIST_HEAD(list);
344 	}
345 }
346 
347 /**
348  * list_entry - get the struct for this entry
349  * @ptr:	the &struct list_head pointer.
350  * @type:	the type of the struct this is embedded in.
351  * @member:	the name of the list_struct within the struct.
352  */
353 #define list_entry(ptr, type, member) \
354 	container_of(ptr, type, member)
355 
356 /**
357  * list_first_entry - get the first element from a list
358  * @ptr:	the list head to take the element from.
359  * @type:	the type of the struct this is embedded in.
360  * @member:	the name of the list_struct within the struct.
361  *
362  * Note, that list is expected to be not empty.
363  */
364 #define list_first_entry(ptr, type, member) \
365 	list_entry((ptr)->next, type, member)
366 
367 /**
368  * list_for_each	-	iterate over a list
369  * @pos:	the &struct list_head to use as a loop cursor.
370  * @head:	the head for your list.
371  */
372 #define list_for_each(pos, head) \
373 	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
374 		pos = pos->next)
375 
376 /**
377  * __list_for_each	-	iterate over a list
378  * @pos:	the &struct list_head to use as a loop cursor.
379  * @head:	the head for your list.
380  *
381  * This variant differs from list_for_each() in that it's the
382  * simplest possible list iteration code, no prefetching is done.
383  * Use this for code that knows the list to be very short (empty
384  * or 1 entry) most of the time.
385  */
386 #define __list_for_each(pos, head) \
387 	for (pos = (head)->next; pos != (head); pos = pos->next)
388 
389 /**
390  * list_for_each_prev	-	iterate over a list backwards
391  * @pos:	the &struct list_head to use as a loop cursor.
392  * @head:	the head for your list.
393  */
394 #define list_for_each_prev(pos, head) \
395 	for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
396 		pos = pos->prev)
397 
398 /**
399  * list_for_each_safe - iterate over a list safe against removal of list entry
400  * @pos:	the &struct list_head to use as a loop cursor.
401  * @n:		another &struct list_head to use as temporary storage
402  * @head:	the head for your list.
403  */
404 #define list_for_each_safe(pos, n, head) \
405 	for (pos = (head)->next, n = pos->next; pos != (head); \
406 		pos = n, n = pos->next)
407 
408 /**
409  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
410  * @pos:	the &struct list_head to use as a loop cursor.
411  * @n:		another &struct list_head to use as temporary storage
412  * @head:	the head for your list.
413  */
414 #define list_for_each_prev_safe(pos, n, head) \
415 	for (pos = (head)->prev, n = pos->prev; \
416 	     prefetch(pos->prev), pos != (head); \
417 	     pos = n, n = pos->prev)
418 
419 /**
420  * list_for_each_entry	-	iterate over list of given type
421  * @pos:	the type * to use as a loop cursor.
422  * @head:	the head for your list.
423  * @member:	the name of the list_struct within the struct.
424  */
425 #define list_for_each_entry(pos, head, member)				\
426 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
427 	     &pos->member != (head); 	\
428 	     pos = list_entry(pos->member.next, typeof(*pos), member))
429 
430 /**
431  * list_for_each_entry_reverse - iterate backwards over list of given type.
432  * @pos:	the type * to use as a loop cursor.
433  * @head:	the head for your list.
434  * @member:	the name of the list_struct within the struct.
435  */
436 #define list_for_each_entry_reverse(pos, head, member)			\
437 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
438 	     prefetch(pos->member.prev), &pos->member != (head); 	\
439 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
440 
441 /**
442  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
443  * @pos:	the type * to use as a start point
444  * @head:	the head of the list
445  * @member:	the name of the list_struct within the struct.
446  *
447  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
448  */
449 #define list_prepare_entry(pos, head, member) \
450 	((pos) ? : list_entry(head, typeof(*pos), member))
451 
452 /**
453  * list_for_each_entry_continue - continue iteration over list of given type
454  * @pos:	the type * to use as a loop cursor.
455  * @head:	the head for your list.
456  * @member:	the name of the list_struct within the struct.
457  *
458  * Continue to iterate over list of given type, continuing after
459  * the current position.
460  */
461 #define list_for_each_entry_continue(pos, head, member) 		\
462 	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
463 	     prefetch(pos->member.next), &pos->member != (head);	\
464 	     pos = list_entry(pos->member.next, typeof(*pos), member))
465 
466 /**
467  * list_for_each_entry_continue_reverse - iterate backwards from the given point
468  * @pos:	the type * to use as a loop cursor.
469  * @head:	the head for your list.
470  * @member:	the name of the list_struct within the struct.
471  *
472  * Start to iterate over list of given type backwards, continuing after
473  * the current position.
474  */
475 #define list_for_each_entry_continue_reverse(pos, head, member)		\
476 	for (pos = list_entry(pos->member.prev, typeof(*pos), member);	\
477 	     prefetch(pos->member.prev), &pos->member != (head);	\
478 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
479 
480 /**
481  * list_for_each_entry_from - iterate over list of given type from the current point
482  * @pos:	the type * to use as a loop cursor.
483  * @head:	the head for your list.
484  * @member:	the name of the list_struct within the struct.
485  *
486  * Iterate over list of given type, continuing from current position.
487  */
488 #define list_for_each_entry_from(pos, head, member) 			\
489 	for (; prefetch(pos->member.next), &pos->member != (head);	\
490 	     pos = list_entry(pos->member.next, typeof(*pos), member))
491 
492 /**
493  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
494  * @pos:	the type * to use as a loop cursor.
495  * @n:		another type * to use as temporary storage
496  * @head:	the head for your list.
497  * @member:	the name of the list_struct within the struct.
498  */
499 #define list_for_each_entry_safe(pos, n, head, member)			\
500 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
501 		n = list_entry(pos->member.next, typeof(*pos), member);	\
502 	     &pos->member != (head); 					\
503 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
504 
505 /**
506  * list_for_each_entry_safe_continue
507  * @pos:	the type * to use as a loop cursor.
508  * @n:		another type * to use as temporary storage
509  * @head:	the head for your list.
510  * @member:	the name of the list_struct within the struct.
511  *
512  * Iterate over list of given type, continuing after current point,
513  * safe against removal of list entry.
514  */
515 #define list_for_each_entry_safe_continue(pos, n, head, member) 		\
516 	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
517 		n = list_entry(pos->member.next, typeof(*pos), member);		\
518 	     &pos->member != (head);						\
519 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
520 
521 /**
522  * list_for_each_entry_safe_from
523  * @pos:	the type * to use as a loop cursor.
524  * @n:		another type * to use as temporary storage
525  * @head:	the head for your list.
526  * @member:	the name of the list_struct within the struct.
527  *
528  * Iterate over list of given type from current point, safe against
529  * removal of list entry.
530  */
531 #define list_for_each_entry_safe_from(pos, n, head, member) 			\
532 	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
533 	     &pos->member != (head);						\
534 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
535 
536 /**
537  * list_for_each_entry_safe_reverse
538  * @pos:	the type * to use as a loop cursor.
539  * @n:		another type * to use as temporary storage
540  * @head:	the head for your list.
541  * @member:	the name of the list_struct within the struct.
542  *
543  * Iterate backwards over list of given type, safe against removal
544  * of list entry.
545  */
546 #define list_for_each_entry_safe_reverse(pos, n, head, member)		\
547 	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
548 		n = list_entry(pos->member.prev, typeof(*pos), member);	\
549 	     &pos->member != (head); 					\
550 	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
551 
552 struct offset {
553 	struct list_head list;
554 	unsigned offset;
555 };
556 
557 struct table {
558 	struct list_head offsets;
559 	unsigned offset_max;
560 	unsigned nentry;
561 	unsigned *table;
562 	char *gpu_prefix;
563 };
564 
565 static struct offset *offset_new(unsigned o)
566 {
567 	struct offset *offset;
568 
569 	offset = (struct offset *)malloc(sizeof(struct offset));
570 	if (offset) {
571 		INIT_LIST_HEAD(&offset->list);
572 		offset->offset = o;
573 	}
574 	return offset;
575 }
576 
577 static void table_offset_add(struct table *t, struct offset *offset)
578 {
579 	list_add_tail(&offset->list, &t->offsets);
580 }
581 
582 static void table_init(struct table *t)
583 {
584 	INIT_LIST_HEAD(&t->offsets);
585 	t->offset_max = 0;
586 	t->nentry = 0;
587 	t->table = NULL;
588 }
589 
590 static void table_print(struct table *t)
591 {
592 	unsigned nlloop, i, j, n, c, id;
593 
594 	nlloop = (t->nentry + 3) / 4;
595 	c = t->nentry;
596 	printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
597 	       t->nentry);
598 	for (i = 0, id = 0; i < nlloop; i++) {
599 		n = 4;
600 		if (n > c)
601 			n = c;
602 		c -= n;
603 		for (j = 0; j < n; j++) {
604 			if (j == 0)
605 				printf("\t");
606 			else
607 				printf(" ");
608 			printf("0x%08X,", t->table[id++]);
609 		}
610 		printf("\n");
611 	}
612 	printf("};\n");
613 }
614 
615 static int table_build(struct table *t)
616 {
617 	struct offset *offset;
618 	unsigned i, m;
619 
620 	t->nentry = ((t->offset_max >> 2) + 31) / 32;
621 	t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
622 	if (t->table == NULL)
623 		return -1;
624 	memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
625 	list_for_each_entry(offset, &t->offsets, list) {
626 		i = (offset->offset >> 2) / 32;
627 		m = (offset->offset >> 2) & 31;
628 		m = 1 << m;
629 		t->table[i] ^= m;
630 	}
631 	return 0;
632 }
633 
634 static char gpu_name[10];
635 static int parser_auth(struct table *t, const char *filename)
636 {
637 	FILE *file;
638 	regex_t mask_rex;
639 	regmatch_t match[4];
640 	char buf[1024];
641 	size_t end;
642 	int len;
643 	int done = 0;
644 	int r;
645 	unsigned o;
646 	struct offset *offset;
647 	char last_reg_s[10];
648 	int last_reg;
649 
650 	if (regcomp
651 	    (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
652 		fprintf(stderr, "Failed to compile regular expression\n");
653 		return -1;
654 	}
655 	file = fopen(filename, "r");
656 	if (file == NULL) {
657 		fprintf(stderr, "Failed to open: %s\n", filename);
658 		return -1;
659 	}
660 	fseek(file, 0, SEEK_END);
661 	end = ftell(file);
662 	fseek(file, 0, SEEK_SET);
663 
664 	/* get header */
665 	if (fgets(buf, 1024, file) == NULL) {
666 		fclose(file);
667 		return -1;
668 	}
669 
670 	/* first line will contain the last register
671 	 * and gpu name */
672 	sscanf(buf, "%s %s", gpu_name, last_reg_s);
673 	t->gpu_prefix = gpu_name;
674 	last_reg = strtol(last_reg_s, NULL, 16);
675 
676 	do {
677 		if (fgets(buf, 1024, file) == NULL) {
678 			fclose(file);
679 			return -1;
680 		}
681 		len = strlen(buf);
682 		if (ftell(file) == end)
683 			done = 1;
684 		if (len) {
685 			r = regexec(&mask_rex, buf, 4, match, 0);
686 			if (r == REG_NOMATCH) {
687 			} else if (r) {
688 				fprintf(stderr,
689 					"Error matching regular expression %d in %s\n",
690 					r, filename);
691 				fclose(file);
692 				return -1;
693 			} else {
694 				buf[match[0].rm_eo] = 0;
695 				buf[match[1].rm_eo] = 0;
696 				buf[match[2].rm_eo] = 0;
697 				o = strtol(&buf[match[1].rm_so], NULL, 16);
698 				offset = offset_new(o);
699 				table_offset_add(t, offset);
700 				if (o > t->offset_max)
701 					t->offset_max = o;
702 			}
703 		}
704 	} while (!done);
705 	fclose(file);
706 	if (t->offset_max < last_reg)
707 		t->offset_max = last_reg;
708 	return table_build(t);
709 }
710 
711 int main(int argc, char *argv[])
712 {
713 	struct table t;
714 
715 	if (argc != 2) {
716 		fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
717 		exit(1);
718 	}
719 	table_init(&t);
720 	if (parser_auth(&t, argv[1])) {
721 		fprintf(stderr, "Failed to parse file %s\n", argv[1]);
722 		return -1;
723 	}
724 	table_print(&t);
725 	return 0;
726 }
727