VirtualBox

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

Last change on this file since 42392 was 42392, checked in by vboxsync, 13 years ago

BIOS: Use new C implementation for 32-bit PCI BIOS, too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/* $Id: pcibios.c 42392 2012-07-25 13:36:28Z 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#if DEBUG_PCI
24# define BX_DEBUG_PCI(...) BX_DEBUG(__VA_ARGS__)
25#else
26# define BX_DEBUG_PCI(...)
27#endif
28
29/* PCI function codes. */
30enum pci_func {
31 PCI_BIOS_PRESENT = 0x01, /* PCI BIOS presence check. */
32 FIND_PCI_DEVICE = 0x02, /* Find PCI device by ID. */
33 FIND_PCI_CLASS_CODE = 0x03, /* Find PCI device by class. */
34 GEN_SPECIAL_CYCLE = 0x06, /* Generate special cycle. */
35 READ_CONFIG_BYTE = 0x08, /* Read a byte from PCI config space. */
36 READ_CONFIG_WORD = 0x09, /* Read a word from PCI config space. */
37 READ_CONFIG_DWORD = 0x0A, /* Read a dword from PCI config space. */
38 WRITE_CONFIG_BYTE = 0x0B, /* Write a byte to PCI config space. */
39 WRITE_CONFIG_WORD = 0x0C, /* Write a word to PCI config space. */
40 WRITE_CONFIG_DWORD = 0x0D, /* Write a dword to PCI config space. */
41 GET_IRQ_ROUTING = 0x0E, /* Get IRQ routing table. */
42 SET_PCI_HW_INT = 0x0F, /* Set PCI hardware interrupt. */
43};
44
45enum pci_error {
46 SUCCESSFUL = 0x00, /* Success. */
47 FUNC_NOT_SUPPORTED = 0x81, /* Unsupported function. */
48 BAD_VENDOR_ID = 0x83, /* Bad vendor ID (all bits set) passed. */
49 DEVICE_NOT_FOUND = 0x86, /* No matching device found. */
50 BAD_REGISTER_NUMBER = 0x87, /* Register number out of range. */
51 SET_FAILED = 0x88, /* Failed to set PCI interrupt. */
52 BUFFER_TOO_SMALL = 0x89 /* Routing table buffer insufficient. */
53};
54
55// @todo: merge with system.c
56#define AX r.gr.u.r16.ax
57#define BX r.gr.u.r16.bx
58#define CX r.gr.u.r16.cx
59#define DX r.gr.u.r16.dx
60#define SI r.gr.u.r16.si
61#define DI r.gr.u.r16.di
62#define BP r.gr.u.r16.bp
63#define SP r.gr.u.r16.sp
64#define EAX r.gr.u.r32.eax
65#define EBX r.gr.u.r32.ebx
66#define ECX r.gr.u.r32.ecx
67#define EDX r.gr.u.r32.edx
68#define ES r.es
69
70/* The 16-bit PCI BIOS service must be callable from both real and protected
71 * mode. In protected mode, the caller must set the CS selector base to F0000h
72 * (but the CS selector value is not specified!). The caller does not always
73 * provide a DS which covers the BIOS segment.
74 *
75 * Unlike APM, there are no provisions for the 32-bit PCI BIOS interface
76 * calling the 16-bit implementation.
77 *
78 * The PCI Firmware Specification requires that the PCI BIOS service is called
79 * with at least 1,024 bytes of stack space available, that interrupts are not
80 * enabled during execution, and that the routines are re-entrant.
81 *
82 * Implementation notes:
83 * - The PCI BIOS interface already uses certain 32-bit registers even in
84 * 16-bit mode. To simplify matters, all 32-bit GPRs are saved/restored and
85 * may be used by helper routines (notably for 32-bit port I/O).
86 */
87
88#define PCI_CFG_ADDR 0xCF8
89#define PCI_CFG_DATA 0xCFC
90
91#ifdef __386__
92
93#define PCIxx(x) pci32_##x
94
95/* The stack layout is different in 32-bit mode. */
96typedef struct {
97 pushad_regs_t gr;
98 uint32_t es;
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/* PCI IRQ routing expansion buffer descriptor. */
161typedef struct {
162 uint16_t buf_size;
163 uint8_t __far *buf_ptr;
164} pci_route_buf;
165
166/* Defined in assembler module .*/
167extern char pci_routing_table[];
168extern uint16_t pci_routing_table_size;
169
170/* Write the CONFIG_ADDRESS register to prepare for data access. Requires
171 * the register offset to be DWORD aligned (low two bits clear). Warning:
172 * destroys high bits of EAX.
173 */
174void pci16_w_addr(uint16_t bus_dev_fn, uint16_t ofs, uint16_t cfg_addr);
175#pragma aux pci16_w_addr = \
176 ".386" \
177 "movzx eax, ax" \
178 "shl eax, 8" \
179 "or eax, 80000000h" \
180 "mov al, bl" \
181 "out dx, eax" \
182 parm [ax] [bx] [dx] modify exact [ax] nomemory;
183
184
185/* Select a PCI configuration register given its offset and bus/dev/fn.
186 * This is largely a wrapper to avoid excessive inlining.
187 */
188void PCIxx(select_reg)(uint16_t bus_dev_fn, uint16_t ofs)
189{
190 pci16_w_addr(bus_dev_fn, ofs & ~3, PCI_CFG_ADDR);
191}
192
193/* Selected configuration space offsets. */
194#define PCI_VEN_ID 0x00
195#define PCI_DEV_ID 0x02
196#define PCI_REV_ID 0x08
197#define PCI_CLASS_CODE 0x09
198#define PCI_HEADER_TYPE 0x0E
199#define PCI_BRIDGE_SUBORD 0x1A
200
201/* To avoid problems with 16-bit code, we reserve the last possible
202 * bus/dev/fn combination (65,535). Upon reaching this location, the
203 * probing will end.
204 */
205#define INDEX_NOT_FOUND 0xFFFF
206
207/* Find a specified PCI device, either by vendor+device ID or class.
208 * If index is non-zero, the n-th device will be located.
209 *
210 * Note: This function is somewhat performance critical; since it may
211 * generate a high number of port I/O accesses, it can take a significant
212 * amount of time in cases where the caller is looking for a number of
213 * non-present devices.
214 */
215uint16_t PCIxx(find_device)(uint32_t search_item, uint16_t index, int search_class)
216{
217 uint32_t data;
218 uint16_t bus_dev_fn;
219 uint8_t max_bus;
220 uint8_t hdr_type;
221 uint8_t subordinate;
222 int step;
223 int found;
224
225 if (search_class) {
226 BX_DEBUG_PCI("PCI: Find class %08lX index %u\n",
227 search_item, index);
228 } else
229 BX_DEBUG_PCI("PCI: Find device %04X:%04X index %u\n",
230 (uint16_t)search_item, (uint16_t)(search_item >> 16), index);
231
232 bus_dev_fn = 0; /* Start at the beginning. */
233 max_bus = 0; /* Initially assume primary bus only. */
234
235 do {
236 /* For the first function of a device, read the device's header type.
237 * If the header type has all bits set, there's no device. A PCI
238 * multi-function device must implement function 0 and the header type
239 * will be something other than 0xFF. If the header type has the high
240 * bit clear, there is a device but it's not multi-function, so we can
241 * skip probing the next 7 sub-functions.
242 */
243 if ((bus_dev_fn & 7) == 0) {
244 PCIxx(select_reg)(bus_dev_fn, PCI_HEADER_TYPE);
245 hdr_type = inp(PCI_CFG_DATA + (PCI_HEADER_TYPE & 3));
246 if (hdr_type == 0xFF) {
247 bus_dev_fn += 8; /* Skip to next device. */
248 continue;
249 }
250 if (hdr_type & 0x80)
251 step = 1; /* MFD - try every sub-function. */
252 else
253 step = 8; /* No MFD, go to next device after probing. */
254 }
255
256 /* If the header type indicates a bus, we're interested. The secondary
257 * and subordinate bus numbers will indicate which buses are present;
258 * thus we can determine the highest bus number. In the common case,
259 * there will be only the primary bus (i.e. bus 0) and we can avoid
260 * looking at the remaining 255 theoretically present buses. This check
261 * only needs to be done on the primary bus, since bridges must report
262 * all bridges potentially behind them.
263 */
264 if ((hdr_type & 7) == 1 && (bus_dev_fn >> 8) == 0) {
265 /* Read the subordinate (last) bridge number. */
266 PCIxx(select_reg)(bus_dev_fn, PCI_BRIDGE_SUBORD);
267 subordinate = inp(PCI_CFG_DATA + (PCI_BRIDGE_SUBORD & 3));
268 if (subordinate > max_bus)
269 max_bus = subordinate;
270 }
271
272 /* Select the appropriate register. */
273 PCIxx(select_reg)(bus_dev_fn, search_class ? PCI_REV_ID : PCI_VEN_ID);
274 data = inpd(PCI_CFG_DATA);
275 found = 0;
276
277 /* Only 3 bytes are compared for class searches. */
278 if (search_class)
279 data >>= 8;
280
281#if 0
282 BX_DEBUG_PCI("PCI: Data is %08lX @ %02X:%%02X:%01X\n", data,
283 bus_dev_fn >> 8, bus_dev_fn >> 3 & 31, bus_dev_fn & 7);
284#endif
285
286 if (data == search_item)
287 found = 1;
288
289 /* If device was found but index is non-zero, decrement index and
290 * continue looking. If requested device was found, index will be -1!
291 */
292 if (found && !index--)
293 break;
294
295 bus_dev_fn += step;
296 } while ((bus_dev_fn >> 8) <= max_bus);
297
298 if (index == INDEX_NOT_FOUND)
299 BX_DEBUG_PCI("PCI: Device found (%02X:%%02X:%01X)\n", bus_dev_fn >> 8,
300 bus_dev_fn >> 3 & 31, bus_dev_fn & 7);
301
302 return index == INDEX_NOT_FOUND ? bus_dev_fn : INDEX_NOT_FOUND;
303}
304
305void BIOSCALL PCIxx(function)(volatile pci_regs_t r)
306{
307 pci_route_buf __far *route_buf;
308 uint16_t device;
309
310 BX_DEBUG_PCI("PCI: AX=%04X BX=%04X CX=%04X\n", AX, BX, CX);
311
312 SET_AH(SUCCESSFUL); /* Assume success. */
313 CLEAR_CF();
314
315 switch (GET_AL()) {
316 case PCI_BIOS_PRESENT:
317 AX = 0x0001; /* Configuration mechanism #1 supported. */
318 BX = 0x0210; /* Version 2.1. */
319 //@todo: return true max bus # in CL
320 CX = 0; /* Maximum bus number. */
321 EDX = 'P' | ('C' << 8) | ((uint32_t)'I' << 16) | ((uint32_t)' ' << 24);
322 break;
323 case FIND_PCI_DEVICE:
324 /* Vendor ID FFFFh is reserved so that non-present devices can
325 * be easily detected.
326 */
327 if (DX == 0xFFFF) {
328 SET_AH(BAD_VENDOR_ID);
329 SET_CF();
330 } else {
331 device = PCIxx(find_device)(DX | (uint32_t)CX << 16, SI, 0);
332 if (device == INDEX_NOT_FOUND) {
333 SET_AH(DEVICE_NOT_FOUND);
334 SET_CF();
335 } else {
336 BX = device;
337 }
338 }
339 break;
340 case FIND_PCI_CLASS_CODE:
341 device = PCIxx(find_device)(ECX, SI, 1);
342 if (device == INDEX_NOT_FOUND) {
343 SET_AH(DEVICE_NOT_FOUND);
344 SET_CF();
345 } else {
346 BX = device;
347 }
348 break;
349 case READ_CONFIG_BYTE:
350 case READ_CONFIG_WORD:
351 case READ_CONFIG_DWORD:
352 case WRITE_CONFIG_BYTE:
353 case WRITE_CONFIG_WORD:
354 case WRITE_CONFIG_DWORD:
355 if (DI >= 256) {
356 SET_AH(BAD_REGISTER_NUMBER);
357 SET_CF();
358 } else {
359 PCIxx(select_reg)(BX, DI);
360 switch (GET_AL()) {
361 case READ_CONFIG_BYTE:
362 SET_CL(inp(PCI_CFG_DATA + (DI & 3)));
363 break;
364 case READ_CONFIG_WORD:
365 CX = inpw(PCI_CFG_DATA + (DI & 2));
366 break;
367 case READ_CONFIG_DWORD:
368 ECX = inpd(PCI_CFG_DATA);
369 break;
370 case WRITE_CONFIG_BYTE:
371 outp(PCI_CFG_DATA + (DI & 3), GET_CL());
372 break;
373 case WRITE_CONFIG_WORD:
374 outpw(PCI_CFG_DATA + (DI & 2), CX);
375 break;
376 case WRITE_CONFIG_DWORD:
377 outpd(PCI_CFG_DATA, ECX);
378 break;
379 }
380 }
381 break;
382 case GET_IRQ_ROUTING:
383 route_buf = ES :> (void *)DI;
384 if (pci_routing_table_size > route_buf->buf_size) {
385 SET_AH(BUFFER_TOO_SMALL);
386 SET_CF();
387 } else {
388 rep_movsb(route_buf->buf_ptr, pci_routing_table, pci_routing_table_size);
389 /* IRQs 9 and 11 are PCI only. */
390 BX = (1 << 9) | (1 << 11);
391 }
392 break;
393 default:
394 BX_INFO("PCI: Unsupported function AX=%04X BX=%04X called\n", AX, BX);
395 SET_AH(FUNC_NOT_SUPPORTED);
396 SET_CF();
397 }
398}
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