Path: blob/master/drivers/gpu/drm/radeon/mkregtable.c
15113 views
/* utility to create the register check tables1* this includes inlined list.h safe for userspace.2*3* Copyright 2009 Jerome Glisse4* Copyright 2009 Red Hat Inc.5*6* Authors:7* Jerome Glisse8* Dave Airlie9*/1011#include <sys/types.h>12#include <stdlib.h>13#include <string.h>14#include <stdio.h>15#include <regex.h>16#include <libgen.h>1718#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)19/**20* container_of - cast a member of a structure out to the containing structure21* @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) ({ \27const typeof(((type *)0)->member)*__mptr = (ptr); \28(type *)((char *)__mptr - offsetof(type, member)); })2930/*31* Simple doubly linked list implementation.32*33* Some of the internal functions ("__xxx") are useful when34* manipulating whole lists rather than single entries, as35* sometimes we already know the next/prev entries and we can36* generate better code by using them directly rather than37* using the generic single-entry routines.38*/3940struct list_head {41struct list_head *next, *prev;42};4344#define LIST_HEAD_INIT(name) { &(name), &(name) }4546#define LIST_HEAD(name) \47struct list_head name = LIST_HEAD_INIT(name)4849static inline void INIT_LIST_HEAD(struct list_head *list)50{51list->next = list;52list->prev = list;53}5455/*56* Insert a new entry between two known consecutive entries.57*58* This is only for internal list manipulation where we know59* the prev/next entries already!60*/61#ifndef CONFIG_DEBUG_LIST62static inline void __list_add(struct list_head *new,63struct list_head *prev, struct list_head *next)64{65next->prev = new;66new->next = next;67new->prev = prev;68prev->next = new;69}70#else71extern void __list_add(struct list_head *new,72struct list_head *prev, struct list_head *next);73#endif7475/**76* list_add - add a new entry77* @new: new entry to be added78* @head: list head to add it after79*80* Insert a new entry after the specified head.81* This is good for implementing stacks.82*/83static inline void list_add(struct list_head *new, struct list_head *head)84{85__list_add(new, head, head->next);86}8788/**89* list_add_tail - add a new entry90* @new: new entry to be added91* @head: list head to add it before92*93* Insert a new entry before the specified head.94* This is useful for implementing queues.95*/96static inline void list_add_tail(struct list_head *new, struct list_head *head)97{98__list_add(new, head->prev, head);99}100101/*102* Delete a list entry by making the prev/next entries103* point to each other.104*105* This is only for internal list manipulation where we know106* the prev/next entries already!107*/108static inline void __list_del(struct list_head *prev, struct list_head *next)109{110next->prev = prev;111prev->next = next;112}113114/**115* list_del - deletes entry from list.116* @entry: the element to delete from the list.117* Note: list_empty() on entry does not return true after this, the entry is118* in an undefined state.119*/120#ifndef CONFIG_DEBUG_LIST121static inline void list_del(struct list_head *entry)122{123__list_del(entry->prev, entry->next);124entry->next = (void *)0xDEADBEEF;125entry->prev = (void *)0xBEEFDEAD;126}127#else128extern void list_del(struct list_head *entry);129#endif130131/**132* list_replace - replace old entry by new one133* @old : the element to be replaced134* @new : the new element to insert135*136* If @old was empty, it will be overwritten.137*/138static inline void list_replace(struct list_head *old, struct list_head *new)139{140new->next = old->next;141new->next->prev = new;142new->prev = old->prev;143new->prev->next = new;144}145146static inline void list_replace_init(struct list_head *old,147struct list_head *new)148{149list_replace(old, new);150INIT_LIST_HEAD(old);151}152153/**154* list_del_init - deletes entry from list and reinitialize it.155* @entry: the element to delete from the list.156*/157static inline void list_del_init(struct list_head *entry)158{159__list_del(entry->prev, entry->next);160INIT_LIST_HEAD(entry);161}162163/**164* list_move - delete from one list and add as another's head165* @list: the entry to move166* @head: the head that will precede our entry167*/168static inline void list_move(struct list_head *list, struct list_head *head)169{170__list_del(list->prev, list->next);171list_add(list, head);172}173174/**175* list_move_tail - delete from one list and add as another's tail176* @list: the entry to move177* @head: the head that will follow our entry178*/179static inline void list_move_tail(struct list_head *list,180struct list_head *head)181{182__list_del(list->prev, list->next);183list_add_tail(list, head);184}185186/**187* list_is_last - tests whether @list is the last entry in list @head188* @list: the entry to test189* @head: the head of the list190*/191static inline int list_is_last(const struct list_head *list,192const struct list_head *head)193{194return list->next == head;195}196197/**198* list_empty - tests whether a list is empty199* @head: the list to test.200*/201static inline int list_empty(const struct list_head *head)202{203return head->next == head;204}205206/**207* list_empty_careful - tests whether a list is empty and not being modified208* @head: the list to test209*210* Description:211* tests whether a list is empty _and_ checks that no other CPU might be212* in the process of modifying either member (next or prev)213*214* NOTE: using list_empty_careful() without synchronization215* can only be safe if the only activity that can happen216* to the list entry is list_del_init(). Eg. it cannot be used217* if another CPU could re-list_add() it.218*/219static inline int list_empty_careful(const struct list_head *head)220{221struct list_head *next = head->next;222return (next == head) && (next == head->prev);223}224225/**226* list_is_singular - tests whether a list has just one entry.227* @head: the list to test.228*/229static inline int list_is_singular(const struct list_head *head)230{231return !list_empty(head) && (head->next == head->prev);232}233234static inline void __list_cut_position(struct list_head *list,235struct list_head *head,236struct list_head *entry)237{238struct list_head *new_first = entry->next;239list->next = head->next;240list->next->prev = list;241list->prev = entry;242entry->next = list;243head->next = new_first;244new_first->prev = head;245}246247/**248* list_cut_position - cut a list into two249* @list: a new list to add all removed entries250* @head: a list with entries251* @entry: an entry within head, could be the head itself252* and if so we won't cut the list253*254* This helper moves the initial part of @head, up to and255* including @entry, from @head to @list. You should256* pass on @entry an element you know is on @head. @list257* should be an empty list or a list you do not care about258* losing its data.259*260*/261static inline void list_cut_position(struct list_head *list,262struct list_head *head,263struct list_head *entry)264{265if (list_empty(head))266return;267if (list_is_singular(head) && (head->next != entry && head != entry))268return;269if (entry == head)270INIT_LIST_HEAD(list);271else272__list_cut_position(list, head, entry);273}274275static inline void __list_splice(const struct list_head *list,276struct list_head *prev, struct list_head *next)277{278struct list_head *first = list->next;279struct list_head *last = list->prev;280281first->prev = prev;282prev->next = first;283284last->next = next;285next->prev = last;286}287288/**289* list_splice - join two lists, this is designed for stacks290* @list: the new list to add.291* @head: the place to add it in the first list.292*/293static inline void list_splice(const struct list_head *list,294struct list_head *head)295{296if (!list_empty(list))297__list_splice(list, head, head->next);298}299300/**301* list_splice_tail - join two lists, each list being a queue302* @list: the new list to add.303* @head: the place to add it in the first list.304*/305static inline void list_splice_tail(struct list_head *list,306struct list_head *head)307{308if (!list_empty(list))309__list_splice(list, head->prev, head);310}311312/**313* list_splice_init - join two lists and reinitialise the emptied list.314* @list: the new list to add.315* @head: the place to add it in the first list.316*317* The list at @list is reinitialised318*/319static inline void list_splice_init(struct list_head *list,320struct list_head *head)321{322if (!list_empty(list)) {323__list_splice(list, head, head->next);324INIT_LIST_HEAD(list);325}326}327328/**329* list_splice_tail_init - join two lists and reinitialise the emptied list330* @list: the new list to add.331* @head: the place to add it in the first list.332*333* Each of the lists is a queue.334* The list at @list is reinitialised335*/336static inline void list_splice_tail_init(struct list_head *list,337struct list_head *head)338{339if (!list_empty(list)) {340__list_splice(list, head->prev, head);341INIT_LIST_HEAD(list);342}343}344345/**346* list_entry - get the struct for this entry347* @ptr: the &struct list_head pointer.348* @type: the type of the struct this is embedded in.349* @member: the name of the list_struct within the struct.350*/351#define list_entry(ptr, type, member) \352container_of(ptr, type, member)353354/**355* list_first_entry - get the first element from a list356* @ptr: the list head to take the element from.357* @type: the type of the struct this is embedded in.358* @member: the name of the list_struct within the struct.359*360* Note, that list is expected to be not empty.361*/362#define list_first_entry(ptr, type, member) \363list_entry((ptr)->next, type, member)364365/**366* list_for_each - iterate over a list367* @pos: the &struct list_head to use as a loop cursor.368* @head: the head for your list.369*/370#define list_for_each(pos, head) \371for (pos = (head)->next; prefetch(pos->next), pos != (head); \372pos = pos->next)373374/**375* __list_for_each - iterate over a list376* @pos: the &struct list_head to use as a loop cursor.377* @head: the head for your list.378*379* This variant differs from list_for_each() in that it's the380* simplest possible list iteration code, no prefetching is done.381* Use this for code that knows the list to be very short (empty382* or 1 entry) most of the time.383*/384#define __list_for_each(pos, head) \385for (pos = (head)->next; pos != (head); pos = pos->next)386387/**388* list_for_each_prev - iterate over a list backwards389* @pos: the &struct list_head to use as a loop cursor.390* @head: the head for your list.391*/392#define list_for_each_prev(pos, head) \393for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \394pos = pos->prev)395396/**397* list_for_each_safe - iterate over a list safe against removal of list entry398* @pos: the &struct list_head to use as a loop cursor.399* @n: another &struct list_head to use as temporary storage400* @head: the head for your list.401*/402#define list_for_each_safe(pos, n, head) \403for (pos = (head)->next, n = pos->next; pos != (head); \404pos = n, n = pos->next)405406/**407* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry408* @pos: the &struct list_head to use as a loop cursor.409* @n: another &struct list_head to use as temporary storage410* @head: the head for your list.411*/412#define list_for_each_prev_safe(pos, n, head) \413for (pos = (head)->prev, n = pos->prev; \414prefetch(pos->prev), pos != (head); \415pos = n, n = pos->prev)416417/**418* list_for_each_entry - iterate over list of given type419* @pos: the type * to use as a loop cursor.420* @head: the head for your list.421* @member: the name of the list_struct within the struct.422*/423#define list_for_each_entry(pos, head, member) \424for (pos = list_entry((head)->next, typeof(*pos), member); \425&pos->member != (head); \426pos = list_entry(pos->member.next, typeof(*pos), member))427428/**429* list_for_each_entry_reverse - iterate backwards over list of given type.430* @pos: the type * to use as a loop cursor.431* @head: the head for your list.432* @member: the name of the list_struct within the struct.433*/434#define list_for_each_entry_reverse(pos, head, member) \435for (pos = list_entry((head)->prev, typeof(*pos), member); \436prefetch(pos->member.prev), &pos->member != (head); \437pos = list_entry(pos->member.prev, typeof(*pos), member))438439/**440* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()441* @pos: the type * to use as a start point442* @head: the head of the list443* @member: the name of the list_struct within the struct.444*445* Prepares a pos entry for use as a start point in list_for_each_entry_continue().446*/447#define list_prepare_entry(pos, head, member) \448((pos) ? : list_entry(head, typeof(*pos), member))449450/**451* list_for_each_entry_continue - continue iteration over list of given type452* @pos: the type * to use as a loop cursor.453* @head: the head for your list.454* @member: the name of the list_struct within the struct.455*456* Continue to iterate over list of given type, continuing after457* the current position.458*/459#define list_for_each_entry_continue(pos, head, member) \460for (pos = list_entry(pos->member.next, typeof(*pos), member); \461prefetch(pos->member.next), &pos->member != (head); \462pos = list_entry(pos->member.next, typeof(*pos), member))463464/**465* list_for_each_entry_continue_reverse - iterate backwards from the given point466* @pos: the type * to use as a loop cursor.467* @head: the head for your list.468* @member: the name of the list_struct within the struct.469*470* Start to iterate over list of given type backwards, continuing after471* the current position.472*/473#define list_for_each_entry_continue_reverse(pos, head, member) \474for (pos = list_entry(pos->member.prev, typeof(*pos), member); \475prefetch(pos->member.prev), &pos->member != (head); \476pos = list_entry(pos->member.prev, typeof(*pos), member))477478/**479* list_for_each_entry_from - iterate over list of given type from the current point480* @pos: the type * to use as a loop cursor.481* @head: the head for your list.482* @member: the name of the list_struct within the struct.483*484* Iterate over list of given type, continuing from current position.485*/486#define list_for_each_entry_from(pos, head, member) \487for (; prefetch(pos->member.next), &pos->member != (head); \488pos = list_entry(pos->member.next, typeof(*pos), member))489490/**491* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry492* @pos: the type * to use as a loop cursor.493* @n: another type * to use as temporary storage494* @head: the head for your list.495* @member: the name of the list_struct within the struct.496*/497#define list_for_each_entry_safe(pos, n, head, member) \498for (pos = list_entry((head)->next, typeof(*pos), member), \499n = list_entry(pos->member.next, typeof(*pos), member); \500&pos->member != (head); \501pos = n, n = list_entry(n->member.next, typeof(*n), member))502503/**504* list_for_each_entry_safe_continue505* @pos: the type * to use as a loop cursor.506* @n: another type * to use as temporary storage507* @head: the head for your list.508* @member: the name of the list_struct within the struct.509*510* Iterate over list of given type, continuing after current point,511* safe against removal of list entry.512*/513#define list_for_each_entry_safe_continue(pos, n, head, member) \514for (pos = list_entry(pos->member.next, typeof(*pos), member), \515n = list_entry(pos->member.next, typeof(*pos), member); \516&pos->member != (head); \517pos = n, n = list_entry(n->member.next, typeof(*n), member))518519/**520* list_for_each_entry_safe_from521* @pos: the type * to use as a loop cursor.522* @n: another type * to use as temporary storage523* @head: the head for your list.524* @member: the name of the list_struct within the struct.525*526* Iterate over list of given type from current point, safe against527* removal of list entry.528*/529#define list_for_each_entry_safe_from(pos, n, head, member) \530for (n = list_entry(pos->member.next, typeof(*pos), member); \531&pos->member != (head); \532pos = n, n = list_entry(n->member.next, typeof(*n), member))533534/**535* list_for_each_entry_safe_reverse536* @pos: the type * to use as a loop cursor.537* @n: another type * to use as temporary storage538* @head: the head for your list.539* @member: the name of the list_struct within the struct.540*541* Iterate backwards over list of given type, safe against removal542* of list entry.543*/544#define list_for_each_entry_safe_reverse(pos, n, head, member) \545for (pos = list_entry((head)->prev, typeof(*pos), member), \546n = list_entry(pos->member.prev, typeof(*pos), member); \547&pos->member != (head); \548pos = n, n = list_entry(n->member.prev, typeof(*n), member))549550struct offset {551struct list_head list;552unsigned offset;553};554555struct table {556struct list_head offsets;557unsigned offset_max;558unsigned nentry;559unsigned *table;560char *gpu_prefix;561};562563static struct offset *offset_new(unsigned o)564{565struct offset *offset;566567offset = (struct offset *)malloc(sizeof(struct offset));568if (offset) {569INIT_LIST_HEAD(&offset->list);570offset->offset = o;571}572return offset;573}574575static void table_offset_add(struct table *t, struct offset *offset)576{577list_add_tail(&offset->list, &t->offsets);578}579580static void table_init(struct table *t)581{582INIT_LIST_HEAD(&t->offsets);583t->offset_max = 0;584t->nentry = 0;585t->table = NULL;586}587588static void table_print(struct table *t)589{590unsigned nlloop, i, j, n, c, id;591592nlloop = (t->nentry + 3) / 4;593c = t->nentry;594printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,595t->nentry);596for (i = 0, id = 0; i < nlloop; i++) {597n = 4;598if (n > c)599n = c;600c -= n;601for (j = 0; j < n; j++) {602if (j == 0)603printf("\t");604else605printf(" ");606printf("0x%08X,", t->table[id++]);607}608printf("\n");609}610printf("};\n");611}612613static int table_build(struct table *t)614{615struct offset *offset;616unsigned i, m;617618t->nentry = ((t->offset_max >> 2) + 31) / 32;619t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);620if (t->table == NULL)621return -1;622memset(t->table, 0xff, sizeof(unsigned) * t->nentry);623list_for_each_entry(offset, &t->offsets, list) {624i = (offset->offset >> 2) / 32;625m = (offset->offset >> 2) & 31;626m = 1 << m;627t->table[i] ^= m;628}629return 0;630}631632static char gpu_name[10];633static int parser_auth(struct table *t, const char *filename)634{635FILE *file;636regex_t mask_rex;637regmatch_t match[4];638char buf[1024];639size_t end;640int len;641int done = 0;642int r;643unsigned o;644struct offset *offset;645char last_reg_s[10];646int last_reg;647648if (regcomp649(&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {650fprintf(stderr, "Failed to compile regular expression\n");651return -1;652}653file = fopen(filename, "r");654if (file == NULL) {655fprintf(stderr, "Failed to open: %s\n", filename);656return -1;657}658fseek(file, 0, SEEK_END);659end = ftell(file);660fseek(file, 0, SEEK_SET);661662/* get header */663if (fgets(buf, 1024, file) == NULL) {664fclose(file);665return -1;666}667668/* first line will contain the last register669* and gpu name */670sscanf(buf, "%s %s", gpu_name, last_reg_s);671t->gpu_prefix = gpu_name;672last_reg = strtol(last_reg_s, NULL, 16);673674do {675if (fgets(buf, 1024, file) == NULL) {676fclose(file);677return -1;678}679len = strlen(buf);680if (ftell(file) == end)681done = 1;682if (len) {683r = regexec(&mask_rex, buf, 4, match, 0);684if (r == REG_NOMATCH) {685} else if (r) {686fprintf(stderr,687"Error matching regular expression %d in %s\n",688r, filename);689fclose(file);690return -1;691} else {692buf[match[0].rm_eo] = 0;693buf[match[1].rm_eo] = 0;694buf[match[2].rm_eo] = 0;695o = strtol(&buf[match[1].rm_so], NULL, 16);696offset = offset_new(o);697table_offset_add(t, offset);698if (o > t->offset_max)699t->offset_max = o;700}701}702} while (!done);703fclose(file);704if (t->offset_max < last_reg)705t->offset_max = last_reg;706return table_build(t);707}708709int main(int argc, char *argv[])710{711struct table t;712713if (argc != 2) {714fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);715exit(1);716}717table_init(&t);718if (parser_auth(&t, argv[1])) {719fprintf(stderr, "Failed to parse file %s\n", argv[1]);720return -1;721}722table_print(&t);723return 0;724}725726727