mkregtable.c 6.3 KB

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