1*46924030SJonathan Gray // SPDX-License-Identifier: MIT
250f15303SDave Airlie /* utility to create the register check tables
350f15303SDave Airlie * this includes inlined list.h safe for userspace.
450f15303SDave Airlie *
550f15303SDave Airlie * Copyright 2009 Jerome Glisse
650f15303SDave Airlie * Copyright 2009 Red Hat Inc.
750f15303SDave Airlie *
850f15303SDave Airlie * Authors:
950f15303SDave Airlie * Jerome Glisse
1050f15303SDave Airlie * Dave Airlie
1150f15303SDave Airlie */
1250f15303SDave Airlie
1350f15303SDave Airlie #include <sys/types.h>
1450f15303SDave Airlie #include <stdlib.h>
1550f15303SDave Airlie #include <string.h>
1650f15303SDave Airlie #include <stdio.h>
1750f15303SDave Airlie #include <regex.h>
1850f15303SDave Airlie #include <libgen.h>
1950f15303SDave Airlie
2050f15303SDave Airlie #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
2150f15303SDave Airlie /**
2250f15303SDave Airlie * container_of - cast a member of a structure out to the containing structure
2350f15303SDave Airlie * @ptr: the pointer to the member.
2450f15303SDave Airlie * @type: the type of the container struct this is embedded in.
2550f15303SDave Airlie * @member: the name of the member within the struct.
2650f15303SDave Airlie *
2750f15303SDave Airlie */
2850f15303SDave Airlie #define container_of(ptr, type, member) ({ \
2950f15303SDave Airlie const typeof(((type *)0)->member)*__mptr = (ptr); \
3050f15303SDave Airlie (type *)((char *)__mptr - offsetof(type, member)); })
3150f15303SDave Airlie
3250f15303SDave Airlie /*
3350f15303SDave Airlie * Simple doubly linked list implementation.
3450f15303SDave Airlie *
3550f15303SDave Airlie * Some of the internal functions ("__xxx") are useful when
3650f15303SDave Airlie * manipulating whole lists rather than single entries, as
3750f15303SDave Airlie * sometimes we already know the next/prev entries and we can
3850f15303SDave Airlie * generate better code by using them directly rather than
3950f15303SDave Airlie * using the generic single-entry routines.
4050f15303SDave Airlie */
4150f15303SDave Airlie
4250f15303SDave Airlie struct list_head {
4350f15303SDave Airlie struct list_head *next, *prev;
4450f15303SDave Airlie };
4550f15303SDave Airlie
4650f15303SDave Airlie
INIT_LIST_HEAD(struct list_head * list)4750f15303SDave Airlie static inline void INIT_LIST_HEAD(struct list_head *list)
4850f15303SDave Airlie {
4950f15303SDave Airlie list->next = list;
5050f15303SDave Airlie list->prev = list;
5150f15303SDave Airlie }
5250f15303SDave Airlie
5350f15303SDave Airlie /*
5450f15303SDave Airlie * Insert a new entry between two known consecutive entries.
5550f15303SDave Airlie *
5650f15303SDave Airlie * This is only for internal list manipulation where we know
5750f15303SDave Airlie * the prev/next entries already!
5850f15303SDave Airlie */
5950f15303SDave Airlie #ifndef CONFIG_DEBUG_LIST
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)6050f15303SDave Airlie static inline void __list_add(struct list_head *new,
61689d7c2aSDave Airlie struct list_head *prev, struct list_head *next)
6250f15303SDave Airlie {
6350f15303SDave Airlie next->prev = new;
6450f15303SDave Airlie new->next = next;
6550f15303SDave Airlie new->prev = prev;
6650f15303SDave Airlie prev->next = new;
6750f15303SDave Airlie }
6850f15303SDave Airlie #else
6950f15303SDave Airlie extern void __list_add(struct list_head *new,
70689d7c2aSDave Airlie struct list_head *prev, struct list_head *next);
7150f15303SDave Airlie #endif
7250f15303SDave Airlie
7350f15303SDave Airlie /**
7450f15303SDave Airlie * list_add_tail - add a new entry
7550f15303SDave Airlie * @new: new entry to be added
7650f15303SDave Airlie * @head: list head to add it before
7750f15303SDave Airlie *
7850f15303SDave Airlie * Insert a new entry before the specified head.
7950f15303SDave Airlie * This is useful for implementing queues.
8050f15303SDave Airlie */
list_add_tail(struct list_head * new,struct list_head * head)8150f15303SDave Airlie static inline void list_add_tail(struct list_head *new, struct list_head *head)
8250f15303SDave Airlie {
8350f15303SDave Airlie __list_add(new, head->prev, head);
8450f15303SDave Airlie }
8550f15303SDave Airlie
8650f15303SDave Airlie /**
8750f15303SDave Airlie * list_entry - get the struct for this entry
8850f15303SDave Airlie * @ptr: the &struct list_head pointer.
8950f15303SDave Airlie * @type: the type of the struct this is embedded in.
903943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
9150f15303SDave Airlie */
9250f15303SDave Airlie #define list_entry(ptr, type, member) \
9350f15303SDave Airlie container_of(ptr, type, member)
9450f15303SDave Airlie
9550f15303SDave Airlie /**
9650f15303SDave Airlie * list_for_each_entry - iterate over list of given type
9750f15303SDave Airlie * @pos: the type * to use as a loop cursor.
9850f15303SDave Airlie * @head: the head for your list.
993943f42cSAndrey Utkin * @member: the name of the list_head within the struct.
10050f15303SDave Airlie */
10150f15303SDave Airlie #define list_for_each_entry(pos, head, member) \
10250f15303SDave Airlie for (pos = list_entry((head)->next, typeof(*pos), member); \
10350f15303SDave Airlie &pos->member != (head); \
10450f15303SDave Airlie pos = list_entry(pos->member.next, typeof(*pos), member))
10550f15303SDave Airlie
10650f15303SDave Airlie struct offset {
10750f15303SDave Airlie struct list_head list;
10850f15303SDave Airlie unsigned offset;
10950f15303SDave Airlie };
11050f15303SDave Airlie
11150f15303SDave Airlie struct table {
11250f15303SDave Airlie struct list_head offsets;
11350f15303SDave Airlie unsigned offset_max;
11450f15303SDave Airlie unsigned nentry;
11550f15303SDave Airlie unsigned *table;
11650f15303SDave Airlie char *gpu_prefix;
11750f15303SDave Airlie };
11850f15303SDave Airlie
offset_new(unsigned o)1190592e4c4SJosh Triplett static struct offset *offset_new(unsigned o)
12050f15303SDave Airlie {
12150f15303SDave Airlie struct offset *offset;
12250f15303SDave Airlie
12350f15303SDave Airlie offset = (struct offset *)malloc(sizeof(struct offset));
12450f15303SDave Airlie if (offset) {
12550f15303SDave Airlie INIT_LIST_HEAD(&offset->list);
12650f15303SDave Airlie offset->offset = o;
12750f15303SDave Airlie }
12850f15303SDave Airlie return offset;
12950f15303SDave Airlie }
13050f15303SDave Airlie
table_offset_add(struct table * t,struct offset * offset)1310592e4c4SJosh Triplett static void table_offset_add(struct table *t, struct offset *offset)
13250f15303SDave Airlie {
13350f15303SDave Airlie list_add_tail(&offset->list, &t->offsets);
13450f15303SDave Airlie }
13550f15303SDave Airlie
table_init(struct table * t)1360592e4c4SJosh Triplett static void table_init(struct table *t)
13750f15303SDave Airlie {
13850f15303SDave Airlie INIT_LIST_HEAD(&t->offsets);
13950f15303SDave Airlie t->offset_max = 0;
14050f15303SDave Airlie t->nentry = 0;
14150f15303SDave Airlie t->table = NULL;
14250f15303SDave Airlie }
14350f15303SDave Airlie
table_print(struct table * t)1440592e4c4SJosh Triplett static void table_print(struct table *t)
14550f15303SDave Airlie {
14650f15303SDave Airlie unsigned nlloop, i, j, n, c, id;
14750f15303SDave Airlie
14850f15303SDave Airlie nlloop = (t->nentry + 3) / 4;
14950f15303SDave Airlie c = t->nentry;
150689d7c2aSDave Airlie printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
151689d7c2aSDave Airlie t->nentry);
15250f15303SDave Airlie for (i = 0, id = 0; i < nlloop; i++) {
15350f15303SDave Airlie n = 4;
154689d7c2aSDave Airlie if (n > c)
15550f15303SDave Airlie n = c;
15650f15303SDave Airlie c -= n;
15750f15303SDave Airlie for (j = 0; j < n; j++) {
158689d7c2aSDave Airlie if (j == 0)
159689d7c2aSDave Airlie printf("\t");
160689d7c2aSDave Airlie else
161689d7c2aSDave Airlie printf(" ");
16250f15303SDave Airlie printf("0x%08X,", t->table[id++]);
16350f15303SDave Airlie }
16450f15303SDave Airlie printf("\n");
16550f15303SDave Airlie }
16650f15303SDave Airlie printf("};\n");
16750f15303SDave Airlie }
16850f15303SDave Airlie
table_build(struct table * t)1690592e4c4SJosh Triplett static int table_build(struct table *t)
17050f15303SDave Airlie {
17150f15303SDave Airlie struct offset *offset;
17250f15303SDave Airlie unsigned i, m;
17350f15303SDave Airlie
17450f15303SDave Airlie t->nentry = ((t->offset_max >> 2) + 31) / 32;
17550f15303SDave Airlie t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
176689d7c2aSDave Airlie if (t->table == NULL)
17750f15303SDave Airlie return -1;
17850f15303SDave Airlie memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
17950f15303SDave Airlie list_for_each_entry(offset, &t->offsets, list) {
18050f15303SDave Airlie i = (offset->offset >> 2) / 32;
18150f15303SDave Airlie m = (offset->offset >> 2) & 31;
18250f15303SDave Airlie m = 1 << m;
18350f15303SDave Airlie t->table[i] ^= m;
18450f15303SDave Airlie }
18550f15303SDave Airlie return 0;
18650f15303SDave Airlie }
18750f15303SDave Airlie
18850f15303SDave Airlie static char gpu_name[10];
parser_auth(struct table * t,const char * filename)1890592e4c4SJosh Triplett static int parser_auth(struct table *t, const char *filename)
19050f15303SDave Airlie {
19150f15303SDave Airlie FILE *file;
19250f15303SDave Airlie regex_t mask_rex;
19350f15303SDave Airlie regmatch_t match[4];
19450f15303SDave Airlie char buf[1024];
19550f15303SDave Airlie size_t end;
19650f15303SDave Airlie int len;
19750f15303SDave Airlie int done = 0;
19850f15303SDave Airlie int r;
19950f15303SDave Airlie unsigned o;
20050f15303SDave Airlie struct offset *offset;
20150f15303SDave Airlie char last_reg_s[10];
20250f15303SDave Airlie int last_reg;
20350f15303SDave Airlie
204689d7c2aSDave Airlie if (regcomp
205689d7c2aSDave Airlie (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
20650f15303SDave Airlie fprintf(stderr, "Failed to compile regular expression\n");
20750f15303SDave Airlie return -1;
20850f15303SDave Airlie }
20950f15303SDave Airlie file = fopen(filename, "r");
21050f15303SDave Airlie if (file == NULL) {
21150f15303SDave Airlie fprintf(stderr, "Failed to open: %s\n", filename);
21250f15303SDave Airlie return -1;
21350f15303SDave Airlie }
21450f15303SDave Airlie fseek(file, 0, SEEK_END);
21550f15303SDave Airlie end = ftell(file);
21650f15303SDave Airlie fseek(file, 0, SEEK_SET);
21750f15303SDave Airlie
21850f15303SDave Airlie /* get header */
219059d233fSAlexander Beregalov if (fgets(buf, 1024, file) == NULL) {
220059d233fSAlexander Beregalov fclose(file);
22150f15303SDave Airlie return -1;
222059d233fSAlexander Beregalov }
22350f15303SDave Airlie
22450f15303SDave Airlie /* first line will contain the last register
22550f15303SDave Airlie * and gpu name */
2266b641900SAlan sscanf(buf, "%9s %9s", gpu_name, last_reg_s);
22750f15303SDave Airlie t->gpu_prefix = gpu_name;
22850f15303SDave Airlie last_reg = strtol(last_reg_s, NULL, 16);
22950f15303SDave Airlie
23050f15303SDave Airlie do {
231e917fd39SJesper Juhl if (fgets(buf, 1024, file) == NULL) {
232e917fd39SJesper Juhl fclose(file);
23350f15303SDave Airlie return -1;
234e917fd39SJesper Juhl }
23550f15303SDave Airlie len = strlen(buf);
236689d7c2aSDave Airlie if (ftell(file) == end)
23750f15303SDave Airlie done = 1;
23850f15303SDave Airlie if (len) {
23950f15303SDave Airlie r = regexec(&mask_rex, buf, 4, match, 0);
24050f15303SDave Airlie if (r == REG_NOMATCH) {
24150f15303SDave Airlie } else if (r) {
242689d7c2aSDave Airlie fprintf(stderr,
243689d7c2aSDave Airlie "Error matching regular expression %d in %s\n",
24450f15303SDave Airlie r, filename);
245e917fd39SJesper Juhl fclose(file);
24650f15303SDave Airlie return -1;
24750f15303SDave Airlie } else {
24850f15303SDave Airlie buf[match[0].rm_eo] = 0;
24950f15303SDave Airlie buf[match[1].rm_eo] = 0;
25050f15303SDave Airlie buf[match[2].rm_eo] = 0;
25150f15303SDave Airlie o = strtol(&buf[match[1].rm_so], NULL, 16);
25250f15303SDave Airlie offset = offset_new(o);
25350f15303SDave Airlie table_offset_add(t, offset);
254689d7c2aSDave Airlie if (o > t->offset_max)
25550f15303SDave Airlie t->offset_max = o;
25650f15303SDave Airlie }
25750f15303SDave Airlie }
25850f15303SDave Airlie } while (!done);
25950f15303SDave Airlie fclose(file);
26050f15303SDave Airlie if (t->offset_max < last_reg)
26150f15303SDave Airlie t->offset_max = last_reg;
26250f15303SDave Airlie return table_build(t);
26350f15303SDave Airlie }
26450f15303SDave Airlie
main(int argc,char * argv[])26550f15303SDave Airlie int main(int argc, char *argv[])
26650f15303SDave Airlie {
26750f15303SDave Airlie struct table t;
26850f15303SDave Airlie
26950f15303SDave Airlie if (argc != 2) {
270689d7c2aSDave Airlie fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
27150f15303SDave Airlie exit(1);
27250f15303SDave Airlie }
27350f15303SDave Airlie table_init(&t);
27450f15303SDave Airlie if (parser_auth(&t, argv[1])) {
27550f15303SDave Airlie fprintf(stderr, "Failed to parse file %s\n", argv[1]);
27650f15303SDave Airlie return -1;
27750f15303SDave Airlie }
27850f15303SDave Airlie table_print(&t);
27950f15303SDave Airlie return 0;
28050f15303SDave Airlie }
281