Add CONFIG_WARNINGS_ARE_ERRORS and set it for qemu.
[coreboot.git] / src / cpu / emulation / qemu-x86 / northbridge.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <bitops.h>
9 #include "chip.h"
10 #include "northbridge.h"
11 #include <delay.h>
12
13 static void ram_resource(device_t dev, unsigned long index,
14         unsigned long basek, unsigned long sizek)
15 {
16         struct resource *resource;
17
18         if (!sizek) {
19                 return;
20         }
21         resource = new_resource(dev, index);
22         resource->base  = ((resource_t)basek) << 10;
23         resource->size  = ((resource_t)sizek) << 10;
24         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
25                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
26 }
27
28 static void tolm_test(void *gp, struct device *dev, struct resource *new)
29 {
30         struct resource **best_p = gp;
31         struct resource *best;
32         best = *best_p;
33         if (!best || (best->base > new->base)) {
34                 best = new;
35         }
36         *best_p = best;
37 }
38
39 static uint32_t find_pci_tolm(struct bus *bus)
40 {
41         struct resource *min;
42         uint32_t tolm;
43         min = 0;
44         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
45         tolm = 0xffffffffUL;
46         if (min && tolm > min->base) {
47                 tolm = min->base;
48         }
49         return tolm;
50 }
51
52 #if CONFIG_WRITE_HIGH_TABLES==1
53 #define HIGH_TABLES_SIZE 64     // maximum size of high tables in KB
54 extern uint64_t high_tables_base, high_tables_size;
55 #endif
56
57 static void cpu_pci_domain_set_resources(device_t dev)
58 {
59         static const uint8_t ramregs[] = {
60                 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x56, 0x57
61         };
62         device_t mc_dev;
63         uint32_t pci_tolm;
64
65         pci_tolm = find_pci_tolm(&dev->link[0]);
66         mc_dev = dev->link[0].children;
67         if (mc_dev) {
68                 unsigned long tomk, tolmk;
69                 unsigned char rambits;
70                 int i, idx;
71
72                 for(rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) {
73                         unsigned char reg;
74                         reg = pci_read_config8(mc_dev, ramregs[i]);
75                         /* these are ENDING addresses, not sizes.
76                          * if there is memory in this slot, then reg will be > rambits.
77                          * So we just take the max, that gives us total.
78                          * We take the highest one to cover for once and future coreboot
79                          * bugs. We warn about bugs.
80                          */
81                         if (reg > rambits)
82                                 rambits = reg;
83                         if (reg < rambits)
84                                 printk_err("ERROR! register 0x%x is not set!\n",
85                                         ramregs[i]);
86                 }
87                 if (rambits == 0) {
88                         printk_err("RAM size config registers are empty; defaulting to 64 MBytes\n");
89                         rambits = 8;
90                 }
91                 printk_debug("I would set ram size to 0x%x Kbytes\n", (rambits)*8*1024);
92                 tomk = rambits*8*1024;
93                 /* Compute the top of Low memory */
94                 tolmk = pci_tolm >> 10;
95                 if (tolmk >= tomk) {
96                         /* The PCI hole does not overlap the memory. */
97                         tolmk = tomk;
98                 }
99
100                 /* Report the memory regions. */
101                 idx = 10;
102                 ram_resource(dev, idx++, 0, 640);
103                 ram_resource(dev, idx++, 768, tolmk - 768);
104
105 #if CONFIG_WRITE_HIGH_TABLES==1
106                 /* Leave some space for ACPI, PIRQ and MP tables */
107                 high_tables_base = (tomk - HIGH_TABLES_SIZE) * 1024;
108                 high_tables_size = HIGH_TABLES_SIZE * 1024;
109 #endif
110         }
111         assign_resources(&dev->link[0]);
112 }
113
114 static void cpu_pci_domain_read_resources(struct device *dev)
115 {
116         struct resource *res;
117
118         pci_domain_read_resources(dev);
119
120         /* Reserve space for the IOAPIC.  This should be in the Southbridge,
121          * but I couldn't tell which device to put it in. */
122         res = new_resource(dev, 2);
123         res->base = 0xfec00000UL;
124         res->size = 0x100000UL;
125         res->limit = 0xffffffffUL;
126         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
127                      IORESOURCE_ASSIGNED;
128
129         /* Reserve space for the LAPIC.  There's one in every processor, but
130          * the space only needs to be reserved once, so we do it here. */
131         res = new_resource(dev, 3);
132         res->base = 0xfee00000UL;
133         res->size = 0x10000UL;
134         res->limit = 0xffffffffUL;
135         res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
136                      IORESOURCE_ASSIGNED;
137 }
138
139 static struct device_operations pci_domain_ops = {
140         .read_resources         = cpu_pci_domain_read_resources,
141         .set_resources          = cpu_pci_domain_set_resources,
142         .enable_resources       = enable_childrens_resources,
143         .init                   = 0,
144         .scan_bus               = pci_domain_scan_bus,
145 };
146
147 static void enable_dev(struct device *dev)
148 {
149         /* Set the operations if it is a special bus type */
150         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
151                 dev->ops = &pci_domain_ops;
152                 pci_set_method(dev);
153         }
154 }
155
156 struct chip_operations cpu_emulation_qemu_x86_ops = {
157         CHIP_NAME("QEMU Northbridge")
158         .enable_dev = enable_dev,
159 };
160
161 void udelay(unsigned usecs)
162 {
163         unsigned i;
164         for(i = 0; i < usecs; i++)
165                 inb(0x80);
166 }
167
168