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, "This device supports AGP specification %ld.%ld;",
111		((ai.interface.capability_id & AGP_REV_MAJOR) >> AGP_REV_MAJOR_SHIFT),
112		((ai.interface.capability_id & AGP_REV_MINOR) >> AGP_REV_MINOR_SHIFT));
113
114	/* the AGP devices determine AGP speed scheme version used on power-up/reset */
115	if ((ai.interface.status & AGP_3_MODE) == 0) {
116		/* AGP 2.0 scheme applies */
117		if (ai.interface.status & AGP_2_1x)
118			SHOW_INFO0(4, "AGP 2.0 1x mode is available");
119		if (ai.interface.status & AGP_2_2x)
120			SHOW_INFO0(4, "AGP 2.0 2x mode is available");
121		if (ai.interface.status & AGP_2_4x)
122			SHOW_INFO0(41, "AGP 2.0 4x mode is available");
123	} else {
124		/* AGP 3.0 scheme applies */
125		if (ai.interface.status & AGP_3_4x)
126			SHOW_INFO0(4, "AGP 3.0 4x mode is available");
127		if (ai.interface.status & AGP_3_8x)
128			SHOW_INFO0(4, "AGP 3.0 8x mode is available");
129	}
130
131	if (ai.interface.status & AGP_FAST_WRITE)
132		SHOW_INFO0(4, "Fast write transfers are supported");
133	if (ai.interface.status & AGP_SBA)
134		SHOW_INFO0(4, "Sideband adressing is supported");
135
136	SHOW_INFO(1, "%ld queued AGP requests can be handled.",
137		((ai.interface.status & AGP_REQUEST) >> AGP_REQUEST_SHIFT) + 1);
138
139	/*
140		list current settings,
141		make sure we have the correct speed scheme for logging
142	 */
143	agp_list_active(ai.interface.command | (ai.interface.status & AGP_3_MODE));
144}
145
146
147static void
148agp_list_active(uint32 cmd)
149{
150	SHOW_INFO0(4, "listing settings now in use:");
151	if ((cmd & AGP_3_MODE) == 0) {
152		/* AGP 2.0 scheme applies */
153		if (cmd & AGP_2_1x)
154			SHOW_INFO0(2,"AGP 2.0 1x mode is set");
155		if (cmd & AGP_2_2x)
156			SHOW_INFO0(2,"AGP 2.0 2x mode is set");
157		if (cmd & AGP_2_4x)
158			SHOW_INFO0(2,"AGP 2.0 4x mode is set");
159	} else {
160		/* AGP 3.0 scheme applies */
161		if (cmd & AGP_3_4x)
162			SHOW_INFO0(2,"AGP 3.0 4x mode is set");
163		if (cmd & AGP_3_8x)
164			SHOW_INFO0(2,"AGP 3.0 8x mode is set");
165	}
166
167	if (cmd & AGP_FAST_WRITE) {
168		SHOW_INFO0(2, "Fast write transfers are enabled");
169	} else {
170		SHOW_INFO0(2, "Fast write transfers are disabled");
171	}
172	if (cmd & AGP_SBA)
173		SHOW_INFO0(4, "Sideband adressing is enabled");
174
175	SHOW_INFO(4, "Max. AGP queued request depth is set to %ld",
176		(((cmd & AGP_REQUEST) >> AGP_REQUEST_SHIFT) + 1));
177
178	if (cmd & AGP_ENABLE)
179		SHOW_INFO0(2, "The AGP interface is enabled.");
180	else
181		SHOW_INFO0(2, "The AGP interface is disabled.");
182}
183