VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/pcibios.c@ 42127

Last change on this file since 42127 was 42127, checked in by vboxsync, 12 years ago

BIOS: Updating PCI BIOS service.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: pcibios.c 42127 2012-07-12 15:21:46Z vboxsync $ */
2/** @file
3 * PCI BIOS support.
4 */
5
6/*
7 * Copyright (C) 2004-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <stdint.h>
19#include <string.h>
20#include "biosint.h"
21#include "inlines.h"
22
23//#define DEBUG_PCI 1 //@todo!
24#if DEBUG_PCI
25# define BX_DEBUG_PCI(...) BX_DEBUG(__VA_ARGS__)
26#else
27# define BX_DEBUG_PCI(...)
28#endif
29
30/* PCI function codes. */
31enum pci_func {
32 PCI_BIOS_PRESENT = 0x01, /* PCI BIOS presence check. */
33 FIND_PCI_DEVICE = 0x02, /* Find PCI device by ID. */
34 FIND_PCI_CLASS_CODE = 0x03, /* Find PCI device by class. */
35 GEN_SPECIAL_CYCLE = 0x06, /* Generate special cycle. */
36 READ_CONFIG_BYTE = 0x08, /* Read a byte from PCI config space. */
37 READ_CONFIG_WORD = 0x09, /* Read a word from PCI config space. */
38 READ_CONFIG_DWORD = 0x0A, /* Read a dword from PCI config space. */
39 WRITE_CONFIG_BYTE = 0x0B, /* Write a byte to PCI config space. */
40 WRITE_CONFIG_WORD = 0x0C, /* Write a word to PCI config space. */
41 WRITE_CONFIG_DWORD = 0x0D, /* Write a dword to PCI config space. */
42 GET_IRQ_ROUTING = 0x0E, /* Get IRQ routing table. */
43 SET_PCI_HW_INT = 0x0F, /* Set PCI hardware interrupt. */
44};
45
46enum pci_error {
47 SUCCESSFUL = 0x00, /* Success. */
48 FUNC_NOT_SUPPORTED = 0x81, /* Unsupported function. */
49 BAD_VENDOR_ID = 0x83, /* Bad vendor ID (all bits set) passed. */
50 DEVICE_NOT_FOUND = 0x86, /* No matching device found. */
51 BAD_REGISTER_NUMBER = 0x87, /* Register number out of range. */
52 SET_FAILED = 0x88, /* Failed to set PCI interrupt. */
53 BUFFER_TOO_SMALL = 0x89 /* Routing table buffer insufficient. */
54};
55
56// @todo: merge with system.c
57#define AX r.gr.u.r16.ax
58#define BX r.gr.u.r16.bx
59#define CX r.gr.u.r16.cx
60#define DX r.gr.u.r16.dx
61#define SI r.gr.u.r16.si
62#define DI r.gr.u.r16.di
63#define BP r.gr.u.r16.bp
64#define SP r.gr.u.r16.sp
65#define EAX r.gr.u.r32.eax
66#define EBX r.gr.u.r32.ebx
67#define ECX r.gr.u.r32.ecx
68#define EDX r.gr.u.r32.edx
69#define ES r.es
70
71/* The 16-bit PCI BIOS service must be callable from both real and protected
72 * mode. In protected mode, the caller must set the CS selector base to F0000h
73 * (but the CS selector value is not specified!). The caller does not always
74 * provide a DS which covers the BIOS segment.
75 *
76 * Unlike APM, there are no provisions for the 32-bit PCI BIOS interface
77 * calling the 16-bit implementation.
78 *
79 * The PCI Firmware Specification requires that the PCI BIOS service is called
80 * with at least 1,024 bytes of stack space available, that interrupts are not
81 * enabled during execution, and that the routines are re-entrant.
82 *
83 * Implementation notes:
84 * - The PCI BIOS interface already uses certain 32-bit registers even in
85 * 16-bit mode. To simplify matters, all 32-bit GPRs are saved/restored and
86 * may be used by helper routines (notably for 32-bit port I/O).
87 */
88
89#define PCI_CFG_ADDR 0xCF8
90#define PCI_CFG_DATA 0xCFC
91
92#ifdef __386__
93
94#define PCIxx(x) pci32_##x
95
96/* The stack layout is different in 32-bit mode. */
97typedef struct {
98 pushad_regs_t gr;
99 uint32_t flags;
100} pci_regs_t;
101
102#define FLAGS r.flags
103
104/* In 32-bit mode, don't do any output; not technically impossible but needs
105 * a lot of extra code.
106 */
107#undef BX_INFO
108#define BX_INFO(...)
109#undef BX_DEBUG_PCI
110#define BX_DEBUG_PCI(...)
111
112#else
113
114#define PCIxx(x) pci16_##x
115
116typedef struct {
117 pushad_regs_t gr;
118 uint16_t es;
119 uint16_t ds;
120 iret_addr_t ra;
121} pci_regs_t;
122
123#define FLAGS r.ra.flags.u.r16.flags
124
125#endif
126
127#ifdef __386__
128
129/* 32-bit code can just use the compiler intrinsics. */
130extern unsigned inpd(unsigned port);
131extern unsigned outpd(unsigned port, unsigned value);
132#pragma intrinsic(inpd,outpd)
133
134#else
135
136//@todo: merge with AHCI code
137
138/* Warning: Destroys high bits of EAX. */
139uint32_t inpd(uint16_t port);
140#pragma aux inpd = \
141 ".386" \
142 "in eax, dx" \
143 "mov dx, ax" \
144 "shr eax, 16" \
145 "xchg ax, dx" \
146 parm [dx] value [dx ax] modify nomemory;
147
148/* Warning: Destroys high bits of EAX. */
149void outpd(uint16_t port, uint32_t val);
150#pragma aux outpd = \
151 ".386" \
152 "xchg ax, cx" \
153 "shl eax, 16" \
154 "mov ax, cx" \
155 "out dx, eax" \
156 parm [dx] [cx ax] modify nomemory;
157
158#endif
159
160/* Write the CONFIG_ADDRESS register to prepare for data access. Requires
161 * the register offset to be DWORD aligned (low two bits clear). Warning:
162 * destroys high bits of EAX.
163 */
164void pci16_w_addr(uint16_t bus_dev_fn, uint16_t ofs, uint16_t cfg_addr);
165#pragma aux pci16_w_addr = \
166 ".386" \
167 "movzx eax, ax" \
168 "shl eax, 8" \
169 "or eax, 80000000h" \
170 "mov al, bl" \
171 "out dx, eax" \
172 parm [ax] [bx] [dx] modify exact [ax] nomemory;
173
174
175/* Select a PCI configuration register given its offset and bus/dev/fn.
176 * This is largely a wrapper to avoid excessive inlining.
177 */
178void PCIxx(select_reg)(uint16_t bus_dev_fn, uint16_t ofs)
179{
180 pci16_w_addr(bus_dev_fn, ofs & ~3, PCI_CFG_ADDR);
181}
182
183/* Selected configuration space offsets. */
184#define PCI_VEN_ID 0x00
185#define PCI_DEV_ID 0x02
186#define PCI_REV_ID 0x08
187#define PCI_CLASS_CODE 0x09
188#define PCI_HEADER_TYPE 0x0E
189#define PCI_BRIDGE_SUBORD 0x1A
190
191/* To avoid problems with 16-bit code, we reserve the last possible
192 * bus/dev/fn combination (65,535). Upon reaching this location, the
193 * probing will end.
194 */
195#define INDEX_NOT_FOUND 0xFFFF
196
197/* Find a specified PCI device, either by vendor+device ID or class.
198 * If index is non-zero, the n-th device will be located.
199 *
200 * Note: This function is somewhat performance critical; since it may
201 * generate a high number of port I/O accesses, it can take a significant
202 * amount of time in cases where the caller is looking for a number of
203 * non-present devices.
204 */
205uint16_t PCIxx(find_device)(uint32_t search_item, uint16_t index, int search_class)
206{
207 uint32_t data;
208 uint16_t bus_dev_fn;
209 uint8_t max_bus;
210 uint8_t hdr_type;
211 uint8_t subordinate;
212 int step;
213 int found;
214
215 if (search_class) {
216 BX_DEBUG_PCI("PCI: Find class %08lX index %u\n",
217 search_item, index);
218 } else
219 BX_DEBUG_PCI("PCI: Find device %04X:%04X index %u\n",
220 (uint16_t)search_item, (uint16_t)(search_item >> 16), index);
221
222 bus_dev_fn = 0; /* Start at the beginning. */
223 max_bus = 0; /* Initially assume primary bus only. */
224
225 do {
226 /* For the first function of a device, read the device's header type.
227 * If the header type has all bits set, there's no device. A PCI
228 * multi-function device must implement function 0 and the header type
229 * will be something other than 0xFF. If the header type has the high
230 * bit clear, there is a device but it's not multi-function, so we can
231 * skip probing the next 7 sub-functions.
232 */
233 if ((bus_dev_fn & 7) == 0) {
234 PCIxx(select_reg)(bus_dev_fn, PCI_HEADER_TYPE);
235 hdr_type = inp(PCI_CFG_DATA + (PCI_HEADER_TYPE & 3));
236 if (hdr_type == 0xFF) {
237 bus_dev_fn += 8; /* Skip to next device. */
238 continue;
239 }
240 if (hdr_type & 0x80)
241 step = 1; /* MFD - try every sub-function. */
242 else
243 step = 8; /* No MFD, go to next device after probing. */
244 }
245
246 /* If the header type indicates a bus, we're interested. The secondary
247 * and subordinate bus numbers will indicate which buses are present;
248 * thus we can determine the highest bus number. In the common case,
249 * there will be only the primary bus (i.e. bus 0) and we can avoid
250 * looking at the remaining 255 theoretically present buses. This check
251 * only needs to be done on the primary bus, since bridges must report
252 * all bridges potentially behind them.
253 */
254 if ((hdr_type & 7) == 1 && (bus_dev_fn >> 8) == 0) {
255 /* Read the subordinate (last) bridge number. */
256 PCIxx(select_reg)(bus_dev_fn, PCI_BRIDGE_SUBORD);
257 subordinate = inp(PCI_CFG_DATA + (PCI_BRIDGE_SUBORD & 3));
258 if (subordinate > max_bus)
259 max_bus = subordinate;
260 }
261
262 /* Select the appropriate register. */
263 PCIxx(select_reg)(bus_dev_fn, search_class ? PCI_REV_ID : PCI_VEN_ID);
264 data = inpd(PCI_CFG_DATA);
265 found = 0;
266
267 /* Only 3 bytes are compared for class searches. */
268 if (search_class)
269 data >>= 8;
270
271 BX_DEBUG_PCI("PCI: Data is %08lX @ %02X:%%02X:%01X\n", data,
272 bus_dev_fn >> 8, bus_dev_fn >> 3 & 31, bus_dev_fn & 7);
273
274 if (data == search_item)
275 found = 1;
276
277 /* If device was found but index is non-zero, decrement index and
278 * continue looking. If requested device was found, index will be -1!
279 */
280 if (found && !index--)
281 break;
282
283 bus_dev_fn += step;
284 } while ((bus_dev_fn >> 8) <= max_bus);
285
286 if (index == INDEX_NOT_FOUND)
287 BX_DEBUG_PCI("PCI: Device found (%02X:%%02X:%01X)\n", bus_dev_fn >> 8,
288 bus_dev_fn >> 3 & 31, bus_dev_fn & 7);
289
290 return index == INDEX_NOT_FOUND ? bus_dev_fn : INDEX_NOT_FOUND;
291}
292
293void BIOSCALL PCIxx(function)(volatile pci_regs_t r)
294{
295 uint16_t device;
296
297 BX_DEBUG_PCI("PCI: AX=%04X BX=%04X CX=%04X\n", AX, BX, CX);
298
299 SET_AH(SUCCESSFUL); /* Assume success. */
300 CLEAR_CF();
301
302 switch (GET_AL()) {
303 case PCI_BIOS_PRESENT:
304 AX = 0x0001; /* Configuration mechanism #1 supported. */
305 BX = 0x0210; /* Version 2.1. */
306 //@todo: return true max bus # in CL
307 CX = 0; /* Maximum bus number. */
308 EDX = 'P' | ('C' << 8) | ((uint32_t)'I' << 16) | ((uint32_t)' ' << 24);
309 break;
310 case FIND_PCI_DEVICE:
311 /* Vendor ID FFFFh is reserved so that non-present devices can
312 * be easily detected.
313 */
314 if (DX == 0xFFFF) {
315 SET_AH(BAD_VENDOR_ID);
316 SET_CF();
317 } else {
318 device = PCIxx(find_device)(DX | (uint32_t)CX << 16, SI, 0);
319 if (device == INDEX_NOT_FOUND) {
320 SET_AH(DEVICE_NOT_FOUND);
321 SET_CF();
322 } else {
323 BX = device;
324 }
325 }
326 break;
327 case FIND_PCI_CLASS_CODE:
328 device = PCIxx(find_device)(ECX, SI, 1);
329 if (device == INDEX_NOT_FOUND) {
330 SET_AH(DEVICE_NOT_FOUND);
331 SET_CF();
332 } else {
333 BX = device;
334 }
335 break;
336 case READ_CONFIG_BYTE:
337 case READ_CONFIG_WORD:
338 case READ_CONFIG_DWORD:
339 case WRITE_CONFIG_BYTE:
340 case WRITE_CONFIG_WORD:
341 case WRITE_CONFIG_DWORD:
342 if (DI >= 256) {
343 SET_AH(BAD_REGISTER_NUMBER);
344 SET_CF();
345 } else {
346 PCIxx(select_reg)(BX, DI);
347 switch (GET_AL()) {
348 case READ_CONFIG_BYTE:
349 SET_CL(inp(PCI_CFG_DATA + (DI & 3)));
350 break;
351 case READ_CONFIG_WORD:
352 CX = inpw(PCI_CFG_DATA + (DI & 2));
353 break;
354 case READ_CONFIG_DWORD:
355 ECX = inpd(PCI_CFG_DATA);
356 break;
357 case WRITE_CONFIG_BYTE:
358 outp(PCI_CFG_DATA + (DI & 3), GET_CL());
359 break;
360 case WRITE_CONFIG_WORD:
361 outpw(PCI_CFG_DATA + (DI & 2), CX);
362 break;
363 case WRITE_CONFIG_DWORD:
364 outpd(PCI_CFG_DATA, ECX);
365 break;
366 }
367 }
368 break;
369 default:
370 BX_INFO("PCI: Unsupported function AX=%04X BX=%04X called\n", AX, BX);
371 SET_AH(FUNC_NOT_SUPPORTED);
372 SET_CF();
373 }
374}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette