PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/sonney2k/linux
C | 725 lines | 382 code | 64 blank | 279 comment | 76 complexity | 5a4f236e621bb367007e5502f4cb16fa 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_struct 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_struct 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 - iterate over a list
  343. * @pos: the &struct list_head to use as a loop cursor.
  344. * @head: the head for your list.
  345. *
  346. * This variant differs from list_for_each() in that it's the
  347. * simplest possible list iteration code, no prefetching is done.
  348. * Use this for code that knows the list to be very short (empty
  349. * or 1 entry) most of the time.
  350. */
  351. #define __list_for_each(pos, head) \
  352. for (pos = (head)->next; pos != (head); pos = pos->next)
  353. /**
  354. * list_for_each_prev - iterate over a list backwards
  355. * @pos: the &struct list_head to use as a loop cursor.
  356. * @head: the head for your list.
  357. */
  358. #define list_for_each_prev(pos, head) \
  359. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  360. pos = pos->prev)
  361. /**
  362. * list_for_each_safe - iterate over a list safe against removal of list entry
  363. * @pos: the &struct list_head to use as a loop cursor.
  364. * @n: another &struct list_head to use as temporary storage
  365. * @head: the head for your list.
  366. */
  367. #define list_for_each_safe(pos, n, head) \
  368. for (pos = (head)->next, n = pos->next; pos != (head); \
  369. pos = n, n = pos->next)
  370. /**
  371. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  372. * @pos: the &struct list_head to use as a loop cursor.
  373. * @n: another &struct list_head to use as temporary storage
  374. * @head: the head for your list.
  375. */
  376. #define list_for_each_prev_safe(pos, n, head) \
  377. for (pos = (head)->prev, n = pos->prev; \
  378. prefetch(pos->prev), pos != (head); \
  379. pos = n, n = pos->prev)
  380. /**
  381. * list_for_each_entry - iterate over list of given type
  382. * @pos: the type * to use as a loop cursor.
  383. * @head: the head for your list.
  384. * @member: the name of the list_struct within the struct.
  385. */
  386. #define list_for_each_entry(pos, head, member) \
  387. for (pos = list_entry((head)->next, typeof(*pos), member); \
  388. &pos->member != (head); \
  389. pos = list_entry(pos->member.next, typeof(*pos), member))
  390. /**
  391. * list_for_each_entry_reverse - iterate backwards over list of given type.
  392. * @pos: the type * to use as a loop cursor.
  393. * @head: the head for your list.
  394. * @member: the name of the list_struct within the struct.
  395. */
  396. #define list_for_each_entry_reverse(pos, head, member) \
  397. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  398. prefetch(pos->member.prev), &pos->member != (head); \
  399. pos = list_entry(pos->member.prev, typeof(*pos), member))
  400. /**
  401. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  402. * @pos: the type * to use as a start point
  403. * @head: the head of the list
  404. * @member: the name of the list_struct within the struct.
  405. *
  406. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  407. */
  408. #define list_prepare_entry(pos, head, member) \
  409. ((pos) ? : list_entry(head, typeof(*pos), member))
  410. /**
  411. * list_for_each_entry_continue - continue iteration over list of given type
  412. * @pos: the type * to use as a loop cursor.
  413. * @head: the head for your list.
  414. * @member: the name of the list_struct within the struct.
  415. *
  416. * Continue to iterate over list of given type, continuing after
  417. * the current position.
  418. */
  419. #define list_for_each_entry_continue(pos, head, member) \
  420. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  421. prefetch(pos->member.next), &pos->member != (head); \
  422. pos = list_entry(pos->member.next, typeof(*pos), member))
  423. /**
  424. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  425. * @pos: the type * to use as a loop cursor.
  426. * @head: the head for your list.
  427. * @member: the name of the list_struct within the struct.
  428. *
  429. * Start to iterate over list of given type backwards, continuing after
  430. * the current position.
  431. */
  432. #define list_for_each_entry_continue_reverse(pos, head, member) \
  433. for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
  434. prefetch(pos->member.prev), &pos->member != (head); \
  435. pos = list_entry(pos->member.prev, typeof(*pos), member))
  436. /**
  437. * list_for_each_entry_from - iterate over list of given type from the current point
  438. * @pos: the type * to use as a loop cursor.
  439. * @head: the head for your list.
  440. * @member: the name of the list_struct within the struct.
  441. *
  442. * Iterate over list of given type, continuing from current position.
  443. */
  444. #define list_for_each_entry_from(pos, head, member) \
  445. for (; prefetch(pos->member.next), &pos->member != (head); \
  446. pos = list_entry(pos->member.next, typeof(*pos), member))
  447. /**
  448. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  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_struct within the struct.
  453. */
  454. #define list_for_each_entry_safe(pos, n, head, member) \
  455. for (pos = list_entry((head)->next, typeof(*pos), member), \
  456. n = list_entry(pos->member.next, typeof(*pos), member); \
  457. &pos->member != (head); \
  458. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  459. /**
  460. * list_for_each_entry_safe_continue
  461. * @pos: the type * to use as a loop cursor.
  462. * @n: another type * to use as temporary storage
  463. * @head: the head for your list.
  464. * @member: the name of the list_struct within the struct.
  465. *
  466. * Iterate over list of given type, continuing after current point,
  467. * safe against removal of list entry.
  468. */
  469. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  470. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  471. n = list_entry(pos->member.next, typeof(*pos), member); \
  472. &pos->member != (head); \
  473. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  474. /**
  475. * list_for_each_entry_safe_from
  476. * @pos: the type * to use as a loop cursor.
  477. * @n: another type * to use as temporary storage
  478. * @head: the head for your list.
  479. * @member: the name of the list_struct within the struct.
  480. *
  481. * Iterate over list of given type from current point, safe against
  482. * removal of list entry.
  483. */
  484. #define list_for_each_entry_safe_from(pos, n, head, member) \
  485. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  486. &pos->member != (head); \
  487. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  488. /**
  489. * list_for_each_entry_safe_reverse
  490. * @pos: the type * to use as a loop cursor.
  491. * @n: another type * to use as temporary storage
  492. * @head: the head for your list.
  493. * @member: the name of the list_struct within the struct.
  494. *
  495. * Iterate backwards over list of given type, safe against removal
  496. * of list entry.
  497. */
  498. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  499. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  500. n = list_entry(pos->member.prev, typeof(*pos), member); \
  501. &pos->member != (head); \
  502. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  503. struct offset {
  504. struct list_head list;
  505. unsigned offset;
  506. };
  507. struct table {
  508. struct list_head offsets;
  509. unsigned offset_max;
  510. unsigned nentry;
  511. unsigned *table;
  512. char *gpu_prefix;
  513. };
  514. static struct offset *offset_new(unsigned o)
  515. {
  516. struct offset *offset;
  517. offset = (struct offset *)malloc(sizeof(struct offset));
  518. if (offset) {
  519. INIT_LIST_HEAD(&offset->list);
  520. offset->offset = o;
  521. }
  522. return offset;
  523. }
  524. static void table_offset_add(struct table *t, struct offset *offset)
  525. {
  526. list_add_tail(&offset->list, &t->offsets);
  527. }
  528. static void table_init(struct table *t)
  529. {
  530. INIT_LIST_HEAD(&t->offsets);
  531. t->offset_max = 0;
  532. t->nentry = 0;
  533. t->table = NULL;
  534. }
  535. static void table_print(struct table *t)
  536. {
  537. unsigned nlloop, i, j, n, c, id;
  538. nlloop = (t->nentry + 3) / 4;
  539. c = t->nentry;
  540. printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
  541. t->nentry);
  542. for (i = 0, id = 0; i < nlloop; i++) {
  543. n = 4;
  544. if (n > c)
  545. n = c;
  546. c -= n;
  547. for (j = 0; j < n; j++) {
  548. if (j == 0)
  549. printf("\t");
  550. else
  551. printf(" ");
  552. printf("0x%08X,", t->table[id++]);
  553. }
  554. printf("\n");
  555. }
  556. printf("};\n");
  557. }
  558. static int table_build(struct table *t)
  559. {
  560. struct offset *offset;
  561. unsigned i, m;
  562. t->nentry = ((t->offset_max >> 2) + 31) / 32;
  563. t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
  564. if (t->table == NULL)
  565. return -1;
  566. memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
  567. list_for_each_entry(offset, &t->offsets, list) {
  568. i = (offset->offset >> 2) / 32;
  569. m = (offset->offset >> 2) & 31;
  570. m = 1 << m;
  571. t->table[i] ^= m;
  572. }
  573. return 0;
  574. }
  575. static char gpu_name[10];
  576. static int parser_auth(struct table *t, const char *filename)
  577. {
  578. FILE *file;
  579. regex_t mask_rex;
  580. regmatch_t match[4];
  581. char buf[1024];
  582. size_t end;
  583. int len;
  584. int done = 0;
  585. int r;
  586. unsigned o;
  587. struct offset *offset;
  588. char last_reg_s[10];
  589. int last_reg;
  590. if (regcomp
  591. (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
  592. fprintf(stderr, "Failed to compile regular expression\n");
  593. return -1;
  594. }
  595. file = fopen(filename, "r");
  596. if (file == NULL) {
  597. fprintf(stderr, "Failed to open: %s\n", filename);
  598. return -1;
  599. }
  600. fseek(file, 0, SEEK_END);
  601. end = ftell(file);
  602. fseek(file, 0, SEEK_SET);
  603. /* get header */
  604. if (fgets(buf, 1024, file) == NULL) {
  605. fclose(file);
  606. return -1;
  607. }
  608. /* first line will contain the last register
  609. * and gpu name */
  610. sscanf(buf, "%s %s", gpu_name, last_reg_s);
  611. t->gpu_prefix = gpu_name;
  612. last_reg = strtol(last_reg_s, NULL, 16);
  613. do {
  614. if (fgets(buf, 1024, file) == NULL) {
  615. fclose(file);
  616. return -1;
  617. }
  618. len = strlen(buf);
  619. if (ftell(file) == end)
  620. done = 1;
  621. if (len) {
  622. r = regexec(&mask_rex, buf, 4, match, 0);
  623. if (r == REG_NOMATCH) {
  624. } else if (r) {
  625. fprintf(stderr,
  626. "Error matching regular expression %d in %s\n",
  627. r, filename);
  628. fclose(file);
  629. return -1;
  630. } else {
  631. buf[match[0].rm_eo] = 0;
  632. buf[match[1].rm_eo] = 0;
  633. buf[match[2].rm_eo] = 0;
  634. o = strtol(&buf[match[1].rm_so], NULL, 16);
  635. offset = offset_new(o);
  636. table_offset_add(t, offset);
  637. if (o > t->offset_max)
  638. t->offset_max = o;
  639. }
  640. }
  641. } while (!done);
  642. fclose(file);
  643. if (t->offset_max < last_reg)
  644. t->offset_max = last_reg;
  645. return table_build(t);
  646. }
  647. int main(int argc, char *argv[])
  648. {
  649. struct table t;
  650. if (argc != 2) {
  651. fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
  652. exit(1);
  653. }
  654. table_init(&t);
  655. if (parser_auth(&t, argv[1])) {
  656. fprintf(stderr, "Failed to parse file %s\n", argv[1]);
  657. return -1;
  658. }
  659. table_print(&t);
  660. return 0;
  661. }