1/*
2	Copyright (c) 2002, Thomas Kurschel
3
4	Part of Radeon kernel driver
5
6	AGP fix. Some motherboard BIOSes enable FastWrite even
7	though the graphics card doesn't support it. Here, we'll
8	fix that (hopefully it is generic enough).
9*/
10
11
12#include "radeon_driver.h"
13
14static void agp_list_info(agp_info ai);
15static void agp_list_active(uint32 cmd);
16
17
18//! fix invalid AGP settings
19void
20Radeon_Set_AGP(device_info *di, bool enable_agp)
21{
22	uint8 agp_index = 0;
23	agp_info nth_agp_info;
24	bool found = false;
25	uint32 agp_cmd;
26
27	/* abort if no agp busmanager found */
28	if (!sAGP) {
29		SHOW_INFO0(1, "Busmanager not installed.\nWarning Card May hang if AGP Fastwrites are Enabled." );
30		return;
31	}
32
33	/* contact driver and get a pointer to the registers and shared data */
34	/* get nth AGP device info */
35	while (sAGP->get_nth_agp_info(agp_index, &nth_agp_info) == B_OK) {
36		/* see if we are this one */
37		if (nth_agp_info.device_id == di->pcii.device_id
38			&& nth_agp_info.vendor_id == di->pcii.vendor_id
39			&& nth_agp_info.bus == di->pcii.bus
40			&& nth_agp_info.device == di->pcii.device
41			&& nth_agp_info.function == di->pcii.function) {
42			SHOW_INFO0(1, "Found AGP capable device" );
43			found = true;
44
45			/* remember our info */
46			di->agpi = nth_agp_info;
47
48			/* log capabilities */
49			agp_list_info(nth_agp_info);
50			break;
51		}
52
53		agp_index++;
54	}
55
56	if (!found) {
57		if (agp_index != 0) {
58			SHOW_INFO0(1, "End of AGP capable devices list.");
59		} else {
60			SHOW_INFO0(1, "No AGP capable devices found.");
61		}
62		return;
63	}
64
65	if (di->settings.force_pci | !enable_agp) {
66		SHOW_INFO0(1, "Disabling AGP mode...");
67
68		/* we want zero agp features enabled */
69		agp_cmd = sAGP->set_agp_mode(0);
70	} else {
71		/* activate AGP mode */
72		SHOW_INFO0(1, "Activating AGP mode...");
73		agp_cmd = 0xfffffff7;
74
75		/* set agp 3 speed bit is agp is v3 */
76		if ((nth_agp_info.interface.status & AGP_3_MODE) != 0)
77			agp_cmd |= AGP_3_MODE;
78
79		/* we want to perma disable fastwrites as they're evil, evil i say */
80		agp_cmd &= ~AGP_FAST_WRITE;
81
82		agp_cmd = sAGP->set_agp_mode(agp_cmd);
83	}
84
85	/* list mode now activated,
86	 * make sure we have the correct speed scheme for logging */
87	agp_list_active(agp_cmd | (nth_agp_info.interface.status & AGP_3_MODE));
88}
89
90
91static void
92agp_list_info(agp_info ai)
93{
94	/*
95		list device
96	*/
97	if (ai.class_base == PCI_display) {
98		SHOW_INFO(4, "Device is a graphics card, subclass ID is $%02x", ai.class_sub);
99	} else {
100		SHOW_INFO(4, "Device is a hostbridge, subclass ID is $%02x", ai.class_sub);
101	}
102
103	SHOW_INFO(4, "Vendor ID $%04x", ai.vendor_id);
104	SHOW_INFO(4, "Device ID $%04x", ai.device_id);
105	SHOW_INFO(4, "Bus %d, device %d, function %d", ai.bus, ai.device, ai.function);
106
107	/*
108		list capabilities
109	*/
110	SHOW_INFO(4,
111		"This device supports AGP specification %" B_PRIu32 ".%" B_PRIu32 ";",
112		((ai.interface.capability_id & AGP_REV_MAJOR) >> AGP_REV_MAJOR_SHIFT),
113		((ai.interface.capability_id & AGP_REV_MINOR) >> AGP_REV_MINOR_SHIFT));
114
115	/* the AGP devices determine AGP speed scheme version used on power-up/reset */
116	if ((ai.interface.status & AGP_3_MODE) == 0) {
117		/* AGP 2.0 scheme applies */
118		if (ai.interface.status & AGP_2_1x)
119			SHOW_INFO0(4, "AGP 2.0 1x mode is available");
120		if (ai.interface.status & AGP_2_2x)
121			SHOW_INFO0(4, "AGP 2.0 2x mode is available");
122		if (ai.interface.status & AGP_2_4x)
123			SHOW_INFO0(41, "AGP 2.0 4x mode is available");
124	} else {
125		/* AGP 3.0 scheme applies */
126		if (ai.interface.status & AGP_3_4x)
127			SHOW_INFO0(4, "AGP 3.0 4x mode is available");
128		if (ai.interface.status & AGP_3_8x)
129			SHOW_INFO0(4, "AGP 3.0 8x mode is available");
130	}
131
132	if (ai.interface.status & AGP_FAST_WRITE)
133		SHOW_INFO0(4, "Fast write transfers are supported");
134	if (ai.interface.status & AGP_SBA)
135		SHOW_INFO0(4, "Sideband adressing is supported");
136
137	SHOW_INFO(1, "%" B_PRIu32 " queued AGP requests can be handled.",
138		((ai.interface.status & AGP_REQUEST) >> AGP_REQUEST_SHIFT) + 1);
139
140	/*
141		list current settings,
142		make sure we have the correct speed scheme for logging
143	 */
144	agp_list_active(ai.interface.command | (ai.interface.status & AGP_3_MODE));
145}
146
147
148static void
149agp_list_active(uint32 cmd)
150{
151	SHOW_INFO0(4, "listing settings now in use:");
152	if ((cmd & AGP_3_MODE) == 0) {
153		/* AGP 2.0 scheme applies */
154		if (cmd & AGP_2_1x)
155			SHOW_INFO0(2,"AGP 2.0 1x mode is set");
156		if (cmd & AGP_2_2x)
157			SHOW_INFO0(2,"AGP 2.0 2x mode is set");
158		if (cmd & AGP_2_4x)
159			SHOW_INFO0(2,"AGP 2.0 4x mode is set");
160	} else {
161		/* AGP 3.0 scheme applies */
162		if (cmd & AGP_3_4x)
163			SHOW_INFO0(2,"AGP 3.0 4x mode is set");
164		if (cmd & AGP_3_8x)
165			SHOW_INFO0(2,"AGP 3.0 8x mode is set");
166	}
167
168	if (cmd & AGP_FAST_WRITE) {
169		SHOW_INFO0(2, "Fast write transfers are enabled");
170	} else {
171		SHOW_INFO0(2, "Fast write transfers are disabled");
172	}
173	if (cmd & AGP_SBA)
174		SHOW_INFO0(4, "Sideband adressing is enabled");
175
176	SHOW_INFO(4, "Max. AGP queued request depth is set to %" B_PRIu32,
177		(((cmd & AGP_REQUEST) >> AGP_REQUEST_SHIFT) + 1));
178
179	if (cmd & AGP_ENABLE)
180		SHOW_INFO0(2, "The AGP interface is enabled.");
181	else
182		SHOW_INFO0(2, "The AGP interface is disabled.");
183}
184