1 /* utility to create the register check tables
2 * this includes inlined list.h safe for userspace.
4 * Copyright 2009 Jerome Glisse
5 * Copyright 2009 Red Hat Inc.
12 #include <sys/types.h>
19 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
21 * container_of - cast a member of a structure out to the containing structure
22 * @ptr: the pointer to the member.
23 * @type: the type of the container struct this is embedded in.
24 * @member: the name of the member within the struct.
27 #define container_of(ptr, type, member) ({ \
28 const typeof(((type *)0)->member)*__mptr = (ptr); \
29 (type *)((char *)__mptr - offsetof(type, member)); })
32 * Simple doubly linked list implementation.
34 * Some of the internal functions ("__xxx") are useful when
35 * manipulating whole lists rather than single entries, as
36 * sometimes we already know the next/prev entries and we can
37 * generate better code by using them directly rather than
38 * using the generic single-entry routines.
42 struct list_head
*next
, *prev
;
45 #define LIST_HEAD_INIT(name) { &(name), &(name) }
47 #define LIST_HEAD(name) \
48 struct list_head name = LIST_HEAD_INIT(name)
50 static inline void INIT_LIST_HEAD(struct list_head
*list
)
57 * Insert a new entry between two known consecutive entries.
59 * This is only for internal list manipulation where we know
60 * the prev/next entries already!
62 #ifndef CONFIG_DEBUG_LIST
63 static inline void __list_add(struct list_head
*new,
64 struct list_head
*prev
, struct list_head
*next
)
72 extern void __list_add(struct list_head
*new,
73 struct list_head
*prev
, struct list_head
*next
);
77 * list_add - add a new entry
78 * @new: new entry to be added
79 * @head: list head to add it after
81 * Insert a new entry after the specified head.
82 * This is good for implementing stacks.
84 static inline void list_add(struct list_head
*new, struct list_head
*head
)
86 __list_add(new, head
, head
->next
);
90 * list_add_tail - add a new entry
91 * @new: new entry to be added
92 * @head: list head to add it before
94 * Insert a new entry before the specified head.
95 * This is useful for implementing queues.
97 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
99 __list_add(new, head
->prev
, head
);
103 * Delete a list entry by making the prev/next entries
104 * point to each other.
106 * This is only for internal list manipulation where we know
107 * the prev/next entries already!
109 static inline void __list_del(struct list_head
*prev
, struct list_head
*next
)
116 * list_del - deletes entry from list.
117 * @entry: the element to delete from the list.
118 * Note: list_empty() on entry does not return true after this, the entry is
119 * in an undefined state.
121 #ifndef CONFIG_DEBUG_LIST
122 static inline void list_del(struct list_head
*entry
)
124 __list_del(entry
->prev
, entry
->next
);
125 entry
->next
= (void *)0xDEADBEEF;
126 entry
->prev
= (void *)0xBEEFDEAD;
129 extern void list_del(struct list_head
*entry
);
133 * list_replace - replace old entry by new one
134 * @old : the element to be replaced
135 * @new : the new element to insert
137 * If @old was empty, it will be overwritten.
139 static inline void list_replace(struct list_head
*old
, struct list_head
*new)
141 new->next
= old
->next
;
142 new->next
->prev
= new;
143 new->prev
= old
->prev
;
144 new->prev
->next
= new;
147 static inline void list_replace_init(struct list_head
*old
,
148 struct list_head
*new)
150 list_replace(old
, new);
155 * list_del_init - deletes entry from list and reinitialize it.
156 * @entry: the element to delete from the list.
158 static inline void list_del_init(struct list_head
*entry
)
160 __list_del(entry
->prev
, entry
->next
);
161 INIT_LIST_HEAD(entry
);
165 * list_move - delete from one list and add as another's head
166 * @list: the entry to move
167 * @head: the head that will precede our entry
169 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
171 __list_del(list
->prev
, list
->next
);
172 list_add(list
, head
);
176 * list_move_tail - delete from one list and add as another's tail
177 * @list: the entry to move
178 * @head: the head that will follow our entry
180 static inline void list_move_tail(struct list_head
*list
,
181 struct list_head
*head
)
183 __list_del(list
->prev
, list
->next
);
184 list_add_tail(list
, head
);
188 * list_is_last - tests whether @list is the last entry in list @head
189 * @list: the entry to test
190 * @head: the head of the list
192 static inline int list_is_last(const struct list_head
*list
,
193 const struct list_head
*head
)
195 return list
->next
== head
;
199 * list_empty - tests whether a list is empty
200 * @head: the list to test.
202 static inline int list_empty(const struct list_head
*head
)
204 return head
->next
== head
;
208 * list_empty_careful - tests whether a list is empty and not being modified
209 * @head: the list to test
212 * tests whether a list is empty _and_ checks that no other CPU might be
213 * in the process of modifying either member (next or prev)
215 * NOTE: using list_empty_careful() without synchronization
216 * can only be safe if the only activity that can happen
217 * to the list entry is list_del_init(). Eg. it cannot be used
218 * if another CPU could re-list_add() it.
220 static inline int list_empty_careful(const struct list_head
*head
)
222 struct list_head
*next
= head
->next
;
223 return (next
== head
) && (next
== head
->prev
);
227 * list_is_singular - tests whether a list has just one entry.
228 * @head: the list to test.
230 static inline int list_is_singular(const struct list_head
*head
)
232 return !list_empty(head
) && (head
->next
== head
->prev
);
235 static inline void __list_cut_position(struct list_head
*list
,
236 struct list_head
*head
,
237 struct list_head
*entry
)
239 struct list_head
*new_first
= entry
->next
;
240 list
->next
= head
->next
;
241 list
->next
->prev
= list
;
244 head
->next
= new_first
;
245 new_first
->prev
= head
;
249 * list_cut_position - cut a list into two
250 * @list: a new list to add all removed entries
251 * @head: a list with entries
252 * @entry: an entry within head, could be the head itself
253 * and if so we won't cut the list
255 * This helper moves the initial part of @head, up to and
256 * including @entry, from @head to @list. You should
257 * pass on @entry an element you know is on @head. @list
258 * should be an empty list or a list you do not care about
262 static inline void list_cut_position(struct list_head
*list
,
263 struct list_head
*head
,
264 struct list_head
*entry
)
266 if (list_empty(head
))
268 if (list_is_singular(head
) && (head
->next
!= entry
&& head
!= entry
))
271 INIT_LIST_HEAD(list
);
273 __list_cut_position(list
, head
, entry
);
276 static inline void __list_splice(const struct list_head
*list
,
277 struct list_head
*prev
, struct list_head
*next
)
279 struct list_head
*first
= list
->next
;
280 struct list_head
*last
= list
->prev
;
290 * list_splice - join two lists, this is designed for stacks
291 * @list: the new list to add.
292 * @head: the place to add it in the first list.
294 static inline void list_splice(const struct list_head
*list
,
295 struct list_head
*head
)
297 if (!list_empty(list
))
298 __list_splice(list
, head
, head
->next
);
302 * list_splice_tail - join two lists, each list being a queue
303 * @list: the new list to add.
304 * @head: the place to add it in the first list.
306 static inline void list_splice_tail(struct list_head
*list
,
307 struct list_head
*head
)
309 if (!list_empty(list
))
310 __list_splice(list
, head
->prev
, head
);
314 * list_splice_init - join two lists and reinitialise the emptied list.
315 * @list: the new list to add.
316 * @head: the place to add it in the first list.
318 * The list at @list is reinitialised
320 static inline void list_splice_init(struct list_head
*list
,
321 struct list_head
*head
)
323 if (!list_empty(list
)) {
324 __list_splice(list
, head
, head
->next
);
325 INIT_LIST_HEAD(list
);
330 * list_splice_tail_init - join two lists and reinitialise the emptied list
331 * @list: the new list to add.
332 * @head: the place to add it in the first list.
334 * Each of the lists is a queue.
335 * The list at @list is reinitialised
337 static inline void list_splice_tail_init(struct list_head
*list
,
338 struct list_head
*head
)
340 if (!list_empty(list
)) {
341 __list_splice(list
, head
->prev
, head
);
342 INIT_LIST_HEAD(list
);
347 * list_entry - get the struct for this entry
348 * @ptr: the &struct list_head pointer.
349 * @type: the type of the struct this is embedded in.
350 * @member: the name of the list_struct within the struct.
352 #define list_entry(ptr, type, member) \
353 container_of(ptr, type, member)
356 * list_first_entry - get the first element from a list
357 * @ptr: the list head to take the element from.
358 * @type: the type of the struct this is embedded in.
359 * @member: the name of the list_struct within the struct.
361 * Note, that list is expected to be not empty.
363 #define list_first_entry(ptr, type, member) \
364 list_entry((ptr)->next, type, member)
367 * list_for_each - iterate over a list
368 * @pos: the &struct list_head to use as a loop cursor.
369 * @head: the head for your list.
371 #define list_for_each(pos, head) \
372 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
376 * __list_for_each - iterate over a list
377 * @pos: the &struct list_head to use as a loop cursor.
378 * @head: the head for your list.
380 * This variant differs from list_for_each() in that it's the
381 * simplest possible list iteration code, no prefetching is done.
382 * Use this for code that knows the list to be very short (empty
383 * or 1 entry) most of the time.
385 #define __list_for_each(pos, head) \
386 for (pos = (head)->next; pos != (head); pos = pos->next)
389 * list_for_each_prev - iterate over a list backwards
390 * @pos: the &struct list_head to use as a loop cursor.
391 * @head: the head for your list.
393 #define list_for_each_prev(pos, head) \
394 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
398 * list_for_each_safe - iterate over a list safe against removal of list entry
399 * @pos: the &struct list_head to use as a loop cursor.
400 * @n: another &struct list_head to use as temporary storage
401 * @head: the head for your list.
403 #define list_for_each_safe(pos, n, head) \
404 for (pos = (head)->next, n = pos->next; pos != (head); \
405 pos = n, n = pos->next)
408 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
409 * @pos: the &struct list_head to use as a loop cursor.
410 * @n: another &struct list_head to use as temporary storage
411 * @head: the head for your list.
413 #define list_for_each_prev_safe(pos, n, head) \
414 for (pos = (head)->prev, n = pos->prev; \
415 prefetch(pos->prev), pos != (head); \
416 pos = n, n = pos->prev)
419 * list_for_each_entry - iterate over list of given type
420 * @pos: the type * to use as a loop cursor.
421 * @head: the head for your list.
422 * @member: the name of the list_struct within the struct.
424 #define list_for_each_entry(pos, head, member) \
425 for (pos = list_entry((head)->next, typeof(*pos), member); \
426 &pos->member != (head); \
427 pos = list_entry(pos->member.next, typeof(*pos), member))
430 * list_for_each_entry_reverse - iterate backwards over list of given type.
431 * @pos: the type * to use as a loop cursor.
432 * @head: the head for your list.
433 * @member: the name of the list_struct within the struct.
435 #define list_for_each_entry_reverse(pos, head, member) \
436 for (pos = list_entry((head)->prev, typeof(*pos), member); \
437 prefetch(pos->member.prev), &pos->member != (head); \
438 pos = list_entry(pos->member.prev, typeof(*pos), member))
441 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
442 * @pos: the type * to use as a start point
443 * @head: the head of the list
444 * @member: the name of the list_struct within the struct.
446 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
448 #define list_prepare_entry(pos, head, member) \
449 ((pos) ? : list_entry(head, typeof(*pos), member))
452 * list_for_each_entry_continue - continue iteration over list of given type
453 * @pos: the type * to use as a loop cursor.
454 * @head: the head for your list.
455 * @member: the name of the list_struct within the struct.
457 * Continue to iterate over list of given type, continuing after
458 * the current position.
460 #define list_for_each_entry_continue(pos, head, member) \
461 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
462 prefetch(pos->member.next), &pos->member != (head); \
463 pos = list_entry(pos->member.next, typeof(*pos), member))
466 * list_for_each_entry_continue_reverse - iterate backwards from the given point
467 * @pos: the type * to use as a loop cursor.
468 * @head: the head for your list.
469 * @member: the name of the list_struct within the struct.
471 * Start to iterate over list of given type backwards, continuing after
472 * the current position.
474 #define list_for_each_entry_continue_reverse(pos, head, member) \
475 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
476 prefetch(pos->member.prev), &pos->member != (head); \
477 pos = list_entry(pos->member.prev, typeof(*pos), member))
480 * list_for_each_entry_from - iterate over list of given type from the current point
481 * @pos: the type * to use as a loop cursor.
482 * @head: the head for your list.
483 * @member: the name of the list_struct within the struct.
485 * Iterate over list of given type, continuing from current position.
487 #define list_for_each_entry_from(pos, head, member) \
488 for (; prefetch(pos->member.next), &pos->member != (head); \
489 pos = list_entry(pos->member.next, typeof(*pos), member))
492 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
493 * @pos: the type * to use as a loop cursor.
494 * @n: another type * to use as temporary storage
495 * @head: the head for your list.
496 * @member: the name of the list_struct within the struct.
498 #define list_for_each_entry_safe(pos, n, head, member) \
499 for (pos = list_entry((head)->next, typeof(*pos), member), \
500 n = list_entry(pos->member.next, typeof(*pos), member); \
501 &pos->member != (head); \
502 pos = n, n = list_entry(n->member.next, typeof(*n), member))
505 * list_for_each_entry_safe_continue
506 * @pos: the type * to use as a loop cursor.
507 * @n: another type * to use as temporary storage
508 * @head: the head for your list.
509 * @member: the name of the list_struct within the struct.
511 * Iterate over list of given type, continuing after current point,
512 * safe against removal of list entry.
514 #define list_for_each_entry_safe_continue(pos, n, head, member) \
515 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
516 n = list_entry(pos->member.next, typeof(*pos), member); \
517 &pos->member != (head); \
518 pos = n, n = list_entry(n->member.next, typeof(*n), member))
521 * list_for_each_entry_safe_from
522 * @pos: the type * to use as a loop cursor.
523 * @n: another type * to use as temporary storage
524 * @head: the head for your list.
525 * @member: the name of the list_struct within the struct.
527 * Iterate over list of given type from current point, safe against
528 * removal of list entry.
530 #define list_for_each_entry_safe_from(pos, n, head, member) \
531 for (n = list_entry(pos->member.next, typeof(*pos), member); \
532 &pos->member != (head); \
533 pos = n, n = list_entry(n->member.next, typeof(*n), member))
536 * list_for_each_entry_safe_reverse
537 * @pos: the type * to use as a loop cursor.
538 * @n: another type * to use as temporary storage
539 * @head: the head for your list.
540 * @member: the name of the list_struct within the struct.
542 * Iterate backwards over list of given type, safe against removal
545 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
546 for (pos = list_entry((head)->prev, typeof(*pos), member), \
547 n = list_entry(pos->member.prev, typeof(*pos), member); \
548 &pos->member != (head); \
549 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
552 struct list_head list
;
557 struct list_head offsets
;
564 struct offset
*offset_new(unsigned o
)
566 struct offset
*offset
;
568 offset
= (struct offset
*)malloc(sizeof(struct offset
));
570 INIT_LIST_HEAD(&offset
->list
);
576 void table_offset_add(struct table
*t
, struct offset
*offset
)
578 list_add_tail(&offset
->list
, &t
->offsets
);
581 void table_init(struct table
*t
)
583 INIT_LIST_HEAD(&t
->offsets
);
589 void table_print(struct table
*t
)
591 unsigned nlloop
, i
, j
, n
, c
, id
;
593 nlloop
= (t
->nentry
+ 3) / 4;
595 printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t
->gpu_prefix
,
597 for (i
= 0, id
= 0; i
< nlloop
; i
++) {
602 for (j
= 0; j
< n
; j
++) {
607 printf("0x%08X,", t
->table
[id
++]);
614 int table_build(struct table
*t
)
616 struct offset
*offset
;
619 t
->nentry
= ((t
->offset_max
>> 2) + 31) / 32;
620 t
->table
= (unsigned *)malloc(sizeof(unsigned) * t
->nentry
);
621 if (t
->table
== NULL
)
623 memset(t
->table
, 0xff, sizeof(unsigned) * t
->nentry
);
624 list_for_each_entry(offset
, &t
->offsets
, list
) {
625 i
= (offset
->offset
>> 2) / 32;
626 m
= (offset
->offset
>> 2) & 31;
633 static char gpu_name
[10];
634 int parser_auth(struct table
*t
, const char *filename
)
645 struct offset
*offset
;
650 (&mask_rex
, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED
)) {
651 fprintf(stderr
, "Failed to compile regular expression\n");
654 file
= fopen(filename
, "r");
656 fprintf(stderr
, "Failed to open: %s\n", filename
);
659 fseek(file
, 0, SEEK_END
);
661 fseek(file
, 0, SEEK_SET
);
664 if (fgets(buf
, 1024, file
) == NULL
)
667 /* first line will contain the last register
669 sscanf(buf
, "%s %s", gpu_name
, last_reg_s
);
670 t
->gpu_prefix
= gpu_name
;
671 last_reg
= strtol(last_reg_s
, NULL
, 16);
674 if (fgets(buf
, 1024, file
) == NULL
)
677 if (ftell(file
) == end
)
680 r
= regexec(&mask_rex
, buf
, 4, match
, 0);
681 if (r
== REG_NOMATCH
) {
684 "Error matching regular expression %d in %s\n",
688 buf
[match
[0].rm_eo
] = 0;
689 buf
[match
[1].rm_eo
] = 0;
690 buf
[match
[2].rm_eo
] = 0;
691 o
= strtol(&buf
[match
[1].rm_so
], NULL
, 16);
692 offset
= offset_new(o
);
693 table_offset_add(t
, offset
);
694 if (o
> t
->offset_max
)
700 if (t
->offset_max
< last_reg
)
701 t
->offset_max
= last_reg
;
702 return table_build(t
);
705 int main(int argc
, char *argv
[])
710 fprintf(stderr
, "Usage: %s <authfile>\n", argv
[0]);
714 if (parser_auth(&t
, argv
[1])) {
715 fprintf(stderr
, "Failed to parse file %s\n", argv
[1]);