correctly mark code segments as code in SELF
[coreboot.git] / util / getpir / code_gen.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "pirq_routing.h"
4
5 static char *preamble[] = {
6
7         "/*\n",
8         " * This file is part of the coreboot project.\n",
9         " *\n",
10         " * Copyright (C) 200x TODO <TODO@TODO>\n",
11         " *\n",
12         " * This program is free software; you can redistribute it and/or modify\n",
13         " * it under the terms of the GNU General Public License as published by\n",
14         " * the Free Software Foundation; either version 2 of the License, or\n",
15         " * (at your option) any later version.\n",
16         " *\n",
17         " * This program is distributed in the hope that it will be useful,\n",
18         " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n",
19         " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n",
20         " * GNU General Public License for more details.\n",
21         " *\n",
22         " * You should have received a copy of the GNU General Public License\n",
23         " * along with this program; if not, write to the Free Software\n",
24         " * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\n",
25         " */\n\n",
26         "#ifdef GETPIR                  /* TODO: Drop this when copying to coreboot. */\n",
27         "#include \"pirq_routing.h\"    /* TODO: Drop this when copying to coreboot. */\n",
28         "#else                          /* TODO: Drop this when copying to coreboot. */\n"
29         "#include <arch/pirq_routing.h>\n",
30         "#endif                         /* TODO: Drop this when copying to coreboot. */\n\n"
31         "const struct irq_routing_table intel_irq_routing_table = {\n",
32         "\tPIRQ_SIGNATURE,              /* u32 signature */\n",
33         "\tPIRQ_VERSION,                /* u16 version */\n",
34         0
35 };
36
37 void code_gen(char *filename, struct irq_routing_table *rt)
38 {
39         char **code = preamble;
40         struct irq_info *se_arr = (struct irq_info *) ((char *) rt + 32);
41         int i, ts = (rt->size - 32) / 16;
42         FILE *fpir;
43
44         if ((fpir = fopen(filename, "w")) == NULL) {
45                 printf("Failed creating file!\n");
46                 exit(2);
47         }
48
49         while (*code)
50                 fprintf(fpir, "%s", *code++);
51
52         fprintf(fpir, "\t32 + 16 * %d,          /* Max. number of devices on the bus */\n",
53                 ts);
54         fprintf(fpir, "\t0x%02x,                        /* Interrupt router bus */\n",
55                 rt->rtr_bus);
56         fprintf(fpir, "\t(0x%02x << 3) | 0x%01x,        /* Interrupt router dev */\n",
57                 rt->rtr_devfn >> 3, rt->rtr_devfn & 7);
58         fprintf(fpir, "\t%#x,                   /* IRQs devoted exclusively to PCI usage */\n",
59                 rt->exclusive_irqs);
60         fprintf(fpir, "\t%#x,                   /* Vendor */\n", rt->rtr_vendor);
61         fprintf(fpir, "\t%#x,                   /* Device */\n", rt->rtr_device);
62         fprintf(fpir, "\t%#x,                   /* Miniport */\n",
63                 rt->miniport_data);
64         fprintf(fpir, "\t{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* u8 rfu[11] */\n");
65         fprintf(fpir, "\t%#x,                   /* Checksum (has to be set to some value that\n                          * would give 0 after the sum of all bytes\n                             * for this structure (including checksum).\n                                 */\n",
66                 rt->checksum);
67         fprintf(fpir, "\t{\n");
68         fprintf(fpir, "\t\t/* bus,        dev | fn,   {link, bitmap}, {link, bitmap}, {link, bitmap}, {link, bitmap}, slot, rfu */\n");
69         for (i = 0; i < ts; i++) {
70                 fprintf(fpir, "\t\t{0x%02x, (0x%02x << 3) | 0x%01x, {{0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}}, 0x%x, 0x%x},\n",
71                         (se_arr+i)->bus, (se_arr+i)->devfn >> 3,
72                         (se_arr+i)->devfn & 7, (se_arr+i)->irq[0].link,
73                         (se_arr+i)->irq[0].bitmap, (se_arr+i)->irq[1].link,
74                         (se_arr+i)->irq[1].bitmap, (se_arr+i)->irq[2].link,
75                         (se_arr+i)->irq[2].bitmap, (se_arr+i)->irq[3].link,
76                         (se_arr+i)->irq[3].bitmap, (se_arr+i)->slot,
77                         (se_arr+i)->rfu);
78         }
79         fprintf(fpir, "\t}\n");
80         fprintf(fpir, "};\n");
81
82         fprintf(fpir, "\nunsigned long write_pirq_routing_table(unsigned long addr)\n");
83         fprintf(fpir, "{\n");
84         fprintf(fpir, "\treturn copy_pirq_routing_table(addr);\n");
85         fprintf(fpir, "}\n");
86
87         fclose(fpir);
88 }