Use lists instead of arrays for resources in devices to reduce memory usage.
[coreboot.git] / src / devices / oprom / yabel / device.c
1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * Copyright (c) 2008, 2009 Pattrick Hueper <phueper@hueper.net>
4  * All rights reserved.
5  * This program and the accompanying materials
6  * are made available under the terms of the BSD License
7  * which accompanies this distribution, and is available at
8  * http://www.opensource.org/licenses/bsd-license.php
9  *
10  * Contributors:
11  *     IBM Corporation - initial implementation
12  *****************************************************************************/
13
14
15 #include "device.h"
16 #include "compat/rtas.h"
17 #include <string.h>
18 #include "debug.h"
19
20 #include <device/device.h>
21 #include <device/pci.h>
22 #include <device/pci_ops.h>
23 #include <device/resource.h>
24
25 /* the device we are working with... */
26 biosemu_device_t bios_device;
27 //max. 6 BARs and 1 Exp.ROM plus CfgSpace and 3 legacy ranges
28 translate_address_t translate_address_array[11];
29 u8 taa_last_entry;
30
31 typedef struct {
32         u8 info;
33         u8 bus;
34         u8 devfn;
35         u8 cfg_space_offset;
36         u64 address;
37         u64 size;
38 } __attribute__ ((__packed__)) assigned_address_t;
39
40 #ifdef CONFIG_PCI_OPTION_ROM_RUN_YABEL
41 /* coreboot version */
42
43 static void
44 biosemu_dev_get_addr_info(void)
45 {
46         int taa_index = 0;
47         int i = 0;
48         struct resource *r;
49         u8 bus = bios_device.dev->bus->link;
50         u16 devfn = bios_device.dev->path.pci.devfn;
51
52         bios_device.bus =  bus;
53         bios_device.devfn = devfn;
54
55         DEBUG_PRINTF("bus: %x, devfn: %x\n", bus, devfn);
56         for (r = bios_device.dev->resource_list; r; r = r->next) {
57                 translate_address_array[taa_index].info = r->flags;
58                 translate_address_array[taa_index].bus = bus;
59                 translate_address_array[taa_index].devfn = devfn;
60                 translate_address_array[taa_index].cfg_space_offset =
61                     r->index;
62                 translate_address_array[taa_index].address = r->base;
63                 translate_address_array[taa_index].size = r->size;
64                 /* dont translate addresses... all addresses are 1:1 */
65                 translate_address_array[taa_index].address_offset = 0;
66                 taa_index++;
67         }
68         /* Expansion ROM */
69         translate_address_array[taa_index].info = IORESOURCE_MEM | IORESOURCE_READONLY;
70         translate_address_array[taa_index].bus = bus;
71         translate_address_array[taa_index].devfn = devfn;
72         translate_address_array[taa_index].cfg_space_offset = 0x30;
73         translate_address_array[taa_index].address = bios_device.img_addr;
74         translate_address_array[taa_index].size = 0; /* TODO: do we need the size? */
75         /* dont translate addresses... all addresses are 1:1 */
76         translate_address_array[taa_index].address_offset = 0;
77         taa_index++;
78         /* legacy ranges if its a VGA card... */
79         if ((bios_device.dev->class & 0xFF0000) == 0x030000) {
80                 DEBUG_PRINTF("%s: VGA device found, adding legacy resources... \n", __func__);
81                 /* I/O 0x3B0-0x3BB */
82                 translate_address_array[taa_index].info = IORESOURCE_FIXED | IORESOURCE_IO;
83                 translate_address_array[taa_index].bus = bus;
84                 translate_address_array[taa_index].devfn = devfn;
85                 translate_address_array[taa_index].cfg_space_offset = 0;
86                 translate_address_array[taa_index].address = 0x3b0;
87                 translate_address_array[taa_index].size = 0xc;
88                 /* dont translate addresses... all addresses are 1:1 */
89                 translate_address_array[taa_index].address_offset = 0;
90                 taa_index++;
91                 /* I/O 0x3C0-0x3DF */
92                 translate_address_array[taa_index].info = IORESOURCE_FIXED | IORESOURCE_IO;
93                 translate_address_array[taa_index].bus = bus;
94                 translate_address_array[taa_index].devfn = devfn;
95                 translate_address_array[taa_index].cfg_space_offset = 0;
96                 translate_address_array[taa_index].address = 0x3c0;
97                 translate_address_array[taa_index].size = 0x20;
98                 /* dont translate addresses... all addresses are 1:1 */
99                 translate_address_array[taa_index].address_offset = 0;
100                 taa_index++;
101                 /* Mem 0xA0000-0xBFFFF */
102                 translate_address_array[taa_index].info = IORESOURCE_FIXED | IORESOURCE_MEM;
103                 translate_address_array[taa_index].bus = bus;
104                 translate_address_array[taa_index].devfn = devfn;
105                 translate_address_array[taa_index].cfg_space_offset = 0;
106                 translate_address_array[taa_index].address = 0xa0000;
107                 translate_address_array[taa_index].size = 0x20000;
108                 /* dont translate addresses... all addresses are 1:1 */
109                 translate_address_array[taa_index].address_offset = 0;
110                 taa_index++;
111         }
112         // store last entry index of translate_address_array
113         taa_last_entry = taa_index - 1;
114 #if defined(CONFIG_X86EMU_DEBUG) && CONFIG_X86EMU_DEBUG
115         //dump translate_address_array
116         printf("translate_address_array: \n");
117         translate_address_t ta;
118         for (i = 0; i <= taa_last_entry; i++) {
119                 ta = translate_address_array[i];
120                 printf
121                     ("%d: info: %08lx bus: %02x devfn: %02x cfg_space_offset: %02x\n\taddr: %016llx\n\toffs: %016llx\n\tsize: %016llx\n",
122                      i, ta.info, ta.bus, ta.devfn, ta.cfg_space_offset,
123                      ta.address, ta.address_offset, ta.size);
124         }
125 #endif
126 }
127 #else
128 // use translate_address_dev and get_puid from net-snk's net_support.c
129 void translate_address_dev(u64 *, phandle_t);
130 u64 get_puid(phandle_t node);
131
132
133 // scan all adresses assigned to the device ("assigned-addresses" and "reg")
134 // store in translate_address_array for faster translation using dev_translate_address
135 void
136 biosemu_dev_get_addr_info(void)
137 {
138         // get bus/dev/fn from assigned-addresses
139         int32_t len;
140         //max. 6 BARs and 1 Exp.ROM plus CfgSpace and 3 legacy ranges
141         assigned_address_t buf[11];
142         len =
143             of_getprop(bios_device.phandle, "assigned-addresses", buf,
144                        sizeof(buf));
145         bios_device.bus = buf[0].bus;
146         bios_device.devfn = buf[0].devfn;
147         DEBUG_PRINTF("bus: %x, devfn: %x\n", bios_device.bus,
148                      bios_device.devfn);
149         //store address translations for all assigned-addresses and regs in
150         //translate_address_array for faster translation later on...
151         int i = 0;
152         // index to insert data into translate_address_array
153         int taa_index = 0;
154         u64 address_offset;
155         for (i = 0; i < (len / sizeof(assigned_address_t)); i++, taa_index++) {
156                 //copy all info stored in assigned-addresses
157                 translate_address_array[taa_index].info = buf[i].info;
158                 translate_address_array[taa_index].bus = buf[i].bus;
159                 translate_address_array[taa_index].devfn = buf[i].devfn;
160                 translate_address_array[taa_index].cfg_space_offset =
161                     buf[i].cfg_space_offset;
162                 translate_address_array[taa_index].address = buf[i].address;
163                 translate_address_array[taa_index].size = buf[i].size;
164                 // translate first address and store it as address_offset
165                 address_offset = buf[i].address;
166                 translate_address_dev(&address_offset, bios_device.phandle);
167                 translate_address_array[taa_index].address_offset =
168                     address_offset - buf[i].address;
169         }
170         //get "reg" property
171         len = of_getprop(bios_device.phandle, "reg", buf, sizeof(buf));
172         for (i = 0; i < (len / sizeof(assigned_address_t)); i++) {
173                 if ((buf[i].size == 0) || (buf[i].cfg_space_offset != 0)) {
174                         // we dont care for ranges with size 0 and
175                         // BARs and Expansion ROM must be in assigned-addresses... so in reg
176                         // we only look for those without config space offset set...
177                         // i.e. the legacy ranges
178                         continue;
179                 }
180                 //copy all info stored in assigned-addresses
181                 translate_address_array[taa_index].info = buf[i].info;
182                 translate_address_array[taa_index].bus = buf[i].bus;
183                 translate_address_array[taa_index].devfn = buf[i].devfn;
184                 translate_address_array[taa_index].cfg_space_offset =
185                     buf[i].cfg_space_offset;
186                 translate_address_array[taa_index].address = buf[i].address;
187                 translate_address_array[taa_index].size = buf[i].size;
188                 // translate first address and store it as address_offset
189                 address_offset = buf[i].address;
190                 translate_address_dev(&address_offset, bios_device.phandle);
191                 translate_address_array[taa_index].address_offset =
192                     address_offset - buf[i].address;
193                 taa_index++;
194         }
195         // store last entry index of translate_address_array
196         taa_last_entry = taa_index - 1;
197 #if defined(CONFIG_X86EMU_DEBUG) && CONFIG_X86EMU_DEBUG
198         //dump translate_address_array
199         printf("translate_address_array: \n");
200         translate_address_t ta;
201         for (i = 0; i <= taa_last_entry; i++) {
202                 ta = translate_address_array[i];
203                 printf
204                     ("%d: %02x%02x%02x%02x\n\taddr: %016llx\n\toffs: %016llx\n\tsize: %016llx\n",
205                      i, ta.info, ta.bus, ta.devfn, ta.cfg_space_offset,
206                      ta.address, ta.address_offset, ta.size);
207         }
208 #endif
209 }
210 #endif
211
212 #ifndef CONFIG_PCI_OPTION_ROM_RUN_YABEL
213 // to simulate accesses to legacy VGA Memory (0xA0000-0xBFFFF)
214 // we look for the first prefetchable memory BAR, if no prefetchable BAR found,
215 // we use the first memory BAR
216 // dev_translate_addr will translate accesses to the legacy VGA Memory into the found vmem BAR
217 static void
218 biosemu_dev_find_vmem_addr(void)
219 {
220         int i = 0;
221         translate_address_t ta;
222         s8 tai_np = -1, tai_p = -1;     // translate_address_array index for non-prefetchable and prefetchable memory
223         //search backwards to find first entry
224         for (i = taa_last_entry; i >= 0; i--) {
225                 ta = translate_address_array[i];
226                 if ((ta.cfg_space_offset >= 0x10)
227                     && (ta.cfg_space_offset <= 0x24)) {
228                         //only BARs
229                         if ((ta.info & 0x03) >= 0x02) {
230                                 //32/64bit memory
231                                 tai_np = i;
232                                 if ((ta.info & 0x40) != 0) {
233                                         // prefetchable
234                                         tai_p = i;
235                                 }
236                         }
237                 }
238         }
239         if (tai_p != -1) {
240                 ta = translate_address_array[tai_p];
241                 bios_device.vmem_addr = ta.address;
242                 bios_device.vmem_size = ta.size;
243                 DEBUG_PRINTF
244                     ("%s: Found prefetchable Virtual Legacy Memory BAR: %llx, size: %llx\n",
245                      __func__, bios_device.vmem_addr,
246                      bios_device.vmem_size);
247         } else if (tai_np != -1) {
248                 ta = translate_address_array[tai_np];
249                 bios_device.vmem_addr = ta.address;
250                 bios_device.vmem_size = ta.size;
251                 DEBUG_PRINTF
252                     ("%s: Found non-prefetchable Virtual Legacy Memory BAR: %llx, size: %llx",
253                      __func__, bios_device.vmem_addr,
254                      bios_device.vmem_size);
255         }
256         // disable vmem
257         //bios_device.vmem_size = 0;
258 }
259
260 void
261 biosemu_dev_get_puid(void)
262 {
263         // get puid
264         bios_device.puid = get_puid(bios_device.phandle);
265         DEBUG_PRINTF("puid: 0x%llx\n", bios_device.puid);
266 }
267 #endif
268
269 static void
270 biosemu_dev_get_device_vendor_id(void)
271 {
272
273         u32 pci_config_0;
274 #ifdef CONFIG_PCI_OPTION_ROM_RUN_YABEL
275         pci_config_0 = pci_read_config32(bios_device.dev, 0x0);
276 #else
277         pci_config_0 =
278             rtas_pci_config_read(bios_device.puid, 4, bios_device.bus,
279                                  bios_device.devfn, 0x0);
280 #endif
281         bios_device.pci_device_id =
282             (u16) ((pci_config_0 & 0xFFFF0000) >> 16);
283         bios_device.pci_vendor_id = (u16) (pci_config_0 & 0x0000FFFF);
284         DEBUG_PRINTF("PCI Device ID: %04x, PCI Vendor ID: %x\n",
285                      bios_device.pci_device_id, bios_device.pci_vendor_id);
286 }
287
288 /* Check whether the device has a valid Expansion ROM and search the PCI Data
289  * Structure and any Expansion ROM Header (using dev_scan_exp_header()) for
290  * needed information.  If the rom_addr parameter is != 0, it is the address of
291  * the Expansion ROM image and will be used, if it is == 0, the Expansion ROM
292  * BAR address will be used.
293  */
294 u8
295 biosemu_dev_check_exprom(unsigned long rom_base_addr)
296 {
297         int i = 0;
298         translate_address_t ta;
299         u16 pci_ds_offset;
300         pci_data_struct_t pci_ds;
301         if (rom_base_addr == 0) {
302                 // check for ExpROM Address (Offset 30) in taa
303                 for (i = 0; i <= taa_last_entry; i++) {
304                         ta = translate_address_array[i];
305                         if (ta.cfg_space_offset == 0x30) {
306                                 //translated address
307                                 rom_base_addr = ta.address + ta.address_offset;
308                                 break;
309                         }
310                 }
311         }
312         /* In the ROM there could be multiple Expansion ROM Images... start
313          * searching them for an x86 image.
314          */
315         do {
316                 if (rom_base_addr == 0) {
317                         printf("Error: no Expansion ROM address found!\n");
318                         return -1;
319                 }
320                 set_ci();
321                 u16 rom_signature = in16le((void *) rom_base_addr);
322                 clr_ci();
323                 if (rom_signature != 0xaa55) {
324                         printf
325                             ("Error: invalid Expansion ROM signature: %02x!\n",
326                              *((u16 *) rom_base_addr));
327                         return -1;
328                 }
329                 set_ci();
330                 // at offset 0x18 is the (16bit little-endian) pointer to the PCI Data Structure
331                 pci_ds_offset = in16le((void *) (rom_base_addr + 0x18));
332                 //copy the PCI Data Structure
333                 memcpy(&pci_ds, (void *) (rom_base_addr + pci_ds_offset),
334                        sizeof(pci_ds));
335                 clr_ci();
336 #if defined(CONFIG_X86EMU_DEBUG) && CONFIG_X86EMU_DEBUG
337                 DEBUG_PRINTF("PCI Data Structure @%lx:\n",
338                              rom_base_addr + pci_ds_offset);
339                 dump((void *) &pci_ds, sizeof(pci_ds));
340 #endif
341                 if (strncmp((const char *) pci_ds.signature, "PCIR", 4) != 0) {
342                         printf("Invalid PCI Data Structure found!\n");
343                         break;
344                 }
345                 //little-endian conversion
346                 pci_ds.vendor_id = in16le(&pci_ds.vendor_id);
347                 pci_ds.device_id = in16le(&pci_ds.device_id);
348                 pci_ds.img_length = in16le(&pci_ds.img_length);
349                 pci_ds.pci_ds_length = in16le(&pci_ds.pci_ds_length);
350                 if (pci_ds.vendor_id != bios_device.pci_vendor_id) {
351                         printf
352                             ("Image has invalid Vendor ID: %04x, expected: %04x\n",
353                              pci_ds.vendor_id, bios_device.pci_vendor_id);
354                         break;
355                 }
356                 if (pci_ds.device_id != bios_device.pci_device_id) {
357                         printf
358                             ("Image has invalid Device ID: %04x, expected: %04x\n",
359                              pci_ds.device_id, bios_device.pci_device_id);
360                         break;
361                 }
362                 DEBUG_PRINTF("Image Length: %d\n", pci_ds.img_length * 512);
363                 DEBUG_PRINTF("Image Code Type: %d\n", pci_ds.code_type);
364                 if (pci_ds.code_type == 0) {
365                         //x86 image
366                         //store image address and image length in bios_device struct
367                         bios_device.img_addr = rom_base_addr;
368                         bios_device.img_size = pci_ds.img_length * 512;
369                         // we found the image, exit the loop
370                         break;
371                 } else {
372                         // no x86 image, check next image (if any)
373                         rom_base_addr += pci_ds.img_length * 512;
374                 }
375                 if ((pci_ds.indicator & 0x80) == 0x80) {
376                         //last image found, exit the loop
377                         DEBUG_PRINTF("Last PCI Expansion ROM Image found.\n");
378                         break;
379                 }
380         }
381         while (bios_device.img_addr == 0);
382         // in case we did not find a valid x86 Expansion ROM Image
383         if (bios_device.img_addr == 0) {
384                 printf("Error: no valid x86 Expansion ROM Image found!\n");
385                 return -1;
386         }
387         return 0;
388 }
389
390 u8
391 biosemu_dev_init(struct device * device)
392 {
393         u8 rval = 0;
394         //init bios_device struct
395         DEBUG_PRINTF("%s\n", __func__);
396         memset(&bios_device, 0, sizeof(bios_device));
397
398 #ifndef CONFIG_PCI_OPTION_ROM_RUN_YABEL
399         bios_device.ihandle = of_open(device_name);
400         if (bios_device.ihandle == 0) {
401                 DEBUG_PRINTF("%s is no valid device!\n", device_name);
402                 return -1;
403         }
404         bios_device.phandle = of_finddevice(device_name);
405 #else
406         bios_device.dev = device;
407 #endif
408         biosemu_dev_get_addr_info();
409 #ifndef CONFIG_PCI_OPTION_ROM_RUN_YABEL
410         biosemu_dev_find_vmem_addr();
411         biosemu_dev_get_puid();
412 #endif
413         biosemu_dev_get_device_vendor_id();
414         return rval;
415 }
416
417 // translate address function using translate_address_array assembled
418 // by dev_get_addr_info... MUCH faster than calling translate_address_dev
419 // and accessing client interface for every translation...
420 // returns: 0 if addr not found in translate_address_array, 1 if found.
421 u8
422 biosemu_dev_translate_address(unsigned long * addr)
423 {
424         int i = 0;
425         translate_address_t ta;
426 #ifndef CONFIG_PCI_OPTION_ROM_RUN_YABEL
427         /* we dont need this hack for coreboot... we can access legacy areas */
428         //check if it is an access to legacy VGA Mem... if it is, map the address
429         //to the vmem BAR and then translate it...
430         // (translation info provided by Ben Herrenschmidt)
431         // NOTE: the translation seems to only work for NVIDIA cards... but it is needed
432         // to make some NVIDIA cards work at all...
433         if ((bios_device.vmem_size > 0)
434             && ((*addr >= 0xA0000) && (*addr < 0xB8000))) {
435                 *addr = (*addr - 0xA0000) * 4 + 2 + bios_device.vmem_addr;
436         }
437         if ((bios_device.vmem_size > 0)
438             && ((*addr >= 0xB8000) && (*addr < 0xC0000))) {
439                 u8 shift = *addr & 1;
440                 *addr &= 0xfffffffe;
441                 *addr = (*addr - 0xB8000) * 4 + shift + bios_device.vmem_addr;
442         }
443 #endif
444         for (i = 0; i <= taa_last_entry; i++) {
445                 ta = translate_address_array[i];
446                 if ((*addr >= ta.address) && (*addr <= (ta.address + ta.size))) {
447                         *addr += ta.address_offset;
448                         return 1;
449                 }
450         }
451         return 0;
452 }