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