Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/nvidia.c

1/*
2 * NVidia injector
3 *
4 * Copyright (C) 2009 Jasmin Fazlic, iNDi
5 *
6 * NVidia injector is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * NVidia driver and injector is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NVidia injector. If not, see <http://www.gnu.org/licenses/>.
18 */
19/*
20 * Alternatively you can choose to comply with APSL
21 */
22
23
24/*
25 * DCB-Table parsing is based on software (nouveau driver) originally distributed under following license:
26 *
27 *
28 * Copyright 2005-2006 Erik Waling
29 * Copyright 2006 Stephane Marchesin
30 * Copyright 2007-2009 Stuart Bennett
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
46 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
47 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
48 * SOFTWARE.
49 */
50
51#include "libsaio.h"
52#include "boot.h"
53#include "bootstruct.h"
54#include "pci.h"
55#include "platform.h"
56#include "device_inject.h"
57#include "nvidia.h"
58
59#ifndef DEBUG_NVIDIA
60#define DEBUG_NVIDIA 0
61#endif
62
63#if DEBUG_NVIDIA
64#define DBG(x...)printf(x)
65#else
66#define DBG(x...)
67#endif
68
69#define NVIDIA_ROM_SIZE 0x10000
70#define PATCH_ROM_SUCCESS 1
71#define PATCH_ROM_SUCCESS_HAS_LVDS 2
72#define PATCH_ROM_FAILED 0
73#define MAX_NUM_DCB_ENTRIES 16
74
75#define TYPE_GROUPED 0xff
76
77extern uint32_t devices_number;
78
79const char *nvidia_compatible_0[]={ "@0,compatible","NVDA,NVMac" };
80const char *nvidia_compatible_1[]={ "@1,compatible","NVDA,NVMac" };
81const char *nvidia_device_type_0[]={ "@0,device_type","display" };
82const char *nvidia_device_type_1[]={ "@1,device_type","display" };
83const char *nvidia_device_type[]={ "device_type","NVDA,Parent" };
84const char *nvidia_name_0[]={ "@0,name","NVDA,Display-A" };
85const char *nvidia_name_1[]={ "@1,name","NVDA,Display-B" };
86const char *nvidia_slot_name[]={ "AAPL,slot-name","Slot-1" };
87
88static uint8_t default_NVCAP[]= {
890x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00,
900x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a,
910x00, 0x00, 0x00, 0x00
92};
93
94#define NVCAP_LEN ( sizeof(default_NVCAP) / sizeof(uint8_t) )
95
96static struct nv_chipsets_t NVKnownChipsets[] = {
97{ 0x00000000, "Unknown" },
98{ 0x10DE0040, "GeForce 6800 Ultra" },
99{ 0x10DE0041, "GeForce 6800" },
100{ 0x10DE0042, "GeForce 6800 LE" },
101{ 0x10DE0043, "GeForce 6800 XE" },
102{ 0x10DE0044, "GeForce 6800 XT" },
103{ 0x10DE0045, "GeForce 6800 GT" },
104{ 0x10DE0046, "GeForce 6800 GT" },
105{ 0x10DE0047, "GeForce 6800 GS" },
106{ 0x10DE0048, "GeForce 6800 XT" },
107{ 0x10DE004E, "Quadro FX 4000" },
108{ 0x10DE0090, "GeForce 7800 GTX" },
109{ 0x10DE0091, "GeForce 7800 GTX" },
110{ 0x10DE0092, "GeForce 7800 GT" },
111{ 0x10DE0093, "GeForce 7800 GS" },
112{ 0x10DE0095, "GeForce 7800 SLI" },
113{ 0x10DE0098, "GeForce Go 7800" },
114{ 0x10DE0099, "GeForce Go 7800 GTX" },
115{ 0x10DE009D, "Quadro FX 4500" },
116{ 0x10DE00C0, "GeForce 6800 GS" },
117{ 0x10DE00C1, "GeForce 6800" },
118{ 0x10DE00C2, "GeForce 6800 LE" },
119{ 0x10DE00C3, "GeForce 6800 XT" },
120{ 0x10DE00C8, "GeForce Go 6800" },
121{ 0x10DE00C9, "GeForce Go 6800 Ultra" },
122{ 0x10DE00CC, "Quadro FX Go1400" },
123{ 0x10DE00CD, "Quadro FX 3450/4000 SDI" },
124{ 0x10DE00CE, "Quadro FX 1400" },
125{ 0x10DE0140, "GeForce 6600 GT" },
126{ 0x10DE0141, "GeForce 6600" },
127{ 0x10DE0142, "GeForce 6600 LE" },
128{ 0x10DE0143, "GeForce 6600 VE" },
129{ 0x10DE0144, "GeForce Go 6600" },
130{ 0x10DE0145, "GeForce 6610 XL" },
131{ 0x10DE0146, "GeForce Go 6600 TE/6200 TE" },
132{ 0x10DE0147, "GeForce 6700 XL" },
133{ 0x10DE0148, "GeForce Go 6600" },
134{ 0x10DE0149, "GeForce Go 6600 GT" },
135{ 0x10DE014C, "Quadro FX 550" },
136{ 0x10DE014D, "Quadro FX 550" },
137{ 0x10DE014E, "Quadro FX 540" },
138{ 0x10DE014F, "GeForce 6200" },
139{ 0x10DE0160, "GeForce 6500" },
140{ 0x10DE0161, "GeForce 6200 TurboCache(TM)" },
141{ 0x10DE0162, "GeForce 6200SE TurboCache(TM)" },
142{ 0x10DE0163, "GeForce 6200 LE" },
143{ 0x10DE0164, "GeForce Go 6200" },
144{ 0x10DE0165, "Quadro NVS 285" },
145{ 0x10DE0166, "GeForce Go 6400" },
146{ 0x10DE0167, "GeForce Go 6200" },
147{ 0x10DE0168, "GeForce Go 6400" },
148{ 0x10DE0169, "GeForce 6250" },
149{ 0x10DE016A, "GeForce 7100 GS" },
150{ 0x10DE0191, "GeForce 8800 GTX" },
151{ 0x10DE0193, "GeForce 8800 GTS" },
152{ 0x10DE0194, "GeForce 8800 Ultra" },
153{ 0x10DE019D, "Quadro FX 5600" },
154{ 0x10DE019E, "Quadro FX 4600" },
155{ 0x10DE01D1, "GeForce 7300 LE" },
156{ 0x10DE01D3, "GeForce 7300 SE" },
157{ 0x10DE01D6, "GeForce Go 7200" },
158{ 0x10DE01D7, "GeForce Go 7300" },
159{ 0x10DE01D8, "GeForce Go 7400" },
160{ 0x10DE01D9, "GeForce Go 7400 GS" },
161{ 0x10DE01DA, "Quadro NVS 110M" },
162{ 0x10DE01DB, "Quadro NVS 120M" },
163{ 0x10DE01DC, "Quadro FX 350M" },
164{ 0x10DE01DD, "GeForce 7500 LE" },
165{ 0x10DE01DE, "Quadro FX 350" },
166{ 0x10DE01DF, "GeForce 7300 GS" },
167{ 0x10DE0211, "GeForce 6800" },
168{ 0x10DE0212, "GeForce 6800 LE" },
169{ 0x10DE0215, "GeForce 6800 GT" },
170{ 0x10DE0218, "GeForce 6800 XT" },
171{ 0x10DE0221, "GeForce 6200" },
172{ 0x10DE0222, "GeForce 6200 A-LE" },
173{ 0x10DE0240, "GeForce 6150" },
174{ 0x10DE0241, "GeForce 6150 LE" },
175{ 0x10DE0242, "GeForce 6100" },
176{ 0x10DE0244, "GeForce Go 6150" },
177{ 0x10DE0247, "GeForce Go 6100" },
178{ 0x10DE0290, "GeForce 7900 GTX" },
179{ 0x10DE0291, "GeForce 7900 GT" },
180{ 0x10DE0292, "GeForce 7900 GS" },
181{ 0x10DE0298, "GeForce Go 7900 GS" },
182{ 0x10DE0299, "GeForce Go 7900 GTX" },
183{ 0x10DE029A, "Quadro FX 2500M" },
184{ 0x10DE029B, "Quadro FX 1500M" },
185{ 0x10DE029C, "Quadro FX 5500" },
186{ 0x10DE029D, "Quadro FX 3500" },
187{ 0x10DE029E, "Quadro FX 1500" },
188{ 0x10DE029F, "Quadro FX 4500 X2" },
189{ 0x10DE0301, "GeForce FX 5800 Ultra" },
190{ 0x10DE0302, "GeForce FX 5800" },
191{ 0x10DE0308, "Quadro FX 2000" },
192{ 0x10DE0309, "Quadro FX 1000" },
193{ 0x10DE0311, "GeForce FX 5600 Ultra" },
194{ 0x10DE0312, "GeForce FX 5600" },
195{ 0x10DE0314, "GeForce FX 5600XT" },
196{ 0x10DE031A, "GeForce FX Go5600" },
197{ 0x10DE031B, "GeForce FX Go5650" },
198{ 0x10DE031C, "Quadro FX Go700" },
199{ 0x10DE0324, "GeForce FX Go5200" },
200{ 0x10DE0325, "GeForce FX Go5250" },
201{ 0x10DE0326, "GeForce FX 5500" },
202{ 0x10DE0328, "GeForce FX Go5200 32M/64M" },
203{ 0x10DE032A, "Quadro NVS 55/280 PCI" },
204{ 0x10DE032B, "Quadro FX 500/600 PCI" },
205{ 0x10DE032C, "GeForce FX Go53xx Series" },
206{ 0x10DE032D, "GeForce FX Go5100" },
207{ 0x10DE0330, "GeForce FX 5900 Ultra" },
208{ 0x10DE0331, "GeForce FX 5900" },
209{ 0x10DE0332, "GeForce FX 5900XT" },
210{ 0x10DE0333, "GeForce FX 5950 Ultra" },
211{ 0x10DE0334, "GeForce FX 5900ZT" },
212{ 0x10DE0338, "Quadro FX 3000" },
213{ 0x10DE033F, "Quadro FX 700" },
214{ 0x10DE0341, "GeForce FX 5700 Ultra" },
215{ 0x10DE0342, "GeForce FX 5700" },
216{ 0x10DE0343, "GeForce FX 5700LE" },
217{ 0x10DE0344, "GeForce FX 5700VE" },
218{ 0x10DE0347, "GeForce FX Go5700" },
219{ 0x10DE0348, "GeForce FX Go5700" },
220{ 0x10DE034C, "Quadro FX Go1000" },
221{ 0x10DE034E, "Quadro FX 1100" },
222{ 0x10DE0391, "GeForce 7600 GT" },
223{ 0x10DE0392, "GeForce 7600 GS" },
224{ 0x10DE0393, "GeForce 7300 GT" },
225{ 0x10DE0394, "GeForce 7600 LE" },
226{ 0x10DE0395, "GeForce 7300 GT" },
227{ 0x10DE0397, "GeForce Go 7700" },
228{ 0x10DE0398, "GeForce Go 7600" },
229{ 0x10DE0399, "GeForce Go 7600 GT"},
230{ 0x10DE039A, "Quadro NVS 300M" },
231{ 0x10DE039B, "GeForce Go 7900 SE" },
232{ 0x10DE039C, "Quadro FX 550M" },
233{ 0x10DE039E, "Quadro FX 560" },
234{ 0x10DE0400, "GeForce 8600 GTS" },
235{ 0x10DE0401, "GeForce 8600 GT" },
236{ 0x10DE0402, "GeForce 8600 GT" },
237{ 0x10DE0403, "GeForce 8600 GS" },
238{ 0x10DE0404, "GeForce 8400 GS" },
239{ 0x10DE0405, "GeForce 9500M GS" },
240{ 0x10DE0407, "GeForce 8600M GT" },
241{ 0x10DE0408, "GeForce 9650M GS" },
242{ 0x10DE0409, "GeForce 8700M GT" },
243{ 0x10DE040A, "Quadro FX 370" },
244{ 0x10DE040B, "Quadro NVS 320M" },
245{ 0x10DE040C, "Quadro FX 570M" },
246{ 0x10DE040D, "Quadro FX 1600M" },
247{ 0x10DE040E, "Quadro FX 570" },
248{ 0x10DE040F, "Quadro FX 1700" },
249{ 0x10DE0420, "GeForce 8400 SE" },
250{ 0x10DE0421, "GeForce 8500 GT" },
251{ 0x10DE0422, "GeForce 8400 GS" },
252{ 0x10DE0423, "GeForce 8300 GS" },
253{ 0x10DE0424, "GeForce 8400 GS" },
254{ 0x10DE0425, "GeForce 8600M GS" },
255{ 0x10DE0426, "GeForce 8400M GT" },
256{ 0x10DE0427, "GeForce 8400M GS" },
257{ 0x10DE0428, "GeForce 8400M G" },
258{ 0x10DE0429, "Quadro NVS 140M" },
259{ 0x10DE042A, "Quadro NVS 130M" },
260{ 0x10DE042B, "Quadro NVS 135M" },
261{ 0x10DE042C, "GeForce 9400 GT" },
262{ 0x10DE042D, "Quadro FX 360M" },
263{ 0x10DE042E, "GeForce 9300M G" },
264{ 0x10DE042F, "Quadro NVS 290" },
265{ 0x10DE05E0, "GeForce GTX 295" },
266{ 0x10DE05E1, "GeForce GTX 280" },
267{ 0x10DE05E2, "GeForce GTX 260" },
268{ 0x10DE05E3, "GeForce GTX 285" },
269{ 0x10DE05E6, "GeForce GTX 275" },
270{ 0x10DE05EA, "GeForce GTX 260" },
271{ 0x10DE05EB, "GeForce GTX 295" },
272{ 0x10DE05F9, "Quadro CX" },
273{ 0x10DE05FD, "Quadro FX 5800" },
274{ 0x10DE05FE, "Quadro FX 4800" },
275{ 0x10DE0600, "GeForce 8800 GTS 512" },
276{ 0x10DE0602, "GeForce 8800 GT" },
277{ 0x10DE0604, "GeForce 9800 GX2" },
278{ 0x10DE0605, "GeForce 9800 GT" },
279{ 0x10DE0606, "GeForce 8800 GS" },
280{ 0x10DE0607, "GeForce GTS 240" },
281{ 0x10DE0608, "GeForce 9800M GTX" },
282{ 0x10DE0609, "GeForce 8800M GTS" },
283{ 0x10DE060A, "GeForce GTX 280M" },
284{ 0x10DE060B, "GeForce 9800M GT" },
285{ 0x10DE060C, "GeForce 8800M GTX" },
286{ 0x10DE060D, "GeForce 8800 GS" },
287{ 0x10DE0610, "GeForce 9600 GSO" },
288{ 0x10DE0611, "GeForce 8800 GT" },
289{ 0x10DE0612, "GeForce 9800 GTX" },
290{ 0x10DE0613, "GeForce 9800 GTX+" },
291{ 0x10DE0614, "GeForce 9800 GT" },
292{ 0x10DE0615, "GeForce GTS 250" },
293{ 0x10DE0617, "GeForce 9800M GTX" },
294{ 0x10DE0618, "GeForce GTX 260M" },
295{ 0x10DE061A, "Quadro FX 3700" },
296{ 0x10DE061C, "Quadro FX 3600M" },
297{ 0x10DE061D, "Quadro FX 2800M" },
298{ 0x10DE061F, "Quadro FX 3800M" },
299{ 0x10DE0622, "GeForce 9600 GT" },
300{ 0x10DE0623, "GeForce 9600 GS" },
301{ 0x10DE0625, "GeForce 9600 GSO 512"},
302{ 0x10DE0626, "GeForce GT 130" },
303{ 0x10DE0627, "GeForce GT 140" },
304{ 0x10DE0628, "GeForce 9800M GTS" },
305{ 0x10DE062A, "GeForce 9700M GTS" },
306{ 0x10DE062C, "GeForce 9800M GTS" },
307{ 0x10DE0640, "GeForce 9500 GT" },
308{ 0x10DE0641, "GeForce 9400 GT" },
309{ 0x10DE0642, "GeForce 8400 GS" },
310{ 0x10DE0643, "GeForce 9500 GT" },
311{ 0x10DE0644, "GeForce 9500 GS" },
312{ 0x10DE0645, "GeForce 9500 GS" },
313{ 0x10DE0646, "GeForce GT 120" },
314{ 0x10DE0647, "GeForce 9600M GT" },
315{ 0x10DE0648, "GeForce 9600M GS" },
316{ 0x10DE0649, "GeForce 9600M GT" },
317{ 0x10DE064A, "GeForce 9700M GT" },
318{ 0x10DE064B, "GeForce 9500M G" },
319{ 0x10DE064C, "GeForce 9650M GT" },
320{ 0x10DE0652, "GeForce GT 130M" },
321{ 0x10DE0658, "Quadro FX 380" },
322{ 0x10DE0659, "Quadro FX 580" },
323{ 0x10DE065A, "Quadro FX 1700M" },
324{ 0x10DE065B, "GeForce 9400 GT" },
325{ 0x10DE065C, "Quadro FX 770M" },
326{ 0x10DE06C0, "GeForce GTX 480" },
327{ 0x10DE06C4, "GeForce GTX 465" },
328{ 0x10DE06CA, "GeForce GTX 480M" },
329{ 0x10DE06CD, "GeForce GTX 470" },
330{ 0x10DE06E0, "GeForce 9300 GE" },
331{ 0x10DE06E1, "GeForce 9300 GS" },
332{ 0x10DE06E4, "GeForce 8400 GS" },
333{ 0x10DE06E5, "GeForce 9300M GS" },
334{ 0x10DE06E8, "GeForce 9200M GS" },
335{ 0x10DE06E9, "GeForce 9300M GS" },
336{ 0x10DE06EA, "Quadro NVS 150M" },
337{ 0x10DE06EB, "Quadro NVS 160M" },
338{ 0x10DE06EC, "GeForce G 105M" },
339{ 0x10DE06EF, "GeForce G 103M" },
340{ 0x10DE06F8, "Quadro NVS 420" },
341{ 0x10DE06F9, "Quadro FX 370 LP" },
342{ 0x10DE06FA, "Quadro NVS 450" },
343{ 0x10DE06FD, "Quadro NVS 295" },
344{ 0x10DE086C, "GeForce 9300/nForce 730i" },
345{ 0x10DE087D, "ION 9400M" },
346{ 0x10DE087E, "ION LE" },
347{ 0x10DE0A20, "GeForce GT220" },
348{ 0x10DE0A23, "GeForce 210" },
349{ 0x10DE0A28, "GeForce GT 230M" },
350{ 0x10DE0A29, "GeForce GT 330M" },
351{ 0x10DE0A2A, "GeForce GT 230M" },
352{ 0x10DE0A34, "GeForce GT 240M" },
353{ 0x10DE0A60, "GeForce G210" },
354{ 0x10DE0A62, "GeForce 205" },
355{ 0x10DE0A63, "GeForce 310" },
356{ 0x10DE0A65, "GeForce 210" },
357{ 0x10DE0A66, "GeForce 310" },
358{ 0x10DE0A74, "GeForce G210M" },
359{ 0x10DE0A75, "GeForce G310M" },
360{ 0x10DE0A78, "Quadro FX 380 LP" },
361{ 0x10DE0CA3, "GeForce GT 240" },
362{ 0x10DE0CA8, "GeForce GTS 260M" },
363{ 0x10DE0CA9, "GeForce GTS 250M" },
364{ 0x10DE0CB1, "GeForce GTS 360M" },
365{ 0x10DE0CA3, "GeForce GT240" },
366{ 0x10DE0E22, "GeForce GTX 460" },
367{ 0x10DE0E24, "GeForce GTX 460" },
368{ 0x10DE06D1, "Tesla C2050" },// TODO: sub-device id: 0x0771
369{ 0x10DE06D1, "Tesla C2070" },// TODO: sub-device id: 0x0772
370{ 0x10DE06DE, "Tesla M2050" },// TODO: sub-device id: 0x0846
371{ 0x10DE06DE, "Tesla M2070" },// TODO: sub-device id: ?
372{ 0x10DE06DD, "Quadro 4000" },
373{ 0x10DE0DC4, "GeForce GTS 450" },
374{ 0x10DE0DE1, "GeForce GT 430" },
375{ 0x10DE1080, "GeForce GTX 580" }
376};
377
378static uint16_t swap16(uint16_t x)
379{
380return (((x & 0x00FF) << 8) | ((x & 0xFF00) >> 8));
381}
382
383static uint16_t read16(uint8_t *ptr, uint16_t offset)
384{
385uint8_t ret[2];
386ret[0] = ptr[offset+1];
387ret[1] = ptr[offset];
388return *((uint16_t*)&ret);
389}
390
391#if 0
392static uint32_t swap32(uint32_t x)
393{
394return ((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8 ) | ((x & 0x00FF0000) >> 8 ) | ((x & 0xFF000000) >> 24);
395}
396
397static uint8_t read8(uint8_t *ptr, uint16_t offset)
398{
399return ptr[offset];
400}
401
402static uint32_t read32(uint8_t *ptr, uint16_t offset)
403{
404uint8_t ret[4];
405ret[0] = ptr[offset+3];
406ret[1] = ptr[offset+2];
407ret[2] = ptr[offset+1];
408ret[3] = ptr[offset];
409return *((uint32_t*)&ret);
410}
411#endif
412
413static int patch_nvidia_rom(uint8_t *rom)
414{
415if (!rom || (rom[0] != 0x55 && rom[1] != 0xaa)) {
416printf("False ROM signature: 0x%02x%02x\n", rom[0], rom[1]);
417return PATCH_ROM_FAILED;
418}
419
420uint16_t dcbptr = swap16(read16(rom, 0x36));
421if(!dcbptr) {
422printf("no dcb table found\n");
423return PATCH_ROM_FAILED;
424}/* else
425 printf("dcb table at offset 0x%04x\n", dcbptr);
426 */
427uint8_t *dcbtable = &rom[dcbptr];
428uint8_t dcbtable_version = dcbtable[0];
429uint8_t headerlength = 0;
430uint8_t recordlength = 0;
431uint8_t numentries = 0;
432
433if(dcbtable_version >= 0x20) {
434uint32_t sig;
435
436if(dcbtable_version >= 0x30) {
437headerlength = dcbtable[1];
438numentries = dcbtable[2];
439recordlength = dcbtable[3];
440sig = *(uint32_t *)&dcbtable[6];
441} else {
442sig = *(uint32_t *)&dcbtable[4];
443headerlength = 8;
444}
445if (sig != 0x4edcbdcb) {
446printf("bad display config block signature (0x%8x)\n", sig);
447return PATCH_ROM_FAILED;
448}
449} else if (dcbtable_version >= 0x14) { /* some NV15/16, and NV11+ */
450char sig[8] = { 0 };
451
452strncpy(sig, (char *)&dcbtable[-7], 7);
453recordlength = 10;
454if (strcmp(sig, "DEV_REC")) {
455printf("Bad Display Configuration Block signature (%s)\n", sig);
456return PATCH_ROM_FAILED;
457}
458} else {
459printf("ERROR: dcbtable_version is 0x%X\n", dcbtable_version);
460return PATCH_ROM_FAILED;
461}
462
463if(numentries >= MAX_NUM_DCB_ENTRIES)
464numentries = MAX_NUM_DCB_ENTRIES;
465
466uint8_t num_outputs = 0, i=0;
467struct dcbentry {
468uint8_t type;
469uint8_t index;
470uint8_t *heads;
471} entries[numentries];
472
473for (i = 0; i < numentries; i++) {
474uint32_t connection;
475connection = *(uint32_t *)&dcbtable[headerlength + recordlength * i];
476/* Should we allow discontinuous DCBs? Certainly DCB I2C tables can be discontinuous */
477if ((connection & 0x0000000f) == 0x0000000f) /* end of records */
478continue;
479if (connection == 0x00000000) /* seen on an NV11 with DCB v1.5 */
480continue;
481if ((connection & 0xf) == 0x6) /* we skip type 6 as it doesnt appear on macbook nvcaps */
482continue;
483
484entries[num_outputs].type = connection & 0xf;
485entries[num_outputs].index = num_outputs;
486entries[num_outputs++].heads = (uint8_t*)&(dcbtable[(headerlength + recordlength * i) + 1]);
487
488}
489
490int has_lvds = false;
491uint8_t channel1 = 0, channel2 = 0;
492
493for(i=0; i<num_outputs; i++) {
494if(entries[i].type == 3) {
495has_lvds = true;
496//printf("found LVDS\n");
497channel1 |= ( 0x1 << entries[i].index);
498entries[i].type = TYPE_GROUPED;
499}
500}
501// if we have a LVDS output, we group the rest to the second channel
502if(has_lvds) {
503for(i=0; i<num_outputs; i++) {
504if(entries[i].type == TYPE_GROUPED)
505continue;
506channel2 |= ( 0x1 << entries[i].index);
507entries[i].type = TYPE_GROUPED;
508}
509} else {
510//
511int x;
512// we loop twice as we need to generate two channels
513for(x=0; x<=1; x++) {
514for(i=0; i<num_outputs; i++) {
515if(entries[i].type == TYPE_GROUPED)
516continue;
517// if type is TMDS, the prior output is ANALOG
518// we always group ANALOG and TMDS
519// if there is a TV output after TMDS, we group it to that channel as well
520if(i && entries[i].type == 0x2) {
521switch (x) {
522case 0:
523//printf("group channel 1\n");
524channel1 |= ( 0x1 << entries[i].index);
525entries[i].type = TYPE_GROUPED;
526if((entries[i-1].type == 0x0)) {
527channel1 |= ( 0x1 << entries[i-1].index);
528entries[i-1].type = TYPE_GROUPED;
529}
530// group TV as well if there is one
531if( ((i+1) < num_outputs) && (entries[i+1].type == 0x1) ) {
532//printf("group tv1\n");
533channel1 |= ( 0x1 << entries[i+1].index);
534entries[i+1].type = TYPE_GROUPED;
535}
536break;
537case 1:
538//printf("group channel 2 : %d\n", i);
539channel2 |= ( 0x1 << entries[i].index);
540entries[i].type = TYPE_GROUPED;
541if((entries[i-1].type == 0x0)) {
542channel2 |= ( 0x1 << entries[i-1].index);
543entries[i-1].type = TYPE_GROUPED;
544}
545// group TV as well if there is one
546if( ((i+1) < num_outputs) && (entries[i+1].type == 0x1) ) {
547//printf("group tv2\n");
548channel2 |= ( 0x1 << entries[i+1].index);
549entries[i+1].type = TYPE_GROUPED;
550}
551break;
552
553}
554break;
555}
556}
557}
558}
559
560// if we have left ungrouped outputs merge them to the empty channel
561uint8_t *togroup;// = (channel1 ? (channel2 ? NULL : &channel2) : &channel1);
562togroup = &channel2;
563for(i=0; i<num_outputs;i++)
564if(entries[i].type != TYPE_GROUPED) {
565//printf("%d not grouped\n", i);
566if(togroup)
567*togroup |= ( 0x1 << entries[i].index);
568entries[i].type = TYPE_GROUPED;
569}
570
571if(channel1 > channel2) {
572uint8_t buff = channel1;
573channel1 = channel2;
574channel2 = buff;
575}
576
577default_NVCAP[6] = channel1;
578default_NVCAP[8] = channel2;
579
580// patching HEADS
581for(i=0; i<num_outputs;i++) {
582if(channel1 & (1 << i))
583*entries[i].heads = 1;
584else if(channel2 & (1 << i))
585*entries[i].heads = 2;
586}
587
588return (has_lvds ? PATCH_ROM_SUCCESS_HAS_LVDS : PATCH_ROM_SUCCESS);
589}
590
591static char *get_nvidia_model(uint32_t id) {
592inti;
593
594for (i=1; i< (sizeof(NVKnownChipsets) / sizeof(NVKnownChipsets[0])); i++) {
595if (NVKnownChipsets[i].device == id) {
596return NVKnownChipsets[i].name;
597}
598}
599return NVKnownChipsets[0].name;
600}
601
602static uint32_t load_nvidia_bios_file(const char *filename, uint8_t *buf, int bufsize)
603{
604intfd;
605intsize;
606
607if ((fd = open_bvdev("bt(0,0)", filename, 0)) < 0) {
608return 0;
609}
610size = file_size(fd);
611if (size > bufsize) {
612printf("Filesize of %s is bigger than expected! Truncating to 0x%x Bytes!\n", filename, bufsize);
613size = bufsize;
614}
615size = read(fd, (char *)buf, size);
616close(fd);
617return size > 0 ? size : 0;
618}
619
620static int devprop_add_nvidia_template(struct DevPropDevice *device)
621{
622chartmp[16];
623
624if(!device)
625return 0;
626
627if(!DP_ADD_TEMP_VAL(device, nvidia_compatible_0))
628return 0;
629if(!DP_ADD_TEMP_VAL(device, nvidia_device_type_0))
630return 0;
631if(!DP_ADD_TEMP_VAL(device, nvidia_name_0))
632return 0;
633if(!DP_ADD_TEMP_VAL(device, nvidia_compatible_1))
634return 0;
635if(!DP_ADD_TEMP_VAL(device, nvidia_device_type_1))
636return 0;
637if(!DP_ADD_TEMP_VAL(device, nvidia_name_1))
638return 0;
639if(!DP_ADD_TEMP_VAL(device, nvidia_device_type))
640return 0;
641// Rek : Dont use sprintf return, it does not WORK !! our custom sprintf() always return 0!
642// len = sprintf(tmp, "Slot-%x", devices_number);
643sprintf(tmp, "Slot-%x",devices_number);
644devprop_add_value(device, "AAPL,slot-name", (uint8_t *) tmp, strlen(tmp));
645devices_number++;
646
647return 1;
648}
649
650int hex2bin(const char *hex, uint8_t *bin, int len)
651{
652char*p;
653inti;
654charbuf[3];
655
656if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2) {
657printf("[ERROR] bin2hex input error\n");
658return -1;
659}
660
661buf[2] = '\0';
662p = (char *) hex;
663for (i=0; i<len; i++) {
664if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
665printf("[ERROR] bin2hex '%s' syntax error\n", hex);
666return -2;
667}
668buf[0] = *p++;
669buf[1] = *p++;
670bin[i] = (unsigned char) strtoul(buf, NULL, 16);
671}
672return 0;
673}
674
675unsigned long long mem_detect(volatile uint8_t *regs, uint8_t nvCardType, pci_dt_t *nvda_dev)
676{
677unsigned long long vram_size = 0;
678
679if (nvCardType < NV_ARCH_50) {
680vram_size = REG32(NV04_PFB_FIFO_DATA);
681vram_size &= NV10_PFB_FIFO_DATA_RAM_AMOUNT_MB_MASK;
682}
683else if (nvCardType >= NV_ARCH_C0) {
684vram_size = REG32(NVC0_MEM_CTRLR_COUNT);
685vram_size *= REG32(NVC0_MEM_CTRLR_RAM_AMOUNT);
686vram_size <<= 20;
687}
688else {
689vram_size = REG32(NV04_PFB_FIFO_DATA);
690vram_size |= (vram_size & 0xff) << 32;
691vram_size &= 0xffffffff00ll;
692}
693
694return vram_size;
695}
696
697bool setup_nvidia_devprop(pci_dt_t *nvda_dev)
698{
699struct DevPropDevice*device;
700char*devicepath;
701struct pci_rom_pci_header_t*rom_pci_header;
702volatile uint8_t*regs;
703uint8_t*rom;
704uint8_t*nvRom;
705uint8_tnvCardType;
706unsigned long longvideoRam;
707uint32_tnvBiosOveride;
708uint32_tbar[7];
709uint32_tboot_display;
710intnvPatch;
711intlen;
712charbiosVersion[32];
713charnvFilename[32];
714charkNVCAP[12];
715char*model;
716const char*value;
717booldoit;
718
719devicepath = get_pci_dev_path(nvda_dev);
720bar[0] = pci_config_read32(nvda_dev->dev.addr, 0x10 );
721regs = (uint8_t *) (bar[0] & ~0x0f);
722
723delay(50);
724
725// get card type
726nvCardType = (REG32(0) >> 20) & 0x1ff;
727
728// Amount of VRAM in kilobytes
729videoRam = mem_detect(regs, nvCardType, nvda_dev);
730model = get_nvidia_model((nvda_dev->vendor_id << 16) | nvda_dev->device_id);
731
732verbose("nVidia %s %dMB NV%02x [%04x:%04x] :: %s\n",
733model, (uint32_t)(videoRam / 1024 / 1024),
734(REG32(0) >> 20) & 0x1ff, nvda_dev->vendor_id, nvda_dev->device_id,
735devicepath);
736
737rom = malloc(NVIDIA_ROM_SIZE);
738sprintf(nvFilename, "/Extra/%04x_%04x.rom", (uint16_t)nvda_dev->vendor_id, (uint16_t)nvda_dev->device_id);
739if (getBoolForKey(kUseNvidiaROM, &doit, &bootInfo->bootConfig) && doit) {
740verbose("Looking for nvidia video bios file %s\n", nvFilename);
741nvBiosOveride = load_nvidia_bios_file(nvFilename, rom, NVIDIA_ROM_SIZE);
742if (nvBiosOveride > 0) {
743verbose("Using nVidia Video BIOS File %s (%d Bytes)\n", nvFilename, nvBiosOveride);
744DBG("%s Signature 0x%02x%02x %d bytes\n", nvFilename, rom[0], rom[1], nvBiosOveride);
745} else {
746printf("ERROR: unable to open nVidia Video BIOS File %s\n", nvFilename);
747return false;
748}
749} else {
750// Otherwise read bios from card
751nvBiosOveride = 0;
752
753// TODO: we should really check for the signature before copying the rom, i think.
754
755// PRAMIN first
756nvRom = (uint8_t*)&regs[NV_PRAMIN_OFFSET];
757bcopy((uint32_t *)nvRom, rom, NVIDIA_ROM_SIZE);
758
759// Valid Signature ?
760if (rom[0] != 0x55 && rom[1] != 0xaa) {
761// PROM next
762// Enable PROM access
763(REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED;
764
765nvRom = (uint8_t*)&regs[NV_PROM_OFFSET];
766bcopy((uint8_t *)nvRom, rom, NVIDIA_ROM_SIZE);
767
768// disable PROM access
769(REG32(NV_PBUS_PCI_NV_20)) = NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;
770
771// Valid Signature ?
772if (rom[0] != 0x55 && rom[1] != 0xaa) {
773// 0xC0000 last
774bcopy((char *)0xc0000, rom, NVIDIA_ROM_SIZE);
775
776// Valid Signature ?
777if (rom[0] != 0x55 && rom[1] != 0xaa) {
778printf("ERROR: Unable to locate nVidia Video BIOS\n");
779return false;
780} else {
781DBG("ROM Address 0x%x Signature 0x%02x%02x\n", nvRom, rom[0], rom[1]);
782}
783} else {
784DBG("PROM Address 0x%x Signature 0x%02x%02x\n", nvRom, rom[0], rom[1]);
785}
786} else {
787DBG("PRAM Address 0x%x Signature 0x%02x%02x\n", nvRom, rom[0], rom[1]);
788}
789}
790
791if ((nvPatch = patch_nvidia_rom(rom)) == PATCH_ROM_FAILED) {
792printf("ERROR: nVidia ROM Patching Failed!\n");
793return false;
794}
795
796rom_pci_header = (struct pci_rom_pci_header_t*)(rom + *(uint16_t *)&rom[24]);
797
798// check for 'PCIR' sig
799if (rom_pci_header->signature == 0x50434952) {
800if (rom_pci_header->device != nvda_dev->device_id) {
801// Get Model from the OpROM
802model = get_nvidia_model((rom_pci_header->vendor << 16) | rom_pci_header->device);
803} else {
804printf("nVidia incorrect PCI ROM signature: 0x%x\n", rom_pci_header->signature);
805}
806}
807
808if (!string) {
809string = devprop_create_string();
810}
811device = devprop_add_device(string, devicepath);
812
813/* FIXME: for primary graphics card only */
814boot_display = 1;
815devprop_add_value(device, "@0,AAPL,boot-display", (uint8_t*)&boot_display, 4);
816
817if(nvPatch == PATCH_ROM_SUCCESS_HAS_LVDS) {
818uint8_t built_in = 0x01;
819devprop_add_value(device, "@0,built-in", &built_in, 1);
820}
821
822// get bios version
823const int MAX_BIOS_VERSION_LENGTH = 32;
824char* version_str = (char*)malloc(MAX_BIOS_VERSION_LENGTH);
825memset(version_str, 0, MAX_BIOS_VERSION_LENGTH);
826int i, version_start;
827int crlf_count = 0;
828// only search the first 384 bytes
829for(i = 0; i < 0x180; i++) {
830if(rom[i] == 0x0D && rom[i+1] == 0x0A) {
831crlf_count++;
832// second 0x0D0A was found, extract bios version
833if(crlf_count == 2) {
834if(rom[i-1] == 0x20) i--; // strip last " "
835for(version_start = i; version_start > (i-MAX_BIOS_VERSION_LENGTH); version_start--) {
836// find start
837if(rom[version_start] == 0x00) {
838version_start++;
839
840// strip "Version "
841if(strncmp((const char*)rom+version_start, "Version ", 8) == 0) {
842version_start += 8;
843}
844
845strncpy(version_str, (const char*)rom+version_start, i-version_start);
846break;
847}
848}
849break;
850}
851}
852}
853
854sprintf(biosVersion, "%s", (nvBiosOveride > 0) ? nvFilename : version_str);
855
856sprintf(kNVCAP, "NVCAP_%04x", nvda_dev->device_id);
857if (getValueForKey(kNVCAP, &value, &len, &bootInfo->bootConfig) && len == NVCAP_LEN * 2) {
858uint8_tnew_NVCAP[NVCAP_LEN];
859
860if (hex2bin(value, new_NVCAP, NVCAP_LEN) == 0) {
861verbose("Using user supplied NVCAP for %s :: %s\n", model, devicepath);
862memcpy(default_NVCAP, new_NVCAP, NVCAP_LEN);
863}
864}
865
866 #if DEBUG_NVCAP
867 printf("NVCAP: %02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x%02x%02x\n",
868default_NVCAP[0], default_NVCAP[1], default_NVCAP[2], default_NVCAP[3],
869default_NVCAP[4], default_NVCAP[5], default_NVCAP[6], default_NVCAP[7],
870default_NVCAP[8], default_NVCAP[9], default_NVCAP[10], default_NVCAP[11],
871default_NVCAP[12], default_NVCAP[13], default_NVCAP[14], default_NVCAP[15],
872default_NVCAP[16], default_NVCAP[17], default_NVCAP[18], default_NVCAP[19]);
873#endif
874
875
876devprop_add_nvidia_template(device);
877devprop_add_value(device, "NVCAP", default_NVCAP, NVCAP_LEN);
878devprop_add_value(device, "VRAM,totalsize", (uint8_t*)&videoRam, 4);
879devprop_add_value(device, "model", (uint8_t*)model, strlen(model) + 1);
880devprop_add_value(device, "rom-revision", (uint8_t*)biosVersion, strlen(biosVersion) + 1);
881if (getBoolForKey(kVBIOS, &doit, &bootInfo->bootConfig) && doit) {
882devprop_add_value(device, "vbios", rom, (nvBiosOveride > 0) ? nvBiosOveride : (rom[2] * 512));
883}
884
885stringdata = malloc(sizeof(uint8_t) * string->length);
886memcpy(stringdata, (uint8_t*)devprop_generate_string(string), string->length);
887stringlength = string->length;
888
889return true;
890}
891

Archive Download this file

Revision: 643