PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/radeon/mkregtable.c

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